Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (c) 2001 The Regents of the University of Michigan. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Kendrick Smith <kmsmith@umich.edu> |
| 6 | * Andy Adamson <kandros@umich.edu> |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the name of the University nor the names of its |
| 18 | * contributors may be used to endorse or promote products derived |
| 19 | * from this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | */ |
| 34 | |
Dave Hansen | aceaf78 | 2008-02-15 14:37:31 -0800 | [diff] [blame] | 35 | #include <linux/file.h> |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 36 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 38 | #include <linux/namei.h> |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 39 | #include <linux/swap.h> |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 40 | #include <linux/pagemap.h> |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 41 | #include <linux/ratelimit.h> |
Olga Kornievskaia | 68e76ad | 2008-12-23 16:17:15 -0500 | [diff] [blame] | 42 | #include <linux/sunrpc/svcauth_gss.h> |
Jeff Layton | 5976687 | 2013-02-04 12:50:00 -0500 | [diff] [blame] | 43 | #include <linux/sunrpc/addr.h> |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 44 | #include <linux/hash.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 45 | #include "xdr4.h" |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 46 | #include "xdr4cb.h" |
J. Bruce Fields | 0a3adad | 2009-11-04 18:12:35 -0500 | [diff] [blame] | 47 | #include "vfs.h" |
J. Bruce Fields | bfa4b36 | 2012-04-25 16:49:18 -0400 | [diff] [blame] | 48 | #include "current_stateid.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 50 | #include "netns.h" |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 53 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 54 | #define all_ones {{~0,~0},~0} |
| 55 | static const stateid_t one_stateid = { |
| 56 | .si_generation = ~0, |
| 57 | .si_opaque = all_ones, |
| 58 | }; |
| 59 | static const stateid_t zero_stateid = { |
| 60 | /* all fields zero */ |
| 61 | }; |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 62 | static const stateid_t currentstateid = { |
| 63 | .si_generation = 1, |
| 64 | }; |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 65 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 66 | static u64 current_sessionid = 1; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 67 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 68 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
| 69 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 70 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* forward declarations */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 73 | static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 74 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 76 | /* Locking: */ |
| 77 | |
| 78 | /* Currently used for almost all code touching nfsv4 state: */ |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 79 | static DEFINE_MUTEX(client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 81 | /* |
| 82 | * Currently used for the del_recall_lru and file hash table. In an |
| 83 | * effort to decrease the scope of the client_mutex, this spinlock may |
| 84 | * eventually cover more: |
| 85 | */ |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 86 | static DEFINE_SPINLOCK(state_lock); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 87 | |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 88 | static struct kmem_cache *openowner_slab; |
| 89 | static struct kmem_cache *lockowner_slab; |
| 90 | static struct kmem_cache *file_slab; |
| 91 | static struct kmem_cache *stateid_slab; |
| 92 | static struct kmem_cache *deleg_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | void |
| 95 | nfs4_lock_state(void) |
| 96 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 97 | mutex_lock(&client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 100 | static void free_session(struct nfsd4_session *); |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 101 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 102 | static bool is_session_dead(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 103 | { |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 104 | return ses->se_flags & NFS4_SESSION_DEAD; |
| 105 | } |
| 106 | |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 107 | static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) |
| 108 | { |
| 109 | if (atomic_read(&ses->se_ref) > ref_held_by_me) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 110 | return nfserr_jukebox; |
| 111 | ses->se_flags |= NFS4_SESSION_DEAD; |
| 112 | return nfs_ok; |
| 113 | } |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | void |
| 116 | nfs4_unlock_state(void) |
| 117 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 118 | mutex_unlock(&client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 121 | static bool is_client_expired(struct nfs4_client *clp) |
| 122 | { |
| 123 | return clp->cl_time == 0; |
| 124 | } |
| 125 | |
| 126 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
| 127 | { |
| 128 | if (atomic_read(&clp->cl_refcount)) |
| 129 | return nfserr_jukebox; |
| 130 | clp->cl_time = 0; |
| 131 | return nfs_ok; |
| 132 | } |
| 133 | |
| 134 | static __be32 mark_client_expired(struct nfs4_client *clp) |
| 135 | { |
| 136 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 137 | __be32 ret; |
| 138 | |
| 139 | spin_lock(&nn->client_lock); |
| 140 | ret = mark_client_expired_locked(clp); |
| 141 | spin_unlock(&nn->client_lock); |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static __be32 get_client_locked(struct nfs4_client *clp) |
| 146 | { |
| 147 | if (is_client_expired(clp)) |
| 148 | return nfserr_expired; |
| 149 | atomic_inc(&clp->cl_refcount); |
| 150 | return nfs_ok; |
| 151 | } |
| 152 | |
| 153 | /* must be called under the client_lock */ |
| 154 | static inline void |
| 155 | renew_client_locked(struct nfs4_client *clp) |
| 156 | { |
| 157 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 158 | |
| 159 | if (is_client_expired(clp)) { |
| 160 | WARN_ON(1); |
| 161 | printk("%s: client (clientid %08x/%08x) already expired\n", |
| 162 | __func__, |
| 163 | clp->cl_clientid.cl_boot, |
| 164 | clp->cl_clientid.cl_id); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | dprintk("renewing client (clientid %08x/%08x)\n", |
| 169 | clp->cl_clientid.cl_boot, |
| 170 | clp->cl_clientid.cl_id); |
| 171 | list_move_tail(&clp->cl_lru, &nn->client_lru); |
| 172 | clp->cl_time = get_seconds(); |
| 173 | } |
| 174 | |
| 175 | static inline void |
| 176 | renew_client(struct nfs4_client *clp) |
| 177 | { |
| 178 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 179 | |
| 180 | spin_lock(&nn->client_lock); |
| 181 | renew_client_locked(clp); |
| 182 | spin_unlock(&nn->client_lock); |
| 183 | } |
| 184 | |
Fengguang Wu | ba13843 | 2013-04-16 22:14:15 -0400 | [diff] [blame] | 185 | static void put_client_renew_locked(struct nfs4_client *clp) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 186 | { |
| 187 | if (!atomic_dec_and_test(&clp->cl_refcount)) |
| 188 | return; |
| 189 | if (!is_client_expired(clp)) |
| 190 | renew_client_locked(clp); |
| 191 | } |
| 192 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 193 | static void put_client_renew(struct nfs4_client *clp) |
| 194 | { |
| 195 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 196 | |
Jeff Layton | d6c249b | 2014-07-08 14:02:50 -0400 | [diff] [blame] | 197 | if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock)) |
| 198 | return; |
| 199 | if (!is_client_expired(clp)) |
| 200 | renew_client_locked(clp); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 201 | spin_unlock(&nn->client_lock); |
| 202 | } |
| 203 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 204 | static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) |
| 205 | { |
| 206 | __be32 status; |
| 207 | |
| 208 | if (is_session_dead(ses)) |
| 209 | return nfserr_badsession; |
| 210 | status = get_client_locked(ses->se_client); |
| 211 | if (status) |
| 212 | return status; |
| 213 | atomic_inc(&ses->se_ref); |
| 214 | return nfs_ok; |
| 215 | } |
| 216 | |
| 217 | static void nfsd4_put_session_locked(struct nfsd4_session *ses) |
| 218 | { |
| 219 | struct nfs4_client *clp = ses->se_client; |
| 220 | |
| 221 | if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) |
| 222 | free_session(ses); |
| 223 | put_client_renew_locked(clp); |
| 224 | } |
| 225 | |
| 226 | static void nfsd4_put_session(struct nfsd4_session *ses) |
| 227 | { |
| 228 | struct nfs4_client *clp = ses->se_client; |
| 229 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 230 | |
| 231 | spin_lock(&nn->client_lock); |
| 232 | nfsd4_put_session_locked(ses); |
| 233 | spin_unlock(&nn->client_lock); |
| 234 | } |
| 235 | |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | static inline u32 |
| 238 | opaque_hashval(const void *ptr, int nbytes) |
| 239 | { |
| 240 | unsigned char *cptr = (unsigned char *) ptr; |
| 241 | |
| 242 | u32 x = 0; |
| 243 | while (nbytes--) { |
| 244 | x *= 37; |
| 245 | x += *cptr++; |
| 246 | } |
| 247 | return x; |
| 248 | } |
| 249 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 250 | static void nfsd4_free_file(struct nfs4_file *f) |
| 251 | { |
| 252 | kmem_cache_free(file_slab, f); |
| 253 | } |
| 254 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 255 | static inline void |
| 256 | put_nfs4_file(struct nfs4_file *fi) |
| 257 | { |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 258 | might_lock(&state_lock); |
| 259 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 260 | if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) { |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 261 | hlist_del(&fi->fi_hash); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 262 | spin_unlock(&state_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 263 | nfsd4_free_file(fi); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 264 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static inline void |
| 268 | get_nfs4_file(struct nfs4_file *fi) |
| 269 | { |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 270 | atomic_inc(&fi->fi_ref); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 273 | static struct file * |
| 274 | __nfs4_get_fd(struct nfs4_file *f, int oflag) |
| 275 | { |
| 276 | if (f->fi_fds[oflag]) |
| 277 | return get_file(f->fi_fds[oflag]); |
| 278 | return NULL; |
| 279 | } |
| 280 | |
| 281 | static struct file * |
| 282 | find_writeable_file_locked(struct nfs4_file *f) |
| 283 | { |
| 284 | struct file *ret; |
| 285 | |
| 286 | lockdep_assert_held(&f->fi_lock); |
| 287 | |
| 288 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 289 | if (!ret) |
| 290 | ret = __nfs4_get_fd(f, O_RDWR); |
| 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | static struct file * |
| 295 | find_writeable_file(struct nfs4_file *f) |
| 296 | { |
| 297 | struct file *ret; |
| 298 | |
| 299 | spin_lock(&f->fi_lock); |
| 300 | ret = find_writeable_file_locked(f); |
| 301 | spin_unlock(&f->fi_lock); |
| 302 | |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | static struct file *find_readable_file_locked(struct nfs4_file *f) |
| 307 | { |
| 308 | struct file *ret; |
| 309 | |
| 310 | lockdep_assert_held(&f->fi_lock); |
| 311 | |
| 312 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 313 | if (!ret) |
| 314 | ret = __nfs4_get_fd(f, O_RDWR); |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | static struct file * |
| 319 | find_readable_file(struct nfs4_file *f) |
| 320 | { |
| 321 | struct file *ret; |
| 322 | |
| 323 | spin_lock(&f->fi_lock); |
| 324 | ret = find_readable_file_locked(f); |
| 325 | spin_unlock(&f->fi_lock); |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | static struct file * |
| 331 | find_any_file(struct nfs4_file *f) |
| 332 | { |
| 333 | struct file *ret; |
| 334 | |
| 335 | spin_lock(&f->fi_lock); |
| 336 | ret = __nfs4_get_fd(f, O_RDWR); |
| 337 | if (!ret) { |
| 338 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 339 | if (!ret) |
| 340 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 341 | } |
| 342 | spin_unlock(&f->fi_lock); |
| 343 | return ret; |
| 344 | } |
| 345 | |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 346 | static atomic_long_t num_delegations; |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 347 | unsigned long max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 348 | |
| 349 | /* |
| 350 | * Open owner state (share locks) |
| 351 | */ |
| 352 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 353 | /* hash tables for lock and open owners */ |
| 354 | #define OWNER_HASH_BITS 8 |
| 355 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) |
| 356 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 357 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 358 | static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 359 | { |
| 360 | unsigned int ret; |
| 361 | |
| 362 | ret = opaque_hashval(ownername->data, ownername->len); |
| 363 | ret += clientid; |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 364 | return ret & OWNER_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 365 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 366 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 367 | /* hash table for nfs4_file */ |
| 368 | #define FILE_HASH_BITS 8 |
| 369 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
Shan Wei | 3507958 | 2011-01-14 17:35:59 +0800 | [diff] [blame] | 370 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 371 | static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 372 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 373 | return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0); |
| 374 | } |
| 375 | |
| 376 | static unsigned int file_hashval(struct knfsd_fh *fh) |
| 377 | { |
| 378 | return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1); |
| 379 | } |
| 380 | |
| 381 | static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2) |
| 382 | { |
| 383 | return fh1->fh_size == fh2->fh_size && |
| 384 | !memcmp(fh1->fh_base.fh_pad, |
| 385 | fh2->fh_base.fh_pad, |
| 386 | fh1->fh_size); |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 387 | } |
| 388 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 389 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 390 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 391 | static void |
| 392 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 393 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 394 | lockdep_assert_held(&fp->fi_lock); |
| 395 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 396 | if (access & NFS4_SHARE_ACCESS_WRITE) |
| 397 | atomic_inc(&fp->fi_access[O_WRONLY]); |
| 398 | if (access & NFS4_SHARE_ACCESS_READ) |
| 399 | atomic_inc(&fp->fi_access[O_RDONLY]); |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 400 | } |
| 401 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 402 | static __be32 |
| 403 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 404 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 405 | lockdep_assert_held(&fp->fi_lock); |
| 406 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 407 | /* Does this access mode make sense? */ |
| 408 | if (access & ~NFS4_SHARE_ACCESS_BOTH) |
| 409 | return nfserr_inval; |
| 410 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 411 | /* Does it conflict with a deny mode already set? */ |
| 412 | if ((access & fp->fi_share_deny) != 0) |
| 413 | return nfserr_share_denied; |
| 414 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 415 | __nfs4_file_get_access(fp, access); |
| 416 | return nfs_ok; |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 417 | } |
| 418 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 419 | static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) |
| 420 | { |
| 421 | /* Common case is that there is no deny mode. */ |
| 422 | if (deny) { |
| 423 | /* Does this deny mode make sense? */ |
| 424 | if (deny & ~NFS4_SHARE_DENY_BOTH) |
| 425 | return nfserr_inval; |
| 426 | |
| 427 | if ((deny & NFS4_SHARE_DENY_READ) && |
| 428 | atomic_read(&fp->fi_access[O_RDONLY])) |
| 429 | return nfserr_share_denied; |
| 430 | |
| 431 | if ((deny & NFS4_SHARE_DENY_WRITE) && |
| 432 | atomic_read(&fp->fi_access[O_WRONLY])) |
| 433 | return nfserr_share_denied; |
| 434 | } |
| 435 | return nfs_ok; |
| 436 | } |
| 437 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 438 | 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] | 439 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 440 | might_lock(&fp->fi_lock); |
| 441 | |
| 442 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { |
| 443 | struct file *f1 = NULL; |
| 444 | struct file *f2 = NULL; |
| 445 | |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 446 | swap(f1, fp->fi_fds[oflag]); |
J. Bruce Fields | 0c7c3e6 | 2013-03-28 20:37:14 -0400 | [diff] [blame] | 447 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 448 | swap(f2, fp->fi_fds[O_RDWR]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 449 | spin_unlock(&fp->fi_lock); |
| 450 | if (f1) |
| 451 | fput(f1); |
| 452 | if (f2) |
| 453 | fput(f2); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 457 | 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] | 458 | { |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 459 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
| 460 | |
| 461 | if (access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 462 | __nfs4_file_put_access(fp, O_WRONLY); |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 463 | if (access & NFS4_SHARE_ACCESS_READ) |
| 464 | __nfs4_file_put_access(fp, O_RDONLY); |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 465 | } |
| 466 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 467 | static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, |
| 468 | struct kmem_cache *slab) |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 469 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 470 | struct nfs4_stid *stid; |
| 471 | int new_id; |
| 472 | |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 473 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 474 | if (!stid) |
| 475 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 476 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 477 | idr_preload(GFP_KERNEL); |
| 478 | spin_lock(&cl->cl_lock); |
| 479 | new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT); |
| 480 | spin_unlock(&cl->cl_lock); |
| 481 | idr_preload_end(); |
Tejun Heo | ebd6c70 | 2013-03-13 14:59:37 -0700 | [diff] [blame] | 482 | if (new_id < 0) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 483 | goto out_free; |
| 484 | stid->sc_client = cl; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 485 | stid->sc_stateid.si_opaque.so_id = new_id; |
| 486 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; |
| 487 | /* Will be incremented before return to client: */ |
Trond Myklebust | 72c0b0f | 2014-07-21 09:34:58 -0400 | [diff] [blame] | 488 | atomic_set(&stid->sc_count, 1); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 489 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 490 | /* |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 491 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 492 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 493 | * example, a stray write retransmission could be accepted by |
| 494 | * the server when it should have been rejected. Therefore, |
| 495 | * adopt a trick from the sctp code to attempt to maximize the |
| 496 | * amount of time until an id is reused, by ensuring they always |
| 497 | * "increase" (mod INT_MAX): |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 498 | */ |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 499 | return stid; |
| 500 | out_free: |
Wei Yongjun | 2c44a23 | 2013-04-09 14:15:31 +0800 | [diff] [blame] | 501 | kmem_cache_free(slab, stid); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 502 | return NULL; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 503 | } |
| 504 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame^] | 505 | static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp) |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 506 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 507 | struct nfs4_stid *stid; |
| 508 | struct nfs4_ol_stateid *stp; |
| 509 | |
| 510 | stid = nfs4_alloc_stid(clp, stateid_slab); |
| 511 | if (!stid) |
| 512 | return NULL; |
| 513 | |
| 514 | stp = openlockstateid(stid); |
| 515 | stp->st_stid.sc_free = nfs4_free_ol_stateid; |
| 516 | return stp; |
| 517 | } |
| 518 | |
| 519 | static void nfs4_free_deleg(struct nfs4_stid *stid) |
| 520 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 521 | kmem_cache_free(deleg_slab, stid); |
| 522 | atomic_long_dec(&num_delegations); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 523 | } |
| 524 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 525 | /* |
| 526 | * When we recall a delegation, we should be careful not to hand it |
| 527 | * out again straight away. |
| 528 | * To ensure this we keep a pair of bloom filters ('new' and 'old') |
| 529 | * in which the filehandles of recalled delegations are "stored". |
| 530 | * If a filehandle appear in either filter, a delegation is blocked. |
| 531 | * When a delegation is recalled, the filehandle is stored in the "new" |
| 532 | * filter. |
| 533 | * Every 30 seconds we swap the filters and clear the "new" one, |
| 534 | * unless both are empty of course. |
| 535 | * |
| 536 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the |
| 537 | * low 3 bytes as hash-table indices. |
| 538 | * |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 539 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 540 | * is used to manage concurrent access. Testing does not need the lock |
| 541 | * except when swapping the two filters. |
| 542 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 543 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 544 | static struct bloom_pair { |
| 545 | int entries, old_entries; |
| 546 | time_t swap_time; |
| 547 | int new; /* index into 'set' */ |
| 548 | DECLARE_BITMAP(set[2], 256); |
| 549 | } blocked_delegations; |
| 550 | |
| 551 | static int delegation_blocked(struct knfsd_fh *fh) |
| 552 | { |
| 553 | u32 hash; |
| 554 | struct bloom_pair *bd = &blocked_delegations; |
| 555 | |
| 556 | if (bd->entries == 0) |
| 557 | return 0; |
| 558 | if (seconds_since_boot() - bd->swap_time > 30) { |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 559 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 560 | if (seconds_since_boot() - bd->swap_time > 30) { |
| 561 | bd->entries -= bd->old_entries; |
| 562 | bd->old_entries = bd->entries; |
| 563 | memset(bd->set[bd->new], 0, |
| 564 | sizeof(bd->set[0])); |
| 565 | bd->new = 1-bd->new; |
| 566 | bd->swap_time = seconds_since_boot(); |
| 567 | } |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 568 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 569 | } |
| 570 | hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0); |
| 571 | if (test_bit(hash&255, bd->set[0]) && |
| 572 | test_bit((hash>>8)&255, bd->set[0]) && |
| 573 | test_bit((hash>>16)&255, bd->set[0])) |
| 574 | return 1; |
| 575 | |
| 576 | if (test_bit(hash&255, bd->set[1]) && |
| 577 | test_bit((hash>>8)&255, bd->set[1]) && |
| 578 | test_bit((hash>>16)&255, bd->set[1])) |
| 579 | return 1; |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | static void block_delegations(struct knfsd_fh *fh) |
| 585 | { |
| 586 | u32 hash; |
| 587 | struct bloom_pair *bd = &blocked_delegations; |
| 588 | |
| 589 | hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0); |
| 590 | |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 591 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 592 | __set_bit(hash&255, bd->set[bd->new]); |
| 593 | __set_bit((hash>>8)&255, bd->set[bd->new]); |
| 594 | __set_bit((hash>>16)&255, bd->set[bd->new]); |
| 595 | if (bd->entries == 0) |
| 596 | bd->swap_time = seconds_since_boot(); |
| 597 | bd->entries += 1; |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 598 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 599 | } |
| 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | static struct nfs4_delegation * |
Jeff Layton | f9416e2 | 2014-07-25 07:34:23 -0400 | [diff] [blame] | 602 | alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | { |
| 604 | struct nfs4_delegation *dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 605 | long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | dprintk("NFSD alloc_init_deleg\n"); |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 608 | n = atomic_long_inc_return(&num_delegations); |
| 609 | if (n < 0 || n > max_delegations) |
| 610 | goto out_dec; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 611 | if (delegation_blocked(¤t_fh->fh_handle)) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 612 | goto out_dec; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 613 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab)); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 614 | if (dp == NULL) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 615 | goto out_dec; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 616 | |
| 617 | dp->dl_stid.sc_free = nfs4_free_deleg; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 618 | /* |
| 619 | * delegation seqid's are never incremented. The 4.1 special |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 620 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 621 | * 0 anyway just for consistency and use 1: |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 622 | */ |
| 623 | dp->dl_stid.sc_stateid.si_generation = 1; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 624 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 625 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 627 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 628 | INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | return dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 630 | out_dec: |
| 631 | atomic_long_dec(&num_delegations); |
| 632 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | void |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 636 | nfs4_put_stid(struct nfs4_stid *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 638 | struct nfs4_file *fp = s->sc_file; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 639 | struct nfs4_client *clp = s->sc_client; |
| 640 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 641 | might_lock(&clp->cl_lock); |
| 642 | |
| 643 | if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 644 | return; |
| 645 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 646 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 647 | s->sc_free(s); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 648 | if (fp) |
| 649 | put_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
| 651 | |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 652 | static void nfs4_put_deleg_lease(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 654 | lockdep_assert_held(&state_lock); |
| 655 | |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 656 | if (!fp->fi_lease) |
| 657 | return; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 658 | if (atomic_dec_and_test(&fp->fi_delegees)) { |
| 659 | vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease); |
| 660 | fp->fi_lease = NULL; |
J. Bruce Fields | 4ee6362 | 2011-04-15 18:08:26 -0400 | [diff] [blame] | 661 | fput(fp->fi_deleg_file); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 662 | fp->fi_deleg_file = NULL; |
| 663 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 666 | static void unhash_stid(struct nfs4_stid *s) |
| 667 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 668 | s->sc_type = 0; |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 669 | } |
| 670 | |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 671 | static void |
| 672 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 673 | { |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 674 | lockdep_assert_held(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 675 | lockdep_assert_held(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 676 | |
Benny Halevy | 3fb87d1 | 2014-05-30 09:09:31 -0400 | [diff] [blame] | 677 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 678 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
| 679 | list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations); |
| 680 | } |
| 681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | static void |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 683 | unhash_delegation_locked(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 685 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 686 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 687 | lockdep_assert_held(&state_lock); |
| 688 | |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 689 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
Jeff Layton | d55a166 | 2014-07-22 13:52:06 -0400 | [diff] [blame] | 690 | /* Ensure that deleg break won't try to requeue it */ |
| 691 | ++dp->dl_time; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 692 | spin_lock(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 693 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | list_del_init(&dp->dl_recall_lru); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 695 | list_del_init(&dp->dl_perfile); |
| 696 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 697 | if (fp) |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 698 | nfs4_put_deleg_lease(fp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 699 | } |
| 700 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 701 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 702 | { |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 703 | spin_lock(&state_lock); |
| 704 | unhash_delegation_locked(dp); |
| 705 | spin_unlock(&state_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 706 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 710 | { |
| 711 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 712 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 713 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
| 714 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 715 | if (clp->cl_minorversion == 0) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 716 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 717 | else { |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 718 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 719 | spin_lock(&clp->cl_lock); |
| 720 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 721 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | /* |
| 726 | * SETCLIENTID state |
| 727 | */ |
| 728 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 729 | static unsigned int clientid_hashval(u32 id) |
| 730 | { |
| 731 | return id & CLIENT_HASH_MASK; |
| 732 | } |
| 733 | |
| 734 | static unsigned int clientstr_hashval(const char *name) |
| 735 | { |
| 736 | return opaque_hashval(name, 8) & CLIENT_HASH_MASK; |
| 737 | } |
| 738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | /* |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 740 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 741 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 742 | * only what share bits are currently in force, but also what |
| 743 | * combinations of share bits previous opens have used. This allows us |
| 744 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 745 | * return an error if the client attempt to downgrade to a combination |
| 746 | * of share bits not explicable by closing some of its previous opens. |
| 747 | * |
| 748 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 749 | * track of access/deny bit combinations; so, e.g., we allow: |
| 750 | * |
| 751 | * OPEN allow read, deny write |
| 752 | * OPEN allow both, deny none |
| 753 | * DOWNGRADE allow read, deny none |
| 754 | * |
| 755 | * which we should reject. |
| 756 | */ |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 757 | static unsigned int |
| 758 | bmap_to_share_mode(unsigned long bmap) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 759 | int i; |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 760 | unsigned int access = 0; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 761 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 762 | for (i = 1; i < 4; i++) { |
| 763 | if (test_bit(i, &bmap)) |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 764 | access |= i; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 765 | } |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 766 | return access; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 767 | } |
| 768 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 769 | /* set share access for a given stateid */ |
| 770 | static inline void |
| 771 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 772 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 773 | unsigned char mask = 1 << access; |
| 774 | |
| 775 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 776 | stp->st_access_bmap |= mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /* clear share access for a given stateid */ |
| 780 | static inline void |
| 781 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 782 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 783 | unsigned char mask = 1 << access; |
| 784 | |
| 785 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 786 | stp->st_access_bmap &= ~mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /* test whether a given stateid has access */ |
| 790 | static inline bool |
| 791 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 792 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 793 | unsigned char mask = 1 << access; |
| 794 | |
| 795 | return (bool)(stp->st_access_bmap & mask); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 796 | } |
| 797 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 798 | /* set share deny for a given stateid */ |
| 799 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 800 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 801 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 802 | unsigned char mask = 1 << deny; |
| 803 | |
| 804 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 805 | stp->st_deny_bmap |= mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | /* clear share deny for a given stateid */ |
| 809 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 810 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 811 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 812 | unsigned char mask = 1 << deny; |
| 813 | |
| 814 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 815 | stp->st_deny_bmap &= ~mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | /* test whether a given stateid is denying specific access */ |
| 819 | static inline bool |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 820 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 821 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 822 | unsigned char mask = 1 << deny; |
| 823 | |
| 824 | return (bool)(stp->st_deny_bmap & mask); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | static int nfs4_access_to_omode(u32 access) |
| 828 | { |
J. Bruce Fields | 8f34a43 | 2010-09-02 15:23:16 -0400 | [diff] [blame] | 829 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 830 | case NFS4_SHARE_ACCESS_READ: |
| 831 | return O_RDONLY; |
| 832 | case NFS4_SHARE_ACCESS_WRITE: |
| 833 | return O_WRONLY; |
| 834 | case NFS4_SHARE_ACCESS_BOTH: |
| 835 | return O_RDWR; |
| 836 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 837 | WARN_ON_ONCE(1); |
| 838 | return O_RDONLY; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 839 | } |
| 840 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 841 | /* |
| 842 | * A stateid that had a deny mode associated with it is being released |
| 843 | * or downgraded. Recalculate the deny mode on the file. |
| 844 | */ |
| 845 | static void |
| 846 | recalculate_deny_mode(struct nfs4_file *fp) |
| 847 | { |
| 848 | struct nfs4_ol_stateid *stp; |
| 849 | |
| 850 | spin_lock(&fp->fi_lock); |
| 851 | fp->fi_share_deny = 0; |
| 852 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) |
| 853 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); |
| 854 | spin_unlock(&fp->fi_lock); |
| 855 | } |
| 856 | |
| 857 | static void |
| 858 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 859 | { |
| 860 | int i; |
| 861 | bool change = false; |
| 862 | |
| 863 | for (i = 1; i < 4; i++) { |
| 864 | if ((i & deny) != i) { |
| 865 | change = true; |
| 866 | clear_deny(i, stp); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | /* Recalculate per-file deny mode if there was a change */ |
| 871 | if (change) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 872 | recalculate_deny_mode(stp->st_stid.sc_file); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 873 | } |
| 874 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 875 | /* release all access and file references for a given stateid */ |
| 876 | static void |
| 877 | release_all_access(struct nfs4_ol_stateid *stp) |
| 878 | { |
| 879 | int i; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 880 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 881 | |
| 882 | if (fp && stp->st_deny_bmap != 0) |
| 883 | recalculate_deny_mode(fp); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 884 | |
| 885 | for (i = 1; i < 4; i++) { |
| 886 | if (test_access(i, stp)) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 887 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 888 | clear_access(i, stp); |
| 889 | } |
| 890 | } |
| 891 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 892 | static void unhash_generic_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 893 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 894 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 895 | |
| 896 | spin_lock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 897 | list_del(&stp->st_perfile); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 898 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 899 | list_del(&stp->st_perstateowner); |
| 900 | } |
| 901 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 902 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 903 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 904 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 905 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 906 | release_all_access(stp); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 907 | kmem_cache_free(stateid_slab, stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 908 | } |
| 909 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame^] | 910 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 911 | { |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame^] | 912 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
| 913 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 914 | struct file *file; |
| 915 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame^] | 916 | file = find_any_file(stp->st_stid.sc_file); |
| 917 | if (file) |
| 918 | filp_close(file, (fl_owner_t)lo); |
| 919 | nfs4_free_ol_stateid(stid); |
| 920 | } |
| 921 | |
| 922 | static void __release_lock_stateid(struct nfs4_ol_stateid *stp) |
| 923 | { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 924 | list_del(&stp->st_locks); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 925 | unhash_generic_stateid(stp); |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 926 | unhash_stid(&stp->st_stid); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 927 | nfs4_put_stid(&stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 928 | } |
| 929 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 930 | static void unhash_lockowner(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 931 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 932 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 933 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 934 | list_del(&lo->lo_owner.so_strhash); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 935 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 936 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 937 | struct nfs4_ol_stateid, st_perstateowner); |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 938 | __release_lock_stateid(stp); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
Trond Myklebust | 50cc623 | 2014-04-18 14:44:03 -0400 | [diff] [blame] | 942 | static void nfs4_free_lockowner(struct nfs4_lockowner *lo) |
| 943 | { |
| 944 | kfree(lo->lo_owner.so_owner.data); |
| 945 | kmem_cache_free(lockowner_slab, lo); |
| 946 | } |
| 947 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 948 | static void release_lockowner(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 949 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 950 | unhash_lockowner(lo); |
| 951 | nfs4_free_lockowner(lo); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 952 | } |
| 953 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 954 | static void release_lockowner_if_empty(struct nfs4_lockowner *lo) |
| 955 | { |
| 956 | if (list_empty(&lo->lo_owner.so_stateids)) |
| 957 | release_lockowner(lo); |
| 958 | } |
| 959 | |
| 960 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 961 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 962 | struct nfs4_lockowner *lo; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 963 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 964 | lo = lockowner(stp->st_stateowner); |
| 965 | __release_lock_stateid(stp); |
| 966 | release_lockowner_if_empty(lo); |
| 967 | } |
| 968 | |
| 969 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp) |
| 970 | { |
| 971 | struct nfs4_ol_stateid *stp; |
| 972 | |
| 973 | while (!list_empty(&open_stp->st_locks)) { |
| 974 | stp = list_entry(open_stp->st_locks.next, |
| 975 | struct nfs4_ol_stateid, st_locks); |
| 976 | release_lock_stateid(stp); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 980 | static void unhash_open_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 981 | { |
| 982 | unhash_generic_stateid(stp); |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 983 | release_open_stateid_locks(stp); |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 987 | { |
| 988 | unhash_open_stateid(stp); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 989 | nfs4_put_stid(&stp->st_stid); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 990 | } |
| 991 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 992 | static void unhash_openowner(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 993 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 994 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 995 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 996 | list_del(&oo->oo_owner.so_strhash); |
| 997 | list_del(&oo->oo_perclient); |
| 998 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 999 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 1000 | struct nfs4_ol_stateid, st_perstateowner); |
J. Bruce Fields | f044ff8 | 2009-01-11 15:24:04 -0500 | [diff] [blame] | 1001 | release_open_stateid(stp); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1005 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 1006 | { |
| 1007 | struct nfs4_ol_stateid *s = oo->oo_last_closed_stid; |
| 1008 | |
| 1009 | if (s) { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1010 | nfs4_put_stid(&s->st_stid); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1011 | oo->oo_last_closed_stid = NULL; |
| 1012 | } |
| 1013 | } |
| 1014 | |
Trond Myklebust | 50cc623 | 2014-04-18 14:44:03 -0400 | [diff] [blame] | 1015 | static void nfs4_free_openowner(struct nfs4_openowner *oo) |
| 1016 | { |
| 1017 | kfree(oo->oo_owner.so_owner.data); |
| 1018 | kmem_cache_free(openowner_slab, oo); |
| 1019 | } |
| 1020 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1021 | static void release_openowner(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1022 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1023 | unhash_openowner(oo); |
| 1024 | list_del(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1025 | release_last_closed_stateid(oo); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1026 | nfs4_free_openowner(oo); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1027 | } |
| 1028 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1029 | static inline int |
| 1030 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 1031 | { |
| 1032 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 1033 | |
| 1034 | return sid->sequence % SESSION_HASH_SIZE; |
| 1035 | } |
| 1036 | |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1037 | #ifdef NFSD_DEBUG |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1038 | static inline void |
| 1039 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1040 | { |
| 1041 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 1042 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 1043 | } |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1044 | #else |
| 1045 | static inline void |
| 1046 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1047 | { |
| 1048 | } |
| 1049 | #endif |
| 1050 | |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1051 | /* |
| 1052 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 1053 | * won't be used for replay. |
| 1054 | */ |
| 1055 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 1056 | { |
| 1057 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 1058 | |
| 1059 | if (nfserr == nfserr_replay_me) |
| 1060 | return; |
| 1061 | |
| 1062 | if (!seqid_mutating_err(ntohl(nfserr))) { |
| 1063 | cstate->replay_owner = NULL; |
| 1064 | return; |
| 1065 | } |
| 1066 | if (!so) |
| 1067 | return; |
| 1068 | if (so->so_is_open_owner) |
| 1069 | release_last_closed_stateid(openowner(so)); |
| 1070 | so->so_seqid++; |
| 1071 | return; |
| 1072 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1073 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1074 | static void |
| 1075 | gen_sessionid(struct nfsd4_session *ses) |
| 1076 | { |
| 1077 | struct nfs4_client *clp = ses->se_client; |
| 1078 | struct nfsd4_sessionid *sid; |
| 1079 | |
| 1080 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 1081 | sid->clientid = clp->cl_clientid; |
| 1082 | sid->sequence = current_sessionid++; |
| 1083 | sid->reserved = 0; |
| 1084 | } |
| 1085 | |
| 1086 | /* |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1087 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 1088 | * the rpc header, but all we need to cache is the data starting after |
| 1089 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 1090 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 1091 | * value that is the number of bytes in our cache plus a few additional |
| 1092 | * bytes. In order to stay on the safe side, and not promise more than |
| 1093 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 1094 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 1095 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 1096 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1097 | */ |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1098 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 1099 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1100 | static void |
| 1101 | free_session_slots(struct nfsd4_session *ses) |
| 1102 | { |
| 1103 | int i; |
| 1104 | |
| 1105 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) |
| 1106 | kfree(ses->se_slots[i]); |
| 1107 | } |
| 1108 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1109 | /* |
| 1110 | * We don't actually need to cache the rpc and session headers, so we |
| 1111 | * can allocate a little less for each slot: |
| 1112 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1113 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1114 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1115 | u32 size; |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1116 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1117 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 1118 | size = 0; |
| 1119 | else |
| 1120 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 1121 | return size + sizeof(struct nfsd4_slot); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1122 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1123 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1124 | /* |
| 1125 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 1126 | * re-negotiate active sessions and reduce their slot usage to make |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 1127 | * 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] | 1128 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1129 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1130 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1131 | u32 slotsize = slot_bytes(ca); |
| 1132 | u32 num = ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1133 | int avail; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1134 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1135 | spin_lock(&nfsd_drc_lock); |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 1136 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, |
| 1137 | nfsd_drc_max_mem - nfsd_drc_mem_used); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1138 | num = min_t(int, num, avail / slotsize); |
| 1139 | nfsd_drc_mem_used += num * slotsize; |
| 1140 | spin_unlock(&nfsd_drc_lock); |
| 1141 | |
| 1142 | return num; |
| 1143 | } |
| 1144 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1145 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1146 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1147 | int slotsize = slot_bytes(ca); |
| 1148 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1149 | spin_lock(&nfsd_drc_lock); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1150 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1151 | spin_unlock(&nfsd_drc_lock); |
| 1152 | } |
| 1153 | |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1154 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 1155 | struct nfsd4_channel_attrs *battrs) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1156 | { |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1157 | int numslots = fattrs->maxreqs; |
| 1158 | int slotsize = slot_bytes(fattrs); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1159 | struct nfsd4_session *new; |
| 1160 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1161 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 1162 | 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] | 1163 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 1164 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1165 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1166 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1167 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1168 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1169 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1170 | for (i = 0; i < numslots; i++) { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1171 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1172 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1173 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1174 | } |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1175 | |
| 1176 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 1177 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 1178 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1179 | return new; |
| 1180 | out_free: |
| 1181 | while (i--) |
| 1182 | kfree(new->se_slots[i]); |
| 1183 | kfree(new); |
| 1184 | return NULL; |
| 1185 | } |
| 1186 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1187 | static void free_conn(struct nfsd4_conn *c) |
| 1188 | { |
| 1189 | svc_xprt_put(c->cn_xprt); |
| 1190 | kfree(c); |
| 1191 | } |
| 1192 | |
| 1193 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 1194 | { |
| 1195 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 1196 | struct nfs4_client *clp = c->cn_session->se_client; |
| 1197 | |
| 1198 | spin_lock(&clp->cl_lock); |
| 1199 | if (!list_empty(&c->cn_persession)) { |
| 1200 | list_del(&c->cn_persession); |
| 1201 | free_conn(c); |
| 1202 | } |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 1203 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 2e4b723 | 2013-03-08 09:30:43 -0500 | [diff] [blame] | 1204 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1205 | } |
| 1206 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1207 | 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] | 1208 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1209 | struct nfsd4_conn *conn; |
| 1210 | |
| 1211 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 1212 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1213 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1214 | svc_xprt_get(rqstp->rq_xprt); |
| 1215 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1216 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1217 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1218 | return conn; |
| 1219 | } |
| 1220 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1221 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1222 | { |
| 1223 | conn->cn_session = ses; |
| 1224 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1225 | } |
| 1226 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1227 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1228 | { |
| 1229 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1230 | |
| 1231 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1232 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1233 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1234 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1235 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1236 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1237 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1238 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1239 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1240 | } |
| 1241 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1242 | 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] | 1243 | { |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1244 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1245 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1246 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1247 | ret = nfsd4_register_conn(conn); |
| 1248 | if (ret) |
| 1249 | /* oops; xprt is already down: */ |
| 1250 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | 57a3714 | 2014-07-18 15:06:47 -0400 | [diff] [blame] | 1251 | /* We may have gained or lost a callback channel: */ |
| 1252 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1253 | } |
| 1254 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1255 | 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] | 1256 | { |
| 1257 | u32 dir = NFS4_CDFC4_FORE; |
| 1258 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1259 | if (cses->flags & SESSION4_BACK_CHAN) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1260 | dir |= NFS4_CDFC4_BACK; |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1261 | return alloc_conn(rqstp, dir); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1265 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1266 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1267 | struct nfs4_client *clp = s->se_client; |
| 1268 | struct nfsd4_conn *c; |
| 1269 | |
| 1270 | spin_lock(&clp->cl_lock); |
| 1271 | while (!list_empty(&s->se_conns)) { |
| 1272 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1273 | list_del_init(&c->cn_persession); |
| 1274 | spin_unlock(&clp->cl_lock); |
| 1275 | |
| 1276 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1277 | free_conn(c); |
| 1278 | |
| 1279 | spin_lock(&clp->cl_lock); |
| 1280 | } |
| 1281 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1282 | } |
| 1283 | |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1284 | static void __free_session(struct nfsd4_session *ses) |
| 1285 | { |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1286 | free_session_slots(ses); |
| 1287 | kfree(ses); |
| 1288 | } |
| 1289 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1290 | static void free_session(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 1291 | { |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1292 | struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id); |
| 1293 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1294 | lockdep_assert_held(&nn->client_lock); |
| 1295 | nfsd4_del_conns(ses); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1296 | nfsd4_put_drc_mem(&ses->se_fchannel); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1297 | __free_session(ses); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1298 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1299 | |
Fengguang Wu | 135ae82 | 2012-11-10 07:20:25 -0500 | [diff] [blame] | 1300 | 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] | 1301 | { |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1302 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1303 | 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] | 1304 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1305 | new->se_client = clp; |
| 1306 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1307 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1308 | INIT_LIST_HEAD(&new->se_conns); |
| 1309 | |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1310 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1311 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 1312 | new->se_cb_prog = cses->callback_prog; |
J. Bruce Fields | c6bb3ca | 2012-11-01 16:31:02 -0400 | [diff] [blame] | 1313 | new->se_cb_sec = cses->cb_sec; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1314 | atomic_set(&new->se_ref, 0); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1315 | idx = hash_sessionid(&new->se_sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1316 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1317 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1318 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1319 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1320 | spin_unlock(&clp->cl_lock); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1321 | spin_unlock(&nn->client_lock); |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1322 | |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1323 | if (cses->flags & SESSION4_BACK_CHAN) { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1324 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1325 | /* |
| 1326 | * This is a little silly; with sessions there's no real |
| 1327 | * use for the callback address. Use the peer address |
| 1328 | * as a reasonable default for now, but consider fixing |
| 1329 | * the rpc client not to require an address in the |
| 1330 | * future: |
| 1331 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1332 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1333 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1334 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1335 | } |
| 1336 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1337 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1338 | static struct nfsd4_session * |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1339 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1340 | { |
| 1341 | struct nfsd4_session *elem; |
| 1342 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1343 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1344 | |
| 1345 | dump_sessionid(__func__, sessionid); |
| 1346 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1347 | /* Search in the appropriate list */ |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1348 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1349 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1350 | NFS4_MAX_SESSIONID_LEN)) { |
| 1351 | return elem; |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | dprintk("%s: session not found\n", __func__); |
| 1356 | return NULL; |
| 1357 | } |
| 1358 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1359 | static struct nfsd4_session * |
| 1360 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, |
| 1361 | __be32 *ret) |
| 1362 | { |
| 1363 | struct nfsd4_session *session; |
| 1364 | __be32 status = nfserr_badsession; |
| 1365 | |
| 1366 | session = __find_in_sessionid_hashtbl(sessionid, net); |
| 1367 | if (!session) |
| 1368 | goto out; |
| 1369 | status = nfsd4_get_session_locked(session); |
| 1370 | if (status) |
| 1371 | session = NULL; |
| 1372 | out: |
| 1373 | *ret = status; |
| 1374 | return session; |
| 1375 | } |
| 1376 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1377 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1378 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1379 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1380 | { |
| 1381 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1382 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1383 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1384 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1385 | } |
| 1386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1388 | static int |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1389 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | { |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1391 | if (clid->cl_boot == nn->boot_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | return 0; |
Andy Adamson | 60adfc5 | 2009-04-03 08:28:50 +0300 | [diff] [blame] | 1393 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1394 | clid->cl_boot, clid->cl_id, nn->boot_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | return 1; |
| 1396 | } |
| 1397 | |
| 1398 | /* |
| 1399 | * XXX Should we use a slab cache ? |
| 1400 | * This type of memory management is somewhat inefficient, but we use it |
| 1401 | * anyway since SETCLIENTID is not a common operation. |
| 1402 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1403 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | { |
| 1405 | struct nfs4_client *clp; |
| 1406 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1407 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
| 1408 | if (clp == NULL) |
| 1409 | return NULL; |
Thomas Meyer | 67114fe | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 1410 | clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL); |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1411 | if (clp->cl_name.data == NULL) { |
| 1412 | kfree(clp); |
| 1413 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | } |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1415 | clp->cl_name.len = name.len; |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1416 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1417 | idr_init(&clp->cl_stateids); |
| 1418 | atomic_set(&clp->cl_refcount, 0); |
| 1419 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 1420 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1421 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1422 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 1423 | INIT_LIST_HEAD(&clp->cl_lru); |
| 1424 | INIT_LIST_HEAD(&clp->cl_callbacks); |
| 1425 | INIT_LIST_HEAD(&clp->cl_revoked); |
| 1426 | spin_lock_init(&clp->cl_lock); |
| 1427 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | return clp; |
| 1429 | } |
| 1430 | |
Trond Myklebust | 4dd86e15 | 2014-04-18 14:43:58 -0400 | [diff] [blame] | 1431 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | free_client(struct nfs4_client *clp) |
| 1433 | { |
Stanislav Kinsbursky | bca0ec6 | 2013-01-09 12:38:34 +0300 | [diff] [blame] | 1434 | 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] | 1435 | |
| 1436 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1437 | while (!list_empty(&clp->cl_sessions)) { |
| 1438 | struct nfsd4_session *ses; |
| 1439 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 1440 | se_perclnt); |
| 1441 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1442 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 1443 | free_session(ses); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1444 | } |
Trond Myklebust | 4cb57e3 | 2014-04-18 14:43:57 -0400 | [diff] [blame] | 1445 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1446 | free_svc_cred(&clp->cl_cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | kfree(clp->cl_name.data); |
majianpeng | 2d32b29 | 2013-01-29 13:16:06 +0800 | [diff] [blame] | 1448 | idr_destroy(&clp->cl_stateids); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | kfree(clp); |
| 1450 | } |
| 1451 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1452 | /* must be called under the client_lock */ |
| 1453 | static inline void |
| 1454 | unhash_client_locked(struct nfs4_client *clp) |
| 1455 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1456 | struct nfsd4_session *ses; |
| 1457 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1458 | list_del(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1459 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1460 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 1461 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1462 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1463 | } |
| 1464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | static void |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1466 | destroy_client(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1468 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | struct list_head reaplist; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1471 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1474 | spin_lock(&state_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1475 | while (!list_empty(&clp->cl_delegations)) { |
| 1476 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1477 | unhash_delegation_locked(dp); |
| 1478 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1480 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | while (!list_empty(&reaplist)) { |
| 1482 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1483 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1484 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | } |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1486 | while (!list_empty(&clp->cl_revoked)) { |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 1487 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1488 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1489 | nfs4_put_stid(&dp->dl_stid); |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 1490 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1491 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1492 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
| 1493 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | } |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 1495 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 1496 | if (clp->cl_cb_conn.cb_xprt) |
| 1497 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1498 | list_del(&clp->cl_idhash); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1499 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1500 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1501 | else |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1502 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1503 | spin_lock(&nn->client_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1504 | unhash_client_locked(clp); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 1505 | WARN_ON_ONCE(atomic_read(&clp->cl_refcount)); |
| 1506 | free_client(clp); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1507 | spin_unlock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1510 | static void expire_client(struct nfs4_client *clp) |
| 1511 | { |
| 1512 | nfsd4_client_record_remove(clp); |
| 1513 | destroy_client(clp); |
| 1514 | } |
| 1515 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1516 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 1517 | { |
| 1518 | memcpy(target->cl_verifier.data, source->data, |
| 1519 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1522 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 1523 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 1525 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 1526 | } |
| 1527 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1528 | 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] | 1529 | { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1530 | if (source->cr_principal) { |
| 1531 | target->cr_principal = |
| 1532 | kstrdup(source->cr_principal, GFP_KERNEL); |
| 1533 | if (target->cr_principal == NULL) |
| 1534 | return -ENOMEM; |
| 1535 | } else |
| 1536 | target->cr_principal = NULL; |
J. Bruce Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 1537 | target->cr_flavor = source->cr_flavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | target->cr_uid = source->cr_uid; |
| 1539 | target->cr_gid = source->cr_gid; |
| 1540 | target->cr_group_info = source->cr_group_info; |
| 1541 | get_group_info(target->cr_group_info); |
J. Bruce Fields | 0dc1531 | 2013-05-14 16:07:13 -0400 | [diff] [blame] | 1542 | target->cr_gss_mech = source->cr_gss_mech; |
| 1543 | if (source->cr_gss_mech) |
| 1544 | gss_mech_get(source->cr_gss_mech); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1545 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1548 | static long long |
| 1549 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 1550 | { |
| 1551 | long long res; |
| 1552 | |
| 1553 | res = o1->len - o2->len; |
| 1554 | if (res) |
| 1555 | return res; |
| 1556 | return (long long)memcmp(o1->data, o2->data, o1->len); |
| 1557 | } |
| 1558 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1559 | static int same_name(const char *n1, const char *n2) |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1560 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1561 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1565 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 1566 | { |
| 1567 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1571 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 1572 | { |
| 1573 | 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] | 1574 | } |
| 1575 | |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1576 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 1577 | { |
| 1578 | int i; |
| 1579 | |
| 1580 | if (g1->ngroups != g2->ngroups) |
| 1581 | return false; |
| 1582 | for (i=0; i<g1->ngroups; i++) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1583 | if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i))) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1584 | return false; |
| 1585 | return true; |
| 1586 | } |
| 1587 | |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1588 | /* |
| 1589 | * RFC 3530 language requires clid_inuse be returned when the |
| 1590 | * "principal" associated with a requests differs from that previously |
| 1591 | * used. We use uid, gid's, and gss principal string as our best |
| 1592 | * approximation. We also don't want to allow non-gss use of a client |
| 1593 | * established using gss: in theory cr_principal should catch that |
| 1594 | * change, but in practice cr_principal can be null even in the gss case |
| 1595 | * since gssd doesn't always pass down a principal string. |
| 1596 | */ |
| 1597 | static bool is_gss_cred(struct svc_cred *cr) |
| 1598 | { |
| 1599 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 1600 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 1601 | } |
| 1602 | |
| 1603 | |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1604 | static bool |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1605 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 1606 | { |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1607 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1608 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 1609 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1610 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 1611 | return false; |
| 1612 | if (cr1->cr_principal == cr2->cr_principal) |
| 1613 | return true; |
| 1614 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 1615 | return false; |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1616 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1619 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 1620 | { |
| 1621 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1622 | u32 service; |
| 1623 | |
J. Bruce Fields | c472059 | 2013-08-07 11:41:49 -0400 | [diff] [blame] | 1624 | if (!cr->cr_gss_mech) |
| 1625 | return false; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1626 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 1627 | return service == RPC_GSS_SVC_INTEGRITY || |
| 1628 | service == RPC_GSS_SVC_PRIVACY; |
| 1629 | } |
| 1630 | |
| 1631 | static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) |
| 1632 | { |
| 1633 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1634 | |
| 1635 | if (!cl->cl_mach_cred) |
| 1636 | return true; |
| 1637 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 1638 | return false; |
| 1639 | if (!svc_rqst_integrity_protected(rqstp)) |
| 1640 | return false; |
| 1641 | if (!cr->cr_principal) |
| 1642 | return false; |
| 1643 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 1644 | } |
| 1645 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1646 | 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] | 1647 | { |
| 1648 | static u32 current_clientid = 1; |
| 1649 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1650 | clp->cl_clientid.cl_boot = nn->boot_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | clp->cl_clientid.cl_id = current_clientid++; |
| 1652 | } |
| 1653 | |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 1654 | static void gen_confirm(struct nfs4_client *clp) |
| 1655 | { |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1656 | __be32 verf[2]; |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 1657 | static u32 i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | |
Jeff Layton | f419992 | 2014-06-17 07:44:11 -0400 | [diff] [blame] | 1659 | /* |
| 1660 | * This is opaque to client, so no need to byte-swap. Use |
| 1661 | * __force to keep sparse happy |
| 1662 | */ |
| 1663 | verf[0] = (__force __be32)get_seconds(); |
| 1664 | verf[1] = (__force __be32)i++; |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1665 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1668 | static struct nfs4_stid * |
| 1669 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1670 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 1671 | struct nfs4_stid *ret; |
| 1672 | |
| 1673 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 1674 | if (!ret || !ret->sc_type) |
| 1675 | return NULL; |
| 1676 | return ret; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 1677 | } |
| 1678 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1679 | static struct nfs4_stid * |
| 1680 | find_stateid(struct nfs4_client *cl, stateid_t *t) |
| 1681 | { |
| 1682 | struct nfs4_stid *ret; |
| 1683 | |
| 1684 | spin_lock(&cl->cl_lock); |
| 1685 | ret = find_stateid_locked(cl, t); |
| 1686 | spin_unlock(&cl->cl_lock); |
| 1687 | return ret; |
| 1688 | } |
| 1689 | |
| 1690 | static struct nfs4_stid * |
| 1691 | 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] | 1692 | { |
| 1693 | struct nfs4_stid *s; |
| 1694 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1695 | spin_lock(&cl->cl_lock); |
| 1696 | s = find_stateid_locked(cl, t); |
| 1697 | if (s != NULL && !(typemask & s->sc_type)) |
| 1698 | s = NULL; |
| 1699 | spin_unlock(&cl->cl_lock); |
| 1700 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1701 | } |
| 1702 | |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 1703 | static struct nfs4_client *create_client(struct xdr_netobj name, |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1704 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 1705 | { |
| 1706 | struct nfs4_client *clp; |
| 1707 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1708 | int ret; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1709 | struct net *net = SVC_NET(rqstp); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1710 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1711 | |
| 1712 | clp = alloc_client(name); |
| 1713 | if (clp == NULL) |
| 1714 | return NULL; |
| 1715 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1716 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 1717 | if (ret) { |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1718 | spin_lock(&nn->client_lock); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1719 | free_client(clp); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1720 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1721 | return NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1722 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 1723 | INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null); |
Benny Halevy | 07cd490 | 2010-05-12 00:13:41 +0300 | [diff] [blame] | 1724 | clp->cl_time = get_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1725 | clear_bit(0, &clp->cl_cb_slot_busy); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1726 | copy_verf(clp, verf); |
| 1727 | rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1728 | gen_confirm(clp); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1729 | clp->cl_cb_session = NULL; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1730 | clp->net = net; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1731 | return clp; |
| 1732 | } |
| 1733 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1734 | static void |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1735 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 1736 | { |
| 1737 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 1738 | struct nfs4_client *clp; |
| 1739 | |
| 1740 | while (*new) { |
| 1741 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 1742 | parent = *new; |
| 1743 | |
| 1744 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 1745 | new = &((*new)->rb_left); |
| 1746 | else |
| 1747 | new = &((*new)->rb_right); |
| 1748 | } |
| 1749 | |
| 1750 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 1751 | rb_insert_color(&new_clp->cl_namenode, root); |
| 1752 | } |
| 1753 | |
| 1754 | static struct nfs4_client * |
| 1755 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 1756 | { |
| 1757 | long long cmp; |
| 1758 | struct rb_node *node = root->rb_node; |
| 1759 | struct nfs4_client *clp; |
| 1760 | |
| 1761 | while (node) { |
| 1762 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 1763 | cmp = compare_blob(&clp->cl_name, name); |
| 1764 | if (cmp > 0) |
| 1765 | node = node->rb_left; |
| 1766 | else if (cmp < 0) |
| 1767 | node = node->rb_right; |
| 1768 | else |
| 1769 | return clp; |
| 1770 | } |
| 1771 | return NULL; |
| 1772 | } |
| 1773 | |
| 1774 | static void |
| 1775 | add_to_unconfirmed(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | { |
| 1777 | unsigned int idhashval; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1778 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1780 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1781 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1783 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 1784 | renew_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1787 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | move_to_confirmed(struct nfs4_client *clp) |
| 1789 | { |
| 1790 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 1791 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | |
| 1793 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 1794 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1795 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1796 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1797 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | renew_client(clp); |
| 1799 | } |
| 1800 | |
| 1801 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1802 | 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] | 1803 | { |
| 1804 | struct nfs4_client *clp; |
| 1805 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 1806 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1807 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1808 | if (same_clid(&clp->cl_clientid, clid)) { |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 1809 | if ((bool)clp->cl_minorversion != sessions) |
| 1810 | return NULL; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1811 | renew_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | return clp; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1813 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | } |
| 1815 | return NULL; |
| 1816 | } |
| 1817 | |
| 1818 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1819 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 1820 | { |
| 1821 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 1822 | |
| 1823 | return find_client_in_id_table(tbl, clid, sessions); |
| 1824 | } |
| 1825 | |
| 1826 | static struct nfs4_client * |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1827 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | { |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1829 | struct list_head *tbl = nn->unconf_id_hashtbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1831 | return find_client_in_id_table(tbl, clid, sessions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 1834 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 1835 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 1836 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 1837 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 1838 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1839 | static struct nfs4_client * |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1840 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1841 | { |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1842 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | static struct nfs4_client * |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1846 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1847 | { |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1848 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1849 | } |
| 1850 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1851 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 1852 | 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] | 1853 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1854 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 1855 | struct sockaddr *sa = svc_addr(rqstp); |
| 1856 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 1857 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 1859 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 1860 | if (se->se_callback_netid_len == 3 && |
| 1861 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 1862 | expected_family = AF_INET; |
| 1863 | else if (se->se_callback_netid_len == 4 && |
| 1864 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 1865 | expected_family = AF_INET6; |
| 1866 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | goto out_err; |
| 1868 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1869 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1870 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1871 | (struct sockaddr *)&conn->cb_addr, |
| 1872 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1873 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1874 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1876 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1877 | if (conn->cb_addr.ss_family == AF_INET6) |
| 1878 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 1879 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1880 | conn->cb_prog = se->se_callback_prog; |
| 1881 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 1882 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | return; |
| 1884 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1885 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 1886 | conn->cb_addrlen = 0; |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 1887 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | "will not receive delegations\n", |
| 1889 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 1890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | return; |
| 1892 | } |
| 1893 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1894 | /* |
J. Bruce Fields | 067e1ac | 2014-03-21 17:26:44 -0400 | [diff] [blame] | 1895 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1896 | */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 1897 | static void |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1898 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 1899 | { |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1900 | struct xdr_buf *buf = resp->xdr.buf; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1901 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 1902 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1903 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1904 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1905 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1906 | slot->sl_opcnt = resp->opcnt; |
| 1907 | slot->sl_status = resp->cstate.status; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1908 | |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 1909 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1910 | if (nfsd4_not_cached(resp)) { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1911 | slot->sl_datalen = 0; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1912 | return; |
| 1913 | } |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1914 | base = resp->cstate.data_offset; |
| 1915 | slot->sl_datalen = buf->len - base; |
| 1916 | 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] | 1917 | WARN("%s: sessions DRC could not cache compound\n", __func__); |
| 1918 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1922 | * Encode the replay sequence operation from the slot values. |
| 1923 | * If cachethis is FALSE encode the uncached rep error on the next |
| 1924 | * operation which sets resp->p and increments resp->opcnt for |
| 1925 | * nfs4svc_encode_compoundres. |
| 1926 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1927 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1928 | static __be32 |
| 1929 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 1930 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1931 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1932 | struct nfsd4_op *op; |
| 1933 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1934 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1935 | /* Encode the replayed sequence operation */ |
| 1936 | op = &args->ops[resp->opcnt - 1]; |
| 1937 | nfsd4_encode_operation(resp, op); |
| 1938 | |
| 1939 | /* Return nfserr_retry_uncached_rep in next operation. */ |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 1940 | if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1941 | op = &args->ops[resp->opcnt++]; |
| 1942 | op->status = nfserr_retry_uncached_rep; |
| 1943 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1944 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1945 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1949 | * The sequence operation is not cached because we can use the slot and |
| 1950 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1951 | */ |
J. Bruce Fields | 3ca2eb9 | 2014-03-21 12:04:21 -0400 | [diff] [blame] | 1952 | static __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1953 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 1954 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1955 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1956 | struct nfsd4_slot *slot = resp->cstate.slot; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1957 | struct xdr_stream *xdr = &resp->xdr; |
| 1958 | __be32 *p; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1959 | __be32 status; |
| 1960 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1961 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1962 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1963 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
J. Bruce Fields | 0da7b19 | 2014-03-21 11:43:34 -0400 | [diff] [blame] | 1964 | if (status) |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1965 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1966 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1967 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 1968 | if (!p) { |
| 1969 | WARN_ON_ONCE(1); |
| 1970 | return nfserr_serverfault; |
| 1971 | } |
| 1972 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 1973 | xdr_commit_encode(xdr); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1974 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1975 | resp->opcnt = slot->sl_opcnt; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1976 | return slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1977 | } |
| 1978 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1979 | /* |
| 1980 | * Set the exchange_id flags returned by the server. |
| 1981 | */ |
| 1982 | static void |
| 1983 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 1984 | { |
| 1985 | /* pNFS is not supported */ |
| 1986 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
| 1987 | |
| 1988 | /* Referrals are supported, Migration is not. */ |
| 1989 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 1990 | |
| 1991 | /* set the wire flags to return to client. */ |
| 1992 | clid->flags = new->cl_exchange_flags; |
| 1993 | } |
| 1994 | |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 1995 | static bool client_has_state(struct nfs4_client *clp) |
| 1996 | { |
| 1997 | /* |
| 1998 | * Note clp->cl_openowners check isn't quite right: there's no |
| 1999 | * need to count owners without stateid's. |
| 2000 | * |
| 2001 | * Also note we should probably be using this in 4.0 case too. |
| 2002 | */ |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 2003 | return !list_empty(&clp->cl_openowners) |
| 2004 | || !list_empty(&clp->cl_delegations) |
| 2005 | || !list_empty(&clp->cl_sessions); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2006 | } |
| 2007 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2008 | __be32 |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2009 | nfsd4_exchange_id(struct svc_rqst *rqstp, |
| 2010 | struct nfsd4_compound_state *cstate, |
| 2011 | struct nfsd4_exchange_id *exid) |
| 2012 | { |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2013 | struct nfs4_client *unconf, *conf, *new; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2014 | __be32 status; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2015 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2016 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2017 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 2018 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2019 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2020 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2021 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2022 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2023 | "ip_addr=%s flags %x, spa_how %d\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2024 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2025 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2026 | |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 2027 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2028 | return nfserr_inval; |
| 2029 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2030 | switch (exid->spa_how) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2031 | case SP4_MACH_CRED: |
| 2032 | if (!svc_rqst_integrity_protected(rqstp)) |
| 2033 | return nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2034 | case SP4_NONE: |
| 2035 | break; |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 2036 | default: /* checked by xdr code */ |
| 2037 | WARN_ON_ONCE(1); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2038 | case SP4_SSV: |
J. Bruce Fields | dd30333 | 2013-04-12 18:10:56 -0400 | [diff] [blame] | 2039 | return nfserr_encr_alg_unsupp; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2040 | } |
| 2041 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2042 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2043 | nfs4_lock_state(); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2044 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2045 | if (conf) { |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 2046 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 2047 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 2048 | |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2049 | if (update) { |
| 2050 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2051 | status = nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2052 | goto out; |
| 2053 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2054 | if (!mach_creds_match(conf, rqstp)) { |
| 2055 | status = nfserr_wrong_cred; |
| 2056 | goto out; |
| 2057 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2058 | if (!creds_match) { /* case 9 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2059 | status = nfserr_perm; |
| 2060 | goto out; |
| 2061 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2062 | if (!verfs_match) { /* case 8 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2063 | status = nfserr_not_same; |
| 2064 | goto out; |
| 2065 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2066 | /* case 6 */ |
| 2067 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
| 2068 | new = conf; |
| 2069 | goto out_copy; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2070 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2071 | if (!creds_match) { /* case 3 */ |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2072 | if (client_has_state(conf)) { |
| 2073 | status = nfserr_clid_inuse; |
| 2074 | goto out; |
| 2075 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2076 | expire_client(conf); |
| 2077 | goto out_new; |
| 2078 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2079 | if (verfs_match) { /* case 2 */ |
J. Bruce Fields | 0f1ba0e | 2012-05-25 21:24:40 -0400 | [diff] [blame] | 2080 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2081 | new = conf; |
| 2082 | goto out_copy; |
| 2083 | } |
| 2084 | /* case 5, client reboot */ |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2085 | goto out_new; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 2086 | } |
| 2087 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2088 | if (update) { /* case 7 */ |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 2089 | status = nfserr_noent; |
| 2090 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2091 | } |
| 2092 | |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2093 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2094 | if (unconf) /* case 4, possible retry or client restart */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2095 | expire_client(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2096 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2097 | /* case 1 (normal case) */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2098 | out_new: |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 2099 | new = create_client(exid->clname, rqstp, &verf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2100 | if (new == NULL) { |
J. Bruce Fields | 4731030 | 2010-06-22 16:17:12 -0400 | [diff] [blame] | 2101 | status = nfserr_jukebox; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2102 | goto out; |
| 2103 | } |
J. Bruce Fields | 4f540e2 | 2013-05-07 11:57:52 -0400 | [diff] [blame] | 2104 | new->cl_minorversion = cstate->minorversion; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2105 | new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2106 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2107 | gen_clid(new, nn); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2108 | add_to_unconfirmed(new); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2109 | out_copy: |
| 2110 | exid->clientid.cl_boot = new->cl_clientid.cl_boot; |
| 2111 | exid->clientid.cl_id = new->cl_clientid.cl_id; |
| 2112 | |
J. Bruce Fields | 778df3f | 2012-05-25 21:40:23 -0400 | [diff] [blame] | 2113 | exid->seqid = new->cl_cs_slot.sl_seqid + 1; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2114 | nfsd4_set_ex_flags(new, exid); |
| 2115 | |
| 2116 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2117 | new->cl_cs_slot.sl_seqid, new->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2118 | status = nfs_ok; |
| 2119 | |
| 2120 | out: |
| 2121 | nfs4_unlock_state(); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2122 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2123 | } |
| 2124 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2125 | static __be32 |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2126 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2127 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2128 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 2129 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2130 | |
| 2131 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2132 | if (slot_inuse) { |
| 2133 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2134 | return nfserr_jukebox; |
| 2135 | else |
| 2136 | return nfserr_seq_misordered; |
| 2137 | } |
J. Bruce Fields | f6d8248 | 2012-02-13 16:13:41 -0500 | [diff] [blame] | 2138 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2139 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2140 | return nfs_ok; |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2141 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2142 | return nfserr_replay_cache; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2143 | return nfserr_seq_misordered; |
| 2144 | } |
| 2145 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2146 | /* |
| 2147 | * Cache the create session result into the create session single DRC |
| 2148 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 2149 | * Do this for solo or embedded create session operations. |
| 2150 | */ |
| 2151 | static void |
| 2152 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2153 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2154 | { |
| 2155 | slot->sl_status = nfserr; |
| 2156 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 2157 | } |
| 2158 | |
| 2159 | static __be32 |
| 2160 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 2161 | struct nfsd4_clid_slot *slot) |
| 2162 | { |
| 2163 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 2164 | return slot->sl_status; |
| 2165 | } |
| 2166 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2167 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 2168 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 2169 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2170 | 3 + /* version, opcount, opcode */ \ |
| 2171 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2172 | /* seqid, slotID, slotID, cache */ \ |
| 2173 | 4 ) * sizeof(__be32)) |
| 2174 | |
| 2175 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 2176 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 2177 | 1 + /* status */ \ |
| 2178 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2179 | 3 + /* opcount, opcode, opstatus*/ \ |
| 2180 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2181 | /* seqid, slotID, slotID, slotID, status */ \ |
| 2182 | 5 ) * sizeof(__be32)) |
| 2183 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2184 | 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] | 2185 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2186 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 2187 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2188 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 2189 | return nfserr_toosmall; |
| 2190 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 2191 | return nfserr_toosmall; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2192 | ca->headerpadsz = 0; |
| 2193 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 2194 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 2195 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 2196 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 2197 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 2198 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 2199 | /* |
| 2200 | * Note decreasing slot size below client's request may make it |
| 2201 | * difficult for client to function correctly, whereas |
| 2202 | * decreasing the number of slots will (just?) affect |
| 2203 | * performance. When short on memory we therefore prefer to |
| 2204 | * decrease number of slots instead of their size. Clients that |
| 2205 | * request larger slots than they need will get poor results: |
| 2206 | */ |
| 2207 | ca->maxreqs = nfsd4_get_drc_mem(ca); |
| 2208 | if (!ca->maxreqs) |
| 2209 | return nfserr_jukebox; |
| 2210 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2211 | return nfs_ok; |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2212 | } |
| 2213 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2214 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
| 2215 | RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32)) |
| 2216 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
| 2217 | RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32)) |
| 2218 | |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2219 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 2220 | { |
| 2221 | ca->headerpadsz = 0; |
| 2222 | |
| 2223 | /* |
| 2224 | * These RPC_MAX_HEADER macros are overkill, especially since we |
| 2225 | * don't even do gss on the backchannel yet. But this is still |
| 2226 | * less than 1k. Tighten up this estimate in the unlikely event |
| 2227 | * it turns out to be a problem for some client: |
| 2228 | */ |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2229 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2230 | return nfserr_toosmall; |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2231 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2232 | return nfserr_toosmall; |
| 2233 | ca->maxresp_cached = 0; |
| 2234 | if (ca->maxops < 2) |
| 2235 | return nfserr_toosmall; |
| 2236 | |
| 2237 | return nfs_ok; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2238 | } |
| 2239 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2240 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 2241 | { |
| 2242 | switch (cbs->flavor) { |
| 2243 | case RPC_AUTH_NULL: |
| 2244 | case RPC_AUTH_UNIX: |
| 2245 | return nfs_ok; |
| 2246 | default: |
| 2247 | /* |
| 2248 | * GSS case: the spec doesn't allow us to return this |
| 2249 | * error. But it also doesn't allow us not to support |
| 2250 | * GSS. |
| 2251 | * I'd rather this fail hard than return some error the |
| 2252 | * client might think it can already handle: |
| 2253 | */ |
| 2254 | return nfserr_encr_alg_unsupp; |
| 2255 | } |
| 2256 | } |
| 2257 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2258 | __be32 |
| 2259 | nfsd4_create_session(struct svc_rqst *rqstp, |
| 2260 | struct nfsd4_compound_state *cstate, |
| 2261 | struct nfsd4_create_session *cr_ses) |
| 2262 | { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2263 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2264 | struct nfs4_client *conf, *unconf; |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2265 | struct nfsd4_session *new; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2266 | struct nfsd4_conn *conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2267 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2268 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2269 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2270 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2271 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 2272 | return nfserr_inval; |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2273 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 2274 | if (status) |
| 2275 | return status; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2276 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2277 | if (status) |
| 2278 | return status; |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2279 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 2280 | if (status) |
Kinglong Mee | f403e45 | 2013-12-23 17:31:21 +0800 | [diff] [blame] | 2281 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2282 | status = nfserr_jukebox; |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 2283 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2284 | if (!new) |
| 2285 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2286 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 2287 | if (!conn) |
| 2288 | goto out_free_session; |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2289 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2290 | nfs4_lock_state(); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2291 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2292 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2293 | WARN_ON_ONCE(conf && unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2294 | |
| 2295 | if (conf) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2296 | status = nfserr_wrong_cred; |
| 2297 | if (!mach_creds_match(conf, rqstp)) |
| 2298 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2299 | cs_slot = &conf->cl_cs_slot; |
| 2300 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 2301 | if (status == nfserr_replay_cache) { |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2302 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2303 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2304 | } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2305 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2306 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2307 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2308 | } else if (unconf) { |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2309 | struct nfs4_client *old; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2310 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2311 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2312 | status = nfserr_clid_inuse; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2313 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2314 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2315 | status = nfserr_wrong_cred; |
| 2316 | if (!mach_creds_match(unconf, rqstp)) |
| 2317 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2318 | cs_slot = &unconf->cl_cs_slot; |
| 2319 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 2320 | if (status) { |
| 2321 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2322 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2323 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2324 | } |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2325 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2326 | if (old) { |
| 2327 | status = mark_client_expired(old); |
| 2328 | if (status) |
| 2329 | goto out_free_conn; |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2330 | expire_client(old); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2331 | } |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2332 | move_to_confirmed(unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2333 | conf = unconf; |
| 2334 | } else { |
| 2335 | status = nfserr_stale_clientid; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2336 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2337 | } |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2338 | status = nfs_ok; |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 2339 | /* |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 2340 | * We do not support RDMA or persistent sessions |
| 2341 | */ |
| 2342 | cr_ses->flags &= ~SESSION4_PERSIST; |
| 2343 | cr_ses->flags &= ~SESSION4_RDMA; |
| 2344 | |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2345 | init_session(rqstp, new, conf, cr_ses); |
| 2346 | nfsd4_init_conn(rqstp, conn, new); |
| 2347 | |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2348 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2349 | NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 2350 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2351 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2352 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2353 | /* cache solo and embedded create sessions under the state lock */ |
| 2354 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2355 | nfs4_unlock_state(); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2356 | return status; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2357 | out_free_conn: |
Yanchuan Nian | 266533c | 2012-12-24 18:11:45 +0800 | [diff] [blame] | 2358 | nfs4_unlock_state(); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2359 | free_conn(conn); |
| 2360 | out_free_session: |
| 2361 | __free_session(new); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2362 | out_release_drc_mem: |
| 2363 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
J. Bruce Fields | 1ca5079 | 2013-03-14 18:12:03 -0400 | [diff] [blame] | 2364 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2365 | } |
| 2366 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2367 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 2368 | { |
| 2369 | switch (*dir) { |
| 2370 | case NFS4_CDFC4_FORE: |
| 2371 | case NFS4_CDFC4_BACK: |
| 2372 | return nfs_ok; |
| 2373 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 2374 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 2375 | *dir = NFS4_CDFC4_BOTH; |
| 2376 | return nfs_ok; |
| 2377 | }; |
| 2378 | return nfserr_inval; |
| 2379 | } |
| 2380 | |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2381 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc) |
| 2382 | { |
| 2383 | struct nfsd4_session *session = cstate->session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2384 | 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] | 2385 | __be32 status; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2386 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2387 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 2388 | if (status) |
| 2389 | return status; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2390 | spin_lock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2391 | session->se_cb_prog = bc->bc_cb_program; |
| 2392 | session->se_cb_sec = bc->bc_cb_sec; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2393 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2394 | |
| 2395 | nfsd4_probe_callback(session->se_client); |
| 2396 | |
| 2397 | return nfs_ok; |
| 2398 | } |
| 2399 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2400 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 2401 | struct nfsd4_compound_state *cstate, |
| 2402 | struct nfsd4_bind_conn_to_session *bcts) |
| 2403 | { |
| 2404 | __be32 status; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2405 | struct nfsd4_conn *conn; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2406 | struct nfsd4_session *session; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2407 | struct net *net = SVC_NET(rqstp); |
| 2408 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2409 | |
| 2410 | if (!nfsd4_last_compound_op(rqstp)) |
| 2411 | return nfserr_not_only_op; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2412 | nfs4_lock_state(); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2413 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2414 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2415 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2416 | if (!session) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2417 | goto out_no_session; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2418 | status = nfserr_wrong_cred; |
| 2419 | if (!mach_creds_match(session->se_client, rqstp)) |
| 2420 | goto out; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2421 | status = nfsd4_map_bcts_dir(&bcts->dir); |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2422 | if (status) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2423 | goto out; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2424 | conn = alloc_conn(rqstp, bcts->dir); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2425 | status = nfserr_jukebox; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2426 | if (!conn) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2427 | goto out; |
| 2428 | nfsd4_init_conn(rqstp, conn, session); |
| 2429 | status = nfs_ok; |
| 2430 | out: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2431 | nfsd4_put_session(session); |
| 2432 | out_no_session: |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2433 | nfs4_unlock_state(); |
| 2434 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2435 | } |
| 2436 | |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2437 | static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid) |
| 2438 | { |
| 2439 | if (!session) |
| 2440 | return 0; |
| 2441 | return !memcmp(sid, &session->se_sessionid, sizeof(*sid)); |
| 2442 | } |
| 2443 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2444 | __be32 |
| 2445 | nfsd4_destroy_session(struct svc_rqst *r, |
| 2446 | struct nfsd4_compound_state *cstate, |
| 2447 | struct nfsd4_destroy_session *sessionid) |
| 2448 | { |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2449 | struct nfsd4_session *ses; |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2450 | __be32 status; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2451 | int ref_held_by_me = 0; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2452 | struct net *net = SVC_NET(r); |
| 2453 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2454 | |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2455 | nfs4_lock_state(); |
| 2456 | status = nfserr_not_only_op; |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2457 | if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2458 | if (!nfsd4_last_compound_op(r)) |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2459 | goto out; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2460 | ref_held_by_me++; |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2461 | } |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2462 | dump_sessionid(__func__, &sessionid->sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2463 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2464 | ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2465 | if (!ses) |
| 2466 | goto out_client_lock; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2467 | status = nfserr_wrong_cred; |
| 2468 | if (!mach_creds_match(ses->se_client, r)) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2469 | goto out_put_session; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2470 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2471 | if (status) |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2472 | goto out_put_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2473 | unhash_session(ses); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2474 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2475 | |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 2476 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 2477 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2478 | spin_lock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2479 | status = nfs_ok; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2480 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2481 | nfsd4_put_session_locked(ses); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2482 | out_client_lock: |
| 2483 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2484 | out: |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2485 | nfs4_unlock_state(); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2486 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2487 | } |
| 2488 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2489 | 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] | 2490 | { |
| 2491 | struct nfsd4_conn *c; |
| 2492 | |
| 2493 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2494 | if (c->cn_xprt == xpt) { |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2495 | return c; |
| 2496 | } |
| 2497 | } |
| 2498 | return NULL; |
| 2499 | } |
| 2500 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2501 | 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] | 2502 | { |
| 2503 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2504 | struct nfsd4_conn *c; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2505 | __be32 status = nfs_ok; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2506 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2507 | |
| 2508 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2509 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2510 | if (c) |
| 2511 | goto out_free; |
| 2512 | status = nfserr_conn_not_bound_to_session; |
| 2513 | if (clp->cl_mach_cred) |
| 2514 | goto out_free; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2515 | __nfsd4_hash_conn(new, ses); |
| 2516 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2517 | ret = nfsd4_register_conn(new); |
| 2518 | if (ret) |
| 2519 | /* oops; xprt is already down: */ |
| 2520 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2521 | return nfs_ok; |
| 2522 | out_free: |
| 2523 | spin_unlock(&clp->cl_lock); |
| 2524 | free_conn(new); |
| 2525 | return status; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2526 | } |
| 2527 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2528 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 2529 | { |
| 2530 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 2531 | |
| 2532 | return args->opcnt > session->se_fchannel.maxops; |
| 2533 | } |
| 2534 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2535 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 2536 | struct nfsd4_session *session) |
| 2537 | { |
| 2538 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 2539 | |
| 2540 | return xb->len > session->se_fchannel.maxreq_sz; |
| 2541 | } |
| 2542 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2543 | __be32 |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2544 | nfsd4_sequence(struct svc_rqst *rqstp, |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2545 | struct nfsd4_compound_state *cstate, |
| 2546 | struct nfsd4_sequence *seq) |
| 2547 | { |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2548 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2549 | struct xdr_stream *xdr = &resp->xdr; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2550 | struct nfsd4_session *session; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2551 | struct nfs4_client *clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2552 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2553 | struct nfsd4_conn *conn; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2554 | __be32 status; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2555 | int buflen; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2556 | struct net *net = SVC_NET(rqstp); |
| 2557 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2558 | |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2559 | if (resp->opcnt != 1) |
| 2560 | return nfserr_sequence_pos; |
| 2561 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2562 | /* |
| 2563 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 2564 | * below. |
| 2565 | */ |
| 2566 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 2567 | if (!conn) |
| 2568 | return nfserr_jukebox; |
| 2569 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2570 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2571 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2572 | if (!session) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2573 | goto out_no_session; |
| 2574 | clp = session->se_client; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2575 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2576 | status = nfserr_too_many_ops; |
| 2577 | if (nfsd4_session_too_many_ops(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2578 | goto out_put_session; |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2579 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2580 | status = nfserr_req_too_big; |
| 2581 | if (nfsd4_request_too_big(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2582 | goto out_put_session; |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2583 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2584 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 2585 | if (seq->slotid >= session->se_fchannel.maxreqs) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2586 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2587 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2588 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2589 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 2590 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 2591 | /* We do not negotiate the number of slots yet, so set the |
| 2592 | * maxslots to the session maxreqs which is used to encode |
| 2593 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 2594 | seq->maxslots = session->se_fchannel.maxreqs; |
| 2595 | |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2596 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 2597 | slot->sl_flags & NFSD4_SLOT_INUSE); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2598 | if (status == nfserr_replay_cache) { |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2599 | status = nfserr_seq_misordered; |
| 2600 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2601 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2602 | cstate->slot = slot; |
| 2603 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2604 | cstate->clp = clp; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2605 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2606 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2607 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2608 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 2609 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2610 | } |
| 2611 | if (status) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2612 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2613 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2614 | status = nfsd4_sequence_check_conn(conn, session); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2615 | conn = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2616 | if (status) |
| 2617 | goto out_put_session; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2618 | |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2619 | buflen = (seq->cachethis) ? |
| 2620 | session->se_fchannel.maxresp_cached : |
| 2621 | session->se_fchannel.maxresp_sz; |
| 2622 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 2623 | nfserr_rep_too_big; |
J. Bruce Fields | a5cddc8 | 2014-05-12 18:10:58 -0400 | [diff] [blame] | 2624 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2625 | goto out_put_session; |
J. Bruce Fields | 32aaa62 | 2014-03-20 20:47:41 -0400 | [diff] [blame] | 2626 | svc_reserve(rqstp, buflen); |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2627 | |
| 2628 | status = nfs_ok; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2629 | /* Success! bump slot seqid */ |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2630 | slot->sl_seqid = seq->seqid; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2631 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2632 | if (seq->cachethis) |
| 2633 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2634 | else |
| 2635 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2636 | |
| 2637 | cstate->slot = slot; |
| 2638 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2639 | cstate->clp = clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2640 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2641 | out: |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2642 | switch (clp->cl_cb_state) { |
| 2643 | case NFSD4_CB_DOWN: |
| 2644 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 2645 | break; |
| 2646 | case NFSD4_CB_FAULT: |
| 2647 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 2648 | break; |
| 2649 | default: |
| 2650 | seq->status_flags = 0; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 2651 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 2652 | if (!list_empty(&clp->cl_revoked)) |
| 2653 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2654 | out_no_session: |
Kinglong Mee | 3f42d2c | 2014-03-24 11:56:59 +0800 | [diff] [blame] | 2655 | if (conn) |
| 2656 | free_conn(conn); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2657 | spin_unlock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2658 | return status; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2659 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2660 | nfsd4_put_session_locked(session); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2661 | goto out_no_session; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2662 | } |
| 2663 | |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2664 | void |
| 2665 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 2666 | { |
| 2667 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 2668 | |
| 2669 | if (nfsd4_has_session(cs)) { |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2670 | if (cs->status != nfserr_replay_cache) { |
| 2671 | nfsd4_store_cache_entry(resp); |
| 2672 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 2673 | } |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2674 | /* Drop session reference that was taken in nfsd4_sequence() */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2675 | nfsd4_put_session(cs->session); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2676 | } else if (cs->clp) |
| 2677 | put_client_renew(cs->clp); |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2678 | } |
| 2679 | |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2680 | __be32 |
| 2681 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc) |
| 2682 | { |
| 2683 | struct nfs4_client *conf, *unconf, *clp; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2684 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2685 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2686 | |
| 2687 | nfs4_lock_state(); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2688 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2689 | conf = find_confirmed_client(&dc->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2690 | WARN_ON_ONCE(conf && unconf); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2691 | |
| 2692 | if (conf) { |
| 2693 | clp = conf; |
| 2694 | |
J. Bruce Fields | c0293b0 | 2013-03-14 18:20:01 -0400 | [diff] [blame] | 2695 | if (client_has_state(conf)) { |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2696 | status = nfserr_clientid_busy; |
| 2697 | goto out; |
| 2698 | } |
| 2699 | } else if (unconf) |
| 2700 | clp = unconf; |
| 2701 | else { |
| 2702 | status = nfserr_stale_clientid; |
| 2703 | goto out; |
| 2704 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2705 | if (!mach_creds_match(clp, rqstp)) { |
| 2706 | status = nfserr_wrong_cred; |
| 2707 | goto out; |
| 2708 | } |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2709 | expire_client(clp); |
| 2710 | out: |
| 2711 | nfs4_unlock_state(); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2712 | return status; |
| 2713 | } |
| 2714 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2715 | __be32 |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2716 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc) |
| 2717 | { |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2718 | __be32 status = 0; |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2719 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2720 | if (rc->rca_one_fs) { |
| 2721 | if (!cstate->current_fh.fh_dentry) |
| 2722 | return nfserr_nofilehandle; |
| 2723 | /* |
| 2724 | * We don't take advantage of the rca_one_fs case. |
| 2725 | * That's OK, it's optional, we can safely ignore it. |
| 2726 | */ |
| 2727 | return nfs_ok; |
| 2728 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2729 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2730 | nfs4_lock_state(); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2731 | status = nfserr_complete_already; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 2732 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
| 2733 | &cstate->session->se_client->cl_flags)) |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2734 | goto out; |
| 2735 | |
| 2736 | status = nfserr_stale_clientid; |
| 2737 | if (is_client_expired(cstate->session->se_client)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2738 | /* |
| 2739 | * The following error isn't really legal. |
| 2740 | * But we only get here if the client just explicitly |
| 2741 | * destroyed the client. Surely it no longer cares what |
| 2742 | * error it gets back on an operation for the dead |
| 2743 | * client. |
| 2744 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2745 | goto out; |
| 2746 | |
| 2747 | status = nfs_ok; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 2748 | nfsd4_client_record_create(cstate->session->se_client); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2749 | out: |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2750 | nfs4_unlock_state(); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2751 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 2755 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 2756 | struct nfsd4_setclientid *setclid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | { |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 2758 | struct xdr_netobj clname = setclid->se_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | nfs4_verifier clverifier = setclid->se_verf; |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2760 | struct nfs4_client *conf, *unconf, *new; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2761 | __be32 status; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2762 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 2763 | |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2764 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | nfs4_lock_state(); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2766 | conf = find_confirmed_client_by_name(&clname, nn); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2767 | if (conf) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2768 | /* case 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2770 | if (clp_used_exchangeid(conf)) |
| 2771 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 2772 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2773 | char addr_str[INET6_ADDRSTRLEN]; |
| 2774 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, |
| 2775 | sizeof(addr_str)); |
| 2776 | dprintk("NFSD: setclientid: string in use by client " |
| 2777 | "at %s\n", addr_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | goto out; |
| 2779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2781 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 2782 | if (unconf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | status = nfserr_jukebox; |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 2785 | new = create_client(clname, rqstp, &clverifier); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 2786 | if (new == NULL) |
| 2787 | goto out; |
J. Bruce Fields | 34b232b | 2012-05-18 22:23:42 -0400 | [diff] [blame] | 2788 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2789 | /* case 1: probable callback update */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | copy_clid(new, conf); |
J. Bruce Fields | 34b232b | 2012-05-18 22:23:42 -0400 | [diff] [blame] | 2791 | else /* case 4 (new client) or cases 2, 3 (client reboot): */ |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2792 | gen_clid(new, nn); |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 2793 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2794 | gen_callback(new, setclid, rqstp); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2795 | add_to_unconfirmed(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 2797 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 2798 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); |
| 2799 | status = nfs_ok; |
| 2800 | out: |
| 2801 | nfs4_unlock_state(); |
| 2802 | return status; |
| 2803 | } |
| 2804 | |
| 2805 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2806 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 2807 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
| 2808 | struct nfsd4_compound_state *cstate, |
| 2809 | struct nfsd4_setclientid_confirm *setclientid_confirm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | { |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2811 | struct nfs4_client *conf, *unconf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 2813 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2814 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 2815 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 2817 | if (STALE_CLIENTID(clid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | return nfserr_stale_clientid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | nfs4_lock_state(); |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2820 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2821 | conf = find_confirmed_client(clid, false, nn); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2822 | unconf = find_unconfirmed_client(clid, false, nn); |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2823 | /* |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2824 | * We try hard to give out unique clientid's, so if we get an |
| 2825 | * attempt to confirm the same clientid with a different cred, |
| 2826 | * there's a bug somewhere. Let's charitably assume it's our |
| 2827 | * bug. |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2828 | */ |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2829 | status = nfserr_serverfault; |
| 2830 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
| 2831 | goto out; |
| 2832 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
| 2833 | goto out; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2834 | /* cases below refer to rfc 3530 section 14.2.34: */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 2835 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
| 2836 | if (conf && !unconf) /* case 2: probable retransmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | status = nfs_ok; |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 2838 | else /* case 4: client hasn't noticed we rebooted yet? */ |
| 2839 | status = nfserr_stale_clientid; |
| 2840 | goto out; |
| 2841 | } |
| 2842 | status = nfs_ok; |
| 2843 | if (conf) { /* case 1: callback update */ |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2844 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
| 2845 | nfsd4_probe_callback(conf); |
| 2846 | expire_client(unconf); |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 2847 | } else { /* case 3: normal case; new or rebooted client */ |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2848 | conf = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2849 | if (conf) { |
| 2850 | status = mark_client_expired(conf); |
| 2851 | if (status) |
| 2852 | goto out; |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2853 | expire_client(conf); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2854 | } |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2855 | move_to_confirmed(unconf); |
J. Bruce Fields | f3d03b9 | 2012-05-23 11:38:38 -0400 | [diff] [blame] | 2856 | nfsd4_probe_callback(unconf); |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 2857 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | nfs4_unlock_state(); |
| 2860 | return status; |
| 2861 | } |
| 2862 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2863 | static struct nfs4_file *nfsd4_alloc_file(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | { |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2865 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 2866 | } |
| 2867 | |
| 2868 | /* OPEN Share state helper functions */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 2869 | static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2870 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 2871 | unsigned int hashval = file_hashval(fh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 2873 | lockdep_assert_held(&state_lock); |
| 2874 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2875 | atomic_set(&fp->fi_ref, 1); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 2876 | spin_lock_init(&fp->fi_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2877 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 2878 | INIT_LIST_HEAD(&fp->fi_delegations); |
Trond Myklebust | e2cf80d | 2014-07-23 16:17:38 -0400 | [diff] [blame] | 2879 | fh_copy_shallow(&fp->fi_fhandle, fh); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2880 | fp->fi_had_conflict = false; |
| 2881 | fp->fi_lease = NULL; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 2882 | fp->fi_share_deny = 0; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2883 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 2884 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 2885 | hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | } |
| 2887 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 2888 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2889 | nfsd4_free_slabs(void) |
| 2890 | { |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2891 | kmem_cache_destroy(openowner_slab); |
| 2892 | kmem_cache_destroy(lockowner_slab); |
| 2893 | kmem_cache_destroy(file_slab); |
| 2894 | kmem_cache_destroy(stateid_slab); |
| 2895 | kmem_cache_destroy(deleg_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2896 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2897 | |
Bryan Schumaker | 7208339 | 2011-11-01 15:24:59 -0400 | [diff] [blame] | 2898 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | nfsd4_init_slabs(void) |
| 2900 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2901 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 2902 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 2903 | if (openowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2904 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2905 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
Yanchuan Nian | 3c40794 | 2012-10-24 14:44:19 +0800 | [diff] [blame] | 2906 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2907 | if (lockowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2908 | goto out_free_openowner_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2909 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2910 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2911 | if (file_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2912 | goto out_free_lockowner_slab; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2913 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2914 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2915 | if (stateid_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2916 | goto out_free_file_slab; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 2917 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2918 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 2919 | if (deleg_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2920 | goto out_free_stateid_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2921 | return 0; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2922 | |
| 2923 | out_free_stateid_slab: |
| 2924 | kmem_cache_destroy(stateid_slab); |
| 2925 | out_free_file_slab: |
| 2926 | kmem_cache_destroy(file_slab); |
| 2927 | out_free_lockowner_slab: |
| 2928 | kmem_cache_destroy(lockowner_slab); |
| 2929 | out_free_openowner_slab: |
| 2930 | kmem_cache_destroy(openowner_slab); |
| 2931 | out: |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2932 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 2933 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2934 | } |
| 2935 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2936 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 2937 | { |
| 2938 | rp->rp_status = nfserr_serverfault; |
| 2939 | rp->rp_buflen = 0; |
| 2940 | rp->rp_buf = rp->rp_ibuf; |
| 2941 | } |
| 2942 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2943 | 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] | 2944 | { |
| 2945 | struct nfs4_stateowner *sop; |
| 2946 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2947 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2948 | if (!sop) |
| 2949 | return NULL; |
| 2950 | |
| 2951 | sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL); |
| 2952 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2953 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2954 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2956 | sop->so_owner.len = owner->len; |
| 2957 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2958 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2959 | sop->so_client = clp; |
| 2960 | init_nfs4_replay(&sop->so_replay); |
| 2961 | return sop; |
| 2962 | } |
| 2963 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2964 | 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] | 2965 | { |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 2966 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2967 | |
| 2968 | list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2969 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | } |
| 2971 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2972 | static struct nfs4_openowner * |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 2973 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 2974 | struct nfsd4_compound_state *cstate) |
| 2975 | { |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 2976 | struct nfs4_client *clp = cstate->clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2977 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2979 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 2980 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2981 | return NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2982 | oo->oo_owner.so_is_open_owner = 1; |
| 2983 | oo->oo_owner.so_seqid = open->op_seqid; |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 2984 | oo->oo_flags = NFS4_OO_NEW; |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 2985 | if (nfsd4_has_session(cstate)) |
| 2986 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2987 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 2988 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2989 | INIT_LIST_HEAD(&oo->oo_close_lru); |
| 2990 | hash_openowner(oo, clp, strhashval); |
| 2991 | return oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2992 | } |
| 2993 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 2994 | 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] | 2995 | struct nfs4_openowner *oo = open->op_openowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2996 | |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 2997 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 2998 | INIT_LIST_HEAD(&stp->st_locks); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2999 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3000 | stp->st_stateowner = &oo->oo_owner; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3001 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3002 | stp->st_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | stp->st_access_bmap = 0; |
| 3004 | stp->st_deny_bmap = 0; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 3005 | stp->st_openstp = NULL; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3006 | spin_lock(&fp->fi_lock); |
| 3007 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 3008 | spin_unlock(&fp->fi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | static void |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3012 | move_to_close_lru(struct nfs4_openowner *oo, struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | { |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3014 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 3015 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3016 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3018 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3019 | oo->oo_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | } |
| 3021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 3023 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, |
| 3024 | clientid_t *clid) |
| 3025 | { |
| 3026 | return (sop->so_owner.len == owner->len) && |
| 3027 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len) && |
| 3028 | (sop->so_client->cl_clientid.cl_id == clid->cl_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | } |
| 3030 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3031 | static struct nfs4_openowner * |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 3032 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
| 3033 | bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 3035 | struct nfs4_stateowner *so; |
| 3036 | struct nfs4_openowner *oo; |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 3037 | struct nfs4_client *clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 3039 | list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) { |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 3040 | if (!so->so_is_open_owner) |
| 3041 | continue; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 3042 | if (same_owner_str(so, &open->op_owner, &open->op_clientid)) { |
| 3043 | oo = openowner(so); |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 3044 | clp = oo->oo_owner.so_client; |
| 3045 | if ((bool)clp->cl_minorversion != sessions) |
| 3046 | return NULL; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 3047 | renew_client(oo->oo_owner.so_client); |
| 3048 | return oo; |
| 3049 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3050 | } |
| 3051 | return NULL; |
| 3052 | } |
| 3053 | |
| 3054 | /* search file_hashtbl[] for file */ |
| 3055 | static struct nfs4_file * |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3056 | find_file_locked(struct knfsd_fh *fh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3058 | unsigned int hashval = file_hashval(fh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | struct nfs4_file *fp; |
| 3060 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3061 | lockdep_assert_held(&state_lock); |
| 3062 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 3063 | hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3064 | if (nfsd_fh_match(&fp->fi_fhandle, fh)) { |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3065 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | } |
| 3069 | return NULL; |
| 3070 | } |
| 3071 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3072 | static struct nfs4_file * |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3073 | find_file(struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3074 | { |
| 3075 | struct nfs4_file *fp; |
| 3076 | |
| 3077 | spin_lock(&state_lock); |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3078 | fp = find_file_locked(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3079 | spin_unlock(&state_lock); |
| 3080 | return fp; |
| 3081 | } |
| 3082 | |
| 3083 | static struct nfs4_file * |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 3084 | find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3085 | { |
| 3086 | struct nfs4_file *fp; |
| 3087 | |
| 3088 | spin_lock(&state_lock); |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3089 | fp = find_file_locked(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3090 | if (fp == NULL) { |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 3091 | nfsd4_init_file(new, fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3092 | fp = new; |
| 3093 | } |
| 3094 | spin_unlock(&state_lock); |
| 3095 | |
| 3096 | return fp; |
| 3097 | } |
| 3098 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 3099 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | * Called to check deny when READ with all zero stateid or |
| 3101 | * WRITE with all zero or all one stateid |
| 3102 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3103 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 3105 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | struct nfs4_file *fp; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3107 | __be32 ret = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3109 | fp = find_file(¤t_fh->fh_handle); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3110 | if (!fp) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3111 | return ret; |
| 3112 | /* Check for conflicting share reservations */ |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3113 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3114 | if (fp->fi_share_deny & deny_type) |
| 3115 | ret = nfserr_locked; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3116 | spin_unlock(&fp->fi_lock); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3117 | put_nfs4_file(fp); |
| 3118 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | } |
| 3120 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3121 | void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3123 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
| 3124 | nfsd_net_id); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 3125 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3126 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 3127 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3128 | /* |
| 3129 | * We can't do this in nfsd_break_deleg_cb because it is |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 3130 | * already holding inode->i_lock. |
| 3131 | * |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3132 | * If the dl_time != 0, then we know that it has already been |
| 3133 | * queued for a lease break. Don't queue it again. |
| 3134 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 3135 | spin_lock(&state_lock); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3136 | if (dp->dl_time == 0) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3137 | dp->dl_time = get_seconds(); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3138 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3139 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3140 | spin_unlock(&state_lock); |
| 3141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3143 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
| 3144 | { |
| 3145 | /* |
| 3146 | * We're assuming the state code never drops its reference |
| 3147 | * without first removing the lease. Since we're in this lease |
| 3148 | * callback (and since the lease code is serialized by the kernel |
| 3149 | * lock) we know the server hasn't removed the lease yet, we know |
| 3150 | * it's safe to take a reference. |
| 3151 | */ |
Trond Myklebust | 72c0b0f | 2014-07-21 09:34:58 -0400 | [diff] [blame] | 3152 | atomic_inc(&dp->dl_stid.sc_count); |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3153 | nfsd4_cb_recall(dp); |
| 3154 | } |
| 3155 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 3156 | /* Called from break_lease() with i_lock held. */ |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3157 | static void nfsd_break_deleg_cb(struct file_lock *fl) |
| 3158 | { |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3159 | struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; |
| 3160 | struct nfs4_delegation *dp; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3161 | |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 3162 | if (!fp) { |
| 3163 | WARN(1, "(%p)->fl_owner NULL\n", fl); |
| 3164 | return; |
| 3165 | } |
| 3166 | if (fp->fi_had_conflict) { |
| 3167 | WARN(1, "duplicate break on %p\n", fp); |
| 3168 | return; |
| 3169 | } |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3170 | /* |
| 3171 | * 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] | 3172 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3173 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3174 | */ |
| 3175 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3177 | spin_lock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3178 | fp->fi_had_conflict = true; |
| 3179 | /* |
| 3180 | * If there are no delegations on the list, then we can't count on this |
| 3181 | * lease ever being cleaned up. Set the fl_break_time to jiffies so that |
| 3182 | * time_out_leases will do it ASAP. The fact that fi_had_conflict is now |
| 3183 | * true should keep any new delegations from being hashed. |
| 3184 | */ |
| 3185 | if (list_empty(&fp->fi_delegations)) |
| 3186 | fl->fl_break_time = jiffies; |
| 3187 | else |
| 3188 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) |
| 3189 | nfsd_break_one_deleg(dp); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3190 | spin_unlock(&fp->fi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | } |
| 3192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | static |
| 3194 | int nfsd_change_deleg_cb(struct file_lock **onlist, int arg) |
| 3195 | { |
| 3196 | if (arg & F_UNLCK) |
| 3197 | return lease_modify(onlist, arg); |
| 3198 | else |
| 3199 | return -EAGAIN; |
| 3200 | } |
| 3201 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 3202 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 3203 | .lm_break = nfsd_break_deleg_cb, |
| 3204 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3205 | }; |
| 3206 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 3207 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 3208 | { |
| 3209 | if (nfsd4_has_session(cstate)) |
| 3210 | return nfs_ok; |
| 3211 | if (seqid == so->so_seqid - 1) |
| 3212 | return nfserr_replay_me; |
| 3213 | if (seqid == so->so_seqid) |
| 3214 | return nfs_ok; |
| 3215 | return nfserr_bad_seqid; |
| 3216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3218 | static __be32 lookup_clientid(clientid_t *clid, |
| 3219 | struct nfsd4_compound_state *cstate, |
| 3220 | struct nfsd_net *nn) |
| 3221 | { |
| 3222 | struct nfs4_client *found; |
| 3223 | |
| 3224 | if (cstate->clp) { |
| 3225 | found = cstate->clp; |
| 3226 | if (!same_clid(&found->cl_clientid, clid)) |
| 3227 | return nfserr_stale_clientid; |
| 3228 | return nfs_ok; |
| 3229 | } |
| 3230 | |
| 3231 | if (STALE_CLIENTID(clid, nn)) |
| 3232 | return nfserr_stale_clientid; |
| 3233 | |
| 3234 | /* |
| 3235 | * For v4.1+ we get the client in the SEQUENCE op. If we don't have one |
| 3236 | * cached already then we know this is for is for v4.0 and "sessions" |
| 3237 | * will be false. |
| 3238 | */ |
| 3239 | WARN_ON_ONCE(cstate->session); |
| 3240 | found = find_confirmed_client(clid, false, nn); |
| 3241 | if (!found) |
| 3242 | return nfserr_expired; |
| 3243 | |
| 3244 | /* Cache the nfs4_client in cstate! */ |
| 3245 | cstate->clp = found; |
| 3246 | atomic_inc(&found->cl_refcount); |
| 3247 | return nfs_ok; |
| 3248 | } |
| 3249 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3250 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3251 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 3252 | struct nfsd4_open *open, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | clientid_t *clientid = &open->op_clientid; |
| 3255 | struct nfs4_client *clp = NULL; |
| 3256 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3257 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3258 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3259 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 3260 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | return nfserr_stale_clientid; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3262 | /* |
| 3263 | * In case we need it later, after we've already created the |
| 3264 | * file and don't want to risk a further failure: |
| 3265 | */ |
| 3266 | open->op_file = nfsd4_alloc_file(); |
| 3267 | if (open->op_file == NULL) |
| 3268 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | |
Trond Myklebust | 2d91e89 | 2014-06-30 11:48:46 -0400 | [diff] [blame] | 3270 | status = lookup_clientid(clientid, cstate, nn); |
| 3271 | if (status) |
| 3272 | return status; |
| 3273 | clp = cstate->clp; |
| 3274 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 3275 | strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 3276 | oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3277 | open->op_openowner = oo; |
| 3278 | if (!oo) { |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3279 | goto new_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3280 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3281 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3282 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3283 | release_openowner(oo); |
| 3284 | open->op_openowner = NULL; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3285 | goto new_owner; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3286 | } |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3287 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 3288 | if (status) |
| 3289 | return status; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3290 | goto alloc_stateid; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3291 | new_owner: |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3292 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3293 | if (oo == NULL) |
| 3294 | return nfserr_jukebox; |
| 3295 | open->op_openowner = oo; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3296 | alloc_stateid: |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame^] | 3297 | open->op_stp = nfs4_alloc_open_stateid(clp); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3298 | if (!open->op_stp) |
| 3299 | return nfserr_jukebox; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3300 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3301 | } |
| 3302 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3303 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 3304 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 3305 | { |
| 3306 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 3307 | return nfserr_openmode; |
| 3308 | else |
| 3309 | return nfs_ok; |
| 3310 | } |
| 3311 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 3312 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3313 | { |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3314 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 3315 | } |
| 3316 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3317 | 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] | 3318 | { |
| 3319 | struct nfs4_stid *ret; |
| 3320 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3321 | ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 3322 | if (!ret) |
| 3323 | return NULL; |
| 3324 | return delegstateid(ret); |
| 3325 | } |
| 3326 | |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3327 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 3328 | { |
| 3329 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 3330 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 3331 | } |
| 3332 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3333 | static __be32 |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 3334 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3335 | struct nfs4_delegation **dp) |
| 3336 | { |
| 3337 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3338 | __be32 status = nfserr_bad_stateid; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3339 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3340 | *dp = find_deleg_stateid(cl, &open->op_delegate_stateid); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3341 | if (*dp == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3342 | goto out; |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3343 | flags = share_access_to_flags(open->op_share_access); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3344 | status = nfs4_check_delegmode(*dp, flags); |
| 3345 | if (status) |
| 3346 | *dp = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3347 | out: |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3348 | if (!nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3349 | return nfs_ok; |
| 3350 | if (status) |
| 3351 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3352 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3353 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3354 | } |
| 3355 | |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3356 | static struct nfs4_ol_stateid * |
| 3357 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3358 | { |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3359 | struct nfs4_ol_stateid *local, *ret = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3360 | struct nfs4_openowner *oo = open->op_openowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3362 | spin_lock(&fp->fi_lock); |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 3363 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | /* ignore lock owners */ |
| 3365 | if (local->st_stateowner->so_is_open_owner == 0) |
| 3366 | continue; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3367 | if (local->st_stateowner == &oo->oo_owner) { |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3368 | ret = local; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3369 | break; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | } |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3372 | spin_unlock(&fp->fi_lock); |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3373 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3374 | } |
| 3375 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 3376 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 3377 | { |
| 3378 | int flags = 0; |
| 3379 | |
| 3380 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 3381 | flags |= NFSD_MAY_READ; |
| 3382 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 3383 | flags |= NFSD_MAY_WRITE; |
| 3384 | return flags; |
| 3385 | } |
| 3386 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3387 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 3389 | struct nfsd4_open *open) |
| 3390 | { |
| 3391 | struct iattr iattr = { |
| 3392 | .ia_valid = ATTR_SIZE, |
| 3393 | .ia_size = 0, |
| 3394 | }; |
| 3395 | if (!open->op_truncate) |
| 3396 | return 0; |
| 3397 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 3398 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); |
| 3400 | } |
| 3401 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3402 | static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 3403 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
| 3404 | struct nfsd4_open *open) |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3405 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3406 | struct file *filp = NULL; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3407 | __be32 status; |
| 3408 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 3409 | int access = nfs4_access_to_access(open->op_share_access); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3410 | unsigned char old_access_bmap, old_deny_bmap; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3411 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3412 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3413 | |
| 3414 | /* |
| 3415 | * Are we trying to set a deny mode that would conflict with |
| 3416 | * current access? |
| 3417 | */ |
| 3418 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 3419 | if (status != nfs_ok) { |
| 3420 | spin_unlock(&fp->fi_lock); |
| 3421 | goto out; |
| 3422 | } |
| 3423 | |
| 3424 | /* set access to the file */ |
| 3425 | status = nfs4_file_get_access(fp, open->op_share_access); |
| 3426 | if (status != nfs_ok) { |
| 3427 | spin_unlock(&fp->fi_lock); |
| 3428 | goto out; |
| 3429 | } |
| 3430 | |
| 3431 | /* Set access bits in stateid */ |
| 3432 | old_access_bmap = stp->st_access_bmap; |
| 3433 | set_access(open->op_share_access, stp); |
| 3434 | |
| 3435 | /* Set new deny mask */ |
| 3436 | old_deny_bmap = stp->st_deny_bmap; |
| 3437 | set_deny(open->op_share_deny, stp); |
| 3438 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 3439 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3440 | if (!fp->fi_fds[oflag]) { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3441 | spin_unlock(&fp->fi_lock); |
| 3442 | status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3443 | if (status) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3444 | goto out_put_access; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3445 | spin_lock(&fp->fi_lock); |
| 3446 | if (!fp->fi_fds[oflag]) { |
| 3447 | fp->fi_fds[oflag] = filp; |
| 3448 | filp = NULL; |
| 3449 | } |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3450 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3451 | spin_unlock(&fp->fi_lock); |
| 3452 | if (filp) |
| 3453 | fput(filp); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3454 | |
| 3455 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3456 | if (status) |
| 3457 | goto out_put_access; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3458 | out: |
| 3459 | return status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3460 | out_put_access: |
| 3461 | stp->st_access_bmap = old_access_bmap; |
| 3462 | nfs4_file_put_access(fp, open->op_share_access); |
| 3463 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); |
| 3464 | goto out; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3465 | } |
| 3466 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3467 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3468 | 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] | 3469 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3470 | __be32 status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3471 | unsigned char old_deny_bmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 3473 | if (!test_access(open->op_share_access, stp)) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3474 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3475 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3476 | /* test and set deny mode */ |
| 3477 | spin_lock(&fp->fi_lock); |
| 3478 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 3479 | if (status == nfs_ok) { |
| 3480 | old_deny_bmap = stp->st_deny_bmap; |
| 3481 | set_deny(open->op_share_deny, stp); |
| 3482 | fp->fi_share_deny |= |
| 3483 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 3484 | } |
| 3485 | spin_unlock(&fp->fi_lock); |
| 3486 | |
| 3487 | if (status != nfs_ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3488 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3489 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3490 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3491 | if (status != nfs_ok) |
| 3492 | reset_union_bmap_deny(old_deny_bmap, stp); |
| 3493 | return status; |
| 3494 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | static void |
J. Bruce Fields | 1255a8f | 2012-03-06 14:35:16 -0500 | [diff] [blame] | 3497 | nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | { |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3499 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3500 | } |
| 3501 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 3502 | /* Should we give out recallable state?: */ |
| 3503 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 3504 | { |
| 3505 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 3506 | return true; |
| 3507 | /* |
| 3508 | * In the sessions case, since we don't have to establish a |
| 3509 | * separate connection for callbacks, we assume it's OK |
| 3510 | * until we hear otherwise: |
| 3511 | */ |
| 3512 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 3513 | } |
| 3514 | |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 3515 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag) |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3516 | { |
| 3517 | struct file_lock *fl; |
| 3518 | |
| 3519 | fl = locks_alloc_lock(); |
| 3520 | if (!fl) |
| 3521 | return NULL; |
| 3522 | locks_init_lock(fl); |
| 3523 | fl->fl_lmops = &nfsd_lease_mng_ops; |
J. Bruce Fields | 617588d | 2011-07-01 15:18:34 -0400 | [diff] [blame] | 3524 | fl->fl_flags = FL_DELEG; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3525 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 3526 | fl->fl_end = OFFSET_MAX; |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 3527 | fl->fl_owner = (fl_owner_t)fp; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3528 | fl->fl_pid = current->tgid; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3529 | return fl; |
| 3530 | } |
| 3531 | |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3532 | static int nfs4_setlease(struct nfs4_delegation *dp) |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3533 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3534 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3535 | struct file_lock *fl; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3536 | struct file *filp; |
| 3537 | int status = 0; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3538 | |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 3539 | fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3540 | if (!fl) |
| 3541 | return -ENOMEM; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3542 | filp = find_readable_file(fp); |
| 3543 | if (!filp) { |
| 3544 | /* We should always have a readable file here */ |
| 3545 | WARN_ON_ONCE(1); |
| 3546 | return -EBADF; |
| 3547 | } |
| 3548 | fl->fl_file = filp; |
| 3549 | status = vfs_setlease(filp, fl->fl_type, &fl); |
| 3550 | if (status) { |
| 3551 | locks_free_lock(fl); |
| 3552 | goto out_fput; |
| 3553 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3554 | spin_lock(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3555 | spin_lock(&fp->fi_lock); |
| 3556 | /* Did the lease get broken before we took the lock? */ |
| 3557 | status = -EAGAIN; |
| 3558 | if (fp->fi_had_conflict) |
| 3559 | goto out_unlock; |
| 3560 | /* Race breaker */ |
| 3561 | if (fp->fi_lease) { |
| 3562 | status = 0; |
| 3563 | atomic_inc(&fp->fi_delegees); |
| 3564 | hash_delegation_locked(dp, fp); |
| 3565 | goto out_unlock; |
| 3566 | } |
| 3567 | fp->fi_lease = fl; |
| 3568 | fp->fi_deleg_file = filp; |
| 3569 | atomic_set(&fp->fi_delegees, 1); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 3570 | hash_delegation_locked(dp, fp); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3571 | spin_unlock(&fp->fi_lock); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3572 | spin_unlock(&state_lock); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3573 | return 0; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3574 | out_unlock: |
| 3575 | spin_unlock(&fp->fi_lock); |
| 3576 | spin_unlock(&state_lock); |
| 3577 | out_fput: |
| 3578 | fput(filp); |
J. Bruce Fields | e873088 | 2012-01-23 13:52:01 -0500 | [diff] [blame] | 3579 | return status; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3580 | } |
| 3581 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3582 | static struct nfs4_delegation * |
| 3583 | nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, |
| 3584 | struct nfs4_file *fp) |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3585 | { |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3586 | int status; |
| 3587 | struct nfs4_delegation *dp; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3588 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 3589 | if (fp->fi_had_conflict) |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3590 | return ERR_PTR(-EAGAIN); |
| 3591 | |
| 3592 | dp = alloc_init_deleg(clp, fh); |
| 3593 | if (!dp) |
| 3594 | return ERR_PTR(-ENOMEM); |
| 3595 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 3596 | get_nfs4_file(fp); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3597 | spin_lock(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3598 | spin_lock(&fp->fi_lock); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3599 | dp->dl_stid.sc_file = fp; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3600 | if (!fp->fi_lease) { |
| 3601 | spin_unlock(&fp->fi_lock); |
| 3602 | spin_unlock(&state_lock); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3603 | status = nfs4_setlease(dp); |
| 3604 | goto out; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3605 | } |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 3606 | atomic_inc(&fp->fi_delegees); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3607 | if (fp->fi_had_conflict) { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3608 | status = -EAGAIN; |
| 3609 | goto out_unlock; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3610 | } |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 3611 | hash_delegation_locked(dp, fp); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3612 | status = 0; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3613 | out_unlock: |
| 3614 | spin_unlock(&fp->fi_lock); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3615 | spin_unlock(&state_lock); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3616 | out: |
| 3617 | if (status) { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 3618 | nfs4_put_stid(&dp->dl_stid); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3619 | return ERR_PTR(status); |
| 3620 | } |
| 3621 | return dp; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3622 | } |
| 3623 | |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 3624 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 3625 | { |
| 3626 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3627 | if (status == -EAGAIN) |
| 3628 | open->op_why_no_deleg = WND4_CONTENTION; |
| 3629 | else { |
| 3630 | open->op_why_no_deleg = WND4_RESOURCE; |
| 3631 | switch (open->op_deleg_want) { |
| 3632 | case NFS4_SHARE_WANT_READ_DELEG: |
| 3633 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 3634 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 3635 | break; |
| 3636 | case NFS4_SHARE_WANT_CANCEL: |
| 3637 | open->op_why_no_deleg = WND4_CANCELLED; |
| 3638 | break; |
| 3639 | case NFS4_SHARE_WANT_NO_DELEG: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 3640 | WARN_ON_ONCE(1); |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 3641 | } |
| 3642 | } |
| 3643 | } |
| 3644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | /* |
| 3646 | * Attempt to hand out a delegation. |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3647 | * |
| 3648 | * Note we don't support write delegations, and won't until the vfs has |
| 3649 | * proper support for them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | */ |
| 3651 | static void |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 3652 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
| 3653 | struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | { |
| 3655 | struct nfs4_delegation *dp; |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 3656 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
| 3657 | struct nfs4_client *clp = stp->st_stid.sc_client; |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 3658 | int cb_up; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3659 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3661 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3662 | open->op_recall = 0; |
| 3663 | switch (open->op_claim_type) { |
| 3664 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 3665 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3666 | open->op_recall = 1; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3667 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 3668 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3669 | break; |
| 3670 | case NFS4_OPEN_CLAIM_NULL: |
Ming Chen | ed47b06 | 2014-01-09 21:26:10 +0000 | [diff] [blame] | 3671 | case NFS4_OPEN_CLAIM_FH: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3672 | /* |
| 3673 | * Let's not give out any delegations till everyone's |
| 3674 | * had the chance to reclaim theirs.... |
| 3675 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 3676 | if (locks_in_grace(clp->net)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3677 | goto out_no_deleg; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3678 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3679 | goto out_no_deleg; |
Steve Dickson | 9a0590a | 2013-05-15 14:51:49 -0400 | [diff] [blame] | 3680 | /* |
| 3681 | * Also, if the file was opened for write or |
| 3682 | * create, there's a good chance the client's |
| 3683 | * about to write to it, resulting in an |
| 3684 | * immediate recall (since we don't support |
| 3685 | * write delegations): |
| 3686 | */ |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3687 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3688 | goto out_no_deleg; |
| 3689 | if (open->op_create == NFS4_OPEN_CREATE) |
| 3690 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3691 | break; |
| 3692 | default: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3693 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3694 | } |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3695 | dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3696 | if (IS_ERR(dp)) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3697 | goto out_no_deleg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 3699 | 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] | 3700 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 3701 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 3702 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3703 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3704 | return; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3705 | out_no_deleg: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3706 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 3707 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 3708 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3709 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 3710 | open->op_recall = 1; |
| 3711 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3712 | |
| 3713 | /* 4.1 client asking for a delegation? */ |
| 3714 | if (open->op_deleg_want) |
| 3715 | nfsd4_open_deleg_none_ext(open, status); |
| 3716 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3717 | } |
| 3718 | |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 3719 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 3720 | struct nfs4_delegation *dp) |
| 3721 | { |
| 3722 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 3723 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 3724 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3725 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 3726 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 3727 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 3728 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3729 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 3730 | } |
| 3731 | /* Otherwise the client must be confused wanting a delegation |
| 3732 | * it already has, therefore we don't return |
| 3733 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 3734 | */ |
| 3735 | } |
| 3736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3737 | /* |
| 3738 | * called with nfs4_lock_state() held. |
| 3739 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3740 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3741 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 3742 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3743 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3744 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | struct nfs4_file *fp = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3746 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3747 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3748 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | /* |
| 3751 | * Lookup file; if found, lookup stateid and check open request, |
| 3752 | * and check for delegations in the process of being recalled. |
| 3753 | * If not found, create the nfs4_file struct |
| 3754 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 3755 | fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3756 | if (fp != open->op_file) { |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 3757 | status = nfs4_check_deleg(cl, open, &dp); |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3758 | if (status) |
| 3759 | goto out; |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3760 | stp = nfsd4_find_existing_open(fp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | } else { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3762 | open->op_file = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3763 | status = nfserr_bad_stateid; |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3764 | if (nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3765 | goto out; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 3766 | status = nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | /* |
| 3770 | * OPEN the file, or upgrade an existing OPEN. |
| 3771 | * If truncate fails, the OPEN fails. |
| 3772 | */ |
| 3773 | if (stp) { |
| 3774 | /* Stateid was found, this is an OPEN upgrade */ |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 3775 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | if (status) |
| 3777 | goto out; |
| 3778 | } else { |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3779 | stp = open->op_stp; |
| 3780 | open->op_stp = NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 3781 | init_open_stateid(stp, fp, open); |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 3782 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
| 3783 | if (status) { |
| 3784 | release_open_stateid(stp); |
| 3785 | goto out; |
| 3786 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | } |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3788 | update_stateid(&stp->st_stid.sc_stateid); |
| 3789 | memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3790 | |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3791 | if (nfsd4_has_session(&resp->cstate)) { |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3792 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 3793 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3794 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 3795 | goto nodeleg; |
| 3796 | } |
| 3797 | } |
| 3798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3799 | /* |
| 3800 | * Attempt to hand out a delegation. No error return, because the |
| 3801 | * OPEN succeeds even if we fail. |
| 3802 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 3803 | nfs4_open_delegation(current_fh, open, stp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3804 | nodeleg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3805 | status = nfs_ok; |
| 3806 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 3807 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3808 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3809 | out: |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3810 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 3811 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 3812 | open->op_deleg_want) |
| 3813 | nfsd4_deleg_xgrade_none_ext(open, dp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3814 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3815 | if (fp) |
| 3816 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 3817 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
J. Bruce Fields | 1255a8f | 2012-03-06 14:35:16 -0500 | [diff] [blame] | 3818 | nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | /* |
| 3820 | * To finish the open response, we just need to set the rflags. |
| 3821 | */ |
| 3822 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3823 | if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) && |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3824 | !nfsd4_has_session(&resp->cstate)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
| 3826 | |
| 3827 | return status; |
| 3828 | } |
| 3829 | |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 3830 | void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status) |
| 3831 | { |
| 3832 | if (open->op_openowner) { |
| 3833 | struct nfs4_openowner *oo = open->op_openowner; |
| 3834 | |
| 3835 | if (!list_empty(&oo->oo_owner.so_stateids)) |
| 3836 | list_del_init(&oo->oo_close_lru); |
| 3837 | if (oo->oo_flags & NFS4_OO_NEW) { |
| 3838 | if (status) { |
| 3839 | release_openowner(oo); |
| 3840 | open->op_openowner = NULL; |
| 3841 | } else |
| 3842 | oo->oo_flags &= ~NFS4_OO_NEW; |
| 3843 | } |
| 3844 | } |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3845 | if (open->op_file) |
| 3846 | nfsd4_free_file(open->op_file); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3847 | if (open->op_stp) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 3848 | nfs4_put_stid(&open->op_stp->st_stid); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 3849 | } |
| 3850 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3851 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3852 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3853 | clientid_t *clid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3854 | { |
| 3855 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3856 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 3857 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | |
| 3859 | nfs4_lock_state(); |
| 3860 | dprintk("process_renew(%08x/%08x): starting\n", |
| 3861 | clid->cl_boot, clid->cl_id); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3862 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 3863 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3864 | goto out; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3865 | clp = cstate->clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3866 | status = nfserr_cb_path_down; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 3867 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 3868 | && clp->cl_cb_state != NFSD4_CB_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3869 | goto out; |
| 3870 | status = nfs_ok; |
| 3871 | out: |
| 3872 | nfs4_unlock_state(); |
| 3873 | return status; |
| 3874 | } |
| 3875 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3876 | static void |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 3877 | nfsd4_end_grace(struct nfsd_net *nn) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3878 | { |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 3879 | /* do nothing if grace period already ended */ |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 3880 | if (nn->grace_ended) |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 3881 | return; |
| 3882 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3883 | dprintk("NFSD: end of grace period\n"); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 3884 | nn->grace_ended = true; |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 3885 | nfsd4_record_grace_done(nn, nn->boot_time); |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 3886 | locks_end_grace(&nn->nfsd4_manager); |
J. Bruce Fields | e46b498 | 2010-03-01 19:21:21 -0500 | [diff] [blame] | 3887 | /* |
| 3888 | * Now that every NFSv4 client has had the chance to recover and |
| 3889 | * to see the (possibly new, possibly shorter) lease time, we |
| 3890 | * can safely set the next grace time to the current lease time: |
| 3891 | */ |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 3892 | nn->nfsd4_grace = nn->nfsd4_lease; |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3893 | } |
| 3894 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 3895 | static time_t |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3896 | nfs4_laundromat(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3897 | { |
| 3898 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3899 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | struct nfs4_delegation *dp; |
| 3901 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 3d73371 | 2012-11-27 14:11:44 +0300 | [diff] [blame] | 3902 | time_t cutoff = get_seconds() - nn->nfsd4_lease; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3903 | time_t t, new_timeo = nn->nfsd4_lease; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3904 | |
| 3905 | nfs4_lock_state(); |
| 3906 | |
| 3907 | dprintk("NFSD: laundromat service - starting\n"); |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 3908 | nfsd4_end_grace(nn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3909 | INIT_LIST_HEAD(&reaplist); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3910 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 3911 | list_for_each_safe(pos, next, &nn->client_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3912 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
| 3913 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { |
| 3914 | t = clp->cl_time - cutoff; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3915 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | break; |
| 3917 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3918 | if (mark_client_expired_locked(clp)) { |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 3919 | dprintk("NFSD: client in use (clientid %08x)\n", |
| 3920 | clp->cl_clientid.cl_id); |
| 3921 | continue; |
| 3922 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3923 | list_move(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3924 | } |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3925 | spin_unlock(&nn->client_lock); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3926 | list_for_each_safe(pos, next, &reaplist) { |
| 3927 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3928 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 3929 | clp->cl_clientid.cl_id); |
| 3930 | expire_client(clp); |
| 3931 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3932 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 3933 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3934 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Stanislav Kinsbursky | 4e37a7c2 | 2012-11-26 15:22:03 +0300 | [diff] [blame] | 3935 | if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn) |
| 3936 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3937 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3938 | t = dp->dl_time - cutoff; |
| 3939 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | break; |
| 3941 | } |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 3942 | unhash_delegation_locked(dp); |
| 3943 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3945 | spin_unlock(&state_lock); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 3946 | while (!list_empty(&reaplist)) { |
| 3947 | dp = list_first_entry(&reaplist, struct nfs4_delegation, |
| 3948 | dl_recall_lru); |
| 3949 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3950 | revoke_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3951 | } |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3952 | list_for_each_safe(pos, next, &nn->close_lru) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3953 | oo = container_of(pos, struct nfs4_openowner, oo_close_lru); |
| 3954 | if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3955 | t = oo->oo_time - cutoff; |
| 3956 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 | break; |
| 3958 | } |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3959 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | } |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3961 | new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3962 | nfs4_unlock_state(); |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3963 | return new_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3964 | } |
| 3965 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 3966 | static struct workqueue_struct *laundry_wq; |
| 3967 | static void laundromat_main(struct work_struct *); |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 3968 | |
| 3969 | static void |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3970 | laundromat_main(struct work_struct *laundry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | { |
| 3972 | time_t t; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3973 | struct delayed_work *dwork = container_of(laundry, struct delayed_work, |
| 3974 | work); |
| 3975 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 3976 | laundromat_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3977 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3978 | t = nfs4_laundromat(nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3980 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3981 | } |
| 3982 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3983 | 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] | 3984 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3985 | if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle)) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3986 | return nfserr_bad_stateid; |
| 3987 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3988 | } |
| 3989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3990 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3991 | access_permit_read(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3992 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3993 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 3994 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 3995 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3996 | } |
| 3997 | |
| 3998 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3999 | access_permit_write(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4000 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4001 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 4002 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4006 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4008 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4009 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 4010 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 4011 | if (stp->st_openstp) |
| 4012 | stp = stp->st_openstp; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4013 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | goto out; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4015 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | goto out; |
| 4017 | status = nfs_ok; |
| 4018 | out: |
| 4019 | return status; |
| 4020 | } |
| 4021 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4022 | static inline __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4023 | 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] | 4024 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4025 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4026 | return nfs_ok; |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4027 | else if (locks_in_grace(net)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4028 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4029 | * conflicting state; so we must wait out the grace period. */ |
| 4030 | return nfserr_grace; |
| 4031 | } else if (flags & WR_STATE) |
| 4032 | return nfs4_share_conflict(current_fh, |
| 4033 | NFS4_SHARE_DENY_WRITE); |
| 4034 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 4035 | return nfs4_share_conflict(current_fh, |
| 4036 | NFS4_SHARE_DENY_READ); |
| 4037 | } |
| 4038 | |
| 4039 | /* |
| 4040 | * Allow READ/WRITE during grace period on recovered state only for files |
| 4041 | * that are not able to provide mandatory locking. |
| 4042 | */ |
| 4043 | static inline int |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4044 | grace_disallows_io(struct net *net, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4045 | { |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4046 | return locks_in_grace(net) && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4047 | } |
| 4048 | |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4049 | /* Returns true iff a is later than b: */ |
| 4050 | static bool stateid_generation_after(stateid_t *a, stateid_t *b) |
| 4051 | { |
Jim Rees | 1a9357f | 2013-05-17 17:33:00 -0400 | [diff] [blame] | 4052 | return (s32)(a->si_generation - b->si_generation) > 0; |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4053 | } |
| 4054 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 4055 | 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] | 4056 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4057 | /* |
| 4058 | * When sessions are used the stateid generation number is ignored |
| 4059 | * when it is zero. |
| 4060 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 4061 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4062 | return nfs_ok; |
| 4063 | |
| 4064 | if (in->si_generation == ref->si_generation) |
| 4065 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4066 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4067 | /* 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] | 4068 | if (stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4069 | return nfserr_bad_stateid; |
| 4070 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4071 | * However, we could see a stateid from the past, even from a |
| 4072 | * non-buggy client. For example, if the client sends a lock |
| 4073 | * while some IO is outstanding, the lock may bump si_generation |
| 4074 | * while the IO is still in flight. The client could avoid that |
| 4075 | * situation by waiting for responses on all the IO requests, |
| 4076 | * but better performance may result in retrying IO that |
| 4077 | * receives an old_stateid error if requests are rarely |
| 4078 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4079 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4080 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4081 | } |
| 4082 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4083 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4084 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4085 | struct nfs4_stid *s; |
| 4086 | struct nfs4_ol_stateid *ols; |
| 4087 | __be32 status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4088 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4089 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 4090 | return nfserr_bad_stateid; |
| 4091 | /* Client debugging aid. */ |
| 4092 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { |
| 4093 | char addr_str[INET6_ADDRSTRLEN]; |
| 4094 | rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, |
| 4095 | sizeof(addr_str)); |
| 4096 | pr_warn_ratelimited("NFSD: client %s testing state ID " |
| 4097 | "with incorrect client ID\n", addr_str); |
| 4098 | return nfserr_bad_stateid; |
| 4099 | } |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4100 | s = find_stateid(cl, stateid); |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4101 | if (!s) |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4102 | return nfserr_bad_stateid; |
J. Bruce Fields | 36279ac | 2011-09-26 12:53:00 -0400 | [diff] [blame] | 4103 | status = check_stateid_generation(stateid, &s->sc_stateid, 1); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4104 | if (status) |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4105 | return status; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4106 | switch (s->sc_type) { |
| 4107 | case NFS4_DELEG_STID: |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4108 | return nfs_ok; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4109 | case NFS4_REVOKED_DELEG_STID: |
| 4110 | return nfserr_deleg_revoked; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4111 | case NFS4_OPEN_STID: |
| 4112 | case NFS4_LOCK_STID: |
| 4113 | ols = openlockstateid(s); |
| 4114 | if (ols->st_stateowner->so_is_open_owner |
| 4115 | && !(openowner(ols->st_stateowner)->oo_flags |
| 4116 | & NFS4_OO_CONFIRMED)) |
| 4117 | return nfserr_bad_stateid; |
| 4118 | return nfs_ok; |
| 4119 | default: |
| 4120 | printk("unknown stateid type %x\n", s->sc_type); |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 4121 | /* Fallthrough */ |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4122 | case NFS4_CLOSED_STID: |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 4123 | case NFS4_CLOSED_DELEG_STID: |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4124 | return nfserr_bad_stateid; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4125 | } |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4126 | } |
| 4127 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4128 | static __be32 |
| 4129 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
| 4130 | stateid_t *stateid, unsigned char typemask, |
| 4131 | struct nfs4_stid **s, struct nfsd_net *nn) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4132 | { |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 4133 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4134 | |
| 4135 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 4136 | return nfserr_bad_stateid; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4137 | status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn); |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4138 | if (status == nfserr_stale_clientid) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4139 | if (cstate->session) |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4140 | return nfserr_bad_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4141 | return nfserr_stale_stateid; |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4142 | } |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 4143 | if (status) |
| 4144 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4145 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4146 | if (!*s) |
| 4147 | return nfserr_bad_stateid; |
| 4148 | return nfs_ok; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4149 | } |
| 4150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | /* |
| 4152 | * Checks for stateid operations |
| 4153 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4154 | __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4155 | nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate, |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4156 | stateid_t *stateid, int flags, struct file **filpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | { |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4158 | struct nfs4_stid *s; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4159 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | struct nfs4_delegation *dp = NULL; |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4161 | struct svc_fh *current_fh = &cstate->current_fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4162 | struct inode *ino = current_fh->fh_dentry->d_inode; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4163 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4164 | struct file *file = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4165 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | if (filpp) |
| 4168 | *filpp = NULL; |
| 4169 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4170 | if (grace_disallows_io(net, ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | return nfserr_grace; |
| 4172 | |
| 4173 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4174 | return check_special_stateids(net, current_fh, stateid, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4175 | |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4176 | nfs4_lock_state(); |
| 4177 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4178 | status = nfsd4_lookup_stateid(cstate, stateid, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4179 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4180 | &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4181 | if (status) |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4182 | goto out; |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4183 | status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate)); |
| 4184 | if (status) |
| 4185 | goto out; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4186 | switch (s->sc_type) { |
| 4187 | case NFS4_DELEG_STID: |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4188 | dp = delegstateid(s); |
J. Bruce Fields | dc9bf70 | 2009-02-21 11:14:43 -0800 | [diff] [blame] | 4189 | status = nfs4_check_delegmode(dp, flags); |
| 4190 | if (status) |
| 4191 | goto out; |
Dan Carpenter | 43b0178 | 2010-10-27 23:19:04 +0200 | [diff] [blame] | 4192 | if (filpp) { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4193 | file = dp->dl_stid.sc_file->fi_deleg_file; |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4194 | if (!file) { |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4195 | WARN_ON_ONCE(1); |
| 4196 | status = nfserr_serverfault; |
| 4197 | goto out; |
| 4198 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4199 | get_file(file); |
Dan Carpenter | 43b0178 | 2010-10-27 23:19:04 +0200 | [diff] [blame] | 4200 | } |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4201 | break; |
| 4202 | case NFS4_OPEN_STID: |
| 4203 | case NFS4_LOCK_STID: |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4204 | stp = openlockstateid(s); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4205 | status = nfs4_check_fh(current_fh, stp); |
| 4206 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4208 | if (stp->st_stateowner->so_is_open_owner |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4209 | && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | goto out; |
J. Bruce Fields | a4455be | 2009-02-21 10:40:22 -0800 | [diff] [blame] | 4211 | status = nfs4_check_openmode(stp, flags); |
| 4212 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | goto out; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4214 | if (filpp) { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4215 | struct nfs4_file *fp = stp->st_stid.sc_file; |
| 4216 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4217 | if (flags & RD_STATE) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4218 | file = find_readable_file(fp); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4219 | else |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4220 | file = find_writeable_file(fp); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4221 | } |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4222 | break; |
| 4223 | default: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4224 | status = nfserr_bad_stateid; |
| 4225 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | } |
| 4227 | status = nfs_ok; |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4228 | if (file) |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4229 | *filpp = file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | out: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4231 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4232 | return status; |
| 4233 | } |
| 4234 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4235 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4236 | nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4237 | { |
J. Bruce Fields | a1b8ff4 | 2014-05-20 15:55:21 -0400 | [diff] [blame] | 4238 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
| 4239 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4240 | if (check_for_locks(stp->st_stid.sc_file, lo)) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4241 | return nfserr_locks_held; |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4242 | release_lockowner_if_empty(lo); |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4243 | return nfs_ok; |
| 4244 | } |
| 4245 | |
| 4246 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4247 | * Test if the stateid is valid |
| 4248 | */ |
| 4249 | __be32 |
| 4250 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4251 | struct nfsd4_test_stateid *test_stateid) |
| 4252 | { |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4253 | struct nfsd4_test_stateid_id *stateid; |
| 4254 | struct nfs4_client *cl = cstate->session->se_client; |
| 4255 | |
| 4256 | nfs4_lock_state(); |
| 4257 | 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] | 4258 | stateid->ts_id_status = |
| 4259 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4260 | nfs4_unlock_state(); |
| 4261 | |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4262 | return nfs_ok; |
| 4263 | } |
| 4264 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4265 | __be32 |
| 4266 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4267 | struct nfsd4_free_stateid *free_stateid) |
| 4268 | { |
| 4269 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4270 | struct nfs4_stid *s; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4271 | struct nfs4_delegation *dp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4272 | struct nfs4_client *cl = cstate->session->se_client; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4273 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4274 | |
| 4275 | nfs4_lock_state(); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4276 | s = find_stateid(cl, stateid); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4277 | if (!s) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4278 | goto out; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4279 | switch (s->sc_type) { |
| 4280 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4281 | ret = nfserr_locks_held; |
| 4282 | goto out; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4283 | case NFS4_OPEN_STID: |
| 4284 | case NFS4_LOCK_STID: |
| 4285 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 4286 | if (ret) |
| 4287 | goto out; |
| 4288 | if (s->sc_type == NFS4_LOCK_STID) |
| 4289 | ret = nfsd4_free_lock_stateid(openlockstateid(s)); |
| 4290 | else |
| 4291 | ret = nfserr_locks_held; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4292 | break; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4293 | case NFS4_REVOKED_DELEG_STID: |
| 4294 | dp = delegstateid(s); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 4295 | spin_lock(&cl->cl_lock); |
| 4296 | list_del_init(&dp->dl_recall_lru); |
| 4297 | spin_unlock(&cl->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 4298 | nfs4_put_stid(s); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4299 | ret = nfs_ok; |
| 4300 | break; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4301 | default: |
| 4302 | ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4303 | } |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4304 | out: |
| 4305 | nfs4_unlock_state(); |
| 4306 | return ret; |
| 4307 | } |
| 4308 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4309 | static inline int |
| 4310 | setlkflg (int type) |
| 4311 | { |
| 4312 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 4313 | RD_STATE : WR_STATE; |
| 4314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4316 | 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] | 4317 | { |
| 4318 | struct svc_fh *current_fh = &cstate->current_fh; |
| 4319 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 4320 | __be32 status; |
| 4321 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4322 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 4323 | if (status) |
| 4324 | return status; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4325 | if (stp->st_stid.sc_type == NFS4_CLOSED_STID |
| 4326 | || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4327 | /* |
| 4328 | * "Closed" stateid's exist *only* to return |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4329 | * nfserr_replay_me from the previous step, and |
| 4330 | * revoked delegations are kept only for free_stateid. |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4331 | */ |
| 4332 | return nfserr_bad_stateid; |
| 4333 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); |
| 4334 | if (status) |
| 4335 | return status; |
| 4336 | return nfs4_check_fh(current_fh, stp); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4337 | } |
| 4338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | /* |
| 4340 | * Checks for sequence id mutating operations. |
| 4341 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4342 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4343 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 4344 | stateid_t *stateid, char typemask, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4345 | struct nfs4_ol_stateid **stpp, |
| 4346 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4348 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4349 | struct nfs4_stid *s; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4350 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4351 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4352 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
| 4353 | seqid, STATEID_VAL(stateid)); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | *stpp = NULL; |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4356 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4357 | if (status) |
| 4358 | return status; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4359 | stp = openlockstateid(s); |
J. Bruce Fields | 3d74e6a | 2013-03-22 17:44:19 -0400 | [diff] [blame] | 4360 | if (!nfsd4_has_session(cstate)) |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4361 | cstate->replay_owner = stp->st_stateowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4363 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
| 4364 | if (!status) |
| 4365 | *stpp = stp; |
| 4366 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4367 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 4368 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4369 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 4370 | 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] | 4371 | { |
| 4372 | __be32 status; |
| 4373 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4375 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4376 | NFS4_OPEN_STID, stpp, nn); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 4377 | if (status) |
| 4378 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4379 | oo = openowner((*stpp)->st_stateowner); |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4380 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4381 | return nfserr_bad_stateid; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4382 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | } |
| 4384 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4385 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4386 | 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] | 4387 | struct nfsd4_open_confirm *oc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4389 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4390 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4391 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4392 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4393 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4394 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 4395 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4397 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 4398 | if (status) |
| 4399 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 | |
| 4401 | nfs4_lock_state(); |
| 4402 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4403 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4404 | oc->oc_seqid, &oc->oc_req_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4405 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4406 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4407 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4408 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4409 | status = nfserr_bad_stateid; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4410 | if (oo->oo_flags & NFS4_OO_CONFIRMED) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4411 | goto out; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4412 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4413 | update_stateid(&stp->st_stid.sc_stateid); |
| 4414 | 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] | 4415 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4416 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 4417 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 4418 | nfsd4_client_record_create(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4419 | status = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4421 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4422 | if (!cstate->replay_owner) |
| 4423 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4424 | return status; |
| 4425 | } |
| 4426 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4427 | 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] | 4428 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4429 | if (!test_access(access, stp)) |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4430 | return; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4431 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4432 | clear_access(access, stp); |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4433 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 4434 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4435 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 4436 | { |
| 4437 | switch (to_access) { |
| 4438 | case NFS4_SHARE_ACCESS_READ: |
| 4439 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 4440 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 4441 | break; |
| 4442 | case NFS4_SHARE_ACCESS_WRITE: |
| 4443 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 4444 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 4445 | break; |
| 4446 | case NFS4_SHARE_ACCESS_BOTH: |
| 4447 | break; |
| 4448 | default: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4449 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | } |
| 4451 | } |
| 4452 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4453 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4454 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
| 4455 | struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4456 | struct nfsd4_open_downgrade *od) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4458 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4459 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4460 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4462 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 4463 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | |
J. Bruce Fields | c30e92d | 2011-10-10 17:34:31 -0400 | [diff] [blame] | 4465 | /* We don't yet support WANT bits: */ |
Benny Halevy | 2c8bd7e | 2012-02-16 20:57:09 +0200 | [diff] [blame] | 4466 | if (od->od_deleg_want) |
| 4467 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 4468 | od->od_deleg_want); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | |
| 4470 | nfs4_lock_state(); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4471 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4472 | &od->od_stateid, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4473 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | status = nfserr_inval; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4476 | if (!test_access(od->od_share_access, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 4477 | dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | stp->st_access_bmap, od->od_share_access); |
| 4479 | goto out; |
| 4480 | } |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4481 | if (!test_deny(od->od_share_deny, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 4482 | dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4483 | stp->st_deny_bmap, od->od_share_deny); |
| 4484 | goto out; |
| 4485 | } |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4486 | nfs4_stateid_downgrade(stp, od->od_share_access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4488 | reset_union_bmap_deny(od->od_share_deny, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4489 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4490 | update_stateid(&stp->st_stid.sc_stateid); |
| 4491 | memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | status = nfs_ok; |
| 4493 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4494 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4495 | if (!cstate->replay_owner) |
| 4496 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | return status; |
| 4498 | } |
| 4499 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4500 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 4501 | { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4502 | struct nfs4_client *clp = s->st_stid.sc_client; |
| 4503 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 4504 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4505 | s->st_stid.sc_type = NFS4_CLOSED_STID; |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4506 | unhash_open_stateid(s); |
| 4507 | |
| 4508 | if (clp->cl_minorversion) { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4509 | if (list_empty(&oo->oo_owner.so_stateids)) |
| 4510 | release_openowner(oo); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 4511 | nfs4_put_stid(&s->st_stid); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4512 | } else { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 4513 | /* |
| 4514 | * In the 4.0 case we need to keep the owners around a |
| 4515 | * little while to handle CLOSE replay. We still do need |
| 4516 | * to release any file access that is held by them |
| 4517 | * before returning however. |
| 4518 | */ |
| 4519 | release_all_access(s); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4520 | if (s->st_stid.sc_file) { |
| 4521 | put_nfs4_file(s->st_stid.sc_file); |
| 4522 | s->st_stid.sc_file = NULL; |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 4523 | } |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4524 | oo->oo_last_closed_stid = s; |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4525 | if (list_empty(&oo->oo_owner.so_stateids)) |
| 4526 | move_to_close_lru(oo, clp->net); |
| 4527 | } |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 4528 | } |
| 4529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | /* |
| 4531 | * nfs4_unlock_state() called after encode |
| 4532 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4533 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4534 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4535 | struct nfsd4_close *close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4537 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4538 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4539 | struct net *net = SVC_NET(rqstp); |
| 4540 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4542 | dprintk("NFSD: nfsd4_close on file %pd\n", |
| 4543 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | |
| 4545 | nfs4_lock_state(); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4546 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 4547 | &close->cl_stateid, |
| 4548 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4549 | &stp, nn); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4550 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4551 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | goto out; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4553 | update_stateid(&stp->st_stid.sc_stateid); |
| 4554 | memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4555 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4556 | nfsd4_close_open_stateid(stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | out: |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4558 | if (!cstate->replay_owner) |
| 4559 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | return status; |
| 4561 | } |
| 4562 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4563 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4564 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4565 | struct nfsd4_delegreturn *dr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4567 | struct nfs4_delegation *dp; |
| 4568 | stateid_t *stateid = &dr->dr_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4569 | struct nfs4_stid *s; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4570 | __be32 status; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4571 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4573 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4574 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4575 | |
| 4576 | nfs4_lock_state(); |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4577 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4578 | if (status) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4579 | goto out; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4580 | dp = delegstateid(s); |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 4581 | 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] | 4582 | if (status) |
| 4583 | goto out; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4584 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4585 | destroy_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | out: |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4587 | nfs4_unlock_state(); |
| 4588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | return status; |
| 4590 | } |
| 4591 | |
| 4592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4595 | static inline u64 |
| 4596 | end_offset(u64 start, u64 len) |
| 4597 | { |
| 4598 | u64 end; |
| 4599 | |
| 4600 | end = start + len; |
| 4601 | return end >= start ? end: NFS4_MAX_UINT64; |
| 4602 | } |
| 4603 | |
| 4604 | /* last octet in a range */ |
| 4605 | static inline u64 |
| 4606 | last_byte_offset(u64 start, u64 len) |
| 4607 | { |
| 4608 | u64 end; |
| 4609 | |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4610 | WARN_ON_ONCE(!len); |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4611 | end = start + len; |
| 4612 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 4613 | } |
| 4614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | /* |
| 4616 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 4617 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 4618 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 4619 | * locking, this prevents us from being completely protocol-compliant. The |
| 4620 | * real solution to this problem is to start using unsigned file offsets in |
| 4621 | * the VFS, but this is a very deep change! |
| 4622 | */ |
| 4623 | static inline void |
| 4624 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 4625 | { |
| 4626 | if (lock->fl_start < 0) |
| 4627 | lock->fl_start = OFFSET_MAX; |
| 4628 | if (lock->fl_end < 0) |
| 4629 | lock->fl_end = OFFSET_MAX; |
| 4630 | } |
| 4631 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4632 | /* Hack!: For now, we're defining this just so we can use a pointer to it |
| 4633 | * as a unique cookie to identify our (NFSv4's) posix locks. */ |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 4634 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4635 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | |
| 4637 | static inline void |
| 4638 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 4639 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4640 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4641 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4642 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4643 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
| 4644 | deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data, |
| 4645 | lo->lo_owner.so_owner.len, GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 4646 | if (!deny->ld_owner.data) |
| 4647 | /* We just don't care that much */ |
| 4648 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4649 | deny->ld_owner.len = lo->lo_owner.so_owner.len; |
| 4650 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4651 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 4652 | nevermind: |
| 4653 | deny->ld_owner.len = 0; |
| 4654 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4655 | deny->ld_clientid.cl_boot = 0; |
| 4656 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | } |
| 4658 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4659 | deny->ld_length = NFS4_MAX_UINT64; |
| 4660 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 4662 | deny->ld_type = NFS4_READ_LT; |
| 4663 | if (fl->fl_type != F_RDLCK) |
| 4664 | deny->ld_type = NFS4_WRITE_LT; |
| 4665 | } |
| 4666 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4667 | static struct nfs4_lockowner * |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4668 | find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner, |
| 4669 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4670 | { |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4671 | unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner); |
| 4672 | struct nfs4_stateowner *so; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4674 | list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) { |
| 4675 | if (so->so_is_open_owner) |
| 4676 | continue; |
| 4677 | if (!same_owner_str(so, owner, clid)) |
| 4678 | continue; |
| 4679 | return lockowner(so); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | } |
| 4681 | return NULL; |
| 4682 | } |
| 4683 | |
| 4684 | /* |
| 4685 | * Alloc a lock owner structure. |
| 4686 | * 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] | 4687 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | * |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 4689 | * strhashval = ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4691 | static struct nfs4_lockowner * |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4692 | 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] | 4693 | struct nfs4_lockowner *lo; |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4694 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4695 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4696 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 4697 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4698 | return NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4699 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 4700 | lo->lo_owner.so_is_open_owner = 0; |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 4701 | /* It is the openowner seqid that will be incremented in encode in the |
| 4702 | * case of new lockowners; so increment the lock seqid manually: */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4703 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1; |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4704 | list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4705 | return lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4706 | } |
| 4707 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4708 | static struct nfs4_ol_stateid * |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 4709 | alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, |
| 4710 | struct inode *inode, |
| 4711 | struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | { |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame^] | 4713 | struct nfs4_stid *s; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4714 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 4715 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4716 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame^] | 4717 | s = nfs4_alloc_stid(clp, stateid_slab); |
| 4718 | if (s == NULL) |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 4719 | return NULL; |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame^] | 4720 | stp = openlockstateid(s); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 4721 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4722 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
| 4723 | stp->st_stateowner = &lo->lo_owner; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4724 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4725 | stp->st_stid.sc_file = fp; |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame^] | 4726 | stp->st_stid.sc_free = nfs4_free_lock_stateid; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4727 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4729 | stp->st_openstp = open_stp; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4730 | list_add(&stp->st_locks, &open_stp->st_locks); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4731 | spin_lock(&fp->fi_lock); |
| 4732 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 4733 | spin_unlock(&fp->fi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | return stp; |
| 4735 | } |
| 4736 | |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4737 | static struct nfs4_ol_stateid * |
| 4738 | find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) |
| 4739 | { |
| 4740 | struct nfs4_ol_stateid *lst; |
| 4741 | |
| 4742 | list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4743 | if (lst->st_stid.sc_file == fp) |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4744 | return lst; |
| 4745 | } |
| 4746 | return NULL; |
| 4747 | } |
| 4748 | |
| 4749 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 4750 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4751 | check_lock_length(u64 offset, u64 length) |
| 4752 | { |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4753 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | LOFF_OVERFLOW(offset, length))); |
| 4755 | } |
| 4756 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4757 | 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] | 4758 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4759 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4760 | |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 4761 | lockdep_assert_held(&fp->fi_lock); |
| 4762 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4763 | if (test_access(access, lock_stp)) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4764 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 4765 | __nfs4_file_get_access(fp, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4766 | set_access(access, lock_stp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4767 | } |
| 4768 | |
J. Bruce Fields | 2355c59 | 2012-04-25 16:56:22 -0400 | [diff] [blame] | 4769 | 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] | 4770 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4771 | struct nfs4_file *fi = ost->st_stid.sc_file; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4772 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 4773 | struct nfs4_client *cl = oo->oo_owner.so_client; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 4774 | struct inode *inode = cstate->current_fh.fh_dentry->d_inode; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4775 | struct nfs4_lockowner *lo; |
| 4776 | unsigned int strhashval; |
Stanislav Kinsbursky | 20e9e2b | 2012-11-14 18:21:46 +0300 | [diff] [blame] | 4777 | struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4778 | |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4779 | lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4780 | if (!lo) { |
| 4781 | strhashval = ownerstr_hashval(cl->cl_clientid.cl_id, |
| 4782 | &lock->v.new.owner); |
| 4783 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 4784 | if (lo == NULL) |
| 4785 | return nfserr_jukebox; |
| 4786 | } else { |
| 4787 | /* with an existing lockowner, seqids must be the same */ |
| 4788 | if (!cstate->minorversion && |
| 4789 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4790 | return nfserr_bad_seqid; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4791 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4792 | |
| 4793 | *lst = find_lock_stateid(lo, fi); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4794 | if (*lst == NULL) { |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 4795 | *lst = alloc_init_lock_stateid(lo, fi, inode, ost); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4796 | if (*lst == NULL) { |
| 4797 | release_lockowner_if_empty(lo); |
| 4798 | return nfserr_jukebox; |
| 4799 | } |
| 4800 | *new = true; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4801 | } |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4802 | return nfs_ok; |
| 4803 | } |
| 4804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | /* |
| 4806 | * LOCK operation |
| 4807 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4808 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4809 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4810 | struct nfsd4_lock *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4812 | struct nfs4_openowner *open_sop = NULL; |
| 4813 | struct nfs4_lockowner *lock_sop = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4814 | struct nfs4_ol_stateid *lock_stp; |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 4815 | struct nfs4_file *fp; |
J. Bruce Fields | 7d94784 | 2010-08-20 18:09:31 -0400 | [diff] [blame] | 4816 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4817 | struct file_lock *file_lock = NULL; |
| 4818 | struct file_lock *conflock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4819 | __be32 status = 0; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4820 | bool new_state = false; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4821 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4822 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4823 | struct net *net = SVC_NET(rqstp); |
| 4824 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | |
| 4826 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 4827 | (long long) lock->lk_offset, |
| 4828 | (long long) lock->lk_length); |
| 4829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4830 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 4831 | return nfserr_inval; |
| 4832 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4833 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 4834 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 4835 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 4836 | return status; |
| 4837 | } |
| 4838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | nfs4_lock_state(); |
| 4840 | |
| 4841 | if (lock->lk_is_new) { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4842 | struct nfs4_ol_stateid *open_stp = NULL; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 4843 | |
| 4844 | if (nfsd4_has_session(cstate)) |
| 4845 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
| 4846 | memcpy(&lock->v.new.clientid, |
| 4847 | &cstate->session->se_client->cl_clientid, |
| 4848 | sizeof(clientid_t)); |
| 4849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4850 | status = nfserr_stale_clientid; |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 4851 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4854 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4855 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | lock->lk_new_open_seqid, |
| 4857 | &lock->lk_new_open_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4858 | &open_stp, nn); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 4859 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4860 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4861 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4862 | status = nfserr_bad_stateid; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 4863 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4864 | &lock->v.new.clientid)) |
| 4865 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4866 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
| 4867 | &lock_stp, &new_state); |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 4868 | } else |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4869 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4870 | lock->lk_old_lock_seqid, |
| 4871 | &lock->lk_old_lock_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4872 | NFS4_LOCK_STID, &lock_stp, nn); |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 4873 | if (status) |
| 4874 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4875 | lock_sop = lockowner(lock_stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4877 | lkflg = setlkflg(lock->lk_type); |
| 4878 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 4879 | if (status) |
| 4880 | goto out; |
| 4881 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 4882 | status = nfserr_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4883 | if (locks_in_grace(net) && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 4884 | goto out; |
| 4885 | status = nfserr_no_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4886 | if (!locks_in_grace(net) && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 4887 | goto out; |
| 4888 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4889 | file_lock = locks_alloc_lock(); |
| 4890 | if (!file_lock) { |
| 4891 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 4892 | status = nfserr_jukebox; |
| 4893 | goto out; |
| 4894 | } |
| 4895 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4896 | fp = lock_stp->st_stid.sc_file; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4897 | locks_init_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | switch (lock->lk_type) { |
| 4899 | case NFS4_READ_LT: |
| 4900 | case NFS4_READW_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 4901 | spin_lock(&fp->fi_lock); |
| 4902 | filp = find_readable_file_locked(fp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4903 | if (filp) |
| 4904 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 4905 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4906 | file_lock->fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 4907 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4908 | case NFS4_WRITE_LT: |
| 4909 | case NFS4_WRITEW_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 4910 | spin_lock(&fp->fi_lock); |
| 4911 | filp = find_writeable_file_locked(fp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4912 | if (filp) |
| 4913 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 4914 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4915 | file_lock->fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 4916 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4917 | default: |
| 4918 | status = nfserr_inval; |
| 4919 | goto out; |
| 4920 | } |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4921 | if (!filp) { |
| 4922 | status = nfserr_openmode; |
| 4923 | goto out; |
| 4924 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4925 | file_lock->fl_owner = (fl_owner_t)lock_sop; |
| 4926 | file_lock->fl_pid = current->tgid; |
| 4927 | file_lock->fl_file = filp; |
| 4928 | file_lock->fl_flags = FL_POSIX; |
| 4929 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 4930 | file_lock->fl_start = lock->lk_offset; |
| 4931 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 4932 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4934 | conflock = locks_alloc_lock(); |
| 4935 | if (!conflock) { |
| 4936 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 4937 | status = nfserr_jukebox; |
| 4938 | goto out; |
| 4939 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4941 | err = vfs_lock_file(filp, F_SETLK, file_lock, conflock); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4942 | switch (-err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4943 | case 0: /* success! */ |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4944 | update_stateid(&lock_stp->st_stid.sc_stateid); |
| 4945 | memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | sizeof(stateid_t)); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4947 | status = 0; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4948 | break; |
| 4949 | case (EAGAIN): /* conflock holds conflicting lock */ |
| 4950 | status = nfserr_denied; |
| 4951 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4952 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4953 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | case (EDEADLK): |
| 4955 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4956 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 4957 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 4958 | 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] | 4959 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4960 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | out: |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4963 | if (filp) |
| 4964 | fput(filp); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4965 | if (status && new_state) |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4966 | release_lock_stateid(lock_stp); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4967 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4968 | if (!cstate->replay_owner) |
| 4969 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4970 | if (file_lock) |
| 4971 | locks_free_lock(file_lock); |
| 4972 | if (conflock) |
| 4973 | locks_free_lock(conflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | return status; |
| 4975 | } |
| 4976 | |
| 4977 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 4978 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 4979 | * so we do a temporary open here just to get an open file to pass to |
| 4980 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 4981 | * inode operation.) |
| 4982 | */ |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 4983 | 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] | 4984 | { |
| 4985 | struct file *file; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 4986 | __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); |
| 4987 | if (!err) { |
| 4988 | err = nfserrno(vfs_test_lock(file, lock)); |
| 4989 | nfsd_close(file); |
| 4990 | } |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 4991 | return err; |
| 4992 | } |
| 4993 | |
| 4994 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | * LOCKT operation |
| 4996 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4997 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4998 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4999 | struct nfsd4_lockt *lockt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | { |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5001 | struct file_lock *file_lock = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5002 | struct nfs4_lockowner *lo; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5003 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5004 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5005 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5006 | if (locks_in_grace(SVC_NET(rqstp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5007 | return nfserr_grace; |
| 5008 | |
| 5009 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 5010 | return nfserr_inval; |
| 5011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | nfs4_lock_state(); |
| 5013 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5014 | if (!nfsd4_has_session(cstate)) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5015 | status = lookup_clientid(&lockt->lt_clientid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5016 | if (status) |
| 5017 | goto out; |
| 5018 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 5020 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5021 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5022 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5023 | file_lock = locks_alloc_lock(); |
| 5024 | if (!file_lock) { |
| 5025 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5026 | status = nfserr_jukebox; |
| 5027 | goto out; |
| 5028 | } |
| 5029 | locks_init_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | switch (lockt->lt_type) { |
| 5031 | case NFS4_READ_LT: |
| 5032 | case NFS4_READW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5033 | file_lock->fl_type = F_RDLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | break; |
| 5035 | case NFS4_WRITE_LT: |
| 5036 | case NFS4_WRITEW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5037 | file_lock->fl_type = F_WRLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | break; |
| 5039 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 5040 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | status = nfserr_inval; |
| 5042 | goto out; |
| 5043 | } |
| 5044 | |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 5045 | lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5046 | if (lo) |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5047 | file_lock->fl_owner = (fl_owner_t)lo; |
| 5048 | file_lock->fl_pid = current->tgid; |
| 5049 | file_lock->fl_flags = FL_POSIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5051 | file_lock->fl_start = lockt->lt_offset; |
| 5052 | 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] | 5053 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5054 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5055 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5056 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5057 | if (status) |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 5058 | goto out; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5059 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5060 | if (file_lock->fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5061 | status = nfserr_denied; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5062 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5063 | } |
| 5064 | out: |
| 5065 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5066 | if (file_lock) |
| 5067 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | return status; |
| 5069 | } |
| 5070 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5071 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5072 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 5073 | struct nfsd4_locku *locku) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5075 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5077 | struct file_lock *file_lock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5078 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5079 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5080 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 5081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5082 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 5083 | (long long) locku->lu_offset, |
| 5084 | (long long) locku->lu_length); |
| 5085 | |
| 5086 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 5087 | return nfserr_inval; |
| 5088 | |
| 5089 | nfs4_lock_state(); |
| 5090 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5091 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5092 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 5093 | &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5094 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5095 | goto out; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5096 | filp = find_any_file(stp->st_stid.sc_file); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5097 | if (!filp) { |
| 5098 | status = nfserr_lock_range; |
| 5099 | goto out; |
| 5100 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5101 | file_lock = locks_alloc_lock(); |
| 5102 | if (!file_lock) { |
| 5103 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5104 | status = nfserr_jukebox; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5105 | goto fput; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5106 | } |
| 5107 | locks_init_lock(file_lock); |
| 5108 | file_lock->fl_type = F_UNLCK; |
J. Bruce Fields | 0a262ff | 2013-06-17 17:29:40 -0400 | [diff] [blame] | 5109 | file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5110 | file_lock->fl_pid = current->tgid; |
| 5111 | file_lock->fl_file = filp; |
| 5112 | file_lock->fl_flags = FL_POSIX; |
| 5113 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 5114 | file_lock->fl_start = locku->lu_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5116 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 5117 | locku->lu_length); |
| 5118 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5119 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5120 | err = vfs_lock_file(filp, F_SETLK, file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5121 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 5122 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5123 | goto out_nfserr; |
| 5124 | } |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5125 | update_stateid(&stp->st_stid.sc_stateid); |
| 5126 | memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5127 | fput: |
| 5128 | fput(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5129 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5130 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 71c3bcd | 2011-09-27 21:42:29 -0400 | [diff] [blame] | 5131 | if (!cstate->replay_owner) |
| 5132 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5133 | if (file_lock) |
| 5134 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5135 | return status; |
| 5136 | |
| 5137 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5138 | status = nfserrno(err); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5139 | goto fput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | /* |
| 5143 | * returns |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5144 | * true: locks held by lockowner |
| 5145 | * false: no locks held by lockowner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5147 | static bool |
| 5148 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | { |
| 5150 | struct file_lock **flpp; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5151 | int status = false; |
| 5152 | struct file *filp = find_any_file(fp); |
| 5153 | struct inode *inode; |
| 5154 | |
| 5155 | if (!filp) { |
| 5156 | /* Any valid lock stateid should have some sort of access */ |
| 5157 | WARN_ON_ONCE(1); |
| 5158 | return status; |
| 5159 | } |
| 5160 | |
| 5161 | inode = file_inode(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 5163 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) { |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 5165 | if ((*flpp)->fl_owner == (fl_owner_t)lowner) { |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5166 | status = true; |
| 5167 | break; |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 5168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | } |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 5170 | spin_unlock(&inode->i_lock); |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5171 | fput(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | return status; |
| 5173 | } |
| 5174 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5175 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 5176 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 5177 | struct nfsd4_compound_state *cstate, |
| 5178 | struct nfsd4_release_lockowner *rlockowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | { |
| 5180 | clientid_t *clid = &rlockowner->rl_clientid; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5181 | struct nfs4_stateowner *sop = NULL, *tmp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5182 | struct nfs4_lockowner *lo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5183 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 5185 | unsigned int hashval = ownerstr_hashval(clid->cl_id, owner); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5186 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5187 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5188 | |
| 5189 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 5190 | clid->cl_boot, clid->cl_id); |
| 5191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | nfs4_lock_state(); |
| 5193 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5194 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5195 | if (status) |
| 5196 | goto out; |
| 5197 | |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 5198 | status = nfserr_locks_held; |
J. Bruce Fields | 06f1f86 | 2011-11-07 16:58:18 -0500 | [diff] [blame] | 5199 | |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5200 | /* Find the matching lock stateowner */ |
| 5201 | list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) { |
| 5202 | if (tmp->so_is_open_owner) |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 5203 | continue; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5204 | if (same_owner_str(tmp, owner, clid)) { |
| 5205 | sop = tmp; |
| 5206 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | } |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 5208 | } |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5209 | |
| 5210 | /* No matching owner found, maybe a replay? Just declare victory... */ |
| 5211 | if (!sop) { |
| 5212 | status = nfs_ok; |
| 5213 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | } |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5215 | |
| 5216 | lo = lockowner(sop); |
| 5217 | /* see if there are still any locks associated with it */ |
| 5218 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5219 | if (check_for_locks(stp->st_stid.sc_file, lo)) |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5220 | goto out; |
| 5221 | } |
| 5222 | |
| 5223 | status = nfs_ok; |
| 5224 | release_lockowner(lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5225 | out: |
| 5226 | nfs4_unlock_state(); |
| 5227 | return status; |
| 5228 | } |
| 5229 | |
| 5230 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5231 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5232 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5233 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5234 | } |
| 5235 | |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5236 | bool |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5237 | nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5238 | { |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5239 | struct nfs4_client_reclaim *crp; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5240 | |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5241 | crp = nfsd4_find_reclaim_client(name, nn); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5242 | return (crp && crp->cr_clp); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5243 | } |
| 5244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5245 | /* |
| 5246 | * failure => all reset bets are off, nfserr_no_grace... |
| 5247 | */ |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5248 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5249 | nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | { |
| 5251 | unsigned int strhashval; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5252 | struct nfs4_client_reclaim *crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5254 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
| 5255 | crp = alloc_reclaim(); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5256 | if (crp) { |
| 5257 | strhashval = clientstr_hashval(name); |
| 5258 | INIT_LIST_HEAD(&crp->cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5259 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5260 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5261 | crp->cr_clp = NULL; |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5262 | nn->reclaim_str_hashtbl_size++; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5263 | } |
| 5264 | return crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | } |
| 5266 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 5267 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5268 | 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] | 5269 | { |
| 5270 | list_del(&crp->cr_strhash); |
| 5271 | kfree(crp); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5272 | nn->reclaim_str_hashtbl_size--; |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 5273 | } |
| 5274 | |
| 5275 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5276 | nfs4_release_reclaim(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | { |
| 5278 | struct nfs4_client_reclaim *crp = NULL; |
| 5279 | int i; |
| 5280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5282 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 5283 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5284 | struct nfs4_client_reclaim, cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5285 | nfs4_remove_reclaim_record(crp, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | } |
| 5287 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 5288 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5289 | } |
| 5290 | |
| 5291 | /* |
| 5292 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 5293 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5294 | nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | { |
| 5296 | unsigned int strhashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | struct nfs4_client_reclaim *crp = NULL; |
| 5298 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5299 | dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5301 | strhashval = clientstr_hashval(recdir); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5302 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5303 | if (same_name(crp->cr_recdir, recdir)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | return crp; |
| 5305 | } |
| 5306 | } |
| 5307 | return NULL; |
| 5308 | } |
| 5309 | |
| 5310 | /* |
| 5311 | * Called from OPEN. Look for clientid in reclaim list. |
| 5312 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5313 | __be32 |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5314 | nfs4_check_open_reclaim(clientid_t *clid, |
| 5315 | struct nfsd4_compound_state *cstate, |
| 5316 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5317 | { |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5318 | __be32 status; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 5319 | |
| 5320 | /* find clientid in conf_id_hashtbl */ |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5321 | status = lookup_clientid(clid, cstate, nn); |
| 5322 | if (status) |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 5323 | return nfserr_reclaim_bad; |
| 5324 | |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5325 | if (nfsd4_client_record_check(cstate->clp)) |
| 5326 | return nfserr_reclaim_bad; |
| 5327 | |
| 5328 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5329 | } |
| 5330 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5331 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
| 5332 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5333 | u64 nfsd_forget_client(struct nfs4_client *clp, u64 max) |
| 5334 | { |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 5335 | if (mark_client_expired(clp)) |
| 5336 | return 0; |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5337 | expire_client(clp); |
| 5338 | return 1; |
| 5339 | } |
| 5340 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5341 | u64 nfsd_print_client(struct nfs4_client *clp, u64 num) |
| 5342 | { |
| 5343 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 5344 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5345 | printk(KERN_INFO "NFS Client: %s\n", buf); |
| 5346 | return 1; |
| 5347 | } |
| 5348 | |
| 5349 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, |
| 5350 | const char *type) |
| 5351 | { |
| 5352 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 5353 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5354 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
| 5355 | } |
| 5356 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5357 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, |
| 5358 | void (*func)(struct nfs4_ol_stateid *)) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5359 | { |
| 5360 | struct nfs4_openowner *oop; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5361 | struct nfs4_ol_stateid *stp, *st_next; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5362 | struct nfs4_ol_stateid *lst, *lst_next; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5363 | u64 count = 0; |
| 5364 | |
| 5365 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5366 | list_for_each_entry_safe(stp, st_next, |
| 5367 | &oop->oo_owner.so_stateids, st_perstateowner) { |
| 5368 | list_for_each_entry_safe(lst, lst_next, |
| 5369 | &stp->st_locks, st_locks) { |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5370 | if (func) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5371 | func(lst); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5372 | if (++count == max) |
| 5373 | return count; |
| 5374 | } |
| 5375 | } |
| 5376 | } |
| 5377 | |
| 5378 | return count; |
| 5379 | } |
| 5380 | |
| 5381 | u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max) |
| 5382 | { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5383 | return nfsd_foreach_client_lock(clp, max, release_lock_stateid); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5384 | } |
| 5385 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5386 | u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max) |
| 5387 | { |
| 5388 | u64 count = nfsd_foreach_client_lock(clp, max, NULL); |
| 5389 | nfsd_print_count(clp, count, "locked files"); |
| 5390 | return count; |
| 5391 | } |
| 5392 | |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 5393 | static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *)) |
| 5394 | { |
| 5395 | struct nfs4_openowner *oop, *next; |
| 5396 | u64 count = 0; |
| 5397 | |
| 5398 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { |
| 5399 | if (func) |
| 5400 | func(oop); |
| 5401 | if (++count == max) |
| 5402 | break; |
| 5403 | } |
| 5404 | |
| 5405 | return count; |
| 5406 | } |
| 5407 | |
| 5408 | u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max) |
| 5409 | { |
| 5410 | return nfsd_foreach_client_open(clp, max, release_openowner); |
| 5411 | } |
| 5412 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5413 | u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max) |
| 5414 | { |
| 5415 | u64 count = nfsd_foreach_client_open(clp, max, NULL); |
| 5416 | nfsd_print_count(clp, count, "open files"); |
| 5417 | return count; |
| 5418 | } |
| 5419 | |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5420 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
| 5421 | struct list_head *victims) |
| 5422 | { |
| 5423 | struct nfs4_delegation *dp, *next; |
| 5424 | u64 count = 0; |
| 5425 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5426 | lockdep_assert_held(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5427 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5428 | if (victims) { |
| 5429 | /* |
| 5430 | * It's not safe to mess with delegations that have a |
| 5431 | * non-zero dl_time. They might have already been broken |
| 5432 | * and could be processed by the laundromat outside of |
| 5433 | * the state_lock. Just leave them be. |
| 5434 | */ |
| 5435 | if (dp->dl_time != 0) |
| 5436 | continue; |
| 5437 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 5438 | unhash_delegation_locked(dp); |
| 5439 | list_add(&dp->dl_recall_lru, victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5440 | } |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5441 | if (++count == max) |
| 5442 | break; |
| 5443 | } |
| 5444 | return count; |
| 5445 | } |
| 5446 | |
| 5447 | u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max) |
| 5448 | { |
| 5449 | struct nfs4_delegation *dp, *next; |
| 5450 | LIST_HEAD(victims); |
| 5451 | u64 count; |
| 5452 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5453 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5454 | count = nfsd_find_all_delegations(clp, max, &victims); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5455 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5456 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5457 | list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) { |
| 5458 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5459 | revoke_delegation(dp); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5460 | } |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5461 | |
| 5462 | return count; |
| 5463 | } |
| 5464 | |
| 5465 | u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max) |
| 5466 | { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5467 | struct nfs4_delegation *dp; |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5468 | LIST_HEAD(victims); |
| 5469 | u64 count; |
| 5470 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5471 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5472 | count = nfsd_find_all_delegations(clp, max, &victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5473 | while (!list_empty(&victims)) { |
| 5474 | dp = list_first_entry(&victims, struct nfs4_delegation, |
| 5475 | dl_recall_lru); |
| 5476 | list_del_init(&dp->dl_recall_lru); |
| 5477 | dp->dl_time = 0; |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5478 | nfsd_break_one_deleg(dp); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5479 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5480 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5481 | |
| 5482 | return count; |
| 5483 | } |
| 5484 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5485 | u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max) |
| 5486 | { |
| 5487 | u64 count = 0; |
| 5488 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5489 | spin_lock(&state_lock); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5490 | count = nfsd_find_all_delegations(clp, max, NULL); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5491 | spin_unlock(&state_lock); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5492 | |
| 5493 | nfsd_print_count(clp, count, "delegations"); |
| 5494 | return count; |
| 5495 | } |
| 5496 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5497 | 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] | 5498 | { |
| 5499 | struct nfs4_client *clp, *next; |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5500 | u64 count = 0; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5501 | 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] | 5502 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5503 | if (!nfsd_netns_ready(nn)) |
| 5504 | return 0; |
| 5505 | |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5506 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5507 | count += func(clp, max - count); |
| 5508 | if ((max != 0) && (count >= max)) |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5509 | break; |
| 5510 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5511 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5512 | return count; |
| 5513 | } |
| 5514 | |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 5515 | struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) |
| 5516 | { |
| 5517 | struct nfs4_client *clp; |
| 5518 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id); |
| 5519 | |
| 5520 | if (!nfsd_netns_ready(nn)) |
| 5521 | return NULL; |
| 5522 | |
| 5523 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 5524 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) |
| 5525 | return clp; |
| 5526 | } |
| 5527 | return NULL; |
| 5528 | } |
| 5529 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5530 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
| 5531 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 5532 | /* |
| 5533 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 5534 | * client may quite reasonably hang on to a delegation as long as it has |
| 5535 | * the inode cached. This becomes an obvious problem the first time a |
| 5536 | * client's inode cache approaches the size of the server's total memory. |
| 5537 | * |
| 5538 | * For now we avoid this problem by imposing a hard limit on the number |
| 5539 | * of delegations, which varies according to the server's memory size. |
| 5540 | */ |
| 5541 | static void |
| 5542 | set_max_delegations(void) |
| 5543 | { |
| 5544 | /* |
| 5545 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 5546 | * estimates suggest that in the worst case (where every delegation |
| 5547 | * is for a different inode), a delegation could take about 1.5K, |
| 5548 | * giving a worst case usage of about 6% of memory. |
| 5549 | */ |
| 5550 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 5551 | } |
| 5552 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5553 | static int nfs4_state_create_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5554 | { |
| 5555 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 5556 | int i; |
| 5557 | |
| 5558 | nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5559 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 5560 | if (!nn->conf_id_hashtbl) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5561 | goto err; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5562 | nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5563 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 5564 | if (!nn->unconf_id_hashtbl) |
| 5565 | goto err_unconf_id; |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5566 | nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5567 | OWNER_HASH_SIZE, GFP_KERNEL); |
| 5568 | if (!nn->ownerstr_hashtbl) |
| 5569 | goto err_ownerstr; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5570 | nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5571 | SESSION_HASH_SIZE, GFP_KERNEL); |
| 5572 | if (!nn->sessionid_hashtbl) |
| 5573 | goto err_sessionid; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5574 | |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5575 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5576 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5577 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5578 | } |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5579 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 5580 | INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5581 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 5582 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5583 | nn->conf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5584 | nn->unconf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5585 | INIT_LIST_HEAD(&nn->client_lru); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 5586 | INIT_LIST_HEAD(&nn->close_lru); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5587 | INIT_LIST_HEAD(&nn->del_recall_lru); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5588 | spin_lock_init(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5589 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5590 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5591 | get_net(net); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5592 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5593 | return 0; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5594 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5595 | err_sessionid: |
Stanislav Kinsbursky | 20e9e2b | 2012-11-14 18:21:46 +0300 | [diff] [blame] | 5596 | kfree(nn->ownerstr_hashtbl); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5597 | err_ownerstr: |
| 5598 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5599 | err_unconf_id: |
| 5600 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5601 | err: |
| 5602 | return -ENOMEM; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5603 | } |
| 5604 | |
| 5605 | static void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5606 | nfs4_state_destroy_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5607 | { |
| 5608 | int i; |
| 5609 | struct nfs4_client *clp = NULL; |
| 5610 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 5611 | |
| 5612 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 5613 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 5614 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 5615 | destroy_client(clp); |
| 5616 | } |
| 5617 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5618 | |
Kinglong Mee | 2b90563 | 2014-03-26 22:09:30 +0800 | [diff] [blame] | 5619 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 5620 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 5621 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 5622 | destroy_client(clp); |
| 5623 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5624 | } |
| 5625 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5626 | kfree(nn->sessionid_hashtbl); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5627 | kfree(nn->ownerstr_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5628 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5629 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5630 | put_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5631 | } |
| 5632 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 5633 | int |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5634 | nfs4_state_start_net(struct net *net) |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 5635 | { |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 5636 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5637 | int ret; |
| 5638 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5639 | ret = nfs4_state_create_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5640 | if (ret) |
| 5641 | return ret; |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 5642 | nfsd4_client_tracking_init(net); |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 5643 | nn->boot_time = get_seconds(); |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5644 | locks_start_grace(net, &nn->nfsd4_manager); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5645 | nn->grace_ended = false; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5646 | printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n", |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 5647 | nn->nfsd4_grace, net); |
| 5648 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5649 | return 0; |
| 5650 | } |
| 5651 | |
| 5652 | /* initialization to perform when the nfsd service is started: */ |
| 5653 | |
| 5654 | int |
| 5655 | nfs4_state_start(void) |
| 5656 | { |
| 5657 | int ret; |
| 5658 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5659 | ret = set_callback_cred(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5660 | if (ret) |
| 5661 | return -ENOMEM; |
NeilBrown | 58da282 | 2005-06-23 22:03:19 -0700 | [diff] [blame] | 5662 | laundry_wq = create_singlethread_workqueue("nfsd4"); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 5663 | if (laundry_wq == NULL) { |
| 5664 | ret = -ENOMEM; |
| 5665 | goto out_recovery; |
| 5666 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5667 | ret = nfsd4_create_callback_queue(); |
| 5668 | if (ret) |
| 5669 | goto out_free_laundry; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5670 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 5671 | set_max_delegations(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5672 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5673 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5674 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5675 | out_free_laundry: |
| 5676 | destroy_workqueue(laundry_wq); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 5677 | out_recovery: |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5678 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | } |
| 5680 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 5681 | void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5682 | nfs4_state_shutdown_net(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5684 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5685 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5686 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5688 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 5689 | locks_end_grace(&nn->nfsd4_manager); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 5690 | |
J. Bruce Fields | e50a26d | 2013-10-30 10:33:09 -0400 | [diff] [blame] | 5691 | nfs4_lock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5693 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5694 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 5696 | unhash_delegation_locked(dp); |
| 5697 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5698 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5699 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | list_for_each_safe(pos, next, &reaplist) { |
| 5701 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 5702 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 5703 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5704 | } |
| 5705 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5706 | nfsd4_client_tracking_exit(net); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5707 | nfs4_state_destroy_net(net); |
J. Bruce Fields | e50a26d | 2013-10-30 10:33:09 -0400 | [diff] [blame] | 5708 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | } |
| 5710 | |
| 5711 | void |
| 5712 | nfs4_state_shutdown(void) |
| 5713 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 5714 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 5715 | nfsd4_destroy_callback_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5716 | } |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5717 | |
| 5718 | static void |
| 5719 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 5720 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 5721 | if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid)) |
| 5722 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5723 | } |
| 5724 | |
| 5725 | static void |
| 5726 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 5727 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 5728 | if (cstate->minorversion) { |
| 5729 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
| 5730 | SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
| 5731 | } |
| 5732 | } |
| 5733 | |
| 5734 | void |
| 5735 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 5736 | { |
| 5737 | CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5738 | } |
| 5739 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5740 | /* |
| 5741 | * functions to set current state id |
| 5742 | */ |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5743 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 5744 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 5745 | { |
| 5746 | put_stateid(cstate, &odp->od_stateid); |
| 5747 | } |
| 5748 | |
| 5749 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5750 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open) |
| 5751 | { |
| 5752 | put_stateid(cstate, &open->op_stateid); |
| 5753 | } |
| 5754 | |
| 5755 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5756 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 5757 | { |
| 5758 | put_stateid(cstate, &close->cl_stateid); |
| 5759 | } |
| 5760 | |
| 5761 | void |
| 5762 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock) |
| 5763 | { |
| 5764 | put_stateid(cstate, &lock->lk_resp_stateid); |
| 5765 | } |
| 5766 | |
| 5767 | /* |
| 5768 | * functions to consume current state id |
| 5769 | */ |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 5770 | |
| 5771 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 5772 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 5773 | { |
| 5774 | get_stateid(cstate, &odp->od_stateid); |
| 5775 | } |
| 5776 | |
| 5777 | void |
| 5778 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp) |
| 5779 | { |
| 5780 | get_stateid(cstate, &drp->dr_stateid); |
| 5781 | } |
| 5782 | |
| 5783 | void |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 5784 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp) |
| 5785 | { |
| 5786 | get_stateid(cstate, &fsp->fr_stateid); |
| 5787 | } |
| 5788 | |
| 5789 | void |
| 5790 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr) |
| 5791 | { |
| 5792 | get_stateid(cstate, &setattr->sa_stateid); |
| 5793 | } |
| 5794 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5795 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5796 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 5797 | { |
| 5798 | get_stateid(cstate, &close->cl_stateid); |
| 5799 | } |
| 5800 | |
| 5801 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5802 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5803 | { |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5804 | get_stateid(cstate, &locku->lu_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5805 | } |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 5806 | |
| 5807 | void |
| 5808 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read) |
| 5809 | { |
| 5810 | get_stateid(cstate, &read->rd_stateid); |
| 5811 | } |
| 5812 | |
| 5813 | void |
| 5814 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write) |
| 5815 | { |
| 5816 | get_stateid(cstate, &write->wr_stateid); |
| 5817 | } |