Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame^] | 1 | /* |
| 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 | |
| 35 | #include <linux/file.h> |
| 36 | #include <linux/fs.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <linux/namei.h> |
| 39 | #include <linux/swap.h> |
| 40 | #include <linux/pagemap.h> |
| 41 | #include <linux/ratelimit.h> |
| 42 | #include <linux/sunrpc/svcauth_gss.h> |
| 43 | #include <linux/sunrpc/addr.h> |
| 44 | #include <linux/jhash.h> |
| 45 | #include "xdr4.h" |
| 46 | #include "xdr4cb.h" |
| 47 | #include "vfs.h" |
| 48 | #include "current_stateid.h" |
| 49 | |
| 50 | #include "netns.h" |
| 51 | #include "pnfs.h" |
| 52 | |
| 53 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 54 | |
| 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 | }; |
| 63 | static const stateid_t currentstateid = { |
| 64 | .si_generation = 1, |
| 65 | }; |
| 66 | |
| 67 | static u64 current_sessionid = 1; |
| 68 | |
| 69 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
| 70 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) |
| 71 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
| 72 | |
| 73 | /* forward declarations */ |
| 74 | static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner); |
| 75 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid); |
| 76 | |
| 77 | /* Locking: */ |
| 78 | |
| 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 | */ |
| 84 | static DEFINE_SPINLOCK(state_lock); |
| 85 | |
| 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 | |
| 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; |
| 97 | static struct kmem_cache *odstate_slab; |
| 98 | |
| 99 | static void free_session(struct nfsd4_session *); |
| 100 | |
| 101 | static struct nfsd4_callback_ops nfsd4_cb_recall_ops; |
| 102 | |
| 103 | static bool is_session_dead(struct nfsd4_session *ses) |
| 104 | { |
| 105 | return ses->se_flags & NFS4_SESSION_DEAD; |
| 106 | } |
| 107 | |
| 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) |
| 111 | return nfserr_jukebox; |
| 112 | ses->se_flags |= NFS4_SESSION_DEAD; |
| 113 | return nfs_ok; |
| 114 | } |
| 115 | |
| 116 | static bool is_client_expired(struct nfs4_client *clp) |
| 117 | { |
| 118 | return clp->cl_time == 0; |
| 119 | } |
| 120 | |
| 121 | static __be32 get_client_locked(struct nfs4_client *clp) |
| 122 | { |
| 123 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 124 | |
| 125 | lockdep_assert_held(&nn->client_lock); |
| 126 | |
| 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 | |
| 155 | static void put_client_renew_locked(struct nfs4_client *clp) |
| 156 | { |
| 157 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 158 | |
| 159 | lockdep_assert_held(&nn->client_lock); |
| 160 | |
| 161 | if (!atomic_dec_and_test(&clp->cl_refcount)) |
| 162 | return; |
| 163 | if (!is_client_expired(clp)) |
| 164 | renew_client_locked(clp); |
| 165 | } |
| 166 | |
| 167 | static void put_client_renew(struct nfs4_client *clp) |
| 168 | { |
| 169 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 170 | |
| 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); |
| 175 | spin_unlock(&nn->client_lock); |
| 176 | } |
| 177 | |
| 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; |
| 194 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 195 | |
| 196 | lockdep_assert_held(&nn->client_lock); |
| 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 | |
| 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 | |
| 220 | static int |
| 221 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) |
| 222 | { |
| 223 | return (sop->so_owner.len == owner->len) && |
| 224 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len); |
| 225 | } |
| 226 | |
| 227 | static struct nfs4_openowner * |
| 228 | find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open, |
| 229 | struct nfs4_client *clp) |
| 230 | { |
| 231 | struct nfs4_stateowner *so; |
| 232 | |
| 233 | lockdep_assert_held(&clp->cl_lock); |
| 234 | |
| 235 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], |
| 236 | so_strhash) { |
| 237 | if (!so->so_is_open_owner) |
| 238 | continue; |
| 239 | if (same_owner_str(so, &open->op_owner)) |
| 240 | return openowner(nfs4_get_stateowner(so)); |
| 241 | } |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static struct nfs4_openowner * |
| 246 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
| 247 | struct nfs4_client *clp) |
| 248 | { |
| 249 | struct nfs4_openowner *oo; |
| 250 | |
| 251 | spin_lock(&clp->cl_lock); |
| 252 | oo = find_openstateowner_str_locked(hashval, open, clp); |
| 253 | spin_unlock(&clp->cl_lock); |
| 254 | return oo; |
| 255 | } |
| 256 | |
| 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 | |
| 270 | static void nfsd4_free_file_rcu(struct rcu_head *rcu) |
| 271 | { |
| 272 | struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); |
| 273 | |
| 274 | kmem_cache_free(file_slab, fp); |
| 275 | } |
| 276 | |
| 277 | void |
| 278 | put_nfs4_file(struct nfs4_file *fi) |
| 279 | { |
| 280 | might_lock(&state_lock); |
| 281 | |
| 282 | if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) { |
| 283 | hlist_del_rcu(&fi->fi_hash); |
| 284 | spin_unlock(&state_lock); |
| 285 | WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); |
| 286 | WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); |
| 287 | call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu); |
| 288 | } |
| 289 | } |
| 290 | |
| 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 | |
| 348 | struct file * |
| 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 | |
| 364 | static atomic_long_t num_delegations; |
| 365 | unsigned long max_delegations; |
| 366 | |
| 367 | /* |
| 368 | * Open owner state (share locks) |
| 369 | */ |
| 370 | |
| 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) |
| 375 | |
| 376 | static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) |
| 377 | { |
| 378 | unsigned int ret; |
| 379 | |
| 380 | ret = opaque_hashval(ownername->data, ownername->len); |
| 381 | return ret & OWNER_HASH_MASK; |
| 382 | } |
| 383 | |
| 384 | /* hash table for nfs4_file */ |
| 385 | #define FILE_HASH_BITS 8 |
| 386 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
| 387 | |
| 388 | static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh) |
| 389 | { |
| 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 | |
| 398 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
| 399 | |
| 400 | static void |
| 401 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
| 402 | { |
| 403 | lockdep_assert_held(&fp->fi_lock); |
| 404 | |
| 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]); |
| 409 | } |
| 410 | |
| 411 | static __be32 |
| 412 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
| 413 | { |
| 414 | lockdep_assert_held(&fp->fi_lock); |
| 415 | |
| 416 | /* Does this access mode make sense? */ |
| 417 | if (access & ~NFS4_SHARE_ACCESS_BOTH) |
| 418 | return nfserr_inval; |
| 419 | |
| 420 | /* Does it conflict with a deny mode already set? */ |
| 421 | if ((access & fp->fi_share_deny) != 0) |
| 422 | return nfserr_share_denied; |
| 423 | |
| 424 | __nfs4_file_get_access(fp, access); |
| 425 | return nfs_ok; |
| 426 | } |
| 427 | |
| 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 | |
| 447 | static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag) |
| 448 | { |
| 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 | |
| 455 | swap(f1, fp->fi_fds[oflag]); |
| 456 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
| 457 | swap(f2, fp->fi_fds[O_RDWR]); |
| 458 | spin_unlock(&fp->fi_lock); |
| 459 | if (f1) |
| 460 | fput(f1); |
| 461 | if (f2) |
| 462 | fput(f2); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static void nfs4_file_put_access(struct nfs4_file *fp, u32 access) |
| 467 | { |
| 468 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
| 469 | |
| 470 | if (access & NFS4_SHARE_ACCESS_WRITE) |
| 471 | __nfs4_file_put_access(fp, O_WRONLY); |
| 472 | if (access & NFS4_SHARE_ACCESS_READ) |
| 473 | __nfs4_file_put_access(fp, O_RDONLY); |
| 474 | } |
| 475 | |
| 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 | |
| 556 | struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab, |
| 557 | void (*sc_free)(struct nfs4_stid *)) |
| 558 | { |
| 559 | struct nfs4_stid *stid; |
| 560 | int new_id; |
| 561 | |
| 562 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
| 563 | if (!stid) |
| 564 | return NULL; |
| 565 | |
| 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(); |
| 571 | if (new_id < 0) |
| 572 | goto out_free; |
| 573 | |
| 574 | stid->sc_free = sc_free; |
| 575 | stid->sc_client = cl; |
| 576 | stid->sc_stateid.si_opaque.so_id = new_id; |
| 577 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; |
| 578 | /* Will be incremented before return to client: */ |
| 579 | atomic_set(&stid->sc_count, 1); |
| 580 | spin_lock_init(&stid->sc_lock); |
| 581 | |
| 582 | /* |
| 583 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 584 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 585 | * example, a stray write retransmission could be accepted by |
| 586 | * the server when it should have been rejected. Therefore, |
| 587 | * adopt a trick from the sctp code to attempt to maximize the |
| 588 | * amount of time until an id is reused, by ensuring they always |
| 589 | * "increase" (mod INT_MAX): |
| 590 | */ |
| 591 | return stid; |
| 592 | out_free: |
| 593 | kmem_cache_free(slab, stid); |
| 594 | return NULL; |
| 595 | } |
| 596 | |
| 597 | static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp) |
| 598 | { |
| 599 | struct nfs4_stid *stid; |
| 600 | |
| 601 | stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid); |
| 602 | if (!stid) |
| 603 | return NULL; |
| 604 | |
| 605 | return openlockstateid(stid); |
| 606 | } |
| 607 | |
| 608 | static void nfs4_free_deleg(struct nfs4_stid *stid) |
| 609 | { |
| 610 | kmem_cache_free(deleg_slab, stid); |
| 611 | atomic_long_dec(&num_delegations); |
| 612 | } |
| 613 | |
| 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 | * |
| 628 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
| 629 | * is used to manage concurrent access. Testing does not need the lock |
| 630 | * except when swapping the two filters. |
| 631 | */ |
| 632 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
| 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) { |
| 648 | spin_lock(&blocked_delegations_lock); |
| 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 | } |
| 657 | spin_unlock(&blocked_delegations_lock); |
| 658 | } |
| 659 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
| 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 | |
| 678 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
| 679 | |
| 680 | spin_lock(&blocked_delegations_lock); |
| 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; |
| 687 | spin_unlock(&blocked_delegations_lock); |
| 688 | } |
| 689 | |
| 690 | static struct nfs4_delegation * |
| 691 | alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh, |
| 692 | struct nfs4_clnt_odstate *odstate) |
| 693 | { |
| 694 | struct nfs4_delegation *dp; |
| 695 | long n; |
| 696 | |
| 697 | dprintk("NFSD alloc_init_deleg\n"); |
| 698 | n = atomic_long_inc_return(&num_delegations); |
| 699 | if (n < 0 || n > max_delegations) |
| 700 | goto out_dec; |
| 701 | if (delegation_blocked(¤t_fh->fh_handle)) |
| 702 | goto out_dec; |
| 703 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg)); |
| 704 | if (dp == NULL) |
| 705 | goto out_dec; |
| 706 | |
| 707 | /* |
| 708 | * delegation seqid's are never incremented. The 4.1 special |
| 709 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 710 | * 0 anyway just for consistency and use 1: |
| 711 | */ |
| 712 | dp->dl_stid.sc_stateid.si_generation = 1; |
| 713 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 714 | INIT_LIST_HEAD(&dp->dl_perclnt); |
| 715 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
| 716 | dp->dl_clnt_odstate = odstate; |
| 717 | get_clnt_odstate(odstate); |
| 718 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
| 719 | dp->dl_retries = 1; |
| 720 | nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, |
| 721 | &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); |
| 722 | return dp; |
| 723 | out_dec: |
| 724 | atomic_long_dec(&num_delegations); |
| 725 | return NULL; |
| 726 | } |
| 727 | |
| 728 | void |
| 729 | nfs4_put_stid(struct nfs4_stid *s) |
| 730 | { |
| 731 | struct nfs4_file *fp = s->sc_file; |
| 732 | struct nfs4_client *clp = s->sc_client; |
| 733 | |
| 734 | might_lock(&clp->cl_lock); |
| 735 | |
| 736 | if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) { |
| 737 | wake_up_all(&close_wq); |
| 738 | return; |
| 739 | } |
| 740 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
| 741 | spin_unlock(&clp->cl_lock); |
| 742 | s->sc_free(s); |
| 743 | if (fp) |
| 744 | put_nfs4_file(fp); |
| 745 | } |
| 746 | |
| 747 | void |
| 748 | nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) |
| 749 | { |
| 750 | stateid_t *src = &stid->sc_stateid; |
| 751 | |
| 752 | spin_lock(&stid->sc_lock); |
| 753 | if (unlikely(++src->si_generation == 0)) |
| 754 | src->si_generation = 1; |
| 755 | memcpy(dst, src, sizeof(*dst)); |
| 756 | spin_unlock(&stid->sc_lock); |
| 757 | } |
| 758 | |
| 759 | static void nfs4_put_deleg_lease(struct nfs4_file *fp) |
| 760 | { |
| 761 | struct file *filp = NULL; |
| 762 | |
| 763 | spin_lock(&fp->fi_lock); |
| 764 | if (fp->fi_deleg_file && --fp->fi_delegees == 0) |
| 765 | swap(filp, fp->fi_deleg_file); |
| 766 | spin_unlock(&fp->fi_lock); |
| 767 | |
| 768 | if (filp) { |
| 769 | vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp); |
| 770 | fput(filp); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | void nfs4_unhash_stid(struct nfs4_stid *s) |
| 775 | { |
| 776 | s->sc_type = 0; |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * nfs4_get_existing_delegation - Discover if this delegation already exists |
| 781 | * @clp: a pointer to the nfs4_client we're granting a delegation to |
| 782 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 783 | * |
| 784 | * Return: |
| 785 | * On success: NULL if an existing delegation was not found. |
| 786 | * |
| 787 | * On error: -EAGAIN if one was previously granted to this nfs4_client |
| 788 | * for this nfs4_file. |
| 789 | * |
| 790 | */ |
| 791 | |
| 792 | static int |
| 793 | nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp) |
| 794 | { |
| 795 | struct nfs4_delegation *searchdp = NULL; |
| 796 | struct nfs4_client *searchclp = NULL; |
| 797 | |
| 798 | lockdep_assert_held(&state_lock); |
| 799 | lockdep_assert_held(&fp->fi_lock); |
| 800 | |
| 801 | list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { |
| 802 | searchclp = searchdp->dl_stid.sc_client; |
| 803 | if (clp == searchclp) { |
| 804 | return -EAGAIN; |
| 805 | } |
| 806 | } |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * hash_delegation_locked - Add a delegation to the appropriate lists |
| 812 | * @dp: a pointer to the nfs4_delegation we are adding. |
| 813 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 814 | * |
| 815 | * Return: |
| 816 | * On success: NULL if the delegation was successfully hashed. |
| 817 | * |
| 818 | * On error: -EAGAIN if one was previously granted to this |
| 819 | * nfs4_client for this nfs4_file. Delegation is not hashed. |
| 820 | * |
| 821 | */ |
| 822 | |
| 823 | static int |
| 824 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 825 | { |
| 826 | int status; |
| 827 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 828 | |
| 829 | lockdep_assert_held(&state_lock); |
| 830 | lockdep_assert_held(&fp->fi_lock); |
| 831 | |
| 832 | status = nfs4_get_existing_delegation(clp, fp); |
| 833 | if (status) |
| 834 | return status; |
| 835 | ++fp->fi_delegees; |
| 836 | atomic_inc(&dp->dl_stid.sc_count); |
| 837 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
| 838 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
| 839 | list_add(&dp->dl_perclnt, &clp->cl_delegations); |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | static bool |
| 844 | unhash_delegation_locked(struct nfs4_delegation *dp) |
| 845 | { |
| 846 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
| 847 | |
| 848 | lockdep_assert_held(&state_lock); |
| 849 | |
| 850 | if (list_empty(&dp->dl_perfile)) |
| 851 | return false; |
| 852 | |
| 853 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
| 854 | /* Ensure that deleg break won't try to requeue it */ |
| 855 | ++dp->dl_time; |
| 856 | spin_lock(&fp->fi_lock); |
| 857 | list_del_init(&dp->dl_perclnt); |
| 858 | list_del_init(&dp->dl_recall_lru); |
| 859 | list_del_init(&dp->dl_perfile); |
| 860 | spin_unlock(&fp->fi_lock); |
| 861 | return true; |
| 862 | } |
| 863 | |
| 864 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 865 | { |
| 866 | bool unhashed; |
| 867 | |
| 868 | spin_lock(&state_lock); |
| 869 | unhashed = unhash_delegation_locked(dp); |
| 870 | spin_unlock(&state_lock); |
| 871 | if (unhashed) { |
| 872 | put_clnt_odstate(dp->dl_clnt_odstate); |
| 873 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
| 874 | nfs4_put_stid(&dp->dl_stid); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 879 | { |
| 880 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 881 | |
| 882 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
| 883 | |
| 884 | put_clnt_odstate(dp->dl_clnt_odstate); |
| 885 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
| 886 | |
| 887 | if (clp->cl_minorversion == 0) |
| 888 | nfs4_put_stid(&dp->dl_stid); |
| 889 | else { |
| 890 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
| 891 | spin_lock(&clp->cl_lock); |
| 892 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 893 | spin_unlock(&clp->cl_lock); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | /* |
| 898 | * SETCLIENTID state |
| 899 | */ |
| 900 | |
| 901 | static unsigned int clientid_hashval(u32 id) |
| 902 | { |
| 903 | return id & CLIENT_HASH_MASK; |
| 904 | } |
| 905 | |
| 906 | static unsigned int clientstr_hashval(const char *name) |
| 907 | { |
| 908 | return opaque_hashval(name, 8) & CLIENT_HASH_MASK; |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 913 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 914 | * only what share bits are currently in force, but also what |
| 915 | * combinations of share bits previous opens have used. This allows us |
| 916 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 917 | * return an error if the client attempt to downgrade to a combination |
| 918 | * of share bits not explicable by closing some of its previous opens. |
| 919 | * |
| 920 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 921 | * track of access/deny bit combinations; so, e.g., we allow: |
| 922 | * |
| 923 | * OPEN allow read, deny write |
| 924 | * OPEN allow both, deny none |
| 925 | * DOWNGRADE allow read, deny none |
| 926 | * |
| 927 | * which we should reject. |
| 928 | */ |
| 929 | static unsigned int |
| 930 | bmap_to_share_mode(unsigned long bmap) { |
| 931 | int i; |
| 932 | unsigned int access = 0; |
| 933 | |
| 934 | for (i = 1; i < 4; i++) { |
| 935 | if (test_bit(i, &bmap)) |
| 936 | access |= i; |
| 937 | } |
| 938 | return access; |
| 939 | } |
| 940 | |
| 941 | /* set share access for a given stateid */ |
| 942 | static inline void |
| 943 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 944 | { |
| 945 | unsigned char mask = 1 << access; |
| 946 | |
| 947 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 948 | stp->st_access_bmap |= mask; |
| 949 | } |
| 950 | |
| 951 | /* clear share access for a given stateid */ |
| 952 | static inline void |
| 953 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 954 | { |
| 955 | unsigned char mask = 1 << access; |
| 956 | |
| 957 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 958 | stp->st_access_bmap &= ~mask; |
| 959 | } |
| 960 | |
| 961 | /* test whether a given stateid has access */ |
| 962 | static inline bool |
| 963 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 964 | { |
| 965 | unsigned char mask = 1 << access; |
| 966 | |
| 967 | return (bool)(stp->st_access_bmap & mask); |
| 968 | } |
| 969 | |
| 970 | /* set share deny for a given stateid */ |
| 971 | static inline void |
| 972 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 973 | { |
| 974 | unsigned char mask = 1 << deny; |
| 975 | |
| 976 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 977 | stp->st_deny_bmap |= mask; |
| 978 | } |
| 979 | |
| 980 | /* clear share deny for a given stateid */ |
| 981 | static inline void |
| 982 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 983 | { |
| 984 | unsigned char mask = 1 << deny; |
| 985 | |
| 986 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 987 | stp->st_deny_bmap &= ~mask; |
| 988 | } |
| 989 | |
| 990 | /* test whether a given stateid is denying specific access */ |
| 991 | static inline bool |
| 992 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 993 | { |
| 994 | unsigned char mask = 1 << deny; |
| 995 | |
| 996 | return (bool)(stp->st_deny_bmap & mask); |
| 997 | } |
| 998 | |
| 999 | static int nfs4_access_to_omode(u32 access) |
| 1000 | { |
| 1001 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
| 1002 | case NFS4_SHARE_ACCESS_READ: |
| 1003 | return O_RDONLY; |
| 1004 | case NFS4_SHARE_ACCESS_WRITE: |
| 1005 | return O_WRONLY; |
| 1006 | case NFS4_SHARE_ACCESS_BOTH: |
| 1007 | return O_RDWR; |
| 1008 | } |
| 1009 | WARN_ON_ONCE(1); |
| 1010 | return O_RDONLY; |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * A stateid that had a deny mode associated with it is being released |
| 1015 | * or downgraded. Recalculate the deny mode on the file. |
| 1016 | */ |
| 1017 | static void |
| 1018 | recalculate_deny_mode(struct nfs4_file *fp) |
| 1019 | { |
| 1020 | struct nfs4_ol_stateid *stp; |
| 1021 | |
| 1022 | spin_lock(&fp->fi_lock); |
| 1023 | fp->fi_share_deny = 0; |
| 1024 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) |
| 1025 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); |
| 1026 | spin_unlock(&fp->fi_lock); |
| 1027 | } |
| 1028 | |
| 1029 | static void |
| 1030 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 1031 | { |
| 1032 | int i; |
| 1033 | bool change = false; |
| 1034 | |
| 1035 | for (i = 1; i < 4; i++) { |
| 1036 | if ((i & deny) != i) { |
| 1037 | change = true; |
| 1038 | clear_deny(i, stp); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | /* Recalculate per-file deny mode if there was a change */ |
| 1043 | if (change) |
| 1044 | recalculate_deny_mode(stp->st_stid.sc_file); |
| 1045 | } |
| 1046 | |
| 1047 | /* release all access and file references for a given stateid */ |
| 1048 | static void |
| 1049 | release_all_access(struct nfs4_ol_stateid *stp) |
| 1050 | { |
| 1051 | int i; |
| 1052 | struct nfs4_file *fp = stp->st_stid.sc_file; |
| 1053 | |
| 1054 | if (fp && stp->st_deny_bmap != 0) |
| 1055 | recalculate_deny_mode(fp); |
| 1056 | |
| 1057 | for (i = 1; i < 4; i++) { |
| 1058 | if (test_access(i, stp)) |
| 1059 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
| 1060 | clear_access(i, stp); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) |
| 1065 | { |
| 1066 | kfree(sop->so_owner.data); |
| 1067 | sop->so_ops->so_free(sop); |
| 1068 | } |
| 1069 | |
| 1070 | static void nfs4_put_stateowner(struct nfs4_stateowner *sop) |
| 1071 | { |
| 1072 | struct nfs4_client *clp = sop->so_client; |
| 1073 | |
| 1074 | might_lock(&clp->cl_lock); |
| 1075 | |
| 1076 | if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) |
| 1077 | return; |
| 1078 | sop->so_ops->so_unhash(sop); |
| 1079 | spin_unlock(&clp->cl_lock); |
| 1080 | nfs4_free_stateowner(sop); |
| 1081 | } |
| 1082 | |
| 1083 | static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) |
| 1084 | { |
| 1085 | struct nfs4_file *fp = stp->st_stid.sc_file; |
| 1086 | |
| 1087 | lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); |
| 1088 | |
| 1089 | if (list_empty(&stp->st_perfile)) |
| 1090 | return false; |
| 1091 | |
| 1092 | spin_lock(&fp->fi_lock); |
| 1093 | list_del_init(&stp->st_perfile); |
| 1094 | spin_unlock(&fp->fi_lock); |
| 1095 | list_del(&stp->st_perstateowner); |
| 1096 | return true; |
| 1097 | } |
| 1098 | |
| 1099 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
| 1100 | { |
| 1101 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
| 1102 | |
| 1103 | put_clnt_odstate(stp->st_clnt_odstate); |
| 1104 | release_all_access(stp); |
| 1105 | if (stp->st_stateowner) |
| 1106 | nfs4_put_stateowner(stp->st_stateowner); |
| 1107 | kmem_cache_free(stateid_slab, stid); |
| 1108 | } |
| 1109 | |
| 1110 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
| 1111 | { |
| 1112 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
| 1113 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
| 1114 | struct file *file; |
| 1115 | |
| 1116 | file = find_any_file(stp->st_stid.sc_file); |
| 1117 | if (file) |
| 1118 | filp_close(file, (fl_owner_t)lo); |
| 1119 | nfs4_free_ol_stateid(stid); |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * Put the persistent reference to an already unhashed generic stateid, while |
| 1124 | * holding the cl_lock. If it's the last reference, then put it onto the |
| 1125 | * reaplist for later destruction. |
| 1126 | */ |
| 1127 | static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, |
| 1128 | struct list_head *reaplist) |
| 1129 | { |
| 1130 | struct nfs4_stid *s = &stp->st_stid; |
| 1131 | struct nfs4_client *clp = s->sc_client; |
| 1132 | |
| 1133 | lockdep_assert_held(&clp->cl_lock); |
| 1134 | |
| 1135 | WARN_ON_ONCE(!list_empty(&stp->st_locks)); |
| 1136 | |
| 1137 | if (!atomic_dec_and_test(&s->sc_count)) { |
| 1138 | wake_up_all(&close_wq); |
| 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
| 1143 | list_add(&stp->st_locks, reaplist); |
| 1144 | } |
| 1145 | |
| 1146 | static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) |
| 1147 | { |
| 1148 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
| 1149 | |
| 1150 | list_del_init(&stp->st_locks); |
| 1151 | nfs4_unhash_stid(&stp->st_stid); |
| 1152 | return unhash_ol_stateid(stp); |
| 1153 | } |
| 1154 | |
| 1155 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
| 1156 | { |
| 1157 | struct nfs4_client *clp = stp->st_stid.sc_client; |
| 1158 | bool unhashed; |
| 1159 | |
| 1160 | spin_lock(&clp->cl_lock); |
| 1161 | unhashed = unhash_lock_stateid(stp); |
| 1162 | spin_unlock(&clp->cl_lock); |
| 1163 | if (unhashed) |
| 1164 | nfs4_put_stid(&stp->st_stid); |
| 1165 | } |
| 1166 | |
| 1167 | static void unhash_lockowner_locked(struct nfs4_lockowner *lo) |
| 1168 | { |
| 1169 | struct nfs4_client *clp = lo->lo_owner.so_client; |
| 1170 | |
| 1171 | lockdep_assert_held(&clp->cl_lock); |
| 1172 | |
| 1173 | list_del_init(&lo->lo_owner.so_strhash); |
| 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | * Free a list of generic stateids that were collected earlier after being |
| 1178 | * fully unhashed. |
| 1179 | */ |
| 1180 | static void |
| 1181 | free_ol_stateid_reaplist(struct list_head *reaplist) |
| 1182 | { |
| 1183 | struct nfs4_ol_stateid *stp; |
| 1184 | struct nfs4_file *fp; |
| 1185 | |
| 1186 | might_sleep(); |
| 1187 | |
| 1188 | while (!list_empty(reaplist)) { |
| 1189 | stp = list_first_entry(reaplist, struct nfs4_ol_stateid, |
| 1190 | st_locks); |
| 1191 | list_del(&stp->st_locks); |
| 1192 | fp = stp->st_stid.sc_file; |
| 1193 | stp->st_stid.sc_free(&stp->st_stid); |
| 1194 | if (fp) |
| 1195 | put_nfs4_file(fp); |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, |
| 1200 | struct list_head *reaplist) |
| 1201 | { |
| 1202 | struct nfs4_ol_stateid *stp; |
| 1203 | |
| 1204 | lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); |
| 1205 | |
| 1206 | while (!list_empty(&open_stp->st_locks)) { |
| 1207 | stp = list_entry(open_stp->st_locks.next, |
| 1208 | struct nfs4_ol_stateid, st_locks); |
| 1209 | WARN_ON(!unhash_lock_stateid(stp)); |
| 1210 | put_ol_stateid_locked(stp, reaplist); |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, |
| 1215 | struct list_head *reaplist) |
| 1216 | { |
| 1217 | bool unhashed; |
| 1218 | |
| 1219 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
| 1220 | |
| 1221 | unhashed = unhash_ol_stateid(stp); |
| 1222 | release_open_stateid_locks(stp, reaplist); |
| 1223 | return unhashed; |
| 1224 | } |
| 1225 | |
| 1226 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 1227 | { |
| 1228 | LIST_HEAD(reaplist); |
| 1229 | |
| 1230 | spin_lock(&stp->st_stid.sc_client->cl_lock); |
| 1231 | if (unhash_open_stateid(stp, &reaplist)) |
| 1232 | put_ol_stateid_locked(stp, &reaplist); |
| 1233 | spin_unlock(&stp->st_stid.sc_client->cl_lock); |
| 1234 | free_ol_stateid_reaplist(&reaplist); |
| 1235 | } |
| 1236 | |
| 1237 | static void unhash_openowner_locked(struct nfs4_openowner *oo) |
| 1238 | { |
| 1239 | struct nfs4_client *clp = oo->oo_owner.so_client; |
| 1240 | |
| 1241 | lockdep_assert_held(&clp->cl_lock); |
| 1242 | |
| 1243 | list_del_init(&oo->oo_owner.so_strhash); |
| 1244 | list_del_init(&oo->oo_perclient); |
| 1245 | } |
| 1246 | |
| 1247 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 1248 | { |
| 1249 | struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, |
| 1250 | nfsd_net_id); |
| 1251 | struct nfs4_ol_stateid *s; |
| 1252 | |
| 1253 | spin_lock(&nn->client_lock); |
| 1254 | s = oo->oo_last_closed_stid; |
| 1255 | if (s) { |
| 1256 | list_del_init(&oo->oo_close_lru); |
| 1257 | oo->oo_last_closed_stid = NULL; |
| 1258 | } |
| 1259 | spin_unlock(&nn->client_lock); |
| 1260 | if (s) |
| 1261 | nfs4_put_stid(&s->st_stid); |
| 1262 | } |
| 1263 | |
| 1264 | static void release_openowner(struct nfs4_openowner *oo) |
| 1265 | { |
| 1266 | struct nfs4_ol_stateid *stp; |
| 1267 | struct nfs4_client *clp = oo->oo_owner.so_client; |
| 1268 | struct list_head reaplist; |
| 1269 | |
| 1270 | INIT_LIST_HEAD(&reaplist); |
| 1271 | |
| 1272 | spin_lock(&clp->cl_lock); |
| 1273 | unhash_openowner_locked(oo); |
| 1274 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 1275 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
| 1276 | struct nfs4_ol_stateid, st_perstateowner); |
| 1277 | if (unhash_open_stateid(stp, &reaplist)) |
| 1278 | put_ol_stateid_locked(stp, &reaplist); |
| 1279 | } |
| 1280 | spin_unlock(&clp->cl_lock); |
| 1281 | free_ol_stateid_reaplist(&reaplist); |
| 1282 | release_last_closed_stateid(oo); |
| 1283 | nfs4_put_stateowner(&oo->oo_owner); |
| 1284 | } |
| 1285 | |
| 1286 | static inline int |
| 1287 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 1288 | { |
| 1289 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 1290 | |
| 1291 | return sid->sequence % SESSION_HASH_SIZE; |
| 1292 | } |
| 1293 | |
| 1294 | #ifdef CONFIG_SUNRPC_DEBUG |
| 1295 | static inline void |
| 1296 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1297 | { |
| 1298 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 1299 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 1300 | } |
| 1301 | #else |
| 1302 | static inline void |
| 1303 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1304 | { |
| 1305 | } |
| 1306 | #endif |
| 1307 | |
| 1308 | /* |
| 1309 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 1310 | * won't be used for replay. |
| 1311 | */ |
| 1312 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 1313 | { |
| 1314 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 1315 | |
| 1316 | if (nfserr == nfserr_replay_me) |
| 1317 | return; |
| 1318 | |
| 1319 | if (!seqid_mutating_err(ntohl(nfserr))) { |
| 1320 | nfsd4_cstate_clear_replay(cstate); |
| 1321 | return; |
| 1322 | } |
| 1323 | if (!so) |
| 1324 | return; |
| 1325 | if (so->so_is_open_owner) |
| 1326 | release_last_closed_stateid(openowner(so)); |
| 1327 | so->so_seqid++; |
| 1328 | return; |
| 1329 | } |
| 1330 | |
| 1331 | static void |
| 1332 | gen_sessionid(struct nfsd4_session *ses) |
| 1333 | { |
| 1334 | struct nfs4_client *clp = ses->se_client; |
| 1335 | struct nfsd4_sessionid *sid; |
| 1336 | |
| 1337 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 1338 | sid->clientid = clp->cl_clientid; |
| 1339 | sid->sequence = current_sessionid++; |
| 1340 | sid->reserved = 0; |
| 1341 | } |
| 1342 | |
| 1343 | /* |
| 1344 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 1345 | * the rpc header, but all we need to cache is the data starting after |
| 1346 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 1347 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 1348 | * value that is the number of bytes in our cache plus a few additional |
| 1349 | * bytes. In order to stay on the safe side, and not promise more than |
| 1350 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 1351 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 1352 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 1353 | * for the SEQUENCE op response: |
| 1354 | */ |
| 1355 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 1356 | |
| 1357 | static void |
| 1358 | free_session_slots(struct nfsd4_session *ses) |
| 1359 | { |
| 1360 | int i; |
| 1361 | |
| 1362 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) |
| 1363 | kfree(ses->se_slots[i]); |
| 1364 | } |
| 1365 | |
| 1366 | /* |
| 1367 | * We don't actually need to cache the rpc and session headers, so we |
| 1368 | * can allocate a little less for each slot: |
| 1369 | */ |
| 1370 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
| 1371 | { |
| 1372 | u32 size; |
| 1373 | |
| 1374 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 1375 | size = 0; |
| 1376 | else |
| 1377 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 1378 | return size + sizeof(struct nfsd4_slot); |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 1383 | * re-negotiate active sessions and reduce their slot usage to make |
| 1384 | * room for new connections. For now we just fail the create session. |
| 1385 | */ |
| 1386 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca) |
| 1387 | { |
| 1388 | u32 slotsize = slot_bytes(ca); |
| 1389 | u32 num = ca->maxreqs; |
| 1390 | int avail; |
| 1391 | |
| 1392 | spin_lock(&nfsd_drc_lock); |
| 1393 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, |
| 1394 | nfsd_drc_max_mem - nfsd_drc_mem_used); |
| 1395 | num = min_t(int, num, avail / slotsize); |
| 1396 | nfsd_drc_mem_used += num * slotsize; |
| 1397 | spin_unlock(&nfsd_drc_lock); |
| 1398 | |
| 1399 | return num; |
| 1400 | } |
| 1401 | |
| 1402 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
| 1403 | { |
| 1404 | int slotsize = slot_bytes(ca); |
| 1405 | |
| 1406 | spin_lock(&nfsd_drc_lock); |
| 1407 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
| 1408 | spin_unlock(&nfsd_drc_lock); |
| 1409 | } |
| 1410 | |
| 1411 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 1412 | struct nfsd4_channel_attrs *battrs) |
| 1413 | { |
| 1414 | int numslots = fattrs->maxreqs; |
| 1415 | int slotsize = slot_bytes(fattrs); |
| 1416 | struct nfsd4_session *new; |
| 1417 | int mem, i; |
| 1418 | |
| 1419 | BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *) |
| 1420 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 1421 | mem = numslots * sizeof(struct nfsd4_slot *); |
| 1422 | |
| 1423 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
| 1424 | if (!new) |
| 1425 | return NULL; |
| 1426 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
| 1427 | for (i = 0; i < numslots; i++) { |
| 1428 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
| 1429 | if (!new->se_slots[i]) |
| 1430 | goto out_free; |
| 1431 | } |
| 1432 | |
| 1433 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 1434 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 1435 | |
| 1436 | return new; |
| 1437 | out_free: |
| 1438 | while (i--) |
| 1439 | kfree(new->se_slots[i]); |
| 1440 | kfree(new); |
| 1441 | return NULL; |
| 1442 | } |
| 1443 | |
| 1444 | static void free_conn(struct nfsd4_conn *c) |
| 1445 | { |
| 1446 | svc_xprt_put(c->cn_xprt); |
| 1447 | kfree(c); |
| 1448 | } |
| 1449 | |
| 1450 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 1451 | { |
| 1452 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 1453 | struct nfs4_client *clp = c->cn_session->se_client; |
| 1454 | |
| 1455 | spin_lock(&clp->cl_lock); |
| 1456 | if (!list_empty(&c->cn_persession)) { |
| 1457 | list_del(&c->cn_persession); |
| 1458 | free_conn(c); |
| 1459 | } |
| 1460 | nfsd4_probe_callback(clp); |
| 1461 | spin_unlock(&clp->cl_lock); |
| 1462 | } |
| 1463 | |
| 1464 | static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags) |
| 1465 | { |
| 1466 | struct nfsd4_conn *conn; |
| 1467 | |
| 1468 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 1469 | if (!conn) |
| 1470 | return NULL; |
| 1471 | svc_xprt_get(rqstp->rq_xprt); |
| 1472 | conn->cn_xprt = rqstp->rq_xprt; |
| 1473 | conn->cn_flags = flags; |
| 1474 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1475 | return conn; |
| 1476 | } |
| 1477 | |
| 1478 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1479 | { |
| 1480 | conn->cn_session = ses; |
| 1481 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1482 | } |
| 1483 | |
| 1484 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1485 | { |
| 1486 | struct nfs4_client *clp = ses->se_client; |
| 1487 | |
| 1488 | spin_lock(&clp->cl_lock); |
| 1489 | __nfsd4_hash_conn(conn, ses); |
| 1490 | spin_unlock(&clp->cl_lock); |
| 1491 | } |
| 1492 | |
| 1493 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
| 1494 | { |
| 1495 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
| 1496 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
| 1497 | } |
| 1498 | |
| 1499 | static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1500 | { |
| 1501 | int ret; |
| 1502 | |
| 1503 | nfsd4_hash_conn(conn, ses); |
| 1504 | ret = nfsd4_register_conn(conn); |
| 1505 | if (ret) |
| 1506 | /* oops; xprt is already down: */ |
| 1507 | nfsd4_conn_lost(&conn->cn_xpt_user); |
| 1508 | /* We may have gained or lost a callback channel: */ |
| 1509 | nfsd4_probe_callback_sync(ses->se_client); |
| 1510 | } |
| 1511 | |
| 1512 | static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses) |
| 1513 | { |
| 1514 | u32 dir = NFS4_CDFC4_FORE; |
| 1515 | |
| 1516 | if (cses->flags & SESSION4_BACK_CHAN) |
| 1517 | dir |= NFS4_CDFC4_BACK; |
| 1518 | return alloc_conn(rqstp, dir); |
| 1519 | } |
| 1520 | |
| 1521 | /* must be called under client_lock */ |
| 1522 | static void nfsd4_del_conns(struct nfsd4_session *s) |
| 1523 | { |
| 1524 | struct nfs4_client *clp = s->se_client; |
| 1525 | struct nfsd4_conn *c; |
| 1526 | |
| 1527 | spin_lock(&clp->cl_lock); |
| 1528 | while (!list_empty(&s->se_conns)) { |
| 1529 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1530 | list_del_init(&c->cn_persession); |
| 1531 | spin_unlock(&clp->cl_lock); |
| 1532 | |
| 1533 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1534 | free_conn(c); |
| 1535 | |
| 1536 | spin_lock(&clp->cl_lock); |
| 1537 | } |
| 1538 | spin_unlock(&clp->cl_lock); |
| 1539 | } |
| 1540 | |
| 1541 | static void __free_session(struct nfsd4_session *ses) |
| 1542 | { |
| 1543 | free_session_slots(ses); |
| 1544 | kfree(ses); |
| 1545 | } |
| 1546 | |
| 1547 | static void free_session(struct nfsd4_session *ses) |
| 1548 | { |
| 1549 | nfsd4_del_conns(ses); |
| 1550 | nfsd4_put_drc_mem(&ses->se_fchannel); |
| 1551 | __free_session(ses); |
| 1552 | } |
| 1553 | |
| 1554 | static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses) |
| 1555 | { |
| 1556 | int idx; |
| 1557 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 1558 | |
| 1559 | new->se_client = clp; |
| 1560 | gen_sessionid(new); |
| 1561 | |
| 1562 | INIT_LIST_HEAD(&new->se_conns); |
| 1563 | |
| 1564 | new->se_cb_seq_nr = 1; |
| 1565 | new->se_flags = cses->flags; |
| 1566 | new->se_cb_prog = cses->callback_prog; |
| 1567 | new->se_cb_sec = cses->cb_sec; |
| 1568 | atomic_set(&new->se_ref, 0); |
| 1569 | idx = hash_sessionid(&new->se_sessionid); |
| 1570 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
| 1571 | spin_lock(&clp->cl_lock); |
| 1572 | list_add(&new->se_perclnt, &clp->cl_sessions); |
| 1573 | spin_unlock(&clp->cl_lock); |
| 1574 | |
| 1575 | { |
| 1576 | struct sockaddr *sa = svc_addr(rqstp); |
| 1577 | /* |
| 1578 | * This is a little silly; with sessions there's no real |
| 1579 | * use for the callback address. Use the peer address |
| 1580 | * as a reasonable default for now, but consider fixing |
| 1581 | * the rpc client not to require an address in the |
| 1582 | * future: |
| 1583 | */ |
| 1584 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1585 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | /* caller must hold client_lock */ |
| 1590 | static struct nfsd4_session * |
| 1591 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
| 1592 | { |
| 1593 | struct nfsd4_session *elem; |
| 1594 | int idx; |
| 1595 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 1596 | |
| 1597 | lockdep_assert_held(&nn->client_lock); |
| 1598 | |
| 1599 | dump_sessionid(__func__, sessionid); |
| 1600 | idx = hash_sessionid(sessionid); |
| 1601 | /* Search in the appropriate list */ |
| 1602 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
| 1603 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1604 | NFS4_MAX_SESSIONID_LEN)) { |
| 1605 | return elem; |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | dprintk("%s: session not found\n", __func__); |
| 1610 | return NULL; |
| 1611 | } |
| 1612 | |
| 1613 | static struct nfsd4_session * |
| 1614 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, |
| 1615 | __be32 *ret) |
| 1616 | { |
| 1617 | struct nfsd4_session *session; |
| 1618 | __be32 status = nfserr_badsession; |
| 1619 | |
| 1620 | session = __find_in_sessionid_hashtbl(sessionid, net); |
| 1621 | if (!session) |
| 1622 | goto out; |
| 1623 | status = nfsd4_get_session_locked(session); |
| 1624 | if (status) |
| 1625 | session = NULL; |
| 1626 | out: |
| 1627 | *ret = status; |
| 1628 | return session; |
| 1629 | } |
| 1630 | |
| 1631 | /* caller must hold client_lock */ |
| 1632 | static void |
| 1633 | unhash_session(struct nfsd4_session *ses) |
| 1634 | { |
| 1635 | struct nfs4_client *clp = ses->se_client; |
| 1636 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1637 | |
| 1638 | lockdep_assert_held(&nn->client_lock); |
| 1639 | |
| 1640 | list_del(&ses->se_hash); |
| 1641 | spin_lock(&ses->se_client->cl_lock); |
| 1642 | list_del(&ses->se_perclnt); |
| 1643 | spin_unlock(&ses->se_client->cl_lock); |
| 1644 | } |
| 1645 | |
| 1646 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1647 | static int |
| 1648 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
| 1649 | { |
| 1650 | /* |
| 1651 | * We're assuming the clid was not given out from a boot |
| 1652 | * precisely 2^32 (about 136 years) before this one. That seems |
| 1653 | * a safe assumption: |
| 1654 | */ |
| 1655 | if (clid->cl_boot == (u32)nn->boot_time) |
| 1656 | return 0; |
| 1657 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
| 1658 | clid->cl_boot, clid->cl_id, nn->boot_time); |
| 1659 | return 1; |
| 1660 | } |
| 1661 | |
| 1662 | /* |
| 1663 | * XXX Should we use a slab cache ? |
| 1664 | * This type of memory management is somewhat inefficient, but we use it |
| 1665 | * anyway since SETCLIENTID is not a common operation. |
| 1666 | */ |
| 1667 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
| 1668 | { |
| 1669 | struct nfs4_client *clp; |
| 1670 | int i; |
| 1671 | |
| 1672 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
| 1673 | if (clp == NULL) |
| 1674 | return NULL; |
| 1675 | clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL); |
| 1676 | if (clp->cl_name.data == NULL) |
| 1677 | goto err_no_name; |
| 1678 | clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) * |
| 1679 | OWNER_HASH_SIZE, GFP_KERNEL); |
| 1680 | if (!clp->cl_ownerstr_hashtbl) |
| 1681 | goto err_no_hashtbl; |
| 1682 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 1683 | INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); |
| 1684 | clp->cl_name.len = name.len; |
| 1685 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1686 | idr_init(&clp->cl_stateids); |
| 1687 | atomic_set(&clp->cl_refcount, 0); |
| 1688 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 1689 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1690 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1691 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 1692 | INIT_LIST_HEAD(&clp->cl_lru); |
| 1693 | INIT_LIST_HEAD(&clp->cl_revoked); |
| 1694 | #ifdef CONFIG_NFSD_PNFS |
| 1695 | INIT_LIST_HEAD(&clp->cl_lo_states); |
| 1696 | #endif |
| 1697 | spin_lock_init(&clp->cl_lock); |
| 1698 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
| 1699 | return clp; |
| 1700 | err_no_hashtbl: |
| 1701 | kfree(clp->cl_name.data); |
| 1702 | err_no_name: |
| 1703 | kfree(clp); |
| 1704 | return NULL; |
| 1705 | } |
| 1706 | |
| 1707 | static void |
| 1708 | free_client(struct nfs4_client *clp) |
| 1709 | { |
| 1710 | while (!list_empty(&clp->cl_sessions)) { |
| 1711 | struct nfsd4_session *ses; |
| 1712 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 1713 | se_perclnt); |
| 1714 | list_del(&ses->se_perclnt); |
| 1715 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 1716 | free_session(ses); |
| 1717 | } |
| 1718 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
| 1719 | free_svc_cred(&clp->cl_cred); |
| 1720 | kfree(clp->cl_ownerstr_hashtbl); |
| 1721 | kfree(clp->cl_name.data); |
| 1722 | idr_destroy(&clp->cl_stateids); |
| 1723 | kfree(clp); |
| 1724 | } |
| 1725 | |
| 1726 | /* must be called under the client_lock */ |
| 1727 | static void |
| 1728 | unhash_client_locked(struct nfs4_client *clp) |
| 1729 | { |
| 1730 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1731 | struct nfsd4_session *ses; |
| 1732 | |
| 1733 | lockdep_assert_held(&nn->client_lock); |
| 1734 | |
| 1735 | /* Mark the client as expired! */ |
| 1736 | clp->cl_time = 0; |
| 1737 | /* Make it invisible */ |
| 1738 | if (!list_empty(&clp->cl_idhash)) { |
| 1739 | list_del_init(&clp->cl_idhash); |
| 1740 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
| 1741 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
| 1742 | else |
| 1743 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
| 1744 | } |
| 1745 | list_del_init(&clp->cl_lru); |
| 1746 | spin_lock(&clp->cl_lock); |
| 1747 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 1748 | list_del_init(&ses->se_hash); |
| 1749 | spin_unlock(&clp->cl_lock); |
| 1750 | } |
| 1751 | |
| 1752 | static void |
| 1753 | unhash_client(struct nfs4_client *clp) |
| 1754 | { |
| 1755 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1756 | |
| 1757 | spin_lock(&nn->client_lock); |
| 1758 | unhash_client_locked(clp); |
| 1759 | spin_unlock(&nn->client_lock); |
| 1760 | } |
| 1761 | |
| 1762 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
| 1763 | { |
| 1764 | if (atomic_read(&clp->cl_refcount)) |
| 1765 | return nfserr_jukebox; |
| 1766 | unhash_client_locked(clp); |
| 1767 | return nfs_ok; |
| 1768 | } |
| 1769 | |
| 1770 | static void |
| 1771 | __destroy_client(struct nfs4_client *clp) |
| 1772 | { |
| 1773 | struct nfs4_openowner *oo; |
| 1774 | struct nfs4_delegation *dp; |
| 1775 | struct list_head reaplist; |
| 1776 | |
| 1777 | INIT_LIST_HEAD(&reaplist); |
| 1778 | spin_lock(&state_lock); |
| 1779 | while (!list_empty(&clp->cl_delegations)) { |
| 1780 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
| 1781 | WARN_ON(!unhash_delegation_locked(dp)); |
| 1782 | list_add(&dp->dl_recall_lru, &reaplist); |
| 1783 | } |
| 1784 | spin_unlock(&state_lock); |
| 1785 | while (!list_empty(&reaplist)) { |
| 1786 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
| 1787 | list_del_init(&dp->dl_recall_lru); |
| 1788 | put_clnt_odstate(dp->dl_clnt_odstate); |
| 1789 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
| 1790 | nfs4_put_stid(&dp->dl_stid); |
| 1791 | } |
| 1792 | while (!list_empty(&clp->cl_revoked)) { |
| 1793 | dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru); |
| 1794 | list_del_init(&dp->dl_recall_lru); |
| 1795 | nfs4_put_stid(&dp->dl_stid); |
| 1796 | } |
| 1797 | while (!list_empty(&clp->cl_openowners)) { |
| 1798 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
| 1799 | nfs4_get_stateowner(&oo->oo_owner); |
| 1800 | release_openowner(oo); |
| 1801 | } |
| 1802 | nfsd4_return_all_client_layouts(clp); |
| 1803 | nfsd4_shutdown_callback(clp); |
| 1804 | if (clp->cl_cb_conn.cb_xprt) |
| 1805 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
| 1806 | free_client(clp); |
| 1807 | } |
| 1808 | |
| 1809 | static void |
| 1810 | destroy_client(struct nfs4_client *clp) |
| 1811 | { |
| 1812 | unhash_client(clp); |
| 1813 | __destroy_client(clp); |
| 1814 | } |
| 1815 | |
| 1816 | static void expire_client(struct nfs4_client *clp) |
| 1817 | { |
| 1818 | unhash_client(clp); |
| 1819 | nfsd4_client_record_remove(clp); |
| 1820 | __destroy_client(clp); |
| 1821 | } |
| 1822 | |
| 1823 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 1824 | { |
| 1825 | memcpy(target->cl_verifier.data, source->data, |
| 1826 | sizeof(target->cl_verifier.data)); |
| 1827 | } |
| 1828 | |
| 1829 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 1830 | { |
| 1831 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 1832 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 1833 | } |
| 1834 | |
| 1835 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) |
| 1836 | { |
| 1837 | if (source->cr_principal) { |
| 1838 | target->cr_principal = |
| 1839 | kstrdup(source->cr_principal, GFP_KERNEL); |
| 1840 | if (target->cr_principal == NULL) |
| 1841 | return -ENOMEM; |
| 1842 | } else |
| 1843 | target->cr_principal = NULL; |
| 1844 | target->cr_flavor = source->cr_flavor; |
| 1845 | target->cr_uid = source->cr_uid; |
| 1846 | target->cr_gid = source->cr_gid; |
| 1847 | target->cr_group_info = source->cr_group_info; |
| 1848 | get_group_info(target->cr_group_info); |
| 1849 | target->cr_gss_mech = source->cr_gss_mech; |
| 1850 | if (source->cr_gss_mech) |
| 1851 | gss_mech_get(source->cr_gss_mech); |
| 1852 | return 0; |
| 1853 | } |
| 1854 | |
| 1855 | static int |
| 1856 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 1857 | { |
| 1858 | if (o1->len < o2->len) |
| 1859 | return -1; |
| 1860 | if (o1->len > o2->len) |
| 1861 | return 1; |
| 1862 | return memcmp(o1->data, o2->data, o1->len); |
| 1863 | } |
| 1864 | |
| 1865 | static int same_name(const char *n1, const char *n2) |
| 1866 | { |
| 1867 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
| 1868 | } |
| 1869 | |
| 1870 | static int |
| 1871 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 1872 | { |
| 1873 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
| 1874 | } |
| 1875 | |
| 1876 | static int |
| 1877 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 1878 | { |
| 1879 | return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id); |
| 1880 | } |
| 1881 | |
| 1882 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 1883 | { |
| 1884 | int i; |
| 1885 | |
| 1886 | if (g1->ngroups != g2->ngroups) |
| 1887 | return false; |
| 1888 | for (i=0; i<g1->ngroups; i++) |
| 1889 | if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i))) |
| 1890 | return false; |
| 1891 | return true; |
| 1892 | } |
| 1893 | |
| 1894 | /* |
| 1895 | * RFC 3530 language requires clid_inuse be returned when the |
| 1896 | * "principal" associated with a requests differs from that previously |
| 1897 | * used. We use uid, gid's, and gss principal string as our best |
| 1898 | * approximation. We also don't want to allow non-gss use of a client |
| 1899 | * established using gss: in theory cr_principal should catch that |
| 1900 | * change, but in practice cr_principal can be null even in the gss case |
| 1901 | * since gssd doesn't always pass down a principal string. |
| 1902 | */ |
| 1903 | static bool is_gss_cred(struct svc_cred *cr) |
| 1904 | { |
| 1905 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 1906 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 1907 | } |
| 1908 | |
| 1909 | |
| 1910 | static bool |
| 1911 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 1912 | { |
| 1913 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
| 1914 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 1915 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
| 1916 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 1917 | return false; |
| 1918 | if (cr1->cr_principal == cr2->cr_principal) |
| 1919 | return true; |
| 1920 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 1921 | return false; |
| 1922 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
| 1923 | } |
| 1924 | |
| 1925 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 1926 | { |
| 1927 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1928 | u32 service; |
| 1929 | |
| 1930 | if (!cr->cr_gss_mech) |
| 1931 | return false; |
| 1932 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 1933 | return service == RPC_GSS_SVC_INTEGRITY || |
| 1934 | service == RPC_GSS_SVC_PRIVACY; |
| 1935 | } |
| 1936 | |
| 1937 | static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) |
| 1938 | { |
| 1939 | struct svc_cred *cr = &rqstp->rq_cred; |
| 1940 | |
| 1941 | if (!cl->cl_mach_cred) |
| 1942 | return true; |
| 1943 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 1944 | return false; |
| 1945 | if (!svc_rqst_integrity_protected(rqstp)) |
| 1946 | return false; |
| 1947 | if (!cr->cr_principal) |
| 1948 | return false; |
| 1949 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 1950 | } |
| 1951 | |
| 1952 | static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn) |
| 1953 | { |
| 1954 | __be32 verf[2]; |
| 1955 | |
| 1956 | /* |
| 1957 | * This is opaque to client, so no need to byte-swap. Use |
| 1958 | * __force to keep sparse happy |
| 1959 | */ |
| 1960 | verf[0] = (__force __be32)get_seconds(); |
| 1961 | verf[1] = (__force __be32)nn->clverifier_counter++; |
| 1962 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
| 1963 | } |
| 1964 | |
| 1965 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
| 1966 | { |
| 1967 | clp->cl_clientid.cl_boot = nn->boot_time; |
| 1968 | clp->cl_clientid.cl_id = nn->clientid_counter++; |
| 1969 | gen_confirm(clp, nn); |
| 1970 | } |
| 1971 | |
| 1972 | static struct nfs4_stid * |
| 1973 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) |
| 1974 | { |
| 1975 | struct nfs4_stid *ret; |
| 1976 | |
| 1977 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 1978 | if (!ret || !ret->sc_type) |
| 1979 | return NULL; |
| 1980 | return ret; |
| 1981 | } |
| 1982 | |
| 1983 | static struct nfs4_stid * |
| 1984 | find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask) |
| 1985 | { |
| 1986 | struct nfs4_stid *s; |
| 1987 | |
| 1988 | spin_lock(&cl->cl_lock); |
| 1989 | s = find_stateid_locked(cl, t); |
| 1990 | if (s != NULL) { |
| 1991 | if (typemask & s->sc_type) |
| 1992 | atomic_inc(&s->sc_count); |
| 1993 | else |
| 1994 | s = NULL; |
| 1995 | } |
| 1996 | spin_unlock(&cl->cl_lock); |
| 1997 | return s; |
| 1998 | } |
| 1999 | |
| 2000 | static struct nfs4_client *create_client(struct xdr_netobj name, |
| 2001 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 2002 | { |
| 2003 | struct nfs4_client *clp; |
| 2004 | struct sockaddr *sa = svc_addr(rqstp); |
| 2005 | int ret; |
| 2006 | struct net *net = SVC_NET(rqstp); |
| 2007 | |
| 2008 | clp = alloc_client(name); |
| 2009 | if (clp == NULL) |
| 2010 | return NULL; |
| 2011 | |
| 2012 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 2013 | if (ret) { |
| 2014 | free_client(clp); |
| 2015 | return NULL; |
| 2016 | } |
| 2017 | nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL); |
| 2018 | clp->cl_time = get_seconds(); |
| 2019 | clear_bit(0, &clp->cl_cb_slot_busy); |
| 2020 | copy_verf(clp, verf); |
| 2021 | rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); |
| 2022 | clp->cl_cb_session = NULL; |
| 2023 | clp->net = net; |
| 2024 | return clp; |
| 2025 | } |
| 2026 | |
| 2027 | static void |
| 2028 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 2029 | { |
| 2030 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 2031 | struct nfs4_client *clp; |
| 2032 | |
| 2033 | while (*new) { |
| 2034 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 2035 | parent = *new; |
| 2036 | |
| 2037 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 2038 | new = &((*new)->rb_left); |
| 2039 | else |
| 2040 | new = &((*new)->rb_right); |
| 2041 | } |
| 2042 | |
| 2043 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 2044 | rb_insert_color(&new_clp->cl_namenode, root); |
| 2045 | } |
| 2046 | |
| 2047 | static struct nfs4_client * |
| 2048 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 2049 | { |
| 2050 | int cmp; |
| 2051 | struct rb_node *node = root->rb_node; |
| 2052 | struct nfs4_client *clp; |
| 2053 | |
| 2054 | while (node) { |
| 2055 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 2056 | cmp = compare_blob(&clp->cl_name, name); |
| 2057 | if (cmp > 0) |
| 2058 | node = node->rb_left; |
| 2059 | else if (cmp < 0) |
| 2060 | node = node->rb_right; |
| 2061 | else |
| 2062 | return clp; |
| 2063 | } |
| 2064 | return NULL; |
| 2065 | } |
| 2066 | |
| 2067 | static void |
| 2068 | add_to_unconfirmed(struct nfs4_client *clp) |
| 2069 | { |
| 2070 | unsigned int idhashval; |
| 2071 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2072 | |
| 2073 | lockdep_assert_held(&nn->client_lock); |
| 2074 | |
| 2075 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
| 2076 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
| 2077 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
| 2078 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
| 2079 | renew_client_locked(clp); |
| 2080 | } |
| 2081 | |
| 2082 | static void |
| 2083 | move_to_confirmed(struct nfs4_client *clp) |
| 2084 | { |
| 2085 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
| 2086 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2087 | |
| 2088 | lockdep_assert_held(&nn->client_lock); |
| 2089 | |
| 2090 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
| 2091 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
| 2092 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
| 2093 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
| 2094 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
| 2095 | renew_client_locked(clp); |
| 2096 | } |
| 2097 | |
| 2098 | static struct nfs4_client * |
| 2099 | find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions) |
| 2100 | { |
| 2101 | struct nfs4_client *clp; |
| 2102 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 2103 | |
| 2104 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
| 2105 | if (same_clid(&clp->cl_clientid, clid)) { |
| 2106 | if ((bool)clp->cl_minorversion != sessions) |
| 2107 | return NULL; |
| 2108 | renew_client_locked(clp); |
| 2109 | return clp; |
| 2110 | } |
| 2111 | } |
| 2112 | return NULL; |
| 2113 | } |
| 2114 | |
| 2115 | static struct nfs4_client * |
| 2116 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 2117 | { |
| 2118 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 2119 | |
| 2120 | lockdep_assert_held(&nn->client_lock); |
| 2121 | return find_client_in_id_table(tbl, clid, sessions); |
| 2122 | } |
| 2123 | |
| 2124 | static struct nfs4_client * |
| 2125 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 2126 | { |
| 2127 | struct list_head *tbl = nn->unconf_id_hashtbl; |
| 2128 | |
| 2129 | lockdep_assert_held(&nn->client_lock); |
| 2130 | return find_client_in_id_table(tbl, clid, sessions); |
| 2131 | } |
| 2132 | |
| 2133 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
| 2134 | { |
| 2135 | return clp->cl_exchange_flags != 0; |
| 2136 | } |
| 2137 | |
| 2138 | static struct nfs4_client * |
| 2139 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
| 2140 | { |
| 2141 | lockdep_assert_held(&nn->client_lock); |
| 2142 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
| 2143 | } |
| 2144 | |
| 2145 | static struct nfs4_client * |
| 2146 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
| 2147 | { |
| 2148 | lockdep_assert_held(&nn->client_lock); |
| 2149 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
| 2150 | } |
| 2151 | |
| 2152 | static void |
| 2153 | gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp) |
| 2154 | { |
| 2155 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
| 2156 | struct sockaddr *sa = svc_addr(rqstp); |
| 2157 | u32 scopeid = rpc_get_scope_id(sa); |
| 2158 | unsigned short expected_family; |
| 2159 | |
| 2160 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 2161 | if (se->se_callback_netid_len == 3 && |
| 2162 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 2163 | expected_family = AF_INET; |
| 2164 | else if (se->se_callback_netid_len == 4 && |
| 2165 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 2166 | expected_family = AF_INET6; |
| 2167 | else |
| 2168 | goto out_err; |
| 2169 | |
| 2170 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
| 2171 | se->se_callback_addr_len, |
| 2172 | (struct sockaddr *)&conn->cb_addr, |
| 2173 | sizeof(conn->cb_addr)); |
| 2174 | |
| 2175 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
| 2176 | goto out_err; |
| 2177 | |
| 2178 | if (conn->cb_addr.ss_family == AF_INET6) |
| 2179 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
| 2180 | |
| 2181 | conn->cb_prog = se->se_callback_prog; |
| 2182 | conn->cb_ident = se->se_callback_ident; |
| 2183 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
| 2184 | return; |
| 2185 | out_err: |
| 2186 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 2187 | conn->cb_addrlen = 0; |
| 2188 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
| 2189 | "will not receive delegations\n", |
| 2190 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 2191 | |
| 2192 | return; |
| 2193 | } |
| 2194 | |
| 2195 | /* |
| 2196 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
| 2197 | */ |
| 2198 | static void |
| 2199 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 2200 | { |
| 2201 | struct xdr_buf *buf = resp->xdr.buf; |
| 2202 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 2203 | unsigned int base; |
| 2204 | |
| 2205 | dprintk("--> %s slot %p\n", __func__, slot); |
| 2206 | |
| 2207 | slot->sl_opcnt = resp->opcnt; |
| 2208 | slot->sl_status = resp->cstate.status; |
| 2209 | |
| 2210 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
| 2211 | if (nfsd4_not_cached(resp)) { |
| 2212 | slot->sl_datalen = 0; |
| 2213 | return; |
| 2214 | } |
| 2215 | base = resp->cstate.data_offset; |
| 2216 | slot->sl_datalen = buf->len - base; |
| 2217 | if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen)) |
| 2218 | WARN("%s: sessions DRC could not cache compound\n", __func__); |
| 2219 | return; |
| 2220 | } |
| 2221 | |
| 2222 | /* |
| 2223 | * Encode the replay sequence operation from the slot values. |
| 2224 | * If cachethis is FALSE encode the uncached rep error on the next |
| 2225 | * operation which sets resp->p and increments resp->opcnt for |
| 2226 | * nfs4svc_encode_compoundres. |
| 2227 | * |
| 2228 | */ |
| 2229 | static __be32 |
| 2230 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 2231 | struct nfsd4_compoundres *resp) |
| 2232 | { |
| 2233 | struct nfsd4_op *op; |
| 2234 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 2235 | |
| 2236 | /* Encode the replayed sequence operation */ |
| 2237 | op = &args->ops[resp->opcnt - 1]; |
| 2238 | nfsd4_encode_operation(resp, op); |
| 2239 | |
| 2240 | /* Return nfserr_retry_uncached_rep in next operation. */ |
| 2241 | if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) { |
| 2242 | op = &args->ops[resp->opcnt++]; |
| 2243 | op->status = nfserr_retry_uncached_rep; |
| 2244 | nfsd4_encode_operation(resp, op); |
| 2245 | } |
| 2246 | return op->status; |
| 2247 | } |
| 2248 | |
| 2249 | /* |
| 2250 | * The sequence operation is not cached because we can use the slot and |
| 2251 | * session values. |
| 2252 | */ |
| 2253 | static __be32 |
| 2254 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 2255 | struct nfsd4_sequence *seq) |
| 2256 | { |
| 2257 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 2258 | struct xdr_stream *xdr = &resp->xdr; |
| 2259 | __be32 *p; |
| 2260 | __be32 status; |
| 2261 | |
| 2262 | dprintk("--> %s slot %p\n", __func__, slot); |
| 2263 | |
| 2264 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
| 2265 | if (status) |
| 2266 | return status; |
| 2267 | |
| 2268 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 2269 | if (!p) { |
| 2270 | WARN_ON_ONCE(1); |
| 2271 | return nfserr_serverfault; |
| 2272 | } |
| 2273 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 2274 | xdr_commit_encode(xdr); |
| 2275 | |
| 2276 | resp->opcnt = slot->sl_opcnt; |
| 2277 | return slot->sl_status; |
| 2278 | } |
| 2279 | |
| 2280 | /* |
| 2281 | * Set the exchange_id flags returned by the server. |
| 2282 | */ |
| 2283 | static void |
| 2284 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 2285 | { |
| 2286 | #ifdef CONFIG_NFSD_PNFS |
| 2287 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; |
| 2288 | #else |
| 2289 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
| 2290 | #endif |
| 2291 | |
| 2292 | /* Referrals are supported, Migration is not. */ |
| 2293 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 2294 | |
| 2295 | /* set the wire flags to return to client. */ |
| 2296 | clid->flags = new->cl_exchange_flags; |
| 2297 | } |
| 2298 | |
| 2299 | static bool client_has_openowners(struct nfs4_client *clp) |
| 2300 | { |
| 2301 | struct nfs4_openowner *oo; |
| 2302 | |
| 2303 | list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { |
| 2304 | if (!list_empty(&oo->oo_owner.so_stateids)) |
| 2305 | return true; |
| 2306 | } |
| 2307 | return false; |
| 2308 | } |
| 2309 | |
| 2310 | static bool client_has_state(struct nfs4_client *clp) |
| 2311 | { |
| 2312 | return client_has_openowners(clp) |
| 2313 | #ifdef CONFIG_NFSD_PNFS |
| 2314 | || !list_empty(&clp->cl_lo_states) |
| 2315 | #endif |
| 2316 | || !list_empty(&clp->cl_delegations) |
| 2317 | || !list_empty(&clp->cl_sessions); |
| 2318 | } |
| 2319 | |
| 2320 | __be32 |
| 2321 | nfsd4_exchange_id(struct svc_rqst *rqstp, |
| 2322 | struct nfsd4_compound_state *cstate, |
| 2323 | struct nfsd4_exchange_id *exid) |
| 2324 | { |
| 2325 | struct nfs4_client *conf, *new; |
| 2326 | struct nfs4_client *unconf = NULL; |
| 2327 | __be32 status; |
| 2328 | char addr_str[INET6_ADDRSTRLEN]; |
| 2329 | nfs4_verifier verf = exid->verifier; |
| 2330 | struct sockaddr *sa = svc_addr(rqstp); |
| 2331 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
| 2332 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 2333 | |
| 2334 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
| 2335 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
| 2336 | "ip_addr=%s flags %x, spa_how %d\n", |
| 2337 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
| 2338 | addr_str, exid->flags, exid->spa_how); |
| 2339 | |
| 2340 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
| 2341 | return nfserr_inval; |
| 2342 | |
| 2343 | switch (exid->spa_how) { |
| 2344 | case SP4_MACH_CRED: |
| 2345 | if (!svc_rqst_integrity_protected(rqstp)) |
| 2346 | return nfserr_inval; |
| 2347 | case SP4_NONE: |
| 2348 | break; |
| 2349 | default: /* checked by xdr code */ |
| 2350 | WARN_ON_ONCE(1); |
| 2351 | case SP4_SSV: |
| 2352 | return nfserr_encr_alg_unsupp; |
| 2353 | } |
| 2354 | |
| 2355 | new = create_client(exid->clname, rqstp, &verf); |
| 2356 | if (new == NULL) |
| 2357 | return nfserr_jukebox; |
| 2358 | |
| 2359 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
| 2360 | spin_lock(&nn->client_lock); |
| 2361 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
| 2362 | if (conf) { |
| 2363 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 2364 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 2365 | |
| 2366 | if (update) { |
| 2367 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
| 2368 | status = nfserr_inval; |
| 2369 | goto out; |
| 2370 | } |
| 2371 | if (!mach_creds_match(conf, rqstp)) { |
| 2372 | status = nfserr_wrong_cred; |
| 2373 | goto out; |
| 2374 | } |
| 2375 | if (!creds_match) { /* case 9 */ |
| 2376 | status = nfserr_perm; |
| 2377 | goto out; |
| 2378 | } |
| 2379 | if (!verfs_match) { /* case 8 */ |
| 2380 | status = nfserr_not_same; |
| 2381 | goto out; |
| 2382 | } |
| 2383 | /* case 6 */ |
| 2384 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
| 2385 | goto out_copy; |
| 2386 | } |
| 2387 | if (!creds_match) { /* case 3 */ |
| 2388 | if (client_has_state(conf)) { |
| 2389 | status = nfserr_clid_inuse; |
| 2390 | goto out; |
| 2391 | } |
| 2392 | goto out_new; |
| 2393 | } |
| 2394 | if (verfs_match) { /* case 2 */ |
| 2395 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
| 2396 | goto out_copy; |
| 2397 | } |
| 2398 | /* case 5, client reboot */ |
| 2399 | conf = NULL; |
| 2400 | goto out_new; |
| 2401 | } |
| 2402 | |
| 2403 | if (update) { /* case 7 */ |
| 2404 | status = nfserr_noent; |
| 2405 | goto out; |
| 2406 | } |
| 2407 | |
| 2408 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
| 2409 | if (unconf) /* case 4, possible retry or client restart */ |
| 2410 | unhash_client_locked(unconf); |
| 2411 | |
| 2412 | /* case 1 (normal case) */ |
| 2413 | out_new: |
| 2414 | if (conf) { |
| 2415 | status = mark_client_expired_locked(conf); |
| 2416 | if (status) |
| 2417 | goto out; |
| 2418 | } |
| 2419 | new->cl_minorversion = cstate->minorversion; |
| 2420 | new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED); |
| 2421 | |
| 2422 | gen_clid(new, nn); |
| 2423 | add_to_unconfirmed(new); |
| 2424 | swap(new, conf); |
| 2425 | out_copy: |
| 2426 | exid->clientid.cl_boot = conf->cl_clientid.cl_boot; |
| 2427 | exid->clientid.cl_id = conf->cl_clientid.cl_id; |
| 2428 | |
| 2429 | exid->seqid = conf->cl_cs_slot.sl_seqid + 1; |
| 2430 | nfsd4_set_ex_flags(conf, exid); |
| 2431 | |
| 2432 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
| 2433 | conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); |
| 2434 | status = nfs_ok; |
| 2435 | |
| 2436 | out: |
| 2437 | spin_unlock(&nn->client_lock); |
| 2438 | if (new) |
| 2439 | expire_client(new); |
| 2440 | if (unconf) |
| 2441 | expire_client(unconf); |
| 2442 | return status; |
| 2443 | } |
| 2444 | |
| 2445 | static __be32 |
| 2446 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
| 2447 | { |
| 2448 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 2449 | slot_seqid); |
| 2450 | |
| 2451 | /* The slot is in use, and no response has been sent. */ |
| 2452 | if (slot_inuse) { |
| 2453 | if (seqid == slot_seqid) |
| 2454 | return nfserr_jukebox; |
| 2455 | else |
| 2456 | return nfserr_seq_misordered; |
| 2457 | } |
| 2458 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
| 2459 | if (likely(seqid == slot_seqid + 1)) |
| 2460 | return nfs_ok; |
| 2461 | if (seqid == slot_seqid) |
| 2462 | return nfserr_replay_cache; |
| 2463 | return nfserr_seq_misordered; |
| 2464 | } |
| 2465 | |
| 2466 | /* |
| 2467 | * Cache the create session result into the create session single DRC |
| 2468 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 2469 | * Do this for solo or embedded create session operations. |
| 2470 | */ |
| 2471 | static void |
| 2472 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
| 2473 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
| 2474 | { |
| 2475 | slot->sl_status = nfserr; |
| 2476 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 2477 | } |
| 2478 | |
| 2479 | static __be32 |
| 2480 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 2481 | struct nfsd4_clid_slot *slot) |
| 2482 | { |
| 2483 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 2484 | return slot->sl_status; |
| 2485 | } |
| 2486 | |
| 2487 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 2488 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 2489 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2490 | 3 + /* version, opcount, opcode */ \ |
| 2491 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2492 | /* seqid, slotID, slotID, cache */ \ |
| 2493 | 4 ) * sizeof(__be32)) |
| 2494 | |
| 2495 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 2496 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 2497 | 1 + /* status */ \ |
| 2498 | 1 + /* MIN tag is length with zero, only length */ \ |
| 2499 | 3 + /* opcount, opcode, opstatus*/ \ |
| 2500 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 2501 | /* seqid, slotID, slotID, slotID, status */ \ |
| 2502 | 5 ) * sizeof(__be32)) |
| 2503 | |
| 2504 | static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) |
| 2505 | { |
| 2506 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 2507 | |
| 2508 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 2509 | return nfserr_toosmall; |
| 2510 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 2511 | return nfserr_toosmall; |
| 2512 | ca->headerpadsz = 0; |
| 2513 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 2514 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 2515 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 2516 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 2517 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 2518 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 2519 | /* |
| 2520 | * Note decreasing slot size below client's request may make it |
| 2521 | * difficult for client to function correctly, whereas |
| 2522 | * decreasing the number of slots will (just?) affect |
| 2523 | * performance. When short on memory we therefore prefer to |
| 2524 | * decrease number of slots instead of their size. Clients that |
| 2525 | * request larger slots than they need will get poor results: |
| 2526 | */ |
| 2527 | ca->maxreqs = nfsd4_get_drc_mem(ca); |
| 2528 | if (!ca->maxreqs) |
| 2529 | return nfserr_jukebox; |
| 2530 | |
| 2531 | return nfs_ok; |
| 2532 | } |
| 2533 | |
| 2534 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
| 2535 | RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32)) |
| 2536 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
| 2537 | RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32)) |
| 2538 | |
| 2539 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 2540 | { |
| 2541 | ca->headerpadsz = 0; |
| 2542 | |
| 2543 | /* |
| 2544 | * These RPC_MAX_HEADER macros are overkill, especially since we |
| 2545 | * don't even do gss on the backchannel yet. But this is still |
| 2546 | * less than 1k. Tighten up this estimate in the unlikely event |
| 2547 | * it turns out to be a problem for some client: |
| 2548 | */ |
| 2549 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
| 2550 | return nfserr_toosmall; |
| 2551 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
| 2552 | return nfserr_toosmall; |
| 2553 | ca->maxresp_cached = 0; |
| 2554 | if (ca->maxops < 2) |
| 2555 | return nfserr_toosmall; |
| 2556 | |
| 2557 | return nfs_ok; |
| 2558 | } |
| 2559 | |
| 2560 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 2561 | { |
| 2562 | switch (cbs->flavor) { |
| 2563 | case RPC_AUTH_NULL: |
| 2564 | case RPC_AUTH_UNIX: |
| 2565 | return nfs_ok; |
| 2566 | default: |
| 2567 | /* |
| 2568 | * GSS case: the spec doesn't allow us to return this |
| 2569 | * error. But it also doesn't allow us not to support |
| 2570 | * GSS. |
| 2571 | * I'd rather this fail hard than return some error the |
| 2572 | * client might think it can already handle: |
| 2573 | */ |
| 2574 | return nfserr_encr_alg_unsupp; |
| 2575 | } |
| 2576 | } |
| 2577 | |
| 2578 | __be32 |
| 2579 | nfsd4_create_session(struct svc_rqst *rqstp, |
| 2580 | struct nfsd4_compound_state *cstate, |
| 2581 | struct nfsd4_create_session *cr_ses) |
| 2582 | { |
| 2583 | struct sockaddr *sa = svc_addr(rqstp); |
| 2584 | struct nfs4_client *conf, *unconf; |
| 2585 | struct nfs4_client *old = NULL; |
| 2586 | struct nfsd4_session *new; |
| 2587 | struct nfsd4_conn *conn; |
| 2588 | struct nfsd4_clid_slot *cs_slot = NULL; |
| 2589 | __be32 status = 0; |
| 2590 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 2591 | |
| 2592 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 2593 | return nfserr_inval; |
| 2594 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 2595 | if (status) |
| 2596 | return status; |
| 2597 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
| 2598 | if (status) |
| 2599 | return status; |
| 2600 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 2601 | if (status) |
| 2602 | goto out_release_drc_mem; |
| 2603 | status = nfserr_jukebox; |
| 2604 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
| 2605 | if (!new) |
| 2606 | goto out_release_drc_mem; |
| 2607 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 2608 | if (!conn) |
| 2609 | goto out_free_session; |
| 2610 | |
| 2611 | spin_lock(&nn->client_lock); |
| 2612 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
| 2613 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
| 2614 | WARN_ON_ONCE(conf && unconf); |
| 2615 | |
| 2616 | if (conf) { |
| 2617 | status = nfserr_wrong_cred; |
| 2618 | if (!mach_creds_match(conf, rqstp)) |
| 2619 | goto out_free_conn; |
| 2620 | cs_slot = &conf->cl_cs_slot; |
| 2621 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
| 2622 | if (status) { |
| 2623 | if (status == nfserr_replay_cache) |
| 2624 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
| 2625 | goto out_free_conn; |
| 2626 | } |
| 2627 | } else if (unconf) { |
| 2628 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
| 2629 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
| 2630 | status = nfserr_clid_inuse; |
| 2631 | goto out_free_conn; |
| 2632 | } |
| 2633 | status = nfserr_wrong_cred; |
| 2634 | if (!mach_creds_match(unconf, rqstp)) |
| 2635 | goto out_free_conn; |
| 2636 | cs_slot = &unconf->cl_cs_slot; |
| 2637 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
| 2638 | if (status) { |
| 2639 | /* an unconfirmed replay returns misordered */ |
| 2640 | status = nfserr_seq_misordered; |
| 2641 | goto out_free_conn; |
| 2642 | } |
| 2643 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
| 2644 | if (old) { |
| 2645 | status = mark_client_expired_locked(old); |
| 2646 | if (status) { |
| 2647 | old = NULL; |
| 2648 | goto out_free_conn; |
| 2649 | } |
| 2650 | } |
| 2651 | move_to_confirmed(unconf); |
| 2652 | conf = unconf; |
| 2653 | } else { |
| 2654 | status = nfserr_stale_clientid; |
| 2655 | goto out_free_conn; |
| 2656 | } |
| 2657 | status = nfs_ok; |
| 2658 | /* |
| 2659 | * We do not support RDMA or persistent sessions |
| 2660 | */ |
| 2661 | cr_ses->flags &= ~SESSION4_PERSIST; |
| 2662 | cr_ses->flags &= ~SESSION4_RDMA; |
| 2663 | |
| 2664 | init_session(rqstp, new, conf, cr_ses); |
| 2665 | nfsd4_get_session_locked(new); |
| 2666 | |
| 2667 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
| 2668 | NFS4_MAX_SESSIONID_LEN); |
| 2669 | cs_slot->sl_seqid++; |
| 2670 | cr_ses->seqid = cs_slot->sl_seqid; |
| 2671 | |
| 2672 | /* cache solo and embedded create sessions under the client_lock */ |
| 2673 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
| 2674 | spin_unlock(&nn->client_lock); |
| 2675 | /* init connection and backchannel */ |
| 2676 | nfsd4_init_conn(rqstp, conn, new); |
| 2677 | nfsd4_put_session(new); |
| 2678 | if (old) |
| 2679 | expire_client(old); |
| 2680 | return status; |
| 2681 | out_free_conn: |
| 2682 | spin_unlock(&nn->client_lock); |
| 2683 | free_conn(conn); |
| 2684 | if (old) |
| 2685 | expire_client(old); |
| 2686 | out_free_session: |
| 2687 | __free_session(new); |
| 2688 | out_release_drc_mem: |
| 2689 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
| 2690 | return status; |
| 2691 | } |
| 2692 | |
| 2693 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 2694 | { |
| 2695 | switch (*dir) { |
| 2696 | case NFS4_CDFC4_FORE: |
| 2697 | case NFS4_CDFC4_BACK: |
| 2698 | return nfs_ok; |
| 2699 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 2700 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 2701 | *dir = NFS4_CDFC4_BOTH; |
| 2702 | return nfs_ok; |
| 2703 | }; |
| 2704 | return nfserr_inval; |
| 2705 | } |
| 2706 | |
| 2707 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc) |
| 2708 | { |
| 2709 | struct nfsd4_session *session = cstate->session; |
| 2710 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 2711 | __be32 status; |
| 2712 | |
| 2713 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 2714 | if (status) |
| 2715 | return status; |
| 2716 | spin_lock(&nn->client_lock); |
| 2717 | session->se_cb_prog = bc->bc_cb_program; |
| 2718 | session->se_cb_sec = bc->bc_cb_sec; |
| 2719 | spin_unlock(&nn->client_lock); |
| 2720 | |
| 2721 | nfsd4_probe_callback(session->se_client); |
| 2722 | |
| 2723 | return nfs_ok; |
| 2724 | } |
| 2725 | |
| 2726 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 2727 | struct nfsd4_compound_state *cstate, |
| 2728 | struct nfsd4_bind_conn_to_session *bcts) |
| 2729 | { |
| 2730 | __be32 status; |
| 2731 | struct nfsd4_conn *conn; |
| 2732 | struct nfsd4_session *session; |
| 2733 | struct net *net = SVC_NET(rqstp); |
| 2734 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 2735 | |
| 2736 | if (!nfsd4_last_compound_op(rqstp)) |
| 2737 | return nfserr_not_only_op; |
| 2738 | spin_lock(&nn->client_lock); |
| 2739 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
| 2740 | spin_unlock(&nn->client_lock); |
| 2741 | if (!session) |
| 2742 | goto out_no_session; |
| 2743 | status = nfserr_wrong_cred; |
| 2744 | if (!mach_creds_match(session->se_client, rqstp)) |
| 2745 | goto out; |
| 2746 | status = nfsd4_map_bcts_dir(&bcts->dir); |
| 2747 | if (status) |
| 2748 | goto out; |
| 2749 | conn = alloc_conn(rqstp, bcts->dir); |
| 2750 | status = nfserr_jukebox; |
| 2751 | if (!conn) |
| 2752 | goto out; |
| 2753 | nfsd4_init_conn(rqstp, conn, session); |
| 2754 | status = nfs_ok; |
| 2755 | out: |
| 2756 | nfsd4_put_session(session); |
| 2757 | out_no_session: |
| 2758 | return status; |
| 2759 | } |
| 2760 | |
| 2761 | static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid) |
| 2762 | { |
| 2763 | if (!session) |
| 2764 | return 0; |
| 2765 | return !memcmp(sid, &session->se_sessionid, sizeof(*sid)); |
| 2766 | } |
| 2767 | |
| 2768 | __be32 |
| 2769 | nfsd4_destroy_session(struct svc_rqst *r, |
| 2770 | struct nfsd4_compound_state *cstate, |
| 2771 | struct nfsd4_destroy_session *sessionid) |
| 2772 | { |
| 2773 | struct nfsd4_session *ses; |
| 2774 | __be32 status; |
| 2775 | int ref_held_by_me = 0; |
| 2776 | struct net *net = SVC_NET(r); |
| 2777 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 2778 | |
| 2779 | status = nfserr_not_only_op; |
| 2780 | if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) { |
| 2781 | if (!nfsd4_last_compound_op(r)) |
| 2782 | goto out; |
| 2783 | ref_held_by_me++; |
| 2784 | } |
| 2785 | dump_sessionid(__func__, &sessionid->sessionid); |
| 2786 | spin_lock(&nn->client_lock); |
| 2787 | ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status); |
| 2788 | if (!ses) |
| 2789 | goto out_client_lock; |
| 2790 | status = nfserr_wrong_cred; |
| 2791 | if (!mach_creds_match(ses->se_client, r)) |
| 2792 | goto out_put_session; |
| 2793 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
| 2794 | if (status) |
| 2795 | goto out_put_session; |
| 2796 | unhash_session(ses); |
| 2797 | spin_unlock(&nn->client_lock); |
| 2798 | |
| 2799 | nfsd4_probe_callback_sync(ses->se_client); |
| 2800 | |
| 2801 | spin_lock(&nn->client_lock); |
| 2802 | status = nfs_ok; |
| 2803 | out_put_session: |
| 2804 | nfsd4_put_session_locked(ses); |
| 2805 | out_client_lock: |
| 2806 | spin_unlock(&nn->client_lock); |
| 2807 | out: |
| 2808 | return status; |
| 2809 | } |
| 2810 | |
| 2811 | static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) |
| 2812 | { |
| 2813 | struct nfsd4_conn *c; |
| 2814 | |
| 2815 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
| 2816 | if (c->cn_xprt == xpt) { |
| 2817 | return c; |
| 2818 | } |
| 2819 | } |
| 2820 | return NULL; |
| 2821 | } |
| 2822 | |
| 2823 | static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses) |
| 2824 | { |
| 2825 | struct nfs4_client *clp = ses->se_client; |
| 2826 | struct nfsd4_conn *c; |
| 2827 | __be32 status = nfs_ok; |
| 2828 | int ret; |
| 2829 | |
| 2830 | spin_lock(&clp->cl_lock); |
| 2831 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
| 2832 | if (c) |
| 2833 | goto out_free; |
| 2834 | status = nfserr_conn_not_bound_to_session; |
| 2835 | if (clp->cl_mach_cred) |
| 2836 | goto out_free; |
| 2837 | __nfsd4_hash_conn(new, ses); |
| 2838 | spin_unlock(&clp->cl_lock); |
| 2839 | ret = nfsd4_register_conn(new); |
| 2840 | if (ret) |
| 2841 | /* oops; xprt is already down: */ |
| 2842 | nfsd4_conn_lost(&new->cn_xpt_user); |
| 2843 | return nfs_ok; |
| 2844 | out_free: |
| 2845 | spin_unlock(&clp->cl_lock); |
| 2846 | free_conn(new); |
| 2847 | return status; |
| 2848 | } |
| 2849 | |
| 2850 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 2851 | { |
| 2852 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 2853 | |
| 2854 | return args->opcnt > session->se_fchannel.maxops; |
| 2855 | } |
| 2856 | |
| 2857 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 2858 | struct nfsd4_session *session) |
| 2859 | { |
| 2860 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 2861 | |
| 2862 | return xb->len > session->se_fchannel.maxreq_sz; |
| 2863 | } |
| 2864 | |
| 2865 | __be32 |
| 2866 | nfsd4_sequence(struct svc_rqst *rqstp, |
| 2867 | struct nfsd4_compound_state *cstate, |
| 2868 | struct nfsd4_sequence *seq) |
| 2869 | { |
| 2870 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
| 2871 | struct xdr_stream *xdr = &resp->xdr; |
| 2872 | struct nfsd4_session *session; |
| 2873 | struct nfs4_client *clp; |
| 2874 | struct nfsd4_slot *slot; |
| 2875 | struct nfsd4_conn *conn; |
| 2876 | __be32 status; |
| 2877 | int buflen; |
| 2878 | struct net *net = SVC_NET(rqstp); |
| 2879 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 2880 | |
| 2881 | if (resp->opcnt != 1) |
| 2882 | return nfserr_sequence_pos; |
| 2883 | |
| 2884 | /* |
| 2885 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 2886 | * below. |
| 2887 | */ |
| 2888 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 2889 | if (!conn) |
| 2890 | return nfserr_jukebox; |
| 2891 | |
| 2892 | spin_lock(&nn->client_lock); |
| 2893 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
| 2894 | if (!session) |
| 2895 | goto out_no_session; |
| 2896 | clp = session->se_client; |
| 2897 | |
| 2898 | status = nfserr_too_many_ops; |
| 2899 | if (nfsd4_session_too_many_ops(rqstp, session)) |
| 2900 | goto out_put_session; |
| 2901 | |
| 2902 | status = nfserr_req_too_big; |
| 2903 | if (nfsd4_request_too_big(rqstp, session)) |
| 2904 | goto out_put_session; |
| 2905 | |
| 2906 | status = nfserr_badslot; |
| 2907 | if (seq->slotid >= session->se_fchannel.maxreqs) |
| 2908 | goto out_put_session; |
| 2909 | |
| 2910 | slot = session->se_slots[seq->slotid]; |
| 2911 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 2912 | |
| 2913 | /* We do not negotiate the number of slots yet, so set the |
| 2914 | * maxslots to the session maxreqs which is used to encode |
| 2915 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 2916 | seq->maxslots = session->se_fchannel.maxreqs; |
| 2917 | |
| 2918 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 2919 | slot->sl_flags & NFSD4_SLOT_INUSE); |
| 2920 | if (status == nfserr_replay_cache) { |
| 2921 | status = nfserr_seq_misordered; |
| 2922 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
| 2923 | goto out_put_session; |
| 2924 | cstate->slot = slot; |
| 2925 | cstate->session = session; |
| 2926 | cstate->clp = clp; |
| 2927 | /* Return the cached reply status and set cstate->status |
| 2928 | * for nfsd4_proc_compound processing */ |
| 2929 | status = nfsd4_replay_cache_entry(resp, seq); |
| 2930 | cstate->status = nfserr_replay_cache; |
| 2931 | goto out; |
| 2932 | } |
| 2933 | if (status) |
| 2934 | goto out_put_session; |
| 2935 | |
| 2936 | status = nfsd4_sequence_check_conn(conn, session); |
| 2937 | conn = NULL; |
| 2938 | if (status) |
| 2939 | goto out_put_session; |
| 2940 | |
| 2941 | buflen = (seq->cachethis) ? |
| 2942 | session->se_fchannel.maxresp_cached : |
| 2943 | session->se_fchannel.maxresp_sz; |
| 2944 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 2945 | nfserr_rep_too_big; |
| 2946 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
| 2947 | goto out_put_session; |
| 2948 | svc_reserve(rqstp, buflen); |
| 2949 | |
| 2950 | status = nfs_ok; |
| 2951 | /* Success! bump slot seqid */ |
| 2952 | slot->sl_seqid = seq->seqid; |
| 2953 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
| 2954 | if (seq->cachethis) |
| 2955 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
| 2956 | else |
| 2957 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
| 2958 | |
| 2959 | cstate->slot = slot; |
| 2960 | cstate->session = session; |
| 2961 | cstate->clp = clp; |
| 2962 | |
| 2963 | out: |
| 2964 | switch (clp->cl_cb_state) { |
| 2965 | case NFSD4_CB_DOWN: |
| 2966 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 2967 | break; |
| 2968 | case NFSD4_CB_FAULT: |
| 2969 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 2970 | break; |
| 2971 | default: |
| 2972 | seq->status_flags = 0; |
| 2973 | } |
| 2974 | if (!list_empty(&clp->cl_revoked)) |
| 2975 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
| 2976 | out_no_session: |
| 2977 | if (conn) |
| 2978 | free_conn(conn); |
| 2979 | spin_unlock(&nn->client_lock); |
| 2980 | return status; |
| 2981 | out_put_session: |
| 2982 | nfsd4_put_session_locked(session); |
| 2983 | goto out_no_session; |
| 2984 | } |
| 2985 | |
| 2986 | void |
| 2987 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 2988 | { |
| 2989 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 2990 | |
| 2991 | if (nfsd4_has_session(cs)) { |
| 2992 | if (cs->status != nfserr_replay_cache) { |
| 2993 | nfsd4_store_cache_entry(resp); |
| 2994 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 2995 | } |
| 2996 | /* Drop session reference that was taken in nfsd4_sequence() */ |
| 2997 | nfsd4_put_session(cs->session); |
| 2998 | } else if (cs->clp) |
| 2999 | put_client_renew(cs->clp); |
| 3000 | } |
| 3001 | |
| 3002 | __be32 |
| 3003 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc) |
| 3004 | { |
| 3005 | struct nfs4_client *conf, *unconf; |
| 3006 | struct nfs4_client *clp = NULL; |
| 3007 | __be32 status = 0; |
| 3008 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 3009 | |
| 3010 | spin_lock(&nn->client_lock); |
| 3011 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
| 3012 | conf = find_confirmed_client(&dc->clientid, true, nn); |
| 3013 | WARN_ON_ONCE(conf && unconf); |
| 3014 | |
| 3015 | if (conf) { |
| 3016 | if (client_has_state(conf)) { |
| 3017 | status = nfserr_clientid_busy; |
| 3018 | goto out; |
| 3019 | } |
| 3020 | status = mark_client_expired_locked(conf); |
| 3021 | if (status) |
| 3022 | goto out; |
| 3023 | clp = conf; |
| 3024 | } else if (unconf) |
| 3025 | clp = unconf; |
| 3026 | else { |
| 3027 | status = nfserr_stale_clientid; |
| 3028 | goto out; |
| 3029 | } |
| 3030 | if (!mach_creds_match(clp, rqstp)) { |
| 3031 | clp = NULL; |
| 3032 | status = nfserr_wrong_cred; |
| 3033 | goto out; |
| 3034 | } |
| 3035 | unhash_client_locked(clp); |
| 3036 | out: |
| 3037 | spin_unlock(&nn->client_lock); |
| 3038 | if (clp) |
| 3039 | expire_client(clp); |
| 3040 | return status; |
| 3041 | } |
| 3042 | |
| 3043 | __be32 |
| 3044 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc) |
| 3045 | { |
| 3046 | __be32 status = 0; |
| 3047 | |
| 3048 | if (rc->rca_one_fs) { |
| 3049 | if (!cstate->current_fh.fh_dentry) |
| 3050 | return nfserr_nofilehandle; |
| 3051 | /* |
| 3052 | * We don't take advantage of the rca_one_fs case. |
| 3053 | * That's OK, it's optional, we can safely ignore it. |
| 3054 | */ |
| 3055 | return nfs_ok; |
| 3056 | } |
| 3057 | |
| 3058 | status = nfserr_complete_already; |
| 3059 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
| 3060 | &cstate->session->se_client->cl_flags)) |
| 3061 | goto out; |
| 3062 | |
| 3063 | status = nfserr_stale_clientid; |
| 3064 | if (is_client_expired(cstate->session->se_client)) |
| 3065 | /* |
| 3066 | * The following error isn't really legal. |
| 3067 | * But we only get here if the client just explicitly |
| 3068 | * destroyed the client. Surely it no longer cares what |
| 3069 | * error it gets back on an operation for the dead |
| 3070 | * client. |
| 3071 | */ |
| 3072 | goto out; |
| 3073 | |
| 3074 | status = nfs_ok; |
| 3075 | nfsd4_client_record_create(cstate->session->se_client); |
| 3076 | out: |
| 3077 | return status; |
| 3078 | } |
| 3079 | |
| 3080 | __be32 |
| 3081 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3082 | struct nfsd4_setclientid *setclid) |
| 3083 | { |
| 3084 | struct xdr_netobj clname = setclid->se_name; |
| 3085 | nfs4_verifier clverifier = setclid->se_verf; |
| 3086 | struct nfs4_client *conf, *new; |
| 3087 | struct nfs4_client *unconf = NULL; |
| 3088 | __be32 status; |
| 3089 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 3090 | |
| 3091 | new = create_client(clname, rqstp, &clverifier); |
| 3092 | if (new == NULL) |
| 3093 | return nfserr_jukebox; |
| 3094 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
| 3095 | spin_lock(&nn->client_lock); |
| 3096 | conf = find_confirmed_client_by_name(&clname, nn); |
| 3097 | if (conf && client_has_state(conf)) { |
| 3098 | /* case 0: */ |
| 3099 | status = nfserr_clid_inuse; |
| 3100 | if (clp_used_exchangeid(conf)) |
| 3101 | goto out; |
| 3102 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
| 3103 | char addr_str[INET6_ADDRSTRLEN]; |
| 3104 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, |
| 3105 | sizeof(addr_str)); |
| 3106 | dprintk("NFSD: setclientid: string in use by client " |
| 3107 | "at %s\n", addr_str); |
| 3108 | goto out; |
| 3109 | } |
| 3110 | } |
| 3111 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
| 3112 | if (unconf) |
| 3113 | unhash_client_locked(unconf); |
| 3114 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) { |
| 3115 | /* case 1: probable callback update */ |
| 3116 | copy_clid(new, conf); |
| 3117 | gen_confirm(new, nn); |
| 3118 | } else /* case 4 (new client) or cases 2, 3 (client reboot): */ |
| 3119 | gen_clid(new, nn); |
| 3120 | new->cl_minorversion = 0; |
| 3121 | gen_callback(new, setclid, rqstp); |
| 3122 | add_to_unconfirmed(new); |
| 3123 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 3124 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 3125 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); |
| 3126 | new = NULL; |
| 3127 | status = nfs_ok; |
| 3128 | out: |
| 3129 | spin_unlock(&nn->client_lock); |
| 3130 | if (new) |
| 3131 | free_client(new); |
| 3132 | if (unconf) |
| 3133 | expire_client(unconf); |
| 3134 | return status; |
| 3135 | } |
| 3136 | |
| 3137 | |
| 3138 | __be32 |
| 3139 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
| 3140 | struct nfsd4_compound_state *cstate, |
| 3141 | struct nfsd4_setclientid_confirm *setclientid_confirm) |
| 3142 | { |
| 3143 | struct nfs4_client *conf, *unconf; |
| 3144 | struct nfs4_client *old = NULL; |
| 3145 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 3146 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
| 3147 | __be32 status; |
| 3148 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 3149 | |
| 3150 | if (STALE_CLIENTID(clid, nn)) |
| 3151 | return nfserr_stale_clientid; |
| 3152 | |
| 3153 | spin_lock(&nn->client_lock); |
| 3154 | conf = find_confirmed_client(clid, false, nn); |
| 3155 | unconf = find_unconfirmed_client(clid, false, nn); |
| 3156 | /* |
| 3157 | * We try hard to give out unique clientid's, so if we get an |
| 3158 | * attempt to confirm the same clientid with a different cred, |
| 3159 | * the client may be buggy; this should never happen. |
| 3160 | * |
| 3161 | * Nevertheless, RFC 7530 recommends INUSE for this case: |
| 3162 | */ |
| 3163 | status = nfserr_clid_inuse; |
| 3164 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
| 3165 | goto out; |
| 3166 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
| 3167 | goto out; |
| 3168 | /* cases below refer to rfc 3530 section 14.2.34: */ |
| 3169 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
| 3170 | if (conf && !unconf) /* case 2: probable retransmit */ |
| 3171 | status = nfs_ok; |
| 3172 | else /* case 4: client hasn't noticed we rebooted yet? */ |
| 3173 | status = nfserr_stale_clientid; |
| 3174 | goto out; |
| 3175 | } |
| 3176 | status = nfs_ok; |
| 3177 | if (conf) { /* case 1: callback update */ |
| 3178 | old = unconf; |
| 3179 | unhash_client_locked(old); |
| 3180 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
| 3181 | } else { /* case 3: normal case; new or rebooted client */ |
| 3182 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
| 3183 | if (old) { |
| 3184 | status = nfserr_clid_inuse; |
| 3185 | if (client_has_state(old) |
| 3186 | && !same_creds(&unconf->cl_cred, |
| 3187 | &old->cl_cred)) |
| 3188 | goto out; |
| 3189 | status = mark_client_expired_locked(old); |
| 3190 | if (status) { |
| 3191 | old = NULL; |
| 3192 | goto out; |
| 3193 | } |
| 3194 | } |
| 3195 | move_to_confirmed(unconf); |
| 3196 | conf = unconf; |
| 3197 | } |
| 3198 | get_client_locked(conf); |
| 3199 | spin_unlock(&nn->client_lock); |
| 3200 | nfsd4_probe_callback(conf); |
| 3201 | spin_lock(&nn->client_lock); |
| 3202 | put_client_renew_locked(conf); |
| 3203 | out: |
| 3204 | spin_unlock(&nn->client_lock); |
| 3205 | if (old) |
| 3206 | expire_client(old); |
| 3207 | return status; |
| 3208 | } |
| 3209 | |
| 3210 | static struct nfs4_file *nfsd4_alloc_file(void) |
| 3211 | { |
| 3212 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 3213 | } |
| 3214 | |
| 3215 | /* OPEN Share state helper functions */ |
| 3216 | static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval, |
| 3217 | struct nfs4_file *fp) |
| 3218 | { |
| 3219 | lockdep_assert_held(&state_lock); |
| 3220 | |
| 3221 | atomic_set(&fp->fi_ref, 1); |
| 3222 | spin_lock_init(&fp->fi_lock); |
| 3223 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 3224 | INIT_LIST_HEAD(&fp->fi_delegations); |
| 3225 | INIT_LIST_HEAD(&fp->fi_clnt_odstate); |
| 3226 | fh_copy_shallow(&fp->fi_fhandle, fh); |
| 3227 | fp->fi_deleg_file = NULL; |
| 3228 | fp->fi_had_conflict = false; |
| 3229 | fp->fi_share_deny = 0; |
| 3230 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 3231 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
| 3232 | #ifdef CONFIG_NFSD_PNFS |
| 3233 | INIT_LIST_HEAD(&fp->fi_lo_states); |
| 3234 | atomic_set(&fp->fi_lo_recalls, 0); |
| 3235 | #endif |
| 3236 | hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]); |
| 3237 | } |
| 3238 | |
| 3239 | void |
| 3240 | nfsd4_free_slabs(void) |
| 3241 | { |
| 3242 | kmem_cache_destroy(odstate_slab); |
| 3243 | kmem_cache_destroy(openowner_slab); |
| 3244 | kmem_cache_destroy(lockowner_slab); |
| 3245 | kmem_cache_destroy(file_slab); |
| 3246 | kmem_cache_destroy(stateid_slab); |
| 3247 | kmem_cache_destroy(deleg_slab); |
| 3248 | } |
| 3249 | |
| 3250 | int |
| 3251 | nfsd4_init_slabs(void) |
| 3252 | { |
| 3253 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 3254 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 3255 | if (openowner_slab == NULL) |
| 3256 | goto out; |
| 3257 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
| 3258 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
| 3259 | if (lockowner_slab == NULL) |
| 3260 | goto out_free_openowner_slab; |
| 3261 | file_slab = kmem_cache_create("nfsd4_files", |
| 3262 | sizeof(struct nfs4_file), 0, 0, NULL); |
| 3263 | if (file_slab == NULL) |
| 3264 | goto out_free_lockowner_slab; |
| 3265 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
| 3266 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
| 3267 | if (stateid_slab == NULL) |
| 3268 | goto out_free_file_slab; |
| 3269 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
| 3270 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
| 3271 | if (deleg_slab == NULL) |
| 3272 | goto out_free_stateid_slab; |
| 3273 | odstate_slab = kmem_cache_create("nfsd4_odstate", |
| 3274 | sizeof(struct nfs4_clnt_odstate), 0, 0, NULL); |
| 3275 | if (odstate_slab == NULL) |
| 3276 | goto out_free_deleg_slab; |
| 3277 | return 0; |
| 3278 | |
| 3279 | out_free_deleg_slab: |
| 3280 | kmem_cache_destroy(deleg_slab); |
| 3281 | out_free_stateid_slab: |
| 3282 | kmem_cache_destroy(stateid_slab); |
| 3283 | out_free_file_slab: |
| 3284 | kmem_cache_destroy(file_slab); |
| 3285 | out_free_lockowner_slab: |
| 3286 | kmem_cache_destroy(lockowner_slab); |
| 3287 | out_free_openowner_slab: |
| 3288 | kmem_cache_destroy(openowner_slab); |
| 3289 | out: |
| 3290 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 3291 | return -ENOMEM; |
| 3292 | } |
| 3293 | |
| 3294 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 3295 | { |
| 3296 | rp->rp_status = nfserr_serverfault; |
| 3297 | rp->rp_buflen = 0; |
| 3298 | rp->rp_buf = rp->rp_ibuf; |
| 3299 | mutex_init(&rp->rp_mutex); |
| 3300 | } |
| 3301 | |
| 3302 | static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, |
| 3303 | struct nfs4_stateowner *so) |
| 3304 | { |
| 3305 | if (!nfsd4_has_session(cstate)) { |
| 3306 | mutex_lock(&so->so_replay.rp_mutex); |
| 3307 | cstate->replay_owner = nfs4_get_stateowner(so); |
| 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) |
| 3312 | { |
| 3313 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 3314 | |
| 3315 | if (so != NULL) { |
| 3316 | cstate->replay_owner = NULL; |
| 3317 | mutex_unlock(&so->so_replay.rp_mutex); |
| 3318 | nfs4_put_stateowner(so); |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp) |
| 3323 | { |
| 3324 | struct nfs4_stateowner *sop; |
| 3325 | |
| 3326 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
| 3327 | if (!sop) |
| 3328 | return NULL; |
| 3329 | |
| 3330 | sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL); |
| 3331 | if (!sop->so_owner.data) { |
| 3332 | kmem_cache_free(slab, sop); |
| 3333 | return NULL; |
| 3334 | } |
| 3335 | sop->so_owner.len = owner->len; |
| 3336 | |
| 3337 | INIT_LIST_HEAD(&sop->so_stateids); |
| 3338 | sop->so_client = clp; |
| 3339 | init_nfs4_replay(&sop->so_replay); |
| 3340 | atomic_set(&sop->so_count, 1); |
| 3341 | return sop; |
| 3342 | } |
| 3343 | |
| 3344 | static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval) |
| 3345 | { |
| 3346 | lockdep_assert_held(&clp->cl_lock); |
| 3347 | |
| 3348 | list_add(&oo->oo_owner.so_strhash, |
| 3349 | &clp->cl_ownerstr_hashtbl[strhashval]); |
| 3350 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
| 3351 | } |
| 3352 | |
| 3353 | static void nfs4_unhash_openowner(struct nfs4_stateowner *so) |
| 3354 | { |
| 3355 | unhash_openowner_locked(openowner(so)); |
| 3356 | } |
| 3357 | |
| 3358 | static void nfs4_free_openowner(struct nfs4_stateowner *so) |
| 3359 | { |
| 3360 | struct nfs4_openowner *oo = openowner(so); |
| 3361 | |
| 3362 | kmem_cache_free(openowner_slab, oo); |
| 3363 | } |
| 3364 | |
| 3365 | static const struct nfs4_stateowner_operations openowner_ops = { |
| 3366 | .so_unhash = nfs4_unhash_openowner, |
| 3367 | .so_free = nfs4_free_openowner, |
| 3368 | }; |
| 3369 | |
| 3370 | static struct nfs4_ol_stateid * |
| 3371 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 3372 | { |
| 3373 | struct nfs4_ol_stateid *local, *ret = NULL; |
| 3374 | struct nfs4_openowner *oo = open->op_openowner; |
| 3375 | |
| 3376 | lockdep_assert_held(&fp->fi_lock); |
| 3377 | |
| 3378 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
| 3379 | /* ignore lock owners */ |
| 3380 | if (local->st_stateowner->so_is_open_owner == 0) |
| 3381 | continue; |
| 3382 | if (local->st_stateowner == &oo->oo_owner) { |
| 3383 | ret = local; |
| 3384 | atomic_inc(&ret->st_stid.sc_count); |
| 3385 | break; |
| 3386 | } |
| 3387 | } |
| 3388 | return ret; |
| 3389 | } |
| 3390 | |
| 3391 | static struct nfs4_openowner * |
| 3392 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
| 3393 | struct nfsd4_compound_state *cstate) |
| 3394 | { |
| 3395 | struct nfs4_client *clp = cstate->clp; |
| 3396 | struct nfs4_openowner *oo, *ret; |
| 3397 | |
| 3398 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 3399 | if (!oo) |
| 3400 | return NULL; |
| 3401 | oo->oo_owner.so_ops = &openowner_ops; |
| 3402 | oo->oo_owner.so_is_open_owner = 1; |
| 3403 | oo->oo_owner.so_seqid = open->op_seqid; |
| 3404 | oo->oo_flags = 0; |
| 3405 | if (nfsd4_has_session(cstate)) |
| 3406 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
| 3407 | oo->oo_time = 0; |
| 3408 | oo->oo_last_closed_stid = NULL; |
| 3409 | INIT_LIST_HEAD(&oo->oo_close_lru); |
| 3410 | spin_lock(&clp->cl_lock); |
| 3411 | ret = find_openstateowner_str_locked(strhashval, open, clp); |
| 3412 | if (ret == NULL) { |
| 3413 | hash_openowner(oo, clp, strhashval); |
| 3414 | ret = oo; |
| 3415 | } else |
| 3416 | nfs4_free_stateowner(&oo->oo_owner); |
| 3417 | |
| 3418 | spin_unlock(&clp->cl_lock); |
| 3419 | return ret; |
| 3420 | } |
| 3421 | |
| 3422 | static struct nfs4_ol_stateid * |
| 3423 | init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, |
| 3424 | struct nfsd4_open *open) |
| 3425 | { |
| 3426 | |
| 3427 | struct nfs4_openowner *oo = open->op_openowner; |
| 3428 | struct nfs4_ol_stateid *retstp = NULL; |
| 3429 | |
| 3430 | /* We are moving these outside of the spinlocks to avoid the warnings */ |
| 3431 | mutex_init(&stp->st_mutex); |
| 3432 | mutex_lock(&stp->st_mutex); |
| 3433 | |
| 3434 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
| 3435 | spin_lock(&fp->fi_lock); |
| 3436 | |
| 3437 | retstp = nfsd4_find_existing_open(fp, open); |
| 3438 | if (retstp) |
| 3439 | goto out_unlock; |
| 3440 | atomic_inc(&stp->st_stid.sc_count); |
| 3441 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
| 3442 | INIT_LIST_HEAD(&stp->st_locks); |
| 3443 | stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); |
| 3444 | get_nfs4_file(fp); |
| 3445 | stp->st_stid.sc_file = fp; |
| 3446 | stp->st_access_bmap = 0; |
| 3447 | stp->st_deny_bmap = 0; |
| 3448 | stp->st_openstp = NULL; |
| 3449 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
| 3450 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 3451 | |
| 3452 | out_unlock: |
| 3453 | spin_unlock(&fp->fi_lock); |
| 3454 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
| 3455 | if (retstp) { |
| 3456 | mutex_lock(&retstp->st_mutex); |
| 3457 | /* Not that we need to, just for neatness */ |
| 3458 | mutex_unlock(&stp->st_mutex); |
| 3459 | } |
| 3460 | return retstp; |
| 3461 | } |
| 3462 | |
| 3463 | /* |
| 3464 | * In the 4.0 case we need to keep the owners around a little while to handle |
| 3465 | * CLOSE replay. We still do need to release any file access that is held by |
| 3466 | * them before returning however. |
| 3467 | */ |
| 3468 | static void |
| 3469 | move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) |
| 3470 | { |
| 3471 | struct nfs4_ol_stateid *last; |
| 3472 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 3473 | struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, |
| 3474 | nfsd_net_id); |
| 3475 | |
| 3476 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
| 3477 | |
| 3478 | /* |
| 3479 | * We know that we hold one reference via nfsd4_close, and another |
| 3480 | * "persistent" reference for the client. If the refcount is higher |
| 3481 | * than 2, then there are still calls in progress that are using this |
| 3482 | * stateid. We can't put the sc_file reference until they are finished. |
| 3483 | * Wait for the refcount to drop to 2. Since it has been unhashed, |
| 3484 | * there should be no danger of the refcount going back up again at |
| 3485 | * this point. |
| 3486 | */ |
| 3487 | wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2); |
| 3488 | |
| 3489 | release_all_access(s); |
| 3490 | if (s->st_stid.sc_file) { |
| 3491 | put_nfs4_file(s->st_stid.sc_file); |
| 3492 | s->st_stid.sc_file = NULL; |
| 3493 | } |
| 3494 | |
| 3495 | spin_lock(&nn->client_lock); |
| 3496 | last = oo->oo_last_closed_stid; |
| 3497 | oo->oo_last_closed_stid = s; |
| 3498 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
| 3499 | oo->oo_time = get_seconds(); |
| 3500 | spin_unlock(&nn->client_lock); |
| 3501 | if (last) |
| 3502 | nfs4_put_stid(&last->st_stid); |
| 3503 | } |
| 3504 | |
| 3505 | /* search file_hashtbl[] for file */ |
| 3506 | static struct nfs4_file * |
| 3507 | find_file_locked(struct knfsd_fh *fh, unsigned int hashval) |
| 3508 | { |
| 3509 | struct nfs4_file *fp; |
| 3510 | |
| 3511 | hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) { |
| 3512 | if (fh_match(&fp->fi_fhandle, fh)) { |
| 3513 | if (atomic_inc_not_zero(&fp->fi_ref)) |
| 3514 | return fp; |
| 3515 | } |
| 3516 | } |
| 3517 | return NULL; |
| 3518 | } |
| 3519 | |
| 3520 | struct nfs4_file * |
| 3521 | find_file(struct knfsd_fh *fh) |
| 3522 | { |
| 3523 | struct nfs4_file *fp; |
| 3524 | unsigned int hashval = file_hashval(fh); |
| 3525 | |
| 3526 | rcu_read_lock(); |
| 3527 | fp = find_file_locked(fh, hashval); |
| 3528 | rcu_read_unlock(); |
| 3529 | return fp; |
| 3530 | } |
| 3531 | |
| 3532 | static struct nfs4_file * |
| 3533 | find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) |
| 3534 | { |
| 3535 | struct nfs4_file *fp; |
| 3536 | unsigned int hashval = file_hashval(fh); |
| 3537 | |
| 3538 | rcu_read_lock(); |
| 3539 | fp = find_file_locked(fh, hashval); |
| 3540 | rcu_read_unlock(); |
| 3541 | if (fp) |
| 3542 | return fp; |
| 3543 | |
| 3544 | spin_lock(&state_lock); |
| 3545 | fp = find_file_locked(fh, hashval); |
| 3546 | if (likely(fp == NULL)) { |
| 3547 | nfsd4_init_file(fh, hashval, new); |
| 3548 | fp = new; |
| 3549 | } |
| 3550 | spin_unlock(&state_lock); |
| 3551 | |
| 3552 | return fp; |
| 3553 | } |
| 3554 | |
| 3555 | /* |
| 3556 | * Called to check deny when READ with all zero stateid or |
| 3557 | * WRITE with all zero or all one stateid |
| 3558 | */ |
| 3559 | static __be32 |
| 3560 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 3561 | { |
| 3562 | struct nfs4_file *fp; |
| 3563 | __be32 ret = nfs_ok; |
| 3564 | |
| 3565 | fp = find_file(¤t_fh->fh_handle); |
| 3566 | if (!fp) |
| 3567 | return ret; |
| 3568 | /* Check for conflicting share reservations */ |
| 3569 | spin_lock(&fp->fi_lock); |
| 3570 | if (fp->fi_share_deny & deny_type) |
| 3571 | ret = nfserr_locked; |
| 3572 | spin_unlock(&fp->fi_lock); |
| 3573 | put_nfs4_file(fp); |
| 3574 | return ret; |
| 3575 | } |
| 3576 | |
| 3577 | static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) |
| 3578 | { |
| 3579 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 3580 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
| 3581 | nfsd_net_id); |
| 3582 | |
| 3583 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
| 3584 | |
| 3585 | /* |
| 3586 | * We can't do this in nfsd_break_deleg_cb because it is |
| 3587 | * already holding inode->i_lock. |
| 3588 | * |
| 3589 | * If the dl_time != 0, then we know that it has already been |
| 3590 | * queued for a lease break. Don't queue it again. |
| 3591 | */ |
| 3592 | spin_lock(&state_lock); |
| 3593 | if (dp->dl_time == 0) { |
| 3594 | dp->dl_time = get_seconds(); |
| 3595 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
| 3596 | } |
| 3597 | spin_unlock(&state_lock); |
| 3598 | } |
| 3599 | |
| 3600 | static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, |
| 3601 | struct rpc_task *task) |
| 3602 | { |
| 3603 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 3604 | |
| 3605 | if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID) |
| 3606 | return 1; |
| 3607 | |
| 3608 | switch (task->tk_status) { |
| 3609 | case 0: |
| 3610 | return 1; |
| 3611 | case -EBADHANDLE: |
| 3612 | case -NFS4ERR_BAD_STATEID: |
| 3613 | /* |
| 3614 | * Race: client probably got cb_recall before open reply |
| 3615 | * granting delegation. |
| 3616 | */ |
| 3617 | if (dp->dl_retries--) { |
| 3618 | rpc_delay(task, 2 * HZ); |
| 3619 | return 0; |
| 3620 | } |
| 3621 | /*FALLTHRU*/ |
| 3622 | default: |
| 3623 | return -1; |
| 3624 | } |
| 3625 | } |
| 3626 | |
| 3627 | static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) |
| 3628 | { |
| 3629 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 3630 | |
| 3631 | nfs4_put_stid(&dp->dl_stid); |
| 3632 | } |
| 3633 | |
| 3634 | static struct nfsd4_callback_ops nfsd4_cb_recall_ops = { |
| 3635 | .prepare = nfsd4_cb_recall_prepare, |
| 3636 | .done = nfsd4_cb_recall_done, |
| 3637 | .release = nfsd4_cb_recall_release, |
| 3638 | }; |
| 3639 | |
| 3640 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
| 3641 | { |
| 3642 | /* |
| 3643 | * We're assuming the state code never drops its reference |
| 3644 | * without first removing the lease. Since we're in this lease |
| 3645 | * callback (and since the lease code is serialized by the kernel |
| 3646 | * lock) we know the server hasn't removed the lease yet, we know |
| 3647 | * it's safe to take a reference. |
| 3648 | */ |
| 3649 | atomic_inc(&dp->dl_stid.sc_count); |
| 3650 | nfsd4_run_cb(&dp->dl_recall); |
| 3651 | } |
| 3652 | |
| 3653 | /* Called from break_lease() with i_lock held. */ |
| 3654 | static bool |
| 3655 | nfsd_break_deleg_cb(struct file_lock *fl) |
| 3656 | { |
| 3657 | bool ret = false; |
| 3658 | struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; |
| 3659 | struct nfs4_delegation *dp; |
| 3660 | |
| 3661 | if (!fp) { |
| 3662 | WARN(1, "(%p)->fl_owner NULL\n", fl); |
| 3663 | return ret; |
| 3664 | } |
| 3665 | if (fp->fi_had_conflict) { |
| 3666 | WARN(1, "duplicate break on %p\n", fp); |
| 3667 | return ret; |
| 3668 | } |
| 3669 | /* |
| 3670 | * We don't want the locks code to timeout the lease for us; |
| 3671 | * we'll remove it ourself if a delegation isn't returned |
| 3672 | * in time: |
| 3673 | */ |
| 3674 | fl->fl_break_time = 0; |
| 3675 | |
| 3676 | spin_lock(&fp->fi_lock); |
| 3677 | fp->fi_had_conflict = true; |
| 3678 | /* |
| 3679 | * If there are no delegations on the list, then return true |
| 3680 | * so that the lease code will go ahead and delete it. |
| 3681 | */ |
| 3682 | if (list_empty(&fp->fi_delegations)) |
| 3683 | ret = true; |
| 3684 | else |
| 3685 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) |
| 3686 | nfsd_break_one_deleg(dp); |
| 3687 | spin_unlock(&fp->fi_lock); |
| 3688 | return ret; |
| 3689 | } |
| 3690 | |
| 3691 | static int |
| 3692 | nfsd_change_deleg_cb(struct file_lock *onlist, int arg, |
| 3693 | struct list_head *dispose) |
| 3694 | { |
| 3695 | if (arg & F_UNLCK) |
| 3696 | return lease_modify(onlist, arg, dispose); |
| 3697 | else |
| 3698 | return -EAGAIN; |
| 3699 | } |
| 3700 | |
| 3701 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
| 3702 | .lm_break = nfsd_break_deleg_cb, |
| 3703 | .lm_change = nfsd_change_deleg_cb, |
| 3704 | }; |
| 3705 | |
| 3706 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 3707 | { |
| 3708 | if (nfsd4_has_session(cstate)) |
| 3709 | return nfs_ok; |
| 3710 | if (seqid == so->so_seqid - 1) |
| 3711 | return nfserr_replay_me; |
| 3712 | if (seqid == so->so_seqid) |
| 3713 | return nfs_ok; |
| 3714 | return nfserr_bad_seqid; |
| 3715 | } |
| 3716 | |
| 3717 | static __be32 lookup_clientid(clientid_t *clid, |
| 3718 | struct nfsd4_compound_state *cstate, |
| 3719 | struct nfsd_net *nn) |
| 3720 | { |
| 3721 | struct nfs4_client *found; |
| 3722 | |
| 3723 | if (cstate->clp) { |
| 3724 | found = cstate->clp; |
| 3725 | if (!same_clid(&found->cl_clientid, clid)) |
| 3726 | return nfserr_stale_clientid; |
| 3727 | return nfs_ok; |
| 3728 | } |
| 3729 | |
| 3730 | if (STALE_CLIENTID(clid, nn)) |
| 3731 | return nfserr_stale_clientid; |
| 3732 | |
| 3733 | /* |
| 3734 | * For v4.1+ we get the client in the SEQUENCE op. If we don't have one |
| 3735 | * cached already then we know this is for is for v4.0 and "sessions" |
| 3736 | * will be false. |
| 3737 | */ |
| 3738 | WARN_ON_ONCE(cstate->session); |
| 3739 | spin_lock(&nn->client_lock); |
| 3740 | found = find_confirmed_client(clid, false, nn); |
| 3741 | if (!found) { |
| 3742 | spin_unlock(&nn->client_lock); |
| 3743 | return nfserr_expired; |
| 3744 | } |
| 3745 | atomic_inc(&found->cl_refcount); |
| 3746 | spin_unlock(&nn->client_lock); |
| 3747 | |
| 3748 | /* Cache the nfs4_client in cstate! */ |
| 3749 | cstate->clp = found; |
| 3750 | return nfs_ok; |
| 3751 | } |
| 3752 | |
| 3753 | __be32 |
| 3754 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
| 3755 | struct nfsd4_open *open, struct nfsd_net *nn) |
| 3756 | { |
| 3757 | clientid_t *clientid = &open->op_clientid; |
| 3758 | struct nfs4_client *clp = NULL; |
| 3759 | unsigned int strhashval; |
| 3760 | struct nfs4_openowner *oo = NULL; |
| 3761 | __be32 status; |
| 3762 | |
| 3763 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
| 3764 | return nfserr_stale_clientid; |
| 3765 | /* |
| 3766 | * In case we need it later, after we've already created the |
| 3767 | * file and don't want to risk a further failure: |
| 3768 | */ |
| 3769 | open->op_file = nfsd4_alloc_file(); |
| 3770 | if (open->op_file == NULL) |
| 3771 | return nfserr_jukebox; |
| 3772 | |
| 3773 | status = lookup_clientid(clientid, cstate, nn); |
| 3774 | if (status) |
| 3775 | return status; |
| 3776 | clp = cstate->clp; |
| 3777 | |
| 3778 | strhashval = ownerstr_hashval(&open->op_owner); |
| 3779 | oo = find_openstateowner_str(strhashval, open, clp); |
| 3780 | open->op_openowner = oo; |
| 3781 | if (!oo) { |
| 3782 | goto new_owner; |
| 3783 | } |
| 3784 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
| 3785 | /* Replace unconfirmed owners without checking for replay. */ |
| 3786 | release_openowner(oo); |
| 3787 | open->op_openowner = NULL; |
| 3788 | goto new_owner; |
| 3789 | } |
| 3790 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 3791 | if (status) |
| 3792 | return status; |
| 3793 | goto alloc_stateid; |
| 3794 | new_owner: |
| 3795 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
| 3796 | if (oo == NULL) |
| 3797 | return nfserr_jukebox; |
| 3798 | open->op_openowner = oo; |
| 3799 | alloc_stateid: |
| 3800 | open->op_stp = nfs4_alloc_open_stateid(clp); |
| 3801 | if (!open->op_stp) |
| 3802 | return nfserr_jukebox; |
| 3803 | |
| 3804 | if (nfsd4_has_session(cstate) && |
| 3805 | (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { |
| 3806 | open->op_odstate = alloc_clnt_odstate(clp); |
| 3807 | if (!open->op_odstate) |
| 3808 | return nfserr_jukebox; |
| 3809 | } |
| 3810 | |
| 3811 | return nfs_ok; |
| 3812 | } |
| 3813 | |
| 3814 | static inline __be32 |
| 3815 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 3816 | { |
| 3817 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 3818 | return nfserr_openmode; |
| 3819 | else |
| 3820 | return nfs_ok; |
| 3821 | } |
| 3822 | |
| 3823 | static int share_access_to_flags(u32 share_access) |
| 3824 | { |
| 3825 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 3826 | } |
| 3827 | |
| 3828 | static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s) |
| 3829 | { |
| 3830 | struct nfs4_stid *ret; |
| 3831 | |
| 3832 | ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID); |
| 3833 | if (!ret) |
| 3834 | return NULL; |
| 3835 | return delegstateid(ret); |
| 3836 | } |
| 3837 | |
| 3838 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 3839 | { |
| 3840 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 3841 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 3842 | } |
| 3843 | |
| 3844 | static __be32 |
| 3845 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
| 3846 | struct nfs4_delegation **dp) |
| 3847 | { |
| 3848 | int flags; |
| 3849 | __be32 status = nfserr_bad_stateid; |
| 3850 | struct nfs4_delegation *deleg; |
| 3851 | |
| 3852 | deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); |
| 3853 | if (deleg == NULL) |
| 3854 | goto out; |
| 3855 | flags = share_access_to_flags(open->op_share_access); |
| 3856 | status = nfs4_check_delegmode(deleg, flags); |
| 3857 | if (status) { |
| 3858 | nfs4_put_stid(&deleg->dl_stid); |
| 3859 | goto out; |
| 3860 | } |
| 3861 | *dp = deleg; |
| 3862 | out: |
| 3863 | if (!nfsd4_is_deleg_cur(open)) |
| 3864 | return nfs_ok; |
| 3865 | if (status) |
| 3866 | return status; |
| 3867 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
| 3868 | return nfs_ok; |
| 3869 | } |
| 3870 | |
| 3871 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 3872 | { |
| 3873 | int flags = 0; |
| 3874 | |
| 3875 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 3876 | flags |= NFSD_MAY_READ; |
| 3877 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 3878 | flags |= NFSD_MAY_WRITE; |
| 3879 | return flags; |
| 3880 | } |
| 3881 | |
| 3882 | static inline __be32 |
| 3883 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 3884 | struct nfsd4_open *open) |
| 3885 | { |
| 3886 | struct iattr iattr = { |
| 3887 | .ia_valid = ATTR_SIZE, |
| 3888 | .ia_size = 0, |
| 3889 | }; |
| 3890 | if (!open->op_truncate) |
| 3891 | return 0; |
| 3892 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
| 3893 | return nfserr_inval; |
| 3894 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); |
| 3895 | } |
| 3896 | |
| 3897 | static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, |
| 3898 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
| 3899 | struct nfsd4_open *open) |
| 3900 | { |
| 3901 | struct file *filp = NULL; |
| 3902 | __be32 status; |
| 3903 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 3904 | int access = nfs4_access_to_access(open->op_share_access); |
| 3905 | unsigned char old_access_bmap, old_deny_bmap; |
| 3906 | |
| 3907 | spin_lock(&fp->fi_lock); |
| 3908 | |
| 3909 | /* |
| 3910 | * Are we trying to set a deny mode that would conflict with |
| 3911 | * current access? |
| 3912 | */ |
| 3913 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 3914 | if (status != nfs_ok) { |
| 3915 | spin_unlock(&fp->fi_lock); |
| 3916 | goto out; |
| 3917 | } |
| 3918 | |
| 3919 | /* set access to the file */ |
| 3920 | status = nfs4_file_get_access(fp, open->op_share_access); |
| 3921 | if (status != nfs_ok) { |
| 3922 | spin_unlock(&fp->fi_lock); |
| 3923 | goto out; |
| 3924 | } |
| 3925 | |
| 3926 | /* Set access bits in stateid */ |
| 3927 | old_access_bmap = stp->st_access_bmap; |
| 3928 | set_access(open->op_share_access, stp); |
| 3929 | |
| 3930 | /* Set new deny mask */ |
| 3931 | old_deny_bmap = stp->st_deny_bmap; |
| 3932 | set_deny(open->op_share_deny, stp); |
| 3933 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 3934 | |
| 3935 | if (!fp->fi_fds[oflag]) { |
| 3936 | spin_unlock(&fp->fi_lock); |
| 3937 | status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp); |
| 3938 | if (status) |
| 3939 | goto out_put_access; |
| 3940 | spin_lock(&fp->fi_lock); |
| 3941 | if (!fp->fi_fds[oflag]) { |
| 3942 | fp->fi_fds[oflag] = filp; |
| 3943 | filp = NULL; |
| 3944 | } |
| 3945 | } |
| 3946 | spin_unlock(&fp->fi_lock); |
| 3947 | if (filp) |
| 3948 | fput(filp); |
| 3949 | |
| 3950 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3951 | if (status) |
| 3952 | goto out_put_access; |
| 3953 | out: |
| 3954 | return status; |
| 3955 | out_put_access: |
| 3956 | stp->st_access_bmap = old_access_bmap; |
| 3957 | nfs4_file_put_access(fp, open->op_share_access); |
| 3958 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); |
| 3959 | goto out; |
| 3960 | } |
| 3961 | |
| 3962 | static __be32 |
| 3963 | 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) |
| 3964 | { |
| 3965 | __be32 status; |
| 3966 | unsigned char old_deny_bmap = stp->st_deny_bmap; |
| 3967 | |
| 3968 | if (!test_access(open->op_share_access, stp)) |
| 3969 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
| 3970 | |
| 3971 | /* test and set deny mode */ |
| 3972 | spin_lock(&fp->fi_lock); |
| 3973 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 3974 | if (status == nfs_ok) { |
| 3975 | set_deny(open->op_share_deny, stp); |
| 3976 | fp->fi_share_deny |= |
| 3977 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 3978 | } |
| 3979 | spin_unlock(&fp->fi_lock); |
| 3980 | |
| 3981 | if (status != nfs_ok) |
| 3982 | return status; |
| 3983 | |
| 3984 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 3985 | if (status != nfs_ok) |
| 3986 | reset_union_bmap_deny(old_deny_bmap, stp); |
| 3987 | return status; |
| 3988 | } |
| 3989 | |
| 3990 | /* Should we give out recallable state?: */ |
| 3991 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 3992 | { |
| 3993 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 3994 | return true; |
| 3995 | /* |
| 3996 | * In the sessions case, since we don't have to establish a |
| 3997 | * separate connection for callbacks, we assume it's OK |
| 3998 | * until we hear otherwise: |
| 3999 | */ |
| 4000 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 4001 | } |
| 4002 | |
| 4003 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag) |
| 4004 | { |
| 4005 | struct file_lock *fl; |
| 4006 | |
| 4007 | fl = locks_alloc_lock(); |
| 4008 | if (!fl) |
| 4009 | return NULL; |
| 4010 | fl->fl_lmops = &nfsd_lease_mng_ops; |
| 4011 | fl->fl_flags = FL_DELEG; |
| 4012 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 4013 | fl->fl_end = OFFSET_MAX; |
| 4014 | fl->fl_owner = (fl_owner_t)fp; |
| 4015 | fl->fl_pid = current->tgid; |
| 4016 | return fl; |
| 4017 | } |
| 4018 | |
| 4019 | /** |
| 4020 | * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer |
| 4021 | * @dp: a pointer to the nfs4_delegation we're adding. |
| 4022 | * |
| 4023 | * Return: |
| 4024 | * On success: Return code will be 0 on success. |
| 4025 | * |
| 4026 | * On error: -EAGAIN if there was an existing delegation. |
| 4027 | * nonzero if there is an error in other cases. |
| 4028 | * |
| 4029 | */ |
| 4030 | |
| 4031 | static int nfs4_setlease(struct nfs4_delegation *dp) |
| 4032 | { |
| 4033 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
| 4034 | struct file_lock *fl; |
| 4035 | struct file *filp; |
| 4036 | int status = 0; |
| 4037 | |
| 4038 | fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ); |
| 4039 | if (!fl) |
| 4040 | return -ENOMEM; |
| 4041 | filp = find_readable_file(fp); |
| 4042 | if (!filp) { |
| 4043 | /* We should always have a readable file here */ |
| 4044 | WARN_ON_ONCE(1); |
| 4045 | locks_free_lock(fl); |
| 4046 | return -EBADF; |
| 4047 | } |
| 4048 | fl->fl_file = filp; |
| 4049 | status = vfs_setlease(filp, fl->fl_type, &fl, NULL); |
| 4050 | if (fl) |
| 4051 | locks_free_lock(fl); |
| 4052 | if (status) |
| 4053 | goto out_fput; |
| 4054 | spin_lock(&state_lock); |
| 4055 | spin_lock(&fp->fi_lock); |
| 4056 | /* Did the lease get broken before we took the lock? */ |
| 4057 | status = -EAGAIN; |
| 4058 | if (fp->fi_had_conflict) |
| 4059 | goto out_unlock; |
| 4060 | /* Race breaker */ |
| 4061 | if (fp->fi_deleg_file) { |
| 4062 | status = hash_delegation_locked(dp, fp); |
| 4063 | goto out_unlock; |
| 4064 | } |
| 4065 | fp->fi_deleg_file = filp; |
| 4066 | fp->fi_delegees = 0; |
| 4067 | status = hash_delegation_locked(dp, fp); |
| 4068 | spin_unlock(&fp->fi_lock); |
| 4069 | spin_unlock(&state_lock); |
| 4070 | if (status) { |
| 4071 | /* Should never happen, this is a new fi_deleg_file */ |
| 4072 | WARN_ON_ONCE(1); |
| 4073 | goto out_fput; |
| 4074 | } |
| 4075 | return 0; |
| 4076 | out_unlock: |
| 4077 | spin_unlock(&fp->fi_lock); |
| 4078 | spin_unlock(&state_lock); |
| 4079 | out_fput: |
| 4080 | fput(filp); |
| 4081 | return status; |
| 4082 | } |
| 4083 | |
| 4084 | static struct nfs4_delegation * |
| 4085 | nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, |
| 4086 | struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) |
| 4087 | { |
| 4088 | int status; |
| 4089 | struct nfs4_delegation *dp; |
| 4090 | |
| 4091 | if (fp->fi_had_conflict) |
| 4092 | return ERR_PTR(-EAGAIN); |
| 4093 | |
| 4094 | spin_lock(&state_lock); |
| 4095 | spin_lock(&fp->fi_lock); |
| 4096 | status = nfs4_get_existing_delegation(clp, fp); |
| 4097 | spin_unlock(&fp->fi_lock); |
| 4098 | spin_unlock(&state_lock); |
| 4099 | |
| 4100 | if (status) |
| 4101 | return ERR_PTR(status); |
| 4102 | |
| 4103 | dp = alloc_init_deleg(clp, fh, odstate); |
| 4104 | if (!dp) |
| 4105 | return ERR_PTR(-ENOMEM); |
| 4106 | |
| 4107 | get_nfs4_file(fp); |
| 4108 | spin_lock(&state_lock); |
| 4109 | spin_lock(&fp->fi_lock); |
| 4110 | dp->dl_stid.sc_file = fp; |
| 4111 | if (!fp->fi_deleg_file) { |
| 4112 | spin_unlock(&fp->fi_lock); |
| 4113 | spin_unlock(&state_lock); |
| 4114 | status = nfs4_setlease(dp); |
| 4115 | goto out; |
| 4116 | } |
| 4117 | if (fp->fi_had_conflict) { |
| 4118 | status = -EAGAIN; |
| 4119 | goto out_unlock; |
| 4120 | } |
| 4121 | status = hash_delegation_locked(dp, fp); |
| 4122 | out_unlock: |
| 4123 | spin_unlock(&fp->fi_lock); |
| 4124 | spin_unlock(&state_lock); |
| 4125 | out: |
| 4126 | if (status) { |
| 4127 | put_clnt_odstate(dp->dl_clnt_odstate); |
| 4128 | nfs4_put_stid(&dp->dl_stid); |
| 4129 | return ERR_PTR(status); |
| 4130 | } |
| 4131 | return dp; |
| 4132 | } |
| 4133 | |
| 4134 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 4135 | { |
| 4136 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4137 | if (status == -EAGAIN) |
| 4138 | open->op_why_no_deleg = WND4_CONTENTION; |
| 4139 | else { |
| 4140 | open->op_why_no_deleg = WND4_RESOURCE; |
| 4141 | switch (open->op_deleg_want) { |
| 4142 | case NFS4_SHARE_WANT_READ_DELEG: |
| 4143 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 4144 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 4145 | break; |
| 4146 | case NFS4_SHARE_WANT_CANCEL: |
| 4147 | open->op_why_no_deleg = WND4_CANCELLED; |
| 4148 | break; |
| 4149 | case NFS4_SHARE_WANT_NO_DELEG: |
| 4150 | WARN_ON_ONCE(1); |
| 4151 | } |
| 4152 | } |
| 4153 | } |
| 4154 | |
| 4155 | /* |
| 4156 | * Attempt to hand out a delegation. |
| 4157 | * |
| 4158 | * Note we don't support write delegations, and won't until the vfs has |
| 4159 | * proper support for them. |
| 4160 | */ |
| 4161 | static void |
| 4162 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
| 4163 | struct nfs4_ol_stateid *stp) |
| 4164 | { |
| 4165 | struct nfs4_delegation *dp; |
| 4166 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
| 4167 | struct nfs4_client *clp = stp->st_stid.sc_client; |
| 4168 | int cb_up; |
| 4169 | int status = 0; |
| 4170 | |
| 4171 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
| 4172 | open->op_recall = 0; |
| 4173 | switch (open->op_claim_type) { |
| 4174 | case NFS4_OPEN_CLAIM_PREVIOUS: |
| 4175 | if (!cb_up) |
| 4176 | open->op_recall = 1; |
| 4177 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 4178 | goto out_no_deleg; |
| 4179 | break; |
| 4180 | case NFS4_OPEN_CLAIM_NULL: |
| 4181 | case NFS4_OPEN_CLAIM_FH: |
| 4182 | /* |
| 4183 | * Let's not give out any delegations till everyone's |
| 4184 | * had the chance to reclaim theirs, *and* until |
| 4185 | * NLM locks have all been reclaimed: |
| 4186 | */ |
| 4187 | if (locks_in_grace(clp->net)) |
| 4188 | goto out_no_deleg; |
| 4189 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
| 4190 | goto out_no_deleg; |
| 4191 | /* |
| 4192 | * Also, if the file was opened for write or |
| 4193 | * create, there's a good chance the client's |
| 4194 | * about to write to it, resulting in an |
| 4195 | * immediate recall (since we don't support |
| 4196 | * write delegations): |
| 4197 | */ |
| 4198 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
| 4199 | goto out_no_deleg; |
| 4200 | if (open->op_create == NFS4_OPEN_CREATE) |
| 4201 | goto out_no_deleg; |
| 4202 | break; |
| 4203 | default: |
| 4204 | goto out_no_deleg; |
| 4205 | } |
| 4206 | dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate); |
| 4207 | if (IS_ERR(dp)) |
| 4208 | goto out_no_deleg; |
| 4209 | |
| 4210 | memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid)); |
| 4211 | |
| 4212 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
| 4213 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
| 4214 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
| 4215 | nfs4_put_stid(&dp->dl_stid); |
| 4216 | return; |
| 4217 | out_no_deleg: |
| 4218 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 4219 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
| 4220 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
| 4221 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
| 4222 | open->op_recall = 1; |
| 4223 | } |
| 4224 | |
| 4225 | /* 4.1 client asking for a delegation? */ |
| 4226 | if (open->op_deleg_want) |
| 4227 | nfsd4_open_deleg_none_ext(open, status); |
| 4228 | return; |
| 4229 | } |
| 4230 | |
| 4231 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 4232 | struct nfs4_delegation *dp) |
| 4233 | { |
| 4234 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 4235 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 4236 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4237 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 4238 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 4239 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 4240 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4241 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 4242 | } |
| 4243 | /* Otherwise the client must be confused wanting a delegation |
| 4244 | * it already has, therefore we don't return |
| 4245 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 4246 | */ |
| 4247 | } |
| 4248 | |
| 4249 | __be32 |
| 4250 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 4251 | { |
| 4252 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
| 4253 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
| 4254 | struct nfs4_file *fp = NULL; |
| 4255 | struct nfs4_ol_stateid *stp = NULL; |
| 4256 | struct nfs4_ol_stateid *swapstp = NULL; |
| 4257 | struct nfs4_delegation *dp = NULL; |
| 4258 | __be32 status; |
| 4259 | |
| 4260 | /* |
| 4261 | * Lookup file; if found, lookup stateid and check open request, |
| 4262 | * and check for delegations in the process of being recalled. |
| 4263 | * If not found, create the nfs4_file struct |
| 4264 | */ |
| 4265 | fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle); |
| 4266 | if (fp != open->op_file) { |
| 4267 | status = nfs4_check_deleg(cl, open, &dp); |
| 4268 | if (status) |
| 4269 | goto out; |
| 4270 | spin_lock(&fp->fi_lock); |
| 4271 | stp = nfsd4_find_existing_open(fp, open); |
| 4272 | spin_unlock(&fp->fi_lock); |
| 4273 | } else { |
| 4274 | open->op_file = NULL; |
| 4275 | status = nfserr_bad_stateid; |
| 4276 | if (nfsd4_is_deleg_cur(open)) |
| 4277 | goto out; |
| 4278 | } |
| 4279 | |
| 4280 | /* |
| 4281 | * OPEN the file, or upgrade an existing OPEN. |
| 4282 | * If truncate fails, the OPEN fails. |
| 4283 | */ |
| 4284 | if (stp) { |
| 4285 | /* Stateid was found, this is an OPEN upgrade */ |
| 4286 | mutex_lock(&stp->st_mutex); |
| 4287 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
| 4288 | if (status) { |
| 4289 | mutex_unlock(&stp->st_mutex); |
| 4290 | goto out; |
| 4291 | } |
| 4292 | } else { |
| 4293 | stp = open->op_stp; |
| 4294 | open->op_stp = NULL; |
| 4295 | /* |
| 4296 | * init_open_stateid() either returns a locked stateid |
| 4297 | * it found, or initializes and locks the new one we passed in |
| 4298 | */ |
| 4299 | swapstp = init_open_stateid(stp, fp, open); |
| 4300 | if (swapstp) { |
| 4301 | nfs4_put_stid(&stp->st_stid); |
| 4302 | stp = swapstp; |
| 4303 | status = nfs4_upgrade_open(rqstp, fp, current_fh, |
| 4304 | stp, open); |
| 4305 | if (status) { |
| 4306 | mutex_unlock(&stp->st_mutex); |
| 4307 | goto out; |
| 4308 | } |
| 4309 | goto upgrade_out; |
| 4310 | } |
| 4311 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
| 4312 | if (status) { |
| 4313 | mutex_unlock(&stp->st_mutex); |
| 4314 | release_open_stateid(stp); |
| 4315 | goto out; |
| 4316 | } |
| 4317 | |
| 4318 | stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, |
| 4319 | open->op_odstate); |
| 4320 | if (stp->st_clnt_odstate == open->op_odstate) |
| 4321 | open->op_odstate = NULL; |
| 4322 | } |
| 4323 | upgrade_out: |
| 4324 | nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); |
| 4325 | mutex_unlock(&stp->st_mutex); |
| 4326 | |
| 4327 | if (nfsd4_has_session(&resp->cstate)) { |
| 4328 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 4329 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4330 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 4331 | goto nodeleg; |
| 4332 | } |
| 4333 | } |
| 4334 | |
| 4335 | /* |
| 4336 | * Attempt to hand out a delegation. No error return, because the |
| 4337 | * OPEN succeeds even if we fail. |
| 4338 | */ |
| 4339 | nfs4_open_delegation(current_fh, open, stp); |
| 4340 | nodeleg: |
| 4341 | status = nfs_ok; |
| 4342 | |
| 4343 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
| 4344 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
| 4345 | out: |
| 4346 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 4347 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
| 4348 | open->op_deleg_want) |
| 4349 | nfsd4_deleg_xgrade_none_ext(open, dp); |
| 4350 | |
| 4351 | if (fp) |
| 4352 | put_nfs4_file(fp); |
| 4353 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
| 4354 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
| 4355 | /* |
| 4356 | * To finish the open response, we just need to set the rflags. |
| 4357 | */ |
| 4358 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
| 4359 | if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) && |
| 4360 | !nfsd4_has_session(&resp->cstate)) |
| 4361 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
| 4362 | if (dp) |
| 4363 | nfs4_put_stid(&dp->dl_stid); |
| 4364 | if (stp) |
| 4365 | nfs4_put_stid(&stp->st_stid); |
| 4366 | |
| 4367 | return status; |
| 4368 | } |
| 4369 | |
| 4370 | void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, |
| 4371 | struct nfsd4_open *open) |
| 4372 | { |
| 4373 | if (open->op_openowner) { |
| 4374 | struct nfs4_stateowner *so = &open->op_openowner->oo_owner; |
| 4375 | |
| 4376 | nfsd4_cstate_assign_replay(cstate, so); |
| 4377 | nfs4_put_stateowner(so); |
| 4378 | } |
| 4379 | if (open->op_file) |
| 4380 | kmem_cache_free(file_slab, open->op_file); |
| 4381 | if (open->op_stp) |
| 4382 | nfs4_put_stid(&open->op_stp->st_stid); |
| 4383 | if (open->op_odstate) |
| 4384 | kmem_cache_free(odstate_slab, open->op_odstate); |
| 4385 | } |
| 4386 | |
| 4387 | __be32 |
| 4388 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4389 | clientid_t *clid) |
| 4390 | { |
| 4391 | struct nfs4_client *clp; |
| 4392 | __be32 status; |
| 4393 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 4394 | |
| 4395 | dprintk("process_renew(%08x/%08x): starting\n", |
| 4396 | clid->cl_boot, clid->cl_id); |
| 4397 | status = lookup_clientid(clid, cstate, nn); |
| 4398 | if (status) |
| 4399 | goto out; |
| 4400 | clp = cstate->clp; |
| 4401 | status = nfserr_cb_path_down; |
| 4402 | if (!list_empty(&clp->cl_delegations) |
| 4403 | && clp->cl_cb_state != NFSD4_CB_UP) |
| 4404 | goto out; |
| 4405 | status = nfs_ok; |
| 4406 | out: |
| 4407 | return status; |
| 4408 | } |
| 4409 | |
| 4410 | void |
| 4411 | nfsd4_end_grace(struct nfsd_net *nn) |
| 4412 | { |
| 4413 | /* do nothing if grace period already ended */ |
| 4414 | if (nn->grace_ended) |
| 4415 | return; |
| 4416 | |
| 4417 | dprintk("NFSD: end of grace period\n"); |
| 4418 | nn->grace_ended = true; |
| 4419 | /* |
| 4420 | * If the server goes down again right now, an NFSv4 |
| 4421 | * client will still be allowed to reclaim after it comes back up, |
| 4422 | * even if it hasn't yet had a chance to reclaim state this time. |
| 4423 | * |
| 4424 | */ |
| 4425 | nfsd4_record_grace_done(nn); |
| 4426 | /* |
| 4427 | * At this point, NFSv4 clients can still reclaim. But if the |
| 4428 | * server crashes, any that have not yet reclaimed will be out |
| 4429 | * of luck on the next boot. |
| 4430 | * |
| 4431 | * (NFSv4.1+ clients are considered to have reclaimed once they |
| 4432 | * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to |
| 4433 | * have reclaimed after their first OPEN.) |
| 4434 | */ |
| 4435 | locks_end_grace(&nn->nfsd4_manager); |
| 4436 | /* |
| 4437 | * At this point, and once lockd and/or any other containers |
| 4438 | * exit their grace period, further reclaims will fail and |
| 4439 | * regular locking can resume. |
| 4440 | */ |
| 4441 | } |
| 4442 | |
| 4443 | static time_t |
| 4444 | nfs4_laundromat(struct nfsd_net *nn) |
| 4445 | { |
| 4446 | struct nfs4_client *clp; |
| 4447 | struct nfs4_openowner *oo; |
| 4448 | struct nfs4_delegation *dp; |
| 4449 | struct nfs4_ol_stateid *stp; |
| 4450 | struct list_head *pos, *next, reaplist; |
| 4451 | time_t cutoff = get_seconds() - nn->nfsd4_lease; |
| 4452 | time_t t, new_timeo = nn->nfsd4_lease; |
| 4453 | |
| 4454 | dprintk("NFSD: laundromat service - starting\n"); |
| 4455 | nfsd4_end_grace(nn); |
| 4456 | INIT_LIST_HEAD(&reaplist); |
| 4457 | spin_lock(&nn->client_lock); |
| 4458 | list_for_each_safe(pos, next, &nn->client_lru) { |
| 4459 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
| 4460 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { |
| 4461 | t = clp->cl_time - cutoff; |
| 4462 | new_timeo = min(new_timeo, t); |
| 4463 | break; |
| 4464 | } |
| 4465 | if (mark_client_expired_locked(clp)) { |
| 4466 | dprintk("NFSD: client in use (clientid %08x)\n", |
| 4467 | clp->cl_clientid.cl_id); |
| 4468 | continue; |
| 4469 | } |
| 4470 | list_add(&clp->cl_lru, &reaplist); |
| 4471 | } |
| 4472 | spin_unlock(&nn->client_lock); |
| 4473 | list_for_each_safe(pos, next, &reaplist) { |
| 4474 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
| 4475 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 4476 | clp->cl_clientid.cl_id); |
| 4477 | list_del_init(&clp->cl_lru); |
| 4478 | expire_client(clp); |
| 4479 | } |
| 4480 | spin_lock(&state_lock); |
| 4481 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
| 4482 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 4483 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { |
| 4484 | t = dp->dl_time - cutoff; |
| 4485 | new_timeo = min(new_timeo, t); |
| 4486 | break; |
| 4487 | } |
| 4488 | WARN_ON(!unhash_delegation_locked(dp)); |
| 4489 | list_add(&dp->dl_recall_lru, &reaplist); |
| 4490 | } |
| 4491 | spin_unlock(&state_lock); |
| 4492 | while (!list_empty(&reaplist)) { |
| 4493 | dp = list_first_entry(&reaplist, struct nfs4_delegation, |
| 4494 | dl_recall_lru); |
| 4495 | list_del_init(&dp->dl_recall_lru); |
| 4496 | revoke_delegation(dp); |
| 4497 | } |
| 4498 | |
| 4499 | spin_lock(&nn->client_lock); |
| 4500 | while (!list_empty(&nn->close_lru)) { |
| 4501 | oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, |
| 4502 | oo_close_lru); |
| 4503 | if (time_after((unsigned long)oo->oo_time, |
| 4504 | (unsigned long)cutoff)) { |
| 4505 | t = oo->oo_time - cutoff; |
| 4506 | new_timeo = min(new_timeo, t); |
| 4507 | break; |
| 4508 | } |
| 4509 | list_del_init(&oo->oo_close_lru); |
| 4510 | stp = oo->oo_last_closed_stid; |
| 4511 | oo->oo_last_closed_stid = NULL; |
| 4512 | spin_unlock(&nn->client_lock); |
| 4513 | nfs4_put_stid(&stp->st_stid); |
| 4514 | spin_lock(&nn->client_lock); |
| 4515 | } |
| 4516 | spin_unlock(&nn->client_lock); |
| 4517 | |
| 4518 | new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
| 4519 | return new_timeo; |
| 4520 | } |
| 4521 | |
| 4522 | static struct workqueue_struct *laundry_wq; |
| 4523 | static void laundromat_main(struct work_struct *); |
| 4524 | |
| 4525 | static void |
| 4526 | laundromat_main(struct work_struct *laundry) |
| 4527 | { |
| 4528 | time_t t; |
| 4529 | struct delayed_work *dwork = container_of(laundry, struct delayed_work, |
| 4530 | work); |
| 4531 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 4532 | laundromat_work); |
| 4533 | |
| 4534 | t = nfs4_laundromat(nn); |
| 4535 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
| 4536 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
| 4537 | } |
| 4538 | |
| 4539 | static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp) |
| 4540 | { |
| 4541 | if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) |
| 4542 | return nfserr_bad_stateid; |
| 4543 | return nfs_ok; |
| 4544 | } |
| 4545 | |
| 4546 | static inline int |
| 4547 | access_permit_read(struct nfs4_ol_stateid *stp) |
| 4548 | { |
| 4549 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 4550 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 4551 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
| 4552 | } |
| 4553 | |
| 4554 | static inline int |
| 4555 | access_permit_write(struct nfs4_ol_stateid *stp) |
| 4556 | { |
| 4557 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 4558 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
| 4559 | } |
| 4560 | |
| 4561 | static |
| 4562 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
| 4563 | { |
| 4564 | __be32 status = nfserr_openmode; |
| 4565 | |
| 4566 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 4567 | if (stp->st_openstp) |
| 4568 | stp = stp->st_openstp; |
| 4569 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
| 4570 | goto out; |
| 4571 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
| 4572 | goto out; |
| 4573 | status = nfs_ok; |
| 4574 | out: |
| 4575 | return status; |
| 4576 | } |
| 4577 | |
| 4578 | static inline __be32 |
| 4579 | check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags) |
| 4580 | { |
| 4581 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
| 4582 | return nfs_ok; |
| 4583 | else if (opens_in_grace(net)) { |
| 4584 | /* Answer in remaining cases depends on existence of |
| 4585 | * conflicting state; so we must wait out the grace period. */ |
| 4586 | return nfserr_grace; |
| 4587 | } else if (flags & WR_STATE) |
| 4588 | return nfs4_share_conflict(current_fh, |
| 4589 | NFS4_SHARE_DENY_WRITE); |
| 4590 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 4591 | return nfs4_share_conflict(current_fh, |
| 4592 | NFS4_SHARE_DENY_READ); |
| 4593 | } |
| 4594 | |
| 4595 | /* |
| 4596 | * Allow READ/WRITE during grace period on recovered state only for files |
| 4597 | * that are not able to provide mandatory locking. |
| 4598 | */ |
| 4599 | static inline int |
| 4600 | grace_disallows_io(struct net *net, struct inode *inode) |
| 4601 | { |
| 4602 | return opens_in_grace(net) && mandatory_lock(inode); |
| 4603 | } |
| 4604 | |
| 4605 | /* Returns true iff a is later than b: */ |
| 4606 | static bool stateid_generation_after(stateid_t *a, stateid_t *b) |
| 4607 | { |
| 4608 | return (s32)(a->si_generation - b->si_generation) > 0; |
| 4609 | } |
| 4610 | |
| 4611 | static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session) |
| 4612 | { |
| 4613 | /* |
| 4614 | * When sessions are used the stateid generation number is ignored |
| 4615 | * when it is zero. |
| 4616 | */ |
| 4617 | if (has_session && in->si_generation == 0) |
| 4618 | return nfs_ok; |
| 4619 | |
| 4620 | if (in->si_generation == ref->si_generation) |
| 4621 | return nfs_ok; |
| 4622 | |
| 4623 | /* If the client sends us a stateid from the future, it's buggy: */ |
| 4624 | if (stateid_generation_after(in, ref)) |
| 4625 | return nfserr_bad_stateid; |
| 4626 | /* |
| 4627 | * However, we could see a stateid from the past, even from a |
| 4628 | * non-buggy client. For example, if the client sends a lock |
| 4629 | * while some IO is outstanding, the lock may bump si_generation |
| 4630 | * while the IO is still in flight. The client could avoid that |
| 4631 | * situation by waiting for responses on all the IO requests, |
| 4632 | * but better performance may result in retrying IO that |
| 4633 | * receives an old_stateid error if requests are rarely |
| 4634 | * reordered in flight: |
| 4635 | */ |
| 4636 | return nfserr_old_stateid; |
| 4637 | } |
| 4638 | |
| 4639 | static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) |
| 4640 | { |
| 4641 | if (ols->st_stateowner->so_is_open_owner && |
| 4642 | !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
| 4643 | return nfserr_bad_stateid; |
| 4644 | return nfs_ok; |
| 4645 | } |
| 4646 | |
| 4647 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
| 4648 | { |
| 4649 | struct nfs4_stid *s; |
| 4650 | __be32 status = nfserr_bad_stateid; |
| 4651 | |
| 4652 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 4653 | return status; |
| 4654 | /* Client debugging aid. */ |
| 4655 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { |
| 4656 | char addr_str[INET6_ADDRSTRLEN]; |
| 4657 | rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, |
| 4658 | sizeof(addr_str)); |
| 4659 | pr_warn_ratelimited("NFSD: client %s testing state ID " |
| 4660 | "with incorrect client ID\n", addr_str); |
| 4661 | return status; |
| 4662 | } |
| 4663 | spin_lock(&cl->cl_lock); |
| 4664 | s = find_stateid_locked(cl, stateid); |
| 4665 | if (!s) |
| 4666 | goto out_unlock; |
| 4667 | status = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 4668 | if (status) |
| 4669 | goto out_unlock; |
| 4670 | switch (s->sc_type) { |
| 4671 | case NFS4_DELEG_STID: |
| 4672 | status = nfs_ok; |
| 4673 | break; |
| 4674 | case NFS4_REVOKED_DELEG_STID: |
| 4675 | status = nfserr_deleg_revoked; |
| 4676 | break; |
| 4677 | case NFS4_OPEN_STID: |
| 4678 | case NFS4_LOCK_STID: |
| 4679 | status = nfsd4_check_openowner_confirmed(openlockstateid(s)); |
| 4680 | break; |
| 4681 | default: |
| 4682 | printk("unknown stateid type %x\n", s->sc_type); |
| 4683 | /* Fallthrough */ |
| 4684 | case NFS4_CLOSED_STID: |
| 4685 | case NFS4_CLOSED_DELEG_STID: |
| 4686 | status = nfserr_bad_stateid; |
| 4687 | } |
| 4688 | out_unlock: |
| 4689 | spin_unlock(&cl->cl_lock); |
| 4690 | return status; |
| 4691 | } |
| 4692 | |
| 4693 | __be32 |
| 4694 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
| 4695 | stateid_t *stateid, unsigned char typemask, |
| 4696 | struct nfs4_stid **s, struct nfsd_net *nn) |
| 4697 | { |
| 4698 | __be32 status; |
| 4699 | |
| 4700 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 4701 | return nfserr_bad_stateid; |
| 4702 | status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn); |
| 4703 | if (status == nfserr_stale_clientid) { |
| 4704 | if (cstate->session) |
| 4705 | return nfserr_bad_stateid; |
| 4706 | return nfserr_stale_stateid; |
| 4707 | } |
| 4708 | if (status) |
| 4709 | return status; |
| 4710 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
| 4711 | if (!*s) |
| 4712 | return nfserr_bad_stateid; |
| 4713 | return nfs_ok; |
| 4714 | } |
| 4715 | |
| 4716 | static struct file * |
| 4717 | nfs4_find_file(struct nfs4_stid *s, int flags) |
| 4718 | { |
| 4719 | if (!s) |
| 4720 | return NULL; |
| 4721 | |
| 4722 | switch (s->sc_type) { |
| 4723 | case NFS4_DELEG_STID: |
| 4724 | if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) |
| 4725 | return NULL; |
| 4726 | return get_file(s->sc_file->fi_deleg_file); |
| 4727 | case NFS4_OPEN_STID: |
| 4728 | case NFS4_LOCK_STID: |
| 4729 | if (flags & RD_STATE) |
| 4730 | return find_readable_file(s->sc_file); |
| 4731 | else |
| 4732 | return find_writeable_file(s->sc_file); |
| 4733 | break; |
| 4734 | } |
| 4735 | |
| 4736 | return NULL; |
| 4737 | } |
| 4738 | |
| 4739 | static __be32 |
| 4740 | nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags) |
| 4741 | { |
| 4742 | __be32 status; |
| 4743 | |
| 4744 | status = nfsd4_check_openowner_confirmed(ols); |
| 4745 | if (status) |
| 4746 | return status; |
| 4747 | return nfs4_check_openmode(ols, flags); |
| 4748 | } |
| 4749 | |
| 4750 | static __be32 |
| 4751 | nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s, |
| 4752 | struct file **filpp, bool *tmp_file, int flags) |
| 4753 | { |
| 4754 | int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; |
| 4755 | struct file *file; |
| 4756 | __be32 status; |
| 4757 | |
| 4758 | file = nfs4_find_file(s, flags); |
| 4759 | if (file) { |
| 4760 | status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
| 4761 | acc | NFSD_MAY_OWNER_OVERRIDE); |
| 4762 | if (status) { |
| 4763 | fput(file); |
| 4764 | return status; |
| 4765 | } |
| 4766 | |
| 4767 | *filpp = file; |
| 4768 | } else { |
| 4769 | status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp); |
| 4770 | if (status) |
| 4771 | return status; |
| 4772 | |
| 4773 | if (tmp_file) |
| 4774 | *tmp_file = true; |
| 4775 | } |
| 4776 | |
| 4777 | return 0; |
| 4778 | } |
| 4779 | |
| 4780 | /* |
| 4781 | * Checks for stateid operations |
| 4782 | */ |
| 4783 | __be32 |
| 4784 | nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, |
| 4785 | struct nfsd4_compound_state *cstate, stateid_t *stateid, |
| 4786 | int flags, struct file **filpp, bool *tmp_file) |
| 4787 | { |
| 4788 | struct svc_fh *fhp = &cstate->current_fh; |
| 4789 | struct inode *ino = d_inode(fhp->fh_dentry); |
| 4790 | struct net *net = SVC_NET(rqstp); |
| 4791 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 4792 | struct nfs4_stid *s = NULL; |
| 4793 | __be32 status; |
| 4794 | |
| 4795 | if (filpp) |
| 4796 | *filpp = NULL; |
| 4797 | if (tmp_file) |
| 4798 | *tmp_file = false; |
| 4799 | |
| 4800 | if (grace_disallows_io(net, ino)) |
| 4801 | return nfserr_grace; |
| 4802 | |
| 4803 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
| 4804 | status = check_special_stateids(net, fhp, stateid, flags); |
| 4805 | goto done; |
| 4806 | } |
| 4807 | |
| 4808 | status = nfsd4_lookup_stateid(cstate, stateid, |
| 4809 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
| 4810 | &s, nn); |
| 4811 | if (status) |
| 4812 | return status; |
| 4813 | status = check_stateid_generation(stateid, &s->sc_stateid, |
| 4814 | nfsd4_has_session(cstate)); |
| 4815 | if (status) |
| 4816 | goto out; |
| 4817 | |
| 4818 | switch (s->sc_type) { |
| 4819 | case NFS4_DELEG_STID: |
| 4820 | status = nfs4_check_delegmode(delegstateid(s), flags); |
| 4821 | break; |
| 4822 | case NFS4_OPEN_STID: |
| 4823 | case NFS4_LOCK_STID: |
| 4824 | status = nfs4_check_olstateid(fhp, openlockstateid(s), flags); |
| 4825 | break; |
| 4826 | default: |
| 4827 | status = nfserr_bad_stateid; |
| 4828 | break; |
| 4829 | } |
| 4830 | if (status) |
| 4831 | goto out; |
| 4832 | status = nfs4_check_fh(fhp, s); |
| 4833 | |
| 4834 | done: |
| 4835 | if (!status && filpp) |
| 4836 | status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags); |
| 4837 | out: |
| 4838 | if (s) |
| 4839 | nfs4_put_stid(s); |
| 4840 | return status; |
| 4841 | } |
| 4842 | |
| 4843 | /* |
| 4844 | * Test if the stateid is valid |
| 4845 | */ |
| 4846 | __be32 |
| 4847 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4848 | struct nfsd4_test_stateid *test_stateid) |
| 4849 | { |
| 4850 | struct nfsd4_test_stateid_id *stateid; |
| 4851 | struct nfs4_client *cl = cstate->session->se_client; |
| 4852 | |
| 4853 | list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list) |
| 4854 | stateid->ts_id_status = |
| 4855 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
| 4856 | |
| 4857 | return nfs_ok; |
| 4858 | } |
| 4859 | |
| 4860 | static __be32 |
| 4861 | nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) |
| 4862 | { |
| 4863 | struct nfs4_ol_stateid *stp = openlockstateid(s); |
| 4864 | __be32 ret; |
| 4865 | |
| 4866 | mutex_lock(&stp->st_mutex); |
| 4867 | |
| 4868 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 4869 | if (ret) |
| 4870 | goto out; |
| 4871 | |
| 4872 | ret = nfserr_locks_held; |
| 4873 | if (check_for_locks(stp->st_stid.sc_file, |
| 4874 | lockowner(stp->st_stateowner))) |
| 4875 | goto out; |
| 4876 | |
| 4877 | release_lock_stateid(stp); |
| 4878 | ret = nfs_ok; |
| 4879 | |
| 4880 | out: |
| 4881 | mutex_unlock(&stp->st_mutex); |
| 4882 | nfs4_put_stid(s); |
| 4883 | return ret; |
| 4884 | } |
| 4885 | |
| 4886 | __be32 |
| 4887 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4888 | struct nfsd4_free_stateid *free_stateid) |
| 4889 | { |
| 4890 | stateid_t *stateid = &free_stateid->fr_stateid; |
| 4891 | struct nfs4_stid *s; |
| 4892 | struct nfs4_delegation *dp; |
| 4893 | struct nfs4_client *cl = cstate->session->se_client; |
| 4894 | __be32 ret = nfserr_bad_stateid; |
| 4895 | |
| 4896 | spin_lock(&cl->cl_lock); |
| 4897 | s = find_stateid_locked(cl, stateid); |
| 4898 | if (!s) |
| 4899 | goto out_unlock; |
| 4900 | switch (s->sc_type) { |
| 4901 | case NFS4_DELEG_STID: |
| 4902 | ret = nfserr_locks_held; |
| 4903 | break; |
| 4904 | case NFS4_OPEN_STID: |
| 4905 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 4906 | if (ret) |
| 4907 | break; |
| 4908 | ret = nfserr_locks_held; |
| 4909 | break; |
| 4910 | case NFS4_LOCK_STID: |
| 4911 | atomic_inc(&s->sc_count); |
| 4912 | spin_unlock(&cl->cl_lock); |
| 4913 | ret = nfsd4_free_lock_stateid(stateid, s); |
| 4914 | goto out; |
| 4915 | case NFS4_REVOKED_DELEG_STID: |
| 4916 | dp = delegstateid(s); |
| 4917 | list_del_init(&dp->dl_recall_lru); |
| 4918 | spin_unlock(&cl->cl_lock); |
| 4919 | nfs4_put_stid(s); |
| 4920 | ret = nfs_ok; |
| 4921 | goto out; |
| 4922 | /* Default falls through and returns nfserr_bad_stateid */ |
| 4923 | } |
| 4924 | out_unlock: |
| 4925 | spin_unlock(&cl->cl_lock); |
| 4926 | out: |
| 4927 | return ret; |
| 4928 | } |
| 4929 | |
| 4930 | static inline int |
| 4931 | setlkflg (int type) |
| 4932 | { |
| 4933 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 4934 | RD_STATE : WR_STATE; |
| 4935 | } |
| 4936 | |
| 4937 | static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp) |
| 4938 | { |
| 4939 | struct svc_fh *current_fh = &cstate->current_fh; |
| 4940 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 4941 | __be32 status; |
| 4942 | |
| 4943 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 4944 | if (status) |
| 4945 | return status; |
| 4946 | if (stp->st_stid.sc_type == NFS4_CLOSED_STID |
| 4947 | || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID) |
| 4948 | /* |
| 4949 | * "Closed" stateid's exist *only* to return |
| 4950 | * nfserr_replay_me from the previous step, and |
| 4951 | * revoked delegations are kept only for free_stateid. |
| 4952 | */ |
| 4953 | return nfserr_bad_stateid; |
| 4954 | mutex_lock(&stp->st_mutex); |
| 4955 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); |
| 4956 | if (status == nfs_ok) |
| 4957 | status = nfs4_check_fh(current_fh, &stp->st_stid); |
| 4958 | if (status != nfs_ok) |
| 4959 | mutex_unlock(&stp->st_mutex); |
| 4960 | return status; |
| 4961 | } |
| 4962 | |
| 4963 | /* |
| 4964 | * Checks for sequence id mutating operations. |
| 4965 | */ |
| 4966 | static __be32 |
| 4967 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 4968 | stateid_t *stateid, char typemask, |
| 4969 | struct nfs4_ol_stateid **stpp, |
| 4970 | struct nfsd_net *nn) |
| 4971 | { |
| 4972 | __be32 status; |
| 4973 | struct nfs4_stid *s; |
| 4974 | struct nfs4_ol_stateid *stp = NULL; |
| 4975 | |
| 4976 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
| 4977 | seqid, STATEID_VAL(stateid)); |
| 4978 | |
| 4979 | *stpp = NULL; |
| 4980 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
| 4981 | if (status) |
| 4982 | return status; |
| 4983 | stp = openlockstateid(s); |
| 4984 | nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); |
| 4985 | |
| 4986 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
| 4987 | if (!status) |
| 4988 | *stpp = stp; |
| 4989 | else |
| 4990 | nfs4_put_stid(&stp->st_stid); |
| 4991 | return status; |
| 4992 | } |
| 4993 | |
| 4994 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 4995 | stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn) |
| 4996 | { |
| 4997 | __be32 status; |
| 4998 | struct nfs4_openowner *oo; |
| 4999 | struct nfs4_ol_stateid *stp; |
| 5000 | |
| 5001 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
| 5002 | NFS4_OPEN_STID, &stp, nn); |
| 5003 | if (status) |
| 5004 | return status; |
| 5005 | oo = openowner(stp->st_stateowner); |
| 5006 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
| 5007 | mutex_unlock(&stp->st_mutex); |
| 5008 | nfs4_put_stid(&stp->st_stid); |
| 5009 | return nfserr_bad_stateid; |
| 5010 | } |
| 5011 | *stpp = stp; |
| 5012 | return nfs_ok; |
| 5013 | } |
| 5014 | |
| 5015 | __be32 |
| 5016 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 5017 | struct nfsd4_open_confirm *oc) |
| 5018 | { |
| 5019 | __be32 status; |
| 5020 | struct nfs4_openowner *oo; |
| 5021 | struct nfs4_ol_stateid *stp; |
| 5022 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 5023 | |
| 5024 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 5025 | cstate->current_fh.fh_dentry); |
| 5026 | |
| 5027 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
| 5028 | if (status) |
| 5029 | return status; |
| 5030 | |
| 5031 | status = nfs4_preprocess_seqid_op(cstate, |
| 5032 | oc->oc_seqid, &oc->oc_req_stateid, |
| 5033 | NFS4_OPEN_STID, &stp, nn); |
| 5034 | if (status) |
| 5035 | goto out; |
| 5036 | oo = openowner(stp->st_stateowner); |
| 5037 | status = nfserr_bad_stateid; |
| 5038 | if (oo->oo_flags & NFS4_OO_CONFIRMED) { |
| 5039 | mutex_unlock(&stp->st_mutex); |
| 5040 | goto put_stateid; |
| 5041 | } |
| 5042 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
| 5043 | nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); |
| 5044 | mutex_unlock(&stp->st_mutex); |
| 5045 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
| 5046 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
| 5047 | |
| 5048 | nfsd4_client_record_create(oo->oo_owner.so_client); |
| 5049 | status = nfs_ok; |
| 5050 | put_stateid: |
| 5051 | nfs4_put_stid(&stp->st_stid); |
| 5052 | out: |
| 5053 | nfsd4_bump_seqid(cstate, status); |
| 5054 | return status; |
| 5055 | } |
| 5056 | |
| 5057 | static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access) |
| 5058 | { |
| 5059 | if (!test_access(access, stp)) |
| 5060 | return; |
| 5061 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
| 5062 | clear_access(access, stp); |
| 5063 | } |
| 5064 | |
| 5065 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 5066 | { |
| 5067 | switch (to_access) { |
| 5068 | case NFS4_SHARE_ACCESS_READ: |
| 5069 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 5070 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 5071 | break; |
| 5072 | case NFS4_SHARE_ACCESS_WRITE: |
| 5073 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 5074 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 5075 | break; |
| 5076 | case NFS4_SHARE_ACCESS_BOTH: |
| 5077 | break; |
| 5078 | default: |
| 5079 | WARN_ON_ONCE(1); |
| 5080 | } |
| 5081 | } |
| 5082 | |
| 5083 | __be32 |
| 5084 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
| 5085 | struct nfsd4_compound_state *cstate, |
| 5086 | struct nfsd4_open_downgrade *od) |
| 5087 | { |
| 5088 | __be32 status; |
| 5089 | struct nfs4_ol_stateid *stp; |
| 5090 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 5091 | |
| 5092 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 5093 | cstate->current_fh.fh_dentry); |
| 5094 | |
| 5095 | /* We don't yet support WANT bits: */ |
| 5096 | if (od->od_deleg_want) |
| 5097 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 5098 | od->od_deleg_want); |
| 5099 | |
| 5100 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
| 5101 | &od->od_stateid, &stp, nn); |
| 5102 | if (status) |
| 5103 | goto out; |
| 5104 | status = nfserr_inval; |
| 5105 | if (!test_access(od->od_share_access, stp)) { |
| 5106 | dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n", |
| 5107 | stp->st_access_bmap, od->od_share_access); |
| 5108 | goto put_stateid; |
| 5109 | } |
| 5110 | if (!test_deny(od->od_share_deny, stp)) { |
| 5111 | dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n", |
| 5112 | stp->st_deny_bmap, od->od_share_deny); |
| 5113 | goto put_stateid; |
| 5114 | } |
| 5115 | nfs4_stateid_downgrade(stp, od->od_share_access); |
| 5116 | reset_union_bmap_deny(od->od_share_deny, stp); |
| 5117 | nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); |
| 5118 | status = nfs_ok; |
| 5119 | put_stateid: |
| 5120 | mutex_unlock(&stp->st_mutex); |
| 5121 | nfs4_put_stid(&stp->st_stid); |
| 5122 | out: |
| 5123 | nfsd4_bump_seqid(cstate, status); |
| 5124 | return status; |
| 5125 | } |
| 5126 | |
| 5127 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 5128 | { |
| 5129 | struct nfs4_client *clp = s->st_stid.sc_client; |
| 5130 | bool unhashed; |
| 5131 | LIST_HEAD(reaplist); |
| 5132 | |
| 5133 | s->st_stid.sc_type = NFS4_CLOSED_STID; |
| 5134 | spin_lock(&clp->cl_lock); |
| 5135 | unhashed = unhash_open_stateid(s, &reaplist); |
| 5136 | |
| 5137 | if (clp->cl_minorversion) { |
| 5138 | if (unhashed) |
| 5139 | put_ol_stateid_locked(s, &reaplist); |
| 5140 | spin_unlock(&clp->cl_lock); |
| 5141 | free_ol_stateid_reaplist(&reaplist); |
| 5142 | } else { |
| 5143 | spin_unlock(&clp->cl_lock); |
| 5144 | free_ol_stateid_reaplist(&reaplist); |
| 5145 | if (unhashed) |
| 5146 | move_to_close_lru(s, clp->net); |
| 5147 | } |
| 5148 | } |
| 5149 | |
| 5150 | /* |
| 5151 | * nfs4_unlock_state() called after encode |
| 5152 | */ |
| 5153 | __be32 |
| 5154 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 5155 | struct nfsd4_close *close) |
| 5156 | { |
| 5157 | __be32 status; |
| 5158 | struct nfs4_ol_stateid *stp; |
| 5159 | struct net *net = SVC_NET(rqstp); |
| 5160 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 5161 | |
| 5162 | dprintk("NFSD: nfsd4_close on file %pd\n", |
| 5163 | cstate->current_fh.fh_dentry); |
| 5164 | |
| 5165 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 5166 | &close->cl_stateid, |
| 5167 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
| 5168 | &stp, nn); |
| 5169 | nfsd4_bump_seqid(cstate, status); |
| 5170 | if (status) |
| 5171 | goto out; |
| 5172 | nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); |
| 5173 | mutex_unlock(&stp->st_mutex); |
| 5174 | |
| 5175 | nfsd4_close_open_stateid(stp); |
| 5176 | |
| 5177 | /* put reference from nfs4_preprocess_seqid_op */ |
| 5178 | nfs4_put_stid(&stp->st_stid); |
| 5179 | out: |
| 5180 | return status; |
| 5181 | } |
| 5182 | |
| 5183 | __be32 |
| 5184 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 5185 | struct nfsd4_delegreturn *dr) |
| 5186 | { |
| 5187 | struct nfs4_delegation *dp; |
| 5188 | stateid_t *stateid = &dr->dr_stateid; |
| 5189 | struct nfs4_stid *s; |
| 5190 | __be32 status; |
| 5191 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 5192 | |
| 5193 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
| 5194 | return status; |
| 5195 | |
| 5196 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
| 5197 | if (status) |
| 5198 | goto out; |
| 5199 | dp = delegstateid(s); |
| 5200 | status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate)); |
| 5201 | if (status) |
| 5202 | goto put_stateid; |
| 5203 | |
| 5204 | destroy_delegation(dp); |
| 5205 | put_stateid: |
| 5206 | nfs4_put_stid(&dp->dl_stid); |
| 5207 | out: |
| 5208 | return status; |
| 5209 | } |
| 5210 | |
| 5211 | static inline u64 |
| 5212 | end_offset(u64 start, u64 len) |
| 5213 | { |
| 5214 | u64 end; |
| 5215 | |
| 5216 | end = start + len; |
| 5217 | return end >= start ? end: NFS4_MAX_UINT64; |
| 5218 | } |
| 5219 | |
| 5220 | /* last octet in a range */ |
| 5221 | static inline u64 |
| 5222 | last_byte_offset(u64 start, u64 len) |
| 5223 | { |
| 5224 | u64 end; |
| 5225 | |
| 5226 | WARN_ON_ONCE(!len); |
| 5227 | end = start + len; |
| 5228 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 5229 | } |
| 5230 | |
| 5231 | /* |
| 5232 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 5233 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 5234 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 5235 | * locking, this prevents us from being completely protocol-compliant. The |
| 5236 | * real solution to this problem is to start using unsigned file offsets in |
| 5237 | * the VFS, but this is a very deep change! |
| 5238 | */ |
| 5239 | static inline void |
| 5240 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 5241 | { |
| 5242 | if (lock->fl_start < 0) |
| 5243 | lock->fl_start = OFFSET_MAX; |
| 5244 | if (lock->fl_end < 0) |
| 5245 | lock->fl_end = OFFSET_MAX; |
| 5246 | } |
| 5247 | |
| 5248 | static fl_owner_t |
| 5249 | nfsd4_fl_get_owner(fl_owner_t owner) |
| 5250 | { |
| 5251 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
| 5252 | |
| 5253 | nfs4_get_stateowner(&lo->lo_owner); |
| 5254 | return owner; |
| 5255 | } |
| 5256 | |
| 5257 | static void |
| 5258 | nfsd4_fl_put_owner(fl_owner_t owner) |
| 5259 | { |
| 5260 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
| 5261 | |
| 5262 | if (lo) |
| 5263 | nfs4_put_stateowner(&lo->lo_owner); |
| 5264 | } |
| 5265 | |
| 5266 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
| 5267 | .lm_get_owner = nfsd4_fl_get_owner, |
| 5268 | .lm_put_owner = nfsd4_fl_put_owner, |
| 5269 | }; |
| 5270 | |
| 5271 | static inline void |
| 5272 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 5273 | { |
| 5274 | struct nfs4_lockowner *lo; |
| 5275 | |
| 5276 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
| 5277 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
| 5278 | deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data, |
| 5279 | lo->lo_owner.so_owner.len, GFP_KERNEL); |
| 5280 | if (!deny->ld_owner.data) |
| 5281 | /* We just don't care that much */ |
| 5282 | goto nevermind; |
| 5283 | deny->ld_owner.len = lo->lo_owner.so_owner.len; |
| 5284 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
| 5285 | } else { |
| 5286 | nevermind: |
| 5287 | deny->ld_owner.len = 0; |
| 5288 | deny->ld_owner.data = NULL; |
| 5289 | deny->ld_clientid.cl_boot = 0; |
| 5290 | deny->ld_clientid.cl_id = 0; |
| 5291 | } |
| 5292 | deny->ld_start = fl->fl_start; |
| 5293 | deny->ld_length = NFS4_MAX_UINT64; |
| 5294 | if (fl->fl_end != NFS4_MAX_UINT64) |
| 5295 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 5296 | deny->ld_type = NFS4_READ_LT; |
| 5297 | if (fl->fl_type != F_RDLCK) |
| 5298 | deny->ld_type = NFS4_WRITE_LT; |
| 5299 | } |
| 5300 | |
| 5301 | static struct nfs4_lockowner * |
| 5302 | find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) |
| 5303 | { |
| 5304 | unsigned int strhashval = ownerstr_hashval(owner); |
| 5305 | struct nfs4_stateowner *so; |
| 5306 | |
| 5307 | lockdep_assert_held(&clp->cl_lock); |
| 5308 | |
| 5309 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], |
| 5310 | so_strhash) { |
| 5311 | if (so->so_is_open_owner) |
| 5312 | continue; |
| 5313 | if (same_owner_str(so, owner)) |
| 5314 | return lockowner(nfs4_get_stateowner(so)); |
| 5315 | } |
| 5316 | return NULL; |
| 5317 | } |
| 5318 | |
| 5319 | static struct nfs4_lockowner * |
| 5320 | find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) |
| 5321 | { |
| 5322 | struct nfs4_lockowner *lo; |
| 5323 | |
| 5324 | spin_lock(&clp->cl_lock); |
| 5325 | lo = find_lockowner_str_locked(clp, owner); |
| 5326 | spin_unlock(&clp->cl_lock); |
| 5327 | return lo; |
| 5328 | } |
| 5329 | |
| 5330 | static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) |
| 5331 | { |
| 5332 | unhash_lockowner_locked(lockowner(sop)); |
| 5333 | } |
| 5334 | |
| 5335 | static void nfs4_free_lockowner(struct nfs4_stateowner *sop) |
| 5336 | { |
| 5337 | struct nfs4_lockowner *lo = lockowner(sop); |
| 5338 | |
| 5339 | kmem_cache_free(lockowner_slab, lo); |
| 5340 | } |
| 5341 | |
| 5342 | static const struct nfs4_stateowner_operations lockowner_ops = { |
| 5343 | .so_unhash = nfs4_unhash_lockowner, |
| 5344 | .so_free = nfs4_free_lockowner, |
| 5345 | }; |
| 5346 | |
| 5347 | /* |
| 5348 | * Alloc a lock owner structure. |
| 5349 | * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has |
| 5350 | * occurred. |
| 5351 | * |
| 5352 | * strhashval = ownerstr_hashval |
| 5353 | */ |
| 5354 | static struct nfs4_lockowner * |
| 5355 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
| 5356 | struct nfs4_ol_stateid *open_stp, |
| 5357 | struct nfsd4_lock *lock) |
| 5358 | { |
| 5359 | struct nfs4_lockowner *lo, *ret; |
| 5360 | |
| 5361 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 5362 | if (!lo) |
| 5363 | return NULL; |
| 5364 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 5365 | lo->lo_owner.so_is_open_owner = 0; |
| 5366 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; |
| 5367 | lo->lo_owner.so_ops = &lockowner_ops; |
| 5368 | spin_lock(&clp->cl_lock); |
| 5369 | ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); |
| 5370 | if (ret == NULL) { |
| 5371 | list_add(&lo->lo_owner.so_strhash, |
| 5372 | &clp->cl_ownerstr_hashtbl[strhashval]); |
| 5373 | ret = lo; |
| 5374 | } else |
| 5375 | nfs4_free_stateowner(&lo->lo_owner); |
| 5376 | |
| 5377 | spin_unlock(&clp->cl_lock); |
| 5378 | return ret; |
| 5379 | } |
| 5380 | |
| 5381 | static void |
| 5382 | init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, |
| 5383 | struct nfs4_file *fp, struct inode *inode, |
| 5384 | struct nfs4_ol_stateid *open_stp) |
| 5385 | { |
| 5386 | struct nfs4_client *clp = lo->lo_owner.so_client; |
| 5387 | |
| 5388 | lockdep_assert_held(&clp->cl_lock); |
| 5389 | |
| 5390 | atomic_inc(&stp->st_stid.sc_count); |
| 5391 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
| 5392 | stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); |
| 5393 | get_nfs4_file(fp); |
| 5394 | stp->st_stid.sc_file = fp; |
| 5395 | stp->st_access_bmap = 0; |
| 5396 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
| 5397 | stp->st_openstp = open_stp; |
| 5398 | mutex_init(&stp->st_mutex); |
| 5399 | list_add(&stp->st_locks, &open_stp->st_locks); |
| 5400 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
| 5401 | spin_lock(&fp->fi_lock); |
| 5402 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 5403 | spin_unlock(&fp->fi_lock); |
| 5404 | } |
| 5405 | |
| 5406 | static struct nfs4_ol_stateid * |
| 5407 | find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) |
| 5408 | { |
| 5409 | struct nfs4_ol_stateid *lst; |
| 5410 | struct nfs4_client *clp = lo->lo_owner.so_client; |
| 5411 | |
| 5412 | lockdep_assert_held(&clp->cl_lock); |
| 5413 | |
| 5414 | list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { |
| 5415 | if (lst->st_stid.sc_file == fp) { |
| 5416 | atomic_inc(&lst->st_stid.sc_count); |
| 5417 | return lst; |
| 5418 | } |
| 5419 | } |
| 5420 | return NULL; |
| 5421 | } |
| 5422 | |
| 5423 | static struct nfs4_ol_stateid * |
| 5424 | find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, |
| 5425 | struct inode *inode, struct nfs4_ol_stateid *ost, |
| 5426 | bool *new) |
| 5427 | { |
| 5428 | struct nfs4_stid *ns = NULL; |
| 5429 | struct nfs4_ol_stateid *lst; |
| 5430 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 5431 | struct nfs4_client *clp = oo->oo_owner.so_client; |
| 5432 | |
| 5433 | spin_lock(&clp->cl_lock); |
| 5434 | lst = find_lock_stateid(lo, fi); |
| 5435 | if (lst == NULL) { |
| 5436 | spin_unlock(&clp->cl_lock); |
| 5437 | ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid); |
| 5438 | if (ns == NULL) |
| 5439 | return NULL; |
| 5440 | |
| 5441 | spin_lock(&clp->cl_lock); |
| 5442 | lst = find_lock_stateid(lo, fi); |
| 5443 | if (likely(!lst)) { |
| 5444 | lst = openlockstateid(ns); |
| 5445 | init_lock_stateid(lst, lo, fi, inode, ost); |
| 5446 | ns = NULL; |
| 5447 | *new = true; |
| 5448 | } |
| 5449 | } |
| 5450 | spin_unlock(&clp->cl_lock); |
| 5451 | if (ns) |
| 5452 | nfs4_put_stid(ns); |
| 5453 | return lst; |
| 5454 | } |
| 5455 | |
| 5456 | static int |
| 5457 | check_lock_length(u64 offset, u64 length) |
| 5458 | { |
| 5459 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
| 5460 | (length > ~offset))); |
| 5461 | } |
| 5462 | |
| 5463 | static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access) |
| 5464 | { |
| 5465 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
| 5466 | |
| 5467 | lockdep_assert_held(&fp->fi_lock); |
| 5468 | |
| 5469 | if (test_access(access, lock_stp)) |
| 5470 | return; |
| 5471 | __nfs4_file_get_access(fp, access); |
| 5472 | set_access(access, lock_stp); |
| 5473 | } |
| 5474 | |
| 5475 | static __be32 |
| 5476 | lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, |
| 5477 | struct nfs4_ol_stateid *ost, |
| 5478 | struct nfsd4_lock *lock, |
| 5479 | struct nfs4_ol_stateid **plst, bool *new) |
| 5480 | { |
| 5481 | __be32 status; |
| 5482 | struct nfs4_file *fi = ost->st_stid.sc_file; |
| 5483 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 5484 | struct nfs4_client *cl = oo->oo_owner.so_client; |
| 5485 | struct inode *inode = d_inode(cstate->current_fh.fh_dentry); |
| 5486 | struct nfs4_lockowner *lo; |
| 5487 | struct nfs4_ol_stateid *lst; |
| 5488 | unsigned int strhashval; |
| 5489 | bool hashed; |
| 5490 | |
| 5491 | lo = find_lockowner_str(cl, &lock->lk_new_owner); |
| 5492 | if (!lo) { |
| 5493 | strhashval = ownerstr_hashval(&lock->lk_new_owner); |
| 5494 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 5495 | if (lo == NULL) |
| 5496 | return nfserr_jukebox; |
| 5497 | } else { |
| 5498 | /* with an existing lockowner, seqids must be the same */ |
| 5499 | status = nfserr_bad_seqid; |
| 5500 | if (!cstate->minorversion && |
| 5501 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
| 5502 | goto out; |
| 5503 | } |
| 5504 | |
| 5505 | retry: |
| 5506 | lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); |
| 5507 | if (lst == NULL) { |
| 5508 | status = nfserr_jukebox; |
| 5509 | goto out; |
| 5510 | } |
| 5511 | |
| 5512 | mutex_lock(&lst->st_mutex); |
| 5513 | |
| 5514 | /* See if it's still hashed to avoid race with FREE_STATEID */ |
| 5515 | spin_lock(&cl->cl_lock); |
| 5516 | hashed = !list_empty(&lst->st_perfile); |
| 5517 | spin_unlock(&cl->cl_lock); |
| 5518 | |
| 5519 | if (!hashed) { |
| 5520 | mutex_unlock(&lst->st_mutex); |
| 5521 | nfs4_put_stid(&lst->st_stid); |
| 5522 | goto retry; |
| 5523 | } |
| 5524 | status = nfs_ok; |
| 5525 | *plst = lst; |
| 5526 | out: |
| 5527 | nfs4_put_stateowner(&lo->lo_owner); |
| 5528 | return status; |
| 5529 | } |
| 5530 | |
| 5531 | /* |
| 5532 | * LOCK operation |
| 5533 | */ |
| 5534 | __be32 |
| 5535 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 5536 | struct nfsd4_lock *lock) |
| 5537 | { |
| 5538 | struct nfs4_openowner *open_sop = NULL; |
| 5539 | struct nfs4_lockowner *lock_sop = NULL; |
| 5540 | struct nfs4_ol_stateid *lock_stp = NULL; |
| 5541 | struct nfs4_ol_stateid *open_stp = NULL; |
| 5542 | struct nfs4_file *fp; |
| 5543 | struct file *filp = NULL; |
| 5544 | struct file_lock *file_lock = NULL; |
| 5545 | struct file_lock *conflock = NULL; |
| 5546 | __be32 status = 0; |
| 5547 | int lkflg; |
| 5548 | int err; |
| 5549 | bool new = false; |
| 5550 | struct net *net = SVC_NET(rqstp); |
| 5551 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 5552 | |
| 5553 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 5554 | (long long) lock->lk_offset, |
| 5555 | (long long) lock->lk_length); |
| 5556 | |
| 5557 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 5558 | return nfserr_inval; |
| 5559 | |
| 5560 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
| 5561 | S_IFREG, NFSD_MAY_LOCK))) { |
| 5562 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 5563 | return status; |
| 5564 | } |
| 5565 | |
| 5566 | if (lock->lk_is_new) { |
| 5567 | if (nfsd4_has_session(cstate)) |
| 5568 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
| 5569 | memcpy(&lock->lk_new_clientid, |
| 5570 | &cstate->session->se_client->cl_clientid, |
| 5571 | sizeof(clientid_t)); |
| 5572 | |
| 5573 | status = nfserr_stale_clientid; |
| 5574 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
| 5575 | goto out; |
| 5576 | |
| 5577 | /* validate and update open stateid and open seqid */ |
| 5578 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
| 5579 | lock->lk_new_open_seqid, |
| 5580 | &lock->lk_new_open_stateid, |
| 5581 | &open_stp, nn); |
| 5582 | if (status) |
| 5583 | goto out; |
| 5584 | mutex_unlock(&open_stp->st_mutex); |
| 5585 | open_sop = openowner(open_stp->st_stateowner); |
| 5586 | status = nfserr_bad_stateid; |
| 5587 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
| 5588 | &lock->lk_new_clientid)) |
| 5589 | goto out; |
| 5590 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
| 5591 | &lock_stp, &new); |
| 5592 | } else { |
| 5593 | status = nfs4_preprocess_seqid_op(cstate, |
| 5594 | lock->lk_old_lock_seqid, |
| 5595 | &lock->lk_old_lock_stateid, |
| 5596 | NFS4_LOCK_STID, &lock_stp, nn); |
| 5597 | } |
| 5598 | if (status) |
| 5599 | goto out; |
| 5600 | lock_sop = lockowner(lock_stp->st_stateowner); |
| 5601 | |
| 5602 | lkflg = setlkflg(lock->lk_type); |
| 5603 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 5604 | if (status) |
| 5605 | goto out; |
| 5606 | |
| 5607 | status = nfserr_grace; |
| 5608 | if (locks_in_grace(net) && !lock->lk_reclaim) |
| 5609 | goto out; |
| 5610 | status = nfserr_no_grace; |
| 5611 | if (!locks_in_grace(net) && lock->lk_reclaim) |
| 5612 | goto out; |
| 5613 | |
| 5614 | file_lock = locks_alloc_lock(); |
| 5615 | if (!file_lock) { |
| 5616 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5617 | status = nfserr_jukebox; |
| 5618 | goto out; |
| 5619 | } |
| 5620 | |
| 5621 | fp = lock_stp->st_stid.sc_file; |
| 5622 | switch (lock->lk_type) { |
| 5623 | case NFS4_READ_LT: |
| 5624 | case NFS4_READW_LT: |
| 5625 | spin_lock(&fp->fi_lock); |
| 5626 | filp = find_readable_file_locked(fp); |
| 5627 | if (filp) |
| 5628 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
| 5629 | spin_unlock(&fp->fi_lock); |
| 5630 | file_lock->fl_type = F_RDLCK; |
| 5631 | break; |
| 5632 | case NFS4_WRITE_LT: |
| 5633 | case NFS4_WRITEW_LT: |
| 5634 | spin_lock(&fp->fi_lock); |
| 5635 | filp = find_writeable_file_locked(fp); |
| 5636 | if (filp) |
| 5637 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
| 5638 | spin_unlock(&fp->fi_lock); |
| 5639 | file_lock->fl_type = F_WRLCK; |
| 5640 | break; |
| 5641 | default: |
| 5642 | status = nfserr_inval; |
| 5643 | goto out; |
| 5644 | } |
| 5645 | if (!filp) { |
| 5646 | status = nfserr_openmode; |
| 5647 | goto out; |
| 5648 | } |
| 5649 | |
| 5650 | file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner)); |
| 5651 | file_lock->fl_pid = current->tgid; |
| 5652 | file_lock->fl_file = filp; |
| 5653 | file_lock->fl_flags = FL_POSIX; |
| 5654 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 5655 | file_lock->fl_start = lock->lk_offset; |
| 5656 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 5657 | nfs4_transform_lock_offset(file_lock); |
| 5658 | |
| 5659 | conflock = locks_alloc_lock(); |
| 5660 | if (!conflock) { |
| 5661 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5662 | status = nfserr_jukebox; |
| 5663 | goto out; |
| 5664 | } |
| 5665 | |
| 5666 | err = vfs_lock_file(filp, F_SETLK, file_lock, conflock); |
| 5667 | switch (-err) { |
| 5668 | case 0: /* success! */ |
| 5669 | nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid); |
| 5670 | status = 0; |
| 5671 | break; |
| 5672 | case (EAGAIN): /* conflock holds conflicting lock */ |
| 5673 | status = nfserr_denied; |
| 5674 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
| 5675 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
| 5676 | break; |
| 5677 | case (EDEADLK): |
| 5678 | status = nfserr_deadlock; |
| 5679 | break; |
| 5680 | default: |
| 5681 | dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err); |
| 5682 | status = nfserrno(err); |
| 5683 | break; |
| 5684 | } |
| 5685 | out: |
| 5686 | if (filp) |
| 5687 | fput(filp); |
| 5688 | if (lock_stp) { |
| 5689 | /* Bump seqid manually if the 4.0 replay owner is openowner */ |
| 5690 | if (cstate->replay_owner && |
| 5691 | cstate->replay_owner != &lock_sop->lo_owner && |
| 5692 | seqid_mutating_err(ntohl(status))) |
| 5693 | lock_sop->lo_owner.so_seqid++; |
| 5694 | |
| 5695 | mutex_unlock(&lock_stp->st_mutex); |
| 5696 | |
| 5697 | /* |
| 5698 | * If this is a new, never-before-used stateid, and we are |
| 5699 | * returning an error, then just go ahead and release it. |
| 5700 | */ |
| 5701 | if (status && new) |
| 5702 | release_lock_stateid(lock_stp); |
| 5703 | |
| 5704 | nfs4_put_stid(&lock_stp->st_stid); |
| 5705 | } |
| 5706 | if (open_stp) |
| 5707 | nfs4_put_stid(&open_stp->st_stid); |
| 5708 | nfsd4_bump_seqid(cstate, status); |
| 5709 | if (file_lock) |
| 5710 | locks_free_lock(file_lock); |
| 5711 | if (conflock) |
| 5712 | locks_free_lock(conflock); |
| 5713 | return status; |
| 5714 | } |
| 5715 | |
| 5716 | /* |
| 5717 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 5718 | * so we do a temporary open here just to get an open file to pass to |
| 5719 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 5720 | * inode operation.) |
| 5721 | */ |
| 5722 | static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock) |
| 5723 | { |
| 5724 | struct file *file; |
| 5725 | __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); |
| 5726 | if (!err) { |
| 5727 | err = nfserrno(vfs_test_lock(file, lock)); |
| 5728 | fput(file); |
| 5729 | } |
| 5730 | return err; |
| 5731 | } |
| 5732 | |
| 5733 | /* |
| 5734 | * LOCKT operation |
| 5735 | */ |
| 5736 | __be32 |
| 5737 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 5738 | struct nfsd4_lockt *lockt) |
| 5739 | { |
| 5740 | struct file_lock *file_lock = NULL; |
| 5741 | struct nfs4_lockowner *lo = NULL; |
| 5742 | __be32 status; |
| 5743 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 5744 | |
| 5745 | if (locks_in_grace(SVC_NET(rqstp))) |
| 5746 | return nfserr_grace; |
| 5747 | |
| 5748 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 5749 | return nfserr_inval; |
| 5750 | |
| 5751 | if (!nfsd4_has_session(cstate)) { |
| 5752 | status = lookup_clientid(&lockt->lt_clientid, cstate, nn); |
| 5753 | if (status) |
| 5754 | goto out; |
| 5755 | } |
| 5756 | |
| 5757 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
| 5758 | goto out; |
| 5759 | |
| 5760 | file_lock = locks_alloc_lock(); |
| 5761 | if (!file_lock) { |
| 5762 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5763 | status = nfserr_jukebox; |
| 5764 | goto out; |
| 5765 | } |
| 5766 | |
| 5767 | switch (lockt->lt_type) { |
| 5768 | case NFS4_READ_LT: |
| 5769 | case NFS4_READW_LT: |
| 5770 | file_lock->fl_type = F_RDLCK; |
| 5771 | break; |
| 5772 | case NFS4_WRITE_LT: |
| 5773 | case NFS4_WRITEW_LT: |
| 5774 | file_lock->fl_type = F_WRLCK; |
| 5775 | break; |
| 5776 | default: |
| 5777 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
| 5778 | status = nfserr_inval; |
| 5779 | goto out; |
| 5780 | } |
| 5781 | |
| 5782 | lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); |
| 5783 | if (lo) |
| 5784 | file_lock->fl_owner = (fl_owner_t)lo; |
| 5785 | file_lock->fl_pid = current->tgid; |
| 5786 | file_lock->fl_flags = FL_POSIX; |
| 5787 | |
| 5788 | file_lock->fl_start = lockt->lt_offset; |
| 5789 | file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length); |
| 5790 | |
| 5791 | nfs4_transform_lock_offset(file_lock); |
| 5792 | |
| 5793 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
| 5794 | if (status) |
| 5795 | goto out; |
| 5796 | |
| 5797 | if (file_lock->fl_type != F_UNLCK) { |
| 5798 | status = nfserr_denied; |
| 5799 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
| 5800 | } |
| 5801 | out: |
| 5802 | if (lo) |
| 5803 | nfs4_put_stateowner(&lo->lo_owner); |
| 5804 | if (file_lock) |
| 5805 | locks_free_lock(file_lock); |
| 5806 | return status; |
| 5807 | } |
| 5808 | |
| 5809 | __be32 |
| 5810 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 5811 | struct nfsd4_locku *locku) |
| 5812 | { |
| 5813 | struct nfs4_ol_stateid *stp; |
| 5814 | struct file *filp = NULL; |
| 5815 | struct file_lock *file_lock = NULL; |
| 5816 | __be32 status; |
| 5817 | int err; |
| 5818 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 5819 | |
| 5820 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 5821 | (long long) locku->lu_offset, |
| 5822 | (long long) locku->lu_length); |
| 5823 | |
| 5824 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 5825 | return nfserr_inval; |
| 5826 | |
| 5827 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
| 5828 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 5829 | &stp, nn); |
| 5830 | if (status) |
| 5831 | goto out; |
| 5832 | filp = find_any_file(stp->st_stid.sc_file); |
| 5833 | if (!filp) { |
| 5834 | status = nfserr_lock_range; |
| 5835 | goto put_stateid; |
| 5836 | } |
| 5837 | file_lock = locks_alloc_lock(); |
| 5838 | if (!file_lock) { |
| 5839 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 5840 | status = nfserr_jukebox; |
| 5841 | goto fput; |
| 5842 | } |
| 5843 | |
| 5844 | file_lock->fl_type = F_UNLCK; |
| 5845 | file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner)); |
| 5846 | file_lock->fl_pid = current->tgid; |
| 5847 | file_lock->fl_file = filp; |
| 5848 | file_lock->fl_flags = FL_POSIX; |
| 5849 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 5850 | file_lock->fl_start = locku->lu_offset; |
| 5851 | |
| 5852 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 5853 | locku->lu_length); |
| 5854 | nfs4_transform_lock_offset(file_lock); |
| 5855 | |
| 5856 | err = vfs_lock_file(filp, F_SETLK, file_lock, NULL); |
| 5857 | if (err) { |
| 5858 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
| 5859 | goto out_nfserr; |
| 5860 | } |
| 5861 | nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); |
| 5862 | fput: |
| 5863 | fput(filp); |
| 5864 | put_stateid: |
| 5865 | mutex_unlock(&stp->st_mutex); |
| 5866 | nfs4_put_stid(&stp->st_stid); |
| 5867 | out: |
| 5868 | nfsd4_bump_seqid(cstate, status); |
| 5869 | if (file_lock) |
| 5870 | locks_free_lock(file_lock); |
| 5871 | return status; |
| 5872 | |
| 5873 | out_nfserr: |
| 5874 | status = nfserrno(err); |
| 5875 | goto fput; |
| 5876 | } |
| 5877 | |
| 5878 | /* |
| 5879 | * returns |
| 5880 | * true: locks held by lockowner |
| 5881 | * false: no locks held by lockowner |
| 5882 | */ |
| 5883 | static bool |
| 5884 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) |
| 5885 | { |
| 5886 | struct file_lock *fl; |
| 5887 | int status = false; |
| 5888 | struct file *filp = find_any_file(fp); |
| 5889 | struct inode *inode; |
| 5890 | struct file_lock_context *flctx; |
| 5891 | |
| 5892 | if (!filp) { |
| 5893 | /* Any valid lock stateid should have some sort of access */ |
| 5894 | WARN_ON_ONCE(1); |
| 5895 | return status; |
| 5896 | } |
| 5897 | |
| 5898 | inode = file_inode(filp); |
| 5899 | flctx = inode->i_flctx; |
| 5900 | |
| 5901 | if (flctx && !list_empty_careful(&flctx->flc_posix)) { |
| 5902 | spin_lock(&flctx->flc_lock); |
| 5903 | list_for_each_entry(fl, &flctx->flc_posix, fl_list) { |
| 5904 | if (fl->fl_owner == (fl_owner_t)lowner) { |
| 5905 | status = true; |
| 5906 | break; |
| 5907 | } |
| 5908 | } |
| 5909 | spin_unlock(&flctx->flc_lock); |
| 5910 | } |
| 5911 | fput(filp); |
| 5912 | return status; |
| 5913 | } |
| 5914 | |
| 5915 | __be32 |
| 5916 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 5917 | struct nfsd4_compound_state *cstate, |
| 5918 | struct nfsd4_release_lockowner *rlockowner) |
| 5919 | { |
| 5920 | clientid_t *clid = &rlockowner->rl_clientid; |
| 5921 | struct nfs4_stateowner *sop; |
| 5922 | struct nfs4_lockowner *lo = NULL; |
| 5923 | struct nfs4_ol_stateid *stp; |
| 5924 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
| 5925 | unsigned int hashval = ownerstr_hashval(owner); |
| 5926 | __be32 status; |
| 5927 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 5928 | struct nfs4_client *clp; |
| 5929 | LIST_HEAD (reaplist); |
| 5930 | |
| 5931 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 5932 | clid->cl_boot, clid->cl_id); |
| 5933 | |
| 5934 | status = lookup_clientid(clid, cstate, nn); |
| 5935 | if (status) |
| 5936 | return status; |
| 5937 | |
| 5938 | clp = cstate->clp; |
| 5939 | /* Find the matching lock stateowner */ |
| 5940 | spin_lock(&clp->cl_lock); |
| 5941 | list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval], |
| 5942 | so_strhash) { |
| 5943 | |
| 5944 | if (sop->so_is_open_owner || !same_owner_str(sop, owner)) |
| 5945 | continue; |
| 5946 | |
| 5947 | /* see if there are still any locks associated with it */ |
| 5948 | lo = lockowner(sop); |
| 5949 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { |
| 5950 | if (check_for_locks(stp->st_stid.sc_file, lo)) { |
| 5951 | status = nfserr_locks_held; |
| 5952 | spin_unlock(&clp->cl_lock); |
| 5953 | return status; |
| 5954 | } |
| 5955 | } |
| 5956 | |
| 5957 | nfs4_get_stateowner(sop); |
| 5958 | break; |
| 5959 | } |
| 5960 | if (!lo) { |
| 5961 | spin_unlock(&clp->cl_lock); |
| 5962 | return status; |
| 5963 | } |
| 5964 | |
| 5965 | unhash_lockowner_locked(lo); |
| 5966 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 5967 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
| 5968 | struct nfs4_ol_stateid, |
| 5969 | st_perstateowner); |
| 5970 | WARN_ON(!unhash_lock_stateid(stp)); |
| 5971 | put_ol_stateid_locked(stp, &reaplist); |
| 5972 | } |
| 5973 | spin_unlock(&clp->cl_lock); |
| 5974 | free_ol_stateid_reaplist(&reaplist); |
| 5975 | nfs4_put_stateowner(&lo->lo_owner); |
| 5976 | |
| 5977 | return status; |
| 5978 | } |
| 5979 | |
| 5980 | static inline struct nfs4_client_reclaim * |
| 5981 | alloc_reclaim(void) |
| 5982 | { |
| 5983 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
| 5984 | } |
| 5985 | |
| 5986 | bool |
| 5987 | nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn) |
| 5988 | { |
| 5989 | struct nfs4_client_reclaim *crp; |
| 5990 | |
| 5991 | crp = nfsd4_find_reclaim_client(name, nn); |
| 5992 | return (crp && crp->cr_clp); |
| 5993 | } |
| 5994 | |
| 5995 | /* |
| 5996 | * failure => all reset bets are off, nfserr_no_grace... |
| 5997 | */ |
| 5998 | struct nfs4_client_reclaim * |
| 5999 | nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn) |
| 6000 | { |
| 6001 | unsigned int strhashval; |
| 6002 | struct nfs4_client_reclaim *crp; |
| 6003 | |
| 6004 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
| 6005 | crp = alloc_reclaim(); |
| 6006 | if (crp) { |
| 6007 | strhashval = clientstr_hashval(name); |
| 6008 | INIT_LIST_HEAD(&crp->cr_strhash); |
| 6009 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
| 6010 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
| 6011 | crp->cr_clp = NULL; |
| 6012 | nn->reclaim_str_hashtbl_size++; |
| 6013 | } |
| 6014 | return crp; |
| 6015 | } |
| 6016 | |
| 6017 | void |
| 6018 | nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn) |
| 6019 | { |
| 6020 | list_del(&crp->cr_strhash); |
| 6021 | kfree(crp); |
| 6022 | nn->reclaim_str_hashtbl_size--; |
| 6023 | } |
| 6024 | |
| 6025 | void |
| 6026 | nfs4_release_reclaim(struct nfsd_net *nn) |
| 6027 | { |
| 6028 | struct nfs4_client_reclaim *crp = NULL; |
| 6029 | int i; |
| 6030 | |
| 6031 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 6032 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 6033 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
| 6034 | struct nfs4_client_reclaim, cr_strhash); |
| 6035 | nfs4_remove_reclaim_record(crp, nn); |
| 6036 | } |
| 6037 | } |
| 6038 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
| 6039 | } |
| 6040 | |
| 6041 | /* |
| 6042 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
| 6043 | struct nfs4_client_reclaim * |
| 6044 | nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn) |
| 6045 | { |
| 6046 | unsigned int strhashval; |
| 6047 | struct nfs4_client_reclaim *crp = NULL; |
| 6048 | |
| 6049 | dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir); |
| 6050 | |
| 6051 | strhashval = clientstr_hashval(recdir); |
| 6052 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
| 6053 | if (same_name(crp->cr_recdir, recdir)) { |
| 6054 | return crp; |
| 6055 | } |
| 6056 | } |
| 6057 | return NULL; |
| 6058 | } |
| 6059 | |
| 6060 | /* |
| 6061 | * Called from OPEN. Look for clientid in reclaim list. |
| 6062 | */ |
| 6063 | __be32 |
| 6064 | nfs4_check_open_reclaim(clientid_t *clid, |
| 6065 | struct nfsd4_compound_state *cstate, |
| 6066 | struct nfsd_net *nn) |
| 6067 | { |
| 6068 | __be32 status; |
| 6069 | |
| 6070 | /* find clientid in conf_id_hashtbl */ |
| 6071 | status = lookup_clientid(clid, cstate, nn); |
| 6072 | if (status) |
| 6073 | return nfserr_reclaim_bad; |
| 6074 | |
| 6075 | if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags)) |
| 6076 | return nfserr_no_grace; |
| 6077 | |
| 6078 | if (nfsd4_client_record_check(cstate->clp)) |
| 6079 | return nfserr_reclaim_bad; |
| 6080 | |
| 6081 | return nfs_ok; |
| 6082 | } |
| 6083 | |
| 6084 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
| 6085 | static inline void |
| 6086 | put_client(struct nfs4_client *clp) |
| 6087 | { |
| 6088 | atomic_dec(&clp->cl_refcount); |
| 6089 | } |
| 6090 | |
| 6091 | static struct nfs4_client * |
| 6092 | nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) |
| 6093 | { |
| 6094 | struct nfs4_client *clp; |
| 6095 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6096 | nfsd_net_id); |
| 6097 | |
| 6098 | if (!nfsd_netns_ready(nn)) |
| 6099 | return NULL; |
| 6100 | |
| 6101 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6102 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) |
| 6103 | return clp; |
| 6104 | } |
| 6105 | return NULL; |
| 6106 | } |
| 6107 | |
| 6108 | u64 |
| 6109 | nfsd_inject_print_clients(void) |
| 6110 | { |
| 6111 | struct nfs4_client *clp; |
| 6112 | u64 count = 0; |
| 6113 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6114 | nfsd_net_id); |
| 6115 | char buf[INET6_ADDRSTRLEN]; |
| 6116 | |
| 6117 | if (!nfsd_netns_ready(nn)) |
| 6118 | return 0; |
| 6119 | |
| 6120 | spin_lock(&nn->client_lock); |
| 6121 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6122 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
| 6123 | pr_info("NFS Client: %s\n", buf); |
| 6124 | ++count; |
| 6125 | } |
| 6126 | spin_unlock(&nn->client_lock); |
| 6127 | |
| 6128 | return count; |
| 6129 | } |
| 6130 | |
| 6131 | u64 |
| 6132 | nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size) |
| 6133 | { |
| 6134 | u64 count = 0; |
| 6135 | struct nfs4_client *clp; |
| 6136 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6137 | nfsd_net_id); |
| 6138 | |
| 6139 | if (!nfsd_netns_ready(nn)) |
| 6140 | return count; |
| 6141 | |
| 6142 | spin_lock(&nn->client_lock); |
| 6143 | clp = nfsd_find_client(addr, addr_size); |
| 6144 | if (clp) { |
| 6145 | if (mark_client_expired_locked(clp) == nfs_ok) |
| 6146 | ++count; |
| 6147 | else |
| 6148 | clp = NULL; |
| 6149 | } |
| 6150 | spin_unlock(&nn->client_lock); |
| 6151 | |
| 6152 | if (clp) |
| 6153 | expire_client(clp); |
| 6154 | |
| 6155 | return count; |
| 6156 | } |
| 6157 | |
| 6158 | u64 |
| 6159 | nfsd_inject_forget_clients(u64 max) |
| 6160 | { |
| 6161 | u64 count = 0; |
| 6162 | struct nfs4_client *clp, *next; |
| 6163 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6164 | nfsd_net_id); |
| 6165 | LIST_HEAD(reaplist); |
| 6166 | |
| 6167 | if (!nfsd_netns_ready(nn)) |
| 6168 | return count; |
| 6169 | |
| 6170 | spin_lock(&nn->client_lock); |
| 6171 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 6172 | if (mark_client_expired_locked(clp) == nfs_ok) { |
| 6173 | list_add(&clp->cl_lru, &reaplist); |
| 6174 | if (max != 0 && ++count >= max) |
| 6175 | break; |
| 6176 | } |
| 6177 | } |
| 6178 | spin_unlock(&nn->client_lock); |
| 6179 | |
| 6180 | list_for_each_entry_safe(clp, next, &reaplist, cl_lru) |
| 6181 | expire_client(clp); |
| 6182 | |
| 6183 | return count; |
| 6184 | } |
| 6185 | |
| 6186 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, |
| 6187 | const char *type) |
| 6188 | { |
| 6189 | char buf[INET6_ADDRSTRLEN]; |
| 6190 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
| 6191 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
| 6192 | } |
| 6193 | |
| 6194 | static void |
| 6195 | nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst, |
| 6196 | struct list_head *collect) |
| 6197 | { |
| 6198 | struct nfs4_client *clp = lst->st_stid.sc_client; |
| 6199 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6200 | nfsd_net_id); |
| 6201 | |
| 6202 | if (!collect) |
| 6203 | return; |
| 6204 | |
| 6205 | lockdep_assert_held(&nn->client_lock); |
| 6206 | atomic_inc(&clp->cl_refcount); |
| 6207 | list_add(&lst->st_locks, collect); |
| 6208 | } |
| 6209 | |
| 6210 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, |
| 6211 | struct list_head *collect, |
| 6212 | bool (*func)(struct nfs4_ol_stateid *)) |
| 6213 | { |
| 6214 | struct nfs4_openowner *oop; |
| 6215 | struct nfs4_ol_stateid *stp, *st_next; |
| 6216 | struct nfs4_ol_stateid *lst, *lst_next; |
| 6217 | u64 count = 0; |
| 6218 | |
| 6219 | spin_lock(&clp->cl_lock); |
| 6220 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { |
| 6221 | list_for_each_entry_safe(stp, st_next, |
| 6222 | &oop->oo_owner.so_stateids, st_perstateowner) { |
| 6223 | list_for_each_entry_safe(lst, lst_next, |
| 6224 | &stp->st_locks, st_locks) { |
| 6225 | if (func) { |
| 6226 | if (func(lst)) |
| 6227 | nfsd_inject_add_lock_to_list(lst, |
| 6228 | collect); |
| 6229 | } |
| 6230 | ++count; |
| 6231 | /* |
| 6232 | * Despite the fact that these functions deal |
| 6233 | * with 64-bit integers for "count", we must |
| 6234 | * ensure that it doesn't blow up the |
| 6235 | * clp->cl_refcount. Throw a warning if we |
| 6236 | * start to approach INT_MAX here. |
| 6237 | */ |
| 6238 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 6239 | if (count == max) |
| 6240 | goto out; |
| 6241 | } |
| 6242 | } |
| 6243 | } |
| 6244 | out: |
| 6245 | spin_unlock(&clp->cl_lock); |
| 6246 | |
| 6247 | return count; |
| 6248 | } |
| 6249 | |
| 6250 | static u64 |
| 6251 | nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect, |
| 6252 | u64 max) |
| 6253 | { |
| 6254 | return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid); |
| 6255 | } |
| 6256 | |
| 6257 | static u64 |
| 6258 | nfsd_print_client_locks(struct nfs4_client *clp) |
| 6259 | { |
| 6260 | u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL); |
| 6261 | nfsd_print_count(clp, count, "locked files"); |
| 6262 | return count; |
| 6263 | } |
| 6264 | |
| 6265 | u64 |
| 6266 | nfsd_inject_print_locks(void) |
| 6267 | { |
| 6268 | struct nfs4_client *clp; |
| 6269 | u64 count = 0; |
| 6270 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6271 | nfsd_net_id); |
| 6272 | |
| 6273 | if (!nfsd_netns_ready(nn)) |
| 6274 | return 0; |
| 6275 | |
| 6276 | spin_lock(&nn->client_lock); |
| 6277 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 6278 | count += nfsd_print_client_locks(clp); |
| 6279 | spin_unlock(&nn->client_lock); |
| 6280 | |
| 6281 | return count; |
| 6282 | } |
| 6283 | |
| 6284 | static void |
| 6285 | nfsd_reap_locks(struct list_head *reaplist) |
| 6286 | { |
| 6287 | struct nfs4_client *clp; |
| 6288 | struct nfs4_ol_stateid *stp, *next; |
| 6289 | |
| 6290 | list_for_each_entry_safe(stp, next, reaplist, st_locks) { |
| 6291 | list_del_init(&stp->st_locks); |
| 6292 | clp = stp->st_stid.sc_client; |
| 6293 | nfs4_put_stid(&stp->st_stid); |
| 6294 | put_client(clp); |
| 6295 | } |
| 6296 | } |
| 6297 | |
| 6298 | u64 |
| 6299 | nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size) |
| 6300 | { |
| 6301 | unsigned int count = 0; |
| 6302 | struct nfs4_client *clp; |
| 6303 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6304 | nfsd_net_id); |
| 6305 | LIST_HEAD(reaplist); |
| 6306 | |
| 6307 | if (!nfsd_netns_ready(nn)) |
| 6308 | return count; |
| 6309 | |
| 6310 | spin_lock(&nn->client_lock); |
| 6311 | clp = nfsd_find_client(addr, addr_size); |
| 6312 | if (clp) |
| 6313 | count = nfsd_collect_client_locks(clp, &reaplist, 0); |
| 6314 | spin_unlock(&nn->client_lock); |
| 6315 | nfsd_reap_locks(&reaplist); |
| 6316 | return count; |
| 6317 | } |
| 6318 | |
| 6319 | u64 |
| 6320 | nfsd_inject_forget_locks(u64 max) |
| 6321 | { |
| 6322 | u64 count = 0; |
| 6323 | struct nfs4_client *clp; |
| 6324 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6325 | nfsd_net_id); |
| 6326 | LIST_HEAD(reaplist); |
| 6327 | |
| 6328 | if (!nfsd_netns_ready(nn)) |
| 6329 | return count; |
| 6330 | |
| 6331 | spin_lock(&nn->client_lock); |
| 6332 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6333 | count += nfsd_collect_client_locks(clp, &reaplist, max - count); |
| 6334 | if (max != 0 && count >= max) |
| 6335 | break; |
| 6336 | } |
| 6337 | spin_unlock(&nn->client_lock); |
| 6338 | nfsd_reap_locks(&reaplist); |
| 6339 | return count; |
| 6340 | } |
| 6341 | |
| 6342 | static u64 |
| 6343 | nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max, |
| 6344 | struct list_head *collect, |
| 6345 | void (*func)(struct nfs4_openowner *)) |
| 6346 | { |
| 6347 | struct nfs4_openowner *oop, *next; |
| 6348 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6349 | nfsd_net_id); |
| 6350 | u64 count = 0; |
| 6351 | |
| 6352 | lockdep_assert_held(&nn->client_lock); |
| 6353 | |
| 6354 | spin_lock(&clp->cl_lock); |
| 6355 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { |
| 6356 | if (func) { |
| 6357 | func(oop); |
| 6358 | if (collect) { |
| 6359 | atomic_inc(&clp->cl_refcount); |
| 6360 | list_add(&oop->oo_perclient, collect); |
| 6361 | } |
| 6362 | } |
| 6363 | ++count; |
| 6364 | /* |
| 6365 | * Despite the fact that these functions deal with |
| 6366 | * 64-bit integers for "count", we must ensure that |
| 6367 | * it doesn't blow up the clp->cl_refcount. Throw a |
| 6368 | * warning if we start to approach INT_MAX here. |
| 6369 | */ |
| 6370 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 6371 | if (count == max) |
| 6372 | break; |
| 6373 | } |
| 6374 | spin_unlock(&clp->cl_lock); |
| 6375 | |
| 6376 | return count; |
| 6377 | } |
| 6378 | |
| 6379 | static u64 |
| 6380 | nfsd_print_client_openowners(struct nfs4_client *clp) |
| 6381 | { |
| 6382 | u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL); |
| 6383 | |
| 6384 | nfsd_print_count(clp, count, "openowners"); |
| 6385 | return count; |
| 6386 | } |
| 6387 | |
| 6388 | static u64 |
| 6389 | nfsd_collect_client_openowners(struct nfs4_client *clp, |
| 6390 | struct list_head *collect, u64 max) |
| 6391 | { |
| 6392 | return nfsd_foreach_client_openowner(clp, max, collect, |
| 6393 | unhash_openowner_locked); |
| 6394 | } |
| 6395 | |
| 6396 | u64 |
| 6397 | nfsd_inject_print_openowners(void) |
| 6398 | { |
| 6399 | struct nfs4_client *clp; |
| 6400 | u64 count = 0; |
| 6401 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6402 | nfsd_net_id); |
| 6403 | |
| 6404 | if (!nfsd_netns_ready(nn)) |
| 6405 | return 0; |
| 6406 | |
| 6407 | spin_lock(&nn->client_lock); |
| 6408 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 6409 | count += nfsd_print_client_openowners(clp); |
| 6410 | spin_unlock(&nn->client_lock); |
| 6411 | |
| 6412 | return count; |
| 6413 | } |
| 6414 | |
| 6415 | static void |
| 6416 | nfsd_reap_openowners(struct list_head *reaplist) |
| 6417 | { |
| 6418 | struct nfs4_client *clp; |
| 6419 | struct nfs4_openowner *oop, *next; |
| 6420 | |
| 6421 | list_for_each_entry_safe(oop, next, reaplist, oo_perclient) { |
| 6422 | list_del_init(&oop->oo_perclient); |
| 6423 | clp = oop->oo_owner.so_client; |
| 6424 | release_openowner(oop); |
| 6425 | put_client(clp); |
| 6426 | } |
| 6427 | } |
| 6428 | |
| 6429 | u64 |
| 6430 | nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr, |
| 6431 | size_t addr_size) |
| 6432 | { |
| 6433 | unsigned int count = 0; |
| 6434 | struct nfs4_client *clp; |
| 6435 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6436 | nfsd_net_id); |
| 6437 | LIST_HEAD(reaplist); |
| 6438 | |
| 6439 | if (!nfsd_netns_ready(nn)) |
| 6440 | return count; |
| 6441 | |
| 6442 | spin_lock(&nn->client_lock); |
| 6443 | clp = nfsd_find_client(addr, addr_size); |
| 6444 | if (clp) |
| 6445 | count = nfsd_collect_client_openowners(clp, &reaplist, 0); |
| 6446 | spin_unlock(&nn->client_lock); |
| 6447 | nfsd_reap_openowners(&reaplist); |
| 6448 | return count; |
| 6449 | } |
| 6450 | |
| 6451 | u64 |
| 6452 | nfsd_inject_forget_openowners(u64 max) |
| 6453 | { |
| 6454 | u64 count = 0; |
| 6455 | struct nfs4_client *clp; |
| 6456 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6457 | nfsd_net_id); |
| 6458 | LIST_HEAD(reaplist); |
| 6459 | |
| 6460 | if (!nfsd_netns_ready(nn)) |
| 6461 | return count; |
| 6462 | |
| 6463 | spin_lock(&nn->client_lock); |
| 6464 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6465 | count += nfsd_collect_client_openowners(clp, &reaplist, |
| 6466 | max - count); |
| 6467 | if (max != 0 && count >= max) |
| 6468 | break; |
| 6469 | } |
| 6470 | spin_unlock(&nn->client_lock); |
| 6471 | nfsd_reap_openowners(&reaplist); |
| 6472 | return count; |
| 6473 | } |
| 6474 | |
| 6475 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
| 6476 | struct list_head *victims) |
| 6477 | { |
| 6478 | struct nfs4_delegation *dp, *next; |
| 6479 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6480 | nfsd_net_id); |
| 6481 | u64 count = 0; |
| 6482 | |
| 6483 | lockdep_assert_held(&nn->client_lock); |
| 6484 | |
| 6485 | spin_lock(&state_lock); |
| 6486 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
| 6487 | if (victims) { |
| 6488 | /* |
| 6489 | * It's not safe to mess with delegations that have a |
| 6490 | * non-zero dl_time. They might have already been broken |
| 6491 | * and could be processed by the laundromat outside of |
| 6492 | * the state_lock. Just leave them be. |
| 6493 | */ |
| 6494 | if (dp->dl_time != 0) |
| 6495 | continue; |
| 6496 | |
| 6497 | atomic_inc(&clp->cl_refcount); |
| 6498 | WARN_ON(!unhash_delegation_locked(dp)); |
| 6499 | list_add(&dp->dl_recall_lru, victims); |
| 6500 | } |
| 6501 | ++count; |
| 6502 | /* |
| 6503 | * Despite the fact that these functions deal with |
| 6504 | * 64-bit integers for "count", we must ensure that |
| 6505 | * it doesn't blow up the clp->cl_refcount. Throw a |
| 6506 | * warning if we start to approach INT_MAX here. |
| 6507 | */ |
| 6508 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 6509 | if (count == max) |
| 6510 | break; |
| 6511 | } |
| 6512 | spin_unlock(&state_lock); |
| 6513 | return count; |
| 6514 | } |
| 6515 | |
| 6516 | static u64 |
| 6517 | nfsd_print_client_delegations(struct nfs4_client *clp) |
| 6518 | { |
| 6519 | u64 count = nfsd_find_all_delegations(clp, 0, NULL); |
| 6520 | |
| 6521 | nfsd_print_count(clp, count, "delegations"); |
| 6522 | return count; |
| 6523 | } |
| 6524 | |
| 6525 | u64 |
| 6526 | nfsd_inject_print_delegations(void) |
| 6527 | { |
| 6528 | struct nfs4_client *clp; |
| 6529 | u64 count = 0; |
| 6530 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6531 | nfsd_net_id); |
| 6532 | |
| 6533 | if (!nfsd_netns_ready(nn)) |
| 6534 | return 0; |
| 6535 | |
| 6536 | spin_lock(&nn->client_lock); |
| 6537 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 6538 | count += nfsd_print_client_delegations(clp); |
| 6539 | spin_unlock(&nn->client_lock); |
| 6540 | |
| 6541 | return count; |
| 6542 | } |
| 6543 | |
| 6544 | static void |
| 6545 | nfsd_forget_delegations(struct list_head *reaplist) |
| 6546 | { |
| 6547 | struct nfs4_client *clp; |
| 6548 | struct nfs4_delegation *dp, *next; |
| 6549 | |
| 6550 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 6551 | list_del_init(&dp->dl_recall_lru); |
| 6552 | clp = dp->dl_stid.sc_client; |
| 6553 | revoke_delegation(dp); |
| 6554 | put_client(clp); |
| 6555 | } |
| 6556 | } |
| 6557 | |
| 6558 | u64 |
| 6559 | nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr, |
| 6560 | size_t addr_size) |
| 6561 | { |
| 6562 | u64 count = 0; |
| 6563 | struct nfs4_client *clp; |
| 6564 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6565 | nfsd_net_id); |
| 6566 | LIST_HEAD(reaplist); |
| 6567 | |
| 6568 | if (!nfsd_netns_ready(nn)) |
| 6569 | return count; |
| 6570 | |
| 6571 | spin_lock(&nn->client_lock); |
| 6572 | clp = nfsd_find_client(addr, addr_size); |
| 6573 | if (clp) |
| 6574 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 6575 | spin_unlock(&nn->client_lock); |
| 6576 | |
| 6577 | nfsd_forget_delegations(&reaplist); |
| 6578 | return count; |
| 6579 | } |
| 6580 | |
| 6581 | u64 |
| 6582 | nfsd_inject_forget_delegations(u64 max) |
| 6583 | { |
| 6584 | u64 count = 0; |
| 6585 | struct nfs4_client *clp; |
| 6586 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6587 | nfsd_net_id); |
| 6588 | LIST_HEAD(reaplist); |
| 6589 | |
| 6590 | if (!nfsd_netns_ready(nn)) |
| 6591 | return count; |
| 6592 | |
| 6593 | spin_lock(&nn->client_lock); |
| 6594 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 6595 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 6596 | if (max != 0 && count >= max) |
| 6597 | break; |
| 6598 | } |
| 6599 | spin_unlock(&nn->client_lock); |
| 6600 | nfsd_forget_delegations(&reaplist); |
| 6601 | return count; |
| 6602 | } |
| 6603 | |
| 6604 | static void |
| 6605 | nfsd_recall_delegations(struct list_head *reaplist) |
| 6606 | { |
| 6607 | struct nfs4_client *clp; |
| 6608 | struct nfs4_delegation *dp, *next; |
| 6609 | |
| 6610 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 6611 | list_del_init(&dp->dl_recall_lru); |
| 6612 | clp = dp->dl_stid.sc_client; |
| 6613 | /* |
| 6614 | * We skipped all entries that had a zero dl_time before, |
| 6615 | * so we can now reset the dl_time back to 0. If a delegation |
| 6616 | * break comes in now, then it won't make any difference since |
| 6617 | * we're recalling it either way. |
| 6618 | */ |
| 6619 | spin_lock(&state_lock); |
| 6620 | dp->dl_time = 0; |
| 6621 | spin_unlock(&state_lock); |
| 6622 | nfsd_break_one_deleg(dp); |
| 6623 | put_client(clp); |
| 6624 | } |
| 6625 | } |
| 6626 | |
| 6627 | u64 |
| 6628 | nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr, |
| 6629 | size_t addr_size) |
| 6630 | { |
| 6631 | u64 count = 0; |
| 6632 | struct nfs4_client *clp; |
| 6633 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6634 | nfsd_net_id); |
| 6635 | LIST_HEAD(reaplist); |
| 6636 | |
| 6637 | if (!nfsd_netns_ready(nn)) |
| 6638 | return count; |
| 6639 | |
| 6640 | spin_lock(&nn->client_lock); |
| 6641 | clp = nfsd_find_client(addr, addr_size); |
| 6642 | if (clp) |
| 6643 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 6644 | spin_unlock(&nn->client_lock); |
| 6645 | |
| 6646 | nfsd_recall_delegations(&reaplist); |
| 6647 | return count; |
| 6648 | } |
| 6649 | |
| 6650 | u64 |
| 6651 | nfsd_inject_recall_delegations(u64 max) |
| 6652 | { |
| 6653 | u64 count = 0; |
| 6654 | struct nfs4_client *clp, *next; |
| 6655 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 6656 | nfsd_net_id); |
| 6657 | LIST_HEAD(reaplist); |
| 6658 | |
| 6659 | if (!nfsd_netns_ready(nn)) |
| 6660 | return count; |
| 6661 | |
| 6662 | spin_lock(&nn->client_lock); |
| 6663 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 6664 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 6665 | if (max != 0 && ++count >= max) |
| 6666 | break; |
| 6667 | } |
| 6668 | spin_unlock(&nn->client_lock); |
| 6669 | nfsd_recall_delegations(&reaplist); |
| 6670 | return count; |
| 6671 | } |
| 6672 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
| 6673 | |
| 6674 | /* |
| 6675 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 6676 | * client may quite reasonably hang on to a delegation as long as it has |
| 6677 | * the inode cached. This becomes an obvious problem the first time a |
| 6678 | * client's inode cache approaches the size of the server's total memory. |
| 6679 | * |
| 6680 | * For now we avoid this problem by imposing a hard limit on the number |
| 6681 | * of delegations, which varies according to the server's memory size. |
| 6682 | */ |
| 6683 | static void |
| 6684 | set_max_delegations(void) |
| 6685 | { |
| 6686 | /* |
| 6687 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 6688 | * estimates suggest that in the worst case (where every delegation |
| 6689 | * is for a different inode), a delegation could take about 1.5K, |
| 6690 | * giving a worst case usage of about 6% of memory. |
| 6691 | */ |
| 6692 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 6693 | } |
| 6694 | |
| 6695 | static int nfs4_state_create_net(struct net *net) |
| 6696 | { |
| 6697 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 6698 | int i; |
| 6699 | |
| 6700 | nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 6701 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 6702 | if (!nn->conf_id_hashtbl) |
| 6703 | goto err; |
| 6704 | nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
| 6705 | CLIENT_HASH_SIZE, GFP_KERNEL); |
| 6706 | if (!nn->unconf_id_hashtbl) |
| 6707 | goto err_unconf_id; |
| 6708 | nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) * |
| 6709 | SESSION_HASH_SIZE, GFP_KERNEL); |
| 6710 | if (!nn->sessionid_hashtbl) |
| 6711 | goto err_sessionid; |
| 6712 | |
| 6713 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 6714 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
| 6715 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
| 6716 | } |
| 6717 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 6718 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
| 6719 | nn->conf_name_tree = RB_ROOT; |
| 6720 | nn->unconf_name_tree = RB_ROOT; |
| 6721 | INIT_LIST_HEAD(&nn->client_lru); |
| 6722 | INIT_LIST_HEAD(&nn->close_lru); |
| 6723 | INIT_LIST_HEAD(&nn->del_recall_lru); |
| 6724 | spin_lock_init(&nn->client_lock); |
| 6725 | |
| 6726 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
| 6727 | get_net(net); |
| 6728 | |
| 6729 | return 0; |
| 6730 | |
| 6731 | err_sessionid: |
| 6732 | kfree(nn->unconf_id_hashtbl); |
| 6733 | err_unconf_id: |
| 6734 | kfree(nn->conf_id_hashtbl); |
| 6735 | err: |
| 6736 | return -ENOMEM; |
| 6737 | } |
| 6738 | |
| 6739 | static void |
| 6740 | nfs4_state_destroy_net(struct net *net) |
| 6741 | { |
| 6742 | int i; |
| 6743 | struct nfs4_client *clp = NULL; |
| 6744 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 6745 | |
| 6746 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 6747 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 6748 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 6749 | destroy_client(clp); |
| 6750 | } |
| 6751 | } |
| 6752 | |
| 6753 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 6754 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 6755 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 6756 | destroy_client(clp); |
| 6757 | } |
| 6758 | } |
| 6759 | |
| 6760 | kfree(nn->sessionid_hashtbl); |
| 6761 | kfree(nn->unconf_id_hashtbl); |
| 6762 | kfree(nn->conf_id_hashtbl); |
| 6763 | put_net(net); |
| 6764 | } |
| 6765 | |
| 6766 | int |
| 6767 | nfs4_state_start_net(struct net *net) |
| 6768 | { |
| 6769 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 6770 | int ret; |
| 6771 | |
| 6772 | ret = nfs4_state_create_net(net); |
| 6773 | if (ret) |
| 6774 | return ret; |
| 6775 | nn->boot_time = get_seconds(); |
| 6776 | nn->grace_ended = false; |
| 6777 | nn->nfsd4_manager.block_opens = true; |
| 6778 | locks_start_grace(net, &nn->nfsd4_manager); |
| 6779 | nfsd4_client_tracking_init(net); |
| 6780 | printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n", |
| 6781 | nn->nfsd4_grace, net); |
| 6782 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
| 6783 | return 0; |
| 6784 | } |
| 6785 | |
| 6786 | /* initialization to perform when the nfsd service is started: */ |
| 6787 | |
| 6788 | int |
| 6789 | nfs4_state_start(void) |
| 6790 | { |
| 6791 | int ret; |
| 6792 | |
| 6793 | ret = set_callback_cred(); |
| 6794 | if (ret) |
| 6795 | return ret; |
| 6796 | |
| 6797 | laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); |
| 6798 | if (laundry_wq == NULL) { |
| 6799 | ret = -ENOMEM; |
| 6800 | goto out_cleanup_cred; |
| 6801 | } |
| 6802 | ret = nfsd4_create_callback_queue(); |
| 6803 | if (ret) |
| 6804 | goto out_free_laundry; |
| 6805 | |
| 6806 | set_max_delegations(); |
| 6807 | return 0; |
| 6808 | |
| 6809 | out_free_laundry: |
| 6810 | destroy_workqueue(laundry_wq); |
| 6811 | out_cleanup_cred: |
| 6812 | cleanup_callback_cred(); |
| 6813 | return ret; |
| 6814 | } |
| 6815 | |
| 6816 | void |
| 6817 | nfs4_state_shutdown_net(struct net *net) |
| 6818 | { |
| 6819 | struct nfs4_delegation *dp = NULL; |
| 6820 | struct list_head *pos, *next, reaplist; |
| 6821 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 6822 | |
| 6823 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 6824 | locks_end_grace(&nn->nfsd4_manager); |
| 6825 | |
| 6826 | INIT_LIST_HEAD(&reaplist); |
| 6827 | spin_lock(&state_lock); |
| 6828 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
| 6829 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 6830 | WARN_ON(!unhash_delegation_locked(dp)); |
| 6831 | list_add(&dp->dl_recall_lru, &reaplist); |
| 6832 | } |
| 6833 | spin_unlock(&state_lock); |
| 6834 | list_for_each_safe(pos, next, &reaplist) { |
| 6835 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 6836 | list_del_init(&dp->dl_recall_lru); |
| 6837 | put_clnt_odstate(dp->dl_clnt_odstate); |
| 6838 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
| 6839 | nfs4_put_stid(&dp->dl_stid); |
| 6840 | } |
| 6841 | |
| 6842 | nfsd4_client_tracking_exit(net); |
| 6843 | nfs4_state_destroy_net(net); |
| 6844 | } |
| 6845 | |
| 6846 | void |
| 6847 | nfs4_state_shutdown(void) |
| 6848 | { |
| 6849 | destroy_workqueue(laundry_wq); |
| 6850 | nfsd4_destroy_callback_queue(); |
| 6851 | cleanup_callback_cred(); |
| 6852 | } |
| 6853 | |
| 6854 | static void |
| 6855 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 6856 | { |
| 6857 | if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid)) |
| 6858 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
| 6859 | } |
| 6860 | |
| 6861 | static void |
| 6862 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 6863 | { |
| 6864 | if (cstate->minorversion) { |
| 6865 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
| 6866 | SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
| 6867 | } |
| 6868 | } |
| 6869 | |
| 6870 | void |
| 6871 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 6872 | { |
| 6873 | CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
| 6874 | } |
| 6875 | |
| 6876 | /* |
| 6877 | * functions to set current state id |
| 6878 | */ |
| 6879 | void |
| 6880 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 6881 | { |
| 6882 | put_stateid(cstate, &odp->od_stateid); |
| 6883 | } |
| 6884 | |
| 6885 | void |
| 6886 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open) |
| 6887 | { |
| 6888 | put_stateid(cstate, &open->op_stateid); |
| 6889 | } |
| 6890 | |
| 6891 | void |
| 6892 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 6893 | { |
| 6894 | put_stateid(cstate, &close->cl_stateid); |
| 6895 | } |
| 6896 | |
| 6897 | void |
| 6898 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock) |
| 6899 | { |
| 6900 | put_stateid(cstate, &lock->lk_resp_stateid); |
| 6901 | } |
| 6902 | |
| 6903 | /* |
| 6904 | * functions to consume current state id |
| 6905 | */ |
| 6906 | |
| 6907 | void |
| 6908 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) |
| 6909 | { |
| 6910 | get_stateid(cstate, &odp->od_stateid); |
| 6911 | } |
| 6912 | |
| 6913 | void |
| 6914 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp) |
| 6915 | { |
| 6916 | get_stateid(cstate, &drp->dr_stateid); |
| 6917 | } |
| 6918 | |
| 6919 | void |
| 6920 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp) |
| 6921 | { |
| 6922 | get_stateid(cstate, &fsp->fr_stateid); |
| 6923 | } |
| 6924 | |
| 6925 | void |
| 6926 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr) |
| 6927 | { |
| 6928 | get_stateid(cstate, &setattr->sa_stateid); |
| 6929 | } |
| 6930 | |
| 6931 | void |
| 6932 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) |
| 6933 | { |
| 6934 | get_stateid(cstate, &close->cl_stateid); |
| 6935 | } |
| 6936 | |
| 6937 | void |
| 6938 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku) |
| 6939 | { |
| 6940 | get_stateid(cstate, &locku->lu_stateid); |
| 6941 | } |
| 6942 | |
| 6943 | void |
| 6944 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read) |
| 6945 | { |
| 6946 | get_stateid(cstate, &read->rd_stateid); |
| 6947 | } |
| 6948 | |
| 6949 | void |
| 6950 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write) |
| 6951 | { |
| 6952 | get_stateid(cstate, &write->wr_stateid); |
| 6953 | } |