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> |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 44 | #include <linux/jhash.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" |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 51 | #include "pnfs.h" |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 54 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 55 | #define all_ones {{~0,~0},~0} |
| 56 | static const stateid_t one_stateid = { |
| 57 | .si_generation = ~0, |
| 58 | .si_opaque = all_ones, |
| 59 | }; |
| 60 | static const stateid_t zero_stateid = { |
| 61 | /* all fields zero */ |
| 62 | }; |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 63 | static const stateid_t currentstateid = { |
| 64 | .si_generation = 1, |
| 65 | }; |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 66 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 67 | static u64 current_sessionid = 1; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 68 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 69 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
| 70 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 71 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | /* forward declarations */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 74 | 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] | 75 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 77 | /* Locking: */ |
| 78 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 79 | /* |
| 80 | * Currently used for the del_recall_lru and file hash table. In an |
| 81 | * effort to decrease the scope of the client_mutex, this spinlock may |
| 82 | * eventually cover more: |
| 83 | */ |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 84 | static DEFINE_SPINLOCK(state_lock); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 85 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 86 | /* |
| 87 | * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for |
| 88 | * the refcount on the open stateid to drop. |
| 89 | */ |
| 90 | static DECLARE_WAIT_QUEUE_HEAD(close_wq); |
| 91 | |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 92 | static struct kmem_cache *openowner_slab; |
| 93 | static struct kmem_cache *lockowner_slab; |
| 94 | static struct kmem_cache *file_slab; |
| 95 | static struct kmem_cache *stateid_slab; |
| 96 | static struct kmem_cache *deleg_slab; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 97 | static struct kmem_cache *odstate_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 98 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 99 | static void free_session(struct nfsd4_session *); |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 100 | |
Julia Lawall | c4cb897 | 2015-11-21 22:57:39 +0100 | [diff] [blame] | 101 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 102 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 103 | static bool is_session_dead(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 104 | { |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 105 | return ses->se_flags & NFS4_SESSION_DEAD; |
| 106 | } |
| 107 | |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 108 | static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) |
| 109 | { |
| 110 | if (atomic_read(&ses->se_ref) > ref_held_by_me) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 111 | return nfserr_jukebox; |
| 112 | ses->se_flags |= NFS4_SESSION_DEAD; |
| 113 | return nfs_ok; |
| 114 | } |
| 115 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 116 | static bool is_client_expired(struct nfs4_client *clp) |
| 117 | { |
| 118 | return clp->cl_time == 0; |
| 119 | } |
| 120 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 121 | static __be32 get_client_locked(struct nfs4_client *clp) |
| 122 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 123 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 124 | |
| 125 | lockdep_assert_held(&nn->client_lock); |
| 126 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 127 | if (is_client_expired(clp)) |
| 128 | return nfserr_expired; |
| 129 | atomic_inc(&clp->cl_refcount); |
| 130 | return nfs_ok; |
| 131 | } |
| 132 | |
| 133 | /* must be called under the client_lock */ |
| 134 | static inline void |
| 135 | renew_client_locked(struct nfs4_client *clp) |
| 136 | { |
| 137 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 138 | |
| 139 | if (is_client_expired(clp)) { |
| 140 | WARN_ON(1); |
| 141 | printk("%s: client (clientid %08x/%08x) already expired\n", |
| 142 | __func__, |
| 143 | clp->cl_clientid.cl_boot, |
| 144 | clp->cl_clientid.cl_id); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | dprintk("renewing client (clientid %08x/%08x)\n", |
| 149 | clp->cl_clientid.cl_boot, |
| 150 | clp->cl_clientid.cl_id); |
| 151 | list_move_tail(&clp->cl_lru, &nn->client_lru); |
| 152 | clp->cl_time = get_seconds(); |
| 153 | } |
| 154 | |
Fengguang Wu | ba13843 | 2013-04-16 22:14:15 -0400 | [diff] [blame] | 155 | static void put_client_renew_locked(struct nfs4_client *clp) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 156 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 157 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 158 | |
| 159 | lockdep_assert_held(&nn->client_lock); |
| 160 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 161 | if (!atomic_dec_and_test(&clp->cl_refcount)) |
| 162 | return; |
| 163 | if (!is_client_expired(clp)) |
| 164 | renew_client_locked(clp); |
| 165 | } |
| 166 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 167 | static void put_client_renew(struct nfs4_client *clp) |
| 168 | { |
| 169 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 170 | |
Jeff Layton | d6c249b | 2014-07-08 14:02:50 -0400 | [diff] [blame] | 171 | if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock)) |
| 172 | return; |
| 173 | if (!is_client_expired(clp)) |
| 174 | renew_client_locked(clp); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 175 | spin_unlock(&nn->client_lock); |
| 176 | } |
| 177 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 178 | static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) |
| 179 | { |
| 180 | __be32 status; |
| 181 | |
| 182 | if (is_session_dead(ses)) |
| 183 | return nfserr_badsession; |
| 184 | status = get_client_locked(ses->se_client); |
| 185 | if (status) |
| 186 | return status; |
| 187 | atomic_inc(&ses->se_ref); |
| 188 | return nfs_ok; |
| 189 | } |
| 190 | |
| 191 | static void nfsd4_put_session_locked(struct nfsd4_session *ses) |
| 192 | { |
| 193 | struct nfs4_client *clp = ses->se_client; |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 194 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 195 | |
| 196 | lockdep_assert_held(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 197 | |
| 198 | if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) |
| 199 | free_session(ses); |
| 200 | put_client_renew_locked(clp); |
| 201 | } |
| 202 | |
| 203 | static void nfsd4_put_session(struct nfsd4_session *ses) |
| 204 | { |
| 205 | struct nfs4_client *clp = ses->se_client; |
| 206 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 207 | |
| 208 | spin_lock(&nn->client_lock); |
| 209 | nfsd4_put_session_locked(ses); |
| 210 | spin_unlock(&nn->client_lock); |
| 211 | } |
| 212 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 213 | static inline struct nfs4_stateowner * |
| 214 | nfs4_get_stateowner(struct nfs4_stateowner *sop) |
| 215 | { |
| 216 | atomic_inc(&sop->so_count); |
| 217 | return sop; |
| 218 | } |
| 219 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 220 | static int |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 221 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 222 | { |
| 223 | return (sop->so_owner.len == owner->len) && |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 224 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static struct nfs4_openowner * |
| 228 | find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 229 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 230 | { |
| 231 | struct nfs4_stateowner *so; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 232 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 233 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 234 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 235 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], |
| 236 | so_strhash) { |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 237 | if (!so->so_is_open_owner) |
| 238 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 239 | if (same_owner_str(so, &open->op_owner)) |
| 240 | return openowner(nfs4_get_stateowner(so)); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 241 | } |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static struct nfs4_openowner * |
| 246 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 247 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 248 | { |
| 249 | struct nfs4_openowner *oo; |
| 250 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 251 | spin_lock(&clp->cl_lock); |
| 252 | oo = find_openstateowner_str_locked(hashval, open, clp); |
| 253 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 254 | return oo; |
| 255 | } |
| 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | static inline u32 |
| 258 | opaque_hashval(const void *ptr, int nbytes) |
| 259 | { |
| 260 | unsigned char *cptr = (unsigned char *) ptr; |
| 261 | |
| 262 | u32 x = 0; |
| 263 | while (nbytes--) { |
| 264 | x *= 37; |
| 265 | x += *cptr++; |
| 266 | } |
| 267 | return x; |
| 268 | } |
| 269 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 270 | static void nfsd4_free_file_rcu(struct rcu_head *rcu) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 271 | { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 272 | struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); |
| 273 | |
| 274 | kmem_cache_free(file_slab, fp); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Christoph Hellwig | e6ba76e | 2014-08-14 08:50:16 +0200 | [diff] [blame] | 277 | void |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 278 | put_nfs4_file(struct nfs4_file *fi) |
| 279 | { |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 280 | might_lock(&state_lock); |
| 281 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 282 | if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 283 | hlist_del_rcu(&fi->fi_hash); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 284 | spin_unlock(&state_lock); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 285 | WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 286 | WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); |
| 287 | call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 288 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 291 | static struct file * |
| 292 | __nfs4_get_fd(struct nfs4_file *f, int oflag) |
| 293 | { |
| 294 | if (f->fi_fds[oflag]) |
| 295 | return get_file(f->fi_fds[oflag]); |
| 296 | return NULL; |
| 297 | } |
| 298 | |
| 299 | static struct file * |
| 300 | find_writeable_file_locked(struct nfs4_file *f) |
| 301 | { |
| 302 | struct file *ret; |
| 303 | |
| 304 | lockdep_assert_held(&f->fi_lock); |
| 305 | |
| 306 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 307 | if (!ret) |
| 308 | ret = __nfs4_get_fd(f, O_RDWR); |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | static struct file * |
| 313 | find_writeable_file(struct nfs4_file *f) |
| 314 | { |
| 315 | struct file *ret; |
| 316 | |
| 317 | spin_lock(&f->fi_lock); |
| 318 | ret = find_writeable_file_locked(f); |
| 319 | spin_unlock(&f->fi_lock); |
| 320 | |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | static struct file *find_readable_file_locked(struct nfs4_file *f) |
| 325 | { |
| 326 | struct file *ret; |
| 327 | |
| 328 | lockdep_assert_held(&f->fi_lock); |
| 329 | |
| 330 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 331 | if (!ret) |
| 332 | ret = __nfs4_get_fd(f, O_RDWR); |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | static struct file * |
| 337 | find_readable_file(struct nfs4_file *f) |
| 338 | { |
| 339 | struct file *ret; |
| 340 | |
| 341 | spin_lock(&f->fi_lock); |
| 342 | ret = find_readable_file_locked(f); |
| 343 | spin_unlock(&f->fi_lock); |
| 344 | |
| 345 | return ret; |
| 346 | } |
| 347 | |
Christoph Hellwig | 4d227fc | 2014-08-17 07:40:00 -0500 | [diff] [blame] | 348 | struct file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 349 | find_any_file(struct nfs4_file *f) |
| 350 | { |
| 351 | struct file *ret; |
| 352 | |
| 353 | spin_lock(&f->fi_lock); |
| 354 | ret = __nfs4_get_fd(f, O_RDWR); |
| 355 | if (!ret) { |
| 356 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 357 | if (!ret) |
| 358 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 359 | } |
| 360 | spin_unlock(&f->fi_lock); |
| 361 | return ret; |
| 362 | } |
| 363 | |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 364 | static atomic_long_t num_delegations; |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 365 | unsigned long max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 366 | |
| 367 | /* |
| 368 | * Open owner state (share locks) |
| 369 | */ |
| 370 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 371 | /* hash tables for lock and open owners */ |
| 372 | #define OWNER_HASH_BITS 8 |
| 373 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) |
| 374 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 375 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 376 | static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 377 | { |
| 378 | unsigned int ret; |
| 379 | |
| 380 | ret = opaque_hashval(ownername->data, ownername->len); |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 381 | return ret & OWNER_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 382 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 383 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 384 | /* hash table for nfs4_file */ |
| 385 | #define FILE_HASH_BITS 8 |
| 386 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
Shan Wei | 3507958 | 2011-01-14 17:35:59 +0800 | [diff] [blame] | 387 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 388 | static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 389 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 390 | return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0); |
| 391 | } |
| 392 | |
| 393 | static unsigned int file_hashval(struct knfsd_fh *fh) |
| 394 | { |
| 395 | return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1); |
| 396 | } |
| 397 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 398 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 399 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 400 | static void |
| 401 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 402 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 403 | lockdep_assert_held(&fp->fi_lock); |
| 404 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 405 | if (access & NFS4_SHARE_ACCESS_WRITE) |
| 406 | atomic_inc(&fp->fi_access[O_WRONLY]); |
| 407 | if (access & NFS4_SHARE_ACCESS_READ) |
| 408 | atomic_inc(&fp->fi_access[O_RDONLY]); |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 409 | } |
| 410 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 411 | static __be32 |
| 412 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 413 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 414 | lockdep_assert_held(&fp->fi_lock); |
| 415 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 416 | /* Does this access mode make sense? */ |
| 417 | if (access & ~NFS4_SHARE_ACCESS_BOTH) |
| 418 | return nfserr_inval; |
| 419 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 420 | /* Does it conflict with a deny mode already set? */ |
| 421 | if ((access & fp->fi_share_deny) != 0) |
| 422 | return nfserr_share_denied; |
| 423 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 424 | __nfs4_file_get_access(fp, access); |
| 425 | return nfs_ok; |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 426 | } |
| 427 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 428 | static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) |
| 429 | { |
| 430 | /* Common case is that there is no deny mode. */ |
| 431 | if (deny) { |
| 432 | /* Does this deny mode make sense? */ |
| 433 | if (deny & ~NFS4_SHARE_DENY_BOTH) |
| 434 | return nfserr_inval; |
| 435 | |
| 436 | if ((deny & NFS4_SHARE_DENY_READ) && |
| 437 | atomic_read(&fp->fi_access[O_RDONLY])) |
| 438 | return nfserr_share_denied; |
| 439 | |
| 440 | if ((deny & NFS4_SHARE_DENY_WRITE) && |
| 441 | atomic_read(&fp->fi_access[O_WRONLY])) |
| 442 | return nfserr_share_denied; |
| 443 | } |
| 444 | return nfs_ok; |
| 445 | } |
| 446 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 447 | 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] | 448 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 449 | might_lock(&fp->fi_lock); |
| 450 | |
| 451 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { |
| 452 | struct file *f1 = NULL; |
| 453 | struct file *f2 = NULL; |
| 454 | |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 455 | swap(f1, fp->fi_fds[oflag]); |
J. Bruce Fields | 0c7c3e6 | 2013-03-28 20:37:14 -0400 | [diff] [blame] | 456 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 457 | swap(f2, fp->fi_fds[O_RDWR]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 458 | spin_unlock(&fp->fi_lock); |
| 459 | if (f1) |
| 460 | fput(f1); |
| 461 | if (f2) |
| 462 | fput(f2); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 466 | 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] | 467 | { |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 468 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
| 469 | |
| 470 | if (access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 471 | __nfs4_file_put_access(fp, O_WRONLY); |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 472 | if (access & NFS4_SHARE_ACCESS_READ) |
| 473 | __nfs4_file_put_access(fp, O_RDONLY); |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 474 | } |
| 475 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 476 | /* |
| 477 | * Allocate a new open/delegation state counter. This is needed for |
| 478 | * pNFS for proper return on close semantics. |
| 479 | * |
| 480 | * Note that we only allocate it for pNFS-enabled exports, otherwise |
| 481 | * all pointers to struct nfs4_clnt_odstate are always NULL. |
| 482 | */ |
| 483 | static struct nfs4_clnt_odstate * |
| 484 | alloc_clnt_odstate(struct nfs4_client *clp) |
| 485 | { |
| 486 | struct nfs4_clnt_odstate *co; |
| 487 | |
| 488 | co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); |
| 489 | if (co) { |
| 490 | co->co_client = clp; |
| 491 | atomic_set(&co->co_odcount, 1); |
| 492 | } |
| 493 | return co; |
| 494 | } |
| 495 | |
| 496 | static void |
| 497 | hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) |
| 498 | { |
| 499 | struct nfs4_file *fp = co->co_file; |
| 500 | |
| 501 | lockdep_assert_held(&fp->fi_lock); |
| 502 | list_add(&co->co_perfile, &fp->fi_clnt_odstate); |
| 503 | } |
| 504 | |
| 505 | static inline void |
| 506 | get_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 507 | { |
| 508 | if (co) |
| 509 | atomic_inc(&co->co_odcount); |
| 510 | } |
| 511 | |
| 512 | static void |
| 513 | put_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 514 | { |
| 515 | struct nfs4_file *fp; |
| 516 | |
| 517 | if (!co) |
| 518 | return; |
| 519 | |
| 520 | fp = co->co_file; |
| 521 | if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { |
| 522 | list_del(&co->co_perfile); |
| 523 | spin_unlock(&fp->fi_lock); |
| 524 | |
| 525 | nfsd4_return_all_file_layouts(co->co_client, fp); |
| 526 | kmem_cache_free(odstate_slab, co); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | static struct nfs4_clnt_odstate * |
| 531 | find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) |
| 532 | { |
| 533 | struct nfs4_clnt_odstate *co; |
| 534 | struct nfs4_client *cl; |
| 535 | |
| 536 | if (!new) |
| 537 | return NULL; |
| 538 | |
| 539 | cl = new->co_client; |
| 540 | |
| 541 | spin_lock(&fp->fi_lock); |
| 542 | list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { |
| 543 | if (co->co_client == cl) { |
| 544 | get_clnt_odstate(co); |
| 545 | goto out; |
| 546 | } |
| 547 | } |
| 548 | co = new; |
| 549 | co->co_file = fp; |
| 550 | hash_clnt_odstate_locked(new); |
| 551 | out: |
| 552 | spin_unlock(&fp->fi_lock); |
| 553 | return co; |
| 554 | } |
| 555 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 556 | struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 557 | struct kmem_cache *slab) |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 558 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 559 | struct nfs4_stid *stid; |
| 560 | int new_id; |
| 561 | |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 562 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 563 | if (!stid) |
| 564 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 565 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 566 | idr_preload(GFP_KERNEL); |
| 567 | spin_lock(&cl->cl_lock); |
| 568 | new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT); |
| 569 | spin_unlock(&cl->cl_lock); |
| 570 | idr_preload_end(); |
Tejun Heo | ebd6c70 | 2013-03-13 14:59:37 -0700 | [diff] [blame] | 571 | if (new_id < 0) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 572 | goto out_free; |
| 573 | stid->sc_client = cl; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 574 | stid->sc_stateid.si_opaque.so_id = new_id; |
| 575 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; |
| 576 | /* Will be incremented before return to client: */ |
Trond Myklebust | 72c0b0f | 2014-07-21 09:34:58 -0400 | [diff] [blame] | 577 | atomic_set(&stid->sc_count, 1); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 578 | spin_lock_init(&stid->sc_lock); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 579 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 580 | /* |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 581 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 582 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 583 | * example, a stray write retransmission could be accepted by |
| 584 | * the server when it should have been rejected. Therefore, |
| 585 | * adopt a trick from the sctp code to attempt to maximize the |
| 586 | * amount of time until an id is reused, by ensuring they always |
| 587 | * "increase" (mod INT_MAX): |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 588 | */ |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 589 | return stid; |
| 590 | out_free: |
Wei Yongjun | 2c44a23 | 2013-04-09 14:15:31 +0800 | [diff] [blame] | 591 | kmem_cache_free(slab, stid); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 592 | return NULL; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 593 | } |
| 594 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 595 | 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] | 596 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 597 | struct nfs4_stid *stid; |
| 598 | struct nfs4_ol_stateid *stp; |
| 599 | |
| 600 | stid = nfs4_alloc_stid(clp, stateid_slab); |
| 601 | if (!stid) |
| 602 | return NULL; |
| 603 | |
| 604 | stp = openlockstateid(stid); |
| 605 | stp->st_stid.sc_free = nfs4_free_ol_stateid; |
| 606 | return stp; |
| 607 | } |
| 608 | |
| 609 | static void nfs4_free_deleg(struct nfs4_stid *stid) |
| 610 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 611 | kmem_cache_free(deleg_slab, stid); |
| 612 | atomic_long_dec(&num_delegations); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 613 | } |
| 614 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 615 | /* |
| 616 | * When we recall a delegation, we should be careful not to hand it |
| 617 | * out again straight away. |
| 618 | * To ensure this we keep a pair of bloom filters ('new' and 'old') |
| 619 | * in which the filehandles of recalled delegations are "stored". |
| 620 | * If a filehandle appear in either filter, a delegation is blocked. |
| 621 | * When a delegation is recalled, the filehandle is stored in the "new" |
| 622 | * filter. |
| 623 | * Every 30 seconds we swap the filters and clear the "new" one, |
| 624 | * unless both are empty of course. |
| 625 | * |
| 626 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the |
| 627 | * low 3 bytes as hash-table indices. |
| 628 | * |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 629 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 630 | * is used to manage concurrent access. Testing does not need the lock |
| 631 | * except when swapping the two filters. |
| 632 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 633 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 634 | static struct bloom_pair { |
| 635 | int entries, old_entries; |
| 636 | time_t swap_time; |
| 637 | int new; /* index into 'set' */ |
| 638 | DECLARE_BITMAP(set[2], 256); |
| 639 | } blocked_delegations; |
| 640 | |
| 641 | static int delegation_blocked(struct knfsd_fh *fh) |
| 642 | { |
| 643 | u32 hash; |
| 644 | struct bloom_pair *bd = &blocked_delegations; |
| 645 | |
| 646 | if (bd->entries == 0) |
| 647 | return 0; |
| 648 | if (seconds_since_boot() - bd->swap_time > 30) { |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 649 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 650 | if (seconds_since_boot() - bd->swap_time > 30) { |
| 651 | bd->entries -= bd->old_entries; |
| 652 | bd->old_entries = bd->entries; |
| 653 | memset(bd->set[bd->new], 0, |
| 654 | sizeof(bd->set[0])); |
| 655 | bd->new = 1-bd->new; |
| 656 | bd->swap_time = seconds_since_boot(); |
| 657 | } |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 658 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 659 | } |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 660 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 661 | if (test_bit(hash&255, bd->set[0]) && |
| 662 | test_bit((hash>>8)&255, bd->set[0]) && |
| 663 | test_bit((hash>>16)&255, bd->set[0])) |
| 664 | return 1; |
| 665 | |
| 666 | if (test_bit(hash&255, bd->set[1]) && |
| 667 | test_bit((hash>>8)&255, bd->set[1]) && |
| 668 | test_bit((hash>>16)&255, bd->set[1])) |
| 669 | return 1; |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | static void block_delegations(struct knfsd_fh *fh) |
| 675 | { |
| 676 | u32 hash; |
| 677 | struct bloom_pair *bd = &blocked_delegations; |
| 678 | |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 679 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 680 | |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 681 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 682 | __set_bit(hash&255, bd->set[bd->new]); |
| 683 | __set_bit((hash>>8)&255, bd->set[bd->new]); |
| 684 | __set_bit((hash>>16)&255, bd->set[bd->new]); |
| 685 | if (bd->entries == 0) |
| 686 | bd->swap_time = seconds_since_boot(); |
| 687 | bd->entries += 1; |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 688 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 689 | } |
| 690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | static struct nfs4_delegation * |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 692 | alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh, |
| 693 | struct nfs4_clnt_odstate *odstate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
| 695 | struct nfs4_delegation *dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 696 | long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
| 698 | dprintk("NFSD alloc_init_deleg\n"); |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 699 | n = atomic_long_inc_return(&num_delegations); |
| 700 | if (n < 0 || n > max_delegations) |
| 701 | goto out_dec; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 702 | if (delegation_blocked(¤t_fh->fh_handle)) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 703 | goto out_dec; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 704 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab)); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 705 | if (dp == NULL) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 706 | goto out_dec; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 707 | |
| 708 | dp->dl_stid.sc_free = nfs4_free_deleg; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 709 | /* |
| 710 | * delegation seqid's are never incremented. The 4.1 special |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 711 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 712 | * 0 anyway just for consistency and use 1: |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 713 | */ |
| 714 | dp->dl_stid.sc_stateid.si_generation = 1; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 715 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 716 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 718 | dp->dl_clnt_odstate = odstate; |
| 719 | get_clnt_odstate(odstate); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 720 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 721 | dp->dl_retries = 1; |
| 722 | nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 723 | &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | return dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 725 | out_dec: |
| 726 | atomic_long_dec(&num_delegations); |
| 727 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | void |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 731 | nfs4_put_stid(struct nfs4_stid *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 733 | struct nfs4_file *fp = s->sc_file; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 734 | struct nfs4_client *clp = s->sc_client; |
| 735 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 736 | might_lock(&clp->cl_lock); |
| 737 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 738 | if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) { |
| 739 | wake_up_all(&close_wq); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 740 | return; |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 741 | } |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 742 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 743 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 744 | s->sc_free(s); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 745 | if (fp) |
| 746 | put_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 749 | void |
| 750 | nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) |
| 751 | { |
| 752 | stateid_t *src = &stid->sc_stateid; |
| 753 | |
| 754 | spin_lock(&stid->sc_lock); |
| 755 | if (unlikely(++src->si_generation == 0)) |
| 756 | src->si_generation = 1; |
| 757 | memcpy(dst, src, sizeof(*dst)); |
| 758 | spin_unlock(&stid->sc_lock); |
| 759 | } |
| 760 | |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 761 | static void nfs4_put_deleg_lease(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 763 | struct file *filp = NULL; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 764 | |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 765 | spin_lock(&fp->fi_lock); |
Jeff Layton | 67db103 | 2014-12-13 09:11:40 -0500 | [diff] [blame] | 766 | if (fp->fi_deleg_file && --fp->fi_delegees == 0) |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 767 | swap(filp, fp->fi_deleg_file); |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 768 | spin_unlock(&fp->fi_lock); |
| 769 | |
| 770 | if (filp) { |
Christoph Hellwig | 2ab99ee | 2015-01-21 19:14:02 +0100 | [diff] [blame] | 771 | vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp); |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 772 | fput(filp); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 773 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 776 | void nfs4_unhash_stid(struct nfs4_stid *s) |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 777 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 778 | s->sc_type = 0; |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 779 | } |
| 780 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 781 | /** |
| 782 | * nfs4_get_existing_delegation - Discover if this delegation already exists |
| 783 | * @clp: a pointer to the nfs4_client we're granting a delegation to |
| 784 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 785 | * |
| 786 | * Return: |
| 787 | * On success: NULL if an existing delegation was not found. |
| 788 | * |
| 789 | * On error: -EAGAIN if one was previously granted to this nfs4_client |
| 790 | * for this nfs4_file. |
| 791 | * |
| 792 | */ |
| 793 | |
| 794 | static int |
| 795 | nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp) |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 796 | { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 797 | struct nfs4_delegation *searchdp = NULL; |
| 798 | struct nfs4_client *searchclp = NULL; |
| 799 | |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 800 | lockdep_assert_held(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 801 | lockdep_assert_held(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 802 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 803 | list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { |
| 804 | searchclp = searchdp->dl_stid.sc_client; |
| 805 | if (clp == searchclp) { |
| 806 | return -EAGAIN; |
| 807 | } |
| 808 | } |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * hash_delegation_locked - Add a delegation to the appropriate lists |
| 814 | * @dp: a pointer to the nfs4_delegation we are adding. |
| 815 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 816 | * |
| 817 | * Return: |
| 818 | * On success: NULL if the delegation was successfully hashed. |
| 819 | * |
| 820 | * On error: -EAGAIN if one was previously granted to this |
| 821 | * nfs4_client for this nfs4_file. Delegation is not hashed. |
| 822 | * |
| 823 | */ |
| 824 | |
| 825 | static int |
| 826 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 827 | { |
| 828 | int status; |
| 829 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 830 | |
| 831 | lockdep_assert_held(&state_lock); |
| 832 | lockdep_assert_held(&fp->fi_lock); |
| 833 | |
| 834 | status = nfs4_get_existing_delegation(clp, fp); |
| 835 | if (status) |
| 836 | return status; |
| 837 | ++fp->fi_delegees; |
Trond Myklebust | 67cb127 | 2014-07-29 21:34:17 -0400 | [diff] [blame] | 838 | atomic_inc(&dp->dl_stid.sc_count); |
Benny Halevy | 3fb87d1 | 2014-05-30 09:09:31 -0400 | [diff] [blame] | 839 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 840 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 841 | list_add(&dp->dl_perclnt, &clp->cl_delegations); |
| 842 | return 0; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 843 | } |
| 844 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 845 | static bool |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 846 | unhash_delegation_locked(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 848 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 849 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 850 | lockdep_assert_held(&state_lock); |
| 851 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 852 | if (list_empty(&dp->dl_perfile)) |
| 853 | return false; |
| 854 | |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 855 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
Jeff Layton | d55a166 | 2014-07-22 13:52:06 -0400 | [diff] [blame] | 856 | /* Ensure that deleg break won't try to requeue it */ |
| 857 | ++dp->dl_time; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 858 | spin_lock(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 859 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | list_del_init(&dp->dl_recall_lru); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 861 | list_del_init(&dp->dl_perfile); |
| 862 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 863 | return true; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 864 | } |
| 865 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 866 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 867 | { |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 868 | bool unhashed; |
| 869 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 870 | spin_lock(&state_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 871 | unhashed = unhash_delegation_locked(dp); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 872 | spin_unlock(&state_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 873 | if (unhashed) { |
| 874 | put_clnt_odstate(dp->dl_clnt_odstate); |
| 875 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
| 876 | nfs4_put_stid(&dp->dl_stid); |
| 877 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 881 | { |
| 882 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 883 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 884 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
| 885 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 886 | put_clnt_odstate(dp->dl_clnt_odstate); |
Jeff Layton | afbda40 | 2014-08-09 10:22:41 -0400 | [diff] [blame] | 887 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
| 888 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 889 | if (clp->cl_minorversion == 0) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 890 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 891 | else { |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 892 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 893 | spin_lock(&clp->cl_lock); |
| 894 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 895 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | /* |
| 900 | * SETCLIENTID state |
| 901 | */ |
| 902 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 903 | static unsigned int clientid_hashval(u32 id) |
| 904 | { |
| 905 | return id & CLIENT_HASH_MASK; |
| 906 | } |
| 907 | |
| 908 | static unsigned int clientstr_hashval(const char *name) |
| 909 | { |
| 910 | return opaque_hashval(name, 8) & CLIENT_HASH_MASK; |
| 911 | } |
| 912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | /* |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 914 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 915 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 916 | * only what share bits are currently in force, but also what |
| 917 | * combinations of share bits previous opens have used. This allows us |
| 918 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 919 | * return an error if the client attempt to downgrade to a combination |
| 920 | * of share bits not explicable by closing some of its previous opens. |
| 921 | * |
| 922 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 923 | * track of access/deny bit combinations; so, e.g., we allow: |
| 924 | * |
| 925 | * OPEN allow read, deny write |
| 926 | * OPEN allow both, deny none |
| 927 | * DOWNGRADE allow read, deny none |
| 928 | * |
| 929 | * which we should reject. |
| 930 | */ |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 931 | static unsigned int |
| 932 | bmap_to_share_mode(unsigned long bmap) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 933 | int i; |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 934 | unsigned int access = 0; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 935 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 936 | for (i = 1; i < 4; i++) { |
| 937 | if (test_bit(i, &bmap)) |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 938 | access |= i; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 939 | } |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 940 | return access; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 941 | } |
| 942 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 943 | /* set share access for a given stateid */ |
| 944 | static inline void |
| 945 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 946 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 947 | unsigned char mask = 1 << access; |
| 948 | |
| 949 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 950 | stp->st_access_bmap |= mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | /* clear share access for a given stateid */ |
| 954 | static inline void |
| 955 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 956 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 957 | unsigned char mask = 1 << access; |
| 958 | |
| 959 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 960 | stp->st_access_bmap &= ~mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /* test whether a given stateid has access */ |
| 964 | static inline bool |
| 965 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 966 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 967 | unsigned char mask = 1 << access; |
| 968 | |
| 969 | return (bool)(stp->st_access_bmap & mask); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 970 | } |
| 971 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 972 | /* set share deny for a given stateid */ |
| 973 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 974 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 975 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 976 | unsigned char mask = 1 << deny; |
| 977 | |
| 978 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 979 | stp->st_deny_bmap |= mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /* clear share deny for a given stateid */ |
| 983 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 984 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 985 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 986 | unsigned char mask = 1 << deny; |
| 987 | |
| 988 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 989 | stp->st_deny_bmap &= ~mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /* test whether a given stateid is denying specific access */ |
| 993 | static inline bool |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 994 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 995 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 996 | unsigned char mask = 1 << deny; |
| 997 | |
| 998 | return (bool)(stp->st_deny_bmap & mask); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | static int nfs4_access_to_omode(u32 access) |
| 1002 | { |
J. Bruce Fields | 8f34a43 | 2010-09-02 15:23:16 -0400 | [diff] [blame] | 1003 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1004 | case NFS4_SHARE_ACCESS_READ: |
| 1005 | return O_RDONLY; |
| 1006 | case NFS4_SHARE_ACCESS_WRITE: |
| 1007 | return O_WRONLY; |
| 1008 | case NFS4_SHARE_ACCESS_BOTH: |
| 1009 | return O_RDWR; |
| 1010 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 1011 | WARN_ON_ONCE(1); |
| 1012 | return O_RDONLY; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1013 | } |
| 1014 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1015 | /* |
| 1016 | * A stateid that had a deny mode associated with it is being released |
| 1017 | * or downgraded. Recalculate the deny mode on the file. |
| 1018 | */ |
| 1019 | static void |
| 1020 | recalculate_deny_mode(struct nfs4_file *fp) |
| 1021 | { |
| 1022 | struct nfs4_ol_stateid *stp; |
| 1023 | |
| 1024 | spin_lock(&fp->fi_lock); |
| 1025 | fp->fi_share_deny = 0; |
| 1026 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) |
| 1027 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); |
| 1028 | spin_unlock(&fp->fi_lock); |
| 1029 | } |
| 1030 | |
| 1031 | static void |
| 1032 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 1033 | { |
| 1034 | int i; |
| 1035 | bool change = false; |
| 1036 | |
| 1037 | for (i = 1; i < 4; i++) { |
| 1038 | if ((i & deny) != i) { |
| 1039 | change = true; |
| 1040 | clear_deny(i, stp); |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | /* Recalculate per-file deny mode if there was a change */ |
| 1045 | if (change) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1046 | recalculate_deny_mode(stp->st_stid.sc_file); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1047 | } |
| 1048 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1049 | /* release all access and file references for a given stateid */ |
| 1050 | static void |
| 1051 | release_all_access(struct nfs4_ol_stateid *stp) |
| 1052 | { |
| 1053 | int i; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1054 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1055 | |
| 1056 | if (fp && stp->st_deny_bmap != 0) |
| 1057 | recalculate_deny_mode(fp); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1058 | |
| 1059 | for (i = 1; i < 4; i++) { |
| 1060 | if (test_access(i, stp)) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1061 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1062 | clear_access(i, stp); |
| 1063 | } |
| 1064 | } |
| 1065 | |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1066 | static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) |
| 1067 | { |
| 1068 | kfree(sop->so_owner.data); |
| 1069 | sop->so_ops->so_free(sop); |
| 1070 | } |
| 1071 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1072 | static void nfs4_put_stateowner(struct nfs4_stateowner *sop) |
| 1073 | { |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1074 | struct nfs4_client *clp = sop->so_client; |
| 1075 | |
| 1076 | might_lock(&clp->cl_lock); |
| 1077 | |
| 1078 | if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1079 | return; |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1080 | sop->so_ops->so_unhash(sop); |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1081 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1082 | nfs4_free_stateowner(sop); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1083 | } |
| 1084 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1085 | static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1086 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1087 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1088 | |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1089 | lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); |
| 1090 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1091 | if (list_empty(&stp->st_perfile)) |
| 1092 | return false; |
| 1093 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1094 | spin_lock(&fp->fi_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1095 | list_del_init(&stp->st_perfile); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1096 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1097 | list_del(&stp->st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1098 | return true; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1099 | } |
| 1100 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1101 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1102 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1103 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 1104 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 1105 | put_clnt_odstate(stp->st_clnt_odstate); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1106 | release_all_access(stp); |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1107 | if (stp->st_stateowner) |
| 1108 | nfs4_put_stateowner(stp->st_stateowner); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1109 | kmem_cache_free(stateid_slab, stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1110 | } |
| 1111 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1112 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1113 | { |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1114 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
| 1115 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1116 | struct file *file; |
| 1117 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1118 | file = find_any_file(stp->st_stid.sc_file); |
| 1119 | if (file) |
| 1120 | filp_close(file, (fl_owner_t)lo); |
| 1121 | nfs4_free_ol_stateid(stid); |
| 1122 | } |
| 1123 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1124 | /* |
| 1125 | * Put the persistent reference to an already unhashed generic stateid, while |
| 1126 | * holding the cl_lock. If it's the last reference, then put it onto the |
| 1127 | * reaplist for later destruction. |
| 1128 | */ |
| 1129 | static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, |
| 1130 | struct list_head *reaplist) |
| 1131 | { |
| 1132 | struct nfs4_stid *s = &stp->st_stid; |
| 1133 | struct nfs4_client *clp = s->sc_client; |
| 1134 | |
| 1135 | lockdep_assert_held(&clp->cl_lock); |
| 1136 | |
| 1137 | WARN_ON_ONCE(!list_empty(&stp->st_locks)); |
| 1138 | |
| 1139 | if (!atomic_dec_and_test(&s->sc_count)) { |
| 1140 | wake_up_all(&close_wq); |
| 1141 | return; |
| 1142 | } |
| 1143 | |
| 1144 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
| 1145 | list_add(&stp->st_locks, reaplist); |
| 1146 | } |
| 1147 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1148 | static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1149 | { |
| 1150 | struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner); |
| 1151 | |
| 1152 | lockdep_assert_held(&oo->oo_owner.so_client->cl_lock); |
| 1153 | |
| 1154 | list_del_init(&stp->st_locks); |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 1155 | nfs4_unhash_stid(&stp->st_stid); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1156 | return unhash_ol_stateid(stp); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1157 | } |
| 1158 | |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 1159 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1160 | { |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1161 | struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1162 | bool unhashed; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1163 | |
| 1164 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1165 | unhashed = unhash_lock_stateid(stp); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1166 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1167 | if (unhashed) |
| 1168 | nfs4_put_stid(&stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1169 | } |
| 1170 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1171 | static void unhash_lockowner_locked(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1172 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1173 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1174 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1175 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1176 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1177 | list_del_init(&lo->lo_owner.so_strhash); |
| 1178 | } |
| 1179 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1180 | /* |
| 1181 | * Free a list of generic stateids that were collected earlier after being |
| 1182 | * fully unhashed. |
| 1183 | */ |
| 1184 | static void |
| 1185 | free_ol_stateid_reaplist(struct list_head *reaplist) |
| 1186 | { |
| 1187 | struct nfs4_ol_stateid *stp; |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1188 | struct nfs4_file *fp; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1189 | |
| 1190 | might_sleep(); |
| 1191 | |
| 1192 | while (!list_empty(reaplist)) { |
| 1193 | stp = list_first_entry(reaplist, struct nfs4_ol_stateid, |
| 1194 | st_locks); |
| 1195 | list_del(&stp->st_locks); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1196 | fp = stp->st_stid.sc_file; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1197 | stp->st_stid.sc_free(&stp->st_stid); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1198 | if (fp) |
| 1199 | put_nfs4_file(fp); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1203 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, |
| 1204 | struct list_head *reaplist) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1205 | { |
| 1206 | struct nfs4_ol_stateid *stp; |
| 1207 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1208 | lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); |
| 1209 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1210 | while (!list_empty(&open_stp->st_locks)) { |
| 1211 | stp = list_entry(open_stp->st_locks.next, |
| 1212 | struct nfs4_ol_stateid, st_locks); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1213 | WARN_ON(!unhash_lock_stateid(stp)); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1214 | put_ol_stateid_locked(stp, reaplist); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1218 | static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1219 | struct list_head *reaplist) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1220 | { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1221 | bool unhashed; |
| 1222 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1223 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
| 1224 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1225 | unhashed = unhash_ol_stateid(stp); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1226 | release_open_stateid_locks(stp, reaplist); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1227 | return unhashed; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 1231 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1232 | LIST_HEAD(reaplist); |
| 1233 | |
| 1234 | spin_lock(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1235 | if (unhash_open_stateid(stp, &reaplist)) |
| 1236 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1237 | spin_unlock(&stp->st_stid.sc_client->cl_lock); |
| 1238 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1239 | } |
| 1240 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1241 | static void unhash_openowner_locked(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1242 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1243 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1244 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1245 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1246 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1247 | list_del_init(&oo->oo_owner.so_strhash); |
| 1248 | list_del_init(&oo->oo_perclient); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1249 | } |
| 1250 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1251 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 1252 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1253 | struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, |
| 1254 | nfsd_net_id); |
| 1255 | struct nfs4_ol_stateid *s; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1256 | |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1257 | spin_lock(&nn->client_lock); |
| 1258 | s = oo->oo_last_closed_stid; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1259 | if (s) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1260 | list_del_init(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1261 | oo->oo_last_closed_stid = NULL; |
| 1262 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1263 | spin_unlock(&nn->client_lock); |
| 1264 | if (s) |
| 1265 | nfs4_put_stid(&s->st_stid); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1266 | } |
| 1267 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1268 | static void release_openowner(struct nfs4_openowner *oo) |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1269 | { |
| 1270 | struct nfs4_ol_stateid *stp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1271 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1272 | struct list_head reaplist; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1273 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1274 | INIT_LIST_HEAD(&reaplist); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1275 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1276 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1277 | unhash_openowner_locked(oo); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1278 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 1279 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
| 1280 | struct nfs4_ol_stateid, st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1281 | if (unhash_open_stateid(stp, &reaplist)) |
| 1282 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1283 | } |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1284 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1285 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1286 | release_last_closed_stateid(oo); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1287 | nfs4_put_stateowner(&oo->oo_owner); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1288 | } |
| 1289 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1290 | static inline int |
| 1291 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 1292 | { |
| 1293 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 1294 | |
| 1295 | return sid->sequence % SESSION_HASH_SIZE; |
| 1296 | } |
| 1297 | |
Mark Salter | 135dd00 | 2015-04-06 09:46:00 -0400 | [diff] [blame] | 1298 | #ifdef CONFIG_SUNRPC_DEBUG |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1299 | static inline void |
| 1300 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1301 | { |
| 1302 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 1303 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 1304 | } |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1305 | #else |
| 1306 | static inline void |
| 1307 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1308 | { |
| 1309 | } |
| 1310 | #endif |
| 1311 | |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1312 | /* |
| 1313 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 1314 | * won't be used for replay. |
| 1315 | */ |
| 1316 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 1317 | { |
| 1318 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 1319 | |
| 1320 | if (nfserr == nfserr_replay_me) |
| 1321 | return; |
| 1322 | |
| 1323 | if (!seqid_mutating_err(ntohl(nfserr))) { |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 1324 | nfsd4_cstate_clear_replay(cstate); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1325 | return; |
| 1326 | } |
| 1327 | if (!so) |
| 1328 | return; |
| 1329 | if (so->so_is_open_owner) |
| 1330 | release_last_closed_stateid(openowner(so)); |
| 1331 | so->so_seqid++; |
| 1332 | return; |
| 1333 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1334 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1335 | static void |
| 1336 | gen_sessionid(struct nfsd4_session *ses) |
| 1337 | { |
| 1338 | struct nfs4_client *clp = ses->se_client; |
| 1339 | struct nfsd4_sessionid *sid; |
| 1340 | |
| 1341 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 1342 | sid->clientid = clp->cl_clientid; |
| 1343 | sid->sequence = current_sessionid++; |
| 1344 | sid->reserved = 0; |
| 1345 | } |
| 1346 | |
| 1347 | /* |
Andy Adamson | a649637 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1348 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 1349 | * the rpc header, but all we need to cache is the data starting after |
| 1350 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 1351 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 1352 | * value that is the number of bytes in our cache plus a few additional |
| 1353 | * bytes. In order to stay on the safe side, and not promise more than |
| 1354 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 1355 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 1356 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 1357 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1358 | */ |
Andy Adamson | a649637 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1359 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 1360 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1361 | static void |
| 1362 | free_session_slots(struct nfsd4_session *ses) |
| 1363 | { |
| 1364 | int i; |
| 1365 | |
| 1366 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) |
| 1367 | kfree(ses->se_slots[i]); |
| 1368 | } |
| 1369 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1370 | /* |
| 1371 | * We don't actually need to cache the rpc and session headers, so we |
| 1372 | * can allocate a little less for each slot: |
| 1373 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1374 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1375 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1376 | u32 size; |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1377 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1378 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 1379 | size = 0; |
| 1380 | else |
| 1381 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 1382 | return size + sizeof(struct nfsd4_slot); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1383 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1384 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1385 | /* |
| 1386 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 1387 | * re-negotiate active sessions and reduce their slot usage to make |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 1388 | * 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] | 1389 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1390 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1391 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1392 | u32 slotsize = slot_bytes(ca); |
| 1393 | u32 num = ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1394 | int avail; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1395 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1396 | spin_lock(&nfsd_drc_lock); |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 1397 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, |
| 1398 | nfsd_drc_max_mem - nfsd_drc_mem_used); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1399 | num = min_t(int, num, avail / slotsize); |
| 1400 | nfsd_drc_mem_used += num * slotsize; |
| 1401 | spin_unlock(&nfsd_drc_lock); |
| 1402 | |
| 1403 | return num; |
| 1404 | } |
| 1405 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1406 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1407 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1408 | int slotsize = slot_bytes(ca); |
| 1409 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1410 | spin_lock(&nfsd_drc_lock); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1411 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1412 | spin_unlock(&nfsd_drc_lock); |
| 1413 | } |
| 1414 | |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1415 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 1416 | struct nfsd4_channel_attrs *battrs) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1417 | { |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1418 | int numslots = fattrs->maxreqs; |
| 1419 | int slotsize = slot_bytes(fattrs); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1420 | struct nfsd4_session *new; |
| 1421 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1422 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 1423 | 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] | 1424 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 1425 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1426 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1427 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1428 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1429 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1430 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1431 | for (i = 0; i < numslots; i++) { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1432 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1433 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1434 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1435 | } |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1436 | |
| 1437 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 1438 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 1439 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1440 | return new; |
| 1441 | out_free: |
| 1442 | while (i--) |
| 1443 | kfree(new->se_slots[i]); |
| 1444 | kfree(new); |
| 1445 | return NULL; |
| 1446 | } |
| 1447 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1448 | static void free_conn(struct nfsd4_conn *c) |
| 1449 | { |
| 1450 | svc_xprt_put(c->cn_xprt); |
| 1451 | kfree(c); |
| 1452 | } |
| 1453 | |
| 1454 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 1455 | { |
| 1456 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 1457 | struct nfs4_client *clp = c->cn_session->se_client; |
| 1458 | |
| 1459 | spin_lock(&clp->cl_lock); |
| 1460 | if (!list_empty(&c->cn_persession)) { |
| 1461 | list_del(&c->cn_persession); |
| 1462 | free_conn(c); |
| 1463 | } |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 1464 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 2e4b723 | 2013-03-08 09:30:43 -0500 | [diff] [blame] | 1465 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1466 | } |
| 1467 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1468 | 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] | 1469 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1470 | struct nfsd4_conn *conn; |
| 1471 | |
| 1472 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 1473 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1474 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1475 | svc_xprt_get(rqstp->rq_xprt); |
| 1476 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1477 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1478 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1479 | return conn; |
| 1480 | } |
| 1481 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1482 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1483 | { |
| 1484 | conn->cn_session = ses; |
| 1485 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1486 | } |
| 1487 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1488 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1489 | { |
| 1490 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1491 | |
| 1492 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1493 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1494 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1495 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1496 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1497 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1498 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1499 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1500 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1501 | } |
| 1502 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1503 | 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] | 1504 | { |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1505 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1506 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1507 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1508 | ret = nfsd4_register_conn(conn); |
| 1509 | if (ret) |
| 1510 | /* oops; xprt is already down: */ |
| 1511 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | 57a3714 | 2014-07-18 15:06:47 -0400 | [diff] [blame] | 1512 | /* We may have gained or lost a callback channel: */ |
| 1513 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1514 | } |
| 1515 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1516 | 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] | 1517 | { |
| 1518 | u32 dir = NFS4_CDFC4_FORE; |
| 1519 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1520 | if (cses->flags & SESSION4_BACK_CHAN) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1521 | dir |= NFS4_CDFC4_BACK; |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1522 | return alloc_conn(rqstp, dir); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1526 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1527 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1528 | struct nfs4_client *clp = s->se_client; |
| 1529 | struct nfsd4_conn *c; |
| 1530 | |
| 1531 | spin_lock(&clp->cl_lock); |
| 1532 | while (!list_empty(&s->se_conns)) { |
| 1533 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1534 | list_del_init(&c->cn_persession); |
| 1535 | spin_unlock(&clp->cl_lock); |
| 1536 | |
| 1537 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1538 | free_conn(c); |
| 1539 | |
| 1540 | spin_lock(&clp->cl_lock); |
| 1541 | } |
| 1542 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1543 | } |
| 1544 | |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1545 | static void __free_session(struct nfsd4_session *ses) |
| 1546 | { |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1547 | free_session_slots(ses); |
| 1548 | kfree(ses); |
| 1549 | } |
| 1550 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1551 | static void free_session(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 1552 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1553 | nfsd4_del_conns(ses); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1554 | nfsd4_put_drc_mem(&ses->se_fchannel); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1555 | __free_session(ses); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1556 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1557 | |
Fengguang Wu | 135ae82 | 2012-11-10 07:20:25 -0500 | [diff] [blame] | 1558 | 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] | 1559 | { |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1560 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1561 | 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] | 1562 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1563 | new->se_client = clp; |
| 1564 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1565 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1566 | INIT_LIST_HEAD(&new->se_conns); |
| 1567 | |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1568 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1569 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 1570 | new->se_cb_prog = cses->callback_prog; |
J. Bruce Fields | c6bb3ca | 2012-11-01 16:31:02 -0400 | [diff] [blame] | 1571 | new->se_cb_sec = cses->cb_sec; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1572 | atomic_set(&new->se_ref, 0); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1573 | idx = hash_sessionid(&new->se_sessionid); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1574 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1575 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1576 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1577 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1578 | |
Chuck Lever | b0d2e42 | 2014-08-22 15:10:59 -0400 | [diff] [blame] | 1579 | { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1580 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1581 | /* |
| 1582 | * This is a little silly; with sessions there's no real |
| 1583 | * use for the callback address. Use the peer address |
| 1584 | * as a reasonable default for now, but consider fixing |
| 1585 | * the rpc client not to require an address in the |
| 1586 | * future: |
| 1587 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1588 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1589 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1590 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1591 | } |
| 1592 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1593 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1594 | static struct nfsd4_session * |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1595 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1596 | { |
| 1597 | struct nfsd4_session *elem; |
| 1598 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1599 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1600 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1601 | lockdep_assert_held(&nn->client_lock); |
| 1602 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1603 | dump_sessionid(__func__, sessionid); |
| 1604 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1605 | /* Search in the appropriate list */ |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1606 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1607 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1608 | NFS4_MAX_SESSIONID_LEN)) { |
| 1609 | return elem; |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | dprintk("%s: session not found\n", __func__); |
| 1614 | return NULL; |
| 1615 | } |
| 1616 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1617 | static struct nfsd4_session * |
| 1618 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, |
| 1619 | __be32 *ret) |
| 1620 | { |
| 1621 | struct nfsd4_session *session; |
| 1622 | __be32 status = nfserr_badsession; |
| 1623 | |
| 1624 | session = __find_in_sessionid_hashtbl(sessionid, net); |
| 1625 | if (!session) |
| 1626 | goto out; |
| 1627 | status = nfsd4_get_session_locked(session); |
| 1628 | if (status) |
| 1629 | session = NULL; |
| 1630 | out: |
| 1631 | *ret = status; |
| 1632 | return session; |
| 1633 | } |
| 1634 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1635 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1636 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1637 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1638 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1639 | struct nfs4_client *clp = ses->se_client; |
| 1640 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1641 | |
| 1642 | lockdep_assert_held(&nn->client_lock); |
| 1643 | |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1644 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1645 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1646 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1647 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1648 | } |
| 1649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1651 | static int |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1652 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | { |
J. Bruce Fields | bbc7f33 | 2015-01-20 11:51:26 -0500 | [diff] [blame] | 1654 | /* |
| 1655 | * We're assuming the clid was not given out from a boot |
| 1656 | * precisely 2^32 (about 136 years) before this one. That seems |
| 1657 | * a safe assumption: |
| 1658 | */ |
| 1659 | if (clid->cl_boot == (u32)nn->boot_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | return 0; |
Andy Adamson | 60adfc5 | 2009-04-03 08:28:50 +0300 | [diff] [blame] | 1661 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1662 | clid->cl_boot, clid->cl_id, nn->boot_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | return 1; |
| 1664 | } |
| 1665 | |
| 1666 | /* |
| 1667 | * XXX Should we use a slab cache ? |
| 1668 | * This type of memory management is somewhat inefficient, but we use it |
| 1669 | * anyway since SETCLIENTID is not a common operation. |
| 1670 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1671 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | { |
| 1673 | struct nfs4_client *clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1674 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1676 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
| 1677 | if (clp == NULL) |
| 1678 | return NULL; |
Thomas Meyer | 67114fe | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 1679 | clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1680 | if (clp->cl_name.data == NULL) |
| 1681 | goto err_no_name; |
| 1682 | clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) * |
| 1683 | OWNER_HASH_SIZE, GFP_KERNEL); |
| 1684 | if (!clp->cl_ownerstr_hashtbl) |
| 1685 | goto err_no_hashtbl; |
| 1686 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 1687 | INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1688 | clp->cl_name.len = name.len; |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1689 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1690 | idr_init(&clp->cl_stateids); |
| 1691 | atomic_set(&clp->cl_refcount, 0); |
| 1692 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 1693 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1694 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1695 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 1696 | INIT_LIST_HEAD(&clp->cl_lru); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1697 | INIT_LIST_HEAD(&clp->cl_revoked); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 1698 | #ifdef CONFIG_NFSD_PNFS |
| 1699 | INIT_LIST_HEAD(&clp->cl_lo_states); |
| 1700 | #endif |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1701 | spin_lock_init(&clp->cl_lock); |
| 1702 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | return clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1704 | err_no_hashtbl: |
| 1705 | kfree(clp->cl_name.data); |
| 1706 | err_no_name: |
| 1707 | kfree(clp); |
| 1708 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
Trond Myklebust | 4dd86e15 | 2014-04-18 14:43:58 -0400 | [diff] [blame] | 1711 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | free_client(struct nfs4_client *clp) |
| 1713 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1714 | while (!list_empty(&clp->cl_sessions)) { |
| 1715 | struct nfsd4_session *ses; |
| 1716 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 1717 | se_perclnt); |
| 1718 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1719 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 1720 | free_session(ses); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1721 | } |
Trond Myklebust | 4cb57e3 | 2014-04-18 14:43:57 -0400 | [diff] [blame] | 1722 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1723 | free_svc_cred(&clp->cl_cred); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1724 | kfree(clp->cl_ownerstr_hashtbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | kfree(clp->cl_name.data); |
majianpeng | 2d32b29 | 2013-01-29 13:16:06 +0800 | [diff] [blame] | 1726 | idr_destroy(&clp->cl_stateids); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | kfree(clp); |
| 1728 | } |
| 1729 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1730 | /* must be called under the client_lock */ |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1731 | static void |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1732 | unhash_client_locked(struct nfs4_client *clp) |
| 1733 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1734 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1735 | struct nfsd4_session *ses; |
| 1736 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1737 | lockdep_assert_held(&nn->client_lock); |
| 1738 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1739 | /* Mark the client as expired! */ |
| 1740 | clp->cl_time = 0; |
| 1741 | /* Make it invisible */ |
| 1742 | if (!list_empty(&clp->cl_idhash)) { |
| 1743 | list_del_init(&clp->cl_idhash); |
| 1744 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
| 1745 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
| 1746 | else |
| 1747 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
| 1748 | } |
| 1749 | list_del_init(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1750 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1751 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 1752 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1753 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1754 | } |
| 1755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | static void |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1757 | unhash_client(struct nfs4_client *clp) |
| 1758 | { |
| 1759 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1760 | |
| 1761 | spin_lock(&nn->client_lock); |
| 1762 | unhash_client_locked(clp); |
| 1763 | spin_unlock(&nn->client_lock); |
| 1764 | } |
| 1765 | |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame] | 1766 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
| 1767 | { |
| 1768 | if (atomic_read(&clp->cl_refcount)) |
| 1769 | return nfserr_jukebox; |
| 1770 | unhash_client_locked(clp); |
| 1771 | return nfs_ok; |
| 1772 | } |
| 1773 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1774 | static void |
| 1775 | __destroy_client(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1777 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | struct list_head reaplist; |
| 1780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1782 | spin_lock(&state_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1783 | while (!list_empty(&clp->cl_delegations)) { |
| 1784 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1785 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1786 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1788 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | while (!list_empty(&reaplist)) { |
| 1790 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1791 | list_del_init(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 1792 | put_clnt_odstate(dp->dl_clnt_odstate); |
Jeff Layton | afbda40 | 2014-08-09 10:22:41 -0400 | [diff] [blame] | 1793 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1794 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | } |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1796 | while (!list_empty(&clp->cl_revoked)) { |
Andrew Elble | c876486 | 2015-02-25 17:46:27 -0500 | [diff] [blame] | 1797 | dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1798 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1799 | nfs4_put_stid(&dp->dl_stid); |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 1800 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1801 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1802 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 1803 | nfs4_get_stateowner(&oo->oo_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1804 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | } |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 1806 | nfsd4_return_all_client_layouts(clp); |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 1807 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 1808 | if (clp->cl_cb_conn.cb_xprt) |
| 1809 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 1810 | free_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1813 | static void |
| 1814 | destroy_client(struct nfs4_client *clp) |
| 1815 | { |
| 1816 | unhash_client(clp); |
| 1817 | __destroy_client(clp); |
| 1818 | } |
| 1819 | |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1820 | static void expire_client(struct nfs4_client *clp) |
| 1821 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1822 | unhash_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1823 | nfsd4_client_record_remove(clp); |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1824 | __destroy_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1825 | } |
| 1826 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1827 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 1828 | { |
| 1829 | memcpy(target->cl_verifier.data, source->data, |
| 1830 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1833 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 1834 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 1836 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 1837 | } |
| 1838 | |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 1839 | int strdup_if_nonnull(char **target, char *source) |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1840 | { |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 1841 | if (source) { |
| 1842 | *target = kstrdup(source, GFP_KERNEL); |
| 1843 | if (!*target) |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1844 | return -ENOMEM; |
| 1845 | } else |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 1846 | *target = NULL; |
| 1847 | return 0; |
| 1848 | } |
| 1849 | |
| 1850 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) |
| 1851 | { |
| 1852 | int ret; |
| 1853 | |
| 1854 | ret = strdup_if_nonnull(&target->cr_principal, source->cr_principal); |
| 1855 | if (ret) |
| 1856 | return ret; |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 1857 | ret = strdup_if_nonnull(&target->cr_raw_principal, |
| 1858 | source->cr_raw_principal); |
| 1859 | if (ret) |
| 1860 | return ret; |
J. Bruce Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 1861 | target->cr_flavor = source->cr_flavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | target->cr_uid = source->cr_uid; |
| 1863 | target->cr_gid = source->cr_gid; |
| 1864 | target->cr_group_info = source->cr_group_info; |
| 1865 | get_group_info(target->cr_group_info); |
J. Bruce Fields | 0dc1531 | 2013-05-14 16:07:13 -0400 | [diff] [blame] | 1866 | target->cr_gss_mech = source->cr_gss_mech; |
| 1867 | if (source->cr_gss_mech) |
| 1868 | gss_mech_get(source->cr_gss_mech); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1869 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 1872 | static int |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1873 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 1874 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 1875 | if (o1->len < o2->len) |
| 1876 | return -1; |
| 1877 | if (o1->len > o2->len) |
| 1878 | return 1; |
| 1879 | return memcmp(o1->data, o2->data, o1->len); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1880 | } |
| 1881 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1882 | static int same_name(const char *n1, const char *n2) |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1883 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1884 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1888 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 1889 | { |
| 1890 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1894 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 1895 | { |
| 1896 | 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] | 1897 | } |
| 1898 | |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1899 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 1900 | { |
| 1901 | int i; |
| 1902 | |
| 1903 | if (g1->ngroups != g2->ngroups) |
| 1904 | return false; |
| 1905 | for (i=0; i<g1->ngroups; i++) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1906 | if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i))) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1907 | return false; |
| 1908 | return true; |
| 1909 | } |
| 1910 | |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1911 | /* |
| 1912 | * RFC 3530 language requires clid_inuse be returned when the |
| 1913 | * "principal" associated with a requests differs from that previously |
| 1914 | * used. We use uid, gid's, and gss principal string as our best |
| 1915 | * approximation. We also don't want to allow non-gss use of a client |
| 1916 | * established using gss: in theory cr_principal should catch that |
| 1917 | * change, but in practice cr_principal can be null even in the gss case |
| 1918 | * since gssd doesn't always pass down a principal string. |
| 1919 | */ |
| 1920 | static bool is_gss_cred(struct svc_cred *cr) |
| 1921 | { |
| 1922 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 1923 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 1924 | } |
| 1925 | |
| 1926 | |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1927 | static bool |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1928 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 1929 | { |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1930 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1931 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 1932 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1933 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 1934 | return false; |
| 1935 | if (cr1->cr_principal == cr2->cr_principal) |
| 1936 | return true; |
| 1937 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 1938 | return false; |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1939 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1942 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 1943 | { |
| 1944 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1945 | u32 service; |
| 1946 | |
J. Bruce Fields | c472059 | 2013-08-07 11:41:49 -0400 | [diff] [blame] | 1947 | if (!cr->cr_gss_mech) |
| 1948 | return false; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1949 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 1950 | return service == RPC_GSS_SVC_INTEGRITY || |
| 1951 | service == RPC_GSS_SVC_PRIVACY; |
| 1952 | } |
| 1953 | |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 1954 | bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1955 | { |
| 1956 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1957 | |
| 1958 | if (!cl->cl_mach_cred) |
| 1959 | return true; |
| 1960 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 1961 | return false; |
| 1962 | if (!svc_rqst_integrity_protected(rqstp)) |
| 1963 | return false; |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 1964 | if (cl->cl_cred.cr_raw_principal) |
| 1965 | return 0 == strcmp(cl->cl_cred.cr_raw_principal, |
| 1966 | cr->cr_raw_principal); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1967 | if (!cr->cr_principal) |
| 1968 | return false; |
| 1969 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 1970 | } |
| 1971 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 1972 | static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn) |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 1973 | { |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1974 | __be32 verf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | |
Jeff Layton | f419992 | 2014-06-17 07:44:11 -0400 | [diff] [blame] | 1976 | /* |
| 1977 | * This is opaque to client, so no need to byte-swap. Use |
| 1978 | * __force to keep sparse happy |
| 1979 | */ |
| 1980 | verf[0] = (__force __be32)get_seconds(); |
Kinglong Mee | 19311aa8 | 2015-07-18 07:33:31 +0800 | [diff] [blame] | 1981 | verf[1] = (__force __be32)nn->clverifier_counter++; |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1982 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | } |
| 1984 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 1985 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
| 1986 | { |
| 1987 | clp->cl_clientid.cl_boot = nn->boot_time; |
| 1988 | clp->cl_clientid.cl_id = nn->clientid_counter++; |
| 1989 | gen_confirm(clp, nn); |
| 1990 | } |
| 1991 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1992 | static struct nfs4_stid * |
| 1993 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1994 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 1995 | struct nfs4_stid *ret; |
| 1996 | |
| 1997 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 1998 | if (!ret || !ret->sc_type) |
| 1999 | return NULL; |
| 2000 | return ret; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 2001 | } |
| 2002 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2003 | static struct nfs4_stid * |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2004 | 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] | 2005 | { |
| 2006 | struct nfs4_stid *s; |
| 2007 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2008 | spin_lock(&cl->cl_lock); |
| 2009 | s = find_stateid_locked(cl, t); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 2010 | if (s != NULL) { |
| 2011 | if (typemask & s->sc_type) |
| 2012 | atomic_inc(&s->sc_count); |
| 2013 | else |
| 2014 | s = NULL; |
| 2015 | } |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2016 | spin_unlock(&cl->cl_lock); |
| 2017 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 2018 | } |
| 2019 | |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 2020 | static struct nfs4_client *create_client(struct xdr_netobj name, |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2021 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 2022 | { |
| 2023 | struct nfs4_client *clp; |
| 2024 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2025 | int ret; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2026 | struct net *net = SVC_NET(rqstp); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2027 | |
| 2028 | clp = alloc_client(name); |
| 2029 | if (clp == NULL) |
| 2030 | return NULL; |
| 2031 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2032 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 2033 | if (ret) { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2034 | free_client(clp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2035 | return NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2036 | } |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 2037 | nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL); |
Benny Halevy | 07cd490 | 2010-05-12 00:13:41 +0300 | [diff] [blame] | 2038 | clp->cl_time = get_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2039 | clear_bit(0, &clp->cl_cb_slot_busy); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2040 | copy_verf(clp, verf); |
| 2041 | rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 2042 | clp->cl_cb_session = NULL; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2043 | clp->net = net; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2044 | return clp; |
| 2045 | } |
| 2046 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2047 | static void |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2048 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 2049 | { |
| 2050 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 2051 | struct nfs4_client *clp; |
| 2052 | |
| 2053 | while (*new) { |
| 2054 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 2055 | parent = *new; |
| 2056 | |
| 2057 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 2058 | new = &((*new)->rb_left); |
| 2059 | else |
| 2060 | new = &((*new)->rb_right); |
| 2061 | } |
| 2062 | |
| 2063 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 2064 | rb_insert_color(&new_clp->cl_namenode, root); |
| 2065 | } |
| 2066 | |
| 2067 | static struct nfs4_client * |
| 2068 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 2069 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2070 | int cmp; |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2071 | struct rb_node *node = root->rb_node; |
| 2072 | struct nfs4_client *clp; |
| 2073 | |
| 2074 | while (node) { |
| 2075 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 2076 | cmp = compare_blob(&clp->cl_name, name); |
| 2077 | if (cmp > 0) |
| 2078 | node = node->rb_left; |
| 2079 | else if (cmp < 0) |
| 2080 | node = node->rb_right; |
| 2081 | else |
| 2082 | return clp; |
| 2083 | } |
| 2084 | return NULL; |
| 2085 | } |
| 2086 | |
| 2087 | static void |
| 2088 | add_to_unconfirmed(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | { |
| 2090 | unsigned int idhashval; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2091 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2093 | lockdep_assert_held(&nn->client_lock); |
| 2094 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2095 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2096 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2098 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2099 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | } |
| 2101 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2102 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | move_to_confirmed(struct nfs4_client *clp) |
| 2104 | { |
| 2105 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2106 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2108 | lockdep_assert_held(&nn->client_lock); |
| 2109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2111 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2112 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2113 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2114 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2115 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2119 | 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] | 2120 | { |
| 2121 | struct nfs4_client *clp; |
| 2122 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 2123 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2124 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2125 | if (same_clid(&clp->cl_clientid, clid)) { |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 2126 | if ((bool)clp->cl_minorversion != sessions) |
| 2127 | return NULL; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2128 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | return clp; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | } |
| 2132 | return NULL; |
| 2133 | } |
| 2134 | |
| 2135 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2136 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 2137 | { |
| 2138 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 2139 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2140 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2141 | return find_client_in_id_table(tbl, clid, sessions); |
| 2142 | } |
| 2143 | |
| 2144 | static struct nfs4_client * |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2145 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | { |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2147 | struct list_head *tbl = nn->unconf_id_hashtbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2149 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2150 | return find_client_in_id_table(tbl, clid, sessions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2153 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2154 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2155 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2156 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2157 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2158 | static struct nfs4_client * |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2159 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2160 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2161 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2162 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | static struct nfs4_client * |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2166 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2167 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2168 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2169 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2170 | } |
| 2171 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2172 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2173 | 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] | 2174 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2175 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2176 | struct sockaddr *sa = svc_addr(rqstp); |
| 2177 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2178 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2180 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 2181 | if (se->se_callback_netid_len == 3 && |
| 2182 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 2183 | expected_family = AF_INET; |
| 2184 | else if (se->se_callback_netid_len == 4 && |
| 2185 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 2186 | expected_family = AF_INET6; |
| 2187 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | goto out_err; |
| 2189 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2190 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2191 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2192 | (struct sockaddr *)&conn->cb_addr, |
| 2193 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2194 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2195 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2197 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2198 | if (conn->cb_addr.ss_family == AF_INET6) |
| 2199 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 2200 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2201 | conn->cb_prog = se->se_callback_prog; |
| 2202 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 2203 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | return; |
| 2205 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2206 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 2207 | conn->cb_addrlen = 0; |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 2208 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | "will not receive delegations\n", |
| 2210 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 2211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | return; |
| 2213 | } |
| 2214 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2215 | /* |
J. Bruce Fields | 067e1ac | 2014-03-21 17:26:44 -0400 | [diff] [blame] | 2216 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2217 | */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2218 | static void |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2219 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 2220 | { |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2221 | struct xdr_buf *buf = resp->xdr.buf; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2222 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 2223 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2224 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2225 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2226 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2227 | slot->sl_opcnt = resp->opcnt; |
| 2228 | slot->sl_status = resp->cstate.status; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2229 | |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2230 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2231 | if (nfsd4_not_cached(resp)) { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2232 | slot->sl_datalen = 0; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2233 | return; |
| 2234 | } |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2235 | base = resp->cstate.data_offset; |
| 2236 | slot->sl_datalen = buf->len - base; |
| 2237 | if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen)) |
Dan Carpenter | d3f0340 | 2015-11-21 13:28:50 +0300 | [diff] [blame] | 2238 | WARN(1, "%s: sessions DRC could not cache compound\n", |
| 2239 | __func__); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2240 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2244 | * Encode the replay sequence operation from the slot values. |
| 2245 | * If cachethis is FALSE encode the uncached rep error on the next |
| 2246 | * operation which sets resp->p and increments resp->opcnt for |
| 2247 | * nfs4svc_encode_compoundres. |
| 2248 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2249 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2250 | static __be32 |
| 2251 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 2252 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2253 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2254 | struct nfsd4_op *op; |
| 2255 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2256 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2257 | /* Encode the replayed sequence operation */ |
| 2258 | op = &args->ops[resp->opcnt - 1]; |
| 2259 | nfsd4_encode_operation(resp, op); |
| 2260 | |
| 2261 | /* Return nfserr_retry_uncached_rep in next operation. */ |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2262 | if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2263 | op = &args->ops[resp->opcnt++]; |
| 2264 | op->status = nfserr_retry_uncached_rep; |
| 2265 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2266 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2267 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2271 | * The sequence operation is not cached because we can use the slot and |
| 2272 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2273 | */ |
J. Bruce Fields | 3ca2eb9 | 2014-03-21 12:04:21 -0400 | [diff] [blame] | 2274 | static __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2275 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 2276 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2277 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2278 | struct nfsd4_slot *slot = resp->cstate.slot; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2279 | struct xdr_stream *xdr = &resp->xdr; |
| 2280 | __be32 *p; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2281 | __be32 status; |
| 2282 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2283 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2284 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2285 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
J. Bruce Fields | 0da7b19 | 2014-03-21 11:43:34 -0400 | [diff] [blame] | 2286 | if (status) |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2287 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2288 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2289 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 2290 | if (!p) { |
| 2291 | WARN_ON_ONCE(1); |
| 2292 | return nfserr_serverfault; |
| 2293 | } |
| 2294 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 2295 | xdr_commit_encode(xdr); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2296 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2297 | resp->opcnt = slot->sl_opcnt; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2298 | return slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2299 | } |
| 2300 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2301 | /* |
| 2302 | * Set the exchange_id flags returned by the server. |
| 2303 | */ |
| 2304 | static void |
| 2305 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 2306 | { |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2307 | #ifdef CONFIG_NFSD_PNFS |
| 2308 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; |
| 2309 | #else |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2310 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2311 | #endif |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2312 | |
| 2313 | /* Referrals are supported, Migration is not. */ |
| 2314 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 2315 | |
| 2316 | /* set the wire flags to return to client. */ |
| 2317 | clid->flags = new->cl_exchange_flags; |
| 2318 | } |
| 2319 | |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 2320 | static bool client_has_openowners(struct nfs4_client *clp) |
| 2321 | { |
| 2322 | struct nfs4_openowner *oo; |
| 2323 | |
| 2324 | list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { |
| 2325 | if (!list_empty(&oo->oo_owner.so_stateids)) |
| 2326 | return true; |
| 2327 | } |
| 2328 | return false; |
| 2329 | } |
| 2330 | |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2331 | static bool client_has_state(struct nfs4_client *clp) |
| 2332 | { |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 2333 | return client_has_openowners(clp) |
Kinglong Mee | 47e970b | 2015-07-21 13:09:17 +0800 | [diff] [blame] | 2334 | #ifdef CONFIG_NFSD_PNFS |
| 2335 | || !list_empty(&clp->cl_lo_states) |
| 2336 | #endif |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 2337 | || !list_empty(&clp->cl_delegations) |
| 2338 | || !list_empty(&clp->cl_sessions); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2339 | } |
| 2340 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2341 | __be32 |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2342 | nfsd4_exchange_id(struct svc_rqst *rqstp, |
| 2343 | struct nfsd4_compound_state *cstate, |
| 2344 | struct nfsd4_exchange_id *exid) |
| 2345 | { |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2346 | struct nfs4_client *conf, *new; |
| 2347 | struct nfs4_client *unconf = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2348 | __be32 status; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2349 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2350 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2351 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 2352 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2353 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2354 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2355 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2356 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2357 | "ip_addr=%s flags %x, spa_how %d\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2358 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2359 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2360 | |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 2361 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2362 | return nfserr_inval; |
| 2363 | |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 2364 | new = create_client(exid->clname, rqstp, &verf); |
| 2365 | if (new == NULL) |
| 2366 | return nfserr_jukebox; |
| 2367 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2368 | switch (exid->spa_how) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2369 | case SP4_MACH_CRED: |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 2370 | exid->spo_must_enforce[0] = 0; |
| 2371 | exid->spo_must_enforce[1] = ( |
| 2372 | 1 << (OP_BIND_CONN_TO_SESSION - 32) | |
| 2373 | 1 << (OP_EXCHANGE_ID - 32) | |
| 2374 | 1 << (OP_CREATE_SESSION - 32) | |
| 2375 | 1 << (OP_DESTROY_SESSION - 32) | |
| 2376 | 1 << (OP_DESTROY_CLIENTID - 32)); |
| 2377 | |
| 2378 | exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | |
| 2379 | 1 << (OP_OPEN_DOWNGRADE) | |
| 2380 | 1 << (OP_LOCKU) | |
| 2381 | 1 << (OP_DELEGRETURN)); |
| 2382 | |
| 2383 | exid->spo_must_allow[1] &= ( |
| 2384 | 1 << (OP_TEST_STATEID - 32) | |
| 2385 | 1 << (OP_FREE_STATEID - 32)); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 2386 | if (!svc_rqst_integrity_protected(rqstp)) { |
| 2387 | status = nfserr_inval; |
| 2388 | goto out_nolock; |
| 2389 | } |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 2390 | /* |
| 2391 | * Sometimes userspace doesn't give us a principal. |
| 2392 | * Which is a bug, really. Anyway, we can't enforce |
| 2393 | * MACH_CRED in that case, better to give up now: |
| 2394 | */ |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 2395 | if (!new->cl_cred.cr_principal && |
| 2396 | !new->cl_cred.cr_raw_principal) { |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 2397 | status = nfserr_serverfault; |
| 2398 | goto out_nolock; |
| 2399 | } |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 2400 | new->cl_mach_cred = true; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2401 | case SP4_NONE: |
| 2402 | break; |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 2403 | default: /* checked by xdr code */ |
| 2404 | WARN_ON_ONCE(1); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2405 | case SP4_SSV: |
Kinglong Mee | 8edf4b0 | 2016-02-26 22:36:42 +0800 | [diff] [blame] | 2406 | status = nfserr_encr_alg_unsupp; |
| 2407 | goto out_nolock; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2408 | } |
| 2409 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2410 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2411 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2412 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2413 | if (conf) { |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 2414 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 2415 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 2416 | |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2417 | if (update) { |
| 2418 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2419 | status = nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2420 | goto out; |
| 2421 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2422 | if (!nfsd4_mach_creds_match(conf, rqstp)) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2423 | status = nfserr_wrong_cred; |
| 2424 | goto out; |
| 2425 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2426 | if (!creds_match) { /* case 9 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2427 | status = nfserr_perm; |
| 2428 | goto out; |
| 2429 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2430 | if (!verfs_match) { /* case 8 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2431 | status = nfserr_not_same; |
| 2432 | goto out; |
| 2433 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2434 | /* case 6 */ |
| 2435 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2436 | goto out_copy; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2437 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2438 | if (!creds_match) { /* case 3 */ |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2439 | if (client_has_state(conf)) { |
| 2440 | status = nfserr_clid_inuse; |
| 2441 | goto out; |
| 2442 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2443 | goto out_new; |
| 2444 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2445 | if (verfs_match) { /* case 2 */ |
J. Bruce Fields | 0f1ba0e | 2012-05-25 21:24:40 -0400 | [diff] [blame] | 2446 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2447 | goto out_copy; |
| 2448 | } |
| 2449 | /* case 5, client reboot */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2450 | conf = NULL; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2451 | goto out_new; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 2452 | } |
| 2453 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2454 | if (update) { /* case 7 */ |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 2455 | status = nfserr_noent; |
| 2456 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2457 | } |
| 2458 | |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2459 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2460 | if (unconf) /* case 4, possible retry or client restart */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2461 | unhash_client_locked(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2462 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2463 | /* case 1 (normal case) */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2464 | out_new: |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 2465 | if (conf) { |
| 2466 | status = mark_client_expired_locked(conf); |
| 2467 | if (status) |
| 2468 | goto out; |
| 2469 | } |
J. Bruce Fields | 4f540e2 | 2013-05-07 11:57:52 -0400 | [diff] [blame] | 2470 | new->cl_minorversion = cstate->minorversion; |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 2471 | new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; |
| 2472 | new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2473 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2474 | gen_clid(new, nn); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2475 | add_to_unconfirmed(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2476 | swap(new, conf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2477 | out_copy: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2478 | exid->clientid.cl_boot = conf->cl_clientid.cl_boot; |
| 2479 | exid->clientid.cl_id = conf->cl_clientid.cl_id; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2480 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2481 | exid->seqid = conf->cl_cs_slot.sl_seqid + 1; |
| 2482 | nfsd4_set_ex_flags(conf, exid); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2483 | |
| 2484 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2485 | conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2486 | status = nfs_ok; |
| 2487 | |
| 2488 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2489 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 2490 | out_nolock: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2491 | if (new) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2492 | expire_client(new); |
| 2493 | if (unconf) |
| 2494 | expire_client(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2495 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2496 | } |
| 2497 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2498 | static __be32 |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2499 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2500 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2501 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 2502 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2503 | |
| 2504 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2505 | if (slot_inuse) { |
| 2506 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2507 | return nfserr_jukebox; |
| 2508 | else |
| 2509 | return nfserr_seq_misordered; |
| 2510 | } |
J. Bruce Fields | f6d8248 | 2012-02-13 16:13:41 -0500 | [diff] [blame] | 2511 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2512 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2513 | return nfs_ok; |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2514 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2515 | return nfserr_replay_cache; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2516 | return nfserr_seq_misordered; |
| 2517 | } |
| 2518 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2519 | /* |
| 2520 | * Cache the create session result into the create session single DRC |
| 2521 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 2522 | * Do this for solo or embedded create session operations. |
| 2523 | */ |
| 2524 | static void |
| 2525 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2526 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2527 | { |
| 2528 | slot->sl_status = nfserr; |
| 2529 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 2530 | } |
| 2531 | |
| 2532 | static __be32 |
| 2533 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 2534 | struct nfsd4_clid_slot *slot) |
| 2535 | { |
| 2536 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 2537 | return slot->sl_status; |
| 2538 | } |
| 2539 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2540 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 2541 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 2542 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2543 | 3 + /* version, opcount, opcode */ \ |
| 2544 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2545 | /* seqid, slotID, slotID, cache */ \ |
| 2546 | 4 ) * sizeof(__be32)) |
| 2547 | |
| 2548 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 2549 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 2550 | 1 + /* status */ \ |
| 2551 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2552 | 3 + /* opcount, opcode, opstatus*/ \ |
| 2553 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2554 | /* seqid, slotID, slotID, slotID, status */ \ |
| 2555 | 5 ) * sizeof(__be32)) |
| 2556 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2557 | 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] | 2558 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2559 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 2560 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2561 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 2562 | return nfserr_toosmall; |
| 2563 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 2564 | return nfserr_toosmall; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2565 | ca->headerpadsz = 0; |
| 2566 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 2567 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 2568 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 2569 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 2570 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 2571 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 2572 | /* |
| 2573 | * Note decreasing slot size below client's request may make it |
| 2574 | * difficult for client to function correctly, whereas |
| 2575 | * decreasing the number of slots will (just?) affect |
| 2576 | * performance. When short on memory we therefore prefer to |
| 2577 | * decrease number of slots instead of their size. Clients that |
| 2578 | * request larger slots than they need will get poor results: |
| 2579 | */ |
| 2580 | ca->maxreqs = nfsd4_get_drc_mem(ca); |
| 2581 | if (!ca->maxreqs) |
| 2582 | return nfserr_jukebox; |
| 2583 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2584 | return nfs_ok; |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2585 | } |
| 2586 | |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 2587 | /* |
| 2588 | * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. |
| 2589 | * These are based on similar macros in linux/sunrpc/msg_prot.h . |
| 2590 | */ |
| 2591 | #define RPC_MAX_HEADER_WITH_AUTH_SYS \ |
| 2592 | (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) |
| 2593 | |
| 2594 | #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ |
| 2595 | (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) |
| 2596 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2597 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 2598 | RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2599 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 2600 | RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ |
| 2601 | sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2602 | |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2603 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 2604 | { |
| 2605 | ca->headerpadsz = 0; |
| 2606 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2607 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2608 | return nfserr_toosmall; |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2609 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2610 | return nfserr_toosmall; |
| 2611 | ca->maxresp_cached = 0; |
| 2612 | if (ca->maxops < 2) |
| 2613 | return nfserr_toosmall; |
| 2614 | |
| 2615 | return nfs_ok; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2616 | } |
| 2617 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2618 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 2619 | { |
| 2620 | switch (cbs->flavor) { |
| 2621 | case RPC_AUTH_NULL: |
| 2622 | case RPC_AUTH_UNIX: |
| 2623 | return nfs_ok; |
| 2624 | default: |
| 2625 | /* |
| 2626 | * GSS case: the spec doesn't allow us to return this |
| 2627 | * error. But it also doesn't allow us not to support |
| 2628 | * GSS. |
| 2629 | * I'd rather this fail hard than return some error the |
| 2630 | * client might think it can already handle: |
| 2631 | */ |
| 2632 | return nfserr_encr_alg_unsupp; |
| 2633 | } |
| 2634 | } |
| 2635 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2636 | __be32 |
| 2637 | nfsd4_create_session(struct svc_rqst *rqstp, |
| 2638 | struct nfsd4_compound_state *cstate, |
| 2639 | struct nfsd4_create_session *cr_ses) |
| 2640 | { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2641 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2642 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2643 | struct nfs4_client *old = NULL; |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2644 | struct nfsd4_session *new; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2645 | struct nfsd4_conn *conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2646 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2647 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2648 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2649 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2650 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 2651 | return nfserr_inval; |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2652 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 2653 | if (status) |
| 2654 | return status; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2655 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2656 | if (status) |
| 2657 | return status; |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2658 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 2659 | if (status) |
Kinglong Mee | f403e45 | 2013-12-23 17:31:21 +0800 | [diff] [blame] | 2660 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2661 | status = nfserr_jukebox; |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 2662 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2663 | if (!new) |
| 2664 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2665 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 2666 | if (!conn) |
| 2667 | goto out_free_session; |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2668 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2669 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2670 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2671 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2672 | WARN_ON_ONCE(conf && unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2673 | |
| 2674 | if (conf) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2675 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2676 | if (!nfsd4_mach_creds_match(conf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2677 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2678 | cs_slot = &conf->cl_cs_slot; |
| 2679 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Kinglong Mee | f5e22bb | 2015-07-13 17:32:34 +0800 | [diff] [blame] | 2680 | if (status) { |
| 2681 | if (status == nfserr_replay_cache) |
| 2682 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2683 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2684 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2685 | } else if (unconf) { |
| 2686 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2687 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2688 | status = nfserr_clid_inuse; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2689 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2690 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2691 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2692 | if (!nfsd4_mach_creds_match(unconf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2693 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2694 | cs_slot = &unconf->cl_cs_slot; |
| 2695 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 2696 | if (status) { |
| 2697 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2698 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2699 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2700 | } |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2701 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2702 | if (old) { |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2703 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 2704 | if (status) { |
| 2705 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2706 | goto out_free_conn; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 2707 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2708 | } |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2709 | move_to_confirmed(unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2710 | conf = unconf; |
| 2711 | } else { |
| 2712 | status = nfserr_stale_clientid; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2713 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2714 | } |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2715 | status = nfs_ok; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 2716 | /* Persistent sessions are not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 2717 | cr_ses->flags &= ~SESSION4_PERSIST; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 2718 | /* Upshifting from TCP to RDMA is not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 2719 | cr_ses->flags &= ~SESSION4_RDMA; |
| 2720 | |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2721 | init_session(rqstp, new, conf, cr_ses); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2722 | nfsd4_get_session_locked(new); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2723 | |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2724 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2725 | NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 2726 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2727 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2728 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2729 | /* cache solo and embedded create sessions under the client_lock */ |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2730 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2731 | spin_unlock(&nn->client_lock); |
| 2732 | /* init connection and backchannel */ |
| 2733 | nfsd4_init_conn(rqstp, conn, new); |
| 2734 | nfsd4_put_session(new); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2735 | if (old) |
| 2736 | expire_client(old); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2737 | return status; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2738 | out_free_conn: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2739 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2740 | free_conn(conn); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2741 | if (old) |
| 2742 | expire_client(old); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2743 | out_free_session: |
| 2744 | __free_session(new); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2745 | out_release_drc_mem: |
| 2746 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
J. Bruce Fields | 1ca5079 | 2013-03-14 18:12:03 -0400 | [diff] [blame] | 2747 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2748 | } |
| 2749 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2750 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 2751 | { |
| 2752 | switch (*dir) { |
| 2753 | case NFS4_CDFC4_FORE: |
| 2754 | case NFS4_CDFC4_BACK: |
| 2755 | return nfs_ok; |
| 2756 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 2757 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 2758 | *dir = NFS4_CDFC4_BOTH; |
| 2759 | return nfs_ok; |
| 2760 | }; |
| 2761 | return nfserr_inval; |
| 2762 | } |
| 2763 | |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2764 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc) |
| 2765 | { |
| 2766 | struct nfsd4_session *session = cstate->session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2767 | 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] | 2768 | __be32 status; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2769 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2770 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 2771 | if (status) |
| 2772 | return status; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2773 | spin_lock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2774 | session->se_cb_prog = bc->bc_cb_program; |
| 2775 | session->se_cb_sec = bc->bc_cb_sec; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2776 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2777 | |
| 2778 | nfsd4_probe_callback(session->se_client); |
| 2779 | |
| 2780 | return nfs_ok; |
| 2781 | } |
| 2782 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2783 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 2784 | struct nfsd4_compound_state *cstate, |
| 2785 | struct nfsd4_bind_conn_to_session *bcts) |
| 2786 | { |
| 2787 | __be32 status; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2788 | struct nfsd4_conn *conn; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2789 | struct nfsd4_session *session; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2790 | struct net *net = SVC_NET(rqstp); |
| 2791 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2792 | |
| 2793 | if (!nfsd4_last_compound_op(rqstp)) |
| 2794 | return nfserr_not_only_op; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2795 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2796 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2797 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2798 | if (!session) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2799 | goto out_no_session; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2800 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2801 | if (!nfsd4_mach_creds_match(session->se_client, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2802 | goto out; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2803 | status = nfsd4_map_bcts_dir(&bcts->dir); |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2804 | if (status) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2805 | goto out; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2806 | conn = alloc_conn(rqstp, bcts->dir); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2807 | status = nfserr_jukebox; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2808 | if (!conn) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2809 | goto out; |
| 2810 | nfsd4_init_conn(rqstp, conn, session); |
| 2811 | status = nfs_ok; |
| 2812 | out: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2813 | nfsd4_put_session(session); |
| 2814 | out_no_session: |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2815 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2816 | } |
| 2817 | |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2818 | static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid) |
| 2819 | { |
| 2820 | if (!session) |
| 2821 | return 0; |
| 2822 | return !memcmp(sid, &session->se_sessionid, sizeof(*sid)); |
| 2823 | } |
| 2824 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2825 | __be32 |
| 2826 | nfsd4_destroy_session(struct svc_rqst *r, |
| 2827 | struct nfsd4_compound_state *cstate, |
| 2828 | struct nfsd4_destroy_session *sessionid) |
| 2829 | { |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2830 | struct nfsd4_session *ses; |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2831 | __be32 status; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2832 | int ref_held_by_me = 0; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2833 | struct net *net = SVC_NET(r); |
| 2834 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2835 | |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2836 | status = nfserr_not_only_op; |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2837 | if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2838 | if (!nfsd4_last_compound_op(r)) |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2839 | goto out; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2840 | ref_held_by_me++; |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2841 | } |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2842 | dump_sessionid(__func__, &sessionid->sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2843 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2844 | ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2845 | if (!ses) |
| 2846 | goto out_client_lock; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2847 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2848 | if (!nfsd4_mach_creds_match(ses->se_client, r)) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2849 | goto out_put_session; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2850 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2851 | if (status) |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2852 | goto out_put_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2853 | unhash_session(ses); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2854 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2855 | |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 2856 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 2857 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2858 | spin_lock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2859 | status = nfs_ok; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2860 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2861 | nfsd4_put_session_locked(ses); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2862 | out_client_lock: |
| 2863 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2864 | out: |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2865 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2866 | } |
| 2867 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2868 | 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] | 2869 | { |
| 2870 | struct nfsd4_conn *c; |
| 2871 | |
| 2872 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2873 | if (c->cn_xprt == xpt) { |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2874 | return c; |
| 2875 | } |
| 2876 | } |
| 2877 | return NULL; |
| 2878 | } |
| 2879 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2880 | 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] | 2881 | { |
| 2882 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2883 | struct nfsd4_conn *c; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2884 | __be32 status = nfs_ok; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2885 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2886 | |
| 2887 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2888 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2889 | if (c) |
| 2890 | goto out_free; |
| 2891 | status = nfserr_conn_not_bound_to_session; |
| 2892 | if (clp->cl_mach_cred) |
| 2893 | goto out_free; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2894 | __nfsd4_hash_conn(new, ses); |
| 2895 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2896 | ret = nfsd4_register_conn(new); |
| 2897 | if (ret) |
| 2898 | /* oops; xprt is already down: */ |
| 2899 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2900 | return nfs_ok; |
| 2901 | out_free: |
| 2902 | spin_unlock(&clp->cl_lock); |
| 2903 | free_conn(new); |
| 2904 | return status; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2905 | } |
| 2906 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2907 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 2908 | { |
| 2909 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 2910 | |
| 2911 | return args->opcnt > session->se_fchannel.maxops; |
| 2912 | } |
| 2913 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2914 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 2915 | struct nfsd4_session *session) |
| 2916 | { |
| 2917 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 2918 | |
| 2919 | return xb->len > session->se_fchannel.maxreq_sz; |
| 2920 | } |
| 2921 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2922 | __be32 |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2923 | nfsd4_sequence(struct svc_rqst *rqstp, |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2924 | struct nfsd4_compound_state *cstate, |
| 2925 | struct nfsd4_sequence *seq) |
| 2926 | { |
Andy Adamson | f9bb94c | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2927 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2928 | struct xdr_stream *xdr = &resp->xdr; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2929 | struct nfsd4_session *session; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2930 | struct nfs4_client *clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2931 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2932 | struct nfsd4_conn *conn; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2933 | __be32 status; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2934 | int buflen; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2935 | struct net *net = SVC_NET(rqstp); |
| 2936 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2937 | |
Andy Adamson | f9bb94c | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2938 | if (resp->opcnt != 1) |
| 2939 | return nfserr_sequence_pos; |
| 2940 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2941 | /* |
| 2942 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 2943 | * below. |
| 2944 | */ |
| 2945 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 2946 | if (!conn) |
| 2947 | return nfserr_jukebox; |
| 2948 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2949 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2950 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2951 | if (!session) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2952 | goto out_no_session; |
| 2953 | clp = session->se_client; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2954 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2955 | status = nfserr_too_many_ops; |
| 2956 | if (nfsd4_session_too_many_ops(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2957 | goto out_put_session; |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2958 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2959 | status = nfserr_req_too_big; |
| 2960 | if (nfsd4_request_too_big(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2961 | goto out_put_session; |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2962 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2963 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 2964 | if (seq->slotid >= session->se_fchannel.maxreqs) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2965 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2966 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2967 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2968 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 2969 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 2970 | /* We do not negotiate the number of slots yet, so set the |
| 2971 | * maxslots to the session maxreqs which is used to encode |
| 2972 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 2973 | seq->maxslots = session->se_fchannel.maxreqs; |
| 2974 | |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2975 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 2976 | slot->sl_flags & NFSD4_SLOT_INUSE); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2977 | if (status == nfserr_replay_cache) { |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2978 | status = nfserr_seq_misordered; |
| 2979 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2980 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2981 | cstate->slot = slot; |
| 2982 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2983 | cstate->clp = clp; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2984 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2985 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2986 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2987 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 2988 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2989 | } |
| 2990 | if (status) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2991 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2992 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2993 | status = nfsd4_sequence_check_conn(conn, session); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2994 | conn = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2995 | if (status) |
| 2996 | goto out_put_session; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2997 | |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2998 | buflen = (seq->cachethis) ? |
| 2999 | session->se_fchannel.maxresp_cached : |
| 3000 | session->se_fchannel.maxresp_sz; |
| 3001 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 3002 | nfserr_rep_too_big; |
J. Bruce Fields | a5cddc8 | 2014-05-12 18:10:58 -0400 | [diff] [blame] | 3003 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3004 | goto out_put_session; |
J. Bruce Fields | 32aaa62 | 2014-03-20 20:47:41 -0400 | [diff] [blame] | 3005 | svc_reserve(rqstp, buflen); |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3006 | |
| 3007 | status = nfs_ok; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3008 | /* Success! bump slot seqid */ |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3009 | slot->sl_seqid = seq->seqid; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3010 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 3011 | if (seq->cachethis) |
| 3012 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3013 | else |
| 3014 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3015 | |
| 3016 | cstate->slot = slot; |
| 3017 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3018 | cstate->clp = clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3019 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3020 | out: |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3021 | switch (clp->cl_cb_state) { |
| 3022 | case NFSD4_CB_DOWN: |
| 3023 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 3024 | break; |
| 3025 | case NFSD4_CB_FAULT: |
| 3026 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 3027 | break; |
| 3028 | default: |
| 3029 | seq->status_flags = 0; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 3030 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3031 | if (!list_empty(&clp->cl_revoked)) |
| 3032 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3033 | out_no_session: |
Kinglong Mee | 3f42d2c | 2014-03-24 11:56:59 +0800 | [diff] [blame] | 3034 | if (conn) |
| 3035 | free_conn(conn); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3036 | spin_unlock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3037 | return status; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3038 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3039 | nfsd4_put_session_locked(session); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3040 | goto out_no_session; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3041 | } |
| 3042 | |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3043 | void |
| 3044 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 3045 | { |
| 3046 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 3047 | |
| 3048 | if (nfsd4_has_session(cs)) { |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3049 | if (cs->status != nfserr_replay_cache) { |
| 3050 | nfsd4_store_cache_entry(resp); |
| 3051 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 3052 | } |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3053 | /* Drop session reference that was taken in nfsd4_sequence() */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3054 | nfsd4_put_session(cs->session); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3055 | } else if (cs->clp) |
| 3056 | put_client_renew(cs->clp); |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3057 | } |
| 3058 | |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3059 | __be32 |
| 3060 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc) |
| 3061 | { |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3062 | struct nfs4_client *conf, *unconf; |
| 3063 | struct nfs4_client *clp = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3064 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3065 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3066 | |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3067 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3068 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3069 | conf = find_confirmed_client(&dc->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 3070 | WARN_ON_ONCE(conf && unconf); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3071 | |
| 3072 | if (conf) { |
J. Bruce Fields | c0293b0 | 2013-03-14 18:20:01 -0400 | [diff] [blame] | 3073 | if (client_has_state(conf)) { |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3074 | status = nfserr_clientid_busy; |
| 3075 | goto out; |
| 3076 | } |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 3077 | status = mark_client_expired_locked(conf); |
| 3078 | if (status) |
| 3079 | goto out; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3080 | clp = conf; |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3081 | } else if (unconf) |
| 3082 | clp = unconf; |
| 3083 | else { |
| 3084 | status = nfserr_stale_clientid; |
| 3085 | goto out; |
| 3086 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3087 | if (!nfsd4_mach_creds_match(clp, rqstp)) { |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3088 | clp = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3089 | status = nfserr_wrong_cred; |
| 3090 | goto out; |
| 3091 | } |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3092 | unhash_client_locked(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3093 | out: |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3094 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3095 | if (clp) |
| 3096 | expire_client(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3097 | return status; |
| 3098 | } |
| 3099 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3100 | __be32 |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3101 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc) |
| 3102 | { |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3103 | __be32 status = 0; |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3104 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3105 | if (rc->rca_one_fs) { |
| 3106 | if (!cstate->current_fh.fh_dentry) |
| 3107 | return nfserr_nofilehandle; |
| 3108 | /* |
| 3109 | * We don't take advantage of the rca_one_fs case. |
| 3110 | * That's OK, it's optional, we can safely ignore it. |
| 3111 | */ |
Christophe JAILLET | d28c442 | 2016-07-02 08:24:32 +0200 | [diff] [blame] | 3112 | return nfs_ok; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3113 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3114 | |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3115 | status = nfserr_complete_already; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 3116 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
| 3117 | &cstate->session->se_client->cl_flags)) |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3118 | goto out; |
| 3119 | |
| 3120 | status = nfserr_stale_clientid; |
| 3121 | if (is_client_expired(cstate->session->se_client)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3122 | /* |
| 3123 | * The following error isn't really legal. |
| 3124 | * But we only get here if the client just explicitly |
| 3125 | * destroyed the client. Surely it no longer cares what |
| 3126 | * error it gets back on an operation for the dead |
| 3127 | * client. |
| 3128 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3129 | goto out; |
| 3130 | |
| 3131 | status = nfs_ok; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 3132 | nfsd4_client_record_create(cstate->session->se_client); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3133 | out: |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3134 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3138 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3139 | struct nfsd4_setclientid *setclid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3140 | { |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 3141 | struct xdr_netobj clname = setclid->se_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | nfs4_verifier clverifier = setclid->se_verf; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3143 | struct nfs4_client *conf, *new; |
| 3144 | struct nfs4_client *unconf = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3145 | __be32 status; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3146 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 3147 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3148 | new = create_client(clname, rqstp, &clverifier); |
| 3149 | if (new == NULL) |
| 3150 | return nfserr_jukebox; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3151 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3152 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3153 | conf = find_confirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 3154 | if (conf && client_has_state(conf)) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3155 | /* case 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 3157 | if (clp_used_exchangeid(conf)) |
| 3158 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 3159 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3160 | char addr_str[INET6_ADDRSTRLEN]; |
| 3161 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, |
| 3162 | sizeof(addr_str)); |
| 3163 | dprintk("NFSD: setclientid: string in use by client " |
| 3164 | "at %s\n", addr_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3165 | goto out; |
| 3166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 3168 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 3169 | if (unconf) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3170 | unhash_client_locked(unconf); |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3171 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3172 | /* case 1: probable callback update */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3173 | copy_clid(new, conf); |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3174 | gen_confirm(new, nn); |
| 3175 | } else /* case 4 (new client) or cases 2, 3 (client reboot): */ |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3176 | gen_clid(new, nn); |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 3177 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 3178 | gen_callback(new, setclid, rqstp); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 3179 | add_to_unconfirmed(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3180 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 3181 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 3182 | 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] | 3183 | new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | status = nfs_ok; |
| 3185 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3186 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3187 | if (new) |
| 3188 | free_client(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3189 | if (unconf) |
| 3190 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | return status; |
| 3192 | } |
| 3193 | |
| 3194 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3195 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3196 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
| 3197 | struct nfsd4_compound_state *cstate, |
| 3198 | struct nfsd4_setclientid_confirm *setclientid_confirm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | { |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3200 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3201 | struct nfs4_client *old = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 3203 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3204 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 3205 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 3207 | if (STALE_CLIENTID(clid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | return nfserr_stale_clientid; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3209 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3210 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3211 | conf = find_confirmed_client(clid, false, nn); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3212 | unconf = find_unconfirmed_client(clid, false, nn); |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3213 | /* |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3214 | * We try hard to give out unique clientid's, so if we get an |
| 3215 | * attempt to confirm the same clientid with a different cred, |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 3216 | * the client may be buggy; this should never happen. |
| 3217 | * |
| 3218 | * Nevertheless, RFC 7530 recommends INUSE for this case: |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3219 | */ |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 3220 | status = nfserr_clid_inuse; |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3221 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
| 3222 | goto out; |
| 3223 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
| 3224 | goto out; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3225 | /* cases below refer to rfc 3530 section 14.2.34: */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3226 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
| 3227 | if (conf && !unconf) /* case 2: probable retransmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3228 | status = nfs_ok; |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3229 | else /* case 4: client hasn't noticed we rebooted yet? */ |
| 3230 | status = nfserr_stale_clientid; |
| 3231 | goto out; |
| 3232 | } |
| 3233 | status = nfs_ok; |
| 3234 | if (conf) { /* case 1: callback update */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3235 | old = unconf; |
| 3236 | unhash_client_locked(old); |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3237 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3238 | } else { /* case 3: normal case; new or rebooted client */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3239 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
| 3240 | if (old) { |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 3241 | status = nfserr_clid_inuse; |
| 3242 | if (client_has_state(old) |
| 3243 | && !same_creds(&unconf->cl_cred, |
| 3244 | &old->cl_cred)) |
| 3245 | goto out; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3246 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3247 | if (status) { |
| 3248 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3249 | goto out; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3250 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3251 | } |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3252 | move_to_confirmed(unconf); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3253 | conf = unconf; |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 3254 | } |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3255 | get_client_locked(conf); |
| 3256 | spin_unlock(&nn->client_lock); |
| 3257 | nfsd4_probe_callback(conf); |
| 3258 | spin_lock(&nn->client_lock); |
| 3259 | put_client_renew_locked(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | out: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3261 | spin_unlock(&nn->client_lock); |
| 3262 | if (old) |
| 3263 | expire_client(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | return status; |
| 3265 | } |
| 3266 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3267 | static struct nfs4_file *nfsd4_alloc_file(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3268 | { |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3269 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 3270 | } |
| 3271 | |
| 3272 | /* OPEN Share state helper functions */ |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3273 | static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval, |
| 3274 | struct nfs4_file *fp) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3275 | { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3276 | lockdep_assert_held(&state_lock); |
| 3277 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3278 | atomic_set(&fp->fi_ref, 1); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3279 | spin_lock_init(&fp->fi_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3280 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 3281 | INIT_LIST_HEAD(&fp->fi_delegations); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3282 | INIT_LIST_HEAD(&fp->fi_clnt_odstate); |
Trond Myklebust | e2cf80d | 2014-07-23 16:17:38 -0400 | [diff] [blame] | 3283 | fh_copy_shallow(&fp->fi_fhandle, fh); |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 3284 | fp->fi_deleg_file = NULL; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3285 | fp->fi_had_conflict = false; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3286 | fp->fi_share_deny = 0; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3287 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 3288 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 3289 | #ifdef CONFIG_NFSD_PNFS |
| 3290 | INIT_LIST_HEAD(&fp->fi_lo_states); |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 3291 | atomic_set(&fp->fi_lo_recalls, 0); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 3292 | #endif |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3293 | hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | } |
| 3295 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 3296 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3297 | nfsd4_free_slabs(void) |
| 3298 | { |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3299 | kmem_cache_destroy(odstate_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3300 | kmem_cache_destroy(openowner_slab); |
| 3301 | kmem_cache_destroy(lockowner_slab); |
| 3302 | kmem_cache_destroy(file_slab); |
| 3303 | kmem_cache_destroy(stateid_slab); |
| 3304 | kmem_cache_destroy(deleg_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | |
Bryan Schumaker | 7208339 | 2011-11-01 15:24:59 -0400 | [diff] [blame] | 3307 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | nfsd4_init_slabs(void) |
| 3309 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3310 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 3311 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 3312 | if (openowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3313 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3314 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
Yanchuan Nian | 3c40794 | 2012-10-24 14:44:19 +0800 | [diff] [blame] | 3315 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3316 | if (lockowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3317 | goto out_free_openowner_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3318 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3319 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3320 | if (file_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3321 | goto out_free_lockowner_slab; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 3322 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3323 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 3324 | if (stateid_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3325 | goto out_free_file_slab; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 3326 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3327 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 3328 | if (deleg_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3329 | goto out_free_stateid_slab; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3330 | odstate_slab = kmem_cache_create("nfsd4_odstate", |
| 3331 | sizeof(struct nfs4_clnt_odstate), 0, 0, NULL); |
| 3332 | if (odstate_slab == NULL) |
| 3333 | goto out_free_deleg_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | return 0; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3335 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3336 | out_free_deleg_slab: |
| 3337 | kmem_cache_destroy(deleg_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3338 | out_free_stateid_slab: |
| 3339 | kmem_cache_destroy(stateid_slab); |
| 3340 | out_free_file_slab: |
| 3341 | kmem_cache_destroy(file_slab); |
| 3342 | out_free_lockowner_slab: |
| 3343 | kmem_cache_destroy(lockowner_slab); |
| 3344 | out_free_openowner_slab: |
| 3345 | kmem_cache_destroy(openowner_slab); |
| 3346 | out: |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3347 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 3348 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | } |
| 3350 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3351 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 3352 | { |
| 3353 | rp->rp_status = nfserr_serverfault; |
| 3354 | rp->rp_buflen = 0; |
| 3355 | rp->rp_buf = rp->rp_ibuf; |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 3356 | mutex_init(&rp->rp_mutex); |
| 3357 | } |
| 3358 | |
| 3359 | static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, |
| 3360 | struct nfs4_stateowner *so) |
| 3361 | { |
| 3362 | if (!nfsd4_has_session(cstate)) { |
| 3363 | mutex_lock(&so->so_replay.rp_mutex); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 3364 | cstate->replay_owner = nfs4_get_stateowner(so); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 3365 | } |
| 3366 | } |
| 3367 | |
| 3368 | void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) |
| 3369 | { |
| 3370 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 3371 | |
| 3372 | if (so != NULL) { |
| 3373 | cstate->replay_owner = NULL; |
| 3374 | mutex_unlock(&so->so_replay.rp_mutex); |
| 3375 | nfs4_put_stateowner(so); |
| 3376 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3377 | } |
| 3378 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3379 | 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] | 3380 | { |
| 3381 | struct nfs4_stateowner *sop; |
| 3382 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3383 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3384 | if (!sop) |
| 3385 | return NULL; |
| 3386 | |
| 3387 | sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL); |
| 3388 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3389 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3390 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3391 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3392 | sop->so_owner.len = owner->len; |
| 3393 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3394 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3395 | sop->so_client = clp; |
| 3396 | init_nfs4_replay(&sop->so_replay); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3397 | atomic_set(&sop->so_count, 1); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3398 | return sop; |
| 3399 | } |
| 3400 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3401 | 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] | 3402 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3403 | lockdep_assert_held(&clp->cl_lock); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 3404 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3405 | list_add(&oo->oo_owner.so_strhash, |
| 3406 | &clp->cl_ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3407 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | } |
| 3409 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 3410 | static void nfs4_unhash_openowner(struct nfs4_stateowner *so) |
| 3411 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3412 | unhash_openowner_locked(openowner(so)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 3413 | } |
| 3414 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3415 | static void nfs4_free_openowner(struct nfs4_stateowner *so) |
| 3416 | { |
| 3417 | struct nfs4_openowner *oo = openowner(so); |
| 3418 | |
| 3419 | kmem_cache_free(openowner_slab, oo); |
| 3420 | } |
| 3421 | |
| 3422 | static const struct nfs4_stateowner_operations openowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 3423 | .so_unhash = nfs4_unhash_openowner, |
| 3424 | .so_free = nfs4_free_openowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3425 | }; |
| 3426 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3427 | static struct nfs4_ol_stateid * |
| 3428 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 3429 | { |
| 3430 | struct nfs4_ol_stateid *local, *ret = NULL; |
| 3431 | struct nfs4_openowner *oo = open->op_openowner; |
| 3432 | |
| 3433 | lockdep_assert_held(&fp->fi_lock); |
| 3434 | |
| 3435 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
| 3436 | /* ignore lock owners */ |
| 3437 | if (local->st_stateowner->so_is_open_owner == 0) |
| 3438 | continue; |
| 3439 | if (local->st_stateowner == &oo->oo_owner) { |
| 3440 | ret = local; |
| 3441 | atomic_inc(&ret->st_stid.sc_count); |
| 3442 | break; |
| 3443 | } |
| 3444 | } |
| 3445 | return ret; |
| 3446 | } |
| 3447 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3448 | static struct nfs4_openowner * |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3449 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 3450 | struct nfsd4_compound_state *cstate) |
| 3451 | { |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3452 | struct nfs4_client *clp = cstate->clp; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 3453 | struct nfs4_openowner *oo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3455 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 3456 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | return NULL; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3458 | oo->oo_owner.so_ops = &openowner_ops; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3459 | oo->oo_owner.so_is_open_owner = 1; |
| 3460 | oo->oo_owner.so_seqid = open->op_seqid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3461 | oo->oo_flags = 0; |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 3462 | if (nfsd4_has_session(cstate)) |
| 3463 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3464 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 3465 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3466 | INIT_LIST_HEAD(&oo->oo_close_lru); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3467 | spin_lock(&clp->cl_lock); |
| 3468 | ret = find_openstateowner_str_locked(strhashval, open, clp); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 3469 | if (ret == NULL) { |
| 3470 | hash_openowner(oo, clp, strhashval); |
| 3471 | ret = oo; |
| 3472 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 3473 | nfs4_free_stateowner(&oo->oo_owner); |
| 3474 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3475 | spin_unlock(&clp->cl_lock); |
Jeff Layton | c595233 | 2015-03-23 10:53:42 -0400 | [diff] [blame] | 3476 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3477 | } |
| 3478 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3479 | static struct nfs4_ol_stateid * |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3480 | init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3481 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3482 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3483 | struct nfs4_openowner *oo = open->op_openowner; |
| 3484 | struct nfs4_ol_stateid *retstp = NULL; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3485 | struct nfs4_ol_stateid *stp; |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3486 | |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3487 | stp = open->op_stp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 3488 | /* We are moving these outside of the spinlocks to avoid the warnings */ |
| 3489 | mutex_init(&stp->st_mutex); |
| 3490 | mutex_lock(&stp->st_mutex); |
| 3491 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3492 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
| 3493 | spin_lock(&fp->fi_lock); |
| 3494 | |
| 3495 | retstp = nfsd4_find_existing_open(fp, open); |
| 3496 | if (retstp) |
| 3497 | goto out_unlock; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3498 | |
| 3499 | open->op_stp = NULL; |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 3500 | atomic_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 3501 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 3502 | INIT_LIST_HEAD(&stp->st_locks); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 3503 | stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3504 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3505 | stp->st_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | stp->st_access_bmap = 0; |
| 3507 | stp->st_deny_bmap = 0; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 3508 | stp->st_openstp = NULL; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 3509 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3510 | list_add(&stp->st_perfile, &fp->fi_stateids); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 3511 | |
| 3512 | out_unlock: |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3513 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 3514 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 3515 | if (retstp) { |
| 3516 | mutex_lock(&retstp->st_mutex); |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3517 | /* To keep mutex tracking happy */ |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 3518 | mutex_unlock(&stp->st_mutex); |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3519 | stp = retstp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 3520 | } |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 3521 | return stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3522 | } |
| 3523 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3524 | /* |
| 3525 | * In the 4.0 case we need to keep the owners around a little while to handle |
| 3526 | * CLOSE replay. We still do need to release any file access that is held by |
| 3527 | * them before returning however. |
| 3528 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | static void |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3530 | move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 3532 | struct nfs4_ol_stateid *last; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3533 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 3534 | struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, |
| 3535 | nfsd_net_id); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3536 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3537 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3538 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 3539 | /* |
| 3540 | * We know that we hold one reference via nfsd4_close, and another |
| 3541 | * "persistent" reference for the client. If the refcount is higher |
| 3542 | * than 2, then there are still calls in progress that are using this |
| 3543 | * stateid. We can't put the sc_file reference until they are finished. |
| 3544 | * Wait for the refcount to drop to 2. Since it has been unhashed, |
| 3545 | * there should be no danger of the refcount going back up again at |
| 3546 | * this point. |
| 3547 | */ |
| 3548 | wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2); |
| 3549 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3550 | release_all_access(s); |
| 3551 | if (s->st_stid.sc_file) { |
| 3552 | put_nfs4_file(s->st_stid.sc_file); |
| 3553 | s->st_stid.sc_file = NULL; |
| 3554 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 3555 | |
| 3556 | spin_lock(&nn->client_lock); |
| 3557 | last = oo->oo_last_closed_stid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3558 | oo->oo_last_closed_stid = s; |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3559 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3560 | oo->oo_time = get_seconds(); |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 3561 | spin_unlock(&nn->client_lock); |
| 3562 | if (last) |
| 3563 | nfs4_put_stid(&last->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | } |
| 3565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | /* search file_hashtbl[] for file */ |
| 3567 | static struct nfs4_file * |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3568 | find_file_locked(struct knfsd_fh *fh, unsigned int hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3569 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | struct nfs4_file *fp; |
| 3571 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3572 | hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) { |
Christoph Hellwig | 4d94c2e | 2014-08-14 08:41:48 +0200 | [diff] [blame] | 3573 | if (fh_match(&fp->fi_fhandle, fh)) { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3574 | if (atomic_inc_not_zero(&fp->fi_ref)) |
| 3575 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | } |
| 3578 | return NULL; |
| 3579 | } |
| 3580 | |
Christoph Hellwig | e6ba76e | 2014-08-14 08:50:16 +0200 | [diff] [blame] | 3581 | struct nfs4_file * |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3582 | find_file(struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3583 | { |
| 3584 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3585 | unsigned int hashval = file_hashval(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3586 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3587 | rcu_read_lock(); |
| 3588 | fp = find_file_locked(fh, hashval); |
| 3589 | rcu_read_unlock(); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3590 | return fp; |
| 3591 | } |
| 3592 | |
| 3593 | static struct nfs4_file * |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 3594 | find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3595 | { |
| 3596 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3597 | unsigned int hashval = file_hashval(fh); |
| 3598 | |
| 3599 | rcu_read_lock(); |
| 3600 | fp = find_file_locked(fh, hashval); |
| 3601 | rcu_read_unlock(); |
| 3602 | if (fp) |
| 3603 | return fp; |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3604 | |
| 3605 | spin_lock(&state_lock); |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3606 | fp = find_file_locked(fh, hashval); |
| 3607 | if (likely(fp == NULL)) { |
| 3608 | nfsd4_init_file(fh, hashval, new); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3609 | fp = new; |
| 3610 | } |
| 3611 | spin_unlock(&state_lock); |
| 3612 | |
| 3613 | return fp; |
| 3614 | } |
| 3615 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 3616 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3617 | * Called to check deny when READ with all zero stateid or |
| 3618 | * WRITE with all zero or all one stateid |
| 3619 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3620 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 3622 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | struct nfs4_file *fp; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3624 | __be32 ret = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3625 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3626 | fp = find_file(¤t_fh->fh_handle); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3627 | if (!fp) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3628 | return ret; |
| 3629 | /* Check for conflicting share reservations */ |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3630 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3631 | if (fp->fi_share_deny & deny_type) |
| 3632 | ret = nfserr_locked; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3633 | spin_unlock(&fp->fi_lock); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3634 | put_nfs4_file(fp); |
| 3635 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3636 | } |
| 3637 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3638 | static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3639 | { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3640 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3641 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
| 3642 | nfsd_net_id); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 3643 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3644 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 3645 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3646 | /* |
| 3647 | * 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] | 3648 | * already holding inode->i_lock. |
| 3649 | * |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3650 | * If the dl_time != 0, then we know that it has already been |
| 3651 | * queued for a lease break. Don't queue it again. |
| 3652 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 3653 | spin_lock(&state_lock); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3654 | if (dp->dl_time == 0) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3655 | dp->dl_time = get_seconds(); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3656 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3657 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3658 | spin_unlock(&state_lock); |
| 3659 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3661 | static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, |
| 3662 | struct rpc_task *task) |
| 3663 | { |
| 3664 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 3665 | |
Andrew Elble | a457974 | 2015-08-31 12:06:41 -0400 | [diff] [blame] | 3666 | if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID) |
| 3667 | return 1; |
| 3668 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3669 | switch (task->tk_status) { |
| 3670 | case 0: |
| 3671 | return 1; |
| 3672 | case -EBADHANDLE: |
| 3673 | case -NFS4ERR_BAD_STATEID: |
| 3674 | /* |
| 3675 | * Race: client probably got cb_recall before open reply |
| 3676 | * granting delegation. |
| 3677 | */ |
| 3678 | if (dp->dl_retries--) { |
| 3679 | rpc_delay(task, 2 * HZ); |
| 3680 | return 0; |
| 3681 | } |
| 3682 | /*FALLTHRU*/ |
| 3683 | default: |
| 3684 | return -1; |
| 3685 | } |
| 3686 | } |
| 3687 | |
| 3688 | static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) |
| 3689 | { |
| 3690 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 3691 | |
| 3692 | nfs4_put_stid(&dp->dl_stid); |
| 3693 | } |
| 3694 | |
Julia Lawall | c4cb897 | 2015-11-21 22:57:39 +0100 | [diff] [blame] | 3695 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3696 | .prepare = nfsd4_cb_recall_prepare, |
| 3697 | .done = nfsd4_cb_recall_done, |
| 3698 | .release = nfsd4_cb_recall_release, |
| 3699 | }; |
| 3700 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3701 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
| 3702 | { |
| 3703 | /* |
| 3704 | * We're assuming the state code never drops its reference |
| 3705 | * without first removing the lease. Since we're in this lease |
| 3706 | * callback (and since the lease code is serialized by the kernel |
| 3707 | * lock) we know the server hasn't removed the lease yet, we know |
| 3708 | * it's safe to take a reference. |
| 3709 | */ |
Trond Myklebust | 72c0b0f | 2014-07-21 09:34:58 -0400 | [diff] [blame] | 3710 | atomic_inc(&dp->dl_stid.sc_count); |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 3711 | nfsd4_run_cb(&dp->dl_recall); |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3712 | } |
| 3713 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 3714 | /* Called from break_lease() with i_lock held. */ |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3715 | static bool |
| 3716 | nfsd_break_deleg_cb(struct file_lock *fl) |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3717 | { |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3718 | bool ret = false; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3719 | struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; |
| 3720 | struct nfs4_delegation *dp; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3721 | |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 3722 | if (!fp) { |
| 3723 | WARN(1, "(%p)->fl_owner NULL\n", fl); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3724 | return ret; |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 3725 | } |
| 3726 | if (fp->fi_had_conflict) { |
| 3727 | WARN(1, "duplicate break on %p\n", fp); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3728 | return ret; |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 3729 | } |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3730 | /* |
| 3731 | * 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] | 3732 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3733 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3734 | */ |
| 3735 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3736 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3737 | spin_lock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3738 | fp->fi_had_conflict = true; |
| 3739 | /* |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3740 | * If there are no delegations on the list, then return true |
| 3741 | * so that the lease code will go ahead and delete it. |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3742 | */ |
| 3743 | if (list_empty(&fp->fi_delegations)) |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3744 | ret = true; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3745 | else |
| 3746 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) |
| 3747 | nfsd_break_one_deleg(dp); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3748 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3749 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | } |
| 3751 | |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 3752 | static int |
Jeff Layton | 7448cc3 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 3753 | nfsd_change_deleg_cb(struct file_lock *onlist, int arg, |
| 3754 | struct list_head *dispose) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | { |
| 3756 | if (arg & F_UNLCK) |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 3757 | return lease_modify(onlist, arg, dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3758 | else |
| 3759 | return -EAGAIN; |
| 3760 | } |
| 3761 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 3762 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 3763 | .lm_break = nfsd_break_deleg_cb, |
| 3764 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | }; |
| 3766 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 3767 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 3768 | { |
| 3769 | if (nfsd4_has_session(cstate)) |
| 3770 | return nfs_ok; |
| 3771 | if (seqid == so->so_seqid - 1) |
| 3772 | return nfserr_replay_me; |
| 3773 | if (seqid == so->so_seqid) |
| 3774 | return nfs_ok; |
| 3775 | return nfserr_bad_seqid; |
| 3776 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3777 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3778 | static __be32 lookup_clientid(clientid_t *clid, |
| 3779 | struct nfsd4_compound_state *cstate, |
| 3780 | struct nfsd_net *nn) |
| 3781 | { |
| 3782 | struct nfs4_client *found; |
| 3783 | |
| 3784 | if (cstate->clp) { |
| 3785 | found = cstate->clp; |
| 3786 | if (!same_clid(&found->cl_clientid, clid)) |
| 3787 | return nfserr_stale_clientid; |
| 3788 | return nfs_ok; |
| 3789 | } |
| 3790 | |
| 3791 | if (STALE_CLIENTID(clid, nn)) |
| 3792 | return nfserr_stale_clientid; |
| 3793 | |
| 3794 | /* |
| 3795 | * For v4.1+ we get the client in the SEQUENCE op. If we don't have one |
| 3796 | * cached already then we know this is for is for v4.0 and "sessions" |
| 3797 | * will be false. |
| 3798 | */ |
| 3799 | WARN_ON_ONCE(cstate->session); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 3800 | spin_lock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3801 | found = find_confirmed_client(clid, false, nn); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 3802 | if (!found) { |
| 3803 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3804 | return nfserr_expired; |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 3805 | } |
| 3806 | atomic_inc(&found->cl_refcount); |
| 3807 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3808 | |
| 3809 | /* Cache the nfs4_client in cstate! */ |
| 3810 | cstate->clp = found; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3811 | return nfs_ok; |
| 3812 | } |
| 3813 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3814 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3815 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 3816 | struct nfsd4_open *open, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3817 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3818 | clientid_t *clientid = &open->op_clientid; |
| 3819 | struct nfs4_client *clp = NULL; |
| 3820 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3821 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3822 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3823 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 3824 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | return nfserr_stale_clientid; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3826 | /* |
| 3827 | * In case we need it later, after we've already created the |
| 3828 | * file and don't want to risk a further failure: |
| 3829 | */ |
| 3830 | open->op_file = nfsd4_alloc_file(); |
| 3831 | if (open->op_file == NULL) |
| 3832 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3833 | |
Trond Myklebust | 2d91e89 | 2014-06-30 11:48:46 -0400 | [diff] [blame] | 3834 | status = lookup_clientid(clientid, cstate, nn); |
| 3835 | if (status) |
| 3836 | return status; |
| 3837 | clp = cstate->clp; |
| 3838 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3839 | strhashval = ownerstr_hashval(&open->op_owner); |
| 3840 | oo = find_openstateowner_str(strhashval, open, clp); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3841 | open->op_openowner = oo; |
| 3842 | if (!oo) { |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3843 | goto new_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3844 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3845 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3846 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3847 | release_openowner(oo); |
| 3848 | open->op_openowner = NULL; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3849 | goto new_owner; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3850 | } |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3851 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 3852 | if (status) |
| 3853 | return status; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3854 | goto alloc_stateid; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3855 | new_owner: |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3856 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3857 | if (oo == NULL) |
| 3858 | return nfserr_jukebox; |
| 3859 | open->op_openowner = oo; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3860 | alloc_stateid: |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 3861 | open->op_stp = nfs4_alloc_open_stateid(clp); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3862 | if (!open->op_stp) |
| 3863 | return nfserr_jukebox; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3864 | |
| 3865 | if (nfsd4_has_session(cstate) && |
| 3866 | (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { |
| 3867 | open->op_odstate = alloc_clnt_odstate(clp); |
| 3868 | if (!open->op_odstate) |
| 3869 | return nfserr_jukebox; |
| 3870 | } |
| 3871 | |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3872 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | } |
| 3874 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3875 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 3876 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 3877 | { |
| 3878 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 3879 | return nfserr_openmode; |
| 3880 | else |
| 3881 | return nfs_ok; |
| 3882 | } |
| 3883 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 3884 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3885 | { |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3886 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 3887 | } |
| 3888 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3889 | 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] | 3890 | { |
| 3891 | struct nfs4_stid *ret; |
| 3892 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3893 | ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 3894 | if (!ret) |
| 3895 | return NULL; |
| 3896 | return delegstateid(ret); |
| 3897 | } |
| 3898 | |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3899 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 3900 | { |
| 3901 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 3902 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 3903 | } |
| 3904 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3905 | static __be32 |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 3906 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3907 | struct nfs4_delegation **dp) |
| 3908 | { |
| 3909 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3910 | __be32 status = nfserr_bad_stateid; |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 3911 | struct nfs4_delegation *deleg; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3912 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 3913 | deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); |
| 3914 | if (deleg == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3915 | goto out; |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3916 | flags = share_access_to_flags(open->op_share_access); |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 3917 | status = nfs4_check_delegmode(deleg, flags); |
| 3918 | if (status) { |
| 3919 | nfs4_put_stid(&deleg->dl_stid); |
| 3920 | goto out; |
| 3921 | } |
| 3922 | *dp = deleg; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3923 | out: |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3924 | if (!nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3925 | return nfs_ok; |
| 3926 | if (status) |
| 3927 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3928 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3929 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3930 | } |
| 3931 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 3932 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 3933 | { |
| 3934 | int flags = 0; |
| 3935 | |
| 3936 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 3937 | flags |= NFSD_MAY_READ; |
| 3938 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 3939 | flags |= NFSD_MAY_WRITE; |
| 3940 | return flags; |
| 3941 | } |
| 3942 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3943 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 3945 | struct nfsd4_open *open) |
| 3946 | { |
| 3947 | struct iattr iattr = { |
| 3948 | .ia_valid = ATTR_SIZE, |
| 3949 | .ia_size = 0, |
| 3950 | }; |
| 3951 | if (!open->op_truncate) |
| 3952 | return 0; |
| 3953 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 3954 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); |
| 3956 | } |
| 3957 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3958 | 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] | 3959 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
| 3960 | struct nfsd4_open *open) |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3961 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3962 | struct file *filp = NULL; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3963 | __be32 status; |
| 3964 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 3965 | int access = nfs4_access_to_access(open->op_share_access); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3966 | unsigned char old_access_bmap, old_deny_bmap; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3967 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3968 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3969 | |
| 3970 | /* |
| 3971 | * Are we trying to set a deny mode that would conflict with |
| 3972 | * current access? |
| 3973 | */ |
| 3974 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 3975 | if (status != nfs_ok) { |
| 3976 | spin_unlock(&fp->fi_lock); |
| 3977 | goto out; |
| 3978 | } |
| 3979 | |
| 3980 | /* set access to the file */ |
| 3981 | status = nfs4_file_get_access(fp, open->op_share_access); |
| 3982 | if (status != nfs_ok) { |
| 3983 | spin_unlock(&fp->fi_lock); |
| 3984 | goto out; |
| 3985 | } |
| 3986 | |
| 3987 | /* Set access bits in stateid */ |
| 3988 | old_access_bmap = stp->st_access_bmap; |
| 3989 | set_access(open->op_share_access, stp); |
| 3990 | |
| 3991 | /* Set new deny mask */ |
| 3992 | old_deny_bmap = stp->st_deny_bmap; |
| 3993 | set_deny(open->op_share_deny, stp); |
| 3994 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 3995 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3996 | if (!fp->fi_fds[oflag]) { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3997 | spin_unlock(&fp->fi_lock); |
| 3998 | status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3999 | if (status) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4000 | goto out_put_access; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4001 | spin_lock(&fp->fi_lock); |
| 4002 | if (!fp->fi_fds[oflag]) { |
| 4003 | fp->fi_fds[oflag] = filp; |
| 4004 | filp = NULL; |
| 4005 | } |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4006 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4007 | spin_unlock(&fp->fi_lock); |
| 4008 | if (filp) |
| 4009 | fput(filp); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4010 | |
| 4011 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 4012 | if (status) |
| 4013 | goto out_put_access; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4014 | out: |
| 4015 | return status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4016 | out_put_access: |
| 4017 | stp->st_access_bmap = old_access_bmap; |
| 4018 | nfs4_file_put_access(fp, open->op_share_access); |
| 4019 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); |
| 4020 | goto out; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4021 | } |
| 4022 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4023 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4024 | 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] | 4025 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4026 | __be32 status; |
Arnd Bergmann | 6ac7536 | 2015-05-12 23:31:29 +0200 | [diff] [blame] | 4027 | unsigned char old_deny_bmap = stp->st_deny_bmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4028 | |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4029 | if (!test_access(open->op_share_access, stp)) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4030 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4031 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4032 | /* test and set deny mode */ |
| 4033 | spin_lock(&fp->fi_lock); |
| 4034 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 4035 | if (status == nfs_ok) { |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4036 | set_deny(open->op_share_deny, stp); |
| 4037 | fp->fi_share_deny |= |
| 4038 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 4039 | } |
| 4040 | spin_unlock(&fp->fi_lock); |
| 4041 | |
| 4042 | if (status != nfs_ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4044 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4045 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 4046 | if (status != nfs_ok) |
| 4047 | reset_union_bmap_deny(old_deny_bmap, stp); |
| 4048 | return status; |
| 4049 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4050 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 4051 | /* Should we give out recallable state?: */ |
| 4052 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 4053 | { |
| 4054 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 4055 | return true; |
| 4056 | /* |
| 4057 | * In the sessions case, since we don't have to establish a |
| 4058 | * separate connection for callbacks, we assume it's OK |
| 4059 | * until we hear otherwise: |
| 4060 | */ |
| 4061 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 4062 | } |
| 4063 | |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 4064 | 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] | 4065 | { |
| 4066 | struct file_lock *fl; |
| 4067 | |
| 4068 | fl = locks_alloc_lock(); |
| 4069 | if (!fl) |
| 4070 | return NULL; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4071 | fl->fl_lmops = &nfsd_lease_mng_ops; |
J. Bruce Fields | 617588d | 2011-07-01 15:18:34 -0400 | [diff] [blame] | 4072 | fl->fl_flags = FL_DELEG; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4073 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 4074 | fl->fl_end = OFFSET_MAX; |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 4075 | fl->fl_owner = (fl_owner_t)fp; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4076 | fl->fl_pid = current->tgid; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4077 | return fl; |
| 4078 | } |
| 4079 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4080 | /** |
| 4081 | * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer |
| 4082 | * @dp: a pointer to the nfs4_delegation we're adding. |
| 4083 | * |
| 4084 | * Return: |
| 4085 | * On success: Return code will be 0 on success. |
| 4086 | * |
| 4087 | * On error: -EAGAIN if there was an existing delegation. |
| 4088 | * nonzero if there is an error in other cases. |
| 4089 | * |
| 4090 | */ |
| 4091 | |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4092 | static int nfs4_setlease(struct nfs4_delegation *dp) |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 4093 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4094 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Kinglong Mee | efde6b4 | 2015-07-13 17:33:24 +0800 | [diff] [blame] | 4095 | struct file_lock *fl; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4096 | struct file *filp; |
| 4097 | int status = 0; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 4098 | |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 4099 | fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 4100 | if (!fl) |
| 4101 | return -ENOMEM; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4102 | filp = find_readable_file(fp); |
| 4103 | if (!filp) { |
| 4104 | /* We should always have a readable file here */ |
| 4105 | WARN_ON_ONCE(1); |
Kinglong Mee | af9dbaf | 2015-07-13 17:30:51 +0800 | [diff] [blame] | 4106 | locks_free_lock(fl); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4107 | return -EBADF; |
| 4108 | } |
| 4109 | fl->fl_file = filp; |
Jeff Layton | e6f5c78 | 2014-08-22 10:40:25 -0400 | [diff] [blame] | 4110 | status = vfs_setlease(filp, fl->fl_type, &fl, NULL); |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 4111 | if (fl) |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4112 | locks_free_lock(fl); |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 4113 | if (status) |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4114 | goto out_fput; |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4115 | spin_lock(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4116 | spin_lock(&fp->fi_lock); |
| 4117 | /* Did the lease get broken before we took the lock? */ |
| 4118 | status = -EAGAIN; |
| 4119 | if (fp->fi_had_conflict) |
| 4120 | goto out_unlock; |
| 4121 | /* Race breaker */ |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 4122 | if (fp->fi_deleg_file) { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4123 | status = hash_delegation_locked(dp, fp); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4124 | goto out_unlock; |
| 4125 | } |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4126 | fp->fi_deleg_file = filp; |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4127 | fp->fi_delegees = 0; |
| 4128 | status = hash_delegation_locked(dp, fp); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4129 | spin_unlock(&fp->fi_lock); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4130 | spin_unlock(&state_lock); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4131 | if (status) { |
| 4132 | /* Should never happen, this is a new fi_deleg_file */ |
| 4133 | WARN_ON_ONCE(1); |
| 4134 | goto out_fput; |
| 4135 | } |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4136 | return 0; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4137 | out_unlock: |
| 4138 | spin_unlock(&fp->fi_lock); |
| 4139 | spin_unlock(&state_lock); |
| 4140 | out_fput: |
| 4141 | fput(filp); |
J. Bruce Fields | e873088 | 2012-01-23 13:52:01 -0500 | [diff] [blame] | 4142 | return status; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4143 | } |
| 4144 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4145 | static struct nfs4_delegation * |
| 4146 | nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4147 | struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4148 | { |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4149 | int status; |
| 4150 | struct nfs4_delegation *dp; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4151 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 4152 | if (fp->fi_had_conflict) |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4153 | return ERR_PTR(-EAGAIN); |
| 4154 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4155 | spin_lock(&state_lock); |
| 4156 | spin_lock(&fp->fi_lock); |
| 4157 | status = nfs4_get_existing_delegation(clp, fp); |
| 4158 | spin_unlock(&fp->fi_lock); |
| 4159 | spin_unlock(&state_lock); |
| 4160 | |
| 4161 | if (status) |
| 4162 | return ERR_PTR(status); |
| 4163 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4164 | dp = alloc_init_deleg(clp, fh, odstate); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4165 | if (!dp) |
| 4166 | return ERR_PTR(-ENOMEM); |
| 4167 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 4168 | get_nfs4_file(fp); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4169 | spin_lock(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4170 | spin_lock(&fp->fi_lock); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4171 | dp->dl_stid.sc_file = fp; |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 4172 | if (!fp->fi_deleg_file) { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4173 | spin_unlock(&fp->fi_lock); |
| 4174 | spin_unlock(&state_lock); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4175 | status = nfs4_setlease(dp); |
| 4176 | goto out; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4177 | } |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4178 | if (fp->fi_had_conflict) { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4179 | status = -EAGAIN; |
| 4180 | goto out_unlock; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4181 | } |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4182 | status = hash_delegation_locked(dp, fp); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4183 | out_unlock: |
| 4184 | spin_unlock(&fp->fi_lock); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4185 | spin_unlock(&state_lock); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4186 | out: |
| 4187 | if (status) { |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4188 | put_clnt_odstate(dp->dl_clnt_odstate); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 4189 | nfs4_put_stid(&dp->dl_stid); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4190 | return ERR_PTR(status); |
| 4191 | } |
| 4192 | return dp; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 4193 | } |
| 4194 | |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 4195 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 4196 | { |
| 4197 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4198 | if (status == -EAGAIN) |
| 4199 | open->op_why_no_deleg = WND4_CONTENTION; |
| 4200 | else { |
| 4201 | open->op_why_no_deleg = WND4_RESOURCE; |
| 4202 | switch (open->op_deleg_want) { |
| 4203 | case NFS4_SHARE_WANT_READ_DELEG: |
| 4204 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 4205 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 4206 | break; |
| 4207 | case NFS4_SHARE_WANT_CANCEL: |
| 4208 | open->op_why_no_deleg = WND4_CANCELLED; |
| 4209 | break; |
| 4210 | case NFS4_SHARE_WANT_NO_DELEG: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4211 | WARN_ON_ONCE(1); |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 4212 | } |
| 4213 | } |
| 4214 | } |
| 4215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | /* |
| 4217 | * Attempt to hand out a delegation. |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4218 | * |
| 4219 | * Note we don't support write delegations, and won't until the vfs has |
| 4220 | * proper support for them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | */ |
| 4222 | static void |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4223 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
| 4224 | struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | { |
| 4226 | struct nfs4_delegation *dp; |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4227 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
| 4228 | struct nfs4_client *clp = stp->st_stid.sc_client; |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 4229 | int cb_up; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4230 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4232 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4233 | open->op_recall = 0; |
| 4234 | switch (open->op_claim_type) { |
| 4235 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 4236 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4237 | open->op_recall = 1; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4238 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 4239 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4240 | break; |
| 4241 | case NFS4_OPEN_CLAIM_NULL: |
Ming Chen | ed47b06 | 2014-01-09 21:26:10 +0000 | [diff] [blame] | 4242 | case NFS4_OPEN_CLAIM_FH: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4243 | /* |
| 4244 | * Let's not give out any delegations till everyone's |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 4245 | * had the chance to reclaim theirs, *and* until |
| 4246 | * NLM locks have all been reclaimed: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4247 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4248 | if (locks_in_grace(clp->net)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4249 | goto out_no_deleg; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4250 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4251 | goto out_no_deleg; |
Steve Dickson | 9a0590a | 2013-05-15 14:51:49 -0400 | [diff] [blame] | 4252 | /* |
| 4253 | * Also, if the file was opened for write or |
| 4254 | * create, there's a good chance the client's |
| 4255 | * about to write to it, resulting in an |
| 4256 | * immediate recall (since we don't support |
| 4257 | * write delegations): |
| 4258 | */ |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4259 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4260 | goto out_no_deleg; |
| 4261 | if (open->op_create == NFS4_OPEN_CREATE) |
| 4262 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4263 | break; |
| 4264 | default: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4265 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4266 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4267 | dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4268 | if (IS_ERR(dp)) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 4269 | goto out_no_deleg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 4271 | 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] | 4272 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4273 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 4274 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4275 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
Trond Myklebust | 67cb127 | 2014-07-29 21:34:17 -0400 | [diff] [blame] | 4276 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 4277 | return; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 4278 | out_no_deleg: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4279 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 4280 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 4281 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4282 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 4283 | open->op_recall = 1; |
| 4284 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4285 | |
| 4286 | /* 4.1 client asking for a delegation? */ |
| 4287 | if (open->op_deleg_want) |
| 4288 | nfsd4_open_deleg_none_ext(open, status); |
| 4289 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4290 | } |
| 4291 | |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 4292 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 4293 | struct nfs4_delegation *dp) |
| 4294 | { |
| 4295 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 4296 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 4297 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4298 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 4299 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 4300 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 4301 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4302 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 4303 | } |
| 4304 | /* Otherwise the client must be confused wanting a delegation |
| 4305 | * it already has, therefore we don't return |
| 4306 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 4307 | */ |
| 4308 | } |
| 4309 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4310 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 4312 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4313 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4314 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | struct nfs4_file *fp = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4316 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4317 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4318 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | /* |
| 4321 | * Lookup file; if found, lookup stateid and check open request, |
| 4322 | * and check for delegations in the process of being recalled. |
| 4323 | * If not found, create the nfs4_file struct |
| 4324 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 4325 | fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4326 | if (fp != open->op_file) { |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 4327 | status = nfs4_check_deleg(cl, open, &dp); |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4328 | if (status) |
| 4329 | goto out; |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4330 | spin_lock(&fp->fi_lock); |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 4331 | stp = nfsd4_find_existing_open(fp, open); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4332 | spin_unlock(&fp->fi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | } else { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4334 | open->op_file = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4335 | status = nfserr_bad_stateid; |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4336 | if (nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4337 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | /* |
| 4341 | * OPEN the file, or upgrade an existing OPEN. |
| 4342 | * If truncate fails, the OPEN fails. |
| 4343 | */ |
| 4344 | if (stp) { |
| 4345 | /* Stateid was found, this is an OPEN upgrade */ |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 4346 | mutex_lock(&stp->st_mutex); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4347 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 4348 | if (status) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 4349 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4350 | goto out; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 4351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | } else { |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4353 | /* stp is returned locked. */ |
| 4354 | stp = init_open_stateid(fp, open); |
| 4355 | /* See if we lost the race to some other thread */ |
| 4356 | if (stp->st_access_bmap != 0) { |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4357 | status = nfs4_upgrade_open(rqstp, fp, current_fh, |
| 4358 | stp, open); |
| 4359 | if (status) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 4360 | mutex_unlock(&stp->st_mutex); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4361 | goto out; |
| 4362 | } |
| 4363 | goto upgrade_out; |
| 4364 | } |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4365 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
| 4366 | if (status) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 4367 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4368 | release_open_stateid(stp); |
| 4369 | goto out; |
| 4370 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4371 | |
| 4372 | stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, |
| 4373 | open->op_odstate); |
| 4374 | if (stp->st_clnt_odstate == open->op_odstate) |
| 4375 | open->op_odstate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | } |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4377 | upgrade_out: |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 4378 | nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 4379 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4380 | |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4381 | if (nfsd4_has_session(&resp->cstate)) { |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4382 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 4383 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4384 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 4385 | goto nodeleg; |
| 4386 | } |
| 4387 | } |
| 4388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | /* |
| 4390 | * Attempt to hand out a delegation. No error return, because the |
| 4391 | * OPEN succeeds even if we fail. |
| 4392 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4393 | nfs4_open_delegation(current_fh, open, stp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4394 | nodeleg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | status = nfs_ok; |
| 4396 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4397 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4398 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | out: |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4400 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 4401 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 4402 | open->op_deleg_want) |
| 4403 | nfsd4_deleg_xgrade_none_ext(open, dp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4404 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4405 | if (fp) |
| 4406 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 4407 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
Kinglong Mee | 8718602 | 2015-07-13 17:32:59 +0800 | [diff] [blame] | 4408 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | /* |
| 4410 | * To finish the open response, we just need to set the rflags. |
| 4411 | */ |
| 4412 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4413 | if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) && |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4414 | !nfsd4_has_session(&resp->cstate)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4416 | if (dp) |
| 4417 | nfs4_put_stid(&dp->dl_stid); |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 4418 | if (stp) |
| 4419 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | |
| 4421 | return status; |
| 4422 | } |
| 4423 | |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4424 | void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, |
Jeff Layton | 4229789 | 2015-03-23 10:53:44 -0400 | [diff] [blame] | 4425 | struct nfsd4_open *open) |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4426 | { |
| 4427 | if (open->op_openowner) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4428 | struct nfs4_stateowner *so = &open->op_openowner->oo_owner; |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4429 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4430 | nfsd4_cstate_assign_replay(cstate, so); |
| 4431 | nfs4_put_stateowner(so); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4432 | } |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4433 | if (open->op_file) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4434 | kmem_cache_free(file_slab, open->op_file); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4435 | if (open->op_stp) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 4436 | nfs4_put_stid(&open->op_stp->st_stid); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4437 | if (open->op_odstate) |
| 4438 | kmem_cache_free(odstate_slab, open->op_odstate); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4439 | } |
| 4440 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4441 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 4442 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4443 | clientid_t *clid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | { |
| 4445 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4446 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 4447 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | dprintk("process_renew(%08x/%08x): starting\n", |
| 4450 | clid->cl_boot, clid->cl_id); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4451 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 4452 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | goto out; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4454 | clp = cstate->clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | status = nfserr_cb_path_down; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 4456 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 4457 | && clp->cl_cb_state != NFSD4_CB_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | goto out; |
| 4459 | status = nfs_ok; |
| 4460 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | return status; |
| 4462 | } |
| 4463 | |
Jeff Layton | 7f5ef2e | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 4464 | void |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 4465 | nfsd4_end_grace(struct nfsd_net *nn) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 4466 | { |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 4467 | /* do nothing if grace period already ended */ |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 4468 | if (nn->grace_ended) |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 4469 | return; |
| 4470 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 4471 | dprintk("NFSD: end of grace period\n"); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 4472 | nn->grace_ended = true; |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 4473 | /* |
| 4474 | * If the server goes down again right now, an NFSv4 |
| 4475 | * client will still be allowed to reclaim after it comes back up, |
| 4476 | * even if it hasn't yet had a chance to reclaim state this time. |
| 4477 | * |
| 4478 | */ |
Jeff Layton | 919b804 | 2014-09-12 16:40:20 -0400 | [diff] [blame] | 4479 | nfsd4_record_grace_done(nn); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 4480 | /* |
| 4481 | * At this point, NFSv4 clients can still reclaim. But if the |
| 4482 | * server crashes, any that have not yet reclaimed will be out |
| 4483 | * of luck on the next boot. |
| 4484 | * |
| 4485 | * (NFSv4.1+ clients are considered to have reclaimed once they |
| 4486 | * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to |
| 4487 | * have reclaimed after their first OPEN.) |
| 4488 | */ |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 4489 | locks_end_grace(&nn->nfsd4_manager); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 4490 | /* |
| 4491 | * At this point, and once lockd and/or any other containers |
| 4492 | * exit their grace period, further reclaims will fail and |
| 4493 | * regular locking can resume. |
| 4494 | */ |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 4495 | } |
| 4496 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 4497 | static time_t |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4498 | nfs4_laundromat(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | { |
| 4500 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4501 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | struct nfs4_delegation *dp; |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4503 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 3d73371 | 2012-11-27 14:11:44 +0300 | [diff] [blame] | 4505 | time_t cutoff = get_seconds() - nn->nfsd4_lease; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4506 | time_t t, new_timeo = nn->nfsd4_lease; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4508 | dprintk("NFSD: laundromat service - starting\n"); |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 4509 | nfsd4_end_grace(nn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 4510 | INIT_LIST_HEAD(&reaplist); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 4511 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 4512 | list_for_each_safe(pos, next, &nn->client_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4513 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
| 4514 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { |
| 4515 | t = clp->cl_time - cutoff; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4516 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | break; |
| 4518 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 4519 | if (mark_client_expired_locked(clp)) { |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 4520 | dprintk("NFSD: client in use (clientid %08x)\n", |
| 4521 | clp->cl_clientid.cl_id); |
| 4522 | continue; |
| 4523 | } |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 4524 | list_add(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 4525 | } |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 4526 | spin_unlock(&nn->client_lock); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 4527 | list_for_each_safe(pos, next, &reaplist) { |
| 4528 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 4530 | clp->cl_clientid.cl_id); |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 4531 | list_del_init(&clp->cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | expire_client(clp); |
| 4533 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4534 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 4535 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 4537 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4538 | t = dp->dl_time - cutoff; |
| 4539 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4540 | break; |
| 4541 | } |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 4542 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 4543 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4545 | spin_unlock(&state_lock); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 4546 | while (!list_empty(&reaplist)) { |
| 4547 | dp = list_first_entry(&reaplist, struct nfs4_delegation, |
| 4548 | dl_recall_lru); |
| 4549 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4550 | revoke_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4552 | |
| 4553 | spin_lock(&nn->client_lock); |
| 4554 | while (!list_empty(&nn->close_lru)) { |
| 4555 | oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, |
| 4556 | oo_close_lru); |
| 4557 | if (time_after((unsigned long)oo->oo_time, |
| 4558 | (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4559 | t = oo->oo_time - cutoff; |
| 4560 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | break; |
| 4562 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4563 | list_del_init(&oo->oo_close_lru); |
| 4564 | stp = oo->oo_last_closed_stid; |
| 4565 | oo->oo_last_closed_stid = NULL; |
| 4566 | spin_unlock(&nn->client_lock); |
| 4567 | nfs4_put_stid(&stp->st_stid); |
| 4568 | spin_lock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4570 | spin_unlock(&nn->client_lock); |
| 4571 | |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4572 | new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4573 | return new_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 | } |
| 4575 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 4576 | static struct workqueue_struct *laundry_wq; |
| 4577 | static void laundromat_main(struct work_struct *); |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 4578 | |
| 4579 | static void |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4580 | laundromat_main(struct work_struct *laundry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | { |
| 4582 | time_t t; |
Geliang Tang | 2e55f3a | 2016-01-01 22:06:28 +0800 | [diff] [blame] | 4583 | struct delayed_work *dwork = to_delayed_work(laundry); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4584 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 4585 | laundromat_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4587 | t = nfs4_laundromat(nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4589 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | } |
| 4591 | |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 4592 | static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp) |
NeilBrown | f881651 | 2005-07-07 17:59:25 -0700 | [diff] [blame] | 4593 | { |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 4594 | if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4595 | return nfserr_bad_stateid; |
| 4596 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | } |
| 4598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4600 | access_permit_read(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4601 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4602 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 4603 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 4604 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4608 | access_permit_write(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4610 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 4611 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | } |
| 4613 | |
| 4614 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4615 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4616 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4617 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 4619 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 4620 | if (stp->st_openstp) |
| 4621 | stp = stp->st_openstp; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4622 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | goto out; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4624 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | goto out; |
| 4626 | status = nfs_ok; |
| 4627 | out: |
| 4628 | return status; |
| 4629 | } |
| 4630 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4631 | static inline __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4632 | 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] | 4633 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4634 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | return nfs_ok; |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 4636 | else if (opens_in_grace(net)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4637 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | * conflicting state; so we must wait out the grace period. */ |
| 4639 | return nfserr_grace; |
| 4640 | } else if (flags & WR_STATE) |
| 4641 | return nfs4_share_conflict(current_fh, |
| 4642 | NFS4_SHARE_DENY_WRITE); |
| 4643 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 4644 | return nfs4_share_conflict(current_fh, |
| 4645 | NFS4_SHARE_DENY_READ); |
| 4646 | } |
| 4647 | |
| 4648 | /* |
| 4649 | * Allow READ/WRITE during grace period on recovered state only for files |
| 4650 | * that are not able to provide mandatory locking. |
| 4651 | */ |
| 4652 | static inline int |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4653 | grace_disallows_io(struct net *net, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | { |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 4655 | return opens_in_grace(net) && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | } |
| 4657 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 4658 | 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] | 4659 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4660 | /* |
| 4661 | * When sessions are used the stateid generation number is ignored |
| 4662 | * when it is zero. |
| 4663 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 4664 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4665 | return nfs_ok; |
| 4666 | |
| 4667 | if (in->si_generation == ref->si_generation) |
| 4668 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4669 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4670 | /* If the client sends us a stateid from the future, it's buggy: */ |
Jeff Layton | 14b7f4a | 2016-05-05 06:53:47 -0400 | [diff] [blame] | 4671 | if (nfsd4_stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4672 | return nfserr_bad_stateid; |
| 4673 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4674 | * However, we could see a stateid from the past, even from a |
| 4675 | * non-buggy client. For example, if the client sends a lock |
| 4676 | * while some IO is outstanding, the lock may bump si_generation |
| 4677 | * while the IO is still in flight. The client could avoid that |
| 4678 | * situation by waiting for responses on all the IO requests, |
| 4679 | * but better performance may result in retrying IO that |
| 4680 | * receives an old_stateid error if requests are rarely |
| 4681 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4682 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4683 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4684 | } |
| 4685 | |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 4686 | static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) |
| 4687 | { |
| 4688 | if (ols->st_stateowner->so_is_open_owner && |
| 4689 | !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
| 4690 | return nfserr_bad_stateid; |
| 4691 | return nfs_ok; |
| 4692 | } |
| 4693 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4694 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4695 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4696 | struct nfs4_stid *s; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4697 | __be32 status = nfserr_bad_stateid; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4698 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4699 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4700 | return status; |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4701 | /* Client debugging aid. */ |
| 4702 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { |
| 4703 | char addr_str[INET6_ADDRSTRLEN]; |
| 4704 | rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, |
| 4705 | sizeof(addr_str)); |
| 4706 | pr_warn_ratelimited("NFSD: client %s testing state ID " |
| 4707 | "with incorrect client ID\n", addr_str); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4708 | return status; |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4709 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4710 | spin_lock(&cl->cl_lock); |
| 4711 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4712 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4713 | goto out_unlock; |
J. Bruce Fields | 36279ac | 2011-09-26 12:53:00 -0400 | [diff] [blame] | 4714 | status = check_stateid_generation(stateid, &s->sc_stateid, 1); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4715 | if (status) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4716 | goto out_unlock; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4717 | switch (s->sc_type) { |
| 4718 | case NFS4_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4719 | status = nfs_ok; |
| 4720 | break; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4721 | case NFS4_REVOKED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4722 | status = nfserr_deleg_revoked; |
| 4723 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4724 | case NFS4_OPEN_STID: |
| 4725 | case NFS4_LOCK_STID: |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 4726 | status = nfsd4_check_openowner_confirmed(openlockstateid(s)); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4727 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4728 | default: |
| 4729 | printk("unknown stateid type %x\n", s->sc_type); |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 4730 | /* Fallthrough */ |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4731 | case NFS4_CLOSED_STID: |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 4732 | case NFS4_CLOSED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4733 | status = nfserr_bad_stateid; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4734 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4735 | out_unlock: |
| 4736 | spin_unlock(&cl->cl_lock); |
| 4737 | return status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4738 | } |
| 4739 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 4740 | __be32 |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4741 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
| 4742 | stateid_t *stateid, unsigned char typemask, |
| 4743 | struct nfs4_stid **s, struct nfsd_net *nn) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4744 | { |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 4745 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4746 | |
| 4747 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 4748 | return nfserr_bad_stateid; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4749 | status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn); |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4750 | if (status == nfserr_stale_clientid) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4751 | if (cstate->session) |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4752 | return nfserr_bad_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4753 | return nfserr_stale_stateid; |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4754 | } |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 4755 | if (status) |
| 4756 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4757 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4758 | if (!*s) |
| 4759 | return nfserr_bad_stateid; |
| 4760 | return nfs_ok; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4761 | } |
| 4762 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4763 | static struct file * |
| 4764 | nfs4_find_file(struct nfs4_stid *s, int flags) |
| 4765 | { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4766 | if (!s) |
| 4767 | return NULL; |
| 4768 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4769 | switch (s->sc_type) { |
| 4770 | case NFS4_DELEG_STID: |
| 4771 | if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) |
| 4772 | return NULL; |
| 4773 | return get_file(s->sc_file->fi_deleg_file); |
| 4774 | case NFS4_OPEN_STID: |
| 4775 | case NFS4_LOCK_STID: |
| 4776 | if (flags & RD_STATE) |
| 4777 | return find_readable_file(s->sc_file); |
| 4778 | else |
| 4779 | return find_writeable_file(s->sc_file); |
| 4780 | break; |
| 4781 | } |
| 4782 | |
| 4783 | return NULL; |
| 4784 | } |
| 4785 | |
| 4786 | static __be32 |
| 4787 | nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags) |
| 4788 | { |
| 4789 | __be32 status; |
| 4790 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4791 | status = nfsd4_check_openowner_confirmed(ols); |
| 4792 | if (status) |
| 4793 | return status; |
| 4794 | return nfs4_check_openmode(ols, flags); |
| 4795 | } |
| 4796 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4797 | static __be32 |
| 4798 | nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s, |
| 4799 | struct file **filpp, bool *tmp_file, int flags) |
| 4800 | { |
| 4801 | int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; |
| 4802 | struct file *file; |
| 4803 | __be32 status; |
| 4804 | |
| 4805 | file = nfs4_find_file(s, flags); |
| 4806 | if (file) { |
| 4807 | status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
| 4808 | acc | NFSD_MAY_OWNER_OVERRIDE); |
| 4809 | if (status) { |
| 4810 | fput(file); |
| 4811 | return status; |
| 4812 | } |
| 4813 | |
| 4814 | *filpp = file; |
| 4815 | } else { |
| 4816 | status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp); |
| 4817 | if (status) |
| 4818 | return status; |
| 4819 | |
| 4820 | if (tmp_file) |
| 4821 | *tmp_file = true; |
| 4822 | } |
| 4823 | |
| 4824 | return 0; |
| 4825 | } |
| 4826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4827 | /* |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4828 | * Checks for stateid operations |
| 4829 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4830 | __be32 |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4831 | nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, |
Anna Schumaker | aa0d6ae | 2015-12-03 12:59:51 +0100 | [diff] [blame] | 4832 | struct nfsd4_compound_state *cstate, struct svc_fh *fhp, |
| 4833 | stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | { |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4835 | struct inode *ino = d_inode(fhp->fh_dentry); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4836 | struct net *net = SVC_NET(rqstp); |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4837 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4838 | struct nfs4_stid *s = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4839 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4841 | if (filpp) |
| 4842 | *filpp = NULL; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4843 | if (tmp_file) |
| 4844 | *tmp_file = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4846 | if (grace_disallows_io(net, ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | return nfserr_grace; |
| 4848 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4849 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
| 4850 | status = check_special_stateids(net, fhp, stateid, flags); |
| 4851 | goto done; |
| 4852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4854 | status = nfsd4_lookup_stateid(cstate, stateid, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4855 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4856 | &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4857 | if (status) |
Trond Myklebust | c2d1d6a | 2014-07-30 08:27:25 -0400 | [diff] [blame] | 4858 | return status; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4859 | status = check_stateid_generation(stateid, &s->sc_stateid, |
| 4860 | nfsd4_has_session(cstate)); |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4861 | if (status) |
| 4862 | goto out; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4863 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4864 | switch (s->sc_type) { |
| 4865 | case NFS4_DELEG_STID: |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4866 | status = nfs4_check_delegmode(delegstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4867 | break; |
| 4868 | case NFS4_OPEN_STID: |
| 4869 | case NFS4_LOCK_STID: |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4870 | status = nfs4_check_olstateid(fhp, openlockstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4871 | break; |
| 4872 | default: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4873 | status = nfserr_bad_stateid; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4874 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | } |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 4876 | if (status) |
| 4877 | goto out; |
| 4878 | status = nfs4_check_fh(fhp, s); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4879 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4880 | done: |
| 4881 | if (!status && filpp) |
| 4882 | status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | out: |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4884 | if (s) |
| 4885 | nfs4_put_stid(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4886 | return status; |
| 4887 | } |
| 4888 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4889 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4890 | * Test if the stateid is valid |
| 4891 | */ |
| 4892 | __be32 |
| 4893 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4894 | struct nfsd4_test_stateid *test_stateid) |
| 4895 | { |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4896 | struct nfsd4_test_stateid_id *stateid; |
| 4897 | struct nfs4_client *cl = cstate->session->se_client; |
| 4898 | |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4899 | 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] | 4900 | stateid->ts_id_status = |
| 4901 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4902 | |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4903 | return nfs_ok; |
| 4904 | } |
| 4905 | |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 4906 | static __be32 |
| 4907 | nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) |
| 4908 | { |
| 4909 | struct nfs4_ol_stateid *stp = openlockstateid(s); |
| 4910 | __be32 ret; |
| 4911 | |
| 4912 | mutex_lock(&stp->st_mutex); |
| 4913 | |
| 4914 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 4915 | if (ret) |
| 4916 | goto out; |
| 4917 | |
| 4918 | ret = nfserr_locks_held; |
| 4919 | if (check_for_locks(stp->st_stid.sc_file, |
| 4920 | lockowner(stp->st_stateowner))) |
| 4921 | goto out; |
| 4922 | |
| 4923 | release_lock_stateid(stp); |
| 4924 | ret = nfs_ok; |
| 4925 | |
| 4926 | out: |
| 4927 | mutex_unlock(&stp->st_mutex); |
| 4928 | nfs4_put_stid(s); |
| 4929 | return ret; |
| 4930 | } |
| 4931 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4932 | __be32 |
| 4933 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4934 | struct nfsd4_free_stateid *free_stateid) |
| 4935 | { |
| 4936 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4937 | struct nfs4_stid *s; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4938 | struct nfs4_delegation *dp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4939 | struct nfs4_client *cl = cstate->session->se_client; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4940 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4941 | |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4942 | spin_lock(&cl->cl_lock); |
| 4943 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4944 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4945 | goto out_unlock; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4946 | switch (s->sc_type) { |
| 4947 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4948 | ret = nfserr_locks_held; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4949 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4950 | case NFS4_OPEN_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4951 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 4952 | if (ret) |
| 4953 | break; |
| 4954 | ret = nfserr_locks_held; |
| 4955 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4956 | case NFS4_LOCK_STID: |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 4957 | atomic_inc(&s->sc_count); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4958 | spin_unlock(&cl->cl_lock); |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 4959 | ret = nfsd4_free_lock_stateid(stateid, s); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4960 | goto out; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4961 | case NFS4_REVOKED_DELEG_STID: |
| 4962 | dp = delegstateid(s); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 4963 | list_del_init(&dp->dl_recall_lru); |
| 4964 | spin_unlock(&cl->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 4965 | nfs4_put_stid(s); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4966 | ret = nfs_ok; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4967 | goto out; |
| 4968 | /* Default falls through and returns nfserr_bad_stateid */ |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4969 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4970 | out_unlock: |
| 4971 | spin_unlock(&cl->cl_lock); |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4972 | out: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4973 | return ret; |
| 4974 | } |
| 4975 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4976 | static inline int |
| 4977 | setlkflg (int type) |
| 4978 | { |
| 4979 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 4980 | RD_STATE : WR_STATE; |
| 4981 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4983 | 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] | 4984 | { |
| 4985 | struct svc_fh *current_fh = &cstate->current_fh; |
| 4986 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 4987 | __be32 status; |
| 4988 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4989 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 4990 | if (status) |
| 4991 | return status; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4992 | if (stp->st_stid.sc_type == NFS4_CLOSED_STID |
| 4993 | || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4994 | /* |
| 4995 | * "Closed" stateid's exist *only* to return |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4996 | * nfserr_replay_me from the previous step, and |
| 4997 | * revoked delegations are kept only for free_stateid. |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4998 | */ |
| 4999 | return nfserr_bad_stateid; |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5000 | mutex_lock(&stp->st_mutex); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5001 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5002 | if (status == nfs_ok) |
| 5003 | status = nfs4_check_fh(current_fh, &stp->st_stid); |
| 5004 | if (status != nfs_ok) |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5005 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5006 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5007 | } |
| 5008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5009 | /* |
| 5010 | * Checks for sequence id mutating operations. |
| 5011 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5012 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 5013 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 5014 | stateid_t *stateid, char typemask, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5015 | struct nfs4_ol_stateid **stpp, |
| 5016 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5017 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5018 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5019 | struct nfs4_stid *s; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5020 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5021 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5022 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
| 5023 | seqid, STATEID_VAL(stateid)); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | *stpp = NULL; |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5026 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5027 | if (status) |
| 5028 | return status; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5029 | stp = openlockstateid(s); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 5030 | nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5032 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5033 | if (!status) |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5034 | *stpp = stp; |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5035 | else |
| 5036 | nfs4_put_stid(&stp->st_stid); |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5037 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5038 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 5039 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5040 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 5041 | 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] | 5042 | { |
| 5043 | __be32 status; |
| 5044 | struct nfs4_openowner *oo; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5045 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5047 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5048 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 5049 | if (status) |
| 5050 | return status; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5051 | oo = openowner(stp->st_stateowner); |
| 5052 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5053 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5054 | nfs4_put_stid(&stp->st_stid); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5055 | return nfserr_bad_stateid; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5056 | } |
| 5057 | *stpp = stp; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5058 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | } |
| 5060 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5061 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5062 | 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] | 5063 | struct nfsd4_open_confirm *oc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5065 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5066 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5067 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5068 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5069 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 5070 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 5071 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5072 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5073 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 5074 | if (status) |
| 5075 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5077 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5078 | oc->oc_seqid, &oc->oc_req_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5079 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5080 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 5081 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5082 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 5083 | status = nfserr_bad_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5084 | if (oo->oo_flags & NFS4_OO_CONFIRMED) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5085 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 5086 | goto put_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5087 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 5088 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5089 | nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5090 | mutex_unlock(&stp->st_mutex); |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5091 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5092 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5093 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 5094 | nfsd4_client_record_create(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 5095 | status = nfs_ok; |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 5096 | put_stateid: |
| 5097 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5099 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | return status; |
| 5101 | } |
| 5102 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5103 | 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] | 5104 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5105 | if (!test_access(access, stp)) |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5106 | return; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5107 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5108 | clear_access(access, stp); |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5109 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 5110 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5111 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 5112 | { |
| 5113 | switch (to_access) { |
| 5114 | case NFS4_SHARE_ACCESS_READ: |
| 5115 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 5116 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 5117 | break; |
| 5118 | case NFS4_SHARE_ACCESS_WRITE: |
| 5119 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 5120 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 5121 | break; |
| 5122 | case NFS4_SHARE_ACCESS_BOTH: |
| 5123 | break; |
| 5124 | default: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 5125 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | } |
| 5127 | } |
| 5128 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5129 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5130 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
| 5131 | struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 5132 | struct nfsd4_open_downgrade *od) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5133 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5134 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5135 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5136 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 5138 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 5139 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | |
J. Bruce Fields | c30e92d | 2011-10-10 17:34:31 -0400 | [diff] [blame] | 5141 | /* We don't yet support WANT bits: */ |
Benny Halevy | 2c8bd7e | 2012-02-16 20:57:09 +0200 | [diff] [blame] | 5142 | if (od->od_deleg_want) |
| 5143 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 5144 | od->od_deleg_want); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5146 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5147 | &od->od_stateid, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5148 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | status = nfserr_inval; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5151 | if (!test_access(od->od_share_access, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 5152 | 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] | 5153 | stp->st_access_bmap, od->od_share_access); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5154 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | } |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 5156 | if (!test_deny(od->od_share_deny, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 5157 | 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] | 5158 | stp->st_deny_bmap, od->od_share_deny); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5159 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5160 | } |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5161 | nfs4_stateid_downgrade(stp, od->od_share_access); |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 5162 | reset_union_bmap_deny(od->od_share_deny, stp); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5163 | nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | status = nfs_ok; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5165 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5166 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5167 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5168 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5169 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | return status; |
| 5171 | } |
| 5172 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5173 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 5174 | { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 5175 | struct nfs4_client *clp = s->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 5176 | bool unhashed; |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 5177 | LIST_HEAD(reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 5178 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5179 | s->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 5180 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 5181 | unhashed = unhash_open_stateid(s, &reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 5182 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 5183 | if (clp->cl_minorversion) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 5184 | if (unhashed) |
| 5185 | put_ol_stateid_locked(s, &reaplist); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 5186 | spin_unlock(&clp->cl_lock); |
| 5187 | free_ol_stateid_reaplist(&reaplist); |
| 5188 | } else { |
| 5189 | spin_unlock(&clp->cl_lock); |
| 5190 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 5191 | if (unhashed) |
| 5192 | move_to_close_lru(s, clp->net); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 5193 | } |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 5194 | } |
| 5195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5196 | /* |
| 5197 | * nfs4_unlock_state() called after encode |
| 5198 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5199 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5200 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 5201 | struct nfsd4_close *close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5202 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5203 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5204 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5205 | struct net *net = SVC_NET(rqstp); |
| 5206 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 5208 | dprintk("NFSD: nfsd4_close on file %pd\n", |
| 5209 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5210 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5211 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 5212 | &close->cl_stateid, |
| 5213 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5214 | &stp, nn); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5215 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5216 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | goto out; |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5218 | nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5219 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5221 | nfsd4_close_open_stateid(stp); |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 5222 | |
| 5223 | /* put reference from nfs4_preprocess_seqid_op */ |
| 5224 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5225 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | return status; |
| 5227 | } |
| 5228 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5229 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5230 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 5231 | struct nfsd4_delegreturn *dr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5232 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5233 | struct nfs4_delegation *dp; |
| 5234 | stateid_t *stateid = &dr->dr_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5235 | struct nfs4_stid *s; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5236 | __be32 status; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5237 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5239 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5240 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5242 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5243 | if (status) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5244 | goto out; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5245 | dp = delegstateid(s); |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 5246 | 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] | 5247 | if (status) |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5248 | goto put_stateid; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5249 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5250 | destroy_delegation(dp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5251 | put_stateid: |
| 5252 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | out: |
| 5254 | return status; |
| 5255 | } |
| 5256 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 5257 | static inline u64 |
| 5258 | end_offset(u64 start, u64 len) |
| 5259 | { |
| 5260 | u64 end; |
| 5261 | |
| 5262 | end = start + len; |
| 5263 | return end >= start ? end: NFS4_MAX_UINT64; |
| 5264 | } |
| 5265 | |
| 5266 | /* last octet in a range */ |
| 5267 | static inline u64 |
| 5268 | last_byte_offset(u64 start, u64 len) |
| 5269 | { |
| 5270 | u64 end; |
| 5271 | |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 5272 | WARN_ON_ONCE(!len); |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 5273 | end = start + len; |
| 5274 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 5275 | } |
| 5276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | /* |
| 5278 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 5279 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 5280 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 5281 | * locking, this prevents us from being completely protocol-compliant. The |
| 5282 | * real solution to this problem is to start using unsigned file offsets in |
| 5283 | * the VFS, but this is a very deep change! |
| 5284 | */ |
| 5285 | static inline void |
| 5286 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 5287 | { |
| 5288 | if (lock->fl_start < 0) |
| 5289 | lock->fl_start = OFFSET_MAX; |
| 5290 | if (lock->fl_end < 0) |
| 5291 | lock->fl_end = OFFSET_MAX; |
| 5292 | } |
| 5293 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5294 | static fl_owner_t |
| 5295 | nfsd4_fl_get_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5296 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5297 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
| 5298 | |
| 5299 | nfs4_get_stateowner(&lo->lo_owner); |
| 5300 | return owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5301 | } |
| 5302 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5303 | static void |
| 5304 | nfsd4_fl_put_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5305 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5306 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5307 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5308 | if (lo) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5309 | nfs4_put_stateowner(&lo->lo_owner); |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5310 | } |
| 5311 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 5312 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5313 | .lm_get_owner = nfsd4_fl_get_owner, |
| 5314 | .lm_put_owner = nfsd4_fl_put_owner, |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 5315 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | |
| 5317 | static inline void |
| 5318 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 5319 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5320 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 5322 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5323 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
| 5324 | deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data, |
| 5325 | lo->lo_owner.so_owner.len, GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 5326 | if (!deny->ld_owner.data) |
| 5327 | /* We just don't care that much */ |
| 5328 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5329 | deny->ld_owner.len = lo->lo_owner.so_owner.len; |
| 5330 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 5331 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 5332 | nevermind: |
| 5333 | deny->ld_owner.len = 0; |
| 5334 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 5335 | deny->ld_clientid.cl_boot = 0; |
| 5336 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | } |
| 5338 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 5339 | deny->ld_length = NFS4_MAX_UINT64; |
| 5340 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 5342 | deny->ld_type = NFS4_READ_LT; |
| 5343 | if (fl->fl_type != F_RDLCK) |
| 5344 | deny->ld_type = NFS4_WRITE_LT; |
| 5345 | } |
| 5346 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5347 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5348 | find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5349 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5350 | unsigned int strhashval = ownerstr_hashval(owner); |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 5351 | struct nfs4_stateowner *so; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 5353 | lockdep_assert_held(&clp->cl_lock); |
| 5354 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5355 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], |
| 5356 | so_strhash) { |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 5357 | if (so->so_is_open_owner) |
| 5358 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 5359 | if (same_owner_str(so, owner)) |
| 5360 | return lockowner(nfs4_get_stateowner(so)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | } |
| 5362 | return NULL; |
| 5363 | } |
| 5364 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5365 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5366 | find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5367 | { |
| 5368 | struct nfs4_lockowner *lo; |
| 5369 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5370 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5371 | lo = find_lockowner_str_locked(clp, owner); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5372 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5373 | return lo; |
| 5374 | } |
| 5375 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 5376 | static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) |
| 5377 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5378 | unhash_lockowner_locked(lockowner(sop)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 5379 | } |
| 5380 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 5381 | static void nfs4_free_lockowner(struct nfs4_stateowner *sop) |
| 5382 | { |
| 5383 | struct nfs4_lockowner *lo = lockowner(sop); |
| 5384 | |
| 5385 | kmem_cache_free(lockowner_slab, lo); |
| 5386 | } |
| 5387 | |
| 5388 | static const struct nfs4_stateowner_operations lockowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 5389 | .so_unhash = nfs4_unhash_lockowner, |
| 5390 | .so_free = nfs4_free_lockowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 5391 | }; |
| 5392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | /* |
| 5394 | * Alloc a lock owner structure. |
| 5395 | * 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] | 5396 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | * |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 5398 | * strhashval = ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5400 | static struct nfs4_lockowner * |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5401 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
| 5402 | struct nfs4_ol_stateid *open_stp, |
| 5403 | struct nfsd4_lock *lock) |
| 5404 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5405 | struct nfs4_lockowner *lo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5407 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 5408 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | return NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5410 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 5411 | lo->lo_owner.so_is_open_owner = 0; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5412 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 5413 | lo->lo_owner.so_ops = &lockowner_ops; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5414 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5415 | ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5416 | if (ret == NULL) { |
| 5417 | list_add(&lo->lo_owner.so_strhash, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5418 | &clp->cl_ownerstr_hashtbl[strhashval]); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5419 | ret = lo; |
| 5420 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 5421 | nfs4_free_stateowner(&lo->lo_owner); |
| 5422 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5423 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 340f0ba | 2015-03-23 11:02:30 -0400 | [diff] [blame] | 5424 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5425 | } |
| 5426 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5427 | static void |
| 5428 | init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, |
| 5429 | struct nfs4_file *fp, struct inode *inode, |
| 5430 | struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | { |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 5432 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5433 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5434 | lockdep_assert_held(&clp->cl_lock); |
| 5435 | |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5436 | atomic_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 5437 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 5438 | stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 5439 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5440 | stp->st_stid.sc_file = fp; |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 5441 | stp->st_stid.sc_free = nfs4_free_lock_stateid; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5442 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5443 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 5444 | stp->st_openstp = open_stp; |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5445 | mutex_init(&stp->st_mutex); |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5446 | list_add(&stp->st_locks, &open_stp->st_locks); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 5447 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 5448 | spin_lock(&fp->fi_lock); |
| 5449 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 5450 | spin_unlock(&fp->fi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | } |
| 5452 | |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5453 | static struct nfs4_ol_stateid * |
| 5454 | find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) |
| 5455 | { |
| 5456 | struct nfs4_ol_stateid *lst; |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5457 | struct nfs4_client *clp = lo->lo_owner.so_client; |
| 5458 | |
| 5459 | lockdep_assert_held(&clp->cl_lock); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5460 | |
| 5461 | list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5462 | if (lst->st_stid.sc_file == fp) { |
| 5463 | atomic_inc(&lst->st_stid.sc_count); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5464 | return lst; |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5465 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5466 | } |
| 5467 | return NULL; |
| 5468 | } |
| 5469 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5470 | static struct nfs4_ol_stateid * |
| 5471 | find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, |
| 5472 | struct inode *inode, struct nfs4_ol_stateid *ost, |
| 5473 | bool *new) |
| 5474 | { |
| 5475 | struct nfs4_stid *ns = NULL; |
| 5476 | struct nfs4_ol_stateid *lst; |
| 5477 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 5478 | struct nfs4_client *clp = oo->oo_owner.so_client; |
| 5479 | |
| 5480 | spin_lock(&clp->cl_lock); |
| 5481 | lst = find_lock_stateid(lo, fi); |
| 5482 | if (lst == NULL) { |
| 5483 | spin_unlock(&clp->cl_lock); |
| 5484 | ns = nfs4_alloc_stid(clp, stateid_slab); |
| 5485 | if (ns == NULL) |
| 5486 | return NULL; |
| 5487 | |
| 5488 | spin_lock(&clp->cl_lock); |
| 5489 | lst = find_lock_stateid(lo, fi); |
| 5490 | if (likely(!lst)) { |
| 5491 | lst = openlockstateid(ns); |
| 5492 | init_lock_stateid(lst, lo, fi, inode, ost); |
| 5493 | ns = NULL; |
| 5494 | *new = true; |
| 5495 | } |
| 5496 | } |
| 5497 | spin_unlock(&clp->cl_lock); |
| 5498 | if (ns) |
| 5499 | nfs4_put_stid(ns); |
| 5500 | return lst; |
| 5501 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5502 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 5503 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | check_lock_length(u64 offset, u64 length) |
| 5505 | { |
Kinglong Mee | e796931 | 2015-07-13 17:34:19 +0800 | [diff] [blame] | 5506 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
| 5507 | (length > ~offset))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | } |
| 5509 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5510 | 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] | 5511 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5512 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5513 | |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5514 | lockdep_assert_held(&fp->fi_lock); |
| 5515 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5516 | if (test_access(access, lock_stp)) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5517 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 5518 | __nfs4_file_get_access(fp, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5519 | set_access(access, lock_stp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5520 | } |
| 5521 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5522 | static __be32 |
| 5523 | lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, |
| 5524 | struct nfs4_ol_stateid *ost, |
| 5525 | struct nfsd4_lock *lock, |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 5526 | struct nfs4_ol_stateid **plst, bool *new) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5527 | { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5528 | __be32 status; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5529 | struct nfs4_file *fi = ost->st_stid.sc_file; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5530 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 5531 | struct nfs4_client *cl = oo->oo_owner.so_client; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5532 | struct inode *inode = d_inode(cstate->current_fh.fh_dentry); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5533 | struct nfs4_lockowner *lo; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 5534 | struct nfs4_ol_stateid *lst; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5535 | unsigned int strhashval; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 5536 | bool hashed; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5537 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5538 | lo = find_lockowner_str(cl, &lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5539 | if (!lo) { |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 5540 | strhashval = ownerstr_hashval(&lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5541 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 5542 | if (lo == NULL) |
| 5543 | return nfserr_jukebox; |
| 5544 | } else { |
| 5545 | /* with an existing lockowner, seqids must be the same */ |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5546 | status = nfserr_bad_seqid; |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5547 | if (!cstate->minorversion && |
| 5548 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5549 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5550 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5551 | |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 5552 | retry: |
| 5553 | lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); |
| 5554 | if (lst == NULL) { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5555 | status = nfserr_jukebox; |
| 5556 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5557 | } |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 5558 | |
| 5559 | mutex_lock(&lst->st_mutex); |
| 5560 | |
| 5561 | /* See if it's still hashed to avoid race with FREE_STATEID */ |
| 5562 | spin_lock(&cl->cl_lock); |
| 5563 | hashed = !list_empty(&lst->st_perfile); |
| 5564 | spin_unlock(&cl->cl_lock); |
| 5565 | |
| 5566 | if (!hashed) { |
| 5567 | mutex_unlock(&lst->st_mutex); |
| 5568 | nfs4_put_stid(&lst->st_stid); |
| 5569 | goto retry; |
| 5570 | } |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5571 | status = nfs_ok; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 5572 | *plst = lst; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5573 | out: |
| 5574 | nfs4_put_stateowner(&lo->lo_owner); |
| 5575 | return status; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5576 | } |
| 5577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5578 | /* |
| 5579 | * LOCK operation |
| 5580 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5581 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5582 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 5583 | struct nfsd4_lock *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5584 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5585 | struct nfs4_openowner *open_sop = NULL; |
| 5586 | struct nfs4_lockowner *lock_sop = NULL; |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5587 | struct nfs4_ol_stateid *lock_stp = NULL; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5588 | struct nfs4_ol_stateid *open_stp = NULL; |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5589 | struct nfs4_file *fp; |
J. Bruce Fields | 7d94784 | 2010-08-20 18:09:31 -0400 | [diff] [blame] | 5590 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5591 | struct file_lock *file_lock = NULL; |
| 5592 | struct file_lock *conflock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5593 | __be32 status = 0; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5594 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5595 | int err; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5596 | bool new = false; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5597 | struct net *net = SVC_NET(rqstp); |
| 5598 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5599 | |
| 5600 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 5601 | (long long) lock->lk_offset, |
| 5602 | (long long) lock->lk_length); |
| 5603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 5605 | return nfserr_inval; |
| 5606 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5607 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 5608 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 5609 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 5610 | return status; |
| 5611 | } |
| 5612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5613 | if (lock->lk_is_new) { |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 5614 | if (nfsd4_has_session(cstate)) |
| 5615 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 5616 | memcpy(&lock->lk_new_clientid, |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 5617 | &cstate->session->se_client->cl_clientid, |
| 5618 | sizeof(clientid_t)); |
| 5619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | status = nfserr_stale_clientid; |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 5621 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5622 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5625 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5626 | lock->lk_new_open_seqid, |
| 5627 | &lock->lk_new_open_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5628 | &open_stp, nn); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 5629 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5630 | goto out; |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5631 | mutex_unlock(&open_stp->st_mutex); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5632 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5633 | status = nfserr_bad_stateid; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 5634 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 5635 | &lock->lk_new_clientid)) |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5636 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5637 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5638 | &lock_stp, &new); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5639 | } else { |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 5640 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5641 | lock->lk_old_lock_seqid, |
| 5642 | &lock->lk_old_lock_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5643 | NFS4_LOCK_STID, &lock_stp, nn); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5644 | } |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 5645 | if (status) |
| 5646 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5647 | lock_sop = lockowner(lock_stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5649 | lkflg = setlkflg(lock->lk_type); |
| 5650 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 5651 | if (status) |
| 5652 | goto out; |
| 5653 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 5654 | status = nfserr_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5655 | if (locks_in_grace(net) && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 5656 | goto out; |
| 5657 | status = nfserr_no_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5658 | if (!locks_in_grace(net) && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 5659 | goto out; |
| 5660 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5661 | file_lock = locks_alloc_lock(); |
| 5662 | if (!file_lock) { |
| 5663 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5664 | status = nfserr_jukebox; |
| 5665 | goto out; |
| 5666 | } |
| 5667 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5668 | fp = lock_stp->st_stid.sc_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | switch (lock->lk_type) { |
| 5670 | case NFS4_READ_LT: |
| 5671 | case NFS4_READW_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5672 | spin_lock(&fp->fi_lock); |
| 5673 | filp = find_readable_file_locked(fp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5674 | if (filp) |
| 5675 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5676 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5677 | file_lock->fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 5678 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | case NFS4_WRITE_LT: |
| 5680 | case NFS4_WRITEW_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5681 | spin_lock(&fp->fi_lock); |
| 5682 | filp = find_writeable_file_locked(fp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5683 | if (filp) |
| 5684 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5685 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5686 | file_lock->fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 5687 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | default: |
| 5689 | status = nfserr_inval; |
| 5690 | goto out; |
| 5691 | } |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5692 | if (!filp) { |
| 5693 | status = nfserr_openmode; |
| 5694 | goto out; |
| 5695 | } |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5696 | |
| 5697 | file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner)); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5698 | file_lock->fl_pid = current->tgid; |
| 5699 | file_lock->fl_file = filp; |
| 5700 | file_lock->fl_flags = FL_POSIX; |
| 5701 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 5702 | file_lock->fl_start = lock->lk_offset; |
| 5703 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 5704 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5706 | conflock = locks_alloc_lock(); |
| 5707 | if (!conflock) { |
| 5708 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5709 | status = nfserr_jukebox; |
| 5710 | goto out; |
| 5711 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5712 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5713 | err = vfs_lock_file(filp, F_SETLK, file_lock, conflock); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5714 | switch (-err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | case 0: /* success! */ |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5716 | nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5717 | status = 0; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 5718 | break; |
| 5719 | case (EAGAIN): /* conflock holds conflicting lock */ |
| 5720 | status = nfserr_denied; |
| 5721 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5722 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 5723 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | case (EDEADLK): |
| 5725 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 5726 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 5727 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 5728 | 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] | 5729 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 5730 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | out: |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5733 | if (filp) |
| 5734 | fput(filp); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5735 | if (lock_stp) { |
| 5736 | /* Bump seqid manually if the 4.0 replay owner is openowner */ |
| 5737 | if (cstate->replay_owner && |
| 5738 | cstate->replay_owner != &lock_sop->lo_owner && |
| 5739 | seqid_mutating_err(ntohl(status))) |
| 5740 | lock_sop->lo_owner.so_seqid++; |
| 5741 | |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5742 | mutex_unlock(&lock_stp->st_mutex); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5743 | |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5744 | /* |
| 5745 | * If this is a new, never-before-used stateid, and we are |
| 5746 | * returning an error, then just go ahead and release it. |
| 5747 | */ |
| 5748 | if (status && new) |
| 5749 | release_lock_stateid(lock_stp); |
| 5750 | |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5751 | nfs4_put_stid(&lock_stp->st_stid); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5752 | } |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5753 | if (open_stp) |
| 5754 | nfs4_put_stid(&open_stp->st_stid); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5755 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5756 | if (file_lock) |
| 5757 | locks_free_lock(file_lock); |
| 5758 | if (conflock) |
| 5759 | locks_free_lock(conflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | return status; |
| 5761 | } |
| 5762 | |
| 5763 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 5764 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 5765 | * so we do a temporary open here just to get an open file to pass to |
| 5766 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 5767 | * inode operation.) |
| 5768 | */ |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5769 | 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] | 5770 | { |
| 5771 | struct file *file; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5772 | __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); |
| 5773 | if (!err) { |
| 5774 | err = nfserrno(vfs_test_lock(file, lock)); |
Christoph Hellwig | fd89145 | 2015-04-28 15:41:16 +0200 | [diff] [blame] | 5775 | fput(file); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5776 | } |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 5777 | return err; |
| 5778 | } |
| 5779 | |
| 5780 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | * LOCKT operation |
| 5782 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5783 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5784 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 5785 | struct nfsd4_lockt *lockt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5786 | { |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5787 | struct file_lock *file_lock = NULL; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5788 | struct nfs4_lockowner *lo = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5789 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5790 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5791 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5792 | if (locks_in_grace(SVC_NET(rqstp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5793 | return nfserr_grace; |
| 5794 | |
| 5795 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 5796 | return nfserr_inval; |
| 5797 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5798 | if (!nfsd4_has_session(cstate)) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5799 | status = lookup_clientid(&lockt->lt_clientid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5800 | if (status) |
| 5801 | goto out; |
| 5802 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5803 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 5804 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5806 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5807 | file_lock = locks_alloc_lock(); |
| 5808 | if (!file_lock) { |
| 5809 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5810 | status = nfserr_jukebox; |
| 5811 | goto out; |
| 5812 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 5813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | switch (lockt->lt_type) { |
| 5815 | case NFS4_READ_LT: |
| 5816 | case NFS4_READW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5817 | file_lock->fl_type = F_RDLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5818 | break; |
| 5819 | case NFS4_WRITE_LT: |
| 5820 | case NFS4_WRITEW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5821 | file_lock->fl_type = F_WRLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5822 | break; |
| 5823 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 5824 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | status = nfserr_inval; |
| 5826 | goto out; |
| 5827 | } |
| 5828 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5829 | lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5830 | if (lo) |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5831 | file_lock->fl_owner = (fl_owner_t)lo; |
| 5832 | file_lock->fl_pid = current->tgid; |
| 5833 | file_lock->fl_flags = FL_POSIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5834 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5835 | file_lock->fl_start = lockt->lt_offset; |
| 5836 | 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] | 5837 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5838 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5840 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5841 | if (status) |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 5842 | goto out; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5843 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5844 | if (file_lock->fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | status = nfserr_denied; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5846 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5847 | } |
| 5848 | out: |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5849 | if (lo) |
| 5850 | nfs4_put_stateowner(&lo->lo_owner); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5851 | if (file_lock) |
| 5852 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5853 | return status; |
| 5854 | } |
| 5855 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5856 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5857 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 5858 | struct nfsd4_locku *locku) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5859 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5860 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5861 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5862 | struct file_lock *file_lock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5863 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5864 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5865 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 5866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 5868 | (long long) locku->lu_offset, |
| 5869 | (long long) locku->lu_length); |
| 5870 | |
| 5871 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 5872 | return nfserr_inval; |
| 5873 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5874 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5875 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 5876 | &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5877 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5878 | goto out; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5879 | filp = find_any_file(stp->st_stid.sc_file); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5880 | if (!filp) { |
| 5881 | status = nfserr_lock_range; |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 5882 | goto put_stateid; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5883 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5884 | file_lock = locks_alloc_lock(); |
| 5885 | if (!file_lock) { |
| 5886 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5887 | status = nfserr_jukebox; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5888 | goto fput; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5889 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 5890 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5891 | file_lock->fl_type = F_UNLCK; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5892 | file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner)); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5893 | file_lock->fl_pid = current->tgid; |
| 5894 | file_lock->fl_file = filp; |
| 5895 | file_lock->fl_flags = FL_POSIX; |
| 5896 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 5897 | file_lock->fl_start = locku->lu_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5899 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 5900 | locku->lu_length); |
| 5901 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5902 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5903 | err = vfs_lock_file(filp, F_SETLK, file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5904 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 5905 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5906 | goto out_nfserr; |
| 5907 | } |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5908 | nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5909 | fput: |
| 5910 | fput(filp); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 5911 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5912 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 5913 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5915 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5916 | if (file_lock) |
| 5917 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | return status; |
| 5919 | |
| 5920 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5921 | status = nfserrno(err); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5922 | goto fput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | } |
| 5924 | |
| 5925 | /* |
| 5926 | * returns |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5927 | * true: locks held by lockowner |
| 5928 | * false: no locks held by lockowner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5930 | static bool |
| 5931 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | { |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 5933 | struct file_lock *fl; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5934 | int status = false; |
| 5935 | struct file *filp = find_any_file(fp); |
| 5936 | struct inode *inode; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 5937 | struct file_lock_context *flctx; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5938 | |
| 5939 | if (!filp) { |
| 5940 | /* Any valid lock stateid should have some sort of access */ |
| 5941 | WARN_ON_ONCE(1); |
| 5942 | return status; |
| 5943 | } |
| 5944 | |
| 5945 | inode = file_inode(filp); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 5946 | flctx = inode->i_flctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5947 | |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 5948 | if (flctx && !list_empty_careful(&flctx->flc_posix)) { |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 5949 | spin_lock(&flctx->flc_lock); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 5950 | list_for_each_entry(fl, &flctx->flc_posix, fl_list) { |
| 5951 | if (fl->fl_owner == (fl_owner_t)lowner) { |
| 5952 | status = true; |
| 5953 | break; |
| 5954 | } |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 5955 | } |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 5956 | spin_unlock(&flctx->flc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5957 | } |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5958 | fput(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5959 | return status; |
| 5960 | } |
| 5961 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5962 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 5963 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 5964 | struct nfsd4_compound_state *cstate, |
| 5965 | struct nfsd4_release_lockowner *rlockowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5966 | { |
| 5967 | clientid_t *clid = &rlockowner->rl_clientid; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5968 | struct nfs4_stateowner *sop; |
| 5969 | struct nfs4_lockowner *lo = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5970 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5971 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5972 | unsigned int hashval = ownerstr_hashval(owner); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5973 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5974 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5975 | struct nfs4_client *clp; |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 5976 | LIST_HEAD (reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5977 | |
| 5978 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 5979 | clid->cl_boot, clid->cl_id); |
| 5980 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5981 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5982 | if (status) |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 5983 | return status; |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5984 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5985 | clp = cstate->clp; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5986 | /* Find the matching lock stateowner */ |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5987 | spin_lock(&clp->cl_lock); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5988 | list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval], |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5989 | so_strhash) { |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5990 | |
| 5991 | if (sop->so_is_open_owner || !same_owner_str(sop, owner)) |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 5992 | continue; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5993 | |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5994 | /* see if there are still any locks associated with it */ |
| 5995 | lo = lockowner(sop); |
| 5996 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { |
| 5997 | if (check_for_locks(stp->st_stid.sc_file, lo)) { |
| 5998 | status = nfserr_locks_held; |
| 5999 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 6000 | return status; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6001 | } |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 6002 | } |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6003 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 6004 | nfs4_get_stateowner(sop); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6005 | break; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 6006 | } |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 6007 | if (!lo) { |
| 6008 | spin_unlock(&clp->cl_lock); |
| 6009 | return status; |
| 6010 | } |
| 6011 | |
| 6012 | unhash_lockowner_locked(lo); |
| 6013 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 6014 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
| 6015 | struct nfs4_ol_stateid, |
| 6016 | st_perstateowner); |
| 6017 | WARN_ON(!unhash_lock_stateid(stp)); |
| 6018 | put_ol_stateid_locked(stp, &reaplist); |
| 6019 | } |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6020 | spin_unlock(&clp->cl_lock); |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 6021 | free_ol_stateid_reaplist(&reaplist); |
| 6022 | nfs4_put_stateowner(&lo->lo_owner); |
| 6023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6024 | return status; |
| 6025 | } |
| 6026 | |
| 6027 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 6028 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6029 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 6030 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6031 | } |
| 6032 | |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 6033 | bool |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6034 | nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 6035 | { |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 6036 | struct nfs4_client_reclaim *crp; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 6037 | |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6038 | crp = nfsd4_find_reclaim_client(name, nn); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 6039 | return (crp && crp->cr_clp); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 6040 | } |
| 6041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6042 | /* |
| 6043 | * failure => all reset bets are off, nfserr_no_grace... |
| 6044 | */ |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 6045 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6046 | nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | { |
| 6048 | unsigned int strhashval; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 6049 | struct nfs4_client_reclaim *crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6050 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 6051 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
| 6052 | crp = alloc_reclaim(); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 6053 | if (crp) { |
| 6054 | strhashval = clientstr_hashval(name); |
| 6055 | INIT_LIST_HEAD(&crp->cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6056 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 6057 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 6058 | crp->cr_clp = NULL; |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6059 | nn->reclaim_str_hashtbl_size++; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 6060 | } |
| 6061 | return crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | } |
| 6063 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 6064 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6065 | 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] | 6066 | { |
| 6067 | list_del(&crp->cr_strhash); |
| 6068 | kfree(crp); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6069 | nn->reclaim_str_hashtbl_size--; |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 6070 | } |
| 6071 | |
| 6072 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6073 | nfs4_release_reclaim(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | { |
| 6075 | struct nfs4_client_reclaim *crp = NULL; |
| 6076 | int i; |
| 6077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6079 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 6080 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6081 | struct nfs4_client_reclaim, cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6082 | nfs4_remove_reclaim_record(crp, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6083 | } |
| 6084 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 6085 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | } |
| 6087 | |
| 6088 | /* |
| 6089 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 6090 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6091 | nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6092 | { |
| 6093 | unsigned int strhashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6094 | struct nfs4_client_reclaim *crp = NULL; |
| 6095 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 6096 | dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 6098 | strhashval = clientstr_hashval(recdir); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6099 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 6100 | if (same_name(crp->cr_recdir, recdir)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6101 | return crp; |
| 6102 | } |
| 6103 | } |
| 6104 | return NULL; |
| 6105 | } |
| 6106 | |
| 6107 | /* |
| 6108 | * Called from OPEN. Look for clientid in reclaim list. |
| 6109 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6110 | __be32 |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 6111 | nfs4_check_open_reclaim(clientid_t *clid, |
| 6112 | struct nfsd4_compound_state *cstate, |
| 6113 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | { |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 6115 | __be32 status; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 6116 | |
| 6117 | /* find clientid in conf_id_hashtbl */ |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 6118 | status = lookup_clientid(clid, cstate, nn); |
| 6119 | if (status) |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 6120 | return nfserr_reclaim_bad; |
| 6121 | |
Jeff Layton | 3b3e7b7 | 2014-09-12 16:40:22 -0400 | [diff] [blame] | 6122 | if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags)) |
| 6123 | return nfserr_no_grace; |
| 6124 | |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 6125 | if (nfsd4_client_record_check(cstate->clp)) |
| 6126 | return nfserr_reclaim_bad; |
| 6127 | |
| 6128 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6129 | } |
| 6130 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 6131 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6132 | static inline void |
| 6133 | put_client(struct nfs4_client *clp) |
| 6134 | { |
| 6135 | atomic_dec(&clp->cl_refcount); |
| 6136 | } |
| 6137 | |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6138 | static struct nfs4_client * |
| 6139 | nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) |
| 6140 | { |
| 6141 | struct nfs4_client *clp; |
| 6142 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6143 | nfsd_net_id); |
| 6144 | |
| 6145 | if (!nfsd_netns_ready(nn)) |
| 6146 | return NULL; |
| 6147 | |
| 6148 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6149 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) |
| 6150 | return clp; |
| 6151 | } |
| 6152 | return NULL; |
| 6153 | } |
| 6154 | |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 6155 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6156 | nfsd_inject_print_clients(void) |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 6157 | { |
| 6158 | struct nfs4_client *clp; |
| 6159 | u64 count = 0; |
| 6160 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6161 | nfsd_net_id); |
| 6162 | char buf[INET6_ADDRSTRLEN]; |
| 6163 | |
| 6164 | if (!nfsd_netns_ready(nn)) |
| 6165 | return 0; |
| 6166 | |
| 6167 | spin_lock(&nn->client_lock); |
| 6168 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6169 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
| 6170 | pr_info("NFS Client: %s\n", buf); |
| 6171 | ++count; |
| 6172 | } |
| 6173 | spin_unlock(&nn->client_lock); |
| 6174 | |
| 6175 | return count; |
| 6176 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 6177 | |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 6178 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6179 | nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size) |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 6180 | { |
| 6181 | u64 count = 0; |
| 6182 | struct nfs4_client *clp; |
| 6183 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6184 | nfsd_net_id); |
| 6185 | |
| 6186 | if (!nfsd_netns_ready(nn)) |
| 6187 | return count; |
| 6188 | |
| 6189 | spin_lock(&nn->client_lock); |
| 6190 | clp = nfsd_find_client(addr, addr_size); |
| 6191 | if (clp) { |
| 6192 | if (mark_client_expired_locked(clp) == nfs_ok) |
| 6193 | ++count; |
| 6194 | else |
| 6195 | clp = NULL; |
| 6196 | } |
| 6197 | spin_unlock(&nn->client_lock); |
| 6198 | |
| 6199 | if (clp) |
| 6200 | expire_client(clp); |
| 6201 | |
| 6202 | return count; |
| 6203 | } |
| 6204 | |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 6205 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6206 | nfsd_inject_forget_clients(u64 max) |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 6207 | { |
| 6208 | u64 count = 0; |
| 6209 | struct nfs4_client *clp, *next; |
| 6210 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6211 | nfsd_net_id); |
| 6212 | LIST_HEAD(reaplist); |
| 6213 | |
| 6214 | if (!nfsd_netns_ready(nn)) |
| 6215 | return count; |
| 6216 | |
| 6217 | spin_lock(&nn->client_lock); |
| 6218 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 6219 | if (mark_client_expired_locked(clp) == nfs_ok) { |
| 6220 | list_add(&clp->cl_lru, &reaplist); |
| 6221 | if (max != 0 && ++count >= max) |
| 6222 | break; |
| 6223 | } |
| 6224 | } |
| 6225 | spin_unlock(&nn->client_lock); |
| 6226 | |
| 6227 | list_for_each_entry_safe(clp, next, &reaplist, cl_lru) |
| 6228 | expire_client(clp); |
| 6229 | |
| 6230 | return count; |
| 6231 | } |
| 6232 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6233 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, |
| 6234 | const char *type) |
| 6235 | { |
| 6236 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 6237 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6238 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
| 6239 | } |
| 6240 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6241 | static void |
| 6242 | nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst, |
| 6243 | struct list_head *collect) |
| 6244 | { |
| 6245 | struct nfs4_client *clp = lst->st_stid.sc_client; |
| 6246 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6247 | nfsd_net_id); |
| 6248 | |
| 6249 | if (!collect) |
| 6250 | return; |
| 6251 | |
| 6252 | lockdep_assert_held(&nn->client_lock); |
| 6253 | atomic_inc(&clp->cl_refcount); |
| 6254 | list_add(&lst->st_locks, collect); |
| 6255 | } |
| 6256 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6257 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 6258 | struct list_head *collect, |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6259 | bool (*func)(struct nfs4_ol_stateid *)) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6260 | { |
| 6261 | struct nfs4_openowner *oop; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6262 | struct nfs4_ol_stateid *stp, *st_next; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6263 | struct nfs4_ol_stateid *lst, *lst_next; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6264 | u64 count = 0; |
| 6265 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6266 | spin_lock(&clp->cl_lock); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6267 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6268 | list_for_each_entry_safe(stp, st_next, |
| 6269 | &oop->oo_owner.so_stateids, st_perstateowner) { |
| 6270 | list_for_each_entry_safe(lst, lst_next, |
| 6271 | &stp->st_locks, st_locks) { |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 6272 | if (func) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6273 | if (func(lst)) |
| 6274 | nfsd_inject_add_lock_to_list(lst, |
| 6275 | collect); |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 6276 | } |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6277 | ++count; |
| 6278 | /* |
| 6279 | * Despite the fact that these functions deal |
| 6280 | * with 64-bit integers for "count", we must |
| 6281 | * ensure that it doesn't blow up the |
| 6282 | * clp->cl_refcount. Throw a warning if we |
| 6283 | * start to approach INT_MAX here. |
| 6284 | */ |
| 6285 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 6286 | if (count == max) |
| 6287 | goto out; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6288 | } |
| 6289 | } |
| 6290 | } |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6291 | out: |
| 6292 | spin_unlock(&clp->cl_lock); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6293 | |
| 6294 | return count; |
| 6295 | } |
| 6296 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6297 | static u64 |
| 6298 | nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect, |
| 6299 | u64 max) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6300 | { |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6301 | return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6302 | } |
| 6303 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6304 | static u64 |
| 6305 | nfsd_print_client_locks(struct nfs4_client *clp) |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6306 | { |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6307 | u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6308 | nfsd_print_count(clp, count, "locked files"); |
| 6309 | return count; |
| 6310 | } |
| 6311 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6312 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6313 | nfsd_inject_print_locks(void) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6314 | { |
| 6315 | struct nfs4_client *clp; |
| 6316 | u64 count = 0; |
| 6317 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6318 | nfsd_net_id); |
| 6319 | |
| 6320 | if (!nfsd_netns_ready(nn)) |
| 6321 | return 0; |
| 6322 | |
| 6323 | spin_lock(&nn->client_lock); |
| 6324 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 6325 | count += nfsd_print_client_locks(clp); |
| 6326 | spin_unlock(&nn->client_lock); |
| 6327 | |
| 6328 | return count; |
| 6329 | } |
| 6330 | |
| 6331 | static void |
| 6332 | nfsd_reap_locks(struct list_head *reaplist) |
| 6333 | { |
| 6334 | struct nfs4_client *clp; |
| 6335 | struct nfs4_ol_stateid *stp, *next; |
| 6336 | |
| 6337 | list_for_each_entry_safe(stp, next, reaplist, st_locks) { |
| 6338 | list_del_init(&stp->st_locks); |
| 6339 | clp = stp->st_stid.sc_client; |
| 6340 | nfs4_put_stid(&stp->st_stid); |
| 6341 | put_client(clp); |
| 6342 | } |
| 6343 | } |
| 6344 | |
| 6345 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6346 | nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6347 | { |
| 6348 | unsigned int count = 0; |
| 6349 | struct nfs4_client *clp; |
| 6350 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6351 | nfsd_net_id); |
| 6352 | LIST_HEAD(reaplist); |
| 6353 | |
| 6354 | if (!nfsd_netns_ready(nn)) |
| 6355 | return count; |
| 6356 | |
| 6357 | spin_lock(&nn->client_lock); |
| 6358 | clp = nfsd_find_client(addr, addr_size); |
| 6359 | if (clp) |
| 6360 | count = nfsd_collect_client_locks(clp, &reaplist, 0); |
| 6361 | spin_unlock(&nn->client_lock); |
| 6362 | nfsd_reap_locks(&reaplist); |
| 6363 | return count; |
| 6364 | } |
| 6365 | |
| 6366 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6367 | nfsd_inject_forget_locks(u64 max) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6368 | { |
| 6369 | u64 count = 0; |
| 6370 | struct nfs4_client *clp; |
| 6371 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6372 | nfsd_net_id); |
| 6373 | LIST_HEAD(reaplist); |
| 6374 | |
| 6375 | if (!nfsd_netns_ready(nn)) |
| 6376 | return count; |
| 6377 | |
| 6378 | spin_lock(&nn->client_lock); |
| 6379 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6380 | count += nfsd_collect_client_locks(clp, &reaplist, max - count); |
| 6381 | if (max != 0 && count >= max) |
| 6382 | break; |
| 6383 | } |
| 6384 | spin_unlock(&nn->client_lock); |
| 6385 | nfsd_reap_locks(&reaplist); |
| 6386 | return count; |
| 6387 | } |
| 6388 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6389 | static u64 |
| 6390 | nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max, |
| 6391 | struct list_head *collect, |
| 6392 | void (*func)(struct nfs4_openowner *)) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6393 | { |
| 6394 | struct nfs4_openowner *oop, *next; |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6395 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6396 | nfsd_net_id); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6397 | u64 count = 0; |
| 6398 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6399 | lockdep_assert_held(&nn->client_lock); |
| 6400 | |
| 6401 | spin_lock(&clp->cl_lock); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6402 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6403 | if (func) { |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6404 | func(oop); |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6405 | if (collect) { |
| 6406 | atomic_inc(&clp->cl_refcount); |
| 6407 | list_add(&oop->oo_perclient, collect); |
| 6408 | } |
| 6409 | } |
| 6410 | ++count; |
| 6411 | /* |
| 6412 | * Despite the fact that these functions deal with |
| 6413 | * 64-bit integers for "count", we must ensure that |
| 6414 | * it doesn't blow up the clp->cl_refcount. Throw a |
| 6415 | * warning if we start to approach INT_MAX here. |
| 6416 | */ |
| 6417 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 6418 | if (count == max) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6419 | break; |
| 6420 | } |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6421 | spin_unlock(&clp->cl_lock); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6422 | |
| 6423 | return count; |
| 6424 | } |
| 6425 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6426 | static u64 |
| 6427 | nfsd_print_client_openowners(struct nfs4_client *clp) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6428 | { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6429 | u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL); |
| 6430 | |
| 6431 | nfsd_print_count(clp, count, "openowners"); |
| 6432 | return count; |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6433 | } |
| 6434 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6435 | static u64 |
| 6436 | nfsd_collect_client_openowners(struct nfs4_client *clp, |
| 6437 | struct list_head *collect, u64 max) |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6438 | { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6439 | return nfsd_foreach_client_openowner(clp, max, collect, |
| 6440 | unhash_openowner_locked); |
| 6441 | } |
| 6442 | |
| 6443 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6444 | nfsd_inject_print_openowners(void) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6445 | { |
| 6446 | struct nfs4_client *clp; |
| 6447 | u64 count = 0; |
| 6448 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6449 | nfsd_net_id); |
| 6450 | |
| 6451 | if (!nfsd_netns_ready(nn)) |
| 6452 | return 0; |
| 6453 | |
| 6454 | spin_lock(&nn->client_lock); |
| 6455 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 6456 | count += nfsd_print_client_openowners(clp); |
| 6457 | spin_unlock(&nn->client_lock); |
| 6458 | |
| 6459 | return count; |
| 6460 | } |
| 6461 | |
| 6462 | static void |
| 6463 | nfsd_reap_openowners(struct list_head *reaplist) |
| 6464 | { |
| 6465 | struct nfs4_client *clp; |
| 6466 | struct nfs4_openowner *oop, *next; |
| 6467 | |
| 6468 | list_for_each_entry_safe(oop, next, reaplist, oo_perclient) { |
| 6469 | list_del_init(&oop->oo_perclient); |
| 6470 | clp = oop->oo_owner.so_client; |
| 6471 | release_openowner(oop); |
| 6472 | put_client(clp); |
| 6473 | } |
| 6474 | } |
| 6475 | |
| 6476 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6477 | nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr, |
| 6478 | size_t addr_size) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6479 | { |
| 6480 | unsigned int count = 0; |
| 6481 | struct nfs4_client *clp; |
| 6482 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6483 | nfsd_net_id); |
| 6484 | LIST_HEAD(reaplist); |
| 6485 | |
| 6486 | if (!nfsd_netns_ready(nn)) |
| 6487 | return count; |
| 6488 | |
| 6489 | spin_lock(&nn->client_lock); |
| 6490 | clp = nfsd_find_client(addr, addr_size); |
| 6491 | if (clp) |
| 6492 | count = nfsd_collect_client_openowners(clp, &reaplist, 0); |
| 6493 | spin_unlock(&nn->client_lock); |
| 6494 | nfsd_reap_openowners(&reaplist); |
| 6495 | return count; |
| 6496 | } |
| 6497 | |
| 6498 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6499 | nfsd_inject_forget_openowners(u64 max) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6500 | { |
| 6501 | u64 count = 0; |
| 6502 | struct nfs4_client *clp; |
| 6503 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6504 | nfsd_net_id); |
| 6505 | LIST_HEAD(reaplist); |
| 6506 | |
| 6507 | if (!nfsd_netns_ready(nn)) |
| 6508 | return count; |
| 6509 | |
| 6510 | spin_lock(&nn->client_lock); |
| 6511 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6512 | count += nfsd_collect_client_openowners(clp, &reaplist, |
| 6513 | max - count); |
| 6514 | if (max != 0 && count >= max) |
| 6515 | break; |
| 6516 | } |
| 6517 | spin_unlock(&nn->client_lock); |
| 6518 | nfsd_reap_openowners(&reaplist); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6519 | return count; |
| 6520 | } |
| 6521 | |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6522 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
| 6523 | struct list_head *victims) |
| 6524 | { |
| 6525 | struct nfs4_delegation *dp, *next; |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6526 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6527 | nfsd_net_id); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6528 | u64 count = 0; |
| 6529 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6530 | lockdep_assert_held(&nn->client_lock); |
| 6531 | |
| 6532 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6533 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 6534 | if (victims) { |
| 6535 | /* |
| 6536 | * It's not safe to mess with delegations that have a |
| 6537 | * non-zero dl_time. They might have already been broken |
| 6538 | * and could be processed by the laundromat outside of |
| 6539 | * the state_lock. Just leave them be. |
| 6540 | */ |
| 6541 | if (dp->dl_time != 0) |
| 6542 | continue; |
| 6543 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6544 | atomic_inc(&clp->cl_refcount); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 6545 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 6546 | list_add(&dp->dl_recall_lru, victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 6547 | } |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6548 | ++count; |
| 6549 | /* |
| 6550 | * Despite the fact that these functions deal with |
| 6551 | * 64-bit integers for "count", we must ensure that |
| 6552 | * it doesn't blow up the clp->cl_refcount. Throw a |
| 6553 | * warning if we start to approach INT_MAX here. |
| 6554 | */ |
| 6555 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 6556 | if (count == max) |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6557 | break; |
| 6558 | } |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6559 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6560 | return count; |
| 6561 | } |
| 6562 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6563 | static u64 |
| 6564 | nfsd_print_client_delegations(struct nfs4_client *clp) |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6565 | { |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6566 | u64 count = nfsd_find_all_delegations(clp, 0, NULL); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6567 | |
| 6568 | nfsd_print_count(clp, count, "delegations"); |
| 6569 | return count; |
| 6570 | } |
| 6571 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6572 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6573 | nfsd_inject_print_delegations(void) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6574 | { |
| 6575 | struct nfs4_client *clp; |
| 6576 | u64 count = 0; |
| 6577 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6578 | nfsd_net_id); |
| 6579 | |
| 6580 | if (!nfsd_netns_ready(nn)) |
| 6581 | return 0; |
| 6582 | |
| 6583 | spin_lock(&nn->client_lock); |
| 6584 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 6585 | count += nfsd_print_client_delegations(clp); |
| 6586 | spin_unlock(&nn->client_lock); |
| 6587 | |
| 6588 | return count; |
| 6589 | } |
| 6590 | |
| 6591 | static void |
| 6592 | nfsd_forget_delegations(struct list_head *reaplist) |
| 6593 | { |
| 6594 | struct nfs4_client *clp; |
| 6595 | struct nfs4_delegation *dp, *next; |
| 6596 | |
| 6597 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 6598 | list_del_init(&dp->dl_recall_lru); |
| 6599 | clp = dp->dl_stid.sc_client; |
| 6600 | revoke_delegation(dp); |
| 6601 | put_client(clp); |
| 6602 | } |
| 6603 | } |
| 6604 | |
| 6605 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6606 | nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr, |
| 6607 | size_t addr_size) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6608 | { |
| 6609 | u64 count = 0; |
| 6610 | struct nfs4_client *clp; |
| 6611 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6612 | nfsd_net_id); |
| 6613 | LIST_HEAD(reaplist); |
| 6614 | |
| 6615 | if (!nfsd_netns_ready(nn)) |
| 6616 | return count; |
| 6617 | |
| 6618 | spin_lock(&nn->client_lock); |
| 6619 | clp = nfsd_find_client(addr, addr_size); |
| 6620 | if (clp) |
| 6621 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 6622 | spin_unlock(&nn->client_lock); |
| 6623 | |
| 6624 | nfsd_forget_delegations(&reaplist); |
| 6625 | return count; |
| 6626 | } |
| 6627 | |
| 6628 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6629 | nfsd_inject_forget_delegations(u64 max) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6630 | { |
| 6631 | u64 count = 0; |
| 6632 | struct nfs4_client *clp; |
| 6633 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6634 | nfsd_net_id); |
| 6635 | LIST_HEAD(reaplist); |
| 6636 | |
| 6637 | if (!nfsd_netns_ready(nn)) |
| 6638 | return count; |
| 6639 | |
| 6640 | spin_lock(&nn->client_lock); |
| 6641 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6642 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 6643 | if (max != 0 && count >= max) |
| 6644 | break; |
| 6645 | } |
| 6646 | spin_unlock(&nn->client_lock); |
| 6647 | nfsd_forget_delegations(&reaplist); |
| 6648 | return count; |
| 6649 | } |
| 6650 | |
| 6651 | static void |
| 6652 | nfsd_recall_delegations(struct list_head *reaplist) |
| 6653 | { |
| 6654 | struct nfs4_client *clp; |
| 6655 | struct nfs4_delegation *dp, *next; |
| 6656 | |
| 6657 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 6658 | list_del_init(&dp->dl_recall_lru); |
| 6659 | clp = dp->dl_stid.sc_client; |
| 6660 | /* |
| 6661 | * We skipped all entries that had a zero dl_time before, |
| 6662 | * so we can now reset the dl_time back to 0. If a delegation |
| 6663 | * break comes in now, then it won't make any difference since |
| 6664 | * we're recalling it either way. |
| 6665 | */ |
| 6666 | spin_lock(&state_lock); |
| 6667 | dp->dl_time = 0; |
| 6668 | spin_unlock(&state_lock); |
| 6669 | nfsd_break_one_deleg(dp); |
| 6670 | put_client(clp); |
| 6671 | } |
| 6672 | } |
| 6673 | |
| 6674 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6675 | nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr, |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6676 | size_t addr_size) |
| 6677 | { |
| 6678 | u64 count = 0; |
| 6679 | struct nfs4_client *clp; |
| 6680 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6681 | nfsd_net_id); |
| 6682 | LIST_HEAD(reaplist); |
| 6683 | |
| 6684 | if (!nfsd_netns_ready(nn)) |
| 6685 | return count; |
| 6686 | |
| 6687 | spin_lock(&nn->client_lock); |
| 6688 | clp = nfsd_find_client(addr, addr_size); |
| 6689 | if (clp) |
| 6690 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 6691 | spin_unlock(&nn->client_lock); |
| 6692 | |
| 6693 | nfsd_recall_delegations(&reaplist); |
| 6694 | return count; |
| 6695 | } |
| 6696 | |
| 6697 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6698 | nfsd_inject_recall_delegations(u64 max) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6699 | { |
| 6700 | u64 count = 0; |
| 6701 | struct nfs4_client *clp, *next; |
| 6702 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6703 | nfsd_net_id); |
| 6704 | LIST_HEAD(reaplist); |
| 6705 | |
| 6706 | if (!nfsd_netns_ready(nn)) |
| 6707 | return count; |
| 6708 | |
| 6709 | spin_lock(&nn->client_lock); |
| 6710 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 6711 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 6712 | if (max != 0 && ++count >= max) |
| 6713 | break; |
| 6714 | } |
| 6715 | spin_unlock(&nn->client_lock); |
| 6716 | nfsd_recall_delegations(&reaplist); |
| 6717 | return count; |
| 6718 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 6719 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
| 6720 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 6721 | /* |
| 6722 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 6723 | * client may quite reasonably hang on to a delegation as long as it has |
| 6724 | * the inode cached. This becomes an obvious problem the first time a |
| 6725 | * client's inode cache approaches the size of the server's total memory. |
| 6726 | * |
| 6727 | * For now we avoid this problem by imposing a hard limit on the number |
| 6728 | * of delegations, which varies according to the server's memory size. |
| 6729 | */ |
| 6730 | static void |
| 6731 | set_max_delegations(void) |
| 6732 | { |
| 6733 | /* |
| 6734 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 6735 | * estimates suggest that in the worst case (where every delegation |
| 6736 | * is for a different inode), a delegation could take about 1.5K, |
| 6737 | * giving a worst case usage of about 6% of memory. |
| 6738 | */ |
| 6739 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 6740 | } |
| 6741 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6742 | static int nfs4_state_create_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6743 | { |
| 6744 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 6745 | int i; |
| 6746 | |
| 6747 | nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 6748 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 6749 | if (!nn->conf_id_hashtbl) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 6750 | goto err; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 6751 | nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 6752 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 6753 | if (!nn->unconf_id_hashtbl) |
| 6754 | goto err_unconf_id; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 6755 | nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) * |
| 6756 | SESSION_HASH_SIZE, GFP_KERNEL); |
| 6757 | if (!nn->sessionid_hashtbl) |
| 6758 | goto err_sessionid; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6759 | |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 6760 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6761 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 6762 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 6763 | } |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 6764 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 6765 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 6766 | nn->conf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 6767 | nn->unconf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 6768 | INIT_LIST_HEAD(&nn->client_lru); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 6769 | INIT_LIST_HEAD(&nn->close_lru); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 6770 | INIT_LIST_HEAD(&nn->del_recall_lru); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 6771 | spin_lock_init(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6772 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 6773 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6774 | get_net(net); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 6775 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6776 | return 0; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 6777 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 6778 | err_sessionid: |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 6779 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 6780 | err_unconf_id: |
| 6781 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 6782 | err: |
| 6783 | return -ENOMEM; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6784 | } |
| 6785 | |
| 6786 | static void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6787 | nfs4_state_destroy_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6788 | { |
| 6789 | int i; |
| 6790 | struct nfs4_client *clp = NULL; |
| 6791 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 6792 | |
| 6793 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 6794 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 6795 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 6796 | destroy_client(clp); |
| 6797 | } |
| 6798 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 6799 | |
Kinglong Mee | 2b90563 | 2014-03-26 22:09:30 +0800 | [diff] [blame] | 6800 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 6801 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 6802 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 6803 | destroy_client(clp); |
| 6804 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 6805 | } |
| 6806 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 6807 | kfree(nn->sessionid_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 6808 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6809 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6810 | put_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6811 | } |
| 6812 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 6813 | int |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6814 | nfs4_state_start_net(struct net *net) |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 6815 | { |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 6816 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6817 | int ret; |
| 6818 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6819 | ret = nfs4_state_create_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6820 | if (ret) |
| 6821 | return ret; |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 6822 | nn->boot_time = get_seconds(); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 6823 | nn->grace_ended = false; |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 6824 | nn->nfsd4_manager.block_opens = true; |
Jeff Layton | d4318ac | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 6825 | locks_start_grace(net, &nn->nfsd4_manager); |
| 6826 | nfsd4_client_tracking_init(net); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6827 | printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n", |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 6828 | nn->nfsd4_grace, net); |
| 6829 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6830 | return 0; |
| 6831 | } |
| 6832 | |
| 6833 | /* initialization to perform when the nfsd service is started: */ |
| 6834 | |
| 6835 | int |
| 6836 | nfs4_state_start(void) |
| 6837 | { |
| 6838 | int ret; |
| 6839 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6840 | ret = set_callback_cred(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6841 | if (ret) |
| 6842 | return -ENOMEM; |
Jeff Layton | 51a5456 | 2015-08-20 07:17:01 -0400 | [diff] [blame] | 6843 | laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 6844 | if (laundry_wq == NULL) { |
| 6845 | ret = -ENOMEM; |
| 6846 | goto out_recovery; |
| 6847 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6848 | ret = nfsd4_create_callback_queue(); |
| 6849 | if (ret) |
| 6850 | goto out_free_laundry; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 6851 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 6852 | set_max_delegations(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6853 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6854 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6855 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6856 | out_free_laundry: |
| 6857 | destroy_workqueue(laundry_wq); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 6858 | out_recovery: |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6859 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6860 | } |
| 6861 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 6862 | void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6863 | nfs4_state_shutdown_net(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6864 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6866 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6867 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6868 | |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6869 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 6870 | locks_end_grace(&nn->nfsd4_manager); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 6871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6872 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 6873 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 6874 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6875 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 6876 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 6877 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 6879 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6880 | list_for_each_safe(pos, next, &reaplist) { |
| 6881 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 6882 | list_del_init(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 6883 | put_clnt_odstate(dp->dl_clnt_odstate); |
Jeff Layton | afbda40 | 2014-08-09 10:22:41 -0400 | [diff] [blame] | 6884 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 6885 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6886 | } |
| 6887 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6888 | nfsd4_client_tracking_exit(net); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6889 | nfs4_state_destroy_net(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6890 | } |
| 6891 | |
| 6892 | void |
| 6893 | nfs4_state_shutdown(void) |
| 6894 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 6895 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 6896 | nfsd4_destroy_callback_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6897 | } |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6898 | |
| 6899 | static void |
| 6900 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 6901 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 6902 | if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid)) |
| 6903 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6904 | } |
| 6905 | |
| 6906 | static void |
| 6907 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 6908 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 6909 | if (cstate->minorversion) { |
| 6910 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
| 6911 | SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
| 6912 | } |
| 6913 | } |
| 6914 | |
| 6915 | void |
| 6916 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 6917 | { |
| 6918 | CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6919 | } |
| 6920 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6921 | /* |
| 6922 | * functions to set current state id |
| 6923 | */ |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6924 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 6925 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 6926 | { |
| 6927 | put_stateid(cstate, &odp->od_stateid); |
| 6928 | } |
| 6929 | |
| 6930 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6931 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open) |
| 6932 | { |
| 6933 | put_stateid(cstate, &open->op_stateid); |
| 6934 | } |
| 6935 | |
| 6936 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6937 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 6938 | { |
| 6939 | put_stateid(cstate, &close->cl_stateid); |
| 6940 | } |
| 6941 | |
| 6942 | void |
| 6943 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock) |
| 6944 | { |
| 6945 | put_stateid(cstate, &lock->lk_resp_stateid); |
| 6946 | } |
| 6947 | |
| 6948 | /* |
| 6949 | * functions to consume current state id |
| 6950 | */ |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 6951 | |
| 6952 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 6953 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 6954 | { |
| 6955 | get_stateid(cstate, &odp->od_stateid); |
| 6956 | } |
| 6957 | |
| 6958 | void |
| 6959 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp) |
| 6960 | { |
| 6961 | get_stateid(cstate, &drp->dr_stateid); |
| 6962 | } |
| 6963 | |
| 6964 | void |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 6965 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp) |
| 6966 | { |
| 6967 | get_stateid(cstate, &fsp->fr_stateid); |
| 6968 | } |
| 6969 | |
| 6970 | void |
| 6971 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr) |
| 6972 | { |
| 6973 | get_stateid(cstate, &setattr->sa_stateid); |
| 6974 | } |
| 6975 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6976 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6977 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 6978 | { |
| 6979 | get_stateid(cstate, &close->cl_stateid); |
| 6980 | } |
| 6981 | |
| 6982 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6983 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6984 | { |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6985 | get_stateid(cstate, &locku->lu_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6986 | } |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 6987 | |
| 6988 | void |
| 6989 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read) |
| 6990 | { |
| 6991 | get_stateid(cstate, &read->rd_stateid); |
| 6992 | } |
| 6993 | |
| 6994 | void |
| 6995 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write) |
| 6996 | { |
| 6997 | get_stateid(cstate, &write->wr_stateid); |
| 6998 | } |