Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (c) 2001 The Regents of the University of Michigan. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Kendrick Smith <kmsmith@umich.edu> |
| 6 | * Andy Adamson <kandros@umich.edu> |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the name of the University nor the names of its |
| 18 | * contributors may be used to endorse or promote products derived |
| 19 | * from this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | */ |
| 34 | |
Dave Hansen | aceaf78 | 2008-02-15 14:37:31 -0800 | [diff] [blame] | 35 | #include <linux/file.h> |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 36 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 38 | #include <linux/namei.h> |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 39 | #include <linux/swap.h> |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 40 | #include <linux/pagemap.h> |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 41 | #include <linux/ratelimit.h> |
Olga Kornievskaia | 68e76ad | 2008-12-23 16:17:15 -0500 | [diff] [blame] | 42 | #include <linux/sunrpc/svcauth_gss.h> |
Jeff Layton | 5976687 | 2013-02-04 12:50:00 -0500 | [diff] [blame] | 43 | #include <linux/sunrpc/addr.h> |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 44 | #include <linux/hash.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 45 | #include "xdr4.h" |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 46 | #include "xdr4cb.h" |
J. Bruce Fields | 0a3adad | 2009-11-04 18:12:35 -0500 | [diff] [blame] | 47 | #include "vfs.h" |
J. Bruce Fields | bfa4b36 | 2012-04-25 16:49:18 -0400 | [diff] [blame] | 48 | #include "current_stateid.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 50 | #include "netns.h" |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 53 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 54 | #define all_ones {{~0,~0},~0} |
| 55 | static const stateid_t one_stateid = { |
| 56 | .si_generation = ~0, |
| 57 | .si_opaque = all_ones, |
| 58 | }; |
| 59 | static const stateid_t zero_stateid = { |
| 60 | /* all fields zero */ |
| 61 | }; |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 62 | static const stateid_t currentstateid = { |
| 63 | .si_generation = 1, |
| 64 | }; |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 65 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 66 | static u64 current_sessionid = 1; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 67 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 68 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
| 69 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 70 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* forward declarations */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 73 | static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 74 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 76 | /* Locking: */ |
| 77 | |
| 78 | /* Currently used for almost all code touching nfsv4 state: */ |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 79 | static DEFINE_MUTEX(client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 81 | /* |
| 82 | * Currently used for the del_recall_lru and file hash table. In an |
| 83 | * effort to decrease the scope of the client_mutex, this spinlock may |
| 84 | * eventually cover more: |
| 85 | */ |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 86 | static DEFINE_SPINLOCK(state_lock); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 87 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 88 | /* |
| 89 | * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for |
| 90 | * the refcount on the open stateid to drop. |
| 91 | */ |
| 92 | static DECLARE_WAIT_QUEUE_HEAD(close_wq); |
| 93 | |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 94 | static struct kmem_cache *openowner_slab; |
| 95 | static struct kmem_cache *lockowner_slab; |
| 96 | static struct kmem_cache *file_slab; |
| 97 | static struct kmem_cache *stateid_slab; |
| 98 | static struct kmem_cache *deleg_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | void |
| 101 | nfs4_lock_state(void) |
| 102 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 103 | mutex_lock(&client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 106 | static void free_session(struct nfsd4_session *); |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 107 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 108 | static bool is_session_dead(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 109 | { |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 110 | return ses->se_flags & NFS4_SESSION_DEAD; |
| 111 | } |
| 112 | |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 113 | static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) |
| 114 | { |
| 115 | if (atomic_read(&ses->se_ref) > ref_held_by_me) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 116 | return nfserr_jukebox; |
| 117 | ses->se_flags |= NFS4_SESSION_DEAD; |
| 118 | return nfs_ok; |
| 119 | } |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | void |
| 122 | nfs4_unlock_state(void) |
| 123 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 124 | mutex_unlock(&client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 127 | static bool is_client_expired(struct nfs4_client *clp) |
| 128 | { |
| 129 | return clp->cl_time == 0; |
| 130 | } |
| 131 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 132 | static __be32 get_client_locked(struct nfs4_client *clp) |
| 133 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 134 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 135 | |
| 136 | lockdep_assert_held(&nn->client_lock); |
| 137 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 138 | if (is_client_expired(clp)) |
| 139 | return nfserr_expired; |
| 140 | atomic_inc(&clp->cl_refcount); |
| 141 | return nfs_ok; |
| 142 | } |
| 143 | |
| 144 | /* must be called under the client_lock */ |
| 145 | static inline void |
| 146 | renew_client_locked(struct nfs4_client *clp) |
| 147 | { |
| 148 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 149 | |
| 150 | if (is_client_expired(clp)) { |
| 151 | WARN_ON(1); |
| 152 | printk("%s: client (clientid %08x/%08x) already expired\n", |
| 153 | __func__, |
| 154 | clp->cl_clientid.cl_boot, |
| 155 | clp->cl_clientid.cl_id); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | dprintk("renewing client (clientid %08x/%08x)\n", |
| 160 | clp->cl_clientid.cl_boot, |
| 161 | clp->cl_clientid.cl_id); |
| 162 | list_move_tail(&clp->cl_lru, &nn->client_lru); |
| 163 | clp->cl_time = get_seconds(); |
| 164 | } |
| 165 | |
| 166 | static inline void |
| 167 | renew_client(struct nfs4_client *clp) |
| 168 | { |
| 169 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 170 | |
| 171 | spin_lock(&nn->client_lock); |
| 172 | renew_client_locked(clp); |
| 173 | spin_unlock(&nn->client_lock); |
| 174 | } |
| 175 | |
Fengguang Wu | ba13843 | 2013-04-16 22:14:15 -0400 | [diff] [blame] | 176 | static void put_client_renew_locked(struct nfs4_client *clp) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 177 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 178 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 179 | |
| 180 | lockdep_assert_held(&nn->client_lock); |
| 181 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 182 | if (!atomic_dec_and_test(&clp->cl_refcount)) |
| 183 | return; |
| 184 | if (!is_client_expired(clp)) |
| 185 | renew_client_locked(clp); |
| 186 | } |
| 187 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 188 | static void put_client_renew(struct nfs4_client *clp) |
| 189 | { |
| 190 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 191 | |
Jeff Layton | d6c249b | 2014-07-08 14:02:50 -0400 | [diff] [blame] | 192 | if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock)) |
| 193 | return; |
| 194 | if (!is_client_expired(clp)) |
| 195 | renew_client_locked(clp); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 196 | spin_unlock(&nn->client_lock); |
| 197 | } |
| 198 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 199 | static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) |
| 200 | { |
| 201 | __be32 status; |
| 202 | |
| 203 | if (is_session_dead(ses)) |
| 204 | return nfserr_badsession; |
| 205 | status = get_client_locked(ses->se_client); |
| 206 | if (status) |
| 207 | return status; |
| 208 | atomic_inc(&ses->se_ref); |
| 209 | return nfs_ok; |
| 210 | } |
| 211 | |
| 212 | static void nfsd4_put_session_locked(struct nfsd4_session *ses) |
| 213 | { |
| 214 | struct nfs4_client *clp = ses->se_client; |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 215 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 216 | |
| 217 | lockdep_assert_held(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 218 | |
| 219 | if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) |
| 220 | free_session(ses); |
| 221 | put_client_renew_locked(clp); |
| 222 | } |
| 223 | |
| 224 | static void nfsd4_put_session(struct nfsd4_session *ses) |
| 225 | { |
| 226 | struct nfs4_client *clp = ses->se_client; |
| 227 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 228 | |
| 229 | spin_lock(&nn->client_lock); |
| 230 | nfsd4_put_session_locked(ses); |
| 231 | spin_unlock(&nn->client_lock); |
| 232 | } |
| 233 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 234 | static int |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 235 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 236 | { |
| 237 | return (sop->so_owner.len == owner->len) && |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 238 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static struct nfs4_openowner * |
| 242 | find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 243 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 244 | { |
| 245 | struct nfs4_stateowner *so; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 246 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 247 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 248 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 249 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], |
| 250 | so_strhash) { |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 251 | if (!so->so_is_open_owner) |
| 252 | continue; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 253 | if (same_owner_str(so, &open->op_owner)) { |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 254 | atomic_inc(&so->so_count); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 255 | return openowner(so); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | static struct nfs4_openowner * |
| 262 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 263 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 264 | { |
| 265 | struct nfs4_openowner *oo; |
| 266 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 267 | spin_lock(&clp->cl_lock); |
| 268 | oo = find_openstateowner_str_locked(hashval, open, clp); |
| 269 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 270 | return oo; |
| 271 | } |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | static inline u32 |
| 274 | opaque_hashval(const void *ptr, int nbytes) |
| 275 | { |
| 276 | unsigned char *cptr = (unsigned char *) ptr; |
| 277 | |
| 278 | u32 x = 0; |
| 279 | while (nbytes--) { |
| 280 | x *= 37; |
| 281 | x += *cptr++; |
| 282 | } |
| 283 | return x; |
| 284 | } |
| 285 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 286 | static void nfsd4_free_file(struct nfs4_file *f) |
| 287 | { |
| 288 | kmem_cache_free(file_slab, f); |
| 289 | } |
| 290 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 291 | static inline void |
| 292 | put_nfs4_file(struct nfs4_file *fi) |
| 293 | { |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 294 | might_lock(&state_lock); |
| 295 | |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 296 | if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) { |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 297 | hlist_del(&fi->fi_hash); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 298 | spin_unlock(&state_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 299 | nfsd4_free_file(fi); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 300 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static inline void |
| 304 | get_nfs4_file(struct nfs4_file *fi) |
| 305 | { |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 306 | atomic_inc(&fi->fi_ref); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 309 | static struct file * |
| 310 | __nfs4_get_fd(struct nfs4_file *f, int oflag) |
| 311 | { |
| 312 | if (f->fi_fds[oflag]) |
| 313 | return get_file(f->fi_fds[oflag]); |
| 314 | return NULL; |
| 315 | } |
| 316 | |
| 317 | static struct file * |
| 318 | find_writeable_file_locked(struct nfs4_file *f) |
| 319 | { |
| 320 | struct file *ret; |
| 321 | |
| 322 | lockdep_assert_held(&f->fi_lock); |
| 323 | |
| 324 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 325 | if (!ret) |
| 326 | ret = __nfs4_get_fd(f, O_RDWR); |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | static struct file * |
| 331 | find_writeable_file(struct nfs4_file *f) |
| 332 | { |
| 333 | struct file *ret; |
| 334 | |
| 335 | spin_lock(&f->fi_lock); |
| 336 | ret = find_writeable_file_locked(f); |
| 337 | spin_unlock(&f->fi_lock); |
| 338 | |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | static struct file *find_readable_file_locked(struct nfs4_file *f) |
| 343 | { |
| 344 | struct file *ret; |
| 345 | |
| 346 | lockdep_assert_held(&f->fi_lock); |
| 347 | |
| 348 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 349 | if (!ret) |
| 350 | ret = __nfs4_get_fd(f, O_RDWR); |
| 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | static struct file * |
| 355 | find_readable_file(struct nfs4_file *f) |
| 356 | { |
| 357 | struct file *ret; |
| 358 | |
| 359 | spin_lock(&f->fi_lock); |
| 360 | ret = find_readable_file_locked(f); |
| 361 | spin_unlock(&f->fi_lock); |
| 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | static struct file * |
| 367 | find_any_file(struct nfs4_file *f) |
| 368 | { |
| 369 | struct file *ret; |
| 370 | |
| 371 | spin_lock(&f->fi_lock); |
| 372 | ret = __nfs4_get_fd(f, O_RDWR); |
| 373 | if (!ret) { |
| 374 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 375 | if (!ret) |
| 376 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 377 | } |
| 378 | spin_unlock(&f->fi_lock); |
| 379 | return ret; |
| 380 | } |
| 381 | |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 382 | static atomic_long_t num_delegations; |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 383 | unsigned long max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | * Open owner state (share locks) |
| 387 | */ |
| 388 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 389 | /* hash tables for lock and open owners */ |
| 390 | #define OWNER_HASH_BITS 8 |
| 391 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) |
| 392 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 393 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 394 | static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 395 | { |
| 396 | unsigned int ret; |
| 397 | |
| 398 | ret = opaque_hashval(ownername->data, ownername->len); |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 399 | return ret & OWNER_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 400 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 401 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 402 | /* hash table for nfs4_file */ |
| 403 | #define FILE_HASH_BITS 8 |
| 404 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
Shan Wei | 3507958 | 2011-01-14 17:35:59 +0800 | [diff] [blame] | 405 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 406 | static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 407 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 408 | return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0); |
| 409 | } |
| 410 | |
| 411 | static unsigned int file_hashval(struct knfsd_fh *fh) |
| 412 | { |
| 413 | return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1); |
| 414 | } |
| 415 | |
| 416 | static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2) |
| 417 | { |
| 418 | return fh1->fh_size == fh2->fh_size && |
| 419 | !memcmp(fh1->fh_base.fh_pad, |
| 420 | fh2->fh_base.fh_pad, |
| 421 | fh1->fh_size); |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 422 | } |
| 423 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 424 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 425 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 426 | static void |
| 427 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 428 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 429 | lockdep_assert_held(&fp->fi_lock); |
| 430 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 431 | if (access & NFS4_SHARE_ACCESS_WRITE) |
| 432 | atomic_inc(&fp->fi_access[O_WRONLY]); |
| 433 | if (access & NFS4_SHARE_ACCESS_READ) |
| 434 | atomic_inc(&fp->fi_access[O_RDONLY]); |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 435 | } |
| 436 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 437 | static __be32 |
| 438 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 439 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 440 | lockdep_assert_held(&fp->fi_lock); |
| 441 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 442 | /* Does this access mode make sense? */ |
| 443 | if (access & ~NFS4_SHARE_ACCESS_BOTH) |
| 444 | return nfserr_inval; |
| 445 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 446 | /* Does it conflict with a deny mode already set? */ |
| 447 | if ((access & fp->fi_share_deny) != 0) |
| 448 | return nfserr_share_denied; |
| 449 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 450 | __nfs4_file_get_access(fp, access); |
| 451 | return nfs_ok; |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 452 | } |
| 453 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 454 | static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) |
| 455 | { |
| 456 | /* Common case is that there is no deny mode. */ |
| 457 | if (deny) { |
| 458 | /* Does this deny mode make sense? */ |
| 459 | if (deny & ~NFS4_SHARE_DENY_BOTH) |
| 460 | return nfserr_inval; |
| 461 | |
| 462 | if ((deny & NFS4_SHARE_DENY_READ) && |
| 463 | atomic_read(&fp->fi_access[O_RDONLY])) |
| 464 | return nfserr_share_denied; |
| 465 | |
| 466 | if ((deny & NFS4_SHARE_DENY_WRITE) && |
| 467 | atomic_read(&fp->fi_access[O_WRONLY])) |
| 468 | return nfserr_share_denied; |
| 469 | } |
| 470 | return nfs_ok; |
| 471 | } |
| 472 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 473 | 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] | 474 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 475 | might_lock(&fp->fi_lock); |
| 476 | |
| 477 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { |
| 478 | struct file *f1 = NULL; |
| 479 | struct file *f2 = NULL; |
| 480 | |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 481 | swap(f1, fp->fi_fds[oflag]); |
J. Bruce Fields | 0c7c3e6 | 2013-03-28 20:37:14 -0400 | [diff] [blame] | 482 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 483 | swap(f2, fp->fi_fds[O_RDWR]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 484 | spin_unlock(&fp->fi_lock); |
| 485 | if (f1) |
| 486 | fput(f1); |
| 487 | if (f2) |
| 488 | fput(f2); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 492 | static void nfs4_file_put_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 493 | { |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 494 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
| 495 | |
| 496 | if (access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 497 | __nfs4_file_put_access(fp, O_WRONLY); |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 498 | if (access & NFS4_SHARE_ACCESS_READ) |
| 499 | __nfs4_file_put_access(fp, O_RDONLY); |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 500 | } |
| 501 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 502 | static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, |
| 503 | struct kmem_cache *slab) |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 504 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 505 | struct nfs4_stid *stid; |
| 506 | int new_id; |
| 507 | |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 508 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 509 | if (!stid) |
| 510 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 511 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 512 | idr_preload(GFP_KERNEL); |
| 513 | spin_lock(&cl->cl_lock); |
| 514 | new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT); |
| 515 | spin_unlock(&cl->cl_lock); |
| 516 | idr_preload_end(); |
Tejun Heo | ebd6c70 | 2013-03-13 14:59:37 -0700 | [diff] [blame] | 517 | if (new_id < 0) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 518 | goto out_free; |
| 519 | stid->sc_client = cl; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 520 | stid->sc_stateid.si_opaque.so_id = new_id; |
| 521 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; |
| 522 | /* Will be incremented before return to client: */ |
Trond Myklebust | 72c0b0f | 2014-07-21 09:34:58 -0400 | [diff] [blame] | 523 | atomic_set(&stid->sc_count, 1); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 524 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 525 | /* |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 526 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 527 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 528 | * example, a stray write retransmission could be accepted by |
| 529 | * the server when it should have been rejected. Therefore, |
| 530 | * adopt a trick from the sctp code to attempt to maximize the |
| 531 | * amount of time until an id is reused, by ensuring they always |
| 532 | * "increase" (mod INT_MAX): |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 533 | */ |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 534 | return stid; |
| 535 | out_free: |
Wei Yongjun | 2c44a23 | 2013-04-09 14:15:31 +0800 | [diff] [blame] | 536 | kmem_cache_free(slab, stid); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 537 | return NULL; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 538 | } |
| 539 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 540 | static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp) |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 541 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 542 | struct nfs4_stid *stid; |
| 543 | struct nfs4_ol_stateid *stp; |
| 544 | |
| 545 | stid = nfs4_alloc_stid(clp, stateid_slab); |
| 546 | if (!stid) |
| 547 | return NULL; |
| 548 | |
| 549 | stp = openlockstateid(stid); |
| 550 | stp->st_stid.sc_free = nfs4_free_ol_stateid; |
| 551 | return stp; |
| 552 | } |
| 553 | |
| 554 | static void nfs4_free_deleg(struct nfs4_stid *stid) |
| 555 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 556 | kmem_cache_free(deleg_slab, stid); |
| 557 | atomic_long_dec(&num_delegations); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 558 | } |
| 559 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 560 | /* |
| 561 | * When we recall a delegation, we should be careful not to hand it |
| 562 | * out again straight away. |
| 563 | * To ensure this we keep a pair of bloom filters ('new' and 'old') |
| 564 | * in which the filehandles of recalled delegations are "stored". |
| 565 | * If a filehandle appear in either filter, a delegation is blocked. |
| 566 | * When a delegation is recalled, the filehandle is stored in the "new" |
| 567 | * filter. |
| 568 | * Every 30 seconds we swap the filters and clear the "new" one, |
| 569 | * unless both are empty of course. |
| 570 | * |
| 571 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the |
| 572 | * low 3 bytes as hash-table indices. |
| 573 | * |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 574 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 575 | * is used to manage concurrent access. Testing does not need the lock |
| 576 | * except when swapping the two filters. |
| 577 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 578 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 579 | static struct bloom_pair { |
| 580 | int entries, old_entries; |
| 581 | time_t swap_time; |
| 582 | int new; /* index into 'set' */ |
| 583 | DECLARE_BITMAP(set[2], 256); |
| 584 | } blocked_delegations; |
| 585 | |
| 586 | static int delegation_blocked(struct knfsd_fh *fh) |
| 587 | { |
| 588 | u32 hash; |
| 589 | struct bloom_pair *bd = &blocked_delegations; |
| 590 | |
| 591 | if (bd->entries == 0) |
| 592 | return 0; |
| 593 | if (seconds_since_boot() - bd->swap_time > 30) { |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 594 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 595 | if (seconds_since_boot() - bd->swap_time > 30) { |
| 596 | bd->entries -= bd->old_entries; |
| 597 | bd->old_entries = bd->entries; |
| 598 | memset(bd->set[bd->new], 0, |
| 599 | sizeof(bd->set[0])); |
| 600 | bd->new = 1-bd->new; |
| 601 | bd->swap_time = seconds_since_boot(); |
| 602 | } |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 603 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 604 | } |
| 605 | hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0); |
| 606 | if (test_bit(hash&255, bd->set[0]) && |
| 607 | test_bit((hash>>8)&255, bd->set[0]) && |
| 608 | test_bit((hash>>16)&255, bd->set[0])) |
| 609 | return 1; |
| 610 | |
| 611 | if (test_bit(hash&255, bd->set[1]) && |
| 612 | test_bit((hash>>8)&255, bd->set[1]) && |
| 613 | test_bit((hash>>16)&255, bd->set[1])) |
| 614 | return 1; |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static void block_delegations(struct knfsd_fh *fh) |
| 620 | { |
| 621 | u32 hash; |
| 622 | struct bloom_pair *bd = &blocked_delegations; |
| 623 | |
| 624 | hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0); |
| 625 | |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 626 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 627 | __set_bit(hash&255, bd->set[bd->new]); |
| 628 | __set_bit((hash>>8)&255, bd->set[bd->new]); |
| 629 | __set_bit((hash>>16)&255, bd->set[bd->new]); |
| 630 | if (bd->entries == 0) |
| 631 | bd->swap_time = seconds_since_boot(); |
| 632 | bd->entries += 1; |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 633 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 634 | } |
| 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | static struct nfs4_delegation * |
Jeff Layton | f9416e2 | 2014-07-25 07:34:23 -0400 | [diff] [blame] | 637 | alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | { |
| 639 | struct nfs4_delegation *dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 640 | long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
| 642 | dprintk("NFSD alloc_init_deleg\n"); |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 643 | n = atomic_long_inc_return(&num_delegations); |
| 644 | if (n < 0 || n > max_delegations) |
| 645 | goto out_dec; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 646 | if (delegation_blocked(¤t_fh->fh_handle)) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 647 | goto out_dec; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 648 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab)); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 649 | if (dp == NULL) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 650 | goto out_dec; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 651 | |
| 652 | dp->dl_stid.sc_free = nfs4_free_deleg; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 653 | /* |
| 654 | * delegation seqid's are never incremented. The 4.1 special |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 655 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 656 | * 0 anyway just for consistency and use 1: |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 657 | */ |
| 658 | dp->dl_stid.sc_stateid.si_generation = 1; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 659 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 660 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 662 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 663 | INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | return dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 665 | out_dec: |
| 666 | atomic_long_dec(&num_delegations); |
| 667 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | void |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 671 | nfs4_put_stid(struct nfs4_stid *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 673 | struct nfs4_file *fp = s->sc_file; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 674 | struct nfs4_client *clp = s->sc_client; |
| 675 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 676 | might_lock(&clp->cl_lock); |
| 677 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 678 | if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) { |
| 679 | wake_up_all(&close_wq); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 680 | return; |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 681 | } |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 682 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 683 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 684 | s->sc_free(s); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 685 | if (fp) |
| 686 | put_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } |
| 688 | |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 689 | static void nfs4_put_deleg_lease(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 691 | lockdep_assert_held(&state_lock); |
| 692 | |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 693 | if (!fp->fi_lease) |
| 694 | return; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 695 | if (atomic_dec_and_test(&fp->fi_delegees)) { |
| 696 | vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease); |
| 697 | fp->fi_lease = NULL; |
J. Bruce Fields | 4ee6362 | 2011-04-15 18:08:26 -0400 | [diff] [blame] | 698 | fput(fp->fi_deleg_file); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 699 | fp->fi_deleg_file = NULL; |
| 700 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | } |
| 702 | |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 703 | static void unhash_stid(struct nfs4_stid *s) |
| 704 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 705 | s->sc_type = 0; |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 708 | static void |
| 709 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 710 | { |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 711 | lockdep_assert_held(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 712 | lockdep_assert_held(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 713 | |
Trond Myklebust | 67cb127 | 2014-07-29 21:34:17 -0400 | [diff] [blame] | 714 | atomic_inc(&dp->dl_stid.sc_count); |
Benny Halevy | 3fb87d1 | 2014-05-30 09:09:31 -0400 | [diff] [blame] | 715 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 716 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
| 717 | list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations); |
| 718 | } |
| 719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | static void |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 721 | unhash_delegation_locked(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 723 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 724 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 725 | lockdep_assert_held(&state_lock); |
| 726 | |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 727 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
Jeff Layton | d55a166 | 2014-07-22 13:52:06 -0400 | [diff] [blame] | 728 | /* Ensure that deleg break won't try to requeue it */ |
| 729 | ++dp->dl_time; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 730 | spin_lock(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 731 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | list_del_init(&dp->dl_recall_lru); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 733 | list_del_init(&dp->dl_perfile); |
| 734 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 735 | if (fp) |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 736 | nfs4_put_deleg_lease(fp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 737 | } |
| 738 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 739 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 740 | { |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 741 | spin_lock(&state_lock); |
| 742 | unhash_delegation_locked(dp); |
| 743 | spin_unlock(&state_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 744 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 748 | { |
| 749 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 750 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 751 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
| 752 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 753 | if (clp->cl_minorversion == 0) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 754 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 755 | else { |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 756 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 757 | spin_lock(&clp->cl_lock); |
| 758 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 759 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /* |
| 764 | * SETCLIENTID state |
| 765 | */ |
| 766 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 767 | static unsigned int clientid_hashval(u32 id) |
| 768 | { |
| 769 | return id & CLIENT_HASH_MASK; |
| 770 | } |
| 771 | |
| 772 | static unsigned int clientstr_hashval(const char *name) |
| 773 | { |
| 774 | return opaque_hashval(name, 8) & CLIENT_HASH_MASK; |
| 775 | } |
| 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | /* |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 778 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 779 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 780 | * only what share bits are currently in force, but also what |
| 781 | * combinations of share bits previous opens have used. This allows us |
| 782 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 783 | * return an error if the client attempt to downgrade to a combination |
| 784 | * of share bits not explicable by closing some of its previous opens. |
| 785 | * |
| 786 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 787 | * track of access/deny bit combinations; so, e.g., we allow: |
| 788 | * |
| 789 | * OPEN allow read, deny write |
| 790 | * OPEN allow both, deny none |
| 791 | * DOWNGRADE allow read, deny none |
| 792 | * |
| 793 | * which we should reject. |
| 794 | */ |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 795 | static unsigned int |
| 796 | bmap_to_share_mode(unsigned long bmap) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 797 | int i; |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 798 | unsigned int access = 0; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 799 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 800 | for (i = 1; i < 4; i++) { |
| 801 | if (test_bit(i, &bmap)) |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 802 | access |= i; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 803 | } |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 804 | return access; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 805 | } |
| 806 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 807 | /* set share access for a given stateid */ |
| 808 | static inline void |
| 809 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 810 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 811 | unsigned char mask = 1 << access; |
| 812 | |
| 813 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 814 | stp->st_access_bmap |= mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /* clear share access for a given stateid */ |
| 818 | static inline void |
| 819 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 820 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 821 | unsigned char mask = 1 << access; |
| 822 | |
| 823 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 824 | stp->st_access_bmap &= ~mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | /* test whether a given stateid has access */ |
| 828 | static inline bool |
| 829 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 830 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 831 | unsigned char mask = 1 << access; |
| 832 | |
| 833 | return (bool)(stp->st_access_bmap & mask); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 834 | } |
| 835 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 836 | /* set share deny for a given stateid */ |
| 837 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 838 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 839 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 840 | unsigned char mask = 1 << deny; |
| 841 | |
| 842 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 843 | stp->st_deny_bmap |= mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /* clear share deny for a given stateid */ |
| 847 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 848 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 849 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 850 | unsigned char mask = 1 << deny; |
| 851 | |
| 852 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 853 | stp->st_deny_bmap &= ~mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /* test whether a given stateid is denying specific access */ |
| 857 | static inline bool |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 858 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 859 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 860 | unsigned char mask = 1 << deny; |
| 861 | |
| 862 | return (bool)(stp->st_deny_bmap & mask); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static int nfs4_access_to_omode(u32 access) |
| 866 | { |
J. Bruce Fields | 8f34a43 | 2010-09-02 15:23:16 -0400 | [diff] [blame] | 867 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 868 | case NFS4_SHARE_ACCESS_READ: |
| 869 | return O_RDONLY; |
| 870 | case NFS4_SHARE_ACCESS_WRITE: |
| 871 | return O_WRONLY; |
| 872 | case NFS4_SHARE_ACCESS_BOTH: |
| 873 | return O_RDWR; |
| 874 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 875 | WARN_ON_ONCE(1); |
| 876 | return O_RDONLY; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 877 | } |
| 878 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 879 | /* |
| 880 | * A stateid that had a deny mode associated with it is being released |
| 881 | * or downgraded. Recalculate the deny mode on the file. |
| 882 | */ |
| 883 | static void |
| 884 | recalculate_deny_mode(struct nfs4_file *fp) |
| 885 | { |
| 886 | struct nfs4_ol_stateid *stp; |
| 887 | |
| 888 | spin_lock(&fp->fi_lock); |
| 889 | fp->fi_share_deny = 0; |
| 890 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) |
| 891 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); |
| 892 | spin_unlock(&fp->fi_lock); |
| 893 | } |
| 894 | |
| 895 | static void |
| 896 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 897 | { |
| 898 | int i; |
| 899 | bool change = false; |
| 900 | |
| 901 | for (i = 1; i < 4; i++) { |
| 902 | if ((i & deny) != i) { |
| 903 | change = true; |
| 904 | clear_deny(i, stp); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | /* Recalculate per-file deny mode if there was a change */ |
| 909 | if (change) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 910 | recalculate_deny_mode(stp->st_stid.sc_file); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 911 | } |
| 912 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 913 | /* release all access and file references for a given stateid */ |
| 914 | static void |
| 915 | release_all_access(struct nfs4_ol_stateid *stp) |
| 916 | { |
| 917 | int i; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 918 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 919 | |
| 920 | if (fp && stp->st_deny_bmap != 0) |
| 921 | recalculate_deny_mode(fp); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 922 | |
| 923 | for (i = 1; i < 4; i++) { |
| 924 | if (test_access(i, stp)) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 925 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 926 | clear_access(i, stp); |
| 927 | } |
| 928 | } |
| 929 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 930 | static void nfs4_put_stateowner(struct nfs4_stateowner *sop) |
| 931 | { |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 932 | struct nfs4_client *clp = sop->so_client; |
| 933 | |
| 934 | might_lock(&clp->cl_lock); |
| 935 | |
| 936 | if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 937 | return; |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 938 | sop->so_ops->so_unhash(sop); |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 939 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 940 | kfree(sop->so_owner.data); |
| 941 | sop->so_ops->so_free(sop); |
| 942 | } |
| 943 | |
Jeff Layton | 4ae098d | 2014-07-29 21:34:43 -0400 | [diff] [blame] | 944 | static void unhash_ol_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 945 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 946 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 947 | |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 948 | lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); |
| 949 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 950 | spin_lock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 951 | list_del(&stp->st_perfile); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 952 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 953 | list_del(&stp->st_perstateowner); |
| 954 | } |
| 955 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 956 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 957 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 958 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 959 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 960 | release_all_access(stp); |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 961 | if (stp->st_stateowner) |
| 962 | nfs4_put_stateowner(stp->st_stateowner); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 963 | kmem_cache_free(stateid_slab, stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 964 | } |
| 965 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 966 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 967 | { |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 968 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
| 969 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 970 | struct file *file; |
| 971 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 972 | file = find_any_file(stp->st_stid.sc_file); |
| 973 | if (file) |
| 974 | filp_close(file, (fl_owner_t)lo); |
| 975 | nfs4_free_ol_stateid(stid); |
| 976 | } |
| 977 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 978 | /* |
| 979 | * Put the persistent reference to an already unhashed generic stateid, while |
| 980 | * holding the cl_lock. If it's the last reference, then put it onto the |
| 981 | * reaplist for later destruction. |
| 982 | */ |
| 983 | static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, |
| 984 | struct list_head *reaplist) |
| 985 | { |
| 986 | struct nfs4_stid *s = &stp->st_stid; |
| 987 | struct nfs4_client *clp = s->sc_client; |
| 988 | |
| 989 | lockdep_assert_held(&clp->cl_lock); |
| 990 | |
| 991 | WARN_ON_ONCE(!list_empty(&stp->st_locks)); |
| 992 | |
| 993 | if (!atomic_dec_and_test(&s->sc_count)) { |
| 994 | wake_up_all(&close_wq); |
| 995 | return; |
| 996 | } |
| 997 | |
| 998 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
| 999 | list_add(&stp->st_locks, reaplist); |
| 1000 | } |
| 1001 | |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1002 | static void unhash_lock_stateid(struct nfs4_ol_stateid *stp) |
| 1003 | { |
| 1004 | struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner); |
| 1005 | |
| 1006 | lockdep_assert_held(&oo->oo_owner.so_client->cl_lock); |
| 1007 | |
| 1008 | list_del_init(&stp->st_locks); |
Jeff Layton | 4ae098d | 2014-07-29 21:34:43 -0400 | [diff] [blame] | 1009 | unhash_ol_stateid(stp); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1010 | unhash_stid(&stp->st_stid); |
| 1011 | } |
| 1012 | |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 1013 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1014 | { |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1015 | struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner); |
| 1016 | |
| 1017 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1018 | unhash_lock_stateid(stp); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1019 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1020 | nfs4_put_stid(&stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1021 | } |
| 1022 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1023 | static void unhash_lockowner_locked(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1024 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1025 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1026 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1027 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1028 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1029 | list_del_init(&lo->lo_owner.so_strhash); |
| 1030 | } |
| 1031 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1032 | /* |
| 1033 | * Free a list of generic stateids that were collected earlier after being |
| 1034 | * fully unhashed. |
| 1035 | */ |
| 1036 | static void |
| 1037 | free_ol_stateid_reaplist(struct list_head *reaplist) |
| 1038 | { |
| 1039 | struct nfs4_ol_stateid *stp; |
| 1040 | |
| 1041 | might_sleep(); |
| 1042 | |
| 1043 | while (!list_empty(reaplist)) { |
| 1044 | stp = list_first_entry(reaplist, struct nfs4_ol_stateid, |
| 1045 | st_locks); |
| 1046 | list_del(&stp->st_locks); |
| 1047 | stp->st_stid.sc_free(&stp->st_stid); |
| 1048 | } |
| 1049 | } |
| 1050 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1051 | static void release_lockowner(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1052 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1053 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1054 | struct nfs4_ol_stateid *stp; |
| 1055 | struct list_head reaplist; |
| 1056 | |
| 1057 | INIT_LIST_HEAD(&reaplist); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1058 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1059 | spin_lock(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1060 | unhash_lockowner_locked(lo); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1061 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 1062 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
| 1063 | struct nfs4_ol_stateid, st_perstateowner); |
| 1064 | unhash_lock_stateid(stp); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1065 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1066 | } |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1067 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1068 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1069 | nfs4_put_stateowner(&lo->lo_owner); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1070 | } |
| 1071 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1072 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, |
| 1073 | struct list_head *reaplist) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1074 | { |
| 1075 | struct nfs4_ol_stateid *stp; |
| 1076 | |
| 1077 | while (!list_empty(&open_stp->st_locks)) { |
| 1078 | stp = list_entry(open_stp->st_locks.next, |
| 1079 | struct nfs4_ol_stateid, st_locks); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1080 | unhash_lock_stateid(stp); |
| 1081 | put_ol_stateid_locked(stp, reaplist); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1085 | static void unhash_open_stateid(struct nfs4_ol_stateid *stp, |
| 1086 | struct list_head *reaplist) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1087 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1088 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
| 1089 | |
Jeff Layton | 4ae098d | 2014-07-29 21:34:43 -0400 | [diff] [blame] | 1090 | unhash_ol_stateid(stp); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1091 | release_open_stateid_locks(stp, reaplist); |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 1095 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1096 | LIST_HEAD(reaplist); |
| 1097 | |
| 1098 | spin_lock(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1099 | unhash_open_stateid(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1100 | put_ol_stateid_locked(stp, &reaplist); |
| 1101 | spin_unlock(&stp->st_stid.sc_client->cl_lock); |
| 1102 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1103 | } |
| 1104 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1105 | static void unhash_openowner_locked(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1106 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1107 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1108 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1109 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1110 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1111 | list_del_init(&oo->oo_owner.so_strhash); |
| 1112 | list_del_init(&oo->oo_perclient); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1113 | } |
| 1114 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1115 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 1116 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1117 | struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, |
| 1118 | nfsd_net_id); |
| 1119 | struct nfs4_ol_stateid *s; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1120 | |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1121 | spin_lock(&nn->client_lock); |
| 1122 | s = oo->oo_last_closed_stid; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1123 | if (s) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1124 | list_del_init(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1125 | oo->oo_last_closed_stid = NULL; |
| 1126 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1127 | spin_unlock(&nn->client_lock); |
| 1128 | if (s) |
| 1129 | nfs4_put_stid(&s->st_stid); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1130 | } |
| 1131 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1132 | static void release_openowner(struct nfs4_openowner *oo) |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1133 | { |
| 1134 | struct nfs4_ol_stateid *stp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1135 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1136 | struct list_head reaplist; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1137 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1138 | INIT_LIST_HEAD(&reaplist); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1139 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1140 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1141 | unhash_openowner_locked(oo); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1142 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 1143 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
| 1144 | struct nfs4_ol_stateid, st_perstateowner); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1145 | unhash_open_stateid(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1146 | put_ol_stateid_locked(stp, &reaplist); |
| 1147 | } |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1148 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1149 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1150 | release_last_closed_stateid(oo); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1151 | nfs4_put_stateowner(&oo->oo_owner); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1152 | } |
| 1153 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1154 | static inline int |
| 1155 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 1156 | { |
| 1157 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 1158 | |
| 1159 | return sid->sequence % SESSION_HASH_SIZE; |
| 1160 | } |
| 1161 | |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1162 | #ifdef NFSD_DEBUG |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1163 | static inline void |
| 1164 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1165 | { |
| 1166 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 1167 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 1168 | } |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1169 | #else |
| 1170 | static inline void |
| 1171 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1172 | { |
| 1173 | } |
| 1174 | #endif |
| 1175 | |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1176 | /* |
| 1177 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 1178 | * won't be used for replay. |
| 1179 | */ |
| 1180 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 1181 | { |
| 1182 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 1183 | |
| 1184 | if (nfserr == nfserr_replay_me) |
| 1185 | return; |
| 1186 | |
| 1187 | if (!seqid_mutating_err(ntohl(nfserr))) { |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 1188 | nfsd4_cstate_clear_replay(cstate); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1189 | return; |
| 1190 | } |
| 1191 | if (!so) |
| 1192 | return; |
| 1193 | if (so->so_is_open_owner) |
| 1194 | release_last_closed_stateid(openowner(so)); |
| 1195 | so->so_seqid++; |
| 1196 | return; |
| 1197 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1198 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1199 | static void |
| 1200 | gen_sessionid(struct nfsd4_session *ses) |
| 1201 | { |
| 1202 | struct nfs4_client *clp = ses->se_client; |
| 1203 | struct nfsd4_sessionid *sid; |
| 1204 | |
| 1205 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 1206 | sid->clientid = clp->cl_clientid; |
| 1207 | sid->sequence = current_sessionid++; |
| 1208 | sid->reserved = 0; |
| 1209 | } |
| 1210 | |
| 1211 | /* |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1212 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 1213 | * the rpc header, but all we need to cache is the data starting after |
| 1214 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 1215 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 1216 | * value that is the number of bytes in our cache plus a few additional |
| 1217 | * bytes. In order to stay on the safe side, and not promise more than |
| 1218 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 1219 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 1220 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 1221 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1222 | */ |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1223 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 1224 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1225 | static void |
| 1226 | free_session_slots(struct nfsd4_session *ses) |
| 1227 | { |
| 1228 | int i; |
| 1229 | |
| 1230 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) |
| 1231 | kfree(ses->se_slots[i]); |
| 1232 | } |
| 1233 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1234 | /* |
| 1235 | * We don't actually need to cache the rpc and session headers, so we |
| 1236 | * can allocate a little less for each slot: |
| 1237 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1238 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1239 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1240 | u32 size; |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1241 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1242 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 1243 | size = 0; |
| 1244 | else |
| 1245 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 1246 | return size + sizeof(struct nfsd4_slot); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1247 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1248 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1249 | /* |
| 1250 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 1251 | * re-negotiate active sessions and reduce their slot usage to make |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 1252 | * 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] | 1253 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1254 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1255 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1256 | u32 slotsize = slot_bytes(ca); |
| 1257 | u32 num = ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1258 | int avail; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1259 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1260 | spin_lock(&nfsd_drc_lock); |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 1261 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, |
| 1262 | nfsd_drc_max_mem - nfsd_drc_mem_used); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1263 | num = min_t(int, num, avail / slotsize); |
| 1264 | nfsd_drc_mem_used += num * slotsize; |
| 1265 | spin_unlock(&nfsd_drc_lock); |
| 1266 | |
| 1267 | return num; |
| 1268 | } |
| 1269 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1270 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1271 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1272 | int slotsize = slot_bytes(ca); |
| 1273 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1274 | spin_lock(&nfsd_drc_lock); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1275 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1276 | spin_unlock(&nfsd_drc_lock); |
| 1277 | } |
| 1278 | |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1279 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 1280 | struct nfsd4_channel_attrs *battrs) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1281 | { |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1282 | int numslots = fattrs->maxreqs; |
| 1283 | int slotsize = slot_bytes(fattrs); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1284 | struct nfsd4_session *new; |
| 1285 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1286 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 1287 | 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] | 1288 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 1289 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1290 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1291 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1292 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1293 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1294 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1295 | for (i = 0; i < numslots; i++) { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1296 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1297 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1298 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1299 | } |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1300 | |
| 1301 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 1302 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 1303 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1304 | return new; |
| 1305 | out_free: |
| 1306 | while (i--) |
| 1307 | kfree(new->se_slots[i]); |
| 1308 | kfree(new); |
| 1309 | return NULL; |
| 1310 | } |
| 1311 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1312 | static void free_conn(struct nfsd4_conn *c) |
| 1313 | { |
| 1314 | svc_xprt_put(c->cn_xprt); |
| 1315 | kfree(c); |
| 1316 | } |
| 1317 | |
| 1318 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 1319 | { |
| 1320 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 1321 | struct nfs4_client *clp = c->cn_session->se_client; |
| 1322 | |
| 1323 | spin_lock(&clp->cl_lock); |
| 1324 | if (!list_empty(&c->cn_persession)) { |
| 1325 | list_del(&c->cn_persession); |
| 1326 | free_conn(c); |
| 1327 | } |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 1328 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 2e4b723 | 2013-03-08 09:30:43 -0500 | [diff] [blame] | 1329 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1330 | } |
| 1331 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1332 | 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] | 1333 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1334 | struct nfsd4_conn *conn; |
| 1335 | |
| 1336 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 1337 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1338 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1339 | svc_xprt_get(rqstp->rq_xprt); |
| 1340 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1341 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1342 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1343 | return conn; |
| 1344 | } |
| 1345 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1346 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1347 | { |
| 1348 | conn->cn_session = ses; |
| 1349 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1350 | } |
| 1351 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1352 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1353 | { |
| 1354 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1355 | |
| 1356 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1357 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1358 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1359 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1360 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1361 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1362 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1363 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1364 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1365 | } |
| 1366 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1367 | 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] | 1368 | { |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1369 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1370 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1371 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1372 | ret = nfsd4_register_conn(conn); |
| 1373 | if (ret) |
| 1374 | /* oops; xprt is already down: */ |
| 1375 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | 57a3714 | 2014-07-18 15:06:47 -0400 | [diff] [blame] | 1376 | /* We may have gained or lost a callback channel: */ |
| 1377 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1378 | } |
| 1379 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1380 | 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] | 1381 | { |
| 1382 | u32 dir = NFS4_CDFC4_FORE; |
| 1383 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1384 | if (cses->flags & SESSION4_BACK_CHAN) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1385 | dir |= NFS4_CDFC4_BACK; |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1386 | return alloc_conn(rqstp, dir); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1390 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1391 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1392 | struct nfs4_client *clp = s->se_client; |
| 1393 | struct nfsd4_conn *c; |
| 1394 | |
| 1395 | spin_lock(&clp->cl_lock); |
| 1396 | while (!list_empty(&s->se_conns)) { |
| 1397 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1398 | list_del_init(&c->cn_persession); |
| 1399 | spin_unlock(&clp->cl_lock); |
| 1400 | |
| 1401 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1402 | free_conn(c); |
| 1403 | |
| 1404 | spin_lock(&clp->cl_lock); |
| 1405 | } |
| 1406 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1407 | } |
| 1408 | |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1409 | static void __free_session(struct nfsd4_session *ses) |
| 1410 | { |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1411 | free_session_slots(ses); |
| 1412 | kfree(ses); |
| 1413 | } |
| 1414 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1415 | static void free_session(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 1416 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1417 | nfsd4_del_conns(ses); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1418 | nfsd4_put_drc_mem(&ses->se_fchannel); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1419 | __free_session(ses); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1420 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1421 | |
Fengguang Wu | 135ae82 | 2012-11-10 07:20:25 -0500 | [diff] [blame] | 1422 | 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] | 1423 | { |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1424 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1425 | 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] | 1426 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1427 | new->se_client = clp; |
| 1428 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1429 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1430 | INIT_LIST_HEAD(&new->se_conns); |
| 1431 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1432 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1433 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 1434 | new->se_cb_prog = cses->callback_prog; |
J. Bruce Fields | c6bb3ca | 2012-11-01 16:31:02 -0400 | [diff] [blame] | 1435 | new->se_cb_sec = cses->cb_sec; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1436 | atomic_set(&new->se_ref, 0); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1437 | idx = hash_sessionid(&new->se_sessionid); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1438 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1439 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1440 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1441 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1442 | |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1443 | if (cses->flags & SESSION4_BACK_CHAN) { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1444 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1445 | /* |
| 1446 | * This is a little silly; with sessions there's no real |
| 1447 | * use for the callback address. Use the peer address |
| 1448 | * as a reasonable default for now, but consider fixing |
| 1449 | * the rpc client not to require an address in the |
| 1450 | * future: |
| 1451 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1452 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1453 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1454 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1455 | } |
| 1456 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1457 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1458 | static struct nfsd4_session * |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1459 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1460 | { |
| 1461 | struct nfsd4_session *elem; |
| 1462 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1463 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1464 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1465 | lockdep_assert_held(&nn->client_lock); |
| 1466 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1467 | dump_sessionid(__func__, sessionid); |
| 1468 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1469 | /* Search in the appropriate list */ |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1470 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1471 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1472 | NFS4_MAX_SESSIONID_LEN)) { |
| 1473 | return elem; |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | dprintk("%s: session not found\n", __func__); |
| 1478 | return NULL; |
| 1479 | } |
| 1480 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1481 | static struct nfsd4_session * |
| 1482 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, |
| 1483 | __be32 *ret) |
| 1484 | { |
| 1485 | struct nfsd4_session *session; |
| 1486 | __be32 status = nfserr_badsession; |
| 1487 | |
| 1488 | session = __find_in_sessionid_hashtbl(sessionid, net); |
| 1489 | if (!session) |
| 1490 | goto out; |
| 1491 | status = nfsd4_get_session_locked(session); |
| 1492 | if (status) |
| 1493 | session = NULL; |
| 1494 | out: |
| 1495 | *ret = status; |
| 1496 | return session; |
| 1497 | } |
| 1498 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1499 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1500 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1501 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1502 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1503 | struct nfs4_client *clp = ses->se_client; |
| 1504 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1505 | |
| 1506 | lockdep_assert_held(&nn->client_lock); |
| 1507 | |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1508 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1509 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1510 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1511 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1512 | } |
| 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1515 | static int |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1516 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | { |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1518 | if (clid->cl_boot == nn->boot_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | return 0; |
Andy Adamson | 60adfc5 | 2009-04-03 08:28:50 +0300 | [diff] [blame] | 1520 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1521 | clid->cl_boot, clid->cl_id, nn->boot_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | return 1; |
| 1523 | } |
| 1524 | |
| 1525 | /* |
| 1526 | * XXX Should we use a slab cache ? |
| 1527 | * This type of memory management is somewhat inefficient, but we use it |
| 1528 | * anyway since SETCLIENTID is not a common operation. |
| 1529 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1530 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | { |
| 1532 | struct nfs4_client *clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1533 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1535 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
| 1536 | if (clp == NULL) |
| 1537 | return NULL; |
Thomas Meyer | 67114fe | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 1538 | clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1539 | if (clp->cl_name.data == NULL) |
| 1540 | goto err_no_name; |
| 1541 | clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) * |
| 1542 | OWNER_HASH_SIZE, GFP_KERNEL); |
| 1543 | if (!clp->cl_ownerstr_hashtbl) |
| 1544 | goto err_no_hashtbl; |
| 1545 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 1546 | INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1547 | clp->cl_name.len = name.len; |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1548 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1549 | idr_init(&clp->cl_stateids); |
| 1550 | atomic_set(&clp->cl_refcount, 0); |
| 1551 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 1552 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1553 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1554 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 1555 | INIT_LIST_HEAD(&clp->cl_lru); |
| 1556 | INIT_LIST_HEAD(&clp->cl_callbacks); |
| 1557 | INIT_LIST_HEAD(&clp->cl_revoked); |
| 1558 | spin_lock_init(&clp->cl_lock); |
| 1559 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | return clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1561 | err_no_hashtbl: |
| 1562 | kfree(clp->cl_name.data); |
| 1563 | err_no_name: |
| 1564 | kfree(clp); |
| 1565 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | } |
| 1567 | |
Trond Myklebust | 4dd86e15 | 2014-04-18 14:43:58 -0400 | [diff] [blame] | 1568 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | free_client(struct nfs4_client *clp) |
| 1570 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1571 | while (!list_empty(&clp->cl_sessions)) { |
| 1572 | struct nfsd4_session *ses; |
| 1573 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 1574 | se_perclnt); |
| 1575 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1576 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 1577 | free_session(ses); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1578 | } |
Trond Myklebust | 4cb57e3 | 2014-04-18 14:43:57 -0400 | [diff] [blame] | 1579 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1580 | free_svc_cred(&clp->cl_cred); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1581 | kfree(clp->cl_ownerstr_hashtbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | kfree(clp->cl_name.data); |
majianpeng | 2d32b29 | 2013-01-29 13:16:06 +0800 | [diff] [blame] | 1583 | idr_destroy(&clp->cl_stateids); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | kfree(clp); |
| 1585 | } |
| 1586 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1587 | /* must be called under the client_lock */ |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1588 | static void |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1589 | unhash_client_locked(struct nfs4_client *clp) |
| 1590 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1591 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1592 | struct nfsd4_session *ses; |
| 1593 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1594 | lockdep_assert_held(&nn->client_lock); |
| 1595 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1596 | /* Mark the client as expired! */ |
| 1597 | clp->cl_time = 0; |
| 1598 | /* Make it invisible */ |
| 1599 | if (!list_empty(&clp->cl_idhash)) { |
| 1600 | list_del_init(&clp->cl_idhash); |
| 1601 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
| 1602 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
| 1603 | else |
| 1604 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
| 1605 | } |
| 1606 | list_del_init(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1607 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1608 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 1609 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1610 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1611 | } |
| 1612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | static void |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1614 | unhash_client(struct nfs4_client *clp) |
| 1615 | { |
| 1616 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1617 | |
| 1618 | spin_lock(&nn->client_lock); |
| 1619 | unhash_client_locked(clp); |
| 1620 | spin_unlock(&nn->client_lock); |
| 1621 | } |
| 1622 | |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame^] | 1623 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
| 1624 | { |
| 1625 | if (atomic_read(&clp->cl_refcount)) |
| 1626 | return nfserr_jukebox; |
| 1627 | unhash_client_locked(clp); |
| 1628 | return nfs_ok; |
| 1629 | } |
| 1630 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1631 | static void |
| 1632 | __destroy_client(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1634 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | struct list_head reaplist; |
| 1637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1639 | spin_lock(&state_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1640 | while (!list_empty(&clp->cl_delegations)) { |
| 1641 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1642 | unhash_delegation_locked(dp); |
| 1643 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1645 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | while (!list_empty(&reaplist)) { |
| 1647 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1648 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1649 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | } |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1651 | while (!list_empty(&clp->cl_revoked)) { |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 1652 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1653 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1654 | nfs4_put_stid(&dp->dl_stid); |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 1655 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1656 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1657 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1658 | atomic_inc(&oo->oo_owner.so_count); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1659 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | } |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 1661 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 1662 | if (clp->cl_cb_conn.cb_xprt) |
| 1663 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 1664 | free_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1667 | static void |
| 1668 | destroy_client(struct nfs4_client *clp) |
| 1669 | { |
| 1670 | unhash_client(clp); |
| 1671 | __destroy_client(clp); |
| 1672 | } |
| 1673 | |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1674 | static void expire_client(struct nfs4_client *clp) |
| 1675 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1676 | unhash_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1677 | nfsd4_client_record_remove(clp); |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1678 | __destroy_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1679 | } |
| 1680 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1681 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 1682 | { |
| 1683 | memcpy(target->cl_verifier.data, source->data, |
| 1684 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1687 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 1688 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 1690 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 1691 | } |
| 1692 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1693 | 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] | 1694 | { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1695 | if (source->cr_principal) { |
| 1696 | target->cr_principal = |
| 1697 | kstrdup(source->cr_principal, GFP_KERNEL); |
| 1698 | if (target->cr_principal == NULL) |
| 1699 | return -ENOMEM; |
| 1700 | } else |
| 1701 | target->cr_principal = NULL; |
J. Bruce Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 1702 | target->cr_flavor = source->cr_flavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | target->cr_uid = source->cr_uid; |
| 1704 | target->cr_gid = source->cr_gid; |
| 1705 | target->cr_group_info = source->cr_group_info; |
| 1706 | get_group_info(target->cr_group_info); |
J. Bruce Fields | 0dc1531 | 2013-05-14 16:07:13 -0400 | [diff] [blame] | 1707 | target->cr_gss_mech = source->cr_gss_mech; |
| 1708 | if (source->cr_gss_mech) |
| 1709 | gss_mech_get(source->cr_gss_mech); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1710 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1713 | static long long |
| 1714 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 1715 | { |
| 1716 | long long res; |
| 1717 | |
| 1718 | res = o1->len - o2->len; |
| 1719 | if (res) |
| 1720 | return res; |
| 1721 | return (long long)memcmp(o1->data, o2->data, o1->len); |
| 1722 | } |
| 1723 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1724 | static int same_name(const char *n1, const char *n2) |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1725 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1726 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1730 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 1731 | { |
| 1732 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1736 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 1737 | { |
| 1738 | 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] | 1739 | } |
| 1740 | |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1741 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 1742 | { |
| 1743 | int i; |
| 1744 | |
| 1745 | if (g1->ngroups != g2->ngroups) |
| 1746 | return false; |
| 1747 | for (i=0; i<g1->ngroups; i++) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1748 | if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i))) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1749 | return false; |
| 1750 | return true; |
| 1751 | } |
| 1752 | |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1753 | /* |
| 1754 | * RFC 3530 language requires clid_inuse be returned when the |
| 1755 | * "principal" associated with a requests differs from that previously |
| 1756 | * used. We use uid, gid's, and gss principal string as our best |
| 1757 | * approximation. We also don't want to allow non-gss use of a client |
| 1758 | * established using gss: in theory cr_principal should catch that |
| 1759 | * change, but in practice cr_principal can be null even in the gss case |
| 1760 | * since gssd doesn't always pass down a principal string. |
| 1761 | */ |
| 1762 | static bool is_gss_cred(struct svc_cred *cr) |
| 1763 | { |
| 1764 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 1765 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 1766 | } |
| 1767 | |
| 1768 | |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1769 | static bool |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1770 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 1771 | { |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1772 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1773 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 1774 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1775 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 1776 | return false; |
| 1777 | if (cr1->cr_principal == cr2->cr_principal) |
| 1778 | return true; |
| 1779 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 1780 | return false; |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1781 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1784 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 1785 | { |
| 1786 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1787 | u32 service; |
| 1788 | |
J. Bruce Fields | c472059 | 2013-08-07 11:41:49 -0400 | [diff] [blame] | 1789 | if (!cr->cr_gss_mech) |
| 1790 | return false; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1791 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 1792 | return service == RPC_GSS_SVC_INTEGRITY || |
| 1793 | service == RPC_GSS_SVC_PRIVACY; |
| 1794 | } |
| 1795 | |
| 1796 | static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) |
| 1797 | { |
| 1798 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1799 | |
| 1800 | if (!cl->cl_mach_cred) |
| 1801 | return true; |
| 1802 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 1803 | return false; |
| 1804 | if (!svc_rqst_integrity_protected(rqstp)) |
| 1805 | return false; |
| 1806 | if (!cr->cr_principal) |
| 1807 | return false; |
| 1808 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 1809 | } |
| 1810 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1811 | 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] | 1812 | { |
| 1813 | static u32 current_clientid = 1; |
| 1814 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1815 | clp->cl_clientid.cl_boot = nn->boot_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | clp->cl_clientid.cl_id = current_clientid++; |
| 1817 | } |
| 1818 | |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 1819 | static void gen_confirm(struct nfs4_client *clp) |
| 1820 | { |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1821 | __be32 verf[2]; |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 1822 | static u32 i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | |
Jeff Layton | f419992 | 2014-06-17 07:44:11 -0400 | [diff] [blame] | 1824 | /* |
| 1825 | * This is opaque to client, so no need to byte-swap. Use |
| 1826 | * __force to keep sparse happy |
| 1827 | */ |
| 1828 | verf[0] = (__force __be32)get_seconds(); |
| 1829 | verf[1] = (__force __be32)i++; |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1830 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1833 | static struct nfs4_stid * |
| 1834 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1835 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 1836 | struct nfs4_stid *ret; |
| 1837 | |
| 1838 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 1839 | if (!ret || !ret->sc_type) |
| 1840 | return NULL; |
| 1841 | return ret; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 1842 | } |
| 1843 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1844 | static struct nfs4_stid * |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1845 | 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] | 1846 | { |
| 1847 | struct nfs4_stid *s; |
| 1848 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1849 | spin_lock(&cl->cl_lock); |
| 1850 | s = find_stateid_locked(cl, t); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 1851 | if (s != NULL) { |
| 1852 | if (typemask & s->sc_type) |
| 1853 | atomic_inc(&s->sc_count); |
| 1854 | else |
| 1855 | s = NULL; |
| 1856 | } |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1857 | spin_unlock(&cl->cl_lock); |
| 1858 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1859 | } |
| 1860 | |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 1861 | static struct nfs4_client *create_client(struct xdr_netobj name, |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1862 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 1863 | { |
| 1864 | struct nfs4_client *clp; |
| 1865 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1866 | int ret; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1867 | struct net *net = SVC_NET(rqstp); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1868 | |
| 1869 | clp = alloc_client(name); |
| 1870 | if (clp == NULL) |
| 1871 | return NULL; |
| 1872 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1873 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 1874 | if (ret) { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1875 | free_client(clp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1876 | return NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1877 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 1878 | INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null); |
Benny Halevy | 07cd490 | 2010-05-12 00:13:41 +0300 | [diff] [blame] | 1879 | clp->cl_time = get_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1880 | clear_bit(0, &clp->cl_cb_slot_busy); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1881 | copy_verf(clp, verf); |
| 1882 | rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1883 | gen_confirm(clp); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1884 | clp->cl_cb_session = NULL; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1885 | clp->net = net; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1886 | return clp; |
| 1887 | } |
| 1888 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1889 | static void |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1890 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 1891 | { |
| 1892 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 1893 | struct nfs4_client *clp; |
| 1894 | |
| 1895 | while (*new) { |
| 1896 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 1897 | parent = *new; |
| 1898 | |
| 1899 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 1900 | new = &((*new)->rb_left); |
| 1901 | else |
| 1902 | new = &((*new)->rb_right); |
| 1903 | } |
| 1904 | |
| 1905 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 1906 | rb_insert_color(&new_clp->cl_namenode, root); |
| 1907 | } |
| 1908 | |
| 1909 | static struct nfs4_client * |
| 1910 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 1911 | { |
| 1912 | long long cmp; |
| 1913 | struct rb_node *node = root->rb_node; |
| 1914 | struct nfs4_client *clp; |
| 1915 | |
| 1916 | while (node) { |
| 1917 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 1918 | cmp = compare_blob(&clp->cl_name, name); |
| 1919 | if (cmp > 0) |
| 1920 | node = node->rb_left; |
| 1921 | else if (cmp < 0) |
| 1922 | node = node->rb_right; |
| 1923 | else |
| 1924 | return clp; |
| 1925 | } |
| 1926 | return NULL; |
| 1927 | } |
| 1928 | |
| 1929 | static void |
| 1930 | add_to_unconfirmed(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | { |
| 1932 | unsigned int idhashval; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1933 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1935 | lockdep_assert_held(&nn->client_lock); |
| 1936 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1937 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1938 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1940 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 1941 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | } |
| 1943 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1944 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | move_to_confirmed(struct nfs4_client *clp) |
| 1946 | { |
| 1947 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 1948 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1950 | lockdep_assert_held(&nn->client_lock); |
| 1951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 1953 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 1954 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 1955 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1956 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 1957 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1961 | 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] | 1962 | { |
| 1963 | struct nfs4_client *clp; |
| 1964 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 1965 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1966 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1967 | if (same_clid(&clp->cl_clientid, clid)) { |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 1968 | if ((bool)clp->cl_minorversion != sessions) |
| 1969 | return NULL; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 1970 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | return clp; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 1972 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | } |
| 1974 | return NULL; |
| 1975 | } |
| 1976 | |
| 1977 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1978 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 1979 | { |
| 1980 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 1981 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1982 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1983 | return find_client_in_id_table(tbl, clid, sessions); |
| 1984 | } |
| 1985 | |
| 1986 | static struct nfs4_client * |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 1987 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | { |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1989 | struct list_head *tbl = nn->unconf_id_hashtbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1991 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 1992 | return find_client_in_id_table(tbl, clid, sessions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | } |
| 1994 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 1995 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 1996 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 1997 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 1998 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 1999 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2000 | static struct nfs4_client * |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2001 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2002 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2003 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2004 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | static struct nfs4_client * |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2008 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2009 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2010 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2011 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2014 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2015 | 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] | 2016 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2017 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2018 | struct sockaddr *sa = svc_addr(rqstp); |
| 2019 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2020 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2022 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 2023 | if (se->se_callback_netid_len == 3 && |
| 2024 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 2025 | expected_family = AF_INET; |
| 2026 | else if (se->se_callback_netid_len == 4 && |
| 2027 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 2028 | expected_family = AF_INET6; |
| 2029 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | goto out_err; |
| 2031 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2032 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2033 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2034 | (struct sockaddr *)&conn->cb_addr, |
| 2035 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2036 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2037 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2039 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2040 | if (conn->cb_addr.ss_family == AF_INET6) |
| 2041 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 2042 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2043 | conn->cb_prog = se->se_callback_prog; |
| 2044 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 2045 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | return; |
| 2047 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2048 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 2049 | conn->cb_addrlen = 0; |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 2050 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | "will not receive delegations\n", |
| 2052 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 2053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | return; |
| 2055 | } |
| 2056 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2057 | /* |
J. Bruce Fields | 067e1ac | 2014-03-21 17:26:44 -0400 | [diff] [blame] | 2058 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2059 | */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2060 | static void |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2061 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 2062 | { |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2063 | struct xdr_buf *buf = resp->xdr.buf; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2064 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 2065 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2066 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2067 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2068 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2069 | slot->sl_opcnt = resp->opcnt; |
| 2070 | slot->sl_status = resp->cstate.status; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2071 | |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2072 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2073 | if (nfsd4_not_cached(resp)) { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2074 | slot->sl_datalen = 0; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2075 | return; |
| 2076 | } |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2077 | base = resp->cstate.data_offset; |
| 2078 | slot->sl_datalen = buf->len - base; |
| 2079 | 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] | 2080 | WARN("%s: sessions DRC could not cache compound\n", __func__); |
| 2081 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2085 | * Encode the replay sequence operation from the slot values. |
| 2086 | * If cachethis is FALSE encode the uncached rep error on the next |
| 2087 | * operation which sets resp->p and increments resp->opcnt for |
| 2088 | * nfs4svc_encode_compoundres. |
| 2089 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2090 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2091 | static __be32 |
| 2092 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 2093 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2094 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2095 | struct nfsd4_op *op; |
| 2096 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2097 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2098 | /* Encode the replayed sequence operation */ |
| 2099 | op = &args->ops[resp->opcnt - 1]; |
| 2100 | nfsd4_encode_operation(resp, op); |
| 2101 | |
| 2102 | /* Return nfserr_retry_uncached_rep in next operation. */ |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2103 | if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2104 | op = &args->ops[resp->opcnt++]; |
| 2105 | op->status = nfserr_retry_uncached_rep; |
| 2106 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2107 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2108 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2112 | * The sequence operation is not cached because we can use the slot and |
| 2113 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2114 | */ |
J. Bruce Fields | 3ca2eb9 | 2014-03-21 12:04:21 -0400 | [diff] [blame] | 2115 | static __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2116 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 2117 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2118 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2119 | struct nfsd4_slot *slot = resp->cstate.slot; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2120 | struct xdr_stream *xdr = &resp->xdr; |
| 2121 | __be32 *p; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2122 | __be32 status; |
| 2123 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2124 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2125 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2126 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
J. Bruce Fields | 0da7b19 | 2014-03-21 11:43:34 -0400 | [diff] [blame] | 2127 | if (status) |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2128 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2129 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2130 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 2131 | if (!p) { |
| 2132 | WARN_ON_ONCE(1); |
| 2133 | return nfserr_serverfault; |
| 2134 | } |
| 2135 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 2136 | xdr_commit_encode(xdr); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2137 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2138 | resp->opcnt = slot->sl_opcnt; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2139 | return slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2140 | } |
| 2141 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2142 | /* |
| 2143 | * Set the exchange_id flags returned by the server. |
| 2144 | */ |
| 2145 | static void |
| 2146 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 2147 | { |
| 2148 | /* pNFS is not supported */ |
| 2149 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
| 2150 | |
| 2151 | /* Referrals are supported, Migration is not. */ |
| 2152 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 2153 | |
| 2154 | /* set the wire flags to return to client. */ |
| 2155 | clid->flags = new->cl_exchange_flags; |
| 2156 | } |
| 2157 | |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2158 | static bool client_has_state(struct nfs4_client *clp) |
| 2159 | { |
| 2160 | /* |
| 2161 | * Note clp->cl_openowners check isn't quite right: there's no |
| 2162 | * need to count owners without stateid's. |
| 2163 | * |
| 2164 | * Also note we should probably be using this in 4.0 case too. |
| 2165 | */ |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 2166 | return !list_empty(&clp->cl_openowners) |
| 2167 | || !list_empty(&clp->cl_delegations) |
| 2168 | || !list_empty(&clp->cl_sessions); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2169 | } |
| 2170 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2171 | __be32 |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2172 | nfsd4_exchange_id(struct svc_rqst *rqstp, |
| 2173 | struct nfsd4_compound_state *cstate, |
| 2174 | struct nfsd4_exchange_id *exid) |
| 2175 | { |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2176 | struct nfs4_client *conf, *new; |
| 2177 | struct nfs4_client *unconf = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2178 | __be32 status; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2179 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2180 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2181 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 2182 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2183 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2184 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2185 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2186 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2187 | "ip_addr=%s flags %x, spa_how %d\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2188 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2189 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2190 | |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 2191 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2192 | return nfserr_inval; |
| 2193 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2194 | switch (exid->spa_how) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2195 | case SP4_MACH_CRED: |
| 2196 | if (!svc_rqst_integrity_protected(rqstp)) |
| 2197 | return nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2198 | case SP4_NONE: |
| 2199 | break; |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 2200 | default: /* checked by xdr code */ |
| 2201 | WARN_ON_ONCE(1); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2202 | case SP4_SSV: |
J. Bruce Fields | dd30333 | 2013-04-12 18:10:56 -0400 | [diff] [blame] | 2203 | return nfserr_encr_alg_unsupp; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2204 | } |
| 2205 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2206 | new = create_client(exid->clname, rqstp, &verf); |
| 2207 | if (new == NULL) |
| 2208 | return nfserr_jukebox; |
| 2209 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2210 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2211 | nfs4_lock_state(); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2212 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2213 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2214 | if (conf) { |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 2215 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 2216 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 2217 | |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2218 | if (update) { |
| 2219 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2220 | status = nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2221 | goto out; |
| 2222 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2223 | if (!mach_creds_match(conf, rqstp)) { |
| 2224 | status = nfserr_wrong_cred; |
| 2225 | goto out; |
| 2226 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2227 | if (!creds_match) { /* case 9 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2228 | status = nfserr_perm; |
| 2229 | goto out; |
| 2230 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2231 | if (!verfs_match) { /* case 8 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2232 | status = nfserr_not_same; |
| 2233 | goto out; |
| 2234 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2235 | /* case 6 */ |
| 2236 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2237 | goto out_copy; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2238 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2239 | if (!creds_match) { /* case 3 */ |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2240 | if (client_has_state(conf)) { |
| 2241 | status = nfserr_clid_inuse; |
| 2242 | goto out; |
| 2243 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2244 | goto out_new; |
| 2245 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2246 | if (verfs_match) { /* case 2 */ |
J. Bruce Fields | 0f1ba0e | 2012-05-25 21:24:40 -0400 | [diff] [blame] | 2247 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2248 | goto out_copy; |
| 2249 | } |
| 2250 | /* case 5, client reboot */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2251 | conf = NULL; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2252 | goto out_new; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 2253 | } |
| 2254 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2255 | if (update) { /* case 7 */ |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 2256 | status = nfserr_noent; |
| 2257 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2258 | } |
| 2259 | |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2260 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2261 | if (unconf) /* case 4, possible retry or client restart */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2262 | unhash_client_locked(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2263 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2264 | /* case 1 (normal case) */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2265 | out_new: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2266 | if (conf) |
| 2267 | unhash_client_locked(conf); |
J. Bruce Fields | 4f540e2 | 2013-05-07 11:57:52 -0400 | [diff] [blame] | 2268 | new->cl_minorversion = cstate->minorversion; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2269 | new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2270 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2271 | gen_clid(new, nn); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2272 | add_to_unconfirmed(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2273 | swap(new, conf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2274 | out_copy: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2275 | exid->clientid.cl_boot = conf->cl_clientid.cl_boot; |
| 2276 | exid->clientid.cl_id = conf->cl_clientid.cl_id; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2277 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2278 | exid->seqid = conf->cl_cs_slot.sl_seqid + 1; |
| 2279 | nfsd4_set_ex_flags(conf, exid); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2280 | |
| 2281 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2282 | conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2283 | status = nfs_ok; |
| 2284 | |
| 2285 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2286 | spin_unlock(&nn->client_lock); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2287 | nfs4_unlock_state(); |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2288 | if (new) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2289 | expire_client(new); |
| 2290 | if (unconf) |
| 2291 | expire_client(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2292 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2293 | } |
| 2294 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2295 | static __be32 |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2296 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2297 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2298 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 2299 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2300 | |
| 2301 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2302 | if (slot_inuse) { |
| 2303 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2304 | return nfserr_jukebox; |
| 2305 | else |
| 2306 | return nfserr_seq_misordered; |
| 2307 | } |
J. Bruce Fields | f6d8248 | 2012-02-13 16:13:41 -0500 | [diff] [blame] | 2308 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2309 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2310 | return nfs_ok; |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2311 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2312 | return nfserr_replay_cache; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2313 | return nfserr_seq_misordered; |
| 2314 | } |
| 2315 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2316 | /* |
| 2317 | * Cache the create session result into the create session single DRC |
| 2318 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 2319 | * Do this for solo or embedded create session operations. |
| 2320 | */ |
| 2321 | static void |
| 2322 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2323 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2324 | { |
| 2325 | slot->sl_status = nfserr; |
| 2326 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 2327 | } |
| 2328 | |
| 2329 | static __be32 |
| 2330 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 2331 | struct nfsd4_clid_slot *slot) |
| 2332 | { |
| 2333 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 2334 | return slot->sl_status; |
| 2335 | } |
| 2336 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2337 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 2338 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 2339 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2340 | 3 + /* version, opcount, opcode */ \ |
| 2341 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2342 | /* seqid, slotID, slotID, cache */ \ |
| 2343 | 4 ) * sizeof(__be32)) |
| 2344 | |
| 2345 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 2346 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 2347 | 1 + /* status */ \ |
| 2348 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2349 | 3 + /* opcount, opcode, opstatus*/ \ |
| 2350 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2351 | /* seqid, slotID, slotID, slotID, status */ \ |
| 2352 | 5 ) * sizeof(__be32)) |
| 2353 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2354 | 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] | 2355 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2356 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 2357 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2358 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 2359 | return nfserr_toosmall; |
| 2360 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 2361 | return nfserr_toosmall; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2362 | ca->headerpadsz = 0; |
| 2363 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 2364 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 2365 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 2366 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 2367 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 2368 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 2369 | /* |
| 2370 | * Note decreasing slot size below client's request may make it |
| 2371 | * difficult for client to function correctly, whereas |
| 2372 | * decreasing the number of slots will (just?) affect |
| 2373 | * performance. When short on memory we therefore prefer to |
| 2374 | * decrease number of slots instead of their size. Clients that |
| 2375 | * request larger slots than they need will get poor results: |
| 2376 | */ |
| 2377 | ca->maxreqs = nfsd4_get_drc_mem(ca); |
| 2378 | if (!ca->maxreqs) |
| 2379 | return nfserr_jukebox; |
| 2380 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2381 | return nfs_ok; |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2382 | } |
| 2383 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2384 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
| 2385 | RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32)) |
| 2386 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
| 2387 | RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32)) |
| 2388 | |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2389 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 2390 | { |
| 2391 | ca->headerpadsz = 0; |
| 2392 | |
| 2393 | /* |
| 2394 | * These RPC_MAX_HEADER macros are overkill, especially since we |
| 2395 | * don't even do gss on the backchannel yet. But this is still |
| 2396 | * less than 1k. Tighten up this estimate in the unlikely event |
| 2397 | * it turns out to be a problem for some client: |
| 2398 | */ |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2399 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2400 | return nfserr_toosmall; |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2401 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2402 | return nfserr_toosmall; |
| 2403 | ca->maxresp_cached = 0; |
| 2404 | if (ca->maxops < 2) |
| 2405 | return nfserr_toosmall; |
| 2406 | |
| 2407 | return nfs_ok; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2408 | } |
| 2409 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2410 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 2411 | { |
| 2412 | switch (cbs->flavor) { |
| 2413 | case RPC_AUTH_NULL: |
| 2414 | case RPC_AUTH_UNIX: |
| 2415 | return nfs_ok; |
| 2416 | default: |
| 2417 | /* |
| 2418 | * GSS case: the spec doesn't allow us to return this |
| 2419 | * error. But it also doesn't allow us not to support |
| 2420 | * GSS. |
| 2421 | * I'd rather this fail hard than return some error the |
| 2422 | * client might think it can already handle: |
| 2423 | */ |
| 2424 | return nfserr_encr_alg_unsupp; |
| 2425 | } |
| 2426 | } |
| 2427 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2428 | __be32 |
| 2429 | nfsd4_create_session(struct svc_rqst *rqstp, |
| 2430 | struct nfsd4_compound_state *cstate, |
| 2431 | struct nfsd4_create_session *cr_ses) |
| 2432 | { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2433 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2434 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2435 | struct nfs4_client *old = NULL; |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2436 | struct nfsd4_session *new; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2437 | struct nfsd4_conn *conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2438 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2439 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2440 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2441 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2442 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 2443 | return nfserr_inval; |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2444 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 2445 | if (status) |
| 2446 | return status; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2447 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2448 | if (status) |
| 2449 | return status; |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2450 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 2451 | if (status) |
Kinglong Mee | f403e45 | 2013-12-23 17:31:21 +0800 | [diff] [blame] | 2452 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2453 | status = nfserr_jukebox; |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 2454 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2455 | if (!new) |
| 2456 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2457 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 2458 | if (!conn) |
| 2459 | goto out_free_session; |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2460 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2461 | nfs4_lock_state(); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2462 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2463 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2464 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2465 | WARN_ON_ONCE(conf && unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2466 | |
| 2467 | if (conf) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2468 | status = nfserr_wrong_cred; |
| 2469 | if (!mach_creds_match(conf, rqstp)) |
| 2470 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2471 | cs_slot = &conf->cl_cs_slot; |
| 2472 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 2473 | if (status == nfserr_replay_cache) { |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2474 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2475 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2476 | } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2477 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2478 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2479 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2480 | } else if (unconf) { |
| 2481 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2482 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2483 | status = nfserr_clid_inuse; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2484 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2485 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2486 | status = nfserr_wrong_cred; |
| 2487 | if (!mach_creds_match(unconf, rqstp)) |
| 2488 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2489 | cs_slot = &unconf->cl_cs_slot; |
| 2490 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 2491 | if (status) { |
| 2492 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2493 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2494 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2495 | } |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2496 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2497 | if (old) { |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2498 | status = mark_client_expired_locked(old); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2499 | if (status) |
| 2500 | goto out_free_conn; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2501 | } |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2502 | move_to_confirmed(unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2503 | conf = unconf; |
| 2504 | } else { |
| 2505 | status = nfserr_stale_clientid; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2506 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2507 | } |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2508 | status = nfs_ok; |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 2509 | /* |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 2510 | * We do not support RDMA or persistent sessions |
| 2511 | */ |
| 2512 | cr_ses->flags &= ~SESSION4_PERSIST; |
| 2513 | cr_ses->flags &= ~SESSION4_RDMA; |
| 2514 | |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2515 | init_session(rqstp, new, conf, cr_ses); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2516 | nfsd4_get_session_locked(new); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2517 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2518 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2519 | NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 2520 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2521 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2522 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2523 | /* cache solo and embedded create sessions under the client_lock */ |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2524 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2525 | spin_unlock(&nn->client_lock); |
| 2526 | /* init connection and backchannel */ |
| 2527 | nfsd4_init_conn(rqstp, conn, new); |
| 2528 | nfsd4_put_session(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2529 | nfs4_unlock_state(); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2530 | if (old) |
| 2531 | expire_client(old); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2532 | return status; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2533 | out_free_conn: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2534 | spin_unlock(&nn->client_lock); |
Yanchuan Nian | 266533c | 2012-12-24 18:11:45 +0800 | [diff] [blame] | 2535 | nfs4_unlock_state(); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2536 | free_conn(conn); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2537 | if (old) |
| 2538 | expire_client(old); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2539 | out_free_session: |
| 2540 | __free_session(new); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2541 | out_release_drc_mem: |
| 2542 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
J. Bruce Fields | 1ca5079 | 2013-03-14 18:12:03 -0400 | [diff] [blame] | 2543 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2544 | } |
| 2545 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2546 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 2547 | { |
| 2548 | switch (*dir) { |
| 2549 | case NFS4_CDFC4_FORE: |
| 2550 | case NFS4_CDFC4_BACK: |
| 2551 | return nfs_ok; |
| 2552 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 2553 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 2554 | *dir = NFS4_CDFC4_BOTH; |
| 2555 | return nfs_ok; |
| 2556 | }; |
| 2557 | return nfserr_inval; |
| 2558 | } |
| 2559 | |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2560 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc) |
| 2561 | { |
| 2562 | struct nfsd4_session *session = cstate->session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2563 | 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] | 2564 | __be32 status; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2565 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2566 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 2567 | if (status) |
| 2568 | return status; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2569 | spin_lock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2570 | session->se_cb_prog = bc->bc_cb_program; |
| 2571 | session->se_cb_sec = bc->bc_cb_sec; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2572 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2573 | |
| 2574 | nfsd4_probe_callback(session->se_client); |
| 2575 | |
| 2576 | return nfs_ok; |
| 2577 | } |
| 2578 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2579 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 2580 | struct nfsd4_compound_state *cstate, |
| 2581 | struct nfsd4_bind_conn_to_session *bcts) |
| 2582 | { |
| 2583 | __be32 status; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2584 | struct nfsd4_conn *conn; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2585 | struct nfsd4_session *session; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2586 | struct net *net = SVC_NET(rqstp); |
| 2587 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2588 | |
| 2589 | if (!nfsd4_last_compound_op(rqstp)) |
| 2590 | return nfserr_not_only_op; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2591 | nfs4_lock_state(); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2592 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2593 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2594 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2595 | if (!session) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2596 | goto out_no_session; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2597 | status = nfserr_wrong_cred; |
| 2598 | if (!mach_creds_match(session->se_client, rqstp)) |
| 2599 | goto out; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2600 | status = nfsd4_map_bcts_dir(&bcts->dir); |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2601 | if (status) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2602 | goto out; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2603 | conn = alloc_conn(rqstp, bcts->dir); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2604 | status = nfserr_jukebox; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2605 | if (!conn) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2606 | goto out; |
| 2607 | nfsd4_init_conn(rqstp, conn, session); |
| 2608 | status = nfs_ok; |
| 2609 | out: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2610 | nfsd4_put_session(session); |
| 2611 | out_no_session: |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2612 | nfs4_unlock_state(); |
| 2613 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2614 | } |
| 2615 | |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2616 | static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid) |
| 2617 | { |
| 2618 | if (!session) |
| 2619 | return 0; |
| 2620 | return !memcmp(sid, &session->se_sessionid, sizeof(*sid)); |
| 2621 | } |
| 2622 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2623 | __be32 |
| 2624 | nfsd4_destroy_session(struct svc_rqst *r, |
| 2625 | struct nfsd4_compound_state *cstate, |
| 2626 | struct nfsd4_destroy_session *sessionid) |
| 2627 | { |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2628 | struct nfsd4_session *ses; |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2629 | __be32 status; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2630 | int ref_held_by_me = 0; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2631 | struct net *net = SVC_NET(r); |
| 2632 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2633 | |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2634 | nfs4_lock_state(); |
| 2635 | status = nfserr_not_only_op; |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2636 | if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2637 | if (!nfsd4_last_compound_op(r)) |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2638 | goto out; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2639 | ref_held_by_me++; |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2640 | } |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2641 | dump_sessionid(__func__, &sessionid->sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2642 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2643 | ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2644 | if (!ses) |
| 2645 | goto out_client_lock; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2646 | status = nfserr_wrong_cred; |
| 2647 | if (!mach_creds_match(ses->se_client, r)) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2648 | goto out_put_session; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2649 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2650 | if (status) |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2651 | goto out_put_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2652 | unhash_session(ses); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2653 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2654 | |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 2655 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 2656 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2657 | spin_lock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2658 | status = nfs_ok; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2659 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2660 | nfsd4_put_session_locked(ses); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2661 | out_client_lock: |
| 2662 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2663 | out: |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2664 | nfs4_unlock_state(); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2665 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2666 | } |
| 2667 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2668 | 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] | 2669 | { |
| 2670 | struct nfsd4_conn *c; |
| 2671 | |
| 2672 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2673 | if (c->cn_xprt == xpt) { |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2674 | return c; |
| 2675 | } |
| 2676 | } |
| 2677 | return NULL; |
| 2678 | } |
| 2679 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2680 | 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] | 2681 | { |
| 2682 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2683 | struct nfsd4_conn *c; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2684 | __be32 status = nfs_ok; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2685 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2686 | |
| 2687 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2688 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2689 | if (c) |
| 2690 | goto out_free; |
| 2691 | status = nfserr_conn_not_bound_to_session; |
| 2692 | if (clp->cl_mach_cred) |
| 2693 | goto out_free; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2694 | __nfsd4_hash_conn(new, ses); |
| 2695 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2696 | ret = nfsd4_register_conn(new); |
| 2697 | if (ret) |
| 2698 | /* oops; xprt is already down: */ |
| 2699 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2700 | return nfs_ok; |
| 2701 | out_free: |
| 2702 | spin_unlock(&clp->cl_lock); |
| 2703 | free_conn(new); |
| 2704 | return status; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2705 | } |
| 2706 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2707 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 2708 | { |
| 2709 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 2710 | |
| 2711 | return args->opcnt > session->se_fchannel.maxops; |
| 2712 | } |
| 2713 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2714 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 2715 | struct nfsd4_session *session) |
| 2716 | { |
| 2717 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 2718 | |
| 2719 | return xb->len > session->se_fchannel.maxreq_sz; |
| 2720 | } |
| 2721 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2722 | __be32 |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2723 | nfsd4_sequence(struct svc_rqst *rqstp, |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2724 | struct nfsd4_compound_state *cstate, |
| 2725 | struct nfsd4_sequence *seq) |
| 2726 | { |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2727 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2728 | struct xdr_stream *xdr = &resp->xdr; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2729 | struct nfsd4_session *session; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2730 | struct nfs4_client *clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2731 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2732 | struct nfsd4_conn *conn; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2733 | __be32 status; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2734 | int buflen; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2735 | struct net *net = SVC_NET(rqstp); |
| 2736 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2737 | |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2738 | if (resp->opcnt != 1) |
| 2739 | return nfserr_sequence_pos; |
| 2740 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2741 | /* |
| 2742 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 2743 | * below. |
| 2744 | */ |
| 2745 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 2746 | if (!conn) |
| 2747 | return nfserr_jukebox; |
| 2748 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2749 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2750 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2751 | if (!session) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2752 | goto out_no_session; |
| 2753 | clp = session->se_client; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2754 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2755 | status = nfserr_too_many_ops; |
| 2756 | if (nfsd4_session_too_many_ops(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2757 | goto out_put_session; |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2758 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2759 | status = nfserr_req_too_big; |
| 2760 | if (nfsd4_request_too_big(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2761 | goto out_put_session; |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2762 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2763 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 2764 | if (seq->slotid >= session->se_fchannel.maxreqs) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2765 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2766 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2767 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2768 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 2769 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 2770 | /* We do not negotiate the number of slots yet, so set the |
| 2771 | * maxslots to the session maxreqs which is used to encode |
| 2772 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 2773 | seq->maxslots = session->se_fchannel.maxreqs; |
| 2774 | |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2775 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 2776 | slot->sl_flags & NFSD4_SLOT_INUSE); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2777 | if (status == nfserr_replay_cache) { |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2778 | status = nfserr_seq_misordered; |
| 2779 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2780 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2781 | cstate->slot = slot; |
| 2782 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2783 | cstate->clp = clp; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2784 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2785 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2786 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2787 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 2788 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2789 | } |
| 2790 | if (status) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2791 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2792 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2793 | status = nfsd4_sequence_check_conn(conn, session); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2794 | conn = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2795 | if (status) |
| 2796 | goto out_put_session; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2797 | |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2798 | buflen = (seq->cachethis) ? |
| 2799 | session->se_fchannel.maxresp_cached : |
| 2800 | session->se_fchannel.maxresp_sz; |
| 2801 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 2802 | nfserr_rep_too_big; |
J. Bruce Fields | a5cddc8 | 2014-05-12 18:10:58 -0400 | [diff] [blame] | 2803 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2804 | goto out_put_session; |
J. Bruce Fields | 32aaa62 | 2014-03-20 20:47:41 -0400 | [diff] [blame] | 2805 | svc_reserve(rqstp, buflen); |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2806 | |
| 2807 | status = nfs_ok; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2808 | /* Success! bump slot seqid */ |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2809 | slot->sl_seqid = seq->seqid; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2810 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2811 | if (seq->cachethis) |
| 2812 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2813 | else |
| 2814 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2815 | |
| 2816 | cstate->slot = slot; |
| 2817 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2818 | cstate->clp = clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2819 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2820 | out: |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2821 | switch (clp->cl_cb_state) { |
| 2822 | case NFSD4_CB_DOWN: |
| 2823 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 2824 | break; |
| 2825 | case NFSD4_CB_FAULT: |
| 2826 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 2827 | break; |
| 2828 | default: |
| 2829 | seq->status_flags = 0; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 2830 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 2831 | if (!list_empty(&clp->cl_revoked)) |
| 2832 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2833 | out_no_session: |
Kinglong Mee | 3f42d2c | 2014-03-24 11:56:59 +0800 | [diff] [blame] | 2834 | if (conn) |
| 2835 | free_conn(conn); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2836 | spin_unlock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2837 | return status; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2838 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2839 | nfsd4_put_session_locked(session); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2840 | goto out_no_session; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2841 | } |
| 2842 | |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2843 | void |
| 2844 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 2845 | { |
| 2846 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 2847 | |
| 2848 | if (nfsd4_has_session(cs)) { |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2849 | if (cs->status != nfserr_replay_cache) { |
| 2850 | nfsd4_store_cache_entry(resp); |
| 2851 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 2852 | } |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2853 | /* Drop session reference that was taken in nfsd4_sequence() */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2854 | nfsd4_put_session(cs->session); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2855 | } else if (cs->clp) |
| 2856 | put_client_renew(cs->clp); |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2857 | } |
| 2858 | |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2859 | __be32 |
| 2860 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc) |
| 2861 | { |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2862 | struct nfs4_client *conf, *unconf; |
| 2863 | struct nfs4_client *clp = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2864 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2865 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2866 | |
| 2867 | nfs4_lock_state(); |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2868 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2869 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2870 | conf = find_confirmed_client(&dc->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2871 | WARN_ON_ONCE(conf && unconf); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2872 | |
| 2873 | if (conf) { |
J. Bruce Fields | c0293b0 | 2013-03-14 18:20:01 -0400 | [diff] [blame] | 2874 | if (client_has_state(conf)) { |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2875 | status = nfserr_clientid_busy; |
| 2876 | goto out; |
| 2877 | } |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2878 | clp = conf; |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2879 | } else if (unconf) |
| 2880 | clp = unconf; |
| 2881 | else { |
| 2882 | status = nfserr_stale_clientid; |
| 2883 | goto out; |
| 2884 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2885 | if (!mach_creds_match(clp, rqstp)) { |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2886 | clp = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2887 | status = nfserr_wrong_cred; |
| 2888 | goto out; |
| 2889 | } |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2890 | unhash_client_locked(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2891 | out: |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2892 | spin_unlock(&nn->client_lock); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2893 | nfs4_unlock_state(); |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2894 | if (clp) |
| 2895 | expire_client(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2896 | return status; |
| 2897 | } |
| 2898 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2899 | __be32 |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2900 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc) |
| 2901 | { |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2902 | __be32 status = 0; |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2903 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2904 | if (rc->rca_one_fs) { |
| 2905 | if (!cstate->current_fh.fh_dentry) |
| 2906 | return nfserr_nofilehandle; |
| 2907 | /* |
| 2908 | * We don't take advantage of the rca_one_fs case. |
| 2909 | * That's OK, it's optional, we can safely ignore it. |
| 2910 | */ |
| 2911 | return nfs_ok; |
| 2912 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2913 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2914 | nfs4_lock_state(); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2915 | status = nfserr_complete_already; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 2916 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
| 2917 | &cstate->session->se_client->cl_flags)) |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2918 | goto out; |
| 2919 | |
| 2920 | status = nfserr_stale_clientid; |
| 2921 | if (is_client_expired(cstate->session->se_client)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2922 | /* |
| 2923 | * The following error isn't really legal. |
| 2924 | * But we only get here if the client just explicitly |
| 2925 | * destroyed the client. Surely it no longer cares what |
| 2926 | * error it gets back on an operation for the dead |
| 2927 | * client. |
| 2928 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2929 | goto out; |
| 2930 | |
| 2931 | status = nfs_ok; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 2932 | nfsd4_client_record_create(cstate->session->se_client); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2933 | out: |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2934 | nfs4_unlock_state(); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 2935 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 2939 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 2940 | struct nfsd4_setclientid *setclid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2941 | { |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 2942 | struct xdr_netobj clname = setclid->se_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | nfs4_verifier clverifier = setclid->se_verf; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2944 | struct nfs4_client *conf, *new; |
| 2945 | struct nfs4_client *unconf = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2946 | __be32 status; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2947 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 2948 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2949 | new = create_client(clname, rqstp, &clverifier); |
| 2950 | if (new == NULL) |
| 2951 | return nfserr_jukebox; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2952 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | nfs4_lock_state(); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2954 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2955 | conf = find_confirmed_client_by_name(&clname, nn); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2956 | if (conf) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2957 | /* case 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2959 | if (clp_used_exchangeid(conf)) |
| 2960 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 2961 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2962 | char addr_str[INET6_ADDRSTRLEN]; |
| 2963 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, |
| 2964 | sizeof(addr_str)); |
| 2965 | dprintk("NFSD: setclientid: string in use by client " |
| 2966 | "at %s\n", addr_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | goto out; |
| 2968 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2970 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 2971 | if (unconf) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2972 | unhash_client_locked(unconf); |
J. Bruce Fields | 34b232b | 2012-05-18 22:23:42 -0400 | [diff] [blame] | 2973 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 2974 | /* case 1: probable callback update */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | copy_clid(new, conf); |
J. Bruce Fields | 34b232b | 2012-05-18 22:23:42 -0400 | [diff] [blame] | 2976 | else /* case 4 (new client) or cases 2, 3 (client reboot): */ |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2977 | gen_clid(new, nn); |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 2978 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2979 | gen_callback(new, setclid, rqstp); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2980 | add_to_unconfirmed(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2981 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 2982 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 2983 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2984 | new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | status = nfs_ok; |
| 2986 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2987 | spin_unlock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | nfs4_unlock_state(); |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2989 | if (new) |
| 2990 | free_client(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2991 | if (unconf) |
| 2992 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | return status; |
| 2994 | } |
| 2995 | |
| 2996 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2997 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 2998 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
| 2999 | struct nfsd4_compound_state *cstate, |
| 3000 | struct nfsd4_setclientid_confirm *setclientid_confirm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | { |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3002 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3003 | struct nfs4_client *old = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3004 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 3005 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3006 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 3007 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 3009 | if (STALE_CLIENTID(clid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3010 | return nfserr_stale_clientid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | nfs4_lock_state(); |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3012 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3013 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3014 | conf = find_confirmed_client(clid, false, nn); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3015 | unconf = find_unconfirmed_client(clid, false, nn); |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3016 | /* |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3017 | * We try hard to give out unique clientid's, so if we get an |
| 3018 | * attempt to confirm the same clientid with a different cred, |
| 3019 | * there's a bug somewhere. Let's charitably assume it's our |
| 3020 | * bug. |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3021 | */ |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3022 | status = nfserr_serverfault; |
| 3023 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
| 3024 | goto out; |
| 3025 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
| 3026 | goto out; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3027 | /* cases below refer to rfc 3530 section 14.2.34: */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3028 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
| 3029 | if (conf && !unconf) /* case 2: probable retransmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | status = nfs_ok; |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3031 | else /* case 4: client hasn't noticed we rebooted yet? */ |
| 3032 | status = nfserr_stale_clientid; |
| 3033 | goto out; |
| 3034 | } |
| 3035 | status = nfs_ok; |
| 3036 | if (conf) { /* case 1: callback update */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3037 | old = unconf; |
| 3038 | unhash_client_locked(old); |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3039 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3040 | } else { /* case 3: normal case; new or rebooted client */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3041 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
| 3042 | if (old) { |
| 3043 | status = mark_client_expired_locked(old); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3044 | if (status) |
| 3045 | goto out; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3046 | } |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3047 | move_to_confirmed(unconf); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3048 | conf = unconf; |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 3049 | } |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3050 | get_client_locked(conf); |
| 3051 | spin_unlock(&nn->client_lock); |
| 3052 | nfsd4_probe_callback(conf); |
| 3053 | spin_lock(&nn->client_lock); |
| 3054 | put_client_renew_locked(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | out: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3056 | spin_unlock(&nn->client_lock); |
| 3057 | if (old) |
| 3058 | expire_client(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | nfs4_unlock_state(); |
| 3060 | return status; |
| 3061 | } |
| 3062 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3063 | static struct nfs4_file *nfsd4_alloc_file(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | { |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3065 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 3066 | } |
| 3067 | |
| 3068 | /* OPEN Share state helper functions */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 3069 | static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3070 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3071 | unsigned int hashval = file_hashval(fh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3072 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3073 | lockdep_assert_held(&state_lock); |
| 3074 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3075 | atomic_set(&fp->fi_ref, 1); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3076 | spin_lock_init(&fp->fi_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3077 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 3078 | INIT_LIST_HEAD(&fp->fi_delegations); |
Trond Myklebust | e2cf80d | 2014-07-23 16:17:38 -0400 | [diff] [blame] | 3079 | fh_copy_shallow(&fp->fi_fhandle, fh); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3080 | fp->fi_had_conflict = false; |
| 3081 | fp->fi_lease = NULL; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3082 | fp->fi_share_deny = 0; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3083 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 3084 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 3085 | hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | } |
| 3087 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 3088 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3089 | nfsd4_free_slabs(void) |
| 3090 | { |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3091 | kmem_cache_destroy(openowner_slab); |
| 3092 | kmem_cache_destroy(lockowner_slab); |
| 3093 | kmem_cache_destroy(file_slab); |
| 3094 | kmem_cache_destroy(stateid_slab); |
| 3095 | kmem_cache_destroy(deleg_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3096 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | |
Bryan Schumaker | 7208339 | 2011-11-01 15:24:59 -0400 | [diff] [blame] | 3098 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | nfsd4_init_slabs(void) |
| 3100 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3101 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 3102 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 3103 | if (openowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3104 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3105 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
Yanchuan Nian | 3c40794 | 2012-10-24 14:44:19 +0800 | [diff] [blame] | 3106 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3107 | if (lockowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3108 | goto out_free_openowner_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3109 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3110 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3111 | if (file_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3112 | goto out_free_lockowner_slab; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 3113 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3114 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 3115 | if (stateid_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3116 | goto out_free_file_slab; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 3117 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3118 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 3119 | if (deleg_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3120 | goto out_free_stateid_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | return 0; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3122 | |
| 3123 | out_free_stateid_slab: |
| 3124 | kmem_cache_destroy(stateid_slab); |
| 3125 | out_free_file_slab: |
| 3126 | kmem_cache_destroy(file_slab); |
| 3127 | out_free_lockowner_slab: |
| 3128 | kmem_cache_destroy(lockowner_slab); |
| 3129 | out_free_openowner_slab: |
| 3130 | kmem_cache_destroy(openowner_slab); |
| 3131 | out: |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3132 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 3133 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | } |
| 3135 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3136 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 3137 | { |
| 3138 | rp->rp_status = nfserr_serverfault; |
| 3139 | rp->rp_buflen = 0; |
| 3140 | rp->rp_buf = rp->rp_ibuf; |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 3141 | mutex_init(&rp->rp_mutex); |
| 3142 | } |
| 3143 | |
| 3144 | static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, |
| 3145 | struct nfs4_stateowner *so) |
| 3146 | { |
| 3147 | if (!nfsd4_has_session(cstate)) { |
| 3148 | mutex_lock(&so->so_replay.rp_mutex); |
| 3149 | cstate->replay_owner = so; |
| 3150 | atomic_inc(&so->so_count); |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) |
| 3155 | { |
| 3156 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 3157 | |
| 3158 | if (so != NULL) { |
| 3159 | cstate->replay_owner = NULL; |
| 3160 | mutex_unlock(&so->so_replay.rp_mutex); |
| 3161 | nfs4_put_stateowner(so); |
| 3162 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3163 | } |
| 3164 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3165 | 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] | 3166 | { |
| 3167 | struct nfs4_stateowner *sop; |
| 3168 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3169 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3170 | if (!sop) |
| 3171 | return NULL; |
| 3172 | |
| 3173 | sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL); |
| 3174 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3175 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3176 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3177 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3178 | sop->so_owner.len = owner->len; |
| 3179 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3180 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3181 | sop->so_client = clp; |
| 3182 | init_nfs4_replay(&sop->so_replay); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3183 | atomic_set(&sop->so_count, 1); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3184 | return sop; |
| 3185 | } |
| 3186 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3187 | 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] | 3188 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3189 | lockdep_assert_held(&clp->cl_lock); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 3190 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3191 | list_add(&oo->oo_owner.so_strhash, |
| 3192 | &clp->cl_ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3193 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | } |
| 3195 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 3196 | static void nfs4_unhash_openowner(struct nfs4_stateowner *so) |
| 3197 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3198 | unhash_openowner_locked(openowner(so)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 3199 | } |
| 3200 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3201 | static void nfs4_free_openowner(struct nfs4_stateowner *so) |
| 3202 | { |
| 3203 | struct nfs4_openowner *oo = openowner(so); |
| 3204 | |
| 3205 | kmem_cache_free(openowner_slab, oo); |
| 3206 | } |
| 3207 | |
| 3208 | static const struct nfs4_stateowner_operations openowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 3209 | .so_unhash = nfs4_unhash_openowner, |
| 3210 | .so_free = nfs4_free_openowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3211 | }; |
| 3212 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3213 | static struct nfs4_openowner * |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3214 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 3215 | struct nfsd4_compound_state *cstate) |
| 3216 | { |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3217 | struct nfs4_client *clp = cstate->clp; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 3218 | struct nfs4_openowner *oo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3220 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 3221 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3222 | return NULL; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3223 | oo->oo_owner.so_ops = &openowner_ops; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3224 | oo->oo_owner.so_is_open_owner = 1; |
| 3225 | oo->oo_owner.so_seqid = open->op_seqid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3226 | oo->oo_flags = 0; |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 3227 | if (nfsd4_has_session(cstate)) |
| 3228 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3229 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 3230 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3231 | INIT_LIST_HEAD(&oo->oo_close_lru); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3232 | spin_lock(&clp->cl_lock); |
| 3233 | ret = find_openstateowner_str_locked(strhashval, open, clp); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 3234 | if (ret == NULL) { |
| 3235 | hash_openowner(oo, clp, strhashval); |
| 3236 | ret = oo; |
| 3237 | } else |
| 3238 | nfs4_free_openowner(&oo->oo_owner); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3239 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3240 | return oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3241 | } |
| 3242 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 3243 | 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] | 3244 | struct nfs4_openowner *oo = open->op_openowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 3246 | atomic_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 3247 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 3248 | INIT_LIST_HEAD(&stp->st_locks); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3249 | stp->st_stateowner = &oo->oo_owner; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3250 | atomic_inc(&stp->st_stateowner->so_count); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3251 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3252 | stp->st_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | stp->st_access_bmap = 0; |
| 3254 | stp->st_deny_bmap = 0; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 3255 | stp->st_openstp = NULL; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 3256 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
| 3257 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3258 | spin_lock(&fp->fi_lock); |
| 3259 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 3260 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 3261 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | } |
| 3263 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3264 | /* |
| 3265 | * In the 4.0 case we need to keep the owners around a little while to handle |
| 3266 | * CLOSE replay. We still do need to release any file access that is held by |
| 3267 | * them before returning however. |
| 3268 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | static void |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3270 | move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3271 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 3272 | struct nfs4_ol_stateid *last; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3273 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 3274 | struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, |
| 3275 | nfsd_net_id); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3276 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3277 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 3279 | /* |
| 3280 | * We know that we hold one reference via nfsd4_close, and another |
| 3281 | * "persistent" reference for the client. If the refcount is higher |
| 3282 | * than 2, then there are still calls in progress that are using this |
| 3283 | * stateid. We can't put the sc_file reference until they are finished. |
| 3284 | * Wait for the refcount to drop to 2. Since it has been unhashed, |
| 3285 | * there should be no danger of the refcount going back up again at |
| 3286 | * this point. |
| 3287 | */ |
| 3288 | wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2); |
| 3289 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3290 | release_all_access(s); |
| 3291 | if (s->st_stid.sc_file) { |
| 3292 | put_nfs4_file(s->st_stid.sc_file); |
| 3293 | s->st_stid.sc_file = NULL; |
| 3294 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 3295 | |
| 3296 | spin_lock(&nn->client_lock); |
| 3297 | last = oo->oo_last_closed_stid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3298 | oo->oo_last_closed_stid = s; |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3299 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3300 | oo->oo_time = get_seconds(); |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 3301 | spin_unlock(&nn->client_lock); |
| 3302 | if (last) |
| 3303 | nfs4_put_stid(&last->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | } |
| 3305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | /* search file_hashtbl[] for file */ |
| 3307 | static struct nfs4_file * |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3308 | find_file_locked(struct knfsd_fh *fh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3310 | unsigned int hashval = file_hashval(fh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3311 | struct nfs4_file *fp; |
| 3312 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3313 | lockdep_assert_held(&state_lock); |
| 3314 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 3315 | hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3316 | if (nfsd_fh_match(&fp->fi_fhandle, fh)) { |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3317 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3318 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | } |
| 3321 | return NULL; |
| 3322 | } |
| 3323 | |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3324 | static struct nfs4_file * |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3325 | find_file(struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3326 | { |
| 3327 | struct nfs4_file *fp; |
| 3328 | |
| 3329 | spin_lock(&state_lock); |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3330 | fp = find_file_locked(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3331 | spin_unlock(&state_lock); |
| 3332 | return fp; |
| 3333 | } |
| 3334 | |
| 3335 | static struct nfs4_file * |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 3336 | find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3337 | { |
| 3338 | struct nfs4_file *fp; |
| 3339 | |
| 3340 | spin_lock(&state_lock); |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3341 | fp = find_file_locked(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3342 | if (fp == NULL) { |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 3343 | nfsd4_init_file(new, fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3344 | fp = new; |
| 3345 | } |
| 3346 | spin_unlock(&state_lock); |
| 3347 | |
| 3348 | return fp; |
| 3349 | } |
| 3350 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 3351 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | * Called to check deny when READ with all zero stateid or |
| 3353 | * WRITE with all zero or all one stateid |
| 3354 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3355 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 3357 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3358 | struct nfs4_file *fp; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3359 | __be32 ret = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3361 | fp = find_file(¤t_fh->fh_handle); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3362 | if (!fp) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3363 | return ret; |
| 3364 | /* Check for conflicting share reservations */ |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3365 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3366 | if (fp->fi_share_deny & deny_type) |
| 3367 | ret = nfserr_locked; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3368 | spin_unlock(&fp->fi_lock); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3369 | put_nfs4_file(fp); |
| 3370 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | } |
| 3372 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3373 | void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3374 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3375 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
| 3376 | nfsd_net_id); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 3377 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3378 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 3379 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3380 | /* |
| 3381 | * We can't do this in nfsd_break_deleg_cb because it is |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 3382 | * already holding inode->i_lock. |
| 3383 | * |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3384 | * If the dl_time != 0, then we know that it has already been |
| 3385 | * queued for a lease break. Don't queue it again. |
| 3386 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 3387 | spin_lock(&state_lock); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3388 | if (dp->dl_time == 0) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3389 | dp->dl_time = get_seconds(); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3390 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3391 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3392 | spin_unlock(&state_lock); |
| 3393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3394 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3395 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
| 3396 | { |
| 3397 | /* |
| 3398 | * We're assuming the state code never drops its reference |
| 3399 | * without first removing the lease. Since we're in this lease |
| 3400 | * callback (and since the lease code is serialized by the kernel |
| 3401 | * lock) we know the server hasn't removed the lease yet, we know |
| 3402 | * it's safe to take a reference. |
| 3403 | */ |
Trond Myklebust | 72c0b0f | 2014-07-21 09:34:58 -0400 | [diff] [blame] | 3404 | atomic_inc(&dp->dl_stid.sc_count); |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3405 | nfsd4_cb_recall(dp); |
| 3406 | } |
| 3407 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 3408 | /* Called from break_lease() with i_lock held. */ |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3409 | static void nfsd_break_deleg_cb(struct file_lock *fl) |
| 3410 | { |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3411 | struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; |
| 3412 | struct nfs4_delegation *dp; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3413 | |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 3414 | if (!fp) { |
| 3415 | WARN(1, "(%p)->fl_owner NULL\n", fl); |
| 3416 | return; |
| 3417 | } |
| 3418 | if (fp->fi_had_conflict) { |
| 3419 | WARN(1, "duplicate break on %p\n", fp); |
| 3420 | return; |
| 3421 | } |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3422 | /* |
| 3423 | * 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] | 3424 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3425 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3426 | */ |
| 3427 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3429 | spin_lock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3430 | fp->fi_had_conflict = true; |
| 3431 | /* |
| 3432 | * If there are no delegations on the list, then we can't count on this |
| 3433 | * lease ever being cleaned up. Set the fl_break_time to jiffies so that |
| 3434 | * time_out_leases will do it ASAP. The fact that fi_had_conflict is now |
| 3435 | * true should keep any new delegations from being hashed. |
| 3436 | */ |
| 3437 | if (list_empty(&fp->fi_delegations)) |
| 3438 | fl->fl_break_time = jiffies; |
| 3439 | else |
| 3440 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) |
| 3441 | nfsd_break_one_deleg(dp); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3442 | spin_unlock(&fp->fi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | } |
| 3444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | static |
| 3446 | int nfsd_change_deleg_cb(struct file_lock **onlist, int arg) |
| 3447 | { |
| 3448 | if (arg & F_UNLCK) |
| 3449 | return lease_modify(onlist, arg); |
| 3450 | else |
| 3451 | return -EAGAIN; |
| 3452 | } |
| 3453 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 3454 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 3455 | .lm_break = nfsd_break_deleg_cb, |
| 3456 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | }; |
| 3458 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 3459 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 3460 | { |
| 3461 | if (nfsd4_has_session(cstate)) |
| 3462 | return nfs_ok; |
| 3463 | if (seqid == so->so_seqid - 1) |
| 3464 | return nfserr_replay_me; |
| 3465 | if (seqid == so->so_seqid) |
| 3466 | return nfs_ok; |
| 3467 | return nfserr_bad_seqid; |
| 3468 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3470 | static __be32 lookup_clientid(clientid_t *clid, |
| 3471 | struct nfsd4_compound_state *cstate, |
| 3472 | struct nfsd_net *nn) |
| 3473 | { |
| 3474 | struct nfs4_client *found; |
| 3475 | |
| 3476 | if (cstate->clp) { |
| 3477 | found = cstate->clp; |
| 3478 | if (!same_clid(&found->cl_clientid, clid)) |
| 3479 | return nfserr_stale_clientid; |
| 3480 | return nfs_ok; |
| 3481 | } |
| 3482 | |
| 3483 | if (STALE_CLIENTID(clid, nn)) |
| 3484 | return nfserr_stale_clientid; |
| 3485 | |
| 3486 | /* |
| 3487 | * For v4.1+ we get the client in the SEQUENCE op. If we don't have one |
| 3488 | * cached already then we know this is for is for v4.0 and "sessions" |
| 3489 | * will be false. |
| 3490 | */ |
| 3491 | WARN_ON_ONCE(cstate->session); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 3492 | spin_lock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3493 | found = find_confirmed_client(clid, false, nn); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 3494 | if (!found) { |
| 3495 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3496 | return nfserr_expired; |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 3497 | } |
| 3498 | atomic_inc(&found->cl_refcount); |
| 3499 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3500 | |
| 3501 | /* Cache the nfs4_client in cstate! */ |
| 3502 | cstate->clp = found; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3503 | return nfs_ok; |
| 3504 | } |
| 3505 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3506 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3507 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 3508 | struct nfsd4_open *open, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3509 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3510 | clientid_t *clientid = &open->op_clientid; |
| 3511 | struct nfs4_client *clp = NULL; |
| 3512 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3513 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3514 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3515 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 3516 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3517 | return nfserr_stale_clientid; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3518 | /* |
| 3519 | * In case we need it later, after we've already created the |
| 3520 | * file and don't want to risk a further failure: |
| 3521 | */ |
| 3522 | open->op_file = nfsd4_alloc_file(); |
| 3523 | if (open->op_file == NULL) |
| 3524 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | |
Trond Myklebust | 2d91e89 | 2014-06-30 11:48:46 -0400 | [diff] [blame] | 3526 | status = lookup_clientid(clientid, cstate, nn); |
| 3527 | if (status) |
| 3528 | return status; |
| 3529 | clp = cstate->clp; |
| 3530 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3531 | strhashval = ownerstr_hashval(&open->op_owner); |
| 3532 | oo = find_openstateowner_str(strhashval, open, clp); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3533 | open->op_openowner = oo; |
| 3534 | if (!oo) { |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3535 | goto new_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3536 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3537 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3538 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3539 | release_openowner(oo); |
| 3540 | open->op_openowner = NULL; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3541 | goto new_owner; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3542 | } |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3543 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 3544 | if (status) |
| 3545 | return status; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3546 | goto alloc_stateid; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3547 | new_owner: |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3548 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3549 | if (oo == NULL) |
| 3550 | return nfserr_jukebox; |
| 3551 | open->op_openowner = oo; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3552 | alloc_stateid: |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 3553 | open->op_stp = nfs4_alloc_open_stateid(clp); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3554 | if (!open->op_stp) |
| 3555 | return nfserr_jukebox; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3556 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3557 | } |
| 3558 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3559 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 3560 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 3561 | { |
| 3562 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 3563 | return nfserr_openmode; |
| 3564 | else |
| 3565 | return nfs_ok; |
| 3566 | } |
| 3567 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 3568 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3569 | { |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3570 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 3571 | } |
| 3572 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3573 | 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] | 3574 | { |
| 3575 | struct nfs4_stid *ret; |
| 3576 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3577 | ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 3578 | if (!ret) |
| 3579 | return NULL; |
| 3580 | return delegstateid(ret); |
| 3581 | } |
| 3582 | |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3583 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 3584 | { |
| 3585 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 3586 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 3587 | } |
| 3588 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3589 | static __be32 |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 3590 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3591 | struct nfs4_delegation **dp) |
| 3592 | { |
| 3593 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3594 | __be32 status = nfserr_bad_stateid; |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 3595 | struct nfs4_delegation *deleg; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3596 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 3597 | deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); |
| 3598 | if (deleg == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3599 | goto out; |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3600 | flags = share_access_to_flags(open->op_share_access); |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 3601 | status = nfs4_check_delegmode(deleg, flags); |
| 3602 | if (status) { |
| 3603 | nfs4_put_stid(&deleg->dl_stid); |
| 3604 | goto out; |
| 3605 | } |
| 3606 | *dp = deleg; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3607 | out: |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3608 | if (!nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3609 | return nfs_ok; |
| 3610 | if (status) |
| 3611 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3612 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3613 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3614 | } |
| 3615 | |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3616 | static struct nfs4_ol_stateid * |
| 3617 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | { |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3619 | struct nfs4_ol_stateid *local, *ret = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3620 | struct nfs4_openowner *oo = open->op_openowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3622 | spin_lock(&fp->fi_lock); |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 3623 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | /* ignore lock owners */ |
| 3625 | if (local->st_stateowner->so_is_open_owner == 0) |
| 3626 | continue; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3627 | if (local->st_stateowner == &oo->oo_owner) { |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3628 | ret = local; |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 3629 | atomic_inc(&ret->st_stid.sc_count); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3630 | break; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3631 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3632 | } |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3633 | spin_unlock(&fp->fi_lock); |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3634 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | } |
| 3636 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 3637 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 3638 | { |
| 3639 | int flags = 0; |
| 3640 | |
| 3641 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 3642 | flags |= NFSD_MAY_READ; |
| 3643 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 3644 | flags |= NFSD_MAY_WRITE; |
| 3645 | return flags; |
| 3646 | } |
| 3647 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3648 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 3650 | struct nfsd4_open *open) |
| 3651 | { |
| 3652 | struct iattr iattr = { |
| 3653 | .ia_valid = ATTR_SIZE, |
| 3654 | .ia_size = 0, |
| 3655 | }; |
| 3656 | if (!open->op_truncate) |
| 3657 | return 0; |
| 3658 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 3659 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); |
| 3661 | } |
| 3662 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3663 | static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 3664 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
| 3665 | struct nfsd4_open *open) |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3666 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3667 | struct file *filp = NULL; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3668 | __be32 status; |
| 3669 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 3670 | int access = nfs4_access_to_access(open->op_share_access); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3671 | unsigned char old_access_bmap, old_deny_bmap; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3672 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3673 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3674 | |
| 3675 | /* |
| 3676 | * Are we trying to set a deny mode that would conflict with |
| 3677 | * current access? |
| 3678 | */ |
| 3679 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 3680 | if (status != nfs_ok) { |
| 3681 | spin_unlock(&fp->fi_lock); |
| 3682 | goto out; |
| 3683 | } |
| 3684 | |
| 3685 | /* set access to the file */ |
| 3686 | status = nfs4_file_get_access(fp, open->op_share_access); |
| 3687 | if (status != nfs_ok) { |
| 3688 | spin_unlock(&fp->fi_lock); |
| 3689 | goto out; |
| 3690 | } |
| 3691 | |
| 3692 | /* Set access bits in stateid */ |
| 3693 | old_access_bmap = stp->st_access_bmap; |
| 3694 | set_access(open->op_share_access, stp); |
| 3695 | |
| 3696 | /* Set new deny mask */ |
| 3697 | old_deny_bmap = stp->st_deny_bmap; |
| 3698 | set_deny(open->op_share_deny, stp); |
| 3699 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 3700 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3701 | if (!fp->fi_fds[oflag]) { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3702 | spin_unlock(&fp->fi_lock); |
| 3703 | status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3704 | if (status) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3705 | goto out_put_access; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3706 | spin_lock(&fp->fi_lock); |
| 3707 | if (!fp->fi_fds[oflag]) { |
| 3708 | fp->fi_fds[oflag] = filp; |
| 3709 | filp = NULL; |
| 3710 | } |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3711 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3712 | spin_unlock(&fp->fi_lock); |
| 3713 | if (filp) |
| 3714 | fput(filp); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3715 | |
| 3716 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3717 | if (status) |
| 3718 | goto out_put_access; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3719 | out: |
| 3720 | return status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3721 | out_put_access: |
| 3722 | stp->st_access_bmap = old_access_bmap; |
| 3723 | nfs4_file_put_access(fp, open->op_share_access); |
| 3724 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); |
| 3725 | goto out; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3726 | } |
| 3727 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3728 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3729 | 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] | 3730 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3731 | __be32 status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3732 | unsigned char old_deny_bmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 3734 | if (!test_access(open->op_share_access, stp)) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3735 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3736 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3737 | /* test and set deny mode */ |
| 3738 | spin_lock(&fp->fi_lock); |
| 3739 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 3740 | if (status == nfs_ok) { |
| 3741 | old_deny_bmap = stp->st_deny_bmap; |
| 3742 | set_deny(open->op_share_deny, stp); |
| 3743 | fp->fi_share_deny |= |
| 3744 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 3745 | } |
| 3746 | spin_unlock(&fp->fi_lock); |
| 3747 | |
| 3748 | if (status != nfs_ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3749 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3751 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3752 | if (status != nfs_ok) |
| 3753 | reset_union_bmap_deny(old_deny_bmap, stp); |
| 3754 | return status; |
| 3755 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3757 | static void |
J. Bruce Fields | 1255a8f | 2012-03-06 14:35:16 -0500 | [diff] [blame] | 3758 | nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | { |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3760 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | } |
| 3762 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 3763 | /* Should we give out recallable state?: */ |
| 3764 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 3765 | { |
| 3766 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 3767 | return true; |
| 3768 | /* |
| 3769 | * In the sessions case, since we don't have to establish a |
| 3770 | * separate connection for callbacks, we assume it's OK |
| 3771 | * until we hear otherwise: |
| 3772 | */ |
| 3773 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 3774 | } |
| 3775 | |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 3776 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag) |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3777 | { |
| 3778 | struct file_lock *fl; |
| 3779 | |
| 3780 | fl = locks_alloc_lock(); |
| 3781 | if (!fl) |
| 3782 | return NULL; |
| 3783 | locks_init_lock(fl); |
| 3784 | fl->fl_lmops = &nfsd_lease_mng_ops; |
J. Bruce Fields | 617588d | 2011-07-01 15:18:34 -0400 | [diff] [blame] | 3785 | fl->fl_flags = FL_DELEG; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3786 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 3787 | fl->fl_end = OFFSET_MAX; |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 3788 | fl->fl_owner = (fl_owner_t)fp; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3789 | fl->fl_pid = current->tgid; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3790 | return fl; |
| 3791 | } |
| 3792 | |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3793 | static int nfs4_setlease(struct nfs4_delegation *dp) |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3794 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3795 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3796 | struct file_lock *fl; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3797 | struct file *filp; |
| 3798 | int status = 0; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3799 | |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 3800 | fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3801 | if (!fl) |
| 3802 | return -ENOMEM; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3803 | filp = find_readable_file(fp); |
| 3804 | if (!filp) { |
| 3805 | /* We should always have a readable file here */ |
| 3806 | WARN_ON_ONCE(1); |
| 3807 | return -EBADF; |
| 3808 | } |
| 3809 | fl->fl_file = filp; |
| 3810 | status = vfs_setlease(filp, fl->fl_type, &fl); |
| 3811 | if (status) { |
| 3812 | locks_free_lock(fl); |
| 3813 | goto out_fput; |
| 3814 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3815 | spin_lock(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3816 | spin_lock(&fp->fi_lock); |
| 3817 | /* Did the lease get broken before we took the lock? */ |
| 3818 | status = -EAGAIN; |
| 3819 | if (fp->fi_had_conflict) |
| 3820 | goto out_unlock; |
| 3821 | /* Race breaker */ |
| 3822 | if (fp->fi_lease) { |
| 3823 | status = 0; |
| 3824 | atomic_inc(&fp->fi_delegees); |
| 3825 | hash_delegation_locked(dp, fp); |
| 3826 | goto out_unlock; |
| 3827 | } |
| 3828 | fp->fi_lease = fl; |
| 3829 | fp->fi_deleg_file = filp; |
| 3830 | atomic_set(&fp->fi_delegees, 1); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 3831 | hash_delegation_locked(dp, fp); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3832 | spin_unlock(&fp->fi_lock); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3833 | spin_unlock(&state_lock); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3834 | return 0; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3835 | out_unlock: |
| 3836 | spin_unlock(&fp->fi_lock); |
| 3837 | spin_unlock(&state_lock); |
| 3838 | out_fput: |
| 3839 | fput(filp); |
J. Bruce Fields | e873088 | 2012-01-23 13:52:01 -0500 | [diff] [blame] | 3840 | return status; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3841 | } |
| 3842 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3843 | static struct nfs4_delegation * |
| 3844 | nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, |
| 3845 | struct nfs4_file *fp) |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3846 | { |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3847 | int status; |
| 3848 | struct nfs4_delegation *dp; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3849 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 3850 | if (fp->fi_had_conflict) |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3851 | return ERR_PTR(-EAGAIN); |
| 3852 | |
| 3853 | dp = alloc_init_deleg(clp, fh); |
| 3854 | if (!dp) |
| 3855 | return ERR_PTR(-ENOMEM); |
| 3856 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 3857 | get_nfs4_file(fp); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3858 | spin_lock(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3859 | spin_lock(&fp->fi_lock); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3860 | dp->dl_stid.sc_file = fp; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3861 | if (!fp->fi_lease) { |
| 3862 | spin_unlock(&fp->fi_lock); |
| 3863 | spin_unlock(&state_lock); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3864 | status = nfs4_setlease(dp); |
| 3865 | goto out; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3866 | } |
J. Bruce Fields | cbf7a75 | 2014-03-03 12:19:18 -0500 | [diff] [blame] | 3867 | atomic_inc(&fp->fi_delegees); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3868 | if (fp->fi_had_conflict) { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3869 | status = -EAGAIN; |
| 3870 | goto out_unlock; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3871 | } |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 3872 | hash_delegation_locked(dp, fp); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3873 | status = 0; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3874 | out_unlock: |
| 3875 | spin_unlock(&fp->fi_lock); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3876 | spin_unlock(&state_lock); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3877 | out: |
| 3878 | if (status) { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 3879 | nfs4_put_stid(&dp->dl_stid); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3880 | return ERR_PTR(status); |
| 3881 | } |
| 3882 | return dp; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3883 | } |
| 3884 | |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 3885 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 3886 | { |
| 3887 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3888 | if (status == -EAGAIN) |
| 3889 | open->op_why_no_deleg = WND4_CONTENTION; |
| 3890 | else { |
| 3891 | open->op_why_no_deleg = WND4_RESOURCE; |
| 3892 | switch (open->op_deleg_want) { |
| 3893 | case NFS4_SHARE_WANT_READ_DELEG: |
| 3894 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 3895 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 3896 | break; |
| 3897 | case NFS4_SHARE_WANT_CANCEL: |
| 3898 | open->op_why_no_deleg = WND4_CANCELLED; |
| 3899 | break; |
| 3900 | case NFS4_SHARE_WANT_NO_DELEG: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 3901 | WARN_ON_ONCE(1); |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 3902 | } |
| 3903 | } |
| 3904 | } |
| 3905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3906 | /* |
| 3907 | * Attempt to hand out a delegation. |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3908 | * |
| 3909 | * Note we don't support write delegations, and won't until the vfs has |
| 3910 | * proper support for them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | */ |
| 3912 | static void |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 3913 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
| 3914 | struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3915 | { |
| 3916 | struct nfs4_delegation *dp; |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 3917 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
| 3918 | struct nfs4_client *clp = stp->st_stid.sc_client; |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 3919 | int cb_up; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3920 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3921 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3922 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3923 | open->op_recall = 0; |
| 3924 | switch (open->op_claim_type) { |
| 3925 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 3926 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3927 | open->op_recall = 1; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3928 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 3929 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3930 | break; |
| 3931 | case NFS4_OPEN_CLAIM_NULL: |
Ming Chen | ed47b06 | 2014-01-09 21:26:10 +0000 | [diff] [blame] | 3932 | case NFS4_OPEN_CLAIM_FH: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3933 | /* |
| 3934 | * Let's not give out any delegations till everyone's |
| 3935 | * had the chance to reclaim theirs.... |
| 3936 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 3937 | if (locks_in_grace(clp->net)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3938 | goto out_no_deleg; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3939 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3940 | goto out_no_deleg; |
Steve Dickson | 9a0590a | 2013-05-15 14:51:49 -0400 | [diff] [blame] | 3941 | /* |
| 3942 | * Also, if the file was opened for write or |
| 3943 | * create, there's a good chance the client's |
| 3944 | * about to write to it, resulting in an |
| 3945 | * immediate recall (since we don't support |
| 3946 | * write delegations): |
| 3947 | */ |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3948 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3949 | goto out_no_deleg; |
| 3950 | if (open->op_create == NFS4_OPEN_CREATE) |
| 3951 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3952 | break; |
| 3953 | default: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3954 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 3955 | } |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3956 | dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3957 | if (IS_ERR(dp)) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3958 | goto out_no_deleg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3959 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 3960 | 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] | 3961 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 3962 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 3963 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3964 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
Trond Myklebust | 67cb127 | 2014-07-29 21:34:17 -0400 | [diff] [blame] | 3965 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3966 | return; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 3967 | out_no_deleg: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3968 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 3969 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 3970 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3971 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 3972 | open->op_recall = 1; |
| 3973 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3974 | |
| 3975 | /* 4.1 client asking for a delegation? */ |
| 3976 | if (open->op_deleg_want) |
| 3977 | nfsd4_open_deleg_none_ext(open, status); |
| 3978 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | } |
| 3980 | |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 3981 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 3982 | struct nfs4_delegation *dp) |
| 3983 | { |
| 3984 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 3985 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 3986 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3987 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 3988 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 3989 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 3990 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 3991 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 3992 | } |
| 3993 | /* Otherwise the client must be confused wanting a delegation |
| 3994 | * it already has, therefore we don't return |
| 3995 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 3996 | */ |
| 3997 | } |
| 3998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3999 | /* |
| 4000 | * called with nfs4_lock_state() held. |
| 4001 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4002 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 4004 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4005 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4006 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | struct nfs4_file *fp = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4008 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4009 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4010 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | /* |
| 4013 | * Lookup file; if found, lookup stateid and check open request, |
| 4014 | * and check for delegations in the process of being recalled. |
| 4015 | * If not found, create the nfs4_file struct |
| 4016 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 4017 | fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4018 | if (fp != open->op_file) { |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 4019 | status = nfs4_check_deleg(cl, open, &dp); |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4020 | if (status) |
| 4021 | goto out; |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 4022 | stp = nfsd4_find_existing_open(fp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4023 | } else { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4024 | open->op_file = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4025 | status = nfserr_bad_stateid; |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4026 | if (nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4027 | goto out; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 4028 | status = nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | /* |
| 4032 | * OPEN the file, or upgrade an existing OPEN. |
| 4033 | * If truncate fails, the OPEN fails. |
| 4034 | */ |
| 4035 | if (stp) { |
| 4036 | /* Stateid was found, this is an OPEN upgrade */ |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4037 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4038 | if (status) |
| 4039 | goto out; |
| 4040 | } else { |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4041 | stp = open->op_stp; |
| 4042 | open->op_stp = NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 4043 | init_open_stateid(stp, fp, open); |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4044 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
| 4045 | if (status) { |
| 4046 | release_open_stateid(stp); |
| 4047 | goto out; |
| 4048 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | } |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4050 | update_stateid(&stp->st_stid.sc_stateid); |
| 4051 | memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4053 | if (nfsd4_has_session(&resp->cstate)) { |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4054 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 4055 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4056 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 4057 | goto nodeleg; |
| 4058 | } |
| 4059 | } |
| 4060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | /* |
| 4062 | * Attempt to hand out a delegation. No error return, because the |
| 4063 | * OPEN succeeds even if we fail. |
| 4064 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4065 | nfs4_open_delegation(current_fh, open, stp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4066 | nodeleg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | status = nfs_ok; |
| 4068 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4069 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4070 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | out: |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4072 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 4073 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 4074 | open->op_deleg_want) |
| 4075 | nfsd4_deleg_xgrade_none_ext(open, dp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4076 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4077 | if (fp) |
| 4078 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 4079 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
J. Bruce Fields | 1255a8f | 2012-03-06 14:35:16 -0500 | [diff] [blame] | 4080 | nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4081 | /* |
| 4082 | * To finish the open response, we just need to set the rflags. |
| 4083 | */ |
| 4084 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4085 | if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) && |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4086 | !nfsd4_has_session(&resp->cstate)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4087 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4088 | if (dp) |
| 4089 | nfs4_put_stid(&dp->dl_stid); |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 4090 | if (stp) |
| 4091 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4092 | |
| 4093 | return status; |
| 4094 | } |
| 4095 | |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4096 | void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, |
| 4097 | struct nfsd4_open *open, __be32 status) |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4098 | { |
| 4099 | if (open->op_openowner) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4100 | struct nfs4_stateowner *so = &open->op_openowner->oo_owner; |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4101 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4102 | nfsd4_cstate_assign_replay(cstate, so); |
| 4103 | nfs4_put_stateowner(so); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4104 | } |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4105 | if (open->op_file) |
| 4106 | nfsd4_free_file(open->op_file); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4107 | if (open->op_stp) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 4108 | nfs4_put_stid(&open->op_stp->st_stid); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4109 | } |
| 4110 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4111 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 4112 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4113 | clientid_t *clid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | { |
| 4115 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4116 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 4117 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | |
| 4119 | nfs4_lock_state(); |
| 4120 | dprintk("process_renew(%08x/%08x): starting\n", |
| 4121 | clid->cl_boot, clid->cl_id); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4122 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 4123 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | goto out; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4125 | clp = cstate->clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | status = nfserr_cb_path_down; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 4127 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 4128 | && clp->cl_cb_state != NFSD4_CB_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | goto out; |
| 4130 | status = nfs_ok; |
| 4131 | out: |
| 4132 | nfs4_unlock_state(); |
| 4133 | return status; |
| 4134 | } |
| 4135 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 4136 | static void |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 4137 | nfsd4_end_grace(struct nfsd_net *nn) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 4138 | { |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 4139 | /* do nothing if grace period already ended */ |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 4140 | if (nn->grace_ended) |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 4141 | return; |
| 4142 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 4143 | dprintk("NFSD: end of grace period\n"); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 4144 | nn->grace_ended = true; |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 4145 | nfsd4_record_grace_done(nn, nn->boot_time); |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 4146 | locks_end_grace(&nn->nfsd4_manager); |
J. Bruce Fields | e46b498 | 2010-03-01 19:21:21 -0500 | [diff] [blame] | 4147 | /* |
| 4148 | * Now that every NFSv4 client has had the chance to recover and |
| 4149 | * to see the (possibly new, possibly shorter) lease time, we |
| 4150 | * can safely set the next grace time to the current lease time: |
| 4151 | */ |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 4152 | nn->nfsd4_grace = nn->nfsd4_lease; |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 4153 | } |
| 4154 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 4155 | static time_t |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4156 | nfs4_laundromat(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | { |
| 4158 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4159 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | struct nfs4_delegation *dp; |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4161 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4162 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 3d73371 | 2012-11-27 14:11:44 +0300 | [diff] [blame] | 4163 | time_t cutoff = get_seconds() - nn->nfsd4_lease; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4164 | time_t t, new_timeo = nn->nfsd4_lease; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | |
| 4166 | nfs4_lock_state(); |
| 4167 | |
| 4168 | dprintk("NFSD: laundromat service - starting\n"); |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 4169 | nfsd4_end_grace(nn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 4170 | INIT_LIST_HEAD(&reaplist); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 4171 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 4172 | list_for_each_safe(pos, next, &nn->client_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4173 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
| 4174 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { |
| 4175 | t = clp->cl_time - cutoff; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4176 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | break; |
| 4178 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 4179 | if (mark_client_expired_locked(clp)) { |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 4180 | dprintk("NFSD: client in use (clientid %08x)\n", |
| 4181 | clp->cl_clientid.cl_id); |
| 4182 | continue; |
| 4183 | } |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 4184 | list_add(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 4185 | } |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 4186 | spin_unlock(&nn->client_lock); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 4187 | list_for_each_safe(pos, next, &reaplist) { |
| 4188 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 4190 | clp->cl_clientid.cl_id); |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 4191 | list_del_init(&clp->cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | expire_client(clp); |
| 4193 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4194 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 4195 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Stanislav Kinsbursky | 4e37a7c2 | 2012-11-26 15:22:03 +0300 | [diff] [blame] | 4197 | if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn) |
| 4198 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4200 | t = dp->dl_time - cutoff; |
| 4201 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | break; |
| 4203 | } |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 4204 | unhash_delegation_locked(dp); |
| 4205 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4206 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4207 | spin_unlock(&state_lock); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 4208 | while (!list_empty(&reaplist)) { |
| 4209 | dp = list_first_entry(&reaplist, struct nfs4_delegation, |
| 4210 | dl_recall_lru); |
| 4211 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4212 | revoke_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4214 | |
| 4215 | spin_lock(&nn->client_lock); |
| 4216 | while (!list_empty(&nn->close_lru)) { |
| 4217 | oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, |
| 4218 | oo_close_lru); |
| 4219 | if (time_after((unsigned long)oo->oo_time, |
| 4220 | (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4221 | t = oo->oo_time - cutoff; |
| 4222 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | break; |
| 4224 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4225 | list_del_init(&oo->oo_close_lru); |
| 4226 | stp = oo->oo_last_closed_stid; |
| 4227 | oo->oo_last_closed_stid = NULL; |
| 4228 | spin_unlock(&nn->client_lock); |
| 4229 | nfs4_put_stid(&stp->st_stid); |
| 4230 | spin_lock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4232 | spin_unlock(&nn->client_lock); |
| 4233 | |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4234 | new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | nfs4_unlock_state(); |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4236 | return new_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | } |
| 4238 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 4239 | static struct workqueue_struct *laundry_wq; |
| 4240 | static void laundromat_main(struct work_struct *); |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 4241 | |
| 4242 | static void |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4243 | laundromat_main(struct work_struct *laundry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4244 | { |
| 4245 | time_t t; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4246 | struct delayed_work *dwork = container_of(laundry, struct delayed_work, |
| 4247 | work); |
| 4248 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 4249 | laundromat_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4251 | t = nfs4_laundromat(nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4253 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | } |
| 4255 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4256 | 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] | 4257 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4258 | if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle)) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4259 | return nfserr_bad_stateid; |
| 4260 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | } |
| 4262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4264 | access_permit_read(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4266 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 4267 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 4268 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4269 | } |
| 4270 | |
| 4271 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4272 | access_permit_write(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4274 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 4275 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4279 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4281 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 4283 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 4284 | if (stp->st_openstp) |
| 4285 | stp = stp->st_openstp; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4286 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4287 | goto out; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4288 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4289 | goto out; |
| 4290 | status = nfs_ok; |
| 4291 | out: |
| 4292 | return status; |
| 4293 | } |
| 4294 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4295 | static inline __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4296 | 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] | 4297 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4298 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | return nfs_ok; |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4300 | else if (locks_in_grace(net)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4301 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | * conflicting state; so we must wait out the grace period. */ |
| 4303 | return nfserr_grace; |
| 4304 | } else if (flags & WR_STATE) |
| 4305 | return nfs4_share_conflict(current_fh, |
| 4306 | NFS4_SHARE_DENY_WRITE); |
| 4307 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 4308 | return nfs4_share_conflict(current_fh, |
| 4309 | NFS4_SHARE_DENY_READ); |
| 4310 | } |
| 4311 | |
| 4312 | /* |
| 4313 | * Allow READ/WRITE during grace period on recovered state only for files |
| 4314 | * that are not able to provide mandatory locking. |
| 4315 | */ |
| 4316 | static inline int |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4317 | grace_disallows_io(struct net *net, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | { |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4319 | return locks_in_grace(net) && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | } |
| 4321 | |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4322 | /* Returns true iff a is later than b: */ |
| 4323 | static bool stateid_generation_after(stateid_t *a, stateid_t *b) |
| 4324 | { |
Jim Rees | 1a9357f | 2013-05-17 17:33:00 -0400 | [diff] [blame] | 4325 | return (s32)(a->si_generation - b->si_generation) > 0; |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4326 | } |
| 4327 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 4328 | 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] | 4329 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4330 | /* |
| 4331 | * When sessions are used the stateid generation number is ignored |
| 4332 | * when it is zero. |
| 4333 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 4334 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4335 | return nfs_ok; |
| 4336 | |
| 4337 | if (in->si_generation == ref->si_generation) |
| 4338 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4339 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4340 | /* 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] | 4341 | if (stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4342 | return nfserr_bad_stateid; |
| 4343 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4344 | * However, we could see a stateid from the past, even from a |
| 4345 | * non-buggy client. For example, if the client sends a lock |
| 4346 | * while some IO is outstanding, the lock may bump si_generation |
| 4347 | * while the IO is still in flight. The client could avoid that |
| 4348 | * situation by waiting for responses on all the IO requests, |
| 4349 | * but better performance may result in retrying IO that |
| 4350 | * receives an old_stateid error if requests are rarely |
| 4351 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4352 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4353 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4354 | } |
| 4355 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4356 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4357 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4358 | struct nfs4_stid *s; |
| 4359 | struct nfs4_ol_stateid *ols; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4360 | __be32 status = nfserr_bad_stateid; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4361 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4362 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4363 | return status; |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4364 | /* Client debugging aid. */ |
| 4365 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { |
| 4366 | char addr_str[INET6_ADDRSTRLEN]; |
| 4367 | rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, |
| 4368 | sizeof(addr_str)); |
| 4369 | pr_warn_ratelimited("NFSD: client %s testing state ID " |
| 4370 | "with incorrect client ID\n", addr_str); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4371 | return status; |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4372 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4373 | spin_lock(&cl->cl_lock); |
| 4374 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4375 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4376 | goto out_unlock; |
J. Bruce Fields | 36279ac | 2011-09-26 12:53:00 -0400 | [diff] [blame] | 4377 | status = check_stateid_generation(stateid, &s->sc_stateid, 1); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4378 | if (status) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4379 | goto out_unlock; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4380 | switch (s->sc_type) { |
| 4381 | case NFS4_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4382 | status = nfs_ok; |
| 4383 | break; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4384 | case NFS4_REVOKED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4385 | status = nfserr_deleg_revoked; |
| 4386 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4387 | case NFS4_OPEN_STID: |
| 4388 | case NFS4_LOCK_STID: |
| 4389 | ols = openlockstateid(s); |
| 4390 | if (ols->st_stateowner->so_is_open_owner |
| 4391 | && !(openowner(ols->st_stateowner)->oo_flags |
| 4392 | & NFS4_OO_CONFIRMED)) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4393 | status = nfserr_bad_stateid; |
| 4394 | else |
| 4395 | status = nfs_ok; |
| 4396 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4397 | default: |
| 4398 | printk("unknown stateid type %x\n", s->sc_type); |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 4399 | /* Fallthrough */ |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4400 | case NFS4_CLOSED_STID: |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 4401 | case NFS4_CLOSED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4402 | status = nfserr_bad_stateid; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4403 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4404 | out_unlock: |
| 4405 | spin_unlock(&cl->cl_lock); |
| 4406 | return status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4407 | } |
| 4408 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4409 | static __be32 |
| 4410 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
| 4411 | stateid_t *stateid, unsigned char typemask, |
| 4412 | struct nfs4_stid **s, struct nfsd_net *nn) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4413 | { |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 4414 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4415 | |
| 4416 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 4417 | return nfserr_bad_stateid; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4418 | status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn); |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4419 | if (status == nfserr_stale_clientid) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4420 | if (cstate->session) |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4421 | return nfserr_bad_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4422 | return nfserr_stale_stateid; |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4423 | } |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 4424 | if (status) |
| 4425 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4426 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4427 | if (!*s) |
| 4428 | return nfserr_bad_stateid; |
| 4429 | return nfs_ok; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4430 | } |
| 4431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4432 | /* |
| 4433 | * Checks for stateid operations |
| 4434 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4435 | __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4436 | nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate, |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4437 | stateid_t *stateid, int flags, struct file **filpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | { |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4439 | struct nfs4_stid *s; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4440 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | struct nfs4_delegation *dp = NULL; |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4442 | struct svc_fh *current_fh = &cstate->current_fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | struct inode *ino = current_fh->fh_dentry->d_inode; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4444 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4445 | struct file *file = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4446 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4448 | if (filpp) |
| 4449 | *filpp = NULL; |
| 4450 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4451 | if (grace_disallows_io(net, ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | return nfserr_grace; |
| 4453 | |
| 4454 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4455 | return check_special_stateids(net, current_fh, stateid, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4457 | nfs4_lock_state(); |
| 4458 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4459 | status = nfsd4_lookup_stateid(cstate, stateid, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4460 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4461 | &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4462 | if (status) |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 4463 | goto unlock_state; |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4464 | status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate)); |
| 4465 | if (status) |
| 4466 | goto out; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4467 | switch (s->sc_type) { |
| 4468 | case NFS4_DELEG_STID: |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4469 | dp = delegstateid(s); |
J. Bruce Fields | dc9bf70 | 2009-02-21 11:14:43 -0800 | [diff] [blame] | 4470 | status = nfs4_check_delegmode(dp, flags); |
| 4471 | if (status) |
| 4472 | goto out; |
Dan Carpenter | 43b0178 | 2010-10-27 23:19:04 +0200 | [diff] [blame] | 4473 | if (filpp) { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4474 | file = dp->dl_stid.sc_file->fi_deleg_file; |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4475 | if (!file) { |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4476 | WARN_ON_ONCE(1); |
| 4477 | status = nfserr_serverfault; |
| 4478 | goto out; |
| 4479 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4480 | get_file(file); |
Dan Carpenter | 43b0178 | 2010-10-27 23:19:04 +0200 | [diff] [blame] | 4481 | } |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4482 | break; |
| 4483 | case NFS4_OPEN_STID: |
| 4484 | case NFS4_LOCK_STID: |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4485 | stp = openlockstateid(s); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4486 | status = nfs4_check_fh(current_fh, stp); |
| 4487 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4489 | if (stp->st_stateowner->so_is_open_owner |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4490 | && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | goto out; |
J. Bruce Fields | a4455be | 2009-02-21 10:40:22 -0800 | [diff] [blame] | 4492 | status = nfs4_check_openmode(stp, flags); |
| 4493 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | goto out; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4495 | if (filpp) { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4496 | struct nfs4_file *fp = stp->st_stid.sc_file; |
| 4497 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4498 | if (flags & RD_STATE) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4499 | file = find_readable_file(fp); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4500 | else |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4501 | file = find_writeable_file(fp); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4502 | } |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4503 | break; |
| 4504 | default: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4505 | status = nfserr_bad_stateid; |
| 4506 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | } |
| 4508 | status = nfs_ok; |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4509 | if (file) |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4510 | *filpp = file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | out: |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 4512 | nfs4_put_stid(s); |
| 4513 | unlock_state: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4514 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | return status; |
| 4516 | } |
| 4517 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4518 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4519 | * Test if the stateid is valid |
| 4520 | */ |
| 4521 | __be32 |
| 4522 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4523 | struct nfsd4_test_stateid *test_stateid) |
| 4524 | { |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4525 | struct nfsd4_test_stateid_id *stateid; |
| 4526 | struct nfs4_client *cl = cstate->session->se_client; |
| 4527 | |
| 4528 | nfs4_lock_state(); |
| 4529 | 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] | 4530 | stateid->ts_id_status = |
| 4531 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4532 | nfs4_unlock_state(); |
| 4533 | |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4534 | return nfs_ok; |
| 4535 | } |
| 4536 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4537 | __be32 |
| 4538 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4539 | struct nfsd4_free_stateid *free_stateid) |
| 4540 | { |
| 4541 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4542 | struct nfs4_stid *s; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4543 | struct nfs4_delegation *dp; |
Jeff Layton | fc5a96c | 2014-07-29 21:34:40 -0400 | [diff] [blame] | 4544 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4545 | struct nfs4_client *cl = cstate->session->se_client; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4546 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4547 | |
| 4548 | nfs4_lock_state(); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4549 | spin_lock(&cl->cl_lock); |
| 4550 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4551 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4552 | goto out_unlock; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4553 | switch (s->sc_type) { |
| 4554 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4555 | ret = nfserr_locks_held; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4556 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4557 | case NFS4_OPEN_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4558 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 4559 | if (ret) |
| 4560 | break; |
| 4561 | ret = nfserr_locks_held; |
| 4562 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4563 | case NFS4_LOCK_STID: |
| 4564 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 4565 | if (ret) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4566 | break; |
Jeff Layton | fc5a96c | 2014-07-29 21:34:40 -0400 | [diff] [blame] | 4567 | stp = openlockstateid(s); |
| 4568 | ret = nfserr_locks_held; |
| 4569 | if (check_for_locks(stp->st_stid.sc_file, |
| 4570 | lockowner(stp->st_stateowner))) |
| 4571 | break; |
| 4572 | unhash_lock_stateid(stp); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4573 | spin_unlock(&cl->cl_lock); |
Jeff Layton | fc5a96c | 2014-07-29 21:34:40 -0400 | [diff] [blame] | 4574 | nfs4_put_stid(s); |
| 4575 | ret = nfs_ok; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4576 | goto out; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4577 | case NFS4_REVOKED_DELEG_STID: |
| 4578 | dp = delegstateid(s); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 4579 | list_del_init(&dp->dl_recall_lru); |
| 4580 | spin_unlock(&cl->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 4581 | nfs4_put_stid(s); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4582 | ret = nfs_ok; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4583 | goto out; |
| 4584 | /* Default falls through and returns nfserr_bad_stateid */ |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4585 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4586 | out_unlock: |
| 4587 | spin_unlock(&cl->cl_lock); |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4588 | out: |
| 4589 | nfs4_unlock_state(); |
| 4590 | return ret; |
| 4591 | } |
| 4592 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4593 | static inline int |
| 4594 | setlkflg (int type) |
| 4595 | { |
| 4596 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 4597 | RD_STATE : WR_STATE; |
| 4598 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4600 | 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] | 4601 | { |
| 4602 | struct svc_fh *current_fh = &cstate->current_fh; |
| 4603 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 4604 | __be32 status; |
| 4605 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4606 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 4607 | if (status) |
| 4608 | return status; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4609 | if (stp->st_stid.sc_type == NFS4_CLOSED_STID |
| 4610 | || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4611 | /* |
| 4612 | * "Closed" stateid's exist *only* to return |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4613 | * nfserr_replay_me from the previous step, and |
| 4614 | * revoked delegations are kept only for free_stateid. |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4615 | */ |
| 4616 | return nfserr_bad_stateid; |
| 4617 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); |
| 4618 | if (status) |
| 4619 | return status; |
| 4620 | return nfs4_check_fh(current_fh, stp); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4621 | } |
| 4622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | /* |
| 4624 | * Checks for sequence id mutating operations. |
| 4625 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4626 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4627 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 4628 | stateid_t *stateid, char typemask, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4629 | struct nfs4_ol_stateid **stpp, |
| 4630 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4632 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4633 | struct nfs4_stid *s; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4634 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4636 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
| 4637 | seqid, STATEID_VAL(stateid)); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | *stpp = NULL; |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4640 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4641 | if (status) |
| 4642 | return status; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4643 | stp = openlockstateid(s); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4644 | nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4646 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 4647 | if (!status) |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4648 | *stpp = stp; |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 4649 | else |
| 4650 | nfs4_put_stid(&stp->st_stid); |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4651 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4652 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 4653 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4654 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 4655 | 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] | 4656 | { |
| 4657 | __be32 status; |
| 4658 | struct nfs4_openowner *oo; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 4659 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4661 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 4662 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 4663 | if (status) |
| 4664 | return status; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 4665 | oo = openowner(stp->st_stateowner); |
| 4666 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
| 4667 | nfs4_put_stid(&stp->st_stid); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4668 | return nfserr_bad_stateid; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 4669 | } |
| 4670 | *stpp = stp; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4671 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4672 | } |
| 4673 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4674 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4675 | 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] | 4676 | struct nfsd4_open_confirm *oc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4677 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4678 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4679 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4680 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4681 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4683 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 4684 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4686 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 4687 | if (status) |
| 4688 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | |
| 4690 | nfs4_lock_state(); |
| 4691 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4692 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4693 | oc->oc_seqid, &oc->oc_req_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4694 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4695 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4696 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4697 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4698 | status = nfserr_bad_stateid; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4699 | if (oo->oo_flags & NFS4_OO_CONFIRMED) |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 4700 | goto put_stateid; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4701 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4702 | update_stateid(&stp->st_stid.sc_stateid); |
| 4703 | 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] | 4704 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4705 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 4706 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 4707 | nfsd4_client_record_create(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4708 | status = nfs_ok; |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 4709 | put_stateid: |
| 4710 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4712 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4713 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | return status; |
| 4715 | } |
| 4716 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4717 | 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] | 4718 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4719 | if (!test_access(access, stp)) |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4720 | return; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4721 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4722 | clear_access(access, stp); |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4723 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 4724 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4725 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 4726 | { |
| 4727 | switch (to_access) { |
| 4728 | case NFS4_SHARE_ACCESS_READ: |
| 4729 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 4730 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 4731 | break; |
| 4732 | case NFS4_SHARE_ACCESS_WRITE: |
| 4733 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 4734 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 4735 | break; |
| 4736 | case NFS4_SHARE_ACCESS_BOTH: |
| 4737 | break; |
| 4738 | default: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4739 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4740 | } |
| 4741 | } |
| 4742 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4743 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4744 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
| 4745 | struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4746 | struct nfsd4_open_downgrade *od) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4747 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4748 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4749 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4750 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4751 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4752 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 4753 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | |
J. Bruce Fields | c30e92d | 2011-10-10 17:34:31 -0400 | [diff] [blame] | 4755 | /* We don't yet support WANT bits: */ |
Benny Halevy | 2c8bd7e | 2012-02-16 20:57:09 +0200 | [diff] [blame] | 4756 | if (od->od_deleg_want) |
| 4757 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 4758 | od->od_deleg_want); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4759 | |
| 4760 | nfs4_lock_state(); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4761 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4762 | &od->od_stateid, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4763 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4764 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4765 | status = nfserr_inval; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4766 | if (!test_access(od->od_share_access, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 4767 | dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4768 | stp->st_access_bmap, od->od_share_access); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 4769 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | } |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4771 | if (!test_deny(od->od_share_deny, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 4772 | dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4773 | stp->st_deny_bmap, od->od_share_deny); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 4774 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4775 | } |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4776 | nfs4_stateid_downgrade(stp, od->od_share_access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4778 | reset_union_bmap_deny(od->od_share_deny, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4779 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4780 | update_stateid(&stp->st_stid.sc_stateid); |
| 4781 | memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4782 | status = nfs_ok; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 4783 | put_stateid: |
| 4784 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4785 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4786 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4787 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4788 | return status; |
| 4789 | } |
| 4790 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4791 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 4792 | { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4793 | struct nfs4_client *clp = s->st_stid.sc_client; |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 4794 | LIST_HEAD(reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4795 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4796 | s->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 4797 | spin_lock(&clp->cl_lock); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 4798 | unhash_open_stateid(s, &reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4799 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 4800 | if (clp->cl_minorversion) { |
| 4801 | put_ol_stateid_locked(s, &reaplist); |
| 4802 | spin_unlock(&clp->cl_lock); |
| 4803 | free_ol_stateid_reaplist(&reaplist); |
| 4804 | } else { |
| 4805 | spin_unlock(&clp->cl_lock); |
| 4806 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4807 | move_to_close_lru(s, clp->net); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 4808 | } |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 4809 | } |
| 4810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | /* |
| 4812 | * nfs4_unlock_state() called after encode |
| 4813 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4814 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4815 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4816 | struct nfsd4_close *close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4817 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4818 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4819 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4820 | struct net *net = SVC_NET(rqstp); |
| 4821 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4823 | dprintk("NFSD: nfsd4_close on file %pd\n", |
| 4824 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | |
| 4826 | nfs4_lock_state(); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4827 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 4828 | &close->cl_stateid, |
| 4829 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4830 | &stp, nn); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4831 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4832 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | goto out; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4834 | update_stateid(&stp->st_stid.sc_stateid); |
| 4835 | memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4837 | nfsd4_close_open_stateid(stp); |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 4838 | |
| 4839 | /* put reference from nfs4_preprocess_seqid_op */ |
| 4840 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4841 | out: |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4842 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4843 | return status; |
| 4844 | } |
| 4845 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4846 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4847 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4848 | struct nfsd4_delegreturn *dr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4849 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4850 | struct nfs4_delegation *dp; |
| 4851 | stateid_t *stateid = &dr->dr_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4852 | struct nfs4_stid *s; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4853 | __be32 status; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4854 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4855 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4856 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4857 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | |
| 4859 | nfs4_lock_state(); |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4860 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4861 | if (status) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4862 | goto out; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4863 | dp = delegstateid(s); |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 4864 | 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] | 4865 | if (status) |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 4866 | goto put_stateid; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4867 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4868 | destroy_delegation(dp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 4869 | put_stateid: |
| 4870 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | out: |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4872 | nfs4_unlock_state(); |
| 4873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | return status; |
| 4875 | } |
| 4876 | |
| 4877 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4878 | #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4880 | static inline u64 |
| 4881 | end_offset(u64 start, u64 len) |
| 4882 | { |
| 4883 | u64 end; |
| 4884 | |
| 4885 | end = start + len; |
| 4886 | return end >= start ? end: NFS4_MAX_UINT64; |
| 4887 | } |
| 4888 | |
| 4889 | /* last octet in a range */ |
| 4890 | static inline u64 |
| 4891 | last_byte_offset(u64 start, u64 len) |
| 4892 | { |
| 4893 | u64 end; |
| 4894 | |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4895 | WARN_ON_ONCE(!len); |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4896 | end = start + len; |
| 4897 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 4898 | } |
| 4899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4900 | /* |
| 4901 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 4902 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 4903 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 4904 | * locking, this prevents us from being completely protocol-compliant. The |
| 4905 | * real solution to this problem is to start using unsigned file offsets in |
| 4906 | * the VFS, but this is a very deep change! |
| 4907 | */ |
| 4908 | static inline void |
| 4909 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 4910 | { |
| 4911 | if (lock->fl_start < 0) |
| 4912 | lock->fl_start = OFFSET_MAX; |
| 4913 | if (lock->fl_end < 0) |
| 4914 | lock->fl_end = OFFSET_MAX; |
| 4915 | } |
| 4916 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4917 | /* Hack!: For now, we're defining this just so we can use a pointer to it |
| 4918 | * as a unique cookie to identify our (NFSv4's) posix locks. */ |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 4919 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4920 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | |
| 4922 | static inline void |
| 4923 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 4924 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4925 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4927 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4928 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
| 4929 | deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data, |
| 4930 | lo->lo_owner.so_owner.len, GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 4931 | if (!deny->ld_owner.data) |
| 4932 | /* We just don't care that much */ |
| 4933 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4934 | deny->ld_owner.len = lo->lo_owner.so_owner.len; |
| 4935 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4936 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 4937 | nevermind: |
| 4938 | deny->ld_owner.len = 0; |
| 4939 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4940 | deny->ld_clientid.cl_boot = 0; |
| 4941 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | } |
| 4943 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4944 | deny->ld_length = NFS4_MAX_UINT64; |
| 4945 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 4947 | deny->ld_type = NFS4_READ_LT; |
| 4948 | if (fl->fl_type != F_RDLCK) |
| 4949 | deny->ld_type = NFS4_WRITE_LT; |
| 4950 | } |
| 4951 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4952 | static struct nfs4_lockowner * |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 4953 | find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4954 | struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4956 | unsigned int strhashval = ownerstr_hashval(owner); |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4957 | struct nfs4_stateowner *so; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4958 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 4959 | lockdep_assert_held(&clp->cl_lock); |
| 4960 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4961 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], |
| 4962 | so_strhash) { |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4963 | if (so->so_is_open_owner) |
| 4964 | continue; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4965 | if (!same_owner_str(so, owner)) |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4966 | continue; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 4967 | atomic_inc(&so->so_count); |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 4968 | return lockowner(so); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | } |
| 4970 | return NULL; |
| 4971 | } |
| 4972 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 4973 | static struct nfs4_lockowner * |
| 4974 | find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4975 | struct nfs4_client *clp) |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 4976 | { |
| 4977 | struct nfs4_lockowner *lo; |
| 4978 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4979 | spin_lock(&clp->cl_lock); |
| 4980 | lo = find_lockowner_str_locked(clid, owner, clp); |
| 4981 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 4982 | return lo; |
| 4983 | } |
| 4984 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4985 | static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) |
| 4986 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 4987 | unhash_lockowner_locked(lockowner(sop)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4988 | } |
| 4989 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4990 | static void nfs4_free_lockowner(struct nfs4_stateowner *sop) |
| 4991 | { |
| 4992 | struct nfs4_lockowner *lo = lockowner(sop); |
| 4993 | |
| 4994 | kmem_cache_free(lockowner_slab, lo); |
| 4995 | } |
| 4996 | |
| 4997 | static const struct nfs4_stateowner_operations lockowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4998 | .so_unhash = nfs4_unhash_lockowner, |
| 4999 | .so_free = nfs4_free_lockowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 5000 | }; |
| 5001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5002 | /* |
| 5003 | * Alloc a lock owner structure. |
| 5004 | * 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] | 5005 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5006 | * |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 5007 | * strhashval = ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5009 | static struct nfs4_lockowner * |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5010 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
| 5011 | struct nfs4_ol_stateid *open_stp, |
| 5012 | struct nfsd4_lock *lock) |
| 5013 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5014 | struct nfs4_lockowner *lo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5016 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 5017 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | return NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5019 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 5020 | lo->lo_owner.so_is_open_owner = 0; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5021 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 5022 | lo->lo_owner.so_ops = &lockowner_ops; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5023 | spin_lock(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5024 | ret = find_lockowner_str_locked(&clp->cl_clientid, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5025 | &lock->lk_new_owner, clp); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5026 | if (ret == NULL) { |
| 5027 | list_add(&lo->lo_owner.so_strhash, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5028 | &clp->cl_ownerstr_hashtbl[strhashval]); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5029 | ret = lo; |
| 5030 | } else |
| 5031 | nfs4_free_lockowner(&lo->lo_owner); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5032 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5033 | return lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | } |
| 5035 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5036 | static void |
| 5037 | init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, |
| 5038 | struct nfs4_file *fp, struct inode *inode, |
| 5039 | struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | { |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 5041 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5042 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5043 | lockdep_assert_held(&clp->cl_lock); |
| 5044 | |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5045 | atomic_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 5046 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5047 | stp->st_stateowner = &lo->lo_owner; |
Trond Myklebust | e4f1dd7 | 2014-07-29 21:34:30 -0400 | [diff] [blame] | 5048 | atomic_inc(&lo->lo_owner.so_count); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 5049 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5050 | stp->st_stid.sc_file = fp; |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 5051 | stp->st_stid.sc_free = nfs4_free_lock_stateid; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5052 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5053 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 5054 | stp->st_openstp = open_stp; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5055 | list_add(&stp->st_locks, &open_stp->st_locks); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 5056 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 5057 | spin_lock(&fp->fi_lock); |
| 5058 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 5059 | spin_unlock(&fp->fi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5060 | } |
| 5061 | |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5062 | static struct nfs4_ol_stateid * |
| 5063 | find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) |
| 5064 | { |
| 5065 | struct nfs4_ol_stateid *lst; |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5066 | struct nfs4_client *clp = lo->lo_owner.so_client; |
| 5067 | |
| 5068 | lockdep_assert_held(&clp->cl_lock); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5069 | |
| 5070 | list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5071 | if (lst->st_stid.sc_file == fp) { |
| 5072 | atomic_inc(&lst->st_stid.sc_count); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5073 | return lst; |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5074 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5075 | } |
| 5076 | return NULL; |
| 5077 | } |
| 5078 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5079 | static struct nfs4_ol_stateid * |
| 5080 | find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, |
| 5081 | struct inode *inode, struct nfs4_ol_stateid *ost, |
| 5082 | bool *new) |
| 5083 | { |
| 5084 | struct nfs4_stid *ns = NULL; |
| 5085 | struct nfs4_ol_stateid *lst; |
| 5086 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 5087 | struct nfs4_client *clp = oo->oo_owner.so_client; |
| 5088 | |
| 5089 | spin_lock(&clp->cl_lock); |
| 5090 | lst = find_lock_stateid(lo, fi); |
| 5091 | if (lst == NULL) { |
| 5092 | spin_unlock(&clp->cl_lock); |
| 5093 | ns = nfs4_alloc_stid(clp, stateid_slab); |
| 5094 | if (ns == NULL) |
| 5095 | return NULL; |
| 5096 | |
| 5097 | spin_lock(&clp->cl_lock); |
| 5098 | lst = find_lock_stateid(lo, fi); |
| 5099 | if (likely(!lst)) { |
| 5100 | lst = openlockstateid(ns); |
| 5101 | init_lock_stateid(lst, lo, fi, inode, ost); |
| 5102 | ns = NULL; |
| 5103 | *new = true; |
| 5104 | } |
| 5105 | } |
| 5106 | spin_unlock(&clp->cl_lock); |
| 5107 | if (ns) |
| 5108 | nfs4_put_stid(ns); |
| 5109 | return lst; |
| 5110 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5111 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 5112 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | check_lock_length(u64 offset, u64 length) |
| 5114 | { |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 5115 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5116 | LOFF_OVERFLOW(offset, length))); |
| 5117 | } |
| 5118 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5119 | 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] | 5120 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5121 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5122 | |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5123 | lockdep_assert_held(&fp->fi_lock); |
| 5124 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5125 | if (test_access(access, lock_stp)) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5126 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 5127 | __nfs4_file_get_access(fp, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5128 | set_access(access, lock_stp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5129 | } |
| 5130 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5131 | static __be32 |
| 5132 | lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, |
| 5133 | struct nfs4_ol_stateid *ost, |
| 5134 | struct nfsd4_lock *lock, |
| 5135 | struct nfs4_ol_stateid **lst, bool *new) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5136 | { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5137 | __be32 status; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5138 | struct nfs4_file *fi = ost->st_stid.sc_file; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5139 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 5140 | struct nfs4_client *cl = oo->oo_owner.so_client; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5141 | struct inode *inode = cstate->current_fh.fh_dentry->d_inode; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5142 | struct nfs4_lockowner *lo; |
| 5143 | unsigned int strhashval; |
| 5144 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5145 | lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5146 | if (!lo) { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5147 | strhashval = ownerstr_hashval(&lock->v.new.owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5148 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 5149 | if (lo == NULL) |
| 5150 | return nfserr_jukebox; |
| 5151 | } else { |
| 5152 | /* with an existing lockowner, seqids must be the same */ |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5153 | status = nfserr_bad_seqid; |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5154 | if (!cstate->minorversion && |
| 5155 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5156 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5157 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5158 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5159 | *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5160 | if (*lst == NULL) { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5161 | status = nfserr_jukebox; |
| 5162 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5163 | } |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5164 | status = nfs_ok; |
| 5165 | out: |
| 5166 | nfs4_put_stateowner(&lo->lo_owner); |
| 5167 | return status; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5168 | } |
| 5169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | /* |
| 5171 | * LOCK operation |
| 5172 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5173 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5174 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 5175 | struct nfsd4_lock *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5177 | struct nfs4_openowner *open_sop = NULL; |
| 5178 | struct nfs4_lockowner *lock_sop = NULL; |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5179 | struct nfs4_ol_stateid *lock_stp = NULL; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5180 | struct nfs4_ol_stateid *open_stp = NULL; |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5181 | struct nfs4_file *fp; |
J. Bruce Fields | 7d94784 | 2010-08-20 18:09:31 -0400 | [diff] [blame] | 5182 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5183 | struct file_lock *file_lock = NULL; |
| 5184 | struct file_lock *conflock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5185 | __be32 status = 0; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5186 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5187 | int err; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5188 | bool new = false; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5189 | struct net *net = SVC_NET(rqstp); |
| 5190 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | |
| 5192 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 5193 | (long long) lock->lk_offset, |
| 5194 | (long long) lock->lk_length); |
| 5195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5196 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 5197 | return nfserr_inval; |
| 5198 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5199 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 5200 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 5201 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 5202 | return status; |
| 5203 | } |
| 5204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | nfs4_lock_state(); |
| 5206 | |
| 5207 | if (lock->lk_is_new) { |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 5208 | if (nfsd4_has_session(cstate)) |
| 5209 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
| 5210 | memcpy(&lock->v.new.clientid, |
| 5211 | &cstate->session->se_client->cl_clientid, |
| 5212 | sizeof(clientid_t)); |
| 5213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | status = nfserr_stale_clientid; |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 5215 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5219 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | lock->lk_new_open_seqid, |
| 5221 | &lock->lk_new_open_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5222 | &open_stp, nn); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 5223 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5224 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5225 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5226 | status = nfserr_bad_stateid; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 5227 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5228 | &lock->v.new.clientid)) |
| 5229 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5230 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5231 | &lock_stp, &new); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5232 | } else { |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 5233 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5234 | lock->lk_old_lock_seqid, |
| 5235 | &lock->lk_old_lock_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5236 | NFS4_LOCK_STID, &lock_stp, nn); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5237 | } |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 5238 | if (status) |
| 5239 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5240 | lock_sop = lockowner(lock_stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5242 | lkflg = setlkflg(lock->lk_type); |
| 5243 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 5244 | if (status) |
| 5245 | goto out; |
| 5246 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 5247 | status = nfserr_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5248 | if (locks_in_grace(net) && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 5249 | goto out; |
| 5250 | status = nfserr_no_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5251 | if (!locks_in_grace(net) && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 5252 | goto out; |
| 5253 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5254 | file_lock = locks_alloc_lock(); |
| 5255 | if (!file_lock) { |
| 5256 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5257 | status = nfserr_jukebox; |
| 5258 | goto out; |
| 5259 | } |
| 5260 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5261 | fp = lock_stp->st_stid.sc_file; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5262 | locks_init_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | switch (lock->lk_type) { |
| 5264 | case NFS4_READ_LT: |
| 5265 | case NFS4_READW_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5266 | spin_lock(&fp->fi_lock); |
| 5267 | filp = find_readable_file_locked(fp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5268 | if (filp) |
| 5269 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5270 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5271 | file_lock->fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 5272 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | case NFS4_WRITE_LT: |
| 5274 | case NFS4_WRITEW_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5275 | spin_lock(&fp->fi_lock); |
| 5276 | filp = find_writeable_file_locked(fp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5277 | if (filp) |
| 5278 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5279 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5280 | file_lock->fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 5281 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | default: |
| 5283 | status = nfserr_inval; |
| 5284 | goto out; |
| 5285 | } |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5286 | if (!filp) { |
| 5287 | status = nfserr_openmode; |
| 5288 | goto out; |
| 5289 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5290 | file_lock->fl_owner = (fl_owner_t)lock_sop; |
| 5291 | file_lock->fl_pid = current->tgid; |
| 5292 | file_lock->fl_file = filp; |
| 5293 | file_lock->fl_flags = FL_POSIX; |
| 5294 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 5295 | file_lock->fl_start = lock->lk_offset; |
| 5296 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 5297 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5298 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5299 | conflock = locks_alloc_lock(); |
| 5300 | if (!conflock) { |
| 5301 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5302 | status = nfserr_jukebox; |
| 5303 | goto out; |
| 5304 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5305 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5306 | err = vfs_lock_file(filp, F_SETLK, file_lock, conflock); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5307 | switch (-err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | case 0: /* success! */ |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5309 | update_stateid(&lock_stp->st_stid.sc_stateid); |
| 5310 | memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5311 | sizeof(stateid_t)); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5312 | status = 0; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 5313 | break; |
| 5314 | case (EAGAIN): /* conflock holds conflicting lock */ |
| 5315 | status = nfserr_denied; |
| 5316 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5317 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 5318 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | case (EDEADLK): |
| 5320 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 5321 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 5322 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 5323 | 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] | 5324 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 5325 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | out: |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5328 | if (filp) |
| 5329 | fput(filp); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5330 | if (lock_stp) { |
| 5331 | /* Bump seqid manually if the 4.0 replay owner is openowner */ |
| 5332 | if (cstate->replay_owner && |
| 5333 | cstate->replay_owner != &lock_sop->lo_owner && |
| 5334 | seqid_mutating_err(ntohl(status))) |
| 5335 | lock_sop->lo_owner.so_seqid++; |
| 5336 | |
| 5337 | /* |
| 5338 | * If this is a new, never-before-used stateid, and we are |
| 5339 | * returning an error, then just go ahead and release it. |
| 5340 | */ |
| 5341 | if (status && new) |
| 5342 | release_lock_stateid(lock_stp); |
| 5343 | |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5344 | nfs4_put_stid(&lock_stp->st_stid); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5345 | } |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5346 | if (open_stp) |
| 5347 | nfs4_put_stid(&open_stp->st_stid); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5348 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 5349 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5350 | if (file_lock) |
| 5351 | locks_free_lock(file_lock); |
| 5352 | if (conflock) |
| 5353 | locks_free_lock(conflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | return status; |
| 5355 | } |
| 5356 | |
| 5357 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 5358 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 5359 | * so we do a temporary open here just to get an open file to pass to |
| 5360 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 5361 | * inode operation.) |
| 5362 | */ |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5363 | 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] | 5364 | { |
| 5365 | struct file *file; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5366 | __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); |
| 5367 | if (!err) { |
| 5368 | err = nfserrno(vfs_test_lock(file, lock)); |
| 5369 | nfsd_close(file); |
| 5370 | } |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 5371 | return err; |
| 5372 | } |
| 5373 | |
| 5374 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5375 | * LOCKT operation |
| 5376 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5377 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5378 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 5379 | struct nfsd4_lockt *lockt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | { |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5381 | struct file_lock *file_lock = NULL; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5382 | struct nfs4_lockowner *lo = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5383 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5384 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5385 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5386 | if (locks_in_grace(SVC_NET(rqstp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | return nfserr_grace; |
| 5388 | |
| 5389 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 5390 | return nfserr_inval; |
| 5391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5392 | nfs4_lock_state(); |
| 5393 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5394 | if (!nfsd4_has_session(cstate)) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5395 | status = lookup_clientid(&lockt->lt_clientid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5396 | if (status) |
| 5397 | goto out; |
| 5398 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 5400 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5403 | file_lock = locks_alloc_lock(); |
| 5404 | if (!file_lock) { |
| 5405 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5406 | status = nfserr_jukebox; |
| 5407 | goto out; |
| 5408 | } |
| 5409 | locks_init_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | switch (lockt->lt_type) { |
| 5411 | case NFS4_READ_LT: |
| 5412 | case NFS4_READW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5413 | file_lock->fl_type = F_RDLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5414 | break; |
| 5415 | case NFS4_WRITE_LT: |
| 5416 | case NFS4_WRITEW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5417 | file_lock->fl_type = F_WRLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5418 | break; |
| 5419 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 5420 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | status = nfserr_inval; |
| 5422 | goto out; |
| 5423 | } |
| 5424 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5425 | lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, |
| 5426 | cstate->clp); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5427 | if (lo) |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5428 | file_lock->fl_owner = (fl_owner_t)lo; |
| 5429 | file_lock->fl_pid = current->tgid; |
| 5430 | file_lock->fl_flags = FL_POSIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5432 | file_lock->fl_start = lockt->lt_offset; |
| 5433 | 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] | 5434 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5435 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5437 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5438 | if (status) |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 5439 | goto out; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5440 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5441 | if (file_lock->fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | status = nfserr_denied; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5443 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | } |
| 5445 | out: |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5446 | if (lo) |
| 5447 | nfs4_put_stateowner(&lo->lo_owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5449 | if (file_lock) |
| 5450 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | return status; |
| 5452 | } |
| 5453 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5454 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5455 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 5456 | struct nfsd4_locku *locku) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5458 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5460 | struct file_lock *file_lock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5461 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5462 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5463 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 5464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 5466 | (long long) locku->lu_offset, |
| 5467 | (long long) locku->lu_length); |
| 5468 | |
| 5469 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 5470 | return nfserr_inval; |
| 5471 | |
| 5472 | nfs4_lock_state(); |
| 5473 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5474 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5475 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 5476 | &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5477 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | goto out; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5479 | filp = find_any_file(stp->st_stid.sc_file); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5480 | if (!filp) { |
| 5481 | status = nfserr_lock_range; |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 5482 | goto put_stateid; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5483 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5484 | file_lock = locks_alloc_lock(); |
| 5485 | if (!file_lock) { |
| 5486 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5487 | status = nfserr_jukebox; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5488 | goto fput; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5489 | } |
| 5490 | locks_init_lock(file_lock); |
| 5491 | file_lock->fl_type = F_UNLCK; |
J. Bruce Fields | 0a262ff | 2013-06-17 17:29:40 -0400 | [diff] [blame] | 5492 | file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5493 | file_lock->fl_pid = current->tgid; |
| 5494 | file_lock->fl_file = filp; |
| 5495 | file_lock->fl_flags = FL_POSIX; |
| 5496 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 5497 | file_lock->fl_start = locku->lu_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5498 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5499 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 5500 | locku->lu_length); |
| 5501 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5503 | err = vfs_lock_file(filp, F_SETLK, file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5504 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 5505 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | goto out_nfserr; |
| 5507 | } |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5508 | update_stateid(&stp->st_stid.sc_stateid); |
| 5509 | memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5510 | fput: |
| 5511 | fput(filp); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 5512 | put_stateid: |
| 5513 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5515 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 5516 | nfs4_unlock_state(); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5517 | if (file_lock) |
| 5518 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | return status; |
| 5520 | |
| 5521 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5522 | status = nfserrno(err); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5523 | goto fput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | } |
| 5525 | |
| 5526 | /* |
| 5527 | * returns |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5528 | * true: locks held by lockowner |
| 5529 | * false: no locks held by lockowner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5530 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5531 | static bool |
| 5532 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | { |
| 5534 | struct file_lock **flpp; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5535 | int status = false; |
| 5536 | struct file *filp = find_any_file(fp); |
| 5537 | struct inode *inode; |
| 5538 | |
| 5539 | if (!filp) { |
| 5540 | /* Any valid lock stateid should have some sort of access */ |
| 5541 | WARN_ON_ONCE(1); |
| 5542 | return status; |
| 5543 | } |
| 5544 | |
| 5545 | inode = file_inode(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 5547 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5548 | for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) { |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 5549 | if ((*flpp)->fl_owner == (fl_owner_t)lowner) { |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5550 | status = true; |
| 5551 | break; |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 5552 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5553 | } |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 5554 | spin_unlock(&inode->i_lock); |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5555 | fput(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5556 | return status; |
| 5557 | } |
| 5558 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5559 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 5560 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 5561 | struct nfsd4_compound_state *cstate, |
| 5562 | struct nfsd4_release_lockowner *rlockowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | { |
| 5564 | clientid_t *clid = &rlockowner->rl_clientid; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5565 | struct nfs4_stateowner *sop; |
| 5566 | struct nfs4_lockowner *lo = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5567 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5569 | unsigned int hashval = ownerstr_hashval(owner); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5570 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5571 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5572 | struct nfs4_client *clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5573 | |
| 5574 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 5575 | clid->cl_boot, clid->cl_id); |
| 5576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5577 | nfs4_lock_state(); |
| 5578 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5579 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5580 | if (status) |
| 5581 | goto out; |
| 5582 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5583 | clp = cstate->clp; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5584 | /* Find the matching lock stateowner */ |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5585 | spin_lock(&clp->cl_lock); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5586 | list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval], |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5587 | so_strhash) { |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5588 | |
| 5589 | if (sop->so_is_open_owner || !same_owner_str(sop, owner)) |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 5590 | continue; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5591 | |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5592 | /* see if there are still any locks associated with it */ |
| 5593 | lo = lockowner(sop); |
| 5594 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { |
| 5595 | if (check_for_locks(stp->st_stid.sc_file, lo)) { |
| 5596 | status = nfserr_locks_held; |
| 5597 | spin_unlock(&clp->cl_lock); |
| 5598 | goto out; |
| 5599 | } |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 5600 | } |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5601 | |
| 5602 | atomic_inc(&sop->so_count); |
| 5603 | break; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5604 | } |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5605 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5606 | if (lo) |
| 5607 | release_lockowner(lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5608 | out: |
| 5609 | nfs4_unlock_state(); |
| 5610 | return status; |
| 5611 | } |
| 5612 | |
| 5613 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5614 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5615 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5616 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | } |
| 5618 | |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5619 | bool |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5620 | nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5621 | { |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5622 | struct nfs4_client_reclaim *crp; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5623 | |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5624 | crp = nfsd4_find_reclaim_client(name, nn); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5625 | return (crp && crp->cr_clp); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5626 | } |
| 5627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5628 | /* |
| 5629 | * failure => all reset bets are off, nfserr_no_grace... |
| 5630 | */ |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5631 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5632 | nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | { |
| 5634 | unsigned int strhashval; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5635 | struct nfs4_client_reclaim *crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5636 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5637 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
| 5638 | crp = alloc_reclaim(); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5639 | if (crp) { |
| 5640 | strhashval = clientstr_hashval(name); |
| 5641 | INIT_LIST_HEAD(&crp->cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5642 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5643 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5644 | crp->cr_clp = NULL; |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5645 | nn->reclaim_str_hashtbl_size++; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5646 | } |
| 5647 | return crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | } |
| 5649 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 5650 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5651 | 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] | 5652 | { |
| 5653 | list_del(&crp->cr_strhash); |
| 5654 | kfree(crp); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5655 | nn->reclaim_str_hashtbl_size--; |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 5656 | } |
| 5657 | |
| 5658 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5659 | nfs4_release_reclaim(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | { |
| 5661 | struct nfs4_client_reclaim *crp = NULL; |
| 5662 | int i; |
| 5663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5665 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 5666 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5667 | struct nfs4_client_reclaim, cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5668 | nfs4_remove_reclaim_record(crp, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | } |
| 5670 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 5671 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5672 | } |
| 5673 | |
| 5674 | /* |
| 5675 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 5676 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5677 | nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | { |
| 5679 | unsigned int strhashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | struct nfs4_client_reclaim *crp = NULL; |
| 5681 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5682 | dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5684 | strhashval = clientstr_hashval(recdir); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5685 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5686 | if (same_name(crp->cr_recdir, recdir)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | return crp; |
| 5688 | } |
| 5689 | } |
| 5690 | return NULL; |
| 5691 | } |
| 5692 | |
| 5693 | /* |
| 5694 | * Called from OPEN. Look for clientid in reclaim list. |
| 5695 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5696 | __be32 |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5697 | nfs4_check_open_reclaim(clientid_t *clid, |
| 5698 | struct nfsd4_compound_state *cstate, |
| 5699 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | { |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5701 | __be32 status; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 5702 | |
| 5703 | /* find clientid in conf_id_hashtbl */ |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5704 | status = lookup_clientid(clid, cstate, nn); |
| 5705 | if (status) |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 5706 | return nfserr_reclaim_bad; |
| 5707 | |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5708 | if (nfsd4_client_record_check(cstate->clp)) |
| 5709 | return nfserr_reclaim_bad; |
| 5710 | |
| 5711 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5712 | } |
| 5713 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5714 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
| 5715 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5716 | u64 nfsd_forget_client(struct nfs4_client *clp, u64 max) |
| 5717 | { |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 5718 | __be32 ret; |
| 5719 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 5720 | |
| 5721 | spin_lock(&nn->client_lock); |
| 5722 | ret = mark_client_expired_locked(clp); |
| 5723 | spin_unlock(&nn->client_lock); |
| 5724 | if (ret != nfs_ok) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 5725 | return 0; |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5726 | expire_client(clp); |
| 5727 | return 1; |
| 5728 | } |
| 5729 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5730 | u64 nfsd_print_client(struct nfs4_client *clp, u64 num) |
| 5731 | { |
| 5732 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 5733 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5734 | printk(KERN_INFO "NFS Client: %s\n", buf); |
| 5735 | return 1; |
| 5736 | } |
| 5737 | |
| 5738 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, |
| 5739 | const char *type) |
| 5740 | { |
| 5741 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 5742 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5743 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
| 5744 | } |
| 5745 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5746 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, |
| 5747 | void (*func)(struct nfs4_ol_stateid *)) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5748 | { |
| 5749 | struct nfs4_openowner *oop; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5750 | struct nfs4_ol_stateid *stp, *st_next; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5751 | struct nfs4_ol_stateid *lst, *lst_next; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5752 | u64 count = 0; |
| 5753 | |
| 5754 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5755 | list_for_each_entry_safe(stp, st_next, |
| 5756 | &oop->oo_owner.so_stateids, st_perstateowner) { |
| 5757 | list_for_each_entry_safe(lst, lst_next, |
| 5758 | &stp->st_locks, st_locks) { |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5759 | if (func) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5760 | func(lst); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5761 | if (++count == max) |
| 5762 | return count; |
| 5763 | } |
| 5764 | } |
| 5765 | } |
| 5766 | |
| 5767 | return count; |
| 5768 | } |
| 5769 | |
| 5770 | u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max) |
| 5771 | { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5772 | return nfsd_foreach_client_lock(clp, max, release_lock_stateid); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 5773 | } |
| 5774 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5775 | u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max) |
| 5776 | { |
| 5777 | u64 count = nfsd_foreach_client_lock(clp, max, NULL); |
| 5778 | nfsd_print_count(clp, count, "locked files"); |
| 5779 | return count; |
| 5780 | } |
| 5781 | |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 5782 | static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *)) |
| 5783 | { |
| 5784 | struct nfs4_openowner *oop, *next; |
| 5785 | u64 count = 0; |
| 5786 | |
| 5787 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { |
| 5788 | if (func) |
| 5789 | func(oop); |
| 5790 | if (++count == max) |
| 5791 | break; |
| 5792 | } |
| 5793 | |
| 5794 | return count; |
| 5795 | } |
| 5796 | |
| 5797 | u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max) |
| 5798 | { |
| 5799 | return nfsd_foreach_client_open(clp, max, release_openowner); |
| 5800 | } |
| 5801 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5802 | u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max) |
| 5803 | { |
| 5804 | u64 count = nfsd_foreach_client_open(clp, max, NULL); |
| 5805 | nfsd_print_count(clp, count, "open files"); |
| 5806 | return count; |
| 5807 | } |
| 5808 | |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5809 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
| 5810 | struct list_head *victims) |
| 5811 | { |
| 5812 | struct nfs4_delegation *dp, *next; |
| 5813 | u64 count = 0; |
| 5814 | |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5815 | lockdep_assert_held(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5816 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5817 | if (victims) { |
| 5818 | /* |
| 5819 | * It's not safe to mess with delegations that have a |
| 5820 | * non-zero dl_time. They might have already been broken |
| 5821 | * and could be processed by the laundromat outside of |
| 5822 | * the state_lock. Just leave them be. |
| 5823 | */ |
| 5824 | if (dp->dl_time != 0) |
| 5825 | continue; |
| 5826 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 5827 | unhash_delegation_locked(dp); |
| 5828 | list_add(&dp->dl_recall_lru, victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5829 | } |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5830 | if (++count == max) |
| 5831 | break; |
| 5832 | } |
| 5833 | return count; |
| 5834 | } |
| 5835 | |
| 5836 | u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max) |
| 5837 | { |
| 5838 | struct nfs4_delegation *dp, *next; |
| 5839 | LIST_HEAD(victims); |
| 5840 | u64 count; |
| 5841 | |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5842 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5843 | count = nfsd_find_all_delegations(clp, max, &victims); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5844 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5845 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5846 | list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) { |
| 5847 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5848 | revoke_delegation(dp); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5849 | } |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5850 | |
| 5851 | return count; |
| 5852 | } |
| 5853 | |
| 5854 | u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max) |
| 5855 | { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5856 | struct nfs4_delegation *dp; |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5857 | LIST_HEAD(victims); |
| 5858 | u64 count; |
| 5859 | |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5860 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5861 | count = nfsd_find_all_delegations(clp, max, &victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5862 | while (!list_empty(&victims)) { |
| 5863 | dp = list_first_entry(&victims, struct nfs4_delegation, |
| 5864 | dl_recall_lru); |
| 5865 | list_del_init(&dp->dl_recall_lru); |
| 5866 | dp->dl_time = 0; |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5867 | nfsd_break_one_deleg(dp); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 5868 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5869 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 5870 | |
| 5871 | return count; |
| 5872 | } |
| 5873 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5874 | u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max) |
| 5875 | { |
| 5876 | u64 count = 0; |
| 5877 | |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5878 | spin_lock(&state_lock); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5879 | count = nfsd_find_all_delegations(clp, max, NULL); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5880 | spin_unlock(&state_lock); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 5881 | |
| 5882 | nfsd_print_count(clp, count, "delegations"); |
| 5883 | return count; |
| 5884 | } |
| 5885 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5886 | 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] | 5887 | { |
| 5888 | struct nfs4_client *clp, *next; |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5889 | u64 count = 0; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5890 | 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] | 5891 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5892 | if (!nfsd_netns_ready(nn)) |
| 5893 | return 0; |
| 5894 | |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5895 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5896 | count += func(clp, max - count); |
| 5897 | if ((max != 0) && (count >= max)) |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5898 | break; |
| 5899 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5900 | |
Bryan Schumaker | 44e34da60 | 2012-11-29 11:40:39 -0500 | [diff] [blame] | 5901 | return count; |
| 5902 | } |
| 5903 | |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 5904 | struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) |
| 5905 | { |
| 5906 | struct nfs4_client *clp; |
| 5907 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id); |
| 5908 | |
| 5909 | if (!nfsd_netns_ready(nn)) |
| 5910 | return NULL; |
| 5911 | |
| 5912 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 5913 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) |
| 5914 | return clp; |
| 5915 | } |
| 5916 | return NULL; |
| 5917 | } |
| 5918 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5919 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
| 5920 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 5921 | /* |
| 5922 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 5923 | * client may quite reasonably hang on to a delegation as long as it has |
| 5924 | * the inode cached. This becomes an obvious problem the first time a |
| 5925 | * client's inode cache approaches the size of the server's total memory. |
| 5926 | * |
| 5927 | * For now we avoid this problem by imposing a hard limit on the number |
| 5928 | * of delegations, which varies according to the server's memory size. |
| 5929 | */ |
| 5930 | static void |
| 5931 | set_max_delegations(void) |
| 5932 | { |
| 5933 | /* |
| 5934 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 5935 | * estimates suggest that in the worst case (where every delegation |
| 5936 | * is for a different inode), a delegation could take about 1.5K, |
| 5937 | * giving a worst case usage of about 6% of memory. |
| 5938 | */ |
| 5939 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 5940 | } |
| 5941 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5942 | static int nfs4_state_create_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5943 | { |
| 5944 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 5945 | int i; |
| 5946 | |
| 5947 | nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5948 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 5949 | if (!nn->conf_id_hashtbl) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5950 | goto err; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5951 | nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5952 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 5953 | if (!nn->unconf_id_hashtbl) |
| 5954 | goto err_unconf_id; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5955 | nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) * |
| 5956 | SESSION_HASH_SIZE, GFP_KERNEL); |
| 5957 | if (!nn->sessionid_hashtbl) |
| 5958 | goto err_sessionid; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5959 | |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5960 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5961 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5962 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5963 | } |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5964 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 5965 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5966 | nn->conf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5967 | nn->unconf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5968 | INIT_LIST_HEAD(&nn->client_lru); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 5969 | INIT_LIST_HEAD(&nn->close_lru); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5970 | INIT_LIST_HEAD(&nn->del_recall_lru); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5971 | spin_lock_init(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5972 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5973 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 5974 | get_net(net); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5975 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5976 | return 0; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5977 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 5978 | err_sessionid: |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 5979 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 5980 | err_unconf_id: |
| 5981 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 5982 | err: |
| 5983 | return -ENOMEM; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5984 | } |
| 5985 | |
| 5986 | static void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 5987 | nfs4_state_destroy_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 5988 | { |
| 5989 | int i; |
| 5990 | struct nfs4_client *clp = NULL; |
| 5991 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 5992 | |
| 5993 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 5994 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 5995 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 5996 | destroy_client(clp); |
| 5997 | } |
| 5998 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 5999 | |
Kinglong Mee | 2b90563 | 2014-03-26 22:09:30 +0800 | [diff] [blame] | 6000 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 6001 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 6002 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 6003 | destroy_client(clp); |
| 6004 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 6005 | } |
| 6006 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 6007 | kfree(nn->sessionid_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 6008 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6009 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6010 | put_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6011 | } |
| 6012 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 6013 | int |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6014 | nfs4_state_start_net(struct net *net) |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 6015 | { |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 6016 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6017 | int ret; |
| 6018 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6019 | ret = nfs4_state_create_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6020 | if (ret) |
| 6021 | return ret; |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 6022 | nfsd4_client_tracking_init(net); |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 6023 | nn->boot_time = get_seconds(); |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 6024 | locks_start_grace(net, &nn->nfsd4_manager); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 6025 | nn->grace_ended = false; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6026 | printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n", |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 6027 | nn->nfsd4_grace, net); |
| 6028 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6029 | return 0; |
| 6030 | } |
| 6031 | |
| 6032 | /* initialization to perform when the nfsd service is started: */ |
| 6033 | |
| 6034 | int |
| 6035 | nfs4_state_start(void) |
| 6036 | { |
| 6037 | int ret; |
| 6038 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6039 | ret = set_callback_cred(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6040 | if (ret) |
| 6041 | return -ENOMEM; |
NeilBrown | 58da282 | 2005-06-23 22:03:19 -0700 | [diff] [blame] | 6042 | laundry_wq = create_singlethread_workqueue("nfsd4"); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 6043 | if (laundry_wq == NULL) { |
| 6044 | ret = -ENOMEM; |
| 6045 | goto out_recovery; |
| 6046 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6047 | ret = nfsd4_create_callback_queue(); |
| 6048 | if (ret) |
| 6049 | goto out_free_laundry; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 6050 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 6051 | set_max_delegations(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6052 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6053 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6054 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6055 | out_free_laundry: |
| 6056 | destroy_workqueue(laundry_wq); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 6057 | out_recovery: |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6058 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | } |
| 6060 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 6061 | void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6062 | nfs4_state_shutdown_net(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6066 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6068 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 6069 | locks_end_grace(&nn->nfsd4_manager); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 6070 | |
J. Bruce Fields | e50a26d | 2013-10-30 10:33:09 -0400 | [diff] [blame] | 6071 | nfs4_lock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 6073 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 6074 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 6076 | unhash_delegation_locked(dp); |
| 6077 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 6079 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | list_for_each_safe(pos, next, &reaplist) { |
| 6081 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 6082 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 6083 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | } |
| 6085 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6086 | nfsd4_client_tracking_exit(net); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6087 | nfs4_state_destroy_net(net); |
J. Bruce Fields | e50a26d | 2013-10-30 10:33:09 -0400 | [diff] [blame] | 6088 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6089 | } |
| 6090 | |
| 6091 | void |
| 6092 | nfs4_state_shutdown(void) |
| 6093 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 6094 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 6095 | nfsd4_destroy_callback_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6096 | } |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6097 | |
| 6098 | static void |
| 6099 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 6100 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 6101 | if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid)) |
| 6102 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6103 | } |
| 6104 | |
| 6105 | static void |
| 6106 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 6107 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 6108 | if (cstate->minorversion) { |
| 6109 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
| 6110 | SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
| 6111 | } |
| 6112 | } |
| 6113 | |
| 6114 | void |
| 6115 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 6116 | { |
| 6117 | CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6118 | } |
| 6119 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6120 | /* |
| 6121 | * functions to set current state id |
| 6122 | */ |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6123 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 6124 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 6125 | { |
| 6126 | put_stateid(cstate, &odp->od_stateid); |
| 6127 | } |
| 6128 | |
| 6129 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6130 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open) |
| 6131 | { |
| 6132 | put_stateid(cstate, &open->op_stateid); |
| 6133 | } |
| 6134 | |
| 6135 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6136 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 6137 | { |
| 6138 | put_stateid(cstate, &close->cl_stateid); |
| 6139 | } |
| 6140 | |
| 6141 | void |
| 6142 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock) |
| 6143 | { |
| 6144 | put_stateid(cstate, &lock->lk_resp_stateid); |
| 6145 | } |
| 6146 | |
| 6147 | /* |
| 6148 | * functions to consume current state id |
| 6149 | */ |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 6150 | |
| 6151 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 6152 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 6153 | { |
| 6154 | get_stateid(cstate, &odp->od_stateid); |
| 6155 | } |
| 6156 | |
| 6157 | void |
| 6158 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp) |
| 6159 | { |
| 6160 | get_stateid(cstate, &drp->dr_stateid); |
| 6161 | } |
| 6162 | |
| 6163 | void |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 6164 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp) |
| 6165 | { |
| 6166 | get_stateid(cstate, &fsp->fr_stateid); |
| 6167 | } |
| 6168 | |
| 6169 | void |
| 6170 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr) |
| 6171 | { |
| 6172 | get_stateid(cstate, &setattr->sa_stateid); |
| 6173 | } |
| 6174 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6175 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6176 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 6177 | { |
| 6178 | get_stateid(cstate, &close->cl_stateid); |
| 6179 | } |
| 6180 | |
| 6181 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6182 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6183 | { |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6184 | get_stateid(cstate, &locku->lu_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6185 | } |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 6186 | |
| 6187 | void |
| 6188 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read) |
| 6189 | { |
| 6190 | get_stateid(cstate, &read->rd_stateid); |
| 6191 | } |
| 6192 | |
| 6193 | void |
| 6194 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write) |
| 6195 | { |
| 6196 | get_stateid(cstate, &write->wr_stateid); |
| 6197 | } |