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> |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 44 | #include <linux/hash.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" |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 53 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 54 | #define all_ones {{~0,~0},~0} |
| 55 | static const stateid_t one_stateid = { |
| 56 | .si_generation = ~0, |
| 57 | .si_opaque = all_ones, |
| 58 | }; |
| 59 | static const stateid_t zero_stateid = { |
| 60 | /* all fields zero */ |
| 61 | }; |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 62 | static const stateid_t currentstateid = { |
| 63 | .si_generation = 1, |
| 64 | }; |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 65 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 66 | static u64 current_sessionid = 1; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 67 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 68 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
| 69 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 70 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* forward declarations */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 73 | static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 75 | /* Locking: */ |
| 76 | |
| 77 | /* Currently used for almost all code touching nfsv4 state: */ |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 78 | static DEFINE_MUTEX(client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 80 | /* |
| 81 | * Currently used for the del_recall_lru and file hash table. In an |
| 82 | * effort to decrease the scope of the client_mutex, this spinlock may |
| 83 | * eventually cover more: |
| 84 | */ |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 85 | static DEFINE_SPINLOCK(state_lock); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 86 | |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 87 | static struct kmem_cache *openowner_slab; |
| 88 | static struct kmem_cache *lockowner_slab; |
| 89 | static struct kmem_cache *file_slab; |
| 90 | static struct kmem_cache *stateid_slab; |
| 91 | static struct kmem_cache *deleg_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | void |
| 94 | nfs4_lock_state(void) |
| 95 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 96 | mutex_lock(&client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 99 | static void free_session(struct nfsd4_session *); |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 100 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 101 | static bool is_session_dead(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 102 | { |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 103 | return ses->se_flags & NFS4_SESSION_DEAD; |
| 104 | } |
| 105 | |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 106 | static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) |
| 107 | { |
| 108 | if (atomic_read(&ses->se_ref) > ref_held_by_me) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 109 | return nfserr_jukebox; |
| 110 | ses->se_flags |= NFS4_SESSION_DEAD; |
| 111 | return nfs_ok; |
| 112 | } |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | void |
| 115 | nfs4_unlock_state(void) |
| 116 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 117 | mutex_unlock(&client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 120 | static bool is_client_expired(struct nfs4_client *clp) |
| 121 | { |
| 122 | return clp->cl_time == 0; |
| 123 | } |
| 124 | |
| 125 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
| 126 | { |
| 127 | if (atomic_read(&clp->cl_refcount)) |
| 128 | return nfserr_jukebox; |
| 129 | clp->cl_time = 0; |
| 130 | return nfs_ok; |
| 131 | } |
| 132 | |
| 133 | static __be32 mark_client_expired(struct nfs4_client *clp) |
| 134 | { |
| 135 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 136 | __be32 ret; |
| 137 | |
| 138 | spin_lock(&nn->client_lock); |
| 139 | ret = mark_client_expired_locked(clp); |
| 140 | spin_unlock(&nn->client_lock); |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | static __be32 get_client_locked(struct nfs4_client *clp) |
| 145 | { |
| 146 | if (is_client_expired(clp)) |
| 147 | return nfserr_expired; |
| 148 | atomic_inc(&clp->cl_refcount); |
| 149 | return nfs_ok; |
| 150 | } |
| 151 | |
| 152 | /* must be called under the client_lock */ |
| 153 | static inline void |
| 154 | renew_client_locked(struct nfs4_client *clp) |
| 155 | { |
| 156 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 157 | |
| 158 | if (is_client_expired(clp)) { |
| 159 | WARN_ON(1); |
| 160 | printk("%s: client (clientid %08x/%08x) already expired\n", |
| 161 | __func__, |
| 162 | clp->cl_clientid.cl_boot, |
| 163 | clp->cl_clientid.cl_id); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | dprintk("renewing client (clientid %08x/%08x)\n", |
| 168 | clp->cl_clientid.cl_boot, |
| 169 | clp->cl_clientid.cl_id); |
| 170 | list_move_tail(&clp->cl_lru, &nn->client_lru); |
| 171 | clp->cl_time = get_seconds(); |
| 172 | } |
| 173 | |
| 174 | static inline void |
| 175 | renew_client(struct nfs4_client *clp) |
| 176 | { |
| 177 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 178 | |
| 179 | spin_lock(&nn->client_lock); |
| 180 | renew_client_locked(clp); |
| 181 | spin_unlock(&nn->client_lock); |
| 182 | } |
| 183 | |
Fengguang Wu | ba13843 | 2013-04-16 22:14:15 -0400 | [diff] [blame] | 184 | static void put_client_renew_locked(struct nfs4_client *clp) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 185 | { |
| 186 | if (!atomic_dec_and_test(&clp->cl_refcount)) |
| 187 | return; |
| 188 | if (!is_client_expired(clp)) |
| 189 | renew_client_locked(clp); |
| 190 | } |
| 191 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 192 | static void put_client_renew(struct nfs4_client *clp) |
| 193 | { |
| 194 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 195 | |
Jeff Layton | d6c249b | 2014-07-08 14:02:50 -0400 | [diff] [blame] | 196 | if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock)) |
| 197 | return; |
| 198 | if (!is_client_expired(clp)) |
| 199 | renew_client_locked(clp); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 200 | spin_unlock(&nn->client_lock); |
| 201 | } |
| 202 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 203 | static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) |
| 204 | { |
| 205 | __be32 status; |
| 206 | |
| 207 | if (is_session_dead(ses)) |
| 208 | return nfserr_badsession; |
| 209 | status = get_client_locked(ses->se_client); |
| 210 | if (status) |
| 211 | return status; |
| 212 | atomic_inc(&ses->se_ref); |
| 213 | return nfs_ok; |
| 214 | } |
| 215 | |
| 216 | static void nfsd4_put_session_locked(struct nfsd4_session *ses) |
| 217 | { |
| 218 | struct nfs4_client *clp = ses->se_client; |
| 219 | |
| 220 | if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) |
| 221 | free_session(ses); |
| 222 | put_client_renew_locked(clp); |
| 223 | } |
| 224 | |
| 225 | static void nfsd4_put_session(struct nfsd4_session *ses) |
| 226 | { |
| 227 | struct nfs4_client *clp = ses->se_client; |
| 228 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 229 | |
| 230 | spin_lock(&nn->client_lock); |
| 231 | nfsd4_put_session_locked(ses); |
| 232 | spin_unlock(&nn->client_lock); |
| 233 | } |
| 234 | |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | static inline u32 |
| 237 | opaque_hashval(const void *ptr, int nbytes) |
| 238 | { |
| 239 | unsigned char *cptr = (unsigned char *) ptr; |
| 240 | |
| 241 | u32 x = 0; |
| 242 | while (nbytes--) { |
| 243 | x *= 37; |
| 244 | x += *cptr++; |
| 245 | } |
| 246 | return x; |
| 247 | } |
| 248 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 249 | static void nfsd4_free_file(struct nfs4_file *f) |
| 250 | { |
| 251 | kmem_cache_free(file_slab, f); |
| 252 | } |
| 253 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 254 | static inline void |
| 255 | put_nfs4_file(struct nfs4_file *fi) |
| 256 | { |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 257 | might_lock(&state_lock); |
| 258 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 259 | if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) { |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 260 | hlist_del(&fi->fi_hash); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 261 | spin_unlock(&state_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 262 | nfsd4_free_file(fi); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 263 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static inline void |
| 267 | get_nfs4_file(struct nfs4_file *fi) |
| 268 | { |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 269 | atomic_inc(&fi->fi_ref); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 272 | static struct file * |
| 273 | __nfs4_get_fd(struct nfs4_file *f, int oflag) |
| 274 | { |
| 275 | if (f->fi_fds[oflag]) |
| 276 | return get_file(f->fi_fds[oflag]); |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | static struct file * |
| 281 | find_writeable_file_locked(struct nfs4_file *f) |
| 282 | { |
| 283 | struct file *ret; |
| 284 | |
| 285 | lockdep_assert_held(&f->fi_lock); |
| 286 | |
| 287 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 288 | if (!ret) |
| 289 | ret = __nfs4_get_fd(f, O_RDWR); |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | static struct file * |
| 294 | find_writeable_file(struct nfs4_file *f) |
| 295 | { |
| 296 | struct file *ret; |
| 297 | |
| 298 | spin_lock(&f->fi_lock); |
| 299 | ret = find_writeable_file_locked(f); |
| 300 | spin_unlock(&f->fi_lock); |
| 301 | |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | static struct file *find_readable_file_locked(struct nfs4_file *f) |
| 306 | { |
| 307 | struct file *ret; |
| 308 | |
| 309 | lockdep_assert_held(&f->fi_lock); |
| 310 | |
| 311 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 312 | if (!ret) |
| 313 | ret = __nfs4_get_fd(f, O_RDWR); |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | static struct file * |
| 318 | find_readable_file(struct nfs4_file *f) |
| 319 | { |
| 320 | struct file *ret; |
| 321 | |
| 322 | spin_lock(&f->fi_lock); |
| 323 | ret = find_readable_file_locked(f); |
| 324 | spin_unlock(&f->fi_lock); |
| 325 | |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | static struct file * |
| 330 | find_any_file(struct nfs4_file *f) |
| 331 | { |
| 332 | struct file *ret; |
| 333 | |
| 334 | spin_lock(&f->fi_lock); |
| 335 | ret = __nfs4_get_fd(f, O_RDWR); |
| 336 | if (!ret) { |
| 337 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 338 | if (!ret) |
| 339 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 340 | } |
| 341 | spin_unlock(&f->fi_lock); |
| 342 | return ret; |
| 343 | } |
| 344 | |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 345 | static atomic_long_t num_delegations; |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 346 | unsigned long max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * Open owner state (share locks) |
| 350 | */ |
| 351 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 352 | /* hash tables for lock and open owners */ |
| 353 | #define OWNER_HASH_BITS 8 |
| 354 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) |
| 355 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 356 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 357 | static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 358 | { |
| 359 | unsigned int ret; |
| 360 | |
| 361 | ret = opaque_hashval(ownername->data, ownername->len); |
| 362 | ret += clientid; |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 363 | return ret & OWNER_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 364 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 365 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 366 | /* hash table for nfs4_file */ |
| 367 | #define FILE_HASH_BITS 8 |
| 368 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
Shan Wei | 3507958 | 2011-01-14 17:35:59 +0800 | [diff] [blame] | 369 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 370 | static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 371 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 372 | return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0); |
| 373 | } |
| 374 | |
| 375 | static unsigned int file_hashval(struct knfsd_fh *fh) |
| 376 | { |
| 377 | return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1); |
| 378 | } |
| 379 | |
| 380 | static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2) |
| 381 | { |
| 382 | return fh1->fh_size == fh2->fh_size && |
| 383 | !memcmp(fh1->fh_base.fh_pad, |
| 384 | fh2->fh_base.fh_pad, |
| 385 | fh1->fh_size); |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 386 | } |
| 387 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 388 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 389 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 390 | static void |
| 391 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 392 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 393 | lockdep_assert_held(&fp->fi_lock); |
| 394 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 395 | if (access & NFS4_SHARE_ACCESS_WRITE) |
| 396 | atomic_inc(&fp->fi_access[O_WRONLY]); |
| 397 | if (access & NFS4_SHARE_ACCESS_READ) |
| 398 | atomic_inc(&fp->fi_access[O_RDONLY]); |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 399 | } |
| 400 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 401 | static __be32 |
| 402 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 403 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 404 | lockdep_assert_held(&fp->fi_lock); |
| 405 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 406 | /* Does this access mode make sense? */ |
| 407 | if (access & ~NFS4_SHARE_ACCESS_BOTH) |
| 408 | return nfserr_inval; |
| 409 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 410 | /* Does it conflict with a deny mode already set? */ |
| 411 | if ((access & fp->fi_share_deny) != 0) |
| 412 | return nfserr_share_denied; |
| 413 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 414 | __nfs4_file_get_access(fp, access); |
| 415 | return nfs_ok; |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 416 | } |
| 417 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 418 | static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) |
| 419 | { |
| 420 | /* Common case is that there is no deny mode. */ |
| 421 | if (deny) { |
| 422 | /* Does this deny mode make sense? */ |
| 423 | if (deny & ~NFS4_SHARE_DENY_BOTH) |
| 424 | return nfserr_inval; |
| 425 | |
| 426 | if ((deny & NFS4_SHARE_DENY_READ) && |
| 427 | atomic_read(&fp->fi_access[O_RDONLY])) |
| 428 | return nfserr_share_denied; |
| 429 | |
| 430 | if ((deny & NFS4_SHARE_DENY_WRITE) && |
| 431 | atomic_read(&fp->fi_access[O_WRONLY])) |
| 432 | return nfserr_share_denied; |
| 433 | } |
| 434 | return nfs_ok; |
| 435 | } |
| 436 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 437 | 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] | 438 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 439 | might_lock(&fp->fi_lock); |
| 440 | |
| 441 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { |
| 442 | struct file *f1 = NULL; |
| 443 | struct file *f2 = NULL; |
| 444 | |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 445 | swap(f1, fp->fi_fds[oflag]); |
J. Bruce Fields | 0c7c3e6 | 2013-03-28 20:37:14 -0400 | [diff] [blame] | 446 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 447 | swap(f2, fp->fi_fds[O_RDWR]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 448 | spin_unlock(&fp->fi_lock); |
| 449 | if (f1) |
| 450 | fput(f1); |
| 451 | if (f2) |
| 452 | fput(f2); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 456 | 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] | 457 | { |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 458 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
| 459 | |
| 460 | if (access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 461 | __nfs4_file_put_access(fp, O_WRONLY); |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 462 | if (access & NFS4_SHARE_ACCESS_READ) |
| 463 | __nfs4_file_put_access(fp, O_RDONLY); |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 464 | } |
| 465 | |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 466 | static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct |
| 467 | kmem_cache *slab) |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 468 | { |
| 469 | struct idr *stateids = &cl->cl_stateids; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 470 | struct nfs4_stid *stid; |
| 471 | int new_id; |
| 472 | |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 473 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 474 | if (!stid) |
| 475 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 476 | |
Jeff Layton | 398c33a | 2013-04-29 16:21:20 -0700 | [diff] [blame] | 477 | new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL); |
Tejun Heo | ebd6c70 | 2013-03-13 14:59:37 -0700 | [diff] [blame] | 478 | if (new_id < 0) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 479 | goto out_free; |
| 480 | stid->sc_client = cl; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 481 | stid->sc_stateid.si_opaque.so_id = new_id; |
| 482 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; |
| 483 | /* Will be incremented before return to client: */ |
Trond Myklebust | 72c0b0f | 2014-07-21 09:34:58 -0400 | [diff] [blame] | 484 | atomic_set(&stid->sc_count, 1); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 485 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 486 | /* |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 487 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 488 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 489 | * example, a stray write retransmission could be accepted by |
| 490 | * the server when it should have been rejected. Therefore, |
| 491 | * adopt a trick from the sctp code to attempt to maximize the |
| 492 | * amount of time until an id is reused, by ensuring they always |
| 493 | * "increase" (mod INT_MAX): |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 494 | */ |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 495 | return stid; |
| 496 | out_free: |
Wei Yongjun | 2c44a23 | 2013-04-09 14:15:31 +0800 | [diff] [blame] | 497 | kmem_cache_free(slab, stid); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 498 | return NULL; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 499 | } |
| 500 | |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 501 | static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp) |
| 502 | { |
| 503 | return openlockstateid(nfs4_alloc_stid(clp, stateid_slab)); |
| 504 | } |
| 505 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 506 | /* |
| 507 | * When we recall a delegation, we should be careful not to hand it |
| 508 | * out again straight away. |
| 509 | * To ensure this we keep a pair of bloom filters ('new' and 'old') |
| 510 | * in which the filehandles of recalled delegations are "stored". |
| 511 | * If a filehandle appear in either filter, a delegation is blocked. |
| 512 | * When a delegation is recalled, the filehandle is stored in the "new" |
| 513 | * filter. |
| 514 | * Every 30 seconds we swap the filters and clear the "new" one, |
| 515 | * unless both are empty of course. |
| 516 | * |
| 517 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the |
| 518 | * low 3 bytes as hash-table indices. |
| 519 | * |
| 520 | * 'state_lock', which is always held when block_delegations() is called, |
| 521 | * is used to manage concurrent access. Testing does not need the lock |
| 522 | * except when swapping the two filters. |
| 523 | */ |
| 524 | static struct bloom_pair { |
| 525 | int entries, old_entries; |
| 526 | time_t swap_time; |
| 527 | int new; /* index into 'set' */ |
| 528 | DECLARE_BITMAP(set[2], 256); |
| 529 | } blocked_delegations; |
| 530 | |
| 531 | static int delegation_blocked(struct knfsd_fh *fh) |
| 532 | { |
| 533 | u32 hash; |
| 534 | struct bloom_pair *bd = &blocked_delegations; |
| 535 | |
| 536 | if (bd->entries == 0) |
| 537 | return 0; |
| 538 | if (seconds_since_boot() - bd->swap_time > 30) { |
| 539 | spin_lock(&state_lock); |
| 540 | if (seconds_since_boot() - bd->swap_time > 30) { |
| 541 | bd->entries -= bd->old_entries; |
| 542 | bd->old_entries = bd->entries; |
| 543 | memset(bd->set[bd->new], 0, |
| 544 | sizeof(bd->set[0])); |
| 545 | bd->new = 1-bd->new; |
| 546 | bd->swap_time = seconds_since_boot(); |
| 547 | } |
| 548 | spin_unlock(&state_lock); |
| 549 | } |
| 550 | hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0); |
| 551 | if (test_bit(hash&255, bd->set[0]) && |
| 552 | test_bit((hash>>8)&255, bd->set[0]) && |
| 553 | test_bit((hash>>16)&255, bd->set[0])) |
| 554 | return 1; |
| 555 | |
| 556 | if (test_bit(hash&255, bd->set[1]) && |
| 557 | test_bit((hash>>8)&255, bd->set[1]) && |
| 558 | test_bit((hash>>16)&255, bd->set[1])) |
| 559 | return 1; |
| 560 | |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static void block_delegations(struct knfsd_fh *fh) |
| 565 | { |
| 566 | u32 hash; |
| 567 | struct bloom_pair *bd = &blocked_delegations; |
| 568 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 569 | lockdep_assert_held(&state_lock); |
| 570 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 571 | hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0); |
| 572 | |
| 573 | __set_bit(hash&255, bd->set[bd->new]); |
| 574 | __set_bit((hash>>8)&255, bd->set[bd->new]); |
| 575 | __set_bit((hash>>16)&255, bd->set[bd->new]); |
| 576 | if (bd->entries == 0) |
| 577 | bd->swap_time = seconds_since_boot(); |
| 578 | bd->entries += 1; |
| 579 | } |
| 580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | static struct nfs4_delegation * |
Jeff Layton | f9416e2 | 2014-07-25 07:34:23 -0400 | [diff] [blame] | 582 | alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | { |
| 584 | struct nfs4_delegation *dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 585 | long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
| 587 | dprintk("NFSD alloc_init_deleg\n"); |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 588 | n = atomic_long_inc_return(&num_delegations); |
| 589 | if (n < 0 || n > max_delegations) |
| 590 | goto out_dec; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 591 | if (delegation_blocked(¤t_fh->fh_handle)) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 592 | goto out_dec; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 593 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab)); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 594 | if (dp == NULL) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 595 | goto out_dec; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 596 | /* |
| 597 | * delegation seqid's are never incremented. The 4.1 special |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 598 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 599 | * 0 anyway just for consistency and use 1: |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 600 | */ |
| 601 | dp->dl_stid.sc_stateid.si_generation = 1; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 602 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 603 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 605 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
J. Bruce Fields | 6c02eaa | 2009-02-02 17:30:51 -0500 | [diff] [blame] | 606 | fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 607 | INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | return dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 609 | out_dec: |
| 610 | atomic_long_dec(&num_delegations); |
| 611 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } |
| 613 | |
J. Bruce Fields | 68a3396 | 2013-03-21 11:21:50 -0400 | [diff] [blame] | 614 | static void remove_stid(struct nfs4_stid *s) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 615 | { |
| 616 | struct idr *stateids = &s->sc_client->cl_stateids; |
| 617 | |
| 618 | idr_remove(stateids, s->sc_stateid.si_opaque.so_id); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 619 | } |
| 620 | |
Benny Halevy | 9857df8 | 2013-10-14 13:44:52 +0300 | [diff] [blame] | 621 | static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s) |
| 622 | { |
| 623 | kmem_cache_free(slab, s); |
| 624 | } |
| 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | void |
| 627 | nfs4_put_delegation(struct nfs4_delegation *dp) |
| 628 | { |
Trond Myklebust | 72c0b0f | 2014-07-21 09:34:58 -0400 | [diff] [blame] | 629 | if (atomic_dec_and_test(&dp->dl_stid.sc_count)) { |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 630 | if (dp->dl_file) |
| 631 | put_nfs4_file(dp->dl_file); |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 632 | remove_stid(&dp->dl_stid); |
Benny Halevy | 9857df8 | 2013-10-14 13:44:52 +0300 | [diff] [blame] | 633 | nfs4_free_stid(deleg_slab, &dp->dl_stid); |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 634 | atomic_long_dec(&num_delegations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 638 | static void nfs4_put_deleg_lease(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 640 | lockdep_assert_held(&state_lock); |
| 641 | |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 642 | if (!fp->fi_lease) |
| 643 | return; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 644 | if (atomic_dec_and_test(&fp->fi_delegees)) { |
| 645 | vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease); |
| 646 | fp->fi_lease = NULL; |
J. Bruce Fields | 4ee6362 | 2011-04-15 18:08:26 -0400 | [diff] [blame] | 647 | fput(fp->fi_deleg_file); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 648 | fp->fi_deleg_file = NULL; |
| 649 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
| 651 | |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 652 | static void unhash_stid(struct nfs4_stid *s) |
| 653 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 654 | s->sc_type = 0; |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 655 | } |
| 656 | |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 657 | static void |
| 658 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 659 | { |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 660 | lockdep_assert_held(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 661 | lockdep_assert_held(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 662 | |
Benny Halevy | 3fb87d1 | 2014-05-30 09:09:31 -0400 | [diff] [blame] | 663 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 664 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
| 665 | list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations); |
| 666 | } |
| 667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | static void |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 669 | unhash_delegation_locked(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | { |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 671 | struct nfs4_file *fp = dp->dl_file; |
| 672 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 673 | lockdep_assert_held(&state_lock); |
| 674 | |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 675 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
Jeff Layton | d55a166 | 2014-07-22 13:52:06 -0400 | [diff] [blame] | 676 | /* Ensure that deleg break won't try to requeue it */ |
| 677 | ++dp->dl_time; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 678 | spin_lock(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 679 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | list_del_init(&dp->dl_recall_lru); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 681 | list_del_init(&dp->dl_perfile); |
| 682 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 683 | if (fp) |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 684 | nfs4_put_deleg_lease(fp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 685 | } |
| 686 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 687 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 688 | { |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 689 | spin_lock(&state_lock); |
| 690 | unhash_delegation_locked(dp); |
| 691 | spin_unlock(&state_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 692 | nfs4_put_delegation(dp); |
| 693 | } |
| 694 | |
| 695 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 696 | { |
| 697 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 698 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 699 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
| 700 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 701 | if (clp->cl_minorversion == 0) |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 702 | nfs4_put_delegation(dp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 703 | else { |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 704 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 705 | spin_lock(&clp->cl_lock); |
| 706 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 707 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | /* |
| 712 | * SETCLIENTID state |
| 713 | */ |
| 714 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 715 | static unsigned int clientid_hashval(u32 id) |
| 716 | { |
| 717 | return id & CLIENT_HASH_MASK; |
| 718 | } |
| 719 | |
| 720 | static unsigned int clientstr_hashval(const char *name) |
| 721 | { |
| 722 | return opaque_hashval(name, 8) & CLIENT_HASH_MASK; |
| 723 | } |
| 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | /* |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 726 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 727 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 728 | * only what share bits are currently in force, but also what |
| 729 | * combinations of share bits previous opens have used. This allows us |
| 730 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 731 | * return an error if the client attempt to downgrade to a combination |
| 732 | * of share bits not explicable by closing some of its previous opens. |
| 733 | * |
| 734 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 735 | * track of access/deny bit combinations; so, e.g., we allow: |
| 736 | * |
| 737 | * OPEN allow read, deny write |
| 738 | * OPEN allow both, deny none |
| 739 | * DOWNGRADE allow read, deny none |
| 740 | * |
| 741 | * which we should reject. |
| 742 | */ |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 743 | static unsigned int |
| 744 | bmap_to_share_mode(unsigned long bmap) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 745 | int i; |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 746 | unsigned int access = 0; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 747 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 748 | for (i = 1; i < 4; i++) { |
| 749 | if (test_bit(i, &bmap)) |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 750 | access |= i; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 751 | } |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 752 | return access; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 753 | } |
| 754 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 755 | /* set share access for a given stateid */ |
| 756 | static inline void |
| 757 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 758 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 759 | unsigned char mask = 1 << access; |
| 760 | |
| 761 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 762 | stp->st_access_bmap |= mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | /* clear share access for a given stateid */ |
| 766 | static inline void |
| 767 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 768 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 769 | unsigned char mask = 1 << access; |
| 770 | |
| 771 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 772 | stp->st_access_bmap &= ~mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* test whether a given stateid has access */ |
| 776 | static inline bool |
| 777 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 778 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 779 | unsigned char mask = 1 << access; |
| 780 | |
| 781 | return (bool)(stp->st_access_bmap & mask); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 782 | } |
| 783 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 784 | /* set share deny for a given stateid */ |
| 785 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 786 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 787 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 788 | unsigned char mask = 1 << deny; |
| 789 | |
| 790 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 791 | stp->st_deny_bmap |= mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* clear share deny for a given stateid */ |
| 795 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 796 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 797 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 798 | unsigned char mask = 1 << deny; |
| 799 | |
| 800 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 801 | stp->st_deny_bmap &= ~mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /* test whether a given stateid is denying specific access */ |
| 805 | static inline bool |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 806 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 807 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 808 | unsigned char mask = 1 << deny; |
| 809 | |
| 810 | return (bool)(stp->st_deny_bmap & mask); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | static int nfs4_access_to_omode(u32 access) |
| 814 | { |
J. Bruce Fields | 8f34a43 | 2010-09-02 15:23:16 -0400 | [diff] [blame] | 815 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 816 | case NFS4_SHARE_ACCESS_READ: |
| 817 | return O_RDONLY; |
| 818 | case NFS4_SHARE_ACCESS_WRITE: |
| 819 | return O_WRONLY; |
| 820 | case NFS4_SHARE_ACCESS_BOTH: |
| 821 | return O_RDWR; |
| 822 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 823 | WARN_ON_ONCE(1); |
| 824 | return O_RDONLY; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 825 | } |
| 826 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 827 | /* |
| 828 | * A stateid that had a deny mode associated with it is being released |
| 829 | * or downgraded. Recalculate the deny mode on the file. |
| 830 | */ |
| 831 | static void |
| 832 | recalculate_deny_mode(struct nfs4_file *fp) |
| 833 | { |
| 834 | struct nfs4_ol_stateid *stp; |
| 835 | |
| 836 | spin_lock(&fp->fi_lock); |
| 837 | fp->fi_share_deny = 0; |
| 838 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) |
| 839 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); |
| 840 | spin_unlock(&fp->fi_lock); |
| 841 | } |
| 842 | |
| 843 | static void |
| 844 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 845 | { |
| 846 | int i; |
| 847 | bool change = false; |
| 848 | |
| 849 | for (i = 1; i < 4; i++) { |
| 850 | if ((i & deny) != i) { |
| 851 | change = true; |
| 852 | clear_deny(i, stp); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | /* Recalculate per-file deny mode if there was a change */ |
| 857 | if (change) |
| 858 | recalculate_deny_mode(stp->st_file); |
| 859 | } |
| 860 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 861 | /* release all access and file references for a given stateid */ |
| 862 | static void |
| 863 | release_all_access(struct nfs4_ol_stateid *stp) |
| 864 | { |
| 865 | int i; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 866 | struct nfs4_file *fp = stp->st_file; |
| 867 | |
| 868 | if (fp && stp->st_deny_bmap != 0) |
| 869 | recalculate_deny_mode(fp); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 870 | |
| 871 | for (i = 1; i < 4; i++) { |
| 872 | if (test_access(i, stp)) |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 873 | nfs4_file_put_access(stp->st_file, i); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 874 | clear_access(i, stp); |
| 875 | } |
| 876 | } |
| 877 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 878 | static void unhash_generic_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 879 | { |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 880 | struct nfs4_file *fp = stp->st_file; |
| 881 | |
| 882 | spin_lock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 883 | list_del(&stp->st_perfile); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 884 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 885 | list_del(&stp->st_perstateowner); |
| 886 | } |
| 887 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 888 | static void close_generic_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 889 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 890 | release_all_access(stp); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 891 | } |
| 892 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 893 | static void free_generic_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 894 | { |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 895 | if (stp->st_file) |
| 896 | put_nfs4_file(stp->st_file); |
J. Bruce Fields | 68a3396 | 2013-03-21 11:21:50 -0400 | [diff] [blame] | 897 | remove_stid(&stp->st_stid); |
Benny Halevy | 9857df8 | 2013-10-14 13:44:52 +0300 | [diff] [blame] | 898 | nfs4_free_stid(stateid_slab, &stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 899 | } |
| 900 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 901 | static void __release_lock_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 902 | { |
| 903 | struct file *file; |
| 904 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 905 | list_del(&stp->st_locks); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 906 | unhash_generic_stateid(stp); |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 907 | unhash_stid(&stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 908 | file = find_any_file(stp->st_file); |
Trond Myklebust | e20fcf1 | 2014-07-10 14:07:27 -0400 | [diff] [blame] | 909 | if (file) |
| 910 | filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner)); |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 911 | close_generic_stateid(stp); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 912 | free_generic_stateid(stp); |
| 913 | } |
| 914 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 915 | static void unhash_lockowner(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 916 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 917 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 918 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 919 | list_del(&lo->lo_owner.so_strhash); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 920 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 921 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 922 | struct nfs4_ol_stateid, st_perstateowner); |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 923 | __release_lock_stateid(stp); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
Trond Myklebust | 50cc623 | 2014-04-18 14:44:03 -0400 | [diff] [blame] | 927 | static void nfs4_free_lockowner(struct nfs4_lockowner *lo) |
| 928 | { |
| 929 | kfree(lo->lo_owner.so_owner.data); |
| 930 | kmem_cache_free(lockowner_slab, lo); |
| 931 | } |
| 932 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 933 | static void release_lockowner(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 934 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 935 | unhash_lockowner(lo); |
| 936 | nfs4_free_lockowner(lo); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 937 | } |
| 938 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 939 | static void release_lockowner_if_empty(struct nfs4_lockowner *lo) |
| 940 | { |
| 941 | if (list_empty(&lo->lo_owner.so_stateids)) |
| 942 | release_lockowner(lo); |
| 943 | } |
| 944 | |
| 945 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 946 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 947 | struct nfs4_lockowner *lo; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 948 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 949 | lo = lockowner(stp->st_stateowner); |
| 950 | __release_lock_stateid(stp); |
| 951 | release_lockowner_if_empty(lo); |
| 952 | } |
| 953 | |
| 954 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp) |
| 955 | { |
| 956 | struct nfs4_ol_stateid *stp; |
| 957 | |
| 958 | while (!list_empty(&open_stp->st_locks)) { |
| 959 | stp = list_entry(open_stp->st_locks.next, |
| 960 | struct nfs4_ol_stateid, st_locks); |
| 961 | release_lock_stateid(stp); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 965 | static void unhash_open_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 966 | { |
| 967 | unhash_generic_stateid(stp); |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 968 | release_open_stateid_locks(stp); |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 969 | close_generic_stateid(stp); |
| 970 | } |
| 971 | |
| 972 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 973 | { |
| 974 | unhash_open_stateid(stp); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 975 | free_generic_stateid(stp); |
| 976 | } |
| 977 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 978 | static void unhash_openowner(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 979 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 980 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 981 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 982 | list_del(&oo->oo_owner.so_strhash); |
| 983 | list_del(&oo->oo_perclient); |
| 984 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 985 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 986 | struct nfs4_ol_stateid, st_perstateowner); |
J. Bruce Fields | f044ff8 | 2009-01-11 15:24:04 -0500 | [diff] [blame] | 987 | release_open_stateid(stp); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 991 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 992 | { |
| 993 | struct nfs4_ol_stateid *s = oo->oo_last_closed_stid; |
| 994 | |
| 995 | if (s) { |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 996 | free_generic_stateid(s); |
| 997 | oo->oo_last_closed_stid = NULL; |
| 998 | } |
| 999 | } |
| 1000 | |
Trond Myklebust | 50cc623 | 2014-04-18 14:44:03 -0400 | [diff] [blame] | 1001 | static void nfs4_free_openowner(struct nfs4_openowner *oo) |
| 1002 | { |
| 1003 | kfree(oo->oo_owner.so_owner.data); |
| 1004 | kmem_cache_free(openowner_slab, oo); |
| 1005 | } |
| 1006 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1007 | static void release_openowner(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1008 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1009 | unhash_openowner(oo); |
| 1010 | list_del(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1011 | release_last_closed_stateid(oo); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1012 | nfs4_free_openowner(oo); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1013 | } |
| 1014 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1015 | static inline int |
| 1016 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 1017 | { |
| 1018 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 1019 | |
| 1020 | return sid->sequence % SESSION_HASH_SIZE; |
| 1021 | } |
| 1022 | |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1023 | #ifdef NFSD_DEBUG |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1024 | static inline void |
| 1025 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1026 | { |
| 1027 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 1028 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 1029 | } |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1030 | #else |
| 1031 | static inline void |
| 1032 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1033 | { |
| 1034 | } |
| 1035 | #endif |
| 1036 | |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1037 | /* |
| 1038 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 1039 | * won't be used for replay. |
| 1040 | */ |
| 1041 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 1042 | { |
| 1043 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 1044 | |
| 1045 | if (nfserr == nfserr_replay_me) |
| 1046 | return; |
| 1047 | |
| 1048 | if (!seqid_mutating_err(ntohl(nfserr))) { |
| 1049 | cstate->replay_owner = NULL; |
| 1050 | return; |
| 1051 | } |
| 1052 | if (!so) |
| 1053 | return; |
| 1054 | if (so->so_is_open_owner) |
| 1055 | release_last_closed_stateid(openowner(so)); |
| 1056 | so->so_seqid++; |
| 1057 | return; |
| 1058 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1059 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1060 | static void |
| 1061 | gen_sessionid(struct nfsd4_session *ses) |
| 1062 | { |
| 1063 | struct nfs4_client *clp = ses->se_client; |
| 1064 | struct nfsd4_sessionid *sid; |
| 1065 | |
| 1066 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 1067 | sid->clientid = clp->cl_clientid; |
| 1068 | sid->sequence = current_sessionid++; |
| 1069 | sid->reserved = 0; |
| 1070 | } |
| 1071 | |
| 1072 | /* |
Andy Adamson | a649637 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1073 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 1074 | * the rpc header, but all we need to cache is the data starting after |
| 1075 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 1076 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 1077 | * value that is the number of bytes in our cache plus a few additional |
| 1078 | * bytes. In order to stay on the safe side, and not promise more than |
| 1079 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 1080 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 1081 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 1082 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1083 | */ |
Andy Adamson | a649637 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1084 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 1085 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1086 | static void |
| 1087 | free_session_slots(struct nfsd4_session *ses) |
| 1088 | { |
| 1089 | int i; |
| 1090 | |
| 1091 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) |
| 1092 | kfree(ses->se_slots[i]); |
| 1093 | } |
| 1094 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1095 | /* |
| 1096 | * We don't actually need to cache the rpc and session headers, so we |
| 1097 | * can allocate a little less for each slot: |
| 1098 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1099 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1100 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1101 | u32 size; |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1102 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1103 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 1104 | size = 0; |
| 1105 | else |
| 1106 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 1107 | return size + sizeof(struct nfsd4_slot); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1108 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1109 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1110 | /* |
| 1111 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 1112 | * re-negotiate active sessions and reduce their slot usage to make |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 1113 | * 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] | 1114 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1115 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1116 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1117 | u32 slotsize = slot_bytes(ca); |
| 1118 | u32 num = ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1119 | int avail; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1120 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1121 | spin_lock(&nfsd_drc_lock); |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 1122 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, |
| 1123 | nfsd_drc_max_mem - nfsd_drc_mem_used); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1124 | num = min_t(int, num, avail / slotsize); |
| 1125 | nfsd_drc_mem_used += num * slotsize; |
| 1126 | spin_unlock(&nfsd_drc_lock); |
| 1127 | |
| 1128 | return num; |
| 1129 | } |
| 1130 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1131 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1132 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1133 | int slotsize = slot_bytes(ca); |
| 1134 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1135 | spin_lock(&nfsd_drc_lock); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1136 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1137 | spin_unlock(&nfsd_drc_lock); |
| 1138 | } |
| 1139 | |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1140 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 1141 | struct nfsd4_channel_attrs *battrs) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1142 | { |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1143 | int numslots = fattrs->maxreqs; |
| 1144 | int slotsize = slot_bytes(fattrs); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1145 | struct nfsd4_session *new; |
| 1146 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1147 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 1148 | 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] | 1149 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 1150 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1151 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1152 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1153 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1154 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1155 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1156 | for (i = 0; i < numslots; i++) { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1157 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1158 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1159 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1160 | } |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1161 | |
| 1162 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 1163 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 1164 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1165 | return new; |
| 1166 | out_free: |
| 1167 | while (i--) |
| 1168 | kfree(new->se_slots[i]); |
| 1169 | kfree(new); |
| 1170 | return NULL; |
| 1171 | } |
| 1172 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1173 | static void free_conn(struct nfsd4_conn *c) |
| 1174 | { |
| 1175 | svc_xprt_put(c->cn_xprt); |
| 1176 | kfree(c); |
| 1177 | } |
| 1178 | |
| 1179 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 1180 | { |
| 1181 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 1182 | struct nfs4_client *clp = c->cn_session->se_client; |
| 1183 | |
| 1184 | spin_lock(&clp->cl_lock); |
| 1185 | if (!list_empty(&c->cn_persession)) { |
| 1186 | list_del(&c->cn_persession); |
| 1187 | free_conn(c); |
| 1188 | } |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 1189 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 2e4b723 | 2013-03-08 09:30:43 -0500 | [diff] [blame] | 1190 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1191 | } |
| 1192 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1193 | 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] | 1194 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1195 | struct nfsd4_conn *conn; |
| 1196 | |
| 1197 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 1198 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1199 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1200 | svc_xprt_get(rqstp->rq_xprt); |
| 1201 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1202 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1203 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1204 | return conn; |
| 1205 | } |
| 1206 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1207 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1208 | { |
| 1209 | conn->cn_session = ses; |
| 1210 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1211 | } |
| 1212 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1213 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1214 | { |
| 1215 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1216 | |
| 1217 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1218 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1219 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1220 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1221 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1222 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1223 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1224 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1225 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1226 | } |
| 1227 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1228 | 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] | 1229 | { |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1230 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1231 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1232 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1233 | ret = nfsd4_register_conn(conn); |
| 1234 | if (ret) |
| 1235 | /* oops; xprt is already down: */ |
| 1236 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | 57a3714 | 2014-07-18 15:06:47 -0400 | [diff] [blame] | 1237 | /* We may have gained or lost a callback channel: */ |
| 1238 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1239 | } |
| 1240 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1241 | 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] | 1242 | { |
| 1243 | u32 dir = NFS4_CDFC4_FORE; |
| 1244 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1245 | if (cses->flags & SESSION4_BACK_CHAN) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1246 | dir |= NFS4_CDFC4_BACK; |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1247 | return alloc_conn(rqstp, dir); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1251 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1252 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1253 | struct nfs4_client *clp = s->se_client; |
| 1254 | struct nfsd4_conn *c; |
| 1255 | |
| 1256 | spin_lock(&clp->cl_lock); |
| 1257 | while (!list_empty(&s->se_conns)) { |
| 1258 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1259 | list_del_init(&c->cn_persession); |
| 1260 | spin_unlock(&clp->cl_lock); |
| 1261 | |
| 1262 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1263 | free_conn(c); |
| 1264 | |
| 1265 | spin_lock(&clp->cl_lock); |
| 1266 | } |
| 1267 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1268 | } |
| 1269 | |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1270 | static void __free_session(struct nfsd4_session *ses) |
| 1271 | { |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1272 | free_session_slots(ses); |
| 1273 | kfree(ses); |
| 1274 | } |
| 1275 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1276 | static void free_session(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 1277 | { |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1278 | struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id); |
| 1279 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1280 | lockdep_assert_held(&nn->client_lock); |
| 1281 | nfsd4_del_conns(ses); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1282 | nfsd4_put_drc_mem(&ses->se_fchannel); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1283 | __free_session(ses); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1284 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1285 | |
Fengguang Wu | 135ae82 | 2012-11-10 07:20:25 -0500 | [diff] [blame] | 1286 | 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] | 1287 | { |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1288 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1289 | 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] | 1290 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1291 | new->se_client = clp; |
| 1292 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1293 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1294 | INIT_LIST_HEAD(&new->se_conns); |
| 1295 | |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1296 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1297 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 1298 | new->se_cb_prog = cses->callback_prog; |
J. Bruce Fields | c6bb3ca | 2012-11-01 16:31:02 -0400 | [diff] [blame] | 1299 | new->se_cb_sec = cses->cb_sec; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1300 | atomic_set(&new->se_ref, 0); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1301 | idx = hash_sessionid(&new->se_sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1302 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1303 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1304 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1305 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1306 | spin_unlock(&clp->cl_lock); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1307 | spin_unlock(&nn->client_lock); |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1308 | |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1309 | if (cses->flags & SESSION4_BACK_CHAN) { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1310 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1311 | /* |
| 1312 | * This is a little silly; with sessions there's no real |
| 1313 | * use for the callback address. Use the peer address |
| 1314 | * as a reasonable default for now, but consider fixing |
| 1315 | * the rpc client not to require an address in the |
| 1316 | * future: |
| 1317 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1318 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1319 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1320 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1321 | } |
| 1322 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1323 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1324 | static struct nfsd4_session * |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1325 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1326 | { |
| 1327 | struct nfsd4_session *elem; |
| 1328 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1329 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1330 | |
| 1331 | dump_sessionid(__func__, sessionid); |
| 1332 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1333 | /* Search in the appropriate list */ |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1334 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1335 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1336 | NFS4_MAX_SESSIONID_LEN)) { |
| 1337 | return elem; |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | dprintk("%s: session not found\n", __func__); |
| 1342 | return NULL; |
| 1343 | } |
| 1344 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1345 | static struct nfsd4_session * |
| 1346 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, |
| 1347 | __be32 *ret) |
| 1348 | { |
| 1349 | struct nfsd4_session *session; |
| 1350 | __be32 status = nfserr_badsession; |
| 1351 | |
| 1352 | session = __find_in_sessionid_hashtbl(sessionid, net); |
| 1353 | if (!session) |
| 1354 | goto out; |
| 1355 | status = nfsd4_get_session_locked(session); |
| 1356 | if (status) |
| 1357 | session = NULL; |
| 1358 | out: |
| 1359 | *ret = status; |
| 1360 | return session; |
| 1361 | } |
| 1362 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1363 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1364 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1365 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1366 | { |
| 1367 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1368 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1369 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1370 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1371 | } |
| 1372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1374 | static int |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1375 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | { |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1377 | if (clid->cl_boot == nn->boot_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | return 0; |
Andy Adamson | 60adfc5 | 2009-04-03 08:28:50 +0300 | [diff] [blame] | 1379 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1380 | clid->cl_boot, clid->cl_id, nn->boot_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | return 1; |
| 1382 | } |
| 1383 | |
| 1384 | /* |
| 1385 | * XXX Should we use a slab cache ? |
| 1386 | * This type of memory management is somewhat inefficient, but we use it |
| 1387 | * anyway since SETCLIENTID is not a common operation. |
| 1388 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1389 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | { |
| 1391 | struct nfs4_client *clp; |
| 1392 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1393 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
| 1394 | if (clp == NULL) |
| 1395 | return NULL; |
Thomas Meyer | 67114fe | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 1396 | clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL); |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1397 | if (clp->cl_name.data == NULL) { |
| 1398 | kfree(clp); |
| 1399 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | } |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1401 | clp->cl_name.len = name.len; |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1402 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1403 | idr_init(&clp->cl_stateids); |
| 1404 | atomic_set(&clp->cl_refcount, 0); |
| 1405 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 1406 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1407 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1408 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 1409 | INIT_LIST_HEAD(&clp->cl_lru); |
| 1410 | INIT_LIST_HEAD(&clp->cl_callbacks); |
| 1411 | INIT_LIST_HEAD(&clp->cl_revoked); |
| 1412 | spin_lock_init(&clp->cl_lock); |
| 1413 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | return clp; |
| 1415 | } |
| 1416 | |
Trond Myklebust | 4dd86e15 | 2014-04-18 14:43:58 -0400 | [diff] [blame] | 1417 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | free_client(struct nfs4_client *clp) |
| 1419 | { |
Stanislav Kinsbursky | bca0ec6 | 2013-01-09 12:38:34 +0300 | [diff] [blame] | 1420 | struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1421 | |
| 1422 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1423 | while (!list_empty(&clp->cl_sessions)) { |
| 1424 | struct nfsd4_session *ses; |
| 1425 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 1426 | se_perclnt); |
| 1427 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1428 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 1429 | free_session(ses); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1430 | } |
Trond Myklebust | 4cb57e3 | 2014-04-18 14:43:57 -0400 | [diff] [blame] | 1431 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1432 | free_svc_cred(&clp->cl_cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | kfree(clp->cl_name.data); |
majianpeng | 2d32b29 | 2013-01-29 13:16:06 +0800 | [diff] [blame] | 1434 | idr_destroy(&clp->cl_stateids); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | kfree(clp); |
| 1436 | } |
| 1437 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1438 | /* must be called under the client_lock */ |
| 1439 | static inline void |
| 1440 | unhash_client_locked(struct nfs4_client *clp) |
| 1441 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1442 | struct nfsd4_session *ses; |
| 1443 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1444 | list_del(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1445 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1446 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 1447 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1448 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1449 | } |
| 1450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | static void |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1452 | destroy_client(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1454 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | struct list_head reaplist; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1457 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1460 | spin_lock(&state_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1461 | while (!list_empty(&clp->cl_delegations)) { |
| 1462 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1463 | unhash_delegation_locked(dp); |
| 1464 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1466 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | while (!list_empty(&reaplist)) { |
| 1468 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1469 | list_del_init(&dp->dl_recall_lru); |
| 1470 | nfs4_put_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | } |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1472 | while (!list_empty(&clp->cl_revoked)) { |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 1473 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1474 | list_del_init(&dp->dl_recall_lru); |
| 1475 | nfs4_put_delegation(dp); |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 1476 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1477 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1478 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
| 1479 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | } |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 1481 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 1482 | if (clp->cl_cb_conn.cb_xprt) |
| 1483 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1484 | list_del(&clp->cl_idhash); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1485 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1486 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1487 | else |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1488 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1489 | spin_lock(&nn->client_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1490 | unhash_client_locked(clp); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 1491 | WARN_ON_ONCE(atomic_read(&clp->cl_refcount)); |
| 1492 | free_client(clp); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1493 | spin_unlock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1496 | static void expire_client(struct nfs4_client *clp) |
| 1497 | { |
| 1498 | nfsd4_client_record_remove(clp); |
| 1499 | destroy_client(clp); |
| 1500 | } |
| 1501 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1502 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 1503 | { |
| 1504 | memcpy(target->cl_verifier.data, source->data, |
| 1505 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1508 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 1509 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 1511 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 1512 | } |
| 1513 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1514 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1515 | { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1516 | if (source->cr_principal) { |
| 1517 | target->cr_principal = |
| 1518 | kstrdup(source->cr_principal, GFP_KERNEL); |
| 1519 | if (target->cr_principal == NULL) |
| 1520 | return -ENOMEM; |
| 1521 | } else |
| 1522 | target->cr_principal = NULL; |
J. Bruce Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 1523 | target->cr_flavor = source->cr_flavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | target->cr_uid = source->cr_uid; |
| 1525 | target->cr_gid = source->cr_gid; |
| 1526 | target->cr_group_info = source->cr_group_info; |
| 1527 | get_group_info(target->cr_group_info); |
J. Bruce Fields | 0dc1531 | 2013-05-14 16:07:13 -0400 | [diff] [blame] | 1528 | target->cr_gss_mech = source->cr_gss_mech; |
| 1529 | if (source->cr_gss_mech) |
| 1530 | gss_mech_get(source->cr_gss_mech); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1531 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1534 | static long long |
| 1535 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 1536 | { |
| 1537 | long long res; |
| 1538 | |
| 1539 | res = o1->len - o2->len; |
| 1540 | if (res) |
| 1541 | return res; |
| 1542 | return (long long)memcmp(o1->data, o2->data, o1->len); |
| 1543 | } |
| 1544 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1545 | static int same_name(const char *n1, const char *n2) |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1546 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1547 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1551 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 1552 | { |
| 1553 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1557 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 1558 | { |
| 1559 | 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] | 1560 | } |
| 1561 | |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1562 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 1563 | { |
| 1564 | int i; |
| 1565 | |
| 1566 | if (g1->ngroups != g2->ngroups) |
| 1567 | return false; |
| 1568 | for (i=0; i<g1->ngroups; i++) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1569 | if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i))) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1570 | return false; |
| 1571 | return true; |
| 1572 | } |
| 1573 | |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1574 | /* |
| 1575 | * RFC 3530 language requires clid_inuse be returned when the |
| 1576 | * "principal" associated with a requests differs from that previously |
| 1577 | * used. We use uid, gid's, and gss principal string as our best |
| 1578 | * approximation. We also don't want to allow non-gss use of a client |
| 1579 | * established using gss: in theory cr_principal should catch that |
| 1580 | * change, but in practice cr_principal can be null even in the gss case |
| 1581 | * since gssd doesn't always pass down a principal string. |
| 1582 | */ |
| 1583 | static bool is_gss_cred(struct svc_cred *cr) |
| 1584 | { |
| 1585 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 1586 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 1587 | } |
| 1588 | |
| 1589 | |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1590 | static bool |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1591 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 1592 | { |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1593 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1594 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 1595 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1596 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 1597 | return false; |
| 1598 | if (cr1->cr_principal == cr2->cr_principal) |
| 1599 | return true; |
| 1600 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 1601 | return false; |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1602 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1605 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 1606 | { |
| 1607 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1608 | u32 service; |
| 1609 | |
J. Bruce Fields | c472059 | 2013-08-07 11:41:49 -0400 | [diff] [blame] | 1610 | if (!cr->cr_gss_mech) |
| 1611 | return false; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1612 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 1613 | return service == RPC_GSS_SVC_INTEGRITY || |
| 1614 | service == RPC_GSS_SVC_PRIVACY; |
| 1615 | } |
| 1616 | |
| 1617 | static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) |
| 1618 | { |
| 1619 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1620 | |
| 1621 | if (!cl->cl_mach_cred) |
| 1622 | return true; |
| 1623 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 1624 | return false; |
| 1625 | if (!svc_rqst_integrity_protected(rqstp)) |
| 1626 | return false; |
| 1627 | if (!cr->cr_principal) |
| 1628 | return false; |
| 1629 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 1630 | } |
| 1631 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1632 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
J. Bruce Fields | 5ec7b46 | 2007-11-21 21:58:56 -0500 | [diff] [blame] | 1633 | { |
| 1634 | static u32 current_clientid = 1; |
| 1635 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1636 | clp->cl_clientid.cl_boot = nn->boot_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | clp->cl_clientid.cl_id = current_clientid++; |
| 1638 | } |
| 1639 | |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 1640 | static void gen_confirm(struct nfs4_client *clp) |
| 1641 | { |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1642 | __be32 verf[2]; |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 1643 | static u32 i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | |
Jeff Layton | f419992 | 2014-06-17 07:44:11 -0400 | [diff] [blame] | 1645 | /* |
| 1646 | * This is opaque to client, so no need to byte-swap. Use |
| 1647 | * __force to keep sparse happy |
| 1648 | */ |
| 1649 | verf[0] = (__force __be32)get_seconds(); |
| 1650 | verf[1] = (__force __be32)i++; |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1651 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 1654 | static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t) |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1655 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 1656 | struct nfs4_stid *ret; |
| 1657 | |
| 1658 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 1659 | if (!ret || !ret->sc_type) |
| 1660 | return NULL; |
| 1661 | return ret; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 1662 | } |
| 1663 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 1664 | static struct nfs4_stid *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] | 1665 | { |
| 1666 | struct nfs4_stid *s; |
| 1667 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 1668 | s = find_stateid(cl, t); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 1669 | if (!s) |
| 1670 | return NULL; |
| 1671 | if (typemask & s->sc_type) |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 1672 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1673 | return NULL; |
| 1674 | } |
| 1675 | |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 1676 | static struct nfs4_client *create_client(struct xdr_netobj name, |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1677 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 1678 | { |
| 1679 | struct nfs4_client *clp; |
| 1680 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1681 | int ret; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1682 | struct net *net = SVC_NET(rqstp); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1683 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1684 | |
| 1685 | clp = alloc_client(name); |
| 1686 | if (clp == NULL) |
| 1687 | return NULL; |
| 1688 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1689 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 1690 | if (ret) { |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1691 | spin_lock(&nn->client_lock); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1692 | free_client(clp); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1693 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1694 | return NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1695 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 1696 | INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null); |
Benny Halevy | 07cd490 | 2010-05-12 00:13:41 +0300 | [diff] [blame] | 1697 | clp->cl_time = get_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1698 | clear_bit(0, &clp->cl_cb_slot_busy); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1699 | copy_verf(clp, verf); |
| 1700 | rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1701 | gen_confirm(clp); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1702 | clp->cl_cb_session = NULL; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1703 | clp->net = net; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1704 | return clp; |
| 1705 | } |
| 1706 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1707 | static void |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1708 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 1709 | { |
| 1710 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 1711 | struct nfs4_client *clp; |
| 1712 | |
| 1713 | while (*new) { |
| 1714 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 1715 | parent = *new; |
| 1716 | |
| 1717 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 1718 | new = &((*new)->rb_left); |
| 1719 | else |
| 1720 | new = &((*new)->rb_right); |
| 1721 | } |
| 1722 | |
| 1723 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 1724 | rb_insert_color(&new_clp->cl_namenode, root); |
| 1725 | } |
| 1726 | |
| 1727 | static struct nfs4_client * |
| 1728 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 1729 | { |
| 1730 | long long cmp; |
| 1731 | struct rb_node *node = root->rb_node; |
| 1732 | struct nfs4_client *clp; |
| 1733 | |
| 1734 | while (node) { |
| 1735 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 1736 | cmp = compare_blob(&clp->cl_name, name); |
| 1737 | if (cmp > 0) |
| 1738 | node = node->rb_left; |
| 1739 | else if (cmp < 0) |
| 1740 | node = node->rb_right; |
| 1741 | else |
| 1742 | return clp; |
| 1743 | } |
| 1744 | return NULL; |
| 1745 | } |
| 1746 | |
| 1747 | static void |
| 1748 | add_to_unconfirmed(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | { |
| 1750 | unsigned int idhashval; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1751 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1753 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1754 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1756 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 1757 | renew_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1760 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | move_to_confirmed(struct nfs4_client *clp) |
| 1762 | { |
| 1763 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 1764 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | |
| 1766 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 1767 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1768 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1769 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1770 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | renew_client(clp); |
| 1772 | } |
| 1773 | |
| 1774 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1775 | 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] | 1776 | { |
| 1777 | struct nfs4_client *clp; |
| 1778 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 1779 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1780 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1781 | if (same_clid(&clp->cl_clientid, clid)) { |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 1782 | if ((bool)clp->cl_minorversion != sessions) |
| 1783 | return NULL; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1784 | renew_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | return clp; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1786 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | } |
| 1788 | return NULL; |
| 1789 | } |
| 1790 | |
| 1791 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1792 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 1793 | { |
| 1794 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 1795 | |
| 1796 | return find_client_in_id_table(tbl, clid, sessions); |
| 1797 | } |
| 1798 | |
| 1799 | static struct nfs4_client * |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1800 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | { |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1802 | struct list_head *tbl = nn->unconf_id_hashtbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1804 | return find_client_in_id_table(tbl, clid, sessions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 1807 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 1808 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 1809 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 1810 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 1811 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1812 | static struct nfs4_client * |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1813 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1814 | { |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1815 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | static struct nfs4_client * |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1819 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1820 | { |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1821 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1824 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 1825 | 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] | 1826 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1827 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 1828 | struct sockaddr *sa = svc_addr(rqstp); |
| 1829 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 1830 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 1832 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 1833 | if (se->se_callback_netid_len == 3 && |
| 1834 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 1835 | expected_family = AF_INET; |
| 1836 | else if (se->se_callback_netid_len == 4 && |
| 1837 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 1838 | expected_family = AF_INET6; |
| 1839 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | goto out_err; |
| 1841 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1842 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1843 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1844 | (struct sockaddr *)&conn->cb_addr, |
| 1845 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1846 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1847 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1849 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1850 | if (conn->cb_addr.ss_family == AF_INET6) |
| 1851 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 1852 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1853 | conn->cb_prog = se->se_callback_prog; |
| 1854 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 1855 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | return; |
| 1857 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1858 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 1859 | conn->cb_addrlen = 0; |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 1860 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | "will not receive delegations\n", |
| 1862 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 1863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | return; |
| 1865 | } |
| 1866 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1867 | /* |
J. Bruce Fields | 067e1ac | 2014-03-21 17:26:44 -0400 | [diff] [blame] | 1868 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1869 | */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 1870 | static void |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1871 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 1872 | { |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1873 | struct xdr_buf *buf = resp->xdr.buf; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1874 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 1875 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1876 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1877 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1878 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1879 | slot->sl_opcnt = resp->opcnt; |
| 1880 | slot->sl_status = resp->cstate.status; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1881 | |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 1882 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1883 | if (nfsd4_not_cached(resp)) { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1884 | slot->sl_datalen = 0; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1885 | return; |
| 1886 | } |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1887 | base = resp->cstate.data_offset; |
| 1888 | slot->sl_datalen = buf->len - base; |
| 1889 | if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen)) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1890 | WARN("%s: sessions DRC could not cache compound\n", __func__); |
| 1891 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1895 | * Encode the replay sequence operation from the slot values. |
| 1896 | * If cachethis is FALSE encode the uncached rep error on the next |
| 1897 | * operation which sets resp->p and increments resp->opcnt for |
| 1898 | * nfs4svc_encode_compoundres. |
| 1899 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1900 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1901 | static __be32 |
| 1902 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 1903 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1904 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1905 | struct nfsd4_op *op; |
| 1906 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1907 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1908 | /* Encode the replayed sequence operation */ |
| 1909 | op = &args->ops[resp->opcnt - 1]; |
| 1910 | nfsd4_encode_operation(resp, op); |
| 1911 | |
| 1912 | /* Return nfserr_retry_uncached_rep in next operation. */ |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 1913 | if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1914 | op = &args->ops[resp->opcnt++]; |
| 1915 | op->status = nfserr_retry_uncached_rep; |
| 1916 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1917 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1918 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1922 | * The sequence operation is not cached because we can use the slot and |
| 1923 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1924 | */ |
J. Bruce Fields | 3ca2eb9 | 2014-03-21 12:04:21 -0400 | [diff] [blame] | 1925 | static __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1926 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 1927 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1928 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1929 | struct nfsd4_slot *slot = resp->cstate.slot; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1930 | struct xdr_stream *xdr = &resp->xdr; |
| 1931 | __be32 *p; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1932 | __be32 status; |
| 1933 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1934 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1935 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1936 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
J. Bruce Fields | 0da7b19 | 2014-03-21 11:43:34 -0400 | [diff] [blame] | 1937 | if (status) |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1938 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1939 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1940 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 1941 | if (!p) { |
| 1942 | WARN_ON_ONCE(1); |
| 1943 | return nfserr_serverfault; |
| 1944 | } |
| 1945 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 1946 | xdr_commit_encode(xdr); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1947 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1948 | resp->opcnt = slot->sl_opcnt; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1949 | return slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1950 | } |
| 1951 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1952 | /* |
| 1953 | * Set the exchange_id flags returned by the server. |
| 1954 | */ |
| 1955 | static void |
| 1956 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 1957 | { |
| 1958 | /* pNFS is not supported */ |
| 1959 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
| 1960 | |
| 1961 | /* Referrals are supported, Migration is not. */ |
| 1962 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 1963 | |
| 1964 | /* set the wire flags to return to client. */ |
| 1965 | clid->flags = new->cl_exchange_flags; |
| 1966 | } |
| 1967 | |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 1968 | static bool client_has_state(struct nfs4_client *clp) |
| 1969 | { |
| 1970 | /* |
| 1971 | * Note clp->cl_openowners check isn't quite right: there's no |
| 1972 | * need to count owners without stateid's. |
| 1973 | * |
| 1974 | * Also note we should probably be using this in 4.0 case too. |
| 1975 | */ |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 1976 | return !list_empty(&clp->cl_openowners) |
| 1977 | || !list_empty(&clp->cl_delegations) |
| 1978 | || !list_empty(&clp->cl_sessions); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 1979 | } |
| 1980 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1981 | __be32 |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1982 | nfsd4_exchange_id(struct svc_rqst *rqstp, |
| 1983 | struct nfsd4_compound_state *cstate, |
| 1984 | struct nfsd4_exchange_id *exid) |
| 1985 | { |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1986 | struct nfs4_client *unconf, *conf, *new; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 1987 | __be32 status; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1988 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1989 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1990 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 1991 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1992 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1993 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1994 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1995 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1996 | "ip_addr=%s flags %x, spa_how %d\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1997 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1998 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1999 | |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 2000 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2001 | return nfserr_inval; |
| 2002 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2003 | switch (exid->spa_how) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2004 | case SP4_MACH_CRED: |
| 2005 | if (!svc_rqst_integrity_protected(rqstp)) |
| 2006 | return nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2007 | case SP4_NONE: |
| 2008 | break; |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 2009 | default: /* checked by xdr code */ |
| 2010 | WARN_ON_ONCE(1); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2011 | case SP4_SSV: |
J. Bruce Fields | dd30333 | 2013-04-12 18:10:56 -0400 | [diff] [blame] | 2012 | return nfserr_encr_alg_unsupp; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2013 | } |
| 2014 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2015 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2016 | nfs4_lock_state(); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2017 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2018 | if (conf) { |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 2019 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 2020 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 2021 | |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2022 | if (update) { |
| 2023 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2024 | status = nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2025 | goto out; |
| 2026 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2027 | if (!mach_creds_match(conf, rqstp)) { |
| 2028 | status = nfserr_wrong_cred; |
| 2029 | goto out; |
| 2030 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2031 | if (!creds_match) { /* case 9 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2032 | status = nfserr_perm; |
| 2033 | goto out; |
| 2034 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2035 | if (!verfs_match) { /* case 8 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2036 | status = nfserr_not_same; |
| 2037 | goto out; |
| 2038 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2039 | /* case 6 */ |
| 2040 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
| 2041 | new = conf; |
| 2042 | goto out_copy; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2043 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2044 | if (!creds_match) { /* case 3 */ |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2045 | if (client_has_state(conf)) { |
| 2046 | status = nfserr_clid_inuse; |
| 2047 | goto out; |
| 2048 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2049 | expire_client(conf); |
| 2050 | goto out_new; |
| 2051 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2052 | if (verfs_match) { /* case 2 */ |
J. Bruce Fields | 0f1ba0e | 2012-05-25 21:24:40 -0400 | [diff] [blame] | 2053 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2054 | new = conf; |
| 2055 | goto out_copy; |
| 2056 | } |
| 2057 | /* case 5, client reboot */ |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2058 | goto out_new; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 2059 | } |
| 2060 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2061 | if (update) { /* case 7 */ |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 2062 | status = nfserr_noent; |
| 2063 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2064 | } |
| 2065 | |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2066 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2067 | if (unconf) /* case 4, possible retry or client restart */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2068 | expire_client(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2069 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2070 | /* case 1 (normal case) */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2071 | out_new: |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 2072 | new = create_client(exid->clname, rqstp, &verf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2073 | if (new == NULL) { |
J. Bruce Fields | 4731030 | 2010-06-22 16:17:12 -0400 | [diff] [blame] | 2074 | status = nfserr_jukebox; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2075 | goto out; |
| 2076 | } |
J. Bruce Fields | 4f540e2 | 2013-05-07 11:57:52 -0400 | [diff] [blame] | 2077 | new->cl_minorversion = cstate->minorversion; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2078 | new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2079 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2080 | gen_clid(new, nn); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2081 | add_to_unconfirmed(new); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2082 | out_copy: |
| 2083 | exid->clientid.cl_boot = new->cl_clientid.cl_boot; |
| 2084 | exid->clientid.cl_id = new->cl_clientid.cl_id; |
| 2085 | |
J. Bruce Fields | 778df3f | 2012-05-25 21:40:23 -0400 | [diff] [blame] | 2086 | exid->seqid = new->cl_cs_slot.sl_seqid + 1; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2087 | nfsd4_set_ex_flags(new, exid); |
| 2088 | |
| 2089 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2090 | new->cl_cs_slot.sl_seqid, new->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2091 | status = nfs_ok; |
| 2092 | |
| 2093 | out: |
| 2094 | nfs4_unlock_state(); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2095 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2096 | } |
| 2097 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2098 | static __be32 |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2099 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2100 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2101 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 2102 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2103 | |
| 2104 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2105 | if (slot_inuse) { |
| 2106 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2107 | return nfserr_jukebox; |
| 2108 | else |
| 2109 | return nfserr_seq_misordered; |
| 2110 | } |
J. Bruce Fields | f6d8248 | 2012-02-13 16:13:41 -0500 | [diff] [blame] | 2111 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2112 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2113 | return nfs_ok; |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2114 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2115 | return nfserr_replay_cache; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2116 | return nfserr_seq_misordered; |
| 2117 | } |
| 2118 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2119 | /* |
| 2120 | * Cache the create session result into the create session single DRC |
| 2121 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 2122 | * Do this for solo or embedded create session operations. |
| 2123 | */ |
| 2124 | static void |
| 2125 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2126 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2127 | { |
| 2128 | slot->sl_status = nfserr; |
| 2129 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 2130 | } |
| 2131 | |
| 2132 | static __be32 |
| 2133 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 2134 | struct nfsd4_clid_slot *slot) |
| 2135 | { |
| 2136 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 2137 | return slot->sl_status; |
| 2138 | } |
| 2139 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2140 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 2141 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 2142 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2143 | 3 + /* version, opcount, opcode */ \ |
| 2144 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2145 | /* seqid, slotID, slotID, cache */ \ |
| 2146 | 4 ) * sizeof(__be32)) |
| 2147 | |
| 2148 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 2149 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 2150 | 1 + /* status */ \ |
| 2151 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2152 | 3 + /* opcount, opcode, opstatus*/ \ |
| 2153 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2154 | /* seqid, slotID, slotID, slotID, status */ \ |
| 2155 | 5 ) * sizeof(__be32)) |
| 2156 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2157 | 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] | 2158 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2159 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 2160 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2161 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 2162 | return nfserr_toosmall; |
| 2163 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 2164 | return nfserr_toosmall; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2165 | ca->headerpadsz = 0; |
| 2166 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 2167 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 2168 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 2169 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 2170 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 2171 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 2172 | /* |
| 2173 | * Note decreasing slot size below client's request may make it |
| 2174 | * difficult for client to function correctly, whereas |
| 2175 | * decreasing the number of slots will (just?) affect |
| 2176 | * performance. When short on memory we therefore prefer to |
| 2177 | * decrease number of slots instead of their size. Clients that |
| 2178 | * request larger slots than they need will get poor results: |
| 2179 | */ |
| 2180 | ca->maxreqs = nfsd4_get_drc_mem(ca); |
| 2181 | if (!ca->maxreqs) |
| 2182 | return nfserr_jukebox; |
| 2183 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2184 | return nfs_ok; |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2185 | } |
| 2186 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2187 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
| 2188 | RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32)) |
| 2189 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
| 2190 | RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32)) |
| 2191 | |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2192 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 2193 | { |
| 2194 | ca->headerpadsz = 0; |
| 2195 | |
| 2196 | /* |
| 2197 | * These RPC_MAX_HEADER macros are overkill, especially since we |
| 2198 | * don't even do gss on the backchannel yet. But this is still |
| 2199 | * less than 1k. Tighten up this estimate in the unlikely event |
| 2200 | * it turns out to be a problem for some client: |
| 2201 | */ |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2202 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2203 | return nfserr_toosmall; |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2204 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2205 | return nfserr_toosmall; |
| 2206 | ca->maxresp_cached = 0; |
| 2207 | if (ca->maxops < 2) |
| 2208 | return nfserr_toosmall; |
| 2209 | |
| 2210 | return nfs_ok; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2211 | } |
| 2212 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2213 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 2214 | { |
| 2215 | switch (cbs->flavor) { |
| 2216 | case RPC_AUTH_NULL: |
| 2217 | case RPC_AUTH_UNIX: |
| 2218 | return nfs_ok; |
| 2219 | default: |
| 2220 | /* |
| 2221 | * GSS case: the spec doesn't allow us to return this |
| 2222 | * error. But it also doesn't allow us not to support |
| 2223 | * GSS. |
| 2224 | * I'd rather this fail hard than return some error the |
| 2225 | * client might think it can already handle: |
| 2226 | */ |
| 2227 | return nfserr_encr_alg_unsupp; |
| 2228 | } |
| 2229 | } |
| 2230 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2231 | __be32 |
| 2232 | nfsd4_create_session(struct svc_rqst *rqstp, |
| 2233 | struct nfsd4_compound_state *cstate, |
| 2234 | struct nfsd4_create_session *cr_ses) |
| 2235 | { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2236 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2237 | struct nfs4_client *conf, *unconf; |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2238 | struct nfsd4_session *new; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2239 | struct nfsd4_conn *conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2240 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2241 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2242 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2243 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2244 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 2245 | return nfserr_inval; |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2246 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 2247 | if (status) |
| 2248 | return status; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2249 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2250 | if (status) |
| 2251 | return status; |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2252 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 2253 | if (status) |
Kinglong Mee | f403e45 | 2013-12-23 17:31:21 +0800 | [diff] [blame] | 2254 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2255 | status = nfserr_jukebox; |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 2256 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2257 | if (!new) |
| 2258 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2259 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 2260 | if (!conn) |
| 2261 | goto out_free_session; |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2262 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2263 | nfs4_lock_state(); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2264 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2265 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2266 | WARN_ON_ONCE(conf && unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2267 | |
| 2268 | if (conf) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2269 | status = nfserr_wrong_cred; |
| 2270 | if (!mach_creds_match(conf, rqstp)) |
| 2271 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2272 | cs_slot = &conf->cl_cs_slot; |
| 2273 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 2274 | if (status == nfserr_replay_cache) { |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2275 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2276 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2277 | } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2278 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2279 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2280 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2281 | } else if (unconf) { |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2282 | struct nfs4_client *old; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2283 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2284 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2285 | status = nfserr_clid_inuse; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2286 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2287 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2288 | status = nfserr_wrong_cred; |
| 2289 | if (!mach_creds_match(unconf, rqstp)) |
| 2290 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2291 | cs_slot = &unconf->cl_cs_slot; |
| 2292 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 2293 | if (status) { |
| 2294 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2295 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2296 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2297 | } |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2298 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2299 | if (old) { |
| 2300 | status = mark_client_expired(old); |
| 2301 | if (status) |
| 2302 | goto out_free_conn; |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2303 | expire_client(old); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2304 | } |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2305 | move_to_confirmed(unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2306 | conf = unconf; |
| 2307 | } else { |
| 2308 | status = nfserr_stale_clientid; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2309 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2310 | } |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2311 | status = nfs_ok; |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 2312 | /* |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 2313 | * We do not support RDMA or persistent sessions |
| 2314 | */ |
| 2315 | cr_ses->flags &= ~SESSION4_PERSIST; |
| 2316 | cr_ses->flags &= ~SESSION4_RDMA; |
| 2317 | |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2318 | init_session(rqstp, new, conf, cr_ses); |
| 2319 | nfsd4_init_conn(rqstp, conn, new); |
| 2320 | |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2321 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2322 | NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 2323 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2324 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2325 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2326 | /* cache solo and embedded create sessions under the state lock */ |
| 2327 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2328 | nfs4_unlock_state(); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2329 | return status; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2330 | out_free_conn: |
Yanchuan Nian | 266533c | 2012-12-24 18:11:45 +0800 | [diff] [blame] | 2331 | nfs4_unlock_state(); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2332 | free_conn(conn); |
| 2333 | out_free_session: |
| 2334 | __free_session(new); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2335 | out_release_drc_mem: |
| 2336 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
J. Bruce Fields | 1ca5079 | 2013-03-14 18:12:03 -0400 | [diff] [blame] | 2337 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2338 | } |
| 2339 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2340 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 2341 | { |
| 2342 | switch (*dir) { |
| 2343 | case NFS4_CDFC4_FORE: |
| 2344 | case NFS4_CDFC4_BACK: |
| 2345 | return nfs_ok; |
| 2346 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 2347 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 2348 | *dir = NFS4_CDFC4_BOTH; |
| 2349 | return nfs_ok; |
| 2350 | }; |
| 2351 | return nfserr_inval; |
| 2352 | } |
| 2353 | |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2354 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc) |
| 2355 | { |
| 2356 | struct nfsd4_session *session = cstate->session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2357 | 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] | 2358 | __be32 status; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2359 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2360 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 2361 | if (status) |
| 2362 | return status; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2363 | spin_lock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2364 | session->se_cb_prog = bc->bc_cb_program; |
| 2365 | session->se_cb_sec = bc->bc_cb_sec; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2366 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2367 | |
| 2368 | nfsd4_probe_callback(session->se_client); |
| 2369 | |
| 2370 | return nfs_ok; |
| 2371 | } |
| 2372 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2373 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 2374 | struct nfsd4_compound_state *cstate, |
| 2375 | struct nfsd4_bind_conn_to_session *bcts) |
| 2376 | { |
| 2377 | __be32 status; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2378 | struct nfsd4_conn *conn; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2379 | struct nfsd4_session *session; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2380 | struct net *net = SVC_NET(rqstp); |
| 2381 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2382 | |
| 2383 | if (!nfsd4_last_compound_op(rqstp)) |
| 2384 | return nfserr_not_only_op; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2385 | nfs4_lock_state(); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2386 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2387 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2388 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2389 | if (!session) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2390 | goto out_no_session; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2391 | status = nfserr_wrong_cred; |
| 2392 | if (!mach_creds_match(session->se_client, rqstp)) |
| 2393 | goto out; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2394 | status = nfsd4_map_bcts_dir(&bcts->dir); |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2395 | if (status) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2396 | goto out; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2397 | conn = alloc_conn(rqstp, bcts->dir); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2398 | status = nfserr_jukebox; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2399 | if (!conn) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2400 | goto out; |
| 2401 | nfsd4_init_conn(rqstp, conn, session); |
| 2402 | status = nfs_ok; |
| 2403 | out: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2404 | nfsd4_put_session(session); |
| 2405 | out_no_session: |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2406 | nfs4_unlock_state(); |
| 2407 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2408 | } |
| 2409 | |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2410 | static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid) |
| 2411 | { |
| 2412 | if (!session) |
| 2413 | return 0; |
| 2414 | return !memcmp(sid, &session->se_sessionid, sizeof(*sid)); |
| 2415 | } |
| 2416 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2417 | __be32 |
| 2418 | nfsd4_destroy_session(struct svc_rqst *r, |
| 2419 | struct nfsd4_compound_state *cstate, |
| 2420 | struct nfsd4_destroy_session *sessionid) |
| 2421 | { |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2422 | struct nfsd4_session *ses; |
J. Bruce Fields | abcdff09 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2423 | __be32 status; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2424 | int ref_held_by_me = 0; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2425 | struct net *net = SVC_NET(r); |
| 2426 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2427 | |
J. Bruce Fields | abcdff09 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2428 | nfs4_lock_state(); |
| 2429 | status = nfserr_not_only_op; |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2430 | if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2431 | if (!nfsd4_last_compound_op(r)) |
J. Bruce Fields | abcdff09 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2432 | goto out; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2433 | ref_held_by_me++; |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2434 | } |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2435 | dump_sessionid(__func__, &sessionid->sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2436 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2437 | ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status); |
J. Bruce Fields | abcdff09 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2438 | if (!ses) |
| 2439 | goto out_client_lock; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2440 | status = nfserr_wrong_cred; |
| 2441 | if (!mach_creds_match(ses->se_client, r)) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2442 | goto out_put_session; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2443 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2444 | if (status) |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2445 | goto out_put_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2446 | unhash_session(ses); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2447 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2448 | |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 2449 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 2450 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2451 | spin_lock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2452 | status = nfs_ok; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2453 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2454 | nfsd4_put_session_locked(ses); |
J. Bruce Fields | abcdff09 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2455 | out_client_lock: |
| 2456 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2457 | out: |
J. Bruce Fields | abcdff09 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2458 | nfs4_unlock_state(); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2459 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2460 | } |
| 2461 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2462 | 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] | 2463 | { |
| 2464 | struct nfsd4_conn *c; |
| 2465 | |
| 2466 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2467 | if (c->cn_xprt == xpt) { |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2468 | return c; |
| 2469 | } |
| 2470 | } |
| 2471 | return NULL; |
| 2472 | } |
| 2473 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2474 | 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] | 2475 | { |
| 2476 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2477 | struct nfsd4_conn *c; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2478 | __be32 status = nfs_ok; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2479 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2480 | |
| 2481 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2482 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2483 | if (c) |
| 2484 | goto out_free; |
| 2485 | status = nfserr_conn_not_bound_to_session; |
| 2486 | if (clp->cl_mach_cred) |
| 2487 | goto out_free; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2488 | __nfsd4_hash_conn(new, ses); |
| 2489 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2490 | ret = nfsd4_register_conn(new); |
| 2491 | if (ret) |
| 2492 | /* oops; xprt is already down: */ |
| 2493 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2494 | return nfs_ok; |
| 2495 | out_free: |
| 2496 | spin_unlock(&clp->cl_lock); |
| 2497 | free_conn(new); |
| 2498 | return status; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2499 | } |
| 2500 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2501 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 2502 | { |
| 2503 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 2504 | |
| 2505 | return args->opcnt > session->se_fchannel.maxops; |
| 2506 | } |
| 2507 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2508 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 2509 | struct nfsd4_session *session) |
| 2510 | { |
| 2511 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 2512 | |
| 2513 | return xb->len > session->se_fchannel.maxreq_sz; |
| 2514 | } |
| 2515 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2516 | __be32 |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2517 | nfsd4_sequence(struct svc_rqst *rqstp, |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2518 | struct nfsd4_compound_state *cstate, |
| 2519 | struct nfsd4_sequence *seq) |
| 2520 | { |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2521 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2522 | struct xdr_stream *xdr = &resp->xdr; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2523 | struct nfsd4_session *session; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2524 | struct nfs4_client *clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2525 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2526 | struct nfsd4_conn *conn; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2527 | __be32 status; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2528 | int buflen; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2529 | struct net *net = SVC_NET(rqstp); |
| 2530 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2531 | |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2532 | if (resp->opcnt != 1) |
| 2533 | return nfserr_sequence_pos; |
| 2534 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2535 | /* |
| 2536 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 2537 | * below. |
| 2538 | */ |
| 2539 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 2540 | if (!conn) |
| 2541 | return nfserr_jukebox; |
| 2542 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2543 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2544 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2545 | if (!session) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2546 | goto out_no_session; |
| 2547 | clp = session->se_client; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2548 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2549 | status = nfserr_too_many_ops; |
| 2550 | if (nfsd4_session_too_many_ops(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2551 | goto out_put_session; |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2552 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2553 | status = nfserr_req_too_big; |
| 2554 | if (nfsd4_request_too_big(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2555 | goto out_put_session; |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2556 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2557 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 2558 | if (seq->slotid >= session->se_fchannel.maxreqs) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2559 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2560 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2561 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2562 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 2563 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 2564 | /* We do not negotiate the number of slots yet, so set the |
| 2565 | * maxslots to the session maxreqs which is used to encode |
| 2566 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 2567 | seq->maxslots = session->se_fchannel.maxreqs; |
| 2568 | |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2569 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 2570 | slot->sl_flags & NFSD4_SLOT_INUSE); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2571 | if (status == nfserr_replay_cache) { |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2572 | status = nfserr_seq_misordered; |
| 2573 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2574 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2575 | cstate->slot = slot; |
| 2576 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2577 | cstate->clp = clp; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2578 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2579 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2580 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2581 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 2582 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2583 | } |
| 2584 | if (status) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2585 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2586 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2587 | status = nfsd4_sequence_check_conn(conn, session); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2588 | conn = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2589 | if (status) |
| 2590 | goto out_put_session; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2591 | |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2592 | buflen = (seq->cachethis) ? |
| 2593 | session->se_fchannel.maxresp_cached : |
| 2594 | session->se_fchannel.maxresp_sz; |
| 2595 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 2596 | nfserr_rep_too_big; |
J. Bruce Fields | a5cddc8 | 2014-05-12 18:10:58 -0400 | [diff] [blame] | 2597 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2598 | goto out_put_session; |
J. Bruce Fields | 32aaa62 | 2014-03-20 20:47:41 -0400 | [diff] [blame] | 2599 | svc_reserve(rqstp, buflen); |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2600 | |
| 2601 | status = nfs_ok; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2602 | /* Success! bump slot seqid */ |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2603 | slot->sl_seqid = seq->seqid; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2604 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2605 | if (seq->cachethis) |
| 2606 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2607 | else |
| 2608 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2609 | |
| 2610 | cstate->slot = slot; |
| 2611 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2612 | cstate->clp = clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2613 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2614 | out: |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2615 | switch (clp->cl_cb_state) { |
| 2616 | case NFSD4_CB_DOWN: |
| 2617 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 2618 | break; |
| 2619 | case NFSD4_CB_FAULT: |
| 2620 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 2621 | break; |
| 2622 | default: |
| 2623 | seq->status_flags = 0; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 2624 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 2625 | if (!list_empty(&clp->cl_revoked)) |
| 2626 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2627 | out_no_session: |
Kinglong Mee | 3f42d2c | 2014-03-24 11:56:59 +0800 | [diff] [blame] | 2628 | if (conn) |
| 2629 | free_conn(conn); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2630 | spin_unlock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2631 | return status; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2632 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2633 | nfsd4_put_session_locked(session); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2634 | goto out_no_session; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2635 | } |
| 2636 | |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2637 | void |
| 2638 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 2639 | { |
| 2640 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 2641 | |
| 2642 | if (nfsd4_has_session(cs)) { |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2643 | if (cs->status != nfserr_replay_cache) { |
| 2644 | nfsd4_store_cache_entry(resp); |
| 2645 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 2646 | } |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2647 | /* Drop session reference that was taken in nfsd4_sequence() */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2648 | nfsd4_put_session(cs->session); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2649 | } else if (cs->clp) |
| 2650 | put_client_renew(cs->clp); |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2651 | } |
| 2652 | |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2653 | __be32 |
| 2654 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc) |
| 2655 | { |
| 2656 | struct nfs4_client *conf, *unconf, *clp; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2657 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2658 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2659 | |
| 2660 | nfs4_lock_state(); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2661 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2662 | conf = find_confirmed_client(&dc->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2663 | WARN_ON_ONCE(conf && unconf); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2664 | |
| 2665 | if (conf) { |
| 2666 | clp = conf; |
| 2667 | |
J. Bruce Fields | c0293b0 | 2013-03-14 18:20:01 -0400 | [diff] [blame] | 2668 | if (client_has_state(conf)) { |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2669 | status = nfserr_clientid_busy; |
| 2670 | goto out; |
| 2671 | } |
| 2672 | } else if (unconf) |
| 2673 | clp = unconf; |
| 2674 | else { |
| 2675 | status = nfserr_stale_clientid; |
| 2676 | goto out; |
| 2677 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2678 | if (!mach_creds_match(clp, rqstp)) { |
| 2679 | status = nfserr_wrong_cred; |
| 2680 | goto out; |
| 2681 | } |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2682 | expire_client(clp); |
| 2683 | out: |
| 2684 | nfs4_unlock_state(); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2685 | return status; |
| 2686 | } |
| 2687 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2688 | __be32 |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2689 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc) |
| 2690 | { |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2691 | __be32 status = 0; |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2692 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2693 | if (rc->rca_one_fs) { |
| 2694 | if (!cstate->current_fh.fh_dentry) |
| 2695 | return nfserr_nofilehandle; |
| 2696 | /* |
| 2697 | * We don't take advantage of the rca_one_fs case. |
| 2698 | * That's OK, it's optional, we can safely ignore it. |
| 2699 | */ |
| 2700 | return nfs_ok; |
| 2701 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2702 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2703 | nfs4_lock_state(); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2704 | status = nfserr_complete_already; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 2705 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
| 2706 | &cstate->session->se_client->cl_flags)) |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2707 | goto out; |
| 2708 | |
| 2709 | status = nfserr_stale_clientid; |
| 2710 | if (is_client_expired(cstate->session->se_client)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2711 | /* |
| 2712 | * The following error isn't really legal. |
| 2713 | * But we only get here if the client just explicitly |
| 2714 | * destroyed the client. Surely it no longer cares what |
| 2715 | * error it gets back on an operation for the dead |
| 2716 | * client. |
| 2717 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2718 | goto out; |
| 2719 | |
| 2720 | status = nfs_ok; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 2721 | nfsd4_client_record_create(cstate->session->se_client); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2722 | out: |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2723 | nfs4_unlock_state(); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2724 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 2728 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 2729 | struct nfsd4_setclientid *setclid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | { |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 2731 | struct xdr_netobj clname = setclid->se_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | nfs4_verifier clverifier = setclid->se_verf; |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2733 | struct nfs4_client *conf, *unconf, *new; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2734 | __be32 status; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2735 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 2736 | |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2737 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | nfs4_lock_state(); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2739 | conf = find_confirmed_client_by_name(&clname, nn); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2740 | if (conf) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2741 | /* case 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2743 | if (clp_used_exchangeid(conf)) |
| 2744 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 2745 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2746 | char addr_str[INET6_ADDRSTRLEN]; |
| 2747 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, |
| 2748 | sizeof(addr_str)); |
| 2749 | dprintk("NFSD: setclientid: string in use by client " |
| 2750 | "at %s\n", addr_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | goto out; |
| 2752 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2754 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 2755 | if (unconf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | status = nfserr_jukebox; |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 2758 | new = create_client(clname, rqstp, &clverifier); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 2759 | if (new == NULL) |
| 2760 | goto out; |
J. Bruce Fields | 34b232b | 2012-05-18 22:23:42 -0400 | [diff] [blame] | 2761 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2762 | /* case 1: probable callback update */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | copy_clid(new, conf); |
J. Bruce Fields | 34b232b | 2012-05-18 22:23:42 -0400 | [diff] [blame] | 2764 | else /* case 4 (new client) or cases 2, 3 (client reboot): */ |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2765 | gen_clid(new, nn); |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 2766 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2767 | gen_callback(new, setclid, rqstp); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2768 | add_to_unconfirmed(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 2770 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 2771 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); |
| 2772 | status = nfs_ok; |
| 2773 | out: |
| 2774 | nfs4_unlock_state(); |
| 2775 | return status; |
| 2776 | } |
| 2777 | |
| 2778 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2779 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 2780 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
| 2781 | struct nfsd4_compound_state *cstate, |
| 2782 | struct nfsd4_setclientid_confirm *setclientid_confirm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | { |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2784 | struct nfs4_client *conf, *unconf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2785 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 2786 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2787 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 2788 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 2790 | if (STALE_CLIENTID(clid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | return nfserr_stale_clientid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | nfs4_lock_state(); |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2793 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2794 | conf = find_confirmed_client(clid, false, nn); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2795 | unconf = find_unconfirmed_client(clid, false, nn); |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2796 | /* |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2797 | * We try hard to give out unique clientid's, so if we get an |
| 2798 | * attempt to confirm the same clientid with a different cred, |
| 2799 | * there's a bug somewhere. Let's charitably assume it's our |
| 2800 | * bug. |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2801 | */ |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2802 | status = nfserr_serverfault; |
| 2803 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
| 2804 | goto out; |
| 2805 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
| 2806 | goto out; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2807 | /* cases below refer to rfc 3530 section 14.2.34: */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 2808 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
| 2809 | if (conf && !unconf) /* case 2: probable retransmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | status = nfs_ok; |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 2811 | else /* case 4: client hasn't noticed we rebooted yet? */ |
| 2812 | status = nfserr_stale_clientid; |
| 2813 | goto out; |
| 2814 | } |
| 2815 | status = nfs_ok; |
| 2816 | if (conf) { /* case 1: callback update */ |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2817 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
| 2818 | nfsd4_probe_callback(conf); |
| 2819 | expire_client(unconf); |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 2820 | } else { /* case 3: normal case; new or rebooted client */ |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2821 | conf = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2822 | if (conf) { |
| 2823 | status = mark_client_expired(conf); |
| 2824 | if (status) |
| 2825 | goto out; |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2826 | expire_client(conf); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2827 | } |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2828 | move_to_confirmed(unconf); |
J. Bruce Fields | f3d03b9 | 2012-05-23 11:38:38 -0400 | [diff] [blame] | 2829 | nfsd4_probe_callback(unconf); |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 2830 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | nfs4_unlock_state(); |
| 2833 | return status; |
| 2834 | } |
| 2835 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2836 | static struct nfs4_file *nfsd4_alloc_file(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | { |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2838 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 2839 | } |
| 2840 | |
| 2841 | /* OPEN Share state helper functions */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 2842 | static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2843 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 2844 | unsigned int hashval = file_hashval(fh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 2846 | lockdep_assert_held(&state_lock); |
| 2847 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2848 | atomic_set(&fp->fi_ref, 1); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 2849 | spin_lock_init(&fp->fi_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2850 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 2851 | INIT_LIST_HEAD(&fp->fi_delegations); |
Trond Myklebust | e2cf80d | 2014-07-23 16:17:38 -0400 | [diff] [blame] | 2852 | fh_copy_shallow(&fp->fi_fhandle, fh); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2853 | fp->fi_had_conflict = false; |
| 2854 | fp->fi_lease = NULL; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 2855 | fp->fi_share_deny = 0; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2856 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 2857 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 2858 | hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | } |
| 2860 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 2861 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2862 | nfsd4_free_slabs(void) |
| 2863 | { |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2864 | kmem_cache_destroy(openowner_slab); |
| 2865 | kmem_cache_destroy(lockowner_slab); |
| 2866 | kmem_cache_destroy(file_slab); |
| 2867 | kmem_cache_destroy(stateid_slab); |
| 2868 | kmem_cache_destroy(deleg_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2869 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | |
Bryan Schumaker | 7208339 | 2011-11-01 15:24:59 -0400 | [diff] [blame] | 2871 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | nfsd4_init_slabs(void) |
| 2873 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2874 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 2875 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 2876 | if (openowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2877 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2878 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
Yanchuan Nian | 3c40794 | 2012-10-24 14:44:19 +0800 | [diff] [blame] | 2879 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2880 | if (lockowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2881 | goto out_free_openowner_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2882 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2883 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2884 | if (file_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2885 | goto out_free_lockowner_slab; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2886 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2887 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2888 | if (stateid_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2889 | goto out_free_file_slab; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 2890 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2891 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 2892 | if (deleg_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2893 | goto out_free_stateid_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | return 0; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2895 | |
| 2896 | out_free_stateid_slab: |
| 2897 | kmem_cache_destroy(stateid_slab); |
| 2898 | out_free_file_slab: |
| 2899 | kmem_cache_destroy(file_slab); |
| 2900 | out_free_lockowner_slab: |
| 2901 | kmem_cache_destroy(lockowner_slab); |
| 2902 | out_free_openowner_slab: |
| 2903 | kmem_cache_destroy(openowner_slab); |
| 2904 | out: |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2905 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 2906 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | } |
| 2908 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2909 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 2910 | { |
| 2911 | rp->rp_status = nfserr_serverfault; |
| 2912 | rp->rp_buflen = 0; |
| 2913 | rp->rp_buf = rp->rp_ibuf; |
| 2914 | } |
| 2915 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2916 | 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] | 2917 | { |
| 2918 | struct nfs4_stateowner *sop; |
| 2919 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2920 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2921 | if (!sop) |
| 2922 | return NULL; |
| 2923 | |
| 2924 | sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL); |
| 2925 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2926 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2927 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2929 | sop->so_owner.len = owner->len; |
| 2930 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2931 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2932 | sop->so_client = clp; |
| 2933 | init_nfs4_replay(&sop->so_replay); |
| 2934 | return sop; |
| 2935 | } |
| 2936 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2937 | 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] | 2938 | { |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 2939 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2940 | |
| 2941 | list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2942 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | } |
| 2944 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2945 | static struct nfs4_openowner * |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 2946 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 2947 | struct nfsd4_compound_state *cstate) |
| 2948 | { |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 2949 | struct nfs4_client *clp = cstate->clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2950 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2952 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 2953 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | return NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2955 | oo->oo_owner.so_is_open_owner = 1; |
| 2956 | oo->oo_owner.so_seqid = open->op_seqid; |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 2957 | oo->oo_flags = NFS4_OO_NEW; |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 2958 | if (nfsd4_has_session(cstate)) |
| 2959 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2960 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 2961 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2962 | INIT_LIST_HEAD(&oo->oo_close_lru); |
| 2963 | hash_openowner(oo, clp, strhashval); |
| 2964 | return oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | } |
| 2966 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 2967 | static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2968 | struct nfs4_openowner *oo = open->op_openowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 2970 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 2971 | INIT_LIST_HEAD(&stp->st_locks); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2972 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2973 | stp->st_stateowner = &oo->oo_owner; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2974 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | stp->st_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | stp->st_access_bmap = 0; |
| 2977 | stp->st_deny_bmap = 0; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 2978 | stp->st_openstp = NULL; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 2979 | spin_lock(&fp->fi_lock); |
| 2980 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 2981 | spin_unlock(&fp->fi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | } |
| 2983 | |
| 2984 | static void |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 2985 | move_to_close_lru(struct nfs4_openowner *oo, struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | { |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 2987 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 2988 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2989 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 2991 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2992 | oo->oo_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | } |
| 2994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2996 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, |
| 2997 | clientid_t *clid) |
| 2998 | { |
| 2999 | return (sop->so_owner.len == owner->len) && |
| 3000 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len) && |
| 3001 | (sop->so_client->cl_clientid.cl_id == clid->cl_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | } |
| 3003 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3004 | static struct nfs4_openowner * |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 3005 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
| 3006 | bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3007 | { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 3008 | struct nfs4_stateowner *so; |
| 3009 | struct nfs4_openowner *oo; |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 3010 | struct nfs4_client *clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 3012 | list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) { |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 3013 | if (!so->so_is_open_owner) |
| 3014 | continue; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 3015 | if (same_owner_str(so, &open->op_owner, &open->op_clientid)) { |
| 3016 | oo = openowner(so); |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 3017 | clp = oo->oo_owner.so_client; |
| 3018 | if ((bool)clp->cl_minorversion != sessions) |
| 3019 | return NULL; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 3020 | renew_client(oo->oo_owner.so_client); |
| 3021 | return oo; |
| 3022 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | } |
| 3024 | return NULL; |
| 3025 | } |
| 3026 | |
| 3027 | /* search file_hashtbl[] for file */ |
| 3028 | static struct nfs4_file * |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3029 | find_file_locked(struct knfsd_fh *fh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3031 | unsigned int hashval = file_hashval(fh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | struct nfs4_file *fp; |
| 3033 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3034 | lockdep_assert_held(&state_lock); |
| 3035 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 3036 | hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3037 | if (nfsd_fh_match(&fp->fi_fhandle, fh)) { |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3038 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3040 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | } |
| 3042 | return NULL; |
| 3043 | } |
| 3044 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3045 | static struct nfs4_file * |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3046 | find_file(struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3047 | { |
| 3048 | struct nfs4_file *fp; |
| 3049 | |
| 3050 | spin_lock(&state_lock); |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3051 | fp = find_file_locked(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3052 | spin_unlock(&state_lock); |
| 3053 | return fp; |
| 3054 | } |
| 3055 | |
| 3056 | static struct nfs4_file * |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 3057 | find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3058 | { |
| 3059 | struct nfs4_file *fp; |
| 3060 | |
| 3061 | spin_lock(&state_lock); |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3062 | fp = find_file_locked(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3063 | if (fp == NULL) { |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 3064 | nfsd4_init_file(new, fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3065 | fp = new; |
| 3066 | } |
| 3067 | spin_unlock(&state_lock); |
| 3068 | |
| 3069 | return fp; |
| 3070 | } |
| 3071 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 3072 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | * Called to check deny when READ with all zero stateid or |
| 3074 | * WRITE with all zero or all one stateid |
| 3075 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3076 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 3078 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | struct nfs4_file *fp; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3080 | __be32 ret = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3082 | fp = find_file(¤t_fh->fh_handle); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3083 | if (!fp) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3084 | return ret; |
| 3085 | /* Check for conflicting share reservations */ |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3086 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3087 | if (fp->fi_share_deny & deny_type) |
| 3088 | ret = nfserr_locked; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3089 | spin_unlock(&fp->fi_lock); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3090 | put_nfs4_file(fp); |
| 3091 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | } |
| 3093 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3094 | void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | { |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 3096 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 3097 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 3098 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3099 | /* |
| 3100 | * We can't do this in nfsd_break_deleg_cb because it is |
| 3101 | * already holding inode->i_lock |
| 3102 | */ |
| 3103 | spin_lock(&state_lock); |
| 3104 | block_delegations(&dp->dl_fh); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3105 | /* |
| 3106 | * If the dl_time != 0, then we know that it has already been |
| 3107 | * queued for a lease break. Don't queue it again. |
| 3108 | */ |
| 3109 | if (dp->dl_time == 0) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3110 | dp->dl_time = get_seconds(); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3111 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3112 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3113 | spin_unlock(&state_lock); |
| 3114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3116 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
| 3117 | { |
| 3118 | /* |
| 3119 | * We're assuming the state code never drops its reference |
| 3120 | * without first removing the lease. Since we're in this lease |
| 3121 | * callback (and since the lease code is serialized by the kernel |
| 3122 | * lock) we know the server hasn't removed the lease yet, we know |
| 3123 | * it's safe to take a reference. |
| 3124 | */ |
Trond Myklebust | 72c0b0f | 2014-07-21 09:34:58 -0400 | [diff] [blame] | 3125 | atomic_inc(&dp->dl_stid.sc_count); |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3126 | nfsd4_cb_recall(dp); |
| 3127 | } |
| 3128 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 3129 | /* Called from break_lease() with i_lock held. */ |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3130 | static void nfsd_break_deleg_cb(struct file_lock *fl) |
| 3131 | { |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3132 | struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; |
| 3133 | struct nfs4_delegation *dp; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3134 | |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 3135 | if (!fp) { |
| 3136 | WARN(1, "(%p)->fl_owner NULL\n", fl); |
| 3137 | return; |
| 3138 | } |
| 3139 | if (fp->fi_had_conflict) { |
| 3140 | WARN(1, "duplicate break on %p\n", fp); |
| 3141 | return; |
| 3142 | } |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3143 | /* |
| 3144 | * 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] | 3145 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3146 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3147 | */ |
| 3148 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3150 | spin_lock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3151 | fp->fi_had_conflict = true; |
| 3152 | /* |
| 3153 | * If there are no delegations on the list, then we can't count on this |
| 3154 | * lease ever being cleaned up. Set the fl_break_time to jiffies so that |
| 3155 | * time_out_leases will do it ASAP. The fact that fi_had_conflict is now |
| 3156 | * true should keep any new delegations from being hashed. |
| 3157 | */ |
| 3158 | if (list_empty(&fp->fi_delegations)) |
| 3159 | fl->fl_break_time = jiffies; |
| 3160 | else |
| 3161 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) |
| 3162 | nfsd_break_one_deleg(dp); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3163 | spin_unlock(&fp->fi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | } |
| 3165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | static |
| 3167 | int nfsd_change_deleg_cb(struct file_lock **onlist, int arg) |
| 3168 | { |
| 3169 | if (arg & F_UNLCK) |
| 3170 | return lease_modify(onlist, arg); |
| 3171 | else |
| 3172 | return -EAGAIN; |
| 3173 | } |
| 3174 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 3175 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 3176 | .lm_break = nfsd_break_deleg_cb, |
| 3177 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | }; |
| 3179 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 3180 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 3181 | { |
| 3182 | if (nfsd4_has_session(cstate)) |
| 3183 | return nfs_ok; |
| 3184 | if (seqid == so->so_seqid - 1) |
| 3185 | return nfserr_replay_me; |
| 3186 | if (seqid == so->so_seqid) |
| 3187 | return nfs_ok; |
| 3188 | return nfserr_bad_seqid; |
| 3189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3191 | static __be32 lookup_clientid(clientid_t *clid, |
| 3192 | struct nfsd4_compound_state *cstate, |
| 3193 | struct nfsd_net *nn) |
| 3194 | { |
| 3195 | struct nfs4_client *found; |
| 3196 | |
| 3197 | if (cstate->clp) { |
| 3198 | found = cstate->clp; |
| 3199 | if (!same_clid(&found->cl_clientid, clid)) |
| 3200 | return nfserr_stale_clientid; |
| 3201 | return nfs_ok; |
| 3202 | } |
| 3203 | |
| 3204 | if (STALE_CLIENTID(clid, nn)) |
| 3205 | return nfserr_stale_clientid; |
| 3206 | |
| 3207 | /* |
| 3208 | * For v4.1+ we get the client in the SEQUENCE op. If we don't have one |
| 3209 | * cached already then we know this is for is for v4.0 and "sessions" |
| 3210 | * will be false. |
| 3211 | */ |
| 3212 | WARN_ON_ONCE(cstate->session); |
| 3213 | found = find_confirmed_client(clid, false, nn); |
| 3214 | if (!found) |
| 3215 | return nfserr_expired; |
| 3216 | |
| 3217 | /* Cache the nfs4_client in cstate! */ |
| 3218 | cstate->clp = found; |
| 3219 | atomic_inc(&found->cl_refcount); |
| 3220 | return nfs_ok; |
| 3221 | } |
| 3222 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3223 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3224 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 3225 | struct nfsd4_open *open, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3226 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3227 | clientid_t *clientid = &open->op_clientid; |
| 3228 | struct nfs4_client *clp = NULL; |
| 3229 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3230 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3231 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 3233 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3234 | return nfserr_stale_clientid; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3235 | /* |
| 3236 | * In case we need it later, after we've already created the |
| 3237 | * file and don't want to risk a further failure: |
| 3238 | */ |
| 3239 | open->op_file = nfsd4_alloc_file(); |
| 3240 | if (open->op_file == NULL) |
| 3241 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3242 | |
Trond Myklebust | 2d91e89 | 2014-06-30 11:48:46 -0400 | [diff] [blame] | 3243 | status = lookup_clientid(clientid, cstate, nn); |
| 3244 | if (status) |
| 3245 | return status; |
| 3246 | clp = cstate->clp; |
| 3247 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 3248 | strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 3249 | oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3250 | open->op_openowner = oo; |
| 3251 | if (!oo) { |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3252 | goto new_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3254 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3255 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3256 | release_openowner(oo); |
| 3257 | open->op_openowner = NULL; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3258 | goto new_owner; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3259 | } |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3260 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 3261 | if (status) |
| 3262 | return status; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3263 | goto alloc_stateid; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3264 | new_owner: |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3265 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3266 | if (oo == NULL) |
| 3267 | return nfserr_jukebox; |
| 3268 | open->op_openowner = oo; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3269 | alloc_stateid: |
| 3270 | open->op_stp = nfs4_alloc_stateid(clp); |
| 3271 | if (!open->op_stp) |
| 3272 | return nfserr_jukebox; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3273 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | } |
| 3275 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3276 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 3277 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 3278 | { |
| 3279 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 3280 | return nfserr_openmode; |
| 3281 | else |
| 3282 | return nfs_ok; |
| 3283 | } |
| 3284 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 3285 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3286 | { |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3287 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 3288 | } |
| 3289 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3290 | 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] | 3291 | { |
| 3292 | struct nfs4_stid *ret; |
| 3293 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3294 | ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 3295 | if (!ret) |
| 3296 | return NULL; |
| 3297 | return delegstateid(ret); |
| 3298 | } |
| 3299 | |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3300 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 3301 | { |
| 3302 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 3303 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 3304 | } |
| 3305 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3306 | static __be32 |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 3307 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3308 | struct nfs4_delegation **dp) |
| 3309 | { |
| 3310 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3311 | __be32 status = nfserr_bad_stateid; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3312 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3313 | *dp = find_deleg_stateid(cl, &open->op_delegate_stateid); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3314 | if (*dp == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3315 | goto out; |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3316 | flags = share_access_to_flags(open->op_share_access); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3317 | status = nfs4_check_delegmode(*dp, flags); |
| 3318 | if (status) |
| 3319 | *dp = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3320 | out: |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3321 | if (!nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3322 | return nfs_ok; |
| 3323 | if (status) |
| 3324 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3325 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3326 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3327 | } |
| 3328 | |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3329 | static struct nfs4_ol_stateid * |
| 3330 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | { |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3332 | struct nfs4_ol_stateid *local, *ret = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3333 | struct nfs4_openowner *oo = open->op_openowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3335 | spin_lock(&fp->fi_lock); |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 3336 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3337 | /* ignore lock owners */ |
| 3338 | if (local->st_stateowner->so_is_open_owner == 0) |
| 3339 | continue; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3340 | if (local->st_stateowner == &oo->oo_owner) { |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3341 | ret = local; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3342 | break; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3344 | } |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3345 | spin_unlock(&fp->fi_lock); |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3346 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | } |
| 3348 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 3349 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 3350 | { |
| 3351 | int flags = 0; |
| 3352 | |
| 3353 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 3354 | flags |= NFSD_MAY_READ; |
| 3355 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 3356 | flags |= NFSD_MAY_WRITE; |
| 3357 | return flags; |
| 3358 | } |
| 3359 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3360 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 3362 | struct nfsd4_open *open) |
| 3363 | { |
| 3364 | struct iattr iattr = { |
| 3365 | .ia_valid = ATTR_SIZE, |
| 3366 | .ia_size = 0, |
| 3367 | }; |
| 3368 | if (!open->op_truncate) |
| 3369 | return 0; |
| 3370 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 3371 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3372 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); |
| 3373 | } |
| 3374 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3375 | 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] | 3376 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
| 3377 | struct nfsd4_open *open) |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3378 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3379 | struct file *filp = NULL; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3380 | __be32 status; |
| 3381 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 3382 | int access = nfs4_access_to_access(open->op_share_access); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3383 | unsigned char old_access_bmap, old_deny_bmap; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3384 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3385 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3386 | |
| 3387 | /* |
| 3388 | * Are we trying to set a deny mode that would conflict with |
| 3389 | * current access? |
| 3390 | */ |
| 3391 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 3392 | if (status != nfs_ok) { |
| 3393 | spin_unlock(&fp->fi_lock); |
| 3394 | goto out; |
| 3395 | } |
| 3396 | |
| 3397 | /* set access to the file */ |
| 3398 | status = nfs4_file_get_access(fp, open->op_share_access); |
| 3399 | if (status != nfs_ok) { |
| 3400 | spin_unlock(&fp->fi_lock); |
| 3401 | goto out; |
| 3402 | } |
| 3403 | |
| 3404 | /* Set access bits in stateid */ |
| 3405 | old_access_bmap = stp->st_access_bmap; |
| 3406 | set_access(open->op_share_access, stp); |
| 3407 | |
| 3408 | /* Set new deny mask */ |
| 3409 | old_deny_bmap = stp->st_deny_bmap; |
| 3410 | set_deny(open->op_share_deny, stp); |
| 3411 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 3412 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3413 | if (!fp->fi_fds[oflag]) { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3414 | spin_unlock(&fp->fi_lock); |
| 3415 | status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3416 | if (status) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3417 | goto out_put_access; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3418 | spin_lock(&fp->fi_lock); |
| 3419 | if (!fp->fi_fds[oflag]) { |
| 3420 | fp->fi_fds[oflag] = filp; |
| 3421 | filp = NULL; |
| 3422 | } |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3423 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3424 | spin_unlock(&fp->fi_lock); |
| 3425 | if (filp) |
| 3426 | fput(filp); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3427 | |
| 3428 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3429 | if (status) |
| 3430 | goto out_put_access; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3431 | out: |
| 3432 | return status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3433 | out_put_access: |
| 3434 | stp->st_access_bmap = old_access_bmap; |
| 3435 | nfs4_file_put_access(fp, open->op_share_access); |
| 3436 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); |
| 3437 | goto out; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3438 | } |
| 3439 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3440 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3441 | 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] | 3442 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3443 | __be32 status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3444 | unsigned char old_deny_bmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 3446 | if (!test_access(open->op_share_access, stp)) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3447 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3448 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3449 | /* test and set deny mode */ |
| 3450 | spin_lock(&fp->fi_lock); |
| 3451 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 3452 | if (status == nfs_ok) { |
| 3453 | old_deny_bmap = stp->st_deny_bmap; |
| 3454 | set_deny(open->op_share_deny, stp); |
| 3455 | fp->fi_share_deny |= |
| 3456 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 3457 | } |
| 3458 | spin_unlock(&fp->fi_lock); |
| 3459 | |
| 3460 | if (status != nfs_ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3463 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3464 | if (status != nfs_ok) |
| 3465 | reset_union_bmap_deny(old_deny_bmap, stp); |
| 3466 | return status; |
| 3467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | static void |
J. Bruce Fields | 1255a8f | 2012-03-06 14:35:16 -0500 | [diff] [blame] | 3470 | nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | { |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3472 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | } |
| 3474 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 3475 | /* Should we give out recallable state?: */ |
| 3476 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 3477 | { |
| 3478 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 3479 | return true; |
| 3480 | /* |
| 3481 | * In the sessions case, since we don't have to establish a |
| 3482 | * separate connection for callbacks, we assume it's OK |
| 3483 | * until we hear otherwise: |
| 3484 | */ |
| 3485 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 3486 | } |
| 3487 | |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 3488 | 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] | 3489 | { |
| 3490 | struct file_lock *fl; |
| 3491 | |
| 3492 | fl = locks_alloc_lock(); |
| 3493 | if (!fl) |
| 3494 | return NULL; |
| 3495 | locks_init_lock(fl); |
| 3496 | fl->fl_lmops = &nfsd_lease_mng_ops; |
J. Bruce Fields | 617588d | 2011-07-01 15:18:34 -0400 | [diff] [blame] | 3497 | fl->fl_flags = FL_DELEG; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3498 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 3499 | fl->fl_end = OFFSET_MAX; |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 3500 | fl->fl_owner = (fl_owner_t)fp; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3501 | fl->fl_pid = current->tgid; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3502 | return fl; |
| 3503 | } |
| 3504 | |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3505 | static int nfs4_setlease(struct nfs4_delegation *dp) |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3506 | { |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3507 | struct nfs4_file *fp = dp->dl_file; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3508 | struct file_lock *fl; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3509 | struct file *filp; |
| 3510 | int status = 0; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3511 | |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 3512 | fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3513 | if (!fl) |
| 3514 | return -ENOMEM; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3515 | filp = find_readable_file(fp); |
| 3516 | if (!filp) { |
| 3517 | /* We should always have a readable file here */ |
| 3518 | WARN_ON_ONCE(1); |
| 3519 | return -EBADF; |
| 3520 | } |
| 3521 | fl->fl_file = filp; |
| 3522 | status = vfs_setlease(filp, fl->fl_type, &fl); |
| 3523 | if (status) { |
| 3524 | locks_free_lock(fl); |
| 3525 | goto out_fput; |
| 3526 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3527 | spin_lock(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3528 | spin_lock(&fp->fi_lock); |
| 3529 | /* Did the lease get broken before we took the lock? */ |
| 3530 | status = -EAGAIN; |
| 3531 | if (fp->fi_had_conflict) |
| 3532 | goto out_unlock; |
| 3533 | /* Race breaker */ |
| 3534 | if (fp->fi_lease) { |
| 3535 | status = 0; |
| 3536 | atomic_inc(&fp->fi_delegees); |
| 3537 | hash_delegation_locked(dp, fp); |
| 3538 | goto out_unlock; |
| 3539 | } |
| 3540 | fp->fi_lease = fl; |
| 3541 | fp->fi_deleg_file = filp; |
| 3542 | atomic_set(&fp->fi_delegees, 1); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 3543 | hash_delegation_locked(dp, fp); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3544 | spin_unlock(&fp->fi_lock); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3545 | spin_unlock(&state_lock); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3546 | return 0; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3547 | out_unlock: |
| 3548 | spin_unlock(&fp->fi_lock); |
| 3549 | spin_unlock(&state_lock); |
| 3550 | out_fput: |
| 3551 | fput(filp); |
J. Bruce Fields | e873088 | 2012-01-23 13:52:01 -0500 | [diff] [blame] | 3552 | return status; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3553 | } |
| 3554 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 3555 | static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp) |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3556 | { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3557 | int status = 0; |
| 3558 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 3559 | if (fp->fi_had_conflict) |
| 3560 | return -EAGAIN; |
| 3561 | get_nfs4_file(fp); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3562 | spin_lock(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3563 | spin_lock(&fp->fi_lock); |
| 3564 | dp->dl_file = fp; |
| 3565 | if (!fp->fi_lease) { |
| 3566 | spin_unlock(&fp->fi_lock); |
| 3567 | spin_unlock(&state_lock); |
| 3568 | return nfs4_setlease(dp); |
| 3569 | } |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 3570 | atomic_inc(&fp->fi_delegees); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3571 | if (fp->fi_had_conflict) { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3572 | status = -EAGAIN; |
| 3573 | goto out_unlock; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3574 | } |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 3575 | hash_delegation_locked(dp, fp); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3576 | out_unlock: |
| 3577 | spin_unlock(&fp->fi_lock); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3578 | spin_unlock(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3579 | return status; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3580 | } |
| 3581 | |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 3582 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 3583 | { |
| 3584 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3585 | if (status == -EAGAIN) |
| 3586 | open->op_why_no_deleg = WND4_CONTENTION; |
| 3587 | else { |
| 3588 | open->op_why_no_deleg = WND4_RESOURCE; |
| 3589 | switch (open->op_deleg_want) { |
| 3590 | case NFS4_SHARE_WANT_READ_DELEG: |
| 3591 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 3592 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 3593 | break; |
| 3594 | case NFS4_SHARE_WANT_CANCEL: |
| 3595 | open->op_why_no_deleg = WND4_CANCELLED; |
| 3596 | break; |
| 3597 | case NFS4_SHARE_WANT_NO_DELEG: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 3598 | WARN_ON_ONCE(1); |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 3599 | } |
| 3600 | } |
| 3601 | } |
| 3602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3603 | /* |
| 3604 | * Attempt to hand out a delegation. |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3605 | * |
| 3606 | * Note we don't support write delegations, and won't until the vfs has |
| 3607 | * proper support for them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3608 | */ |
| 3609 | static void |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame^] | 3610 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
| 3611 | struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | { |
| 3613 | struct nfs4_delegation *dp; |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame^] | 3614 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
| 3615 | struct nfs4_client *clp = stp->st_stid.sc_client; |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 3616 | int cb_up; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3617 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3619 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3620 | open->op_recall = 0; |
| 3621 | switch (open->op_claim_type) { |
| 3622 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 3623 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3624 | open->op_recall = 1; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3625 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 3626 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3627 | break; |
| 3628 | case NFS4_OPEN_CLAIM_NULL: |
Ming Chen | ed47b06 | 2014-01-09 21:26:10 +0000 | [diff] [blame] | 3629 | case NFS4_OPEN_CLAIM_FH: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3630 | /* |
| 3631 | * Let's not give out any delegations till everyone's |
| 3632 | * had the chance to reclaim theirs.... |
| 3633 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame^] | 3634 | if (locks_in_grace(clp->net)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3635 | goto out_no_deleg; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3636 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3637 | goto out_no_deleg; |
Steve Dickson | 9a0590a | 2013-05-15 14:51:49 -0400 | [diff] [blame] | 3638 | /* |
| 3639 | * Also, if the file was opened for write or |
| 3640 | * create, there's a good chance the client's |
| 3641 | * about to write to it, resulting in an |
| 3642 | * immediate recall (since we don't support |
| 3643 | * write delegations): |
| 3644 | */ |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3645 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3646 | goto out_no_deleg; |
| 3647 | if (open->op_create == NFS4_OPEN_CREATE) |
| 3648 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3649 | break; |
| 3650 | default: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3651 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3652 | } |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame^] | 3653 | dp = alloc_init_deleg(clp, fh); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3654 | if (dp == NULL) |
| 3655 | goto out_no_deleg; |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 3656 | status = nfs4_set_delegation(dp, stp->st_file); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3657 | if (status) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3658 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3659 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 3660 | 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] | 3661 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 3662 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 3663 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3664 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3665 | return; |
| 3666 | out_free: |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 3667 | nfs4_put_delegation(dp); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3668 | out_no_deleg: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3669 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 3670 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 3671 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3672 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 3673 | open->op_recall = 1; |
| 3674 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3675 | |
| 3676 | /* 4.1 client asking for a delegation? */ |
| 3677 | if (open->op_deleg_want) |
| 3678 | nfsd4_open_deleg_none_ext(open, status); |
| 3679 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | } |
| 3681 | |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 3682 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 3683 | struct nfs4_delegation *dp) |
| 3684 | { |
| 3685 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 3686 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 3687 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3688 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 3689 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 3690 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 3691 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3692 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 3693 | } |
| 3694 | /* Otherwise the client must be confused wanting a delegation |
| 3695 | * it already has, therefore we don't return |
| 3696 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 3697 | */ |
| 3698 | } |
| 3699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3700 | /* |
| 3701 | * called with nfs4_lock_state() held. |
| 3702 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3703 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 3705 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3706 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3707 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3708 | struct nfs4_file *fp = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3709 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3710 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3711 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | /* |
| 3714 | * Lookup file; if found, lookup stateid and check open request, |
| 3715 | * and check for delegations in the process of being recalled. |
| 3716 | * If not found, create the nfs4_file struct |
| 3717 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 3718 | fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3719 | if (fp != open->op_file) { |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 3720 | status = nfs4_check_deleg(cl, open, &dp); |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3721 | if (status) |
| 3722 | goto out; |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3723 | stp = nfsd4_find_existing_open(fp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | } else { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3725 | open->op_file = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3726 | status = nfserr_bad_stateid; |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3727 | if (nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3728 | goto out; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 3729 | status = nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | } |
| 3731 | |
| 3732 | /* |
| 3733 | * OPEN the file, or upgrade an existing OPEN. |
| 3734 | * If truncate fails, the OPEN fails. |
| 3735 | */ |
| 3736 | if (stp) { |
| 3737 | /* Stateid was found, this is an OPEN upgrade */ |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 3738 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3739 | if (status) |
| 3740 | goto out; |
| 3741 | } else { |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3742 | stp = open->op_stp; |
| 3743 | open->op_stp = NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 3744 | init_open_stateid(stp, fp, open); |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 3745 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
| 3746 | if (status) { |
| 3747 | release_open_stateid(stp); |
| 3748 | goto out; |
| 3749 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | } |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3751 | update_stateid(&stp->st_stid.sc_stateid); |
| 3752 | memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3753 | |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3754 | if (nfsd4_has_session(&resp->cstate)) { |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3755 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 3756 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3757 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 3758 | goto nodeleg; |
| 3759 | } |
| 3760 | } |
| 3761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3762 | /* |
| 3763 | * Attempt to hand out a delegation. No error return, because the |
| 3764 | * OPEN succeeds even if we fail. |
| 3765 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame^] | 3766 | nfs4_open_delegation(current_fh, open, stp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3767 | nodeleg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | status = nfs_ok; |
| 3769 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 3770 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3771 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | out: |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3773 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 3774 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 3775 | open->op_deleg_want) |
| 3776 | nfsd4_deleg_xgrade_none_ext(open, dp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3777 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3778 | if (fp) |
| 3779 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 3780 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
J. Bruce Fields | 1255a8f | 2012-03-06 14:35:16 -0500 | [diff] [blame] | 3781 | nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3782 | /* |
| 3783 | * To finish the open response, we just need to set the rflags. |
| 3784 | */ |
| 3785 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3786 | if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) && |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3787 | !nfsd4_has_session(&resp->cstate)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3788 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
| 3789 | |
| 3790 | return status; |
| 3791 | } |
| 3792 | |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 3793 | void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status) |
| 3794 | { |
| 3795 | if (open->op_openowner) { |
| 3796 | struct nfs4_openowner *oo = open->op_openowner; |
| 3797 | |
| 3798 | if (!list_empty(&oo->oo_owner.so_stateids)) |
| 3799 | list_del_init(&oo->oo_close_lru); |
| 3800 | if (oo->oo_flags & NFS4_OO_NEW) { |
| 3801 | if (status) { |
| 3802 | release_openowner(oo); |
| 3803 | open->op_openowner = NULL; |
| 3804 | } else |
| 3805 | oo->oo_flags &= ~NFS4_OO_NEW; |
| 3806 | } |
| 3807 | } |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3808 | if (open->op_file) |
| 3809 | nfsd4_free_file(open->op_file); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3810 | if (open->op_stp) |
J. Bruce Fields | ef79859 | 2012-08-29 10:32:54 -0700 | [diff] [blame] | 3811 | free_generic_stateid(open->op_stp); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 3812 | } |
| 3813 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3814 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3815 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3816 | clientid_t *clid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3817 | { |
| 3818 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3819 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 3820 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | |
| 3822 | nfs4_lock_state(); |
| 3823 | dprintk("process_renew(%08x/%08x): starting\n", |
| 3824 | clid->cl_boot, clid->cl_id); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3825 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 3826 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3827 | goto out; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3828 | clp = cstate->clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | status = nfserr_cb_path_down; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 3830 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 3831 | && clp->cl_cb_state != NFSD4_CB_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3832 | goto out; |
| 3833 | status = nfs_ok; |
| 3834 | out: |
| 3835 | nfs4_unlock_state(); |
| 3836 | return status; |
| 3837 | } |
| 3838 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3839 | static void |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 3840 | nfsd4_end_grace(struct nfsd_net *nn) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3841 | { |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 3842 | /* do nothing if grace period already ended */ |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 3843 | if (nn->grace_ended) |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 3844 | return; |
| 3845 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3846 | dprintk("NFSD: end of grace period\n"); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 3847 | nn->grace_ended = true; |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 3848 | nfsd4_record_grace_done(nn, nn->boot_time); |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 3849 | locks_end_grace(&nn->nfsd4_manager); |
J. Bruce Fields | e46b498 | 2010-03-01 19:21:21 -0500 | [diff] [blame] | 3850 | /* |
| 3851 | * Now that every NFSv4 client has had the chance to recover and |
| 3852 | * to see the (possibly new, possibly shorter) lease time, we |
| 3853 | * can safely set the next grace time to the current lease time: |
| 3854 | */ |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 3855 | nn->nfsd4_grace = nn->nfsd4_lease; |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3856 | } |
| 3857 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 3858 | static time_t |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3859 | nfs4_laundromat(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3860 | { |
| 3861 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3862 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3863 | struct nfs4_delegation *dp; |
| 3864 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 3d73371 | 2012-11-27 14:11:44 +0300 | [diff] [blame] | 3865 | time_t cutoff = get_seconds() - nn->nfsd4_lease; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3866 | time_t t, new_timeo = nn->nfsd4_lease; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3867 | |
| 3868 | nfs4_lock_state(); |
| 3869 | |
| 3870 | dprintk("NFSD: laundromat service - starting\n"); |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 3871 | nfsd4_end_grace(nn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3872 | INIT_LIST_HEAD(&reaplist); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3873 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 3874 | list_for_each_safe(pos, next, &nn->client_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3875 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
| 3876 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { |
| 3877 | t = clp->cl_time - cutoff; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3878 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3879 | break; |
| 3880 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3881 | if (mark_client_expired_locked(clp)) { |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 3882 | dprintk("NFSD: client in use (clientid %08x)\n", |
| 3883 | clp->cl_clientid.cl_id); |
| 3884 | continue; |
| 3885 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3886 | list_move(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3887 | } |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3888 | spin_unlock(&nn->client_lock); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3889 | list_for_each_safe(pos, next, &reaplist) { |
| 3890 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 3892 | clp->cl_clientid.cl_id); |
| 3893 | expire_client(clp); |
| 3894 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3895 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 3896 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3897 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Stanislav Kinsbursky | 4e37a7c2 | 2012-11-26 15:22:03 +0300 | [diff] [blame] | 3898 | if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn) |
| 3899 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3901 | t = dp->dl_time - cutoff; |
| 3902 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3903 | break; |
| 3904 | } |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 3905 | unhash_delegation_locked(dp); |
| 3906 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3907 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3908 | spin_unlock(&state_lock); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 3909 | while (!list_empty(&reaplist)) { |
| 3910 | dp = list_first_entry(&reaplist, struct nfs4_delegation, |
| 3911 | dl_recall_lru); |
| 3912 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3913 | revoke_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3914 | } |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3915 | list_for_each_safe(pos, next, &nn->close_lru) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3916 | oo = container_of(pos, struct nfs4_openowner, oo_close_lru); |
| 3917 | if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3918 | t = oo->oo_time - cutoff; |
| 3919 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3920 | break; |
| 3921 | } |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3922 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3923 | } |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3924 | new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3925 | nfs4_unlock_state(); |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3926 | return new_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3927 | } |
| 3928 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 3929 | static struct workqueue_struct *laundry_wq; |
| 3930 | static void laundromat_main(struct work_struct *); |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 3931 | |
| 3932 | static void |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3933 | laundromat_main(struct work_struct *laundry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3934 | { |
| 3935 | time_t t; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3936 | struct delayed_work *dwork = container_of(laundry, struct delayed_work, |
| 3937 | work); |
| 3938 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 3939 | laundromat_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3941 | t = nfs4_laundromat(nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3943 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | } |
| 3945 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3946 | static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp) |
NeilBrown | f881651 | 2005-07-07 17:59:25 -0700 | [diff] [blame] | 3947 | { |
Trond Myklebust | b07c54a | 2014-07-23 16:17:40 -0400 | [diff] [blame] | 3948 | if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_file->fi_fhandle)) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3949 | return nfserr_bad_stateid; |
| 3950 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3951 | } |
| 3952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3954 | access_permit_read(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3956 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 3957 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 3958 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3959 | } |
| 3960 | |
| 3961 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3962 | access_permit_write(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3964 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 3965 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | } |
| 3967 | |
| 3968 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3969 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3970 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3971 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3972 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 3973 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 3974 | if (stp->st_openstp) |
| 3975 | stp = stp->st_openstp; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3976 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3977 | goto out; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3978 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | goto out; |
| 3980 | status = nfs_ok; |
| 3981 | out: |
| 3982 | return status; |
| 3983 | } |
| 3984 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3985 | static inline __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3986 | 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] | 3987 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 3988 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3989 | return nfs_ok; |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3990 | else if (locks_in_grace(net)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3991 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3992 | * conflicting state; so we must wait out the grace period. */ |
| 3993 | return nfserr_grace; |
| 3994 | } else if (flags & WR_STATE) |
| 3995 | return nfs4_share_conflict(current_fh, |
| 3996 | NFS4_SHARE_DENY_WRITE); |
| 3997 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 3998 | return nfs4_share_conflict(current_fh, |
| 3999 | NFS4_SHARE_DENY_READ); |
| 4000 | } |
| 4001 | |
| 4002 | /* |
| 4003 | * Allow READ/WRITE during grace period on recovered state only for files |
| 4004 | * that are not able to provide mandatory locking. |
| 4005 | */ |
| 4006 | static inline int |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4007 | grace_disallows_io(struct net *net, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | { |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4009 | return locks_in_grace(net) && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | } |
| 4011 | |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4012 | /* Returns true iff a is later than b: */ |
| 4013 | static bool stateid_generation_after(stateid_t *a, stateid_t *b) |
| 4014 | { |
Jim Rees | 1a9357f | 2013-05-17 17:33:00 -0400 | [diff] [blame] | 4015 | return (s32)(a->si_generation - b->si_generation) > 0; |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4016 | } |
| 4017 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 4018 | 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] | 4019 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4020 | /* |
| 4021 | * When sessions are used the stateid generation number is ignored |
| 4022 | * when it is zero. |
| 4023 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 4024 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4025 | return nfs_ok; |
| 4026 | |
| 4027 | if (in->si_generation == ref->si_generation) |
| 4028 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4029 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4030 | /* If the client sends us a stateid from the future, it's buggy: */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4031 | if (stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4032 | return nfserr_bad_stateid; |
| 4033 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4034 | * However, we could see a stateid from the past, even from a |
| 4035 | * non-buggy client. For example, if the client sends a lock |
| 4036 | * while some IO is outstanding, the lock may bump si_generation |
| 4037 | * while the IO is still in flight. The client could avoid that |
| 4038 | * situation by waiting for responses on all the IO requests, |
| 4039 | * but better performance may result in retrying IO that |
| 4040 | * receives an old_stateid error if requests are rarely |
| 4041 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4042 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4043 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4044 | } |
| 4045 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4046 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4047 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4048 | struct nfs4_stid *s; |
| 4049 | struct nfs4_ol_stateid *ols; |
| 4050 | __be32 status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4051 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4052 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 4053 | return nfserr_bad_stateid; |
| 4054 | /* Client debugging aid. */ |
| 4055 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { |
| 4056 | char addr_str[INET6_ADDRSTRLEN]; |
| 4057 | rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, |
| 4058 | sizeof(addr_str)); |
| 4059 | pr_warn_ratelimited("NFSD: client %s testing state ID " |
| 4060 | "with incorrect client ID\n", addr_str); |
| 4061 | return nfserr_bad_stateid; |
| 4062 | } |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4063 | s = find_stateid(cl, stateid); |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4064 | if (!s) |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4065 | return nfserr_bad_stateid; |
J. Bruce Fields | 36279ac | 2011-09-26 12:53:00 -0400 | [diff] [blame] | 4066 | status = check_stateid_generation(stateid, &s->sc_stateid, 1); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4067 | if (status) |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4068 | return status; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4069 | switch (s->sc_type) { |
| 4070 | case NFS4_DELEG_STID: |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4071 | return nfs_ok; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4072 | case NFS4_REVOKED_DELEG_STID: |
| 4073 | return nfserr_deleg_revoked; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4074 | case NFS4_OPEN_STID: |
| 4075 | case NFS4_LOCK_STID: |
| 4076 | ols = openlockstateid(s); |
| 4077 | if (ols->st_stateowner->so_is_open_owner |
| 4078 | && !(openowner(ols->st_stateowner)->oo_flags |
| 4079 | & NFS4_OO_CONFIRMED)) |
| 4080 | return nfserr_bad_stateid; |
| 4081 | return nfs_ok; |
| 4082 | default: |
| 4083 | printk("unknown stateid type %x\n", s->sc_type); |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 4084 | /* Fallthrough */ |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4085 | case NFS4_CLOSED_STID: |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 4086 | case NFS4_CLOSED_DELEG_STID: |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4087 | return nfserr_bad_stateid; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4088 | } |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4089 | } |
| 4090 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4091 | static __be32 |
| 4092 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
| 4093 | stateid_t *stateid, unsigned char typemask, |
| 4094 | struct nfs4_stid **s, struct nfsd_net *nn) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4095 | { |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 4096 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4097 | |
| 4098 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 4099 | return nfserr_bad_stateid; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4100 | status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn); |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4101 | if (status == nfserr_stale_clientid) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4102 | if (cstate->session) |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4103 | return nfserr_bad_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4104 | return nfserr_stale_stateid; |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4105 | } |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 4106 | if (status) |
| 4107 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4108 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4109 | if (!*s) |
| 4110 | return nfserr_bad_stateid; |
| 4111 | return nfs_ok; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4112 | } |
| 4113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | /* |
| 4115 | * Checks for stateid operations |
| 4116 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4117 | __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4118 | nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate, |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4119 | stateid_t *stateid, int flags, struct file **filpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4120 | { |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4121 | struct nfs4_stid *s; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4122 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4123 | struct nfs4_delegation *dp = NULL; |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4124 | struct svc_fh *current_fh = &cstate->current_fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | struct inode *ino = current_fh->fh_dentry->d_inode; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4126 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4127 | struct file *file = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4128 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | if (filpp) |
| 4131 | *filpp = NULL; |
| 4132 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4133 | if (grace_disallows_io(net, ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | return nfserr_grace; |
| 4135 | |
| 4136 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4137 | return check_special_stateids(net, current_fh, stateid, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4138 | |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4139 | nfs4_lock_state(); |
| 4140 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4141 | status = nfsd4_lookup_stateid(cstate, stateid, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4142 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4143 | &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4144 | if (status) |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4145 | goto out; |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4146 | status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate)); |
| 4147 | if (status) |
| 4148 | goto out; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4149 | switch (s->sc_type) { |
| 4150 | case NFS4_DELEG_STID: |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4151 | dp = delegstateid(s); |
J. Bruce Fields | dc9bf70 | 2009-02-21 11:14:43 -0800 | [diff] [blame] | 4152 | status = nfs4_check_delegmode(dp, flags); |
| 4153 | if (status) |
| 4154 | goto out; |
Dan Carpenter | 43b0178 | 2010-10-27 23:19:04 +0200 | [diff] [blame] | 4155 | if (filpp) { |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4156 | file = dp->dl_file->fi_deleg_file; |
| 4157 | if (!file) { |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4158 | WARN_ON_ONCE(1); |
| 4159 | status = nfserr_serverfault; |
| 4160 | goto out; |
| 4161 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4162 | get_file(file); |
Dan Carpenter | 43b0178 | 2010-10-27 23:19:04 +0200 | [diff] [blame] | 4163 | } |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4164 | break; |
| 4165 | case NFS4_OPEN_STID: |
| 4166 | case NFS4_LOCK_STID: |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4167 | stp = openlockstateid(s); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4168 | status = nfs4_check_fh(current_fh, stp); |
| 4169 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4171 | if (stp->st_stateowner->so_is_open_owner |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4172 | && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4173 | goto out; |
J. Bruce Fields | a4455be | 2009-02-21 10:40:22 -0800 | [diff] [blame] | 4174 | status = nfs4_check_openmode(stp, flags); |
| 4175 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4176 | goto out; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4177 | if (filpp) { |
| 4178 | if (flags & RD_STATE) |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4179 | file = find_readable_file(stp->st_file); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4180 | else |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4181 | file = find_writeable_file(stp->st_file); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4182 | } |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4183 | break; |
| 4184 | default: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4185 | status = nfserr_bad_stateid; |
| 4186 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | } |
| 4188 | status = nfs_ok; |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4189 | if (file) |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4190 | *filpp = file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | out: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4192 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | return status; |
| 4194 | } |
| 4195 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4196 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4197 | nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4198 | { |
J. Bruce Fields | a1b8ff4 | 2014-05-20 15:55:21 -0400 | [diff] [blame] | 4199 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
| 4200 | |
| 4201 | if (check_for_locks(stp->st_file, lo)) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4202 | return nfserr_locks_held; |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4203 | release_lockowner_if_empty(lo); |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4204 | return nfs_ok; |
| 4205 | } |
| 4206 | |
| 4207 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4208 | * Test if the stateid is valid |
| 4209 | */ |
| 4210 | __be32 |
| 4211 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4212 | struct nfsd4_test_stateid *test_stateid) |
| 4213 | { |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4214 | struct nfsd4_test_stateid_id *stateid; |
| 4215 | struct nfs4_client *cl = cstate->session->se_client; |
| 4216 | |
| 4217 | nfs4_lock_state(); |
| 4218 | 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] | 4219 | stateid->ts_id_status = |
| 4220 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4221 | nfs4_unlock_state(); |
| 4222 | |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4223 | return nfs_ok; |
| 4224 | } |
| 4225 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4226 | __be32 |
| 4227 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4228 | struct nfsd4_free_stateid *free_stateid) |
| 4229 | { |
| 4230 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4231 | struct nfs4_stid *s; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4232 | struct nfs4_delegation *dp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4233 | struct nfs4_client *cl = cstate->session->se_client; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4234 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4235 | |
| 4236 | nfs4_lock_state(); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4237 | s = find_stateid(cl, stateid); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4238 | if (!s) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4239 | goto out; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4240 | switch (s->sc_type) { |
| 4241 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4242 | ret = nfserr_locks_held; |
| 4243 | goto out; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4244 | case NFS4_OPEN_STID: |
| 4245 | case NFS4_LOCK_STID: |
| 4246 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 4247 | if (ret) |
| 4248 | goto out; |
| 4249 | if (s->sc_type == NFS4_LOCK_STID) |
| 4250 | ret = nfsd4_free_lock_stateid(openlockstateid(s)); |
| 4251 | else |
| 4252 | ret = nfserr_locks_held; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4253 | break; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4254 | case NFS4_REVOKED_DELEG_STID: |
| 4255 | dp = delegstateid(s); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 4256 | spin_lock(&cl->cl_lock); |
| 4257 | list_del_init(&dp->dl_recall_lru); |
| 4258 | spin_unlock(&cl->cl_lock); |
| 4259 | nfs4_put_delegation(dp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4260 | ret = nfs_ok; |
| 4261 | break; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4262 | default: |
| 4263 | ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4264 | } |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4265 | out: |
| 4266 | nfs4_unlock_state(); |
| 4267 | return ret; |
| 4268 | } |
| 4269 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4270 | static inline int |
| 4271 | setlkflg (int type) |
| 4272 | { |
| 4273 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 4274 | RD_STATE : WR_STATE; |
| 4275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4277 | 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] | 4278 | { |
| 4279 | struct svc_fh *current_fh = &cstate->current_fh; |
| 4280 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 4281 | __be32 status; |
| 4282 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4283 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 4284 | if (status) |
| 4285 | return status; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4286 | if (stp->st_stid.sc_type == NFS4_CLOSED_STID |
| 4287 | || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4288 | /* |
| 4289 | * "Closed" stateid's exist *only* to return |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4290 | * nfserr_replay_me from the previous step, and |
| 4291 | * revoked delegations are kept only for free_stateid. |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4292 | */ |
| 4293 | return nfserr_bad_stateid; |
| 4294 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); |
| 4295 | if (status) |
| 4296 | return status; |
| 4297 | return nfs4_check_fh(current_fh, stp); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4298 | } |
| 4299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | /* |
| 4301 | * Checks for sequence id mutating operations. |
| 4302 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4303 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4304 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 4305 | stateid_t *stateid, char typemask, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4306 | struct nfs4_ol_stateid **stpp, |
| 4307 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4309 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4310 | struct nfs4_stid *s; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4311 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4313 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
| 4314 | seqid, STATEID_VAL(stateid)); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | *stpp = NULL; |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4317 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4318 | if (status) |
| 4319 | return status; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4320 | stp = openlockstateid(s); |
J. Bruce Fields | 3d74e6a | 2013-03-22 17:44:19 -0400 | [diff] [blame] | 4321 | if (!nfsd4_has_session(cstate)) |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4322 | cstate->replay_owner = stp->st_stateowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4324 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
| 4325 | if (!status) |
| 4326 | *stpp = stp; |
| 4327 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4328 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 4329 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4330 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 4331 | 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] | 4332 | { |
| 4333 | __be32 status; |
| 4334 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4336 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4337 | NFS4_OPEN_STID, stpp, nn); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 4338 | if (status) |
| 4339 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4340 | oo = openowner((*stpp)->st_stateowner); |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4341 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4342 | return nfserr_bad_stateid; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4343 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | } |
| 4345 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4346 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4347 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4348 | struct nfsd4_open_confirm *oc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4350 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4351 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4352 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4353 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4355 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 4356 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4357 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4358 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 4359 | if (status) |
| 4360 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 | |
| 4362 | nfs4_lock_state(); |
| 4363 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4364 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4365 | oc->oc_seqid, &oc->oc_req_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4366 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4367 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4368 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4369 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4370 | status = nfserr_bad_stateid; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4371 | if (oo->oo_flags & NFS4_OO_CONFIRMED) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4372 | goto out; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4373 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4374 | update_stateid(&stp->st_stid.sc_stateid); |
| 4375 | memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4376 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4377 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 4378 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 4379 | nfsd4_client_record_create(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4380 | status = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4381 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4382 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4383 | if (!cstate->replay_owner) |
| 4384 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4385 | return status; |
| 4386 | } |
| 4387 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4388 | 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] | 4389 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4390 | if (!test_access(access, stp)) |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4391 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 4392 | nfs4_file_put_access(stp->st_file, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4393 | clear_access(access, stp); |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4394 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 4395 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4396 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 4397 | { |
| 4398 | switch (to_access) { |
| 4399 | case NFS4_SHARE_ACCESS_READ: |
| 4400 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 4401 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 4402 | break; |
| 4403 | case NFS4_SHARE_ACCESS_WRITE: |
| 4404 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 4405 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 4406 | break; |
| 4407 | case NFS4_SHARE_ACCESS_BOTH: |
| 4408 | break; |
| 4409 | default: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4410 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | } |
| 4412 | } |
| 4413 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4414 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4415 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
| 4416 | struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4417 | struct nfsd4_open_downgrade *od) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4419 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4420 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4421 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4423 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 4424 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | |
J. Bruce Fields | c30e92d | 2011-10-10 17:34:31 -0400 | [diff] [blame] | 4426 | /* We don't yet support WANT bits: */ |
Benny Halevy | 2c8bd7e | 2012-02-16 20:57:09 +0200 | [diff] [blame] | 4427 | if (od->od_deleg_want) |
| 4428 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 4429 | od->od_deleg_want); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | |
| 4431 | nfs4_lock_state(); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4432 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4433 | &od->od_stateid, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4434 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | status = nfserr_inval; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4437 | if (!test_access(od->od_share_access, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 4438 | 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] | 4439 | stp->st_access_bmap, od->od_share_access); |
| 4440 | goto out; |
| 4441 | } |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4442 | if (!test_deny(od->od_share_deny, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 4443 | 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] | 4444 | stp->st_deny_bmap, od->od_share_deny); |
| 4445 | goto out; |
| 4446 | } |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4447 | nfs4_stateid_downgrade(stp, od->od_share_access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4448 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4449 | reset_union_bmap_deny(od->od_share_deny, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4451 | update_stateid(&stp->st_stid.sc_stateid); |
| 4452 | memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | status = nfs_ok; |
| 4454 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4455 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4456 | if (!cstate->replay_owner) |
| 4457 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | return status; |
| 4459 | } |
| 4460 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4461 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 4462 | { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4463 | struct nfs4_client *clp = s->st_stid.sc_client; |
| 4464 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 4465 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4466 | s->st_stid.sc_type = NFS4_CLOSED_STID; |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4467 | unhash_open_stateid(s); |
| 4468 | |
| 4469 | if (clp->cl_minorversion) { |
| 4470 | free_generic_stateid(s); |
| 4471 | if (list_empty(&oo->oo_owner.so_stateids)) |
| 4472 | release_openowner(oo); |
| 4473 | } else { |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 4474 | if (s->st_file) { |
| 4475 | put_nfs4_file(s->st_file); |
| 4476 | s->st_file = NULL; |
| 4477 | } |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4478 | oo->oo_last_closed_stid = s; |
| 4479 | /* |
| 4480 | * In the 4.0 case we need to keep the owners around a |
| 4481 | * little while to handle CLOSE replay. |
| 4482 | */ |
| 4483 | if (list_empty(&oo->oo_owner.so_stateids)) |
| 4484 | move_to_close_lru(oo, clp->net); |
| 4485 | } |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 4486 | } |
| 4487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | /* |
| 4489 | * nfs4_unlock_state() called after encode |
| 4490 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4491 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4492 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4493 | struct nfsd4_close *close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4495 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4496 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4497 | struct net *net = SVC_NET(rqstp); |
| 4498 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4500 | dprintk("NFSD: nfsd4_close on file %pd\n", |
| 4501 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | |
| 4503 | nfs4_lock_state(); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4504 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 4505 | &close->cl_stateid, |
| 4506 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4507 | &stp, nn); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4508 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4509 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | goto out; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4511 | update_stateid(&stp->st_stid.sc_stateid); |
| 4512 | memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4513 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4514 | nfsd4_close_open_stateid(stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | out: |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4516 | if (!cstate->replay_owner) |
| 4517 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | return status; |
| 4519 | } |
| 4520 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4521 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4522 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4523 | struct nfsd4_delegreturn *dr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4524 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4525 | struct nfs4_delegation *dp; |
| 4526 | stateid_t *stateid = &dr->dr_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4527 | struct nfs4_stid *s; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4528 | __be32 status; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4529 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4531 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4532 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4533 | |
| 4534 | nfs4_lock_state(); |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4535 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4536 | if (status) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4537 | goto out; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4538 | dp = delegstateid(s); |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 4539 | status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate)); |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4540 | if (status) |
| 4541 | goto out; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4542 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4543 | destroy_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | out: |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4545 | nfs4_unlock_state(); |
| 4546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4547 | return status; |
| 4548 | } |
| 4549 | |
| 4550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4553 | static inline u64 |
| 4554 | end_offset(u64 start, u64 len) |
| 4555 | { |
| 4556 | u64 end; |
| 4557 | |
| 4558 | end = start + len; |
| 4559 | return end >= start ? end: NFS4_MAX_UINT64; |
| 4560 | } |
| 4561 | |
| 4562 | /* last octet in a range */ |
| 4563 | static inline u64 |
| 4564 | last_byte_offset(u64 start, u64 len) |
| 4565 | { |
| 4566 | u64 end; |
| 4567 | |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4568 | WARN_ON_ONCE(!len); |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4569 | end = start + len; |
| 4570 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 4571 | } |
| 4572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4573 | /* |
| 4574 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 4575 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 4576 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 4577 | * locking, this prevents us from being completely protocol-compliant. The |
| 4578 | * real solution to this problem is to start using unsigned file offsets in |
| 4579 | * the VFS, but this is a very deep change! |
| 4580 | */ |
| 4581 | static inline void |
| 4582 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 4583 | { |
| 4584 | if (lock->fl_start < 0) |
| 4585 | lock->fl_start = OFFSET_MAX; |
| 4586 | if (lock->fl_end < 0) |
| 4587 | lock->fl_end = OFFSET_MAX; |
| 4588 | } |
| 4589 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4590 | /* Hack!: For now, we're defining this just so we can use a pointer to it |
| 4591 | * as a unique cookie to identify our (NFSv4's) posix locks. */ |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 4592 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4593 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | |
| 4595 | static inline void |
| 4596 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 4597 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4598 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4600 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4601 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
| 4602 | deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data, |
| 4603 | lo->lo_owner.so_owner.len, GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 4604 | if (!deny->ld_owner.data) |
| 4605 | /* We just don't care that much */ |
| 4606 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4607 | deny->ld_owner.len = lo->lo_owner.so_owner.len; |
| 4608 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4609 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 4610 | nevermind: |
| 4611 | deny->ld_owner.len = 0; |
| 4612 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4613 | deny->ld_clientid.cl_boot = 0; |
| 4614 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | } |
| 4616 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4617 | deny->ld_length = NFS4_MAX_UINT64; |
| 4618 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4619 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 4620 | deny->ld_type = NFS4_READ_LT; |
| 4621 | if (fl->fl_type != F_RDLCK) |
| 4622 | deny->ld_type = NFS4_WRITE_LT; |
| 4623 | } |
| 4624 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4625 | static struct nfs4_lockowner * |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4626 | find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner, |
| 4627 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | { |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4629 | unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner); |
| 4630 | struct nfs4_stateowner *so; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4632 | list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) { |
| 4633 | if (so->so_is_open_owner) |
| 4634 | continue; |
| 4635 | if (!same_owner_str(so, owner, clid)) |
| 4636 | continue; |
| 4637 | return lockowner(so); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | } |
| 4639 | return NULL; |
| 4640 | } |
| 4641 | |
| 4642 | /* |
| 4643 | * Alloc a lock owner structure. |
| 4644 | * 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] | 4645 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | * |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 4647 | * strhashval = ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4649 | static struct nfs4_lockowner * |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4650 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4651 | struct nfs4_lockowner *lo; |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4652 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4654 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 4655 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | return NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4657 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 4658 | lo->lo_owner.so_is_open_owner = 0; |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 4659 | /* It is the openowner seqid that will be incremented in encode in the |
| 4660 | * case of new lockowners; so increment the lock seqid manually: */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4661 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1; |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4662 | list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4663 | return lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | } |
| 4665 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4666 | static struct nfs4_ol_stateid * |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 4667 | alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, |
| 4668 | struct inode *inode, |
| 4669 | struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4670 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4671 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 4672 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 4674 | stp = nfs4_alloc_stateid(clp); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 4675 | if (stp == NULL) |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 4676 | return NULL; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 4677 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4678 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
| 4679 | stp->st_stateowner = &lo->lo_owner; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4680 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4681 | stp->st_file = fp; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4682 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4684 | stp->st_openstp = open_stp; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4685 | list_add(&stp->st_locks, &open_stp->st_locks); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4686 | spin_lock(&fp->fi_lock); |
| 4687 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 4688 | spin_unlock(&fp->fi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | return stp; |
| 4690 | } |
| 4691 | |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4692 | static struct nfs4_ol_stateid * |
| 4693 | find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) |
| 4694 | { |
| 4695 | struct nfs4_ol_stateid *lst; |
| 4696 | |
| 4697 | list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { |
| 4698 | if (lst->st_file == fp) |
| 4699 | return lst; |
| 4700 | } |
| 4701 | return NULL; |
| 4702 | } |
| 4703 | |
| 4704 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 4705 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4706 | check_lock_length(u64 offset, u64 length) |
| 4707 | { |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4708 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | LOFF_OVERFLOW(offset, length))); |
| 4710 | } |
| 4711 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4712 | 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] | 4713 | { |
| 4714 | struct nfs4_file *fp = lock_stp->st_file; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4715 | |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 4716 | lockdep_assert_held(&fp->fi_lock); |
| 4717 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4718 | if (test_access(access, lock_stp)) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4719 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 4720 | __nfs4_file_get_access(fp, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4721 | set_access(access, lock_stp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4722 | } |
| 4723 | |
J. Bruce Fields | 2355c59 | 2012-04-25 16:56:22 -0400 | [diff] [blame] | 4724 | static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4725 | { |
| 4726 | struct nfs4_file *fi = ost->st_file; |
| 4727 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 4728 | struct nfs4_client *cl = oo->oo_owner.so_client; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 4729 | struct inode *inode = cstate->current_fh.fh_dentry->d_inode; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4730 | struct nfs4_lockowner *lo; |
| 4731 | unsigned int strhashval; |
Stanislav Kinsbursky | 20e9e2b | 2012-11-14 18:21:46 +0300 | [diff] [blame] | 4732 | struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4733 | |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4734 | lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4735 | if (!lo) { |
| 4736 | strhashval = ownerstr_hashval(cl->cl_clientid.cl_id, |
| 4737 | &lock->v.new.owner); |
| 4738 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 4739 | if (lo == NULL) |
| 4740 | return nfserr_jukebox; |
| 4741 | } else { |
| 4742 | /* with an existing lockowner, seqids must be the same */ |
| 4743 | if (!cstate->minorversion && |
| 4744 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4745 | return nfserr_bad_seqid; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4746 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4747 | |
| 4748 | *lst = find_lock_stateid(lo, fi); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4749 | if (*lst == NULL) { |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 4750 | *lst = alloc_init_lock_stateid(lo, fi, inode, ost); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4751 | if (*lst == NULL) { |
| 4752 | release_lockowner_if_empty(lo); |
| 4753 | return nfserr_jukebox; |
| 4754 | } |
| 4755 | *new = true; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4756 | } |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4757 | return nfs_ok; |
| 4758 | } |
| 4759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4760 | /* |
| 4761 | * LOCK operation |
| 4762 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4763 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4764 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4765 | struct nfsd4_lock *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4767 | struct nfs4_openowner *open_sop = NULL; |
| 4768 | struct nfs4_lockowner *lock_sop = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4769 | struct nfs4_ol_stateid *lock_stp; |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 4770 | struct nfs4_file *fp; |
J. Bruce Fields | 7d94784 | 2010-08-20 18:09:31 -0400 | [diff] [blame] | 4771 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4772 | struct file_lock *file_lock = NULL; |
| 4773 | struct file_lock *conflock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4774 | __be32 status = 0; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4775 | bool new_state = false; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4776 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4777 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4778 | struct net *net = SVC_NET(rqstp); |
| 4779 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4780 | |
| 4781 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 4782 | (long long) lock->lk_offset, |
| 4783 | (long long) lock->lk_length); |
| 4784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4785 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 4786 | return nfserr_inval; |
| 4787 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4788 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 4789 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 4790 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 4791 | return status; |
| 4792 | } |
| 4793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | nfs4_lock_state(); |
| 4795 | |
| 4796 | if (lock->lk_is_new) { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4797 | struct nfs4_ol_stateid *open_stp = NULL; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 4798 | |
| 4799 | if (nfsd4_has_session(cstate)) |
| 4800 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
| 4801 | memcpy(&lock->v.new.clientid, |
| 4802 | &cstate->session->se_client->cl_clientid, |
| 4803 | sizeof(clientid_t)); |
| 4804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | status = nfserr_stale_clientid; |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 4806 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4810 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | lock->lk_new_open_seqid, |
| 4812 | &lock->lk_new_open_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4813 | &open_stp, nn); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 4814 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4815 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4816 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4817 | status = nfserr_bad_stateid; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 4818 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4819 | &lock->v.new.clientid)) |
| 4820 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4821 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
| 4822 | &lock_stp, &new_state); |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 4823 | } else |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4824 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4825 | lock->lk_old_lock_seqid, |
| 4826 | &lock->lk_old_lock_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4827 | NFS4_LOCK_STID, &lock_stp, nn); |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 4828 | if (status) |
| 4829 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4830 | lock_sop = lockowner(lock_stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4832 | lkflg = setlkflg(lock->lk_type); |
| 4833 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 4834 | if (status) |
| 4835 | goto out; |
| 4836 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 4837 | status = nfserr_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4838 | if (locks_in_grace(net) && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 4839 | goto out; |
| 4840 | status = nfserr_no_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4841 | if (!locks_in_grace(net) && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 4842 | goto out; |
| 4843 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4844 | file_lock = locks_alloc_lock(); |
| 4845 | if (!file_lock) { |
| 4846 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 4847 | status = nfserr_jukebox; |
| 4848 | goto out; |
| 4849 | } |
| 4850 | |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 4851 | fp = lock_stp->st_file; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4852 | locks_init_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | switch (lock->lk_type) { |
| 4854 | case NFS4_READ_LT: |
| 4855 | case NFS4_READW_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 4856 | spin_lock(&fp->fi_lock); |
| 4857 | filp = find_readable_file_locked(fp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4858 | if (filp) |
| 4859 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 4860 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4861 | file_lock->fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 4862 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | case NFS4_WRITE_LT: |
| 4864 | case NFS4_WRITEW_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 4865 | spin_lock(&fp->fi_lock); |
| 4866 | filp = find_writeable_file_locked(fp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4867 | if (filp) |
| 4868 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 4869 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4870 | file_lock->fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 4871 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4872 | default: |
| 4873 | status = nfserr_inval; |
| 4874 | goto out; |
| 4875 | } |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4876 | if (!filp) { |
| 4877 | status = nfserr_openmode; |
| 4878 | goto out; |
| 4879 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4880 | file_lock->fl_owner = (fl_owner_t)lock_sop; |
| 4881 | file_lock->fl_pid = current->tgid; |
| 4882 | file_lock->fl_file = filp; |
| 4883 | file_lock->fl_flags = FL_POSIX; |
| 4884 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 4885 | file_lock->fl_start = lock->lk_offset; |
| 4886 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 4887 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4889 | conflock = locks_alloc_lock(); |
| 4890 | if (!conflock) { |
| 4891 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 4892 | status = nfserr_jukebox; |
| 4893 | goto out; |
| 4894 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4895 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4896 | err = vfs_lock_file(filp, F_SETLK, file_lock, conflock); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4897 | switch (-err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | case 0: /* success! */ |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4899 | update_stateid(&lock_stp->st_stid.sc_stateid); |
| 4900 | memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4901 | sizeof(stateid_t)); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4902 | status = 0; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4903 | break; |
| 4904 | case (EAGAIN): /* conflock holds conflicting lock */ |
| 4905 | status = nfserr_denied; |
| 4906 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4907 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4908 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4909 | case (EDEADLK): |
| 4910 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4911 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 4912 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 4913 | 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] | 4914 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4915 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4917 | out: |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4918 | if (filp) |
| 4919 | fput(filp); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4920 | if (status && new_state) |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4921 | release_lock_stateid(lock_stp); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4922 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4923 | if (!cstate->replay_owner) |
| 4924 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4925 | if (file_lock) |
| 4926 | locks_free_lock(file_lock); |
| 4927 | if (conflock) |
| 4928 | locks_free_lock(conflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4929 | return status; |
| 4930 | } |
| 4931 | |
| 4932 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 4933 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 4934 | * so we do a temporary open here just to get an open file to pass to |
| 4935 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 4936 | * inode operation.) |
| 4937 | */ |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 4938 | 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] | 4939 | { |
| 4940 | struct file *file; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 4941 | __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); |
| 4942 | if (!err) { |
| 4943 | err = nfserrno(vfs_test_lock(file, lock)); |
| 4944 | nfsd_close(file); |
| 4945 | } |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 4946 | return err; |
| 4947 | } |
| 4948 | |
| 4949 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | * LOCKT operation |
| 4951 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4952 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4953 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4954 | struct nfsd4_lockt *lockt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | { |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4956 | struct file_lock *file_lock = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4957 | struct nfs4_lockowner *lo; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4958 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 4959 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4960 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4961 | if (locks_in_grace(SVC_NET(rqstp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | return nfserr_grace; |
| 4963 | |
| 4964 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 4965 | return nfserr_inval; |
| 4966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | nfs4_lock_state(); |
| 4968 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 4969 | if (!nfsd4_has_session(cstate)) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4970 | status = lookup_clientid(&lockt->lt_clientid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 4971 | if (status) |
| 4972 | goto out; |
| 4973 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 4975 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4978 | file_lock = locks_alloc_lock(); |
| 4979 | if (!file_lock) { |
| 4980 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 4981 | status = nfserr_jukebox; |
| 4982 | goto out; |
| 4983 | } |
| 4984 | locks_init_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | switch (lockt->lt_type) { |
| 4986 | case NFS4_READ_LT: |
| 4987 | case NFS4_READW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4988 | file_lock->fl_type = F_RDLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4989 | break; |
| 4990 | case NFS4_WRITE_LT: |
| 4991 | case NFS4_WRITEW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4992 | file_lock->fl_type = F_WRLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4993 | break; |
| 4994 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 4995 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4996 | status = nfserr_inval; |
| 4997 | goto out; |
| 4998 | } |
| 4999 | |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 5000 | lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5001 | if (lo) |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5002 | file_lock->fl_owner = (fl_owner_t)lo; |
| 5003 | file_lock->fl_pid = current->tgid; |
| 5004 | file_lock->fl_flags = FL_POSIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5005 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5006 | file_lock->fl_start = lockt->lt_offset; |
| 5007 | 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] | 5008 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5009 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5010 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5011 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5012 | if (status) |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 5013 | goto out; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5014 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5015 | if (file_lock->fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5016 | status = nfserr_denied; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5017 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | } |
| 5019 | out: |
| 5020 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5021 | if (file_lock) |
| 5022 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | return status; |
| 5024 | } |
| 5025 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5026 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5027 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 5028 | struct nfsd4_locku *locku) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5030 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5032 | struct file_lock *file_lock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5033 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5034 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5035 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 5036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 5038 | (long long) locku->lu_offset, |
| 5039 | (long long) locku->lu_length); |
| 5040 | |
| 5041 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 5042 | return nfserr_inval; |
| 5043 | |
| 5044 | nfs4_lock_state(); |
| 5045 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5046 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5047 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 5048 | &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5049 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | goto out; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5051 | filp = find_any_file(stp->st_file); |
| 5052 | if (!filp) { |
| 5053 | status = nfserr_lock_range; |
| 5054 | goto out; |
| 5055 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5056 | file_lock = locks_alloc_lock(); |
| 5057 | if (!file_lock) { |
| 5058 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5059 | status = nfserr_jukebox; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5060 | goto fput; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5061 | } |
| 5062 | locks_init_lock(file_lock); |
| 5063 | file_lock->fl_type = F_UNLCK; |
J. Bruce Fields | 0a262ff | 2013-06-17 17:29:40 -0400 | [diff] [blame] | 5064 | file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5065 | file_lock->fl_pid = current->tgid; |
| 5066 | file_lock->fl_file = filp; |
| 5067 | file_lock->fl_flags = FL_POSIX; |
| 5068 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 5069 | file_lock->fl_start = locku->lu_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5071 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 5072 | locku->lu_length); |
| 5073 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5075 | err = vfs_lock_file(filp, F_SETLK, file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5076 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 5077 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | goto out_nfserr; |
| 5079 | } |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5080 | update_stateid(&stp->st_stid.sc_stateid); |
| 5081 | memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5082 | fput: |
| 5083 | fput(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5084 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5085 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 71c3bcd | 2011-09-27 21:42:29 -0400 | [diff] [blame] | 5086 | if (!cstate->replay_owner) |
| 5087 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5088 | if (file_lock) |
| 5089 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5090 | return status; |
| 5091 | |
| 5092 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5093 | status = nfserrno(err); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5094 | goto fput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | /* |
| 5098 | * returns |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5099 | * true: locks held by lockowner |
| 5100 | * false: no locks held by lockowner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5102 | static bool |
| 5103 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | { |
| 5105 | struct file_lock **flpp; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5106 | int status = false; |
| 5107 | struct file *filp = find_any_file(fp); |
| 5108 | struct inode *inode; |
| 5109 | |
| 5110 | if (!filp) { |
| 5111 | /* Any valid lock stateid should have some sort of access */ |
| 5112 | WARN_ON_ONCE(1); |
| 5113 | return status; |
| 5114 | } |
| 5115 | |
| 5116 | inode = file_inode(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 5118 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5119 | for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) { |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 5120 | if ((*flpp)->fl_owner == (fl_owner_t)lowner) { |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5121 | status = true; |
| 5122 | break; |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 5123 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5124 | } |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 5125 | spin_unlock(&inode->i_lock); |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5126 | fput(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5127 | return status; |
| 5128 | } |
| 5129 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5130 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 5131 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 5132 | struct nfsd4_compound_state *cstate, |
| 5133 | struct nfsd4_release_lockowner *rlockowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5134 | { |
| 5135 | clientid_t *clid = &rlockowner->rl_clientid; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5136 | struct nfs4_stateowner *sop = NULL, *tmp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5137 | struct nfs4_lockowner *lo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5138 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 5140 | unsigned int hashval = ownerstr_hashval(clid->cl_id, owner); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5141 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5142 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | |
| 5144 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 5145 | clid->cl_boot, clid->cl_id); |
| 5146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | nfs4_lock_state(); |
| 5148 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5149 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5150 | if (status) |
| 5151 | goto out; |
| 5152 | |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 5153 | status = nfserr_locks_held; |
J. Bruce Fields | 06f1f86 | 2011-11-07 16:58:18 -0500 | [diff] [blame] | 5154 | |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5155 | /* Find the matching lock stateowner */ |
| 5156 | list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) { |
| 5157 | if (tmp->so_is_open_owner) |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 5158 | continue; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5159 | if (same_owner_str(tmp, owner, clid)) { |
| 5160 | sop = tmp; |
| 5161 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | } |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 5163 | } |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5164 | |
| 5165 | /* No matching owner found, maybe a replay? Just declare victory... */ |
| 5166 | if (!sop) { |
| 5167 | status = nfs_ok; |
| 5168 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | } |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5170 | |
| 5171 | lo = lockowner(sop); |
| 5172 | /* see if there are still any locks associated with it */ |
| 5173 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { |
| 5174 | if (check_for_locks(stp->st_file, lo)) |
| 5175 | goto out; |
| 5176 | } |
| 5177 | |
| 5178 | status = nfs_ok; |
| 5179 | release_lockowner(lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | out: |
| 5181 | nfs4_unlock_state(); |
| 5182 | return status; |
| 5183 | } |
| 5184 | |
| 5185 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5186 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5188 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | } |
| 5190 | |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5191 | bool |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5192 | nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5193 | { |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5194 | struct nfs4_client_reclaim *crp; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5195 | |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5196 | crp = nfsd4_find_reclaim_client(name, nn); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5197 | return (crp && crp->cr_clp); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5198 | } |
| 5199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | /* |
| 5201 | * failure => all reset bets are off, nfserr_no_grace... |
| 5202 | */ |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5203 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5204 | nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | { |
| 5206 | unsigned int strhashval; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5207 | struct nfs4_client_reclaim *crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5208 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5209 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
| 5210 | crp = alloc_reclaim(); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5211 | if (crp) { |
| 5212 | strhashval = clientstr_hashval(name); |
| 5213 | INIT_LIST_HEAD(&crp->cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5214 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5215 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5216 | crp->cr_clp = NULL; |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5217 | nn->reclaim_str_hashtbl_size++; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5218 | } |
| 5219 | return crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | } |
| 5221 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 5222 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5223 | 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] | 5224 | { |
| 5225 | list_del(&crp->cr_strhash); |
| 5226 | kfree(crp); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5227 | nn->reclaim_str_hashtbl_size--; |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5231 | nfs4_release_reclaim(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5232 | { |
| 5233 | struct nfs4_client_reclaim *crp = NULL; |
| 5234 | int i; |
| 5235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5236 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5237 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 5238 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | struct nfs4_client_reclaim, cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5240 | nfs4_remove_reclaim_record(crp, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | } |
| 5242 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 5243 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | } |
| 5245 | |
| 5246 | /* |
| 5247 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 5248 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5249 | nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | { |
| 5251 | unsigned int strhashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | struct nfs4_client_reclaim *crp = NULL; |
| 5253 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5254 | dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5256 | strhashval = clientstr_hashval(recdir); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5257 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5258 | if (same_name(crp->cr_recdir, recdir)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | return crp; |
| 5260 | } |
| 5261 | } |
| 5262 | return NULL; |
| 5263 | } |
| 5264 | |
| 5265 | /* |
| 5266 | * Called from OPEN. Look for clientid in reclaim list. |
| 5267 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5268 | __be32 |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5269 | nfs4_check_open_reclaim(clientid_t *clid, |
| 5270 | struct nfsd4_compound_state *cstate, |
| 5271 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | { |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5273 | __be32 status; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 5274 | |
| 5275 | /* find clientid in conf_id_hashtbl */ |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5276 | status = lookup_clientid(clid, cstate, nn); |
| 5277 | if (status) |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 5278 | return nfserr_reclaim_bad; |
| 5279 | |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5280 | if (nfsd4_client_record_check(cstate->clp)) |
| 5281 | return nfserr_reclaim_bad; |
| 5282 | |
| 5283 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5284 | } |
| 5285 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5286 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
| 5287 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5288 | u64 nfsd_forget_client(struct nfs4_client *clp, u64 max) |
| 5289 | { |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 5290 | if (mark_client_expired(clp)) |
| 5291 | return 0; |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5292 | expire_client(clp); |
| 5293 | return 1; |
| 5294 | } |
| 5295 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5296 | u64 nfsd_print_client(struct nfs4_client *clp, u64 num) |
| 5297 | { |
| 5298 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 5299 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5300 | printk(KERN_INFO "NFS Client: %s\n", buf); |
| 5301 | return 1; |
| 5302 | } |
| 5303 | |
| 5304 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, |
| 5305 | const char *type) |
| 5306 | { |
| 5307 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 5308 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5309 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
| 5310 | } |
| 5311 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5312 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, |
| 5313 | void (*func)(struct nfs4_ol_stateid *)) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5314 | { |
| 5315 | struct nfs4_openowner *oop; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5316 | struct nfs4_ol_stateid *stp, *st_next; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5317 | struct nfs4_ol_stateid *lst, *lst_next; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5318 | u64 count = 0; |
| 5319 | |
| 5320 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5321 | list_for_each_entry_safe(stp, st_next, |
| 5322 | &oop->oo_owner.so_stateids, st_perstateowner) { |
| 5323 | list_for_each_entry_safe(lst, lst_next, |
| 5324 | &stp->st_locks, st_locks) { |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5325 | if (func) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5326 | func(lst); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5327 | if (++count == max) |
| 5328 | return count; |
| 5329 | } |
| 5330 | } |
| 5331 | } |
| 5332 | |
| 5333 | return count; |
| 5334 | } |
| 5335 | |
| 5336 | u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max) |
| 5337 | { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5338 | return nfsd_foreach_client_lock(clp, max, release_lock_stateid); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5339 | } |
| 5340 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5341 | u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max) |
| 5342 | { |
| 5343 | u64 count = nfsd_foreach_client_lock(clp, max, NULL); |
| 5344 | nfsd_print_count(clp, count, "locked files"); |
| 5345 | return count; |
| 5346 | } |
| 5347 | |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 5348 | static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *)) |
| 5349 | { |
| 5350 | struct nfs4_openowner *oop, *next; |
| 5351 | u64 count = 0; |
| 5352 | |
| 5353 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { |
| 5354 | if (func) |
| 5355 | func(oop); |
| 5356 | if (++count == max) |
| 5357 | break; |
| 5358 | } |
| 5359 | |
| 5360 | return count; |
| 5361 | } |
| 5362 | |
| 5363 | u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max) |
| 5364 | { |
| 5365 | return nfsd_foreach_client_open(clp, max, release_openowner); |
| 5366 | } |
| 5367 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5368 | u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max) |
| 5369 | { |
| 5370 | u64 count = nfsd_foreach_client_open(clp, max, NULL); |
| 5371 | nfsd_print_count(clp, count, "open files"); |
| 5372 | return count; |
| 5373 | } |
| 5374 | |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5375 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
| 5376 | struct list_head *victims) |
| 5377 | { |
| 5378 | struct nfs4_delegation *dp, *next; |
| 5379 | u64 count = 0; |
| 5380 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5381 | lockdep_assert_held(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5382 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5383 | if (victims) { |
| 5384 | /* |
| 5385 | * It's not safe to mess with delegations that have a |
| 5386 | * non-zero dl_time. They might have already been broken |
| 5387 | * and could be processed by the laundromat outside of |
| 5388 | * the state_lock. Just leave them be. |
| 5389 | */ |
| 5390 | if (dp->dl_time != 0) |
| 5391 | continue; |
| 5392 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 5393 | unhash_delegation_locked(dp); |
| 5394 | list_add(&dp->dl_recall_lru, victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5395 | } |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5396 | if (++count == max) |
| 5397 | break; |
| 5398 | } |
| 5399 | return count; |
| 5400 | } |
| 5401 | |
| 5402 | u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max) |
| 5403 | { |
| 5404 | struct nfs4_delegation *dp, *next; |
| 5405 | LIST_HEAD(victims); |
| 5406 | u64 count; |
| 5407 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5408 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5409 | count = nfsd_find_all_delegations(clp, max, &victims); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5410 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5411 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5412 | list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) { |
| 5413 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5414 | revoke_delegation(dp); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5415 | } |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5416 | |
| 5417 | return count; |
| 5418 | } |
| 5419 | |
| 5420 | u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max) |
| 5421 | { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5422 | struct nfs4_delegation *dp; |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5423 | LIST_HEAD(victims); |
| 5424 | u64 count; |
| 5425 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5426 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5427 | count = nfsd_find_all_delegations(clp, max, &victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5428 | while (!list_empty(&victims)) { |
| 5429 | dp = list_first_entry(&victims, struct nfs4_delegation, |
| 5430 | dl_recall_lru); |
| 5431 | list_del_init(&dp->dl_recall_lru); |
| 5432 | dp->dl_time = 0; |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5433 | nfsd_break_one_deleg(dp); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5434 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5435 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5436 | |
| 5437 | return count; |
| 5438 | } |
| 5439 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5440 | u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max) |
| 5441 | { |
| 5442 | u64 count = 0; |
| 5443 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5444 | spin_lock(&state_lock); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5445 | count = nfsd_find_all_delegations(clp, max, NULL); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5446 | spin_unlock(&state_lock); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5447 | |
| 5448 | nfsd_print_count(clp, count, "delegations"); |
| 5449 | return count; |
| 5450 | } |
| 5451 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5452 | u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64)) |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5453 | { |
| 5454 | struct nfs4_client *clp, *next; |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5455 | u64 count = 0; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5456 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id); |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5457 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5458 | if (!nfsd_netns_ready(nn)) |
| 5459 | return 0; |
| 5460 | |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5461 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5462 | count += func(clp, max - count); |
| 5463 | if ((max != 0) && (count >= max)) |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5464 | break; |
| 5465 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5466 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5467 | return count; |
| 5468 | } |
| 5469 | |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 5470 | struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) |
| 5471 | { |
| 5472 | struct nfs4_client *clp; |
| 5473 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id); |
| 5474 | |
| 5475 | if (!nfsd_netns_ready(nn)) |
| 5476 | return NULL; |
| 5477 | |
| 5478 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 5479 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) |
| 5480 | return clp; |
| 5481 | } |
| 5482 | return NULL; |
| 5483 | } |
| 5484 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5485 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
| 5486 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 5487 | /* |
| 5488 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 5489 | * client may quite reasonably hang on to a delegation as long as it has |
| 5490 | * the inode cached. This becomes an obvious problem the first time a |
| 5491 | * client's inode cache approaches the size of the server's total memory. |
| 5492 | * |
| 5493 | * For now we avoid this problem by imposing a hard limit on the number |
| 5494 | * of delegations, which varies according to the server's memory size. |
| 5495 | */ |
| 5496 | static void |
| 5497 | set_max_delegations(void) |
| 5498 | { |
| 5499 | /* |
| 5500 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 5501 | * estimates suggest that in the worst case (where every delegation |
| 5502 | * is for a different inode), a delegation could take about 1.5K, |
| 5503 | * giving a worst case usage of about 6% of memory. |
| 5504 | */ |
| 5505 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 5506 | } |
| 5507 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5508 | static int nfs4_state_create_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5509 | { |
| 5510 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 5511 | int i; |
| 5512 | |
| 5513 | nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5514 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 5515 | if (!nn->conf_id_hashtbl) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5516 | goto err; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5517 | nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5518 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 5519 | if (!nn->unconf_id_hashtbl) |
| 5520 | goto err_unconf_id; |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5521 | nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5522 | OWNER_HASH_SIZE, GFP_KERNEL); |
| 5523 | if (!nn->ownerstr_hashtbl) |
| 5524 | goto err_ownerstr; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5525 | nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5526 | SESSION_HASH_SIZE, GFP_KERNEL); |
| 5527 | if (!nn->sessionid_hashtbl) |
| 5528 | goto err_sessionid; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5529 | |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5530 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5531 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5532 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5533 | } |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5534 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 5535 | INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5536 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 5537 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5538 | nn->conf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5539 | nn->unconf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5540 | INIT_LIST_HEAD(&nn->client_lru); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 5541 | INIT_LIST_HEAD(&nn->close_lru); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5542 | INIT_LIST_HEAD(&nn->del_recall_lru); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5543 | spin_lock_init(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5544 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5545 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5546 | get_net(net); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5547 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5548 | return 0; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5549 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5550 | err_sessionid: |
Stanislav Kinsbursky | 20e9e2b | 2012-11-14 18:21:46 +0300 | [diff] [blame] | 5551 | kfree(nn->ownerstr_hashtbl); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5552 | err_ownerstr: |
| 5553 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5554 | err_unconf_id: |
| 5555 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5556 | err: |
| 5557 | return -ENOMEM; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5558 | } |
| 5559 | |
| 5560 | static void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5561 | nfs4_state_destroy_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5562 | { |
| 5563 | int i; |
| 5564 | struct nfs4_client *clp = NULL; |
| 5565 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 5566 | |
| 5567 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 5568 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 5569 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 5570 | destroy_client(clp); |
| 5571 | } |
| 5572 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5573 | |
Kinglong Mee | 2b90563 | 2014-03-26 22:09:30 +0800 | [diff] [blame] | 5574 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 5575 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 5576 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 5577 | destroy_client(clp); |
| 5578 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5579 | } |
| 5580 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5581 | kfree(nn->sessionid_hashtbl); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5582 | kfree(nn->ownerstr_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5583 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5584 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5585 | put_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5586 | } |
| 5587 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 5588 | int |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5589 | nfs4_state_start_net(struct net *net) |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 5590 | { |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 5591 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5592 | int ret; |
| 5593 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5594 | ret = nfs4_state_create_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5595 | if (ret) |
| 5596 | return ret; |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 5597 | nfsd4_client_tracking_init(net); |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 5598 | nn->boot_time = get_seconds(); |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5599 | locks_start_grace(net, &nn->nfsd4_manager); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5600 | nn->grace_ended = false; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5601 | printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n", |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 5602 | nn->nfsd4_grace, net); |
| 5603 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5604 | return 0; |
| 5605 | } |
| 5606 | |
| 5607 | /* initialization to perform when the nfsd service is started: */ |
| 5608 | |
| 5609 | int |
| 5610 | nfs4_state_start(void) |
| 5611 | { |
| 5612 | int ret; |
| 5613 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5614 | ret = set_callback_cred(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5615 | if (ret) |
| 5616 | return -ENOMEM; |
NeilBrown | 58da282 | 2005-06-23 22:03:19 -0700 | [diff] [blame] | 5617 | laundry_wq = create_singlethread_workqueue("nfsd4"); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 5618 | if (laundry_wq == NULL) { |
| 5619 | ret = -ENOMEM; |
| 5620 | goto out_recovery; |
| 5621 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5622 | ret = nfsd4_create_callback_queue(); |
| 5623 | if (ret) |
| 5624 | goto out_free_laundry; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5625 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 5626 | set_max_delegations(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5627 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5628 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5629 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5630 | out_free_laundry: |
| 5631 | destroy_workqueue(laundry_wq); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 5632 | out_recovery: |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5633 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | } |
| 5635 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 5636 | void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5637 | nfs4_state_shutdown_net(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5639 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5641 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5643 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 5644 | locks_end_grace(&nn->nfsd4_manager); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 5645 | |
J. Bruce Fields | e50a26d | 2013-10-30 10:33:09 -0400 | [diff] [blame] | 5646 | nfs4_lock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5647 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5648 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5649 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5650 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 5651 | unhash_delegation_locked(dp); |
| 5652 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5653 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5654 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | list_for_each_safe(pos, next, &reaplist) { |
| 5656 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 5657 | list_del_init(&dp->dl_recall_lru); |
| 5658 | nfs4_put_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | } |
| 5660 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5661 | nfsd4_client_tracking_exit(net); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5662 | nfs4_state_destroy_net(net); |
J. Bruce Fields | e50a26d | 2013-10-30 10:33:09 -0400 | [diff] [blame] | 5663 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | } |
| 5665 | |
| 5666 | void |
| 5667 | nfs4_state_shutdown(void) |
| 5668 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 5669 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 5670 | nfsd4_destroy_callback_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | } |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5672 | |
| 5673 | static void |
| 5674 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 5675 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 5676 | if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid)) |
| 5677 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5678 | } |
| 5679 | |
| 5680 | static void |
| 5681 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 5682 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 5683 | if (cstate->minorversion) { |
| 5684 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
| 5685 | SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
| 5686 | } |
| 5687 | } |
| 5688 | |
| 5689 | void |
| 5690 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 5691 | { |
| 5692 | CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5693 | } |
| 5694 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5695 | /* |
| 5696 | * functions to set current state id |
| 5697 | */ |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5698 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 5699 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 5700 | { |
| 5701 | put_stateid(cstate, &odp->od_stateid); |
| 5702 | } |
| 5703 | |
| 5704 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5705 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open) |
| 5706 | { |
| 5707 | put_stateid(cstate, &open->op_stateid); |
| 5708 | } |
| 5709 | |
| 5710 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5711 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 5712 | { |
| 5713 | put_stateid(cstate, &close->cl_stateid); |
| 5714 | } |
| 5715 | |
| 5716 | void |
| 5717 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock) |
| 5718 | { |
| 5719 | put_stateid(cstate, &lock->lk_resp_stateid); |
| 5720 | } |
| 5721 | |
| 5722 | /* |
| 5723 | * functions to consume current state id |
| 5724 | */ |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 5725 | |
| 5726 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 5727 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 5728 | { |
| 5729 | get_stateid(cstate, &odp->od_stateid); |
| 5730 | } |
| 5731 | |
| 5732 | void |
| 5733 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp) |
| 5734 | { |
| 5735 | get_stateid(cstate, &drp->dr_stateid); |
| 5736 | } |
| 5737 | |
| 5738 | void |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 5739 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp) |
| 5740 | { |
| 5741 | get_stateid(cstate, &fsp->fr_stateid); |
| 5742 | } |
| 5743 | |
| 5744 | void |
| 5745 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr) |
| 5746 | { |
| 5747 | get_stateid(cstate, &setattr->sa_stateid); |
| 5748 | } |
| 5749 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5750 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5751 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 5752 | { |
| 5753 | get_stateid(cstate, &close->cl_stateid); |
| 5754 | } |
| 5755 | |
| 5756 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5757 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5758 | { |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5759 | get_stateid(cstate, &locku->lu_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5760 | } |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 5761 | |
| 5762 | void |
| 5763 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read) |
| 5764 | { |
| 5765 | get_stateid(cstate, &read->rd_stateid); |
| 5766 | } |
| 5767 | |
| 5768 | void |
| 5769 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write) |
| 5770 | { |
| 5771 | get_stateid(cstate, &write->wr_stateid); |
| 5772 | } |