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> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 45 | #include "xdr4.h" |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 46 | #include "xdr4cb.h" |
J. Bruce Fields | 0a3adad | 2009-11-04 18:12:35 -0500 | [diff] [blame] | 47 | #include "vfs.h" |
J. Bruce Fields | bfa4b36 | 2012-04-25 16:49:18 -0400 | [diff] [blame] | 48 | #include "current_stateid.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 50 | #include "netns.h" |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 51 | #include "pnfs.h" |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 54 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 55 | #define all_ones {{~0,~0},~0} |
| 56 | static const stateid_t one_stateid = { |
| 57 | .si_generation = ~0, |
| 58 | .si_opaque = all_ones, |
| 59 | }; |
| 60 | static const stateid_t zero_stateid = { |
| 61 | /* all fields zero */ |
| 62 | }; |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 63 | static const stateid_t currentstateid = { |
| 64 | .si_generation = 1, |
| 65 | }; |
Trond Myklebust | fb500a7 | 2017-11-03 08:00:12 -0400 | [diff] [blame] | 66 | static const stateid_t close_stateid = { |
| 67 | .si_generation = 0xffffffffU, |
| 68 | }; |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 69 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 70 | static u64 current_sessionid = 1; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 71 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 72 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
| 73 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 74 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 75 | #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* forward declarations */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 78 | 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] | 79 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 81 | /* Locking: */ |
| 82 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 83 | /* |
| 84 | * Currently used for the del_recall_lru and file hash table. In an |
| 85 | * effort to decrease the scope of the client_mutex, this spinlock may |
| 86 | * eventually cover more: |
| 87 | */ |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 88 | static DEFINE_SPINLOCK(state_lock); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 89 | |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 90 | enum nfsd4_st_mutex_lock_subclass { |
| 91 | OPEN_STATEID_MUTEX = 0, |
| 92 | LOCK_STATEID_MUTEX = 1, |
| 93 | }; |
| 94 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 95 | /* |
| 96 | * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for |
| 97 | * the refcount on the open stateid to drop. |
| 98 | */ |
| 99 | static DECLARE_WAIT_QUEUE_HEAD(close_wq); |
| 100 | |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 101 | static struct kmem_cache *openowner_slab; |
| 102 | static struct kmem_cache *lockowner_slab; |
| 103 | static struct kmem_cache *file_slab; |
| 104 | static struct kmem_cache *stateid_slab; |
| 105 | static struct kmem_cache *deleg_slab; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 106 | static struct kmem_cache *odstate_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 107 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 108 | static void free_session(struct nfsd4_session *); |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 109 | |
Julia Lawall | c4cb897 | 2015-11-21 22:57:39 +0100 | [diff] [blame] | 110 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 111 | static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 112 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 113 | static bool is_session_dead(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 114 | { |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 115 | return ses->se_flags & NFS4_SESSION_DEAD; |
| 116 | } |
| 117 | |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 118 | static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) |
| 119 | { |
| 120 | if (atomic_read(&ses->se_ref) > ref_held_by_me) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 121 | return nfserr_jukebox; |
| 122 | ses->se_flags |= NFS4_SESSION_DEAD; |
| 123 | return nfs_ok; |
| 124 | } |
| 125 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 126 | static bool is_client_expired(struct nfs4_client *clp) |
| 127 | { |
| 128 | return clp->cl_time == 0; |
| 129 | } |
| 130 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 131 | static __be32 get_client_locked(struct nfs4_client *clp) |
| 132 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 133 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 134 | |
| 135 | lockdep_assert_held(&nn->client_lock); |
| 136 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 137 | if (is_client_expired(clp)) |
| 138 | return nfserr_expired; |
| 139 | atomic_inc(&clp->cl_refcount); |
| 140 | return nfs_ok; |
| 141 | } |
| 142 | |
| 143 | /* must be called under the client_lock */ |
| 144 | static inline void |
| 145 | renew_client_locked(struct nfs4_client *clp) |
| 146 | { |
| 147 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 148 | |
| 149 | if (is_client_expired(clp)) { |
| 150 | WARN_ON(1); |
| 151 | printk("%s: client (clientid %08x/%08x) already expired\n", |
| 152 | __func__, |
| 153 | clp->cl_clientid.cl_boot, |
| 154 | clp->cl_clientid.cl_id); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | dprintk("renewing client (clientid %08x/%08x)\n", |
| 159 | clp->cl_clientid.cl_boot, |
| 160 | clp->cl_clientid.cl_id); |
| 161 | list_move_tail(&clp->cl_lru, &nn->client_lru); |
| 162 | clp->cl_time = get_seconds(); |
| 163 | } |
| 164 | |
Fengguang Wu | ba13843 | 2013-04-16 22:14:15 -0400 | [diff] [blame] | 165 | static void put_client_renew_locked(struct nfs4_client *clp) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 166 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 167 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 168 | |
| 169 | lockdep_assert_held(&nn->client_lock); |
| 170 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 171 | if (!atomic_dec_and_test(&clp->cl_refcount)) |
| 172 | return; |
| 173 | if (!is_client_expired(clp)) |
| 174 | renew_client_locked(clp); |
| 175 | } |
| 176 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 177 | static void put_client_renew(struct nfs4_client *clp) |
| 178 | { |
| 179 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 180 | |
Jeff Layton | d6c249b | 2014-07-08 14:02:50 -0400 | [diff] [blame] | 181 | if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock)) |
| 182 | return; |
| 183 | if (!is_client_expired(clp)) |
| 184 | renew_client_locked(clp); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 185 | spin_unlock(&nn->client_lock); |
| 186 | } |
| 187 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 188 | static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) |
| 189 | { |
| 190 | __be32 status; |
| 191 | |
| 192 | if (is_session_dead(ses)) |
| 193 | return nfserr_badsession; |
| 194 | status = get_client_locked(ses->se_client); |
| 195 | if (status) |
| 196 | return status; |
| 197 | atomic_inc(&ses->se_ref); |
| 198 | return nfs_ok; |
| 199 | } |
| 200 | |
| 201 | static void nfsd4_put_session_locked(struct nfsd4_session *ses) |
| 202 | { |
| 203 | struct nfs4_client *clp = ses->se_client; |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 204 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 205 | |
| 206 | lockdep_assert_held(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 207 | |
| 208 | if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) |
| 209 | free_session(ses); |
| 210 | put_client_renew_locked(clp); |
| 211 | } |
| 212 | |
| 213 | static void nfsd4_put_session(struct nfsd4_session *ses) |
| 214 | { |
| 215 | struct nfs4_client *clp = ses->se_client; |
| 216 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 217 | |
| 218 | spin_lock(&nn->client_lock); |
| 219 | nfsd4_put_session_locked(ses); |
| 220 | spin_unlock(&nn->client_lock); |
| 221 | } |
| 222 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 223 | static struct nfsd4_blocked_lock * |
| 224 | find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh, |
| 225 | struct nfsd_net *nn) |
| 226 | { |
| 227 | struct nfsd4_blocked_lock *cur, *found = NULL; |
| 228 | |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 229 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 230 | list_for_each_entry(cur, &lo->lo_blocked, nbl_list) { |
| 231 | if (fh_match(fh, &cur->nbl_fh)) { |
| 232 | list_del_init(&cur->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 233 | list_del_init(&cur->nbl_lru); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 234 | found = cur; |
| 235 | break; |
| 236 | } |
| 237 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 238 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 239 | if (found) |
| 240 | posix_unblock_lock(&found->nbl_lock); |
| 241 | return found; |
| 242 | } |
| 243 | |
| 244 | static struct nfsd4_blocked_lock * |
| 245 | find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh, |
| 246 | struct nfsd_net *nn) |
| 247 | { |
| 248 | struct nfsd4_blocked_lock *nbl; |
| 249 | |
| 250 | nbl = find_blocked_lock(lo, fh, nn); |
| 251 | if (!nbl) { |
| 252 | nbl= kmalloc(sizeof(*nbl), GFP_KERNEL); |
| 253 | if (nbl) { |
| 254 | fh_copy_shallow(&nbl->nbl_fh, fh); |
| 255 | locks_init_lock(&nbl->nbl_lock); |
| 256 | nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client, |
| 257 | &nfsd4_cb_notify_lock_ops, |
| 258 | NFSPROC4_CLNT_CB_NOTIFY_LOCK); |
| 259 | } |
| 260 | } |
| 261 | return nbl; |
| 262 | } |
| 263 | |
| 264 | static void |
| 265 | free_blocked_lock(struct nfsd4_blocked_lock *nbl) |
| 266 | { |
| 267 | locks_release_private(&nbl->nbl_lock); |
| 268 | kfree(nbl); |
| 269 | } |
| 270 | |
| 271 | static int |
| 272 | nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task) |
| 273 | { |
| 274 | /* |
| 275 | * Since this is just an optimization, we don't try very hard if it |
| 276 | * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and |
| 277 | * just quit trying on anything else. |
| 278 | */ |
| 279 | switch (task->tk_status) { |
| 280 | case -NFS4ERR_DELAY: |
| 281 | rpc_delay(task, 1 * HZ); |
| 282 | return 0; |
| 283 | default: |
| 284 | return 1; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static void |
| 289 | nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb) |
| 290 | { |
| 291 | struct nfsd4_blocked_lock *nbl = container_of(cb, |
| 292 | struct nfsd4_blocked_lock, nbl_cb); |
| 293 | |
| 294 | free_blocked_lock(nbl); |
| 295 | } |
| 296 | |
| 297 | static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = { |
| 298 | .done = nfsd4_cb_notify_lock_done, |
| 299 | .release = nfsd4_cb_notify_lock_release, |
| 300 | }; |
| 301 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 302 | static inline struct nfs4_stateowner * |
| 303 | nfs4_get_stateowner(struct nfs4_stateowner *sop) |
| 304 | { |
| 305 | atomic_inc(&sop->so_count); |
| 306 | return sop; |
| 307 | } |
| 308 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 309 | static int |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 310 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 311 | { |
| 312 | return (sop->so_owner.len == owner->len) && |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 313 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static struct nfs4_openowner * |
| 317 | find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 318 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 319 | { |
| 320 | struct nfs4_stateowner *so; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 321 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 322 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 323 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 324 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], |
| 325 | so_strhash) { |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 326 | if (!so->so_is_open_owner) |
| 327 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 328 | if (same_owner_str(so, &open->op_owner)) |
| 329 | return openowner(nfs4_get_stateowner(so)); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 330 | } |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | static struct nfs4_openowner * |
| 335 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 336 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 337 | { |
| 338 | struct nfs4_openowner *oo; |
| 339 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 340 | spin_lock(&clp->cl_lock); |
| 341 | oo = find_openstateowner_str_locked(hashval, open, clp); |
| 342 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 343 | return oo; |
| 344 | } |
| 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | static inline u32 |
| 347 | opaque_hashval(const void *ptr, int nbytes) |
| 348 | { |
| 349 | unsigned char *cptr = (unsigned char *) ptr; |
| 350 | |
| 351 | u32 x = 0; |
| 352 | while (nbytes--) { |
| 353 | x *= 37; |
| 354 | x += *cptr++; |
| 355 | } |
| 356 | return x; |
| 357 | } |
| 358 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 359 | static void nfsd4_free_file_rcu(struct rcu_head *rcu) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 360 | { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 361 | struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); |
| 362 | |
| 363 | kmem_cache_free(file_slab, fp); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 364 | } |
| 365 | |
Christoph Hellwig | e6ba76e | 2014-08-14 08:50:16 +0200 | [diff] [blame] | 366 | void |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 367 | put_nfs4_file(struct nfs4_file *fi) |
| 368 | { |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 369 | might_lock(&state_lock); |
| 370 | |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 371 | if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 372 | hlist_del_rcu(&fi->fi_hash); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 373 | spin_unlock(&state_lock); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 374 | WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 375 | WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); |
| 376 | call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 377 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 380 | static struct file * |
| 381 | __nfs4_get_fd(struct nfs4_file *f, int oflag) |
| 382 | { |
| 383 | if (f->fi_fds[oflag]) |
| 384 | return get_file(f->fi_fds[oflag]); |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | static struct file * |
| 389 | find_writeable_file_locked(struct nfs4_file *f) |
| 390 | { |
| 391 | struct file *ret; |
| 392 | |
| 393 | lockdep_assert_held(&f->fi_lock); |
| 394 | |
| 395 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 396 | if (!ret) |
| 397 | ret = __nfs4_get_fd(f, O_RDWR); |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | static struct file * |
| 402 | find_writeable_file(struct nfs4_file *f) |
| 403 | { |
| 404 | struct file *ret; |
| 405 | |
| 406 | spin_lock(&f->fi_lock); |
| 407 | ret = find_writeable_file_locked(f); |
| 408 | spin_unlock(&f->fi_lock); |
| 409 | |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | static struct file *find_readable_file_locked(struct nfs4_file *f) |
| 414 | { |
| 415 | struct file *ret; |
| 416 | |
| 417 | lockdep_assert_held(&f->fi_lock); |
| 418 | |
| 419 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 420 | if (!ret) |
| 421 | ret = __nfs4_get_fd(f, O_RDWR); |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | static struct file * |
| 426 | find_readable_file(struct nfs4_file *f) |
| 427 | { |
| 428 | struct file *ret; |
| 429 | |
| 430 | spin_lock(&f->fi_lock); |
| 431 | ret = find_readable_file_locked(f); |
| 432 | spin_unlock(&f->fi_lock); |
| 433 | |
| 434 | return ret; |
| 435 | } |
| 436 | |
Christoph Hellwig | 4d227fc | 2014-08-17 07:40:00 -0500 | [diff] [blame] | 437 | struct file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 438 | find_any_file(struct nfs4_file *f) |
| 439 | { |
| 440 | struct file *ret; |
| 441 | |
| 442 | spin_lock(&f->fi_lock); |
| 443 | ret = __nfs4_get_fd(f, O_RDWR); |
| 444 | if (!ret) { |
| 445 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 446 | if (!ret) |
| 447 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 448 | } |
| 449 | spin_unlock(&f->fi_lock); |
| 450 | return ret; |
| 451 | } |
| 452 | |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 453 | static atomic_long_t num_delegations; |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 454 | unsigned long max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 455 | |
| 456 | /* |
| 457 | * Open owner state (share locks) |
| 458 | */ |
| 459 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 460 | /* hash tables for lock and open owners */ |
| 461 | #define OWNER_HASH_BITS 8 |
| 462 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) |
| 463 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 464 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 465 | static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 466 | { |
| 467 | unsigned int ret; |
| 468 | |
| 469 | ret = opaque_hashval(ownername->data, ownername->len); |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 470 | return ret & OWNER_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 471 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 472 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 473 | /* hash table for nfs4_file */ |
| 474 | #define FILE_HASH_BITS 8 |
| 475 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
Shan Wei | 3507958 | 2011-01-14 17:35:59 +0800 | [diff] [blame] | 476 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 477 | static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 478 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 479 | return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0); |
| 480 | } |
| 481 | |
| 482 | static unsigned int file_hashval(struct knfsd_fh *fh) |
| 483 | { |
| 484 | return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1); |
| 485 | } |
| 486 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 487 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 488 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 489 | static void |
| 490 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 491 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 492 | lockdep_assert_held(&fp->fi_lock); |
| 493 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 494 | if (access & NFS4_SHARE_ACCESS_WRITE) |
| 495 | atomic_inc(&fp->fi_access[O_WRONLY]); |
| 496 | if (access & NFS4_SHARE_ACCESS_READ) |
| 497 | atomic_inc(&fp->fi_access[O_RDONLY]); |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 498 | } |
| 499 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 500 | static __be32 |
| 501 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 502 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 503 | lockdep_assert_held(&fp->fi_lock); |
| 504 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 505 | /* Does this access mode make sense? */ |
| 506 | if (access & ~NFS4_SHARE_ACCESS_BOTH) |
| 507 | return nfserr_inval; |
| 508 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 509 | /* Does it conflict with a deny mode already set? */ |
| 510 | if ((access & fp->fi_share_deny) != 0) |
| 511 | return nfserr_share_denied; |
| 512 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 513 | __nfs4_file_get_access(fp, access); |
| 514 | return nfs_ok; |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 515 | } |
| 516 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 517 | static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) |
| 518 | { |
| 519 | /* Common case is that there is no deny mode. */ |
| 520 | if (deny) { |
| 521 | /* Does this deny mode make sense? */ |
| 522 | if (deny & ~NFS4_SHARE_DENY_BOTH) |
| 523 | return nfserr_inval; |
| 524 | |
| 525 | if ((deny & NFS4_SHARE_DENY_READ) && |
| 526 | atomic_read(&fp->fi_access[O_RDONLY])) |
| 527 | return nfserr_share_denied; |
| 528 | |
| 529 | if ((deny & NFS4_SHARE_DENY_WRITE) && |
| 530 | atomic_read(&fp->fi_access[O_WRONLY])) |
| 531 | return nfserr_share_denied; |
| 532 | } |
| 533 | return nfs_ok; |
| 534 | } |
| 535 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 536 | 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] | 537 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 538 | might_lock(&fp->fi_lock); |
| 539 | |
| 540 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { |
| 541 | struct file *f1 = NULL; |
| 542 | struct file *f2 = NULL; |
| 543 | |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 544 | swap(f1, fp->fi_fds[oflag]); |
J. Bruce Fields | 0c7c3e6 | 2013-03-28 20:37:14 -0400 | [diff] [blame] | 545 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 546 | swap(f2, fp->fi_fds[O_RDWR]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 547 | spin_unlock(&fp->fi_lock); |
| 548 | if (f1) |
| 549 | fput(f1); |
| 550 | if (f2) |
| 551 | fput(f2); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 555 | 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] | 556 | { |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 557 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
| 558 | |
| 559 | if (access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 560 | __nfs4_file_put_access(fp, O_WRONLY); |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 561 | if (access & NFS4_SHARE_ACCESS_READ) |
| 562 | __nfs4_file_put_access(fp, O_RDONLY); |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 563 | } |
| 564 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 565 | /* |
| 566 | * Allocate a new open/delegation state counter. This is needed for |
| 567 | * pNFS for proper return on close semantics. |
| 568 | * |
| 569 | * Note that we only allocate it for pNFS-enabled exports, otherwise |
| 570 | * all pointers to struct nfs4_clnt_odstate are always NULL. |
| 571 | */ |
| 572 | static struct nfs4_clnt_odstate * |
| 573 | alloc_clnt_odstate(struct nfs4_client *clp) |
| 574 | { |
| 575 | struct nfs4_clnt_odstate *co; |
| 576 | |
| 577 | co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); |
| 578 | if (co) { |
| 579 | co->co_client = clp; |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 580 | refcount_set(&co->co_odcount, 1); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 581 | } |
| 582 | return co; |
| 583 | } |
| 584 | |
| 585 | static void |
| 586 | hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) |
| 587 | { |
| 588 | struct nfs4_file *fp = co->co_file; |
| 589 | |
| 590 | lockdep_assert_held(&fp->fi_lock); |
| 591 | list_add(&co->co_perfile, &fp->fi_clnt_odstate); |
| 592 | } |
| 593 | |
| 594 | static inline void |
| 595 | get_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 596 | { |
| 597 | if (co) |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 598 | refcount_inc(&co->co_odcount); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | static void |
| 602 | put_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 603 | { |
| 604 | struct nfs4_file *fp; |
| 605 | |
| 606 | if (!co) |
| 607 | return; |
| 608 | |
| 609 | fp = co->co_file; |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 610 | if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 611 | list_del(&co->co_perfile); |
| 612 | spin_unlock(&fp->fi_lock); |
| 613 | |
| 614 | nfsd4_return_all_file_layouts(co->co_client, fp); |
| 615 | kmem_cache_free(odstate_slab, co); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | static struct nfs4_clnt_odstate * |
| 620 | find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) |
| 621 | { |
| 622 | struct nfs4_clnt_odstate *co; |
| 623 | struct nfs4_client *cl; |
| 624 | |
| 625 | if (!new) |
| 626 | return NULL; |
| 627 | |
| 628 | cl = new->co_client; |
| 629 | |
| 630 | spin_lock(&fp->fi_lock); |
| 631 | list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { |
| 632 | if (co->co_client == cl) { |
| 633 | get_clnt_odstate(co); |
| 634 | goto out; |
| 635 | } |
| 636 | } |
| 637 | co = new; |
| 638 | co->co_file = fp; |
| 639 | hash_clnt_odstate_locked(new); |
| 640 | out: |
| 641 | spin_unlock(&fp->fi_lock); |
| 642 | return co; |
| 643 | } |
| 644 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 645 | struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab, |
| 646 | void (*sc_free)(struct nfs4_stid *)) |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 647 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 648 | struct nfs4_stid *stid; |
| 649 | int new_id; |
| 650 | |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 651 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 652 | if (!stid) |
| 653 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 654 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 655 | idr_preload(GFP_KERNEL); |
| 656 | spin_lock(&cl->cl_lock); |
| 657 | new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT); |
| 658 | spin_unlock(&cl->cl_lock); |
| 659 | idr_preload_end(); |
Tejun Heo | ebd6c70 | 2013-03-13 14:59:37 -0700 | [diff] [blame] | 660 | if (new_id < 0) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 661 | goto out_free; |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 662 | |
| 663 | stid->sc_free = sc_free; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 664 | stid->sc_client = cl; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 665 | stid->sc_stateid.si_opaque.so_id = new_id; |
| 666 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; |
| 667 | /* Will be incremented before return to client: */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 668 | refcount_set(&stid->sc_count, 1); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 669 | spin_lock_init(&stid->sc_lock); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 670 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 671 | /* |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 672 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 673 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 674 | * example, a stray write retransmission could be accepted by |
| 675 | * the server when it should have been rejected. Therefore, |
| 676 | * adopt a trick from the sctp code to attempt to maximize the |
| 677 | * amount of time until an id is reused, by ensuring they always |
| 678 | * "increase" (mod INT_MAX): |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 679 | */ |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 680 | return stid; |
| 681 | out_free: |
Wei Yongjun | 2c44a23 | 2013-04-09 14:15:31 +0800 | [diff] [blame] | 682 | kmem_cache_free(slab, stid); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 683 | return NULL; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 684 | } |
| 685 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 686 | 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] | 687 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 688 | struct nfs4_stid *stid; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 689 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 690 | stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 691 | if (!stid) |
| 692 | return NULL; |
| 693 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 694 | return openlockstateid(stid); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | static void nfs4_free_deleg(struct nfs4_stid *stid) |
| 698 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 699 | kmem_cache_free(deleg_slab, stid); |
| 700 | atomic_long_dec(&num_delegations); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 701 | } |
| 702 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 703 | /* |
| 704 | * When we recall a delegation, we should be careful not to hand it |
| 705 | * out again straight away. |
| 706 | * To ensure this we keep a pair of bloom filters ('new' and 'old') |
| 707 | * in which the filehandles of recalled delegations are "stored". |
| 708 | * If a filehandle appear in either filter, a delegation is blocked. |
| 709 | * When a delegation is recalled, the filehandle is stored in the "new" |
| 710 | * filter. |
| 711 | * Every 30 seconds we swap the filters and clear the "new" one, |
| 712 | * unless both are empty of course. |
| 713 | * |
| 714 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the |
| 715 | * low 3 bytes as hash-table indices. |
| 716 | * |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 717 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 718 | * is used to manage concurrent access. Testing does not need the lock |
| 719 | * except when swapping the two filters. |
| 720 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 721 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 722 | static struct bloom_pair { |
| 723 | int entries, old_entries; |
| 724 | time_t swap_time; |
| 725 | int new; /* index into 'set' */ |
| 726 | DECLARE_BITMAP(set[2], 256); |
| 727 | } blocked_delegations; |
| 728 | |
| 729 | static int delegation_blocked(struct knfsd_fh *fh) |
| 730 | { |
| 731 | u32 hash; |
| 732 | struct bloom_pair *bd = &blocked_delegations; |
| 733 | |
| 734 | if (bd->entries == 0) |
| 735 | return 0; |
| 736 | if (seconds_since_boot() - bd->swap_time > 30) { |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 737 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 738 | if (seconds_since_boot() - bd->swap_time > 30) { |
| 739 | bd->entries -= bd->old_entries; |
| 740 | bd->old_entries = bd->entries; |
| 741 | memset(bd->set[bd->new], 0, |
| 742 | sizeof(bd->set[0])); |
| 743 | bd->new = 1-bd->new; |
| 744 | bd->swap_time = seconds_since_boot(); |
| 745 | } |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 746 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 747 | } |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 748 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 749 | if (test_bit(hash&255, bd->set[0]) && |
| 750 | test_bit((hash>>8)&255, bd->set[0]) && |
| 751 | test_bit((hash>>16)&255, bd->set[0])) |
| 752 | return 1; |
| 753 | |
| 754 | if (test_bit(hash&255, bd->set[1]) && |
| 755 | test_bit((hash>>8)&255, bd->set[1]) && |
| 756 | test_bit((hash>>16)&255, bd->set[1])) |
| 757 | return 1; |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static void block_delegations(struct knfsd_fh *fh) |
| 763 | { |
| 764 | u32 hash; |
| 765 | struct bloom_pair *bd = &blocked_delegations; |
| 766 | |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 767 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 768 | |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 769 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 770 | __set_bit(hash&255, bd->set[bd->new]); |
| 771 | __set_bit((hash>>8)&255, bd->set[bd->new]); |
| 772 | __set_bit((hash>>16)&255, bd->set[bd->new]); |
| 773 | if (bd->entries == 0) |
| 774 | bd->swap_time = seconds_since_boot(); |
| 775 | bd->entries += 1; |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 776 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 777 | } |
| 778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | static struct nfs4_delegation * |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 780 | alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh, |
| 781 | struct nfs4_clnt_odstate *odstate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | { |
| 783 | struct nfs4_delegation *dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 784 | long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | |
| 786 | dprintk("NFSD alloc_init_deleg\n"); |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 787 | n = atomic_long_inc_return(&num_delegations); |
| 788 | if (n < 0 || n > max_delegations) |
| 789 | goto out_dec; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 790 | if (delegation_blocked(¤t_fh->fh_handle)) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 791 | goto out_dec; |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 792 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg)); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 793 | if (dp == NULL) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 794 | goto out_dec; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 795 | |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 796 | /* |
| 797 | * delegation seqid's are never incremented. The 4.1 special |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 798 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 799 | * 0 anyway just for consistency and use 1: |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 800 | */ |
| 801 | dp->dl_stid.sc_stateid.si_generation = 1; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 802 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 803 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 805 | dp->dl_clnt_odstate = odstate; |
| 806 | get_clnt_odstate(odstate); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 807 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 808 | dp->dl_retries = 1; |
| 809 | nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 810 | &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | return dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 812 | out_dec: |
| 813 | atomic_long_dec(&num_delegations); |
| 814 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | void |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 818 | nfs4_put_stid(struct nfs4_stid *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 820 | struct nfs4_file *fp = s->sc_file; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 821 | struct nfs4_client *clp = s->sc_client; |
| 822 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 823 | might_lock(&clp->cl_lock); |
| 824 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 825 | if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) { |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 826 | wake_up_all(&close_wq); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 827 | return; |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 828 | } |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 829 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 830 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 831 | s->sc_free(s); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 832 | if (fp) |
| 833 | put_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 836 | void |
| 837 | nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) |
| 838 | { |
| 839 | stateid_t *src = &stid->sc_stateid; |
| 840 | |
| 841 | spin_lock(&stid->sc_lock); |
| 842 | if (unlikely(++src->si_generation == 0)) |
| 843 | src->si_generation = 1; |
| 844 | memcpy(dst, src, sizeof(*dst)); |
| 845 | spin_unlock(&stid->sc_lock); |
| 846 | } |
| 847 | |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 848 | static void nfs4_put_deleg_lease(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | { |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 850 | struct file *filp = NULL; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 851 | |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 852 | spin_lock(&fp->fi_lock); |
Jeff Layton | 67db103 | 2014-12-13 09:11:40 -0500 | [diff] [blame] | 853 | if (fp->fi_deleg_file && --fp->fi_delegees == 0) |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 854 | swap(filp, fp->fi_deleg_file); |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 855 | spin_unlock(&fp->fi_lock); |
| 856 | |
| 857 | if (filp) { |
Christoph Hellwig | 2ab99ee | 2015-01-21 19:14:02 +0100 | [diff] [blame] | 858 | vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp); |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 859 | fput(filp); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 860 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 863 | void nfs4_unhash_stid(struct nfs4_stid *s) |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 864 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 865 | s->sc_type = 0; |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 866 | } |
| 867 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 868 | /** |
| 869 | * nfs4_get_existing_delegation - Discover if this delegation already exists |
| 870 | * @clp: a pointer to the nfs4_client we're granting a delegation to |
| 871 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 872 | * |
| 873 | * Return: |
| 874 | * On success: NULL if an existing delegation was not found. |
| 875 | * |
| 876 | * On error: -EAGAIN if one was previously granted to this nfs4_client |
| 877 | * for this nfs4_file. |
| 878 | * |
| 879 | */ |
| 880 | |
| 881 | static int |
| 882 | nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp) |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 883 | { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 884 | struct nfs4_delegation *searchdp = NULL; |
| 885 | struct nfs4_client *searchclp = NULL; |
| 886 | |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 887 | lockdep_assert_held(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 888 | lockdep_assert_held(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 889 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 890 | list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { |
| 891 | searchclp = searchdp->dl_stid.sc_client; |
| 892 | if (clp == searchclp) { |
| 893 | return -EAGAIN; |
| 894 | } |
| 895 | } |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | /** |
| 900 | * hash_delegation_locked - Add a delegation to the appropriate lists |
| 901 | * @dp: a pointer to the nfs4_delegation we are adding. |
| 902 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 903 | * |
| 904 | * Return: |
| 905 | * On success: NULL if the delegation was successfully hashed. |
| 906 | * |
| 907 | * On error: -EAGAIN if one was previously granted to this |
| 908 | * nfs4_client for this nfs4_file. Delegation is not hashed. |
| 909 | * |
| 910 | */ |
| 911 | |
| 912 | static int |
| 913 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 914 | { |
| 915 | int status; |
| 916 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 917 | |
| 918 | lockdep_assert_held(&state_lock); |
| 919 | lockdep_assert_held(&fp->fi_lock); |
| 920 | |
| 921 | status = nfs4_get_existing_delegation(clp, fp); |
| 922 | if (status) |
| 923 | return status; |
| 924 | ++fp->fi_delegees; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 925 | refcount_inc(&dp->dl_stid.sc_count); |
Benny Halevy | 3fb87d1 | 2014-05-30 09:09:31 -0400 | [diff] [blame] | 926 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 927 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 928 | list_add(&dp->dl_perclnt, &clp->cl_delegations); |
| 929 | return 0; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 930 | } |
| 931 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 932 | static bool |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 933 | unhash_delegation_locked(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 935 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 936 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 937 | lockdep_assert_held(&state_lock); |
| 938 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 939 | if (list_empty(&dp->dl_perfile)) |
| 940 | return false; |
| 941 | |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 942 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
Jeff Layton | d55a166 | 2014-07-22 13:52:06 -0400 | [diff] [blame] | 943 | /* Ensure that deleg break won't try to requeue it */ |
| 944 | ++dp->dl_time; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 945 | spin_lock(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 946 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | list_del_init(&dp->dl_recall_lru); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 948 | list_del_init(&dp->dl_perfile); |
| 949 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 950 | return true; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 951 | } |
| 952 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 953 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 954 | { |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 955 | bool unhashed; |
| 956 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 957 | spin_lock(&state_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 958 | unhashed = unhash_delegation_locked(dp); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 959 | spin_unlock(&state_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 960 | if (unhashed) { |
| 961 | put_clnt_odstate(dp->dl_clnt_odstate); |
| 962 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
| 963 | nfs4_put_stid(&dp->dl_stid); |
| 964 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 968 | { |
| 969 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 970 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 971 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
| 972 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 973 | put_clnt_odstate(dp->dl_clnt_odstate); |
Jeff Layton | afbda40 | 2014-08-09 10:22:41 -0400 | [diff] [blame] | 974 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
| 975 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 976 | if (clp->cl_minorversion == 0) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 977 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 978 | else { |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 979 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 980 | spin_lock(&clp->cl_lock); |
| 981 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 982 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | /* |
| 987 | * SETCLIENTID state |
| 988 | */ |
| 989 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 990 | static unsigned int clientid_hashval(u32 id) |
| 991 | { |
| 992 | return id & CLIENT_HASH_MASK; |
| 993 | } |
| 994 | |
| 995 | static unsigned int clientstr_hashval(const char *name) |
| 996 | { |
| 997 | return opaque_hashval(name, 8) & CLIENT_HASH_MASK; |
| 998 | } |
| 999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | /* |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1001 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 1002 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 1003 | * only what share bits are currently in force, but also what |
| 1004 | * combinations of share bits previous opens have used. This allows us |
| 1005 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 1006 | * return an error if the client attempt to downgrade to a combination |
| 1007 | * of share bits not explicable by closing some of its previous opens. |
| 1008 | * |
| 1009 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 1010 | * track of access/deny bit combinations; so, e.g., we allow: |
| 1011 | * |
| 1012 | * OPEN allow read, deny write |
| 1013 | * OPEN allow both, deny none |
| 1014 | * DOWNGRADE allow read, deny none |
| 1015 | * |
| 1016 | * which we should reject. |
| 1017 | */ |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1018 | static unsigned int |
| 1019 | bmap_to_share_mode(unsigned long bmap) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1020 | int i; |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1021 | unsigned int access = 0; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1022 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1023 | for (i = 1; i < 4; i++) { |
| 1024 | if (test_bit(i, &bmap)) |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1025 | access |= i; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1026 | } |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1027 | return access; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1028 | } |
| 1029 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1030 | /* set share access for a given stateid */ |
| 1031 | static inline void |
| 1032 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1033 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1034 | unsigned char mask = 1 << access; |
| 1035 | |
| 1036 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 1037 | stp->st_access_bmap |= mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /* clear share access for a given stateid */ |
| 1041 | static inline void |
| 1042 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1043 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1044 | unsigned char mask = 1 << access; |
| 1045 | |
| 1046 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 1047 | stp->st_access_bmap &= ~mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | /* test whether a given stateid has access */ |
| 1051 | static inline bool |
| 1052 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1053 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1054 | unsigned char mask = 1 << access; |
| 1055 | |
| 1056 | return (bool)(stp->st_access_bmap & mask); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1057 | } |
| 1058 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1059 | /* set share deny for a given stateid */ |
| 1060 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1061 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1062 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1063 | unsigned char mask = 1 << deny; |
| 1064 | |
| 1065 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 1066 | stp->st_deny_bmap |= mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | /* clear share deny for a given stateid */ |
| 1070 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1071 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1072 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1073 | unsigned char mask = 1 << deny; |
| 1074 | |
| 1075 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 1076 | stp->st_deny_bmap &= ~mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /* test whether a given stateid is denying specific access */ |
| 1080 | static inline bool |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1081 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1082 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1083 | unsigned char mask = 1 << deny; |
| 1084 | |
| 1085 | return (bool)(stp->st_deny_bmap & mask); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | static int nfs4_access_to_omode(u32 access) |
| 1089 | { |
J. Bruce Fields | 8f34a43 | 2010-09-02 15:23:16 -0400 | [diff] [blame] | 1090 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1091 | case NFS4_SHARE_ACCESS_READ: |
| 1092 | return O_RDONLY; |
| 1093 | case NFS4_SHARE_ACCESS_WRITE: |
| 1094 | return O_WRONLY; |
| 1095 | case NFS4_SHARE_ACCESS_BOTH: |
| 1096 | return O_RDWR; |
| 1097 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 1098 | WARN_ON_ONCE(1); |
| 1099 | return O_RDONLY; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1100 | } |
| 1101 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1102 | /* |
| 1103 | * A stateid that had a deny mode associated with it is being released |
| 1104 | * or downgraded. Recalculate the deny mode on the file. |
| 1105 | */ |
| 1106 | static void |
| 1107 | recalculate_deny_mode(struct nfs4_file *fp) |
| 1108 | { |
| 1109 | struct nfs4_ol_stateid *stp; |
| 1110 | |
| 1111 | spin_lock(&fp->fi_lock); |
| 1112 | fp->fi_share_deny = 0; |
| 1113 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) |
| 1114 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); |
| 1115 | spin_unlock(&fp->fi_lock); |
| 1116 | } |
| 1117 | |
| 1118 | static void |
| 1119 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 1120 | { |
| 1121 | int i; |
| 1122 | bool change = false; |
| 1123 | |
| 1124 | for (i = 1; i < 4; i++) { |
| 1125 | if ((i & deny) != i) { |
| 1126 | change = true; |
| 1127 | clear_deny(i, stp); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | /* Recalculate per-file deny mode if there was a change */ |
| 1132 | if (change) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1133 | recalculate_deny_mode(stp->st_stid.sc_file); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1134 | } |
| 1135 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1136 | /* release all access and file references for a given stateid */ |
| 1137 | static void |
| 1138 | release_all_access(struct nfs4_ol_stateid *stp) |
| 1139 | { |
| 1140 | int i; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1141 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1142 | |
| 1143 | if (fp && stp->st_deny_bmap != 0) |
| 1144 | recalculate_deny_mode(fp); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1145 | |
| 1146 | for (i = 1; i < 4; i++) { |
| 1147 | if (test_access(i, stp)) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1148 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1149 | clear_access(i, stp); |
| 1150 | } |
| 1151 | } |
| 1152 | |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1153 | static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) |
| 1154 | { |
| 1155 | kfree(sop->so_owner.data); |
| 1156 | sop->so_ops->so_free(sop); |
| 1157 | } |
| 1158 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1159 | static void nfs4_put_stateowner(struct nfs4_stateowner *sop) |
| 1160 | { |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1161 | struct nfs4_client *clp = sop->so_client; |
| 1162 | |
| 1163 | might_lock(&clp->cl_lock); |
| 1164 | |
| 1165 | if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1166 | return; |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1167 | sop->so_ops->so_unhash(sop); |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1168 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1169 | nfs4_free_stateowner(sop); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1170 | } |
| 1171 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1172 | static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1173 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1174 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1175 | |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1176 | lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); |
| 1177 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1178 | if (list_empty(&stp->st_perfile)) |
| 1179 | return false; |
| 1180 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1181 | spin_lock(&fp->fi_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1182 | list_del_init(&stp->st_perfile); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1183 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1184 | list_del(&stp->st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1185 | return true; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1186 | } |
| 1187 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1188 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1189 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1190 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 1191 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 1192 | put_clnt_odstate(stp->st_clnt_odstate); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1193 | release_all_access(stp); |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1194 | if (stp->st_stateowner) |
| 1195 | nfs4_put_stateowner(stp->st_stateowner); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1196 | kmem_cache_free(stateid_slab, stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1197 | } |
| 1198 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1199 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1200 | { |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1201 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
| 1202 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1203 | struct file *file; |
| 1204 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1205 | file = find_any_file(stp->st_stid.sc_file); |
| 1206 | if (file) |
| 1207 | filp_close(file, (fl_owner_t)lo); |
| 1208 | nfs4_free_ol_stateid(stid); |
| 1209 | } |
| 1210 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1211 | /* |
| 1212 | * Put the persistent reference to an already unhashed generic stateid, while |
| 1213 | * holding the cl_lock. If it's the last reference, then put it onto the |
| 1214 | * reaplist for later destruction. |
| 1215 | */ |
| 1216 | static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, |
| 1217 | struct list_head *reaplist) |
| 1218 | { |
| 1219 | struct nfs4_stid *s = &stp->st_stid; |
| 1220 | struct nfs4_client *clp = s->sc_client; |
| 1221 | |
| 1222 | lockdep_assert_held(&clp->cl_lock); |
| 1223 | |
| 1224 | WARN_ON_ONCE(!list_empty(&stp->st_locks)); |
| 1225 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 1226 | if (!refcount_dec_and_test(&s->sc_count)) { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1227 | wake_up_all(&close_wq); |
| 1228 | return; |
| 1229 | } |
| 1230 | |
| 1231 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
| 1232 | list_add(&stp->st_locks, reaplist); |
| 1233 | } |
| 1234 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1235 | static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1236 | { |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1237 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1238 | |
| 1239 | list_del_init(&stp->st_locks); |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 1240 | nfs4_unhash_stid(&stp->st_stid); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1241 | return unhash_ol_stateid(stp); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1242 | } |
| 1243 | |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 1244 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1245 | { |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1246 | struct nfs4_client *clp = stp->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1247 | bool unhashed; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1248 | |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1249 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1250 | unhashed = unhash_lock_stateid(stp); |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1251 | spin_unlock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1252 | if (unhashed) |
| 1253 | nfs4_put_stid(&stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1254 | } |
| 1255 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1256 | static void unhash_lockowner_locked(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1257 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1258 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1259 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1260 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1261 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1262 | list_del_init(&lo->lo_owner.so_strhash); |
| 1263 | } |
| 1264 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1265 | /* |
| 1266 | * Free a list of generic stateids that were collected earlier after being |
| 1267 | * fully unhashed. |
| 1268 | */ |
| 1269 | static void |
| 1270 | free_ol_stateid_reaplist(struct list_head *reaplist) |
| 1271 | { |
| 1272 | struct nfs4_ol_stateid *stp; |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1273 | struct nfs4_file *fp; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1274 | |
| 1275 | might_sleep(); |
| 1276 | |
| 1277 | while (!list_empty(reaplist)) { |
| 1278 | stp = list_first_entry(reaplist, struct nfs4_ol_stateid, |
| 1279 | st_locks); |
| 1280 | list_del(&stp->st_locks); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1281 | fp = stp->st_stid.sc_file; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1282 | stp->st_stid.sc_free(&stp->st_stid); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1283 | if (fp) |
| 1284 | put_nfs4_file(fp); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1288 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, |
| 1289 | struct list_head *reaplist) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1290 | { |
| 1291 | struct nfs4_ol_stateid *stp; |
| 1292 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1293 | lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); |
| 1294 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1295 | while (!list_empty(&open_stp->st_locks)) { |
| 1296 | stp = list_entry(open_stp->st_locks.next, |
| 1297 | struct nfs4_ol_stateid, st_locks); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1298 | WARN_ON(!unhash_lock_stateid(stp)); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1299 | put_ol_stateid_locked(stp, reaplist); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1303 | static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1304 | struct list_head *reaplist) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1305 | { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1306 | bool unhashed; |
| 1307 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1308 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
| 1309 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1310 | unhashed = unhash_ol_stateid(stp); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1311 | release_open_stateid_locks(stp, reaplist); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1312 | return unhashed; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 1316 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1317 | LIST_HEAD(reaplist); |
| 1318 | |
| 1319 | spin_lock(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1320 | if (unhash_open_stateid(stp, &reaplist)) |
| 1321 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1322 | spin_unlock(&stp->st_stid.sc_client->cl_lock); |
| 1323 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1324 | } |
| 1325 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1326 | static void unhash_openowner_locked(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1327 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1328 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1329 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1330 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1331 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1332 | list_del_init(&oo->oo_owner.so_strhash); |
| 1333 | list_del_init(&oo->oo_perclient); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1334 | } |
| 1335 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1336 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 1337 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1338 | struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, |
| 1339 | nfsd_net_id); |
| 1340 | struct nfs4_ol_stateid *s; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1341 | |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1342 | spin_lock(&nn->client_lock); |
| 1343 | s = oo->oo_last_closed_stid; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1344 | if (s) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1345 | list_del_init(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1346 | oo->oo_last_closed_stid = NULL; |
| 1347 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1348 | spin_unlock(&nn->client_lock); |
| 1349 | if (s) |
| 1350 | nfs4_put_stid(&s->st_stid); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1351 | } |
| 1352 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1353 | static void release_openowner(struct nfs4_openowner *oo) |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1354 | { |
| 1355 | struct nfs4_ol_stateid *stp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1356 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1357 | struct list_head reaplist; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1358 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1359 | INIT_LIST_HEAD(&reaplist); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1360 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1361 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1362 | unhash_openowner_locked(oo); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1363 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 1364 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
| 1365 | struct nfs4_ol_stateid, st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1366 | if (unhash_open_stateid(stp, &reaplist)) |
| 1367 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1368 | } |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1369 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1370 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1371 | release_last_closed_stateid(oo); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1372 | nfs4_put_stateowner(&oo->oo_owner); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1373 | } |
| 1374 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1375 | static inline int |
| 1376 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 1377 | { |
| 1378 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 1379 | |
| 1380 | return sid->sequence % SESSION_HASH_SIZE; |
| 1381 | } |
| 1382 | |
Mark Salter | 135dd00 | 2015-04-06 09:46:00 -0400 | [diff] [blame] | 1383 | #ifdef CONFIG_SUNRPC_DEBUG |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1384 | static inline void |
| 1385 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1386 | { |
| 1387 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 1388 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 1389 | } |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1390 | #else |
| 1391 | static inline void |
| 1392 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1393 | { |
| 1394 | } |
| 1395 | #endif |
| 1396 | |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1397 | /* |
| 1398 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 1399 | * won't be used for replay. |
| 1400 | */ |
| 1401 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 1402 | { |
| 1403 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 1404 | |
| 1405 | if (nfserr == nfserr_replay_me) |
| 1406 | return; |
| 1407 | |
| 1408 | if (!seqid_mutating_err(ntohl(nfserr))) { |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 1409 | nfsd4_cstate_clear_replay(cstate); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1410 | return; |
| 1411 | } |
| 1412 | if (!so) |
| 1413 | return; |
| 1414 | if (so->so_is_open_owner) |
| 1415 | release_last_closed_stateid(openowner(so)); |
| 1416 | so->so_seqid++; |
| 1417 | return; |
| 1418 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1419 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1420 | static void |
| 1421 | gen_sessionid(struct nfsd4_session *ses) |
| 1422 | { |
| 1423 | struct nfs4_client *clp = ses->se_client; |
| 1424 | struct nfsd4_sessionid *sid; |
| 1425 | |
| 1426 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 1427 | sid->clientid = clp->cl_clientid; |
| 1428 | sid->sequence = current_sessionid++; |
| 1429 | sid->reserved = 0; |
| 1430 | } |
| 1431 | |
| 1432 | /* |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1433 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 1434 | * the rpc header, but all we need to cache is the data starting after |
| 1435 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 1436 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 1437 | * value that is the number of bytes in our cache plus a few additional |
| 1438 | * bytes. In order to stay on the safe side, and not promise more than |
| 1439 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 1440 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 1441 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 1442 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1443 | */ |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1444 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 1445 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1446 | static void |
| 1447 | free_session_slots(struct nfsd4_session *ses) |
| 1448 | { |
| 1449 | int i; |
| 1450 | |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 1451 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) { |
| 1452 | free_svc_cred(&ses->se_slots[i]->sl_cred); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1453 | kfree(ses->se_slots[i]); |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 1454 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1455 | } |
| 1456 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1457 | /* |
| 1458 | * We don't actually need to cache the rpc and session headers, so we |
| 1459 | * can allocate a little less for each slot: |
| 1460 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1461 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1462 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1463 | u32 size; |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1464 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1465 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 1466 | size = 0; |
| 1467 | else |
| 1468 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 1469 | return size + sizeof(struct nfsd4_slot); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1470 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1471 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1472 | /* |
| 1473 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 1474 | * re-negotiate active sessions and reduce their slot usage to make |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 1475 | * 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] | 1476 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1477 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1478 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1479 | u32 slotsize = slot_bytes(ca); |
| 1480 | u32 num = ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1481 | int avail; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1482 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1483 | spin_lock(&nfsd_drc_lock); |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 1484 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, |
| 1485 | nfsd_drc_max_mem - nfsd_drc_mem_used); |
J. Bruce Fields | de766e5 | 2017-09-19 19:25:41 -0400 | [diff] [blame] | 1486 | /* |
| 1487 | * Never use more than a third of the remaining memory, |
| 1488 | * unless it's the only way to give this client a slot: |
| 1489 | */ |
| 1490 | avail = clamp_t(int, avail, slotsize, avail/3); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1491 | num = min_t(int, num, avail / slotsize); |
| 1492 | nfsd_drc_mem_used += num * slotsize; |
| 1493 | spin_unlock(&nfsd_drc_lock); |
| 1494 | |
| 1495 | return num; |
| 1496 | } |
| 1497 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1498 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1499 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1500 | int slotsize = slot_bytes(ca); |
| 1501 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1502 | spin_lock(&nfsd_drc_lock); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1503 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1504 | spin_unlock(&nfsd_drc_lock); |
| 1505 | } |
| 1506 | |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1507 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 1508 | struct nfsd4_channel_attrs *battrs) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1509 | { |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1510 | int numslots = fattrs->maxreqs; |
| 1511 | int slotsize = slot_bytes(fattrs); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1512 | struct nfsd4_session *new; |
| 1513 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1514 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 1515 | 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] | 1516 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 1517 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1518 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1519 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1520 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1521 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1522 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1523 | for (i = 0; i < numslots; i++) { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1524 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1525 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1526 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1527 | } |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1528 | |
| 1529 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 1530 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 1531 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1532 | return new; |
| 1533 | out_free: |
| 1534 | while (i--) |
| 1535 | kfree(new->se_slots[i]); |
| 1536 | kfree(new); |
| 1537 | return NULL; |
| 1538 | } |
| 1539 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1540 | static void free_conn(struct nfsd4_conn *c) |
| 1541 | { |
| 1542 | svc_xprt_put(c->cn_xprt); |
| 1543 | kfree(c); |
| 1544 | } |
| 1545 | |
| 1546 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 1547 | { |
| 1548 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 1549 | struct nfs4_client *clp = c->cn_session->se_client; |
| 1550 | |
| 1551 | spin_lock(&clp->cl_lock); |
| 1552 | if (!list_empty(&c->cn_persession)) { |
| 1553 | list_del(&c->cn_persession); |
| 1554 | free_conn(c); |
| 1555 | } |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 1556 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 2e4b723 | 2013-03-08 09:30:43 -0500 | [diff] [blame] | 1557 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1558 | } |
| 1559 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1560 | 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] | 1561 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1562 | struct nfsd4_conn *conn; |
| 1563 | |
| 1564 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 1565 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1566 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1567 | svc_xprt_get(rqstp->rq_xprt); |
| 1568 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1569 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1570 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1571 | return conn; |
| 1572 | } |
| 1573 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1574 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1575 | { |
| 1576 | conn->cn_session = ses; |
| 1577 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1578 | } |
| 1579 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1580 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1581 | { |
| 1582 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1583 | |
| 1584 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1585 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1586 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1587 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1588 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1589 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1590 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1591 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1592 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1593 | } |
| 1594 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1595 | 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] | 1596 | { |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1597 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1598 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1599 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1600 | ret = nfsd4_register_conn(conn); |
| 1601 | if (ret) |
| 1602 | /* oops; xprt is already down: */ |
| 1603 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | 57a3714 | 2014-07-18 15:06:47 -0400 | [diff] [blame] | 1604 | /* We may have gained or lost a callback channel: */ |
| 1605 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1606 | } |
| 1607 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1608 | 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] | 1609 | { |
| 1610 | u32 dir = NFS4_CDFC4_FORE; |
| 1611 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1612 | if (cses->flags & SESSION4_BACK_CHAN) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1613 | dir |= NFS4_CDFC4_BACK; |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1614 | return alloc_conn(rqstp, dir); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1618 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1619 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1620 | struct nfs4_client *clp = s->se_client; |
| 1621 | struct nfsd4_conn *c; |
| 1622 | |
| 1623 | spin_lock(&clp->cl_lock); |
| 1624 | while (!list_empty(&s->se_conns)) { |
| 1625 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1626 | list_del_init(&c->cn_persession); |
| 1627 | spin_unlock(&clp->cl_lock); |
| 1628 | |
| 1629 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1630 | free_conn(c); |
| 1631 | |
| 1632 | spin_lock(&clp->cl_lock); |
| 1633 | } |
| 1634 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1635 | } |
| 1636 | |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1637 | static void __free_session(struct nfsd4_session *ses) |
| 1638 | { |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1639 | free_session_slots(ses); |
| 1640 | kfree(ses); |
| 1641 | } |
| 1642 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1643 | static void free_session(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 1644 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1645 | nfsd4_del_conns(ses); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1646 | nfsd4_put_drc_mem(&ses->se_fchannel); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1647 | __free_session(ses); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1648 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1649 | |
Fengguang Wu | 135ae82 | 2012-11-10 07:20:25 -0500 | [diff] [blame] | 1650 | 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] | 1651 | { |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1652 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1653 | 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] | 1654 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1655 | new->se_client = clp; |
| 1656 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1657 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1658 | INIT_LIST_HEAD(&new->se_conns); |
| 1659 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1660 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1661 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 1662 | new->se_cb_prog = cses->callback_prog; |
J. Bruce Fields | c6bb3ca | 2012-11-01 16:31:02 -0400 | [diff] [blame] | 1663 | new->se_cb_sec = cses->cb_sec; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1664 | atomic_set(&new->se_ref, 0); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1665 | idx = hash_sessionid(&new->se_sessionid); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1666 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1667 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1668 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1669 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1670 | |
Chuck Lever | b0d2e42 | 2014-08-22 15:10:59 -0400 | [diff] [blame] | 1671 | { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1672 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1673 | /* |
| 1674 | * This is a little silly; with sessions there's no real |
| 1675 | * use for the callback address. Use the peer address |
| 1676 | * as a reasonable default for now, but consider fixing |
| 1677 | * the rpc client not to require an address in the |
| 1678 | * future: |
| 1679 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1680 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1681 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1682 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1683 | } |
| 1684 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1685 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1686 | static struct nfsd4_session * |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1687 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1688 | { |
| 1689 | struct nfsd4_session *elem; |
| 1690 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1691 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1692 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1693 | lockdep_assert_held(&nn->client_lock); |
| 1694 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1695 | dump_sessionid(__func__, sessionid); |
| 1696 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1697 | /* Search in the appropriate list */ |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1698 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1699 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1700 | NFS4_MAX_SESSIONID_LEN)) { |
| 1701 | return elem; |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | dprintk("%s: session not found\n", __func__); |
| 1706 | return NULL; |
| 1707 | } |
| 1708 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1709 | static struct nfsd4_session * |
| 1710 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, |
| 1711 | __be32 *ret) |
| 1712 | { |
| 1713 | struct nfsd4_session *session; |
| 1714 | __be32 status = nfserr_badsession; |
| 1715 | |
| 1716 | session = __find_in_sessionid_hashtbl(sessionid, net); |
| 1717 | if (!session) |
| 1718 | goto out; |
| 1719 | status = nfsd4_get_session_locked(session); |
| 1720 | if (status) |
| 1721 | session = NULL; |
| 1722 | out: |
| 1723 | *ret = status; |
| 1724 | return session; |
| 1725 | } |
| 1726 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1727 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1728 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1729 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1730 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1731 | struct nfs4_client *clp = ses->se_client; |
| 1732 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1733 | |
| 1734 | lockdep_assert_held(&nn->client_lock); |
| 1735 | |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1736 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1737 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1738 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1739 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1740 | } |
| 1741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1743 | static int |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1744 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | { |
J. Bruce Fields | bbc7f33 | 2015-01-20 11:51:26 -0500 | [diff] [blame] | 1746 | /* |
| 1747 | * We're assuming the clid was not given out from a boot |
| 1748 | * precisely 2^32 (about 136 years) before this one. That seems |
| 1749 | * a safe assumption: |
| 1750 | */ |
| 1751 | if (clid->cl_boot == (u32)nn->boot_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | return 0; |
Andy Adamson | 60adfc5 | 2009-04-03 08:28:50 +0300 | [diff] [blame] | 1753 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1754 | clid->cl_boot, clid->cl_id, nn->boot_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | return 1; |
| 1756 | } |
| 1757 | |
| 1758 | /* |
| 1759 | * XXX Should we use a slab cache ? |
| 1760 | * This type of memory management is somewhat inefficient, but we use it |
| 1761 | * anyway since SETCLIENTID is not a common operation. |
| 1762 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1763 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | { |
| 1765 | struct nfs4_client *clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1766 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1768 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
| 1769 | if (clp == NULL) |
| 1770 | return NULL; |
Thomas Meyer | 67114fe | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 1771 | clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1772 | if (clp->cl_name.data == NULL) |
| 1773 | goto err_no_name; |
| 1774 | clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) * |
| 1775 | OWNER_HASH_SIZE, GFP_KERNEL); |
| 1776 | if (!clp->cl_ownerstr_hashtbl) |
| 1777 | goto err_no_hashtbl; |
| 1778 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 1779 | INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1780 | clp->cl_name.len = name.len; |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1781 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1782 | idr_init(&clp->cl_stateids); |
| 1783 | atomic_set(&clp->cl_refcount, 0); |
| 1784 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 1785 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1786 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1787 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 1788 | INIT_LIST_HEAD(&clp->cl_lru); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1789 | INIT_LIST_HEAD(&clp->cl_revoked); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 1790 | #ifdef CONFIG_NFSD_PNFS |
| 1791 | INIT_LIST_HEAD(&clp->cl_lo_states); |
| 1792 | #endif |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1793 | spin_lock_init(&clp->cl_lock); |
| 1794 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | return clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1796 | err_no_hashtbl: |
| 1797 | kfree(clp->cl_name.data); |
| 1798 | err_no_name: |
| 1799 | kfree(clp); |
| 1800 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
Trond Myklebust | 4dd86e15 | 2014-04-18 14:43:58 -0400 | [diff] [blame] | 1803 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | free_client(struct nfs4_client *clp) |
| 1805 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1806 | while (!list_empty(&clp->cl_sessions)) { |
| 1807 | struct nfsd4_session *ses; |
| 1808 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 1809 | se_perclnt); |
| 1810 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1811 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 1812 | free_session(ses); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1813 | } |
Trond Myklebust | 4cb57e3 | 2014-04-18 14:43:57 -0400 | [diff] [blame] | 1814 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1815 | free_svc_cred(&clp->cl_cred); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1816 | kfree(clp->cl_ownerstr_hashtbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | kfree(clp->cl_name.data); |
majianpeng | 2d32b29 | 2013-01-29 13:16:06 +0800 | [diff] [blame] | 1818 | idr_destroy(&clp->cl_stateids); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | kfree(clp); |
| 1820 | } |
| 1821 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1822 | /* must be called under the client_lock */ |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1823 | static void |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1824 | unhash_client_locked(struct nfs4_client *clp) |
| 1825 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1826 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1827 | struct nfsd4_session *ses; |
| 1828 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1829 | lockdep_assert_held(&nn->client_lock); |
| 1830 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1831 | /* Mark the client as expired! */ |
| 1832 | clp->cl_time = 0; |
| 1833 | /* Make it invisible */ |
| 1834 | if (!list_empty(&clp->cl_idhash)) { |
| 1835 | list_del_init(&clp->cl_idhash); |
| 1836 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
| 1837 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
| 1838 | else |
| 1839 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
| 1840 | } |
| 1841 | list_del_init(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1842 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1843 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 1844 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1845 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1846 | } |
| 1847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | static void |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1849 | unhash_client(struct nfs4_client *clp) |
| 1850 | { |
| 1851 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1852 | |
| 1853 | spin_lock(&nn->client_lock); |
| 1854 | unhash_client_locked(clp); |
| 1855 | spin_unlock(&nn->client_lock); |
| 1856 | } |
| 1857 | |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame] | 1858 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
| 1859 | { |
| 1860 | if (atomic_read(&clp->cl_refcount)) |
| 1861 | return nfserr_jukebox; |
| 1862 | unhash_client_locked(clp); |
| 1863 | return nfs_ok; |
| 1864 | } |
| 1865 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1866 | static void |
| 1867 | __destroy_client(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1869 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | struct list_head reaplist; |
| 1872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1874 | spin_lock(&state_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1875 | while (!list_empty(&clp->cl_delegations)) { |
| 1876 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1877 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1878 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1880 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | while (!list_empty(&reaplist)) { |
| 1882 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1883 | list_del_init(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 1884 | put_clnt_odstate(dp->dl_clnt_odstate); |
Jeff Layton | afbda40 | 2014-08-09 10:22:41 -0400 | [diff] [blame] | 1885 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1886 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | } |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1888 | while (!list_empty(&clp->cl_revoked)) { |
Andrew Elble | c876486 | 2015-02-25 17:46:27 -0500 | [diff] [blame] | 1889 | 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] | 1890 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1891 | nfs4_put_stid(&dp->dl_stid); |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 1892 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1893 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1894 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 1895 | nfs4_get_stateowner(&oo->oo_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1896 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | } |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 1898 | nfsd4_return_all_client_layouts(clp); |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 1899 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 1900 | if (clp->cl_cb_conn.cb_xprt) |
| 1901 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 1902 | free_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1905 | static void |
| 1906 | destroy_client(struct nfs4_client *clp) |
| 1907 | { |
| 1908 | unhash_client(clp); |
| 1909 | __destroy_client(clp); |
| 1910 | } |
| 1911 | |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1912 | static void expire_client(struct nfs4_client *clp) |
| 1913 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1914 | unhash_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1915 | nfsd4_client_record_remove(clp); |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1916 | __destroy_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1917 | } |
| 1918 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1919 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 1920 | { |
| 1921 | memcpy(target->cl_verifier.data, source->data, |
| 1922 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1925 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 1926 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 1928 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 1929 | } |
| 1930 | |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 1931 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) |
| 1932 | { |
NeilBrown | 2f10fdc | 2017-03-23 16:57:36 +0800 | [diff] [blame] | 1933 | target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL); |
| 1934 | target->cr_raw_principal = kstrdup(source->cr_raw_principal, |
| 1935 | GFP_KERNEL); |
| 1936 | if ((source->cr_principal && ! target->cr_principal) || |
| 1937 | (source->cr_raw_principal && ! target->cr_raw_principal)) |
| 1938 | return -ENOMEM; |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 1939 | |
J. Bruce Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 1940 | target->cr_flavor = source->cr_flavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | target->cr_uid = source->cr_uid; |
| 1942 | target->cr_gid = source->cr_gid; |
| 1943 | target->cr_group_info = source->cr_group_info; |
| 1944 | get_group_info(target->cr_group_info); |
J. Bruce Fields | 0dc1531 | 2013-05-14 16:07:13 -0400 | [diff] [blame] | 1945 | target->cr_gss_mech = source->cr_gss_mech; |
| 1946 | if (source->cr_gss_mech) |
| 1947 | gss_mech_get(source->cr_gss_mech); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1948 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | } |
| 1950 | |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 1951 | static int |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1952 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 1953 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 1954 | if (o1->len < o2->len) |
| 1955 | return -1; |
| 1956 | if (o1->len > o2->len) |
| 1957 | return 1; |
| 1958 | return memcmp(o1->data, o2->data, o1->len); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1959 | } |
| 1960 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1961 | static int same_name(const char *n1, const char *n2) |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1962 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1963 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1967 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 1968 | { |
| 1969 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1973 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 1974 | { |
| 1975 | 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] | 1976 | } |
| 1977 | |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1978 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 1979 | { |
| 1980 | int i; |
| 1981 | |
| 1982 | if (g1->ngroups != g2->ngroups) |
| 1983 | return false; |
| 1984 | for (i=0; i<g1->ngroups; i++) |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 1985 | if (!gid_eq(g1->gid[i], g2->gid[i])) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1986 | return false; |
| 1987 | return true; |
| 1988 | } |
| 1989 | |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1990 | /* |
| 1991 | * RFC 3530 language requires clid_inuse be returned when the |
| 1992 | * "principal" associated with a requests differs from that previously |
| 1993 | * used. We use uid, gid's, and gss principal string as our best |
| 1994 | * approximation. We also don't want to allow non-gss use of a client |
| 1995 | * established using gss: in theory cr_principal should catch that |
| 1996 | * change, but in practice cr_principal can be null even in the gss case |
| 1997 | * since gssd doesn't always pass down a principal string. |
| 1998 | */ |
| 1999 | static bool is_gss_cred(struct svc_cred *cr) |
| 2000 | { |
| 2001 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 2002 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 2003 | } |
| 2004 | |
| 2005 | |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 2006 | static bool |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2007 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 2008 | { |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 2009 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 2010 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 2011 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2012 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 2013 | return false; |
| 2014 | if (cr1->cr_principal == cr2->cr_principal) |
| 2015 | return true; |
| 2016 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 2017 | return false; |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 2018 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2021 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 2022 | { |
| 2023 | struct svc_cred *cr = &rqstp->rq_cred; |
| 2024 | u32 service; |
| 2025 | |
J. Bruce Fields | c472059 | 2013-08-07 11:41:49 -0400 | [diff] [blame] | 2026 | if (!cr->cr_gss_mech) |
| 2027 | return false; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2028 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 2029 | return service == RPC_GSS_SVC_INTEGRITY || |
| 2030 | service == RPC_GSS_SVC_PRIVACY; |
| 2031 | } |
| 2032 | |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2033 | 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] | 2034 | { |
| 2035 | struct svc_cred *cr = &rqstp->rq_cred; |
| 2036 | |
| 2037 | if (!cl->cl_mach_cred) |
| 2038 | return true; |
| 2039 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 2040 | return false; |
| 2041 | if (!svc_rqst_integrity_protected(rqstp)) |
| 2042 | return false; |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 2043 | if (cl->cl_cred.cr_raw_principal) |
| 2044 | return 0 == strcmp(cl->cl_cred.cr_raw_principal, |
| 2045 | cr->cr_raw_principal); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2046 | if (!cr->cr_principal) |
| 2047 | return false; |
| 2048 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 2049 | } |
| 2050 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2051 | 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] | 2052 | { |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 2053 | __be32 verf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | |
Jeff Layton | f419992 | 2014-06-17 07:44:11 -0400 | [diff] [blame] | 2055 | /* |
| 2056 | * This is opaque to client, so no need to byte-swap. Use |
| 2057 | * __force to keep sparse happy |
| 2058 | */ |
| 2059 | verf[0] = (__force __be32)get_seconds(); |
Kinglong Mee | 19311aa8 | 2015-07-18 07:33:31 +0800 | [diff] [blame] | 2060 | verf[1] = (__force __be32)nn->clverifier_counter++; |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 2061 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | } |
| 2063 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2064 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
| 2065 | { |
| 2066 | clp->cl_clientid.cl_boot = nn->boot_time; |
| 2067 | clp->cl_clientid.cl_id = nn->clientid_counter++; |
| 2068 | gen_confirm(clp, nn); |
| 2069 | } |
| 2070 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2071 | static struct nfs4_stid * |
| 2072 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 2073 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 2074 | struct nfs4_stid *ret; |
| 2075 | |
| 2076 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 2077 | if (!ret || !ret->sc_type) |
| 2078 | return NULL; |
| 2079 | return ret; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 2080 | } |
| 2081 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2082 | static struct nfs4_stid * |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2083 | 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] | 2084 | { |
| 2085 | struct nfs4_stid *s; |
| 2086 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2087 | spin_lock(&cl->cl_lock); |
| 2088 | s = find_stateid_locked(cl, t); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 2089 | if (s != NULL) { |
| 2090 | if (typemask & s->sc_type) |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 2091 | refcount_inc(&s->sc_count); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 2092 | else |
| 2093 | s = NULL; |
| 2094 | } |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2095 | spin_unlock(&cl->cl_lock); |
| 2096 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 2097 | } |
| 2098 | |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 2099 | static struct nfs4_client *create_client(struct xdr_netobj name, |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2100 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 2101 | { |
| 2102 | struct nfs4_client *clp; |
| 2103 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2104 | int ret; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2105 | struct net *net = SVC_NET(rqstp); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2106 | |
| 2107 | clp = alloc_client(name); |
| 2108 | if (clp == NULL) |
| 2109 | return NULL; |
| 2110 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2111 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 2112 | if (ret) { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2113 | free_client(clp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2114 | return NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2115 | } |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 2116 | 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] | 2117 | clp->cl_time = get_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2118 | clear_bit(0, &clp->cl_cb_slot_busy); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2119 | copy_verf(clp, verf); |
| 2120 | rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 2121 | clp->cl_cb_session = NULL; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2122 | clp->net = net; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2123 | return clp; |
| 2124 | } |
| 2125 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2126 | static void |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2127 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 2128 | { |
| 2129 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 2130 | struct nfs4_client *clp; |
| 2131 | |
| 2132 | while (*new) { |
| 2133 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 2134 | parent = *new; |
| 2135 | |
| 2136 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 2137 | new = &((*new)->rb_left); |
| 2138 | else |
| 2139 | new = &((*new)->rb_right); |
| 2140 | } |
| 2141 | |
| 2142 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 2143 | rb_insert_color(&new_clp->cl_namenode, root); |
| 2144 | } |
| 2145 | |
| 2146 | static struct nfs4_client * |
| 2147 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 2148 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2149 | int cmp; |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2150 | struct rb_node *node = root->rb_node; |
| 2151 | struct nfs4_client *clp; |
| 2152 | |
| 2153 | while (node) { |
| 2154 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 2155 | cmp = compare_blob(&clp->cl_name, name); |
| 2156 | if (cmp > 0) |
| 2157 | node = node->rb_left; |
| 2158 | else if (cmp < 0) |
| 2159 | node = node->rb_right; |
| 2160 | else |
| 2161 | return clp; |
| 2162 | } |
| 2163 | return NULL; |
| 2164 | } |
| 2165 | |
| 2166 | static void |
| 2167 | add_to_unconfirmed(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | { |
| 2169 | unsigned int idhashval; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2170 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2172 | lockdep_assert_held(&nn->client_lock); |
| 2173 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2174 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2175 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2177 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2178 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2181 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | move_to_confirmed(struct nfs4_client *clp) |
| 2183 | { |
| 2184 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2185 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2187 | lockdep_assert_held(&nn->client_lock); |
| 2188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2190 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2191 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2192 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2193 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2194 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2198 | 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] | 2199 | { |
| 2200 | struct nfs4_client *clp; |
| 2201 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 2202 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2203 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2204 | if (same_clid(&clp->cl_clientid, clid)) { |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 2205 | if ((bool)clp->cl_minorversion != sessions) |
| 2206 | return NULL; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2207 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | return clp; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2209 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | } |
| 2211 | return NULL; |
| 2212 | } |
| 2213 | |
| 2214 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2215 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 2216 | { |
| 2217 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 2218 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2219 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2220 | return find_client_in_id_table(tbl, clid, sessions); |
| 2221 | } |
| 2222 | |
| 2223 | static struct nfs4_client * |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2224 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | { |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2226 | struct list_head *tbl = nn->unconf_id_hashtbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2228 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2229 | return find_client_in_id_table(tbl, clid, sessions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2232 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2233 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2234 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2235 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2236 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2237 | static struct nfs4_client * |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2238 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2239 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2240 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2241 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | static struct nfs4_client * |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2245 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2246 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2247 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2248 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2249 | } |
| 2250 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2251 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2252 | 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] | 2253 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2254 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2255 | struct sockaddr *sa = svc_addr(rqstp); |
| 2256 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2257 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2259 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 2260 | if (se->se_callback_netid_len == 3 && |
| 2261 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 2262 | expected_family = AF_INET; |
| 2263 | else if (se->se_callback_netid_len == 4 && |
| 2264 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 2265 | expected_family = AF_INET6; |
| 2266 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | goto out_err; |
| 2268 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2269 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2270 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2271 | (struct sockaddr *)&conn->cb_addr, |
| 2272 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2273 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2274 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2276 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2277 | if (conn->cb_addr.ss_family == AF_INET6) |
| 2278 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 2279 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2280 | conn->cb_prog = se->se_callback_prog; |
| 2281 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 2282 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | return; |
| 2284 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2285 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 2286 | conn->cb_addrlen = 0; |
Rasmus Villemoes | 4ab495b | 2017-02-24 01:15:55 +0100 | [diff] [blame] | 2287 | dprintk("NFSD: this client (clientid %08x/%08x) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | "will not receive delegations\n", |
| 2289 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 2290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | return; |
| 2292 | } |
| 2293 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2294 | /* |
J. Bruce Fields | 067e1ac | 2014-03-21 17:26:44 -0400 | [diff] [blame] | 2295 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2296 | */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2297 | static void |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2298 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 2299 | { |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2300 | struct xdr_buf *buf = resp->xdr.buf; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2301 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 2302 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2303 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2304 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2305 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2306 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2307 | slot->sl_opcnt = resp->opcnt; |
| 2308 | slot->sl_status = resp->cstate.status; |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 2309 | free_svc_cred(&slot->sl_cred); |
| 2310 | copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred); |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2311 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2312 | if (!nfsd4_cache_this(resp)) { |
| 2313 | slot->sl_flags &= ~NFSD4_SLOT_CACHED; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2314 | return; |
| 2315 | } |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2316 | slot->sl_flags |= NFSD4_SLOT_CACHED; |
| 2317 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2318 | base = resp->cstate.data_offset; |
| 2319 | slot->sl_datalen = buf->len - base; |
| 2320 | 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] | 2321 | WARN(1, "%s: sessions DRC could not cache compound\n", |
| 2322 | __func__); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2323 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2327 | * Encode the replay sequence operation from the slot values. |
| 2328 | * If cachethis is FALSE encode the uncached rep error on the next |
| 2329 | * operation which sets resp->p and increments resp->opcnt for |
| 2330 | * nfs4svc_encode_compoundres. |
| 2331 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2332 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2333 | static __be32 |
| 2334 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 2335 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2336 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2337 | struct nfsd4_op *op; |
| 2338 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2339 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2340 | /* Encode the replayed sequence operation */ |
| 2341 | op = &args->ops[resp->opcnt - 1]; |
| 2342 | nfsd4_encode_operation(resp, op); |
| 2343 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2344 | if (slot->sl_flags & NFSD4_SLOT_CACHED) |
| 2345 | return op->status; |
| 2346 | if (args->opcnt == 1) { |
| 2347 | /* |
| 2348 | * The original operation wasn't a solo sequence--we |
| 2349 | * always cache those--so this retry must not match the |
| 2350 | * original: |
| 2351 | */ |
| 2352 | op->status = nfserr_seq_false_retry; |
| 2353 | } else { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2354 | op = &args->ops[resp->opcnt++]; |
| 2355 | op->status = nfserr_retry_uncached_rep; |
| 2356 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2357 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2358 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2362 | * The sequence operation is not cached because we can use the slot and |
| 2363 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2364 | */ |
J. Bruce Fields | 3ca2eb9 | 2014-03-21 12:04:21 -0400 | [diff] [blame] | 2365 | static __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2366 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 2367 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2368 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2369 | struct nfsd4_slot *slot = resp->cstate.slot; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2370 | struct xdr_stream *xdr = &resp->xdr; |
| 2371 | __be32 *p; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2372 | __be32 status; |
| 2373 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2374 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2375 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2376 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
J. Bruce Fields | 0da7b19 | 2014-03-21 11:43:34 -0400 | [diff] [blame] | 2377 | if (status) |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2378 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2379 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2380 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 2381 | if (!p) { |
| 2382 | WARN_ON_ONCE(1); |
| 2383 | return nfserr_serverfault; |
| 2384 | } |
| 2385 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 2386 | xdr_commit_encode(xdr); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2387 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2388 | resp->opcnt = slot->sl_opcnt; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2389 | return slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2390 | } |
| 2391 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2392 | /* |
| 2393 | * Set the exchange_id flags returned by the server. |
| 2394 | */ |
| 2395 | static void |
| 2396 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 2397 | { |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2398 | #ifdef CONFIG_NFSD_PNFS |
| 2399 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; |
| 2400 | #else |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2401 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2402 | #endif |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2403 | |
| 2404 | /* Referrals are supported, Migration is not. */ |
| 2405 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 2406 | |
| 2407 | /* set the wire flags to return to client. */ |
| 2408 | clid->flags = new->cl_exchange_flags; |
| 2409 | } |
| 2410 | |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 2411 | static bool client_has_openowners(struct nfs4_client *clp) |
| 2412 | { |
| 2413 | struct nfs4_openowner *oo; |
| 2414 | |
| 2415 | list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { |
| 2416 | if (!list_empty(&oo->oo_owner.so_stateids)) |
| 2417 | return true; |
| 2418 | } |
| 2419 | return false; |
| 2420 | } |
| 2421 | |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2422 | static bool client_has_state(struct nfs4_client *clp) |
| 2423 | { |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 2424 | return client_has_openowners(clp) |
Kinglong Mee | 47e970b | 2015-07-21 13:09:17 +0800 | [diff] [blame] | 2425 | #ifdef CONFIG_NFSD_PNFS |
| 2426 | || !list_empty(&clp->cl_lo_states) |
| 2427 | #endif |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 2428 | || !list_empty(&clp->cl_delegations) |
| 2429 | || !list_empty(&clp->cl_sessions); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2430 | } |
| 2431 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2432 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 2433 | nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 2434 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2435 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 2436 | struct nfsd4_exchange_id *exid = &u->exchange_id; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2437 | struct nfs4_client *conf, *new; |
| 2438 | struct nfs4_client *unconf = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2439 | __be32 status; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2440 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2441 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2442 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 2443 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2444 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2445 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2446 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2447 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2448 | "ip_addr=%s flags %x, spa_how %d\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2449 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2450 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2451 | |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 2452 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2453 | return nfserr_inval; |
| 2454 | |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 2455 | new = create_client(exid->clname, rqstp, &verf); |
| 2456 | if (new == NULL) |
| 2457 | return nfserr_jukebox; |
| 2458 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2459 | switch (exid->spa_how) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2460 | case SP4_MACH_CRED: |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 2461 | exid->spo_must_enforce[0] = 0; |
| 2462 | exid->spo_must_enforce[1] = ( |
| 2463 | 1 << (OP_BIND_CONN_TO_SESSION - 32) | |
| 2464 | 1 << (OP_EXCHANGE_ID - 32) | |
| 2465 | 1 << (OP_CREATE_SESSION - 32) | |
| 2466 | 1 << (OP_DESTROY_SESSION - 32) | |
| 2467 | 1 << (OP_DESTROY_CLIENTID - 32)); |
| 2468 | |
| 2469 | exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | |
| 2470 | 1 << (OP_OPEN_DOWNGRADE) | |
| 2471 | 1 << (OP_LOCKU) | |
| 2472 | 1 << (OP_DELEGRETURN)); |
| 2473 | |
| 2474 | exid->spo_must_allow[1] &= ( |
| 2475 | 1 << (OP_TEST_STATEID - 32) | |
| 2476 | 1 << (OP_FREE_STATEID - 32)); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 2477 | if (!svc_rqst_integrity_protected(rqstp)) { |
| 2478 | status = nfserr_inval; |
| 2479 | goto out_nolock; |
| 2480 | } |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 2481 | /* |
| 2482 | * Sometimes userspace doesn't give us a principal. |
| 2483 | * Which is a bug, really. Anyway, we can't enforce |
| 2484 | * MACH_CRED in that case, better to give up now: |
| 2485 | */ |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 2486 | if (!new->cl_cred.cr_principal && |
| 2487 | !new->cl_cred.cr_raw_principal) { |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 2488 | status = nfserr_serverfault; |
| 2489 | goto out_nolock; |
| 2490 | } |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 2491 | new->cl_mach_cred = true; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2492 | case SP4_NONE: |
| 2493 | break; |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 2494 | default: /* checked by xdr code */ |
| 2495 | WARN_ON_ONCE(1); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2496 | case SP4_SSV: |
Kinglong Mee | 8edf4b0 | 2016-02-26 22:36:42 +0800 | [diff] [blame] | 2497 | status = nfserr_encr_alg_unsupp; |
| 2498 | goto out_nolock; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2499 | } |
| 2500 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2501 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2502 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2503 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2504 | if (conf) { |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 2505 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 2506 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 2507 | |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2508 | if (update) { |
| 2509 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2510 | status = nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2511 | goto out; |
| 2512 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2513 | if (!nfsd4_mach_creds_match(conf, rqstp)) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2514 | status = nfserr_wrong_cred; |
| 2515 | goto out; |
| 2516 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2517 | if (!creds_match) { /* case 9 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2518 | status = nfserr_perm; |
| 2519 | goto out; |
| 2520 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2521 | if (!verfs_match) { /* case 8 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2522 | status = nfserr_not_same; |
| 2523 | goto out; |
| 2524 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2525 | /* case 6 */ |
| 2526 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2527 | goto out_copy; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2528 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2529 | if (!creds_match) { /* case 3 */ |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2530 | if (client_has_state(conf)) { |
| 2531 | status = nfserr_clid_inuse; |
| 2532 | goto out; |
| 2533 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2534 | goto out_new; |
| 2535 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2536 | if (verfs_match) { /* case 2 */ |
J. Bruce Fields | 0f1ba0e | 2012-05-25 21:24:40 -0400 | [diff] [blame] | 2537 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2538 | goto out_copy; |
| 2539 | } |
| 2540 | /* case 5, client reboot */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2541 | conf = NULL; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2542 | goto out_new; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 2543 | } |
| 2544 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2545 | if (update) { /* case 7 */ |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 2546 | status = nfserr_noent; |
| 2547 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2548 | } |
| 2549 | |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2550 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2551 | if (unconf) /* case 4, possible retry or client restart */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2552 | unhash_client_locked(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2553 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2554 | /* case 1 (normal case) */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2555 | out_new: |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 2556 | if (conf) { |
| 2557 | status = mark_client_expired_locked(conf); |
| 2558 | if (status) |
| 2559 | goto out; |
| 2560 | } |
J. Bruce Fields | 4f540e2 | 2013-05-07 11:57:52 -0400 | [diff] [blame] | 2561 | new->cl_minorversion = cstate->minorversion; |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 2562 | new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; |
| 2563 | 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] | 2564 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2565 | gen_clid(new, nn); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2566 | add_to_unconfirmed(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2567 | swap(new, conf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2568 | out_copy: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2569 | exid->clientid.cl_boot = conf->cl_clientid.cl_boot; |
| 2570 | exid->clientid.cl_id = conf->cl_clientid.cl_id; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2571 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2572 | exid->seqid = conf->cl_cs_slot.sl_seqid + 1; |
| 2573 | nfsd4_set_ex_flags(conf, exid); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2574 | |
| 2575 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2576 | conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2577 | status = nfs_ok; |
| 2578 | |
| 2579 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2580 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 2581 | out_nolock: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2582 | if (new) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2583 | expire_client(new); |
| 2584 | if (unconf) |
| 2585 | expire_client(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2586 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2587 | } |
| 2588 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2589 | static __be32 |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2590 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2591 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2592 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 2593 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2594 | |
| 2595 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2596 | if (slot_inuse) { |
| 2597 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2598 | return nfserr_jukebox; |
| 2599 | else |
| 2600 | return nfserr_seq_misordered; |
| 2601 | } |
J. Bruce Fields | f6d8248 | 2012-02-13 16:13:41 -0500 | [diff] [blame] | 2602 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2603 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2604 | return nfs_ok; |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2605 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2606 | return nfserr_replay_cache; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2607 | return nfserr_seq_misordered; |
| 2608 | } |
| 2609 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2610 | /* |
| 2611 | * Cache the create session result into the create session single DRC |
| 2612 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 2613 | * Do this for solo or embedded create session operations. |
| 2614 | */ |
| 2615 | static void |
| 2616 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2617 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2618 | { |
| 2619 | slot->sl_status = nfserr; |
| 2620 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 2621 | } |
| 2622 | |
| 2623 | static __be32 |
| 2624 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 2625 | struct nfsd4_clid_slot *slot) |
| 2626 | { |
| 2627 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 2628 | return slot->sl_status; |
| 2629 | } |
| 2630 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2631 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 2632 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 2633 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2634 | 3 + /* version, opcount, opcode */ \ |
| 2635 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2636 | /* seqid, slotID, slotID, cache */ \ |
| 2637 | 4 ) * sizeof(__be32)) |
| 2638 | |
| 2639 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 2640 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 2641 | 1 + /* status */ \ |
| 2642 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2643 | 3 + /* opcount, opcode, opstatus*/ \ |
| 2644 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2645 | /* seqid, slotID, slotID, slotID, status */ \ |
| 2646 | 5 ) * sizeof(__be32)) |
| 2647 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2648 | 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] | 2649 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2650 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 2651 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2652 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 2653 | return nfserr_toosmall; |
| 2654 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 2655 | return nfserr_toosmall; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2656 | ca->headerpadsz = 0; |
| 2657 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 2658 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 2659 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 2660 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 2661 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 2662 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 2663 | /* |
| 2664 | * Note decreasing slot size below client's request may make it |
| 2665 | * difficult for client to function correctly, whereas |
| 2666 | * decreasing the number of slots will (just?) affect |
| 2667 | * performance. When short on memory we therefore prefer to |
| 2668 | * decrease number of slots instead of their size. Clients that |
| 2669 | * request larger slots than they need will get poor results: |
| 2670 | */ |
| 2671 | ca->maxreqs = nfsd4_get_drc_mem(ca); |
| 2672 | if (!ca->maxreqs) |
| 2673 | return nfserr_jukebox; |
| 2674 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2675 | return nfs_ok; |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2676 | } |
| 2677 | |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 2678 | /* |
| 2679 | * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. |
| 2680 | * These are based on similar macros in linux/sunrpc/msg_prot.h . |
| 2681 | */ |
| 2682 | #define RPC_MAX_HEADER_WITH_AUTH_SYS \ |
| 2683 | (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) |
| 2684 | |
| 2685 | #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ |
| 2686 | (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) |
| 2687 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2688 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 2689 | RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2690 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 2691 | RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ |
| 2692 | sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2693 | |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2694 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 2695 | { |
| 2696 | ca->headerpadsz = 0; |
| 2697 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2698 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2699 | return nfserr_toosmall; |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2700 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2701 | return nfserr_toosmall; |
| 2702 | ca->maxresp_cached = 0; |
| 2703 | if (ca->maxops < 2) |
| 2704 | return nfserr_toosmall; |
| 2705 | |
| 2706 | return nfs_ok; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2707 | } |
| 2708 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2709 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 2710 | { |
| 2711 | switch (cbs->flavor) { |
| 2712 | case RPC_AUTH_NULL: |
| 2713 | case RPC_AUTH_UNIX: |
| 2714 | return nfs_ok; |
| 2715 | default: |
| 2716 | /* |
| 2717 | * GSS case: the spec doesn't allow us to return this |
| 2718 | * error. But it also doesn't allow us not to support |
| 2719 | * GSS. |
| 2720 | * I'd rather this fail hard than return some error the |
| 2721 | * client might think it can already handle: |
| 2722 | */ |
| 2723 | return nfserr_encr_alg_unsupp; |
| 2724 | } |
| 2725 | } |
| 2726 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2727 | __be32 |
| 2728 | nfsd4_create_session(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 2729 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2730 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 2731 | struct nfsd4_create_session *cr_ses = &u->create_session; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2732 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2733 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2734 | struct nfs4_client *old = NULL; |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2735 | struct nfsd4_session *new; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2736 | struct nfsd4_conn *conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2737 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2738 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2739 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2740 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2741 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 2742 | return nfserr_inval; |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2743 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 2744 | if (status) |
| 2745 | return status; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2746 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2747 | if (status) |
| 2748 | return status; |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2749 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 2750 | if (status) |
Kinglong Mee | f403e45 | 2013-12-23 17:31:21 +0800 | [diff] [blame] | 2751 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2752 | status = nfserr_jukebox; |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 2753 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2754 | if (!new) |
| 2755 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2756 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 2757 | if (!conn) |
| 2758 | goto out_free_session; |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2759 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2760 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2761 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2762 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2763 | WARN_ON_ONCE(conf && unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2764 | |
| 2765 | if (conf) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2766 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2767 | if (!nfsd4_mach_creds_match(conf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2768 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2769 | cs_slot = &conf->cl_cs_slot; |
| 2770 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Kinglong Mee | f5e22bb | 2015-07-13 17:32:34 +0800 | [diff] [blame] | 2771 | if (status) { |
| 2772 | if (status == nfserr_replay_cache) |
| 2773 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2774 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2775 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2776 | } else if (unconf) { |
| 2777 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2778 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2779 | status = nfserr_clid_inuse; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2780 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2781 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2782 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2783 | if (!nfsd4_mach_creds_match(unconf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2784 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2785 | cs_slot = &unconf->cl_cs_slot; |
| 2786 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 2787 | if (status) { |
| 2788 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2789 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2790 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2791 | } |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2792 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2793 | if (old) { |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2794 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 2795 | if (status) { |
| 2796 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2797 | goto out_free_conn; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 2798 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2799 | } |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2800 | move_to_confirmed(unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2801 | conf = unconf; |
| 2802 | } else { |
| 2803 | status = nfserr_stale_clientid; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2804 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2805 | } |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2806 | status = nfs_ok; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 2807 | /* Persistent sessions are not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 2808 | cr_ses->flags &= ~SESSION4_PERSIST; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 2809 | /* Upshifting from TCP to RDMA is not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 2810 | cr_ses->flags &= ~SESSION4_RDMA; |
| 2811 | |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2812 | init_session(rqstp, new, conf, cr_ses); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2813 | nfsd4_get_session_locked(new); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2814 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2815 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2816 | NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 2817 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2818 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2819 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2820 | /* cache solo and embedded create sessions under the client_lock */ |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2821 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2822 | spin_unlock(&nn->client_lock); |
| 2823 | /* init connection and backchannel */ |
| 2824 | nfsd4_init_conn(rqstp, conn, new); |
| 2825 | nfsd4_put_session(new); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2826 | if (old) |
| 2827 | expire_client(old); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2828 | return status; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2829 | out_free_conn: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2830 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2831 | free_conn(conn); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2832 | if (old) |
| 2833 | expire_client(old); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2834 | out_free_session: |
| 2835 | __free_session(new); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2836 | out_release_drc_mem: |
| 2837 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
J. Bruce Fields | 1ca5079 | 2013-03-14 18:12:03 -0400 | [diff] [blame] | 2838 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2839 | } |
| 2840 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2841 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 2842 | { |
| 2843 | switch (*dir) { |
| 2844 | case NFS4_CDFC4_FORE: |
| 2845 | case NFS4_CDFC4_BACK: |
| 2846 | return nfs_ok; |
| 2847 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 2848 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 2849 | *dir = NFS4_CDFC4_BOTH; |
| 2850 | return nfs_ok; |
| 2851 | }; |
| 2852 | return nfserr_inval; |
| 2853 | } |
| 2854 | |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 2855 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, |
| 2856 | struct nfsd4_compound_state *cstate, |
| 2857 | union nfsd4_op_u *u) |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2858 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 2859 | struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2860 | struct nfsd4_session *session = cstate->session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2861 | 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] | 2862 | __be32 status; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2863 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2864 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 2865 | if (status) |
| 2866 | return status; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2867 | spin_lock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2868 | session->se_cb_prog = bc->bc_cb_program; |
| 2869 | session->se_cb_sec = bc->bc_cb_sec; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2870 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2871 | |
| 2872 | nfsd4_probe_callback(session->se_client); |
| 2873 | |
| 2874 | return nfs_ok; |
| 2875 | } |
| 2876 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2877 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 2878 | struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 2879 | union nfsd4_op_u *u) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2880 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 2881 | 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] | 2882 | __be32 status; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2883 | struct nfsd4_conn *conn; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2884 | struct nfsd4_session *session; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2885 | struct net *net = SVC_NET(rqstp); |
| 2886 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2887 | |
| 2888 | if (!nfsd4_last_compound_op(rqstp)) |
| 2889 | return nfserr_not_only_op; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2890 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2891 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2892 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2893 | if (!session) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2894 | goto out_no_session; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2895 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2896 | if (!nfsd4_mach_creds_match(session->se_client, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2897 | goto out; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2898 | status = nfsd4_map_bcts_dir(&bcts->dir); |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2899 | if (status) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2900 | goto out; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2901 | conn = alloc_conn(rqstp, bcts->dir); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2902 | status = nfserr_jukebox; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2903 | if (!conn) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2904 | goto out; |
| 2905 | nfsd4_init_conn(rqstp, conn, session); |
| 2906 | status = nfs_ok; |
| 2907 | out: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2908 | nfsd4_put_session(session); |
| 2909 | out_no_session: |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2910 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2911 | } |
| 2912 | |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2913 | static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid) |
| 2914 | { |
| 2915 | if (!session) |
| 2916 | return 0; |
| 2917 | return !memcmp(sid, &session->se_sessionid, sizeof(*sid)); |
| 2918 | } |
| 2919 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2920 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 2921 | nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate, |
| 2922 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2923 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 2924 | struct nfsd4_destroy_session *sessionid = &u->destroy_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2925 | struct nfsd4_session *ses; |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2926 | __be32 status; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2927 | int ref_held_by_me = 0; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2928 | struct net *net = SVC_NET(r); |
| 2929 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2930 | |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2931 | status = nfserr_not_only_op; |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2932 | if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2933 | if (!nfsd4_last_compound_op(r)) |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2934 | goto out; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2935 | ref_held_by_me++; |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2936 | } |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2937 | dump_sessionid(__func__, &sessionid->sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2938 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2939 | ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2940 | if (!ses) |
| 2941 | goto out_client_lock; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2942 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2943 | if (!nfsd4_mach_creds_match(ses->se_client, r)) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2944 | goto out_put_session; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2945 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2946 | if (status) |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2947 | goto out_put_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2948 | unhash_session(ses); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2949 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2950 | |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 2951 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 2952 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2953 | spin_lock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2954 | status = nfs_ok; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2955 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2956 | nfsd4_put_session_locked(ses); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2957 | out_client_lock: |
| 2958 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2959 | out: |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2960 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2961 | } |
| 2962 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2963 | 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] | 2964 | { |
| 2965 | struct nfsd4_conn *c; |
| 2966 | |
| 2967 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2968 | if (c->cn_xprt == xpt) { |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2969 | return c; |
| 2970 | } |
| 2971 | } |
| 2972 | return NULL; |
| 2973 | } |
| 2974 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2975 | 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] | 2976 | { |
| 2977 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2978 | struct nfsd4_conn *c; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2979 | __be32 status = nfs_ok; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2980 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2981 | |
| 2982 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2983 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2984 | if (c) |
| 2985 | goto out_free; |
| 2986 | status = nfserr_conn_not_bound_to_session; |
| 2987 | if (clp->cl_mach_cred) |
| 2988 | goto out_free; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2989 | __nfsd4_hash_conn(new, ses); |
| 2990 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2991 | ret = nfsd4_register_conn(new); |
| 2992 | if (ret) |
| 2993 | /* oops; xprt is already down: */ |
| 2994 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2995 | return nfs_ok; |
| 2996 | out_free: |
| 2997 | spin_unlock(&clp->cl_lock); |
| 2998 | free_conn(new); |
| 2999 | return status; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3000 | } |
| 3001 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3002 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 3003 | { |
| 3004 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 3005 | |
| 3006 | return args->opcnt > session->se_fchannel.maxops; |
| 3007 | } |
| 3008 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3009 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 3010 | struct nfsd4_session *session) |
| 3011 | { |
| 3012 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 3013 | |
| 3014 | return xb->len > session->se_fchannel.maxreq_sz; |
| 3015 | } |
| 3016 | |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3017 | static bool replay_matches_cache(struct svc_rqst *rqstp, |
| 3018 | struct nfsd4_sequence *seq, struct nfsd4_slot *slot) |
| 3019 | { |
| 3020 | struct nfsd4_compoundargs *argp = rqstp->rq_argp; |
| 3021 | |
| 3022 | if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) != |
| 3023 | (bool)seq->cachethis) |
| 3024 | return false; |
| 3025 | /* |
| 3026 | * If there's an error than the reply can have fewer ops than |
| 3027 | * the call. But if we cached a reply with *more* ops than the |
| 3028 | * call you're sending us now, then this new call is clearly not |
| 3029 | * really a replay of the old one: |
| 3030 | */ |
| 3031 | if (slot->sl_opcnt < argp->opcnt) |
| 3032 | return false; |
| 3033 | /* This is the only check explicitly called by spec: */ |
| 3034 | if (!same_creds(&rqstp->rq_cred, &slot->sl_cred)) |
| 3035 | return false; |
| 3036 | /* |
| 3037 | * There may be more comparisons we could actually do, but the |
| 3038 | * spec doesn't require us to catch every case where the calls |
| 3039 | * don't match (that would require caching the call as well as |
| 3040 | * the reply), so we don't bother. |
| 3041 | */ |
| 3042 | return true; |
| 3043 | } |
| 3044 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3045 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3046 | nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3047 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3048 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3049 | struct nfsd4_sequence *seq = &u->sequence; |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 3050 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3051 | struct xdr_stream *xdr = &resp->xdr; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3052 | struct nfsd4_session *session; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3053 | struct nfs4_client *clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3054 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3055 | struct nfsd4_conn *conn; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3056 | __be32 status; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3057 | int buflen; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3058 | struct net *net = SVC_NET(rqstp); |
| 3059 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3060 | |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 3061 | if (resp->opcnt != 1) |
| 3062 | return nfserr_sequence_pos; |
| 3063 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3064 | /* |
| 3065 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 3066 | * below. |
| 3067 | */ |
| 3068 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 3069 | if (!conn) |
| 3070 | return nfserr_jukebox; |
| 3071 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3072 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3073 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3074 | if (!session) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3075 | goto out_no_session; |
| 3076 | clp = session->se_client; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3077 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3078 | status = nfserr_too_many_ops; |
| 3079 | if (nfsd4_session_too_many_ops(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3080 | goto out_put_session; |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3081 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3082 | status = nfserr_req_too_big; |
| 3083 | if (nfsd4_request_too_big(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3084 | goto out_put_session; |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3085 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3086 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 3087 | if (seq->slotid >= session->se_fchannel.maxreqs) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3088 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3089 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3090 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3091 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 3092 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 3093 | /* We do not negotiate the number of slots yet, so set the |
| 3094 | * maxslots to the session maxreqs which is used to encode |
| 3095 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 3096 | seq->maxslots = session->se_fchannel.maxreqs; |
| 3097 | |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 3098 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 3099 | slot->sl_flags & NFSD4_SLOT_INUSE); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3100 | if (status == nfserr_replay_cache) { |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3101 | status = nfserr_seq_misordered; |
| 3102 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3103 | goto out_put_session; |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3104 | status = nfserr_seq_false_retry; |
| 3105 | if (!replay_matches_cache(rqstp, seq, slot)) |
| 3106 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3107 | cstate->slot = slot; |
| 3108 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3109 | cstate->clp = clp; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 3110 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3111 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 3112 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 3113 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 3114 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3115 | } |
| 3116 | if (status) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3117 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3118 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3119 | status = nfsd4_sequence_check_conn(conn, session); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3120 | conn = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3121 | if (status) |
| 3122 | goto out_put_session; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3123 | |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3124 | buflen = (seq->cachethis) ? |
| 3125 | session->se_fchannel.maxresp_cached : |
| 3126 | session->se_fchannel.maxresp_sz; |
| 3127 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 3128 | nfserr_rep_too_big; |
J. Bruce Fields | a5cddc8 | 2014-05-12 18:10:58 -0400 | [diff] [blame] | 3129 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3130 | goto out_put_session; |
J. Bruce Fields | 32aaa62 | 2014-03-20 20:47:41 -0400 | [diff] [blame] | 3131 | svc_reserve(rqstp, buflen); |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3132 | |
| 3133 | status = nfs_ok; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3134 | /* Success! bump slot seqid */ |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3135 | slot->sl_seqid = seq->seqid; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3136 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 3137 | if (seq->cachethis) |
| 3138 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3139 | else |
| 3140 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3141 | |
| 3142 | cstate->slot = slot; |
| 3143 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3144 | cstate->clp = clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3145 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3146 | out: |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3147 | switch (clp->cl_cb_state) { |
| 3148 | case NFSD4_CB_DOWN: |
| 3149 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 3150 | break; |
| 3151 | case NFSD4_CB_FAULT: |
| 3152 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 3153 | break; |
| 3154 | default: |
| 3155 | seq->status_flags = 0; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 3156 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3157 | if (!list_empty(&clp->cl_revoked)) |
| 3158 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3159 | out_no_session: |
Kinglong Mee | 3f42d2c | 2014-03-24 11:56:59 +0800 | [diff] [blame] | 3160 | if (conn) |
| 3161 | free_conn(conn); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3162 | spin_unlock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3163 | return status; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3164 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3165 | nfsd4_put_session_locked(session); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3166 | goto out_no_session; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3167 | } |
| 3168 | |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3169 | void |
| 3170 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 3171 | { |
| 3172 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 3173 | |
| 3174 | if (nfsd4_has_session(cs)) { |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3175 | if (cs->status != nfserr_replay_cache) { |
| 3176 | nfsd4_store_cache_entry(resp); |
| 3177 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 3178 | } |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3179 | /* Drop session reference that was taken in nfsd4_sequence() */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3180 | nfsd4_put_session(cs->session); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3181 | } else if (cs->clp) |
| 3182 | put_client_renew(cs->clp); |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3183 | } |
| 3184 | |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3185 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3186 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, |
| 3187 | struct nfsd4_compound_state *cstate, |
| 3188 | union nfsd4_op_u *u) |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3189 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3190 | struct nfsd4_destroy_clientid *dc = &u->destroy_clientid; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3191 | struct nfs4_client *conf, *unconf; |
| 3192 | struct nfs4_client *clp = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3193 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3194 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3195 | |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3196 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3197 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3198 | conf = find_confirmed_client(&dc->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 3199 | WARN_ON_ONCE(conf && unconf); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3200 | |
| 3201 | if (conf) { |
J. Bruce Fields | c0293b0 | 2013-03-14 18:20:01 -0400 | [diff] [blame] | 3202 | if (client_has_state(conf)) { |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3203 | status = nfserr_clientid_busy; |
| 3204 | goto out; |
| 3205 | } |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 3206 | status = mark_client_expired_locked(conf); |
| 3207 | if (status) |
| 3208 | goto out; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3209 | clp = conf; |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3210 | } else if (unconf) |
| 3211 | clp = unconf; |
| 3212 | else { |
| 3213 | status = nfserr_stale_clientid; |
| 3214 | goto out; |
| 3215 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3216 | if (!nfsd4_mach_creds_match(clp, rqstp)) { |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3217 | clp = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3218 | status = nfserr_wrong_cred; |
| 3219 | goto out; |
| 3220 | } |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3221 | unhash_client_locked(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3222 | out: |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3223 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3224 | if (clp) |
| 3225 | expire_client(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3226 | return status; |
| 3227 | } |
| 3228 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3229 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3230 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, |
| 3231 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3232 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3233 | struct nfsd4_reclaim_complete *rc = &u->reclaim_complete; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3234 | __be32 status = 0; |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3235 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3236 | if (rc->rca_one_fs) { |
| 3237 | if (!cstate->current_fh.fh_dentry) |
| 3238 | return nfserr_nofilehandle; |
| 3239 | /* |
| 3240 | * We don't take advantage of the rca_one_fs case. |
| 3241 | * That's OK, it's optional, we can safely ignore it. |
| 3242 | */ |
Christophe JAILLET | d28c442 | 2016-07-02 08:24:32 +0200 | [diff] [blame] | 3243 | return nfs_ok; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3244 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3245 | |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3246 | status = nfserr_complete_already; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 3247 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
| 3248 | &cstate->session->se_client->cl_flags)) |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3249 | goto out; |
| 3250 | |
| 3251 | status = nfserr_stale_clientid; |
| 3252 | if (is_client_expired(cstate->session->se_client)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3253 | /* |
| 3254 | * The following error isn't really legal. |
| 3255 | * But we only get here if the client just explicitly |
| 3256 | * destroyed the client. Surely it no longer cares what |
| 3257 | * error it gets back on an operation for the dead |
| 3258 | * client. |
| 3259 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3260 | goto out; |
| 3261 | |
| 3262 | status = nfs_ok; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 3263 | nfsd4_client_record_create(cstate->session->se_client); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3264 | out: |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3265 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3269 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3270 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3271 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3272 | struct nfsd4_setclientid *setclid = &u->setclientid; |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 3273 | struct xdr_netobj clname = setclid->se_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | nfs4_verifier clverifier = setclid->se_verf; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3275 | struct nfs4_client *conf, *new; |
| 3276 | struct nfs4_client *unconf = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3277 | __be32 status; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3278 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 3279 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3280 | new = create_client(clname, rqstp, &clverifier); |
| 3281 | if (new == NULL) |
| 3282 | return nfserr_jukebox; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3283 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3284 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3285 | conf = find_confirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 3286 | if (conf && client_has_state(conf)) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3287 | /* case 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 3289 | if (clp_used_exchangeid(conf)) |
| 3290 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 3291 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3292 | char addr_str[INET6_ADDRSTRLEN]; |
| 3293 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, |
| 3294 | sizeof(addr_str)); |
| 3295 | dprintk("NFSD: setclientid: string in use by client " |
| 3296 | "at %s\n", addr_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | goto out; |
| 3298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 3300 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 3301 | if (unconf) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3302 | unhash_client_locked(unconf); |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3303 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3304 | /* case 1: probable callback update */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | copy_clid(new, conf); |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3306 | gen_confirm(new, nn); |
| 3307 | } else /* case 4 (new client) or cases 2, 3 (client reboot): */ |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3308 | gen_clid(new, nn); |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 3309 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 3310 | gen_callback(new, setclid, rqstp); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 3311 | add_to_unconfirmed(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 3313 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 3314 | 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] | 3315 | new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | status = nfs_ok; |
| 3317 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3318 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3319 | if (new) |
| 3320 | free_client(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3321 | if (unconf) |
| 3322 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | return status; |
| 3324 | } |
| 3325 | |
| 3326 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3327 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3328 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3329 | struct nfsd4_compound_state *cstate, |
| 3330 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3332 | struct nfsd4_setclientid_confirm *setclientid_confirm = |
| 3333 | &u->setclientid_confirm; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3334 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3335 | struct nfs4_client *old = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3336 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 3337 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3338 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 3339 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 3341 | if (STALE_CLIENTID(clid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | return nfserr_stale_clientid; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3343 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3344 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3345 | conf = find_confirmed_client(clid, false, nn); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3346 | unconf = find_unconfirmed_client(clid, false, nn); |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3347 | /* |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3348 | * We try hard to give out unique clientid's, so if we get an |
| 3349 | * attempt to confirm the same clientid with a different cred, |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 3350 | * the client may be buggy; this should never happen. |
| 3351 | * |
| 3352 | * Nevertheless, RFC 7530 recommends INUSE for this case: |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3353 | */ |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 3354 | status = nfserr_clid_inuse; |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3355 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
| 3356 | goto out; |
| 3357 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
| 3358 | goto out; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3359 | /* cases below refer to rfc 3530 section 14.2.34: */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3360 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
J. Bruce Fields | 7d22fc1 | 2016-09-20 20:55:36 -0400 | [diff] [blame] | 3361 | if (conf && same_verf(&confirm, &conf->cl_confirm)) { |
| 3362 | /* case 2: probable retransmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3363 | status = nfs_ok; |
J. Bruce Fields | 7d22fc1 | 2016-09-20 20:55:36 -0400 | [diff] [blame] | 3364 | } else /* case 4: client hasn't noticed we rebooted yet? */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3365 | status = nfserr_stale_clientid; |
| 3366 | goto out; |
| 3367 | } |
| 3368 | status = nfs_ok; |
| 3369 | if (conf) { /* case 1: callback update */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3370 | old = unconf; |
| 3371 | unhash_client_locked(old); |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3372 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3373 | } else { /* case 3: normal case; new or rebooted client */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3374 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
| 3375 | if (old) { |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 3376 | status = nfserr_clid_inuse; |
| 3377 | if (client_has_state(old) |
| 3378 | && !same_creds(&unconf->cl_cred, |
| 3379 | &old->cl_cred)) |
| 3380 | goto out; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3381 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3382 | if (status) { |
| 3383 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3384 | goto out; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3385 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3386 | } |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3387 | move_to_confirmed(unconf); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3388 | conf = unconf; |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 3389 | } |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3390 | get_client_locked(conf); |
| 3391 | spin_unlock(&nn->client_lock); |
| 3392 | nfsd4_probe_callback(conf); |
| 3393 | spin_lock(&nn->client_lock); |
| 3394 | put_client_renew_locked(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | out: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3396 | spin_unlock(&nn->client_lock); |
| 3397 | if (old) |
| 3398 | expire_client(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | return status; |
| 3400 | } |
| 3401 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3402 | static struct nfs4_file *nfsd4_alloc_file(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | { |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3404 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 3405 | } |
| 3406 | |
| 3407 | /* OPEN Share state helper functions */ |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3408 | static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval, |
| 3409 | struct nfs4_file *fp) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3410 | { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3411 | lockdep_assert_held(&state_lock); |
| 3412 | |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 3413 | refcount_set(&fp->fi_ref, 1); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3414 | spin_lock_init(&fp->fi_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3415 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 3416 | INIT_LIST_HEAD(&fp->fi_delegations); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3417 | INIT_LIST_HEAD(&fp->fi_clnt_odstate); |
Trond Myklebust | e2cf80d | 2014-07-23 16:17:38 -0400 | [diff] [blame] | 3418 | fh_copy_shallow(&fp->fi_fhandle, fh); |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 3419 | fp->fi_deleg_file = NULL; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3420 | fp->fi_had_conflict = false; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3421 | fp->fi_share_deny = 0; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3422 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 3423 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 3424 | #ifdef CONFIG_NFSD_PNFS |
| 3425 | INIT_LIST_HEAD(&fp->fi_lo_states); |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 3426 | atomic_set(&fp->fi_lo_recalls, 0); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 3427 | #endif |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3428 | hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | } |
| 3430 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 3431 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3432 | nfsd4_free_slabs(void) |
| 3433 | { |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3434 | kmem_cache_destroy(odstate_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3435 | kmem_cache_destroy(openowner_slab); |
| 3436 | kmem_cache_destroy(lockowner_slab); |
| 3437 | kmem_cache_destroy(file_slab); |
| 3438 | kmem_cache_destroy(stateid_slab); |
| 3439 | kmem_cache_destroy(deleg_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3441 | |
Bryan Schumaker | 7208339 | 2011-11-01 15:24:59 -0400 | [diff] [blame] | 3442 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | nfsd4_init_slabs(void) |
| 3444 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3445 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 3446 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 3447 | if (openowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3448 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3449 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
Yanchuan Nian | 3c40794 | 2012-10-24 14:44:19 +0800 | [diff] [blame] | 3450 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3451 | if (lockowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3452 | goto out_free_openowner_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3453 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3454 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3455 | if (file_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3456 | goto out_free_lockowner_slab; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 3457 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3458 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 3459 | if (stateid_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3460 | goto out_free_file_slab; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 3461 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3462 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 3463 | if (deleg_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3464 | goto out_free_stateid_slab; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3465 | odstate_slab = kmem_cache_create("nfsd4_odstate", |
| 3466 | sizeof(struct nfs4_clnt_odstate), 0, 0, NULL); |
| 3467 | if (odstate_slab == NULL) |
| 3468 | goto out_free_deleg_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | return 0; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3470 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3471 | out_free_deleg_slab: |
| 3472 | kmem_cache_destroy(deleg_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3473 | out_free_stateid_slab: |
| 3474 | kmem_cache_destroy(stateid_slab); |
| 3475 | out_free_file_slab: |
| 3476 | kmem_cache_destroy(file_slab); |
| 3477 | out_free_lockowner_slab: |
| 3478 | kmem_cache_destroy(lockowner_slab); |
| 3479 | out_free_openowner_slab: |
| 3480 | kmem_cache_destroy(openowner_slab); |
| 3481 | out: |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3482 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 3483 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | } |
| 3485 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3486 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 3487 | { |
| 3488 | rp->rp_status = nfserr_serverfault; |
| 3489 | rp->rp_buflen = 0; |
| 3490 | rp->rp_buf = rp->rp_ibuf; |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 3491 | mutex_init(&rp->rp_mutex); |
| 3492 | } |
| 3493 | |
| 3494 | static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, |
| 3495 | struct nfs4_stateowner *so) |
| 3496 | { |
| 3497 | if (!nfsd4_has_session(cstate)) { |
| 3498 | mutex_lock(&so->so_replay.rp_mutex); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 3499 | cstate->replay_owner = nfs4_get_stateowner(so); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 3500 | } |
| 3501 | } |
| 3502 | |
| 3503 | void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) |
| 3504 | { |
| 3505 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 3506 | |
| 3507 | if (so != NULL) { |
| 3508 | cstate->replay_owner = NULL; |
| 3509 | mutex_unlock(&so->so_replay.rp_mutex); |
| 3510 | nfs4_put_stateowner(so); |
| 3511 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3512 | } |
| 3513 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3514 | 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] | 3515 | { |
| 3516 | struct nfs4_stateowner *sop; |
| 3517 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3518 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3519 | if (!sop) |
| 3520 | return NULL; |
| 3521 | |
| 3522 | sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL); |
| 3523 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3524 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3525 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3527 | sop->so_owner.len = owner->len; |
| 3528 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3529 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3530 | sop->so_client = clp; |
| 3531 | init_nfs4_replay(&sop->so_replay); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3532 | atomic_set(&sop->so_count, 1); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3533 | return sop; |
| 3534 | } |
| 3535 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3536 | 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] | 3537 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3538 | lockdep_assert_held(&clp->cl_lock); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 3539 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3540 | list_add(&oo->oo_owner.so_strhash, |
| 3541 | &clp->cl_ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3542 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | } |
| 3544 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 3545 | static void nfs4_unhash_openowner(struct nfs4_stateowner *so) |
| 3546 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3547 | unhash_openowner_locked(openowner(so)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 3548 | } |
| 3549 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3550 | static void nfs4_free_openowner(struct nfs4_stateowner *so) |
| 3551 | { |
| 3552 | struct nfs4_openowner *oo = openowner(so); |
| 3553 | |
| 3554 | kmem_cache_free(openowner_slab, oo); |
| 3555 | } |
| 3556 | |
| 3557 | static const struct nfs4_stateowner_operations openowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 3558 | .so_unhash = nfs4_unhash_openowner, |
| 3559 | .so_free = nfs4_free_openowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3560 | }; |
| 3561 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3562 | static struct nfs4_ol_stateid * |
| 3563 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 3564 | { |
| 3565 | struct nfs4_ol_stateid *local, *ret = NULL; |
| 3566 | struct nfs4_openowner *oo = open->op_openowner; |
| 3567 | |
| 3568 | lockdep_assert_held(&fp->fi_lock); |
| 3569 | |
| 3570 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
| 3571 | /* ignore lock owners */ |
| 3572 | if (local->st_stateowner->so_is_open_owner == 0) |
| 3573 | continue; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 3574 | if (local->st_stateowner != &oo->oo_owner) |
| 3575 | continue; |
| 3576 | if (local->st_stid.sc_type == NFS4_OPEN_STID) { |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3577 | ret = local; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 3578 | refcount_inc(&ret->st_stid.sc_count); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3579 | break; |
| 3580 | } |
| 3581 | } |
| 3582 | return ret; |
| 3583 | } |
| 3584 | |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 3585 | static __be32 |
| 3586 | nfsd4_verify_open_stid(struct nfs4_stid *s) |
| 3587 | { |
| 3588 | __be32 ret = nfs_ok; |
| 3589 | |
| 3590 | switch (s->sc_type) { |
| 3591 | default: |
| 3592 | break; |
Trond Myklebust | 4f17641 | 2018-01-12 17:42:30 -0500 | [diff] [blame] | 3593 | case 0: |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 3594 | case NFS4_CLOSED_STID: |
| 3595 | case NFS4_CLOSED_DELEG_STID: |
| 3596 | ret = nfserr_bad_stateid; |
| 3597 | break; |
| 3598 | case NFS4_REVOKED_DELEG_STID: |
| 3599 | ret = nfserr_deleg_revoked; |
| 3600 | } |
| 3601 | return ret; |
| 3602 | } |
| 3603 | |
| 3604 | /* Lock the stateid st_mutex, and deal with races with CLOSE */ |
| 3605 | static __be32 |
| 3606 | nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp) |
| 3607 | { |
| 3608 | __be32 ret; |
| 3609 | |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 3610 | mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX); |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 3611 | ret = nfsd4_verify_open_stid(&stp->st_stid); |
| 3612 | if (ret != nfs_ok) |
| 3613 | mutex_unlock(&stp->st_mutex); |
| 3614 | return ret; |
| 3615 | } |
| 3616 | |
| 3617 | static struct nfs4_ol_stateid * |
| 3618 | nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 3619 | { |
| 3620 | struct nfs4_ol_stateid *stp; |
| 3621 | for (;;) { |
| 3622 | spin_lock(&fp->fi_lock); |
| 3623 | stp = nfsd4_find_existing_open(fp, open); |
| 3624 | spin_unlock(&fp->fi_lock); |
| 3625 | if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok) |
| 3626 | break; |
| 3627 | nfs4_put_stid(&stp->st_stid); |
| 3628 | } |
| 3629 | return stp; |
| 3630 | } |
| 3631 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3632 | static struct nfs4_openowner * |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3633 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 3634 | struct nfsd4_compound_state *cstate) |
| 3635 | { |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3636 | struct nfs4_client *clp = cstate->clp; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 3637 | struct nfs4_openowner *oo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3638 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3639 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 3640 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3641 | return NULL; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3642 | oo->oo_owner.so_ops = &openowner_ops; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3643 | oo->oo_owner.so_is_open_owner = 1; |
| 3644 | oo->oo_owner.so_seqid = open->op_seqid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3645 | oo->oo_flags = 0; |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 3646 | if (nfsd4_has_session(cstate)) |
| 3647 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3648 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 3649 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3650 | INIT_LIST_HEAD(&oo->oo_close_lru); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3651 | spin_lock(&clp->cl_lock); |
| 3652 | ret = find_openstateowner_str_locked(strhashval, open, clp); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 3653 | if (ret == NULL) { |
| 3654 | hash_openowner(oo, clp, strhashval); |
| 3655 | ret = oo; |
| 3656 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 3657 | nfs4_free_stateowner(&oo->oo_owner); |
| 3658 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3659 | spin_unlock(&clp->cl_lock); |
Jeff Layton | c595233 | 2015-03-23 10:53:42 -0400 | [diff] [blame] | 3660 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | } |
| 3662 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3663 | static struct nfs4_ol_stateid * |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3664 | init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3665 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3667 | struct nfs4_openowner *oo = open->op_openowner; |
| 3668 | struct nfs4_ol_stateid *retstp = NULL; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3669 | struct nfs4_ol_stateid *stp; |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3670 | |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3671 | stp = open->op_stp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 3672 | /* We are moving these outside of the spinlocks to avoid the warnings */ |
| 3673 | mutex_init(&stp->st_mutex); |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 3674 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 3675 | |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 3676 | retry: |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3677 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
| 3678 | spin_lock(&fp->fi_lock); |
| 3679 | |
| 3680 | retstp = nfsd4_find_existing_open(fp, open); |
| 3681 | if (retstp) |
| 3682 | goto out_unlock; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3683 | |
| 3684 | open->op_stp = NULL; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 3685 | refcount_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 3686 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 3687 | INIT_LIST_HEAD(&stp->st_locks); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 3688 | stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3689 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3690 | stp->st_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | stp->st_access_bmap = 0; |
| 3692 | stp->st_deny_bmap = 0; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 3693 | stp->st_openstp = NULL; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 3694 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3695 | list_add(&stp->st_perfile, &fp->fi_stateids); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3696 | |
| 3697 | out_unlock: |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3698 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 3699 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 3700 | if (retstp) { |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 3701 | /* Handle races with CLOSE */ |
| 3702 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { |
| 3703 | nfs4_put_stid(&retstp->st_stid); |
| 3704 | goto retry; |
| 3705 | } |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3706 | /* To keep mutex tracking happy */ |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 3707 | mutex_unlock(&stp->st_mutex); |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3708 | stp = retstp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 3709 | } |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3710 | return stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | } |
| 3712 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3713 | /* |
| 3714 | * In the 4.0 case we need to keep the owners around a little while to handle |
| 3715 | * CLOSE replay. We still do need to release any file access that is held by |
| 3716 | * them before returning however. |
| 3717 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3718 | static void |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3719 | move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 3721 | struct nfs4_ol_stateid *last; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3722 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 3723 | struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, |
| 3724 | nfsd_net_id); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3725 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3726 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3727 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 3728 | /* |
| 3729 | * We know that we hold one reference via nfsd4_close, and another |
| 3730 | * "persistent" reference for the client. If the refcount is higher |
| 3731 | * than 2, then there are still calls in progress that are using this |
| 3732 | * stateid. We can't put the sc_file reference until they are finished. |
| 3733 | * Wait for the refcount to drop to 2. Since it has been unhashed, |
| 3734 | * there should be no danger of the refcount going back up again at |
| 3735 | * this point. |
| 3736 | */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 3737 | wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2); |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 3738 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3739 | release_all_access(s); |
| 3740 | if (s->st_stid.sc_file) { |
| 3741 | put_nfs4_file(s->st_stid.sc_file); |
| 3742 | s->st_stid.sc_file = NULL; |
| 3743 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 3744 | |
| 3745 | spin_lock(&nn->client_lock); |
| 3746 | last = oo->oo_last_closed_stid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3747 | oo->oo_last_closed_stid = s; |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3748 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3749 | oo->oo_time = get_seconds(); |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 3750 | spin_unlock(&nn->client_lock); |
| 3751 | if (last) |
| 3752 | nfs4_put_stid(&last->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3753 | } |
| 3754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | /* search file_hashtbl[] for file */ |
| 3756 | static struct nfs4_file * |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3757 | find_file_locked(struct knfsd_fh *fh, unsigned int hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3758 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | struct nfs4_file *fp; |
| 3760 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3761 | hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) { |
Christoph Hellwig | 4d94c2e | 2014-08-14 08:41:48 +0200 | [diff] [blame] | 3762 | if (fh_match(&fp->fi_fhandle, fh)) { |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 3763 | if (refcount_inc_not_zero(&fp->fi_ref)) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3764 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3765 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3766 | } |
| 3767 | return NULL; |
| 3768 | } |
| 3769 | |
Christoph Hellwig | e6ba76e | 2014-08-14 08:50:16 +0200 | [diff] [blame] | 3770 | struct nfs4_file * |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3771 | find_file(struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3772 | { |
| 3773 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3774 | unsigned int hashval = file_hashval(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3775 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3776 | rcu_read_lock(); |
| 3777 | fp = find_file_locked(fh, hashval); |
| 3778 | rcu_read_unlock(); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3779 | return fp; |
| 3780 | } |
| 3781 | |
| 3782 | static struct nfs4_file * |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 3783 | find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3784 | { |
| 3785 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3786 | unsigned int hashval = file_hashval(fh); |
| 3787 | |
| 3788 | rcu_read_lock(); |
| 3789 | fp = find_file_locked(fh, hashval); |
| 3790 | rcu_read_unlock(); |
| 3791 | if (fp) |
| 3792 | return fp; |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3793 | |
| 3794 | spin_lock(&state_lock); |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3795 | fp = find_file_locked(fh, hashval); |
| 3796 | if (likely(fp == NULL)) { |
| 3797 | nfsd4_init_file(fh, hashval, new); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3798 | fp = new; |
| 3799 | } |
| 3800 | spin_unlock(&state_lock); |
| 3801 | |
| 3802 | return fp; |
| 3803 | } |
| 3804 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 3805 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3806 | * Called to check deny when READ with all zero stateid or |
| 3807 | * WRITE with all zero or all one stateid |
| 3808 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3809 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3810 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 3811 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | struct nfs4_file *fp; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3813 | __be32 ret = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3815 | fp = find_file(¤t_fh->fh_handle); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3816 | if (!fp) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3817 | return ret; |
| 3818 | /* Check for conflicting share reservations */ |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3819 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3820 | if (fp->fi_share_deny & deny_type) |
| 3821 | ret = nfserr_locked; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3822 | spin_unlock(&fp->fi_lock); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3823 | put_nfs4_file(fp); |
| 3824 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | } |
| 3826 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3827 | static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3829 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3830 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
| 3831 | nfsd_net_id); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 3832 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3833 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 3834 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3835 | /* |
| 3836 | * 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] | 3837 | * already holding inode->i_lock. |
| 3838 | * |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3839 | * If the dl_time != 0, then we know that it has already been |
| 3840 | * queued for a lease break. Don't queue it again. |
| 3841 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 3842 | spin_lock(&state_lock); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3843 | if (dp->dl_time == 0) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3844 | dp->dl_time = get_seconds(); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3845 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3846 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3847 | spin_unlock(&state_lock); |
| 3848 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3849 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3850 | static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, |
| 3851 | struct rpc_task *task) |
| 3852 | { |
| 3853 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 3854 | |
Andrew Elble | a457974 | 2015-08-31 12:06:41 -0400 | [diff] [blame] | 3855 | if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID) |
| 3856 | return 1; |
| 3857 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3858 | switch (task->tk_status) { |
| 3859 | case 0: |
| 3860 | return 1; |
| 3861 | case -EBADHANDLE: |
| 3862 | case -NFS4ERR_BAD_STATEID: |
| 3863 | /* |
| 3864 | * Race: client probably got cb_recall before open reply |
| 3865 | * granting delegation. |
| 3866 | */ |
| 3867 | if (dp->dl_retries--) { |
| 3868 | rpc_delay(task, 2 * HZ); |
| 3869 | return 0; |
| 3870 | } |
| 3871 | /*FALLTHRU*/ |
| 3872 | default: |
| 3873 | return -1; |
| 3874 | } |
| 3875 | } |
| 3876 | |
| 3877 | static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) |
| 3878 | { |
| 3879 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 3880 | |
| 3881 | nfs4_put_stid(&dp->dl_stid); |
| 3882 | } |
| 3883 | |
Julia Lawall | c4cb897 | 2015-11-21 22:57:39 +0100 | [diff] [blame] | 3884 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3885 | .prepare = nfsd4_cb_recall_prepare, |
| 3886 | .done = nfsd4_cb_recall_done, |
| 3887 | .release = nfsd4_cb_recall_release, |
| 3888 | }; |
| 3889 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3890 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
| 3891 | { |
| 3892 | /* |
| 3893 | * We're assuming the state code never drops its reference |
| 3894 | * without first removing the lease. Since we're in this lease |
| 3895 | * callback (and since the lease code is serialized by the kernel |
| 3896 | * lock) we know the server hasn't removed the lease yet, we know |
| 3897 | * it's safe to take a reference. |
| 3898 | */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 3899 | refcount_inc(&dp->dl_stid.sc_count); |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 3900 | nfsd4_run_cb(&dp->dl_recall); |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3901 | } |
| 3902 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 3903 | /* Called from break_lease() with i_lock held. */ |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3904 | static bool |
| 3905 | nfsd_break_deleg_cb(struct file_lock *fl) |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3906 | { |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3907 | bool ret = false; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3908 | struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; |
| 3909 | struct nfs4_delegation *dp; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3910 | |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 3911 | if (!fp) { |
| 3912 | WARN(1, "(%p)->fl_owner NULL\n", fl); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3913 | return ret; |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 3914 | } |
| 3915 | if (fp->fi_had_conflict) { |
| 3916 | WARN(1, "duplicate break on %p\n", fp); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3917 | return ret; |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 3918 | } |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3919 | /* |
| 3920 | * 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] | 3921 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3922 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3923 | */ |
| 3924 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3925 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3926 | spin_lock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3927 | fp->fi_had_conflict = true; |
| 3928 | /* |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3929 | * If there are no delegations on the list, then return true |
| 3930 | * so that the lease code will go ahead and delete it. |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3931 | */ |
| 3932 | if (list_empty(&fp->fi_delegations)) |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3933 | ret = true; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3934 | else |
| 3935 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) |
| 3936 | nfsd_break_one_deleg(dp); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3937 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3938 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3939 | } |
| 3940 | |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 3941 | static int |
Jeff Layton | 7448cc3 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 3942 | nfsd_change_deleg_cb(struct file_lock *onlist, int arg, |
| 3943 | struct list_head *dispose) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | { |
| 3945 | if (arg & F_UNLCK) |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 3946 | return lease_modify(onlist, arg, dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3947 | else |
| 3948 | return -EAGAIN; |
| 3949 | } |
| 3950 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 3951 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 3952 | .lm_break = nfsd_break_deleg_cb, |
| 3953 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3954 | }; |
| 3955 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 3956 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 3957 | { |
| 3958 | if (nfsd4_has_session(cstate)) |
| 3959 | return nfs_ok; |
| 3960 | if (seqid == so->so_seqid - 1) |
| 3961 | return nfserr_replay_me; |
| 3962 | if (seqid == so->so_seqid) |
| 3963 | return nfs_ok; |
| 3964 | return nfserr_bad_seqid; |
| 3965 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3967 | static __be32 lookup_clientid(clientid_t *clid, |
| 3968 | struct nfsd4_compound_state *cstate, |
| 3969 | struct nfsd_net *nn) |
| 3970 | { |
| 3971 | struct nfs4_client *found; |
| 3972 | |
| 3973 | if (cstate->clp) { |
| 3974 | found = cstate->clp; |
| 3975 | if (!same_clid(&found->cl_clientid, clid)) |
| 3976 | return nfserr_stale_clientid; |
| 3977 | return nfs_ok; |
| 3978 | } |
| 3979 | |
| 3980 | if (STALE_CLIENTID(clid, nn)) |
| 3981 | return nfserr_stale_clientid; |
| 3982 | |
| 3983 | /* |
| 3984 | * For v4.1+ we get the client in the SEQUENCE op. If we don't have one |
| 3985 | * cached already then we know this is for is for v4.0 and "sessions" |
| 3986 | * will be false. |
| 3987 | */ |
| 3988 | WARN_ON_ONCE(cstate->session); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 3989 | spin_lock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3990 | found = find_confirmed_client(clid, false, nn); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 3991 | if (!found) { |
| 3992 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3993 | return nfserr_expired; |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 3994 | } |
| 3995 | atomic_inc(&found->cl_refcount); |
| 3996 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3997 | |
| 3998 | /* Cache the nfs4_client in cstate! */ |
| 3999 | cstate->clp = found; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4000 | return nfs_ok; |
| 4001 | } |
| 4002 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4003 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4004 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4005 | struct nfsd4_open *open, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4006 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | clientid_t *clientid = &open->op_clientid; |
| 4008 | struct nfs4_client *clp = NULL; |
| 4009 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4010 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4011 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 4013 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | return nfserr_stale_clientid; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4015 | /* |
| 4016 | * In case we need it later, after we've already created the |
| 4017 | * file and don't want to risk a further failure: |
| 4018 | */ |
| 4019 | open->op_file = nfsd4_alloc_file(); |
| 4020 | if (open->op_file == NULL) |
| 4021 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4022 | |
Trond Myklebust | 2d91e89 | 2014-06-30 11:48:46 -0400 | [diff] [blame] | 4023 | status = lookup_clientid(clientid, cstate, nn); |
| 4024 | if (status) |
| 4025 | return status; |
| 4026 | clp = cstate->clp; |
| 4027 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4028 | strhashval = ownerstr_hashval(&open->op_owner); |
| 4029 | oo = find_openstateowner_str(strhashval, open, clp); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4030 | open->op_openowner = oo; |
| 4031 | if (!oo) { |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4032 | goto new_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4033 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4034 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4035 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4036 | release_openowner(oo); |
| 4037 | open->op_openowner = NULL; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4038 | goto new_owner; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4039 | } |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4040 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 4041 | if (status) |
| 4042 | return status; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4043 | goto alloc_stateid; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4044 | new_owner: |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4045 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4046 | if (oo == NULL) |
| 4047 | return nfserr_jukebox; |
| 4048 | open->op_openowner = oo; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4049 | alloc_stateid: |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 4050 | open->op_stp = nfs4_alloc_open_stateid(clp); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4051 | if (!open->op_stp) |
| 4052 | return nfserr_jukebox; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4053 | |
| 4054 | if (nfsd4_has_session(cstate) && |
| 4055 | (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { |
| 4056 | open->op_odstate = alloc_clnt_odstate(clp); |
| 4057 | if (!open->op_odstate) |
| 4058 | return nfserr_jukebox; |
| 4059 | } |
| 4060 | |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4061 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | } |
| 4063 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4064 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 4065 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 4066 | { |
| 4067 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 4068 | return nfserr_openmode; |
| 4069 | else |
| 4070 | return nfs_ok; |
| 4071 | } |
| 4072 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 4073 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4074 | { |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4075 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 4076 | } |
| 4077 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4078 | 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] | 4079 | { |
| 4080 | struct nfs4_stid *ret; |
| 4081 | |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 4082 | ret = find_stateid_by_type(cl, s, |
| 4083 | NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 4084 | if (!ret) |
| 4085 | return NULL; |
| 4086 | return delegstateid(ret); |
| 4087 | } |
| 4088 | |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4089 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 4090 | { |
| 4091 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 4092 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 4093 | } |
| 4094 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4095 | static __be32 |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 4096 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4097 | struct nfs4_delegation **dp) |
| 4098 | { |
| 4099 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4100 | __be32 status = nfserr_bad_stateid; |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4101 | struct nfs4_delegation *deleg; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4102 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4103 | deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); |
| 4104 | if (deleg == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4105 | goto out; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 4106 | if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) { |
| 4107 | nfs4_put_stid(&deleg->dl_stid); |
| 4108 | if (cl->cl_minorversion) |
| 4109 | status = nfserr_deleg_revoked; |
| 4110 | goto out; |
| 4111 | } |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4112 | flags = share_access_to_flags(open->op_share_access); |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4113 | status = nfs4_check_delegmode(deleg, flags); |
| 4114 | if (status) { |
| 4115 | nfs4_put_stid(&deleg->dl_stid); |
| 4116 | goto out; |
| 4117 | } |
| 4118 | *dp = deleg; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4119 | out: |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4120 | if (!nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4121 | return nfs_ok; |
| 4122 | if (status) |
| 4123 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4124 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4125 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4126 | } |
| 4127 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 4128 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 4129 | { |
| 4130 | int flags = 0; |
| 4131 | |
| 4132 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 4133 | flags |= NFSD_MAY_READ; |
| 4134 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 4135 | flags |= NFSD_MAY_WRITE; |
| 4136 | return flags; |
| 4137 | } |
| 4138 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4139 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 4141 | struct nfsd4_open *open) |
| 4142 | { |
| 4143 | struct iattr iattr = { |
| 4144 | .ia_valid = ATTR_SIZE, |
| 4145 | .ia_size = 0, |
| 4146 | }; |
| 4147 | if (!open->op_truncate) |
| 4148 | return 0; |
| 4149 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 4150 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); |
| 4152 | } |
| 4153 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4154 | 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] | 4155 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
| 4156 | struct nfsd4_open *open) |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4157 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4158 | struct file *filp = NULL; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4159 | __be32 status; |
| 4160 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 4161 | int access = nfs4_access_to_access(open->op_share_access); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4162 | unsigned char old_access_bmap, old_deny_bmap; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4163 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4164 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4165 | |
| 4166 | /* |
| 4167 | * Are we trying to set a deny mode that would conflict with |
| 4168 | * current access? |
| 4169 | */ |
| 4170 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 4171 | if (status != nfs_ok) { |
| 4172 | spin_unlock(&fp->fi_lock); |
| 4173 | goto out; |
| 4174 | } |
| 4175 | |
| 4176 | /* set access to the file */ |
| 4177 | status = nfs4_file_get_access(fp, open->op_share_access); |
| 4178 | if (status != nfs_ok) { |
| 4179 | spin_unlock(&fp->fi_lock); |
| 4180 | goto out; |
| 4181 | } |
| 4182 | |
| 4183 | /* Set access bits in stateid */ |
| 4184 | old_access_bmap = stp->st_access_bmap; |
| 4185 | set_access(open->op_share_access, stp); |
| 4186 | |
| 4187 | /* Set new deny mask */ |
| 4188 | old_deny_bmap = stp->st_deny_bmap; |
| 4189 | set_deny(open->op_share_deny, stp); |
| 4190 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 4191 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4192 | if (!fp->fi_fds[oflag]) { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4193 | spin_unlock(&fp->fi_lock); |
| 4194 | status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4195 | if (status) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4196 | goto out_put_access; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4197 | spin_lock(&fp->fi_lock); |
| 4198 | if (!fp->fi_fds[oflag]) { |
| 4199 | fp->fi_fds[oflag] = filp; |
| 4200 | filp = NULL; |
| 4201 | } |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4202 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4203 | spin_unlock(&fp->fi_lock); |
| 4204 | if (filp) |
| 4205 | fput(filp); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4206 | |
| 4207 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 4208 | if (status) |
| 4209 | goto out_put_access; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4210 | out: |
| 4211 | return status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4212 | out_put_access: |
| 4213 | stp->st_access_bmap = old_access_bmap; |
| 4214 | nfs4_file_put_access(fp, open->op_share_access); |
| 4215 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); |
| 4216 | goto out; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4217 | } |
| 4218 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4219 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4220 | 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] | 4221 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4222 | __be32 status; |
Arnd Bergmann | 6ac7536 | 2015-05-12 23:31:29 +0200 | [diff] [blame] | 4223 | unsigned char old_deny_bmap = stp->st_deny_bmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4225 | if (!test_access(open->op_share_access, stp)) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4226 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4227 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4228 | /* test and set deny mode */ |
| 4229 | spin_lock(&fp->fi_lock); |
| 4230 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 4231 | if (status == nfs_ok) { |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4232 | set_deny(open->op_share_deny, stp); |
| 4233 | fp->fi_share_deny |= |
| 4234 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 4235 | } |
| 4236 | spin_unlock(&fp->fi_lock); |
| 4237 | |
| 4238 | if (status != nfs_ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4239 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4241 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 4242 | if (status != nfs_ok) |
| 4243 | reset_union_bmap_deny(old_deny_bmap, stp); |
| 4244 | return status; |
| 4245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 4247 | /* Should we give out recallable state?: */ |
| 4248 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 4249 | { |
| 4250 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 4251 | return true; |
| 4252 | /* |
| 4253 | * In the sessions case, since we don't have to establish a |
| 4254 | * separate connection for callbacks, we assume it's OK |
| 4255 | * until we hear otherwise: |
| 4256 | */ |
| 4257 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 4258 | } |
| 4259 | |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 4260 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag) |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4261 | { |
| 4262 | struct file_lock *fl; |
| 4263 | |
| 4264 | fl = locks_alloc_lock(); |
| 4265 | if (!fl) |
| 4266 | return NULL; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4267 | fl->fl_lmops = &nfsd_lease_mng_ops; |
J. Bruce Fields | 617588d | 2011-07-01 15:18:34 -0400 | [diff] [blame] | 4268 | fl->fl_flags = FL_DELEG; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4269 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 4270 | fl->fl_end = OFFSET_MAX; |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 4271 | fl->fl_owner = (fl_owner_t)fp; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4272 | fl->fl_pid = current->tgid; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4273 | return fl; |
| 4274 | } |
| 4275 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4276 | /** |
| 4277 | * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer |
| 4278 | * @dp: a pointer to the nfs4_delegation we're adding. |
| 4279 | * |
| 4280 | * Return: |
| 4281 | * On success: Return code will be 0 on success. |
| 4282 | * |
| 4283 | * On error: -EAGAIN if there was an existing delegation. |
| 4284 | * nonzero if there is an error in other cases. |
| 4285 | * |
| 4286 | */ |
| 4287 | |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4288 | static int nfs4_setlease(struct nfs4_delegation *dp) |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 4289 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4290 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Kinglong Mee | efde6b4 | 2015-07-13 17:33:24 +0800 | [diff] [blame] | 4291 | struct file_lock *fl; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4292 | struct file *filp; |
| 4293 | int status = 0; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 4294 | |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 4295 | fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 4296 | if (!fl) |
| 4297 | return -ENOMEM; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4298 | filp = find_readable_file(fp); |
| 4299 | if (!filp) { |
| 4300 | /* We should always have a readable file here */ |
| 4301 | WARN_ON_ONCE(1); |
Kinglong Mee | af9dbaf | 2015-07-13 17:30:51 +0800 | [diff] [blame] | 4302 | locks_free_lock(fl); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4303 | return -EBADF; |
| 4304 | } |
| 4305 | fl->fl_file = filp; |
Jeff Layton | e6f5c78 | 2014-08-22 10:40:25 -0400 | [diff] [blame] | 4306 | status = vfs_setlease(filp, fl->fl_type, &fl, NULL); |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 4307 | if (fl) |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4308 | locks_free_lock(fl); |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 4309 | if (status) |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4310 | goto out_fput; |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4311 | spin_lock(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4312 | spin_lock(&fp->fi_lock); |
| 4313 | /* Did the lease get broken before we took the lock? */ |
| 4314 | status = -EAGAIN; |
| 4315 | if (fp->fi_had_conflict) |
| 4316 | goto out_unlock; |
| 4317 | /* Race breaker */ |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 4318 | if (fp->fi_deleg_file) { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4319 | status = hash_delegation_locked(dp, fp); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4320 | goto out_unlock; |
| 4321 | } |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4322 | fp->fi_deleg_file = filp; |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4323 | fp->fi_delegees = 0; |
| 4324 | status = hash_delegation_locked(dp, fp); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4325 | spin_unlock(&fp->fi_lock); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4326 | spin_unlock(&state_lock); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4327 | if (status) { |
| 4328 | /* Should never happen, this is a new fi_deleg_file */ |
| 4329 | WARN_ON_ONCE(1); |
| 4330 | goto out_fput; |
| 4331 | } |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4332 | return 0; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4333 | out_unlock: |
| 4334 | spin_unlock(&fp->fi_lock); |
| 4335 | spin_unlock(&state_lock); |
| 4336 | out_fput: |
| 4337 | fput(filp); |
J. Bruce Fields | e873088 | 2012-01-23 13:52:01 -0500 | [diff] [blame] | 4338 | return status; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4339 | } |
| 4340 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4341 | static struct nfs4_delegation * |
| 4342 | nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4343 | struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4344 | { |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4345 | int status; |
| 4346 | struct nfs4_delegation *dp; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4347 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 4348 | if (fp->fi_had_conflict) |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4349 | return ERR_PTR(-EAGAIN); |
| 4350 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4351 | spin_lock(&state_lock); |
| 4352 | spin_lock(&fp->fi_lock); |
| 4353 | status = nfs4_get_existing_delegation(clp, fp); |
| 4354 | spin_unlock(&fp->fi_lock); |
| 4355 | spin_unlock(&state_lock); |
| 4356 | |
| 4357 | if (status) |
| 4358 | return ERR_PTR(status); |
| 4359 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4360 | dp = alloc_init_deleg(clp, fh, odstate); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4361 | if (!dp) |
| 4362 | return ERR_PTR(-ENOMEM); |
| 4363 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 4364 | get_nfs4_file(fp); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4365 | spin_lock(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4366 | spin_lock(&fp->fi_lock); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4367 | dp->dl_stid.sc_file = fp; |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 4368 | if (!fp->fi_deleg_file) { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4369 | spin_unlock(&fp->fi_lock); |
| 4370 | spin_unlock(&state_lock); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4371 | status = nfs4_setlease(dp); |
| 4372 | goto out; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4373 | } |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4374 | if (fp->fi_had_conflict) { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4375 | status = -EAGAIN; |
| 4376 | goto out_unlock; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4377 | } |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4378 | status = hash_delegation_locked(dp, fp); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4379 | out_unlock: |
| 4380 | spin_unlock(&fp->fi_lock); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4381 | spin_unlock(&state_lock); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4382 | out: |
| 4383 | if (status) { |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4384 | put_clnt_odstate(dp->dl_clnt_odstate); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 4385 | nfs4_put_stid(&dp->dl_stid); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4386 | return ERR_PTR(status); |
| 4387 | } |
| 4388 | return dp; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 4389 | } |
| 4390 | |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 4391 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 4392 | { |
| 4393 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4394 | if (status == -EAGAIN) |
| 4395 | open->op_why_no_deleg = WND4_CONTENTION; |
| 4396 | else { |
| 4397 | open->op_why_no_deleg = WND4_RESOURCE; |
| 4398 | switch (open->op_deleg_want) { |
| 4399 | case NFS4_SHARE_WANT_READ_DELEG: |
| 4400 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 4401 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 4402 | break; |
| 4403 | case NFS4_SHARE_WANT_CANCEL: |
| 4404 | open->op_why_no_deleg = WND4_CANCELLED; |
| 4405 | break; |
| 4406 | case NFS4_SHARE_WANT_NO_DELEG: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4407 | WARN_ON_ONCE(1); |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 4408 | } |
| 4409 | } |
| 4410 | } |
| 4411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | /* |
| 4413 | * Attempt to hand out a delegation. |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4414 | * |
| 4415 | * Note we don't support write delegations, and won't until the vfs has |
| 4416 | * proper support for them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | */ |
| 4418 | static void |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4419 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
| 4420 | struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | { |
| 4422 | struct nfs4_delegation *dp; |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4423 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
| 4424 | struct nfs4_client *clp = stp->st_stid.sc_client; |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 4425 | int cb_up; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4426 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4428 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4429 | open->op_recall = 0; |
| 4430 | switch (open->op_claim_type) { |
| 4431 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 4432 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4433 | open->op_recall = 1; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4434 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 4435 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4436 | break; |
| 4437 | case NFS4_OPEN_CLAIM_NULL: |
Ming Chen | ed47b06 | 2014-01-09 21:26:10 +0000 | [diff] [blame] | 4438 | case NFS4_OPEN_CLAIM_FH: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4439 | /* |
| 4440 | * Let's not give out any delegations till everyone's |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 4441 | * had the chance to reclaim theirs, *and* until |
| 4442 | * NLM locks have all been reclaimed: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4443 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4444 | if (locks_in_grace(clp->net)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4445 | goto out_no_deleg; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4446 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4447 | goto out_no_deleg; |
Steve Dickson | 9a0590a | 2013-05-15 14:51:49 -0400 | [diff] [blame] | 4448 | /* |
| 4449 | * Also, if the file was opened for write or |
| 4450 | * create, there's a good chance the client's |
| 4451 | * about to write to it, resulting in an |
| 4452 | * immediate recall (since we don't support |
| 4453 | * write delegations): |
| 4454 | */ |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4455 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4456 | goto out_no_deleg; |
| 4457 | if (open->op_create == NFS4_OPEN_CREATE) |
| 4458 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4459 | break; |
| 4460 | default: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4461 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4462 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4463 | 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] | 4464 | if (IS_ERR(dp)) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 4465 | goto out_no_deleg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 4467 | 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] | 4468 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4469 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 4470 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4471 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
Trond Myklebust | 67cb127 | 2014-07-29 21:34:17 -0400 | [diff] [blame] | 4472 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 4473 | return; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 4474 | out_no_deleg: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4475 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 4476 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 4477 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4478 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 4479 | open->op_recall = 1; |
| 4480 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4481 | |
| 4482 | /* 4.1 client asking for a delegation? */ |
| 4483 | if (open->op_deleg_want) |
| 4484 | nfsd4_open_deleg_none_ext(open, status); |
| 4485 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4486 | } |
| 4487 | |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 4488 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 4489 | struct nfs4_delegation *dp) |
| 4490 | { |
| 4491 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 4492 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 4493 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4494 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 4495 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 4496 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 4497 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4498 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 4499 | } |
| 4500 | /* Otherwise the client must be confused wanting a delegation |
| 4501 | * it already has, therefore we don't return |
| 4502 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 4503 | */ |
| 4504 | } |
| 4505 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4506 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 4508 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4509 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4510 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | struct nfs4_file *fp = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4512 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4513 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4514 | __be32 status; |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 4515 | bool new_stp = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | /* |
| 4518 | * Lookup file; if found, lookup stateid and check open request, |
| 4519 | * and check for delegations in the process of being recalled. |
| 4520 | * If not found, create the nfs4_file struct |
| 4521 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 4522 | fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4523 | if (fp != open->op_file) { |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 4524 | status = nfs4_check_deleg(cl, open, &dp); |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4525 | if (status) |
| 4526 | goto out; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4527 | stp = nfsd4_find_and_lock_existing_open(fp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4528 | } else { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4529 | open->op_file = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4530 | status = nfserr_bad_stateid; |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4531 | if (nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4532 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4533 | } |
| 4534 | |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 4535 | if (!stp) { |
| 4536 | stp = init_open_stateid(fp, open); |
| 4537 | if (!open->op_stp) |
| 4538 | new_stp = true; |
| 4539 | } |
| 4540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | /* |
| 4542 | * OPEN the file, or upgrade an existing OPEN. |
| 4543 | * If truncate fails, the OPEN fails. |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 4544 | * |
| 4545 | * stp is already locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | */ |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 4547 | if (!new_stp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | /* Stateid was found, this is an OPEN upgrade */ |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4549 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 4550 | if (status) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 4551 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | goto out; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 4553 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4554 | } else { |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4555 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
| 4556 | if (status) { |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 4557 | stp->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4558 | release_open_stateid(stp); |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 4559 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4560 | goto out; |
| 4561 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4562 | |
| 4563 | stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, |
| 4564 | open->op_odstate); |
| 4565 | if (stp->st_clnt_odstate == open->op_odstate) |
| 4566 | open->op_odstate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | } |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 4568 | |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 4569 | nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 4570 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4572 | if (nfsd4_has_session(&resp->cstate)) { |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4573 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 4574 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4575 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 4576 | goto nodeleg; |
| 4577 | } |
| 4578 | } |
| 4579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | /* |
| 4581 | * Attempt to hand out a delegation. No error return, because the |
| 4582 | * OPEN succeeds even if we fail. |
| 4583 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4584 | nfs4_open_delegation(current_fh, open, stp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4585 | nodeleg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | status = nfs_ok; |
| 4587 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4588 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4589 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | out: |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4591 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 4592 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 4593 | open->op_deleg_want) |
| 4594 | nfsd4_deleg_xgrade_none_ext(open, dp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4595 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4596 | if (fp) |
| 4597 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 4598 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
Kinglong Mee | 8718602 | 2015-07-13 17:32:59 +0800 | [diff] [blame] | 4599 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | /* |
| 4601 | * To finish the open response, we just need to set the rflags. |
| 4602 | */ |
| 4603 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
Jeff Layton | 19e4c34 | 2016-09-16 16:28:27 -0400 | [diff] [blame] | 4604 | if (nfsd4_has_session(&resp->cstate)) |
| 4605 | open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK; |
| 4606 | else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
Jeff Layton | 19e4c34 | 2016-09-16 16:28:27 -0400 | [diff] [blame] | 4608 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4609 | if (dp) |
| 4610 | nfs4_put_stid(&dp->dl_stid); |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 4611 | if (stp) |
| 4612 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | |
| 4614 | return status; |
| 4615 | } |
| 4616 | |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4617 | void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, |
Jeff Layton | 4229789 | 2015-03-23 10:53:44 -0400 | [diff] [blame] | 4618 | struct nfsd4_open *open) |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4619 | { |
| 4620 | if (open->op_openowner) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4621 | struct nfs4_stateowner *so = &open->op_openowner->oo_owner; |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4622 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4623 | nfsd4_cstate_assign_replay(cstate, so); |
| 4624 | nfs4_put_stateowner(so); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4625 | } |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4626 | if (open->op_file) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4627 | kmem_cache_free(file_slab, open->op_file); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4628 | if (open->op_stp) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 4629 | nfs4_put_stid(&open->op_stp->st_stid); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4630 | if (open->op_odstate) |
| 4631 | kmem_cache_free(odstate_slab, open->op_odstate); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4632 | } |
| 4633 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4634 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 4635 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 4636 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4637 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 4638 | clientid_t *clid = &u->renew; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4640 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 4641 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | dprintk("process_renew(%08x/%08x): starting\n", |
| 4644 | clid->cl_boot, clid->cl_id); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4645 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 4646 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | goto out; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4648 | clp = cstate->clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | status = nfserr_cb_path_down; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 4650 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 4651 | && clp->cl_cb_state != NFSD4_CB_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | goto out; |
| 4653 | status = nfs_ok; |
| 4654 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | return status; |
| 4656 | } |
| 4657 | |
Jeff Layton | 7f5ef2e | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 4658 | void |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 4659 | nfsd4_end_grace(struct nfsd_net *nn) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 4660 | { |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 4661 | /* do nothing if grace period already ended */ |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 4662 | if (nn->grace_ended) |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 4663 | return; |
| 4664 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 4665 | dprintk("NFSD: end of grace period\n"); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 4666 | nn->grace_ended = true; |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 4667 | /* |
| 4668 | * If the server goes down again right now, an NFSv4 |
| 4669 | * client will still be allowed to reclaim after it comes back up, |
| 4670 | * even if it hasn't yet had a chance to reclaim state this time. |
| 4671 | * |
| 4672 | */ |
Jeff Layton | 919b804 | 2014-09-12 16:40:20 -0400 | [diff] [blame] | 4673 | nfsd4_record_grace_done(nn); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 4674 | /* |
| 4675 | * At this point, NFSv4 clients can still reclaim. But if the |
| 4676 | * server crashes, any that have not yet reclaimed will be out |
| 4677 | * of luck on the next boot. |
| 4678 | * |
| 4679 | * (NFSv4.1+ clients are considered to have reclaimed once they |
| 4680 | * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to |
| 4681 | * have reclaimed after their first OPEN.) |
| 4682 | */ |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 4683 | locks_end_grace(&nn->nfsd4_manager); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 4684 | /* |
| 4685 | * At this point, and once lockd and/or any other containers |
| 4686 | * exit their grace period, further reclaims will fail and |
| 4687 | * regular locking can resume. |
| 4688 | */ |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 4689 | } |
| 4690 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 4691 | static time_t |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4692 | nfs4_laundromat(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | { |
| 4694 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4695 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | struct nfs4_delegation *dp; |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4697 | struct nfs4_ol_stateid *stp; |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 4698 | struct nfsd4_blocked_lock *nbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 3d73371 | 2012-11-27 14:11:44 +0300 | [diff] [blame] | 4700 | time_t cutoff = get_seconds() - nn->nfsd4_lease; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4701 | time_t t, new_timeo = nn->nfsd4_lease; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4703 | dprintk("NFSD: laundromat service - starting\n"); |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 4704 | nfsd4_end_grace(nn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 4705 | INIT_LIST_HEAD(&reaplist); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 4706 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 4707 | list_for_each_safe(pos, next, &nn->client_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4708 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
| 4709 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { |
| 4710 | t = clp->cl_time - cutoff; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4711 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | break; |
| 4713 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 4714 | if (mark_client_expired_locked(clp)) { |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 4715 | dprintk("NFSD: client in use (clientid %08x)\n", |
| 4716 | clp->cl_clientid.cl_id); |
| 4717 | continue; |
| 4718 | } |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 4719 | list_add(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 4720 | } |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 4721 | spin_unlock(&nn->client_lock); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 4722 | list_for_each_safe(pos, next, &reaplist) { |
| 4723 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 4725 | clp->cl_clientid.cl_id); |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 4726 | list_del_init(&clp->cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | expire_client(clp); |
| 4728 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4729 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 4730 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4731 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 4732 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4733 | t = dp->dl_time - cutoff; |
| 4734 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4735 | break; |
| 4736 | } |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 4737 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 4738 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4740 | spin_unlock(&state_lock); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 4741 | while (!list_empty(&reaplist)) { |
| 4742 | dp = list_first_entry(&reaplist, struct nfs4_delegation, |
| 4743 | dl_recall_lru); |
| 4744 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4745 | revoke_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4747 | |
| 4748 | spin_lock(&nn->client_lock); |
| 4749 | while (!list_empty(&nn->close_lru)) { |
| 4750 | oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, |
| 4751 | oo_close_lru); |
| 4752 | if (time_after((unsigned long)oo->oo_time, |
| 4753 | (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4754 | t = oo->oo_time - cutoff; |
| 4755 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | break; |
| 4757 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4758 | list_del_init(&oo->oo_close_lru); |
| 4759 | stp = oo->oo_last_closed_stid; |
| 4760 | oo->oo_last_closed_stid = NULL; |
| 4761 | spin_unlock(&nn->client_lock); |
| 4762 | nfs4_put_stid(&stp->st_stid); |
| 4763 | spin_lock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4764 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4765 | spin_unlock(&nn->client_lock); |
| 4766 | |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 4767 | /* |
| 4768 | * It's possible for a client to try and acquire an already held lock |
| 4769 | * that is being held for a long time, and then lose interest in it. |
| 4770 | * So, we clean out any un-revisited request after a lease period |
| 4771 | * under the assumption that the client is no longer interested. |
| 4772 | * |
| 4773 | * RFC5661, sec. 9.6 states that the client must not rely on getting |
| 4774 | * notifications and must continue to poll for locks, even when the |
| 4775 | * server supports them. Thus this shouldn't lead to clients blocking |
| 4776 | * indefinitely once the lock does become free. |
| 4777 | */ |
| 4778 | BUG_ON(!list_empty(&reaplist)); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 4779 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 4780 | while (!list_empty(&nn->blocked_locks_lru)) { |
| 4781 | nbl = list_first_entry(&nn->blocked_locks_lru, |
| 4782 | struct nfsd4_blocked_lock, nbl_lru); |
| 4783 | if (time_after((unsigned long)nbl->nbl_time, |
| 4784 | (unsigned long)cutoff)) { |
| 4785 | t = nbl->nbl_time - cutoff; |
| 4786 | new_timeo = min(new_timeo, t); |
| 4787 | break; |
| 4788 | } |
| 4789 | list_move(&nbl->nbl_lru, &reaplist); |
| 4790 | list_del_init(&nbl->nbl_list); |
| 4791 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 4792 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 4793 | |
| 4794 | while (!list_empty(&reaplist)) { |
Naofumi Honda | 64ebe124 | 2017-11-09 10:57:16 -0500 | [diff] [blame] | 4795 | nbl = list_first_entry(&reaplist, |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 4796 | struct nfsd4_blocked_lock, nbl_lru); |
| 4797 | list_del_init(&nbl->nbl_lru); |
| 4798 | posix_unblock_lock(&nbl->nbl_lock); |
| 4799 | free_blocked_lock(nbl); |
| 4800 | } |
| 4801 | |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4802 | new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4803 | return new_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | } |
| 4805 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 4806 | static struct workqueue_struct *laundry_wq; |
| 4807 | static void laundromat_main(struct work_struct *); |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 4808 | |
| 4809 | static void |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4810 | laundromat_main(struct work_struct *laundry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | { |
| 4812 | time_t t; |
Geliang Tang | 2e55f3a | 2016-01-01 22:06:28 +0800 | [diff] [blame] | 4813 | struct delayed_work *dwork = to_delayed_work(laundry); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4814 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 4815 | laundromat_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4816 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4817 | t = nfs4_laundromat(nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4818 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4819 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4820 | } |
| 4821 | |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 4822 | 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] | 4823 | { |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 4824 | if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4825 | return nfserr_bad_stateid; |
| 4826 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4827 | } |
| 4828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4830 | access_permit_read(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4832 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 4833 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 4834 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | } |
| 4836 | |
| 4837 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4838 | access_permit_write(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4840 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 4841 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4842 | } |
| 4843 | |
| 4844 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4845 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4847 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4848 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 4849 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 4850 | if (stp->st_openstp) |
| 4851 | stp = stp->st_openstp; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4852 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | goto out; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4854 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4855 | goto out; |
| 4856 | status = nfs_ok; |
| 4857 | out: |
| 4858 | return status; |
| 4859 | } |
| 4860 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4861 | static inline __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4862 | 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] | 4863 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4864 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | return nfs_ok; |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 4866 | else if (opens_in_grace(net)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4867 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | * conflicting state; so we must wait out the grace period. */ |
| 4869 | return nfserr_grace; |
| 4870 | } else if (flags & WR_STATE) |
| 4871 | return nfs4_share_conflict(current_fh, |
| 4872 | NFS4_SHARE_DENY_WRITE); |
| 4873 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 4874 | return nfs4_share_conflict(current_fh, |
| 4875 | NFS4_SHARE_DENY_READ); |
| 4876 | } |
| 4877 | |
| 4878 | /* |
| 4879 | * Allow READ/WRITE during grace period on recovered state only for files |
| 4880 | * that are not able to provide mandatory locking. |
| 4881 | */ |
| 4882 | static inline int |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4883 | grace_disallows_io(struct net *net, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4884 | { |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 4885 | return opens_in_grace(net) && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4886 | } |
| 4887 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 4888 | 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] | 4889 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4890 | /* |
| 4891 | * When sessions are used the stateid generation number is ignored |
| 4892 | * when it is zero. |
| 4893 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 4894 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4895 | return nfs_ok; |
| 4896 | |
| 4897 | if (in->si_generation == ref->si_generation) |
| 4898 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4899 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4900 | /* 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] | 4901 | if (nfsd4_stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4902 | return nfserr_bad_stateid; |
| 4903 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4904 | * However, we could see a stateid from the past, even from a |
| 4905 | * non-buggy client. For example, if the client sends a lock |
| 4906 | * while some IO is outstanding, the lock may bump si_generation |
| 4907 | * while the IO is still in flight. The client could avoid that |
| 4908 | * situation by waiting for responses on all the IO requests, |
| 4909 | * but better performance may result in retrying IO that |
| 4910 | * receives an old_stateid error if requests are rarely |
| 4911 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4912 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4913 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4914 | } |
| 4915 | |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 4916 | static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session) |
| 4917 | { |
| 4918 | __be32 ret; |
| 4919 | |
| 4920 | spin_lock(&s->sc_lock); |
| 4921 | ret = nfsd4_verify_open_stid(s); |
| 4922 | if (ret == nfs_ok) |
| 4923 | ret = check_stateid_generation(in, &s->sc_stateid, has_session); |
| 4924 | spin_unlock(&s->sc_lock); |
| 4925 | return ret; |
| 4926 | } |
| 4927 | |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 4928 | static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) |
| 4929 | { |
| 4930 | if (ols->st_stateowner->so_is_open_owner && |
| 4931 | !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
| 4932 | return nfserr_bad_stateid; |
| 4933 | return nfs_ok; |
| 4934 | } |
| 4935 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4936 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4937 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4938 | struct nfs4_stid *s; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4939 | __be32 status = nfserr_bad_stateid; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4940 | |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 4941 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
| 4942 | CLOSE_STATEID(stateid)) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4943 | return status; |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4944 | /* Client debugging aid. */ |
| 4945 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { |
| 4946 | char addr_str[INET6_ADDRSTRLEN]; |
| 4947 | rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, |
| 4948 | sizeof(addr_str)); |
| 4949 | pr_warn_ratelimited("NFSD: client %s testing state ID " |
| 4950 | "with incorrect client ID\n", addr_str); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4951 | return status; |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4952 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4953 | spin_lock(&cl->cl_lock); |
| 4954 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4955 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4956 | goto out_unlock; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 4957 | status = nfsd4_stid_check_stateid_generation(stateid, s, 1); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4958 | if (status) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4959 | goto out_unlock; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4960 | switch (s->sc_type) { |
| 4961 | case NFS4_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4962 | status = nfs_ok; |
| 4963 | break; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4964 | case NFS4_REVOKED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4965 | status = nfserr_deleg_revoked; |
| 4966 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4967 | case NFS4_OPEN_STID: |
| 4968 | case NFS4_LOCK_STID: |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 4969 | status = nfsd4_check_openowner_confirmed(openlockstateid(s)); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4970 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4971 | default: |
| 4972 | printk("unknown stateid type %x\n", s->sc_type); |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 4973 | /* Fallthrough */ |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4974 | case NFS4_CLOSED_STID: |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 4975 | case NFS4_CLOSED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4976 | status = nfserr_bad_stateid; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4977 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4978 | out_unlock: |
| 4979 | spin_unlock(&cl->cl_lock); |
| 4980 | return status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4981 | } |
| 4982 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 4983 | __be32 |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4984 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
| 4985 | stateid_t *stateid, unsigned char typemask, |
| 4986 | struct nfs4_stid **s, struct nfsd_net *nn) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4987 | { |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 4988 | __be32 status; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 4989 | bool return_revoked = false; |
| 4990 | |
| 4991 | /* |
| 4992 | * only return revoked delegations if explicitly asked. |
| 4993 | * otherwise we report revoked or bad_stateid status. |
| 4994 | */ |
| 4995 | if (typemask & NFS4_REVOKED_DELEG_STID) |
| 4996 | return_revoked = true; |
| 4997 | else if (typemask & NFS4_DELEG_STID) |
| 4998 | typemask |= NFS4_REVOKED_DELEG_STID; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4999 | |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 5000 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
| 5001 | CLOSE_STATEID(stateid)) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5002 | return nfserr_bad_stateid; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5003 | status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn); |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5004 | if (status == nfserr_stale_clientid) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5005 | if (cstate->session) |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5006 | return nfserr_bad_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5007 | return nfserr_stale_stateid; |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5008 | } |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 5009 | if (status) |
| 5010 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5011 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5012 | if (!*s) |
| 5013 | return nfserr_bad_stateid; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 5014 | if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) { |
| 5015 | nfs4_put_stid(*s); |
| 5016 | if (cstate->minorversion) |
| 5017 | return nfserr_deleg_revoked; |
| 5018 | return nfserr_bad_stateid; |
| 5019 | } |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5020 | return nfs_ok; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5021 | } |
| 5022 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5023 | static struct file * |
| 5024 | nfs4_find_file(struct nfs4_stid *s, int flags) |
| 5025 | { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5026 | if (!s) |
| 5027 | return NULL; |
| 5028 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5029 | switch (s->sc_type) { |
| 5030 | case NFS4_DELEG_STID: |
| 5031 | if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) |
| 5032 | return NULL; |
| 5033 | return get_file(s->sc_file->fi_deleg_file); |
| 5034 | case NFS4_OPEN_STID: |
| 5035 | case NFS4_LOCK_STID: |
| 5036 | if (flags & RD_STATE) |
| 5037 | return find_readable_file(s->sc_file); |
| 5038 | else |
| 5039 | return find_writeable_file(s->sc_file); |
| 5040 | break; |
| 5041 | } |
| 5042 | |
| 5043 | return NULL; |
| 5044 | } |
| 5045 | |
| 5046 | static __be32 |
| 5047 | nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags) |
| 5048 | { |
| 5049 | __be32 status; |
| 5050 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5051 | status = nfsd4_check_openowner_confirmed(ols); |
| 5052 | if (status) |
| 5053 | return status; |
| 5054 | return nfs4_check_openmode(ols, flags); |
| 5055 | } |
| 5056 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5057 | static __be32 |
| 5058 | nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s, |
| 5059 | struct file **filpp, bool *tmp_file, int flags) |
| 5060 | { |
| 5061 | int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; |
| 5062 | struct file *file; |
| 5063 | __be32 status; |
| 5064 | |
| 5065 | file = nfs4_find_file(s, flags); |
| 5066 | if (file) { |
| 5067 | status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
| 5068 | acc | NFSD_MAY_OWNER_OVERRIDE); |
| 5069 | if (status) { |
| 5070 | fput(file); |
| 5071 | return status; |
| 5072 | } |
| 5073 | |
| 5074 | *filpp = file; |
| 5075 | } else { |
| 5076 | status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp); |
| 5077 | if (status) |
| 5078 | return status; |
| 5079 | |
| 5080 | if (tmp_file) |
| 5081 | *tmp_file = true; |
| 5082 | } |
| 5083 | |
| 5084 | return 0; |
| 5085 | } |
| 5086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | /* |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5088 | * Checks for stateid operations |
| 5089 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5090 | __be32 |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5091 | nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, |
Anna Schumaker | aa0d6ae | 2015-12-03 12:59:51 +0100 | [diff] [blame] | 5092 | struct nfsd4_compound_state *cstate, struct svc_fh *fhp, |
| 5093 | stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | { |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5095 | struct inode *ino = d_inode(fhp->fh_dentry); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5096 | struct net *net = SVC_NET(rqstp); |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5097 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5098 | struct nfs4_stid *s = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5099 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | if (filpp) |
| 5102 | *filpp = NULL; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5103 | if (tmp_file) |
| 5104 | *tmp_file = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5106 | if (grace_disallows_io(net, ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | return nfserr_grace; |
| 5108 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5109 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
| 5110 | status = check_special_stateids(net, fhp, stateid, flags); |
| 5111 | goto done; |
| 5112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5114 | status = nfsd4_lookup_stateid(cstate, stateid, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 5115 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5116 | &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5117 | if (status) |
Trond Myklebust | c2d1d6a | 2014-07-30 08:27:25 -0400 | [diff] [blame] | 5118 | return status; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5119 | status = nfsd4_stid_check_stateid_generation(stateid, s, |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5120 | nfsd4_has_session(cstate)); |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 5121 | if (status) |
| 5122 | goto out; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5123 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5124 | switch (s->sc_type) { |
| 5125 | case NFS4_DELEG_STID: |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5126 | status = nfs4_check_delegmode(delegstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5127 | break; |
| 5128 | case NFS4_OPEN_STID: |
| 5129 | case NFS4_LOCK_STID: |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5130 | status = nfs4_check_olstateid(fhp, openlockstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5131 | break; |
| 5132 | default: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 5133 | status = nfserr_bad_stateid; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5134 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5135 | } |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5136 | if (status) |
| 5137 | goto out; |
| 5138 | status = nfs4_check_fh(fhp, s); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5139 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5140 | done: |
| 5141 | if (!status && filpp) |
| 5142 | status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | out: |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5144 | if (s) |
| 5145 | nfs4_put_stid(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | return status; |
| 5147 | } |
| 5148 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5149 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5150 | * Test if the stateid is valid |
| 5151 | */ |
| 5152 | __be32 |
| 5153 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5154 | union nfsd4_op_u *u) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5155 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5156 | struct nfsd4_test_stateid *test_stateid = &u->test_stateid; |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5157 | struct nfsd4_test_stateid_id *stateid; |
| 5158 | struct nfs4_client *cl = cstate->session->se_client; |
| 5159 | |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5160 | 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] | 5161 | stateid->ts_id_status = |
| 5162 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5163 | |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5164 | return nfs_ok; |
| 5165 | } |
| 5166 | |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5167 | static __be32 |
| 5168 | nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) |
| 5169 | { |
| 5170 | struct nfs4_ol_stateid *stp = openlockstateid(s); |
| 5171 | __be32 ret; |
| 5172 | |
Trond Myklebust | 659aefb | 2017-11-03 08:00:13 -0400 | [diff] [blame] | 5173 | ret = nfsd4_lock_ol_stateid(stp); |
| 5174 | if (ret) |
| 5175 | goto out_put_stid; |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5176 | |
| 5177 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 5178 | if (ret) |
| 5179 | goto out; |
| 5180 | |
| 5181 | ret = nfserr_locks_held; |
| 5182 | if (check_for_locks(stp->st_stid.sc_file, |
| 5183 | lockowner(stp->st_stateowner))) |
| 5184 | goto out; |
| 5185 | |
| 5186 | release_lock_stateid(stp); |
| 5187 | ret = nfs_ok; |
| 5188 | |
| 5189 | out: |
| 5190 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 659aefb | 2017-11-03 08:00:13 -0400 | [diff] [blame] | 5191 | out_put_stid: |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5192 | nfs4_put_stid(s); |
| 5193 | return ret; |
| 5194 | } |
| 5195 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5196 | __be32 |
| 5197 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5198 | union nfsd4_op_u *u) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5199 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5200 | struct nfsd4_free_stateid *free_stateid = &u->free_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5201 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5202 | struct nfs4_stid *s; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5203 | struct nfs4_delegation *dp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5204 | struct nfs4_client *cl = cstate->session->se_client; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5205 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5206 | |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5207 | spin_lock(&cl->cl_lock); |
| 5208 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5209 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5210 | goto out_unlock; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5211 | spin_lock(&s->sc_lock); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5212 | switch (s->sc_type) { |
| 5213 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5214 | ret = nfserr_locks_held; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5215 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5216 | case NFS4_OPEN_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5217 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 5218 | if (ret) |
| 5219 | break; |
| 5220 | ret = nfserr_locks_held; |
| 5221 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5222 | case NFS4_LOCK_STID: |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5223 | spin_unlock(&s->sc_lock); |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 5224 | refcount_inc(&s->sc_count); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5225 | spin_unlock(&cl->cl_lock); |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5226 | ret = nfsd4_free_lock_stateid(stateid, s); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5227 | goto out; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5228 | case NFS4_REVOKED_DELEG_STID: |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5229 | spin_unlock(&s->sc_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5230 | dp = delegstateid(s); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5231 | list_del_init(&dp->dl_recall_lru); |
| 5232 | spin_unlock(&cl->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 5233 | nfs4_put_stid(s); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5234 | ret = nfs_ok; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5235 | goto out; |
| 5236 | /* Default falls through and returns nfserr_bad_stateid */ |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5237 | } |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5238 | spin_unlock(&s->sc_lock); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5239 | out_unlock: |
| 5240 | spin_unlock(&cl->cl_lock); |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5241 | out: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5242 | return ret; |
| 5243 | } |
| 5244 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 5245 | static inline int |
| 5246 | setlkflg (int type) |
| 5247 | { |
| 5248 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 5249 | RD_STATE : WR_STATE; |
| 5250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5252 | 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] | 5253 | { |
| 5254 | struct svc_fh *current_fh = &cstate->current_fh; |
| 5255 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 5256 | __be32 status; |
| 5257 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5258 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 5259 | if (status) |
| 5260 | return status; |
Trond Myklebust | 9271d7e | 2017-11-03 08:00:15 -0400 | [diff] [blame] | 5261 | status = nfsd4_lock_ol_stateid(stp); |
| 5262 | if (status != nfs_ok) |
| 5263 | return status; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5264 | 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] | 5265 | if (status == nfs_ok) |
| 5266 | status = nfs4_check_fh(current_fh, &stp->st_stid); |
| 5267 | if (status != nfs_ok) |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5268 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5269 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5270 | } |
| 5271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | /* |
| 5273 | * Checks for sequence id mutating operations. |
| 5274 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5275 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 5276 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 5277 | stateid_t *stateid, char typemask, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5278 | struct nfs4_ol_stateid **stpp, |
| 5279 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5281 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5282 | struct nfs4_stid *s; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5283 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5284 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5285 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
| 5286 | seqid, STATEID_VAL(stateid)); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5288 | *stpp = NULL; |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5289 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5290 | if (status) |
| 5291 | return status; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5292 | stp = openlockstateid(s); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 5293 | nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5295 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5296 | if (!status) |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5297 | *stpp = stp; |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5298 | else |
| 5299 | nfs4_put_stid(&stp->st_stid); |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5300 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5301 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 5302 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5303 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 5304 | 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] | 5305 | { |
| 5306 | __be32 status; |
| 5307 | struct nfs4_openowner *oo; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5308 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5310 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5311 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 5312 | if (status) |
| 5313 | return status; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5314 | oo = openowner(stp->st_stateowner); |
| 5315 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5316 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5317 | nfs4_put_stid(&stp->st_stid); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5318 | return nfserr_bad_stateid; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5319 | } |
| 5320 | *stpp = stp; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5321 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | } |
| 5323 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5324 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5325 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5326 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5328 | struct nfsd4_open_confirm *oc = &u->open_confirm; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5329 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5330 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5331 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5332 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5333 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 5334 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 5335 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5337 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 5338 | if (status) |
| 5339 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5340 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5341 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5342 | oc->oc_seqid, &oc->oc_req_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5343 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5344 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 5345 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5346 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 5347 | status = nfserr_bad_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5348 | if (oo->oo_flags & NFS4_OO_CONFIRMED) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5349 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 5350 | goto put_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5351 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 5352 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5353 | nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5354 | mutex_unlock(&stp->st_mutex); |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5355 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5356 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5357 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 5358 | nfsd4_client_record_create(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 5359 | status = nfs_ok; |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 5360 | put_stateid: |
| 5361 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5363 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | return status; |
| 5365 | } |
| 5366 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5367 | 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] | 5368 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5369 | if (!test_access(access, stp)) |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5370 | return; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5371 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5372 | clear_access(access, stp); |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5373 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 5374 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5375 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 5376 | { |
| 5377 | switch (to_access) { |
| 5378 | case NFS4_SHARE_ACCESS_READ: |
| 5379 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 5380 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 5381 | break; |
| 5382 | case NFS4_SHARE_ACCESS_WRITE: |
| 5383 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 5384 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 5385 | break; |
| 5386 | case NFS4_SHARE_ACCESS_BOTH: |
| 5387 | break; |
| 5388 | default: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 5389 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | } |
| 5391 | } |
| 5392 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5393 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5394 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5395 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5397 | struct nfsd4_open_downgrade *od = &u->open_downgrade; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5398 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5399 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5400 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 5402 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 5403 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | |
J. Bruce Fields | c30e92d | 2011-10-10 17:34:31 -0400 | [diff] [blame] | 5405 | /* We don't yet support WANT bits: */ |
Benny Halevy | 2c8bd7e | 2012-02-16 20:57:09 +0200 | [diff] [blame] | 5406 | if (od->od_deleg_want) |
| 5407 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 5408 | od->od_deleg_want); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5410 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5411 | &od->od_stateid, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5412 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5413 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5414 | status = nfserr_inval; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5415 | if (!test_access(od->od_share_access, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 5416 | 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] | 5417 | stp->st_access_bmap, od->od_share_access); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5418 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | } |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 5420 | if (!test_deny(od->od_share_deny, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 5421 | 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] | 5422 | stp->st_deny_bmap, od->od_share_deny); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5423 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | } |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5425 | nfs4_stateid_downgrade(stp, od->od_share_access); |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 5426 | reset_union_bmap_deny(od->od_share_deny, stp); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5427 | nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5428 | status = nfs_ok; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5429 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5430 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5431 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5433 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | return status; |
| 5435 | } |
| 5436 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5437 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 5438 | { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 5439 | struct nfs4_client *clp = s->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 5440 | bool unhashed; |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 5441 | LIST_HEAD(reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 5442 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 5443 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 5444 | unhashed = unhash_open_stateid(s, &reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 5445 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 5446 | if (clp->cl_minorversion) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 5447 | if (unhashed) |
| 5448 | put_ol_stateid_locked(s, &reaplist); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 5449 | spin_unlock(&clp->cl_lock); |
| 5450 | free_ol_stateid_reaplist(&reaplist); |
| 5451 | } else { |
| 5452 | spin_unlock(&clp->cl_lock); |
| 5453 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 5454 | if (unhashed) |
| 5455 | move_to_close_lru(s, clp->net); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 5456 | } |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 5457 | } |
| 5458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | /* |
| 5460 | * nfs4_unlock_state() called after encode |
| 5461 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5462 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5463 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5464 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5466 | struct nfsd4_close *close = &u->close; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5467 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5468 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5469 | struct net *net = SVC_NET(rqstp); |
| 5470 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 5472 | dprintk("NFSD: nfsd4_close on file %pd\n", |
| 5473 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5475 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 5476 | &close->cl_stateid, |
| 5477 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5478 | &stp, nn); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5479 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5480 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | goto out; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 5482 | |
| 5483 | stp->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5484 | nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5486 | nfsd4_close_open_stateid(stp); |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 5487 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 5488 | |
Trond Myklebust | fb500a7 | 2017-11-03 08:00:12 -0400 | [diff] [blame] | 5489 | /* See RFC5661 sectionm 18.2.4 */ |
| 5490 | if (stp->st_stid.sc_client->cl_minorversion) |
| 5491 | memcpy(&close->cl_stateid, &close_stateid, |
| 5492 | sizeof(close->cl_stateid)); |
| 5493 | |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 5494 | /* put reference from nfs4_preprocess_seqid_op */ |
| 5495 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | return status; |
| 5498 | } |
| 5499 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5500 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5501 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5502 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5504 | struct nfsd4_delegreturn *dr = &u->delegreturn; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5505 | struct nfs4_delegation *dp; |
| 5506 | stateid_t *stateid = &dr->dr_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5507 | struct nfs4_stid *s; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5508 | __be32 status; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5509 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5511 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5512 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5514 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5515 | if (status) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5516 | goto out; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5517 | dp = delegstateid(s); |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5518 | 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] | 5519 | if (status) |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5520 | goto put_stateid; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5521 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5522 | destroy_delegation(dp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5523 | put_stateid: |
| 5524 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5525 | out: |
| 5526 | return status; |
| 5527 | } |
| 5528 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 5529 | static inline u64 |
| 5530 | end_offset(u64 start, u64 len) |
| 5531 | { |
| 5532 | u64 end; |
| 5533 | |
| 5534 | end = start + len; |
| 5535 | return end >= start ? end: NFS4_MAX_UINT64; |
| 5536 | } |
| 5537 | |
| 5538 | /* last octet in a range */ |
| 5539 | static inline u64 |
| 5540 | last_byte_offset(u64 start, u64 len) |
| 5541 | { |
| 5542 | u64 end; |
| 5543 | |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 5544 | WARN_ON_ONCE(!len); |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 5545 | end = start + len; |
| 5546 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 5547 | } |
| 5548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | /* |
| 5550 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 5551 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 5552 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 5553 | * locking, this prevents us from being completely protocol-compliant. The |
| 5554 | * real solution to this problem is to start using unsigned file offsets in |
| 5555 | * the VFS, but this is a very deep change! |
| 5556 | */ |
| 5557 | static inline void |
| 5558 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 5559 | { |
| 5560 | if (lock->fl_start < 0) |
| 5561 | lock->fl_start = OFFSET_MAX; |
| 5562 | if (lock->fl_end < 0) |
| 5563 | lock->fl_end = OFFSET_MAX; |
| 5564 | } |
| 5565 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5566 | static fl_owner_t |
| 5567 | nfsd4_fl_get_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5568 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5569 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
| 5570 | |
| 5571 | nfs4_get_stateowner(&lo->lo_owner); |
| 5572 | return owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5573 | } |
| 5574 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5575 | static void |
| 5576 | nfsd4_fl_put_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5577 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5578 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5579 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5580 | if (lo) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5581 | nfs4_put_stateowner(&lo->lo_owner); |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5582 | } |
| 5583 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 5584 | static void |
| 5585 | nfsd4_lm_notify(struct file_lock *fl) |
| 5586 | { |
| 5587 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner; |
| 5588 | struct net *net = lo->lo_owner.so_client->net; |
| 5589 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 5590 | struct nfsd4_blocked_lock *nbl = container_of(fl, |
| 5591 | struct nfsd4_blocked_lock, nbl_lock); |
| 5592 | bool queue = false; |
| 5593 | |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5594 | /* 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] | 5595 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 5596 | if (!list_empty(&nbl->nbl_list)) { |
| 5597 | list_del_init(&nbl->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5598 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 5599 | queue = true; |
| 5600 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 5601 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 5602 | |
| 5603 | if (queue) |
| 5604 | nfsd4_run_cb(&nbl->nbl_cb); |
| 5605 | } |
| 5606 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 5607 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 5608 | .lm_notify = nfsd4_lm_notify, |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5609 | .lm_get_owner = nfsd4_fl_get_owner, |
| 5610 | .lm_put_owner = nfsd4_fl_put_owner, |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 5611 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | |
| 5613 | static inline void |
| 5614 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 5615 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5616 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 5618 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5619 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
| 5620 | deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data, |
| 5621 | lo->lo_owner.so_owner.len, GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 5622 | if (!deny->ld_owner.data) |
| 5623 | /* We just don't care that much */ |
| 5624 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5625 | deny->ld_owner.len = lo->lo_owner.so_owner.len; |
| 5626 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 5627 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 5628 | nevermind: |
| 5629 | deny->ld_owner.len = 0; |
| 5630 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 5631 | deny->ld_clientid.cl_boot = 0; |
| 5632 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | } |
| 5634 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 5635 | deny->ld_length = NFS4_MAX_UINT64; |
| 5636 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5637 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 5638 | deny->ld_type = NFS4_READ_LT; |
| 5639 | if (fl->fl_type != F_RDLCK) |
| 5640 | deny->ld_type = NFS4_WRITE_LT; |
| 5641 | } |
| 5642 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5643 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5644 | find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5645 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5646 | unsigned int strhashval = ownerstr_hashval(owner); |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 5647 | struct nfs4_stateowner *so; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 5649 | lockdep_assert_held(&clp->cl_lock); |
| 5650 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5651 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], |
| 5652 | so_strhash) { |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 5653 | if (so->so_is_open_owner) |
| 5654 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 5655 | if (same_owner_str(so, owner)) |
| 5656 | return lockowner(nfs4_get_stateowner(so)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | } |
| 5658 | return NULL; |
| 5659 | } |
| 5660 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5661 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5662 | find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5663 | { |
| 5664 | struct nfs4_lockowner *lo; |
| 5665 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5666 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5667 | lo = find_lockowner_str_locked(clp, owner); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5668 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5669 | return lo; |
| 5670 | } |
| 5671 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 5672 | static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) |
| 5673 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5674 | unhash_lockowner_locked(lockowner(sop)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 5675 | } |
| 5676 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 5677 | static void nfs4_free_lockowner(struct nfs4_stateowner *sop) |
| 5678 | { |
| 5679 | struct nfs4_lockowner *lo = lockowner(sop); |
| 5680 | |
| 5681 | kmem_cache_free(lockowner_slab, lo); |
| 5682 | } |
| 5683 | |
| 5684 | static const struct nfs4_stateowner_operations lockowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 5685 | .so_unhash = nfs4_unhash_lockowner, |
| 5686 | .so_free = nfs4_free_lockowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 5687 | }; |
| 5688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5689 | /* |
| 5690 | * Alloc a lock owner structure. |
| 5691 | * 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] | 5692 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5693 | * |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 5694 | * strhashval = ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5696 | static struct nfs4_lockowner * |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5697 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
| 5698 | struct nfs4_ol_stateid *open_stp, |
| 5699 | struct nfsd4_lock *lock) |
| 5700 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5701 | struct nfs4_lockowner *lo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5703 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 5704 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | return NULL; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 5706 | INIT_LIST_HEAD(&lo->lo_blocked); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5707 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 5708 | lo->lo_owner.so_is_open_owner = 0; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5709 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 5710 | lo->lo_owner.so_ops = &lockowner_ops; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5711 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5712 | ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5713 | if (ret == NULL) { |
| 5714 | list_add(&lo->lo_owner.so_strhash, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5715 | &clp->cl_ownerstr_hashtbl[strhashval]); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5716 | ret = lo; |
| 5717 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 5718 | nfs4_free_stateowner(&lo->lo_owner); |
| 5719 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5720 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 340f0ba | 2015-03-23 11:02:30 -0400 | [diff] [blame] | 5721 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | } |
| 5723 | |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 5724 | static struct nfs4_ol_stateid * |
| 5725 | find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) |
| 5726 | { |
| 5727 | struct nfs4_ol_stateid *lst; |
| 5728 | struct nfs4_client *clp = lo->lo_owner.so_client; |
| 5729 | |
| 5730 | lockdep_assert_held(&clp->cl_lock); |
| 5731 | |
| 5732 | list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { |
| 5733 | if (lst->st_stid.sc_type != NFS4_LOCK_STID) |
| 5734 | continue; |
| 5735 | if (lst->st_stid.sc_file == fp) { |
| 5736 | refcount_inc(&lst->st_stid.sc_count); |
| 5737 | return lst; |
| 5738 | } |
| 5739 | } |
| 5740 | return NULL; |
| 5741 | } |
| 5742 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 5743 | static struct nfs4_ol_stateid * |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5744 | init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, |
| 5745 | struct nfs4_file *fp, struct inode *inode, |
| 5746 | struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | { |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 5748 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 5749 | struct nfs4_ol_stateid *retstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5750 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 5751 | mutex_init(&stp->st_mutex); |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 5752 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 5753 | retry: |
| 5754 | spin_lock(&clp->cl_lock); |
| 5755 | spin_lock(&fp->fi_lock); |
| 5756 | retstp = find_lock_stateid(lo, fp); |
| 5757 | if (retstp) |
| 5758 | goto out_unlock; |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5759 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 5760 | refcount_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 5761 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 5762 | stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 5763 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5764 | stp->st_stid.sc_file = fp; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5765 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 5767 | stp->st_openstp = open_stp; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5768 | list_add(&stp->st_locks, &open_stp->st_locks); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 5769 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 5770 | list_add(&stp->st_perfile, &fp->fi_stateids); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 5771 | out_unlock: |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 5772 | spin_unlock(&fp->fi_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 5773 | spin_unlock(&clp->cl_lock); |
| 5774 | if (retstp) { |
| 5775 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { |
| 5776 | nfs4_put_stid(&retstp->st_stid); |
| 5777 | goto retry; |
| 5778 | } |
| 5779 | /* To keep mutex tracking happy */ |
| 5780 | mutex_unlock(&stp->st_mutex); |
| 5781 | stp = retstp; |
| 5782 | } |
| 5783 | return stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5784 | } |
| 5785 | |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5786 | static struct nfs4_ol_stateid * |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5787 | find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, |
| 5788 | struct inode *inode, struct nfs4_ol_stateid *ost, |
| 5789 | bool *new) |
| 5790 | { |
| 5791 | struct nfs4_stid *ns = NULL; |
| 5792 | struct nfs4_ol_stateid *lst; |
| 5793 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 5794 | struct nfs4_client *clp = oo->oo_owner.so_client; |
| 5795 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 5796 | *new = false; |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5797 | spin_lock(&clp->cl_lock); |
| 5798 | lst = find_lock_stateid(lo, fi); |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5799 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 5800 | if (lst != NULL) { |
| 5801 | if (nfsd4_lock_ol_stateid(lst) == nfs_ok) |
| 5802 | goto out; |
| 5803 | nfs4_put_stid(&lst->st_stid); |
| 5804 | } |
| 5805 | ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid); |
| 5806 | if (ns == NULL) |
| 5807 | return NULL; |
| 5808 | |
| 5809 | lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost); |
| 5810 | if (lst == openlockstateid(ns)) |
| 5811 | *new = true; |
| 5812 | else |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5813 | nfs4_put_stid(ns); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 5814 | out: |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5815 | return lst; |
| 5816 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5817 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 5818 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 | check_lock_length(u64 offset, u64 length) |
| 5820 | { |
Kinglong Mee | e796931 | 2015-07-13 17:34:19 +0800 | [diff] [blame] | 5821 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
| 5822 | (length > ~offset))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5823 | } |
| 5824 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5825 | 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] | 5826 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5827 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5828 | |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5829 | lockdep_assert_held(&fp->fi_lock); |
| 5830 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5831 | if (test_access(access, lock_stp)) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5832 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 5833 | __nfs4_file_get_access(fp, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5834 | set_access(access, lock_stp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5835 | } |
| 5836 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5837 | static __be32 |
| 5838 | lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, |
| 5839 | struct nfs4_ol_stateid *ost, |
| 5840 | struct nfsd4_lock *lock, |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 5841 | struct nfs4_ol_stateid **plst, bool *new) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5842 | { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5843 | __be32 status; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5844 | struct nfs4_file *fi = ost->st_stid.sc_file; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5845 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 5846 | struct nfs4_client *cl = oo->oo_owner.so_client; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5847 | struct inode *inode = d_inode(cstate->current_fh.fh_dentry); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5848 | struct nfs4_lockowner *lo; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 5849 | struct nfs4_ol_stateid *lst; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5850 | unsigned int strhashval; |
| 5851 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5852 | lo = find_lockowner_str(cl, &lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5853 | if (!lo) { |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 5854 | strhashval = ownerstr_hashval(&lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5855 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 5856 | if (lo == NULL) |
| 5857 | return nfserr_jukebox; |
| 5858 | } else { |
| 5859 | /* with an existing lockowner, seqids must be the same */ |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5860 | status = nfserr_bad_seqid; |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5861 | if (!cstate->minorversion && |
| 5862 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5863 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5864 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5865 | |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 5866 | lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); |
| 5867 | if (lst == NULL) { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5868 | status = nfserr_jukebox; |
| 5869 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5870 | } |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 5871 | |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5872 | status = nfs_ok; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 5873 | *plst = lst; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5874 | out: |
| 5875 | nfs4_put_stateowner(&lo->lo_owner); |
| 5876 | return status; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5877 | } |
| 5878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5879 | /* |
| 5880 | * LOCK operation |
| 5881 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5882 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5883 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5884 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5886 | struct nfsd4_lock *lock = &u->lock; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5887 | struct nfs4_openowner *open_sop = NULL; |
| 5888 | struct nfs4_lockowner *lock_sop = NULL; |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5889 | struct nfs4_ol_stateid *lock_stp = NULL; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5890 | struct nfs4_ol_stateid *open_stp = NULL; |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5891 | struct nfs4_file *fp; |
J. Bruce Fields | 7d94784 | 2010-08-20 18:09:31 -0400 | [diff] [blame] | 5892 | struct file *filp = NULL; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 5893 | struct nfsd4_blocked_lock *nbl = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5894 | struct file_lock *file_lock = NULL; |
| 5895 | struct file_lock *conflock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5896 | __be32 status = 0; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5897 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5898 | int err; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5899 | bool new = false; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 5900 | unsigned char fl_type; |
| 5901 | unsigned int fl_flags = FL_POSIX; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5902 | struct net *net = SVC_NET(rqstp); |
| 5903 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | |
| 5905 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 5906 | (long long) lock->lk_offset, |
| 5907 | (long long) lock->lk_length); |
| 5908 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 5910 | return nfserr_inval; |
| 5911 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5912 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 5913 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 5914 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 5915 | return status; |
| 5916 | } |
| 5917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | if (lock->lk_is_new) { |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 5919 | if (nfsd4_has_session(cstate)) |
| 5920 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 5921 | memcpy(&lock->lk_new_clientid, |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 5922 | &cstate->session->se_client->cl_clientid, |
| 5923 | sizeof(clientid_t)); |
| 5924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5925 | status = nfserr_stale_clientid; |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 5926 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5927 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5930 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | lock->lk_new_open_seqid, |
| 5932 | &lock->lk_new_open_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5933 | &open_stp, nn); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 5934 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5935 | goto out; |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5936 | mutex_unlock(&open_stp->st_mutex); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5937 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5938 | status = nfserr_bad_stateid; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 5939 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 5940 | &lock->lk_new_clientid)) |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5941 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5942 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5943 | &lock_stp, &new); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5944 | } else { |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 5945 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5946 | lock->lk_old_lock_seqid, |
| 5947 | &lock->lk_old_lock_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5948 | NFS4_LOCK_STID, &lock_stp, nn); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5949 | } |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 5950 | if (status) |
| 5951 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5952 | lock_sop = lockowner(lock_stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5954 | lkflg = setlkflg(lock->lk_type); |
| 5955 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 5956 | if (status) |
| 5957 | goto out; |
| 5958 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 5959 | status = nfserr_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5960 | if (locks_in_grace(net) && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 5961 | goto out; |
| 5962 | status = nfserr_no_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5963 | if (!locks_in_grace(net) && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 5964 | goto out; |
| 5965 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5966 | fp = lock_stp->st_stid.sc_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | switch (lock->lk_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | case NFS4_READW_LT: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 5969 | if (nfsd4_has_session(cstate)) |
| 5970 | fl_flags |= FL_SLEEP; |
| 5971 | /* Fallthrough */ |
| 5972 | case NFS4_READ_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5973 | spin_lock(&fp->fi_lock); |
| 5974 | filp = find_readable_file_locked(fp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5975 | if (filp) |
| 5976 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5977 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 5978 | fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 5979 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5980 | case NFS4_WRITEW_LT: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 5981 | if (nfsd4_has_session(cstate)) |
| 5982 | fl_flags |= FL_SLEEP; |
| 5983 | /* Fallthrough */ |
| 5984 | case NFS4_WRITE_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5985 | spin_lock(&fp->fi_lock); |
| 5986 | filp = find_writeable_file_locked(fp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5987 | if (filp) |
| 5988 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5989 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 5990 | fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 5991 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | default: |
| 5993 | status = nfserr_inval; |
| 5994 | goto out; |
| 5995 | } |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 5996 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5997 | if (!filp) { |
| 5998 | status = nfserr_openmode; |
| 5999 | goto out; |
| 6000 | } |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6001 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6002 | nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn); |
| 6003 | if (!nbl) { |
| 6004 | dprintk("NFSD: %s: unable to allocate block!\n", __func__); |
| 6005 | status = nfserr_jukebox; |
| 6006 | goto out; |
| 6007 | } |
| 6008 | |
| 6009 | file_lock = &nbl->nbl_lock; |
| 6010 | file_lock->fl_type = fl_type; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6011 | 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] | 6012 | file_lock->fl_pid = current->tgid; |
| 6013 | file_lock->fl_file = filp; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6014 | file_lock->fl_flags = fl_flags; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6015 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 6016 | file_lock->fl_start = lock->lk_offset; |
| 6017 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 6018 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6019 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6020 | conflock = locks_alloc_lock(); |
| 6021 | if (!conflock) { |
| 6022 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6023 | status = nfserr_jukebox; |
| 6024 | goto out; |
| 6025 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6027 | if (fl_flags & FL_SLEEP) { |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6028 | nbl->nbl_time = jiffies; |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6029 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6030 | list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6031 | list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6032 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6033 | } |
| 6034 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6035 | err = vfs_lock_file(filp, F_SETLK, file_lock, conflock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6036 | switch (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6037 | case 0: /* success! */ |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6038 | 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] | 6039 | status = 0; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6040 | break; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6041 | case FILE_LOCK_DEFERRED: |
| 6042 | nbl = NULL; |
| 6043 | /* Fallthrough */ |
| 6044 | case -EAGAIN: /* conflock holds conflicting lock */ |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6045 | status = nfserr_denied; |
| 6046 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6047 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6048 | break; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6049 | case -EDEADLK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6050 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6051 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 6052 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6053 | 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] | 6054 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6055 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6056 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | out: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6058 | if (nbl) { |
| 6059 | /* dequeue it if we queued it before */ |
| 6060 | if (fl_flags & FL_SLEEP) { |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6061 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6062 | list_del_init(&nbl->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6063 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6064 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6065 | } |
| 6066 | free_blocked_lock(nbl); |
| 6067 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 6068 | if (filp) |
| 6069 | fput(filp); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6070 | if (lock_stp) { |
| 6071 | /* Bump seqid manually if the 4.0 replay owner is openowner */ |
| 6072 | if (cstate->replay_owner && |
| 6073 | cstate->replay_owner != &lock_sop->lo_owner && |
| 6074 | seqid_mutating_err(ntohl(status))) |
| 6075 | lock_sop->lo_owner.so_seqid++; |
| 6076 | |
| 6077 | /* |
| 6078 | * If this is a new, never-before-used stateid, and we are |
| 6079 | * returning an error, then just go ahead and release it. |
| 6080 | */ |
J. Bruce Fields | 2502072 | 2018-01-17 16:25:59 -0500 | [diff] [blame] | 6081 | if (status && new) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6082 | release_lock_stateid(lock_stp); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6083 | |
| 6084 | mutex_unlock(&lock_stp->st_mutex); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6085 | |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6086 | nfs4_put_stid(&lock_stp->st_stid); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6087 | } |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6088 | if (open_stp) |
| 6089 | nfs4_put_stid(&open_stp->st_stid); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6090 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6091 | if (conflock) |
| 6092 | locks_free_lock(conflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6093 | return status; |
| 6094 | } |
| 6095 | |
| 6096 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 6097 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 6098 | * so we do a temporary open here just to get an open file to pass to |
| 6099 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 6100 | * inode operation.) |
| 6101 | */ |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6102 | 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] | 6103 | { |
| 6104 | struct file *file; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6105 | __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); |
| 6106 | if (!err) { |
| 6107 | err = nfserrno(vfs_test_lock(file, lock)); |
Christoph Hellwig | fd89145 | 2015-04-28 15:41:16 +0200 | [diff] [blame] | 6108 | fput(file); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6109 | } |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 6110 | return err; |
| 6111 | } |
| 6112 | |
| 6113 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | * LOCKT operation |
| 6115 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6116 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6117 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6118 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6119 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6120 | struct nfsd4_lockt *lockt = &u->lockt; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6121 | struct file_lock *file_lock = NULL; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6122 | struct nfs4_lockowner *lo = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6123 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 6124 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 6126 | if (locks_in_grace(SVC_NET(rqstp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6127 | return nfserr_grace; |
| 6128 | |
| 6129 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 6130 | return nfserr_inval; |
| 6131 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6132 | if (!nfsd4_has_session(cstate)) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 6133 | status = lookup_clientid(&lockt->lt_clientid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6134 | if (status) |
| 6135 | goto out; |
| 6136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 6138 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6140 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6141 | file_lock = locks_alloc_lock(); |
| 6142 | if (!file_lock) { |
| 6143 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6144 | status = nfserr_jukebox; |
| 6145 | goto out; |
| 6146 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 6147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6148 | switch (lockt->lt_type) { |
| 6149 | case NFS4_READ_LT: |
| 6150 | case NFS4_READW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6151 | file_lock->fl_type = F_RDLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6152 | break; |
| 6153 | case NFS4_WRITE_LT: |
| 6154 | case NFS4_WRITEW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6155 | file_lock->fl_type = F_WRLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6156 | break; |
| 6157 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 6158 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6159 | status = nfserr_inval; |
| 6160 | goto out; |
| 6161 | } |
| 6162 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6163 | lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6164 | if (lo) |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6165 | file_lock->fl_owner = (fl_owner_t)lo; |
| 6166 | file_lock->fl_pid = current->tgid; |
| 6167 | file_lock->fl_flags = FL_POSIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6169 | file_lock->fl_start = lockt->lt_offset; |
| 6170 | 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] | 6171 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6172 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6173 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6174 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6175 | if (status) |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6176 | goto out; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6177 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6178 | if (file_lock->fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | status = nfserr_denied; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6180 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6181 | } |
| 6182 | out: |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6183 | if (lo) |
| 6184 | nfs4_put_stateowner(&lo->lo_owner); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6185 | if (file_lock) |
| 6186 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6187 | return status; |
| 6188 | } |
| 6189 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6190 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6191 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6192 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6194 | struct nfsd4_locku *locku = &u->locku; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6195 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6196 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6197 | struct file_lock *file_lock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6198 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6199 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6200 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 6201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6202 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 6203 | (long long) locku->lu_offset, |
| 6204 | (long long) locku->lu_length); |
| 6205 | |
| 6206 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 6207 | return nfserr_inval; |
| 6208 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6209 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6210 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 6211 | &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6212 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6213 | goto out; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6214 | filp = find_any_file(stp->st_stid.sc_file); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 6215 | if (!filp) { |
| 6216 | status = nfserr_lock_range; |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 6217 | goto put_stateid; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 6218 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6219 | file_lock = locks_alloc_lock(); |
| 6220 | if (!file_lock) { |
| 6221 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6222 | status = nfserr_jukebox; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 6223 | goto fput; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6224 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 6225 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6226 | file_lock->fl_type = F_UNLCK; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6227 | 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] | 6228 | file_lock->fl_pid = current->tgid; |
| 6229 | file_lock->fl_file = filp; |
| 6230 | file_lock->fl_flags = FL_POSIX; |
| 6231 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 6232 | file_lock->fl_start = locku->lu_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6233 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6234 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 6235 | locku->lu_length); |
| 6236 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6237 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6238 | err = vfs_lock_file(filp, F_SETLK, file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6239 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6240 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6241 | goto out_nfserr; |
| 6242 | } |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6243 | nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 6244 | fput: |
| 6245 | fput(filp); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 6246 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6247 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 6248 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6250 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6251 | if (file_lock) |
| 6252 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6253 | return status; |
| 6254 | |
| 6255 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6256 | status = nfserrno(err); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 6257 | goto fput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6258 | } |
| 6259 | |
| 6260 | /* |
| 6261 | * returns |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6262 | * true: locks held by lockowner |
| 6263 | * false: no locks held by lockowner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6265 | static bool |
| 6266 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6267 | { |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6268 | struct file_lock *fl; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6269 | int status = false; |
| 6270 | struct file *filp = find_any_file(fp); |
| 6271 | struct inode *inode; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6272 | struct file_lock_context *flctx; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6273 | |
| 6274 | if (!filp) { |
| 6275 | /* Any valid lock stateid should have some sort of access */ |
| 6276 | WARN_ON_ONCE(1); |
| 6277 | return status; |
| 6278 | } |
| 6279 | |
| 6280 | inode = file_inode(filp); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6281 | flctx = inode->i_flctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6282 | |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6283 | if (flctx && !list_empty_careful(&flctx->flc_posix)) { |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 6284 | spin_lock(&flctx->flc_lock); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6285 | list_for_each_entry(fl, &flctx->flc_posix, fl_list) { |
| 6286 | if (fl->fl_owner == (fl_owner_t)lowner) { |
| 6287 | status = true; |
| 6288 | break; |
| 6289 | } |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 6290 | } |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 6291 | spin_unlock(&flctx->flc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6292 | } |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6293 | fput(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6294 | return status; |
| 6295 | } |
| 6296 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6297 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 6298 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 6299 | struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6300 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6301 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6302 | struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6303 | clientid_t *clid = &rlockowner->rl_clientid; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6304 | struct nfs4_stateowner *sop; |
| 6305 | struct nfs4_lockowner *lo = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6306 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6307 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6308 | unsigned int hashval = ownerstr_hashval(owner); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6309 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 6310 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6311 | struct nfs4_client *clp; |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 6312 | LIST_HEAD (reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6313 | |
| 6314 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 6315 | clid->cl_boot, clid->cl_id); |
| 6316 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 6317 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6318 | if (status) |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 6319 | return status; |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6320 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6321 | clp = cstate->clp; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 6322 | /* Find the matching lock stateowner */ |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6323 | spin_lock(&clp->cl_lock); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6324 | list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval], |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6325 | so_strhash) { |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6326 | |
| 6327 | if (sop->so_is_open_owner || !same_owner_str(sop, owner)) |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 6328 | continue; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 6329 | |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6330 | /* see if there are still any locks associated with it */ |
| 6331 | lo = lockowner(sop); |
| 6332 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { |
| 6333 | if (check_for_locks(stp->st_stid.sc_file, lo)) { |
| 6334 | status = nfserr_locks_held; |
| 6335 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 6336 | return status; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6337 | } |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 6338 | } |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6339 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 6340 | nfs4_get_stateowner(sop); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6341 | break; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 6342 | } |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 6343 | if (!lo) { |
| 6344 | spin_unlock(&clp->cl_lock); |
| 6345 | return status; |
| 6346 | } |
| 6347 | |
| 6348 | unhash_lockowner_locked(lo); |
| 6349 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 6350 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
| 6351 | struct nfs4_ol_stateid, |
| 6352 | st_perstateowner); |
| 6353 | WARN_ON(!unhash_lock_stateid(stp)); |
| 6354 | put_ol_stateid_locked(stp, &reaplist); |
| 6355 | } |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6356 | spin_unlock(&clp->cl_lock); |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 6357 | free_ol_stateid_reaplist(&reaplist); |
| 6358 | nfs4_put_stateowner(&lo->lo_owner); |
| 6359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6360 | return status; |
| 6361 | } |
| 6362 | |
| 6363 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 6364 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6365 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 6366 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6367 | } |
| 6368 | |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 6369 | bool |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6370 | nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 6371 | { |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 6372 | struct nfs4_client_reclaim *crp; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 6373 | |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6374 | crp = nfsd4_find_reclaim_client(name, nn); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 6375 | return (crp && crp->cr_clp); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 6376 | } |
| 6377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6378 | /* |
| 6379 | * failure => all reset bets are off, nfserr_no_grace... |
| 6380 | */ |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 6381 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6382 | nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | { |
| 6384 | unsigned int strhashval; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 6385 | struct nfs4_client_reclaim *crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6386 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 6387 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
| 6388 | crp = alloc_reclaim(); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 6389 | if (crp) { |
| 6390 | strhashval = clientstr_hashval(name); |
| 6391 | INIT_LIST_HEAD(&crp->cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6392 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 6393 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 6394 | crp->cr_clp = NULL; |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6395 | nn->reclaim_str_hashtbl_size++; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 6396 | } |
| 6397 | return crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | } |
| 6399 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 6400 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6401 | 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] | 6402 | { |
| 6403 | list_del(&crp->cr_strhash); |
| 6404 | kfree(crp); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6405 | nn->reclaim_str_hashtbl_size--; |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 6406 | } |
| 6407 | |
| 6408 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6409 | nfs4_release_reclaim(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6410 | { |
| 6411 | struct nfs4_client_reclaim *crp = NULL; |
| 6412 | int i; |
| 6413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6414 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6415 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 6416 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6417 | struct nfs4_client_reclaim, cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6418 | nfs4_remove_reclaim_record(crp, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6419 | } |
| 6420 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 6421 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6422 | } |
| 6423 | |
| 6424 | /* |
| 6425 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 6426 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6427 | nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6428 | { |
| 6429 | unsigned int strhashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6430 | struct nfs4_client_reclaim *crp = NULL; |
| 6431 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 6432 | dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6433 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 6434 | strhashval = clientstr_hashval(recdir); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6435 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 6436 | if (same_name(crp->cr_recdir, recdir)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6437 | return crp; |
| 6438 | } |
| 6439 | } |
| 6440 | return NULL; |
| 6441 | } |
| 6442 | |
| 6443 | /* |
| 6444 | * Called from OPEN. Look for clientid in reclaim list. |
| 6445 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6446 | __be32 |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 6447 | nfs4_check_open_reclaim(clientid_t *clid, |
| 6448 | struct nfsd4_compound_state *cstate, |
| 6449 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | { |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 6451 | __be32 status; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 6452 | |
| 6453 | /* find clientid in conf_id_hashtbl */ |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 6454 | status = lookup_clientid(clid, cstate, nn); |
| 6455 | if (status) |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 6456 | return nfserr_reclaim_bad; |
| 6457 | |
Jeff Layton | 3b3e7b7 | 2014-09-12 16:40:22 -0400 | [diff] [blame] | 6458 | if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags)) |
| 6459 | return nfserr_no_grace; |
| 6460 | |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 6461 | if (nfsd4_client_record_check(cstate->clp)) |
| 6462 | return nfserr_reclaim_bad; |
| 6463 | |
| 6464 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6465 | } |
| 6466 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 6467 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6468 | static inline void |
| 6469 | put_client(struct nfs4_client *clp) |
| 6470 | { |
| 6471 | atomic_dec(&clp->cl_refcount); |
| 6472 | } |
| 6473 | |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6474 | static struct nfs4_client * |
| 6475 | nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) |
| 6476 | { |
| 6477 | struct nfs4_client *clp; |
| 6478 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6479 | nfsd_net_id); |
| 6480 | |
| 6481 | if (!nfsd_netns_ready(nn)) |
| 6482 | return NULL; |
| 6483 | |
| 6484 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6485 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) |
| 6486 | return clp; |
| 6487 | } |
| 6488 | return NULL; |
| 6489 | } |
| 6490 | |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 6491 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6492 | nfsd_inject_print_clients(void) |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 6493 | { |
| 6494 | struct nfs4_client *clp; |
| 6495 | u64 count = 0; |
| 6496 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6497 | nfsd_net_id); |
| 6498 | char buf[INET6_ADDRSTRLEN]; |
| 6499 | |
| 6500 | if (!nfsd_netns_ready(nn)) |
| 6501 | return 0; |
| 6502 | |
| 6503 | spin_lock(&nn->client_lock); |
| 6504 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6505 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
| 6506 | pr_info("NFS Client: %s\n", buf); |
| 6507 | ++count; |
| 6508 | } |
| 6509 | spin_unlock(&nn->client_lock); |
| 6510 | |
| 6511 | return count; |
| 6512 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 6513 | |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 6514 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6515 | nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size) |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 6516 | { |
| 6517 | u64 count = 0; |
| 6518 | struct nfs4_client *clp; |
| 6519 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6520 | nfsd_net_id); |
| 6521 | |
| 6522 | if (!nfsd_netns_ready(nn)) |
| 6523 | return count; |
| 6524 | |
| 6525 | spin_lock(&nn->client_lock); |
| 6526 | clp = nfsd_find_client(addr, addr_size); |
| 6527 | if (clp) { |
| 6528 | if (mark_client_expired_locked(clp) == nfs_ok) |
| 6529 | ++count; |
| 6530 | else |
| 6531 | clp = NULL; |
| 6532 | } |
| 6533 | spin_unlock(&nn->client_lock); |
| 6534 | |
| 6535 | if (clp) |
| 6536 | expire_client(clp); |
| 6537 | |
| 6538 | return count; |
| 6539 | } |
| 6540 | |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 6541 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6542 | nfsd_inject_forget_clients(u64 max) |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 6543 | { |
| 6544 | u64 count = 0; |
| 6545 | struct nfs4_client *clp, *next; |
| 6546 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6547 | nfsd_net_id); |
| 6548 | LIST_HEAD(reaplist); |
| 6549 | |
| 6550 | if (!nfsd_netns_ready(nn)) |
| 6551 | return count; |
| 6552 | |
| 6553 | spin_lock(&nn->client_lock); |
| 6554 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 6555 | if (mark_client_expired_locked(clp) == nfs_ok) { |
| 6556 | list_add(&clp->cl_lru, &reaplist); |
| 6557 | if (max != 0 && ++count >= max) |
| 6558 | break; |
| 6559 | } |
| 6560 | } |
| 6561 | spin_unlock(&nn->client_lock); |
| 6562 | |
| 6563 | list_for_each_entry_safe(clp, next, &reaplist, cl_lru) |
| 6564 | expire_client(clp); |
| 6565 | |
| 6566 | return count; |
| 6567 | } |
| 6568 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6569 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, |
| 6570 | const char *type) |
| 6571 | { |
| 6572 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 6573 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6574 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
| 6575 | } |
| 6576 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6577 | static void |
| 6578 | nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst, |
| 6579 | struct list_head *collect) |
| 6580 | { |
| 6581 | struct nfs4_client *clp = lst->st_stid.sc_client; |
| 6582 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6583 | nfsd_net_id); |
| 6584 | |
| 6585 | if (!collect) |
| 6586 | return; |
| 6587 | |
| 6588 | lockdep_assert_held(&nn->client_lock); |
| 6589 | atomic_inc(&clp->cl_refcount); |
| 6590 | list_add(&lst->st_locks, collect); |
| 6591 | } |
| 6592 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6593 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 6594 | struct list_head *collect, |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6595 | bool (*func)(struct nfs4_ol_stateid *)) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6596 | { |
| 6597 | struct nfs4_openowner *oop; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6598 | struct nfs4_ol_stateid *stp, *st_next; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6599 | struct nfs4_ol_stateid *lst, *lst_next; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6600 | u64 count = 0; |
| 6601 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6602 | spin_lock(&clp->cl_lock); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6603 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6604 | list_for_each_entry_safe(stp, st_next, |
| 6605 | &oop->oo_owner.so_stateids, st_perstateowner) { |
| 6606 | list_for_each_entry_safe(lst, lst_next, |
| 6607 | &stp->st_locks, st_locks) { |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 6608 | if (func) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6609 | if (func(lst)) |
| 6610 | nfsd_inject_add_lock_to_list(lst, |
| 6611 | collect); |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 6612 | } |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6613 | ++count; |
| 6614 | /* |
| 6615 | * Despite the fact that these functions deal |
| 6616 | * with 64-bit integers for "count", we must |
| 6617 | * ensure that it doesn't blow up the |
| 6618 | * clp->cl_refcount. Throw a warning if we |
| 6619 | * start to approach INT_MAX here. |
| 6620 | */ |
| 6621 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 6622 | if (count == max) |
| 6623 | goto out; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6624 | } |
| 6625 | } |
| 6626 | } |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6627 | out: |
| 6628 | spin_unlock(&clp->cl_lock); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6629 | |
| 6630 | return count; |
| 6631 | } |
| 6632 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6633 | static u64 |
| 6634 | nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect, |
| 6635 | u64 max) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6636 | { |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6637 | return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6638 | } |
| 6639 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6640 | static u64 |
| 6641 | nfsd_print_client_locks(struct nfs4_client *clp) |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6642 | { |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6643 | u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6644 | nfsd_print_count(clp, count, "locked files"); |
| 6645 | return count; |
| 6646 | } |
| 6647 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6648 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6649 | nfsd_inject_print_locks(void) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6650 | { |
| 6651 | struct nfs4_client *clp; |
| 6652 | u64 count = 0; |
| 6653 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6654 | nfsd_net_id); |
| 6655 | |
| 6656 | if (!nfsd_netns_ready(nn)) |
| 6657 | return 0; |
| 6658 | |
| 6659 | spin_lock(&nn->client_lock); |
| 6660 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 6661 | count += nfsd_print_client_locks(clp); |
| 6662 | spin_unlock(&nn->client_lock); |
| 6663 | |
| 6664 | return count; |
| 6665 | } |
| 6666 | |
| 6667 | static void |
| 6668 | nfsd_reap_locks(struct list_head *reaplist) |
| 6669 | { |
| 6670 | struct nfs4_client *clp; |
| 6671 | struct nfs4_ol_stateid *stp, *next; |
| 6672 | |
| 6673 | list_for_each_entry_safe(stp, next, reaplist, st_locks) { |
| 6674 | list_del_init(&stp->st_locks); |
| 6675 | clp = stp->st_stid.sc_client; |
| 6676 | nfs4_put_stid(&stp->st_stid); |
| 6677 | put_client(clp); |
| 6678 | } |
| 6679 | } |
| 6680 | |
| 6681 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6682 | 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] | 6683 | { |
| 6684 | unsigned int count = 0; |
| 6685 | struct nfs4_client *clp; |
| 6686 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6687 | nfsd_net_id); |
| 6688 | LIST_HEAD(reaplist); |
| 6689 | |
| 6690 | if (!nfsd_netns_ready(nn)) |
| 6691 | return count; |
| 6692 | |
| 6693 | spin_lock(&nn->client_lock); |
| 6694 | clp = nfsd_find_client(addr, addr_size); |
| 6695 | if (clp) |
| 6696 | count = nfsd_collect_client_locks(clp, &reaplist, 0); |
| 6697 | spin_unlock(&nn->client_lock); |
| 6698 | nfsd_reap_locks(&reaplist); |
| 6699 | return count; |
| 6700 | } |
| 6701 | |
| 6702 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6703 | nfsd_inject_forget_locks(u64 max) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6704 | { |
| 6705 | u64 count = 0; |
| 6706 | struct nfs4_client *clp; |
| 6707 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6708 | nfsd_net_id); |
| 6709 | LIST_HEAD(reaplist); |
| 6710 | |
| 6711 | if (!nfsd_netns_ready(nn)) |
| 6712 | return count; |
| 6713 | |
| 6714 | spin_lock(&nn->client_lock); |
| 6715 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6716 | count += nfsd_collect_client_locks(clp, &reaplist, max - count); |
| 6717 | if (max != 0 && count >= max) |
| 6718 | break; |
| 6719 | } |
| 6720 | spin_unlock(&nn->client_lock); |
| 6721 | nfsd_reap_locks(&reaplist); |
| 6722 | return count; |
| 6723 | } |
| 6724 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6725 | static u64 |
| 6726 | nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max, |
| 6727 | struct list_head *collect, |
| 6728 | void (*func)(struct nfs4_openowner *)) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6729 | { |
| 6730 | struct nfs4_openowner *oop, *next; |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6731 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6732 | nfsd_net_id); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6733 | u64 count = 0; |
| 6734 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6735 | lockdep_assert_held(&nn->client_lock); |
| 6736 | |
| 6737 | spin_lock(&clp->cl_lock); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6738 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6739 | if (func) { |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6740 | func(oop); |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6741 | if (collect) { |
| 6742 | atomic_inc(&clp->cl_refcount); |
| 6743 | list_add(&oop->oo_perclient, collect); |
| 6744 | } |
| 6745 | } |
| 6746 | ++count; |
| 6747 | /* |
| 6748 | * Despite the fact that these functions deal with |
| 6749 | * 64-bit integers for "count", we must ensure that |
| 6750 | * it doesn't blow up the clp->cl_refcount. Throw a |
| 6751 | * warning if we start to approach INT_MAX here. |
| 6752 | */ |
| 6753 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 6754 | if (count == max) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6755 | break; |
| 6756 | } |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6757 | spin_unlock(&clp->cl_lock); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6758 | |
| 6759 | return count; |
| 6760 | } |
| 6761 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6762 | static u64 |
| 6763 | nfsd_print_client_openowners(struct nfs4_client *clp) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6764 | { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6765 | u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL); |
| 6766 | |
| 6767 | nfsd_print_count(clp, count, "openowners"); |
| 6768 | return count; |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6769 | } |
| 6770 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6771 | static u64 |
| 6772 | nfsd_collect_client_openowners(struct nfs4_client *clp, |
| 6773 | struct list_head *collect, u64 max) |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6774 | { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6775 | return nfsd_foreach_client_openowner(clp, max, collect, |
| 6776 | unhash_openowner_locked); |
| 6777 | } |
| 6778 | |
| 6779 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6780 | nfsd_inject_print_openowners(void) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6781 | { |
| 6782 | struct nfs4_client *clp; |
| 6783 | u64 count = 0; |
| 6784 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6785 | nfsd_net_id); |
| 6786 | |
| 6787 | if (!nfsd_netns_ready(nn)) |
| 6788 | return 0; |
| 6789 | |
| 6790 | spin_lock(&nn->client_lock); |
| 6791 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 6792 | count += nfsd_print_client_openowners(clp); |
| 6793 | spin_unlock(&nn->client_lock); |
| 6794 | |
| 6795 | return count; |
| 6796 | } |
| 6797 | |
| 6798 | static void |
| 6799 | nfsd_reap_openowners(struct list_head *reaplist) |
| 6800 | { |
| 6801 | struct nfs4_client *clp; |
| 6802 | struct nfs4_openowner *oop, *next; |
| 6803 | |
| 6804 | list_for_each_entry_safe(oop, next, reaplist, oo_perclient) { |
| 6805 | list_del_init(&oop->oo_perclient); |
| 6806 | clp = oop->oo_owner.so_client; |
| 6807 | release_openowner(oop); |
| 6808 | put_client(clp); |
| 6809 | } |
| 6810 | } |
| 6811 | |
| 6812 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6813 | nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr, |
| 6814 | size_t addr_size) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6815 | { |
| 6816 | unsigned int count = 0; |
| 6817 | struct nfs4_client *clp; |
| 6818 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6819 | nfsd_net_id); |
| 6820 | LIST_HEAD(reaplist); |
| 6821 | |
| 6822 | if (!nfsd_netns_ready(nn)) |
| 6823 | return count; |
| 6824 | |
| 6825 | spin_lock(&nn->client_lock); |
| 6826 | clp = nfsd_find_client(addr, addr_size); |
| 6827 | if (clp) |
| 6828 | count = nfsd_collect_client_openowners(clp, &reaplist, 0); |
| 6829 | spin_unlock(&nn->client_lock); |
| 6830 | nfsd_reap_openowners(&reaplist); |
| 6831 | return count; |
| 6832 | } |
| 6833 | |
| 6834 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6835 | nfsd_inject_forget_openowners(u64 max) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6836 | { |
| 6837 | u64 count = 0; |
| 6838 | struct nfs4_client *clp; |
| 6839 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6840 | nfsd_net_id); |
| 6841 | LIST_HEAD(reaplist); |
| 6842 | |
| 6843 | if (!nfsd_netns_ready(nn)) |
| 6844 | return count; |
| 6845 | |
| 6846 | spin_lock(&nn->client_lock); |
| 6847 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6848 | count += nfsd_collect_client_openowners(clp, &reaplist, |
| 6849 | max - count); |
| 6850 | if (max != 0 && count >= max) |
| 6851 | break; |
| 6852 | } |
| 6853 | spin_unlock(&nn->client_lock); |
| 6854 | nfsd_reap_openowners(&reaplist); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6855 | return count; |
| 6856 | } |
| 6857 | |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6858 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
| 6859 | struct list_head *victims) |
| 6860 | { |
| 6861 | struct nfs4_delegation *dp, *next; |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6862 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6863 | nfsd_net_id); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6864 | u64 count = 0; |
| 6865 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6866 | lockdep_assert_held(&nn->client_lock); |
| 6867 | |
| 6868 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6869 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 6870 | if (victims) { |
| 6871 | /* |
| 6872 | * It's not safe to mess with delegations that have a |
| 6873 | * non-zero dl_time. They might have already been broken |
| 6874 | * and could be processed by the laundromat outside of |
| 6875 | * the state_lock. Just leave them be. |
| 6876 | */ |
| 6877 | if (dp->dl_time != 0) |
| 6878 | continue; |
| 6879 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6880 | atomic_inc(&clp->cl_refcount); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 6881 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 6882 | list_add(&dp->dl_recall_lru, victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 6883 | } |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6884 | ++count; |
| 6885 | /* |
| 6886 | * Despite the fact that these functions deal with |
| 6887 | * 64-bit integers for "count", we must ensure that |
| 6888 | * it doesn't blow up the clp->cl_refcount. Throw a |
| 6889 | * warning if we start to approach INT_MAX here. |
| 6890 | */ |
| 6891 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 6892 | if (count == max) |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6893 | break; |
| 6894 | } |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6895 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6896 | return count; |
| 6897 | } |
| 6898 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6899 | static u64 |
| 6900 | nfsd_print_client_delegations(struct nfs4_client *clp) |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6901 | { |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6902 | u64 count = nfsd_find_all_delegations(clp, 0, NULL); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6903 | |
| 6904 | nfsd_print_count(clp, count, "delegations"); |
| 6905 | return count; |
| 6906 | } |
| 6907 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6908 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6909 | nfsd_inject_print_delegations(void) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6910 | { |
| 6911 | struct nfs4_client *clp; |
| 6912 | u64 count = 0; |
| 6913 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6914 | nfsd_net_id); |
| 6915 | |
| 6916 | if (!nfsd_netns_ready(nn)) |
| 6917 | return 0; |
| 6918 | |
| 6919 | spin_lock(&nn->client_lock); |
| 6920 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 6921 | count += nfsd_print_client_delegations(clp); |
| 6922 | spin_unlock(&nn->client_lock); |
| 6923 | |
| 6924 | return count; |
| 6925 | } |
| 6926 | |
| 6927 | static void |
| 6928 | nfsd_forget_delegations(struct list_head *reaplist) |
| 6929 | { |
| 6930 | struct nfs4_client *clp; |
| 6931 | struct nfs4_delegation *dp, *next; |
| 6932 | |
| 6933 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 6934 | list_del_init(&dp->dl_recall_lru); |
| 6935 | clp = dp->dl_stid.sc_client; |
| 6936 | revoke_delegation(dp); |
| 6937 | put_client(clp); |
| 6938 | } |
| 6939 | } |
| 6940 | |
| 6941 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6942 | nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr, |
| 6943 | size_t addr_size) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6944 | { |
| 6945 | u64 count = 0; |
| 6946 | struct nfs4_client *clp; |
| 6947 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6948 | nfsd_net_id); |
| 6949 | LIST_HEAD(reaplist); |
| 6950 | |
| 6951 | if (!nfsd_netns_ready(nn)) |
| 6952 | return count; |
| 6953 | |
| 6954 | spin_lock(&nn->client_lock); |
| 6955 | clp = nfsd_find_client(addr, addr_size); |
| 6956 | if (clp) |
| 6957 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 6958 | spin_unlock(&nn->client_lock); |
| 6959 | |
| 6960 | nfsd_forget_delegations(&reaplist); |
| 6961 | return count; |
| 6962 | } |
| 6963 | |
| 6964 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6965 | nfsd_inject_forget_delegations(u64 max) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6966 | { |
| 6967 | u64 count = 0; |
| 6968 | struct nfs4_client *clp; |
| 6969 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6970 | nfsd_net_id); |
| 6971 | LIST_HEAD(reaplist); |
| 6972 | |
| 6973 | if (!nfsd_netns_ready(nn)) |
| 6974 | return count; |
| 6975 | |
| 6976 | spin_lock(&nn->client_lock); |
| 6977 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6978 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 6979 | if (max != 0 && count >= max) |
| 6980 | break; |
| 6981 | } |
| 6982 | spin_unlock(&nn->client_lock); |
| 6983 | nfsd_forget_delegations(&reaplist); |
| 6984 | return count; |
| 6985 | } |
| 6986 | |
| 6987 | static void |
| 6988 | nfsd_recall_delegations(struct list_head *reaplist) |
| 6989 | { |
| 6990 | struct nfs4_client *clp; |
| 6991 | struct nfs4_delegation *dp, *next; |
| 6992 | |
| 6993 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 6994 | list_del_init(&dp->dl_recall_lru); |
| 6995 | clp = dp->dl_stid.sc_client; |
| 6996 | /* |
| 6997 | * We skipped all entries that had a zero dl_time before, |
| 6998 | * so we can now reset the dl_time back to 0. If a delegation |
| 6999 | * break comes in now, then it won't make any difference since |
| 7000 | * we're recalling it either way. |
| 7001 | */ |
| 7002 | spin_lock(&state_lock); |
| 7003 | dp->dl_time = 0; |
| 7004 | spin_unlock(&state_lock); |
| 7005 | nfsd_break_one_deleg(dp); |
| 7006 | put_client(clp); |
| 7007 | } |
| 7008 | } |
| 7009 | |
| 7010 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7011 | nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr, |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7012 | size_t addr_size) |
| 7013 | { |
| 7014 | u64 count = 0; |
| 7015 | struct nfs4_client *clp; |
| 7016 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7017 | nfsd_net_id); |
| 7018 | LIST_HEAD(reaplist); |
| 7019 | |
| 7020 | if (!nfsd_netns_ready(nn)) |
| 7021 | return count; |
| 7022 | |
| 7023 | spin_lock(&nn->client_lock); |
| 7024 | clp = nfsd_find_client(addr, addr_size); |
| 7025 | if (clp) |
| 7026 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 7027 | spin_unlock(&nn->client_lock); |
| 7028 | |
| 7029 | nfsd_recall_delegations(&reaplist); |
| 7030 | return count; |
| 7031 | } |
| 7032 | |
| 7033 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7034 | nfsd_inject_recall_delegations(u64 max) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7035 | { |
| 7036 | u64 count = 0; |
| 7037 | struct nfs4_client *clp, *next; |
| 7038 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7039 | nfsd_net_id); |
| 7040 | LIST_HEAD(reaplist); |
| 7041 | |
| 7042 | if (!nfsd_netns_ready(nn)) |
| 7043 | return count; |
| 7044 | |
| 7045 | spin_lock(&nn->client_lock); |
| 7046 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 7047 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 7048 | if (max != 0 && ++count >= max) |
| 7049 | break; |
| 7050 | } |
| 7051 | spin_unlock(&nn->client_lock); |
| 7052 | nfsd_recall_delegations(&reaplist); |
| 7053 | return count; |
| 7054 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 7055 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
| 7056 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 7057 | /* |
| 7058 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 7059 | * client may quite reasonably hang on to a delegation as long as it has |
| 7060 | * the inode cached. This becomes an obvious problem the first time a |
| 7061 | * client's inode cache approaches the size of the server's total memory. |
| 7062 | * |
| 7063 | * For now we avoid this problem by imposing a hard limit on the number |
| 7064 | * of delegations, which varies according to the server's memory size. |
| 7065 | */ |
| 7066 | static void |
| 7067 | set_max_delegations(void) |
| 7068 | { |
| 7069 | /* |
| 7070 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 7071 | * estimates suggest that in the worst case (where every delegation |
| 7072 | * is for a different inode), a delegation could take about 1.5K, |
| 7073 | * giving a worst case usage of about 6% of memory. |
| 7074 | */ |
| 7075 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 7076 | } |
| 7077 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7078 | static int nfs4_state_create_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7079 | { |
| 7080 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 7081 | int i; |
| 7082 | |
| 7083 | nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 7084 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 7085 | if (!nn->conf_id_hashtbl) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7086 | goto err; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7087 | nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 7088 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 7089 | if (!nn->unconf_id_hashtbl) |
| 7090 | goto err_unconf_id; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7091 | nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) * |
| 7092 | SESSION_HASH_SIZE, GFP_KERNEL); |
| 7093 | if (!nn->sessionid_hashtbl) |
| 7094 | goto err_sessionid; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7095 | |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7096 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7097 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7098 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7099 | } |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7100 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 7101 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7102 | nn->conf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7103 | nn->unconf_name_tree = RB_ROOT; |
Vasily Averin | 81833de | 2017-11-13 07:25:40 +0300 | [diff] [blame] | 7104 | nn->boot_time = get_seconds(); |
| 7105 | nn->grace_ended = false; |
| 7106 | nn->nfsd4_manager.block_opens = true; |
| 7107 | INIT_LIST_HEAD(&nn->nfsd4_manager.list); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 7108 | INIT_LIST_HEAD(&nn->client_lru); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 7109 | INIT_LIST_HEAD(&nn->close_lru); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 7110 | INIT_LIST_HEAD(&nn->del_recall_lru); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 7111 | spin_lock_init(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7112 | |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 7113 | spin_lock_init(&nn->blocked_locks_lock); |
| 7114 | INIT_LIST_HEAD(&nn->blocked_locks_lru); |
| 7115 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7116 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7117 | get_net(net); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7118 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7119 | return 0; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7120 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7121 | err_sessionid: |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 7122 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7123 | err_unconf_id: |
| 7124 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7125 | err: |
| 7126 | return -ENOMEM; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7127 | } |
| 7128 | |
| 7129 | static void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7130 | nfs4_state_destroy_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7131 | { |
| 7132 | int i; |
| 7133 | struct nfs4_client *clp = NULL; |
| 7134 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 7135 | |
| 7136 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 7137 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 7138 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 7139 | destroy_client(clp); |
| 7140 | } |
| 7141 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7142 | |
Kinglong Mee | 2b90563 | 2014-03-26 22:09:30 +0800 | [diff] [blame] | 7143 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 7144 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 7145 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 7146 | destroy_client(clp); |
| 7147 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7148 | } |
| 7149 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7150 | kfree(nn->sessionid_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7151 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7152 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7153 | put_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7154 | } |
| 7155 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 7156 | int |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7157 | nfs4_state_start_net(struct net *net) |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 7158 | { |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 7159 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7160 | int ret; |
| 7161 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7162 | ret = nfs4_state_create_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7163 | if (ret) |
| 7164 | return ret; |
Jeff Layton | d4318ac | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 7165 | locks_start_grace(net, &nn->nfsd4_manager); |
| 7166 | nfsd4_client_tracking_init(net); |
Vasily Averin | 7e981a8 | 2017-11-06 16:46:04 +0300 | [diff] [blame] | 7167 | printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n", |
| 7168 | nn->nfsd4_grace, net->ns.inum); |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 7169 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7170 | return 0; |
| 7171 | } |
| 7172 | |
| 7173 | /* initialization to perform when the nfsd service is started: */ |
| 7174 | |
| 7175 | int |
| 7176 | nfs4_state_start(void) |
| 7177 | { |
| 7178 | int ret; |
| 7179 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7180 | ret = set_callback_cred(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7181 | if (ret) |
Kinglong Mee | f7d1ddb | 2017-02-05 09:57:07 +0800 | [diff] [blame] | 7182 | return ret; |
| 7183 | |
Jeff Layton | 51a5456 | 2015-08-20 07:17:01 -0400 | [diff] [blame] | 7184 | laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 7185 | if (laundry_wq == NULL) { |
| 7186 | ret = -ENOMEM; |
Kinglong Mee | f7d1ddb | 2017-02-05 09:57:07 +0800 | [diff] [blame] | 7187 | goto out_cleanup_cred; |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 7188 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7189 | ret = nfsd4_create_callback_queue(); |
| 7190 | if (ret) |
| 7191 | goto out_free_laundry; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7192 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 7193 | set_max_delegations(); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7194 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7195 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7196 | out_free_laundry: |
| 7197 | destroy_workqueue(laundry_wq); |
Kinglong Mee | f7d1ddb | 2017-02-05 09:57:07 +0800 | [diff] [blame] | 7198 | out_cleanup_cred: |
| 7199 | cleanup_callback_cred(); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7200 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7201 | } |
| 7202 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 7203 | void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7204 | nfs4_state_shutdown_net(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7205 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7206 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7207 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7208 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 7209 | struct nfsd4_blocked_lock *nbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7210 | |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7211 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 7212 | locks_end_grace(&nn->nfsd4_manager); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 7213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7214 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 7215 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 7216 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7217 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 7218 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7219 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7220 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 7221 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7222 | list_for_each_safe(pos, next, &reaplist) { |
| 7223 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7224 | list_del_init(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 7225 | put_clnt_odstate(dp->dl_clnt_odstate); |
Jeff Layton | afbda40 | 2014-08-09 10:22:41 -0400 | [diff] [blame] | 7226 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 7227 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7228 | } |
| 7229 | |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 7230 | BUG_ON(!list_empty(&reaplist)); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 7231 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 7232 | while (!list_empty(&nn->blocked_locks_lru)) { |
| 7233 | nbl = list_first_entry(&nn->blocked_locks_lru, |
| 7234 | struct nfsd4_blocked_lock, nbl_lru); |
| 7235 | list_move(&nbl->nbl_lru, &reaplist); |
| 7236 | list_del_init(&nbl->nbl_list); |
| 7237 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 7238 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 7239 | |
| 7240 | while (!list_empty(&reaplist)) { |
Naofumi Honda | 64ebe124 | 2017-11-09 10:57:16 -0500 | [diff] [blame] | 7241 | nbl = list_first_entry(&reaplist, |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 7242 | struct nfsd4_blocked_lock, nbl_lru); |
| 7243 | list_del_init(&nbl->nbl_lru); |
| 7244 | posix_unblock_lock(&nbl->nbl_lock); |
| 7245 | free_blocked_lock(nbl); |
| 7246 | } |
| 7247 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 7248 | nfsd4_client_tracking_exit(net); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7249 | nfs4_state_destroy_net(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7250 | } |
| 7251 | |
| 7252 | void |
| 7253 | nfs4_state_shutdown(void) |
| 7254 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 7255 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 7256 | nfsd4_destroy_callback_queue(); |
Kinglong Mee | f7d1ddb | 2017-02-05 09:57:07 +0800 | [diff] [blame] | 7257 | cleanup_callback_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7258 | } |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7259 | |
| 7260 | static void |
| 7261 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 7262 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7263 | if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid)) |
| 7264 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7265 | } |
| 7266 | |
| 7267 | static void |
| 7268 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 7269 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7270 | if (cstate->minorversion) { |
| 7271 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
| 7272 | SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
| 7273 | } |
| 7274 | } |
| 7275 | |
| 7276 | void |
| 7277 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 7278 | { |
| 7279 | CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7280 | } |
| 7281 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7282 | /* |
| 7283 | * functions to set current state id |
| 7284 | */ |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7285 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7286 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, |
| 7287 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7288 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7289 | put_stateid(cstate, &u->open_downgrade.od_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7290 | } |
| 7291 | |
| 7292 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7293 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, |
| 7294 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7295 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7296 | put_stateid(cstate, &u->open.op_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7297 | } |
| 7298 | |
| 7299 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7300 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, |
| 7301 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7302 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7303 | put_stateid(cstate, &u->close.cl_stateid); |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7304 | } |
| 7305 | |
| 7306 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7307 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, |
| 7308 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7309 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7310 | put_stateid(cstate, &u->lock.lk_resp_stateid); |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7311 | } |
| 7312 | |
| 7313 | /* |
| 7314 | * functions to consume current state id |
| 7315 | */ |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7316 | |
| 7317 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7318 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, |
| 7319 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7320 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7321 | get_stateid(cstate, &u->open_downgrade.od_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7322 | } |
| 7323 | |
| 7324 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7325 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, |
| 7326 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7327 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7328 | get_stateid(cstate, &u->delegreturn.dr_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7329 | } |
| 7330 | |
| 7331 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7332 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, |
| 7333 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7334 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7335 | get_stateid(cstate, &u->free_stateid.fr_stateid); |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7336 | } |
| 7337 | |
| 7338 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7339 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, |
| 7340 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7341 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7342 | get_stateid(cstate, &u->setattr.sa_stateid); |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7343 | } |
| 7344 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7345 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7346 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, |
| 7347 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7348 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7349 | get_stateid(cstate, &u->close.cl_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7350 | } |
| 7351 | |
| 7352 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7353 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, |
| 7354 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7355 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7356 | get_stateid(cstate, &u->locku.lu_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7357 | } |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7358 | |
| 7359 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7360 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, |
| 7361 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7362 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7363 | get_stateid(cstate, &u->read.rd_stateid); |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7364 | } |
| 7365 | |
| 7366 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7367 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, |
| 7368 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7369 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7370 | get_stateid(cstate, &u->write.wr_stateid); |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7371 | } |