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 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 101 | static struct nfsd4_callback_ops nfsd4_cb_recall_ops; |
| 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); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 578 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 579 | /* |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 580 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 581 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 582 | * example, a stray write retransmission could be accepted by |
| 583 | * the server when it should have been rejected. Therefore, |
| 584 | * adopt a trick from the sctp code to attempt to maximize the |
| 585 | * amount of time until an id is reused, by ensuring they always |
| 586 | * "increase" (mod INT_MAX): |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 587 | */ |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 588 | return stid; |
| 589 | out_free: |
Wei Yongjun | 2c44a23 | 2013-04-09 14:15:31 +0800 | [diff] [blame] | 590 | kmem_cache_free(slab, stid); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 591 | return NULL; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 592 | } |
| 593 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 594 | 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] | 595 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 596 | struct nfs4_stid *stid; |
| 597 | struct nfs4_ol_stateid *stp; |
| 598 | |
| 599 | stid = nfs4_alloc_stid(clp, stateid_slab); |
| 600 | if (!stid) |
| 601 | return NULL; |
| 602 | |
| 603 | stp = openlockstateid(stid); |
| 604 | stp->st_stid.sc_free = nfs4_free_ol_stateid; |
| 605 | return stp; |
| 606 | } |
| 607 | |
| 608 | static void nfs4_free_deleg(struct nfs4_stid *stid) |
| 609 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 610 | kmem_cache_free(deleg_slab, stid); |
| 611 | atomic_long_dec(&num_delegations); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 612 | } |
| 613 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 614 | /* |
| 615 | * When we recall a delegation, we should be careful not to hand it |
| 616 | * out again straight away. |
| 617 | * To ensure this we keep a pair of bloom filters ('new' and 'old') |
| 618 | * in which the filehandles of recalled delegations are "stored". |
| 619 | * If a filehandle appear in either filter, a delegation is blocked. |
| 620 | * When a delegation is recalled, the filehandle is stored in the "new" |
| 621 | * filter. |
| 622 | * Every 30 seconds we swap the filters and clear the "new" one, |
| 623 | * unless both are empty of course. |
| 624 | * |
| 625 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the |
| 626 | * low 3 bytes as hash-table indices. |
| 627 | * |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 628 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 629 | * is used to manage concurrent access. Testing does not need the lock |
| 630 | * except when swapping the two filters. |
| 631 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 632 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 633 | static struct bloom_pair { |
| 634 | int entries, old_entries; |
| 635 | time_t swap_time; |
| 636 | int new; /* index into 'set' */ |
| 637 | DECLARE_BITMAP(set[2], 256); |
| 638 | } blocked_delegations; |
| 639 | |
| 640 | static int delegation_blocked(struct knfsd_fh *fh) |
| 641 | { |
| 642 | u32 hash; |
| 643 | struct bloom_pair *bd = &blocked_delegations; |
| 644 | |
| 645 | if (bd->entries == 0) |
| 646 | return 0; |
| 647 | if (seconds_since_boot() - bd->swap_time > 30) { |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 648 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 649 | if (seconds_since_boot() - bd->swap_time > 30) { |
| 650 | bd->entries -= bd->old_entries; |
| 651 | bd->old_entries = bd->entries; |
| 652 | memset(bd->set[bd->new], 0, |
| 653 | sizeof(bd->set[0])); |
| 654 | bd->new = 1-bd->new; |
| 655 | bd->swap_time = seconds_since_boot(); |
| 656 | } |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 657 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 658 | } |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 659 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 660 | if (test_bit(hash&255, bd->set[0]) && |
| 661 | test_bit((hash>>8)&255, bd->set[0]) && |
| 662 | test_bit((hash>>16)&255, bd->set[0])) |
| 663 | return 1; |
| 664 | |
| 665 | if (test_bit(hash&255, bd->set[1]) && |
| 666 | test_bit((hash>>8)&255, bd->set[1]) && |
| 667 | test_bit((hash>>16)&255, bd->set[1])) |
| 668 | return 1; |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static void block_delegations(struct knfsd_fh *fh) |
| 674 | { |
| 675 | u32 hash; |
| 676 | struct bloom_pair *bd = &blocked_delegations; |
| 677 | |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 678 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 679 | |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 680 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 681 | __set_bit(hash&255, bd->set[bd->new]); |
| 682 | __set_bit((hash>>8)&255, bd->set[bd->new]); |
| 683 | __set_bit((hash>>16)&255, bd->set[bd->new]); |
| 684 | if (bd->entries == 0) |
| 685 | bd->swap_time = seconds_since_boot(); |
| 686 | bd->entries += 1; |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 687 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 688 | } |
| 689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | static struct nfs4_delegation * |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 691 | alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh, |
| 692 | struct nfs4_clnt_odstate *odstate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
| 694 | struct nfs4_delegation *dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 695 | long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
| 697 | dprintk("NFSD alloc_init_deleg\n"); |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 698 | n = atomic_long_inc_return(&num_delegations); |
| 699 | if (n < 0 || n > max_delegations) |
| 700 | goto out_dec; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 701 | if (delegation_blocked(¤t_fh->fh_handle)) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 702 | goto out_dec; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 703 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab)); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 704 | if (dp == NULL) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 705 | goto out_dec; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 706 | |
| 707 | dp->dl_stid.sc_free = nfs4_free_deleg; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 708 | /* |
| 709 | * delegation seqid's are never incremented. The 4.1 special |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 710 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 711 | * 0 anyway just for consistency and use 1: |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 712 | */ |
| 713 | dp->dl_stid.sc_stateid.si_generation = 1; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 714 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 715 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 717 | dp->dl_clnt_odstate = odstate; |
| 718 | get_clnt_odstate(odstate); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 719 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 720 | dp->dl_retries = 1; |
| 721 | nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 722 | &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | return dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 724 | out_dec: |
| 725 | atomic_long_dec(&num_delegations); |
| 726 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | void |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 730 | nfs4_put_stid(struct nfs4_stid *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 732 | struct nfs4_file *fp = s->sc_file; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 733 | struct nfs4_client *clp = s->sc_client; |
| 734 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 735 | might_lock(&clp->cl_lock); |
| 736 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 737 | if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) { |
| 738 | wake_up_all(&close_wq); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 739 | return; |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 740 | } |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 741 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 742 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 743 | s->sc_free(s); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 744 | if (fp) |
| 745 | put_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 748 | static void nfs4_put_deleg_lease(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | { |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 750 | struct file *filp = NULL; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 751 | |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 752 | spin_lock(&fp->fi_lock); |
Jeff Layton | 67db103 | 2014-12-13 09:11:40 -0500 | [diff] [blame] | 753 | if (fp->fi_deleg_file && --fp->fi_delegees == 0) |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 754 | swap(filp, fp->fi_deleg_file); |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 755 | spin_unlock(&fp->fi_lock); |
| 756 | |
| 757 | if (filp) { |
Christoph Hellwig | 2ab99ee | 2015-01-21 19:14:02 +0100 | [diff] [blame] | 758 | vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp); |
Jeff Layton | 6bcc034 | 2014-08-09 10:22:40 -0400 | [diff] [blame] | 759 | fput(filp); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 760 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 763 | void nfs4_unhash_stid(struct nfs4_stid *s) |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 764 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 765 | s->sc_type = 0; |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 766 | } |
| 767 | |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 768 | static void |
| 769 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 770 | { |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 771 | lockdep_assert_held(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 772 | lockdep_assert_held(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 773 | |
Trond Myklebust | 67cb127 | 2014-07-29 21:34:17 -0400 | [diff] [blame] | 774 | atomic_inc(&dp->dl_stid.sc_count); |
Benny Halevy | 3fb87d1 | 2014-05-30 09:09:31 -0400 | [diff] [blame] | 775 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 776 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
| 777 | list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations); |
| 778 | } |
| 779 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 780 | static bool |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 781 | unhash_delegation_locked(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 783 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 784 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 785 | lockdep_assert_held(&state_lock); |
| 786 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 787 | if (list_empty(&dp->dl_perfile)) |
| 788 | return false; |
| 789 | |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 790 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
Jeff Layton | d55a166 | 2014-07-22 13:52:06 -0400 | [diff] [blame] | 791 | /* Ensure that deleg break won't try to requeue it */ |
| 792 | ++dp->dl_time; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 793 | spin_lock(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 794 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | list_del_init(&dp->dl_recall_lru); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 796 | list_del_init(&dp->dl_perfile); |
| 797 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 798 | return true; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 799 | } |
| 800 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 801 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 802 | { |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 803 | bool unhashed; |
| 804 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 805 | spin_lock(&state_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 806 | unhashed = unhash_delegation_locked(dp); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 807 | spin_unlock(&state_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 808 | if (unhashed) { |
| 809 | put_clnt_odstate(dp->dl_clnt_odstate); |
| 810 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
| 811 | nfs4_put_stid(&dp->dl_stid); |
| 812 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 816 | { |
| 817 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 818 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 819 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
| 820 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 821 | put_clnt_odstate(dp->dl_clnt_odstate); |
Jeff Layton | afbda40 | 2014-08-09 10:22:41 -0400 | [diff] [blame] | 822 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
| 823 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 824 | if (clp->cl_minorversion == 0) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 825 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 826 | else { |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 827 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 828 | spin_lock(&clp->cl_lock); |
| 829 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 830 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | /* |
| 835 | * SETCLIENTID state |
| 836 | */ |
| 837 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 838 | static unsigned int clientid_hashval(u32 id) |
| 839 | { |
| 840 | return id & CLIENT_HASH_MASK; |
| 841 | } |
| 842 | |
| 843 | static unsigned int clientstr_hashval(const char *name) |
| 844 | { |
| 845 | return opaque_hashval(name, 8) & CLIENT_HASH_MASK; |
| 846 | } |
| 847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | /* |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 849 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 850 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 851 | * only what share bits are currently in force, but also what |
| 852 | * combinations of share bits previous opens have used. This allows us |
| 853 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 854 | * return an error if the client attempt to downgrade to a combination |
| 855 | * of share bits not explicable by closing some of its previous opens. |
| 856 | * |
| 857 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 858 | * track of access/deny bit combinations; so, e.g., we allow: |
| 859 | * |
| 860 | * OPEN allow read, deny write |
| 861 | * OPEN allow both, deny none |
| 862 | * DOWNGRADE allow read, deny none |
| 863 | * |
| 864 | * which we should reject. |
| 865 | */ |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 866 | static unsigned int |
| 867 | bmap_to_share_mode(unsigned long bmap) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 868 | int i; |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 869 | unsigned int access = 0; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 870 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 871 | for (i = 1; i < 4; i++) { |
| 872 | if (test_bit(i, &bmap)) |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 873 | access |= i; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 874 | } |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 875 | return access; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 876 | } |
| 877 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 878 | /* set share access for a given stateid */ |
| 879 | static inline void |
| 880 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 881 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 882 | unsigned char mask = 1 << access; |
| 883 | |
| 884 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 885 | stp->st_access_bmap |= mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /* clear share access for a given stateid */ |
| 889 | static inline void |
| 890 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 891 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 892 | unsigned char mask = 1 << access; |
| 893 | |
| 894 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 895 | stp->st_access_bmap &= ~mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /* test whether a given stateid has access */ |
| 899 | static inline bool |
| 900 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 901 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 902 | unsigned char mask = 1 << access; |
| 903 | |
| 904 | return (bool)(stp->st_access_bmap & mask); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 905 | } |
| 906 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 907 | /* set share deny for a given stateid */ |
| 908 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 909 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 910 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 911 | unsigned char mask = 1 << deny; |
| 912 | |
| 913 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 914 | stp->st_deny_bmap |= mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | /* clear share deny for a given stateid */ |
| 918 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 919 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 920 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 921 | unsigned char mask = 1 << deny; |
| 922 | |
| 923 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 924 | stp->st_deny_bmap &= ~mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | /* test whether a given stateid is denying specific access */ |
| 928 | static inline bool |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 929 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 930 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 931 | unsigned char mask = 1 << deny; |
| 932 | |
| 933 | return (bool)(stp->st_deny_bmap & mask); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | static int nfs4_access_to_omode(u32 access) |
| 937 | { |
J. Bruce Fields | 8f34a43 | 2010-09-02 15:23:16 -0400 | [diff] [blame] | 938 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 939 | case NFS4_SHARE_ACCESS_READ: |
| 940 | return O_RDONLY; |
| 941 | case NFS4_SHARE_ACCESS_WRITE: |
| 942 | return O_WRONLY; |
| 943 | case NFS4_SHARE_ACCESS_BOTH: |
| 944 | return O_RDWR; |
| 945 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 946 | WARN_ON_ONCE(1); |
| 947 | return O_RDONLY; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 948 | } |
| 949 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 950 | /* |
| 951 | * A stateid that had a deny mode associated with it is being released |
| 952 | * or downgraded. Recalculate the deny mode on the file. |
| 953 | */ |
| 954 | static void |
| 955 | recalculate_deny_mode(struct nfs4_file *fp) |
| 956 | { |
| 957 | struct nfs4_ol_stateid *stp; |
| 958 | |
| 959 | spin_lock(&fp->fi_lock); |
| 960 | fp->fi_share_deny = 0; |
| 961 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) |
| 962 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); |
| 963 | spin_unlock(&fp->fi_lock); |
| 964 | } |
| 965 | |
| 966 | static void |
| 967 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 968 | { |
| 969 | int i; |
| 970 | bool change = false; |
| 971 | |
| 972 | for (i = 1; i < 4; i++) { |
| 973 | if ((i & deny) != i) { |
| 974 | change = true; |
| 975 | clear_deny(i, stp); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | /* Recalculate per-file deny mode if there was a change */ |
| 980 | if (change) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 981 | recalculate_deny_mode(stp->st_stid.sc_file); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 982 | } |
| 983 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 984 | /* release all access and file references for a given stateid */ |
| 985 | static void |
| 986 | release_all_access(struct nfs4_ol_stateid *stp) |
| 987 | { |
| 988 | int i; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 989 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 990 | |
| 991 | if (fp && stp->st_deny_bmap != 0) |
| 992 | recalculate_deny_mode(fp); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 993 | |
| 994 | for (i = 1; i < 4; i++) { |
| 995 | if (test_access(i, stp)) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 996 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 997 | clear_access(i, stp); |
| 998 | } |
| 999 | } |
| 1000 | |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1001 | static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) |
| 1002 | { |
| 1003 | kfree(sop->so_owner.data); |
| 1004 | sop->so_ops->so_free(sop); |
| 1005 | } |
| 1006 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1007 | static void nfs4_put_stateowner(struct nfs4_stateowner *sop) |
| 1008 | { |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1009 | struct nfs4_client *clp = sop->so_client; |
| 1010 | |
| 1011 | might_lock(&clp->cl_lock); |
| 1012 | |
| 1013 | if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1014 | return; |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1015 | sop->so_ops->so_unhash(sop); |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1016 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1017 | nfs4_free_stateowner(sop); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1018 | } |
| 1019 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1020 | static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1021 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1022 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1023 | |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1024 | lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); |
| 1025 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1026 | if (list_empty(&stp->st_perfile)) |
| 1027 | return false; |
| 1028 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1029 | spin_lock(&fp->fi_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1030 | list_del_init(&stp->st_perfile); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1031 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1032 | list_del(&stp->st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1033 | return true; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1034 | } |
| 1035 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1036 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1037 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1038 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 1039 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 1040 | put_clnt_odstate(stp->st_clnt_odstate); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1041 | release_all_access(stp); |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1042 | if (stp->st_stateowner) |
| 1043 | nfs4_put_stateowner(stp->st_stateowner); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1044 | kmem_cache_free(stateid_slab, stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1045 | } |
| 1046 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1047 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1048 | { |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1049 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
| 1050 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1051 | struct file *file; |
| 1052 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1053 | file = find_any_file(stp->st_stid.sc_file); |
| 1054 | if (file) |
| 1055 | filp_close(file, (fl_owner_t)lo); |
| 1056 | nfs4_free_ol_stateid(stid); |
| 1057 | } |
| 1058 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1059 | /* |
| 1060 | * Put the persistent reference to an already unhashed generic stateid, while |
| 1061 | * holding the cl_lock. If it's the last reference, then put it onto the |
| 1062 | * reaplist for later destruction. |
| 1063 | */ |
| 1064 | static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, |
| 1065 | struct list_head *reaplist) |
| 1066 | { |
| 1067 | struct nfs4_stid *s = &stp->st_stid; |
| 1068 | struct nfs4_client *clp = s->sc_client; |
| 1069 | |
| 1070 | lockdep_assert_held(&clp->cl_lock); |
| 1071 | |
| 1072 | WARN_ON_ONCE(!list_empty(&stp->st_locks)); |
| 1073 | |
| 1074 | if (!atomic_dec_and_test(&s->sc_count)) { |
| 1075 | wake_up_all(&close_wq); |
| 1076 | return; |
| 1077 | } |
| 1078 | |
| 1079 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
| 1080 | list_add(&stp->st_locks, reaplist); |
| 1081 | } |
| 1082 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1083 | static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1084 | { |
| 1085 | struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner); |
| 1086 | |
| 1087 | lockdep_assert_held(&oo->oo_owner.so_client->cl_lock); |
| 1088 | |
| 1089 | list_del_init(&stp->st_locks); |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 1090 | nfs4_unhash_stid(&stp->st_stid); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1091 | return unhash_ol_stateid(stp); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1092 | } |
| 1093 | |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 1094 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1095 | { |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1096 | struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1097 | bool unhashed; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1098 | |
| 1099 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1100 | unhashed = unhash_lock_stateid(stp); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1101 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1102 | if (unhashed) |
| 1103 | nfs4_put_stid(&stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1104 | } |
| 1105 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1106 | static void unhash_lockowner_locked(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1107 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1108 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1109 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1110 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1111 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1112 | list_del_init(&lo->lo_owner.so_strhash); |
| 1113 | } |
| 1114 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1115 | /* |
| 1116 | * Free a list of generic stateids that were collected earlier after being |
| 1117 | * fully unhashed. |
| 1118 | */ |
| 1119 | static void |
| 1120 | free_ol_stateid_reaplist(struct list_head *reaplist) |
| 1121 | { |
| 1122 | struct nfs4_ol_stateid *stp; |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1123 | struct nfs4_file *fp; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1124 | |
| 1125 | might_sleep(); |
| 1126 | |
| 1127 | while (!list_empty(reaplist)) { |
| 1128 | stp = list_first_entry(reaplist, struct nfs4_ol_stateid, |
| 1129 | st_locks); |
| 1130 | list_del(&stp->st_locks); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1131 | fp = stp->st_stid.sc_file; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1132 | stp->st_stid.sc_free(&stp->st_stid); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1133 | if (fp) |
| 1134 | put_nfs4_file(fp); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1138 | static void release_lockowner(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1139 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1140 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1141 | struct nfs4_ol_stateid *stp; |
| 1142 | struct list_head reaplist; |
| 1143 | |
| 1144 | INIT_LIST_HEAD(&reaplist); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1145 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1146 | spin_lock(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1147 | unhash_lockowner_locked(lo); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1148 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 1149 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
| 1150 | struct nfs4_ol_stateid, st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1151 | WARN_ON(!unhash_lock_stateid(stp)); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1152 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1153 | } |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1154 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1155 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1156 | nfs4_put_stateowner(&lo->lo_owner); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1157 | } |
| 1158 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1159 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, |
| 1160 | struct list_head *reaplist) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1161 | { |
| 1162 | struct nfs4_ol_stateid *stp; |
| 1163 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1164 | lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); |
| 1165 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1166 | while (!list_empty(&open_stp->st_locks)) { |
| 1167 | stp = list_entry(open_stp->st_locks.next, |
| 1168 | struct nfs4_ol_stateid, st_locks); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1169 | WARN_ON(!unhash_lock_stateid(stp)); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1170 | put_ol_stateid_locked(stp, reaplist); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1174 | static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1175 | struct list_head *reaplist) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1176 | { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1177 | bool unhashed; |
| 1178 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1179 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
| 1180 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1181 | unhashed = unhash_ol_stateid(stp); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1182 | release_open_stateid_locks(stp, reaplist); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1183 | return unhashed; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 1187 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1188 | LIST_HEAD(reaplist); |
| 1189 | |
| 1190 | spin_lock(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1191 | if (unhash_open_stateid(stp, &reaplist)) |
| 1192 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1193 | spin_unlock(&stp->st_stid.sc_client->cl_lock); |
| 1194 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1195 | } |
| 1196 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1197 | static void unhash_openowner_locked(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1198 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1199 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1200 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1201 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1202 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1203 | list_del_init(&oo->oo_owner.so_strhash); |
| 1204 | list_del_init(&oo->oo_perclient); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1205 | } |
| 1206 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1207 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 1208 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1209 | struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, |
| 1210 | nfsd_net_id); |
| 1211 | struct nfs4_ol_stateid *s; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1212 | |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1213 | spin_lock(&nn->client_lock); |
| 1214 | s = oo->oo_last_closed_stid; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1215 | if (s) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1216 | list_del_init(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1217 | oo->oo_last_closed_stid = NULL; |
| 1218 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1219 | spin_unlock(&nn->client_lock); |
| 1220 | if (s) |
| 1221 | nfs4_put_stid(&s->st_stid); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1222 | } |
| 1223 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1224 | static void release_openowner(struct nfs4_openowner *oo) |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1225 | { |
| 1226 | struct nfs4_ol_stateid *stp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1227 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1228 | struct list_head reaplist; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1229 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1230 | INIT_LIST_HEAD(&reaplist); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1231 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1232 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1233 | unhash_openowner_locked(oo); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1234 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 1235 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
| 1236 | struct nfs4_ol_stateid, st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1237 | if (unhash_open_stateid(stp, &reaplist)) |
| 1238 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1239 | } |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1240 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1241 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1242 | release_last_closed_stateid(oo); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1243 | nfs4_put_stateowner(&oo->oo_owner); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1244 | } |
| 1245 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1246 | static inline int |
| 1247 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 1248 | { |
| 1249 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 1250 | |
| 1251 | return sid->sequence % SESSION_HASH_SIZE; |
| 1252 | } |
| 1253 | |
Mark Salter | 135dd00 | 2015-04-06 09:46:00 -0400 | [diff] [blame] | 1254 | #ifdef CONFIG_SUNRPC_DEBUG |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1255 | static inline void |
| 1256 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1257 | { |
| 1258 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 1259 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 1260 | } |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1261 | #else |
| 1262 | static inline void |
| 1263 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1264 | { |
| 1265 | } |
| 1266 | #endif |
| 1267 | |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1268 | /* |
| 1269 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 1270 | * won't be used for replay. |
| 1271 | */ |
| 1272 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 1273 | { |
| 1274 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 1275 | |
| 1276 | if (nfserr == nfserr_replay_me) |
| 1277 | return; |
| 1278 | |
| 1279 | if (!seqid_mutating_err(ntohl(nfserr))) { |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 1280 | nfsd4_cstate_clear_replay(cstate); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1281 | return; |
| 1282 | } |
| 1283 | if (!so) |
| 1284 | return; |
| 1285 | if (so->so_is_open_owner) |
| 1286 | release_last_closed_stateid(openowner(so)); |
| 1287 | so->so_seqid++; |
| 1288 | return; |
| 1289 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1290 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1291 | static void |
| 1292 | gen_sessionid(struct nfsd4_session *ses) |
| 1293 | { |
| 1294 | struct nfs4_client *clp = ses->se_client; |
| 1295 | struct nfsd4_sessionid *sid; |
| 1296 | |
| 1297 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 1298 | sid->clientid = clp->cl_clientid; |
| 1299 | sid->sequence = current_sessionid++; |
| 1300 | sid->reserved = 0; |
| 1301 | } |
| 1302 | |
| 1303 | /* |
Andy Adamson | a649637 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1304 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 1305 | * the rpc header, but all we need to cache is the data starting after |
| 1306 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 1307 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 1308 | * value that is the number of bytes in our cache plus a few additional |
| 1309 | * bytes. In order to stay on the safe side, and not promise more than |
| 1310 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 1311 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 1312 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 1313 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1314 | */ |
Andy Adamson | a649637 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1315 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 1316 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1317 | static void |
| 1318 | free_session_slots(struct nfsd4_session *ses) |
| 1319 | { |
| 1320 | int i; |
| 1321 | |
| 1322 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) |
| 1323 | kfree(ses->se_slots[i]); |
| 1324 | } |
| 1325 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1326 | /* |
| 1327 | * We don't actually need to cache the rpc and session headers, so we |
| 1328 | * can allocate a little less for each slot: |
| 1329 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1330 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1331 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1332 | u32 size; |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1333 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1334 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 1335 | size = 0; |
| 1336 | else |
| 1337 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 1338 | return size + sizeof(struct nfsd4_slot); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1339 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1340 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1341 | /* |
| 1342 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 1343 | * re-negotiate active sessions and reduce their slot usage to make |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 1344 | * 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] | 1345 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1346 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1347 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1348 | u32 slotsize = slot_bytes(ca); |
| 1349 | u32 num = ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1350 | int avail; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1351 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1352 | spin_lock(&nfsd_drc_lock); |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 1353 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, |
| 1354 | nfsd_drc_max_mem - nfsd_drc_mem_used); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1355 | num = min_t(int, num, avail / slotsize); |
| 1356 | nfsd_drc_mem_used += num * slotsize; |
| 1357 | spin_unlock(&nfsd_drc_lock); |
| 1358 | |
| 1359 | return num; |
| 1360 | } |
| 1361 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1362 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1363 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1364 | int slotsize = slot_bytes(ca); |
| 1365 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1366 | spin_lock(&nfsd_drc_lock); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1367 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1368 | spin_unlock(&nfsd_drc_lock); |
| 1369 | } |
| 1370 | |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1371 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 1372 | struct nfsd4_channel_attrs *battrs) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1373 | { |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1374 | int numslots = fattrs->maxreqs; |
| 1375 | int slotsize = slot_bytes(fattrs); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1376 | struct nfsd4_session *new; |
| 1377 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1378 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 1379 | 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] | 1380 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 1381 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1382 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1383 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1384 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1385 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1386 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1387 | for (i = 0; i < numslots; i++) { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1388 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1389 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1390 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1391 | } |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1392 | |
| 1393 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 1394 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 1395 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1396 | return new; |
| 1397 | out_free: |
| 1398 | while (i--) |
| 1399 | kfree(new->se_slots[i]); |
| 1400 | kfree(new); |
| 1401 | return NULL; |
| 1402 | } |
| 1403 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1404 | static void free_conn(struct nfsd4_conn *c) |
| 1405 | { |
| 1406 | svc_xprt_put(c->cn_xprt); |
| 1407 | kfree(c); |
| 1408 | } |
| 1409 | |
| 1410 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 1411 | { |
| 1412 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 1413 | struct nfs4_client *clp = c->cn_session->se_client; |
| 1414 | |
| 1415 | spin_lock(&clp->cl_lock); |
| 1416 | if (!list_empty(&c->cn_persession)) { |
| 1417 | list_del(&c->cn_persession); |
| 1418 | free_conn(c); |
| 1419 | } |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 1420 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 2e4b723 | 2013-03-08 09:30:43 -0500 | [diff] [blame] | 1421 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1422 | } |
| 1423 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1424 | 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] | 1425 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1426 | struct nfsd4_conn *conn; |
| 1427 | |
| 1428 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 1429 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1430 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1431 | svc_xprt_get(rqstp->rq_xprt); |
| 1432 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1433 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1434 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1435 | return conn; |
| 1436 | } |
| 1437 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1438 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1439 | { |
| 1440 | conn->cn_session = ses; |
| 1441 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1442 | } |
| 1443 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1444 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1445 | { |
| 1446 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1447 | |
| 1448 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1449 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1450 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1451 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1452 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1453 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1454 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1455 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1456 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1457 | } |
| 1458 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1459 | 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] | 1460 | { |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1461 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1462 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1463 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1464 | ret = nfsd4_register_conn(conn); |
| 1465 | if (ret) |
| 1466 | /* oops; xprt is already down: */ |
| 1467 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | 57a3714 | 2014-07-18 15:06:47 -0400 | [diff] [blame] | 1468 | /* We may have gained or lost a callback channel: */ |
| 1469 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1470 | } |
| 1471 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1472 | 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] | 1473 | { |
| 1474 | u32 dir = NFS4_CDFC4_FORE; |
| 1475 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1476 | if (cses->flags & SESSION4_BACK_CHAN) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1477 | dir |= NFS4_CDFC4_BACK; |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1478 | return alloc_conn(rqstp, dir); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1482 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1483 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1484 | struct nfs4_client *clp = s->se_client; |
| 1485 | struct nfsd4_conn *c; |
| 1486 | |
| 1487 | spin_lock(&clp->cl_lock); |
| 1488 | while (!list_empty(&s->se_conns)) { |
| 1489 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1490 | list_del_init(&c->cn_persession); |
| 1491 | spin_unlock(&clp->cl_lock); |
| 1492 | |
| 1493 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1494 | free_conn(c); |
| 1495 | |
| 1496 | spin_lock(&clp->cl_lock); |
| 1497 | } |
| 1498 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1499 | } |
| 1500 | |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1501 | static void __free_session(struct nfsd4_session *ses) |
| 1502 | { |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1503 | free_session_slots(ses); |
| 1504 | kfree(ses); |
| 1505 | } |
| 1506 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1507 | static void free_session(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 1508 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1509 | nfsd4_del_conns(ses); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1510 | nfsd4_put_drc_mem(&ses->se_fchannel); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1511 | __free_session(ses); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1512 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1513 | |
Fengguang Wu | 135ae82 | 2012-11-10 07:20:25 -0500 | [diff] [blame] | 1514 | 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] | 1515 | { |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1516 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1517 | 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] | 1518 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1519 | new->se_client = clp; |
| 1520 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1521 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1522 | INIT_LIST_HEAD(&new->se_conns); |
| 1523 | |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1524 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1525 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 1526 | new->se_cb_prog = cses->callback_prog; |
J. Bruce Fields | c6bb3ca | 2012-11-01 16:31:02 -0400 | [diff] [blame] | 1527 | new->se_cb_sec = cses->cb_sec; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1528 | atomic_set(&new->se_ref, 0); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1529 | idx = hash_sessionid(&new->se_sessionid); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1530 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1531 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1532 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1533 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1534 | |
Chuck Lever | b0d2e42 | 2014-08-22 15:10:59 -0400 | [diff] [blame] | 1535 | { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1536 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1537 | /* |
| 1538 | * This is a little silly; with sessions there's no real |
| 1539 | * use for the callback address. Use the peer address |
| 1540 | * as a reasonable default for now, but consider fixing |
| 1541 | * the rpc client not to require an address in the |
| 1542 | * future: |
| 1543 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1544 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1545 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1546 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1547 | } |
| 1548 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1549 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1550 | static struct nfsd4_session * |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1551 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1552 | { |
| 1553 | struct nfsd4_session *elem; |
| 1554 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1555 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1556 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1557 | lockdep_assert_held(&nn->client_lock); |
| 1558 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1559 | dump_sessionid(__func__, sessionid); |
| 1560 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1561 | /* Search in the appropriate list */ |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1562 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1563 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1564 | NFS4_MAX_SESSIONID_LEN)) { |
| 1565 | return elem; |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | dprintk("%s: session not found\n", __func__); |
| 1570 | return NULL; |
| 1571 | } |
| 1572 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1573 | static struct nfsd4_session * |
| 1574 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, |
| 1575 | __be32 *ret) |
| 1576 | { |
| 1577 | struct nfsd4_session *session; |
| 1578 | __be32 status = nfserr_badsession; |
| 1579 | |
| 1580 | session = __find_in_sessionid_hashtbl(sessionid, net); |
| 1581 | if (!session) |
| 1582 | goto out; |
| 1583 | status = nfsd4_get_session_locked(session); |
| 1584 | if (status) |
| 1585 | session = NULL; |
| 1586 | out: |
| 1587 | *ret = status; |
| 1588 | return session; |
| 1589 | } |
| 1590 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1591 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1592 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1593 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1594 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1595 | struct nfs4_client *clp = ses->se_client; |
| 1596 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1597 | |
| 1598 | lockdep_assert_held(&nn->client_lock); |
| 1599 | |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1600 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1601 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1602 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1603 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1604 | } |
| 1605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1607 | static int |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1608 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | { |
J. Bruce Fields | bbc7f33 | 2015-01-20 11:51:26 -0500 | [diff] [blame] | 1610 | /* |
| 1611 | * We're assuming the clid was not given out from a boot |
| 1612 | * precisely 2^32 (about 136 years) before this one. That seems |
| 1613 | * a safe assumption: |
| 1614 | */ |
| 1615 | if (clid->cl_boot == (u32)nn->boot_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | return 0; |
Andy Adamson | 60adfc5 | 2009-04-03 08:28:50 +0300 | [diff] [blame] | 1617 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1618 | clid->cl_boot, clid->cl_id, nn->boot_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | return 1; |
| 1620 | } |
| 1621 | |
| 1622 | /* |
| 1623 | * XXX Should we use a slab cache ? |
| 1624 | * This type of memory management is somewhat inefficient, but we use it |
| 1625 | * anyway since SETCLIENTID is not a common operation. |
| 1626 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1627 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | { |
| 1629 | struct nfs4_client *clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1630 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1632 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
| 1633 | if (clp == NULL) |
| 1634 | return NULL; |
Thomas Meyer | 67114fe | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 1635 | clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1636 | if (clp->cl_name.data == NULL) |
| 1637 | goto err_no_name; |
| 1638 | clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) * |
| 1639 | OWNER_HASH_SIZE, GFP_KERNEL); |
| 1640 | if (!clp->cl_ownerstr_hashtbl) |
| 1641 | goto err_no_hashtbl; |
| 1642 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 1643 | INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1644 | clp->cl_name.len = name.len; |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1645 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1646 | idr_init(&clp->cl_stateids); |
| 1647 | atomic_set(&clp->cl_refcount, 0); |
| 1648 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 1649 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1650 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1651 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 1652 | INIT_LIST_HEAD(&clp->cl_lru); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1653 | INIT_LIST_HEAD(&clp->cl_revoked); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 1654 | #ifdef CONFIG_NFSD_PNFS |
| 1655 | INIT_LIST_HEAD(&clp->cl_lo_states); |
| 1656 | #endif |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1657 | spin_lock_init(&clp->cl_lock); |
| 1658 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | return clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1660 | err_no_hashtbl: |
| 1661 | kfree(clp->cl_name.data); |
| 1662 | err_no_name: |
| 1663 | kfree(clp); |
| 1664 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
Trond Myklebust | 4dd86e15 | 2014-04-18 14:43:58 -0400 | [diff] [blame] | 1667 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | free_client(struct nfs4_client *clp) |
| 1669 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1670 | while (!list_empty(&clp->cl_sessions)) { |
| 1671 | struct nfsd4_session *ses; |
| 1672 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 1673 | se_perclnt); |
| 1674 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1675 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 1676 | free_session(ses); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1677 | } |
Trond Myklebust | 4cb57e3 | 2014-04-18 14:43:57 -0400 | [diff] [blame] | 1678 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1679 | free_svc_cred(&clp->cl_cred); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1680 | kfree(clp->cl_ownerstr_hashtbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | kfree(clp->cl_name.data); |
majianpeng | 2d32b29 | 2013-01-29 13:16:06 +0800 | [diff] [blame] | 1682 | idr_destroy(&clp->cl_stateids); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | kfree(clp); |
| 1684 | } |
| 1685 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1686 | /* must be called under the client_lock */ |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1687 | static void |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1688 | unhash_client_locked(struct nfs4_client *clp) |
| 1689 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1690 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1691 | struct nfsd4_session *ses; |
| 1692 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1693 | lockdep_assert_held(&nn->client_lock); |
| 1694 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1695 | /* Mark the client as expired! */ |
| 1696 | clp->cl_time = 0; |
| 1697 | /* Make it invisible */ |
| 1698 | if (!list_empty(&clp->cl_idhash)) { |
| 1699 | list_del_init(&clp->cl_idhash); |
| 1700 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
| 1701 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
| 1702 | else |
| 1703 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
| 1704 | } |
| 1705 | list_del_init(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1706 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1707 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 1708 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1709 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1710 | } |
| 1711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | static void |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1713 | unhash_client(struct nfs4_client *clp) |
| 1714 | { |
| 1715 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1716 | |
| 1717 | spin_lock(&nn->client_lock); |
| 1718 | unhash_client_locked(clp); |
| 1719 | spin_unlock(&nn->client_lock); |
| 1720 | } |
| 1721 | |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame] | 1722 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
| 1723 | { |
| 1724 | if (atomic_read(&clp->cl_refcount)) |
| 1725 | return nfserr_jukebox; |
| 1726 | unhash_client_locked(clp); |
| 1727 | return nfs_ok; |
| 1728 | } |
| 1729 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1730 | static void |
| 1731 | __destroy_client(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1733 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | struct list_head reaplist; |
| 1736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1738 | spin_lock(&state_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1739 | while (!list_empty(&clp->cl_delegations)) { |
| 1740 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1741 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1742 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1744 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | while (!list_empty(&reaplist)) { |
| 1746 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1747 | list_del_init(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 1748 | put_clnt_odstate(dp->dl_clnt_odstate); |
Jeff Layton | afbda40 | 2014-08-09 10:22:41 -0400 | [diff] [blame] | 1749 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1750 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | } |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1752 | while (!list_empty(&clp->cl_revoked)) { |
Andrew Elble | c876486 | 2015-02-25 17:46:27 -0500 | [diff] [blame] | 1753 | 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] | 1754 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1755 | nfs4_put_stid(&dp->dl_stid); |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 1756 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1757 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1758 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 1759 | nfs4_get_stateowner(&oo->oo_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1760 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | } |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 1762 | nfsd4_return_all_client_layouts(clp); |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 1763 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 1764 | if (clp->cl_cb_conn.cb_xprt) |
| 1765 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 1766 | free_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | } |
| 1768 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1769 | static void |
| 1770 | destroy_client(struct nfs4_client *clp) |
| 1771 | { |
| 1772 | unhash_client(clp); |
| 1773 | __destroy_client(clp); |
| 1774 | } |
| 1775 | |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1776 | static void expire_client(struct nfs4_client *clp) |
| 1777 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1778 | unhash_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1779 | nfsd4_client_record_remove(clp); |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 1780 | __destroy_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 1781 | } |
| 1782 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1783 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 1784 | { |
| 1785 | memcpy(target->cl_verifier.data, source->data, |
| 1786 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | } |
| 1788 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1789 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 1790 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 1792 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 1793 | } |
| 1794 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1795 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1796 | { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1797 | if (source->cr_principal) { |
| 1798 | target->cr_principal = |
| 1799 | kstrdup(source->cr_principal, GFP_KERNEL); |
| 1800 | if (target->cr_principal == NULL) |
| 1801 | return -ENOMEM; |
| 1802 | } else |
| 1803 | target->cr_principal = NULL; |
J. Bruce Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 1804 | target->cr_flavor = source->cr_flavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | target->cr_uid = source->cr_uid; |
| 1806 | target->cr_gid = source->cr_gid; |
| 1807 | target->cr_group_info = source->cr_group_info; |
| 1808 | get_group_info(target->cr_group_info); |
J. Bruce Fields | 0dc1531 | 2013-05-14 16:07:13 -0400 | [diff] [blame] | 1809 | target->cr_gss_mech = source->cr_gss_mech; |
| 1810 | if (source->cr_gss_mech) |
| 1811 | gss_mech_get(source->cr_gss_mech); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1812 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 1815 | static int |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1816 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 1817 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 1818 | if (o1->len < o2->len) |
| 1819 | return -1; |
| 1820 | if (o1->len > o2->len) |
| 1821 | return 1; |
| 1822 | return memcmp(o1->data, o2->data, o1->len); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1823 | } |
| 1824 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1825 | static int same_name(const char *n1, const char *n2) |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1826 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1827 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1831 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 1832 | { |
| 1833 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1837 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 1838 | { |
| 1839 | 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] | 1840 | } |
| 1841 | |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1842 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 1843 | { |
| 1844 | int i; |
| 1845 | |
| 1846 | if (g1->ngroups != g2->ngroups) |
| 1847 | return false; |
| 1848 | for (i=0; i<g1->ngroups; i++) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1849 | if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i))) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1850 | return false; |
| 1851 | return true; |
| 1852 | } |
| 1853 | |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1854 | /* |
| 1855 | * RFC 3530 language requires clid_inuse be returned when the |
| 1856 | * "principal" associated with a requests differs from that previously |
| 1857 | * used. We use uid, gid's, and gss principal string as our best |
| 1858 | * approximation. We also don't want to allow non-gss use of a client |
| 1859 | * established using gss: in theory cr_principal should catch that |
| 1860 | * change, but in practice cr_principal can be null even in the gss case |
| 1861 | * since gssd doesn't always pass down a principal string. |
| 1862 | */ |
| 1863 | static bool is_gss_cred(struct svc_cred *cr) |
| 1864 | { |
| 1865 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 1866 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 1867 | } |
| 1868 | |
| 1869 | |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1870 | static bool |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1871 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 1872 | { |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 1873 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1874 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 1875 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 1876 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 1877 | return false; |
| 1878 | if (cr1->cr_principal == cr2->cr_principal) |
| 1879 | return true; |
| 1880 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 1881 | return false; |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 1882 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1885 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 1886 | { |
| 1887 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1888 | u32 service; |
| 1889 | |
J. Bruce Fields | c472059 | 2013-08-07 11:41:49 -0400 | [diff] [blame] | 1890 | if (!cr->cr_gss_mech) |
| 1891 | return false; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 1892 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 1893 | return service == RPC_GSS_SVC_INTEGRITY || |
| 1894 | service == RPC_GSS_SVC_PRIVACY; |
| 1895 | } |
| 1896 | |
| 1897 | static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) |
| 1898 | { |
| 1899 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1900 | |
| 1901 | if (!cl->cl_mach_cred) |
| 1902 | return true; |
| 1903 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 1904 | return false; |
| 1905 | if (!svc_rqst_integrity_protected(rqstp)) |
| 1906 | return false; |
| 1907 | if (!cr->cr_principal) |
| 1908 | return false; |
| 1909 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 1910 | } |
| 1911 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 1912 | 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] | 1913 | { |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1914 | __be32 verf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | |
Jeff Layton | f419992 | 2014-06-17 07:44:11 -0400 | [diff] [blame] | 1916 | /* |
| 1917 | * This is opaque to client, so no need to byte-swap. Use |
| 1918 | * __force to keep sparse happy |
| 1919 | */ |
| 1920 | verf[0] = (__force __be32)get_seconds(); |
Kinglong Mee | 19311aa8 | 2015-07-18 07:33:31 +0800 | [diff] [blame] | 1921 | verf[1] = (__force __be32)nn->clverifier_counter++; |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 1922 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 1925 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
| 1926 | { |
| 1927 | clp->cl_clientid.cl_boot = nn->boot_time; |
| 1928 | clp->cl_clientid.cl_id = nn->clientid_counter++; |
| 1929 | gen_confirm(clp, nn); |
| 1930 | } |
| 1931 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1932 | static struct nfs4_stid * |
| 1933 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1934 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 1935 | struct nfs4_stid *ret; |
| 1936 | |
| 1937 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 1938 | if (!ret || !ret->sc_type) |
| 1939 | return NULL; |
| 1940 | return ret; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 1941 | } |
| 1942 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1943 | static struct nfs4_stid * |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1944 | 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] | 1945 | { |
| 1946 | struct nfs4_stid *s; |
| 1947 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1948 | spin_lock(&cl->cl_lock); |
| 1949 | s = find_stateid_locked(cl, t); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 1950 | if (s != NULL) { |
| 1951 | if (typemask & s->sc_type) |
| 1952 | atomic_inc(&s->sc_count); |
| 1953 | else |
| 1954 | s = NULL; |
| 1955 | } |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1956 | spin_unlock(&cl->cl_lock); |
| 1957 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1958 | } |
| 1959 | |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 1960 | static struct nfs4_client *create_client(struct xdr_netobj name, |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1961 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 1962 | { |
| 1963 | struct nfs4_client *clp; |
| 1964 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1965 | int ret; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1966 | struct net *net = SVC_NET(rqstp); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1967 | |
| 1968 | clp = alloc_client(name); |
| 1969 | if (clp == NULL) |
| 1970 | return NULL; |
| 1971 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1972 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 1973 | if (ret) { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1974 | free_client(clp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 1975 | return NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1976 | } |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 1977 | 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] | 1978 | clp->cl_time = get_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1979 | clear_bit(0, &clp->cl_cb_slot_busy); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1980 | copy_verf(clp, verf); |
| 1981 | rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1982 | clp->cl_cb_session = NULL; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 1983 | clp->net = net; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1984 | return clp; |
| 1985 | } |
| 1986 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1987 | static void |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 1988 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 1989 | { |
| 1990 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 1991 | struct nfs4_client *clp; |
| 1992 | |
| 1993 | while (*new) { |
| 1994 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 1995 | parent = *new; |
| 1996 | |
| 1997 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 1998 | new = &((*new)->rb_left); |
| 1999 | else |
| 2000 | new = &((*new)->rb_right); |
| 2001 | } |
| 2002 | |
| 2003 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 2004 | rb_insert_color(&new_clp->cl_namenode, root); |
| 2005 | } |
| 2006 | |
| 2007 | static struct nfs4_client * |
| 2008 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 2009 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2010 | int cmp; |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2011 | struct rb_node *node = root->rb_node; |
| 2012 | struct nfs4_client *clp; |
| 2013 | |
| 2014 | while (node) { |
| 2015 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 2016 | cmp = compare_blob(&clp->cl_name, name); |
| 2017 | if (cmp > 0) |
| 2018 | node = node->rb_left; |
| 2019 | else if (cmp < 0) |
| 2020 | node = node->rb_right; |
| 2021 | else |
| 2022 | return clp; |
| 2023 | } |
| 2024 | return NULL; |
| 2025 | } |
| 2026 | |
| 2027 | static void |
| 2028 | add_to_unconfirmed(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | { |
| 2030 | unsigned int idhashval; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2031 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2033 | lockdep_assert_held(&nn->client_lock); |
| 2034 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2035 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2036 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2038 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2039 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | } |
| 2041 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2042 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | move_to_confirmed(struct nfs4_client *clp) |
| 2044 | { |
| 2045 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2046 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2048 | lockdep_assert_held(&nn->client_lock); |
| 2049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2051 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2052 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2053 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2054 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2055 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2059 | 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] | 2060 | { |
| 2061 | struct nfs4_client *clp; |
| 2062 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 2063 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2064 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2065 | if (same_clid(&clp->cl_clientid, clid)) { |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 2066 | if ((bool)clp->cl_minorversion != sessions) |
| 2067 | return NULL; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2068 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | return clp; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2070 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | } |
| 2072 | return NULL; |
| 2073 | } |
| 2074 | |
| 2075 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2076 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 2077 | { |
| 2078 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 2079 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2080 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2081 | return find_client_in_id_table(tbl, clid, sessions); |
| 2082 | } |
| 2083 | |
| 2084 | static struct nfs4_client * |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2085 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | { |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2087 | struct list_head *tbl = nn->unconf_id_hashtbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2089 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2090 | return find_client_in_id_table(tbl, clid, sessions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2093 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2094 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2095 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2096 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2097 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2098 | static struct nfs4_client * |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2099 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2100 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2101 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2102 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | static struct nfs4_client * |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2106 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2107 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2108 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2109 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2112 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2113 | 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] | 2114 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2115 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2116 | struct sockaddr *sa = svc_addr(rqstp); |
| 2117 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2118 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2120 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 2121 | if (se->se_callback_netid_len == 3 && |
| 2122 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 2123 | expected_family = AF_INET; |
| 2124 | else if (se->se_callback_netid_len == 4 && |
| 2125 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 2126 | expected_family = AF_INET6; |
| 2127 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | goto out_err; |
| 2129 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2130 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2131 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2132 | (struct sockaddr *)&conn->cb_addr, |
| 2133 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2134 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2135 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2137 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2138 | if (conn->cb_addr.ss_family == AF_INET6) |
| 2139 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 2140 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2141 | conn->cb_prog = se->se_callback_prog; |
| 2142 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 2143 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | return; |
| 2145 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2146 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 2147 | conn->cb_addrlen = 0; |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 2148 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | "will not receive delegations\n", |
| 2150 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 2151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | return; |
| 2153 | } |
| 2154 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2155 | /* |
J. Bruce Fields | 067e1ac | 2014-03-21 17:26:44 -0400 | [diff] [blame] | 2156 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2157 | */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2158 | static void |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2159 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 2160 | { |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2161 | struct xdr_buf *buf = resp->xdr.buf; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2162 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 2163 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2164 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2165 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2166 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2167 | slot->sl_opcnt = resp->opcnt; |
| 2168 | slot->sl_status = resp->cstate.status; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2169 | |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2170 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2171 | if (nfsd4_not_cached(resp)) { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2172 | slot->sl_datalen = 0; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2173 | return; |
| 2174 | } |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2175 | base = resp->cstate.data_offset; |
| 2176 | slot->sl_datalen = buf->len - base; |
| 2177 | if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen)) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2178 | WARN("%s: sessions DRC could not cache compound\n", __func__); |
| 2179 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2183 | * Encode the replay sequence operation from the slot values. |
| 2184 | * If cachethis is FALSE encode the uncached rep error on the next |
| 2185 | * operation which sets resp->p and increments resp->opcnt for |
| 2186 | * nfs4svc_encode_compoundres. |
| 2187 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2188 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2189 | static __be32 |
| 2190 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 2191 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2192 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2193 | struct nfsd4_op *op; |
| 2194 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2195 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2196 | /* Encode the replayed sequence operation */ |
| 2197 | op = &args->ops[resp->opcnt - 1]; |
| 2198 | nfsd4_encode_operation(resp, op); |
| 2199 | |
| 2200 | /* Return nfserr_retry_uncached_rep in next operation. */ |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2201 | if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2202 | op = &args->ops[resp->opcnt++]; |
| 2203 | op->status = nfserr_retry_uncached_rep; |
| 2204 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2205 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2206 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2210 | * The sequence operation is not cached because we can use the slot and |
| 2211 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2212 | */ |
J. Bruce Fields | 3ca2eb9 | 2014-03-21 12:04:21 -0400 | [diff] [blame] | 2213 | static __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2214 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 2215 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2216 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2217 | struct nfsd4_slot *slot = resp->cstate.slot; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2218 | struct xdr_stream *xdr = &resp->xdr; |
| 2219 | __be32 *p; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2220 | __be32 status; |
| 2221 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2222 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2223 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2224 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
J. Bruce Fields | 0da7b19 | 2014-03-21 11:43:34 -0400 | [diff] [blame] | 2225 | if (status) |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2226 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2227 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2228 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 2229 | if (!p) { |
| 2230 | WARN_ON_ONCE(1); |
| 2231 | return nfserr_serverfault; |
| 2232 | } |
| 2233 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 2234 | xdr_commit_encode(xdr); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2235 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2236 | resp->opcnt = slot->sl_opcnt; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2237 | return slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2238 | } |
| 2239 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2240 | /* |
| 2241 | * Set the exchange_id flags returned by the server. |
| 2242 | */ |
| 2243 | static void |
| 2244 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 2245 | { |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2246 | #ifdef CONFIG_NFSD_PNFS |
| 2247 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; |
| 2248 | #else |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2249 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2250 | #endif |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2251 | |
| 2252 | /* Referrals are supported, Migration is not. */ |
| 2253 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 2254 | |
| 2255 | /* set the wire flags to return to client. */ |
| 2256 | clid->flags = new->cl_exchange_flags; |
| 2257 | } |
| 2258 | |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2259 | static bool client_has_state(struct nfs4_client *clp) |
| 2260 | { |
| 2261 | /* |
| 2262 | * Note clp->cl_openowners check isn't quite right: there's no |
| 2263 | * need to count owners without stateid's. |
| 2264 | * |
| 2265 | * Also note we should probably be using this in 4.0 case too. |
| 2266 | */ |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 2267 | return !list_empty(&clp->cl_openowners) |
Kinglong Mee | 47e970b | 2015-07-21 13:09:17 +0800 | [diff] [blame] | 2268 | #ifdef CONFIG_NFSD_PNFS |
| 2269 | || !list_empty(&clp->cl_lo_states) |
| 2270 | #endif |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 2271 | || !list_empty(&clp->cl_delegations) |
| 2272 | || !list_empty(&clp->cl_sessions); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2273 | } |
| 2274 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2275 | __be32 |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2276 | nfsd4_exchange_id(struct svc_rqst *rqstp, |
| 2277 | struct nfsd4_compound_state *cstate, |
| 2278 | struct nfsd4_exchange_id *exid) |
| 2279 | { |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2280 | struct nfs4_client *conf, *new; |
| 2281 | struct nfs4_client *unconf = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2282 | __be32 status; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2283 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2284 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2285 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 2286 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2287 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2288 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2289 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2290 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2291 | "ip_addr=%s flags %x, spa_how %d\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2292 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2293 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2294 | |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 2295 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2296 | return nfserr_inval; |
| 2297 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2298 | switch (exid->spa_how) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2299 | case SP4_MACH_CRED: |
| 2300 | if (!svc_rqst_integrity_protected(rqstp)) |
| 2301 | return nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2302 | case SP4_NONE: |
| 2303 | break; |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 2304 | default: /* checked by xdr code */ |
| 2305 | WARN_ON_ONCE(1); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2306 | case SP4_SSV: |
J. Bruce Fields | dd30333 | 2013-04-12 18:10:56 -0400 | [diff] [blame] | 2307 | return nfserr_encr_alg_unsupp; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2308 | } |
| 2309 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2310 | new = create_client(exid->clname, rqstp, &verf); |
| 2311 | if (new == NULL) |
| 2312 | return nfserr_jukebox; |
| 2313 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2314 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2315 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2316 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2317 | if (conf) { |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 2318 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 2319 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 2320 | |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2321 | if (update) { |
| 2322 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2323 | status = nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2324 | goto out; |
| 2325 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2326 | if (!mach_creds_match(conf, rqstp)) { |
| 2327 | status = nfserr_wrong_cred; |
| 2328 | goto out; |
| 2329 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2330 | if (!creds_match) { /* case 9 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2331 | status = nfserr_perm; |
| 2332 | goto out; |
| 2333 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2334 | if (!verfs_match) { /* case 8 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2335 | status = nfserr_not_same; |
| 2336 | goto out; |
| 2337 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2338 | /* case 6 */ |
| 2339 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2340 | goto out_copy; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2341 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2342 | if (!creds_match) { /* case 3 */ |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2343 | if (client_has_state(conf)) { |
| 2344 | status = nfserr_clid_inuse; |
| 2345 | goto out; |
| 2346 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2347 | goto out_new; |
| 2348 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2349 | if (verfs_match) { /* case 2 */ |
J. Bruce Fields | 0f1ba0e | 2012-05-25 21:24:40 -0400 | [diff] [blame] | 2350 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2351 | goto out_copy; |
| 2352 | } |
| 2353 | /* case 5, client reboot */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2354 | conf = NULL; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 2355 | goto out_new; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 2356 | } |
| 2357 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2358 | if (update) { /* case 7 */ |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 2359 | status = nfserr_noent; |
| 2360 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2361 | } |
| 2362 | |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2363 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2364 | if (unconf) /* case 4, possible retry or client restart */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2365 | unhash_client_locked(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2366 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 2367 | /* case 1 (normal case) */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2368 | out_new: |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 2369 | if (conf) { |
| 2370 | status = mark_client_expired_locked(conf); |
| 2371 | if (status) |
| 2372 | goto out; |
| 2373 | } |
J. Bruce Fields | 4f540e2 | 2013-05-07 11:57:52 -0400 | [diff] [blame] | 2374 | new->cl_minorversion = cstate->minorversion; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2375 | new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2376 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2377 | gen_clid(new, nn); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2378 | add_to_unconfirmed(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2379 | swap(new, conf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2380 | out_copy: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2381 | exid->clientid.cl_boot = conf->cl_clientid.cl_boot; |
| 2382 | exid->clientid.cl_id = conf->cl_clientid.cl_id; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2383 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2384 | exid->seqid = conf->cl_cs_slot.sl_seqid + 1; |
| 2385 | nfsd4_set_ex_flags(conf, exid); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2386 | |
| 2387 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2388 | conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2389 | status = nfs_ok; |
| 2390 | |
| 2391 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2392 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 2393 | if (new) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2394 | expire_client(new); |
| 2395 | if (unconf) |
| 2396 | expire_client(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2397 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2398 | } |
| 2399 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2400 | static __be32 |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2401 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2402 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2403 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 2404 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2405 | |
| 2406 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2407 | if (slot_inuse) { |
| 2408 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2409 | return nfserr_jukebox; |
| 2410 | else |
| 2411 | return nfserr_seq_misordered; |
| 2412 | } |
J. Bruce Fields | f6d8248 | 2012-02-13 16:13:41 -0500 | [diff] [blame] | 2413 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2414 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2415 | return nfs_ok; |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 2416 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2417 | return nfserr_replay_cache; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2418 | return nfserr_seq_misordered; |
| 2419 | } |
| 2420 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2421 | /* |
| 2422 | * Cache the create session result into the create session single DRC |
| 2423 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 2424 | * Do this for solo or embedded create session operations. |
| 2425 | */ |
| 2426 | static void |
| 2427 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2428 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2429 | { |
| 2430 | slot->sl_status = nfserr; |
| 2431 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 2432 | } |
| 2433 | |
| 2434 | static __be32 |
| 2435 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 2436 | struct nfsd4_clid_slot *slot) |
| 2437 | { |
| 2438 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 2439 | return slot->sl_status; |
| 2440 | } |
| 2441 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2442 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 2443 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 2444 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2445 | 3 + /* version, opcount, opcode */ \ |
| 2446 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2447 | /* seqid, slotID, slotID, cache */ \ |
| 2448 | 4 ) * sizeof(__be32)) |
| 2449 | |
| 2450 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 2451 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 2452 | 1 + /* status */ \ |
| 2453 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2454 | 3 + /* opcount, opcode, opstatus*/ \ |
| 2455 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2456 | /* seqid, slotID, slotID, slotID, status */ \ |
| 2457 | 5 ) * sizeof(__be32)) |
| 2458 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2459 | 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] | 2460 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2461 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 2462 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2463 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 2464 | return nfserr_toosmall; |
| 2465 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 2466 | return nfserr_toosmall; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2467 | ca->headerpadsz = 0; |
| 2468 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 2469 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 2470 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 2471 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 2472 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 2473 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 2474 | /* |
| 2475 | * Note decreasing slot size below client's request may make it |
| 2476 | * difficult for client to function correctly, whereas |
| 2477 | * decreasing the number of slots will (just?) affect |
| 2478 | * performance. When short on memory we therefore prefer to |
| 2479 | * decrease number of slots instead of their size. Clients that |
| 2480 | * request larger slots than they need will get poor results: |
| 2481 | */ |
| 2482 | ca->maxreqs = nfsd4_get_drc_mem(ca); |
| 2483 | if (!ca->maxreqs) |
| 2484 | return nfserr_jukebox; |
| 2485 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2486 | return nfs_ok; |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 2487 | } |
| 2488 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2489 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
| 2490 | RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32)) |
| 2491 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
| 2492 | RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32)) |
| 2493 | |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2494 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 2495 | { |
| 2496 | ca->headerpadsz = 0; |
| 2497 | |
| 2498 | /* |
| 2499 | * These RPC_MAX_HEADER macros are overkill, especially since we |
| 2500 | * don't even do gss on the backchannel yet. But this is still |
| 2501 | * less than 1k. Tighten up this estimate in the unlikely event |
| 2502 | * it turns out to be a problem for some client: |
| 2503 | */ |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2504 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2505 | return nfserr_toosmall; |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 2506 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2507 | return nfserr_toosmall; |
| 2508 | ca->maxresp_cached = 0; |
| 2509 | if (ca->maxops < 2) |
| 2510 | return nfserr_toosmall; |
| 2511 | |
| 2512 | return nfs_ok; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2513 | } |
| 2514 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2515 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 2516 | { |
| 2517 | switch (cbs->flavor) { |
| 2518 | case RPC_AUTH_NULL: |
| 2519 | case RPC_AUTH_UNIX: |
| 2520 | return nfs_ok; |
| 2521 | default: |
| 2522 | /* |
| 2523 | * GSS case: the spec doesn't allow us to return this |
| 2524 | * error. But it also doesn't allow us not to support |
| 2525 | * GSS. |
| 2526 | * I'd rather this fail hard than return some error the |
| 2527 | * client might think it can already handle: |
| 2528 | */ |
| 2529 | return nfserr_encr_alg_unsupp; |
| 2530 | } |
| 2531 | } |
| 2532 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2533 | __be32 |
| 2534 | nfsd4_create_session(struct svc_rqst *rqstp, |
| 2535 | struct nfsd4_compound_state *cstate, |
| 2536 | struct nfsd4_create_session *cr_ses) |
| 2537 | { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2538 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2539 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2540 | struct nfs4_client *old = NULL; |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2541 | struct nfsd4_session *new; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2542 | struct nfsd4_conn *conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2543 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2544 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2545 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2546 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2547 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 2548 | return nfserr_inval; |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2549 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 2550 | if (status) |
| 2551 | return status; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2552 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 2553 | if (status) |
| 2554 | return status; |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 2555 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 2556 | if (status) |
Kinglong Mee | f403e45 | 2013-12-23 17:31:21 +0800 | [diff] [blame] | 2557 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2558 | status = nfserr_jukebox; |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 2559 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2560 | if (!new) |
| 2561 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2562 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 2563 | if (!conn) |
| 2564 | goto out_free_session; |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 2565 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2566 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2567 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2568 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2569 | WARN_ON_ONCE(conf && unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2570 | |
| 2571 | if (conf) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2572 | status = nfserr_wrong_cred; |
| 2573 | if (!mach_creds_match(conf, rqstp)) |
| 2574 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2575 | cs_slot = &conf->cl_cs_slot; |
| 2576 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Kinglong Mee | f5e22bb | 2015-07-13 17:32:34 +0800 | [diff] [blame] | 2577 | if (status) { |
| 2578 | if (status == nfserr_replay_cache) |
| 2579 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2580 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2581 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2582 | } else if (unconf) { |
| 2583 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2584 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2585 | status = nfserr_clid_inuse; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2586 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2587 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2588 | status = nfserr_wrong_cred; |
| 2589 | if (!mach_creds_match(unconf, rqstp)) |
| 2590 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2591 | cs_slot = &unconf->cl_cs_slot; |
| 2592 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 2593 | if (status) { |
| 2594 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2595 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2596 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2597 | } |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2598 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2599 | if (old) { |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2600 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 2601 | if (status) { |
| 2602 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2603 | goto out_free_conn; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 2604 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2605 | } |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 2606 | move_to_confirmed(unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2607 | conf = unconf; |
| 2608 | } else { |
| 2609 | status = nfserr_stale_clientid; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2610 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2611 | } |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2612 | status = nfs_ok; |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 2613 | /* |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 2614 | * We do not support RDMA or persistent sessions |
| 2615 | */ |
| 2616 | cr_ses->flags &= ~SESSION4_PERSIST; |
| 2617 | cr_ses->flags &= ~SESSION4_RDMA; |
| 2618 | |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2619 | init_session(rqstp, new, conf, cr_ses); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2620 | nfsd4_get_session_locked(new); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2621 | |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 2622 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2623 | NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 2624 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2625 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2626 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2627 | /* cache solo and embedded create sessions under the client_lock */ |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 2628 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2629 | spin_unlock(&nn->client_lock); |
| 2630 | /* init connection and backchannel */ |
| 2631 | nfsd4_init_conn(rqstp, conn, new); |
| 2632 | nfsd4_put_session(new); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2633 | if (old) |
| 2634 | expire_client(old); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 2635 | return status; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2636 | out_free_conn: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2637 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2638 | free_conn(conn); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 2639 | if (old) |
| 2640 | expire_client(old); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 2641 | out_free_session: |
| 2642 | __free_session(new); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 2643 | out_release_drc_mem: |
| 2644 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
J. Bruce Fields | 1ca5079 | 2013-03-14 18:12:03 -0400 | [diff] [blame] | 2645 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2646 | } |
| 2647 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2648 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 2649 | { |
| 2650 | switch (*dir) { |
| 2651 | case NFS4_CDFC4_FORE: |
| 2652 | case NFS4_CDFC4_BACK: |
| 2653 | return nfs_ok; |
| 2654 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 2655 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 2656 | *dir = NFS4_CDFC4_BOTH; |
| 2657 | return nfs_ok; |
| 2658 | }; |
| 2659 | return nfserr_inval; |
| 2660 | } |
| 2661 | |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2662 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc) |
| 2663 | { |
| 2664 | struct nfsd4_session *session = cstate->session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2665 | 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] | 2666 | __be32 status; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2667 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 2668 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 2669 | if (status) |
| 2670 | return status; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2671 | spin_lock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2672 | session->se_cb_prog = bc->bc_cb_program; |
| 2673 | session->se_cb_sec = bc->bc_cb_sec; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2674 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 2675 | |
| 2676 | nfsd4_probe_callback(session->se_client); |
| 2677 | |
| 2678 | return nfs_ok; |
| 2679 | } |
| 2680 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2681 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 2682 | struct nfsd4_compound_state *cstate, |
| 2683 | struct nfsd4_bind_conn_to_session *bcts) |
| 2684 | { |
| 2685 | __be32 status; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2686 | struct nfsd4_conn *conn; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2687 | struct nfsd4_session *session; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2688 | struct net *net = SVC_NET(rqstp); |
| 2689 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2690 | |
| 2691 | if (!nfsd4_last_compound_op(rqstp)) |
| 2692 | return nfserr_not_only_op; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2693 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2694 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2695 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2696 | if (!session) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2697 | goto out_no_session; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2698 | status = nfserr_wrong_cred; |
| 2699 | if (!mach_creds_match(session->se_client, rqstp)) |
| 2700 | goto out; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2701 | status = nfsd4_map_bcts_dir(&bcts->dir); |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2702 | if (status) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2703 | goto out; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2704 | conn = alloc_conn(rqstp, bcts->dir); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2705 | status = nfserr_jukebox; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 2706 | if (!conn) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2707 | goto out; |
| 2708 | nfsd4_init_conn(rqstp, conn, session); |
| 2709 | status = nfs_ok; |
| 2710 | out: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2711 | nfsd4_put_session(session); |
| 2712 | out_no_session: |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 2713 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 2714 | } |
| 2715 | |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2716 | static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid) |
| 2717 | { |
| 2718 | if (!session) |
| 2719 | return 0; |
| 2720 | return !memcmp(sid, &session->se_sessionid, sizeof(*sid)); |
| 2721 | } |
| 2722 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2723 | __be32 |
| 2724 | nfsd4_destroy_session(struct svc_rqst *r, |
| 2725 | struct nfsd4_compound_state *cstate, |
| 2726 | struct nfsd4_destroy_session *sessionid) |
| 2727 | { |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2728 | struct nfsd4_session *ses; |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2729 | __be32 status; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2730 | int ref_held_by_me = 0; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2731 | struct net *net = SVC_NET(r); |
| 2732 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2733 | |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2734 | status = nfserr_not_only_op; |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 2735 | if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2736 | if (!nfsd4_last_compound_op(r)) |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2737 | goto out; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2738 | ref_held_by_me++; |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 2739 | } |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2740 | dump_sessionid(__func__, &sessionid->sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2741 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2742 | ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2743 | if (!ses) |
| 2744 | goto out_client_lock; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2745 | status = nfserr_wrong_cred; |
| 2746 | if (!mach_creds_match(ses->se_client, r)) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2747 | goto out_put_session; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2748 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2749 | if (status) |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2750 | goto out_put_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2751 | unhash_session(ses); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2752 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2753 | |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 2754 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 2755 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2756 | spin_lock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2757 | status = nfs_ok; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 2758 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2759 | nfsd4_put_session_locked(ses); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 2760 | out_client_lock: |
| 2761 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2762 | out: |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 2763 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2764 | } |
| 2765 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2766 | 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] | 2767 | { |
| 2768 | struct nfsd4_conn *c; |
| 2769 | |
| 2770 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2771 | if (c->cn_xprt == xpt) { |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2772 | return c; |
| 2773 | } |
| 2774 | } |
| 2775 | return NULL; |
| 2776 | } |
| 2777 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2778 | 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] | 2779 | { |
| 2780 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2781 | struct nfsd4_conn *c; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2782 | __be32 status = nfs_ok; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2783 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2784 | |
| 2785 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2786 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2787 | if (c) |
| 2788 | goto out_free; |
| 2789 | status = nfserr_conn_not_bound_to_session; |
| 2790 | if (clp->cl_mach_cred) |
| 2791 | goto out_free; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2792 | __nfsd4_hash_conn(new, ses); |
| 2793 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 2794 | ret = nfsd4_register_conn(new); |
| 2795 | if (ret) |
| 2796 | /* oops; xprt is already down: */ |
| 2797 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2798 | return nfs_ok; |
| 2799 | out_free: |
| 2800 | spin_unlock(&clp->cl_lock); |
| 2801 | free_conn(new); |
| 2802 | return status; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2803 | } |
| 2804 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2805 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 2806 | { |
| 2807 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 2808 | |
| 2809 | return args->opcnt > session->se_fchannel.maxops; |
| 2810 | } |
| 2811 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2812 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 2813 | struct nfsd4_session *session) |
| 2814 | { |
| 2815 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 2816 | |
| 2817 | return xb->len > session->se_fchannel.maxreq_sz; |
| 2818 | } |
| 2819 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2820 | __be32 |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2821 | nfsd4_sequence(struct svc_rqst *rqstp, |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2822 | struct nfsd4_compound_state *cstate, |
| 2823 | struct nfsd4_sequence *seq) |
| 2824 | { |
Andy Adamson | f9bb94c | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2825 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2826 | struct xdr_stream *xdr = &resp->xdr; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2827 | struct nfsd4_session *session; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2828 | struct nfs4_client *clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2829 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2830 | struct nfsd4_conn *conn; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2831 | __be32 status; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2832 | int buflen; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2833 | struct net *net = SVC_NET(rqstp); |
| 2834 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2835 | |
Andy Adamson | f9bb94c | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 2836 | if (resp->opcnt != 1) |
| 2837 | return nfserr_sequence_pos; |
| 2838 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2839 | /* |
| 2840 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 2841 | * below. |
| 2842 | */ |
| 2843 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 2844 | if (!conn) |
| 2845 | return nfserr_jukebox; |
| 2846 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2847 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2848 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2849 | if (!session) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2850 | goto out_no_session; |
| 2851 | clp = session->se_client; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2852 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2853 | status = nfserr_too_many_ops; |
| 2854 | if (nfsd4_session_too_many_ops(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2855 | goto out_put_session; |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 2856 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2857 | status = nfserr_req_too_big; |
| 2858 | if (nfsd4_request_too_big(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2859 | goto out_put_session; |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 2860 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2861 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 2862 | if (seq->slotid >= session->se_fchannel.maxreqs) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2863 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2864 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2865 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2866 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 2867 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 2868 | /* We do not negotiate the number of slots yet, so set the |
| 2869 | * maxslots to the session maxreqs which is used to encode |
| 2870 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 2871 | seq->maxslots = session->se_fchannel.maxreqs; |
| 2872 | |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2873 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 2874 | slot->sl_flags & NFSD4_SLOT_INUSE); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2875 | if (status == nfserr_replay_cache) { |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2876 | status = nfserr_seq_misordered; |
| 2877 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2878 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2879 | cstate->slot = slot; |
| 2880 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2881 | cstate->clp = clp; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2882 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2883 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2884 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 2885 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 2886 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2887 | } |
| 2888 | if (status) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2889 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2890 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2891 | status = nfsd4_sequence_check_conn(conn, session); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 2892 | conn = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2893 | if (status) |
| 2894 | goto out_put_session; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 2895 | |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2896 | buflen = (seq->cachethis) ? |
| 2897 | session->se_fchannel.maxresp_cached : |
| 2898 | session->se_fchannel.maxresp_sz; |
| 2899 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 2900 | nfserr_rep_too_big; |
J. Bruce Fields | a5cddc8 | 2014-05-12 18:10:58 -0400 | [diff] [blame] | 2901 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2902 | goto out_put_session; |
J. Bruce Fields | 32aaa62 | 2014-03-20 20:47:41 -0400 | [diff] [blame] | 2903 | svc_reserve(rqstp, buflen); |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 2904 | |
| 2905 | status = nfs_ok; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2906 | /* Success! bump slot seqid */ |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2907 | slot->sl_seqid = seq->seqid; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2908 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 2909 | if (seq->cachethis) |
| 2910 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 2911 | else |
| 2912 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2913 | |
| 2914 | cstate->slot = slot; |
| 2915 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2916 | cstate->clp = clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2917 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2918 | out: |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2919 | switch (clp->cl_cb_state) { |
| 2920 | case NFSD4_CB_DOWN: |
| 2921 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 2922 | break; |
| 2923 | case NFSD4_CB_FAULT: |
| 2924 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 2925 | break; |
| 2926 | default: |
| 2927 | seq->status_flags = 0; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 2928 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 2929 | if (!list_empty(&clp->cl_revoked)) |
| 2930 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2931 | out_no_session: |
Kinglong Mee | 3f42d2c | 2014-03-24 11:56:59 +0800 | [diff] [blame] | 2932 | if (conn) |
| 2933 | free_conn(conn); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 2934 | spin_unlock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 2935 | return status; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2936 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2937 | nfsd4_put_session_locked(session); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2938 | goto out_no_session; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2939 | } |
| 2940 | |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2941 | void |
| 2942 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 2943 | { |
| 2944 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 2945 | |
| 2946 | if (nfsd4_has_session(cs)) { |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2947 | if (cs->status != nfserr_replay_cache) { |
| 2948 | nfsd4_store_cache_entry(resp); |
| 2949 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 2950 | } |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 2951 | /* Drop session reference that was taken in nfsd4_sequence() */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2952 | nfsd4_put_session(cs->session); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 2953 | } else if (cs->clp) |
| 2954 | put_client_renew(cs->clp); |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2955 | } |
| 2956 | |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2957 | __be32 |
| 2958 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc) |
| 2959 | { |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2960 | struct nfs4_client *conf, *unconf; |
| 2961 | struct nfs4_client *clp = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 2962 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2963 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2964 | |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2965 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2966 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2967 | conf = find_confirmed_client(&dc->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 2968 | WARN_ON_ONCE(conf && unconf); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2969 | |
| 2970 | if (conf) { |
J. Bruce Fields | c0293b0 | 2013-03-14 18:20:01 -0400 | [diff] [blame] | 2971 | if (client_has_state(conf)) { |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2972 | status = nfserr_clientid_busy; |
| 2973 | goto out; |
| 2974 | } |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 2975 | status = mark_client_expired_locked(conf); |
| 2976 | if (status) |
| 2977 | goto out; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2978 | clp = conf; |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2979 | } else if (unconf) |
| 2980 | clp = unconf; |
| 2981 | else { |
| 2982 | status = nfserr_stale_clientid; |
| 2983 | goto out; |
| 2984 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2985 | if (!mach_creds_match(clp, rqstp)) { |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2986 | clp = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2987 | status = nfserr_wrong_cred; |
| 2988 | goto out; |
| 2989 | } |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2990 | unhash_client_locked(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2991 | out: |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2992 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 2993 | if (clp) |
| 2994 | expire_client(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 2995 | return status; |
| 2996 | } |
| 2997 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 2998 | __be32 |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2999 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc) |
| 3000 | { |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3001 | __be32 status = 0; |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3002 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3003 | if (rc->rca_one_fs) { |
| 3004 | if (!cstate->current_fh.fh_dentry) |
| 3005 | return nfserr_nofilehandle; |
| 3006 | /* |
| 3007 | * We don't take advantage of the rca_one_fs case. |
| 3008 | * That's OK, it's optional, we can safely ignore it. |
| 3009 | */ |
| 3010 | return nfs_ok; |
| 3011 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3012 | |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3013 | status = nfserr_complete_already; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 3014 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
| 3015 | &cstate->session->se_client->cl_flags)) |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3016 | goto out; |
| 3017 | |
| 3018 | status = nfserr_stale_clientid; |
| 3019 | if (is_client_expired(cstate->session->se_client)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3020 | /* |
| 3021 | * The following error isn't really legal. |
| 3022 | * But we only get here if the client just explicitly |
| 3023 | * destroyed the client. Surely it no longer cares what |
| 3024 | * error it gets back on an operation for the dead |
| 3025 | * client. |
| 3026 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3027 | goto out; |
| 3028 | |
| 3029 | status = nfs_ok; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 3030 | nfsd4_client_record_create(cstate->session->se_client); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3031 | out: |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3032 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3036 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3037 | struct nfsd4_setclientid *setclid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | { |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 3039 | struct xdr_netobj clname = setclid->se_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | nfs4_verifier clverifier = setclid->se_verf; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3041 | struct nfs4_client *conf, *new; |
| 3042 | struct nfs4_client *unconf = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3043 | __be32 status; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3044 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 3045 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3046 | new = create_client(clname, rqstp, &clverifier); |
| 3047 | if (new == NULL) |
| 3048 | return nfserr_jukebox; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3049 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3050 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3051 | conf = find_confirmed_client_by_name(&clname, nn); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 3052 | if (conf) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3053 | /* case 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 3055 | if (clp_used_exchangeid(conf)) |
| 3056 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 3057 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3058 | char addr_str[INET6_ADDRSTRLEN]; |
| 3059 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, |
| 3060 | sizeof(addr_str)); |
| 3061 | dprintk("NFSD: setclientid: string in use by client " |
| 3062 | "at %s\n", addr_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | goto out; |
| 3064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 3066 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 3067 | if (unconf) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3068 | unhash_client_locked(unconf); |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3069 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3070 | /* case 1: probable callback update */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | copy_clid(new, conf); |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3072 | gen_confirm(new, nn); |
| 3073 | } else /* case 4 (new client) or cases 2, 3 (client reboot): */ |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3074 | gen_clid(new, nn); |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 3075 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 3076 | gen_callback(new, setclid, rqstp); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 3077 | add_to_unconfirmed(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 3079 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 3080 | 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] | 3081 | new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | status = nfs_ok; |
| 3083 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3084 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3085 | if (new) |
| 3086 | free_client(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3087 | if (unconf) |
| 3088 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | return status; |
| 3090 | } |
| 3091 | |
| 3092 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3093 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3094 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
| 3095 | struct nfsd4_compound_state *cstate, |
| 3096 | struct nfsd4_setclientid_confirm *setclientid_confirm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | { |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3098 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3099 | struct nfs4_client *old = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 3101 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3102 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 3103 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 3105 | if (STALE_CLIENTID(clid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | return nfserr_stale_clientid; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3107 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3108 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3109 | conf = find_confirmed_client(clid, false, nn); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3110 | unconf = find_unconfirmed_client(clid, false, nn); |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3111 | /* |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3112 | * We try hard to give out unique clientid's, so if we get an |
| 3113 | * attempt to confirm the same clientid with a different cred, |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 3114 | * the client may be buggy; this should never happen. |
| 3115 | * |
| 3116 | * Nevertheless, RFC 7530 recommends INUSE for this case: |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3117 | */ |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 3118 | status = nfserr_clid_inuse; |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3119 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
| 3120 | goto out; |
| 3121 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
| 3122 | goto out; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3123 | /* cases below refer to rfc 3530 section 14.2.34: */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3124 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
| 3125 | if (conf && !unconf) /* case 2: probable retransmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | status = nfs_ok; |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3127 | else /* case 4: client hasn't noticed we rebooted yet? */ |
| 3128 | status = nfserr_stale_clientid; |
| 3129 | goto out; |
| 3130 | } |
| 3131 | status = nfs_ok; |
| 3132 | if (conf) { /* case 1: callback update */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3133 | old = unconf; |
| 3134 | unhash_client_locked(old); |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3135 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3136 | } else { /* case 3: normal case; new or rebooted client */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3137 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
| 3138 | if (old) { |
| 3139 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3140 | if (status) { |
| 3141 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3142 | goto out; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3143 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3144 | } |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3145 | move_to_confirmed(unconf); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3146 | conf = unconf; |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 3147 | } |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3148 | get_client_locked(conf); |
| 3149 | spin_unlock(&nn->client_lock); |
| 3150 | nfsd4_probe_callback(conf); |
| 3151 | spin_lock(&nn->client_lock); |
| 3152 | put_client_renew_locked(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | out: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3154 | spin_unlock(&nn->client_lock); |
| 3155 | if (old) |
| 3156 | expire_client(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | return status; |
| 3158 | } |
| 3159 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3160 | static struct nfs4_file *nfsd4_alloc_file(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | { |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3162 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 3163 | } |
| 3164 | |
| 3165 | /* OPEN Share state helper functions */ |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3166 | static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval, |
| 3167 | struct nfs4_file *fp) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3168 | { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3169 | lockdep_assert_held(&state_lock); |
| 3170 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3171 | atomic_set(&fp->fi_ref, 1); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3172 | spin_lock_init(&fp->fi_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3173 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 3174 | INIT_LIST_HEAD(&fp->fi_delegations); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3175 | INIT_LIST_HEAD(&fp->fi_clnt_odstate); |
Trond Myklebust | e2cf80d | 2014-07-23 16:17:38 -0400 | [diff] [blame] | 3176 | fh_copy_shallow(&fp->fi_fhandle, fh); |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 3177 | fp->fi_deleg_file = NULL; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3178 | fp->fi_had_conflict = false; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3179 | fp->fi_share_deny = 0; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3180 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 3181 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 3182 | #ifdef CONFIG_NFSD_PNFS |
| 3183 | INIT_LIST_HEAD(&fp->fi_lo_states); |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 3184 | atomic_set(&fp->fi_lo_recalls, 0); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 3185 | #endif |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3186 | hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3187 | } |
| 3188 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 3189 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3190 | nfsd4_free_slabs(void) |
| 3191 | { |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3192 | kmem_cache_destroy(odstate_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3193 | kmem_cache_destroy(openowner_slab); |
| 3194 | kmem_cache_destroy(lockowner_slab); |
| 3195 | kmem_cache_destroy(file_slab); |
| 3196 | kmem_cache_destroy(stateid_slab); |
| 3197 | kmem_cache_destroy(deleg_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | |
Bryan Schumaker | 7208339 | 2011-11-01 15:24:59 -0400 | [diff] [blame] | 3200 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | nfsd4_init_slabs(void) |
| 3202 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3203 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 3204 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 3205 | if (openowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3206 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3207 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
Yanchuan Nian | 3c40794 | 2012-10-24 14:44:19 +0800 | [diff] [blame] | 3208 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3209 | if (lockowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3210 | goto out_free_openowner_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3211 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3212 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3213 | if (file_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3214 | goto out_free_lockowner_slab; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 3215 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3216 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 3217 | if (stateid_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3218 | goto out_free_file_slab; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 3219 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3220 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 3221 | if (deleg_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3222 | goto out_free_stateid_slab; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3223 | odstate_slab = kmem_cache_create("nfsd4_odstate", |
| 3224 | sizeof(struct nfs4_clnt_odstate), 0, 0, NULL); |
| 3225 | if (odstate_slab == NULL) |
| 3226 | goto out_free_deleg_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3227 | return 0; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3228 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3229 | out_free_deleg_slab: |
| 3230 | kmem_cache_destroy(deleg_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 3231 | out_free_stateid_slab: |
| 3232 | kmem_cache_destroy(stateid_slab); |
| 3233 | out_free_file_slab: |
| 3234 | kmem_cache_destroy(file_slab); |
| 3235 | out_free_lockowner_slab: |
| 3236 | kmem_cache_destroy(lockowner_slab); |
| 3237 | out_free_openowner_slab: |
| 3238 | kmem_cache_destroy(openowner_slab); |
| 3239 | out: |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 3240 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 3241 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3242 | } |
| 3243 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3244 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 3245 | { |
| 3246 | rp->rp_status = nfserr_serverfault; |
| 3247 | rp->rp_buflen = 0; |
| 3248 | rp->rp_buf = rp->rp_ibuf; |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 3249 | mutex_init(&rp->rp_mutex); |
| 3250 | } |
| 3251 | |
| 3252 | static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, |
| 3253 | struct nfs4_stateowner *so) |
| 3254 | { |
| 3255 | if (!nfsd4_has_session(cstate)) { |
| 3256 | mutex_lock(&so->so_replay.rp_mutex); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 3257 | cstate->replay_owner = nfs4_get_stateowner(so); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 3258 | } |
| 3259 | } |
| 3260 | |
| 3261 | void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) |
| 3262 | { |
| 3263 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 3264 | |
| 3265 | if (so != NULL) { |
| 3266 | cstate->replay_owner = NULL; |
| 3267 | mutex_unlock(&so->so_replay.rp_mutex); |
| 3268 | nfs4_put_stateowner(so); |
| 3269 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3270 | } |
| 3271 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3272 | 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] | 3273 | { |
| 3274 | struct nfs4_stateowner *sop; |
| 3275 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3276 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3277 | if (!sop) |
| 3278 | return NULL; |
| 3279 | |
| 3280 | sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL); |
| 3281 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3282 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3283 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3284 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3285 | sop->so_owner.len = owner->len; |
| 3286 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3287 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3288 | sop->so_client = clp; |
| 3289 | init_nfs4_replay(&sop->so_replay); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3290 | atomic_set(&sop->so_count, 1); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3291 | return sop; |
| 3292 | } |
| 3293 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3294 | 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] | 3295 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3296 | lockdep_assert_held(&clp->cl_lock); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 3297 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3298 | list_add(&oo->oo_owner.so_strhash, |
| 3299 | &clp->cl_ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3300 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3301 | } |
| 3302 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 3303 | static void nfs4_unhash_openowner(struct nfs4_stateowner *so) |
| 3304 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3305 | unhash_openowner_locked(openowner(so)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 3306 | } |
| 3307 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3308 | static void nfs4_free_openowner(struct nfs4_stateowner *so) |
| 3309 | { |
| 3310 | struct nfs4_openowner *oo = openowner(so); |
| 3311 | |
| 3312 | kmem_cache_free(openowner_slab, oo); |
| 3313 | } |
| 3314 | |
| 3315 | static const struct nfs4_stateowner_operations openowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 3316 | .so_unhash = nfs4_unhash_openowner, |
| 3317 | .so_free = nfs4_free_openowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3318 | }; |
| 3319 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3320 | static struct nfs4_openowner * |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3321 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 3322 | struct nfsd4_compound_state *cstate) |
| 3323 | { |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3324 | struct nfs4_client *clp = cstate->clp; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 3325 | struct nfs4_openowner *oo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3327 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 3328 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | return NULL; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 3330 | oo->oo_owner.so_ops = &openowner_ops; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3331 | oo->oo_owner.so_is_open_owner = 1; |
| 3332 | oo->oo_owner.so_seqid = open->op_seqid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3333 | oo->oo_flags = 0; |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 3334 | if (nfsd4_has_session(cstate)) |
| 3335 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3336 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 3337 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3338 | INIT_LIST_HEAD(&oo->oo_close_lru); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3339 | spin_lock(&clp->cl_lock); |
| 3340 | ret = find_openstateowner_str_locked(strhashval, open, clp); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 3341 | if (ret == NULL) { |
| 3342 | hash_openowner(oo, clp, strhashval); |
| 3343 | ret = oo; |
| 3344 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 3345 | nfs4_free_stateowner(&oo->oo_owner); |
| 3346 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3347 | spin_unlock(&clp->cl_lock); |
Jeff Layton | c595233 | 2015-03-23 10:53:42 -0400 | [diff] [blame] | 3348 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | } |
| 3350 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 3351 | static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3352 | struct nfs4_openowner *oo = open->op_openowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3353 | |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 3354 | atomic_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 3355 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 3356 | INIT_LIST_HEAD(&stp->st_locks); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 3357 | stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3358 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3359 | stp->st_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | stp->st_access_bmap = 0; |
| 3361 | stp->st_deny_bmap = 0; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 3362 | stp->st_openstp = NULL; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 3363 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
| 3364 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3365 | spin_lock(&fp->fi_lock); |
| 3366 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 3367 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 3368 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | } |
| 3370 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3371 | /* |
| 3372 | * In the 4.0 case we need to keep the owners around a little while to handle |
| 3373 | * CLOSE replay. We still do need to release any file access that is held by |
| 3374 | * them before returning however. |
| 3375 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3376 | static void |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3377 | move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3378 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 3379 | struct nfs4_ol_stateid *last; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3380 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 3381 | struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, |
| 3382 | nfsd_net_id); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3383 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3384 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 3386 | /* |
| 3387 | * We know that we hold one reference via nfsd4_close, and another |
| 3388 | * "persistent" reference for the client. If the refcount is higher |
| 3389 | * than 2, then there are still calls in progress that are using this |
| 3390 | * stateid. We can't put the sc_file reference until they are finished. |
| 3391 | * Wait for the refcount to drop to 2. Since it has been unhashed, |
| 3392 | * there should be no danger of the refcount going back up again at |
| 3393 | * this point. |
| 3394 | */ |
| 3395 | wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2); |
| 3396 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3397 | release_all_access(s); |
| 3398 | if (s->st_stid.sc_file) { |
| 3399 | put_nfs4_file(s->st_stid.sc_file); |
| 3400 | s->st_stid.sc_file = NULL; |
| 3401 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 3402 | |
| 3403 | spin_lock(&nn->client_lock); |
| 3404 | last = oo->oo_last_closed_stid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 3405 | oo->oo_last_closed_stid = s; |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 3406 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3407 | oo->oo_time = get_seconds(); |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 3408 | spin_unlock(&nn->client_lock); |
| 3409 | if (last) |
| 3410 | nfs4_put_stid(&last->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | } |
| 3412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | /* search file_hashtbl[] for file */ |
| 3414 | static struct nfs4_file * |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3415 | find_file_locked(struct knfsd_fh *fh, unsigned int hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | struct nfs4_file *fp; |
| 3418 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3419 | hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) { |
Christoph Hellwig | 4d94c2e | 2014-08-14 08:41:48 +0200 | [diff] [blame] | 3420 | if (fh_match(&fp->fi_fhandle, fh)) { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3421 | if (atomic_inc_not_zero(&fp->fi_ref)) |
| 3422 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | } |
| 3425 | return NULL; |
| 3426 | } |
| 3427 | |
Christoph Hellwig | e6ba76e | 2014-08-14 08:50:16 +0200 | [diff] [blame] | 3428 | struct nfs4_file * |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3429 | find_file(struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3430 | { |
| 3431 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3432 | unsigned int hashval = file_hashval(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3433 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3434 | rcu_read_lock(); |
| 3435 | fp = find_file_locked(fh, hashval); |
| 3436 | rcu_read_unlock(); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3437 | return fp; |
| 3438 | } |
| 3439 | |
| 3440 | static struct nfs4_file * |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 3441 | find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3442 | { |
| 3443 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3444 | unsigned int hashval = file_hashval(fh); |
| 3445 | |
| 3446 | rcu_read_lock(); |
| 3447 | fp = find_file_locked(fh, hashval); |
| 3448 | rcu_read_unlock(); |
| 3449 | if (fp) |
| 3450 | return fp; |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3451 | |
| 3452 | spin_lock(&state_lock); |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3453 | fp = find_file_locked(fh, hashval); |
| 3454 | if (likely(fp == NULL)) { |
| 3455 | nfsd4_init_file(fh, hashval, new); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3456 | fp = new; |
| 3457 | } |
| 3458 | spin_unlock(&state_lock); |
| 3459 | |
| 3460 | return fp; |
| 3461 | } |
| 3462 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 3463 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | * Called to check deny when READ with all zero stateid or |
| 3465 | * WRITE with all zero or all one stateid |
| 3466 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3467 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 3469 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | struct nfs4_file *fp; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3471 | __be32 ret = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 3473 | fp = find_file(¤t_fh->fh_handle); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3474 | if (!fp) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3475 | return ret; |
| 3476 | /* Check for conflicting share reservations */ |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3477 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3478 | if (fp->fi_share_deny & deny_type) |
| 3479 | ret = nfserr_locked; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3480 | spin_unlock(&fp->fi_lock); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3481 | put_nfs4_file(fp); |
| 3482 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | } |
| 3484 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3485 | static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3487 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3488 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
| 3489 | nfsd_net_id); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 3490 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3491 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 3492 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3493 | /* |
| 3494 | * 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] | 3495 | * already holding inode->i_lock. |
| 3496 | * |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3497 | * If the dl_time != 0, then we know that it has already been |
| 3498 | * queued for a lease break. Don't queue it again. |
| 3499 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 3500 | spin_lock(&state_lock); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3501 | if (dp->dl_time == 0) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3502 | dp->dl_time = get_seconds(); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3503 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 3504 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3505 | spin_unlock(&state_lock); |
| 3506 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3507 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3508 | static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, |
| 3509 | struct rpc_task *task) |
| 3510 | { |
| 3511 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 3512 | |
Andrew Elble | a457974 | 2015-08-31 12:06:41 -0400 | [diff] [blame] | 3513 | if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID) |
| 3514 | return 1; |
| 3515 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 3516 | switch (task->tk_status) { |
| 3517 | case 0: |
| 3518 | return 1; |
| 3519 | case -EBADHANDLE: |
| 3520 | case -NFS4ERR_BAD_STATEID: |
| 3521 | /* |
| 3522 | * Race: client probably got cb_recall before open reply |
| 3523 | * granting delegation. |
| 3524 | */ |
| 3525 | if (dp->dl_retries--) { |
| 3526 | rpc_delay(task, 2 * HZ); |
| 3527 | return 0; |
| 3528 | } |
| 3529 | /*FALLTHRU*/ |
| 3530 | default: |
| 3531 | return -1; |
| 3532 | } |
| 3533 | } |
| 3534 | |
| 3535 | static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) |
| 3536 | { |
| 3537 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 3538 | |
| 3539 | nfs4_put_stid(&dp->dl_stid); |
| 3540 | } |
| 3541 | |
| 3542 | static struct nfsd4_callback_ops nfsd4_cb_recall_ops = { |
| 3543 | .prepare = nfsd4_cb_recall_prepare, |
| 3544 | .done = nfsd4_cb_recall_done, |
| 3545 | .release = nfsd4_cb_recall_release, |
| 3546 | }; |
| 3547 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3548 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
| 3549 | { |
| 3550 | /* |
| 3551 | * We're assuming the state code never drops its reference |
| 3552 | * without first removing the lease. Since we're in this lease |
| 3553 | * callback (and since the lease code is serialized by the kernel |
| 3554 | * lock) we know the server hasn't removed the lease yet, we know |
| 3555 | * it's safe to take a reference. |
| 3556 | */ |
Trond Myklebust | 72c0b0f | 2014-07-21 09:34:58 -0400 | [diff] [blame] | 3557 | atomic_inc(&dp->dl_stid.sc_count); |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 3558 | nfsd4_run_cb(&dp->dl_recall); |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3559 | } |
| 3560 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 3561 | /* Called from break_lease() with i_lock held. */ |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3562 | static bool |
| 3563 | nfsd_break_deleg_cb(struct file_lock *fl) |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3564 | { |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3565 | bool ret = false; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3566 | struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; |
| 3567 | struct nfs4_delegation *dp; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3568 | |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 3569 | if (!fp) { |
| 3570 | WARN(1, "(%p)->fl_owner NULL\n", fl); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3571 | return ret; |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 3572 | } |
| 3573 | if (fp->fi_had_conflict) { |
| 3574 | WARN(1, "duplicate break on %p\n", fp); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3575 | return ret; |
J. Bruce Fields | 7fa10cd | 2012-10-16 12:39:33 -0400 | [diff] [blame] | 3576 | } |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3577 | /* |
| 3578 | * 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] | 3579 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 3580 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 3581 | */ |
| 3582 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3583 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3584 | spin_lock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3585 | fp->fi_had_conflict = true; |
| 3586 | /* |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3587 | * If there are no delegations on the list, then return true |
| 3588 | * so that the lease code will go ahead and delete it. |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3589 | */ |
| 3590 | if (list_empty(&fp->fi_delegations)) |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3591 | ret = true; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3592 | else |
| 3593 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) |
| 3594 | nfsd_break_one_deleg(dp); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 3595 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 3596 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3597 | } |
| 3598 | |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 3599 | static int |
Jeff Layton | 7448cc3 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 3600 | nfsd_change_deleg_cb(struct file_lock *onlist, int arg, |
| 3601 | struct list_head *dispose) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3602 | { |
| 3603 | if (arg & F_UNLCK) |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 3604 | return lease_modify(onlist, arg, dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3605 | else |
| 3606 | return -EAGAIN; |
| 3607 | } |
| 3608 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 3609 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 3610 | .lm_break = nfsd_break_deleg_cb, |
| 3611 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | }; |
| 3613 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 3614 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 3615 | { |
| 3616 | if (nfsd4_has_session(cstate)) |
| 3617 | return nfs_ok; |
| 3618 | if (seqid == so->so_seqid - 1) |
| 3619 | return nfserr_replay_me; |
| 3620 | if (seqid == so->so_seqid) |
| 3621 | return nfs_ok; |
| 3622 | return nfserr_bad_seqid; |
| 3623 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3625 | static __be32 lookup_clientid(clientid_t *clid, |
| 3626 | struct nfsd4_compound_state *cstate, |
| 3627 | struct nfsd_net *nn) |
| 3628 | { |
| 3629 | struct nfs4_client *found; |
| 3630 | |
| 3631 | if (cstate->clp) { |
| 3632 | found = cstate->clp; |
| 3633 | if (!same_clid(&found->cl_clientid, clid)) |
| 3634 | return nfserr_stale_clientid; |
| 3635 | return nfs_ok; |
| 3636 | } |
| 3637 | |
| 3638 | if (STALE_CLIENTID(clid, nn)) |
| 3639 | return nfserr_stale_clientid; |
| 3640 | |
| 3641 | /* |
| 3642 | * For v4.1+ we get the client in the SEQUENCE op. If we don't have one |
| 3643 | * cached already then we know this is for is for v4.0 and "sessions" |
| 3644 | * will be false. |
| 3645 | */ |
| 3646 | WARN_ON_ONCE(cstate->session); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 3647 | spin_lock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3648 | found = find_confirmed_client(clid, false, nn); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 3649 | if (!found) { |
| 3650 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3651 | return nfserr_expired; |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 3652 | } |
| 3653 | atomic_inc(&found->cl_refcount); |
| 3654 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3655 | |
| 3656 | /* Cache the nfs4_client in cstate! */ |
| 3657 | cstate->clp = found; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3658 | return nfs_ok; |
| 3659 | } |
| 3660 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3661 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3662 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 3663 | struct nfsd4_open *open, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | clientid_t *clientid = &open->op_clientid; |
| 3666 | struct nfs4_client *clp = NULL; |
| 3667 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3668 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3669 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 3671 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | return nfserr_stale_clientid; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3673 | /* |
| 3674 | * In case we need it later, after we've already created the |
| 3675 | * file and don't want to risk a further failure: |
| 3676 | */ |
| 3677 | open->op_file = nfsd4_alloc_file(); |
| 3678 | if (open->op_file == NULL) |
| 3679 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | |
Trond Myklebust | 2d91e89 | 2014-06-30 11:48:46 -0400 | [diff] [blame] | 3681 | status = lookup_clientid(clientid, cstate, nn); |
| 3682 | if (status) |
| 3683 | return status; |
| 3684 | clp = cstate->clp; |
| 3685 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 3686 | strhashval = ownerstr_hashval(&open->op_owner); |
| 3687 | oo = find_openstateowner_str(strhashval, open, clp); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3688 | open->op_openowner = oo; |
| 3689 | if (!oo) { |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3690 | goto new_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3692 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3693 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3694 | release_openowner(oo); |
| 3695 | open->op_openowner = NULL; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3696 | goto new_owner; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3697 | } |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3698 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 3699 | if (status) |
| 3700 | return status; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3701 | goto alloc_stateid; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3702 | new_owner: |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 3703 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 3704 | if (oo == NULL) |
| 3705 | return nfserr_jukebox; |
| 3706 | open->op_openowner = oo; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3707 | alloc_stateid: |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 3708 | open->op_stp = nfs4_alloc_open_stateid(clp); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 3709 | if (!open->op_stp) |
| 3710 | return nfserr_jukebox; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3711 | |
| 3712 | if (nfsd4_has_session(cstate) && |
| 3713 | (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { |
| 3714 | open->op_odstate = alloc_clnt_odstate(clp); |
| 3715 | if (!open->op_odstate) |
| 3716 | return nfserr_jukebox; |
| 3717 | } |
| 3718 | |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 3719 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | } |
| 3721 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3722 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 3723 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 3724 | { |
| 3725 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 3726 | return nfserr_openmode; |
| 3727 | else |
| 3728 | return nfs_ok; |
| 3729 | } |
| 3730 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 3731 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3732 | { |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3733 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 3734 | } |
| 3735 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3736 | 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] | 3737 | { |
| 3738 | struct nfs4_stid *ret; |
| 3739 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 3740 | ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 3741 | if (!ret) |
| 3742 | return NULL; |
| 3743 | return delegstateid(ret); |
| 3744 | } |
| 3745 | |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3746 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 3747 | { |
| 3748 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 3749 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 3750 | } |
| 3751 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3752 | static __be32 |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 3753 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3754 | struct nfs4_delegation **dp) |
| 3755 | { |
| 3756 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3757 | __be32 status = nfserr_bad_stateid; |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 3758 | struct nfs4_delegation *deleg; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3759 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 3760 | deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); |
| 3761 | if (deleg == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3762 | goto out; |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 3763 | flags = share_access_to_flags(open->op_share_access); |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 3764 | status = nfs4_check_delegmode(deleg, flags); |
| 3765 | if (status) { |
| 3766 | nfs4_put_stid(&deleg->dl_stid); |
| 3767 | goto out; |
| 3768 | } |
| 3769 | *dp = deleg; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3770 | out: |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 3771 | if (!nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3772 | return nfs_ok; |
| 3773 | if (status) |
| 3774 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3775 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 3776 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 3777 | } |
| 3778 | |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3779 | static struct nfs4_ol_stateid * |
| 3780 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3781 | { |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3782 | struct nfs4_ol_stateid *local, *ret = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3783 | struct nfs4_openowner *oo = open->op_openowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3785 | spin_lock(&fp->fi_lock); |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 3786 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | /* ignore lock owners */ |
| 3788 | if (local->st_stateowner->so_is_open_owner == 0) |
| 3789 | continue; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3790 | if (local->st_stateowner == &oo->oo_owner) { |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3791 | ret = local; |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 3792 | atomic_inc(&ret->st_stid.sc_count); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3793 | break; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3794 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | } |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3796 | spin_unlock(&fp->fi_lock); |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 3797 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3798 | } |
| 3799 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 3800 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 3801 | { |
| 3802 | int flags = 0; |
| 3803 | |
| 3804 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 3805 | flags |= NFSD_MAY_READ; |
| 3806 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 3807 | flags |= NFSD_MAY_WRITE; |
| 3808 | return flags; |
| 3809 | } |
| 3810 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3811 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 3813 | struct nfsd4_open *open) |
| 3814 | { |
| 3815 | struct iattr iattr = { |
| 3816 | .ia_valid = ATTR_SIZE, |
| 3817 | .ia_size = 0, |
| 3818 | }; |
| 3819 | if (!open->op_truncate) |
| 3820 | return 0; |
| 3821 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 3822 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3823 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); |
| 3824 | } |
| 3825 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3826 | 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] | 3827 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
| 3828 | struct nfsd4_open *open) |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3829 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3830 | struct file *filp = NULL; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3831 | __be32 status; |
| 3832 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 3833 | int access = nfs4_access_to_access(open->op_share_access); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3834 | unsigned char old_access_bmap, old_deny_bmap; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3835 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3836 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3837 | |
| 3838 | /* |
| 3839 | * Are we trying to set a deny mode that would conflict with |
| 3840 | * current access? |
| 3841 | */ |
| 3842 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 3843 | if (status != nfs_ok) { |
| 3844 | spin_unlock(&fp->fi_lock); |
| 3845 | goto out; |
| 3846 | } |
| 3847 | |
| 3848 | /* set access to the file */ |
| 3849 | status = nfs4_file_get_access(fp, open->op_share_access); |
| 3850 | if (status != nfs_ok) { |
| 3851 | spin_unlock(&fp->fi_lock); |
| 3852 | goto out; |
| 3853 | } |
| 3854 | |
| 3855 | /* Set access bits in stateid */ |
| 3856 | old_access_bmap = stp->st_access_bmap; |
| 3857 | set_access(open->op_share_access, stp); |
| 3858 | |
| 3859 | /* Set new deny mask */ |
| 3860 | old_deny_bmap = stp->st_deny_bmap; |
| 3861 | set_deny(open->op_share_deny, stp); |
| 3862 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 3863 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3864 | if (!fp->fi_fds[oflag]) { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3865 | spin_unlock(&fp->fi_lock); |
| 3866 | status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3867 | if (status) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3868 | goto out_put_access; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3869 | spin_lock(&fp->fi_lock); |
| 3870 | if (!fp->fi_fds[oflag]) { |
| 3871 | fp->fi_fds[oflag] = filp; |
| 3872 | filp = NULL; |
| 3873 | } |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3874 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 3875 | spin_unlock(&fp->fi_lock); |
| 3876 | if (filp) |
| 3877 | fput(filp); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3878 | |
| 3879 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3880 | if (status) |
| 3881 | goto out_put_access; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3882 | out: |
| 3883 | return status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3884 | out_put_access: |
| 3885 | stp->st_access_bmap = old_access_bmap; |
| 3886 | nfs4_file_put_access(fp, open->op_share_access); |
| 3887 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); |
| 3888 | goto out; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3889 | } |
| 3890 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3891 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3892 | 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] | 3893 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3894 | __be32 status; |
Arnd Bergmann | 6ac7536 | 2015-05-12 23:31:29 +0200 | [diff] [blame] | 3895 | unsigned char old_deny_bmap = stp->st_deny_bmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3896 | |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 3897 | if (!test_access(open->op_share_access, stp)) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3898 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 3899 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3900 | /* test and set deny mode */ |
| 3901 | spin_lock(&fp->fi_lock); |
| 3902 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 3903 | if (status == nfs_ok) { |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3904 | set_deny(open->op_share_deny, stp); |
| 3905 | fp->fi_share_deny |= |
| 3906 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 3907 | } |
| 3908 | spin_unlock(&fp->fi_lock); |
| 3909 | |
| 3910 | if (status != nfs_ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3912 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 3913 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3914 | if (status != nfs_ok) |
| 3915 | reset_union_bmap_deny(old_deny_bmap, stp); |
| 3916 | return status; |
| 3917 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3918 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 3919 | /* Should we give out recallable state?: */ |
| 3920 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 3921 | { |
| 3922 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 3923 | return true; |
| 3924 | /* |
| 3925 | * In the sessions case, since we don't have to establish a |
| 3926 | * separate connection for callbacks, we assume it's OK |
| 3927 | * until we hear otherwise: |
| 3928 | */ |
| 3929 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 3930 | } |
| 3931 | |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 3932 | 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] | 3933 | { |
| 3934 | struct file_lock *fl; |
| 3935 | |
| 3936 | fl = locks_alloc_lock(); |
| 3937 | if (!fl) |
| 3938 | return NULL; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3939 | fl->fl_lmops = &nfsd_lease_mng_ops; |
J. Bruce Fields | 617588d | 2011-07-01 15:18:34 -0400 | [diff] [blame] | 3940 | fl->fl_flags = FL_DELEG; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3941 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 3942 | fl->fl_end = OFFSET_MAX; |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 3943 | fl->fl_owner = (fl_owner_t)fp; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3944 | fl->fl_pid = current->tgid; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 3945 | return fl; |
| 3946 | } |
| 3947 | |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 3948 | static int nfs4_setlease(struct nfs4_delegation *dp) |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3949 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 3950 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Kinglong Mee | efde6b4 | 2015-07-13 17:33:24 +0800 | [diff] [blame] | 3951 | struct file_lock *fl; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3952 | struct file *filp; |
| 3953 | int status = 0; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3954 | |
Jeff Layton | d564fbe | 2014-07-16 10:31:58 -0400 | [diff] [blame] | 3955 | fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 3956 | if (!fl) |
| 3957 | return -ENOMEM; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3958 | filp = find_readable_file(fp); |
| 3959 | if (!filp) { |
| 3960 | /* We should always have a readable file here */ |
| 3961 | WARN_ON_ONCE(1); |
Kinglong Mee | af9dbaf | 2015-07-13 17:30:51 +0800 | [diff] [blame] | 3962 | locks_free_lock(fl); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3963 | return -EBADF; |
| 3964 | } |
| 3965 | fl->fl_file = filp; |
Jeff Layton | e6f5c78 | 2014-08-22 10:40:25 -0400 | [diff] [blame] | 3966 | status = vfs_setlease(filp, fl->fl_type, &fl, NULL); |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 3967 | if (fl) |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3968 | locks_free_lock(fl); |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 3969 | if (status) |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3970 | goto out_fput; |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3971 | spin_lock(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3972 | spin_lock(&fp->fi_lock); |
| 3973 | /* Did the lease get broken before we took the lock? */ |
| 3974 | status = -EAGAIN; |
| 3975 | if (fp->fi_had_conflict) |
| 3976 | goto out_unlock; |
| 3977 | /* Race breaker */ |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 3978 | if (fp->fi_deleg_file) { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3979 | status = 0; |
Jeff Layton | 67db103 | 2014-12-13 09:11:40 -0500 | [diff] [blame] | 3980 | ++fp->fi_delegees; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3981 | hash_delegation_locked(dp, fp); |
| 3982 | goto out_unlock; |
| 3983 | } |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3984 | fp->fi_deleg_file = filp; |
Jeff Layton | 67db103 | 2014-12-13 09:11:40 -0500 | [diff] [blame] | 3985 | fp->fi_delegees = 1; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 3986 | hash_delegation_locked(dp, fp); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3987 | spin_unlock(&fp->fi_lock); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 3988 | spin_unlock(&state_lock); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3989 | return 0; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 3990 | out_unlock: |
| 3991 | spin_unlock(&fp->fi_lock); |
| 3992 | spin_unlock(&state_lock); |
| 3993 | out_fput: |
| 3994 | fput(filp); |
J. Bruce Fields | e873088 | 2012-01-23 13:52:01 -0500 | [diff] [blame] | 3995 | return status; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3996 | } |
| 3997 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 3998 | static struct nfs4_delegation * |
| 3999 | nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4000 | struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4001 | { |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4002 | int status; |
| 4003 | struct nfs4_delegation *dp; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4004 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 4005 | if (fp->fi_had_conflict) |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4006 | return ERR_PTR(-EAGAIN); |
| 4007 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4008 | dp = alloc_init_deleg(clp, fh, odstate); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4009 | if (!dp) |
| 4010 | return ERR_PTR(-ENOMEM); |
| 4011 | |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 4012 | get_nfs4_file(fp); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4013 | spin_lock(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4014 | spin_lock(&fp->fi_lock); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4015 | dp->dl_stid.sc_file = fp; |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 4016 | if (!fp->fi_deleg_file) { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4017 | spin_unlock(&fp->fi_lock); |
| 4018 | spin_unlock(&state_lock); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4019 | status = nfs4_setlease(dp); |
| 4020 | goto out; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4021 | } |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4022 | if (fp->fi_had_conflict) { |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4023 | status = -EAGAIN; |
| 4024 | goto out_unlock; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4025 | } |
Jeff Layton | 67db103 | 2014-12-13 09:11:40 -0500 | [diff] [blame] | 4026 | ++fp->fi_delegees; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 4027 | hash_delegation_locked(dp, fp); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4028 | status = 0; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4029 | out_unlock: |
| 4030 | spin_unlock(&fp->fi_lock); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4031 | spin_unlock(&state_lock); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4032 | out: |
| 4033 | if (status) { |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4034 | put_clnt_odstate(dp->dl_clnt_odstate); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 4035 | nfs4_put_stid(&dp->dl_stid); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4036 | return ERR_PTR(status); |
| 4037 | } |
| 4038 | return dp; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 4039 | } |
| 4040 | |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 4041 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 4042 | { |
| 4043 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4044 | if (status == -EAGAIN) |
| 4045 | open->op_why_no_deleg = WND4_CONTENTION; |
| 4046 | else { |
| 4047 | open->op_why_no_deleg = WND4_RESOURCE; |
| 4048 | switch (open->op_deleg_want) { |
| 4049 | case NFS4_SHARE_WANT_READ_DELEG: |
| 4050 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 4051 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 4052 | break; |
| 4053 | case NFS4_SHARE_WANT_CANCEL: |
| 4054 | open->op_why_no_deleg = WND4_CANCELLED; |
| 4055 | break; |
| 4056 | case NFS4_SHARE_WANT_NO_DELEG: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4057 | WARN_ON_ONCE(1); |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 4058 | } |
| 4059 | } |
| 4060 | } |
| 4061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | /* |
| 4063 | * Attempt to hand out a delegation. |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4064 | * |
| 4065 | * Note we don't support write delegations, and won't until the vfs has |
| 4066 | * proper support for them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | */ |
| 4068 | static void |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4069 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
| 4070 | struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | { |
| 4072 | struct nfs4_delegation *dp; |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4073 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
| 4074 | struct nfs4_client *clp = stp->st_stid.sc_client; |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 4075 | int cb_up; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4076 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4077 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4078 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4079 | open->op_recall = 0; |
| 4080 | switch (open->op_claim_type) { |
| 4081 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 4082 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4083 | open->op_recall = 1; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4084 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 4085 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4086 | break; |
| 4087 | case NFS4_OPEN_CLAIM_NULL: |
Ming Chen | ed47b06 | 2014-01-09 21:26:10 +0000 | [diff] [blame] | 4088 | case NFS4_OPEN_CLAIM_FH: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4089 | /* |
| 4090 | * Let's not give out any delegations till everyone's |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 4091 | * had the chance to reclaim theirs, *and* until |
| 4092 | * NLM locks have all been reclaimed: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4093 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4094 | if (locks_in_grace(clp->net)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4095 | goto out_no_deleg; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4096 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4097 | goto out_no_deleg; |
Steve Dickson | 9a0590a | 2013-05-15 14:51:49 -0400 | [diff] [blame] | 4098 | /* |
| 4099 | * Also, if the file was opened for write or |
| 4100 | * create, there's a good chance the client's |
| 4101 | * about to write to it, resulting in an |
| 4102 | * immediate recall (since we don't support |
| 4103 | * write delegations): |
| 4104 | */ |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4105 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4106 | goto out_no_deleg; |
| 4107 | if (open->op_create == NFS4_OPEN_CREATE) |
| 4108 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4109 | break; |
| 4110 | default: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4111 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4112 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4113 | 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] | 4114 | if (IS_ERR(dp)) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 4115 | goto out_no_deleg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 4117 | 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] | 4118 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4119 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 4120 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4121 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
Trond Myklebust | 67cb127 | 2014-07-29 21:34:17 -0400 | [diff] [blame] | 4122 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 4123 | return; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 4124 | out_no_deleg: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4125 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 4126 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 4127 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4128 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 4129 | open->op_recall = 1; |
| 4130 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4131 | |
| 4132 | /* 4.1 client asking for a delegation? */ |
| 4133 | if (open->op_deleg_want) |
| 4134 | nfsd4_open_deleg_none_ext(open, status); |
| 4135 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | } |
| 4137 | |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 4138 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 4139 | struct nfs4_delegation *dp) |
| 4140 | { |
| 4141 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 4142 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 4143 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4144 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 4145 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 4146 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 4147 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4148 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 4149 | } |
| 4150 | /* Otherwise the client must be confused wanting a delegation |
| 4151 | * it already has, therefore we don't return |
| 4152 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 4153 | */ |
| 4154 | } |
| 4155 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4156 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 4158 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4159 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4160 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | struct nfs4_file *fp = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4162 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4163 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4164 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | /* |
| 4167 | * Lookup file; if found, lookup stateid and check open request, |
| 4168 | * and check for delegations in the process of being recalled. |
| 4169 | * If not found, create the nfs4_file struct |
| 4170 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 4171 | fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4172 | if (fp != open->op_file) { |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 4173 | status = nfs4_check_deleg(cl, open, &dp); |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4174 | if (status) |
| 4175 | goto out; |
Jeff Layton | a46cb7f | 2014-07-10 14:07:35 -0400 | [diff] [blame] | 4176 | stp = nfsd4_find_existing_open(fp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | } else { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4178 | open->op_file = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4179 | status = nfserr_bad_stateid; |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4180 | if (nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4181 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | } |
| 4183 | |
| 4184 | /* |
| 4185 | * OPEN the file, or upgrade an existing OPEN. |
| 4186 | * If truncate fails, the OPEN fails. |
| 4187 | */ |
| 4188 | if (stp) { |
| 4189 | /* Stateid was found, this is an OPEN upgrade */ |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4190 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | if (status) |
| 4192 | goto out; |
| 4193 | } else { |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4194 | stp = open->op_stp; |
| 4195 | open->op_stp = NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 4196 | init_open_stateid(stp, fp, open); |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4197 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
| 4198 | if (status) { |
| 4199 | release_open_stateid(stp); |
| 4200 | goto out; |
| 4201 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4202 | |
| 4203 | stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, |
| 4204 | open->op_odstate); |
| 4205 | if (stp->st_clnt_odstate == open->op_odstate) |
| 4206 | open->op_odstate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | } |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4208 | update_stateid(&stp->st_stid.sc_stateid); |
| 4209 | memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4211 | if (nfsd4_has_session(&resp->cstate)) { |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4212 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 4213 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4214 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 4215 | goto nodeleg; |
| 4216 | } |
| 4217 | } |
| 4218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | /* |
| 4220 | * Attempt to hand out a delegation. No error return, because the |
| 4221 | * OPEN succeeds even if we fail. |
| 4222 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4223 | nfs4_open_delegation(current_fh, open, stp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4224 | nodeleg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | status = nfs_ok; |
| 4226 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4227 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4228 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | out: |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4230 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 4231 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 4232 | open->op_deleg_want) |
| 4233 | nfsd4_deleg_xgrade_none_ext(open, dp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 4234 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4235 | if (fp) |
| 4236 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 4237 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
Kinglong Mee | 8718602 | 2015-07-13 17:32:59 +0800 | [diff] [blame] | 4238 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4239 | /* |
| 4240 | * To finish the open response, we just need to set the rflags. |
| 4241 | */ |
| 4242 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4243 | if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) && |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4244 | !nfsd4_has_session(&resp->cstate)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4246 | if (dp) |
| 4247 | nfs4_put_stid(&dp->dl_stid); |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 4248 | if (stp) |
| 4249 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | |
| 4251 | return status; |
| 4252 | } |
| 4253 | |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4254 | void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, |
Jeff Layton | 4229789 | 2015-03-23 10:53:44 -0400 | [diff] [blame] | 4255 | struct nfsd4_open *open) |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4256 | { |
| 4257 | if (open->op_openowner) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4258 | struct nfs4_stateowner *so = &open->op_openowner->oo_owner; |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4259 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4260 | nfsd4_cstate_assign_replay(cstate, so); |
| 4261 | nfs4_put_stateowner(so); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4262 | } |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4263 | if (open->op_file) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4264 | kmem_cache_free(file_slab, open->op_file); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4265 | if (open->op_stp) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 4266 | nfs4_put_stid(&open->op_stp->st_stid); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4267 | if (open->op_odstate) |
| 4268 | kmem_cache_free(odstate_slab, open->op_odstate); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 4269 | } |
| 4270 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4271 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 4272 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4273 | clientid_t *clid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | { |
| 4275 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4276 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 4277 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | dprintk("process_renew(%08x/%08x): starting\n", |
| 4280 | clid->cl_boot, clid->cl_id); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4281 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 4282 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | goto out; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4284 | clp = cstate->clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | status = nfserr_cb_path_down; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 4286 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 4287 | && clp->cl_cb_state != NFSD4_CB_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4288 | goto out; |
| 4289 | status = nfs_ok; |
| 4290 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | return status; |
| 4292 | } |
| 4293 | |
Jeff Layton | 7f5ef2e | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 4294 | void |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 4295 | nfsd4_end_grace(struct nfsd_net *nn) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 4296 | { |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 4297 | /* do nothing if grace period already ended */ |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 4298 | if (nn->grace_ended) |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 4299 | return; |
| 4300 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 4301 | dprintk("NFSD: end of grace period\n"); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 4302 | nn->grace_ended = true; |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 4303 | /* |
| 4304 | * If the server goes down again right now, an NFSv4 |
| 4305 | * client will still be allowed to reclaim after it comes back up, |
| 4306 | * even if it hasn't yet had a chance to reclaim state this time. |
| 4307 | * |
| 4308 | */ |
Jeff Layton | 919b804 | 2014-09-12 16:40:20 -0400 | [diff] [blame] | 4309 | nfsd4_record_grace_done(nn); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 4310 | /* |
| 4311 | * At this point, NFSv4 clients can still reclaim. But if the |
| 4312 | * server crashes, any that have not yet reclaimed will be out |
| 4313 | * of luck on the next boot. |
| 4314 | * |
| 4315 | * (NFSv4.1+ clients are considered to have reclaimed once they |
| 4316 | * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to |
| 4317 | * have reclaimed after their first OPEN.) |
| 4318 | */ |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 4319 | locks_end_grace(&nn->nfsd4_manager); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 4320 | /* |
| 4321 | * At this point, and once lockd and/or any other containers |
| 4322 | * exit their grace period, further reclaims will fail and |
| 4323 | * regular locking can resume. |
| 4324 | */ |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 4325 | } |
| 4326 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 4327 | static time_t |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4328 | nfs4_laundromat(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | { |
| 4330 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4331 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | struct nfs4_delegation *dp; |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4333 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 3d73371 | 2012-11-27 14:11:44 +0300 | [diff] [blame] | 4335 | time_t cutoff = get_seconds() - nn->nfsd4_lease; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4336 | time_t t, new_timeo = nn->nfsd4_lease; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | dprintk("NFSD: laundromat service - starting\n"); |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 4339 | nfsd4_end_grace(nn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 4340 | INIT_LIST_HEAD(&reaplist); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 4341 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 4342 | list_for_each_safe(pos, next, &nn->client_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
| 4344 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { |
| 4345 | t = clp->cl_time - cutoff; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4346 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | break; |
| 4348 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 4349 | if (mark_client_expired_locked(clp)) { |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 4350 | dprintk("NFSD: client in use (clientid %08x)\n", |
| 4351 | clp->cl_clientid.cl_id); |
| 4352 | continue; |
| 4353 | } |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 4354 | list_add(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 4355 | } |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 4356 | spin_unlock(&nn->client_lock); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 4357 | list_for_each_safe(pos, next, &reaplist) { |
| 4358 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4359 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 4360 | clp->cl_clientid.cl_id); |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 4361 | list_del_init(&clp->cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | expire_client(clp); |
| 4363 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4364 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 4365 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4366 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 4367 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4368 | t = dp->dl_time - cutoff; |
| 4369 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | break; |
| 4371 | } |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 4372 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 4373 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 4375 | spin_unlock(&state_lock); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 4376 | while (!list_empty(&reaplist)) { |
| 4377 | dp = list_first_entry(&reaplist, struct nfs4_delegation, |
| 4378 | dl_recall_lru); |
| 4379 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4380 | revoke_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4381 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4382 | |
| 4383 | spin_lock(&nn->client_lock); |
| 4384 | while (!list_empty(&nn->close_lru)) { |
| 4385 | oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, |
| 4386 | oo_close_lru); |
| 4387 | if (time_after((unsigned long)oo->oo_time, |
| 4388 | (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4389 | t = oo->oo_time - cutoff; |
| 4390 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | break; |
| 4392 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4393 | list_del_init(&oo->oo_close_lru); |
| 4394 | stp = oo->oo_last_closed_stid; |
| 4395 | oo->oo_last_closed_stid = NULL; |
| 4396 | spin_unlock(&nn->client_lock); |
| 4397 | nfs4_put_stid(&stp->st_stid); |
| 4398 | spin_lock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4400 | spin_unlock(&nn->client_lock); |
| 4401 | |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4402 | new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 4403 | return new_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | } |
| 4405 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 4406 | static struct workqueue_struct *laundry_wq; |
| 4407 | static void laundromat_main(struct work_struct *); |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 4408 | |
| 4409 | static void |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4410 | laundromat_main(struct work_struct *laundry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | { |
| 4412 | time_t t; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4413 | struct delayed_work *dwork = container_of(laundry, struct delayed_work, |
| 4414 | work); |
| 4415 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 4416 | laundromat_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4418 | t = nfs4_laundromat(nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 4420 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | } |
| 4422 | |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 4423 | 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] | 4424 | { |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 4425 | if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4426 | return nfserr_bad_stateid; |
| 4427 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | } |
| 4429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4431 | access_permit_read(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4432 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4433 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 4434 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 4435 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | } |
| 4437 | |
| 4438 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4439 | access_permit_write(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4441 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 4442 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | } |
| 4444 | |
| 4445 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4446 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4448 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 4450 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 4451 | if (stp->st_openstp) |
| 4452 | stp = stp->st_openstp; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4453 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | goto out; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4455 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | goto out; |
| 4457 | status = nfs_ok; |
| 4458 | out: |
| 4459 | return status; |
| 4460 | } |
| 4461 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4462 | static inline __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4463 | 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] | 4464 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 4465 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | return nfs_ok; |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 4467 | else if (opens_in_grace(net)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4468 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | * conflicting state; so we must wait out the grace period. */ |
| 4470 | return nfserr_grace; |
| 4471 | } else if (flags & WR_STATE) |
| 4472 | return nfs4_share_conflict(current_fh, |
| 4473 | NFS4_SHARE_DENY_WRITE); |
| 4474 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 4475 | return nfs4_share_conflict(current_fh, |
| 4476 | NFS4_SHARE_DENY_READ); |
| 4477 | } |
| 4478 | |
| 4479 | /* |
| 4480 | * Allow READ/WRITE during grace period on recovered state only for files |
| 4481 | * that are not able to provide mandatory locking. |
| 4482 | */ |
| 4483 | static inline int |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4484 | grace_disallows_io(struct net *net, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | { |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 4486 | return opens_in_grace(net) && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | } |
| 4488 | |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4489 | /* Returns true iff a is later than b: */ |
| 4490 | static bool stateid_generation_after(stateid_t *a, stateid_t *b) |
| 4491 | { |
Jim Rees | 1a9357f | 2013-05-17 17:33:00 -0400 | [diff] [blame] | 4492 | return (s32)(a->si_generation - b->si_generation) > 0; |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4493 | } |
| 4494 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 4495 | 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] | 4496 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4497 | /* |
| 4498 | * When sessions are used the stateid generation number is ignored |
| 4499 | * when it is zero. |
| 4500 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 4501 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4502 | return nfs_ok; |
| 4503 | |
| 4504 | if (in->si_generation == ref->si_generation) |
| 4505 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4506 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4507 | /* If the client sends us a stateid from the future, it's buggy: */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4508 | if (stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4509 | return nfserr_bad_stateid; |
| 4510 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4511 | * However, we could see a stateid from the past, even from a |
| 4512 | * non-buggy client. For example, if the client sends a lock |
| 4513 | * while some IO is outstanding, the lock may bump si_generation |
| 4514 | * while the IO is still in flight. The client could avoid that |
| 4515 | * situation by waiting for responses on all the IO requests, |
| 4516 | * but better performance may result in retrying IO that |
| 4517 | * receives an old_stateid error if requests are rarely |
| 4518 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4519 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 4520 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4521 | } |
| 4522 | |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 4523 | static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) |
| 4524 | { |
| 4525 | if (ols->st_stateowner->so_is_open_owner && |
| 4526 | !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
| 4527 | return nfserr_bad_stateid; |
| 4528 | return nfs_ok; |
| 4529 | } |
| 4530 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4531 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4532 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4533 | struct nfs4_stid *s; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4534 | __be32 status = nfserr_bad_stateid; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4535 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4536 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4537 | return status; |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4538 | /* Client debugging aid. */ |
| 4539 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { |
| 4540 | char addr_str[INET6_ADDRSTRLEN]; |
| 4541 | rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, |
| 4542 | sizeof(addr_str)); |
| 4543 | pr_warn_ratelimited("NFSD: client %s testing state ID " |
| 4544 | "with incorrect client ID\n", addr_str); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4545 | return status; |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 4546 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4547 | spin_lock(&cl->cl_lock); |
| 4548 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 4549 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4550 | goto out_unlock; |
J. Bruce Fields | 36279ac | 2011-09-26 12:53:00 -0400 | [diff] [blame] | 4551 | status = check_stateid_generation(stateid, &s->sc_stateid, 1); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4552 | if (status) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4553 | goto out_unlock; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4554 | switch (s->sc_type) { |
| 4555 | case NFS4_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4556 | status = nfs_ok; |
| 4557 | break; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4558 | case NFS4_REVOKED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4559 | status = nfserr_deleg_revoked; |
| 4560 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4561 | case NFS4_OPEN_STID: |
| 4562 | case NFS4_LOCK_STID: |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 4563 | status = nfsd4_check_openowner_confirmed(openlockstateid(s)); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4564 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4565 | default: |
| 4566 | printk("unknown stateid type %x\n", s->sc_type); |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 4567 | /* Fallthrough */ |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4568 | case NFS4_CLOSED_STID: |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 4569 | case NFS4_CLOSED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4570 | status = nfserr_bad_stateid; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 4571 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4572 | out_unlock: |
| 4573 | spin_unlock(&cl->cl_lock); |
| 4574 | return status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4575 | } |
| 4576 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 4577 | __be32 |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4578 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
| 4579 | stateid_t *stateid, unsigned char typemask, |
| 4580 | struct nfs4_stid **s, struct nfsd_net *nn) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4581 | { |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 4582 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4583 | |
| 4584 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 4585 | return nfserr_bad_stateid; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4586 | status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn); |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4587 | if (status == nfserr_stale_clientid) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4588 | if (cstate->session) |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4589 | return nfserr_bad_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4590 | return nfserr_stale_stateid; |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 4591 | } |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 4592 | if (status) |
| 4593 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4594 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4595 | if (!*s) |
| 4596 | return nfserr_bad_stateid; |
| 4597 | return nfs_ok; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4598 | } |
| 4599 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4600 | static struct file * |
| 4601 | nfs4_find_file(struct nfs4_stid *s, int flags) |
| 4602 | { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4603 | if (!s) |
| 4604 | return NULL; |
| 4605 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4606 | switch (s->sc_type) { |
| 4607 | case NFS4_DELEG_STID: |
| 4608 | if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) |
| 4609 | return NULL; |
| 4610 | return get_file(s->sc_file->fi_deleg_file); |
| 4611 | case NFS4_OPEN_STID: |
| 4612 | case NFS4_LOCK_STID: |
| 4613 | if (flags & RD_STATE) |
| 4614 | return find_readable_file(s->sc_file); |
| 4615 | else |
| 4616 | return find_writeable_file(s->sc_file); |
| 4617 | break; |
| 4618 | } |
| 4619 | |
| 4620 | return NULL; |
| 4621 | } |
| 4622 | |
| 4623 | static __be32 |
| 4624 | nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags) |
| 4625 | { |
| 4626 | __be32 status; |
| 4627 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4628 | status = nfsd4_check_openowner_confirmed(ols); |
| 4629 | if (status) |
| 4630 | return status; |
| 4631 | return nfs4_check_openmode(ols, flags); |
| 4632 | } |
| 4633 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4634 | static __be32 |
| 4635 | nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s, |
| 4636 | struct file **filpp, bool *tmp_file, int flags) |
| 4637 | { |
| 4638 | int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; |
| 4639 | struct file *file; |
| 4640 | __be32 status; |
| 4641 | |
| 4642 | file = nfs4_find_file(s, flags); |
| 4643 | if (file) { |
| 4644 | status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
| 4645 | acc | NFSD_MAY_OWNER_OVERRIDE); |
| 4646 | if (status) { |
| 4647 | fput(file); |
| 4648 | return status; |
| 4649 | } |
| 4650 | |
| 4651 | *filpp = file; |
| 4652 | } else { |
| 4653 | status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp); |
| 4654 | if (status) |
| 4655 | return status; |
| 4656 | |
| 4657 | if (tmp_file) |
| 4658 | *tmp_file = true; |
| 4659 | } |
| 4660 | |
| 4661 | return 0; |
| 4662 | } |
| 4663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | /* |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4665 | * Checks for stateid operations |
| 4666 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4667 | __be32 |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4668 | nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, |
| 4669 | struct nfsd4_compound_state *cstate, stateid_t *stateid, |
| 4670 | int flags, struct file **filpp, bool *tmp_file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | { |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4672 | struct svc_fh *fhp = &cstate->current_fh; |
| 4673 | struct inode *ino = d_inode(fhp->fh_dentry); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4674 | struct net *net = SVC_NET(rqstp); |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4675 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4676 | struct nfs4_stid *s = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4677 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | if (filpp) |
| 4680 | *filpp = NULL; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4681 | if (tmp_file) |
| 4682 | *tmp_file = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 4684 | if (grace_disallows_io(net, ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | return nfserr_grace; |
| 4686 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4687 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
| 4688 | status = check_special_stateids(net, fhp, stateid, flags); |
| 4689 | goto done; |
| 4690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4692 | status = nfsd4_lookup_stateid(cstate, stateid, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4693 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4694 | &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4695 | if (status) |
Trond Myklebust | c2d1d6a | 2014-07-30 08:27:25 -0400 | [diff] [blame] | 4696 | return status; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4697 | status = check_stateid_generation(stateid, &s->sc_stateid, |
| 4698 | nfsd4_has_session(cstate)); |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 4699 | if (status) |
| 4700 | goto out; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4701 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4702 | switch (s->sc_type) { |
| 4703 | case NFS4_DELEG_STID: |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4704 | status = nfs4_check_delegmode(delegstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4705 | break; |
| 4706 | case NFS4_OPEN_STID: |
| 4707 | case NFS4_LOCK_STID: |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4708 | status = nfs4_check_olstateid(fhp, openlockstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4709 | break; |
| 4710 | default: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 4711 | status = nfserr_bad_stateid; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4712 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | } |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 4714 | if (status) |
| 4715 | goto out; |
| 4716 | status = nfs4_check_fh(fhp, s); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 4717 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4718 | done: |
| 4719 | if (!status && filpp) |
| 4720 | status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4721 | out: |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 4722 | if (s) |
| 4723 | nfs4_put_stid(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | return status; |
| 4725 | } |
| 4726 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4727 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4728 | * Test if the stateid is valid |
| 4729 | */ |
| 4730 | __be32 |
| 4731 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4732 | struct nfsd4_test_stateid *test_stateid) |
| 4733 | { |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4734 | struct nfsd4_test_stateid_id *stateid; |
| 4735 | struct nfs4_client *cl = cstate->session->se_client; |
| 4736 | |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4737 | 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] | 4738 | stateid->ts_id_status = |
| 4739 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 4740 | |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 4741 | return nfs_ok; |
| 4742 | } |
| 4743 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4744 | __be32 |
| 4745 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4746 | struct nfsd4_free_stateid *free_stateid) |
| 4747 | { |
| 4748 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4749 | struct nfs4_stid *s; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4750 | struct nfs4_delegation *dp; |
Jeff Layton | fc5a96c | 2014-07-29 21:34:40 -0400 | [diff] [blame] | 4751 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4752 | struct nfs4_client *cl = cstate->session->se_client; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4753 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4754 | |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4755 | spin_lock(&cl->cl_lock); |
| 4756 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4757 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4758 | goto out_unlock; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4759 | switch (s->sc_type) { |
| 4760 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4761 | ret = nfserr_locks_held; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4762 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4763 | case NFS4_OPEN_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4764 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 4765 | if (ret) |
| 4766 | break; |
| 4767 | ret = nfserr_locks_held; |
| 4768 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 4769 | case NFS4_LOCK_STID: |
| 4770 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 4771 | if (ret) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4772 | break; |
Jeff Layton | fc5a96c | 2014-07-29 21:34:40 -0400 | [diff] [blame] | 4773 | stp = openlockstateid(s); |
| 4774 | ret = nfserr_locks_held; |
| 4775 | if (check_for_locks(stp->st_stid.sc_file, |
| 4776 | lockowner(stp->st_stateowner))) |
| 4777 | break; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 4778 | WARN_ON(!unhash_lock_stateid(stp)); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4779 | spin_unlock(&cl->cl_lock); |
Jeff Layton | fc5a96c | 2014-07-29 21:34:40 -0400 | [diff] [blame] | 4780 | nfs4_put_stid(s); |
| 4781 | ret = nfs_ok; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4782 | goto out; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4783 | case NFS4_REVOKED_DELEG_STID: |
| 4784 | dp = delegstateid(s); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 4785 | list_del_init(&dp->dl_recall_lru); |
| 4786 | spin_unlock(&cl->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 4787 | nfs4_put_stid(s); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4788 | ret = nfs_ok; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4789 | goto out; |
| 4790 | /* Default falls through and returns nfserr_bad_stateid */ |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4791 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 4792 | out_unlock: |
| 4793 | spin_unlock(&cl->cl_lock); |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4794 | out: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 4795 | return ret; |
| 4796 | } |
| 4797 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4798 | static inline int |
| 4799 | setlkflg (int type) |
| 4800 | { |
| 4801 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 4802 | RD_STATE : WR_STATE; |
| 4803 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4805 | 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] | 4806 | { |
| 4807 | struct svc_fh *current_fh = &cstate->current_fh; |
| 4808 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 4809 | __be32 status; |
| 4810 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4811 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 4812 | if (status) |
| 4813 | return status; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4814 | if (stp->st_stid.sc_type == NFS4_CLOSED_STID |
| 4815 | || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4816 | /* |
| 4817 | * "Closed" stateid's exist *only* to return |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 4818 | * nfserr_replay_me from the previous step, and |
| 4819 | * revoked delegations are kept only for free_stateid. |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4820 | */ |
| 4821 | return nfserr_bad_stateid; |
| 4822 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); |
| 4823 | if (status) |
| 4824 | return status; |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 4825 | return nfs4_check_fh(current_fh, &stp->st_stid); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4826 | } |
| 4827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | /* |
| 4829 | * Checks for sequence id mutating operations. |
| 4830 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4831 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 4832 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 4833 | stateid_t *stateid, char typemask, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4834 | struct nfs4_ol_stateid **stpp, |
| 4835 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 4837 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4838 | struct nfs4_stid *s; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4839 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4841 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
| 4842 | seqid, STATEID_VAL(stateid)); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | *stpp = NULL; |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 4845 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4846 | if (status) |
| 4847 | return status; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4848 | stp = openlockstateid(s); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4849 | nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4850 | |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4851 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 4852 | if (!status) |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4853 | *stpp = stp; |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 4854 | else |
| 4855 | nfs4_put_stid(&stp->st_stid); |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 4856 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4857 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 4858 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4859 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 4860 | 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] | 4861 | { |
| 4862 | __be32 status; |
| 4863 | struct nfs4_openowner *oo; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 4864 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4866 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 4867 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 4868 | if (status) |
| 4869 | return status; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 4870 | oo = openowner(stp->st_stateowner); |
| 4871 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
| 4872 | nfs4_put_stid(&stp->st_stid); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4873 | return nfserr_bad_stateid; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 4874 | } |
| 4875 | *stpp = stp; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 4876 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | } |
| 4878 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4879 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4880 | 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] | 4881 | struct nfsd4_open_confirm *oc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4883 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4884 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4885 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4886 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4888 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 4889 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4891 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 4892 | if (status) |
| 4893 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4895 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4896 | oc->oc_seqid, &oc->oc_req_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4897 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4898 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4899 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4900 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4901 | status = nfserr_bad_stateid; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4902 | if (oo->oo_flags & NFS4_OO_CONFIRMED) |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 4903 | goto put_stateid; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4904 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4905 | update_stateid(&stp->st_stid.sc_stateid); |
| 4906 | memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 4907 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4908 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 4909 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 4910 | nfsd4_client_record_create(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 4911 | status = nfs_ok; |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 4912 | put_stateid: |
| 4913 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4915 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | return status; |
| 4917 | } |
| 4918 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4919 | 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] | 4920 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4921 | if (!test_access(access, stp)) |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4922 | return; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4923 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4924 | clear_access(access, stp); |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4925 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 4926 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4927 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 4928 | { |
| 4929 | switch (to_access) { |
| 4930 | case NFS4_SHARE_ACCESS_READ: |
| 4931 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 4932 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 4933 | break; |
| 4934 | case NFS4_SHARE_ACCESS_WRITE: |
| 4935 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 4936 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 4937 | break; |
| 4938 | case NFS4_SHARE_ACCESS_BOTH: |
| 4939 | break; |
| 4940 | default: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4941 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | } |
| 4943 | } |
| 4944 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4945 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4946 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
| 4947 | struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4948 | struct nfsd4_open_downgrade *od) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4950 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4951 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4952 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4953 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 4954 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 4955 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4956 | |
J. Bruce Fields | c30e92d | 2011-10-10 17:34:31 -0400 | [diff] [blame] | 4957 | /* We don't yet support WANT bits: */ |
Benny Halevy | 2c8bd7e | 2012-02-16 20:57:09 +0200 | [diff] [blame] | 4958 | if (od->od_deleg_want) |
| 4959 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 4960 | od->od_deleg_want); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 4962 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4963 | &od->od_stateid, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4964 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4965 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | status = nfserr_inval; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 4967 | if (!test_access(od->od_share_access, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 4968 | 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] | 4969 | stp->st_access_bmap, od->od_share_access); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 4970 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4971 | } |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4972 | if (!test_deny(od->od_share_deny, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 4973 | 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] | 4974 | stp->st_deny_bmap, od->od_share_deny); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 4975 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | } |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 4977 | nfs4_stateid_downgrade(stp, od->od_share_access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4978 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 4979 | reset_union_bmap_deny(od->od_share_deny, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4981 | update_stateid(&stp->st_stid.sc_stateid); |
| 4982 | memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4983 | status = nfs_ok; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 4984 | put_stateid: |
| 4985 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4986 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 4987 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4988 | return status; |
| 4989 | } |
| 4990 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4991 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 4992 | { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4993 | struct nfs4_client *clp = s->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 4994 | bool unhashed; |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 4995 | LIST_HEAD(reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 4996 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 4997 | s->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 4998 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 4999 | unhashed = unhash_open_stateid(s, &reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 5000 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 5001 | if (clp->cl_minorversion) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 5002 | if (unhashed) |
| 5003 | put_ol_stateid_locked(s, &reaplist); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 5004 | spin_unlock(&clp->cl_lock); |
| 5005 | free_ol_stateid_reaplist(&reaplist); |
| 5006 | } else { |
| 5007 | spin_unlock(&clp->cl_lock); |
| 5008 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 5009 | if (unhashed) |
| 5010 | move_to_close_lru(s, clp->net); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 5011 | } |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 5012 | } |
| 5013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5014 | /* |
| 5015 | * nfs4_unlock_state() called after encode |
| 5016 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5017 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5018 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 5019 | struct nfsd4_close *close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5021 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5022 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5023 | struct net *net = SVC_NET(rqstp); |
| 5024 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 5026 | dprintk("NFSD: nfsd4_close on file %pd\n", |
| 5027 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5029 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 5030 | &close->cl_stateid, |
| 5031 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5032 | &stp, nn); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5033 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5034 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | goto out; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5036 | update_stateid(&stp->st_stid.sc_stateid); |
| 5037 | memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5039 | nfsd4_close_open_stateid(stp); |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 5040 | |
| 5041 | /* put reference from nfs4_preprocess_seqid_op */ |
| 5042 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5044 | return status; |
| 5045 | } |
| 5046 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5047 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5048 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 5049 | struct nfsd4_delegreturn *dr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5051 | struct nfs4_delegation *dp; |
| 5052 | stateid_t *stateid = &dr->dr_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5053 | struct nfs4_stid *s; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5054 | __be32 status; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5055 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5057 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5058 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5060 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5061 | if (status) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5062 | goto out; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5063 | dp = delegstateid(s); |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 5064 | 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] | 5065 | if (status) |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5066 | goto put_stateid; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5067 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5068 | destroy_delegation(dp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5069 | put_stateid: |
| 5070 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | out: |
| 5072 | return status; |
| 5073 | } |
| 5074 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 5075 | static inline u64 |
| 5076 | end_offset(u64 start, u64 len) |
| 5077 | { |
| 5078 | u64 end; |
| 5079 | |
| 5080 | end = start + len; |
| 5081 | return end >= start ? end: NFS4_MAX_UINT64; |
| 5082 | } |
| 5083 | |
| 5084 | /* last octet in a range */ |
| 5085 | static inline u64 |
| 5086 | last_byte_offset(u64 start, u64 len) |
| 5087 | { |
| 5088 | u64 end; |
| 5089 | |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 5090 | WARN_ON_ONCE(!len); |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 5091 | end = start + len; |
| 5092 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 5093 | } |
| 5094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5095 | /* |
| 5096 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 5097 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 5098 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 5099 | * locking, this prevents us from being completely protocol-compliant. The |
| 5100 | * real solution to this problem is to start using unsigned file offsets in |
| 5101 | * the VFS, but this is a very deep change! |
| 5102 | */ |
| 5103 | static inline void |
| 5104 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 5105 | { |
| 5106 | if (lock->fl_start < 0) |
| 5107 | lock->fl_start = OFFSET_MAX; |
| 5108 | if (lock->fl_end < 0) |
| 5109 | lock->fl_end = OFFSET_MAX; |
| 5110 | } |
| 5111 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5112 | static fl_owner_t |
| 5113 | nfsd4_fl_get_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5114 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5115 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
| 5116 | |
| 5117 | nfs4_get_stateowner(&lo->lo_owner); |
| 5118 | return owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5119 | } |
| 5120 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5121 | static void |
| 5122 | nfsd4_fl_put_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5123 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5124 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5125 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 5126 | if (lo) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5127 | nfs4_put_stateowner(&lo->lo_owner); |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5128 | } |
| 5129 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 5130 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5131 | .lm_get_owner = nfsd4_fl_get_owner, |
| 5132 | .lm_put_owner = nfsd4_fl_put_owner, |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 5133 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5134 | |
| 5135 | static inline void |
| 5136 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 5137 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5138 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 5140 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5141 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
| 5142 | deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data, |
| 5143 | lo->lo_owner.so_owner.len, GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 5144 | if (!deny->ld_owner.data) |
| 5145 | /* We just don't care that much */ |
| 5146 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5147 | deny->ld_owner.len = lo->lo_owner.so_owner.len; |
| 5148 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 5149 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 5150 | nevermind: |
| 5151 | deny->ld_owner.len = 0; |
| 5152 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 5153 | deny->ld_clientid.cl_boot = 0; |
| 5154 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | } |
| 5156 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 5157 | deny->ld_length = NFS4_MAX_UINT64; |
| 5158 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 5160 | deny->ld_type = NFS4_READ_LT; |
| 5161 | if (fl->fl_type != F_RDLCK) |
| 5162 | deny->ld_type = NFS4_WRITE_LT; |
| 5163 | } |
| 5164 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5165 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5166 | find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5167 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5168 | unsigned int strhashval = ownerstr_hashval(owner); |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 5169 | struct nfs4_stateowner *so; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 5171 | lockdep_assert_held(&clp->cl_lock); |
| 5172 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5173 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], |
| 5174 | so_strhash) { |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 5175 | if (so->so_is_open_owner) |
| 5176 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 5177 | if (same_owner_str(so, owner)) |
| 5178 | return lockowner(nfs4_get_stateowner(so)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | } |
| 5180 | return NULL; |
| 5181 | } |
| 5182 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5183 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5184 | find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5185 | { |
| 5186 | struct nfs4_lockowner *lo; |
| 5187 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5188 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5189 | lo = find_lockowner_str_locked(clp, owner); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5190 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5191 | return lo; |
| 5192 | } |
| 5193 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 5194 | static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) |
| 5195 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5196 | unhash_lockowner_locked(lockowner(sop)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 5197 | } |
| 5198 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 5199 | static void nfs4_free_lockowner(struct nfs4_stateowner *sop) |
| 5200 | { |
| 5201 | struct nfs4_lockowner *lo = lockowner(sop); |
| 5202 | |
| 5203 | kmem_cache_free(lockowner_slab, lo); |
| 5204 | } |
| 5205 | |
| 5206 | static const struct nfs4_stateowner_operations lockowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 5207 | .so_unhash = nfs4_unhash_lockowner, |
| 5208 | .so_free = nfs4_free_lockowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 5209 | }; |
| 5210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | /* |
| 5212 | * Alloc a lock owner structure. |
| 5213 | * 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] | 5214 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | * |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 5216 | * strhashval = ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5218 | static struct nfs4_lockowner * |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5219 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
| 5220 | struct nfs4_ol_stateid *open_stp, |
| 5221 | struct nfsd4_lock *lock) |
| 5222 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5223 | struct nfs4_lockowner *lo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5224 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5225 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 5226 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5227 | return NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5228 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 5229 | lo->lo_owner.so_is_open_owner = 0; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5230 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 5231 | lo->lo_owner.so_ops = &lockowner_ops; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5232 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5233 | ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5234 | if (ret == NULL) { |
| 5235 | list_add(&lo->lo_owner.so_strhash, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5236 | &clp->cl_ownerstr_hashtbl[strhashval]); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5237 | ret = lo; |
| 5238 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 5239 | nfs4_free_stateowner(&lo->lo_owner); |
| 5240 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5241 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 340f0ba | 2015-03-23 11:02:30 -0400 | [diff] [blame] | 5242 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5243 | } |
| 5244 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5245 | static void |
| 5246 | init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, |
| 5247 | struct nfs4_file *fp, struct inode *inode, |
| 5248 | struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | { |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 5250 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5252 | lockdep_assert_held(&clp->cl_lock); |
| 5253 | |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5254 | atomic_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 5255 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 5256 | stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 5257 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5258 | stp->st_stid.sc_file = fp; |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 5259 | stp->st_stid.sc_free = nfs4_free_lock_stateid; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5260 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 5262 | stp->st_openstp = open_stp; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 5263 | list_add(&stp->st_locks, &open_stp->st_locks); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 5264 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 5265 | spin_lock(&fp->fi_lock); |
| 5266 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 5267 | spin_unlock(&fp->fi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | } |
| 5269 | |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5270 | static struct nfs4_ol_stateid * |
| 5271 | find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) |
| 5272 | { |
| 5273 | struct nfs4_ol_stateid *lst; |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5274 | struct nfs4_client *clp = lo->lo_owner.so_client; |
| 5275 | |
| 5276 | lockdep_assert_held(&clp->cl_lock); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5277 | |
| 5278 | list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5279 | if (lst->st_stid.sc_file == fp) { |
| 5280 | atomic_inc(&lst->st_stid.sc_count); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5281 | return lst; |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5282 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5283 | } |
| 5284 | return NULL; |
| 5285 | } |
| 5286 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5287 | static struct nfs4_ol_stateid * |
| 5288 | find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, |
| 5289 | struct inode *inode, struct nfs4_ol_stateid *ost, |
| 5290 | bool *new) |
| 5291 | { |
| 5292 | struct nfs4_stid *ns = NULL; |
| 5293 | struct nfs4_ol_stateid *lst; |
| 5294 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 5295 | struct nfs4_client *clp = oo->oo_owner.so_client; |
| 5296 | |
| 5297 | spin_lock(&clp->cl_lock); |
| 5298 | lst = find_lock_stateid(lo, fi); |
| 5299 | if (lst == NULL) { |
| 5300 | spin_unlock(&clp->cl_lock); |
| 5301 | ns = nfs4_alloc_stid(clp, stateid_slab); |
| 5302 | if (ns == NULL) |
| 5303 | return NULL; |
| 5304 | |
| 5305 | spin_lock(&clp->cl_lock); |
| 5306 | lst = find_lock_stateid(lo, fi); |
| 5307 | if (likely(!lst)) { |
| 5308 | lst = openlockstateid(ns); |
| 5309 | init_lock_stateid(lst, lo, fi, inode, ost); |
| 5310 | ns = NULL; |
| 5311 | *new = true; |
| 5312 | } |
| 5313 | } |
| 5314 | spin_unlock(&clp->cl_lock); |
| 5315 | if (ns) |
| 5316 | nfs4_put_stid(ns); |
| 5317 | return lst; |
| 5318 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5319 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 5320 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | check_lock_length(u64 offset, u64 length) |
| 5322 | { |
Kinglong Mee | e796931 | 2015-07-13 17:34:19 +0800 | [diff] [blame] | 5323 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
| 5324 | (length > ~offset))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | } |
| 5326 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5327 | 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] | 5328 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5329 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5330 | |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5331 | lockdep_assert_held(&fp->fi_lock); |
| 5332 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5333 | if (test_access(access, lock_stp)) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5334 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 5335 | __nfs4_file_get_access(fp, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5336 | set_access(access, lock_stp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5337 | } |
| 5338 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5339 | static __be32 |
| 5340 | lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, |
| 5341 | struct nfs4_ol_stateid *ost, |
| 5342 | struct nfsd4_lock *lock, |
| 5343 | struct nfs4_ol_stateid **lst, bool *new) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5344 | { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5345 | __be32 status; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5346 | struct nfs4_file *fi = ost->st_stid.sc_file; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5347 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 5348 | struct nfs4_client *cl = oo->oo_owner.so_client; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5349 | struct inode *inode = d_inode(cstate->current_fh.fh_dentry); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5350 | struct nfs4_lockowner *lo; |
| 5351 | unsigned int strhashval; |
| 5352 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5353 | lo = find_lockowner_str(cl, &lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5354 | if (!lo) { |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 5355 | strhashval = ownerstr_hashval(&lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5356 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 5357 | if (lo == NULL) |
| 5358 | return nfserr_jukebox; |
| 5359 | } else { |
| 5360 | /* with an existing lockowner, seqids must be the same */ |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5361 | status = nfserr_bad_seqid; |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5362 | if (!cstate->minorversion && |
| 5363 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5364 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5365 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 5366 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 5367 | *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5368 | if (*lst == NULL) { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5369 | status = nfserr_jukebox; |
| 5370 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5371 | } |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5372 | status = nfs_ok; |
| 5373 | out: |
| 5374 | nfs4_put_stateowner(&lo->lo_owner); |
| 5375 | return status; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5376 | } |
| 5377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5378 | /* |
| 5379 | * LOCK operation |
| 5380 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5381 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5382 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 5383 | struct nfsd4_lock *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5384 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5385 | struct nfs4_openowner *open_sop = NULL; |
| 5386 | struct nfs4_lockowner *lock_sop = NULL; |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5387 | struct nfs4_ol_stateid *lock_stp = NULL; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5388 | struct nfs4_ol_stateid *open_stp = NULL; |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5389 | struct nfs4_file *fp; |
J. Bruce Fields | 7d94784 | 2010-08-20 18:09:31 -0400 | [diff] [blame] | 5390 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5391 | struct file_lock *file_lock = NULL; |
| 5392 | struct file_lock *conflock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5393 | __be32 status = 0; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5394 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5395 | int err; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5396 | bool new = false; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5397 | struct net *net = SVC_NET(rqstp); |
| 5398 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | |
| 5400 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 5401 | (long long) lock->lk_offset, |
| 5402 | (long long) lock->lk_length); |
| 5403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 5405 | return nfserr_inval; |
| 5406 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5407 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 5408 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 5409 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 5410 | return status; |
| 5411 | } |
| 5412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5413 | if (lock->lk_is_new) { |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 5414 | if (nfsd4_has_session(cstate)) |
| 5415 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 5416 | memcpy(&lock->lk_new_clientid, |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 5417 | &cstate->session->se_client->cl_clientid, |
| 5418 | sizeof(clientid_t)); |
| 5419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5420 | status = nfserr_stale_clientid; |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 5421 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5422 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5425 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | lock->lk_new_open_seqid, |
| 5427 | &lock->lk_new_open_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5428 | &open_stp, nn); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 5429 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5430 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5431 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5432 | status = nfserr_bad_stateid; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 5433 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 5434 | &lock->lk_new_clientid)) |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5435 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5436 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5437 | &lock_stp, &new); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5438 | } else { |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 5439 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5440 | lock->lk_old_lock_seqid, |
| 5441 | &lock->lk_old_lock_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5442 | NFS4_LOCK_STID, &lock_stp, nn); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5443 | } |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 5444 | if (status) |
| 5445 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 5446 | lock_sop = lockowner(lock_stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5447 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 5448 | lkflg = setlkflg(lock->lk_type); |
| 5449 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 5450 | if (status) |
| 5451 | goto out; |
| 5452 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 5453 | status = nfserr_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5454 | if (locks_in_grace(net) && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 5455 | goto out; |
| 5456 | status = nfserr_no_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5457 | if (!locks_in_grace(net) && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 5458 | goto out; |
| 5459 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5460 | file_lock = locks_alloc_lock(); |
| 5461 | if (!file_lock) { |
| 5462 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5463 | status = nfserr_jukebox; |
| 5464 | goto out; |
| 5465 | } |
| 5466 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5467 | fp = lock_stp->st_stid.sc_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | switch (lock->lk_type) { |
| 5469 | case NFS4_READ_LT: |
| 5470 | case NFS4_READW_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5471 | spin_lock(&fp->fi_lock); |
| 5472 | filp = find_readable_file_locked(fp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5473 | if (filp) |
| 5474 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5475 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5476 | file_lock->fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 5477 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | case NFS4_WRITE_LT: |
| 5479 | case NFS4_WRITEW_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5480 | spin_lock(&fp->fi_lock); |
| 5481 | filp = find_writeable_file_locked(fp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 5482 | if (filp) |
| 5483 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 5484 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5485 | file_lock->fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 5486 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | default: |
| 5488 | status = nfserr_inval; |
| 5489 | goto out; |
| 5490 | } |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5491 | if (!filp) { |
| 5492 | status = nfserr_openmode; |
| 5493 | goto out; |
| 5494 | } |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5495 | |
| 5496 | 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] | 5497 | file_lock->fl_pid = current->tgid; |
| 5498 | file_lock->fl_file = filp; |
| 5499 | file_lock->fl_flags = FL_POSIX; |
| 5500 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 5501 | file_lock->fl_start = lock->lk_offset; |
| 5502 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 5503 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5505 | conflock = locks_alloc_lock(); |
| 5506 | if (!conflock) { |
| 5507 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5508 | status = nfserr_jukebox; |
| 5509 | goto out; |
| 5510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5511 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5512 | err = vfs_lock_file(filp, F_SETLK, file_lock, conflock); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5513 | switch (-err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | case 0: /* success! */ |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5515 | update_stateid(&lock_stp->st_stid.sc_stateid); |
| 5516 | memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5517 | sizeof(stateid_t)); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5518 | status = 0; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 5519 | break; |
| 5520 | case (EAGAIN): /* conflock holds conflicting lock */ |
| 5521 | status = nfserr_denied; |
| 5522 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5523 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 5524 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5525 | case (EDEADLK): |
| 5526 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 5527 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 5528 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 5529 | 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] | 5530 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 5531 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | out: |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5534 | if (filp) |
| 5535 | fput(filp); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5536 | if (lock_stp) { |
| 5537 | /* Bump seqid manually if the 4.0 replay owner is openowner */ |
| 5538 | if (cstate->replay_owner && |
| 5539 | cstate->replay_owner != &lock_sop->lo_owner && |
| 5540 | seqid_mutating_err(ntohl(status))) |
| 5541 | lock_sop->lo_owner.so_seqid++; |
| 5542 | |
| 5543 | /* |
| 5544 | * If this is a new, never-before-used stateid, and we are |
| 5545 | * returning an error, then just go ahead and release it. |
| 5546 | */ |
| 5547 | if (status && new) |
| 5548 | release_lock_stateid(lock_stp); |
| 5549 | |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 5550 | nfs4_put_stid(&lock_stp->st_stid); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5551 | } |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 5552 | if (open_stp) |
| 5553 | nfs4_put_stid(&open_stp->st_stid); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5554 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5555 | if (file_lock) |
| 5556 | locks_free_lock(file_lock); |
| 5557 | if (conflock) |
| 5558 | locks_free_lock(conflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | return status; |
| 5560 | } |
| 5561 | |
| 5562 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 5563 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 5564 | * so we do a temporary open here just to get an open file to pass to |
| 5565 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 5566 | * inode operation.) |
| 5567 | */ |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5568 | 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] | 5569 | { |
| 5570 | struct file *file; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5571 | __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); |
| 5572 | if (!err) { |
| 5573 | err = nfserrno(vfs_test_lock(file, lock)); |
Christoph Hellwig | fd89145 | 2015-04-28 15:41:16 +0200 | [diff] [blame] | 5574 | fput(file); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5575 | } |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 5576 | return err; |
| 5577 | } |
| 5578 | |
| 5579 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5580 | * LOCKT operation |
| 5581 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5582 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5583 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 5584 | struct nfsd4_lockt *lockt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5585 | { |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5586 | struct file_lock *file_lock = NULL; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5587 | struct nfs4_lockowner *lo = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5588 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5589 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5590 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5591 | if (locks_in_grace(SVC_NET(rqstp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | return nfserr_grace; |
| 5593 | |
| 5594 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 5595 | return nfserr_inval; |
| 5596 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5597 | if (!nfsd4_has_session(cstate)) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5598 | status = lookup_clientid(&lockt->lt_clientid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5599 | if (status) |
| 5600 | goto out; |
| 5601 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 5603 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5606 | file_lock = locks_alloc_lock(); |
| 5607 | if (!file_lock) { |
| 5608 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5609 | status = nfserr_jukebox; |
| 5610 | goto out; |
| 5611 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 5612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5613 | switch (lockt->lt_type) { |
| 5614 | case NFS4_READ_LT: |
| 5615 | case NFS4_READW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5616 | file_lock->fl_type = F_RDLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | break; |
| 5618 | case NFS4_WRITE_LT: |
| 5619 | case NFS4_WRITEW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5620 | file_lock->fl_type = F_WRLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5621 | break; |
| 5622 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 5623 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | status = nfserr_inval; |
| 5625 | goto out; |
| 5626 | } |
| 5627 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 5628 | lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5629 | if (lo) |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5630 | file_lock->fl_owner = (fl_owner_t)lo; |
| 5631 | file_lock->fl_pid = current->tgid; |
| 5632 | file_lock->fl_flags = FL_POSIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5634 | file_lock->fl_start = lockt->lt_offset; |
| 5635 | 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] | 5636 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5637 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5639 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5640 | if (status) |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 5641 | goto out; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 5642 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5643 | if (file_lock->fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | status = nfserr_denied; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5645 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | } |
| 5647 | out: |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 5648 | if (lo) |
| 5649 | nfs4_put_stateowner(&lo->lo_owner); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5650 | if (file_lock) |
| 5651 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | return status; |
| 5653 | } |
| 5654 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5655 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5656 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 5657 | struct nfsd4_locku *locku) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5659 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | struct file *filp = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5661 | struct file_lock *file_lock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5662 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5663 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5664 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 5665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 5667 | (long long) locku->lu_offset, |
| 5668 | (long long) locku->lu_length); |
| 5669 | |
| 5670 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 5671 | return nfserr_inval; |
| 5672 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5673 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5674 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 5675 | &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5676 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5677 | goto out; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5678 | filp = find_any_file(stp->st_stid.sc_file); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5679 | if (!filp) { |
| 5680 | status = nfserr_lock_range; |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 5681 | goto put_stateid; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5682 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5683 | file_lock = locks_alloc_lock(); |
| 5684 | if (!file_lock) { |
| 5685 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5686 | status = nfserr_jukebox; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5687 | goto fput; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5688 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 5689 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5690 | file_lock->fl_type = F_UNLCK; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 5691 | 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] | 5692 | file_lock->fl_pid = current->tgid; |
| 5693 | file_lock->fl_file = filp; |
| 5694 | file_lock->fl_flags = FL_POSIX; |
| 5695 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 5696 | file_lock->fl_start = locku->lu_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5698 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 5699 | locku->lu_length); |
| 5700 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5702 | err = vfs_lock_file(filp, F_SETLK, file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5703 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 5704 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | goto out_nfserr; |
| 5706 | } |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5707 | update_stateid(&stp->st_stid.sc_stateid); |
| 5708 | memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5709 | fput: |
| 5710 | fput(filp); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 5711 | put_stateid: |
| 5712 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5714 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 5715 | if (file_lock) |
| 5716 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | return status; |
| 5718 | |
| 5719 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 5720 | status = nfserrno(err); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 5721 | goto fput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | } |
| 5723 | |
| 5724 | /* |
| 5725 | * returns |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5726 | * true: locks held by lockowner |
| 5727 | * false: no locks held by lockowner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5729 | static bool |
| 5730 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | { |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 5732 | struct file_lock *fl; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5733 | int status = false; |
| 5734 | struct file *filp = find_any_file(fp); |
| 5735 | struct inode *inode; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 5736 | struct file_lock_context *flctx; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5737 | |
| 5738 | if (!filp) { |
| 5739 | /* Any valid lock stateid should have some sort of access */ |
| 5740 | WARN_ON_ONCE(1); |
| 5741 | return status; |
| 5742 | } |
| 5743 | |
| 5744 | inode = file_inode(filp); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 5745 | flctx = inode->i_flctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 5747 | if (flctx && !list_empty_careful(&flctx->flc_posix)) { |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 5748 | spin_lock(&flctx->flc_lock); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 5749 | list_for_each_entry(fl, &flctx->flc_posix, fl_list) { |
| 5750 | if (fl->fl_owner == (fl_owner_t)lowner) { |
| 5751 | status = true; |
| 5752 | break; |
| 5753 | } |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 5754 | } |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 5755 | spin_unlock(&flctx->flc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | } |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5757 | fput(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | return status; |
| 5759 | } |
| 5760 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5761 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 5762 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 5763 | struct nfsd4_compound_state *cstate, |
| 5764 | struct nfsd4_release_lockowner *rlockowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5765 | { |
| 5766 | clientid_t *clid = &rlockowner->rl_clientid; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5767 | struct nfs4_stateowner *sop; |
| 5768 | struct nfs4_lockowner *lo = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5769 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5770 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5771 | unsigned int hashval = ownerstr_hashval(owner); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5772 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5773 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5774 | struct nfs4_client *clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5775 | |
| 5776 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 5777 | clid->cl_boot, clid->cl_id); |
| 5778 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5779 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5780 | if (status) |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 5781 | return status; |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5782 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5783 | clp = cstate->clp; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5784 | /* Find the matching lock stateowner */ |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5785 | spin_lock(&clp->cl_lock); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5786 | list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval], |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 5787 | so_strhash) { |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5788 | |
| 5789 | if (sop->so_is_open_owner || !same_owner_str(sop, owner)) |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 5790 | continue; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5791 | |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5792 | /* see if there are still any locks associated with it */ |
| 5793 | lo = lockowner(sop); |
| 5794 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { |
| 5795 | if (check_for_locks(stp->st_stid.sc_file, lo)) { |
| 5796 | status = nfserr_locks_held; |
| 5797 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 5798 | return status; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5799 | } |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 5800 | } |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5801 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 5802 | nfs4_get_stateowner(sop); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5803 | break; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 5804 | } |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 5805 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 5806 | if (lo) |
| 5807 | release_lockowner(lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5808 | return status; |
| 5809 | } |
| 5810 | |
| 5811 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5812 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5813 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5814 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5815 | } |
| 5816 | |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5817 | bool |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5818 | nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5819 | { |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5820 | struct nfs4_client_reclaim *crp; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5821 | |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5822 | crp = nfsd4_find_reclaim_client(name, nn); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5823 | return (crp && crp->cr_clp); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5824 | } |
| 5825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5826 | /* |
| 5827 | * failure => all reset bets are off, nfserr_no_grace... |
| 5828 | */ |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5829 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5830 | nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | { |
| 5832 | unsigned int strhashval; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5833 | struct nfs4_client_reclaim *crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5834 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 5835 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
| 5836 | crp = alloc_reclaim(); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5837 | if (crp) { |
| 5838 | strhashval = clientstr_hashval(name); |
| 5839 | INIT_LIST_HEAD(&crp->cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5840 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5841 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 5842 | crp->cr_clp = NULL; |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5843 | nn->reclaim_str_hashtbl_size++; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 5844 | } |
| 5845 | return crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | } |
| 5847 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 5848 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5849 | 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] | 5850 | { |
| 5851 | list_del(&crp->cr_strhash); |
| 5852 | kfree(crp); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5853 | nn->reclaim_str_hashtbl_size--; |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 5854 | } |
| 5855 | |
| 5856 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5857 | nfs4_release_reclaim(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | { |
| 5859 | struct nfs4_client_reclaim *crp = NULL; |
| 5860 | int i; |
| 5861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5863 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 5864 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5865 | struct nfs4_client_reclaim, cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5866 | nfs4_remove_reclaim_record(crp, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | } |
| 5868 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 5869 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | } |
| 5871 | |
| 5872 | /* |
| 5873 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 5874 | struct nfs4_client_reclaim * |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5875 | nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | { |
| 5877 | unsigned int strhashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5878 | struct nfs4_client_reclaim *crp = NULL; |
| 5879 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5880 | dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5881 | |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5882 | strhashval = clientstr_hashval(recdir); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 5883 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
Jeff Layton | 278c931 | 2012-11-12 15:00:52 -0500 | [diff] [blame] | 5884 | if (same_name(crp->cr_recdir, recdir)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | return crp; |
| 5886 | } |
| 5887 | } |
| 5888 | return NULL; |
| 5889 | } |
| 5890 | |
| 5891 | /* |
| 5892 | * Called from OPEN. Look for clientid in reclaim list. |
| 5893 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5894 | __be32 |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5895 | nfs4_check_open_reclaim(clientid_t *clid, |
| 5896 | struct nfsd4_compound_state *cstate, |
| 5897 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | { |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5899 | __be32 status; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 5900 | |
| 5901 | /* find clientid in conf_id_hashtbl */ |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5902 | status = lookup_clientid(clid, cstate, nn); |
| 5903 | if (status) |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 5904 | return nfserr_reclaim_bad; |
| 5905 | |
Jeff Layton | 3b3e7b7 | 2014-09-12 16:40:22 -0400 | [diff] [blame] | 5906 | if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags)) |
| 5907 | return nfserr_no_grace; |
| 5908 | |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 5909 | if (nfsd4_client_record_check(cstate->clp)) |
| 5910 | return nfserr_reclaim_bad; |
| 5911 | |
| 5912 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5913 | } |
| 5914 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5915 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 5916 | static inline void |
| 5917 | put_client(struct nfs4_client *clp) |
| 5918 | { |
| 5919 | atomic_dec(&clp->cl_refcount); |
| 5920 | } |
| 5921 | |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 5922 | static struct nfs4_client * |
| 5923 | nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) |
| 5924 | { |
| 5925 | struct nfs4_client *clp; |
| 5926 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 5927 | nfsd_net_id); |
| 5928 | |
| 5929 | if (!nfsd_netns_ready(nn)) |
| 5930 | return NULL; |
| 5931 | |
| 5932 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 5933 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) |
| 5934 | return clp; |
| 5935 | } |
| 5936 | return NULL; |
| 5937 | } |
| 5938 | |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 5939 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 5940 | nfsd_inject_print_clients(void) |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 5941 | { |
| 5942 | struct nfs4_client *clp; |
| 5943 | u64 count = 0; |
| 5944 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 5945 | nfsd_net_id); |
| 5946 | char buf[INET6_ADDRSTRLEN]; |
| 5947 | |
| 5948 | if (!nfsd_netns_ready(nn)) |
| 5949 | return 0; |
| 5950 | |
| 5951 | spin_lock(&nn->client_lock); |
| 5952 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 5953 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
| 5954 | pr_info("NFS Client: %s\n", buf); |
| 5955 | ++count; |
| 5956 | } |
| 5957 | spin_unlock(&nn->client_lock); |
| 5958 | |
| 5959 | return count; |
| 5960 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 5961 | |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 5962 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 5963 | nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size) |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 5964 | { |
| 5965 | u64 count = 0; |
| 5966 | struct nfs4_client *clp; |
| 5967 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 5968 | nfsd_net_id); |
| 5969 | |
| 5970 | if (!nfsd_netns_ready(nn)) |
| 5971 | return count; |
| 5972 | |
| 5973 | spin_lock(&nn->client_lock); |
| 5974 | clp = nfsd_find_client(addr, addr_size); |
| 5975 | if (clp) { |
| 5976 | if (mark_client_expired_locked(clp) == nfs_ok) |
| 5977 | ++count; |
| 5978 | else |
| 5979 | clp = NULL; |
| 5980 | } |
| 5981 | spin_unlock(&nn->client_lock); |
| 5982 | |
| 5983 | if (clp) |
| 5984 | expire_client(clp); |
| 5985 | |
| 5986 | return count; |
| 5987 | } |
| 5988 | |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 5989 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 5990 | nfsd_inject_forget_clients(u64 max) |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 5991 | { |
| 5992 | u64 count = 0; |
| 5993 | struct nfs4_client *clp, *next; |
| 5994 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 5995 | nfsd_net_id); |
| 5996 | LIST_HEAD(reaplist); |
| 5997 | |
| 5998 | if (!nfsd_netns_ready(nn)) |
| 5999 | return count; |
| 6000 | |
| 6001 | spin_lock(&nn->client_lock); |
| 6002 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 6003 | if (mark_client_expired_locked(clp) == nfs_ok) { |
| 6004 | list_add(&clp->cl_lru, &reaplist); |
| 6005 | if (max != 0 && ++count >= max) |
| 6006 | break; |
| 6007 | } |
| 6008 | } |
| 6009 | spin_unlock(&nn->client_lock); |
| 6010 | |
| 6011 | list_for_each_entry_safe(clp, next, &reaplist, cl_lru) |
| 6012 | expire_client(clp); |
| 6013 | |
| 6014 | return count; |
| 6015 | } |
| 6016 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6017 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, |
| 6018 | const char *type) |
| 6019 | { |
| 6020 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 6021 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6022 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
| 6023 | } |
| 6024 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6025 | static void |
| 6026 | nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst, |
| 6027 | struct list_head *collect) |
| 6028 | { |
| 6029 | struct nfs4_client *clp = lst->st_stid.sc_client; |
| 6030 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6031 | nfsd_net_id); |
| 6032 | |
| 6033 | if (!collect) |
| 6034 | return; |
| 6035 | |
| 6036 | lockdep_assert_held(&nn->client_lock); |
| 6037 | atomic_inc(&clp->cl_refcount); |
| 6038 | list_add(&lst->st_locks, collect); |
| 6039 | } |
| 6040 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6041 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 6042 | struct list_head *collect, |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6043 | bool (*func)(struct nfs4_ol_stateid *)) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6044 | { |
| 6045 | struct nfs4_openowner *oop; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6046 | struct nfs4_ol_stateid *stp, *st_next; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6047 | struct nfs4_ol_stateid *lst, *lst_next; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6048 | u64 count = 0; |
| 6049 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6050 | spin_lock(&clp->cl_lock); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6051 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6052 | list_for_each_entry_safe(stp, st_next, |
| 6053 | &oop->oo_owner.so_stateids, st_perstateowner) { |
| 6054 | list_for_each_entry_safe(lst, lst_next, |
| 6055 | &stp->st_locks, st_locks) { |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 6056 | if (func) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6057 | if (func(lst)) |
| 6058 | nfsd_inject_add_lock_to_list(lst, |
| 6059 | collect); |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 6060 | } |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6061 | ++count; |
| 6062 | /* |
| 6063 | * Despite the fact that these functions deal |
| 6064 | * with 64-bit integers for "count", we must |
| 6065 | * ensure that it doesn't blow up the |
| 6066 | * clp->cl_refcount. Throw a warning if we |
| 6067 | * start to approach INT_MAX here. |
| 6068 | */ |
| 6069 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 6070 | if (count == max) |
| 6071 | goto out; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6072 | } |
| 6073 | } |
| 6074 | } |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6075 | out: |
| 6076 | spin_unlock(&clp->cl_lock); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6077 | |
| 6078 | return count; |
| 6079 | } |
| 6080 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6081 | static u64 |
| 6082 | nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect, |
| 6083 | u64 max) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6084 | { |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6085 | return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 6086 | } |
| 6087 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6088 | static u64 |
| 6089 | nfsd_print_client_locks(struct nfs4_client *clp) |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6090 | { |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6091 | u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6092 | nfsd_print_count(clp, count, "locked files"); |
| 6093 | return count; |
| 6094 | } |
| 6095 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6096 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6097 | nfsd_inject_print_locks(void) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6098 | { |
| 6099 | struct nfs4_client *clp; |
| 6100 | u64 count = 0; |
| 6101 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6102 | nfsd_net_id); |
| 6103 | |
| 6104 | if (!nfsd_netns_ready(nn)) |
| 6105 | return 0; |
| 6106 | |
| 6107 | spin_lock(&nn->client_lock); |
| 6108 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 6109 | count += nfsd_print_client_locks(clp); |
| 6110 | spin_unlock(&nn->client_lock); |
| 6111 | |
| 6112 | return count; |
| 6113 | } |
| 6114 | |
| 6115 | static void |
| 6116 | nfsd_reap_locks(struct list_head *reaplist) |
| 6117 | { |
| 6118 | struct nfs4_client *clp; |
| 6119 | struct nfs4_ol_stateid *stp, *next; |
| 6120 | |
| 6121 | list_for_each_entry_safe(stp, next, reaplist, st_locks) { |
| 6122 | list_del_init(&stp->st_locks); |
| 6123 | clp = stp->st_stid.sc_client; |
| 6124 | nfs4_put_stid(&stp->st_stid); |
| 6125 | put_client(clp); |
| 6126 | } |
| 6127 | } |
| 6128 | |
| 6129 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6130 | 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] | 6131 | { |
| 6132 | unsigned int count = 0; |
| 6133 | struct nfs4_client *clp; |
| 6134 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6135 | nfsd_net_id); |
| 6136 | LIST_HEAD(reaplist); |
| 6137 | |
| 6138 | if (!nfsd_netns_ready(nn)) |
| 6139 | return count; |
| 6140 | |
| 6141 | spin_lock(&nn->client_lock); |
| 6142 | clp = nfsd_find_client(addr, addr_size); |
| 6143 | if (clp) |
| 6144 | count = nfsd_collect_client_locks(clp, &reaplist, 0); |
| 6145 | spin_unlock(&nn->client_lock); |
| 6146 | nfsd_reap_locks(&reaplist); |
| 6147 | return count; |
| 6148 | } |
| 6149 | |
| 6150 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6151 | nfsd_inject_forget_locks(u64 max) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 6152 | { |
| 6153 | u64 count = 0; |
| 6154 | struct nfs4_client *clp; |
| 6155 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6156 | nfsd_net_id); |
| 6157 | LIST_HEAD(reaplist); |
| 6158 | |
| 6159 | if (!nfsd_netns_ready(nn)) |
| 6160 | return count; |
| 6161 | |
| 6162 | spin_lock(&nn->client_lock); |
| 6163 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6164 | count += nfsd_collect_client_locks(clp, &reaplist, max - count); |
| 6165 | if (max != 0 && count >= max) |
| 6166 | break; |
| 6167 | } |
| 6168 | spin_unlock(&nn->client_lock); |
| 6169 | nfsd_reap_locks(&reaplist); |
| 6170 | return count; |
| 6171 | } |
| 6172 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6173 | static u64 |
| 6174 | nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max, |
| 6175 | struct list_head *collect, |
| 6176 | void (*func)(struct nfs4_openowner *)) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6177 | { |
| 6178 | struct nfs4_openowner *oop, *next; |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6179 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6180 | nfsd_net_id); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6181 | u64 count = 0; |
| 6182 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6183 | lockdep_assert_held(&nn->client_lock); |
| 6184 | |
| 6185 | spin_lock(&clp->cl_lock); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6186 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6187 | if (func) { |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6188 | func(oop); |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6189 | if (collect) { |
| 6190 | atomic_inc(&clp->cl_refcount); |
| 6191 | list_add(&oop->oo_perclient, collect); |
| 6192 | } |
| 6193 | } |
| 6194 | ++count; |
| 6195 | /* |
| 6196 | * Despite the fact that these functions deal with |
| 6197 | * 64-bit integers for "count", we must ensure that |
| 6198 | * it doesn't blow up the clp->cl_refcount. Throw a |
| 6199 | * warning if we start to approach INT_MAX here. |
| 6200 | */ |
| 6201 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 6202 | if (count == max) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6203 | break; |
| 6204 | } |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6205 | spin_unlock(&clp->cl_lock); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6206 | |
| 6207 | return count; |
| 6208 | } |
| 6209 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6210 | static u64 |
| 6211 | nfsd_print_client_openowners(struct nfs4_client *clp) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6212 | { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6213 | u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL); |
| 6214 | |
| 6215 | nfsd_print_count(clp, count, "openowners"); |
| 6216 | return count; |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 6217 | } |
| 6218 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6219 | static u64 |
| 6220 | nfsd_collect_client_openowners(struct nfs4_client *clp, |
| 6221 | struct list_head *collect, u64 max) |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6222 | { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6223 | return nfsd_foreach_client_openowner(clp, max, collect, |
| 6224 | unhash_openowner_locked); |
| 6225 | } |
| 6226 | |
| 6227 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6228 | nfsd_inject_print_openowners(void) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6229 | { |
| 6230 | struct nfs4_client *clp; |
| 6231 | u64 count = 0; |
| 6232 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6233 | nfsd_net_id); |
| 6234 | |
| 6235 | if (!nfsd_netns_ready(nn)) |
| 6236 | return 0; |
| 6237 | |
| 6238 | spin_lock(&nn->client_lock); |
| 6239 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 6240 | count += nfsd_print_client_openowners(clp); |
| 6241 | spin_unlock(&nn->client_lock); |
| 6242 | |
| 6243 | return count; |
| 6244 | } |
| 6245 | |
| 6246 | static void |
| 6247 | nfsd_reap_openowners(struct list_head *reaplist) |
| 6248 | { |
| 6249 | struct nfs4_client *clp; |
| 6250 | struct nfs4_openowner *oop, *next; |
| 6251 | |
| 6252 | list_for_each_entry_safe(oop, next, reaplist, oo_perclient) { |
| 6253 | list_del_init(&oop->oo_perclient); |
| 6254 | clp = oop->oo_owner.so_client; |
| 6255 | release_openowner(oop); |
| 6256 | put_client(clp); |
| 6257 | } |
| 6258 | } |
| 6259 | |
| 6260 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6261 | nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr, |
| 6262 | size_t addr_size) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6263 | { |
| 6264 | unsigned int count = 0; |
| 6265 | struct nfs4_client *clp; |
| 6266 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6267 | nfsd_net_id); |
| 6268 | LIST_HEAD(reaplist); |
| 6269 | |
| 6270 | if (!nfsd_netns_ready(nn)) |
| 6271 | return count; |
| 6272 | |
| 6273 | spin_lock(&nn->client_lock); |
| 6274 | clp = nfsd_find_client(addr, addr_size); |
| 6275 | if (clp) |
| 6276 | count = nfsd_collect_client_openowners(clp, &reaplist, 0); |
| 6277 | spin_unlock(&nn->client_lock); |
| 6278 | nfsd_reap_openowners(&reaplist); |
| 6279 | return count; |
| 6280 | } |
| 6281 | |
| 6282 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6283 | nfsd_inject_forget_openowners(u64 max) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 6284 | { |
| 6285 | u64 count = 0; |
| 6286 | struct nfs4_client *clp; |
| 6287 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6288 | nfsd_net_id); |
| 6289 | LIST_HEAD(reaplist); |
| 6290 | |
| 6291 | if (!nfsd_netns_ready(nn)) |
| 6292 | return count; |
| 6293 | |
| 6294 | spin_lock(&nn->client_lock); |
| 6295 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6296 | count += nfsd_collect_client_openowners(clp, &reaplist, |
| 6297 | max - count); |
| 6298 | if (max != 0 && count >= max) |
| 6299 | break; |
| 6300 | } |
| 6301 | spin_unlock(&nn->client_lock); |
| 6302 | nfsd_reap_openowners(&reaplist); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6303 | return count; |
| 6304 | } |
| 6305 | |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6306 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
| 6307 | struct list_head *victims) |
| 6308 | { |
| 6309 | struct nfs4_delegation *dp, *next; |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6310 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6311 | nfsd_net_id); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6312 | u64 count = 0; |
| 6313 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6314 | lockdep_assert_held(&nn->client_lock); |
| 6315 | |
| 6316 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6317 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 6318 | if (victims) { |
| 6319 | /* |
| 6320 | * It's not safe to mess with delegations that have a |
| 6321 | * non-zero dl_time. They might have already been broken |
| 6322 | * and could be processed by the laundromat outside of |
| 6323 | * the state_lock. Just leave them be. |
| 6324 | */ |
| 6325 | if (dp->dl_time != 0) |
| 6326 | continue; |
| 6327 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6328 | atomic_inc(&clp->cl_refcount); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 6329 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 6330 | list_add(&dp->dl_recall_lru, victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 6331 | } |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6332 | ++count; |
| 6333 | /* |
| 6334 | * Despite the fact that these functions deal with |
| 6335 | * 64-bit integers for "count", we must ensure that |
| 6336 | * it doesn't blow up the clp->cl_refcount. Throw a |
| 6337 | * warning if we start to approach INT_MAX here. |
| 6338 | */ |
| 6339 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 6340 | if (count == max) |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6341 | break; |
| 6342 | } |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6343 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6344 | return count; |
| 6345 | } |
| 6346 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6347 | static u64 |
| 6348 | nfsd_print_client_delegations(struct nfs4_client *clp) |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 6349 | { |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6350 | u64 count = nfsd_find_all_delegations(clp, 0, NULL); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 6351 | |
| 6352 | nfsd_print_count(clp, count, "delegations"); |
| 6353 | return count; |
| 6354 | } |
| 6355 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6356 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6357 | nfsd_inject_print_delegations(void) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6358 | { |
| 6359 | struct nfs4_client *clp; |
| 6360 | u64 count = 0; |
| 6361 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6362 | nfsd_net_id); |
| 6363 | |
| 6364 | if (!nfsd_netns_ready(nn)) |
| 6365 | return 0; |
| 6366 | |
| 6367 | spin_lock(&nn->client_lock); |
| 6368 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 6369 | count += nfsd_print_client_delegations(clp); |
| 6370 | spin_unlock(&nn->client_lock); |
| 6371 | |
| 6372 | return count; |
| 6373 | } |
| 6374 | |
| 6375 | static void |
| 6376 | nfsd_forget_delegations(struct list_head *reaplist) |
| 6377 | { |
| 6378 | struct nfs4_client *clp; |
| 6379 | struct nfs4_delegation *dp, *next; |
| 6380 | |
| 6381 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 6382 | list_del_init(&dp->dl_recall_lru); |
| 6383 | clp = dp->dl_stid.sc_client; |
| 6384 | revoke_delegation(dp); |
| 6385 | put_client(clp); |
| 6386 | } |
| 6387 | } |
| 6388 | |
| 6389 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6390 | nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr, |
| 6391 | size_t addr_size) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6392 | { |
| 6393 | u64 count = 0; |
| 6394 | struct nfs4_client *clp; |
| 6395 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6396 | nfsd_net_id); |
| 6397 | LIST_HEAD(reaplist); |
| 6398 | |
| 6399 | if (!nfsd_netns_ready(nn)) |
| 6400 | return count; |
| 6401 | |
| 6402 | spin_lock(&nn->client_lock); |
| 6403 | clp = nfsd_find_client(addr, addr_size); |
| 6404 | if (clp) |
| 6405 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 6406 | spin_unlock(&nn->client_lock); |
| 6407 | |
| 6408 | nfsd_forget_delegations(&reaplist); |
| 6409 | return count; |
| 6410 | } |
| 6411 | |
| 6412 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6413 | nfsd_inject_forget_delegations(u64 max) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6414 | { |
| 6415 | u64 count = 0; |
| 6416 | struct nfs4_client *clp; |
| 6417 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6418 | nfsd_net_id); |
| 6419 | LIST_HEAD(reaplist); |
| 6420 | |
| 6421 | if (!nfsd_netns_ready(nn)) |
| 6422 | return count; |
| 6423 | |
| 6424 | spin_lock(&nn->client_lock); |
| 6425 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6426 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 6427 | if (max != 0 && count >= max) |
| 6428 | break; |
| 6429 | } |
| 6430 | spin_unlock(&nn->client_lock); |
| 6431 | nfsd_forget_delegations(&reaplist); |
| 6432 | return count; |
| 6433 | } |
| 6434 | |
| 6435 | static void |
| 6436 | nfsd_recall_delegations(struct list_head *reaplist) |
| 6437 | { |
| 6438 | struct nfs4_client *clp; |
| 6439 | struct nfs4_delegation *dp, *next; |
| 6440 | |
| 6441 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 6442 | list_del_init(&dp->dl_recall_lru); |
| 6443 | clp = dp->dl_stid.sc_client; |
| 6444 | /* |
| 6445 | * We skipped all entries that had a zero dl_time before, |
| 6446 | * so we can now reset the dl_time back to 0. If a delegation |
| 6447 | * break comes in now, then it won't make any difference since |
| 6448 | * we're recalling it either way. |
| 6449 | */ |
| 6450 | spin_lock(&state_lock); |
| 6451 | dp->dl_time = 0; |
| 6452 | spin_unlock(&state_lock); |
| 6453 | nfsd_break_one_deleg(dp); |
| 6454 | put_client(clp); |
| 6455 | } |
| 6456 | } |
| 6457 | |
| 6458 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6459 | nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr, |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6460 | size_t addr_size) |
| 6461 | { |
| 6462 | u64 count = 0; |
| 6463 | struct nfs4_client *clp; |
| 6464 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6465 | nfsd_net_id); |
| 6466 | LIST_HEAD(reaplist); |
| 6467 | |
| 6468 | if (!nfsd_netns_ready(nn)) |
| 6469 | return count; |
| 6470 | |
| 6471 | spin_lock(&nn->client_lock); |
| 6472 | clp = nfsd_find_client(addr, addr_size); |
| 6473 | if (clp) |
| 6474 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 6475 | spin_unlock(&nn->client_lock); |
| 6476 | |
| 6477 | nfsd_recall_delegations(&reaplist); |
| 6478 | return count; |
| 6479 | } |
| 6480 | |
| 6481 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 6482 | nfsd_inject_recall_delegations(u64 max) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 6483 | { |
| 6484 | u64 count = 0; |
| 6485 | struct nfs4_client *clp, *next; |
| 6486 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6487 | nfsd_net_id); |
| 6488 | LIST_HEAD(reaplist); |
| 6489 | |
| 6490 | if (!nfsd_netns_ready(nn)) |
| 6491 | return count; |
| 6492 | |
| 6493 | spin_lock(&nn->client_lock); |
| 6494 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 6495 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 6496 | if (max != 0 && ++count >= max) |
| 6497 | break; |
| 6498 | } |
| 6499 | spin_unlock(&nn->client_lock); |
| 6500 | nfsd_recall_delegations(&reaplist); |
| 6501 | return count; |
| 6502 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 6503 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
| 6504 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 6505 | /* |
| 6506 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 6507 | * client may quite reasonably hang on to a delegation as long as it has |
| 6508 | * the inode cached. This becomes an obvious problem the first time a |
| 6509 | * client's inode cache approaches the size of the server's total memory. |
| 6510 | * |
| 6511 | * For now we avoid this problem by imposing a hard limit on the number |
| 6512 | * of delegations, which varies according to the server's memory size. |
| 6513 | */ |
| 6514 | static void |
| 6515 | set_max_delegations(void) |
| 6516 | { |
| 6517 | /* |
| 6518 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 6519 | * estimates suggest that in the worst case (where every delegation |
| 6520 | * is for a different inode), a delegation could take about 1.5K, |
| 6521 | * giving a worst case usage of about 6% of memory. |
| 6522 | */ |
| 6523 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 6524 | } |
| 6525 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6526 | static int nfs4_state_create_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6527 | { |
| 6528 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 6529 | int i; |
| 6530 | |
| 6531 | nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 6532 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 6533 | if (!nn->conf_id_hashtbl) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 6534 | goto err; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 6535 | nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 6536 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 6537 | if (!nn->unconf_id_hashtbl) |
| 6538 | goto err_unconf_id; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 6539 | nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) * |
| 6540 | SESSION_HASH_SIZE, GFP_KERNEL); |
| 6541 | if (!nn->sessionid_hashtbl) |
| 6542 | goto err_sessionid; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6543 | |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 6544 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6545 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 6546 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 6547 | } |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 6548 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 6549 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 6550 | nn->conf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 6551 | nn->unconf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 6552 | INIT_LIST_HEAD(&nn->client_lru); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 6553 | INIT_LIST_HEAD(&nn->close_lru); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 6554 | INIT_LIST_HEAD(&nn->del_recall_lru); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 6555 | spin_lock_init(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6556 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 6557 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6558 | get_net(net); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 6559 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6560 | return 0; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 6561 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 6562 | err_sessionid: |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 6563 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 6564 | err_unconf_id: |
| 6565 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 6566 | err: |
| 6567 | return -ENOMEM; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6568 | } |
| 6569 | |
| 6570 | static void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6571 | nfs4_state_destroy_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6572 | { |
| 6573 | int i; |
| 6574 | struct nfs4_client *clp = NULL; |
| 6575 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 6576 | |
| 6577 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 6578 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 6579 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 6580 | destroy_client(clp); |
| 6581 | } |
| 6582 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 6583 | |
Kinglong Mee | 2b90563 | 2014-03-26 22:09:30 +0800 | [diff] [blame] | 6584 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 6585 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 6586 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 6587 | destroy_client(clp); |
| 6588 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 6589 | } |
| 6590 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 6591 | kfree(nn->sessionid_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 6592 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6593 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6594 | put_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6595 | } |
| 6596 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 6597 | int |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6598 | nfs4_state_start_net(struct net *net) |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 6599 | { |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 6600 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6601 | int ret; |
| 6602 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6603 | ret = nfs4_state_create_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 6604 | if (ret) |
| 6605 | return ret; |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 6606 | nn->boot_time = get_seconds(); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 6607 | nn->grace_ended = false; |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 6608 | nn->nfsd4_manager.block_opens = true; |
Jeff Layton | d4318ac | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 6609 | locks_start_grace(net, &nn->nfsd4_manager); |
| 6610 | nfsd4_client_tracking_init(net); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6611 | printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n", |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 6612 | nn->nfsd4_grace, net); |
| 6613 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6614 | return 0; |
| 6615 | } |
| 6616 | |
| 6617 | /* initialization to perform when the nfsd service is started: */ |
| 6618 | |
| 6619 | int |
| 6620 | nfs4_state_start(void) |
| 6621 | { |
| 6622 | int ret; |
| 6623 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6624 | ret = set_callback_cred(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6625 | if (ret) |
| 6626 | return -ENOMEM; |
Jeff Layton | 51a5456 | 2015-08-20 07:17:01 -0400 | [diff] [blame] | 6627 | laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 6628 | if (laundry_wq == NULL) { |
| 6629 | ret = -ENOMEM; |
| 6630 | goto out_recovery; |
| 6631 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6632 | ret = nfsd4_create_callback_queue(); |
| 6633 | if (ret) |
| 6634 | goto out_free_laundry; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 6635 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 6636 | set_max_delegations(); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6637 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6638 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 6639 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6640 | out_free_laundry: |
| 6641 | destroy_workqueue(laundry_wq); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 6642 | out_recovery: |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 6643 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6644 | } |
| 6645 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 6646 | void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6647 | nfs4_state_shutdown_net(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6649 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6650 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6651 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6652 | |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6653 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 6654 | locks_end_grace(&nn->nfsd4_manager); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 6655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6656 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 6657 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 6658 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6659 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 6660 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 6661 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | } |
Benny Halevy | cdc9750 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 6663 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6664 | list_for_each_safe(pos, next, &reaplist) { |
| 6665 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 6666 | list_del_init(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 6667 | put_clnt_odstate(dp->dl_clnt_odstate); |
Jeff Layton | afbda40 | 2014-08-09 10:22:41 -0400 | [diff] [blame] | 6668 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 6669 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6670 | } |
| 6671 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6672 | nfsd4_client_tracking_exit(net); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 6673 | nfs4_state_destroy_net(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6674 | } |
| 6675 | |
| 6676 | void |
| 6677 | nfs4_state_shutdown(void) |
| 6678 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 6679 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 6680 | nfsd4_destroy_callback_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6681 | } |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6682 | |
| 6683 | static void |
| 6684 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 6685 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 6686 | if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid)) |
| 6687 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6688 | } |
| 6689 | |
| 6690 | static void |
| 6691 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 6692 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 6693 | if (cstate->minorversion) { |
| 6694 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
| 6695 | SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
| 6696 | } |
| 6697 | } |
| 6698 | |
| 6699 | void |
| 6700 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 6701 | { |
| 6702 | CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6703 | } |
| 6704 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6705 | /* |
| 6706 | * functions to set current state id |
| 6707 | */ |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6708 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 6709 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 6710 | { |
| 6711 | put_stateid(cstate, &odp->od_stateid); |
| 6712 | } |
| 6713 | |
| 6714 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6715 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open) |
| 6716 | { |
| 6717 | put_stateid(cstate, &open->op_stateid); |
| 6718 | } |
| 6719 | |
| 6720 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6721 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 6722 | { |
| 6723 | put_stateid(cstate, &close->cl_stateid); |
| 6724 | } |
| 6725 | |
| 6726 | void |
| 6727 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock) |
| 6728 | { |
| 6729 | put_stateid(cstate, &lock->lk_resp_stateid); |
| 6730 | } |
| 6731 | |
| 6732 | /* |
| 6733 | * functions to consume current state id |
| 6734 | */ |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 6735 | |
| 6736 | void |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 6737 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 6738 | { |
| 6739 | get_stateid(cstate, &odp->od_stateid); |
| 6740 | } |
| 6741 | |
| 6742 | void |
| 6743 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp) |
| 6744 | { |
| 6745 | get_stateid(cstate, &drp->dr_stateid); |
| 6746 | } |
| 6747 | |
| 6748 | void |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 6749 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp) |
| 6750 | { |
| 6751 | get_stateid(cstate, &fsp->fr_stateid); |
| 6752 | } |
| 6753 | |
| 6754 | void |
| 6755 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr) |
| 6756 | { |
| 6757 | get_stateid(cstate, &setattr->sa_stateid); |
| 6758 | } |
| 6759 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6760 | void |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6761 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 6762 | { |
| 6763 | get_stateid(cstate, &close->cl_stateid); |
| 6764 | } |
| 6765 | |
| 6766 | void |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6767 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6768 | { |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 6769 | get_stateid(cstate, &locku->lu_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 6770 | } |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 6771 | |
| 6772 | void |
| 6773 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read) |
| 6774 | { |
| 6775 | get_stateid(cstate, &read->rd_stateid); |
| 6776 | } |
| 6777 | |
| 6778 | void |
| 6779 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write) |
| 6780 | { |
| 6781 | get_stateid(cstate, &write->wr_stateid); |
| 6782 | } |