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 */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 73 | static int check_for_locks(struct nfs4_file *filp, 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 | { |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 257 | if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) { |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 258 | hlist_del(&fi->fi_hash); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 259 | spin_unlock(&state_lock); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 260 | iput(fi->fi_inode); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 261 | nfsd4_free_file(fi); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 262 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static inline void |
| 266 | get_nfs4_file(struct nfs4_file *fi) |
| 267 | { |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 268 | atomic_inc(&fi->fi_ref); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 271 | static struct file * |
| 272 | __nfs4_get_fd(struct nfs4_file *f, int oflag) |
| 273 | { |
| 274 | if (f->fi_fds[oflag]) |
| 275 | return get_file(f->fi_fds[oflag]); |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | static struct file * |
| 280 | find_writeable_file_locked(struct nfs4_file *f) |
| 281 | { |
| 282 | struct file *ret; |
| 283 | |
| 284 | lockdep_assert_held(&f->fi_lock); |
| 285 | |
| 286 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 287 | if (!ret) |
| 288 | ret = __nfs4_get_fd(f, O_RDWR); |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | static struct file * |
| 293 | find_writeable_file(struct nfs4_file *f) |
| 294 | { |
| 295 | struct file *ret; |
| 296 | |
| 297 | spin_lock(&f->fi_lock); |
| 298 | ret = find_writeable_file_locked(f); |
| 299 | spin_unlock(&f->fi_lock); |
| 300 | |
| 301 | return ret; |
| 302 | } |
| 303 | |
| 304 | static struct file *find_readable_file_locked(struct nfs4_file *f) |
| 305 | { |
| 306 | struct file *ret; |
| 307 | |
| 308 | lockdep_assert_held(&f->fi_lock); |
| 309 | |
| 310 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 311 | if (!ret) |
| 312 | ret = __nfs4_get_fd(f, O_RDWR); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static struct file * |
| 317 | find_readable_file(struct nfs4_file *f) |
| 318 | { |
| 319 | struct file *ret; |
| 320 | |
| 321 | spin_lock(&f->fi_lock); |
| 322 | ret = find_readable_file_locked(f); |
| 323 | spin_unlock(&f->fi_lock); |
| 324 | |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | static struct file * |
| 329 | find_any_file(struct nfs4_file *f) |
| 330 | { |
| 331 | struct file *ret; |
| 332 | |
| 333 | spin_lock(&f->fi_lock); |
| 334 | ret = __nfs4_get_fd(f, O_RDWR); |
| 335 | if (!ret) { |
| 336 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 337 | if (!ret) |
| 338 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 339 | } |
| 340 | spin_unlock(&f->fi_lock); |
| 341 | return ret; |
| 342 | } |
| 343 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 344 | static int num_delegations; |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 345 | unsigned long max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 346 | |
| 347 | /* |
| 348 | * Open owner state (share locks) |
| 349 | */ |
| 350 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 351 | /* hash tables for lock and open owners */ |
| 352 | #define OWNER_HASH_BITS 8 |
| 353 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) |
| 354 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 355 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 356 | static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 357 | { |
| 358 | unsigned int ret; |
| 359 | |
| 360 | ret = opaque_hashval(ownername->data, ownername->len); |
| 361 | ret += clientid; |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 362 | return ret & OWNER_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 363 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 364 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 365 | /* hash table for nfs4_file */ |
| 366 | #define FILE_HASH_BITS 8 |
| 367 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
Shan Wei | 3507958 | 2011-01-14 17:35:59 +0800 | [diff] [blame] | 368 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 369 | static unsigned int file_hashval(struct inode *ino) |
| 370 | { |
| 371 | /* XXX: why are we hashing on inode pointer, anyway? */ |
| 372 | return hash_ptr(ino, FILE_HASH_BITS); |
| 373 | } |
| 374 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 375 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 376 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 377 | static void |
| 378 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 379 | { |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 380 | if (access & NFS4_SHARE_ACCESS_WRITE) |
| 381 | atomic_inc(&fp->fi_access[O_WRONLY]); |
| 382 | if (access & NFS4_SHARE_ACCESS_READ) |
| 383 | atomic_inc(&fp->fi_access[O_RDONLY]); |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 384 | } |
| 385 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 386 | static __be32 |
| 387 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 388 | { |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 389 | /* Does this access mode make sense? */ |
| 390 | if (access & ~NFS4_SHARE_ACCESS_BOTH) |
| 391 | return nfserr_inval; |
| 392 | |
| 393 | __nfs4_file_get_access(fp, access); |
| 394 | return nfs_ok; |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 395 | } |
| 396 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 397 | 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] | 398 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 399 | might_lock(&fp->fi_lock); |
| 400 | |
| 401 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { |
| 402 | struct file *f1 = NULL; |
| 403 | struct file *f2 = NULL; |
| 404 | |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame^] | 405 | swap(f1, fp->fi_fds[oflag]); |
J. Bruce Fields | 0c7c3e6 | 2013-03-28 20:37:14 -0400 | [diff] [blame] | 406 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame^] | 407 | swap(f2, fp->fi_fds[O_RDWR]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 408 | spin_unlock(&fp->fi_lock); |
| 409 | if (f1) |
| 410 | fput(f1); |
| 411 | if (f2) |
| 412 | fput(f2); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 416 | 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] | 417 | { |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 418 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
| 419 | |
| 420 | if (access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 421 | __nfs4_file_put_access(fp, O_WRONLY); |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 422 | if (access & NFS4_SHARE_ACCESS_READ) |
| 423 | __nfs4_file_put_access(fp, O_RDONLY); |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 424 | } |
| 425 | |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 426 | static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct |
| 427 | kmem_cache *slab) |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 428 | { |
| 429 | struct idr *stateids = &cl->cl_stateids; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 430 | struct nfs4_stid *stid; |
| 431 | int new_id; |
| 432 | |
| 433 | stid = kmem_cache_alloc(slab, GFP_KERNEL); |
| 434 | if (!stid) |
| 435 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 436 | |
Jeff Layton | 398c33a | 2013-04-29 16:21:20 -0700 | [diff] [blame] | 437 | new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL); |
Tejun Heo | ebd6c70 | 2013-03-13 14:59:37 -0700 | [diff] [blame] | 438 | if (new_id < 0) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 439 | goto out_free; |
| 440 | stid->sc_client = cl; |
| 441 | stid->sc_type = 0; |
| 442 | stid->sc_stateid.si_opaque.so_id = new_id; |
| 443 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; |
| 444 | /* Will be incremented before return to client: */ |
| 445 | stid->sc_stateid.si_generation = 0; |
| 446 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 447 | /* |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 448 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 449 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 450 | * example, a stray write retransmission could be accepted by |
| 451 | * the server when it should have been rejected. Therefore, |
| 452 | * adopt a trick from the sctp code to attempt to maximize the |
| 453 | * amount of time until an id is reused, by ensuring they always |
| 454 | * "increase" (mod INT_MAX): |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 455 | */ |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 456 | return stid; |
| 457 | out_free: |
Wei Yongjun | 2c44a23 | 2013-04-09 14:15:31 +0800 | [diff] [blame] | 458 | kmem_cache_free(slab, stid); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 459 | return NULL; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 460 | } |
| 461 | |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 462 | static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp) |
| 463 | { |
| 464 | return openlockstateid(nfs4_alloc_stid(clp, stateid_slab)); |
| 465 | } |
| 466 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 467 | /* |
| 468 | * When we recall a delegation, we should be careful not to hand it |
| 469 | * out again straight away. |
| 470 | * To ensure this we keep a pair of bloom filters ('new' and 'old') |
| 471 | * in which the filehandles of recalled delegations are "stored". |
| 472 | * If a filehandle appear in either filter, a delegation is blocked. |
| 473 | * When a delegation is recalled, the filehandle is stored in the "new" |
| 474 | * filter. |
| 475 | * Every 30 seconds we swap the filters and clear the "new" one, |
| 476 | * unless both are empty of course. |
| 477 | * |
| 478 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the |
| 479 | * low 3 bytes as hash-table indices. |
| 480 | * |
| 481 | * 'state_lock', which is always held when block_delegations() is called, |
| 482 | * is used to manage concurrent access. Testing does not need the lock |
| 483 | * except when swapping the two filters. |
| 484 | */ |
| 485 | static struct bloom_pair { |
| 486 | int entries, old_entries; |
| 487 | time_t swap_time; |
| 488 | int new; /* index into 'set' */ |
| 489 | DECLARE_BITMAP(set[2], 256); |
| 490 | } blocked_delegations; |
| 491 | |
| 492 | static int delegation_blocked(struct knfsd_fh *fh) |
| 493 | { |
| 494 | u32 hash; |
| 495 | struct bloom_pair *bd = &blocked_delegations; |
| 496 | |
| 497 | if (bd->entries == 0) |
| 498 | return 0; |
| 499 | if (seconds_since_boot() - bd->swap_time > 30) { |
| 500 | spin_lock(&state_lock); |
| 501 | if (seconds_since_boot() - bd->swap_time > 30) { |
| 502 | bd->entries -= bd->old_entries; |
| 503 | bd->old_entries = bd->entries; |
| 504 | memset(bd->set[bd->new], 0, |
| 505 | sizeof(bd->set[0])); |
| 506 | bd->new = 1-bd->new; |
| 507 | bd->swap_time = seconds_since_boot(); |
| 508 | } |
| 509 | spin_unlock(&state_lock); |
| 510 | } |
| 511 | hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0); |
| 512 | if (test_bit(hash&255, bd->set[0]) && |
| 513 | test_bit((hash>>8)&255, bd->set[0]) && |
| 514 | test_bit((hash>>16)&255, bd->set[0])) |
| 515 | return 1; |
| 516 | |
| 517 | if (test_bit(hash&255, bd->set[1]) && |
| 518 | test_bit((hash>>8)&255, bd->set[1]) && |
| 519 | test_bit((hash>>16)&255, bd->set[1])) |
| 520 | return 1; |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static void block_delegations(struct knfsd_fh *fh) |
| 526 | { |
| 527 | u32 hash; |
| 528 | struct bloom_pair *bd = &blocked_delegations; |
| 529 | |
| 530 | hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0); |
| 531 | |
| 532 | __set_bit(hash&255, bd->set[bd->new]); |
| 533 | __set_bit((hash>>8)&255, bd->set[bd->new]); |
| 534 | __set_bit((hash>>16)&255, bd->set[bd->new]); |
| 535 | if (bd->entries == 0) |
| 536 | bd->swap_time = seconds_since_boot(); |
| 537 | bd->entries += 1; |
| 538 | } |
| 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | static struct nfs4_delegation * |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 541 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | { |
| 543 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
| 545 | dprintk("NFSD alloc_init_deleg\n"); |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 546 | if (num_delegations > max_delegations) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 547 | return NULL; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 548 | if (delegation_blocked(¤t_fh->fh_handle)) |
| 549 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 550 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab)); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 551 | if (dp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | return dp; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 553 | /* |
| 554 | * delegation seqid's are never incremented. The 4.1 special |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 555 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 556 | * 0 anyway just for consistency and use 1: |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 557 | */ |
| 558 | dp->dl_stid.sc_stateid.si_generation = 1; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 559 | num_delegations++; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 560 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 561 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 563 | dp->dl_file = NULL; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 564 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
J. Bruce Fields | 6c02eaa | 2009-02-02 17:30:51 -0500 | [diff] [blame] | 565 | fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | dp->dl_time = 0; |
| 567 | atomic_set(&dp->dl_count, 1); |
J. Bruce Fields | 5772515 | 2012-11-05 15:10:26 -0500 | [diff] [blame] | 568 | nfsd4_init_callback(&dp->dl_recall); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | return dp; |
| 570 | } |
| 571 | |
J. Bruce Fields | 68a3396 | 2013-03-21 11:21:50 -0400 | [diff] [blame] | 572 | static void remove_stid(struct nfs4_stid *s) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 573 | { |
| 574 | struct idr *stateids = &s->sc_client->cl_stateids; |
| 575 | |
| 576 | idr_remove(stateids, s->sc_stateid.si_opaque.so_id); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 577 | } |
| 578 | |
Benny Halevy | 9857df8 | 2013-10-14 13:44:52 +0300 | [diff] [blame] | 579 | static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s) |
| 580 | { |
| 581 | kmem_cache_free(slab, s); |
| 582 | } |
| 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | void |
| 585 | nfs4_put_delegation(struct nfs4_delegation *dp) |
| 586 | { |
| 587 | if (atomic_dec_and_test(&dp->dl_count)) { |
Benny Halevy | 9857df8 | 2013-10-14 13:44:52 +0300 | [diff] [blame] | 588 | nfs4_free_stid(deleg_slab, &dp->dl_stid); |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 589 | num_delegations--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 593 | static void nfs4_put_deleg_lease(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | { |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 595 | if (!fp->fi_lease) |
| 596 | return; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 597 | if (atomic_dec_and_test(&fp->fi_delegees)) { |
| 598 | vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease); |
| 599 | fp->fi_lease = NULL; |
J. Bruce Fields | 4ee6362 | 2011-04-15 18:08:26 -0400 | [diff] [blame] | 600 | fput(fp->fi_deleg_file); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 601 | fp->fi_deleg_file = NULL; |
| 602 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 605 | static void unhash_stid(struct nfs4_stid *s) |
| 606 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 607 | s->sc_type = 0; |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 608 | } |
| 609 | |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 610 | static void |
| 611 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 612 | { |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 613 | lockdep_assert_held(&state_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 614 | |
Benny Halevy | 3fb87d1 | 2014-05-30 09:09:31 -0400 | [diff] [blame] | 615 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 616 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
| 617 | list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations); |
| 618 | } |
| 619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | /* Called under the state lock. */ |
| 621 | static void |
| 622 | unhash_delegation(struct nfs4_delegation *dp) |
| 623 | { |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 624 | spin_lock(&state_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 625 | list_del_init(&dp->dl_perclnt); |
J. Bruce Fields | 5d926e8 | 2011-02-07 16:53:46 -0500 | [diff] [blame] | 626 | list_del_init(&dp->dl_perfile); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | list_del_init(&dp->dl_recall_lru); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 628 | spin_unlock(&state_lock); |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 629 | if (dp->dl_file) { |
| 630 | nfs4_put_deleg_lease(dp->dl_file); |
| 631 | put_nfs4_file(dp->dl_file); |
| 632 | dp->dl_file = NULL; |
| 633 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | |
| 637 | |
| 638 | static void destroy_revoked_delegation(struct nfs4_delegation *dp) |
| 639 | { |
| 640 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 68a3396 | 2013-03-21 11:21:50 -0400 | [diff] [blame] | 641 | remove_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | nfs4_put_delegation(dp); |
| 643 | } |
| 644 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 645 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 646 | { |
| 647 | unhash_delegation(dp); |
| 648 | remove_stid(&dp->dl_stid); |
| 649 | nfs4_put_delegation(dp); |
| 650 | } |
| 651 | |
| 652 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 653 | { |
| 654 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 655 | |
| 656 | if (clp->cl_minorversion == 0) |
| 657 | destroy_delegation(dp); |
| 658 | else { |
| 659 | unhash_delegation(dp); |
| 660 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
| 661 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 662 | } |
| 663 | } |
| 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | /* |
| 666 | * SETCLIENTID state |
| 667 | */ |
| 668 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 669 | static unsigned int clientid_hashval(u32 id) |
| 670 | { |
| 671 | return id & CLIENT_HASH_MASK; |
| 672 | } |
| 673 | |
| 674 | static unsigned int clientstr_hashval(const char *name) |
| 675 | { |
| 676 | return opaque_hashval(name, 8) & CLIENT_HASH_MASK; |
| 677 | } |
| 678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | /* |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 680 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 681 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 682 | * only what share bits are currently in force, but also what |
| 683 | * combinations of share bits previous opens have used. This allows us |
| 684 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 685 | * return an error if the client attempt to downgrade to a combination |
| 686 | * of share bits not explicable by closing some of its previous opens. |
| 687 | * |
| 688 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 689 | * track of access/deny bit combinations; so, e.g., we allow: |
| 690 | * |
| 691 | * OPEN allow read, deny write |
| 692 | * OPEN allow both, deny none |
| 693 | * DOWNGRADE allow read, deny none |
| 694 | * |
| 695 | * which we should reject. |
| 696 | */ |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 697 | static unsigned int |
| 698 | bmap_to_share_mode(unsigned long bmap) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 699 | int i; |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 700 | unsigned int access = 0; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 701 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 702 | for (i = 1; i < 4; i++) { |
| 703 | if (test_bit(i, &bmap)) |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 704 | access |= i; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 705 | } |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 706 | return access; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 707 | } |
| 708 | |
Jeff Layton | 3a32861 | 2012-05-11 09:45:12 -0400 | [diff] [blame] | 709 | static bool |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 710 | test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 711 | unsigned int access, deny; |
| 712 | |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 713 | access = bmap_to_share_mode(stp->st_access_bmap); |
| 714 | deny = bmap_to_share_mode(stp->st_deny_bmap); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 715 | if ((access & open->op_share_deny) || (deny & open->op_share_access)) |
Jeff Layton | 3a32861 | 2012-05-11 09:45:12 -0400 | [diff] [blame] | 716 | return false; |
| 717 | return true; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 718 | } |
| 719 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 720 | /* set share access for a given stateid */ |
| 721 | static inline void |
| 722 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 723 | { |
| 724 | __set_bit(access, &stp->st_access_bmap); |
| 725 | } |
| 726 | |
| 727 | /* clear share access for a given stateid */ |
| 728 | static inline void |
| 729 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 730 | { |
| 731 | __clear_bit(access, &stp->st_access_bmap); |
| 732 | } |
| 733 | |
| 734 | /* test whether a given stateid has access */ |
| 735 | static inline bool |
| 736 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 737 | { |
| 738 | return test_bit(access, &stp->st_access_bmap); |
| 739 | } |
| 740 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 741 | /* set share deny for a given stateid */ |
| 742 | static inline void |
| 743 | set_deny(u32 access, struct nfs4_ol_stateid *stp) |
| 744 | { |
| 745 | __set_bit(access, &stp->st_deny_bmap); |
| 746 | } |
| 747 | |
| 748 | /* clear share deny for a given stateid */ |
| 749 | static inline void |
| 750 | clear_deny(u32 access, struct nfs4_ol_stateid *stp) |
| 751 | { |
| 752 | __clear_bit(access, &stp->st_deny_bmap); |
| 753 | } |
| 754 | |
| 755 | /* test whether a given stateid is denying specific access */ |
| 756 | static inline bool |
| 757 | test_deny(u32 access, struct nfs4_ol_stateid *stp) |
| 758 | { |
| 759 | return test_bit(access, &stp->st_deny_bmap); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | static int nfs4_access_to_omode(u32 access) |
| 763 | { |
J. Bruce Fields | 8f34a43 | 2010-09-02 15:23:16 -0400 | [diff] [blame] | 764 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 765 | case NFS4_SHARE_ACCESS_READ: |
| 766 | return O_RDONLY; |
| 767 | case NFS4_SHARE_ACCESS_WRITE: |
| 768 | return O_WRONLY; |
| 769 | case NFS4_SHARE_ACCESS_BOTH: |
| 770 | return O_RDWR; |
| 771 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 772 | WARN_ON_ONCE(1); |
| 773 | return O_RDONLY; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 774 | } |
| 775 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 776 | /* release all access and file references for a given stateid */ |
| 777 | static void |
| 778 | release_all_access(struct nfs4_ol_stateid *stp) |
| 779 | { |
| 780 | int i; |
| 781 | |
| 782 | for (i = 1; i < 4; i++) { |
| 783 | if (test_access(i, stp)) |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 784 | nfs4_file_put_access(stp->st_file, i); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 785 | clear_access(i, stp); |
| 786 | } |
| 787 | } |
| 788 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 789 | static void unhash_generic_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 790 | { |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 791 | struct nfs4_file *fp = stp->st_file; |
| 792 | |
| 793 | spin_lock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 794 | list_del(&stp->st_perfile); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 795 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 796 | list_del(&stp->st_perstateowner); |
| 797 | } |
| 798 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 799 | static void close_generic_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 800 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 801 | release_all_access(stp); |
OGAWA Hirofumi | a96e5b9 | 2011-04-18 11:48:55 -0400 | [diff] [blame] | 802 | put_nfs4_file(stp->st_file); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 803 | stp->st_file = NULL; |
| 804 | } |
| 805 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 806 | static void free_generic_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 807 | { |
J. Bruce Fields | 68a3396 | 2013-03-21 11:21:50 -0400 | [diff] [blame] | 808 | remove_stid(&stp->st_stid); |
Benny Halevy | 9857df8 | 2013-10-14 13:44:52 +0300 | [diff] [blame] | 809 | nfs4_free_stid(stateid_slab, &stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 810 | } |
| 811 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 812 | static void __release_lock_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 813 | { |
| 814 | struct file *file; |
| 815 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 816 | list_del(&stp->st_locks); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 817 | unhash_generic_stateid(stp); |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 818 | unhash_stid(&stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 819 | file = find_any_file(stp->st_file); |
Trond Myklebust | e20fcf1 | 2014-07-10 14:07:27 -0400 | [diff] [blame] | 820 | if (file) |
| 821 | filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner)); |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 822 | close_generic_stateid(stp); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 823 | free_generic_stateid(stp); |
| 824 | } |
| 825 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 826 | static void unhash_lockowner(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 827 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 828 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 829 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 830 | list_del(&lo->lo_owner.so_strhash); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 831 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 832 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 833 | struct nfs4_ol_stateid, st_perstateowner); |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 834 | __release_lock_stateid(stp); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
Trond Myklebust | 50cc623 | 2014-04-18 14:44:03 -0400 | [diff] [blame] | 838 | static void nfs4_free_lockowner(struct nfs4_lockowner *lo) |
| 839 | { |
| 840 | kfree(lo->lo_owner.so_owner.data); |
| 841 | kmem_cache_free(lockowner_slab, lo); |
| 842 | } |
| 843 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 844 | static void release_lockowner(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 845 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 846 | unhash_lockowner(lo); |
| 847 | nfs4_free_lockowner(lo); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 848 | } |
| 849 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 850 | static void release_lockowner_if_empty(struct nfs4_lockowner *lo) |
| 851 | { |
| 852 | if (list_empty(&lo->lo_owner.so_stateids)) |
| 853 | release_lockowner(lo); |
| 854 | } |
| 855 | |
| 856 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 857 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 858 | struct nfs4_lockowner *lo; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 859 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 860 | lo = lockowner(stp->st_stateowner); |
| 861 | __release_lock_stateid(stp); |
| 862 | release_lockowner_if_empty(lo); |
| 863 | } |
| 864 | |
| 865 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp) |
| 866 | { |
| 867 | struct nfs4_ol_stateid *stp; |
| 868 | |
| 869 | while (!list_empty(&open_stp->st_locks)) { |
| 870 | stp = list_entry(open_stp->st_locks.next, |
| 871 | struct nfs4_ol_stateid, st_locks); |
| 872 | release_lock_stateid(stp); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 876 | static void unhash_open_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 877 | { |
| 878 | unhash_generic_stateid(stp); |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 879 | release_open_stateid_locks(stp); |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 880 | close_generic_stateid(stp); |
| 881 | } |
| 882 | |
| 883 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 884 | { |
| 885 | unhash_open_stateid(stp); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 886 | free_generic_stateid(stp); |
| 887 | } |
| 888 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 889 | static void unhash_openowner(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 890 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 891 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 892 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 893 | list_del(&oo->oo_owner.so_strhash); |
| 894 | list_del(&oo->oo_perclient); |
| 895 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 896 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 897 | struct nfs4_ol_stateid, st_perstateowner); |
J. Bruce Fields | f044ff8 | 2009-01-11 15:24:04 -0500 | [diff] [blame] | 898 | release_open_stateid(stp); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 902 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 903 | { |
| 904 | struct nfs4_ol_stateid *s = oo->oo_last_closed_stid; |
| 905 | |
| 906 | if (s) { |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 907 | free_generic_stateid(s); |
| 908 | oo->oo_last_closed_stid = NULL; |
| 909 | } |
| 910 | } |
| 911 | |
Trond Myklebust | 50cc623 | 2014-04-18 14:44:03 -0400 | [diff] [blame] | 912 | static void nfs4_free_openowner(struct nfs4_openowner *oo) |
| 913 | { |
| 914 | kfree(oo->oo_owner.so_owner.data); |
| 915 | kmem_cache_free(openowner_slab, oo); |
| 916 | } |
| 917 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 918 | static void release_openowner(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 919 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 920 | unhash_openowner(oo); |
| 921 | list_del(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 922 | release_last_closed_stateid(oo); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 923 | nfs4_free_openowner(oo); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 924 | } |
| 925 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 926 | static inline int |
| 927 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 928 | { |
| 929 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 930 | |
| 931 | return sid->sequence % SESSION_HASH_SIZE; |
| 932 | } |
| 933 | |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 934 | #ifdef NFSD_DEBUG |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 935 | static inline void |
| 936 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 937 | { |
| 938 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 939 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 940 | } |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 941 | #else |
| 942 | static inline void |
| 943 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 944 | { |
| 945 | } |
| 946 | #endif |
| 947 | |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 948 | /* |
| 949 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 950 | * won't be used for replay. |
| 951 | */ |
| 952 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 953 | { |
| 954 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 955 | |
| 956 | if (nfserr == nfserr_replay_me) |
| 957 | return; |
| 958 | |
| 959 | if (!seqid_mutating_err(ntohl(nfserr))) { |
| 960 | cstate->replay_owner = NULL; |
| 961 | return; |
| 962 | } |
| 963 | if (!so) |
| 964 | return; |
| 965 | if (so->so_is_open_owner) |
| 966 | release_last_closed_stateid(openowner(so)); |
| 967 | so->so_seqid++; |
| 968 | return; |
| 969 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 970 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 971 | static void |
| 972 | gen_sessionid(struct nfsd4_session *ses) |
| 973 | { |
| 974 | struct nfs4_client *clp = ses->se_client; |
| 975 | struct nfsd4_sessionid *sid; |
| 976 | |
| 977 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 978 | sid->clientid = clp->cl_clientid; |
| 979 | sid->sequence = current_sessionid++; |
| 980 | sid->reserved = 0; |
| 981 | } |
| 982 | |
| 983 | /* |
Andy Adamson | a649637 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 984 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 985 | * the rpc header, but all we need to cache is the data starting after |
| 986 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 987 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 988 | * value that is the number of bytes in our cache plus a few additional |
| 989 | * bytes. In order to stay on the safe side, and not promise more than |
| 990 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 991 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 992 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 993 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 994 | */ |
Andy Adamson | a649637 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 995 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 996 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 997 | static void |
| 998 | free_session_slots(struct nfsd4_session *ses) |
| 999 | { |
| 1000 | int i; |
| 1001 | |
| 1002 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) |
| 1003 | kfree(ses->se_slots[i]); |
| 1004 | } |
| 1005 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1006 | /* |
| 1007 | * We don't actually need to cache the rpc and session headers, so we |
| 1008 | * can allocate a little less for each slot: |
| 1009 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1010 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1011 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1012 | u32 size; |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1013 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1014 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 1015 | size = 0; |
| 1016 | else |
| 1017 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 1018 | return size + sizeof(struct nfsd4_slot); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1019 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1020 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1021 | /* |
| 1022 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 1023 | * re-negotiate active sessions and reduce their slot usage to make |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 1024 | * 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] | 1025 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1026 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1027 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1028 | u32 slotsize = slot_bytes(ca); |
| 1029 | u32 num = ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1030 | int avail; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1031 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1032 | spin_lock(&nfsd_drc_lock); |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 1033 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, |
| 1034 | nfsd_drc_max_mem - nfsd_drc_mem_used); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1035 | num = min_t(int, num, avail / slotsize); |
| 1036 | nfsd_drc_mem_used += num * slotsize; |
| 1037 | spin_unlock(&nfsd_drc_lock); |
| 1038 | |
| 1039 | return num; |
| 1040 | } |
| 1041 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1042 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1043 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1044 | int slotsize = slot_bytes(ca); |
| 1045 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1046 | spin_lock(&nfsd_drc_lock); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1047 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1048 | spin_unlock(&nfsd_drc_lock); |
| 1049 | } |
| 1050 | |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1051 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 1052 | struct nfsd4_channel_attrs *battrs) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1053 | { |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1054 | int numslots = fattrs->maxreqs; |
| 1055 | int slotsize = slot_bytes(fattrs); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1056 | struct nfsd4_session *new; |
| 1057 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1058 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 1059 | 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] | 1060 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 1061 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1062 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1063 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1064 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1065 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1066 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1067 | for (i = 0; i < numslots; i++) { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1068 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1069 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1070 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1071 | } |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1072 | |
| 1073 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 1074 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 1075 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1076 | return new; |
| 1077 | out_free: |
| 1078 | while (i--) |
| 1079 | kfree(new->se_slots[i]); |
| 1080 | kfree(new); |
| 1081 | return NULL; |
| 1082 | } |
| 1083 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1084 | static void free_conn(struct nfsd4_conn *c) |
| 1085 | { |
| 1086 | svc_xprt_put(c->cn_xprt); |
| 1087 | kfree(c); |
| 1088 | } |
| 1089 | |
| 1090 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 1091 | { |
| 1092 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 1093 | struct nfs4_client *clp = c->cn_session->se_client; |
| 1094 | |
| 1095 | spin_lock(&clp->cl_lock); |
| 1096 | if (!list_empty(&c->cn_persession)) { |
| 1097 | list_del(&c->cn_persession); |
| 1098 | free_conn(c); |
| 1099 | } |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 1100 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 2e4b723 | 2013-03-08 09:30:43 -0500 | [diff] [blame] | 1101 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1102 | } |
| 1103 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1104 | 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] | 1105 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1106 | struct nfsd4_conn *conn; |
| 1107 | |
| 1108 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 1109 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1110 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1111 | svc_xprt_get(rqstp->rq_xprt); |
| 1112 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1113 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1114 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1115 | return conn; |
| 1116 | } |
| 1117 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1118 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1119 | { |
| 1120 | conn->cn_session = ses; |
| 1121 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1122 | } |
| 1123 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1124 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1125 | { |
| 1126 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1127 | |
| 1128 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1129 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1130 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1131 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1132 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1133 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1134 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1135 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1136 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1137 | } |
| 1138 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1139 | 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] | 1140 | { |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1141 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1142 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1143 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1144 | ret = nfsd4_register_conn(conn); |
| 1145 | if (ret) |
| 1146 | /* oops; xprt is already down: */ |
| 1147 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | 6a3b156 | 2012-09-12 09:59:17 -0400 | [diff] [blame] | 1148 | if (conn->cn_flags & NFS4_CDFC4_BACK) { |
Weston Andros Adamson | 2411967 | 2012-05-24 15:42:17 -0400 | [diff] [blame] | 1149 | /* callback channel may be back up */ |
| 1150 | nfsd4_probe_callback(ses->se_client); |
| 1151 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1152 | } |
| 1153 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1154 | 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] | 1155 | { |
| 1156 | u32 dir = NFS4_CDFC4_FORE; |
| 1157 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1158 | if (cses->flags & SESSION4_BACK_CHAN) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1159 | dir |= NFS4_CDFC4_BACK; |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1160 | return alloc_conn(rqstp, dir); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1164 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1165 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1166 | struct nfs4_client *clp = s->se_client; |
| 1167 | struct nfsd4_conn *c; |
| 1168 | |
| 1169 | spin_lock(&clp->cl_lock); |
| 1170 | while (!list_empty(&s->se_conns)) { |
| 1171 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1172 | list_del_init(&c->cn_persession); |
| 1173 | spin_unlock(&clp->cl_lock); |
| 1174 | |
| 1175 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1176 | free_conn(c); |
| 1177 | |
| 1178 | spin_lock(&clp->cl_lock); |
| 1179 | } |
| 1180 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1181 | } |
| 1182 | |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1183 | static void __free_session(struct nfsd4_session *ses) |
| 1184 | { |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1185 | free_session_slots(ses); |
| 1186 | kfree(ses); |
| 1187 | } |
| 1188 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1189 | static void free_session(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 1190 | { |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1191 | struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id); |
| 1192 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1193 | lockdep_assert_held(&nn->client_lock); |
| 1194 | nfsd4_del_conns(ses); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1195 | nfsd4_put_drc_mem(&ses->se_fchannel); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1196 | __free_session(ses); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1197 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1198 | |
Fengguang Wu | 135ae82 | 2012-11-10 07:20:25 -0500 | [diff] [blame] | 1199 | 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] | 1200 | { |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1201 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1202 | 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] | 1203 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1204 | new->se_client = clp; |
| 1205 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1206 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1207 | INIT_LIST_HEAD(&new->se_conns); |
| 1208 | |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1209 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1210 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 1211 | new->se_cb_prog = cses->callback_prog; |
J. Bruce Fields | c6bb3ca | 2012-11-01 16:31:02 -0400 | [diff] [blame] | 1212 | new->se_cb_sec = cses->cb_sec; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1213 | atomic_set(&new->se_ref, 0); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1214 | idx = hash_sessionid(&new->se_sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1215 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1216 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1217 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1218 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1219 | spin_unlock(&clp->cl_lock); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1220 | spin_unlock(&nn->client_lock); |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1221 | |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1222 | if (cses->flags & SESSION4_BACK_CHAN) { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1223 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1224 | /* |
| 1225 | * This is a little silly; with sessions there's no real |
| 1226 | * use for the callback address. Use the peer address |
| 1227 | * as a reasonable default for now, but consider fixing |
| 1228 | * the rpc client not to require an address in the |
| 1229 | * future: |
| 1230 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1231 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1232 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1233 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1234 | } |
| 1235 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1236 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1237 | static struct nfsd4_session * |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1238 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1239 | { |
| 1240 | struct nfsd4_session *elem; |
| 1241 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1242 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1243 | |
| 1244 | dump_sessionid(__func__, sessionid); |
| 1245 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1246 | /* Search in the appropriate list */ |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1247 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1248 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1249 | NFS4_MAX_SESSIONID_LEN)) { |
| 1250 | return elem; |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | dprintk("%s: session not found\n", __func__); |
| 1255 | return NULL; |
| 1256 | } |
| 1257 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1258 | static struct nfsd4_session * |
| 1259 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, |
| 1260 | __be32 *ret) |
| 1261 | { |
| 1262 | struct nfsd4_session *session; |
| 1263 | __be32 status = nfserr_badsession; |
| 1264 | |
| 1265 | session = __find_in_sessionid_hashtbl(sessionid, net); |
| 1266 | if (!session) |
| 1267 | goto out; |
| 1268 | status = nfsd4_get_session_locked(session); |
| 1269 | if (status) |
| 1270 | session = NULL; |
| 1271 | out: |
| 1272 | *ret = status; |
| 1273 | return session; |
| 1274 | } |
| 1275 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1276 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1277 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1278 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1279 | { |
| 1280 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1281 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1282 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1283 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1284 | } |
| 1285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1287 | static int |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1288 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | { |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1290 | if (clid->cl_boot == nn->boot_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | return 0; |
Andy Adamson | 60adfc5 | 2009-04-03 08:28:50 +0300 | [diff] [blame] | 1292 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1293 | clid->cl_boot, clid->cl_id, nn->boot_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | return 1; |
| 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * XXX Should we use a slab cache ? |
| 1299 | * This type of memory management is somewhat inefficient, but we use it |
| 1300 | * anyway since SETCLIENTID is not a common operation. |
| 1301 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1302 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | { |
| 1304 | struct nfs4_client *clp; |
| 1305 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1306 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
| 1307 | if (clp == NULL) |
| 1308 | return NULL; |
Thomas Meyer | 67114fe | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 1309 | clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL); |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1310 | if (clp->cl_name.data == NULL) { |
| 1311 | kfree(clp); |
| 1312 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | } |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1314 | clp->cl_name.len = name.len; |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1315 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1316 | idr_init(&clp->cl_stateids); |
| 1317 | atomic_set(&clp->cl_refcount, 0); |
| 1318 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 1319 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1320 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1321 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 1322 | INIT_LIST_HEAD(&clp->cl_lru); |
| 1323 | INIT_LIST_HEAD(&clp->cl_callbacks); |
| 1324 | INIT_LIST_HEAD(&clp->cl_revoked); |
| 1325 | spin_lock_init(&clp->cl_lock); |
| 1326 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | return clp; |
| 1328 | } |
| 1329 | |
Trond Myklebust | 4dd86e15 | 2014-04-18 14:43:58 -0400 | [diff] [blame] | 1330 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | free_client(struct nfs4_client *clp) |
| 1332 | { |
Stanislav Kinsbursky | bca0ec6 | 2013-01-09 12:38:34 +0300 | [diff] [blame] | 1333 | 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] | 1334 | |
| 1335 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1336 | while (!list_empty(&clp->cl_sessions)) { |
| 1337 | struct nfsd4_session *ses; |
| 1338 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 1339 | se_perclnt); |
| 1340 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1341 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 1342 | free_session(ses); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1343 | } |
Trond Myklebust | 4cb57e3 | 2014-04-18 14:43:57 -0400 | [diff] [blame] | 1344 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1345 | free_svc_cred(&clp->cl_cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | kfree(clp->cl_name.data); |
majianpeng | 2d32b29 | 2013-01-29 13:16:06 +0800 | [diff] [blame] | 1347 | idr_destroy(&clp->cl_stateids); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | kfree(clp); |
| 1349 | } |
| 1350 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1351 | /* must be called under the client_lock */ |
| 1352 | static inline void |
| 1353 | unhash_client_locked(struct nfs4_client *clp) |
| 1354 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1355 | struct nfsd4_session *ses; |
| 1356 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1357 | list_del(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1358 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1359 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 1360 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1361 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1362 | } |
| 1363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | static void |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1365 | destroy_client(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1367 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | struct list_head reaplist; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1370 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1373 | spin_lock(&state_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1374 | while (!list_empty(&clp->cl_delegations)) { |
| 1375 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1376 | list_del_init(&dp->dl_perclnt); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 1377 | /* Ensure that deleg break won't try to requeue it */ |
| 1378 | ++dp->dl_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | list_move(&dp->dl_recall_lru, &reaplist); |
| 1380 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1381 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | while (!list_empty(&reaplist)) { |
| 1383 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1384 | destroy_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | } |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 1386 | list_splice_init(&clp->cl_revoked, &reaplist); |
| 1387 | while (!list_empty(&reaplist)) { |
| 1388 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
| 1389 | destroy_revoked_delegation(dp); |
| 1390 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1391 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1392 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
| 1393 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | } |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 1395 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 1396 | if (clp->cl_cb_conn.cb_xprt) |
| 1397 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1398 | list_del(&clp->cl_idhash); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1399 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1400 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1401 | else |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1402 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1403 | spin_lock(&nn->client_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1404 | unhash_client_locked(clp); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 1405 | WARN_ON_ONCE(atomic_read(&clp->cl_refcount)); |
| 1406 | free_client(clp); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1407 | spin_unlock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1410 | static void expire_client(struct nfs4_client *clp) |
| 1411 | { |
| 1412 | nfsd4_client_record_remove(clp); |
| 1413 | destroy_client(clp); |
| 1414 | } |
| 1415 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1416 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 1417 | { |
| 1418 | memcpy(target->cl_verifier.data, source->data, |
| 1419 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1422 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 1423 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 1425 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 1426 | } |
| 1427 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1428 | 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] | 1429 | { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1430 | if (source->cr_principal) { |
| 1431 | target->cr_principal = |
| 1432 | kstrdup(source->cr_principal, GFP_KERNEL); |
| 1433 | if (target->cr_principal == NULL) |
| 1434 | return -ENOMEM; |
| 1435 | } else |
| 1436 | target->cr_principal = NULL; |
J. Bruce Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 1437 | target->cr_flavor = source->cr_flavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | target->cr_uid = source->cr_uid; |
| 1439 | target->cr_gid = source->cr_gid; |
| 1440 | target->cr_group_info = source->cr_group_info; |
| 1441 | get_group_info(target->cr_group_info); |
J. Bruce Fields | 0dc1531 | 2013-05-14 16:07:13 -0400 | [diff] [blame] | 1442 | target->cr_gss_mech = source->cr_gss_mech; |
| 1443 | if (source->cr_gss_mech) |
| 1444 | gss_mech_get(source->cr_gss_mech); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1445 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1448 | static long long |
| 1449 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 1450 | { |
| 1451 | long long res; |
| 1452 | |
| 1453 | res = o1->len - o2->len; |
| 1454 | if (res) |
| 1455 | return res; |
| 1456 | return (long long)memcmp(o1->data, o2->data, o1->len); |
| 1457 | } |
| 1458 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1459 | static int same_name(const char *n1, const char *n2) |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1460 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1461 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1465 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 1466 | { |
| 1467 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1471 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 1472 | { |
| 1473 | 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] | 1474 | } |
| 1475 | |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1476 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 1477 | { |
| 1478 | int i; |
| 1479 | |
| 1480 | if (g1->ngroups != g2->ngroups) |
| 1481 | return false; |
| 1482 | for (i=0; i<g1->ngroups; i++) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1483 | if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i))) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1484 | return false; |
| 1485 | return true; |
| 1486 | } |
| 1487 | |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1488 | /* |
| 1489 | * RFC 3530 language requires clid_inuse be returned when the |
| 1490 | * "principal" associated with a requests differs from that previously |
| 1491 | * used. We use uid, gid's, and gss principal string as our best |
| 1492 | * approximation. We also don't want to allow non-gss use of a client |
| 1493 | * established using gss: in theory cr_principal should catch that |
| 1494 | * change, but in practice cr_principal can be null even in the gss case |
| 1495 | * since gssd doesn't always pass down a principal string. |
| 1496 | */ |
| 1497 | static bool is_gss_cred(struct svc_cred *cr) |
| 1498 | { |
| 1499 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 1500 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 1501 | } |
| 1502 | |
| 1503 | |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1504 | static bool |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1505 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 1506 | { |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1507 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1508 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 1509 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1510 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 1511 | return false; |
| 1512 | if (cr1->cr_principal == cr2->cr_principal) |
| 1513 | return true; |
| 1514 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 1515 | return false; |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1516 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1519 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 1520 | { |
| 1521 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1522 | u32 service; |
| 1523 | |
J. Bruce Fields | c472059 | 2013-08-07 11:41:49 -0400 | [diff] [blame] | 1524 | if (!cr->cr_gss_mech) |
| 1525 | return false; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1526 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 1527 | return service == RPC_GSS_SVC_INTEGRITY || |
| 1528 | service == RPC_GSS_SVC_PRIVACY; |
| 1529 | } |
| 1530 | |
| 1531 | static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) |
| 1532 | { |
| 1533 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1534 | |
| 1535 | if (!cl->cl_mach_cred) |
| 1536 | return true; |
| 1537 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 1538 | return false; |
| 1539 | if (!svc_rqst_integrity_protected(rqstp)) |
| 1540 | return false; |
| 1541 | if (!cr->cr_principal) |
| 1542 | return false; |
| 1543 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 1544 | } |
| 1545 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1546 | 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] | 1547 | { |
| 1548 | static u32 current_clientid = 1; |
| 1549 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1550 | clp->cl_clientid.cl_boot = nn->boot_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | clp->cl_clientid.cl_id = current_clientid++; |
| 1552 | } |
| 1553 | |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 1554 | static void gen_confirm(struct nfs4_client *clp) |
| 1555 | { |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1556 | __be32 verf[2]; |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 1557 | static u32 i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
Jeff Layton | f419992 | 2014-06-17 07:44:11 -0400 | [diff] [blame] | 1559 | /* |
| 1560 | * This is opaque to client, so no need to byte-swap. Use |
| 1561 | * __force to keep sparse happy |
| 1562 | */ |
| 1563 | verf[0] = (__force __be32)get_seconds(); |
| 1564 | verf[1] = (__force __be32)i++; |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1565 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | } |
| 1567 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 1568 | 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] | 1569 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 1570 | struct nfs4_stid *ret; |
| 1571 | |
| 1572 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 1573 | if (!ret || !ret->sc_type) |
| 1574 | return NULL; |
| 1575 | return ret; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 1576 | } |
| 1577 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 1578 | 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] | 1579 | { |
| 1580 | struct nfs4_stid *s; |
| 1581 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 1582 | s = find_stateid(cl, t); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 1583 | if (!s) |
| 1584 | return NULL; |
| 1585 | if (typemask & s->sc_type) |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 1586 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1587 | return NULL; |
| 1588 | } |
| 1589 | |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 1590 | static struct nfs4_client *create_client(struct xdr_netobj name, |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1591 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 1592 | { |
| 1593 | struct nfs4_client *clp; |
| 1594 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1595 | int ret; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1596 | struct net *net = SVC_NET(rqstp); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1597 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1598 | |
| 1599 | clp = alloc_client(name); |
| 1600 | if (clp == NULL) |
| 1601 | return NULL; |
| 1602 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1603 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 1604 | if (ret) { |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1605 | spin_lock(&nn->client_lock); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1606 | free_client(clp); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1607 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1608 | return NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1609 | } |
J. Bruce Fields | 5772515 | 2012-11-05 15:10:26 -0500 | [diff] [blame] | 1610 | nfsd4_init_callback(&clp->cl_cb_null); |
Benny Halevy | 07cd490 | 2010-05-12 00:13:41 +0300 | [diff] [blame] | 1611 | clp->cl_time = get_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1612 | clear_bit(0, &clp->cl_cb_slot_busy); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1613 | copy_verf(clp, verf); |
| 1614 | rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1615 | gen_confirm(clp); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1616 | clp->cl_cb_session = NULL; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1617 | clp->net = net; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1618 | return clp; |
| 1619 | } |
| 1620 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1621 | static void |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1622 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 1623 | { |
| 1624 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 1625 | struct nfs4_client *clp; |
| 1626 | |
| 1627 | while (*new) { |
| 1628 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 1629 | parent = *new; |
| 1630 | |
| 1631 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 1632 | new = &((*new)->rb_left); |
| 1633 | else |
| 1634 | new = &((*new)->rb_right); |
| 1635 | } |
| 1636 | |
| 1637 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 1638 | rb_insert_color(&new_clp->cl_namenode, root); |
| 1639 | } |
| 1640 | |
| 1641 | static struct nfs4_client * |
| 1642 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 1643 | { |
| 1644 | long long cmp; |
| 1645 | struct rb_node *node = root->rb_node; |
| 1646 | struct nfs4_client *clp; |
| 1647 | |
| 1648 | while (node) { |
| 1649 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 1650 | cmp = compare_blob(&clp->cl_name, name); |
| 1651 | if (cmp > 0) |
| 1652 | node = node->rb_left; |
| 1653 | else if (cmp < 0) |
| 1654 | node = node->rb_right; |
| 1655 | else |
| 1656 | return clp; |
| 1657 | } |
| 1658 | return NULL; |
| 1659 | } |
| 1660 | |
| 1661 | static void |
| 1662 | add_to_unconfirmed(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | { |
| 1664 | unsigned int idhashval; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1665 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1667 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1668 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1670 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 1671 | renew_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1674 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | move_to_confirmed(struct nfs4_client *clp) |
| 1676 | { |
| 1677 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 1678 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
| 1680 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 1681 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1682 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1683 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1684 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | renew_client(clp); |
| 1686 | } |
| 1687 | |
| 1688 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1689 | 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] | 1690 | { |
| 1691 | struct nfs4_client *clp; |
| 1692 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 1693 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1694 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1695 | if (same_clid(&clp->cl_clientid, clid)) { |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 1696 | if ((bool)clp->cl_minorversion != sessions) |
| 1697 | return NULL; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1698 | renew_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | return clp; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1700 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | } |
| 1702 | return NULL; |
| 1703 | } |
| 1704 | |
| 1705 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1706 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 1707 | { |
| 1708 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 1709 | |
| 1710 | return find_client_in_id_table(tbl, clid, sessions); |
| 1711 | } |
| 1712 | |
| 1713 | static struct nfs4_client * |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1714 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | { |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1716 | struct list_head *tbl = nn->unconf_id_hashtbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1718 | return find_client_in_id_table(tbl, clid, sessions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | } |
| 1720 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 1721 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 1722 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 1723 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 1724 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 1725 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1726 | static struct nfs4_client * |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1727 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1728 | { |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1729 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | static struct nfs4_client * |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1733 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1734 | { |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1735 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1738 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 1739 | 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] | 1740 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1741 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 1742 | struct sockaddr *sa = svc_addr(rqstp); |
| 1743 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 1744 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 1746 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 1747 | if (se->se_callback_netid_len == 3 && |
| 1748 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 1749 | expected_family = AF_INET; |
| 1750 | else if (se->se_callback_netid_len == 4 && |
| 1751 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 1752 | expected_family = AF_INET6; |
| 1753 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | goto out_err; |
| 1755 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1756 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1757 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1758 | (struct sockaddr *)&conn->cb_addr, |
| 1759 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1760 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1761 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1763 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1764 | if (conn->cb_addr.ss_family == AF_INET6) |
| 1765 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 1766 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1767 | conn->cb_prog = se->se_callback_prog; |
| 1768 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 1769 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | return; |
| 1771 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1772 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 1773 | conn->cb_addrlen = 0; |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 1774 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | "will not receive delegations\n", |
| 1776 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 1777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | return; |
| 1779 | } |
| 1780 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1781 | /* |
J. Bruce Fields | 067e1ac | 2014-03-21 17:26:44 -0400 | [diff] [blame] | 1782 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1783 | */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 1784 | static void |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1785 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 1786 | { |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1787 | struct xdr_buf *buf = resp->xdr.buf; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1788 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 1789 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1790 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1791 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1792 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1793 | slot->sl_opcnt = resp->opcnt; |
| 1794 | slot->sl_status = resp->cstate.status; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1795 | |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 1796 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1797 | if (nfsd4_not_cached(resp)) { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1798 | slot->sl_datalen = 0; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1799 | return; |
| 1800 | } |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1801 | base = resp->cstate.data_offset; |
| 1802 | slot->sl_datalen = buf->len - base; |
| 1803 | 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] | 1804 | WARN("%s: sessions DRC could not cache compound\n", __func__); |
| 1805 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1809 | * Encode the replay sequence operation from the slot values. |
| 1810 | * If cachethis is FALSE encode the uncached rep error on the next |
| 1811 | * operation which sets resp->p and increments resp->opcnt for |
| 1812 | * nfs4svc_encode_compoundres. |
| 1813 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1814 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1815 | static __be32 |
| 1816 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 1817 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1818 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1819 | struct nfsd4_op *op; |
| 1820 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1821 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1822 | /* Encode the replayed sequence operation */ |
| 1823 | op = &args->ops[resp->opcnt - 1]; |
| 1824 | nfsd4_encode_operation(resp, op); |
| 1825 | |
| 1826 | /* Return nfserr_retry_uncached_rep in next operation. */ |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 1827 | if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1828 | op = &args->ops[resp->opcnt++]; |
| 1829 | op->status = nfserr_retry_uncached_rep; |
| 1830 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1831 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1832 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1836 | * The sequence operation is not cached because we can use the slot and |
| 1837 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1838 | */ |
J. Bruce Fields | 3ca2eb9 | 2014-03-21 12:04:21 -0400 | [diff] [blame] | 1839 | static __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1840 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 1841 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1842 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1843 | struct nfsd4_slot *slot = resp->cstate.slot; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1844 | struct xdr_stream *xdr = &resp->xdr; |
| 1845 | __be32 *p; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1846 | __be32 status; |
| 1847 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1848 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1849 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1850 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
J. Bruce Fields | 0da7b19 | 2014-03-21 11:43:34 -0400 | [diff] [blame] | 1851 | if (status) |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1852 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1853 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1854 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 1855 | if (!p) { |
| 1856 | WARN_ON_ONCE(1); |
| 1857 | return nfserr_serverfault; |
| 1858 | } |
| 1859 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 1860 | xdr_commit_encode(xdr); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1861 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1862 | resp->opcnt = slot->sl_opcnt; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1863 | return slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1864 | } |
| 1865 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1866 | /* |
| 1867 | * Set the exchange_id flags returned by the server. |
| 1868 | */ |
| 1869 | static void |
| 1870 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 1871 | { |
| 1872 | /* pNFS is not supported */ |
| 1873 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
| 1874 | |
| 1875 | /* Referrals are supported, Migration is not. */ |
| 1876 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 1877 | |
| 1878 | /* set the wire flags to return to client. */ |
| 1879 | clid->flags = new->cl_exchange_flags; |
| 1880 | } |
| 1881 | |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 1882 | static bool client_has_state(struct nfs4_client *clp) |
| 1883 | { |
| 1884 | /* |
| 1885 | * Note clp->cl_openowners check isn't quite right: there's no |
| 1886 | * need to count owners without stateid's. |
| 1887 | * |
| 1888 | * Also note we should probably be using this in 4.0 case too. |
| 1889 | */ |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 1890 | return !list_empty(&clp->cl_openowners) |
| 1891 | || !list_empty(&clp->cl_delegations) |
| 1892 | || !list_empty(&clp->cl_sessions); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 1893 | } |
| 1894 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1895 | __be32 |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1896 | nfsd4_exchange_id(struct svc_rqst *rqstp, |
| 1897 | struct nfsd4_compound_state *cstate, |
| 1898 | struct nfsd4_exchange_id *exid) |
| 1899 | { |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1900 | struct nfs4_client *unconf, *conf, *new; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 1901 | __be32 status; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1902 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1903 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1904 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 1905 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1906 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1907 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1908 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1909 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1910 | "ip_addr=%s flags %x, spa_how %d\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1911 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1912 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1913 | |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 1914 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1915 | return nfserr_inval; |
| 1916 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1917 | switch (exid->spa_how) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1918 | case SP4_MACH_CRED: |
| 1919 | if (!svc_rqst_integrity_protected(rqstp)) |
| 1920 | return nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1921 | case SP4_NONE: |
| 1922 | break; |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 1923 | default: /* checked by xdr code */ |
| 1924 | WARN_ON_ONCE(1); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1925 | case SP4_SSV: |
J. Bruce Fields | dd30333 | 2013-04-12 18:10:56 -0400 | [diff] [blame] | 1926 | return nfserr_encr_alg_unsupp; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1927 | } |
| 1928 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 1929 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1930 | nfs4_lock_state(); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1931 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1932 | if (conf) { |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 1933 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 1934 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 1935 | |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1936 | if (update) { |
| 1937 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 1938 | status = nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1939 | goto out; |
| 1940 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1941 | if (!mach_creds_match(conf, rqstp)) { |
| 1942 | status = nfserr_wrong_cred; |
| 1943 | goto out; |
| 1944 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1945 | if (!creds_match) { /* case 9 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1946 | status = nfserr_perm; |
| 1947 | goto out; |
| 1948 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1949 | if (!verfs_match) { /* case 8 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1950 | status = nfserr_not_same; |
| 1951 | goto out; |
| 1952 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1953 | /* case 6 */ |
| 1954 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
| 1955 | new = conf; |
| 1956 | goto out_copy; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1957 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1958 | if (!creds_match) { /* case 3 */ |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 1959 | if (client_has_state(conf)) { |
| 1960 | status = nfserr_clid_inuse; |
| 1961 | goto out; |
| 1962 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1963 | expire_client(conf); |
| 1964 | goto out_new; |
| 1965 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1966 | if (verfs_match) { /* case 2 */ |
J. Bruce Fields | 0f1ba0e | 2012-05-25 21:24:40 -0400 | [diff] [blame] | 1967 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1968 | new = conf; |
| 1969 | goto out_copy; |
| 1970 | } |
| 1971 | /* case 5, client reboot */ |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1972 | goto out_new; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 1973 | } |
| 1974 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 1975 | if (update) { /* case 7 */ |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 1976 | status = nfserr_noent; |
| 1977 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1978 | } |
| 1979 | |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1980 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 1981 | if (unconf) /* case 4, possible retry or client restart */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1982 | expire_client(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1983 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 1984 | /* case 1 (normal case) */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1985 | out_new: |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 1986 | new = create_client(exid->clname, rqstp, &verf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1987 | if (new == NULL) { |
J. Bruce Fields | 4731030 | 2010-06-22 16:17:12 -0400 | [diff] [blame] | 1988 | status = nfserr_jukebox; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1989 | goto out; |
| 1990 | } |
J. Bruce Fields | 4f540e2 | 2013-05-07 11:57:52 -0400 | [diff] [blame] | 1991 | new->cl_minorversion = cstate->minorversion; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1992 | new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1993 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1994 | gen_clid(new, nn); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1995 | add_to_unconfirmed(new); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1996 | out_copy: |
| 1997 | exid->clientid.cl_boot = new->cl_clientid.cl_boot; |
| 1998 | exid->clientid.cl_id = new->cl_clientid.cl_id; |
| 1999 | |
J. Bruce Fields | 778df3f | 2012-05-25 21:40:23 -0400 | [diff] [blame] | 2000 | exid->seqid = new->cl_cs_slot.sl_seqid + 1; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2001 | nfsd4_set_ex_flags(new, exid); |
| 2002 | |
| 2003 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2004 | new->cl_cs_slot.sl_seqid, new->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2005 | status = nfs_ok; |
| 2006 | |
| 2007 | out: |
| 2008 | nfs4_unlock_state(); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2009 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2010 | } |
| 2011 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2012 | static __be32 |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2013 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2014 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2015 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 2016 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2017 | |
| 2018 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2019 | if (slot_inuse) { |
| 2020 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2021 | return nfserr_jukebox; |
| 2022 | else |
| 2023 | return nfserr_seq_misordered; |
| 2024 | } |
J. Bruce Fields | f6d8248 | 2012-02-13 16:13:41 -0500 | [diff] [blame] | 2025 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2026 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2027 | return nfs_ok; |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2028 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2029 | return nfserr_replay_cache; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2030 | return nfserr_seq_misordered; |
| 2031 | } |
| 2032 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2033 | /* |
| 2034 | * Cache the create session result into the create session single DRC |
| 2035 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 2036 | * Do this for solo or embedded create session operations. |
| 2037 | */ |
| 2038 | static void |
| 2039 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2040 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2041 | { |
| 2042 | slot->sl_status = nfserr; |
| 2043 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 2044 | } |
| 2045 | |
| 2046 | static __be32 |
| 2047 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 2048 | struct nfsd4_clid_slot *slot) |
| 2049 | { |
| 2050 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 2051 | return slot->sl_status; |
| 2052 | } |
| 2053 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2054 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 2055 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 2056 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2057 | 3 + /* version, opcount, opcode */ \ |
| 2058 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2059 | /* seqid, slotID, slotID, cache */ \ |
| 2060 | 4 ) * sizeof(__be32)) |
| 2061 | |
| 2062 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 2063 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 2064 | 1 + /* status */ \ |
| 2065 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2066 | 3 + /* opcount, opcode, opstatus*/ \ |
| 2067 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2068 | /* seqid, slotID, slotID, slotID, status */ \ |
| 2069 | 5 ) * sizeof(__be32)) |
| 2070 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2071 | 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] | 2072 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2073 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 2074 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2075 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 2076 | return nfserr_toosmall; |
| 2077 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 2078 | return nfserr_toosmall; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2079 | ca->headerpadsz = 0; |
| 2080 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 2081 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 2082 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 2083 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 2084 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 2085 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 2086 | /* |
| 2087 | * Note decreasing slot size below client's request may make it |
| 2088 | * difficult for client to function correctly, whereas |
| 2089 | * decreasing the number of slots will (just?) affect |
| 2090 | * performance. When short on memory we therefore prefer to |
| 2091 | * decrease number of slots instead of their size. Clients that |
| 2092 | * request larger slots than they need will get poor results: |
| 2093 | */ |
| 2094 | ca->maxreqs = nfsd4_get_drc_mem(ca); |
| 2095 | if (!ca->maxreqs) |
| 2096 | return nfserr_jukebox; |
| 2097 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2098 | return nfs_ok; |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2099 | } |
| 2100 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2101 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
| 2102 | RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32)) |
| 2103 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
| 2104 | RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32)) |
| 2105 | |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2106 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 2107 | { |
| 2108 | ca->headerpadsz = 0; |
| 2109 | |
| 2110 | /* |
| 2111 | * These RPC_MAX_HEADER macros are overkill, especially since we |
| 2112 | * don't even do gss on the backchannel yet. But this is still |
| 2113 | * less than 1k. Tighten up this estimate in the unlikely event |
| 2114 | * it turns out to be a problem for some client: |
| 2115 | */ |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2116 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2117 | return nfserr_toosmall; |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2118 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2119 | return nfserr_toosmall; |
| 2120 | ca->maxresp_cached = 0; |
| 2121 | if (ca->maxops < 2) |
| 2122 | return nfserr_toosmall; |
| 2123 | |
| 2124 | return nfs_ok; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2125 | } |
| 2126 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2127 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 2128 | { |
| 2129 | switch (cbs->flavor) { |
| 2130 | case RPC_AUTH_NULL: |
| 2131 | case RPC_AUTH_UNIX: |
| 2132 | return nfs_ok; |
| 2133 | default: |
| 2134 | /* |
| 2135 | * GSS case: the spec doesn't allow us to return this |
| 2136 | * error. But it also doesn't allow us not to support |
| 2137 | * GSS. |
| 2138 | * I'd rather this fail hard than return some error the |
| 2139 | * client might think it can already handle: |
| 2140 | */ |
| 2141 | return nfserr_encr_alg_unsupp; |
| 2142 | } |
| 2143 | } |
| 2144 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2145 | __be32 |
| 2146 | nfsd4_create_session(struct svc_rqst *rqstp, |
| 2147 | struct nfsd4_compound_state *cstate, |
| 2148 | struct nfsd4_create_session *cr_ses) |
| 2149 | { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2150 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2151 | struct nfs4_client *conf, *unconf; |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2152 | struct nfsd4_session *new; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2153 | struct nfsd4_conn *conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2154 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2155 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2156 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2157 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2158 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 2159 | return nfserr_inval; |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2160 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 2161 | if (status) |
| 2162 | return status; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2163 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2164 | if (status) |
| 2165 | return status; |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2166 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 2167 | if (status) |
Kinglong Mee | f403e45 | 2013-12-23 17:31:21 +0800 | [diff] [blame] | 2168 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2169 | status = nfserr_jukebox; |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 2170 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2171 | if (!new) |
| 2172 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2173 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 2174 | if (!conn) |
| 2175 | goto out_free_session; |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2176 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2177 | nfs4_lock_state(); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2178 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2179 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2180 | WARN_ON_ONCE(conf && unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2181 | |
| 2182 | if (conf) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2183 | status = nfserr_wrong_cred; |
| 2184 | if (!mach_creds_match(conf, rqstp)) |
| 2185 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2186 | cs_slot = &conf->cl_cs_slot; |
| 2187 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 2188 | if (status == nfserr_replay_cache) { |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2189 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2190 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2191 | } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2192 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2193 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2194 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2195 | } else if (unconf) { |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2196 | struct nfs4_client *old; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2197 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2198 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2199 | status = nfserr_clid_inuse; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2200 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2201 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2202 | status = nfserr_wrong_cred; |
| 2203 | if (!mach_creds_match(unconf, rqstp)) |
| 2204 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2205 | cs_slot = &unconf->cl_cs_slot; |
| 2206 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 2207 | if (status) { |
| 2208 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2209 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2210 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2211 | } |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2212 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2213 | if (old) { |
| 2214 | status = mark_client_expired(old); |
| 2215 | if (status) |
| 2216 | goto out_free_conn; |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2217 | expire_client(old); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2218 | } |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2219 | move_to_confirmed(unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2220 | conf = unconf; |
| 2221 | } else { |
| 2222 | status = nfserr_stale_clientid; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2223 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2224 | } |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2225 | status = nfs_ok; |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 2226 | /* |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 2227 | * We do not support RDMA or persistent sessions |
| 2228 | */ |
| 2229 | cr_ses->flags &= ~SESSION4_PERSIST; |
| 2230 | cr_ses->flags &= ~SESSION4_RDMA; |
| 2231 | |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2232 | init_session(rqstp, new, conf, cr_ses); |
| 2233 | nfsd4_init_conn(rqstp, conn, new); |
| 2234 | |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2235 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2236 | NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 2237 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2238 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2239 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2240 | /* cache solo and embedded create sessions under the state lock */ |
| 2241 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2242 | nfs4_unlock_state(); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2243 | return status; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2244 | out_free_conn: |
Yanchuan Nian | 266533c | 2012-12-24 18:11:45 +0800 | [diff] [blame] | 2245 | nfs4_unlock_state(); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2246 | free_conn(conn); |
| 2247 | out_free_session: |
| 2248 | __free_session(new); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2249 | out_release_drc_mem: |
| 2250 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
J. Bruce Fields | 1ca5079 | 2013-03-14 18:12:03 -0400 | [diff] [blame] | 2251 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2252 | } |
| 2253 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2254 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 2255 | { |
| 2256 | switch (*dir) { |
| 2257 | case NFS4_CDFC4_FORE: |
| 2258 | case NFS4_CDFC4_BACK: |
| 2259 | return nfs_ok; |
| 2260 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 2261 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 2262 | *dir = NFS4_CDFC4_BOTH; |
| 2263 | return nfs_ok; |
| 2264 | }; |
| 2265 | return nfserr_inval; |
| 2266 | } |
| 2267 | |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2268 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc) |
| 2269 | { |
| 2270 | struct nfsd4_session *session = cstate->session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2271 | 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] | 2272 | __be32 status; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2273 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2274 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 2275 | if (status) |
| 2276 | return status; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2277 | spin_lock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2278 | session->se_cb_prog = bc->bc_cb_program; |
| 2279 | session->se_cb_sec = bc->bc_cb_sec; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2280 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2281 | |
| 2282 | nfsd4_probe_callback(session->se_client); |
| 2283 | |
| 2284 | return nfs_ok; |
| 2285 | } |
| 2286 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2287 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 2288 | struct nfsd4_compound_state *cstate, |
| 2289 | struct nfsd4_bind_conn_to_session *bcts) |
| 2290 | { |
| 2291 | __be32 status; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2292 | struct nfsd4_conn *conn; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2293 | struct nfsd4_session *session; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2294 | struct net *net = SVC_NET(rqstp); |
| 2295 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2296 | |
| 2297 | if (!nfsd4_last_compound_op(rqstp)) |
| 2298 | return nfserr_not_only_op; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2299 | nfs4_lock_state(); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2300 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2301 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2302 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2303 | if (!session) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2304 | goto out_no_session; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2305 | status = nfserr_wrong_cred; |
| 2306 | if (!mach_creds_match(session->se_client, rqstp)) |
| 2307 | goto out; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2308 | status = nfsd4_map_bcts_dir(&bcts->dir); |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2309 | if (status) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2310 | goto out; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2311 | conn = alloc_conn(rqstp, bcts->dir); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2312 | status = nfserr_jukebox; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2313 | if (!conn) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2314 | goto out; |
| 2315 | nfsd4_init_conn(rqstp, conn, session); |
| 2316 | status = nfs_ok; |
| 2317 | out: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2318 | nfsd4_put_session(session); |
| 2319 | out_no_session: |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2320 | nfs4_unlock_state(); |
| 2321 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2322 | } |
| 2323 | |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2324 | static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid) |
| 2325 | { |
| 2326 | if (!session) |
| 2327 | return 0; |
| 2328 | return !memcmp(sid, &session->se_sessionid, sizeof(*sid)); |
| 2329 | } |
| 2330 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2331 | __be32 |
| 2332 | nfsd4_destroy_session(struct svc_rqst *r, |
| 2333 | struct nfsd4_compound_state *cstate, |
| 2334 | struct nfsd4_destroy_session *sessionid) |
| 2335 | { |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2336 | struct nfsd4_session *ses; |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2337 | __be32 status; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2338 | int ref_held_by_me = 0; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2339 | struct net *net = SVC_NET(r); |
| 2340 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2341 | |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2342 | nfs4_lock_state(); |
| 2343 | status = nfserr_not_only_op; |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2344 | if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2345 | if (!nfsd4_last_compound_op(r)) |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2346 | goto out; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2347 | ref_held_by_me++; |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2348 | } |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2349 | dump_sessionid(__func__, &sessionid->sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2350 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2351 | ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2352 | if (!ses) |
| 2353 | goto out_client_lock; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2354 | status = nfserr_wrong_cred; |
| 2355 | if (!mach_creds_match(ses->se_client, r)) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2356 | goto out_put_session; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2357 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2358 | if (status) |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2359 | goto out_put_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2360 | unhash_session(ses); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2361 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2362 | |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 2363 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 2364 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2365 | spin_lock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2366 | status = nfs_ok; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2367 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2368 | nfsd4_put_session_locked(ses); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2369 | out_client_lock: |
| 2370 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2371 | out: |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2372 | nfs4_unlock_state(); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2373 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2374 | } |
| 2375 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2376 | 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] | 2377 | { |
| 2378 | struct nfsd4_conn *c; |
| 2379 | |
| 2380 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2381 | if (c->cn_xprt == xpt) { |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2382 | return c; |
| 2383 | } |
| 2384 | } |
| 2385 | return NULL; |
| 2386 | } |
| 2387 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2388 | 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] | 2389 | { |
| 2390 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2391 | struct nfsd4_conn *c; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2392 | __be32 status = nfs_ok; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2393 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2394 | |
| 2395 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2396 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2397 | if (c) |
| 2398 | goto out_free; |
| 2399 | status = nfserr_conn_not_bound_to_session; |
| 2400 | if (clp->cl_mach_cred) |
| 2401 | goto out_free; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2402 | __nfsd4_hash_conn(new, ses); |
| 2403 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2404 | ret = nfsd4_register_conn(new); |
| 2405 | if (ret) |
| 2406 | /* oops; xprt is already down: */ |
| 2407 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2408 | return nfs_ok; |
| 2409 | out_free: |
| 2410 | spin_unlock(&clp->cl_lock); |
| 2411 | free_conn(new); |
| 2412 | return status; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2413 | } |
| 2414 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2415 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 2416 | { |
| 2417 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 2418 | |
| 2419 | return args->opcnt > session->se_fchannel.maxops; |
| 2420 | } |
| 2421 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2422 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 2423 | struct nfsd4_session *session) |
| 2424 | { |
| 2425 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 2426 | |
| 2427 | return xb->len > session->se_fchannel.maxreq_sz; |
| 2428 | } |
| 2429 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2430 | __be32 |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2431 | nfsd4_sequence(struct svc_rqst *rqstp, |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2432 | struct nfsd4_compound_state *cstate, |
| 2433 | struct nfsd4_sequence *seq) |
| 2434 | { |
Andy Adamson | f9bb94c | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2435 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2436 | struct xdr_stream *xdr = &resp->xdr; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2437 | struct nfsd4_session *session; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2438 | struct nfs4_client *clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2439 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2440 | struct nfsd4_conn *conn; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2441 | __be32 status; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2442 | int buflen; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2443 | struct net *net = SVC_NET(rqstp); |
| 2444 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2445 | |
Andy Adamson | f9bb94c | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2446 | if (resp->opcnt != 1) |
| 2447 | return nfserr_sequence_pos; |
| 2448 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2449 | /* |
| 2450 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 2451 | * below. |
| 2452 | */ |
| 2453 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 2454 | if (!conn) |
| 2455 | return nfserr_jukebox; |
| 2456 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2457 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2458 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2459 | if (!session) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2460 | goto out_no_session; |
| 2461 | clp = session->se_client; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2462 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2463 | status = nfserr_too_many_ops; |
| 2464 | if (nfsd4_session_too_many_ops(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2465 | goto out_put_session; |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2466 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2467 | status = nfserr_req_too_big; |
| 2468 | if (nfsd4_request_too_big(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2469 | goto out_put_session; |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2470 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2471 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 2472 | if (seq->slotid >= session->se_fchannel.maxreqs) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2473 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2474 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2475 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2476 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 2477 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 2478 | /* We do not negotiate the number of slots yet, so set the |
| 2479 | * maxslots to the session maxreqs which is used to encode |
| 2480 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 2481 | seq->maxslots = session->se_fchannel.maxreqs; |
| 2482 | |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2483 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 2484 | slot->sl_flags & NFSD4_SLOT_INUSE); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2485 | if (status == nfserr_replay_cache) { |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2486 | status = nfserr_seq_misordered; |
| 2487 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2488 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2489 | cstate->slot = slot; |
| 2490 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2491 | cstate->clp = clp; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2492 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2493 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2494 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2495 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 2496 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2497 | } |
| 2498 | if (status) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2499 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2500 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2501 | status = nfsd4_sequence_check_conn(conn, session); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2502 | conn = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2503 | if (status) |
| 2504 | goto out_put_session; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2505 | |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2506 | buflen = (seq->cachethis) ? |
| 2507 | session->se_fchannel.maxresp_cached : |
| 2508 | session->se_fchannel.maxresp_sz; |
| 2509 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 2510 | nfserr_rep_too_big; |
J. Bruce Fields | a5cddc8 | 2014-05-12 18:10:58 -0400 | [diff] [blame] | 2511 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2512 | goto out_put_session; |
J. Bruce Fields | 32aaa62 | 2014-03-20 20:47:41 -0400 | [diff] [blame] | 2513 | svc_reserve(rqstp, buflen); |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2514 | |
| 2515 | status = nfs_ok; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2516 | /* Success! bump slot seqid */ |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2517 | slot->sl_seqid = seq->seqid; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2518 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2519 | if (seq->cachethis) |
| 2520 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2521 | else |
| 2522 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2523 | |
| 2524 | cstate->slot = slot; |
| 2525 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2526 | cstate->clp = clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2527 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2528 | out: |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2529 | switch (clp->cl_cb_state) { |
| 2530 | case NFSD4_CB_DOWN: |
| 2531 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 2532 | break; |
| 2533 | case NFSD4_CB_FAULT: |
| 2534 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 2535 | break; |
| 2536 | default: |
| 2537 | seq->status_flags = 0; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 2538 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 2539 | if (!list_empty(&clp->cl_revoked)) |
| 2540 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2541 | out_no_session: |
Kinglong Mee | 3f42d2c | 2014-03-24 11:56:59 +0800 | [diff] [blame] | 2542 | if (conn) |
| 2543 | free_conn(conn); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2544 | spin_unlock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2545 | return status; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2546 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2547 | nfsd4_put_session_locked(session); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2548 | goto out_no_session; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2549 | } |
| 2550 | |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2551 | void |
| 2552 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 2553 | { |
| 2554 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 2555 | |
| 2556 | if (nfsd4_has_session(cs)) { |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2557 | if (cs->status != nfserr_replay_cache) { |
| 2558 | nfsd4_store_cache_entry(resp); |
| 2559 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 2560 | } |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2561 | /* Drop session reference that was taken in nfsd4_sequence() */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2562 | nfsd4_put_session(cs->session); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2563 | } else if (cs->clp) |
| 2564 | put_client_renew(cs->clp); |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2565 | } |
| 2566 | |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2567 | __be32 |
| 2568 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc) |
| 2569 | { |
| 2570 | struct nfs4_client *conf, *unconf, *clp; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2571 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2572 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2573 | |
| 2574 | nfs4_lock_state(); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2575 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2576 | conf = find_confirmed_client(&dc->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2577 | WARN_ON_ONCE(conf && unconf); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2578 | |
| 2579 | if (conf) { |
| 2580 | clp = conf; |
| 2581 | |
J. Bruce Fields | c0293b0 | 2013-03-14 18:20:01 -0400 | [diff] [blame] | 2582 | if (client_has_state(conf)) { |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2583 | status = nfserr_clientid_busy; |
| 2584 | goto out; |
| 2585 | } |
| 2586 | } else if (unconf) |
| 2587 | clp = unconf; |
| 2588 | else { |
| 2589 | status = nfserr_stale_clientid; |
| 2590 | goto out; |
| 2591 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2592 | if (!mach_creds_match(clp, rqstp)) { |
| 2593 | status = nfserr_wrong_cred; |
| 2594 | goto out; |
| 2595 | } |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2596 | expire_client(clp); |
| 2597 | out: |
| 2598 | nfs4_unlock_state(); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2599 | return status; |
| 2600 | } |
| 2601 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2602 | __be32 |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2603 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc) |
| 2604 | { |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2605 | __be32 status = 0; |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2606 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2607 | if (rc->rca_one_fs) { |
| 2608 | if (!cstate->current_fh.fh_dentry) |
| 2609 | return nfserr_nofilehandle; |
| 2610 | /* |
| 2611 | * We don't take advantage of the rca_one_fs case. |
| 2612 | * That's OK, it's optional, we can safely ignore it. |
| 2613 | */ |
| 2614 | return nfs_ok; |
| 2615 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2616 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2617 | nfs4_lock_state(); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2618 | status = nfserr_complete_already; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 2619 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
| 2620 | &cstate->session->se_client->cl_flags)) |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2621 | goto out; |
| 2622 | |
| 2623 | status = nfserr_stale_clientid; |
| 2624 | if (is_client_expired(cstate->session->se_client)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2625 | /* |
| 2626 | * The following error isn't really legal. |
| 2627 | * But we only get here if the client just explicitly |
| 2628 | * destroyed the client. Surely it no longer cares what |
| 2629 | * error it gets back on an operation for the dead |
| 2630 | * client. |
| 2631 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2632 | goto out; |
| 2633 | |
| 2634 | status = nfs_ok; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 2635 | nfsd4_client_record_create(cstate->session->se_client); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2636 | out: |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2637 | nfs4_unlock_state(); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2638 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 2642 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 2643 | struct nfsd4_setclientid *setclid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | { |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 2645 | struct xdr_netobj clname = setclid->se_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | nfs4_verifier clverifier = setclid->se_verf; |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2647 | struct nfs4_client *conf, *unconf, *new; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2648 | __be32 status; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2649 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 2650 | |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2651 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | nfs4_lock_state(); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2653 | conf = find_confirmed_client_by_name(&clname, nn); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2654 | if (conf) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2655 | /* case 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2657 | if (clp_used_exchangeid(conf)) |
| 2658 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 2659 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2660 | char addr_str[INET6_ADDRSTRLEN]; |
| 2661 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, |
| 2662 | sizeof(addr_str)); |
| 2663 | dprintk("NFSD: setclientid: string in use by client " |
| 2664 | "at %s\n", addr_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | goto out; |
| 2666 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2668 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 2669 | if (unconf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | status = nfserr_jukebox; |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 2672 | new = create_client(clname, rqstp, &clverifier); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 2673 | if (new == NULL) |
| 2674 | goto out; |
J. Bruce Fields | 34b232b | 2012-05-18 22:23:42 -0400 | [diff] [blame] | 2675 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2676 | /* case 1: probable callback update */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | copy_clid(new, conf); |
J. Bruce Fields | 34b232b | 2012-05-18 22:23:42 -0400 | [diff] [blame] | 2678 | else /* case 4 (new client) or cases 2, 3 (client reboot): */ |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2679 | gen_clid(new, nn); |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 2680 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2681 | gen_callback(new, setclid, rqstp); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2682 | add_to_unconfirmed(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 2684 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 2685 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); |
| 2686 | status = nfs_ok; |
| 2687 | out: |
| 2688 | nfs4_unlock_state(); |
| 2689 | return status; |
| 2690 | } |
| 2691 | |
| 2692 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2693 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 2694 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
| 2695 | struct nfsd4_compound_state *cstate, |
| 2696 | struct nfsd4_setclientid_confirm *setclientid_confirm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | { |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2698 | struct nfs4_client *conf, *unconf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 2700 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2701 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 2702 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 2704 | if (STALE_CLIENTID(clid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | return nfserr_stale_clientid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | nfs4_lock_state(); |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2707 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2708 | conf = find_confirmed_client(clid, false, nn); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2709 | unconf = find_unconfirmed_client(clid, false, nn); |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2710 | /* |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2711 | * We try hard to give out unique clientid's, so if we get an |
| 2712 | * attempt to confirm the same clientid with a different cred, |
| 2713 | * there's a bug somewhere. Let's charitably assume it's our |
| 2714 | * bug. |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2715 | */ |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2716 | status = nfserr_serverfault; |
| 2717 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
| 2718 | goto out; |
| 2719 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
| 2720 | goto out; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2721 | /* cases below refer to rfc 3530 section 14.2.34: */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 2722 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
| 2723 | if (conf && !unconf) /* case 2: probable retransmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | status = nfs_ok; |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 2725 | else /* case 4: client hasn't noticed we rebooted yet? */ |
| 2726 | status = nfserr_stale_clientid; |
| 2727 | goto out; |
| 2728 | } |
| 2729 | status = nfs_ok; |
| 2730 | if (conf) { /* case 1: callback update */ |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2731 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
| 2732 | nfsd4_probe_callback(conf); |
| 2733 | expire_client(unconf); |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 2734 | } else { /* case 3: normal case; new or rebooted client */ |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2735 | conf = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2736 | if (conf) { |
| 2737 | status = mark_client_expired(conf); |
| 2738 | if (status) |
| 2739 | goto out; |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2740 | expire_client(conf); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2741 | } |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2742 | move_to_confirmed(unconf); |
J. Bruce Fields | f3d03b9 | 2012-05-23 11:38:38 -0400 | [diff] [blame] | 2743 | nfsd4_probe_callback(unconf); |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 2744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | nfs4_unlock_state(); |
| 2747 | return status; |
| 2748 | } |
| 2749 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2750 | static struct nfs4_file *nfsd4_alloc_file(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | { |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2752 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 2753 | } |
| 2754 | |
| 2755 | /* OPEN Share state helper functions */ |
| 2756 | static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino) |
| 2757 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | unsigned int hashval = file_hashval(ino); |
| 2759 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 2760 | lockdep_assert_held(&state_lock); |
| 2761 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2762 | atomic_set(&fp->fi_ref, 1); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 2763 | spin_lock_init(&fp->fi_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2764 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 2765 | INIT_LIST_HEAD(&fp->fi_delegations); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 2766 | ihold(ino); |
| 2767 | fp->fi_inode = ino; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2768 | fp->fi_had_conflict = false; |
| 2769 | fp->fi_lease = NULL; |
| 2770 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 2771 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 2772 | hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | } |
| 2774 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 2775 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2776 | nfsd4_free_slabs(void) |
| 2777 | { |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2778 | kmem_cache_destroy(openowner_slab); |
| 2779 | kmem_cache_destroy(lockowner_slab); |
| 2780 | kmem_cache_destroy(file_slab); |
| 2781 | kmem_cache_destroy(stateid_slab); |
| 2782 | kmem_cache_destroy(deleg_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | |
Bryan Schumaker | 7208339 | 2011-11-01 15:24:59 -0400 | [diff] [blame] | 2785 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | nfsd4_init_slabs(void) |
| 2787 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2788 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 2789 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 2790 | if (openowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2791 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2792 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
Yanchuan Nian | 3c40794 | 2012-10-24 14:44:19 +0800 | [diff] [blame] | 2793 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2794 | if (lockowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2795 | goto out_free_openowner_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2796 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2797 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2798 | if (file_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2799 | goto out_free_lockowner_slab; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2800 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2801 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2802 | if (stateid_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2803 | goto out_free_file_slab; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 2804 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2805 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 2806 | if (deleg_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2807 | goto out_free_stateid_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | return 0; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2809 | |
| 2810 | out_free_stateid_slab: |
| 2811 | kmem_cache_destroy(stateid_slab); |
| 2812 | out_free_file_slab: |
| 2813 | kmem_cache_destroy(file_slab); |
| 2814 | out_free_lockowner_slab: |
| 2815 | kmem_cache_destroy(lockowner_slab); |
| 2816 | out_free_openowner_slab: |
| 2817 | kmem_cache_destroy(openowner_slab); |
| 2818 | out: |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2819 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 2820 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | } |
| 2822 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2823 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 2824 | { |
| 2825 | rp->rp_status = nfserr_serverfault; |
| 2826 | rp->rp_buflen = 0; |
| 2827 | rp->rp_buf = rp->rp_ibuf; |
| 2828 | } |
| 2829 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2830 | 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] | 2831 | { |
| 2832 | struct nfs4_stateowner *sop; |
| 2833 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2834 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2835 | if (!sop) |
| 2836 | return NULL; |
| 2837 | |
| 2838 | sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL); |
| 2839 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2840 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2841 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2843 | sop->so_owner.len = owner->len; |
| 2844 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2845 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2846 | sop->so_client = clp; |
| 2847 | init_nfs4_replay(&sop->so_replay); |
| 2848 | return sop; |
| 2849 | } |
| 2850 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2851 | 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] | 2852 | { |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 2853 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2854 | |
| 2855 | list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2856 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | } |
| 2858 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2859 | static struct nfs4_openowner * |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 2860 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 2861 | struct nfsd4_compound_state *cstate) |
| 2862 | { |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 2863 | struct nfs4_client *clp = cstate->clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2864 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2866 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 2867 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | return NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2869 | oo->oo_owner.so_is_open_owner = 1; |
| 2870 | oo->oo_owner.so_seqid = open->op_seqid; |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 2871 | oo->oo_flags = NFS4_OO_NEW; |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 2872 | if (nfsd4_has_session(cstate)) |
| 2873 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2874 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 2875 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2876 | INIT_LIST_HEAD(&oo->oo_close_lru); |
| 2877 | hash_openowner(oo, clp, strhashval); |
| 2878 | return oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | } |
| 2880 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 2881 | 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] | 2882 | struct nfs4_openowner *oo = open->op_openowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 2884 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 2885 | INIT_LIST_HEAD(&stp->st_locks); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2886 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2887 | stp->st_stateowner = &oo->oo_owner; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2888 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | stp->st_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | stp->st_access_bmap = 0; |
| 2891 | stp->st_deny_bmap = 0; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 2892 | set_access(open->op_share_access, stp); |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 2893 | set_deny(open->op_share_deny, stp); |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 2894 | stp->st_openstp = NULL; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 2895 | spin_lock(&fp->fi_lock); |
| 2896 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 2897 | spin_unlock(&fp->fi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | static void |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 2901 | move_to_close_lru(struct nfs4_openowner *oo, struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2902 | { |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 2903 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 2904 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2905 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 2907 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2908 | oo->oo_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | } |
| 2910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2911 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2912 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, |
| 2913 | clientid_t *clid) |
| 2914 | { |
| 2915 | return (sop->so_owner.len == owner->len) && |
| 2916 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len) && |
| 2917 | (sop->so_client->cl_clientid.cl_id == clid->cl_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | } |
| 2919 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2920 | static struct nfs4_openowner * |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 2921 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
| 2922 | bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2924 | struct nfs4_stateowner *so; |
| 2925 | struct nfs4_openowner *oo; |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 2926 | struct nfs4_client *clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2927 | |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 2928 | list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) { |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 2929 | if (!so->so_is_open_owner) |
| 2930 | continue; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2931 | if (same_owner_str(so, &open->op_owner, &open->op_clientid)) { |
| 2932 | oo = openowner(so); |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 2933 | clp = oo->oo_owner.so_client; |
| 2934 | if ((bool)clp->cl_minorversion != sessions) |
| 2935 | return NULL; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2936 | renew_client(oo->oo_owner.so_client); |
| 2937 | return oo; |
| 2938 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | } |
| 2940 | return NULL; |
| 2941 | } |
| 2942 | |
| 2943 | /* search file_hashtbl[] for file */ |
| 2944 | static struct nfs4_file * |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 2945 | find_file_locked(struct inode *ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | { |
| 2947 | unsigned int hashval = file_hashval(ino); |
| 2948 | struct nfs4_file *fp; |
| 2949 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 2950 | lockdep_assert_held(&state_lock); |
| 2951 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 2952 | hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) { |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2953 | if (fp->fi_inode == ino) { |
| 2954 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2956 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | } |
| 2958 | return NULL; |
| 2959 | } |
| 2960 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 2961 | static struct nfs4_file * |
| 2962 | find_file(struct inode *ino) |
| 2963 | { |
| 2964 | struct nfs4_file *fp; |
| 2965 | |
| 2966 | spin_lock(&state_lock); |
| 2967 | fp = find_file_locked(ino); |
| 2968 | spin_unlock(&state_lock); |
| 2969 | return fp; |
| 2970 | } |
| 2971 | |
| 2972 | static struct nfs4_file * |
| 2973 | find_or_add_file(struct inode *ino, struct nfs4_file *new) |
| 2974 | { |
| 2975 | struct nfs4_file *fp; |
| 2976 | |
| 2977 | spin_lock(&state_lock); |
| 2978 | fp = find_file_locked(ino); |
| 2979 | if (fp == NULL) { |
| 2980 | nfsd4_init_file(new, ino); |
| 2981 | fp = new; |
| 2982 | } |
| 2983 | spin_unlock(&state_lock); |
| 2984 | |
| 2985 | return fp; |
| 2986 | } |
| 2987 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 2988 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | * Called to check deny when READ with all zero stateid or |
| 2990 | * WRITE with all zero or all one stateid |
| 2991 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2992 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 2994 | { |
| 2995 | struct inode *ino = current_fh->fh_dentry->d_inode; |
| 2996 | struct nfs4_file *fp; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2997 | struct nfs4_ol_stateid *stp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2998 | __be32 ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | fp = find_file(ino); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3001 | if (!fp) |
| 3002 | return nfs_ok; |
NeilBrown | b700949 | 2005-07-07 17:59:23 -0700 | [diff] [blame] | 3003 | ret = nfserr_locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3004 | /* Search for conflicting share reservations */ |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3005 | spin_lock(&fp->fi_lock); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3006 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) { |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 3007 | if (test_deny(deny_type, stp) || |
| 3008 | test_deny(NFS4_SHARE_DENY_BOTH, stp)) |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3009 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3010 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3011 | ret = nfs_ok; |
| 3012 | out: |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3013 | spin_unlock(&fp->fi_lock); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3014 | put_nfs4_file(fp); |
| 3015 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | } |
| 3017 | |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3018 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | { |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 3020 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 3021 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 3022 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3023 | lockdep_assert_held(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | /* We're assuming the state code never drops its reference |
| 3025 | * without first removing the lease. Since we're in this lease |
| 3026 | * callback (and since the lease code is serialized by the kernel |
| 3027 | * lock) we know the server hasn't removed the lease yet, we know |
| 3028 | * it's safe to take a reference: */ |
| 3029 | atomic_inc(&dp->dl_count); |
| 3030 | |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3031 | /* |
| 3032 | * If the dl_time != 0, then we know that it has already been |
| 3033 | * queued for a lease break. Don't queue it again. |
| 3034 | */ |
| 3035 | if (dp->dl_time == 0) { |
| 3036 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
| 3037 | dp->dl_time = get_seconds(); |
| 3038 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 3040 | block_delegations(&dp->dl_fh); |
| 3041 | |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3042 | nfsd4_cb_recall(dp); |
| 3043 | } |
| 3044 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 3045 | /* Called from break_lease() with i_lock held. */ |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3046 | static void nfsd_break_deleg_cb(struct file_lock *fl) |
| 3047 | { |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3048 | struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; |
| 3049 | struct nfs4_delegation *dp; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3050 | |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 3051 | if (!fp) { |
| 3052 | WARN(1, "(%p)->fl_owner NULL\n", fl); |
| 3053 | return; |
| 3054 | } |
| 3055 | if (fp->fi_had_conflict) { |
| 3056 | WARN(1, "duplicate break on %p\n", fp); |
| 3057 | return; |
| 3058 | } |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3059 | /* |
| 3060 | * 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] | 3061 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3062 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3063 | */ |
| 3064 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3066 | spin_lock(&state_lock); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3067 | fp->fi_had_conflict = true; |
| 3068 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) |
| 3069 | nfsd_break_one_deleg(dp); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3070 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | } |
| 3072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | static |
| 3074 | int nfsd_change_deleg_cb(struct file_lock **onlist, int arg) |
| 3075 | { |
| 3076 | if (arg & F_UNLCK) |
| 3077 | return lease_modify(onlist, arg); |
| 3078 | else |
| 3079 | return -EAGAIN; |
| 3080 | } |
| 3081 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 3082 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 3083 | .lm_break = nfsd_break_deleg_cb, |
| 3084 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | }; |
| 3086 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 3087 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 3088 | { |
| 3089 | if (nfsd4_has_session(cstate)) |
| 3090 | return nfs_ok; |
| 3091 | if (seqid == so->so_seqid - 1) |
| 3092 | return nfserr_replay_me; |
| 3093 | if (seqid == so->so_seqid) |
| 3094 | return nfs_ok; |
| 3095 | return nfserr_bad_seqid; |
| 3096 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3098 | static __be32 lookup_clientid(clientid_t *clid, |
| 3099 | struct nfsd4_compound_state *cstate, |
| 3100 | struct nfsd_net *nn) |
| 3101 | { |
| 3102 | struct nfs4_client *found; |
| 3103 | |
| 3104 | if (cstate->clp) { |
| 3105 | found = cstate->clp; |
| 3106 | if (!same_clid(&found->cl_clientid, clid)) |
| 3107 | return nfserr_stale_clientid; |
| 3108 | return nfs_ok; |
| 3109 | } |
| 3110 | |
| 3111 | if (STALE_CLIENTID(clid, nn)) |
| 3112 | return nfserr_stale_clientid; |
| 3113 | |
| 3114 | /* |
| 3115 | * For v4.1+ we get the client in the SEQUENCE op. If we don't have one |
| 3116 | * cached already then we know this is for is for v4.0 and "sessions" |
| 3117 | * will be false. |
| 3118 | */ |
| 3119 | WARN_ON_ONCE(cstate->session); |
| 3120 | found = find_confirmed_client(clid, false, nn); |
| 3121 | if (!found) |
| 3122 | return nfserr_expired; |
| 3123 | |
| 3124 | /* Cache the nfs4_client in cstate! */ |
| 3125 | cstate->clp = found; |
| 3126 | atomic_inc(&found->cl_refcount); |
| 3127 | return nfs_ok; |
| 3128 | } |
| 3129 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3130 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3131 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 3132 | struct nfsd4_open *open, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | clientid_t *clientid = &open->op_clientid; |
| 3135 | struct nfs4_client *clp = NULL; |
| 3136 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3137 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3138 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 3140 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | return nfserr_stale_clientid; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3142 | /* |
| 3143 | * In case we need it later, after we've already created the |
| 3144 | * file and don't want to risk a further failure: |
| 3145 | */ |
| 3146 | open->op_file = nfsd4_alloc_file(); |
| 3147 | if (open->op_file == NULL) |
| 3148 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | |
Trond Myklebust | 2d91e89 | 2014-06-30 11:48:46 -0400 | [diff] [blame] | 3150 | status = lookup_clientid(clientid, cstate, nn); |
| 3151 | if (status) |
| 3152 | return status; |
| 3153 | clp = cstate->clp; |
| 3154 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 3155 | strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 3156 | oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3157 | open->op_openowner = oo; |
| 3158 | if (!oo) { |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3159 | goto new_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3161 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3162 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3163 | release_openowner(oo); |
| 3164 | open->op_openowner = NULL; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3165 | goto new_owner; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3166 | } |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3167 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 3168 | if (status) |
| 3169 | return status; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3170 | goto alloc_stateid; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3171 | new_owner: |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3172 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3173 | if (oo == NULL) |
| 3174 | return nfserr_jukebox; |
| 3175 | open->op_openowner = oo; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3176 | alloc_stateid: |
| 3177 | open->op_stp = nfs4_alloc_stateid(clp); |
| 3178 | if (!open->op_stp) |
| 3179 | return nfserr_jukebox; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3180 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | } |
| 3182 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3183 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 3184 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 3185 | { |
| 3186 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 3187 | return nfserr_openmode; |
| 3188 | else |
| 3189 | return nfs_ok; |
| 3190 | } |
| 3191 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 3192 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3193 | { |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3194 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 3195 | } |
| 3196 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3197 | 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] | 3198 | { |
| 3199 | struct nfs4_stid *ret; |
| 3200 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3201 | ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 3202 | if (!ret) |
| 3203 | return NULL; |
| 3204 | return delegstateid(ret); |
| 3205 | } |
| 3206 | |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3207 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 3208 | { |
| 3209 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 3210 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 3211 | } |
| 3212 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3213 | static __be32 |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 3214 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3215 | struct nfs4_delegation **dp) |
| 3216 | { |
| 3217 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3218 | __be32 status = nfserr_bad_stateid; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3219 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3220 | *dp = find_deleg_stateid(cl, &open->op_delegate_stateid); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3221 | if (*dp == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3222 | goto out; |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3223 | flags = share_access_to_flags(open->op_share_access); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3224 | status = nfs4_check_delegmode(*dp, flags); |
| 3225 | if (status) |
| 3226 | *dp = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3227 | out: |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3228 | if (!nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3229 | return nfs_ok; |
| 3230 | if (status) |
| 3231 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3232 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3233 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3234 | } |
| 3235 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3236 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3237 | nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3238 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3239 | struct nfs4_ol_stateid *local; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3240 | struct nfs4_openowner *oo = open->op_openowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3241 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3242 | spin_lock(&fp->fi_lock); |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 3243 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | /* ignore lock owners */ |
| 3245 | if (local->st_stateowner->so_is_open_owner == 0) |
| 3246 | continue; |
| 3247 | /* remember if we have seen this open owner */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3248 | if (local->st_stateowner == &oo->oo_owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3249 | *stpp = local; |
| 3250 | /* check for conflicting share reservations */ |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3251 | if (!test_share(local, open)) { |
| 3252 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 77eaae8 | 2011-09-02 12:08:20 -0400 | [diff] [blame] | 3253 | return nfserr_share_denied; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3255 | } |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3256 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 77eaae8 | 2011-09-02 12:08:20 -0400 | [diff] [blame] | 3257 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | } |
| 3259 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 3260 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 3261 | { |
| 3262 | int flags = 0; |
| 3263 | |
| 3264 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 3265 | flags |= NFSD_MAY_READ; |
| 3266 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 3267 | flags |= NFSD_MAY_WRITE; |
| 3268 | return flags; |
| 3269 | } |
| 3270 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3271 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 3273 | struct nfsd4_open *open) |
| 3274 | { |
| 3275 | struct iattr iattr = { |
| 3276 | .ia_valid = ATTR_SIZE, |
| 3277 | .ia_size = 0, |
| 3278 | }; |
| 3279 | if (!open->op_truncate) |
| 3280 | return 0; |
| 3281 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 3282 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); |
| 3284 | } |
| 3285 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3286 | static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, |
| 3287 | struct svc_fh *cur_fh, struct nfsd4_open *open) |
| 3288 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3289 | struct file *filp = NULL; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3290 | __be32 status; |
| 3291 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 3292 | int access = nfs4_access_to_access(open->op_share_access); |
| 3293 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3294 | spin_lock(&fp->fi_lock); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3295 | if (!fp->fi_fds[oflag]) { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3296 | spin_unlock(&fp->fi_lock); |
| 3297 | status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3298 | if (status) |
| 3299 | goto out; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3300 | spin_lock(&fp->fi_lock); |
| 3301 | if (!fp->fi_fds[oflag]) { |
| 3302 | fp->fi_fds[oflag] = filp; |
| 3303 | filp = NULL; |
| 3304 | } |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3305 | } |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 3306 | status = nfs4_file_get_access(fp, open->op_share_access); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3307 | spin_unlock(&fp->fi_lock); |
| 3308 | if (filp) |
| 3309 | fput(filp); |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 3310 | if (status) |
| 3311 | goto out_put_access; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3312 | |
| 3313 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3314 | if (status) |
| 3315 | goto out_put_access; |
| 3316 | |
| 3317 | return nfs_ok; |
| 3318 | |
| 3319 | out_put_access: |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 3320 | nfs4_file_put_access(fp, open->op_share_access); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3321 | out: |
| 3322 | return status; |
| 3323 | } |
| 3324 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3325 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3326 | 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] | 3327 | { |
J. Bruce Fields | b6d2f1c | 2011-10-10 17:44:19 -0400 | [diff] [blame] | 3328 | u32 op_share_access = open->op_share_access; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3329 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3331 | if (!test_access(op_share_access, stp)) |
Casey Bodley | 0c12eaf | 2011-07-23 14:58:10 -0400 | [diff] [blame] | 3332 | status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3333 | else |
| 3334 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3335 | |
| 3336 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3337 | return status; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3339 | /* remember the open */ |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3340 | set_access(op_share_access, stp); |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 3341 | set_deny(open->op_share_deny, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | return nfs_ok; |
| 3343 | } |
| 3344 | |
| 3345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | static void |
J. Bruce Fields | 1255a8f | 2012-03-06 14:35:16 -0500 | [diff] [blame] | 3347 | nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | { |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3349 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | } |
| 3351 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 3352 | /* Should we give out recallable state?: */ |
| 3353 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 3354 | { |
| 3355 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 3356 | return true; |
| 3357 | /* |
| 3358 | * In the sessions case, since we don't have to establish a |
| 3359 | * separate connection for callbacks, we assume it's OK |
| 3360 | * until we hear otherwise: |
| 3361 | */ |
| 3362 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 3363 | } |
| 3364 | |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3365 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag) |
| 3366 | { |
| 3367 | struct file_lock *fl; |
| 3368 | |
| 3369 | fl = locks_alloc_lock(); |
| 3370 | if (!fl) |
| 3371 | return NULL; |
| 3372 | locks_init_lock(fl); |
| 3373 | fl->fl_lmops = &nfsd_lease_mng_ops; |
J. Bruce Fields | 617588d | 2011-07-01 15:18:34 -0400 | [diff] [blame] | 3374 | fl->fl_flags = FL_DELEG; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3375 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 3376 | fl->fl_end = OFFSET_MAX; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3377 | fl->fl_owner = (fl_owner_t)(dp->dl_file); |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3378 | fl->fl_pid = current->tgid; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3379 | return fl; |
| 3380 | } |
| 3381 | |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3382 | static int nfs4_setlease(struct nfs4_delegation *dp) |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3383 | { |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3384 | struct nfs4_file *fp = dp->dl_file; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3385 | struct file_lock *fl; |
| 3386 | int status; |
| 3387 | |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3388 | fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3389 | if (!fl) |
| 3390 | return -ENOMEM; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3391 | fl->fl_file = find_readable_file(fp); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3392 | status = vfs_setlease(fl->fl_file, fl->fl_type, &fl); |
J. Bruce Fields | e873088 | 2012-01-23 13:52:01 -0500 | [diff] [blame] | 3393 | if (status) |
| 3394 | goto out_free; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3395 | fp->fi_lease = fl; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3396 | fp->fi_deleg_file = fl->fl_file; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3397 | atomic_set(&fp->fi_delegees, 1); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3398 | spin_lock(&state_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 3399 | hash_delegation_locked(dp, fp); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3400 | spin_unlock(&state_lock); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3401 | return 0; |
J. Bruce Fields | e873088 | 2012-01-23 13:52:01 -0500 | [diff] [blame] | 3402 | out_free: |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3403 | if (fl->fl_file) |
| 3404 | fput(fl->fl_file); |
J. Bruce Fields | e873088 | 2012-01-23 13:52:01 -0500 | [diff] [blame] | 3405 | locks_free_lock(fl); |
| 3406 | return status; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3407 | } |
| 3408 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 3409 | 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] | 3410 | { |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 3411 | if (fp->fi_had_conflict) |
| 3412 | return -EAGAIN; |
| 3413 | get_nfs4_file(fp); |
| 3414 | dp->dl_file = fp; |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 3415 | if (!fp->fi_lease) |
| 3416 | return nfs4_setlease(dp); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3417 | spin_lock(&state_lock); |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 3418 | atomic_inc(&fp->fi_delegees); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3419 | if (fp->fi_had_conflict) { |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3420 | spin_unlock(&state_lock); |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 3421 | return -EAGAIN; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3422 | } |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 3423 | hash_delegation_locked(dp, fp); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3424 | spin_unlock(&state_lock); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3425 | return 0; |
| 3426 | } |
| 3427 | |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 3428 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 3429 | { |
| 3430 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3431 | if (status == -EAGAIN) |
| 3432 | open->op_why_no_deleg = WND4_CONTENTION; |
| 3433 | else { |
| 3434 | open->op_why_no_deleg = WND4_RESOURCE; |
| 3435 | switch (open->op_deleg_want) { |
| 3436 | case NFS4_SHARE_WANT_READ_DELEG: |
| 3437 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 3438 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 3439 | break; |
| 3440 | case NFS4_SHARE_WANT_CANCEL: |
| 3441 | open->op_why_no_deleg = WND4_CANCELLED; |
| 3442 | break; |
| 3443 | case NFS4_SHARE_WANT_NO_DELEG: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 3444 | WARN_ON_ONCE(1); |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 3445 | } |
| 3446 | } |
| 3447 | } |
| 3448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | /* |
| 3450 | * Attempt to hand out a delegation. |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3451 | * |
| 3452 | * Note we don't support write delegations, and won't until the vfs has |
| 3453 | * proper support for them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | */ |
| 3455 | static void |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3456 | nfs4_open_delegation(struct net *net, struct svc_fh *fh, |
| 3457 | struct nfsd4_open *open, struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | { |
| 3459 | struct nfs4_delegation *dp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3460 | struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner); |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 3461 | int cb_up; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3462 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3464 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3465 | open->op_recall = 0; |
| 3466 | switch (open->op_claim_type) { |
| 3467 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 3468 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3469 | open->op_recall = 1; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3470 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 3471 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3472 | break; |
| 3473 | case NFS4_OPEN_CLAIM_NULL: |
Ming Chen | ed47b06 | 2014-01-09 21:26:10 +0000 | [diff] [blame] | 3474 | case NFS4_OPEN_CLAIM_FH: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3475 | /* |
| 3476 | * Let's not give out any delegations till everyone's |
| 3477 | * had the chance to reclaim theirs.... |
| 3478 | */ |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3479 | if (locks_in_grace(net)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3480 | goto out_no_deleg; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3481 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3482 | goto out_no_deleg; |
Steve Dickson | 9a0590a | 2013-05-15 14:51:49 -0400 | [diff] [blame] | 3483 | /* |
| 3484 | * Also, if the file was opened for write or |
| 3485 | * create, there's a good chance the client's |
| 3486 | * about to write to it, resulting in an |
| 3487 | * immediate recall (since we don't support |
| 3488 | * write delegations): |
| 3489 | */ |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3490 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3491 | goto out_no_deleg; |
| 3492 | if (open->op_create == NFS4_OPEN_CREATE) |
| 3493 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3494 | break; |
| 3495 | default: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3496 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3497 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3498 | dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3499 | if (dp == NULL) |
| 3500 | goto out_no_deleg; |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 3501 | status = nfs4_set_delegation(dp, stp->st_file); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3502 | if (status) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3503 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 3505 | 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] | 3506 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 3507 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 3508 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3509 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3510 | return; |
| 3511 | out_free: |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 3512 | destroy_delegation(dp); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3513 | out_no_deleg: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3514 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 3515 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 3516 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3517 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 3518 | open->op_recall = 1; |
| 3519 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3520 | |
| 3521 | /* 4.1 client asking for a delegation? */ |
| 3522 | if (open->op_deleg_want) |
| 3523 | nfsd4_open_deleg_none_ext(open, status); |
| 3524 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | } |
| 3526 | |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 3527 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 3528 | struct nfs4_delegation *dp) |
| 3529 | { |
| 3530 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 3531 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 3532 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3533 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 3534 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 3535 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 3536 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3537 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 3538 | } |
| 3539 | /* Otherwise the client must be confused wanting a delegation |
| 3540 | * it already has, therefore we don't return |
| 3541 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 3542 | */ |
| 3543 | } |
| 3544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | /* |
| 3546 | * called with nfs4_lock_state() held. |
| 3547 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3548 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 3550 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3551 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3552 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | struct nfs4_file *fp = NULL; |
| 3554 | struct inode *ino = current_fh->fh_dentry->d_inode; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3555 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3556 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3557 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | /* |
| 3560 | * Lookup file; if found, lookup stateid and check open request, |
| 3561 | * and check for delegations in the process of being recalled. |
| 3562 | * If not found, create the nfs4_file struct |
| 3563 | */ |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3564 | fp = find_or_add_file(ino, open->op_file); |
| 3565 | if (fp != open->op_file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | if ((status = nfs4_check_open(fp, open, &stp))) |
| 3567 | goto out; |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 3568 | status = nfs4_check_deleg(cl, open, &dp); |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3569 | if (status) |
| 3570 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3571 | } else { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3572 | open->op_file = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3573 | status = nfserr_bad_stateid; |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3574 | if (nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3575 | goto out; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 3576 | status = nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | /* |
| 3580 | * OPEN the file, or upgrade an existing OPEN. |
| 3581 | * If truncate fails, the OPEN fails. |
| 3582 | */ |
| 3583 | if (stp) { |
| 3584 | /* Stateid was found, this is an OPEN upgrade */ |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 3585 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3586 | if (status) |
| 3587 | goto out; |
| 3588 | } else { |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3589 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, open); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3590 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3591 | goto out; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3592 | stp = open->op_stp; |
| 3593 | open->op_stp = NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 3594 | init_open_stateid(stp, fp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3595 | } |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3596 | update_stateid(&stp->st_stid.sc_stateid); |
| 3597 | memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3599 | if (nfsd4_has_session(&resp->cstate)) { |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3600 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 3601 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3602 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 3603 | goto nodeleg; |
| 3604 | } |
| 3605 | } |
| 3606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3607 | /* |
| 3608 | * Attempt to hand out a delegation. No error return, because the |
| 3609 | * OPEN succeeds even if we fail. |
| 3610 | */ |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3611 | nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3612 | nodeleg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | status = nfs_ok; |
| 3614 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 3615 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3616 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3617 | out: |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3618 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 3619 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 3620 | open->op_deleg_want) |
| 3621 | nfsd4_deleg_xgrade_none_ext(open, dp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3622 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3623 | if (fp) |
| 3624 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 3625 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
J. Bruce Fields | 1255a8f | 2012-03-06 14:35:16 -0500 | [diff] [blame] | 3626 | nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | /* |
| 3628 | * To finish the open response, we just need to set the rflags. |
| 3629 | */ |
| 3630 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3631 | if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) && |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3632 | !nfsd4_has_session(&resp->cstate)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3633 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
| 3634 | |
| 3635 | return status; |
| 3636 | } |
| 3637 | |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 3638 | void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status) |
| 3639 | { |
| 3640 | if (open->op_openowner) { |
| 3641 | struct nfs4_openowner *oo = open->op_openowner; |
| 3642 | |
| 3643 | if (!list_empty(&oo->oo_owner.so_stateids)) |
| 3644 | list_del_init(&oo->oo_close_lru); |
| 3645 | if (oo->oo_flags & NFS4_OO_NEW) { |
| 3646 | if (status) { |
| 3647 | release_openowner(oo); |
| 3648 | open->op_openowner = NULL; |
| 3649 | } else |
| 3650 | oo->oo_flags &= ~NFS4_OO_NEW; |
| 3651 | } |
| 3652 | } |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3653 | if (open->op_file) |
| 3654 | nfsd4_free_file(open->op_file); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3655 | if (open->op_stp) |
J. Bruce Fields | ef79859 | 2012-08-29 10:32:54 -0700 | [diff] [blame] | 3656 | free_generic_stateid(open->op_stp); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 3657 | } |
| 3658 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3659 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3660 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3661 | clientid_t *clid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | { |
| 3663 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3664 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 3665 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | |
| 3667 | nfs4_lock_state(); |
| 3668 | dprintk("process_renew(%08x/%08x): starting\n", |
| 3669 | clid->cl_boot, clid->cl_id); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3670 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 3671 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | goto out; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3673 | clp = cstate->clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3674 | status = nfserr_cb_path_down; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 3675 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 3676 | && clp->cl_cb_state != NFSD4_CB_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | goto out; |
| 3678 | status = nfs_ok; |
| 3679 | out: |
| 3680 | nfs4_unlock_state(); |
| 3681 | return status; |
| 3682 | } |
| 3683 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3684 | static void |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 3685 | nfsd4_end_grace(struct nfsd_net *nn) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3686 | { |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 3687 | /* do nothing if grace period already ended */ |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 3688 | if (nn->grace_ended) |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 3689 | return; |
| 3690 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3691 | dprintk("NFSD: end of grace period\n"); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 3692 | nn->grace_ended = true; |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 3693 | nfsd4_record_grace_done(nn, nn->boot_time); |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 3694 | locks_end_grace(&nn->nfsd4_manager); |
J. Bruce Fields | e46b498 | 2010-03-01 19:21:21 -0500 | [diff] [blame] | 3695 | /* |
| 3696 | * Now that every NFSv4 client has had the chance to recover and |
| 3697 | * to see the (possibly new, possibly shorter) lease time, we |
| 3698 | * can safely set the next grace time to the current lease time: |
| 3699 | */ |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 3700 | nn->nfsd4_grace = nn->nfsd4_lease; |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3701 | } |
| 3702 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 3703 | static time_t |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3704 | nfs4_laundromat(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3705 | { |
| 3706 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3707 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3708 | struct nfs4_delegation *dp; |
| 3709 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 3d73371 | 2012-11-27 14:11:44 +0300 | [diff] [blame] | 3710 | time_t cutoff = get_seconds() - nn->nfsd4_lease; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3711 | time_t t, new_timeo = nn->nfsd4_lease; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3712 | |
| 3713 | nfs4_lock_state(); |
| 3714 | |
| 3715 | dprintk("NFSD: laundromat service - starting\n"); |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 3716 | nfsd4_end_grace(nn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3717 | INIT_LIST_HEAD(&reaplist); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3718 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 3719 | list_for_each_safe(pos, next, &nn->client_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
| 3721 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { |
| 3722 | t = clp->cl_time - cutoff; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3723 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | break; |
| 3725 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3726 | if (mark_client_expired_locked(clp)) { |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 3727 | dprintk("NFSD: client in use (clientid %08x)\n", |
| 3728 | clp->cl_clientid.cl_id); |
| 3729 | continue; |
| 3730 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3731 | list_move(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3732 | } |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3733 | spin_unlock(&nn->client_lock); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3734 | list_for_each_safe(pos, next, &reaplist) { |
| 3735 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3736 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 3737 | clp->cl_clientid.cl_id); |
| 3738 | expire_client(clp); |
| 3739 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3740 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 3741 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3742 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Stanislav Kinsbursky | 4e37a7c2 | 2012-11-26 15:22:03 +0300 | [diff] [blame] | 3743 | if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn) |
| 3744 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3746 | t = dp->dl_time - cutoff; |
| 3747 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3748 | break; |
| 3749 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | list_move(&dp->dl_recall_lru, &reaplist); |
| 3751 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3752 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3753 | list_for_each_safe(pos, next, &reaplist) { |
| 3754 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3755 | revoke_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | } |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3757 | list_for_each_safe(pos, next, &nn->close_lru) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3758 | oo = container_of(pos, struct nfs4_openowner, oo_close_lru); |
| 3759 | if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3760 | t = oo->oo_time - cutoff; |
| 3761 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3762 | break; |
| 3763 | } |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3764 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | } |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3766 | new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | nfs4_unlock_state(); |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3768 | return new_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3769 | } |
| 3770 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 3771 | static struct workqueue_struct *laundry_wq; |
| 3772 | static void laundromat_main(struct work_struct *); |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 3773 | |
| 3774 | static void |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3775 | laundromat_main(struct work_struct *laundry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | { |
| 3777 | time_t t; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3778 | struct delayed_work *dwork = container_of(laundry, struct delayed_work, |
| 3779 | work); |
| 3780 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 3781 | laundromat_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3782 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3783 | t = nfs4_laundromat(nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3785 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3786 | } |
| 3787 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3788 | 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] | 3789 | { |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3790 | if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode) |
| 3791 | return nfserr_bad_stateid; |
| 3792 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | } |
| 3794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3796 | access_permit_read(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3797 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3798 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 3799 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 3800 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3804 | access_permit_write(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3805 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3806 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 3807 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3808 | } |
| 3809 | |
| 3810 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3811 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3813 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 3815 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 3816 | if (stp->st_openstp) |
| 3817 | stp = stp->st_openstp; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3818 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | goto out; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3820 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | goto out; |
| 3822 | status = nfs_ok; |
| 3823 | out: |
| 3824 | return status; |
| 3825 | } |
| 3826 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3827 | static inline __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3828 | 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] | 3829 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 3830 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | return nfs_ok; |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3832 | else if (locks_in_grace(net)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3833 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | * conflicting state; so we must wait out the grace period. */ |
| 3835 | return nfserr_grace; |
| 3836 | } else if (flags & WR_STATE) |
| 3837 | return nfs4_share_conflict(current_fh, |
| 3838 | NFS4_SHARE_DENY_WRITE); |
| 3839 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 3840 | return nfs4_share_conflict(current_fh, |
| 3841 | NFS4_SHARE_DENY_READ); |
| 3842 | } |
| 3843 | |
| 3844 | /* |
| 3845 | * Allow READ/WRITE during grace period on recovered state only for files |
| 3846 | * that are not able to provide mandatory locking. |
| 3847 | */ |
| 3848 | static inline int |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3849 | grace_disallows_io(struct net *net, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3850 | { |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3851 | return locks_in_grace(net) && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3852 | } |
| 3853 | |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3854 | /* Returns true iff a is later than b: */ |
| 3855 | static bool stateid_generation_after(stateid_t *a, stateid_t *b) |
| 3856 | { |
Jim Rees | 1a9357f | 2013-05-17 17:33:00 -0400 | [diff] [blame] | 3857 | return (s32)(a->si_generation - b->si_generation) > 0; |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3858 | } |
| 3859 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3860 | 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] | 3861 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3862 | /* |
| 3863 | * When sessions are used the stateid generation number is ignored |
| 3864 | * when it is zero. |
| 3865 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 3866 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3867 | return nfs_ok; |
| 3868 | |
| 3869 | if (in->si_generation == ref->si_generation) |
| 3870 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3871 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3872 | /* 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] | 3873 | if (stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3874 | return nfserr_bad_stateid; |
| 3875 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3876 | * However, we could see a stateid from the past, even from a |
| 3877 | * non-buggy client. For example, if the client sends a lock |
| 3878 | * while some IO is outstanding, the lock may bump si_generation |
| 3879 | * while the IO is still in flight. The client could avoid that |
| 3880 | * situation by waiting for responses on all the IO requests, |
| 3881 | * but better performance may result in retrying IO that |
| 3882 | * receives an old_stateid error if requests are rarely |
| 3883 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3884 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3885 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3886 | } |
| 3887 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 3888 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3889 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3890 | struct nfs4_stid *s; |
| 3891 | struct nfs4_ol_stateid *ols; |
| 3892 | __be32 status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3893 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 3894 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 3895 | return nfserr_bad_stateid; |
| 3896 | /* Client debugging aid. */ |
| 3897 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { |
| 3898 | char addr_str[INET6_ADDRSTRLEN]; |
| 3899 | rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, |
| 3900 | sizeof(addr_str)); |
| 3901 | pr_warn_ratelimited("NFSD: client %s testing state ID " |
| 3902 | "with incorrect client ID\n", addr_str); |
| 3903 | return nfserr_bad_stateid; |
| 3904 | } |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3905 | s = find_stateid(cl, stateid); |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3906 | if (!s) |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 3907 | return nfserr_bad_stateid; |
J. Bruce Fields | 36279ac | 2011-09-26 12:53:00 -0400 | [diff] [blame] | 3908 | status = check_stateid_generation(stateid, &s->sc_stateid, 1); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3909 | if (status) |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3910 | return status; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 3911 | switch (s->sc_type) { |
| 3912 | case NFS4_DELEG_STID: |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3913 | return nfs_ok; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3914 | case NFS4_REVOKED_DELEG_STID: |
| 3915 | return nfserr_deleg_revoked; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 3916 | case NFS4_OPEN_STID: |
| 3917 | case NFS4_LOCK_STID: |
| 3918 | ols = openlockstateid(s); |
| 3919 | if (ols->st_stateowner->so_is_open_owner |
| 3920 | && !(openowner(ols->st_stateowner)->oo_flags |
| 3921 | & NFS4_OO_CONFIRMED)) |
| 3922 | return nfserr_bad_stateid; |
| 3923 | return nfs_ok; |
| 3924 | default: |
| 3925 | printk("unknown stateid type %x\n", s->sc_type); |
| 3926 | case NFS4_CLOSED_STID: |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3927 | return nfserr_bad_stateid; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 3928 | } |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3929 | } |
| 3930 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 3931 | static __be32 |
| 3932 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
| 3933 | stateid_t *stateid, unsigned char typemask, |
| 3934 | struct nfs4_stid **s, struct nfsd_net *nn) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3935 | { |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 3936 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3937 | |
| 3938 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 3939 | return nfserr_bad_stateid; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3940 | status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn); |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 3941 | if (status == nfserr_stale_clientid) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3942 | if (cstate->session) |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 3943 | return nfserr_bad_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3944 | return nfserr_stale_stateid; |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 3945 | } |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 3946 | if (status) |
| 3947 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3948 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3949 | if (!*s) |
| 3950 | return nfserr_bad_stateid; |
| 3951 | return nfs_ok; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3952 | } |
| 3953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3954 | /* |
| 3955 | * Checks for stateid operations |
| 3956 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3957 | __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3958 | nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate, |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 3959 | stateid_t *stateid, int flags, struct file **filpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | { |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 3961 | struct nfs4_stid *s; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3962 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | struct nfs4_delegation *dp = NULL; |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 3964 | struct svc_fh *current_fh = &cstate->current_fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | struct inode *ino = current_fh->fh_dentry->d_inode; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 3966 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 3967 | struct file *file = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3968 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3970 | if (filpp) |
| 3971 | *filpp = NULL; |
| 3972 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3973 | if (grace_disallows_io(net, ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | return nfserr_grace; |
| 3975 | |
| 3976 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3977 | return check_special_stateids(net, current_fh, stateid, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3978 | |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 3979 | nfs4_lock_state(); |
| 3980 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 3981 | status = nfsd4_lookup_stateid(cstate, stateid, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 3982 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 3983 | &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3984 | if (status) |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 3985 | goto out; |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 3986 | status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate)); |
| 3987 | if (status) |
| 3988 | goto out; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3989 | switch (s->sc_type) { |
| 3990 | case NFS4_DELEG_STID: |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 3991 | dp = delegstateid(s); |
J. Bruce Fields | dc9bf70 | 2009-02-21 11:14:43 -0800 | [diff] [blame] | 3992 | status = nfs4_check_delegmode(dp, flags); |
| 3993 | if (status) |
| 3994 | goto out; |
Dan Carpenter | 43b0178 | 2010-10-27 23:19:04 +0200 | [diff] [blame] | 3995 | if (filpp) { |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 3996 | file = dp->dl_file->fi_deleg_file; |
| 3997 | if (!file) { |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 3998 | WARN_ON_ONCE(1); |
| 3999 | status = nfserr_serverfault; |
| 4000 | goto out; |
| 4001 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4002 | get_file(file); |
Dan Carpenter | 43b0178 | 2010-10-27 23:19:04 +0200 | [diff] [blame] | 4003 | } |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4004 | break; |
| 4005 | case NFS4_OPEN_STID: |
| 4006 | case NFS4_LOCK_STID: |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4007 | stp = openlockstateid(s); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4008 | status = nfs4_check_fh(current_fh, stp); |
| 4009 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4011 | if (stp->st_stateowner->so_is_open_owner |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4012 | && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4013 | goto out; |
J. Bruce Fields | a4455be | 2009-02-21 10:40:22 -0800 | [diff] [blame] | 4014 | status = nfs4_check_openmode(stp, flags); |
| 4015 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | goto out; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4017 | if (filpp) { |
| 4018 | if (flags & RD_STATE) |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4019 | file = find_readable_file(stp->st_file); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4020 | else |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4021 | file = find_writeable_file(stp->st_file); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4022 | } |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4023 | break; |
| 4024 | default: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4025 | status = nfserr_bad_stateid; |
| 4026 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4027 | } |
| 4028 | status = nfs_ok; |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4029 | if (file) |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4030 | *filpp = file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | out: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4032 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4033 | return status; |
| 4034 | } |
| 4035 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4036 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4037 | nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4038 | { |
J. Bruce Fields | a1b8ff4 | 2014-05-20 15:55:21 -0400 | [diff] [blame] | 4039 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
| 4040 | |
| 4041 | if (check_for_locks(stp->st_file, lo)) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4042 | return nfserr_locks_held; |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4043 | release_lockowner_if_empty(lo); |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4044 | return nfs_ok; |
| 4045 | } |
| 4046 | |
| 4047 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4048 | * Test if the stateid is valid |
| 4049 | */ |
| 4050 | __be32 |
| 4051 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4052 | struct nfsd4_test_stateid *test_stateid) |
| 4053 | { |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4054 | struct nfsd4_test_stateid_id *stateid; |
| 4055 | struct nfs4_client *cl = cstate->session->se_client; |
| 4056 | |
| 4057 | nfs4_lock_state(); |
| 4058 | 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] | 4059 | stateid->ts_id_status = |
| 4060 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4061 | nfs4_unlock_state(); |
| 4062 | |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4063 | return nfs_ok; |
| 4064 | } |
| 4065 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4066 | __be32 |
| 4067 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4068 | struct nfsd4_free_stateid *free_stateid) |
| 4069 | { |
| 4070 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4071 | struct nfs4_stid *s; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4072 | struct nfs4_delegation *dp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4073 | struct nfs4_client *cl = cstate->session->se_client; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4074 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4075 | |
| 4076 | nfs4_lock_state(); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4077 | s = find_stateid(cl, stateid); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4078 | if (!s) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4079 | goto out; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4080 | switch (s->sc_type) { |
| 4081 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4082 | ret = nfserr_locks_held; |
| 4083 | goto out; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4084 | case NFS4_OPEN_STID: |
| 4085 | case NFS4_LOCK_STID: |
| 4086 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 4087 | if (ret) |
| 4088 | goto out; |
| 4089 | if (s->sc_type == NFS4_LOCK_STID) |
| 4090 | ret = nfsd4_free_lock_stateid(openlockstateid(s)); |
| 4091 | else |
| 4092 | ret = nfserr_locks_held; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4093 | break; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4094 | case NFS4_REVOKED_DELEG_STID: |
| 4095 | dp = delegstateid(s); |
| 4096 | destroy_revoked_delegation(dp); |
| 4097 | ret = nfs_ok; |
| 4098 | break; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4099 | default: |
| 4100 | ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4101 | } |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4102 | out: |
| 4103 | nfs4_unlock_state(); |
| 4104 | return ret; |
| 4105 | } |
| 4106 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4107 | static inline int |
| 4108 | setlkflg (int type) |
| 4109 | { |
| 4110 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 4111 | RD_STATE : WR_STATE; |
| 4112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4114 | 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] | 4115 | { |
| 4116 | struct svc_fh *current_fh = &cstate->current_fh; |
| 4117 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 4118 | __be32 status; |
| 4119 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4120 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 4121 | if (status) |
| 4122 | return status; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4123 | if (stp->st_stid.sc_type == NFS4_CLOSED_STID |
| 4124 | || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4125 | /* |
| 4126 | * "Closed" stateid's exist *only* to return |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4127 | * nfserr_replay_me from the previous step, and |
| 4128 | * revoked delegations are kept only for free_stateid. |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4129 | */ |
| 4130 | return nfserr_bad_stateid; |
| 4131 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); |
| 4132 | if (status) |
| 4133 | return status; |
| 4134 | return nfs4_check_fh(current_fh, stp); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4135 | } |
| 4136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4137 | /* |
| 4138 | * Checks for sequence id mutating operations. |
| 4139 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4140 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4141 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 4142 | stateid_t *stateid, char typemask, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4143 | struct nfs4_ol_stateid **stpp, |
| 4144 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4146 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4147 | struct nfs4_stid *s; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4148 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4150 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
| 4151 | seqid, STATEID_VAL(stateid)); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | *stpp = NULL; |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4154 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4155 | if (status) |
| 4156 | return status; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4157 | stp = openlockstateid(s); |
J. Bruce Fields | 3d74e6a | 2013-03-22 17:44:19 -0400 | [diff] [blame] | 4158 | if (!nfsd4_has_session(cstate)) |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4159 | cstate->replay_owner = stp->st_stateowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4161 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
| 4162 | if (!status) |
| 4163 | *stpp = stp; |
| 4164 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4165 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 4166 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4167 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 4168 | 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] | 4169 | { |
| 4170 | __be32 status; |
| 4171 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4173 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4174 | NFS4_OPEN_STID, stpp, nn); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 4175 | if (status) |
| 4176 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4177 | oo = openowner((*stpp)->st_stateowner); |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4178 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4179 | return nfserr_bad_stateid; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4180 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | } |
| 4182 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4183 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4184 | 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] | 4185 | struct nfsd4_open_confirm *oc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4187 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4188 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4189 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4190 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4192 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 4193 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4195 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 4196 | if (status) |
| 4197 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | |
| 4199 | nfs4_lock_state(); |
| 4200 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4201 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4202 | oc->oc_seqid, &oc->oc_req_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4203 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4204 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4205 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4206 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4207 | status = nfserr_bad_stateid; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4208 | if (oo->oo_flags & NFS4_OO_CONFIRMED) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4209 | goto out; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4210 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4211 | update_stateid(&stp->st_stid.sc_stateid); |
| 4212 | 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] | 4213 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4214 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 4215 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 4216 | nfsd4_client_record_create(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4217 | status = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4218 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4219 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4220 | if (!cstate->replay_owner) |
| 4221 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4222 | return status; |
| 4223 | } |
| 4224 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4225 | 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] | 4226 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4227 | if (!test_access(access, stp)) |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4228 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 4229 | nfs4_file_put_access(stp->st_file, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4230 | clear_access(access, stp); |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4231 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 4232 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4233 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 4234 | { |
| 4235 | switch (to_access) { |
| 4236 | case NFS4_SHARE_ACCESS_READ: |
| 4237 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 4238 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 4239 | break; |
| 4240 | case NFS4_SHARE_ACCESS_WRITE: |
| 4241 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 4242 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 4243 | break; |
| 4244 | case NFS4_SHARE_ACCESS_BOTH: |
| 4245 | break; |
| 4246 | default: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4247 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | } |
| 4249 | } |
| 4250 | |
| 4251 | static void |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4252 | reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | { |
| 4254 | int i; |
| 4255 | for (i = 0; i < 4; i++) { |
| 4256 | if ((i & deny) != i) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4257 | clear_deny(i, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | } |
| 4259 | } |
| 4260 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4261 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4262 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
| 4263 | struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4264 | struct nfsd4_open_downgrade *od) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4266 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4267 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4268 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4269 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4270 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 4271 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | |
J. Bruce Fields | c30e92d | 2011-10-10 17:34:31 -0400 | [diff] [blame] | 4273 | /* We don't yet support WANT bits: */ |
Benny Halevy | 2c8bd7e | 2012-02-16 20:57:09 +0200 | [diff] [blame] | 4274 | if (od->od_deleg_want) |
| 4275 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 4276 | od->od_deleg_want); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | |
| 4278 | nfs4_lock_state(); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4279 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4280 | &od->od_stateid, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4281 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | status = nfserr_inval; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4284 | if (!test_access(od->od_share_access, stp)) { |
| 4285 | dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | stp->st_access_bmap, od->od_share_access); |
| 4287 | goto out; |
| 4288 | } |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4289 | if (!test_deny(od->od_share_deny, stp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4290 | dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n", |
| 4291 | stp->st_deny_bmap, od->od_share_deny); |
| 4292 | goto out; |
| 4293 | } |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4294 | nfs4_stateid_downgrade(stp, od->od_share_access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4296 | reset_union_bmap_deny(od->od_share_deny, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4298 | update_stateid(&stp->st_stid.sc_stateid); |
| 4299 | memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | status = nfs_ok; |
| 4301 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4302 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4303 | if (!cstate->replay_owner) |
| 4304 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | return status; |
| 4306 | } |
| 4307 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4308 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 4309 | { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4310 | struct nfs4_client *clp = s->st_stid.sc_client; |
| 4311 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 4312 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4313 | s->st_stid.sc_type = NFS4_CLOSED_STID; |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4314 | unhash_open_stateid(s); |
| 4315 | |
| 4316 | if (clp->cl_minorversion) { |
| 4317 | free_generic_stateid(s); |
| 4318 | if (list_empty(&oo->oo_owner.so_stateids)) |
| 4319 | release_openowner(oo); |
| 4320 | } else { |
| 4321 | oo->oo_last_closed_stid = s; |
| 4322 | /* |
| 4323 | * In the 4.0 case we need to keep the owners around a |
| 4324 | * little while to handle CLOSE replay. |
| 4325 | */ |
| 4326 | if (list_empty(&oo->oo_owner.so_stateids)) |
| 4327 | move_to_close_lru(oo, clp->net); |
| 4328 | } |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 4329 | } |
| 4330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | /* |
| 4332 | * nfs4_unlock_state() called after encode |
| 4333 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4334 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4335 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4336 | struct nfsd4_close *close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4337 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4338 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4339 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4340 | struct net *net = SVC_NET(rqstp); |
| 4341 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4343 | dprintk("NFSD: nfsd4_close on file %pd\n", |
| 4344 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | |
| 4346 | nfs4_lock_state(); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4347 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 4348 | &close->cl_stateid, |
| 4349 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4350 | &stp, nn); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4351 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4352 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | goto out; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4354 | update_stateid(&stp->st_stid.sc_stateid); |
| 4355 | memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4357 | nfsd4_close_open_stateid(stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4358 | out: |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4359 | if (!cstate->replay_owner) |
| 4360 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 | return status; |
| 4362 | } |
| 4363 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4364 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4365 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4366 | struct nfsd4_delegreturn *dr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4368 | struct nfs4_delegation *dp; |
| 4369 | stateid_t *stateid = &dr->dr_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4370 | struct nfs4_stid *s; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4371 | __be32 status; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4372 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4373 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4374 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4375 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | |
| 4377 | nfs4_lock_state(); |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4378 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4379 | if (status) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4380 | goto out; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4381 | dp = delegstateid(s); |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 4382 | 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] | 4383 | if (status) |
| 4384 | goto out; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4385 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4386 | destroy_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | out: |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4388 | nfs4_unlock_state(); |
| 4389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | return status; |
| 4391 | } |
| 4392 | |
| 4393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4396 | static inline u64 |
| 4397 | end_offset(u64 start, u64 len) |
| 4398 | { |
| 4399 | u64 end; |
| 4400 | |
| 4401 | end = start + len; |
| 4402 | return end >= start ? end: NFS4_MAX_UINT64; |
| 4403 | } |
| 4404 | |
| 4405 | /* last octet in a range */ |
| 4406 | static inline u64 |
| 4407 | last_byte_offset(u64 start, u64 len) |
| 4408 | { |
| 4409 | u64 end; |
| 4410 | |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4411 | WARN_ON_ONCE(!len); |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4412 | end = start + len; |
| 4413 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 4414 | } |
| 4415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4416 | /* |
| 4417 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 4418 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 4419 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 4420 | * locking, this prevents us from being completely protocol-compliant. The |
| 4421 | * real solution to this problem is to start using unsigned file offsets in |
| 4422 | * the VFS, but this is a very deep change! |
| 4423 | */ |
| 4424 | static inline void |
| 4425 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 4426 | { |
| 4427 | if (lock->fl_start < 0) |
| 4428 | lock->fl_start = OFFSET_MAX; |
| 4429 | if (lock->fl_end < 0) |
| 4430 | lock->fl_end = OFFSET_MAX; |
| 4431 | } |
| 4432 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4433 | /* Hack!: For now, we're defining this just so we can use a pointer to it |
| 4434 | * as a unique cookie to identify our (NFSv4's) posix locks. */ |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 4435 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4436 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | |
| 4438 | static inline void |
| 4439 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 4440 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4441 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4443 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4444 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
| 4445 | deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data, |
| 4446 | lo->lo_owner.so_owner.len, GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 4447 | if (!deny->ld_owner.data) |
| 4448 | /* We just don't care that much */ |
| 4449 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4450 | deny->ld_owner.len = lo->lo_owner.so_owner.len; |
| 4451 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4452 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 4453 | nevermind: |
| 4454 | deny->ld_owner.len = 0; |
| 4455 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4456 | deny->ld_clientid.cl_boot = 0; |
| 4457 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | } |
| 4459 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4460 | deny->ld_length = NFS4_MAX_UINT64; |
| 4461 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 4463 | deny->ld_type = NFS4_READ_LT; |
| 4464 | if (fl->fl_type != F_RDLCK) |
| 4465 | deny->ld_type = NFS4_WRITE_LT; |
| 4466 | } |
| 4467 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4468 | static struct nfs4_lockowner * |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4469 | find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner, |
| 4470 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | { |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4472 | unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner); |
| 4473 | struct nfs4_stateowner *so; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4475 | list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) { |
| 4476 | if (so->so_is_open_owner) |
| 4477 | continue; |
| 4478 | if (!same_owner_str(so, owner, clid)) |
| 4479 | continue; |
| 4480 | return lockowner(so); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | } |
| 4482 | return NULL; |
| 4483 | } |
| 4484 | |
| 4485 | /* |
| 4486 | * Alloc a lock owner structure. |
| 4487 | * 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] | 4488 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4489 | * |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 4490 | * strhashval = ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4492 | static struct nfs4_lockowner * |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4493 | 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] | 4494 | struct nfs4_lockowner *lo; |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4495 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4497 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 4498 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | return NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4500 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 4501 | lo->lo_owner.so_is_open_owner = 0; |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 4502 | /* It is the openowner seqid that will be incremented in encode in the |
| 4503 | * case of new lockowners; so increment the lock seqid manually: */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4504 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1; |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4505 | list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4506 | return lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | } |
| 4508 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4509 | static struct nfs4_ol_stateid * |
| 4510 | alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4512 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 4513 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 4515 | stp = nfs4_alloc_stateid(clp); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 4516 | if (stp == NULL) |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 4517 | return NULL; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 4518 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4519 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
| 4520 | stp->st_stateowner = &lo->lo_owner; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4521 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | stp->st_file = fp; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4523 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4524 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4525 | stp->st_openstp = open_stp; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4526 | list_add(&stp->st_locks, &open_stp->st_locks); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4527 | spin_lock(&fp->fi_lock); |
| 4528 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 4529 | spin_unlock(&fp->fi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | return stp; |
| 4531 | } |
| 4532 | |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4533 | static struct nfs4_ol_stateid * |
| 4534 | find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) |
| 4535 | { |
| 4536 | struct nfs4_ol_stateid *lst; |
| 4537 | |
| 4538 | list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { |
| 4539 | if (lst->st_file == fp) |
| 4540 | return lst; |
| 4541 | } |
| 4542 | return NULL; |
| 4543 | } |
| 4544 | |
| 4545 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 4546 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4547 | check_lock_length(u64 offset, u64 length) |
| 4548 | { |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4549 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | LOFF_OVERFLOW(offset, length))); |
| 4551 | } |
| 4552 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4553 | 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] | 4554 | { |
| 4555 | struct nfs4_file *fp = lock_stp->st_file; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4556 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4557 | if (test_access(access, lock_stp)) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4558 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 4559 | __nfs4_file_get_access(fp, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4560 | set_access(access, lock_stp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4561 | } |
| 4562 | |
J. Bruce Fields | 2355c59 | 2012-04-25 16:56:22 -0400 | [diff] [blame] | 4563 | 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] | 4564 | { |
| 4565 | struct nfs4_file *fi = ost->st_file; |
| 4566 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 4567 | struct nfs4_client *cl = oo->oo_owner.so_client; |
| 4568 | struct nfs4_lockowner *lo; |
| 4569 | unsigned int strhashval; |
Stanislav Kinsbursky | 20e9e2b | 2012-11-14 18:21:46 +0300 | [diff] [blame] | 4570 | struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4571 | |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4572 | lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4573 | if (!lo) { |
| 4574 | strhashval = ownerstr_hashval(cl->cl_clientid.cl_id, |
| 4575 | &lock->v.new.owner); |
| 4576 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 4577 | if (lo == NULL) |
| 4578 | return nfserr_jukebox; |
| 4579 | } else { |
| 4580 | /* with an existing lockowner, seqids must be the same */ |
| 4581 | if (!cstate->minorversion && |
| 4582 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4583 | return nfserr_bad_seqid; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4584 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4585 | |
| 4586 | *lst = find_lock_stateid(lo, fi); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4587 | if (*lst == NULL) { |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4588 | *lst = alloc_init_lock_stateid(lo, fi, ost); |
| 4589 | if (*lst == NULL) { |
| 4590 | release_lockowner_if_empty(lo); |
| 4591 | return nfserr_jukebox; |
| 4592 | } |
| 4593 | *new = true; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4594 | } |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4595 | return nfs_ok; |
| 4596 | } |
| 4597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4598 | /* |
| 4599 | * LOCK operation |
| 4600 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4601 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4602 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4603 | struct nfsd4_lock *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4605 | struct nfs4_openowner *open_sop = NULL; |
| 4606 | struct nfs4_lockowner *lock_sop = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4607 | struct nfs4_ol_stateid *lock_stp; |
J. Bruce Fields | 7d94784 | 2010-08-20 18:09:31 -0400 | [diff] [blame] | 4608 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4609 | struct file_lock *file_lock = NULL; |
| 4610 | struct file_lock *conflock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4611 | __be32 status = 0; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4612 | bool new_state = false; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4613 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4614 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4615 | struct net *net = SVC_NET(rqstp); |
| 4616 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4617 | |
| 4618 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 4619 | (long long) lock->lk_offset, |
| 4620 | (long long) lock->lk_length); |
| 4621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4622 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 4623 | return nfserr_inval; |
| 4624 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4625 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 4626 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 4627 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 4628 | return status; |
| 4629 | } |
| 4630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | nfs4_lock_state(); |
| 4632 | |
| 4633 | if (lock->lk_is_new) { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4634 | struct nfs4_ol_stateid *open_stp = NULL; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 4635 | |
| 4636 | if (nfsd4_has_session(cstate)) |
| 4637 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
| 4638 | memcpy(&lock->v.new.clientid, |
| 4639 | &cstate->session->se_client->cl_clientid, |
| 4640 | sizeof(clientid_t)); |
| 4641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | status = nfserr_stale_clientid; |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 4643 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4647 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | lock->lk_new_open_seqid, |
| 4649 | &lock->lk_new_open_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4650 | &open_stp, nn); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 4651 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4653 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4654 | status = nfserr_bad_stateid; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 4655 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4656 | &lock->v.new.clientid)) |
| 4657 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4658 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
| 4659 | &lock_stp, &new_state); |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 4660 | } else |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4661 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4662 | lock->lk_old_lock_seqid, |
| 4663 | &lock->lk_old_lock_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4664 | NFS4_LOCK_STID, &lock_stp, nn); |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 4665 | if (status) |
| 4666 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4667 | lock_sop = lockowner(lock_stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4668 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4669 | lkflg = setlkflg(lock->lk_type); |
| 4670 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 4671 | if (status) |
| 4672 | goto out; |
| 4673 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 4674 | status = nfserr_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4675 | if (locks_in_grace(net) && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 4676 | goto out; |
| 4677 | status = nfserr_no_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4678 | if (!locks_in_grace(net) && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 4679 | goto out; |
| 4680 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4681 | file_lock = locks_alloc_lock(); |
| 4682 | if (!file_lock) { |
| 4683 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 4684 | status = nfserr_jukebox; |
| 4685 | goto out; |
| 4686 | } |
| 4687 | |
| 4688 | locks_init_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | switch (lock->lk_type) { |
| 4690 | case NFS4_READ_LT: |
| 4691 | case NFS4_READW_LT: |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4692 | filp = find_readable_file(lock_stp->st_file); |
| 4693 | if (filp) |
| 4694 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4695 | file_lock->fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 4696 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | case NFS4_WRITE_LT: |
| 4698 | case NFS4_WRITEW_LT: |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4699 | filp = find_writeable_file(lock_stp->st_file); |
| 4700 | if (filp) |
| 4701 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4702 | file_lock->fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 4703 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4704 | default: |
| 4705 | status = nfserr_inval; |
| 4706 | goto out; |
| 4707 | } |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4708 | if (!filp) { |
| 4709 | status = nfserr_openmode; |
| 4710 | goto out; |
| 4711 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4712 | file_lock->fl_owner = (fl_owner_t)lock_sop; |
| 4713 | file_lock->fl_pid = current->tgid; |
| 4714 | file_lock->fl_file = filp; |
| 4715 | file_lock->fl_flags = FL_POSIX; |
| 4716 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 4717 | file_lock->fl_start = lock->lk_offset; |
| 4718 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 4719 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4720 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4721 | conflock = locks_alloc_lock(); |
| 4722 | if (!conflock) { |
| 4723 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 4724 | status = nfserr_jukebox; |
| 4725 | goto out; |
| 4726 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4728 | err = vfs_lock_file(filp, F_SETLK, file_lock, conflock); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4729 | switch (-err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4730 | case 0: /* success! */ |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4731 | update_stateid(&lock_stp->st_stid.sc_stateid); |
| 4732 | memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4733 | sizeof(stateid_t)); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4734 | status = 0; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4735 | break; |
| 4736 | case (EAGAIN): /* conflock holds conflicting lock */ |
| 4737 | status = nfserr_denied; |
| 4738 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4739 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4740 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4741 | case (EDEADLK): |
| 4742 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4743 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 4744 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 4745 | 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] | 4746 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4747 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | out: |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4750 | if (filp) |
| 4751 | fput(filp); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4752 | if (status && new_state) |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4753 | release_lock_stateid(lock_stp); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4754 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4755 | if (!cstate->replay_owner) |
| 4756 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4757 | if (file_lock) |
| 4758 | locks_free_lock(file_lock); |
| 4759 | if (conflock) |
| 4760 | locks_free_lock(conflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | return status; |
| 4762 | } |
| 4763 | |
| 4764 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 4765 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 4766 | * so we do a temporary open here just to get an open file to pass to |
| 4767 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 4768 | * inode operation.) |
| 4769 | */ |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 4770 | 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] | 4771 | { |
| 4772 | struct file *file; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 4773 | __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); |
| 4774 | if (!err) { |
| 4775 | err = nfserrno(vfs_test_lock(file, lock)); |
| 4776 | nfsd_close(file); |
| 4777 | } |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 4778 | return err; |
| 4779 | } |
| 4780 | |
| 4781 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4782 | * LOCKT operation |
| 4783 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4784 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4785 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4786 | struct nfsd4_lockt *lockt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4787 | { |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4788 | struct file_lock *file_lock = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4789 | struct nfs4_lockowner *lo; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4790 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 4791 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4792 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4793 | if (locks_in_grace(SVC_NET(rqstp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | return nfserr_grace; |
| 4795 | |
| 4796 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 4797 | return nfserr_inval; |
| 4798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4799 | nfs4_lock_state(); |
| 4800 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 4801 | if (!nfsd4_has_session(cstate)) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4802 | status = lookup_clientid(&lockt->lt_clientid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 4803 | if (status) |
| 4804 | goto out; |
| 4805 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 4807 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4808 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4810 | file_lock = locks_alloc_lock(); |
| 4811 | if (!file_lock) { |
| 4812 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 4813 | status = nfserr_jukebox; |
| 4814 | goto out; |
| 4815 | } |
| 4816 | locks_init_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4817 | switch (lockt->lt_type) { |
| 4818 | case NFS4_READ_LT: |
| 4819 | case NFS4_READW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4820 | file_lock->fl_type = F_RDLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | break; |
| 4822 | case NFS4_WRITE_LT: |
| 4823 | case NFS4_WRITEW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4824 | file_lock->fl_type = F_WRLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | break; |
| 4826 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 4827 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | status = nfserr_inval; |
| 4829 | goto out; |
| 4830 | } |
| 4831 | |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4832 | lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4833 | if (lo) |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4834 | file_lock->fl_owner = (fl_owner_t)lo; |
| 4835 | file_lock->fl_pid = current->tgid; |
| 4836 | file_lock->fl_flags = FL_POSIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4837 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4838 | file_lock->fl_start = lockt->lt_offset; |
| 4839 | 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] | 4840 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4841 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4842 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4843 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 4844 | if (status) |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 4845 | goto out; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 4846 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4847 | if (file_lock->fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4848 | status = nfserr_denied; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4849 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4850 | } |
| 4851 | out: |
| 4852 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4853 | if (file_lock) |
| 4854 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4855 | return status; |
| 4856 | } |
| 4857 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4858 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4859 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4860 | struct nfsd4_locku *locku) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4862 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4864 | struct file_lock *file_lock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4865 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4866 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4867 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 4868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4869 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 4870 | (long long) locku->lu_offset, |
| 4871 | (long long) locku->lu_length); |
| 4872 | |
| 4873 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 4874 | return nfserr_inval; |
| 4875 | |
| 4876 | nfs4_lock_state(); |
| 4877 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4878 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4879 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 4880 | &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4881 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | goto out; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4883 | filp = find_any_file(stp->st_file); |
| 4884 | if (!filp) { |
| 4885 | status = nfserr_lock_range; |
| 4886 | goto out; |
| 4887 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4888 | file_lock = locks_alloc_lock(); |
| 4889 | if (!file_lock) { |
| 4890 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 4891 | status = nfserr_jukebox; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4892 | goto fput; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4893 | } |
| 4894 | locks_init_lock(file_lock); |
| 4895 | file_lock->fl_type = F_UNLCK; |
J. Bruce Fields | 0a262ff | 2013-06-17 17:29:40 -0400 | [diff] [blame] | 4896 | file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4897 | file_lock->fl_pid = current->tgid; |
| 4898 | file_lock->fl_file = filp; |
| 4899 | file_lock->fl_flags = FL_POSIX; |
| 4900 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 4901 | file_lock->fl_start = locku->lu_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4902 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4903 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 4904 | locku->lu_length); |
| 4905 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4907 | err = vfs_lock_file(filp, F_SETLK, file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4908 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 4909 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | goto out_nfserr; |
| 4911 | } |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4912 | update_stateid(&stp->st_stid.sc_stateid); |
| 4913 | memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4914 | fput: |
| 4915 | fput(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4917 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 71c3bcd | 2011-09-27 21:42:29 -0400 | [diff] [blame] | 4918 | if (!cstate->replay_owner) |
| 4919 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4920 | if (file_lock) |
| 4921 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4922 | return status; |
| 4923 | |
| 4924 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4925 | status = nfserrno(err); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4926 | goto fput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | } |
| 4928 | |
| 4929 | /* |
| 4930 | * returns |
| 4931 | * 1: locks held by lockowner |
| 4932 | * 0: no locks held by lockowner |
| 4933 | */ |
| 4934 | static int |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4935 | check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | { |
| 4937 | struct file_lock **flpp; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4938 | struct inode *inode = filp->fi_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | int status = 0; |
| 4940 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 4941 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) { |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 4943 | if ((*flpp)->fl_owner == (fl_owner_t)lowner) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | status = 1; |
| 4945 | goto out; |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 4946 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | } |
| 4948 | out: |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 4949 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | return status; |
| 4951 | } |
| 4952 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4953 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 4954 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 4955 | struct nfsd4_compound_state *cstate, |
| 4956 | struct nfsd4_release_lockowner *rlockowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | { |
| 4958 | clientid_t *clid = &rlockowner->rl_clientid; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 4959 | struct nfs4_stateowner *sop = NULL, *tmp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4960 | struct nfs4_lockowner *lo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4961 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 4963 | unsigned int hashval = ownerstr_hashval(clid->cl_id, owner); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4964 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 4965 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | |
| 4967 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 4968 | clid->cl_boot, clid->cl_id); |
| 4969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4970 | nfs4_lock_state(); |
| 4971 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4972 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 4973 | if (status) |
| 4974 | goto out; |
| 4975 | |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4976 | status = nfserr_locks_held; |
J. Bruce Fields | 06f1f86 | 2011-11-07 16:58:18 -0500 | [diff] [blame] | 4977 | |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 4978 | /* Find the matching lock stateowner */ |
| 4979 | list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) { |
| 4980 | if (tmp->so_is_open_owner) |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 4981 | continue; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 4982 | if (same_owner_str(tmp, owner, clid)) { |
| 4983 | sop = tmp; |
| 4984 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | } |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4986 | } |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 4987 | |
| 4988 | /* No matching owner found, maybe a replay? Just declare victory... */ |
| 4989 | if (!sop) { |
| 4990 | status = nfs_ok; |
| 4991 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4992 | } |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 4993 | |
| 4994 | lo = lockowner(sop); |
| 4995 | /* see if there are still any locks associated with it */ |
| 4996 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { |
| 4997 | if (check_for_locks(stp->st_file, lo)) |
| 4998 | goto out; |
| 4999 | } |
| 5000 | |
| 5001 | status = nfs_ok; |
| 5002 | release_lockowner(lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5003 | out: |
| 5004 | nfs4_unlock_state(); |
| 5005 | return status; |
| 5006 | } |
| 5007 | |
| 5008 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5009 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5010 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5011 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | } |
| 5013 | |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5014 | bool |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5015 | nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5016 | { |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5017 | struct nfs4_client_reclaim *crp; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5018 | |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5019 | crp = nfsd4_find_reclaim_client(name, nn); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5020 | return (crp && crp->cr_clp); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5021 | } |
| 5022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | /* |
| 5024 | * failure => all reset bets are off, nfserr_no_grace... |
| 5025 | */ |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5026 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5027 | nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | { |
| 5029 | unsigned int strhashval; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5030 | struct nfs4_client_reclaim *crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5032 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
| 5033 | crp = alloc_reclaim(); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5034 | if (crp) { |
| 5035 | strhashval = clientstr_hashval(name); |
| 5036 | INIT_LIST_HEAD(&crp->cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5037 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5038 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5039 | crp->cr_clp = NULL; |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5040 | nn->reclaim_str_hashtbl_size++; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5041 | } |
| 5042 | return crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | } |
| 5044 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 5045 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5046 | 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] | 5047 | { |
| 5048 | list_del(&crp->cr_strhash); |
| 5049 | kfree(crp); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5050 | nn->reclaim_str_hashtbl_size--; |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 5051 | } |
| 5052 | |
| 5053 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5054 | nfs4_release_reclaim(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5055 | { |
| 5056 | struct nfs4_client_reclaim *crp = NULL; |
| 5057 | int i; |
| 5058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5060 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 5061 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5062 | struct nfs4_client_reclaim, cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5063 | nfs4_remove_reclaim_record(crp, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | } |
| 5065 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 5066 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | /* |
| 5070 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 5071 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5072 | nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5073 | { |
| 5074 | unsigned int strhashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5075 | struct nfs4_client_reclaim *crp = NULL; |
| 5076 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5077 | dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5079 | strhashval = clientstr_hashval(recdir); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5080 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5081 | if (same_name(crp->cr_recdir, recdir)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5082 | return crp; |
| 5083 | } |
| 5084 | } |
| 5085 | return NULL; |
| 5086 | } |
| 5087 | |
| 5088 | /* |
| 5089 | * Called from OPEN. Look for clientid in reclaim list. |
| 5090 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5091 | __be32 |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5092 | nfs4_check_open_reclaim(clientid_t *clid, |
| 5093 | struct nfsd4_compound_state *cstate, |
| 5094 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5095 | { |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5096 | __be32 status; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 5097 | |
| 5098 | /* find clientid in conf_id_hashtbl */ |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5099 | status = lookup_clientid(clid, cstate, nn); |
| 5100 | if (status) |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 5101 | return nfserr_reclaim_bad; |
| 5102 | |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5103 | if (nfsd4_client_record_check(cstate->clp)) |
| 5104 | return nfserr_reclaim_bad; |
| 5105 | |
| 5106 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | } |
| 5108 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5109 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
| 5110 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5111 | u64 nfsd_forget_client(struct nfs4_client *clp, u64 max) |
| 5112 | { |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 5113 | if (mark_client_expired(clp)) |
| 5114 | return 0; |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5115 | expire_client(clp); |
| 5116 | return 1; |
| 5117 | } |
| 5118 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5119 | u64 nfsd_print_client(struct nfs4_client *clp, u64 num) |
| 5120 | { |
| 5121 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 5122 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5123 | printk(KERN_INFO "NFS Client: %s\n", buf); |
| 5124 | return 1; |
| 5125 | } |
| 5126 | |
| 5127 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, |
| 5128 | const char *type) |
| 5129 | { |
| 5130 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 5131 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5132 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
| 5133 | } |
| 5134 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5135 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, |
| 5136 | void (*func)(struct nfs4_ol_stateid *)) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5137 | { |
| 5138 | struct nfs4_openowner *oop; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5139 | struct nfs4_ol_stateid *stp, *st_next; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5140 | struct nfs4_ol_stateid *lst, *lst_next; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5141 | u64 count = 0; |
| 5142 | |
| 5143 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5144 | list_for_each_entry_safe(stp, st_next, |
| 5145 | &oop->oo_owner.so_stateids, st_perstateowner) { |
| 5146 | list_for_each_entry_safe(lst, lst_next, |
| 5147 | &stp->st_locks, st_locks) { |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5148 | if (func) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5149 | func(lst); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5150 | if (++count == max) |
| 5151 | return count; |
| 5152 | } |
| 5153 | } |
| 5154 | } |
| 5155 | |
| 5156 | return count; |
| 5157 | } |
| 5158 | |
| 5159 | u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max) |
| 5160 | { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5161 | return nfsd_foreach_client_lock(clp, max, release_lock_stateid); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5162 | } |
| 5163 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5164 | u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max) |
| 5165 | { |
| 5166 | u64 count = nfsd_foreach_client_lock(clp, max, NULL); |
| 5167 | nfsd_print_count(clp, count, "locked files"); |
| 5168 | return count; |
| 5169 | } |
| 5170 | |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 5171 | static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *)) |
| 5172 | { |
| 5173 | struct nfs4_openowner *oop, *next; |
| 5174 | u64 count = 0; |
| 5175 | |
| 5176 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { |
| 5177 | if (func) |
| 5178 | func(oop); |
| 5179 | if (++count == max) |
| 5180 | break; |
| 5181 | } |
| 5182 | |
| 5183 | return count; |
| 5184 | } |
| 5185 | |
| 5186 | u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max) |
| 5187 | { |
| 5188 | return nfsd_foreach_client_open(clp, max, release_openowner); |
| 5189 | } |
| 5190 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5191 | u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max) |
| 5192 | { |
| 5193 | u64 count = nfsd_foreach_client_open(clp, max, NULL); |
| 5194 | nfsd_print_count(clp, count, "open files"); |
| 5195 | return count; |
| 5196 | } |
| 5197 | |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5198 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
| 5199 | struct list_head *victims) |
| 5200 | { |
| 5201 | struct nfs4_delegation *dp, *next; |
| 5202 | u64 count = 0; |
| 5203 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5204 | lockdep_assert_held(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5205 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5206 | if (victims) { |
| 5207 | /* |
| 5208 | * It's not safe to mess with delegations that have a |
| 5209 | * non-zero dl_time. They might have already been broken |
| 5210 | * and could be processed by the laundromat outside of |
| 5211 | * the state_lock. Just leave them be. |
| 5212 | */ |
| 5213 | if (dp->dl_time != 0) |
| 5214 | continue; |
| 5215 | |
| 5216 | /* |
| 5217 | * Increment dl_time to ensure that delegation breaks |
| 5218 | * don't monkey with it now that we are. |
| 5219 | */ |
| 5220 | ++dp->dl_time; |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5221 | list_move(&dp->dl_recall_lru, victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5222 | } |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5223 | if (++count == max) |
| 5224 | break; |
| 5225 | } |
| 5226 | return count; |
| 5227 | } |
| 5228 | |
| 5229 | u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max) |
| 5230 | { |
| 5231 | struct nfs4_delegation *dp, *next; |
| 5232 | LIST_HEAD(victims); |
| 5233 | u64 count; |
| 5234 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5235 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5236 | count = nfsd_find_all_delegations(clp, max, &victims); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5237 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5238 | |
| 5239 | list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5240 | revoke_delegation(dp); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5241 | |
| 5242 | return count; |
| 5243 | } |
| 5244 | |
| 5245 | u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max) |
| 5246 | { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5247 | struct nfs4_delegation *dp; |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5248 | LIST_HEAD(victims); |
| 5249 | u64 count; |
| 5250 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5251 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5252 | count = nfsd_find_all_delegations(clp, max, &victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5253 | while (!list_empty(&victims)) { |
| 5254 | dp = list_first_entry(&victims, struct nfs4_delegation, |
| 5255 | dl_recall_lru); |
| 5256 | list_del_init(&dp->dl_recall_lru); |
| 5257 | dp->dl_time = 0; |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5258 | nfsd_break_one_deleg(dp); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5259 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5260 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5261 | |
| 5262 | return count; |
| 5263 | } |
| 5264 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5265 | u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max) |
| 5266 | { |
| 5267 | u64 count = 0; |
| 5268 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5269 | spin_lock(&state_lock); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5270 | count = nfsd_find_all_delegations(clp, max, NULL); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5271 | spin_unlock(&state_lock); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5272 | |
| 5273 | nfsd_print_count(clp, count, "delegations"); |
| 5274 | return count; |
| 5275 | } |
| 5276 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5277 | 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] | 5278 | { |
| 5279 | struct nfs4_client *clp, *next; |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5280 | u64 count = 0; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5281 | 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] | 5282 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5283 | if (!nfsd_netns_ready(nn)) |
| 5284 | return 0; |
| 5285 | |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5286 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5287 | count += func(clp, max - count); |
| 5288 | if ((max != 0) && (count >= max)) |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5289 | break; |
| 5290 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5291 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5292 | return count; |
| 5293 | } |
| 5294 | |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 5295 | struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) |
| 5296 | { |
| 5297 | struct nfs4_client *clp; |
| 5298 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id); |
| 5299 | |
| 5300 | if (!nfsd_netns_ready(nn)) |
| 5301 | return NULL; |
| 5302 | |
| 5303 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 5304 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) |
| 5305 | return clp; |
| 5306 | } |
| 5307 | return NULL; |
| 5308 | } |
| 5309 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5310 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
| 5311 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 5312 | /* |
| 5313 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 5314 | * client may quite reasonably hang on to a delegation as long as it has |
| 5315 | * the inode cached. This becomes an obvious problem the first time a |
| 5316 | * client's inode cache approaches the size of the server's total memory. |
| 5317 | * |
| 5318 | * For now we avoid this problem by imposing a hard limit on the number |
| 5319 | * of delegations, which varies according to the server's memory size. |
| 5320 | */ |
| 5321 | static void |
| 5322 | set_max_delegations(void) |
| 5323 | { |
| 5324 | /* |
| 5325 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 5326 | * estimates suggest that in the worst case (where every delegation |
| 5327 | * is for a different inode), a delegation could take about 1.5K, |
| 5328 | * giving a worst case usage of about 6% of memory. |
| 5329 | */ |
| 5330 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 5331 | } |
| 5332 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5333 | static int nfs4_state_create_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5334 | { |
| 5335 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 5336 | int i; |
| 5337 | |
| 5338 | nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5339 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 5340 | if (!nn->conf_id_hashtbl) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5341 | goto err; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5342 | nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5343 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 5344 | if (!nn->unconf_id_hashtbl) |
| 5345 | goto err_unconf_id; |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5346 | nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5347 | OWNER_HASH_SIZE, GFP_KERNEL); |
| 5348 | if (!nn->ownerstr_hashtbl) |
| 5349 | goto err_ownerstr; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5350 | nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5351 | SESSION_HASH_SIZE, GFP_KERNEL); |
| 5352 | if (!nn->sessionid_hashtbl) |
| 5353 | goto err_sessionid; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5354 | |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5355 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5356 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5357 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5358 | } |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5359 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 5360 | INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5361 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 5362 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5363 | nn->conf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5364 | nn->unconf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5365 | INIT_LIST_HEAD(&nn->client_lru); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 5366 | INIT_LIST_HEAD(&nn->close_lru); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5367 | INIT_LIST_HEAD(&nn->del_recall_lru); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5368 | spin_lock_init(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5369 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5370 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5371 | get_net(net); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5372 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5373 | return 0; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5374 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5375 | err_sessionid: |
Stanislav Kinsbursky | 20e9e2b | 2012-11-14 18:21:46 +0300 | [diff] [blame] | 5376 | kfree(nn->ownerstr_hashtbl); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5377 | err_ownerstr: |
| 5378 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5379 | err_unconf_id: |
| 5380 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5381 | err: |
| 5382 | return -ENOMEM; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5383 | } |
| 5384 | |
| 5385 | static void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5386 | nfs4_state_destroy_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5387 | { |
| 5388 | int i; |
| 5389 | struct nfs4_client *clp = NULL; |
| 5390 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 5391 | |
| 5392 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 5393 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 5394 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 5395 | destroy_client(clp); |
| 5396 | } |
| 5397 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5398 | |
Kinglong Mee | 2b90563 | 2014-03-26 22:09:30 +0800 | [diff] [blame] | 5399 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 5400 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 5401 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 5402 | destroy_client(clp); |
| 5403 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5404 | } |
| 5405 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5406 | kfree(nn->sessionid_hashtbl); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5407 | kfree(nn->ownerstr_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5408 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5409 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5410 | put_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5411 | } |
| 5412 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 5413 | int |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5414 | nfs4_state_start_net(struct net *net) |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 5415 | { |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 5416 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5417 | int ret; |
| 5418 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5419 | ret = nfs4_state_create_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5420 | if (ret) |
| 5421 | return ret; |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 5422 | nfsd4_client_tracking_init(net); |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 5423 | nn->boot_time = get_seconds(); |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5424 | locks_start_grace(net, &nn->nfsd4_manager); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5425 | nn->grace_ended = false; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5426 | printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n", |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 5427 | nn->nfsd4_grace, net); |
| 5428 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5429 | return 0; |
| 5430 | } |
| 5431 | |
| 5432 | /* initialization to perform when the nfsd service is started: */ |
| 5433 | |
| 5434 | int |
| 5435 | nfs4_state_start(void) |
| 5436 | { |
| 5437 | int ret; |
| 5438 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5439 | ret = set_callback_cred(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5440 | if (ret) |
| 5441 | return -ENOMEM; |
NeilBrown | 58da282 | 2005-06-23 22:03:19 -0700 | [diff] [blame] | 5442 | laundry_wq = create_singlethread_workqueue("nfsd4"); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 5443 | if (laundry_wq == NULL) { |
| 5444 | ret = -ENOMEM; |
| 5445 | goto out_recovery; |
| 5446 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5447 | ret = nfsd4_create_callback_queue(); |
| 5448 | if (ret) |
| 5449 | goto out_free_laundry; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5450 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 5451 | set_max_delegations(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5452 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5453 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5454 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5455 | out_free_laundry: |
| 5456 | destroy_workqueue(laundry_wq); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 5457 | out_recovery: |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5458 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | } |
| 5460 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 5461 | void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5462 | nfs4_state_shutdown_net(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5463 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5466 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5468 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 5469 | locks_end_grace(&nn->nfsd4_manager); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 5470 | |
J. Bruce Fields | e50a26d | 2013-10-30 10:33:09 -0400 | [diff] [blame] | 5471 | nfs4_lock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5473 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5474 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 5476 | list_move(&dp->dl_recall_lru, &reaplist); |
| 5477 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5478 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | list_for_each_safe(pos, next, &reaplist) { |
| 5480 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5481 | destroy_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5482 | } |
| 5483 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5484 | nfsd4_client_tracking_exit(net); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5485 | nfs4_state_destroy_net(net); |
J. Bruce Fields | e50a26d | 2013-10-30 10:33:09 -0400 | [diff] [blame] | 5486 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | } |
| 5488 | |
| 5489 | void |
| 5490 | nfs4_state_shutdown(void) |
| 5491 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 5492 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 5493 | nfsd4_destroy_callback_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | } |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5495 | |
| 5496 | static void |
| 5497 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 5498 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 5499 | if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid)) |
| 5500 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | static void |
| 5504 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 5505 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 5506 | if (cstate->minorversion) { |
| 5507 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
| 5508 | SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
| 5509 | } |
| 5510 | } |
| 5511 | |
| 5512 | void |
| 5513 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 5514 | { |
| 5515 | CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5516 | } |
| 5517 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5518 | /* |
| 5519 | * functions to set current state id |
| 5520 | */ |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5521 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 5522 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 5523 | { |
| 5524 | put_stateid(cstate, &odp->od_stateid); |
| 5525 | } |
| 5526 | |
| 5527 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5528 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open) |
| 5529 | { |
| 5530 | put_stateid(cstate, &open->op_stateid); |
| 5531 | } |
| 5532 | |
| 5533 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5534 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 5535 | { |
| 5536 | put_stateid(cstate, &close->cl_stateid); |
| 5537 | } |
| 5538 | |
| 5539 | void |
| 5540 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock) |
| 5541 | { |
| 5542 | put_stateid(cstate, &lock->lk_resp_stateid); |
| 5543 | } |
| 5544 | |
| 5545 | /* |
| 5546 | * functions to consume current state id |
| 5547 | */ |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 5548 | |
| 5549 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 5550 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 5551 | { |
| 5552 | get_stateid(cstate, &odp->od_stateid); |
| 5553 | } |
| 5554 | |
| 5555 | void |
| 5556 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp) |
| 5557 | { |
| 5558 | get_stateid(cstate, &drp->dr_stateid); |
| 5559 | } |
| 5560 | |
| 5561 | void |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 5562 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp) |
| 5563 | { |
| 5564 | get_stateid(cstate, &fsp->fr_stateid); |
| 5565 | } |
| 5566 | |
| 5567 | void |
| 5568 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr) |
| 5569 | { |
| 5570 | get_stateid(cstate, &setattr->sa_stateid); |
| 5571 | } |
| 5572 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5573 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5574 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 5575 | { |
| 5576 | get_stateid(cstate, &close->cl_stateid); |
| 5577 | } |
| 5578 | |
| 5579 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5580 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5581 | { |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5582 | get_stateid(cstate, &locku->lu_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5583 | } |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 5584 | |
| 5585 | void |
| 5586 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read) |
| 5587 | { |
| 5588 | get_stateid(cstate, &read->rd_stateid); |
| 5589 | } |
| 5590 | |
| 5591 | void |
| 5592 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write) |
| 5593 | { |
| 5594 | get_stateid(cstate, &write->wr_stateid); |
| 5595 | } |