Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (c) 2001 The Regents of the University of Michigan. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Kendrick Smith <kmsmith@umich.edu> |
| 6 | * Andy Adamson <kandros@umich.edu> |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the name of the University nor the names of its |
| 18 | * contributors may be used to endorse or promote products derived |
| 19 | * from this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | */ |
| 34 | |
Dave Hansen | aceaf78 | 2008-02-15 14:37:31 -0800 | [diff] [blame] | 35 | #include <linux/file.h> |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 36 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 38 | #include <linux/namei.h> |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 39 | #include <linux/swap.h> |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 40 | #include <linux/pagemap.h> |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 41 | #include <linux/ratelimit.h> |
Olga Kornievskaia | 68e76ad | 2008-12-23 16:17:15 -0500 | [diff] [blame] | 42 | #include <linux/sunrpc/svcauth_gss.h> |
Jeff Layton | 5976687 | 2013-02-04 12:50:00 -0500 | [diff] [blame] | 43 | #include <linux/sunrpc/addr.h> |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 44 | #include <linux/hash.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 45 | #include "xdr4.h" |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 46 | #include "xdr4cb.h" |
J. Bruce Fields | 0a3adad | 2009-11-04 18:12:35 -0500 | [diff] [blame] | 47 | #include "vfs.h" |
J. Bruce Fields | bfa4b36 | 2012-04-25 16:49:18 -0400 | [diff] [blame] | 48 | #include "current_stateid.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 50 | #include "netns.h" |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 53 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 54 | #define all_ones {{~0,~0},~0} |
| 55 | static const stateid_t one_stateid = { |
| 56 | .si_generation = ~0, |
| 57 | .si_opaque = all_ones, |
| 58 | }; |
| 59 | static const stateid_t zero_stateid = { |
| 60 | /* all fields zero */ |
| 61 | }; |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 62 | static const stateid_t currentstateid = { |
| 63 | .si_generation = 1, |
| 64 | }; |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 65 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 66 | static u64 current_sessionid = 1; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 67 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 68 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
| 69 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 70 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* forward declarations */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 73 | static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 75 | /* Locking: */ |
| 76 | |
| 77 | /* Currently used for almost all code touching nfsv4 state: */ |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 78 | static DEFINE_MUTEX(client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 80 | /* |
| 81 | * Currently used for the del_recall_lru and file hash table. In an |
| 82 | * effort to decrease the scope of the client_mutex, this spinlock may |
| 83 | * eventually cover more: |
| 84 | */ |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 85 | static DEFINE_SPINLOCK(state_lock); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 86 | |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 87 | static struct kmem_cache *openowner_slab; |
| 88 | static struct kmem_cache *lockowner_slab; |
| 89 | static struct kmem_cache *file_slab; |
| 90 | static struct kmem_cache *stateid_slab; |
| 91 | static struct kmem_cache *deleg_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | void |
| 94 | nfs4_lock_state(void) |
| 95 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 96 | mutex_lock(&client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 99 | static void free_session(struct nfsd4_session *); |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 100 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 101 | static bool is_session_dead(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 102 | { |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 103 | return ses->se_flags & NFS4_SESSION_DEAD; |
| 104 | } |
| 105 | |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 106 | void nfsd4_put_session(struct nfsd4_session *ses) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 107 | { |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 108 | if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) |
| 109 | free_session(ses); |
| 110 | } |
| 111 | |
| 112 | static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) |
| 113 | { |
| 114 | if (atomic_read(&ses->se_ref) > ref_held_by_me) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 115 | return nfserr_jukebox; |
| 116 | ses->se_flags |= NFS4_SESSION_DEAD; |
| 117 | return nfs_ok; |
| 118 | } |
| 119 | |
| 120 | static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) |
| 121 | { |
| 122 | if (is_session_dead(ses)) |
| 123 | return nfserr_badsession; |
| 124 | atomic_inc(&ses->se_ref); |
| 125 | return nfs_ok; |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | void |
| 129 | nfs4_unlock_state(void) |
| 130 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 131 | mutex_unlock(&client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 134 | static bool is_client_expired(struct nfs4_client *clp) |
| 135 | { |
| 136 | return clp->cl_time == 0; |
| 137 | } |
| 138 | |
| 139 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
| 140 | { |
| 141 | if (atomic_read(&clp->cl_refcount)) |
| 142 | return nfserr_jukebox; |
| 143 | clp->cl_time = 0; |
| 144 | return nfs_ok; |
| 145 | } |
| 146 | |
| 147 | static __be32 mark_client_expired(struct nfs4_client *clp) |
| 148 | { |
| 149 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 150 | __be32 ret; |
| 151 | |
| 152 | spin_lock(&nn->client_lock); |
| 153 | ret = mark_client_expired_locked(clp); |
| 154 | spin_unlock(&nn->client_lock); |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | static __be32 get_client_locked(struct nfs4_client *clp) |
| 159 | { |
| 160 | if (is_client_expired(clp)) |
| 161 | return nfserr_expired; |
| 162 | atomic_inc(&clp->cl_refcount); |
| 163 | return nfs_ok; |
| 164 | } |
| 165 | |
| 166 | /* must be called under the client_lock */ |
| 167 | static inline void |
| 168 | renew_client_locked(struct nfs4_client *clp) |
| 169 | { |
| 170 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 171 | |
| 172 | if (is_client_expired(clp)) { |
| 173 | WARN_ON(1); |
| 174 | printk("%s: client (clientid %08x/%08x) already expired\n", |
| 175 | __func__, |
| 176 | clp->cl_clientid.cl_boot, |
| 177 | clp->cl_clientid.cl_id); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | dprintk("renewing client (clientid %08x/%08x)\n", |
| 182 | clp->cl_clientid.cl_boot, |
| 183 | clp->cl_clientid.cl_id); |
| 184 | list_move_tail(&clp->cl_lru, &nn->client_lru); |
| 185 | clp->cl_time = get_seconds(); |
| 186 | } |
| 187 | |
| 188 | static inline void |
| 189 | renew_client(struct nfs4_client *clp) |
| 190 | { |
| 191 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 192 | |
| 193 | spin_lock(&nn->client_lock); |
| 194 | renew_client_locked(clp); |
| 195 | spin_unlock(&nn->client_lock); |
| 196 | } |
| 197 | |
Fengguang Wu | ba13843 | 2013-04-16 22:14:15 -0400 | [diff] [blame] | 198 | static void put_client_renew_locked(struct nfs4_client *clp) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 199 | { |
| 200 | if (!atomic_dec_and_test(&clp->cl_refcount)) |
| 201 | return; |
| 202 | if (!is_client_expired(clp)) |
| 203 | renew_client_locked(clp); |
| 204 | } |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | static inline u32 |
| 207 | opaque_hashval(const void *ptr, int nbytes) |
| 208 | { |
| 209 | unsigned char *cptr = (unsigned char *) ptr; |
| 210 | |
| 211 | u32 x = 0; |
| 212 | while (nbytes--) { |
| 213 | x *= 37; |
| 214 | x += *cptr++; |
| 215 | } |
| 216 | return x; |
| 217 | } |
| 218 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 219 | static void nfsd4_free_file(struct nfs4_file *f) |
| 220 | { |
| 221 | kmem_cache_free(file_slab, f); |
| 222 | } |
| 223 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 224 | static inline void |
| 225 | put_nfs4_file(struct nfs4_file *fi) |
| 226 | { |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 227 | if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) { |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 228 | hlist_del(&fi->fi_hash); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 229 | spin_unlock(&state_lock); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 230 | iput(fi->fi_inode); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 231 | nfsd4_free_file(fi); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 232 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static inline void |
| 236 | get_nfs4_file(struct nfs4_file *fi) |
| 237 | { |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 238 | atomic_inc(&fi->fi_ref); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 239 | } |
| 240 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 241 | static int num_delegations; |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 242 | unsigned long max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * Open owner state (share locks) |
| 246 | */ |
| 247 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 248 | /* hash tables for lock and open owners */ |
| 249 | #define OWNER_HASH_BITS 8 |
| 250 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) |
| 251 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 252 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 253 | static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 254 | { |
| 255 | unsigned int ret; |
| 256 | |
| 257 | ret = opaque_hashval(ownername->data, ownername->len); |
| 258 | ret += clientid; |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 259 | return ret & OWNER_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 260 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 261 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 262 | /* hash table for nfs4_file */ |
| 263 | #define FILE_HASH_BITS 8 |
| 264 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
Shan Wei | 3507958 | 2011-01-14 17:35:59 +0800 | [diff] [blame] | 265 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 266 | static unsigned int file_hashval(struct inode *ino) |
| 267 | { |
| 268 | /* XXX: why are we hashing on inode pointer, anyway? */ |
| 269 | return hash_ptr(ino, FILE_HASH_BITS); |
| 270 | } |
| 271 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 272 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 273 | |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 274 | static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag) |
| 275 | { |
| 276 | WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR])); |
| 277 | atomic_inc(&fp->fi_access[oflag]); |
| 278 | } |
| 279 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 280 | static void nfs4_file_get_access(struct nfs4_file *fp, int oflag) |
| 281 | { |
| 282 | if (oflag == O_RDWR) { |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 283 | __nfs4_file_get_access(fp, O_RDONLY); |
| 284 | __nfs4_file_get_access(fp, O_WRONLY); |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 285 | } else |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 286 | __nfs4_file_get_access(fp, oflag); |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag) |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 290 | { |
| 291 | if (fp->fi_fds[oflag]) { |
| 292 | fput(fp->fi_fds[oflag]); |
| 293 | fp->fi_fds[oflag] = NULL; |
| 294 | } |
| 295 | } |
| 296 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 297 | 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] | 298 | { |
| 299 | if (atomic_dec_and_test(&fp->fi_access[oflag])) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 300 | nfs4_file_put_fd(fp, oflag); |
J. Bruce Fields | 0c7c3e6 | 2013-03-28 20:37:14 -0400 | [diff] [blame] | 301 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
J. Bruce Fields | 3d02fa2 | 2011-09-19 15:07:41 -0400 | [diff] [blame] | 302 | nfs4_file_put_fd(fp, O_RDWR); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 306 | static void nfs4_file_put_access(struct nfs4_file *fp, int oflag) |
| 307 | { |
| 308 | if (oflag == O_RDWR) { |
| 309 | __nfs4_file_put_access(fp, O_RDONLY); |
| 310 | __nfs4_file_put_access(fp, O_WRONLY); |
| 311 | } else |
| 312 | __nfs4_file_put_access(fp, oflag); |
| 313 | } |
| 314 | |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 315 | static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct |
| 316 | kmem_cache *slab) |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 317 | { |
| 318 | struct idr *stateids = &cl->cl_stateids; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 319 | struct nfs4_stid *stid; |
| 320 | int new_id; |
| 321 | |
| 322 | stid = kmem_cache_alloc(slab, GFP_KERNEL); |
| 323 | if (!stid) |
| 324 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 325 | |
Jeff Layton | 398c33a | 2013-04-29 16:21:20 -0700 | [diff] [blame] | 326 | new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL); |
Tejun Heo | ebd6c70 | 2013-03-13 14:59:37 -0700 | [diff] [blame] | 327 | if (new_id < 0) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 328 | goto out_free; |
| 329 | stid->sc_client = cl; |
| 330 | stid->sc_type = 0; |
| 331 | stid->sc_stateid.si_opaque.so_id = new_id; |
| 332 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; |
| 333 | /* Will be incremented before return to client: */ |
| 334 | stid->sc_stateid.si_generation = 0; |
| 335 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 336 | /* |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 337 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 338 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 339 | * example, a stray write retransmission could be accepted by |
| 340 | * the server when it should have been rejected. Therefore, |
| 341 | * adopt a trick from the sctp code to attempt to maximize the |
| 342 | * amount of time until an id is reused, by ensuring they always |
| 343 | * "increase" (mod INT_MAX): |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 344 | */ |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 345 | return stid; |
| 346 | out_free: |
Wei Yongjun | 2c44a23 | 2013-04-09 14:15:31 +0800 | [diff] [blame] | 347 | kmem_cache_free(slab, stid); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 348 | return NULL; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 349 | } |
| 350 | |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 351 | static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp) |
| 352 | { |
| 353 | return openlockstateid(nfs4_alloc_stid(clp, stateid_slab)); |
| 354 | } |
| 355 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 356 | /* |
| 357 | * When we recall a delegation, we should be careful not to hand it |
| 358 | * out again straight away. |
| 359 | * To ensure this we keep a pair of bloom filters ('new' and 'old') |
| 360 | * in which the filehandles of recalled delegations are "stored". |
| 361 | * If a filehandle appear in either filter, a delegation is blocked. |
| 362 | * When a delegation is recalled, the filehandle is stored in the "new" |
| 363 | * filter. |
| 364 | * Every 30 seconds we swap the filters and clear the "new" one, |
| 365 | * unless both are empty of course. |
| 366 | * |
| 367 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the |
| 368 | * low 3 bytes as hash-table indices. |
| 369 | * |
| 370 | * 'state_lock', which is always held when block_delegations() is called, |
| 371 | * is used to manage concurrent access. Testing does not need the lock |
| 372 | * except when swapping the two filters. |
| 373 | */ |
| 374 | static struct bloom_pair { |
| 375 | int entries, old_entries; |
| 376 | time_t swap_time; |
| 377 | int new; /* index into 'set' */ |
| 378 | DECLARE_BITMAP(set[2], 256); |
| 379 | } blocked_delegations; |
| 380 | |
| 381 | static int delegation_blocked(struct knfsd_fh *fh) |
| 382 | { |
| 383 | u32 hash; |
| 384 | struct bloom_pair *bd = &blocked_delegations; |
| 385 | |
| 386 | if (bd->entries == 0) |
| 387 | return 0; |
| 388 | if (seconds_since_boot() - bd->swap_time > 30) { |
| 389 | spin_lock(&state_lock); |
| 390 | if (seconds_since_boot() - bd->swap_time > 30) { |
| 391 | bd->entries -= bd->old_entries; |
| 392 | bd->old_entries = bd->entries; |
| 393 | memset(bd->set[bd->new], 0, |
| 394 | sizeof(bd->set[0])); |
| 395 | bd->new = 1-bd->new; |
| 396 | bd->swap_time = seconds_since_boot(); |
| 397 | } |
| 398 | spin_unlock(&state_lock); |
| 399 | } |
| 400 | hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0); |
| 401 | if (test_bit(hash&255, bd->set[0]) && |
| 402 | test_bit((hash>>8)&255, bd->set[0]) && |
| 403 | test_bit((hash>>16)&255, bd->set[0])) |
| 404 | return 1; |
| 405 | |
| 406 | if (test_bit(hash&255, bd->set[1]) && |
| 407 | test_bit((hash>>8)&255, bd->set[1]) && |
| 408 | test_bit((hash>>16)&255, bd->set[1])) |
| 409 | return 1; |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static void block_delegations(struct knfsd_fh *fh) |
| 415 | { |
| 416 | u32 hash; |
| 417 | struct bloom_pair *bd = &blocked_delegations; |
| 418 | |
| 419 | hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0); |
| 420 | |
| 421 | __set_bit(hash&255, bd->set[bd->new]); |
| 422 | __set_bit((hash>>8)&255, bd->set[bd->new]); |
| 423 | __set_bit((hash>>16)&255, bd->set[bd->new]); |
| 424 | if (bd->entries == 0) |
| 425 | bd->swap_time = seconds_since_boot(); |
| 426 | bd->entries += 1; |
| 427 | } |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | static struct nfs4_delegation * |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 430 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
| 432 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | dprintk("NFSD alloc_init_deleg\n"); |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 435 | if (num_delegations > max_delegations) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 436 | return NULL; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 437 | if (delegation_blocked(¤t_fh->fh_handle)) |
| 438 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 439 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab)); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 440 | if (dp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | return dp; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 442 | /* |
| 443 | * delegation seqid's are never incremented. The 4.1 special |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 444 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 445 | * 0 anyway just for consistency and use 1: |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 446 | */ |
| 447 | dp->dl_stid.sc_stateid.si_generation = 1; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 448 | num_delegations++; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 449 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 450 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 452 | dp->dl_file = NULL; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 453 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
J. Bruce Fields | 6c02eaa | 2009-02-02 17:30:51 -0500 | [diff] [blame] | 454 | fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | dp->dl_time = 0; |
| 456 | atomic_set(&dp->dl_count, 1); |
J. Bruce Fields | 5772515 | 2012-11-05 15:10:26 -0500 | [diff] [blame] | 457 | nfsd4_init_callback(&dp->dl_recall); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | return dp; |
| 459 | } |
| 460 | |
J. Bruce Fields | 68a3396 | 2013-03-21 11:21:50 -0400 | [diff] [blame] | 461 | static void remove_stid(struct nfs4_stid *s) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 462 | { |
| 463 | struct idr *stateids = &s->sc_client->cl_stateids; |
| 464 | |
| 465 | idr_remove(stateids, s->sc_stateid.si_opaque.so_id); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 466 | } |
| 467 | |
Benny Halevy | 9857df8 | 2013-10-14 13:44:52 +0300 | [diff] [blame] | 468 | static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s) |
| 469 | { |
| 470 | kmem_cache_free(slab, s); |
| 471 | } |
| 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | void |
| 474 | nfs4_put_delegation(struct nfs4_delegation *dp) |
| 475 | { |
| 476 | if (atomic_dec_and_test(&dp->dl_count)) { |
Benny Halevy | 9857df8 | 2013-10-14 13:44:52 +0300 | [diff] [blame] | 477 | nfs4_free_stid(deleg_slab, &dp->dl_stid); |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 478 | num_delegations--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 482 | static void nfs4_put_deleg_lease(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | { |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 484 | if (!fp->fi_lease) |
| 485 | return; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 486 | if (atomic_dec_and_test(&fp->fi_delegees)) { |
| 487 | vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease); |
| 488 | fp->fi_lease = NULL; |
J. Bruce Fields | 4ee6362 | 2011-04-15 18:08:26 -0400 | [diff] [blame] | 489 | fput(fp->fi_deleg_file); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 490 | fp->fi_deleg_file = NULL; |
| 491 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 494 | static void unhash_stid(struct nfs4_stid *s) |
| 495 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 496 | s->sc_type = 0; |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 497 | } |
| 498 | |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 499 | static void |
| 500 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 501 | { |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 502 | lockdep_assert_held(&state_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 503 | |
Benny Halevy | 3fb87d1 | 2014-05-30 09:09:31 -0400 | [diff] [blame] | 504 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 505 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
| 506 | list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations); |
| 507 | } |
| 508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | /* Called under the state lock. */ |
| 510 | static void |
| 511 | unhash_delegation(struct nfs4_delegation *dp) |
| 512 | { |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 513 | spin_lock(&state_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 514 | list_del_init(&dp->dl_perclnt); |
J. Bruce Fields | 5d926e8 | 2011-02-07 16:53:46 -0500 | [diff] [blame] | 515 | list_del_init(&dp->dl_perfile); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | list_del_init(&dp->dl_recall_lru); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 517 | spin_unlock(&state_lock); |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 518 | if (dp->dl_file) { |
| 519 | nfs4_put_deleg_lease(dp->dl_file); |
| 520 | put_nfs4_file(dp->dl_file); |
| 521 | dp->dl_file = NULL; |
| 522 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | |
| 526 | |
| 527 | static void destroy_revoked_delegation(struct nfs4_delegation *dp) |
| 528 | { |
| 529 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 68a3396 | 2013-03-21 11:21:50 -0400 | [diff] [blame] | 530 | remove_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | nfs4_put_delegation(dp); |
| 532 | } |
| 533 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 534 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 535 | { |
| 536 | unhash_delegation(dp); |
| 537 | remove_stid(&dp->dl_stid); |
| 538 | nfs4_put_delegation(dp); |
| 539 | } |
| 540 | |
| 541 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 542 | { |
| 543 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 544 | |
| 545 | if (clp->cl_minorversion == 0) |
| 546 | destroy_delegation(dp); |
| 547 | else { |
| 548 | unhash_delegation(dp); |
| 549 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
| 550 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 551 | } |
| 552 | } |
| 553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | /* |
| 555 | * SETCLIENTID state |
| 556 | */ |
| 557 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 558 | static unsigned int clientid_hashval(u32 id) |
| 559 | { |
| 560 | return id & CLIENT_HASH_MASK; |
| 561 | } |
| 562 | |
| 563 | static unsigned int clientstr_hashval(const char *name) |
| 564 | { |
| 565 | return opaque_hashval(name, 8) & CLIENT_HASH_MASK; |
| 566 | } |
| 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | /* |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 569 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 570 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 571 | * only what share bits are currently in force, but also what |
| 572 | * combinations of share bits previous opens have used. This allows us |
| 573 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 574 | * return an error if the client attempt to downgrade to a combination |
| 575 | * of share bits not explicable by closing some of its previous opens. |
| 576 | * |
| 577 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 578 | * track of access/deny bit combinations; so, e.g., we allow: |
| 579 | * |
| 580 | * OPEN allow read, deny write |
| 581 | * OPEN allow both, deny none |
| 582 | * DOWNGRADE allow read, deny none |
| 583 | * |
| 584 | * which we should reject. |
| 585 | */ |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 586 | static unsigned int |
| 587 | bmap_to_share_mode(unsigned long bmap) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 588 | int i; |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 589 | unsigned int access = 0; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 590 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 591 | for (i = 1; i < 4; i++) { |
| 592 | if (test_bit(i, &bmap)) |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 593 | access |= i; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 594 | } |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 595 | return access; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 596 | } |
| 597 | |
Jeff Layton | 3a32861 | 2012-05-11 09:45:12 -0400 | [diff] [blame] | 598 | static bool |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 599 | test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 600 | unsigned int access, deny; |
| 601 | |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 602 | access = bmap_to_share_mode(stp->st_access_bmap); |
| 603 | deny = bmap_to_share_mode(stp->st_deny_bmap); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 604 | if ((access & open->op_share_deny) || (deny & open->op_share_access)) |
Jeff Layton | 3a32861 | 2012-05-11 09:45:12 -0400 | [diff] [blame] | 605 | return false; |
| 606 | return true; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 607 | } |
| 608 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 609 | /* set share access for a given stateid */ |
| 610 | static inline void |
| 611 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 612 | { |
| 613 | __set_bit(access, &stp->st_access_bmap); |
| 614 | } |
| 615 | |
| 616 | /* clear share access for a given stateid */ |
| 617 | static inline void |
| 618 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 619 | { |
| 620 | __clear_bit(access, &stp->st_access_bmap); |
| 621 | } |
| 622 | |
| 623 | /* test whether a given stateid has access */ |
| 624 | static inline bool |
| 625 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 626 | { |
| 627 | return test_bit(access, &stp->st_access_bmap); |
| 628 | } |
| 629 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 630 | /* set share deny for a given stateid */ |
| 631 | static inline void |
| 632 | set_deny(u32 access, struct nfs4_ol_stateid *stp) |
| 633 | { |
| 634 | __set_bit(access, &stp->st_deny_bmap); |
| 635 | } |
| 636 | |
| 637 | /* clear share deny for a given stateid */ |
| 638 | static inline void |
| 639 | clear_deny(u32 access, struct nfs4_ol_stateid *stp) |
| 640 | { |
| 641 | __clear_bit(access, &stp->st_deny_bmap); |
| 642 | } |
| 643 | |
| 644 | /* test whether a given stateid is denying specific access */ |
| 645 | static inline bool |
| 646 | test_deny(u32 access, struct nfs4_ol_stateid *stp) |
| 647 | { |
| 648 | return test_bit(access, &stp->st_deny_bmap); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | static int nfs4_access_to_omode(u32 access) |
| 652 | { |
J. Bruce Fields | 8f34a43 | 2010-09-02 15:23:16 -0400 | [diff] [blame] | 653 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 654 | case NFS4_SHARE_ACCESS_READ: |
| 655 | return O_RDONLY; |
| 656 | case NFS4_SHARE_ACCESS_WRITE: |
| 657 | return O_WRONLY; |
| 658 | case NFS4_SHARE_ACCESS_BOTH: |
| 659 | return O_RDWR; |
| 660 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 661 | WARN_ON_ONCE(1); |
| 662 | return O_RDONLY; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 663 | } |
| 664 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 665 | /* release all access and file references for a given stateid */ |
| 666 | static void |
| 667 | release_all_access(struct nfs4_ol_stateid *stp) |
| 668 | { |
| 669 | int i; |
| 670 | |
| 671 | for (i = 1; i < 4; i++) { |
| 672 | if (test_access(i, stp)) |
| 673 | nfs4_file_put_access(stp->st_file, |
| 674 | nfs4_access_to_omode(i)); |
| 675 | clear_access(i, stp); |
| 676 | } |
| 677 | } |
| 678 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 679 | static void unhash_generic_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 680 | { |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 681 | list_del(&stp->st_perfile); |
| 682 | list_del(&stp->st_perstateowner); |
| 683 | } |
| 684 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 685 | static void close_generic_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 686 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 687 | release_all_access(stp); |
OGAWA Hirofumi | a96e5b9 | 2011-04-18 11:48:55 -0400 | [diff] [blame] | 688 | put_nfs4_file(stp->st_file); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 689 | stp->st_file = NULL; |
| 690 | } |
| 691 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 692 | static void free_generic_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 693 | { |
J. Bruce Fields | 68a3396 | 2013-03-21 11:21:50 -0400 | [diff] [blame] | 694 | remove_stid(&stp->st_stid); |
Benny Halevy | 9857df8 | 2013-10-14 13:44:52 +0300 | [diff] [blame] | 695 | nfs4_free_stid(stateid_slab, &stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 696 | } |
| 697 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 698 | static void __release_lock_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 699 | { |
| 700 | struct file *file; |
| 701 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 702 | list_del(&stp->st_locks); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 703 | unhash_generic_stateid(stp); |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 704 | unhash_stid(&stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 705 | file = find_any_file(stp->st_file); |
| 706 | if (file) |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 707 | locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner)); |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 708 | close_generic_stateid(stp); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 709 | free_generic_stateid(stp); |
| 710 | } |
| 711 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 712 | static void unhash_lockowner(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 713 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 714 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 715 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 716 | list_del(&lo->lo_owner.so_strhash); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 717 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 718 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 719 | struct nfs4_ol_stateid, st_perstateowner); |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 720 | __release_lock_stateid(stp); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
Trond Myklebust | 50cc623 | 2014-04-18 14:44:03 -0400 | [diff] [blame] | 724 | static void nfs4_free_lockowner(struct nfs4_lockowner *lo) |
| 725 | { |
| 726 | kfree(lo->lo_owner.so_owner.data); |
| 727 | kmem_cache_free(lockowner_slab, lo); |
| 728 | } |
| 729 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 730 | static void release_lockowner(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 731 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 732 | unhash_lockowner(lo); |
| 733 | nfs4_free_lockowner(lo); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 734 | } |
| 735 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 736 | static void release_lockowner_if_empty(struct nfs4_lockowner *lo) |
| 737 | { |
| 738 | if (list_empty(&lo->lo_owner.so_stateids)) |
| 739 | release_lockowner(lo); |
| 740 | } |
| 741 | |
| 742 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 743 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 744 | struct nfs4_lockowner *lo; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 745 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 746 | lo = lockowner(stp->st_stateowner); |
| 747 | __release_lock_stateid(stp); |
| 748 | release_lockowner_if_empty(lo); |
| 749 | } |
| 750 | |
| 751 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp) |
| 752 | { |
| 753 | struct nfs4_ol_stateid *stp; |
| 754 | |
| 755 | while (!list_empty(&open_stp->st_locks)) { |
| 756 | stp = list_entry(open_stp->st_locks.next, |
| 757 | struct nfs4_ol_stateid, st_locks); |
| 758 | release_lock_stateid(stp); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 762 | static void unhash_open_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 763 | { |
| 764 | unhash_generic_stateid(stp); |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 765 | release_open_stateid_locks(stp); |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 766 | close_generic_stateid(stp); |
| 767 | } |
| 768 | |
| 769 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 770 | { |
| 771 | unhash_open_stateid(stp); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 772 | free_generic_stateid(stp); |
| 773 | } |
| 774 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 775 | static void unhash_openowner(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 776 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 777 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 778 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 779 | list_del(&oo->oo_owner.so_strhash); |
| 780 | list_del(&oo->oo_perclient); |
| 781 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 782 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 783 | struct nfs4_ol_stateid, st_perstateowner); |
J. Bruce Fields | f044ff8 | 2009-01-11 15:24:04 -0500 | [diff] [blame] | 784 | release_open_stateid(stp); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 788 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 789 | { |
| 790 | struct nfs4_ol_stateid *s = oo->oo_last_closed_stid; |
| 791 | |
| 792 | if (s) { |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 793 | free_generic_stateid(s); |
| 794 | oo->oo_last_closed_stid = NULL; |
| 795 | } |
| 796 | } |
| 797 | |
Trond Myklebust | 50cc623 | 2014-04-18 14:44:03 -0400 | [diff] [blame] | 798 | static void nfs4_free_openowner(struct nfs4_openowner *oo) |
| 799 | { |
| 800 | kfree(oo->oo_owner.so_owner.data); |
| 801 | kmem_cache_free(openowner_slab, oo); |
| 802 | } |
| 803 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 804 | static void release_openowner(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 805 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 806 | unhash_openowner(oo); |
| 807 | list_del(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 808 | release_last_closed_stateid(oo); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 809 | nfs4_free_openowner(oo); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 810 | } |
| 811 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 812 | static inline int |
| 813 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 814 | { |
| 815 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 816 | |
| 817 | return sid->sequence % SESSION_HASH_SIZE; |
| 818 | } |
| 819 | |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 820 | #ifdef NFSD_DEBUG |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 821 | static inline void |
| 822 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 823 | { |
| 824 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 825 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 826 | } |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 827 | #else |
| 828 | static inline void |
| 829 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 830 | { |
| 831 | } |
| 832 | #endif |
| 833 | |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 834 | /* |
| 835 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 836 | * won't be used for replay. |
| 837 | */ |
| 838 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 839 | { |
| 840 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 841 | |
| 842 | if (nfserr == nfserr_replay_me) |
| 843 | return; |
| 844 | |
| 845 | if (!seqid_mutating_err(ntohl(nfserr))) { |
| 846 | cstate->replay_owner = NULL; |
| 847 | return; |
| 848 | } |
| 849 | if (!so) |
| 850 | return; |
| 851 | if (so->so_is_open_owner) |
| 852 | release_last_closed_stateid(openowner(so)); |
| 853 | so->so_seqid++; |
| 854 | return; |
| 855 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 856 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 857 | static void |
| 858 | gen_sessionid(struct nfsd4_session *ses) |
| 859 | { |
| 860 | struct nfs4_client *clp = ses->se_client; |
| 861 | struct nfsd4_sessionid *sid; |
| 862 | |
| 863 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 864 | sid->clientid = clp->cl_clientid; |
| 865 | sid->sequence = current_sessionid++; |
| 866 | sid->reserved = 0; |
| 867 | } |
| 868 | |
| 869 | /* |
Andy Adamson | a649637 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 870 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 871 | * the rpc header, but all we need to cache is the data starting after |
| 872 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 873 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 874 | * value that is the number of bytes in our cache plus a few additional |
| 875 | * bytes. In order to stay on the safe side, and not promise more than |
| 876 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 877 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 878 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 879 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 880 | */ |
Andy Adamson | a649637 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 881 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 882 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 883 | static void |
| 884 | free_session_slots(struct nfsd4_session *ses) |
| 885 | { |
| 886 | int i; |
| 887 | |
| 888 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) |
| 889 | kfree(ses->se_slots[i]); |
| 890 | } |
| 891 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 892 | /* |
| 893 | * We don't actually need to cache the rpc and session headers, so we |
| 894 | * can allocate a little less for each slot: |
| 895 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 896 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 897 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 898 | u32 size; |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 899 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 900 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 901 | size = 0; |
| 902 | else |
| 903 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 904 | return size + sizeof(struct nfsd4_slot); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 905 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 906 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 907 | /* |
| 908 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 909 | * re-negotiate active sessions and reduce their slot usage to make |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 910 | * 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] | 911 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 912 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 913 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 914 | u32 slotsize = slot_bytes(ca); |
| 915 | u32 num = ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 916 | int avail; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 917 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 918 | spin_lock(&nfsd_drc_lock); |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 919 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, |
| 920 | nfsd_drc_max_mem - nfsd_drc_mem_used); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 921 | num = min_t(int, num, avail / slotsize); |
| 922 | nfsd_drc_mem_used += num * slotsize; |
| 923 | spin_unlock(&nfsd_drc_lock); |
| 924 | |
| 925 | return num; |
| 926 | } |
| 927 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 928 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 929 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 930 | int slotsize = slot_bytes(ca); |
| 931 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 932 | spin_lock(&nfsd_drc_lock); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 933 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 934 | spin_unlock(&nfsd_drc_lock); |
| 935 | } |
| 936 | |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 937 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 938 | struct nfsd4_channel_attrs *battrs) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 939 | { |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 940 | int numslots = fattrs->maxreqs; |
| 941 | int slotsize = slot_bytes(fattrs); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 942 | struct nfsd4_session *new; |
| 943 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 944 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 945 | 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] | 946 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 947 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 948 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 949 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 950 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 951 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 952 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 953 | for (i = 0; i < numslots; i++) { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 954 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 955 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 956 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 957 | } |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 958 | |
| 959 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 960 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 961 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 962 | return new; |
| 963 | out_free: |
| 964 | while (i--) |
| 965 | kfree(new->se_slots[i]); |
| 966 | kfree(new); |
| 967 | return NULL; |
| 968 | } |
| 969 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 970 | static void free_conn(struct nfsd4_conn *c) |
| 971 | { |
| 972 | svc_xprt_put(c->cn_xprt); |
| 973 | kfree(c); |
| 974 | } |
| 975 | |
| 976 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 977 | { |
| 978 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 979 | struct nfs4_client *clp = c->cn_session->se_client; |
| 980 | |
| 981 | spin_lock(&clp->cl_lock); |
| 982 | if (!list_empty(&c->cn_persession)) { |
| 983 | list_del(&c->cn_persession); |
| 984 | free_conn(c); |
| 985 | } |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 986 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 2e4b723 | 2013-03-08 09:30:43 -0500 | [diff] [blame] | 987 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 988 | } |
| 989 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 990 | 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] | 991 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 992 | struct nfsd4_conn *conn; |
| 993 | |
| 994 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 995 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 996 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 997 | svc_xprt_get(rqstp->rq_xprt); |
| 998 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 999 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1000 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1001 | return conn; |
| 1002 | } |
| 1003 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1004 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1005 | { |
| 1006 | conn->cn_session = ses; |
| 1007 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1008 | } |
| 1009 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1010 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1011 | { |
| 1012 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1013 | |
| 1014 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1015 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1016 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1017 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1018 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1019 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1020 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1021 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1022 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1023 | } |
| 1024 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1025 | 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] | 1026 | { |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1027 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1028 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1029 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1030 | ret = nfsd4_register_conn(conn); |
| 1031 | if (ret) |
| 1032 | /* oops; xprt is already down: */ |
| 1033 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | 6a3b156 | 2012-09-12 09:59:17 -0400 | [diff] [blame] | 1034 | if (conn->cn_flags & NFS4_CDFC4_BACK) { |
Weston Andros Adamson | 2411967 | 2012-05-24 15:42:17 -0400 | [diff] [blame] | 1035 | /* callback channel may be back up */ |
| 1036 | nfsd4_probe_callback(ses->se_client); |
| 1037 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1038 | } |
| 1039 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1040 | 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] | 1041 | { |
| 1042 | u32 dir = NFS4_CDFC4_FORE; |
| 1043 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1044 | if (cses->flags & SESSION4_BACK_CHAN) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1045 | dir |= NFS4_CDFC4_BACK; |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1046 | return alloc_conn(rqstp, dir); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1050 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1051 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1052 | struct nfs4_client *clp = s->se_client; |
| 1053 | struct nfsd4_conn *c; |
| 1054 | |
| 1055 | spin_lock(&clp->cl_lock); |
| 1056 | while (!list_empty(&s->se_conns)) { |
| 1057 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1058 | list_del_init(&c->cn_persession); |
| 1059 | spin_unlock(&clp->cl_lock); |
| 1060 | |
| 1061 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1062 | free_conn(c); |
| 1063 | |
| 1064 | spin_lock(&clp->cl_lock); |
| 1065 | } |
| 1066 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1067 | } |
| 1068 | |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1069 | static void __free_session(struct nfsd4_session *ses) |
| 1070 | { |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1071 | free_session_slots(ses); |
| 1072 | kfree(ses); |
| 1073 | } |
| 1074 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1075 | static void free_session(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 1076 | { |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1077 | struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id); |
| 1078 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1079 | lockdep_assert_held(&nn->client_lock); |
| 1080 | nfsd4_del_conns(ses); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1081 | nfsd4_put_drc_mem(&ses->se_fchannel); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1082 | __free_session(ses); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1083 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1084 | |
Fengguang Wu | 135ae82 | 2012-11-10 07:20:25 -0500 | [diff] [blame] | 1085 | 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] | 1086 | { |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1087 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1088 | 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] | 1089 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1090 | new->se_client = clp; |
| 1091 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1092 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1093 | INIT_LIST_HEAD(&new->se_conns); |
| 1094 | |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1095 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1096 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 1097 | new->se_cb_prog = cses->callback_prog; |
J. Bruce Fields | c6bb3ca | 2012-11-01 16:31:02 -0400 | [diff] [blame] | 1098 | new->se_cb_sec = cses->cb_sec; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1099 | atomic_set(&new->se_ref, 0); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1100 | idx = hash_sessionid(&new->se_sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1101 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1102 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1103 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1104 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1105 | spin_unlock(&clp->cl_lock); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1106 | spin_unlock(&nn->client_lock); |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1107 | |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1108 | if (cses->flags & SESSION4_BACK_CHAN) { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1109 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1110 | /* |
| 1111 | * This is a little silly; with sessions there's no real |
| 1112 | * use for the callback address. Use the peer address |
| 1113 | * as a reasonable default for now, but consider fixing |
| 1114 | * the rpc client not to require an address in the |
| 1115 | * future: |
| 1116 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1117 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1118 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1119 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1120 | } |
| 1121 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1122 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1123 | static struct nfsd4_session * |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1124 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1125 | { |
| 1126 | struct nfsd4_session *elem; |
| 1127 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1128 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1129 | |
| 1130 | dump_sessionid(__func__, sessionid); |
| 1131 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1132 | /* Search in the appropriate list */ |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1133 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1134 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1135 | NFS4_MAX_SESSIONID_LEN)) { |
| 1136 | return elem; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | dprintk("%s: session not found\n", __func__); |
| 1141 | return NULL; |
| 1142 | } |
| 1143 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1144 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1145 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1146 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1147 | { |
| 1148 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1149 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1150 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1151 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1152 | } |
| 1153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1155 | static int |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1156 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | { |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1158 | if (clid->cl_boot == nn->boot_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | return 0; |
Andy Adamson | 60adfc5 | 2009-04-03 08:28:50 +0300 | [diff] [blame] | 1160 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1161 | clid->cl_boot, clid->cl_id, nn->boot_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | return 1; |
| 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | * XXX Should we use a slab cache ? |
| 1167 | * This type of memory management is somewhat inefficient, but we use it |
| 1168 | * anyway since SETCLIENTID is not a common operation. |
| 1169 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1170 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | { |
| 1172 | struct nfs4_client *clp; |
| 1173 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1174 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
| 1175 | if (clp == NULL) |
| 1176 | return NULL; |
Thomas Meyer | 67114fe | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 1177 | clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL); |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1178 | if (clp->cl_name.data == NULL) { |
| 1179 | kfree(clp); |
| 1180 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | } |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1182 | clp->cl_name.len = name.len; |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1183 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1184 | idr_init(&clp->cl_stateids); |
| 1185 | atomic_set(&clp->cl_refcount, 0); |
| 1186 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 1187 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1188 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1189 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 1190 | INIT_LIST_HEAD(&clp->cl_lru); |
| 1191 | INIT_LIST_HEAD(&clp->cl_callbacks); |
| 1192 | INIT_LIST_HEAD(&clp->cl_revoked); |
| 1193 | spin_lock_init(&clp->cl_lock); |
| 1194 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | return clp; |
| 1196 | } |
| 1197 | |
Trond Myklebust | 4dd86e15 | 2014-04-18 14:43:58 -0400 | [diff] [blame] | 1198 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | free_client(struct nfs4_client *clp) |
| 1200 | { |
Stanislav Kinsbursky | bca0ec6 | 2013-01-09 12:38:34 +0300 | [diff] [blame] | 1201 | 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] | 1202 | |
| 1203 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1204 | while (!list_empty(&clp->cl_sessions)) { |
| 1205 | struct nfsd4_session *ses; |
| 1206 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 1207 | se_perclnt); |
| 1208 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1209 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 1210 | free_session(ses); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1211 | } |
Trond Myklebust | 4cb57e3 | 2014-04-18 14:43:57 -0400 | [diff] [blame] | 1212 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1213 | free_svc_cred(&clp->cl_cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | kfree(clp->cl_name.data); |
majianpeng | 2d32b29 | 2013-01-29 13:16:06 +0800 | [diff] [blame] | 1215 | idr_destroy(&clp->cl_stateids); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | kfree(clp); |
| 1217 | } |
| 1218 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1219 | /* must be called under the client_lock */ |
| 1220 | static inline void |
| 1221 | unhash_client_locked(struct nfs4_client *clp) |
| 1222 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1223 | struct nfsd4_session *ses; |
| 1224 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1225 | list_del(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1226 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1227 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 1228 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1229 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1230 | } |
| 1231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | static void |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1233 | destroy_client(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1235 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | struct list_head reaplist; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1238 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1241 | spin_lock(&state_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1242 | while (!list_empty(&clp->cl_delegations)) { |
| 1243 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1244 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | list_move(&dp->dl_recall_lru, &reaplist); |
| 1246 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1247 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | while (!list_empty(&reaplist)) { |
| 1249 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1250 | destroy_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | } |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 1252 | list_splice_init(&clp->cl_revoked, &reaplist); |
| 1253 | while (!list_empty(&reaplist)) { |
| 1254 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
| 1255 | destroy_revoked_delegation(dp); |
| 1256 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1257 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1258 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
| 1259 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | } |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 1261 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 1262 | if (clp->cl_cb_conn.cb_xprt) |
| 1263 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1264 | list_del(&clp->cl_idhash); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1265 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1266 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1267 | else |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1268 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1269 | spin_lock(&nn->client_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1270 | unhash_client_locked(clp); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 1271 | WARN_ON_ONCE(atomic_read(&clp->cl_refcount)); |
| 1272 | free_client(clp); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1273 | spin_unlock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1276 | static void expire_client(struct nfs4_client *clp) |
| 1277 | { |
| 1278 | nfsd4_client_record_remove(clp); |
| 1279 | destroy_client(clp); |
| 1280 | } |
| 1281 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1282 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 1283 | { |
| 1284 | memcpy(target->cl_verifier.data, source->data, |
| 1285 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1288 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 1289 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 1291 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 1292 | } |
| 1293 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1294 | 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] | 1295 | { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1296 | if (source->cr_principal) { |
| 1297 | target->cr_principal = |
| 1298 | kstrdup(source->cr_principal, GFP_KERNEL); |
| 1299 | if (target->cr_principal == NULL) |
| 1300 | return -ENOMEM; |
| 1301 | } else |
| 1302 | target->cr_principal = NULL; |
J. Bruce Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 1303 | target->cr_flavor = source->cr_flavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | target->cr_uid = source->cr_uid; |
| 1305 | target->cr_gid = source->cr_gid; |
| 1306 | target->cr_group_info = source->cr_group_info; |
| 1307 | get_group_info(target->cr_group_info); |
J. Bruce Fields | 0dc1531 | 2013-05-14 16:07:13 -0400 | [diff] [blame] | 1308 | target->cr_gss_mech = source->cr_gss_mech; |
| 1309 | if (source->cr_gss_mech) |
| 1310 | gss_mech_get(source->cr_gss_mech); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1311 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1314 | static long long |
| 1315 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 1316 | { |
| 1317 | long long res; |
| 1318 | |
| 1319 | res = o1->len - o2->len; |
| 1320 | if (res) |
| 1321 | return res; |
| 1322 | return (long long)memcmp(o1->data, o2->data, o1->len); |
| 1323 | } |
| 1324 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1325 | static int same_name(const char *n1, const char *n2) |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1326 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1327 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1331 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 1332 | { |
| 1333 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1337 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 1338 | { |
| 1339 | 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] | 1340 | } |
| 1341 | |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1342 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 1343 | { |
| 1344 | int i; |
| 1345 | |
| 1346 | if (g1->ngroups != g2->ngroups) |
| 1347 | return false; |
| 1348 | for (i=0; i<g1->ngroups; i++) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1349 | if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i))) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1350 | return false; |
| 1351 | return true; |
| 1352 | } |
| 1353 | |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1354 | /* |
| 1355 | * RFC 3530 language requires clid_inuse be returned when the |
| 1356 | * "principal" associated with a requests differs from that previously |
| 1357 | * used. We use uid, gid's, and gss principal string as our best |
| 1358 | * approximation. We also don't want to allow non-gss use of a client |
| 1359 | * established using gss: in theory cr_principal should catch that |
| 1360 | * change, but in practice cr_principal can be null even in the gss case |
| 1361 | * since gssd doesn't always pass down a principal string. |
| 1362 | */ |
| 1363 | static bool is_gss_cred(struct svc_cred *cr) |
| 1364 | { |
| 1365 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 1366 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 1367 | } |
| 1368 | |
| 1369 | |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1370 | static bool |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1371 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 1372 | { |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1373 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1374 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 1375 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1376 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 1377 | return false; |
| 1378 | if (cr1->cr_principal == cr2->cr_principal) |
| 1379 | return true; |
| 1380 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 1381 | return false; |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1382 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1385 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 1386 | { |
| 1387 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1388 | u32 service; |
| 1389 | |
J. Bruce Fields | c472059 | 2013-08-07 11:41:49 -0400 | [diff] [blame] | 1390 | if (!cr->cr_gss_mech) |
| 1391 | return false; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1392 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 1393 | return service == RPC_GSS_SVC_INTEGRITY || |
| 1394 | service == RPC_GSS_SVC_PRIVACY; |
| 1395 | } |
| 1396 | |
| 1397 | static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) |
| 1398 | { |
| 1399 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1400 | |
| 1401 | if (!cl->cl_mach_cred) |
| 1402 | return true; |
| 1403 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 1404 | return false; |
| 1405 | if (!svc_rqst_integrity_protected(rqstp)) |
| 1406 | return false; |
| 1407 | if (!cr->cr_principal) |
| 1408 | return false; |
| 1409 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 1410 | } |
| 1411 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1412 | 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] | 1413 | { |
| 1414 | static u32 current_clientid = 1; |
| 1415 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1416 | clp->cl_clientid.cl_boot = nn->boot_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | clp->cl_clientid.cl_id = current_clientid++; |
| 1418 | } |
| 1419 | |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 1420 | static void gen_confirm(struct nfs4_client *clp) |
| 1421 | { |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1422 | __be32 verf[2]; |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 1423 | static u32 i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
Jeff Layton | f419992 | 2014-06-17 07:44:11 -0400 | [diff] [blame] | 1425 | /* |
| 1426 | * This is opaque to client, so no need to byte-swap. Use |
| 1427 | * __force to keep sparse happy |
| 1428 | */ |
| 1429 | verf[0] = (__force __be32)get_seconds(); |
| 1430 | verf[1] = (__force __be32)i++; |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1431 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 1434 | static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t) |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1435 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 1436 | struct nfs4_stid *ret; |
| 1437 | |
| 1438 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 1439 | if (!ret || !ret->sc_type) |
| 1440 | return NULL; |
| 1441 | return ret; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 1442 | } |
| 1443 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 1444 | static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask) |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 1445 | { |
| 1446 | struct nfs4_stid *s; |
| 1447 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 1448 | s = find_stateid(cl, t); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 1449 | if (!s) |
| 1450 | return NULL; |
| 1451 | if (typemask & s->sc_type) |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 1452 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1453 | return NULL; |
| 1454 | } |
| 1455 | |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 1456 | static struct nfs4_client *create_client(struct xdr_netobj name, |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1457 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 1458 | { |
| 1459 | struct nfs4_client *clp; |
| 1460 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1461 | int ret; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1462 | struct net *net = SVC_NET(rqstp); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1463 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1464 | |
| 1465 | clp = alloc_client(name); |
| 1466 | if (clp == NULL) |
| 1467 | return NULL; |
| 1468 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1469 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 1470 | if (ret) { |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1471 | spin_lock(&nn->client_lock); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1472 | free_client(clp); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 1473 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1474 | return NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1475 | } |
J. Bruce Fields | 5772515 | 2012-11-05 15:10:26 -0500 | [diff] [blame] | 1476 | nfsd4_init_callback(&clp->cl_cb_null); |
Benny Halevy | 07cd490 | 2010-05-12 00:13:41 +0300 | [diff] [blame] | 1477 | clp->cl_time = get_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1478 | clear_bit(0, &clp->cl_cb_slot_busy); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1479 | copy_verf(clp, verf); |
| 1480 | rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1481 | gen_confirm(clp); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1482 | clp->cl_cb_session = NULL; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1483 | clp->net = net; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1484 | return clp; |
| 1485 | } |
| 1486 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1487 | static void |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1488 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 1489 | { |
| 1490 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 1491 | struct nfs4_client *clp; |
| 1492 | |
| 1493 | while (*new) { |
| 1494 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 1495 | parent = *new; |
| 1496 | |
| 1497 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 1498 | new = &((*new)->rb_left); |
| 1499 | else |
| 1500 | new = &((*new)->rb_right); |
| 1501 | } |
| 1502 | |
| 1503 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 1504 | rb_insert_color(&new_clp->cl_namenode, root); |
| 1505 | } |
| 1506 | |
| 1507 | static struct nfs4_client * |
| 1508 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 1509 | { |
| 1510 | long long cmp; |
| 1511 | struct rb_node *node = root->rb_node; |
| 1512 | struct nfs4_client *clp; |
| 1513 | |
| 1514 | while (node) { |
| 1515 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 1516 | cmp = compare_blob(&clp->cl_name, name); |
| 1517 | if (cmp > 0) |
| 1518 | node = node->rb_left; |
| 1519 | else if (cmp < 0) |
| 1520 | node = node->rb_right; |
| 1521 | else |
| 1522 | return clp; |
| 1523 | } |
| 1524 | return NULL; |
| 1525 | } |
| 1526 | |
| 1527 | static void |
| 1528 | add_to_unconfirmed(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | { |
| 1530 | unsigned int idhashval; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1531 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1533 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1534 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1536 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 1537 | renew_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1540 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | move_to_confirmed(struct nfs4_client *clp) |
| 1542 | { |
| 1543 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 1544 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | |
| 1546 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 1547 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1548 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1549 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1550 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | renew_client(clp); |
| 1552 | } |
| 1553 | |
| 1554 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1555 | 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] | 1556 | { |
| 1557 | struct nfs4_client *clp; |
| 1558 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 1559 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1560 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1561 | if (same_clid(&clp->cl_clientid, clid)) { |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 1562 | if ((bool)clp->cl_minorversion != sessions) |
| 1563 | return NULL; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1564 | renew_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | return clp; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | } |
| 1568 | return NULL; |
| 1569 | } |
| 1570 | |
| 1571 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1572 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 1573 | { |
| 1574 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 1575 | |
| 1576 | return find_client_in_id_table(tbl, clid, sessions); |
| 1577 | } |
| 1578 | |
| 1579 | static struct nfs4_client * |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1580 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | { |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1582 | struct list_head *tbl = nn->unconf_id_hashtbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1584 | return find_client_in_id_table(tbl, clid, sessions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 1587 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 1588 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 1589 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 1590 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 1591 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1592 | static struct nfs4_client * |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1593 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1594 | { |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1595 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | static struct nfs4_client * |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1599 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1600 | { |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1601 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1604 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 1605 | 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] | 1606 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1607 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 1608 | struct sockaddr *sa = svc_addr(rqstp); |
| 1609 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 1610 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 1612 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 1613 | if (se->se_callback_netid_len == 3 && |
| 1614 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 1615 | expected_family = AF_INET; |
| 1616 | else if (se->se_callback_netid_len == 4 && |
| 1617 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 1618 | expected_family = AF_INET6; |
| 1619 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | goto out_err; |
| 1621 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1622 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1623 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1624 | (struct sockaddr *)&conn->cb_addr, |
| 1625 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1626 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1627 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1629 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1630 | if (conn->cb_addr.ss_family == AF_INET6) |
| 1631 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 1632 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1633 | conn->cb_prog = se->se_callback_prog; |
| 1634 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 1635 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | return; |
| 1637 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1638 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 1639 | conn->cb_addrlen = 0; |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 1640 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | "will not receive delegations\n", |
| 1642 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 1643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | return; |
| 1645 | } |
| 1646 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1647 | /* |
J. Bruce Fields | 067e1ac | 2014-03-21 17:26:44 -0400 | [diff] [blame] | 1648 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1649 | */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 1650 | static void |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1651 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 1652 | { |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1653 | struct xdr_buf *buf = resp->xdr.buf; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1654 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 1655 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1656 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1657 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1658 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1659 | slot->sl_opcnt = resp->opcnt; |
| 1660 | slot->sl_status = resp->cstate.status; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1661 | |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 1662 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1663 | if (nfsd4_not_cached(resp)) { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1664 | slot->sl_datalen = 0; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1665 | return; |
| 1666 | } |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1667 | base = resp->cstate.data_offset; |
| 1668 | slot->sl_datalen = buf->len - base; |
| 1669 | 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] | 1670 | WARN("%s: sessions DRC could not cache compound\n", __func__); |
| 1671 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1675 | * Encode the replay sequence operation from the slot values. |
| 1676 | * If cachethis is FALSE encode the uncached rep error on the next |
| 1677 | * operation which sets resp->p and increments resp->opcnt for |
| 1678 | * nfs4svc_encode_compoundres. |
| 1679 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1680 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1681 | static __be32 |
| 1682 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 1683 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1684 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1685 | struct nfsd4_op *op; |
| 1686 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1687 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1688 | /* Encode the replayed sequence operation */ |
| 1689 | op = &args->ops[resp->opcnt - 1]; |
| 1690 | nfsd4_encode_operation(resp, op); |
| 1691 | |
| 1692 | /* Return nfserr_retry_uncached_rep in next operation. */ |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 1693 | if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1694 | op = &args->ops[resp->opcnt++]; |
| 1695 | op->status = nfserr_retry_uncached_rep; |
| 1696 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1697 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1698 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1702 | * The sequence operation is not cached because we can use the slot and |
| 1703 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1704 | */ |
J. Bruce Fields | 3ca2eb9 | 2014-03-21 12:04:21 -0400 | [diff] [blame] | 1705 | static __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1706 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 1707 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1708 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1709 | struct nfsd4_slot *slot = resp->cstate.slot; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1710 | struct xdr_stream *xdr = &resp->xdr; |
| 1711 | __be32 *p; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1712 | __be32 status; |
| 1713 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1714 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1715 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1716 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
J. Bruce Fields | 0da7b19 | 2014-03-21 11:43:34 -0400 | [diff] [blame] | 1717 | if (status) |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1718 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1719 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1720 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 1721 | if (!p) { |
| 1722 | WARN_ON_ONCE(1); |
| 1723 | return nfserr_serverfault; |
| 1724 | } |
| 1725 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 1726 | xdr_commit_encode(xdr); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1727 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1728 | resp->opcnt = slot->sl_opcnt; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 1729 | return slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1730 | } |
| 1731 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1732 | /* |
| 1733 | * Set the exchange_id flags returned by the server. |
| 1734 | */ |
| 1735 | static void |
| 1736 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 1737 | { |
| 1738 | /* pNFS is not supported */ |
| 1739 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
| 1740 | |
| 1741 | /* Referrals are supported, Migration is not. */ |
| 1742 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 1743 | |
| 1744 | /* set the wire flags to return to client. */ |
| 1745 | clid->flags = new->cl_exchange_flags; |
| 1746 | } |
| 1747 | |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 1748 | static bool client_has_state(struct nfs4_client *clp) |
| 1749 | { |
| 1750 | /* |
| 1751 | * Note clp->cl_openowners check isn't quite right: there's no |
| 1752 | * need to count owners without stateid's. |
| 1753 | * |
| 1754 | * Also note we should probably be using this in 4.0 case too. |
| 1755 | */ |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 1756 | return !list_empty(&clp->cl_openowners) |
| 1757 | || !list_empty(&clp->cl_delegations) |
| 1758 | || !list_empty(&clp->cl_sessions); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 1759 | } |
| 1760 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1761 | __be32 |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1762 | nfsd4_exchange_id(struct svc_rqst *rqstp, |
| 1763 | struct nfsd4_compound_state *cstate, |
| 1764 | struct nfsd4_exchange_id *exid) |
| 1765 | { |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1766 | struct nfs4_client *unconf, *conf, *new; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 1767 | __be32 status; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1768 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1769 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1770 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 1771 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1772 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1773 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1774 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1775 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1776 | "ip_addr=%s flags %x, spa_how %d\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1777 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1778 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1779 | |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 1780 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1781 | return nfserr_inval; |
| 1782 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1783 | switch (exid->spa_how) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1784 | case SP4_MACH_CRED: |
| 1785 | if (!svc_rqst_integrity_protected(rqstp)) |
| 1786 | return nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1787 | case SP4_NONE: |
| 1788 | break; |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 1789 | default: /* checked by xdr code */ |
| 1790 | WARN_ON_ONCE(1); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1791 | case SP4_SSV: |
J. Bruce Fields | dd30333 | 2013-04-12 18:10:56 -0400 | [diff] [blame] | 1792 | return nfserr_encr_alg_unsupp; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1793 | } |
| 1794 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 1795 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1796 | nfs4_lock_state(); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1797 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1798 | if (conf) { |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 1799 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 1800 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 1801 | |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1802 | if (update) { |
| 1803 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 1804 | status = nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1805 | goto out; |
| 1806 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1807 | if (!mach_creds_match(conf, rqstp)) { |
| 1808 | status = nfserr_wrong_cred; |
| 1809 | goto out; |
| 1810 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1811 | if (!creds_match) { /* case 9 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1812 | status = nfserr_perm; |
| 1813 | goto out; |
| 1814 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1815 | if (!verfs_match) { /* case 8 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1816 | status = nfserr_not_same; |
| 1817 | goto out; |
| 1818 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1819 | /* case 6 */ |
| 1820 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
| 1821 | new = conf; |
| 1822 | goto out_copy; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1823 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1824 | if (!creds_match) { /* case 3 */ |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 1825 | if (client_has_state(conf)) { |
| 1826 | status = nfserr_clid_inuse; |
| 1827 | goto out; |
| 1828 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1829 | expire_client(conf); |
| 1830 | goto out_new; |
| 1831 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1832 | if (verfs_match) { /* case 2 */ |
J. Bruce Fields | 0f1ba0e | 2012-05-25 21:24:40 -0400 | [diff] [blame] | 1833 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1834 | new = conf; |
| 1835 | goto out_copy; |
| 1836 | } |
| 1837 | /* case 5, client reboot */ |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 1838 | goto out_new; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 1839 | } |
| 1840 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 1841 | if (update) { /* case 7 */ |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 1842 | status = nfserr_noent; |
| 1843 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1844 | } |
| 1845 | |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1846 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 1847 | if (unconf) /* case 4, possible retry or client restart */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1848 | expire_client(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1849 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 1850 | /* case 1 (normal case) */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1851 | out_new: |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 1852 | new = create_client(exid->clname, rqstp, &verf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1853 | if (new == NULL) { |
J. Bruce Fields | 4731030 | 2010-06-22 16:17:12 -0400 | [diff] [blame] | 1854 | status = nfserr_jukebox; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1855 | goto out; |
| 1856 | } |
J. Bruce Fields | 4f540e2 | 2013-05-07 11:57:52 -0400 | [diff] [blame] | 1857 | new->cl_minorversion = cstate->minorversion; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1858 | new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1859 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1860 | gen_clid(new, nn); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1861 | add_to_unconfirmed(new); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1862 | out_copy: |
| 1863 | exid->clientid.cl_boot = new->cl_clientid.cl_boot; |
| 1864 | exid->clientid.cl_id = new->cl_clientid.cl_id; |
| 1865 | |
J. Bruce Fields | 778df3f | 2012-05-25 21:40:23 -0400 | [diff] [blame] | 1866 | exid->seqid = new->cl_cs_slot.sl_seqid + 1; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1867 | nfsd4_set_ex_flags(new, exid); |
| 1868 | |
| 1869 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1870 | new->cl_cs_slot.sl_seqid, new->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1871 | status = nfs_ok; |
| 1872 | |
| 1873 | out: |
| 1874 | nfs4_unlock_state(); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1875 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1876 | } |
| 1877 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 1878 | static __be32 |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 1879 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1880 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 1881 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 1882 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1883 | |
| 1884 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 1885 | if (slot_inuse) { |
| 1886 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1887 | return nfserr_jukebox; |
| 1888 | else |
| 1889 | return nfserr_seq_misordered; |
| 1890 | } |
J. Bruce Fields | f6d8248 | 2012-02-13 16:13:41 -0500 | [diff] [blame] | 1891 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 1892 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1893 | return nfs_ok; |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 1894 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1895 | return nfserr_replay_cache; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1896 | return nfserr_seq_misordered; |
| 1897 | } |
| 1898 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1899 | /* |
| 1900 | * Cache the create session result into the create session single DRC |
| 1901 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 1902 | * Do this for solo or embedded create session operations. |
| 1903 | */ |
| 1904 | static void |
| 1905 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 1906 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1907 | { |
| 1908 | slot->sl_status = nfserr; |
| 1909 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 1910 | } |
| 1911 | |
| 1912 | static __be32 |
| 1913 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 1914 | struct nfsd4_clid_slot *slot) |
| 1915 | { |
| 1916 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 1917 | return slot->sl_status; |
| 1918 | } |
| 1919 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 1920 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 1921 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 1922 | 1 + /* MIN tag is length with zero, only length */ \ |
| 1923 | 3 + /* version, opcount, opcode */ \ |
| 1924 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 1925 | /* seqid, slotID, slotID, cache */ \ |
| 1926 | 4 ) * sizeof(__be32)) |
| 1927 | |
| 1928 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 1929 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 1930 | 1 + /* status */ \ |
| 1931 | 1 + /* MIN tag is length with zero, only length */ \ |
| 1932 | 3 + /* opcount, opcode, opstatus*/ \ |
| 1933 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 1934 | /* seqid, slotID, slotID, slotID, status */ \ |
| 1935 | 5 ) * sizeof(__be32)) |
| 1936 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1937 | 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] | 1938 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1939 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 1940 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 1941 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 1942 | return nfserr_toosmall; |
| 1943 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 1944 | return nfserr_toosmall; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1945 | ca->headerpadsz = 0; |
| 1946 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 1947 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 1948 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 1949 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 1950 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 1951 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 1952 | /* |
| 1953 | * Note decreasing slot size below client's request may make it |
| 1954 | * difficult for client to function correctly, whereas |
| 1955 | * decreasing the number of slots will (just?) affect |
| 1956 | * performance. When short on memory we therefore prefer to |
| 1957 | * decrease number of slots instead of their size. Clients that |
| 1958 | * request larger slots than they need will get poor results: |
| 1959 | */ |
| 1960 | ca->maxreqs = nfsd4_get_drc_mem(ca); |
| 1961 | if (!ca->maxreqs) |
| 1962 | return nfserr_jukebox; |
| 1963 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 1964 | return nfs_ok; |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 1965 | } |
| 1966 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 1967 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
| 1968 | RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32)) |
| 1969 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
| 1970 | RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32)) |
| 1971 | |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 1972 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 1973 | { |
| 1974 | ca->headerpadsz = 0; |
| 1975 | |
| 1976 | /* |
| 1977 | * These RPC_MAX_HEADER macros are overkill, especially since we |
| 1978 | * don't even do gss on the backchannel yet. But this is still |
| 1979 | * less than 1k. Tighten up this estimate in the unlikely event |
| 1980 | * it turns out to be a problem for some client: |
| 1981 | */ |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 1982 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 1983 | return nfserr_toosmall; |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 1984 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 1985 | return nfserr_toosmall; |
| 1986 | ca->maxresp_cached = 0; |
| 1987 | if (ca->maxops < 2) |
| 1988 | return nfserr_toosmall; |
| 1989 | |
| 1990 | return nfs_ok; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1991 | } |
| 1992 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 1993 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 1994 | { |
| 1995 | switch (cbs->flavor) { |
| 1996 | case RPC_AUTH_NULL: |
| 1997 | case RPC_AUTH_UNIX: |
| 1998 | return nfs_ok; |
| 1999 | default: |
| 2000 | /* |
| 2001 | * GSS case: the spec doesn't allow us to return this |
| 2002 | * error. But it also doesn't allow us not to support |
| 2003 | * GSS. |
| 2004 | * I'd rather this fail hard than return some error the |
| 2005 | * client might think it can already handle: |
| 2006 | */ |
| 2007 | return nfserr_encr_alg_unsupp; |
| 2008 | } |
| 2009 | } |
| 2010 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2011 | __be32 |
| 2012 | nfsd4_create_session(struct svc_rqst *rqstp, |
| 2013 | struct nfsd4_compound_state *cstate, |
| 2014 | struct nfsd4_create_session *cr_ses) |
| 2015 | { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2016 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2017 | struct nfs4_client *conf, *unconf; |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2018 | struct nfsd4_session *new; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2019 | struct nfsd4_conn *conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2020 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2021 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2022 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2023 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2024 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 2025 | return nfserr_inval; |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2026 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 2027 | if (status) |
| 2028 | return status; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2029 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2030 | if (status) |
| 2031 | return status; |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2032 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 2033 | if (status) |
Kinglong Mee | f403e45 | 2013-12-23 17:31:21 +0800 | [diff] [blame] | 2034 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2035 | status = nfserr_jukebox; |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 2036 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2037 | if (!new) |
| 2038 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2039 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 2040 | if (!conn) |
| 2041 | goto out_free_session; |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2042 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2043 | nfs4_lock_state(); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2044 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2045 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2046 | WARN_ON_ONCE(conf && unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2047 | |
| 2048 | if (conf) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2049 | status = nfserr_wrong_cred; |
| 2050 | if (!mach_creds_match(conf, rqstp)) |
| 2051 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2052 | cs_slot = &conf->cl_cs_slot; |
| 2053 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 2054 | if (status == nfserr_replay_cache) { |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2055 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2056 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2057 | } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2058 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2059 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2060 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2061 | } else if (unconf) { |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2062 | struct nfs4_client *old; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2063 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2064 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2065 | status = nfserr_clid_inuse; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2066 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2067 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2068 | status = nfserr_wrong_cred; |
| 2069 | if (!mach_creds_match(unconf, rqstp)) |
| 2070 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2071 | cs_slot = &unconf->cl_cs_slot; |
| 2072 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 2073 | if (status) { |
| 2074 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2075 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2076 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2077 | } |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2078 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2079 | if (old) { |
| 2080 | status = mark_client_expired(old); |
| 2081 | if (status) |
| 2082 | goto out_free_conn; |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2083 | expire_client(old); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2084 | } |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2085 | move_to_confirmed(unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2086 | conf = unconf; |
| 2087 | } else { |
| 2088 | status = nfserr_stale_clientid; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2089 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2090 | } |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2091 | status = nfs_ok; |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 2092 | /* |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 2093 | * We do not support RDMA or persistent sessions |
| 2094 | */ |
| 2095 | cr_ses->flags &= ~SESSION4_PERSIST; |
| 2096 | cr_ses->flags &= ~SESSION4_RDMA; |
| 2097 | |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2098 | init_session(rqstp, new, conf, cr_ses); |
| 2099 | nfsd4_init_conn(rqstp, conn, new); |
| 2100 | |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2101 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2102 | NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 2103 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2104 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2105 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2106 | /* cache solo and embedded create sessions under the state lock */ |
| 2107 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2108 | nfs4_unlock_state(); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2109 | return status; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2110 | out_free_conn: |
Yanchuan Nian | 266533c | 2012-12-24 18:11:45 +0800 | [diff] [blame] | 2111 | nfs4_unlock_state(); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2112 | free_conn(conn); |
| 2113 | out_free_session: |
| 2114 | __free_session(new); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2115 | out_release_drc_mem: |
| 2116 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
J. Bruce Fields | 1ca5079 | 2013-03-14 18:12:03 -0400 | [diff] [blame] | 2117 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2118 | } |
| 2119 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2120 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 2121 | { |
| 2122 | switch (*dir) { |
| 2123 | case NFS4_CDFC4_FORE: |
| 2124 | case NFS4_CDFC4_BACK: |
| 2125 | return nfs_ok; |
| 2126 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 2127 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 2128 | *dir = NFS4_CDFC4_BOTH; |
| 2129 | return nfs_ok; |
| 2130 | }; |
| 2131 | return nfserr_inval; |
| 2132 | } |
| 2133 | |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2134 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc) |
| 2135 | { |
| 2136 | struct nfsd4_session *session = cstate->session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2137 | 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] | 2138 | __be32 status; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2139 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2140 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 2141 | if (status) |
| 2142 | return status; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2143 | spin_lock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2144 | session->se_cb_prog = bc->bc_cb_program; |
| 2145 | session->se_cb_sec = bc->bc_cb_sec; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2146 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2147 | |
| 2148 | nfsd4_probe_callback(session->se_client); |
| 2149 | |
| 2150 | return nfs_ok; |
| 2151 | } |
| 2152 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2153 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 2154 | struct nfsd4_compound_state *cstate, |
| 2155 | struct nfsd4_bind_conn_to_session *bcts) |
| 2156 | { |
| 2157 | __be32 status; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2158 | struct nfsd4_conn *conn; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2159 | struct nfsd4_session *session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2160 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2161 | |
| 2162 | if (!nfsd4_last_compound_op(rqstp)) |
| 2163 | return nfserr_not_only_op; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2164 | nfs4_lock_state(); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2165 | spin_lock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2166 | session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp)); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2167 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2168 | status = nfserr_badsession; |
| 2169 | if (!session) |
| 2170 | goto out; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2171 | status = nfserr_wrong_cred; |
| 2172 | if (!mach_creds_match(session->se_client, rqstp)) |
| 2173 | goto out; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2174 | status = nfsd4_map_bcts_dir(&bcts->dir); |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2175 | if (status) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2176 | goto out; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2177 | conn = alloc_conn(rqstp, bcts->dir); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2178 | status = nfserr_jukebox; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2179 | if (!conn) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2180 | goto out; |
| 2181 | nfsd4_init_conn(rqstp, conn, session); |
| 2182 | status = nfs_ok; |
| 2183 | out: |
| 2184 | nfs4_unlock_state(); |
| 2185 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2186 | } |
| 2187 | |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2188 | static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid) |
| 2189 | { |
| 2190 | if (!session) |
| 2191 | return 0; |
| 2192 | return !memcmp(sid, &session->se_sessionid, sizeof(*sid)); |
| 2193 | } |
| 2194 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2195 | __be32 |
| 2196 | nfsd4_destroy_session(struct svc_rqst *r, |
| 2197 | struct nfsd4_compound_state *cstate, |
| 2198 | struct nfsd4_destroy_session *sessionid) |
| 2199 | { |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2200 | struct nfsd4_session *ses; |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2201 | __be32 status; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2202 | int ref_held_by_me = 0; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2203 | struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2204 | |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2205 | nfs4_lock_state(); |
| 2206 | status = nfserr_not_only_op; |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2207 | if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2208 | if (!nfsd4_last_compound_op(r)) |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2209 | goto out; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2210 | ref_held_by_me++; |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2211 | } |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2212 | dump_sessionid(__func__, &sessionid->sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2213 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 2214 | ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r)); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2215 | status = nfserr_badsession; |
| 2216 | if (!ses) |
| 2217 | goto out_client_lock; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2218 | status = nfserr_wrong_cred; |
| 2219 | if (!mach_creds_match(ses->se_client, r)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2220 | goto out_client_lock; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2221 | nfsd4_get_session_locked(ses); |
| 2222 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2223 | if (status) |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2224 | goto out_put_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2225 | unhash_session(ses); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2226 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2227 | |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 2228 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 2229 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2230 | spin_lock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2231 | status = nfs_ok; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2232 | out_put_session: |
| 2233 | nfsd4_put_session(ses); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2234 | out_client_lock: |
| 2235 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2236 | out: |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2237 | nfs4_unlock_state(); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2238 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2239 | } |
| 2240 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2241 | 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] | 2242 | { |
| 2243 | struct nfsd4_conn *c; |
| 2244 | |
| 2245 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2246 | if (c->cn_xprt == xpt) { |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2247 | return c; |
| 2248 | } |
| 2249 | } |
| 2250 | return NULL; |
| 2251 | } |
| 2252 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2253 | 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] | 2254 | { |
| 2255 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2256 | struct nfsd4_conn *c; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2257 | __be32 status = nfs_ok; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2258 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2259 | |
| 2260 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2261 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2262 | if (c) |
| 2263 | goto out_free; |
| 2264 | status = nfserr_conn_not_bound_to_session; |
| 2265 | if (clp->cl_mach_cred) |
| 2266 | goto out_free; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2267 | __nfsd4_hash_conn(new, ses); |
| 2268 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2269 | ret = nfsd4_register_conn(new); |
| 2270 | if (ret) |
| 2271 | /* oops; xprt is already down: */ |
| 2272 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2273 | return nfs_ok; |
| 2274 | out_free: |
| 2275 | spin_unlock(&clp->cl_lock); |
| 2276 | free_conn(new); |
| 2277 | return status; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2278 | } |
| 2279 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2280 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 2281 | { |
| 2282 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 2283 | |
| 2284 | return args->opcnt > session->se_fchannel.maxops; |
| 2285 | } |
| 2286 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2287 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 2288 | struct nfsd4_session *session) |
| 2289 | { |
| 2290 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 2291 | |
| 2292 | return xb->len > session->se_fchannel.maxreq_sz; |
| 2293 | } |
| 2294 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2295 | __be32 |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2296 | nfsd4_sequence(struct svc_rqst *rqstp, |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2297 | struct nfsd4_compound_state *cstate, |
| 2298 | struct nfsd4_sequence *seq) |
| 2299 | { |
Andy Adamson | f9bb94c | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2300 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2301 | struct xdr_stream *xdr = &resp->xdr; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2302 | struct nfsd4_session *session; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2303 | struct nfs4_client *clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2304 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2305 | struct nfsd4_conn *conn; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2306 | __be32 status; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2307 | int buflen; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2308 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2309 | |
Andy Adamson | f9bb94c | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2310 | if (resp->opcnt != 1) |
| 2311 | return nfserr_sequence_pos; |
| 2312 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2313 | /* |
| 2314 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 2315 | * below. |
| 2316 | */ |
| 2317 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 2318 | if (!conn) |
| 2319 | return nfserr_jukebox; |
| 2320 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2321 | spin_lock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2322 | status = nfserr_badsession; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 2323 | session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp)); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2324 | if (!session) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2325 | goto out_no_session; |
| 2326 | clp = session->se_client; |
| 2327 | status = get_client_locked(clp); |
| 2328 | if (status) |
| 2329 | goto out_no_session; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2330 | status = nfsd4_get_session_locked(session); |
| 2331 | if (status) |
| 2332 | goto out_put_client; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2333 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2334 | status = nfserr_too_many_ops; |
| 2335 | if (nfsd4_session_too_many_ops(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2336 | goto out_put_session; |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2337 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2338 | status = nfserr_req_too_big; |
| 2339 | if (nfsd4_request_too_big(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2340 | goto out_put_session; |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2341 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2342 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 2343 | if (seq->slotid >= session->se_fchannel.maxreqs) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2344 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2345 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2346 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2347 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 2348 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 2349 | /* We do not negotiate the number of slots yet, so set the |
| 2350 | * maxslots to the session maxreqs which is used to encode |
| 2351 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 2352 | seq->maxslots = session->se_fchannel.maxreqs; |
| 2353 | |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2354 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 2355 | slot->sl_flags & NFSD4_SLOT_INUSE); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2356 | if (status == nfserr_replay_cache) { |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2357 | status = nfserr_seq_misordered; |
| 2358 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2359 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2360 | cstate->slot = slot; |
| 2361 | cstate->session = session; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2362 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2363 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2364 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2365 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 2366 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2367 | } |
| 2368 | if (status) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2369 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2370 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2371 | status = nfsd4_sequence_check_conn(conn, session); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2372 | conn = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2373 | if (status) |
| 2374 | goto out_put_session; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2375 | |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2376 | buflen = (seq->cachethis) ? |
| 2377 | session->se_fchannel.maxresp_cached : |
| 2378 | session->se_fchannel.maxresp_sz; |
| 2379 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 2380 | nfserr_rep_too_big; |
J. Bruce Fields | a5cddc8 | 2014-05-12 18:10:58 -0400 | [diff] [blame] | 2381 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2382 | goto out_put_session; |
J. Bruce Fields | 32aaa62 | 2014-03-20 20:47:41 -0400 | [diff] [blame] | 2383 | svc_reserve(rqstp, buflen); |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2384 | |
| 2385 | status = nfs_ok; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2386 | /* Success! bump slot seqid */ |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2387 | slot->sl_seqid = seq->seqid; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2388 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2389 | if (seq->cachethis) |
| 2390 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2391 | else |
| 2392 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2393 | |
| 2394 | cstate->slot = slot; |
| 2395 | cstate->session = session; |
| 2396 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2397 | out: |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2398 | switch (clp->cl_cb_state) { |
| 2399 | case NFSD4_CB_DOWN: |
| 2400 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 2401 | break; |
| 2402 | case NFSD4_CB_FAULT: |
| 2403 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 2404 | break; |
| 2405 | default: |
| 2406 | seq->status_flags = 0; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 2407 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 2408 | if (!list_empty(&clp->cl_revoked)) |
| 2409 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2410 | out_no_session: |
Kinglong Mee | 3f42d2c | 2014-03-24 11:56:59 +0800 | [diff] [blame] | 2411 | if (conn) |
| 2412 | free_conn(conn); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2413 | spin_unlock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2414 | return status; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2415 | out_put_session: |
| 2416 | nfsd4_put_session(session); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2417 | out_put_client: |
| 2418 | put_client_renew_locked(clp); |
| 2419 | goto out_no_session; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2420 | } |
| 2421 | |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2422 | void |
| 2423 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 2424 | { |
| 2425 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 2426 | |
| 2427 | if (nfsd4_has_session(cs)) { |
| 2428 | struct nfs4_client *clp = cs->session->se_client; |
| 2429 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2430 | |
| 2431 | if (cs->status != nfserr_replay_cache) { |
| 2432 | nfsd4_store_cache_entry(resp); |
| 2433 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 2434 | } |
| 2435 | /* Renew the clientid on success and on replay */ |
| 2436 | spin_lock(&nn->client_lock); |
| 2437 | nfsd4_put_session(cs->session); |
| 2438 | put_client_renew_locked(clp); |
| 2439 | spin_unlock(&nn->client_lock); |
| 2440 | } |
| 2441 | } |
| 2442 | |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2443 | __be32 |
| 2444 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc) |
| 2445 | { |
| 2446 | struct nfs4_client *conf, *unconf, *clp; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2447 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2448 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2449 | |
| 2450 | nfs4_lock_state(); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2451 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2452 | conf = find_confirmed_client(&dc->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2453 | WARN_ON_ONCE(conf && unconf); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2454 | |
| 2455 | if (conf) { |
| 2456 | clp = conf; |
| 2457 | |
J. Bruce Fields | c0293b0 | 2013-03-14 18:20:01 -0400 | [diff] [blame] | 2458 | if (client_has_state(conf)) { |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2459 | status = nfserr_clientid_busy; |
| 2460 | goto out; |
| 2461 | } |
| 2462 | } else if (unconf) |
| 2463 | clp = unconf; |
| 2464 | else { |
| 2465 | status = nfserr_stale_clientid; |
| 2466 | goto out; |
| 2467 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2468 | if (!mach_creds_match(clp, rqstp)) { |
| 2469 | status = nfserr_wrong_cred; |
| 2470 | goto out; |
| 2471 | } |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2472 | expire_client(clp); |
| 2473 | out: |
| 2474 | nfs4_unlock_state(); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2475 | return status; |
| 2476 | } |
| 2477 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2478 | __be32 |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2479 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc) |
| 2480 | { |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2481 | __be32 status = 0; |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2482 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2483 | if (rc->rca_one_fs) { |
| 2484 | if (!cstate->current_fh.fh_dentry) |
| 2485 | return nfserr_nofilehandle; |
| 2486 | /* |
| 2487 | * We don't take advantage of the rca_one_fs case. |
| 2488 | * That's OK, it's optional, we can safely ignore it. |
| 2489 | */ |
| 2490 | return nfs_ok; |
| 2491 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2492 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2493 | nfs4_lock_state(); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2494 | status = nfserr_complete_already; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 2495 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
| 2496 | &cstate->session->se_client->cl_flags)) |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2497 | goto out; |
| 2498 | |
| 2499 | status = nfserr_stale_clientid; |
| 2500 | if (is_client_expired(cstate->session->se_client)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2501 | /* |
| 2502 | * The following error isn't really legal. |
| 2503 | * But we only get here if the client just explicitly |
| 2504 | * destroyed the client. Surely it no longer cares what |
| 2505 | * error it gets back on an operation for the dead |
| 2506 | * client. |
| 2507 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2508 | goto out; |
| 2509 | |
| 2510 | status = nfs_ok; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 2511 | nfsd4_client_record_create(cstate->session->se_client); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2512 | out: |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2513 | nfs4_unlock_state(); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2514 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 2518 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 2519 | struct nfsd4_setclientid *setclid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | { |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 2521 | struct xdr_netobj clname = setclid->se_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | nfs4_verifier clverifier = setclid->se_verf; |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2523 | struct nfs4_client *conf, *unconf, *new; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2524 | __be32 status; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2525 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 2526 | |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2527 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | nfs4_lock_state(); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2529 | conf = find_confirmed_client_by_name(&clname, nn); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2530 | if (conf) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2531 | /* case 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2533 | if (clp_used_exchangeid(conf)) |
| 2534 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 2535 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2536 | char addr_str[INET6_ADDRSTRLEN]; |
| 2537 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, |
| 2538 | sizeof(addr_str)); |
| 2539 | dprintk("NFSD: setclientid: string in use by client " |
| 2540 | "at %s\n", addr_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | goto out; |
| 2542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2544 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 2545 | if (unconf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | status = nfserr_jukebox; |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 2548 | new = create_client(clname, rqstp, &clverifier); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 2549 | if (new == NULL) |
| 2550 | goto out; |
J. Bruce Fields | 34b232b | 2012-05-18 22:23:42 -0400 | [diff] [blame] | 2551 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2552 | /* case 1: probable callback update */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | copy_clid(new, conf); |
J. Bruce Fields | 34b232b | 2012-05-18 22:23:42 -0400 | [diff] [blame] | 2554 | else /* case 4 (new client) or cases 2, 3 (client reboot): */ |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2555 | gen_clid(new, nn); |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 2556 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2557 | gen_callback(new, setclid, rqstp); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2558 | add_to_unconfirmed(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 2560 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 2561 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); |
| 2562 | status = nfs_ok; |
| 2563 | out: |
| 2564 | nfs4_unlock_state(); |
| 2565 | return status; |
| 2566 | } |
| 2567 | |
| 2568 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2569 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 2570 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
| 2571 | struct nfsd4_compound_state *cstate, |
| 2572 | struct nfsd4_setclientid_confirm *setclientid_confirm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | { |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2574 | struct nfs4_client *conf, *unconf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 2576 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2577 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 2578 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 2580 | if (STALE_CLIENTID(clid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | return nfserr_stale_clientid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | nfs4_lock_state(); |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2583 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2584 | conf = find_confirmed_client(clid, false, nn); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2585 | unconf = find_unconfirmed_client(clid, false, nn); |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2586 | /* |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2587 | * We try hard to give out unique clientid's, so if we get an |
| 2588 | * attempt to confirm the same clientid with a different cred, |
| 2589 | * there's a bug somewhere. Let's charitably assume it's our |
| 2590 | * bug. |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2591 | */ |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2592 | status = nfserr_serverfault; |
| 2593 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
| 2594 | goto out; |
| 2595 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
| 2596 | goto out; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2597 | /* cases below refer to rfc 3530 section 14.2.34: */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 2598 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
| 2599 | if (conf && !unconf) /* case 2: probable retransmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | status = nfs_ok; |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 2601 | else /* case 4: client hasn't noticed we rebooted yet? */ |
| 2602 | status = nfserr_stale_clientid; |
| 2603 | goto out; |
| 2604 | } |
| 2605 | status = nfs_ok; |
| 2606 | if (conf) { /* case 1: callback update */ |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2607 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
| 2608 | nfsd4_probe_callback(conf); |
| 2609 | expire_client(unconf); |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 2610 | } else { /* case 3: normal case; new or rebooted client */ |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2611 | conf = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2612 | if (conf) { |
| 2613 | status = mark_client_expired(conf); |
| 2614 | if (status) |
| 2615 | goto out; |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2616 | expire_client(conf); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2617 | } |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 2618 | move_to_confirmed(unconf); |
J. Bruce Fields | f3d03b9 | 2012-05-23 11:38:38 -0400 | [diff] [blame] | 2619 | nfsd4_probe_callback(unconf); |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 2620 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | nfs4_unlock_state(); |
| 2623 | return status; |
| 2624 | } |
| 2625 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2626 | static struct nfs4_file *nfsd4_alloc_file(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | { |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2628 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 2629 | } |
| 2630 | |
| 2631 | /* OPEN Share state helper functions */ |
| 2632 | static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino) |
| 2633 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | unsigned int hashval = file_hashval(ino); |
| 2635 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 2636 | lockdep_assert_held(&state_lock); |
| 2637 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2638 | atomic_set(&fp->fi_ref, 1); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2639 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 2640 | INIT_LIST_HEAD(&fp->fi_delegations); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 2641 | ihold(ino); |
| 2642 | fp->fi_inode = ino; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2643 | fp->fi_had_conflict = false; |
| 2644 | fp->fi_lease = NULL; |
| 2645 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 2646 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 2647 | hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | } |
| 2649 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 2650 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2651 | nfsd4_free_slabs(void) |
| 2652 | { |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2653 | kmem_cache_destroy(openowner_slab); |
| 2654 | kmem_cache_destroy(lockowner_slab); |
| 2655 | kmem_cache_destroy(file_slab); |
| 2656 | kmem_cache_destroy(stateid_slab); |
| 2657 | kmem_cache_destroy(deleg_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2658 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | |
Bryan Schumaker | 7208339 | 2011-11-01 15:24:59 -0400 | [diff] [blame] | 2660 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | nfsd4_init_slabs(void) |
| 2662 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2663 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 2664 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 2665 | if (openowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2666 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2667 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
Yanchuan Nian | 3c40794 | 2012-10-24 14:44:19 +0800 | [diff] [blame] | 2668 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2669 | if (lockowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2670 | goto out_free_openowner_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2671 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2672 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2673 | if (file_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2674 | goto out_free_lockowner_slab; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2675 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2676 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2677 | if (stateid_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2678 | goto out_free_file_slab; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 2679 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2680 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 2681 | if (deleg_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2682 | goto out_free_stateid_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | return 0; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 2684 | |
| 2685 | out_free_stateid_slab: |
| 2686 | kmem_cache_destroy(stateid_slab); |
| 2687 | out_free_file_slab: |
| 2688 | kmem_cache_destroy(file_slab); |
| 2689 | out_free_lockowner_slab: |
| 2690 | kmem_cache_destroy(lockowner_slab); |
| 2691 | out_free_openowner_slab: |
| 2692 | kmem_cache_destroy(openowner_slab); |
| 2693 | out: |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2694 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 2695 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | } |
| 2697 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2698 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 2699 | { |
| 2700 | rp->rp_status = nfserr_serverfault; |
| 2701 | rp->rp_buflen = 0; |
| 2702 | rp->rp_buf = rp->rp_ibuf; |
| 2703 | } |
| 2704 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2705 | 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] | 2706 | { |
| 2707 | struct nfs4_stateowner *sop; |
| 2708 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2709 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2710 | if (!sop) |
| 2711 | return NULL; |
| 2712 | |
| 2713 | sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL); |
| 2714 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2715 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2716 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2718 | sop->so_owner.len = owner->len; |
| 2719 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2720 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2721 | sop->so_client = clp; |
| 2722 | init_nfs4_replay(&sop->so_replay); |
| 2723 | return sop; |
| 2724 | } |
| 2725 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2726 | 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] | 2727 | { |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 2728 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2729 | |
| 2730 | list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2731 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | } |
| 2733 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2734 | static struct nfs4_openowner * |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 2735 | alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
| 2736 | struct nfsd4_open *open, |
| 2737 | struct nfsd4_compound_state *cstate) |
| 2738 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2739 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2741 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 2742 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | return NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2744 | oo->oo_owner.so_is_open_owner = 1; |
| 2745 | oo->oo_owner.so_seqid = open->op_seqid; |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 2746 | oo->oo_flags = NFS4_OO_NEW; |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 2747 | if (nfsd4_has_session(cstate)) |
| 2748 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2749 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 2750 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2751 | INIT_LIST_HEAD(&oo->oo_close_lru); |
| 2752 | hash_openowner(oo, clp, strhashval); |
| 2753 | return oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | } |
| 2755 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 2756 | 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] | 2757 | struct nfs4_openowner *oo = open->op_openowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 2759 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 2760 | INIT_LIST_HEAD(&stp->st_locks); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2761 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 2762 | list_add(&stp->st_perfile, &fp->fi_stateids); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2763 | stp->st_stateowner = &oo->oo_owner; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2764 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | stp->st_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | stp->st_access_bmap = 0; |
| 2767 | stp->st_deny_bmap = 0; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 2768 | set_access(open->op_share_access, stp); |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 2769 | set_deny(open->op_share_deny, stp); |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 2770 | stp->st_openstp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | static void |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 2774 | move_to_close_lru(struct nfs4_openowner *oo, struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | { |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 2776 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 2777 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2778 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 2780 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2781 | oo->oo_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | } |
| 2783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2785 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, |
| 2786 | clientid_t *clid) |
| 2787 | { |
| 2788 | return (sop->so_owner.len == owner->len) && |
| 2789 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len) && |
| 2790 | (sop->so_client->cl_clientid.cl_id == clid->cl_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | } |
| 2792 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2793 | static struct nfs4_openowner * |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 2794 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
| 2795 | bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2797 | struct nfs4_stateowner *so; |
| 2798 | struct nfs4_openowner *oo; |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 2799 | struct nfs4_client *clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 2801 | list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) { |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 2802 | if (!so->so_is_open_owner) |
| 2803 | continue; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2804 | if (same_owner_str(so, &open->op_owner, &open->op_clientid)) { |
| 2805 | oo = openowner(so); |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 2806 | clp = oo->oo_owner.so_client; |
| 2807 | if ((bool)clp->cl_minorversion != sessions) |
| 2808 | return NULL; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2809 | renew_client(oo->oo_owner.so_client); |
| 2810 | return oo; |
| 2811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | } |
| 2813 | return NULL; |
| 2814 | } |
| 2815 | |
| 2816 | /* search file_hashtbl[] for file */ |
| 2817 | static struct nfs4_file * |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 2818 | find_file_locked(struct inode *ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | { |
| 2820 | unsigned int hashval = file_hashval(ino); |
| 2821 | struct nfs4_file *fp; |
| 2822 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 2823 | lockdep_assert_held(&state_lock); |
| 2824 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 2825 | hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) { |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2826 | if (fp->fi_inode == ino) { |
| 2827 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | } |
| 2831 | return NULL; |
| 2832 | } |
| 2833 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 2834 | static struct nfs4_file * |
| 2835 | find_file(struct inode *ino) |
| 2836 | { |
| 2837 | struct nfs4_file *fp; |
| 2838 | |
| 2839 | spin_lock(&state_lock); |
| 2840 | fp = find_file_locked(ino); |
| 2841 | spin_unlock(&state_lock); |
| 2842 | return fp; |
| 2843 | } |
| 2844 | |
| 2845 | static struct nfs4_file * |
| 2846 | find_or_add_file(struct inode *ino, struct nfs4_file *new) |
| 2847 | { |
| 2848 | struct nfs4_file *fp; |
| 2849 | |
| 2850 | spin_lock(&state_lock); |
| 2851 | fp = find_file_locked(ino); |
| 2852 | if (fp == NULL) { |
| 2853 | nfsd4_init_file(new, ino); |
| 2854 | fp = new; |
| 2855 | } |
| 2856 | spin_unlock(&state_lock); |
| 2857 | |
| 2858 | return fp; |
| 2859 | } |
| 2860 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 2861 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | * Called to check deny when READ with all zero stateid or |
| 2863 | * WRITE with all zero or all one stateid |
| 2864 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2865 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 2867 | { |
| 2868 | struct inode *ino = current_fh->fh_dentry->d_inode; |
| 2869 | struct nfs4_file *fp; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2870 | struct nfs4_ol_stateid *stp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2871 | __be32 ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | fp = find_file(ino); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2874 | if (!fp) |
| 2875 | return nfs_ok; |
NeilBrown | b700949 | 2005-07-07 17:59:23 -0700 | [diff] [blame] | 2876 | ret = nfserr_locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | /* Search for conflicting share reservations */ |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2878 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) { |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 2879 | if (test_deny(deny_type, stp) || |
| 2880 | test_deny(NFS4_SHARE_DENY_BOTH, stp)) |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2881 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2882 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2883 | ret = nfs_ok; |
| 2884 | out: |
| 2885 | put_nfs4_file(fp); |
| 2886 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2887 | } |
| 2888 | |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 2889 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | { |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 2891 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 2892 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2893 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 2894 | lockdep_assert_held(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | /* We're assuming the state code never drops its reference |
| 2896 | * without first removing the lease. Since we're in this lease |
| 2897 | * callback (and since the lease code is serialized by the kernel |
| 2898 | * lock) we know the server hasn't removed the lease yet, we know |
| 2899 | * it's safe to take a reference: */ |
| 2900 | atomic_inc(&dp->dl_count); |
| 2901 | |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 2902 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 2904 | /* Only place dl_time is set; protected by i_lock: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2905 | dp->dl_time = get_seconds(); |
| 2906 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 2907 | block_delegations(&dp->dl_fh); |
| 2908 | |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 2909 | nfsd4_cb_recall(dp); |
| 2910 | } |
| 2911 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 2912 | /* Called from break_lease() with i_lock held. */ |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 2913 | static void nfsd_break_deleg_cb(struct file_lock *fl) |
| 2914 | { |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2915 | struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; |
| 2916 | struct nfs4_delegation *dp; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 2917 | |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 2918 | if (!fp) { |
| 2919 | WARN(1, "(%p)->fl_owner NULL\n", fl); |
| 2920 | return; |
| 2921 | } |
| 2922 | if (fp->fi_had_conflict) { |
| 2923 | WARN(1, "duplicate break on %p\n", fp); |
| 2924 | return; |
| 2925 | } |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 2926 | /* |
| 2927 | * 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] | 2928 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 2929 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 2930 | */ |
| 2931 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 2933 | spin_lock(&state_lock); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2934 | fp->fi_had_conflict = true; |
| 2935 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) |
| 2936 | nfsd_break_one_deleg(dp); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 2937 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | } |
| 2939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | static |
| 2941 | int nfsd_change_deleg_cb(struct file_lock **onlist, int arg) |
| 2942 | { |
| 2943 | if (arg & F_UNLCK) |
| 2944 | return lease_modify(onlist, arg); |
| 2945 | else |
| 2946 | return -EAGAIN; |
| 2947 | } |
| 2948 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 2949 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 2950 | .lm_break = nfsd_break_deleg_cb, |
| 2951 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | }; |
| 2953 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 2954 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 2955 | { |
| 2956 | if (nfsd4_has_session(cstate)) |
| 2957 | return nfs_ok; |
| 2958 | if (seqid == so->so_seqid - 1) |
| 2959 | return nfserr_replay_me; |
| 2960 | if (seqid == so->so_seqid) |
| 2961 | return nfs_ok; |
| 2962 | return nfserr_bad_seqid; |
| 2963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2965 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 2966 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 2967 | struct nfsd4_open *open, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | clientid_t *clientid = &open->op_clientid; |
| 2970 | struct nfs4_client *clp = NULL; |
| 2971 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2972 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 2973 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 2975 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | return nfserr_stale_clientid; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 2977 | /* |
| 2978 | * In case we need it later, after we've already created the |
| 2979 | * file and don't want to risk a further failure: |
| 2980 | */ |
| 2981 | open->op_file = nfsd4_alloc_file(); |
| 2982 | if (open->op_file == NULL) |
| 2983 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 2985 | strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 2986 | oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2987 | open->op_openowner = oo; |
| 2988 | if (!oo) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2989 | clp = find_confirmed_client(clientid, cstate->minorversion, |
| 2990 | nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2991 | if (clp == NULL) |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 2992 | return nfserr_expired; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 2993 | goto new_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 2995 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 2996 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2997 | clp = oo->oo_owner.so_client; |
| 2998 | release_openowner(oo); |
| 2999 | open->op_openowner = NULL; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3000 | goto new_owner; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3001 | } |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3002 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 3003 | if (status) |
| 3004 | return status; |
| 3005 | clp = oo->oo_owner.so_client; |
| 3006 | goto alloc_stateid; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3007 | new_owner: |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 3008 | oo = alloc_init_open_stateowner(strhashval, clp, open, cstate); |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3009 | if (oo == NULL) |
| 3010 | return nfserr_jukebox; |
| 3011 | open->op_openowner = oo; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3012 | alloc_stateid: |
| 3013 | open->op_stp = nfs4_alloc_stateid(clp); |
| 3014 | if (!open->op_stp) |
| 3015 | return nfserr_jukebox; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3016 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | } |
| 3018 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3019 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 3020 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 3021 | { |
| 3022 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 3023 | return nfserr_openmode; |
| 3024 | else |
| 3025 | return nfs_ok; |
| 3026 | } |
| 3027 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 3028 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3029 | { |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3030 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 3031 | } |
| 3032 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3033 | 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] | 3034 | { |
| 3035 | struct nfs4_stid *ret; |
| 3036 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3037 | ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 3038 | if (!ret) |
| 3039 | return NULL; |
| 3040 | return delegstateid(ret); |
| 3041 | } |
| 3042 | |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3043 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 3044 | { |
| 3045 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 3046 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 3047 | } |
| 3048 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3049 | static __be32 |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 3050 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3051 | struct nfs4_delegation **dp) |
| 3052 | { |
| 3053 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3054 | __be32 status = nfserr_bad_stateid; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3055 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3056 | *dp = find_deleg_stateid(cl, &open->op_delegate_stateid); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3057 | if (*dp == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3058 | goto out; |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3059 | flags = share_access_to_flags(open->op_share_access); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3060 | status = nfs4_check_delegmode(*dp, flags); |
| 3061 | if (status) |
| 3062 | *dp = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3063 | out: |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3064 | if (!nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3065 | return nfs_ok; |
| 3066 | if (status) |
| 3067 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3068 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3069 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3070 | } |
| 3071 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3072 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3073 | nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3075 | struct nfs4_ol_stateid *local; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3076 | struct nfs4_openowner *oo = open->op_openowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 3078 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | /* ignore lock owners */ |
| 3080 | if (local->st_stateowner->so_is_open_owner == 0) |
| 3081 | continue; |
| 3082 | /* remember if we have seen this open owner */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3083 | if (local->st_stateowner == &oo->oo_owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | *stpp = local; |
| 3085 | /* check for conflicting share reservations */ |
| 3086 | if (!test_share(local, open)) |
J. Bruce Fields | 77eaae8 | 2011-09-02 12:08:20 -0400 | [diff] [blame] | 3087 | return nfserr_share_denied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | } |
J. Bruce Fields | 77eaae8 | 2011-09-02 12:08:20 -0400 | [diff] [blame] | 3089 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | } |
| 3091 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 3092 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 3093 | { |
| 3094 | int flags = 0; |
| 3095 | |
| 3096 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 3097 | flags |= NFSD_MAY_READ; |
| 3098 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 3099 | flags |= NFSD_MAY_WRITE; |
| 3100 | return flags; |
| 3101 | } |
| 3102 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3103 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 3105 | struct nfsd4_open *open) |
| 3106 | { |
| 3107 | struct iattr iattr = { |
| 3108 | .ia_valid = ATTR_SIZE, |
| 3109 | .ia_size = 0, |
| 3110 | }; |
| 3111 | if (!open->op_truncate) |
| 3112 | return 0; |
| 3113 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 3114 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); |
| 3116 | } |
| 3117 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3118 | static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, |
| 3119 | struct svc_fh *cur_fh, struct nfsd4_open *open) |
| 3120 | { |
| 3121 | __be32 status; |
| 3122 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 3123 | int access = nfs4_access_to_access(open->op_share_access); |
| 3124 | |
| 3125 | if (!fp->fi_fds[oflag]) { |
| 3126 | status = nfsd_open(rqstp, cur_fh, S_IFREG, access, |
| 3127 | &fp->fi_fds[oflag]); |
| 3128 | if (status) |
| 3129 | goto out; |
| 3130 | } |
| 3131 | nfs4_file_get_access(fp, oflag); |
| 3132 | |
| 3133 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3134 | if (status) |
| 3135 | goto out_put_access; |
| 3136 | |
| 3137 | return nfs_ok; |
| 3138 | |
| 3139 | out_put_access: |
| 3140 | nfs4_file_put_access(fp, oflag); |
| 3141 | out: |
| 3142 | return status; |
| 3143 | } |
| 3144 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3145 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3146 | 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] | 3147 | { |
J. Bruce Fields | b6d2f1c | 2011-10-10 17:44:19 -0400 | [diff] [blame] | 3148 | u32 op_share_access = open->op_share_access; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3149 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3151 | if (!test_access(op_share_access, stp)) |
Casey Bodley | 0c12eaf | 2011-07-23 14:58:10 -0400 | [diff] [blame] | 3152 | status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3153 | else |
| 3154 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3155 | |
| 3156 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | return status; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | /* remember the open */ |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3160 | set_access(op_share_access, stp); |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 3161 | set_deny(open->op_share_deny, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | return nfs_ok; |
| 3163 | } |
| 3164 | |
| 3165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | static void |
J. Bruce Fields | 1255a8f | 2012-03-06 14:35:16 -0500 | [diff] [blame] | 3167 | nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | { |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3169 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | } |
| 3171 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 3172 | /* Should we give out recallable state?: */ |
| 3173 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 3174 | { |
| 3175 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 3176 | return true; |
| 3177 | /* |
| 3178 | * In the sessions case, since we don't have to establish a |
| 3179 | * separate connection for callbacks, we assume it's OK |
| 3180 | * until we hear otherwise: |
| 3181 | */ |
| 3182 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 3183 | } |
| 3184 | |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3185 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag) |
| 3186 | { |
| 3187 | struct file_lock *fl; |
| 3188 | |
| 3189 | fl = locks_alloc_lock(); |
| 3190 | if (!fl) |
| 3191 | return NULL; |
| 3192 | locks_init_lock(fl); |
| 3193 | fl->fl_lmops = &nfsd_lease_mng_ops; |
J. Bruce Fields | 617588d | 2011-07-01 15:18:34 -0400 | [diff] [blame] | 3194 | fl->fl_flags = FL_DELEG; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3195 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 3196 | fl->fl_end = OFFSET_MAX; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3197 | fl->fl_owner = (fl_owner_t)(dp->dl_file); |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3198 | fl->fl_pid = current->tgid; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3199 | return fl; |
| 3200 | } |
| 3201 | |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3202 | static int nfs4_setlease(struct nfs4_delegation *dp) |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3203 | { |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3204 | struct nfs4_file *fp = dp->dl_file; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3205 | struct file_lock *fl; |
| 3206 | int status; |
| 3207 | |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3208 | fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3209 | if (!fl) |
| 3210 | return -ENOMEM; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3211 | fl->fl_file = find_readable_file(fp); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3212 | status = vfs_setlease(fl->fl_file, fl->fl_type, &fl); |
J. Bruce Fields | e873088 | 2012-01-23 13:52:01 -0500 | [diff] [blame] | 3213 | if (status) |
| 3214 | goto out_free; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3215 | fp->fi_lease = fl; |
Al Viro | cb0942b | 2012-08-27 14:48:26 -0400 | [diff] [blame] | 3216 | fp->fi_deleg_file = get_file(fl->fl_file); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3217 | atomic_set(&fp->fi_delegees, 1); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3218 | spin_lock(&state_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 3219 | hash_delegation_locked(dp, fp); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3220 | spin_unlock(&state_lock); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3221 | return 0; |
J. Bruce Fields | e873088 | 2012-01-23 13:52:01 -0500 | [diff] [blame] | 3222 | out_free: |
| 3223 | locks_free_lock(fl); |
| 3224 | return status; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3225 | } |
| 3226 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 3227 | static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp) |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3228 | { |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 3229 | if (fp->fi_had_conflict) |
| 3230 | return -EAGAIN; |
| 3231 | get_nfs4_file(fp); |
| 3232 | dp->dl_file = fp; |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 3233 | if (!fp->fi_lease) |
| 3234 | return nfs4_setlease(dp); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3235 | spin_lock(&state_lock); |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 3236 | atomic_inc(&fp->fi_delegees); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3237 | if (fp->fi_had_conflict) { |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3238 | spin_unlock(&state_lock); |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 3239 | return -EAGAIN; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3240 | } |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 3241 | hash_delegation_locked(dp, fp); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3242 | spin_unlock(&state_lock); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3243 | return 0; |
| 3244 | } |
| 3245 | |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 3246 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 3247 | { |
| 3248 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3249 | if (status == -EAGAIN) |
| 3250 | open->op_why_no_deleg = WND4_CONTENTION; |
| 3251 | else { |
| 3252 | open->op_why_no_deleg = WND4_RESOURCE; |
| 3253 | switch (open->op_deleg_want) { |
| 3254 | case NFS4_SHARE_WANT_READ_DELEG: |
| 3255 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 3256 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 3257 | break; |
| 3258 | case NFS4_SHARE_WANT_CANCEL: |
| 3259 | open->op_why_no_deleg = WND4_CANCELLED; |
| 3260 | break; |
| 3261 | case NFS4_SHARE_WANT_NO_DELEG: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 3262 | WARN_ON_ONCE(1); |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 3263 | } |
| 3264 | } |
| 3265 | } |
| 3266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | /* |
| 3268 | * Attempt to hand out a delegation. |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3269 | * |
| 3270 | * Note we don't support write delegations, and won't until the vfs has |
| 3271 | * proper support for them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | */ |
| 3273 | static void |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3274 | nfs4_open_delegation(struct net *net, struct svc_fh *fh, |
| 3275 | struct nfsd4_open *open, struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | { |
| 3277 | struct nfs4_delegation *dp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3278 | struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner); |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 3279 | int cb_up; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3280 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3282 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3283 | open->op_recall = 0; |
| 3284 | switch (open->op_claim_type) { |
| 3285 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 3286 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3287 | open->op_recall = 1; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3288 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 3289 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3290 | break; |
| 3291 | case NFS4_OPEN_CLAIM_NULL: |
Ming Chen | ed47b06 | 2014-01-09 21:26:10 +0000 | [diff] [blame] | 3292 | case NFS4_OPEN_CLAIM_FH: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3293 | /* |
| 3294 | * Let's not give out any delegations till everyone's |
| 3295 | * had the chance to reclaim theirs.... |
| 3296 | */ |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3297 | if (locks_in_grace(net)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3298 | goto out_no_deleg; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3299 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3300 | goto out_no_deleg; |
Steve Dickson | 9a0590a | 2013-05-15 14:51:49 -0400 | [diff] [blame] | 3301 | /* |
| 3302 | * Also, if the file was opened for write or |
| 3303 | * create, there's a good chance the client's |
| 3304 | * about to write to it, resulting in an |
| 3305 | * immediate recall (since we don't support |
| 3306 | * write delegations): |
| 3307 | */ |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3308 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3309 | goto out_no_deleg; |
| 3310 | if (open->op_create == NFS4_OPEN_CREATE) |
| 3311 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3312 | break; |
| 3313 | default: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3314 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3315 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3316 | dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3317 | if (dp == NULL) |
| 3318 | goto out_no_deleg; |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 3319 | status = nfs4_set_delegation(dp, stp->st_file); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3320 | if (status) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3321 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 3323 | 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] | 3324 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 3325 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 3326 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3327 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3328 | return; |
| 3329 | out_free: |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 3330 | destroy_delegation(dp); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3331 | out_no_deleg: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3332 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 3333 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 3334 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3335 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 3336 | open->op_recall = 1; |
| 3337 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3338 | |
| 3339 | /* 4.1 client asking for a delegation? */ |
| 3340 | if (open->op_deleg_want) |
| 3341 | nfsd4_open_deleg_none_ext(open, status); |
| 3342 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3343 | } |
| 3344 | |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 3345 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 3346 | struct nfs4_delegation *dp) |
| 3347 | { |
| 3348 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 3349 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 3350 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3351 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 3352 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 3353 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 3354 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3355 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 3356 | } |
| 3357 | /* Otherwise the client must be confused wanting a delegation |
| 3358 | * it already has, therefore we don't return |
| 3359 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 3360 | */ |
| 3361 | } |
| 3362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3363 | /* |
| 3364 | * called with nfs4_lock_state() held. |
| 3365 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3366 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3367 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 3368 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3369 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3370 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | struct nfs4_file *fp = NULL; |
| 3372 | struct inode *ino = current_fh->fh_dentry->d_inode; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3373 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3374 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3375 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | /* |
| 3378 | * Lookup file; if found, lookup stateid and check open request, |
| 3379 | * and check for delegations in the process of being recalled. |
| 3380 | * If not found, create the nfs4_file struct |
| 3381 | */ |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3382 | fp = find_or_add_file(ino, open->op_file); |
| 3383 | if (fp != open->op_file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | if ((status = nfs4_check_open(fp, open, &stp))) |
| 3385 | goto out; |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 3386 | status = nfs4_check_deleg(cl, open, &dp); |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3387 | if (status) |
| 3388 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | } else { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3390 | open->op_file = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3391 | status = nfserr_bad_stateid; |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3392 | if (nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3393 | goto out; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 3394 | status = nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | /* |
| 3398 | * OPEN the file, or upgrade an existing OPEN. |
| 3399 | * If truncate fails, the OPEN fails. |
| 3400 | */ |
| 3401 | if (stp) { |
| 3402 | /* Stateid was found, this is an OPEN upgrade */ |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 3403 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | if (status) |
| 3405 | goto out; |
| 3406 | } else { |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3407 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, open); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3408 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | goto out; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3410 | stp = open->op_stp; |
| 3411 | open->op_stp = NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 3412 | init_open_stateid(stp, fp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | } |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3414 | update_stateid(&stp->st_stid.sc_stateid); |
| 3415 | memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3417 | if (nfsd4_has_session(&resp->cstate)) { |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3418 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 3419 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3420 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 3421 | goto nodeleg; |
| 3422 | } |
| 3423 | } |
| 3424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | /* |
| 3426 | * Attempt to hand out a delegation. No error return, because the |
| 3427 | * OPEN succeeds even if we fail. |
| 3428 | */ |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3429 | nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3430 | nodeleg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | status = nfs_ok; |
| 3432 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 3433 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3434 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | out: |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3436 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 3437 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 3438 | open->op_deleg_want) |
| 3439 | nfsd4_deleg_xgrade_none_ext(open, dp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 3440 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3441 | if (fp) |
| 3442 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 3443 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
J. Bruce Fields | 1255a8f | 2012-03-06 14:35:16 -0500 | [diff] [blame] | 3444 | nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | /* |
| 3446 | * To finish the open response, we just need to set the rflags. |
| 3447 | */ |
| 3448 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3449 | if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) && |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3450 | !nfsd4_has_session(&resp->cstate)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
| 3452 | |
| 3453 | return status; |
| 3454 | } |
| 3455 | |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 3456 | void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status) |
| 3457 | { |
| 3458 | if (open->op_openowner) { |
| 3459 | struct nfs4_openowner *oo = open->op_openowner; |
| 3460 | |
| 3461 | if (!list_empty(&oo->oo_owner.so_stateids)) |
| 3462 | list_del_init(&oo->oo_close_lru); |
| 3463 | if (oo->oo_flags & NFS4_OO_NEW) { |
| 3464 | if (status) { |
| 3465 | release_openowner(oo); |
| 3466 | open->op_openowner = NULL; |
| 3467 | } else |
| 3468 | oo->oo_flags &= ~NFS4_OO_NEW; |
| 3469 | } |
| 3470 | } |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3471 | if (open->op_file) |
| 3472 | nfsd4_free_file(open->op_file); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3473 | if (open->op_stp) |
J. Bruce Fields | ef79859 | 2012-08-29 10:32:54 -0700 | [diff] [blame] | 3474 | free_generic_stateid(open->op_stp); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 3475 | } |
| 3476 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 3477 | static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp) |
| 3478 | { |
| 3479 | struct nfs4_client *found; |
| 3480 | |
| 3481 | if (STALE_CLIENTID(clid, nn)) |
| 3482 | return nfserr_stale_clientid; |
| 3483 | found = find_confirmed_client(clid, session, nn); |
| 3484 | if (clp) |
| 3485 | *clp = found; |
| 3486 | return found ? nfs_ok : nfserr_expired; |
| 3487 | } |
| 3488 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3489 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3490 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3491 | clientid_t *clid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | { |
| 3493 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3494 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 3495 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | |
| 3497 | nfs4_lock_state(); |
| 3498 | dprintk("process_renew(%08x/%08x): starting\n", |
| 3499 | clid->cl_boot, clid->cl_id); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 3500 | status = lookup_clientid(clid, cstate->minorversion, nn, &clp); |
| 3501 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3502 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | status = nfserr_cb_path_down; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 3504 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 3505 | && clp->cl_cb_state != NFSD4_CB_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | goto out; |
| 3507 | status = nfs_ok; |
| 3508 | out: |
| 3509 | nfs4_unlock_state(); |
| 3510 | return status; |
| 3511 | } |
| 3512 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3513 | static void |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 3514 | nfsd4_end_grace(struct nfsd_net *nn) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3515 | { |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 3516 | /* do nothing if grace period already ended */ |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 3517 | if (nn->grace_ended) |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 3518 | return; |
| 3519 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3520 | dprintk("NFSD: end of grace period\n"); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 3521 | nn->grace_ended = true; |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 3522 | nfsd4_record_grace_done(nn, nn->boot_time); |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 3523 | locks_end_grace(&nn->nfsd4_manager); |
J. Bruce Fields | e46b498 | 2010-03-01 19:21:21 -0500 | [diff] [blame] | 3524 | /* |
| 3525 | * Now that every NFSv4 client has had the chance to recover and |
| 3526 | * to see the (possibly new, possibly shorter) lease time, we |
| 3527 | * can safely set the next grace time to the current lease time: |
| 3528 | */ |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 3529 | nn->nfsd4_grace = nn->nfsd4_lease; |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3530 | } |
| 3531 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 3532 | static time_t |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3533 | nfs4_laundromat(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | { |
| 3535 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3536 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | struct nfs4_delegation *dp; |
| 3538 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 3d73371 | 2012-11-27 14:11:44 +0300 | [diff] [blame] | 3539 | time_t cutoff = get_seconds() - nn->nfsd4_lease; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3540 | time_t t, new_timeo = nn->nfsd4_lease; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | |
| 3542 | nfs4_lock_state(); |
| 3543 | |
| 3544 | dprintk("NFSD: laundromat service - starting\n"); |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 3545 | nfsd4_end_grace(nn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3546 | INIT_LIST_HEAD(&reaplist); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3547 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 3548 | list_for_each_safe(pos, next, &nn->client_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
| 3550 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { |
| 3551 | t = clp->cl_time - cutoff; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3552 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | break; |
| 3554 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3555 | if (mark_client_expired_locked(clp)) { |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 3556 | dprintk("NFSD: client in use (clientid %08x)\n", |
| 3557 | clp->cl_clientid.cl_id); |
| 3558 | continue; |
| 3559 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3560 | list_move(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3561 | } |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3562 | spin_unlock(&nn->client_lock); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3563 | list_for_each_safe(pos, next, &reaplist) { |
| 3564 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3565 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 3566 | clp->cl_clientid.cl_id); |
| 3567 | expire_client(clp); |
| 3568 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3569 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 3570 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3571 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Stanislav Kinsbursky | 4e37a7c2 | 2012-11-26 15:22:03 +0300 | [diff] [blame] | 3572 | if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn) |
| 3573 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3574 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3575 | t = dp->dl_time - cutoff; |
| 3576 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | break; |
| 3578 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3579 | list_move(&dp->dl_recall_lru, &reaplist); |
| 3580 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3581 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | list_for_each_safe(pos, next, &reaplist) { |
| 3583 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3584 | revoke_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3585 | } |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3586 | list_for_each_safe(pos, next, &nn->close_lru) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3587 | oo = container_of(pos, struct nfs4_openowner, oo_close_lru); |
| 3588 | if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3589 | t = oo->oo_time - cutoff; |
| 3590 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3591 | break; |
| 3592 | } |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3593 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | } |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3595 | new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3596 | nfs4_unlock_state(); |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 3597 | return new_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | } |
| 3599 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 3600 | static struct workqueue_struct *laundry_wq; |
| 3601 | static void laundromat_main(struct work_struct *); |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 3602 | |
| 3603 | static void |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3604 | laundromat_main(struct work_struct *laundry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3605 | { |
| 3606 | time_t t; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3607 | struct delayed_work *dwork = container_of(laundry, struct delayed_work, |
| 3608 | work); |
| 3609 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 3610 | laundromat_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3612 | t = nfs4_laundromat(nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 3614 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | } |
| 3616 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3617 | 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] | 3618 | { |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3619 | if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode) |
| 3620 | return nfserr_bad_stateid; |
| 3621 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3622 | } |
| 3623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3625 | access_permit_read(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3626 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3627 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 3628 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 3629 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3633 | access_permit_write(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3634 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3635 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 3636 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3640 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3641 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3642 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3643 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 3644 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 3645 | if (stp->st_openstp) |
| 3646 | stp = stp->st_openstp; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3647 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | goto out; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 3649 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | goto out; |
| 3651 | status = nfs_ok; |
| 3652 | out: |
| 3653 | return status; |
| 3654 | } |
| 3655 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3656 | static inline __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3657 | 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] | 3658 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 3659 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | return nfs_ok; |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3661 | else if (locks_in_grace(net)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3662 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | * conflicting state; so we must wait out the grace period. */ |
| 3664 | return nfserr_grace; |
| 3665 | } else if (flags & WR_STATE) |
| 3666 | return nfs4_share_conflict(current_fh, |
| 3667 | NFS4_SHARE_DENY_WRITE); |
| 3668 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 3669 | return nfs4_share_conflict(current_fh, |
| 3670 | NFS4_SHARE_DENY_READ); |
| 3671 | } |
| 3672 | |
| 3673 | /* |
| 3674 | * Allow READ/WRITE during grace period on recovered state only for files |
| 3675 | * that are not able to provide mandatory locking. |
| 3676 | */ |
| 3677 | static inline int |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3678 | grace_disallows_io(struct net *net, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | { |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3680 | return locks_in_grace(net) && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | } |
| 3682 | |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3683 | /* Returns true iff a is later than b: */ |
| 3684 | static bool stateid_generation_after(stateid_t *a, stateid_t *b) |
| 3685 | { |
Jim Rees | 1a9357f | 2013-05-17 17:33:00 -0400 | [diff] [blame] | 3686 | return (s32)(a->si_generation - b->si_generation) > 0; |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3687 | } |
| 3688 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3689 | 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] | 3690 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3691 | /* |
| 3692 | * When sessions are used the stateid generation number is ignored |
| 3693 | * when it is zero. |
| 3694 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 3695 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3696 | return nfs_ok; |
| 3697 | |
| 3698 | if (in->si_generation == ref->si_generation) |
| 3699 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3700 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3701 | /* 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] | 3702 | if (stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3703 | return nfserr_bad_stateid; |
| 3704 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3705 | * However, we could see a stateid from the past, even from a |
| 3706 | * non-buggy client. For example, if the client sends a lock |
| 3707 | * while some IO is outstanding, the lock may bump si_generation |
| 3708 | * while the IO is still in flight. The client could avoid that |
| 3709 | * situation by waiting for responses on all the IO requests, |
| 3710 | * but better performance may result in retrying IO that |
| 3711 | * receives an old_stateid error if requests are rarely |
| 3712 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3713 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3714 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3715 | } |
| 3716 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 3717 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3718 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3719 | struct nfs4_stid *s; |
| 3720 | struct nfs4_ol_stateid *ols; |
| 3721 | __be32 status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3722 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 3723 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 3724 | return nfserr_bad_stateid; |
| 3725 | /* Client debugging aid. */ |
| 3726 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { |
| 3727 | char addr_str[INET6_ADDRSTRLEN]; |
| 3728 | rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, |
| 3729 | sizeof(addr_str)); |
| 3730 | pr_warn_ratelimited("NFSD: client %s testing state ID " |
| 3731 | "with incorrect client ID\n", addr_str); |
| 3732 | return nfserr_bad_stateid; |
| 3733 | } |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3734 | s = find_stateid(cl, stateid); |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3735 | if (!s) |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 3736 | return nfserr_bad_stateid; |
J. Bruce Fields | 36279ac | 2011-09-26 12:53:00 -0400 | [diff] [blame] | 3737 | status = check_stateid_generation(stateid, &s->sc_stateid, 1); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3738 | if (status) |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3739 | return status; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 3740 | switch (s->sc_type) { |
| 3741 | case NFS4_DELEG_STID: |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3742 | return nfs_ok; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3743 | case NFS4_REVOKED_DELEG_STID: |
| 3744 | return nfserr_deleg_revoked; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 3745 | case NFS4_OPEN_STID: |
| 3746 | case NFS4_LOCK_STID: |
| 3747 | ols = openlockstateid(s); |
| 3748 | if (ols->st_stateowner->so_is_open_owner |
| 3749 | && !(openowner(ols->st_stateowner)->oo_flags |
| 3750 | & NFS4_OO_CONFIRMED)) |
| 3751 | return nfserr_bad_stateid; |
| 3752 | return nfs_ok; |
| 3753 | default: |
| 3754 | printk("unknown stateid type %x\n", s->sc_type); |
| 3755 | case NFS4_CLOSED_STID: |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3756 | return nfserr_bad_stateid; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 3757 | } |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3758 | } |
| 3759 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 3760 | static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask, |
| 3761 | struct nfs4_stid **s, bool sessions, |
| 3762 | struct nfsd_net *nn) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3763 | { |
| 3764 | struct nfs4_client *cl; |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 3765 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3766 | |
| 3767 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 3768 | return nfserr_bad_stateid; |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 3769 | status = lookup_clientid(&stateid->si_opaque.so_clid, sessions, |
| 3770 | nn, &cl); |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 3771 | if (status == nfserr_stale_clientid) { |
| 3772 | if (sessions) |
| 3773 | return nfserr_bad_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3774 | return nfserr_stale_stateid; |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 3775 | } |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 3776 | if (status) |
| 3777 | return status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3778 | *s = find_stateid_by_type(cl, stateid, typemask); |
| 3779 | if (!*s) |
| 3780 | return nfserr_bad_stateid; |
| 3781 | return nfs_ok; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3782 | } |
| 3783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | /* |
| 3785 | * Checks for stateid operations |
| 3786 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3787 | __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3788 | nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate, |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 3789 | stateid_t *stateid, int flags, struct file **filpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3790 | { |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 3791 | struct nfs4_stid *s; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3792 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | struct nfs4_delegation *dp = NULL; |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 3794 | struct svc_fh *current_fh = &cstate->current_fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | struct inode *ino = current_fh->fh_dentry->d_inode; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 3796 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 3797 | struct file *file = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3798 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3800 | if (filpp) |
| 3801 | *filpp = NULL; |
| 3802 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3803 | if (grace_disallows_io(net, ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3804 | return nfserr_grace; |
| 3805 | |
| 3806 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 3807 | return check_special_stateids(net, current_fh, stateid, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3808 | |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 3809 | nfs4_lock_state(); |
| 3810 | |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 3811 | status = nfsd4_lookup_stateid(stateid, |
| 3812 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
| 3813 | &s, cstate->minorversion, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3814 | if (status) |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 3815 | goto out; |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 3816 | status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate)); |
| 3817 | if (status) |
| 3818 | goto out; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3819 | switch (s->sc_type) { |
| 3820 | case NFS4_DELEG_STID: |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 3821 | dp = delegstateid(s); |
J. Bruce Fields | dc9bf70 | 2009-02-21 11:14:43 -0800 | [diff] [blame] | 3822 | status = nfs4_check_delegmode(dp, flags); |
| 3823 | if (status) |
| 3824 | goto out; |
Dan Carpenter | 43b0178 | 2010-10-27 23:19:04 +0200 | [diff] [blame] | 3825 | if (filpp) { |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 3826 | file = dp->dl_file->fi_deleg_file; |
| 3827 | if (!file) { |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 3828 | WARN_ON_ONCE(1); |
| 3829 | status = nfserr_serverfault; |
| 3830 | goto out; |
| 3831 | } |
Dan Carpenter | 43b0178 | 2010-10-27 23:19:04 +0200 | [diff] [blame] | 3832 | } |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3833 | break; |
| 3834 | case NFS4_OPEN_STID: |
| 3835 | case NFS4_LOCK_STID: |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 3836 | stp = openlockstateid(s); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3837 | status = nfs4_check_fh(current_fh, stp); |
| 3838 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3840 | if (stp->st_stateowner->so_is_open_owner |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3841 | && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3842 | goto out; |
J. Bruce Fields | a4455be | 2009-02-21 10:40:22 -0800 | [diff] [blame] | 3843 | status = nfs4_check_openmode(stp, flags); |
| 3844 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3845 | goto out; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 3846 | if (filpp) { |
| 3847 | if (flags & RD_STATE) |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 3848 | file = find_readable_file(stp->st_file); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 3849 | else |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 3850 | file = find_writeable_file(stp->st_file); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 3851 | } |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3852 | break; |
| 3853 | default: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 3854 | status = nfserr_bad_stateid; |
| 3855 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3856 | } |
| 3857 | status = nfs_ok; |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 3858 | if (file) |
| 3859 | *filpp = get_file(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3860 | out: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 3861 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3862 | return status; |
| 3863 | } |
| 3864 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3865 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3866 | nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3867 | { |
J. Bruce Fields | a1b8ff4 | 2014-05-20 15:55:21 -0400 | [diff] [blame] | 3868 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
| 3869 | |
| 3870 | if (check_for_locks(stp->st_file, lo)) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3871 | return nfserr_locks_held; |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 3872 | release_lockowner_if_empty(lo); |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3873 | return nfs_ok; |
| 3874 | } |
| 3875 | |
| 3876 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3877 | * Test if the stateid is valid |
| 3878 | */ |
| 3879 | __be32 |
| 3880 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3881 | struct nfsd4_test_stateid *test_stateid) |
| 3882 | { |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 3883 | struct nfsd4_test_stateid_id *stateid; |
| 3884 | struct nfs4_client *cl = cstate->session->se_client; |
| 3885 | |
| 3886 | nfs4_lock_state(); |
| 3887 | 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] | 3888 | stateid->ts_id_status = |
| 3889 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 3890 | nfs4_unlock_state(); |
| 3891 | |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3892 | return nfs_ok; |
| 3893 | } |
| 3894 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3895 | __be32 |
| 3896 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3897 | struct nfsd4_free_stateid *free_stateid) |
| 3898 | { |
| 3899 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 3900 | struct nfs4_stid *s; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3901 | struct nfs4_delegation *dp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3902 | struct nfs4_client *cl = cstate->session->se_client; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 3903 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3904 | |
| 3905 | nfs4_lock_state(); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3906 | s = find_stateid(cl, stateid); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 3907 | if (!s) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3908 | goto out; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 3909 | switch (s->sc_type) { |
| 3910 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3911 | ret = nfserr_locks_held; |
| 3912 | goto out; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 3913 | case NFS4_OPEN_STID: |
| 3914 | case NFS4_LOCK_STID: |
| 3915 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 3916 | if (ret) |
| 3917 | goto out; |
| 3918 | if (s->sc_type == NFS4_LOCK_STID) |
| 3919 | ret = nfsd4_free_lock_stateid(openlockstateid(s)); |
| 3920 | else |
| 3921 | ret = nfserr_locks_held; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3922 | break; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3923 | case NFS4_REVOKED_DELEG_STID: |
| 3924 | dp = delegstateid(s); |
| 3925 | destroy_revoked_delegation(dp); |
| 3926 | ret = nfs_ok; |
| 3927 | break; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3928 | default: |
| 3929 | ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3930 | } |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3931 | out: |
| 3932 | nfs4_unlock_state(); |
| 3933 | return ret; |
| 3934 | } |
| 3935 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 3936 | static inline int |
| 3937 | setlkflg (int type) |
| 3938 | { |
| 3939 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 3940 | RD_STATE : WR_STATE; |
| 3941 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3943 | 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] | 3944 | { |
| 3945 | struct svc_fh *current_fh = &cstate->current_fh; |
| 3946 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 3947 | __be32 status; |
| 3948 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3949 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 3950 | if (status) |
| 3951 | return status; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3952 | if (stp->st_stid.sc_type == NFS4_CLOSED_STID |
| 3953 | || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3954 | /* |
| 3955 | * "Closed" stateid's exist *only* to return |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3956 | * nfserr_replay_me from the previous step, and |
| 3957 | * revoked delegations are kept only for free_stateid. |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3958 | */ |
| 3959 | return nfserr_bad_stateid; |
| 3960 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); |
| 3961 | if (status) |
| 3962 | return status; |
| 3963 | return nfs4_check_fh(current_fh, stp); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3964 | } |
| 3965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | /* |
| 3967 | * Checks for sequence id mutating operations. |
| 3968 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3969 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 3970 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 3971 | stateid_t *stateid, char typemask, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 3972 | struct nfs4_ol_stateid **stpp, |
| 3973 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3975 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3976 | struct nfs4_stid *s; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 3977 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3978 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 3979 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
| 3980 | seqid, STATEID_VAL(stateid)); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 3981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3982 | *stpp = NULL; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 3983 | status = nfsd4_lookup_stateid(stateid, typemask, &s, |
| 3984 | cstate->minorversion, nn); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3985 | if (status) |
| 3986 | return status; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 3987 | stp = openlockstateid(s); |
J. Bruce Fields | 3d74e6a | 2013-03-22 17:44:19 -0400 | [diff] [blame] | 3988 | if (!nfsd4_has_session(cstate)) |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 3989 | cstate->replay_owner = stp->st_stateowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3990 | |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 3991 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
| 3992 | if (!status) |
| 3993 | *stpp = stp; |
| 3994 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3995 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 3996 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 3997 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 3998 | 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] | 3999 | { |
| 4000 | __be32 status; |
| 4001 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4003 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4004 | NFS4_OPEN_STID, stpp, nn); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 4005 | if (status) |
| 4006 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4007 | oo = openowner((*stpp)->st_stateowner); |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4008 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4009 | return nfserr_bad_stateid; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4010 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | } |
| 4012 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4013 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4014 | 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] | 4015 | struct nfsd4_open_confirm *oc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4017 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4018 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4019 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4020 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4021 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4022 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 4023 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4025 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 4026 | if (status) |
| 4027 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4028 | |
| 4029 | nfs4_lock_state(); |
| 4030 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4031 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4032 | oc->oc_seqid, &oc->oc_req_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4033 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4034 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4035 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4036 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4037 | status = nfserr_bad_stateid; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4038 | if (oo->oo_flags & NFS4_OO_CONFIRMED) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4039 | goto out; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4040 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4041 | update_stateid(&stp->st_stid.sc_stateid); |
| 4042 | 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] | 4043 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4044 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 4045 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 4046 | nfsd4_client_record_create(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4047 | status = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4048 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4049 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4050 | if (!cstate->replay_owner) |
| 4051 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | return status; |
| 4053 | } |
| 4054 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4055 | 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] | 4056 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4057 | if (!test_access(access, stp)) |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4058 | return; |
| 4059 | nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access)); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4060 | clear_access(access, stp); |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4061 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 4062 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4063 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 4064 | { |
| 4065 | switch (to_access) { |
| 4066 | case NFS4_SHARE_ACCESS_READ: |
| 4067 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 4068 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 4069 | break; |
| 4070 | case NFS4_SHARE_ACCESS_WRITE: |
| 4071 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 4072 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 4073 | break; |
| 4074 | case NFS4_SHARE_ACCESS_BOTH: |
| 4075 | break; |
| 4076 | default: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4077 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4078 | } |
| 4079 | } |
| 4080 | |
| 4081 | static void |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4082 | reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | { |
| 4084 | int i; |
| 4085 | for (i = 0; i < 4; i++) { |
| 4086 | if ((i & deny) != i) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4087 | clear_deny(i, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4088 | } |
| 4089 | } |
| 4090 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4091 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4092 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
| 4093 | struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4094 | struct nfsd4_open_downgrade *od) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4095 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4096 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4097 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4098 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4099 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4100 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 4101 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4102 | |
J. Bruce Fields | c30e92d | 2011-10-10 17:34:31 -0400 | [diff] [blame] | 4103 | /* We don't yet support WANT bits: */ |
Benny Halevy | 2c8bd7e | 2012-02-16 20:57:09 +0200 | [diff] [blame] | 4104 | if (od->od_deleg_want) |
| 4105 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 4106 | od->od_deleg_want); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4107 | |
| 4108 | nfs4_lock_state(); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4109 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4110 | &od->od_stateid, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4111 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4112 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | status = nfserr_inval; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4114 | if (!test_access(od->od_share_access, stp)) { |
| 4115 | dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | stp->st_access_bmap, od->od_share_access); |
| 4117 | goto out; |
| 4118 | } |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4119 | if (!test_deny(od->od_share_deny, stp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4120 | dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n", |
| 4121 | stp->st_deny_bmap, od->od_share_deny); |
| 4122 | goto out; |
| 4123 | } |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4124 | nfs4_stateid_downgrade(stp, od->od_share_access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4126 | reset_union_bmap_deny(od->od_share_deny, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4128 | update_stateid(&stp->st_stid.sc_stateid); |
| 4129 | memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | status = nfs_ok; |
| 4131 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4132 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4133 | if (!cstate->replay_owner) |
| 4134 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | return status; |
| 4136 | } |
| 4137 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4138 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 4139 | { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4140 | struct nfs4_client *clp = s->st_stid.sc_client; |
| 4141 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 4142 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4143 | s->st_stid.sc_type = NFS4_CLOSED_STID; |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4144 | unhash_open_stateid(s); |
| 4145 | |
| 4146 | if (clp->cl_minorversion) { |
| 4147 | free_generic_stateid(s); |
| 4148 | if (list_empty(&oo->oo_owner.so_stateids)) |
| 4149 | release_openowner(oo); |
| 4150 | } else { |
| 4151 | oo->oo_last_closed_stid = s; |
| 4152 | /* |
| 4153 | * In the 4.0 case we need to keep the owners around a |
| 4154 | * little while to handle CLOSE replay. |
| 4155 | */ |
| 4156 | if (list_empty(&oo->oo_owner.so_stateids)) |
| 4157 | move_to_close_lru(oo, clp->net); |
| 4158 | } |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 4159 | } |
| 4160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | /* |
| 4162 | * nfs4_unlock_state() called after encode |
| 4163 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4164 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4165 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4166 | struct nfsd4_close *close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4168 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4169 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4170 | struct net *net = SVC_NET(rqstp); |
| 4171 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4173 | dprintk("NFSD: nfsd4_close on file %pd\n", |
| 4174 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4175 | |
| 4176 | nfs4_lock_state(); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4177 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 4178 | &close->cl_stateid, |
| 4179 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4180 | &stp, nn); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4181 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4182 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | goto out; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4184 | update_stateid(&stp->st_stid.sc_stateid); |
| 4185 | memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4187 | nfsd4_close_open_stateid(stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | out: |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4189 | if (!cstate->replay_owner) |
| 4190 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | return status; |
| 4192 | } |
| 4193 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4194 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4195 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4196 | struct nfsd4_delegreturn *dr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4198 | struct nfs4_delegation *dp; |
| 4199 | stateid_t *stateid = &dr->dr_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4200 | struct nfs4_stid *s; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4201 | __be32 status; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4202 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4204 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4205 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4206 | |
| 4207 | nfs4_lock_state(); |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4208 | status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s, |
| 4209 | cstate->minorversion, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4210 | if (status) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4211 | goto out; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4212 | dp = delegstateid(s); |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 4213 | 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] | 4214 | if (status) |
| 4215 | goto out; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4216 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4217 | destroy_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4218 | out: |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4219 | nfs4_unlock_state(); |
| 4220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | return status; |
| 4222 | } |
| 4223 | |
| 4224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4227 | static inline u64 |
| 4228 | end_offset(u64 start, u64 len) |
| 4229 | { |
| 4230 | u64 end; |
| 4231 | |
| 4232 | end = start + len; |
| 4233 | return end >= start ? end: NFS4_MAX_UINT64; |
| 4234 | } |
| 4235 | |
| 4236 | /* last octet in a range */ |
| 4237 | static inline u64 |
| 4238 | last_byte_offset(u64 start, u64 len) |
| 4239 | { |
| 4240 | u64 end; |
| 4241 | |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4242 | WARN_ON_ONCE(!len); |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4243 | end = start + len; |
| 4244 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 4245 | } |
| 4246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | /* |
| 4248 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 4249 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 4250 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 4251 | * locking, this prevents us from being completely protocol-compliant. The |
| 4252 | * real solution to this problem is to start using unsigned file offsets in |
| 4253 | * the VFS, but this is a very deep change! |
| 4254 | */ |
| 4255 | static inline void |
| 4256 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 4257 | { |
| 4258 | if (lock->fl_start < 0) |
| 4259 | lock->fl_start = OFFSET_MAX; |
| 4260 | if (lock->fl_end < 0) |
| 4261 | lock->fl_end = OFFSET_MAX; |
| 4262 | } |
| 4263 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4264 | /* Hack!: For now, we're defining this just so we can use a pointer to it |
| 4265 | * as a unique cookie to identify our (NFSv4's) posix locks. */ |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 4266 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4267 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | |
| 4269 | static inline void |
| 4270 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 4271 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4272 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4274 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4275 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
| 4276 | deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data, |
| 4277 | lo->lo_owner.so_owner.len, GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 4278 | if (!deny->ld_owner.data) |
| 4279 | /* We just don't care that much */ |
| 4280 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4281 | deny->ld_owner.len = lo->lo_owner.so_owner.len; |
| 4282 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4283 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 4284 | nevermind: |
| 4285 | deny->ld_owner.len = 0; |
| 4286 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4287 | deny->ld_clientid.cl_boot = 0; |
| 4288 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4289 | } |
| 4290 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4291 | deny->ld_length = NFS4_MAX_UINT64; |
| 4292 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4293 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 4294 | deny->ld_type = NFS4_READ_LT; |
| 4295 | if (fl->fl_type != F_RDLCK) |
| 4296 | deny->ld_type = NFS4_WRITE_LT; |
| 4297 | } |
| 4298 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4299 | static struct nfs4_lockowner * |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame^] | 4300 | find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner, |
| 4301 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | { |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame^] | 4303 | unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner); |
| 4304 | struct nfs4_stateowner *so; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame^] | 4306 | list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) { |
| 4307 | if (so->so_is_open_owner) |
| 4308 | continue; |
| 4309 | if (!same_owner_str(so, owner, clid)) |
| 4310 | continue; |
| 4311 | return lockowner(so); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | } |
| 4313 | return NULL; |
| 4314 | } |
| 4315 | |
| 4316 | /* |
| 4317 | * Alloc a lock owner structure. |
| 4318 | * 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] | 4319 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | * |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 4321 | * strhashval = ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4323 | static struct nfs4_lockowner * |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4324 | 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] | 4325 | struct nfs4_lockowner *lo; |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame^] | 4326 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4328 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 4329 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | return NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4331 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 4332 | lo->lo_owner.so_is_open_owner = 0; |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 4333 | /* It is the openowner seqid that will be incremented in encode in the |
| 4334 | * case of new lockowners; so increment the lock seqid manually: */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4335 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1; |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame^] | 4336 | list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4337 | return lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | } |
| 4339 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4340 | static struct nfs4_ol_stateid * |
| 4341 | alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4343 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 4344 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 4346 | stp = nfs4_alloc_stateid(clp); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 4347 | if (stp == NULL) |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 4348 | return NULL; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 4349 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 4350 | list_add(&stp->st_perfile, &fp->fi_stateids); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4351 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
| 4352 | stp->st_stateowner = &lo->lo_owner; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4353 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | stp->st_file = fp; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4355 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4357 | stp->st_openstp = open_stp; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4358 | list_add(&stp->st_locks, &open_stp->st_locks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4359 | return stp; |
| 4360 | } |
| 4361 | |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4362 | static struct nfs4_ol_stateid * |
| 4363 | find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) |
| 4364 | { |
| 4365 | struct nfs4_ol_stateid *lst; |
| 4366 | |
| 4367 | list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { |
| 4368 | if (lst->st_file == fp) |
| 4369 | return lst; |
| 4370 | } |
| 4371 | return NULL; |
| 4372 | } |
| 4373 | |
| 4374 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 4375 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | check_lock_length(u64 offset, u64 length) |
| 4377 | { |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4378 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | LOFF_OVERFLOW(offset, length))); |
| 4380 | } |
| 4381 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4382 | 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] | 4383 | { |
| 4384 | struct nfs4_file *fp = lock_stp->st_file; |
| 4385 | int oflag = nfs4_access_to_omode(access); |
| 4386 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4387 | if (test_access(access, lock_stp)) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4388 | return; |
| 4389 | nfs4_file_get_access(fp, oflag); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4390 | set_access(access, lock_stp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4391 | } |
| 4392 | |
J. Bruce Fields | 2355c59 | 2012-04-25 16:56:22 -0400 | [diff] [blame] | 4393 | 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] | 4394 | { |
| 4395 | struct nfs4_file *fi = ost->st_file; |
| 4396 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 4397 | struct nfs4_client *cl = oo->oo_owner.so_client; |
| 4398 | struct nfs4_lockowner *lo; |
| 4399 | unsigned int strhashval; |
Stanislav Kinsbursky | 20e9e2b | 2012-11-14 18:21:46 +0300 | [diff] [blame] | 4400 | struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4401 | |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame^] | 4402 | lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4403 | if (!lo) { |
| 4404 | strhashval = ownerstr_hashval(cl->cl_clientid.cl_id, |
| 4405 | &lock->v.new.owner); |
| 4406 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 4407 | if (lo == NULL) |
| 4408 | return nfserr_jukebox; |
| 4409 | } else { |
| 4410 | /* with an existing lockowner, seqids must be the same */ |
| 4411 | if (!cstate->minorversion && |
| 4412 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4413 | return nfserr_bad_seqid; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4414 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4415 | |
| 4416 | *lst = find_lock_stateid(lo, fi); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4417 | if (*lst == NULL) { |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4418 | *lst = alloc_init_lock_stateid(lo, fi, ost); |
| 4419 | if (*lst == NULL) { |
| 4420 | release_lockowner_if_empty(lo); |
| 4421 | return nfserr_jukebox; |
| 4422 | } |
| 4423 | *new = true; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4424 | } |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4425 | return nfs_ok; |
| 4426 | } |
| 4427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | /* |
| 4429 | * LOCK operation |
| 4430 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4431 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4432 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4433 | struct nfsd4_lock *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4435 | struct nfs4_openowner *open_sop = NULL; |
| 4436 | struct nfs4_lockowner *lock_sop = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4437 | struct nfs4_ol_stateid *lock_stp; |
J. Bruce Fields | 7d94784 | 2010-08-20 18:09:31 -0400 | [diff] [blame] | 4438 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4439 | struct file_lock *file_lock = NULL; |
| 4440 | struct file_lock *conflock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4441 | __be32 status = 0; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4442 | bool new_state = false; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4443 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4444 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4445 | struct net *net = SVC_NET(rqstp); |
| 4446 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | |
| 4448 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 4449 | (long long) lock->lk_offset, |
| 4450 | (long long) lock->lk_length); |
| 4451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 4453 | return nfserr_inval; |
| 4454 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4455 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 4456 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 4457 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 4458 | return status; |
| 4459 | } |
| 4460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | nfs4_lock_state(); |
| 4462 | |
| 4463 | if (lock->lk_is_new) { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4464 | struct nfs4_ol_stateid *open_stp = NULL; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 4465 | |
| 4466 | if (nfsd4_has_session(cstate)) |
| 4467 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
| 4468 | memcpy(&lock->v.new.clientid, |
| 4469 | &cstate->session->se_client->cl_clientid, |
| 4470 | sizeof(clientid_t)); |
| 4471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4472 | status = nfserr_stale_clientid; |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 4473 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4477 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | lock->lk_new_open_seqid, |
| 4479 | &lock->lk_new_open_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4480 | &open_stp, nn); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 4481 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4482 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4483 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4484 | status = nfserr_bad_stateid; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 4485 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4486 | &lock->v.new.clientid)) |
| 4487 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4488 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
| 4489 | &lock_stp, &new_state); |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 4490 | } else |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4491 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4492 | lock->lk_old_lock_seqid, |
| 4493 | &lock->lk_old_lock_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4494 | NFS4_LOCK_STID, &lock_stp, nn); |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 4495 | if (status) |
| 4496 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4497 | lock_sop = lockowner(lock_stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 4499 | lkflg = setlkflg(lock->lk_type); |
| 4500 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 4501 | if (status) |
| 4502 | goto out; |
| 4503 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 4504 | status = nfserr_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4505 | if (locks_in_grace(net) && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 4506 | goto out; |
| 4507 | status = nfserr_no_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4508 | if (!locks_in_grace(net) && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 4509 | goto out; |
| 4510 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4511 | file_lock = locks_alloc_lock(); |
| 4512 | if (!file_lock) { |
| 4513 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 4514 | status = nfserr_jukebox; |
| 4515 | goto out; |
| 4516 | } |
| 4517 | |
| 4518 | locks_init_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | switch (lock->lk_type) { |
| 4520 | case NFS4_READ_LT: |
| 4521 | case NFS4_READW_LT: |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4522 | filp = find_readable_file(lock_stp->st_file); |
| 4523 | if (filp) |
| 4524 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4525 | file_lock->fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 4526 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | case NFS4_WRITE_LT: |
| 4528 | case NFS4_WRITEW_LT: |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 4529 | filp = find_writeable_file(lock_stp->st_file); |
| 4530 | if (filp) |
| 4531 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4532 | file_lock->fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 4533 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | default: |
| 4535 | status = nfserr_inval; |
| 4536 | goto out; |
| 4537 | } |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4538 | if (!filp) { |
| 4539 | status = nfserr_openmode; |
| 4540 | goto out; |
| 4541 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4542 | file_lock->fl_owner = (fl_owner_t)lock_sop; |
| 4543 | file_lock->fl_pid = current->tgid; |
| 4544 | file_lock->fl_file = filp; |
| 4545 | file_lock->fl_flags = FL_POSIX; |
| 4546 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 4547 | file_lock->fl_start = lock->lk_offset; |
| 4548 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 4549 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4551 | conflock = locks_alloc_lock(); |
| 4552 | if (!conflock) { |
| 4553 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 4554 | status = nfserr_jukebox; |
| 4555 | goto out; |
| 4556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4558 | err = vfs_lock_file(filp, F_SETLK, file_lock, conflock); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4559 | switch (-err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | case 0: /* success! */ |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4561 | update_stateid(&lock_stp->st_stid.sc_stateid); |
| 4562 | memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | sizeof(stateid_t)); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4564 | status = 0; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4565 | break; |
| 4566 | case (EAGAIN): /* conflock holds conflicting lock */ |
| 4567 | status = nfserr_denied; |
| 4568 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4569 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4570 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | case (EDEADLK): |
| 4572 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4573 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 4574 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 4575 | 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] | 4576 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4577 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4579 | out: |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 4580 | if (status && new_state) |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 4581 | release_lock_stateid(lock_stp); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4582 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4583 | if (!cstate->replay_owner) |
| 4584 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4585 | if (file_lock) |
| 4586 | locks_free_lock(file_lock); |
| 4587 | if (conflock) |
| 4588 | locks_free_lock(conflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | return status; |
| 4590 | } |
| 4591 | |
| 4592 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 4593 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 4594 | * so we do a temporary open here just to get an open file to pass to |
| 4595 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 4596 | * inode operation.) |
| 4597 | */ |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 4598 | 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] | 4599 | { |
| 4600 | struct file *file; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 4601 | __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); |
| 4602 | if (!err) { |
| 4603 | err = nfserrno(vfs_test_lock(file, lock)); |
| 4604 | nfsd_close(file); |
| 4605 | } |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 4606 | return err; |
| 4607 | } |
| 4608 | |
| 4609 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | * LOCKT operation |
| 4611 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4612 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4613 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4614 | struct nfsd4_lockt *lockt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | { |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4616 | struct file_lock *file_lock = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4617 | struct nfs4_lockowner *lo; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4618 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 4619 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4621 | if (locks_in_grace(SVC_NET(rqstp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4622 | return nfserr_grace; |
| 4623 | |
| 4624 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 4625 | return nfserr_inval; |
| 4626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4627 | nfs4_lock_state(); |
| 4628 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 4629 | if (!nfsd4_has_session(cstate)) { |
| 4630 | status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL); |
| 4631 | if (status) |
| 4632 | goto out; |
| 4633 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 4635 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4637 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4638 | file_lock = locks_alloc_lock(); |
| 4639 | if (!file_lock) { |
| 4640 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 4641 | status = nfserr_jukebox; |
| 4642 | goto out; |
| 4643 | } |
| 4644 | locks_init_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | switch (lockt->lt_type) { |
| 4646 | case NFS4_READ_LT: |
| 4647 | case NFS4_READW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4648 | file_lock->fl_type = F_RDLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | break; |
| 4650 | case NFS4_WRITE_LT: |
| 4651 | case NFS4_WRITEW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4652 | file_lock->fl_type = F_WRLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | break; |
| 4654 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 4655 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | status = nfserr_inval; |
| 4657 | goto out; |
| 4658 | } |
| 4659 | |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame^] | 4660 | lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4661 | if (lo) |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4662 | file_lock->fl_owner = (fl_owner_t)lo; |
| 4663 | file_lock->fl_pid = current->tgid; |
| 4664 | file_lock->fl_flags = FL_POSIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4666 | file_lock->fl_start = lockt->lt_offset; |
| 4667 | 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] | 4668 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4669 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4670 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4671 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 4672 | if (status) |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 4673 | goto out; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 4674 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4675 | if (file_lock->fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4676 | status = nfserr_denied; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4677 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | } |
| 4679 | out: |
| 4680 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4681 | if (file_lock) |
| 4682 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | return status; |
| 4684 | } |
| 4685 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4686 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4687 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4688 | struct nfsd4_locku *locku) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4690 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4692 | struct file_lock *file_lock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4693 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4694 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4695 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 4696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 4698 | (long long) locku->lu_offset, |
| 4699 | (long long) locku->lu_length); |
| 4700 | |
| 4701 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 4702 | return nfserr_inval; |
| 4703 | |
| 4704 | nfs4_lock_state(); |
| 4705 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4706 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4707 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 4708 | &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4709 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4710 | goto out; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4711 | filp = find_any_file(stp->st_file); |
| 4712 | if (!filp) { |
| 4713 | status = nfserr_lock_range; |
| 4714 | goto out; |
| 4715 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4716 | file_lock = locks_alloc_lock(); |
| 4717 | if (!file_lock) { |
| 4718 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 4719 | status = nfserr_jukebox; |
| 4720 | goto out; |
| 4721 | } |
| 4722 | locks_init_lock(file_lock); |
| 4723 | file_lock->fl_type = F_UNLCK; |
J. Bruce Fields | 0a262ff | 2013-06-17 17:29:40 -0400 | [diff] [blame] | 4724 | file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4725 | file_lock->fl_pid = current->tgid; |
| 4726 | file_lock->fl_file = filp; |
| 4727 | file_lock->fl_flags = FL_POSIX; |
| 4728 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 4729 | file_lock->fl_start = locku->lu_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4730 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4731 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 4732 | locku->lu_length); |
| 4733 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4735 | err = vfs_lock_file(filp, F_SETLK, file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4736 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 4737 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4738 | goto out_nfserr; |
| 4739 | } |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4740 | update_stateid(&stp->st_stid.sc_stateid); |
| 4741 | memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | |
| 4743 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4744 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 71c3bcd | 2011-09-27 21:42:29 -0400 | [diff] [blame] | 4745 | if (!cstate->replay_owner) |
| 4746 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 4747 | if (file_lock) |
| 4748 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | return status; |
| 4750 | |
| 4751 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4752 | status = nfserrno(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | goto out; |
| 4754 | } |
| 4755 | |
| 4756 | /* |
| 4757 | * returns |
| 4758 | * 1: locks held by lockowner |
| 4759 | * 0: no locks held by lockowner |
| 4760 | */ |
| 4761 | static int |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4762 | check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4763 | { |
| 4764 | struct file_lock **flpp; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4765 | struct inode *inode = filp->fi_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | int status = 0; |
| 4767 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 4768 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) { |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 4770 | if ((*flpp)->fl_owner == (fl_owner_t)lowner) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | status = 1; |
| 4772 | goto out; |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 4773 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4774 | } |
| 4775 | out: |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 4776 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | return status; |
| 4778 | } |
| 4779 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4780 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 4781 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 4782 | struct nfsd4_compound_state *cstate, |
| 4783 | struct nfsd4_release_lockowner *rlockowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4784 | { |
| 4785 | clientid_t *clid = &rlockowner->rl_clientid; |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4786 | struct nfs4_stateowner *sop; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4787 | struct nfs4_lockowner *lo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4788 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4790 | struct list_head matches; |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 4791 | unsigned int hashval = ownerstr_hashval(clid->cl_id, owner); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4792 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 4793 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | |
| 4795 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 4796 | clid->cl_boot, clid->cl_id); |
| 4797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | nfs4_lock_state(); |
| 4799 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 4800 | status = lookup_clientid(clid, cstate->minorversion, nn, NULL); |
| 4801 | if (status) |
| 4802 | goto out; |
| 4803 | |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4804 | status = nfserr_locks_held; |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4805 | INIT_LIST_HEAD(&matches); |
J. Bruce Fields | 06f1f86 | 2011-11-07 16:58:18 -0500 | [diff] [blame] | 4806 | |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 4807 | list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) { |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 4808 | if (sop->so_is_open_owner) |
| 4809 | continue; |
J. Bruce Fields | 06f1f86 | 2011-11-07 16:58:18 -0500 | [diff] [blame] | 4810 | if (!same_owner_str(sop, owner, clid)) |
| 4811 | continue; |
| 4812 | list_for_each_entry(stp, &sop->so_stateids, |
| 4813 | st_perstateowner) { |
| 4814 | lo = lockowner(sop); |
| 4815 | if (check_for_locks(stp->st_file, lo)) |
| 4816 | goto out; |
| 4817 | list_add(&lo->lo_list, &matches); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4818 | } |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4819 | } |
| 4820 | /* Clients probably won't expect us to return with some (but not all) |
| 4821 | * of the lockowner state released; so don't release any until all |
| 4822 | * have been checked. */ |
| 4823 | status = nfs_ok; |
NeilBrown | 0fa822e | 2005-07-07 17:59:14 -0700 | [diff] [blame] | 4824 | while (!list_empty(&matches)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4825 | lo = list_entry(matches.next, struct nfs4_lockowner, |
| 4826 | lo_list); |
NeilBrown | 0fa822e | 2005-07-07 17:59:14 -0700 | [diff] [blame] | 4827 | /* unhash_stateowner deletes so_perclient only |
| 4828 | * for openowners. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4829 | list_del(&lo->lo_list); |
| 4830 | release_lockowner(lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | } |
| 4832 | out: |
| 4833 | nfs4_unlock_state(); |
| 4834 | return status; |
| 4835 | } |
| 4836 | |
| 4837 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 4838 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 4840 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4841 | } |
| 4842 | |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 4843 | bool |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 4844 | nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 4845 | { |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 4846 | struct nfs4_client_reclaim *crp; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 4847 | |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 4848 | crp = nfsd4_find_reclaim_client(name, nn); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 4849 | return (crp && crp->cr_clp); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 4850 | } |
| 4851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | /* |
| 4853 | * failure => all reset bets are off, nfserr_no_grace... |
| 4854 | */ |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 4855 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 4856 | nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | { |
| 4858 | unsigned int strhashval; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 4859 | struct nfs4_client_reclaim *crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4860 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 4861 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
| 4862 | crp = alloc_reclaim(); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 4863 | if (crp) { |
| 4864 | strhashval = clientstr_hashval(name); |
| 4865 | INIT_LIST_HEAD(&crp->cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 4866 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 4867 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 4868 | crp->cr_clp = NULL; |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 4869 | nn->reclaim_str_hashtbl_size++; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 4870 | } |
| 4871 | return crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4872 | } |
| 4873 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 4874 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 4875 | 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] | 4876 | { |
| 4877 | list_del(&crp->cr_strhash); |
| 4878 | kfree(crp); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 4879 | nn->reclaim_str_hashtbl_size--; |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 4880 | } |
| 4881 | |
| 4882 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 4883 | nfs4_release_reclaim(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4884 | { |
| 4885 | struct nfs4_client_reclaim *crp = NULL; |
| 4886 | int i; |
| 4887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 4889 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 4890 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | struct nfs4_client_reclaim, cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 4892 | nfs4_remove_reclaim_record(crp, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4893 | } |
| 4894 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4895 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | } |
| 4897 | |
| 4898 | /* |
| 4899 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 4900 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 4901 | nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4902 | { |
| 4903 | unsigned int strhashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | struct nfs4_client_reclaim *crp = NULL; |
| 4905 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 4906 | dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 4908 | strhashval = clientstr_hashval(recdir); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 4909 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 4910 | if (same_name(crp->cr_recdir, recdir)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4911 | return crp; |
| 4912 | } |
| 4913 | } |
| 4914 | return NULL; |
| 4915 | } |
| 4916 | |
| 4917 | /* |
| 4918 | * Called from OPEN. Look for clientid in reclaim list. |
| 4919 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4920 | __be32 |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4921 | nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4922 | { |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 4923 | struct nfs4_client *clp; |
| 4924 | |
| 4925 | /* find clientid in conf_id_hashtbl */ |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 4926 | clp = find_confirmed_client(clid, sessions, nn); |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 4927 | if (clp == NULL) |
| 4928 | return nfserr_reclaim_bad; |
| 4929 | |
| 4930 | return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | } |
| 4932 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 4933 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
| 4934 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 4935 | u64 nfsd_forget_client(struct nfs4_client *clp, u64 max) |
| 4936 | { |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 4937 | if (mark_client_expired(clp)) |
| 4938 | return 0; |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 4939 | expire_client(clp); |
| 4940 | return 1; |
| 4941 | } |
| 4942 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 4943 | u64 nfsd_print_client(struct nfs4_client *clp, u64 num) |
| 4944 | { |
| 4945 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 4946 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 4947 | printk(KERN_INFO "NFS Client: %s\n", buf); |
| 4948 | return 1; |
| 4949 | } |
| 4950 | |
| 4951 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, |
| 4952 | const char *type) |
| 4953 | { |
| 4954 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 4955 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 4956 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
| 4957 | } |
| 4958 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4959 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, |
| 4960 | void (*func)(struct nfs4_ol_stateid *)) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 4961 | { |
| 4962 | struct nfs4_openowner *oop; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 4963 | struct nfs4_ol_stateid *stp, *st_next; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4964 | struct nfs4_ol_stateid *lst, *lst_next; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 4965 | u64 count = 0; |
| 4966 | |
| 4967 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4968 | list_for_each_entry_safe(stp, st_next, |
| 4969 | &oop->oo_owner.so_stateids, st_perstateowner) { |
| 4970 | list_for_each_entry_safe(lst, lst_next, |
| 4971 | &stp->st_locks, st_locks) { |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 4972 | if (func) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4973 | func(lst); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 4974 | if (++count == max) |
| 4975 | return count; |
| 4976 | } |
| 4977 | } |
| 4978 | } |
| 4979 | |
| 4980 | return count; |
| 4981 | } |
| 4982 | |
| 4983 | u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max) |
| 4984 | { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4985 | return nfsd_foreach_client_lock(clp, max, release_lock_stateid); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 4986 | } |
| 4987 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 4988 | u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max) |
| 4989 | { |
| 4990 | u64 count = nfsd_foreach_client_lock(clp, max, NULL); |
| 4991 | nfsd_print_count(clp, count, "locked files"); |
| 4992 | return count; |
| 4993 | } |
| 4994 | |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 4995 | static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *)) |
| 4996 | { |
| 4997 | struct nfs4_openowner *oop, *next; |
| 4998 | u64 count = 0; |
| 4999 | |
| 5000 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { |
| 5001 | if (func) |
| 5002 | func(oop); |
| 5003 | if (++count == max) |
| 5004 | break; |
| 5005 | } |
| 5006 | |
| 5007 | return count; |
| 5008 | } |
| 5009 | |
| 5010 | u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max) |
| 5011 | { |
| 5012 | return nfsd_foreach_client_open(clp, max, release_openowner); |
| 5013 | } |
| 5014 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5015 | u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max) |
| 5016 | { |
| 5017 | u64 count = nfsd_foreach_client_open(clp, max, NULL); |
| 5018 | nfsd_print_count(clp, count, "open files"); |
| 5019 | return count; |
| 5020 | } |
| 5021 | |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5022 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
| 5023 | struct list_head *victims) |
| 5024 | { |
| 5025 | struct nfs4_delegation *dp, *next; |
| 5026 | u64 count = 0; |
| 5027 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5028 | lockdep_assert_held(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5029 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
| 5030 | if (victims) |
| 5031 | list_move(&dp->dl_recall_lru, victims); |
| 5032 | if (++count == max) |
| 5033 | break; |
| 5034 | } |
| 5035 | return count; |
| 5036 | } |
| 5037 | |
| 5038 | u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max) |
| 5039 | { |
| 5040 | struct nfs4_delegation *dp, *next; |
| 5041 | LIST_HEAD(victims); |
| 5042 | u64 count; |
| 5043 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5044 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5045 | count = nfsd_find_all_delegations(clp, max, &victims); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5046 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5047 | |
| 5048 | list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5049 | revoke_delegation(dp); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5050 | |
| 5051 | return count; |
| 5052 | } |
| 5053 | |
| 5054 | u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max) |
| 5055 | { |
| 5056 | struct nfs4_delegation *dp, *next; |
| 5057 | LIST_HEAD(victims); |
| 5058 | u64 count; |
| 5059 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5060 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5061 | count = nfsd_find_all_delegations(clp, max, &victims); |
| 5062 | list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) |
| 5063 | nfsd_break_one_deleg(dp); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5064 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5065 | |
| 5066 | return count; |
| 5067 | } |
| 5068 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5069 | u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max) |
| 5070 | { |
| 5071 | u64 count = 0; |
| 5072 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5073 | spin_lock(&state_lock); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5074 | count = nfsd_find_all_delegations(clp, max, NULL); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5075 | spin_unlock(&state_lock); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5076 | |
| 5077 | nfsd_print_count(clp, count, "delegations"); |
| 5078 | return count; |
| 5079 | } |
| 5080 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5081 | 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] | 5082 | { |
| 5083 | struct nfs4_client *clp, *next; |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5084 | u64 count = 0; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5085 | 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] | 5086 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5087 | if (!nfsd_netns_ready(nn)) |
| 5088 | return 0; |
| 5089 | |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5090 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5091 | count += func(clp, max - count); |
| 5092 | if ((max != 0) && (count >= max)) |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5093 | break; |
| 5094 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5095 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5096 | return count; |
| 5097 | } |
| 5098 | |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 5099 | struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) |
| 5100 | { |
| 5101 | struct nfs4_client *clp; |
| 5102 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id); |
| 5103 | |
| 5104 | if (!nfsd_netns_ready(nn)) |
| 5105 | return NULL; |
| 5106 | |
| 5107 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 5108 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) |
| 5109 | return clp; |
| 5110 | } |
| 5111 | return NULL; |
| 5112 | } |
| 5113 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5114 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
| 5115 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 5116 | /* |
| 5117 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 5118 | * client may quite reasonably hang on to a delegation as long as it has |
| 5119 | * the inode cached. This becomes an obvious problem the first time a |
| 5120 | * client's inode cache approaches the size of the server's total memory. |
| 5121 | * |
| 5122 | * For now we avoid this problem by imposing a hard limit on the number |
| 5123 | * of delegations, which varies according to the server's memory size. |
| 5124 | */ |
| 5125 | static void |
| 5126 | set_max_delegations(void) |
| 5127 | { |
| 5128 | /* |
| 5129 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 5130 | * estimates suggest that in the worst case (where every delegation |
| 5131 | * is for a different inode), a delegation could take about 1.5K, |
| 5132 | * giving a worst case usage of about 6% of memory. |
| 5133 | */ |
| 5134 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 5135 | } |
| 5136 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5137 | static int nfs4_state_create_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5138 | { |
| 5139 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 5140 | int i; |
| 5141 | |
| 5142 | nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5143 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 5144 | if (!nn->conf_id_hashtbl) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5145 | goto err; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5146 | nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5147 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 5148 | if (!nn->unconf_id_hashtbl) |
| 5149 | goto err_unconf_id; |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5150 | nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5151 | OWNER_HASH_SIZE, GFP_KERNEL); |
| 5152 | if (!nn->ownerstr_hashtbl) |
| 5153 | goto err_ownerstr; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5154 | nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5155 | SESSION_HASH_SIZE, GFP_KERNEL); |
| 5156 | if (!nn->sessionid_hashtbl) |
| 5157 | goto err_sessionid; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5158 | |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5159 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5160 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5161 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5162 | } |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5163 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 5164 | INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5165 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 5166 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5167 | nn->conf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5168 | nn->unconf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5169 | INIT_LIST_HEAD(&nn->client_lru); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 5170 | INIT_LIST_HEAD(&nn->close_lru); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5171 | INIT_LIST_HEAD(&nn->del_recall_lru); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5172 | spin_lock_init(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5173 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5174 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5175 | get_net(net); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5176 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5177 | return 0; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5178 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5179 | err_sessionid: |
Stanislav Kinsbursky | 20e9e2b | 2012-11-14 18:21:46 +0300 | [diff] [blame] | 5180 | kfree(nn->ownerstr_hashtbl); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5181 | err_ownerstr: |
| 5182 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5183 | err_unconf_id: |
| 5184 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5185 | err: |
| 5186 | return -ENOMEM; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5187 | } |
| 5188 | |
| 5189 | static void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5190 | nfs4_state_destroy_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5191 | { |
| 5192 | int i; |
| 5193 | struct nfs4_client *clp = NULL; |
| 5194 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 5195 | |
| 5196 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 5197 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 5198 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 5199 | destroy_client(clp); |
| 5200 | } |
| 5201 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5202 | |
Kinglong Mee | 2b90563 | 2014-03-26 22:09:30 +0800 | [diff] [blame] | 5203 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 5204 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 5205 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 5206 | destroy_client(clp); |
| 5207 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5208 | } |
| 5209 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5210 | kfree(nn->sessionid_hashtbl); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5211 | kfree(nn->ownerstr_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5212 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5213 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5214 | put_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5215 | } |
| 5216 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 5217 | int |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5218 | nfs4_state_start_net(struct net *net) |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 5219 | { |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 5220 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5221 | int ret; |
| 5222 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5223 | ret = nfs4_state_create_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5224 | if (ret) |
| 5225 | return ret; |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 5226 | nfsd4_client_tracking_init(net); |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 5227 | nn->boot_time = get_seconds(); |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5228 | locks_start_grace(net, &nn->nfsd4_manager); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5229 | nn->grace_ended = false; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5230 | printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n", |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 5231 | nn->nfsd4_grace, net); |
| 5232 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5233 | return 0; |
| 5234 | } |
| 5235 | |
| 5236 | /* initialization to perform when the nfsd service is started: */ |
| 5237 | |
| 5238 | int |
| 5239 | nfs4_state_start(void) |
| 5240 | { |
| 5241 | int ret; |
| 5242 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5243 | ret = set_callback_cred(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5244 | if (ret) |
| 5245 | return -ENOMEM; |
NeilBrown | 58da282 | 2005-06-23 22:03:19 -0700 | [diff] [blame] | 5246 | laundry_wq = create_singlethread_workqueue("nfsd4"); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 5247 | if (laundry_wq == NULL) { |
| 5248 | ret = -ENOMEM; |
| 5249 | goto out_recovery; |
| 5250 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5251 | ret = nfsd4_create_callback_queue(); |
| 5252 | if (ret) |
| 5253 | goto out_free_laundry; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5254 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 5255 | set_max_delegations(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5256 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5257 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5258 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5259 | out_free_laundry: |
| 5260 | destroy_workqueue(laundry_wq); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 5261 | out_recovery: |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 5262 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | } |
| 5264 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 5265 | void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5266 | nfs4_state_shutdown_net(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5270 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5271 | |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5272 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 5273 | locks_end_grace(&nn->nfsd4_manager); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 5274 | |
J. Bruce Fields | e50a26d | 2013-10-30 10:33:09 -0400 | [diff] [blame] | 5275 | nfs4_lock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5277 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5278 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 5280 | list_move(&dp->dl_recall_lru, &reaplist); |
| 5281 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5282 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | list_for_each_safe(pos, next, &reaplist) { |
| 5284 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5285 | destroy_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | } |
| 5287 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5288 | nfsd4_client_tracking_exit(net); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5289 | nfs4_state_destroy_net(net); |
J. Bruce Fields | e50a26d | 2013-10-30 10:33:09 -0400 | [diff] [blame] | 5290 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | } |
| 5292 | |
| 5293 | void |
| 5294 | nfs4_state_shutdown(void) |
| 5295 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 5296 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 5297 | nfsd4_destroy_callback_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5298 | } |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5299 | |
| 5300 | static void |
| 5301 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 5302 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 5303 | if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid)) |
| 5304 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5305 | } |
| 5306 | |
| 5307 | static void |
| 5308 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 5309 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 5310 | if (cstate->minorversion) { |
| 5311 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
| 5312 | SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
| 5313 | } |
| 5314 | } |
| 5315 | |
| 5316 | void |
| 5317 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 5318 | { |
| 5319 | CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5320 | } |
| 5321 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5322 | /* |
| 5323 | * functions to set current state id |
| 5324 | */ |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5325 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 5326 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 5327 | { |
| 5328 | put_stateid(cstate, &odp->od_stateid); |
| 5329 | } |
| 5330 | |
| 5331 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5332 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open) |
| 5333 | { |
| 5334 | put_stateid(cstate, &open->op_stateid); |
| 5335 | } |
| 5336 | |
| 5337 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5338 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 5339 | { |
| 5340 | put_stateid(cstate, &close->cl_stateid); |
| 5341 | } |
| 5342 | |
| 5343 | void |
| 5344 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock) |
| 5345 | { |
| 5346 | put_stateid(cstate, &lock->lk_resp_stateid); |
| 5347 | } |
| 5348 | |
| 5349 | /* |
| 5350 | * functions to consume current state id |
| 5351 | */ |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 5352 | |
| 5353 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 5354 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 5355 | { |
| 5356 | get_stateid(cstate, &odp->od_stateid); |
| 5357 | } |
| 5358 | |
| 5359 | void |
| 5360 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp) |
| 5361 | { |
| 5362 | get_stateid(cstate, &drp->dr_stateid); |
| 5363 | } |
| 5364 | |
| 5365 | void |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 5366 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp) |
| 5367 | { |
| 5368 | get_stateid(cstate, &fsp->fr_stateid); |
| 5369 | } |
| 5370 | |
| 5371 | void |
| 5372 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr) |
| 5373 | { |
| 5374 | get_stateid(cstate, &setattr->sa_stateid); |
| 5375 | } |
| 5376 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5377 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5378 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 5379 | { |
| 5380 | get_stateid(cstate, &close->cl_stateid); |
| 5381 | } |
| 5382 | |
| 5383 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5384 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5385 | { |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 5386 | get_stateid(cstate, &locku->lu_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 5387 | } |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 5388 | |
| 5389 | void |
| 5390 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read) |
| 5391 | { |
| 5392 | get_stateid(cstate, &read->rd_stateid); |
| 5393 | } |
| 5394 | |
| 5395 | void |
| 5396 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write) |
| 5397 | { |
| 5398 | get_stateid(cstate, &write->wr_stateid); |
| 5399 | } |