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 <linux/string_helpers.h> 46 #include <linux/fsnotify.h> 47 #include <linux/rhashtable.h> 48 #include <linux/nfs_ssc.h> 49 50 #include "xdr4.h" 51 #include "xdr4cb.h" 52 #include "vfs.h" 53 #include "current_stateid.h" 54 55 #include "netns.h" 56 #include "pnfs.h" 57 #include "filecache.h" 58 #include "trace.h" 59 60 #define NFSDDBG_FACILITY NFSDDBG_PROC 61 62 #define all_ones {{ ~0, ~0}, ~0} 63 static const stateid_t one_stateid = { 64 .si_generation = ~0, 65 .si_opaque = all_ones, 66 }; 67 static const stateid_t zero_stateid = { 68 /* all fields zero */ 69 }; 70 static const stateid_t currentstateid = { 71 .si_generation = 1, 72 }; 73 static const stateid_t close_stateid = { 74 .si_generation = 0xffffffffU, 75 }; 76 77 static u64 current_sessionid = 1; 78 79 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) 80 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) 81 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) 82 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t))) 83 84 /* forward declarations */ 85 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner); 86 static void nfs4_free_ol_stateid(struct nfs4_stid *stid); 87 void nfsd4_end_grace(struct nfsd_net *nn); 88 static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps); 89 static void nfsd4_file_hash_remove(struct nfs4_file *fi); 90 static void deleg_reaper(struct nfsd_net *nn); 91 92 /* Locking: */ 93 94 /* 95 * Currently used for the del_recall_lru and file hash table. In an 96 * effort to decrease the scope of the client_mutex, this spinlock may 97 * eventually cover more: 98 */ 99 static DEFINE_SPINLOCK(state_lock); 100 101 enum nfsd4_st_mutex_lock_subclass { 102 OPEN_STATEID_MUTEX = 0, 103 LOCK_STATEID_MUTEX = 1, 104 }; 105 106 /* 107 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for 108 * the refcount on the open stateid to drop. 109 */ 110 static DECLARE_WAIT_QUEUE_HEAD(close_wq); 111 112 /* 113 * A waitqueue where a writer to clients/#/ctl destroying a client can 114 * wait for cl_rpc_users to drop to 0 and then for the client to be 115 * unhashed. 116 */ 117 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq); 118 119 static struct kmem_cache *client_slab; 120 static struct kmem_cache *openowner_slab; 121 static struct kmem_cache *lockowner_slab; 122 static struct kmem_cache *file_slab; 123 static struct kmem_cache *stateid_slab; 124 static struct kmem_cache *deleg_slab; 125 static struct kmem_cache *odstate_slab; 126 127 static void free_session(struct nfsd4_session *); 128 129 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops; 130 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops; 131 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops; 132 133 static struct workqueue_struct *laundry_wq; 134 135 int nfsd4_create_laundry_wq(void) 136 { 137 int rc = 0; 138 139 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); 140 if (laundry_wq == NULL) 141 rc = -ENOMEM; 142 return rc; 143 } 144 145 void nfsd4_destroy_laundry_wq(void) 146 { 147 destroy_workqueue(laundry_wq); 148 } 149 150 static bool is_session_dead(struct nfsd4_session *ses) 151 { 152 return ses->se_flags & NFS4_SESSION_DEAD; 153 } 154 155 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) 156 { 157 if (atomic_read(&ses->se_ref) > ref_held_by_me) 158 return nfserr_jukebox; 159 ses->se_flags |= NFS4_SESSION_DEAD; 160 return nfs_ok; 161 } 162 163 static bool is_client_expired(struct nfs4_client *clp) 164 { 165 return clp->cl_time == 0; 166 } 167 168 static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn, 169 struct nfs4_client *clp) 170 { 171 if (clp->cl_state != NFSD4_ACTIVE) 172 atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0); 173 } 174 175 static __be32 get_client_locked(struct nfs4_client *clp) 176 { 177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 178 179 lockdep_assert_held(&nn->client_lock); 180 181 if (is_client_expired(clp)) 182 return nfserr_expired; 183 atomic_inc(&clp->cl_rpc_users); 184 nfsd4_dec_courtesy_client_count(nn, clp); 185 clp->cl_state = NFSD4_ACTIVE; 186 return nfs_ok; 187 } 188 189 /* must be called under the client_lock */ 190 static inline void 191 renew_client_locked(struct nfs4_client *clp) 192 { 193 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 194 195 if (is_client_expired(clp)) { 196 WARN_ON(1); 197 printk("%s: client (clientid %08x/%08x) already expired\n", 198 __func__, 199 clp->cl_clientid.cl_boot, 200 clp->cl_clientid.cl_id); 201 return; 202 } 203 204 list_move_tail(&clp->cl_lru, &nn->client_lru); 205 clp->cl_time = ktime_get_boottime_seconds(); 206 nfsd4_dec_courtesy_client_count(nn, clp); 207 clp->cl_state = NFSD4_ACTIVE; 208 } 209 210 static void put_client_renew_locked(struct nfs4_client *clp) 211 { 212 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 213 214 lockdep_assert_held(&nn->client_lock); 215 216 if (!atomic_dec_and_test(&clp->cl_rpc_users)) 217 return; 218 if (!is_client_expired(clp)) 219 renew_client_locked(clp); 220 else 221 wake_up_all(&expiry_wq); 222 } 223 224 static void put_client_renew(struct nfs4_client *clp) 225 { 226 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 227 228 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock)) 229 return; 230 if (!is_client_expired(clp)) 231 renew_client_locked(clp); 232 else 233 wake_up_all(&expiry_wq); 234 spin_unlock(&nn->client_lock); 235 } 236 237 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) 238 { 239 __be32 status; 240 241 if (is_session_dead(ses)) 242 return nfserr_badsession; 243 status = get_client_locked(ses->se_client); 244 if (status) 245 return status; 246 atomic_inc(&ses->se_ref); 247 return nfs_ok; 248 } 249 250 static void nfsd4_put_session_locked(struct nfsd4_session *ses) 251 { 252 struct nfs4_client *clp = ses->se_client; 253 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 254 255 lockdep_assert_held(&nn->client_lock); 256 257 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) 258 free_session(ses); 259 put_client_renew_locked(clp); 260 } 261 262 static void nfsd4_put_session(struct nfsd4_session *ses) 263 { 264 struct nfs4_client *clp = ses->se_client; 265 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 266 267 spin_lock(&nn->client_lock); 268 nfsd4_put_session_locked(ses); 269 spin_unlock(&nn->client_lock); 270 } 271 272 static struct nfsd4_blocked_lock * 273 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh, 274 struct nfsd_net *nn) 275 { 276 struct nfsd4_blocked_lock *cur, *found = NULL; 277 278 spin_lock(&nn->blocked_locks_lock); 279 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) { 280 if (fh_match(fh, &cur->nbl_fh)) { 281 list_del_init(&cur->nbl_list); 282 WARN_ON(list_empty(&cur->nbl_lru)); 283 list_del_init(&cur->nbl_lru); 284 found = cur; 285 break; 286 } 287 } 288 spin_unlock(&nn->blocked_locks_lock); 289 if (found) 290 locks_delete_block(&found->nbl_lock); 291 return found; 292 } 293 294 static struct nfsd4_blocked_lock * 295 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh, 296 struct nfsd_net *nn) 297 { 298 struct nfsd4_blocked_lock *nbl; 299 300 nbl = find_blocked_lock(lo, fh, nn); 301 if (!nbl) { 302 nbl = kmalloc(sizeof(*nbl), GFP_KERNEL); 303 if (nbl) { 304 INIT_LIST_HEAD(&nbl->nbl_list); 305 INIT_LIST_HEAD(&nbl->nbl_lru); 306 fh_copy_shallow(&nbl->nbl_fh, fh); 307 locks_init_lock(&nbl->nbl_lock); 308 kref_init(&nbl->nbl_kref); 309 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client, 310 &nfsd4_cb_notify_lock_ops, 311 NFSPROC4_CLNT_CB_NOTIFY_LOCK); 312 } 313 } 314 return nbl; 315 } 316 317 static void 318 free_nbl(struct kref *kref) 319 { 320 struct nfsd4_blocked_lock *nbl; 321 322 nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref); 323 locks_release_private(&nbl->nbl_lock); 324 kfree(nbl); 325 } 326 327 static void 328 free_blocked_lock(struct nfsd4_blocked_lock *nbl) 329 { 330 locks_delete_block(&nbl->nbl_lock); 331 kref_put(&nbl->nbl_kref, free_nbl); 332 } 333 334 static void 335 remove_blocked_locks(struct nfs4_lockowner *lo) 336 { 337 struct nfs4_client *clp = lo->lo_owner.so_client; 338 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 339 struct nfsd4_blocked_lock *nbl; 340 LIST_HEAD(reaplist); 341 342 /* Dequeue all blocked locks */ 343 spin_lock(&nn->blocked_locks_lock); 344 while (!list_empty(&lo->lo_blocked)) { 345 nbl = list_first_entry(&lo->lo_blocked, 346 struct nfsd4_blocked_lock, 347 nbl_list); 348 list_del_init(&nbl->nbl_list); 349 WARN_ON(list_empty(&nbl->nbl_lru)); 350 list_move(&nbl->nbl_lru, &reaplist); 351 } 352 spin_unlock(&nn->blocked_locks_lock); 353 354 /* Now free them */ 355 while (!list_empty(&reaplist)) { 356 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock, 357 nbl_lru); 358 list_del_init(&nbl->nbl_lru); 359 free_blocked_lock(nbl); 360 } 361 } 362 363 static void 364 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb) 365 { 366 struct nfsd4_blocked_lock *nbl = container_of(cb, 367 struct nfsd4_blocked_lock, nbl_cb); 368 locks_delete_block(&nbl->nbl_lock); 369 } 370 371 static int 372 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task) 373 { 374 trace_nfsd_cb_notify_lock_done(&zero_stateid, task); 375 376 /* 377 * Since this is just an optimization, we don't try very hard if it 378 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and 379 * just quit trying on anything else. 380 */ 381 switch (task->tk_status) { 382 case -NFS4ERR_DELAY: 383 rpc_delay(task, 1 * HZ); 384 return 0; 385 default: 386 return 1; 387 } 388 } 389 390 static void 391 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb) 392 { 393 struct nfsd4_blocked_lock *nbl = container_of(cb, 394 struct nfsd4_blocked_lock, nbl_cb); 395 396 free_blocked_lock(nbl); 397 } 398 399 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = { 400 .prepare = nfsd4_cb_notify_lock_prepare, 401 .done = nfsd4_cb_notify_lock_done, 402 .release = nfsd4_cb_notify_lock_release, 403 }; 404 405 /* 406 * We store the NONE, READ, WRITE, and BOTH bits separately in the 407 * st_{access,deny}_bmap field of the stateid, in order to track not 408 * only what share bits are currently in force, but also what 409 * combinations of share bits previous opens have used. This allows us 410 * to enforce the recommendation in 411 * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that 412 * the server return an error if the client attempt to downgrade to a 413 * combination of share bits not explicable by closing some of its 414 * previous opens. 415 * 416 * This enforcement is arguably incomplete, since we don't keep 417 * track of access/deny bit combinations; so, e.g., we allow: 418 * 419 * OPEN allow read, deny write 420 * OPEN allow both, deny none 421 * DOWNGRADE allow read, deny none 422 * 423 * which we should reject. 424 * 425 * But you could also argue that our current code is already overkill, 426 * since it only exists to return NFS4ERR_INVAL on incorrect client 427 * behavior. 428 */ 429 static unsigned int 430 bmap_to_share_mode(unsigned long bmap) 431 { 432 int i; 433 unsigned int access = 0; 434 435 for (i = 1; i < 4; i++) { 436 if (test_bit(i, &bmap)) 437 access |= i; 438 } 439 return access; 440 } 441 442 /* set share access for a given stateid */ 443 static inline void 444 set_access(u32 access, struct nfs4_ol_stateid *stp) 445 { 446 unsigned char mask = 1 << access; 447 448 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); 449 stp->st_access_bmap |= mask; 450 } 451 452 /* clear share access for a given stateid */ 453 static inline void 454 clear_access(u32 access, struct nfs4_ol_stateid *stp) 455 { 456 unsigned char mask = 1 << access; 457 458 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); 459 stp->st_access_bmap &= ~mask; 460 } 461 462 /* test whether a given stateid has access */ 463 static inline bool 464 test_access(u32 access, struct nfs4_ol_stateid *stp) 465 { 466 unsigned char mask = 1 << access; 467 468 return (bool)(stp->st_access_bmap & mask); 469 } 470 471 /* set share deny for a given stateid */ 472 static inline void 473 set_deny(u32 deny, struct nfs4_ol_stateid *stp) 474 { 475 unsigned char mask = 1 << deny; 476 477 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); 478 stp->st_deny_bmap |= mask; 479 } 480 481 /* clear share deny for a given stateid */ 482 static inline void 483 clear_deny(u32 deny, struct nfs4_ol_stateid *stp) 484 { 485 unsigned char mask = 1 << deny; 486 487 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); 488 stp->st_deny_bmap &= ~mask; 489 } 490 491 /* test whether a given stateid is denying specific access */ 492 static inline bool 493 test_deny(u32 deny, struct nfs4_ol_stateid *stp) 494 { 495 unsigned char mask = 1 << deny; 496 497 return (bool)(stp->st_deny_bmap & mask); 498 } 499 500 static int nfs4_access_to_omode(u32 access) 501 { 502 switch (access & NFS4_SHARE_ACCESS_BOTH) { 503 case NFS4_SHARE_ACCESS_READ: 504 return O_RDONLY; 505 case NFS4_SHARE_ACCESS_WRITE: 506 return O_WRONLY; 507 case NFS4_SHARE_ACCESS_BOTH: 508 return O_RDWR; 509 } 510 WARN_ON_ONCE(1); 511 return O_RDONLY; 512 } 513 514 static inline int 515 access_permit_read(struct nfs4_ol_stateid *stp) 516 { 517 return test_access(NFS4_SHARE_ACCESS_READ, stp) || 518 test_access(NFS4_SHARE_ACCESS_BOTH, stp) || 519 test_access(NFS4_SHARE_ACCESS_WRITE, stp); 520 } 521 522 static inline int 523 access_permit_write(struct nfs4_ol_stateid *stp) 524 { 525 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || 526 test_access(NFS4_SHARE_ACCESS_BOTH, stp); 527 } 528 529 static inline struct nfs4_stateowner * 530 nfs4_get_stateowner(struct nfs4_stateowner *sop) 531 { 532 atomic_inc(&sop->so_count); 533 return sop; 534 } 535 536 static int 537 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) 538 { 539 return (sop->so_owner.len == owner->len) && 540 0 == memcmp(sop->so_owner.data, owner->data, owner->len); 541 } 542 543 static struct nfs4_openowner * 544 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, 545 struct nfs4_client *clp) 546 { 547 struct nfs4_stateowner *so; 548 549 lockdep_assert_held(&clp->cl_lock); 550 551 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], 552 so_strhash) { 553 if (!so->so_is_open_owner) 554 continue; 555 if (same_owner_str(so, &open->op_owner)) 556 return openowner(nfs4_get_stateowner(so)); 557 } 558 return NULL; 559 } 560 561 static inline u32 562 opaque_hashval(const void *ptr, int nbytes) 563 { 564 unsigned char *cptr = (unsigned char *) ptr; 565 566 u32 x = 0; 567 while (nbytes--) { 568 x *= 37; 569 x += *cptr++; 570 } 571 return x; 572 } 573 574 static void nfsd4_free_file_rcu(struct rcu_head *rcu) 575 { 576 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); 577 578 kmem_cache_free(file_slab, fp); 579 } 580 581 void 582 put_nfs4_file(struct nfs4_file *fi) 583 { 584 if (refcount_dec_and_test(&fi->fi_ref)) { 585 nfsd4_file_hash_remove(fi); 586 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); 587 WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); 588 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu); 589 } 590 } 591 592 static struct nfsd_file * 593 find_writeable_file_locked(struct nfs4_file *f) 594 { 595 struct nfsd_file *ret; 596 597 lockdep_assert_held(&f->fi_lock); 598 599 ret = nfsd_file_get(f->fi_fds[O_WRONLY]); 600 if (!ret) 601 ret = nfsd_file_get(f->fi_fds[O_RDWR]); 602 return ret; 603 } 604 605 static struct nfsd_file * 606 find_writeable_file(struct nfs4_file *f) 607 { 608 struct nfsd_file *ret; 609 610 spin_lock(&f->fi_lock); 611 ret = find_writeable_file_locked(f); 612 spin_unlock(&f->fi_lock); 613 614 return ret; 615 } 616 617 static struct nfsd_file * 618 find_readable_file_locked(struct nfs4_file *f) 619 { 620 struct nfsd_file *ret; 621 622 lockdep_assert_held(&f->fi_lock); 623 624 ret = nfsd_file_get(f->fi_fds[O_RDONLY]); 625 if (!ret) 626 ret = nfsd_file_get(f->fi_fds[O_RDWR]); 627 return ret; 628 } 629 630 static struct nfsd_file * 631 find_readable_file(struct nfs4_file *f) 632 { 633 struct nfsd_file *ret; 634 635 spin_lock(&f->fi_lock); 636 ret = find_readable_file_locked(f); 637 spin_unlock(&f->fi_lock); 638 639 return ret; 640 } 641 642 static struct nfsd_file * 643 find_rw_file(struct nfs4_file *f) 644 { 645 struct nfsd_file *ret; 646 647 spin_lock(&f->fi_lock); 648 ret = nfsd_file_get(f->fi_fds[O_RDWR]); 649 spin_unlock(&f->fi_lock); 650 651 return ret; 652 } 653 654 struct nfsd_file * 655 find_any_file(struct nfs4_file *f) 656 { 657 struct nfsd_file *ret; 658 659 if (!f) 660 return NULL; 661 spin_lock(&f->fi_lock); 662 ret = nfsd_file_get(f->fi_fds[O_RDWR]); 663 if (!ret) { 664 ret = nfsd_file_get(f->fi_fds[O_WRONLY]); 665 if (!ret) 666 ret = nfsd_file_get(f->fi_fds[O_RDONLY]); 667 } 668 spin_unlock(&f->fi_lock); 669 return ret; 670 } 671 672 static struct nfsd_file *find_any_file_locked(struct nfs4_file *f) 673 { 674 lockdep_assert_held(&f->fi_lock); 675 676 if (f->fi_fds[O_RDWR]) 677 return f->fi_fds[O_RDWR]; 678 if (f->fi_fds[O_WRONLY]) 679 return f->fi_fds[O_WRONLY]; 680 if (f->fi_fds[O_RDONLY]) 681 return f->fi_fds[O_RDONLY]; 682 return NULL; 683 } 684 685 static atomic_long_t num_delegations; 686 unsigned long max_delegations; 687 688 /* 689 * Open owner state (share locks) 690 */ 691 692 /* hash tables for lock and open owners */ 693 #define OWNER_HASH_BITS 8 694 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) 695 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) 696 697 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) 698 { 699 unsigned int ret; 700 701 ret = opaque_hashval(ownername->data, ownername->len); 702 return ret & OWNER_HASH_MASK; 703 } 704 705 static struct rhltable nfs4_file_rhltable ____cacheline_aligned_in_smp; 706 707 static const struct rhashtable_params nfs4_file_rhash_params = { 708 .key_len = sizeof_field(struct nfs4_file, fi_inode), 709 .key_offset = offsetof(struct nfs4_file, fi_inode), 710 .head_offset = offsetof(struct nfs4_file, fi_rlist), 711 712 /* 713 * Start with a single page hash table to reduce resizing churn 714 * on light workloads. 715 */ 716 .min_size = 256, 717 .automatic_shrinking = true, 718 }; 719 720 /* 721 * Check if courtesy clients have conflicting access and resolve it if possible 722 * 723 * access: is op_share_access if share_access is true. 724 * Check if access mode, op_share_access, would conflict with 725 * the current deny mode of the file 'fp'. 726 * access: is op_share_deny if share_access is false. 727 * Check if the deny mode, op_share_deny, would conflict with 728 * current access of the file 'fp'. 729 * stp: skip checking this entry. 730 * new_stp: normal open, not open upgrade. 731 * 732 * Function returns: 733 * false - access/deny mode conflict with normal client. 734 * true - no conflict or conflict with courtesy client(s) is resolved. 735 */ 736 static bool 737 nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp, 738 struct nfs4_ol_stateid *stp, u32 access, bool share_access) 739 { 740 struct nfs4_ol_stateid *st; 741 bool resolvable = true; 742 unsigned char bmap; 743 struct nfsd_net *nn; 744 struct nfs4_client *clp; 745 746 lockdep_assert_held(&fp->fi_lock); 747 list_for_each_entry(st, &fp->fi_stateids, st_perfile) { 748 /* ignore lock stateid */ 749 if (st->st_openstp) 750 continue; 751 if (st == stp && new_stp) 752 continue; 753 /* check file access against deny mode or vice versa */ 754 bmap = share_access ? st->st_deny_bmap : st->st_access_bmap; 755 if (!(access & bmap_to_share_mode(bmap))) 756 continue; 757 clp = st->st_stid.sc_client; 758 if (try_to_expire_client(clp)) 759 continue; 760 resolvable = false; 761 break; 762 } 763 if (resolvable) { 764 clp = stp->st_stid.sc_client; 765 nn = net_generic(clp->net, nfsd_net_id); 766 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); 767 } 768 return resolvable; 769 } 770 771 static void 772 __nfs4_file_get_access(struct nfs4_file *fp, u32 access) 773 { 774 lockdep_assert_held(&fp->fi_lock); 775 776 if (access & NFS4_SHARE_ACCESS_WRITE) 777 atomic_inc(&fp->fi_access[O_WRONLY]); 778 if (access & NFS4_SHARE_ACCESS_READ) 779 atomic_inc(&fp->fi_access[O_RDONLY]); 780 } 781 782 static __be32 783 nfs4_file_get_access(struct nfs4_file *fp, u32 access) 784 { 785 lockdep_assert_held(&fp->fi_lock); 786 787 /* Does this access mode make sense? */ 788 if (access & ~NFS4_SHARE_ACCESS_BOTH) 789 return nfserr_inval; 790 791 /* Does it conflict with a deny mode already set? */ 792 if ((access & fp->fi_share_deny) != 0) 793 return nfserr_share_denied; 794 795 __nfs4_file_get_access(fp, access); 796 return nfs_ok; 797 } 798 799 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) 800 { 801 /* Common case is that there is no deny mode. */ 802 if (deny) { 803 /* Does this deny mode make sense? */ 804 if (deny & ~NFS4_SHARE_DENY_BOTH) 805 return nfserr_inval; 806 807 if ((deny & NFS4_SHARE_DENY_READ) && 808 atomic_read(&fp->fi_access[O_RDONLY])) 809 return nfserr_share_denied; 810 811 if ((deny & NFS4_SHARE_DENY_WRITE) && 812 atomic_read(&fp->fi_access[O_WRONLY])) 813 return nfserr_share_denied; 814 } 815 return nfs_ok; 816 } 817 818 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag) 819 { 820 might_lock(&fp->fi_lock); 821 822 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { 823 struct nfsd_file *f1 = NULL; 824 struct nfsd_file *f2 = NULL; 825 826 swap(f1, fp->fi_fds[oflag]); 827 if (atomic_read(&fp->fi_access[1 - oflag]) == 0) 828 swap(f2, fp->fi_fds[O_RDWR]); 829 spin_unlock(&fp->fi_lock); 830 if (f1) 831 nfsd_file_put(f1); 832 if (f2) 833 nfsd_file_put(f2); 834 } 835 } 836 837 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access) 838 { 839 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); 840 841 if (access & NFS4_SHARE_ACCESS_WRITE) 842 __nfs4_file_put_access(fp, O_WRONLY); 843 if (access & NFS4_SHARE_ACCESS_READ) 844 __nfs4_file_put_access(fp, O_RDONLY); 845 } 846 847 /* 848 * Allocate a new open/delegation state counter. This is needed for 849 * pNFS for proper return on close semantics. 850 * 851 * Note that we only allocate it for pNFS-enabled exports, otherwise 852 * all pointers to struct nfs4_clnt_odstate are always NULL. 853 */ 854 static struct nfs4_clnt_odstate * 855 alloc_clnt_odstate(struct nfs4_client *clp) 856 { 857 struct nfs4_clnt_odstate *co; 858 859 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); 860 if (co) { 861 co->co_client = clp; 862 refcount_set(&co->co_odcount, 1); 863 } 864 return co; 865 } 866 867 static void 868 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) 869 { 870 struct nfs4_file *fp = co->co_file; 871 872 lockdep_assert_held(&fp->fi_lock); 873 list_add(&co->co_perfile, &fp->fi_clnt_odstate); 874 } 875 876 static inline void 877 get_clnt_odstate(struct nfs4_clnt_odstate *co) 878 { 879 if (co) 880 refcount_inc(&co->co_odcount); 881 } 882 883 static void 884 put_clnt_odstate(struct nfs4_clnt_odstate *co) 885 { 886 struct nfs4_file *fp; 887 888 if (!co) 889 return; 890 891 fp = co->co_file; 892 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { 893 list_del(&co->co_perfile); 894 spin_unlock(&fp->fi_lock); 895 896 nfsd4_return_all_file_layouts(co->co_client, fp); 897 kmem_cache_free(odstate_slab, co); 898 } 899 } 900 901 static struct nfs4_clnt_odstate * 902 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) 903 { 904 struct nfs4_clnt_odstate *co; 905 struct nfs4_client *cl; 906 907 if (!new) 908 return NULL; 909 910 cl = new->co_client; 911 912 spin_lock(&fp->fi_lock); 913 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { 914 if (co->co_client == cl) { 915 get_clnt_odstate(co); 916 goto out; 917 } 918 } 919 co = new; 920 co->co_file = fp; 921 hash_clnt_odstate_locked(new); 922 out: 923 spin_unlock(&fp->fi_lock); 924 return co; 925 } 926 927 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab, 928 void (*sc_free)(struct nfs4_stid *)) 929 { 930 struct nfs4_stid *stid; 931 int new_id; 932 933 stid = kmem_cache_zalloc(slab, GFP_KERNEL); 934 if (!stid) 935 return NULL; 936 937 idr_preload(GFP_KERNEL); 938 spin_lock(&cl->cl_lock); 939 /* Reserving 0 for start of file in nfsdfs "states" file: */ 940 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT); 941 spin_unlock(&cl->cl_lock); 942 idr_preload_end(); 943 if (new_id < 0) 944 goto out_free; 945 946 stid->sc_free = sc_free; 947 stid->sc_client = cl; 948 stid->sc_stateid.si_opaque.so_id = new_id; 949 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; 950 /* Will be incremented before return to client: */ 951 refcount_set(&stid->sc_count, 1); 952 spin_lock_init(&stid->sc_lock); 953 INIT_LIST_HEAD(&stid->sc_cp_list); 954 955 /* 956 * It shouldn't be a problem to reuse an opaque stateid value. 957 * I don't think it is for 4.1. But with 4.0 I worry that, for 958 * example, a stray write retransmission could be accepted by 959 * the server when it should have been rejected. Therefore, 960 * adopt a trick from the sctp code to attempt to maximize the 961 * amount of time until an id is reused, by ensuring they always 962 * "increase" (mod INT_MAX): 963 */ 964 return stid; 965 out_free: 966 kmem_cache_free(slab, stid); 967 return NULL; 968 } 969 970 /* 971 * Create a unique stateid_t to represent each COPY. 972 */ 973 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid, 974 unsigned char cs_type) 975 { 976 int new_id; 977 978 stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time; 979 stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id; 980 981 idr_preload(GFP_KERNEL); 982 spin_lock(&nn->s2s_cp_lock); 983 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT); 984 stid->cs_stid.si_opaque.so_id = new_id; 985 stid->cs_stid.si_generation = 1; 986 spin_unlock(&nn->s2s_cp_lock); 987 idr_preload_end(); 988 if (new_id < 0) 989 return 0; 990 stid->cs_type = cs_type; 991 return 1; 992 } 993 994 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy) 995 { 996 return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID); 997 } 998 999 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn, 1000 struct nfs4_stid *p_stid) 1001 { 1002 struct nfs4_cpntf_state *cps; 1003 1004 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL); 1005 if (!cps) 1006 return NULL; 1007 cps->cpntf_time = ktime_get_boottime_seconds(); 1008 refcount_set(&cps->cp_stateid.cs_count, 1); 1009 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID)) 1010 goto out_free; 1011 spin_lock(&nn->s2s_cp_lock); 1012 list_add(&cps->cp_list, &p_stid->sc_cp_list); 1013 spin_unlock(&nn->s2s_cp_lock); 1014 return cps; 1015 out_free: 1016 kfree(cps); 1017 return NULL; 1018 } 1019 1020 void nfs4_free_copy_state(struct nfsd4_copy *copy) 1021 { 1022 struct nfsd_net *nn; 1023 1024 if (copy->cp_stateid.cs_type != NFS4_COPY_STID) 1025 return; 1026 nn = net_generic(copy->cp_clp->net, nfsd_net_id); 1027 spin_lock(&nn->s2s_cp_lock); 1028 idr_remove(&nn->s2s_cp_stateids, 1029 copy->cp_stateid.cs_stid.si_opaque.so_id); 1030 spin_unlock(&nn->s2s_cp_lock); 1031 } 1032 1033 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid) 1034 { 1035 struct nfs4_cpntf_state *cps; 1036 struct nfsd_net *nn; 1037 1038 nn = net_generic(net, nfsd_net_id); 1039 spin_lock(&nn->s2s_cp_lock); 1040 while (!list_empty(&stid->sc_cp_list)) { 1041 cps = list_first_entry(&stid->sc_cp_list, 1042 struct nfs4_cpntf_state, cp_list); 1043 _free_cpntf_state_locked(nn, cps); 1044 } 1045 spin_unlock(&nn->s2s_cp_lock); 1046 } 1047 1048 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp) 1049 { 1050 struct nfs4_stid *stid; 1051 1052 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid); 1053 if (!stid) 1054 return NULL; 1055 1056 return openlockstateid(stid); 1057 } 1058 1059 static void nfs4_free_deleg(struct nfs4_stid *stid) 1060 { 1061 struct nfs4_delegation *dp = delegstateid(stid); 1062 1063 WARN_ON_ONCE(!list_empty(&stid->sc_cp_list)); 1064 WARN_ON_ONCE(!list_empty(&dp->dl_perfile)); 1065 WARN_ON_ONCE(!list_empty(&dp->dl_perclnt)); 1066 WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru)); 1067 kmem_cache_free(deleg_slab, stid); 1068 atomic_long_dec(&num_delegations); 1069 } 1070 1071 /* 1072 * When we recall a delegation, we should be careful not to hand it 1073 * out again straight away. 1074 * To ensure this we keep a pair of bloom filters ('new' and 'old') 1075 * in which the filehandles of recalled delegations are "stored". 1076 * If a filehandle appear in either filter, a delegation is blocked. 1077 * When a delegation is recalled, the filehandle is stored in the "new" 1078 * filter. 1079 * Every 30 seconds we swap the filters and clear the "new" one, 1080 * unless both are empty of course. 1081 * 1082 * Each filter is 256 bits. We hash the filehandle to 32bit and use the 1083 * low 3 bytes as hash-table indices. 1084 * 1085 * 'blocked_delegations_lock', which is always taken in block_delegations(), 1086 * is used to manage concurrent access. Testing does not need the lock 1087 * except when swapping the two filters. 1088 */ 1089 static DEFINE_SPINLOCK(blocked_delegations_lock); 1090 static struct bloom_pair { 1091 int entries, old_entries; 1092 time64_t swap_time; 1093 int new; /* index into 'set' */ 1094 DECLARE_BITMAP(set[2], 256); 1095 } blocked_delegations; 1096 1097 static int delegation_blocked(struct knfsd_fh *fh) 1098 { 1099 u32 hash; 1100 struct bloom_pair *bd = &blocked_delegations; 1101 1102 if (bd->entries == 0) 1103 return 0; 1104 if (ktime_get_seconds() - bd->swap_time > 30) { 1105 spin_lock(&blocked_delegations_lock); 1106 if (ktime_get_seconds() - bd->swap_time > 30) { 1107 bd->entries -= bd->old_entries; 1108 bd->old_entries = bd->entries; 1109 memset(bd->set[bd->new], 0, 1110 sizeof(bd->set[0])); 1111 bd->new = 1-bd->new; 1112 bd->swap_time = ktime_get_seconds(); 1113 } 1114 spin_unlock(&blocked_delegations_lock); 1115 } 1116 hash = jhash(&fh->fh_raw, fh->fh_size, 0); 1117 if (test_bit(hash&255, bd->set[0]) && 1118 test_bit((hash>>8)&255, bd->set[0]) && 1119 test_bit((hash>>16)&255, bd->set[0])) 1120 return 1; 1121 1122 if (test_bit(hash&255, bd->set[1]) && 1123 test_bit((hash>>8)&255, bd->set[1]) && 1124 test_bit((hash>>16)&255, bd->set[1])) 1125 return 1; 1126 1127 return 0; 1128 } 1129 1130 static void block_delegations(struct knfsd_fh *fh) 1131 { 1132 u32 hash; 1133 struct bloom_pair *bd = &blocked_delegations; 1134 1135 hash = jhash(&fh->fh_raw, fh->fh_size, 0); 1136 1137 spin_lock(&blocked_delegations_lock); 1138 __set_bit(hash&255, bd->set[bd->new]); 1139 __set_bit((hash>>8)&255, bd->set[bd->new]); 1140 __set_bit((hash>>16)&255, bd->set[bd->new]); 1141 if (bd->entries == 0) 1142 bd->swap_time = ktime_get_seconds(); 1143 bd->entries += 1; 1144 spin_unlock(&blocked_delegations_lock); 1145 } 1146 1147 static struct nfs4_delegation * 1148 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, 1149 struct nfs4_clnt_odstate *odstate, u32 dl_type) 1150 { 1151 struct nfs4_delegation *dp; 1152 struct nfs4_stid *stid; 1153 long n; 1154 1155 dprintk("NFSD alloc_init_deleg\n"); 1156 n = atomic_long_inc_return(&num_delegations); 1157 if (n < 0 || n > max_delegations) 1158 goto out_dec; 1159 if (delegation_blocked(&fp->fi_fhandle)) 1160 goto out_dec; 1161 stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg); 1162 if (stid == NULL) 1163 goto out_dec; 1164 dp = delegstateid(stid); 1165 1166 /* 1167 * delegation seqid's are never incremented. The 4.1 special 1168 * meaning of seqid 0 isn't meaningful, really, but let's avoid 1169 * 0 anyway just for consistency and use 1: 1170 */ 1171 dp->dl_stid.sc_stateid.si_generation = 1; 1172 INIT_LIST_HEAD(&dp->dl_perfile); 1173 INIT_LIST_HEAD(&dp->dl_perclnt); 1174 INIT_LIST_HEAD(&dp->dl_recall_lru); 1175 dp->dl_clnt_odstate = odstate; 1176 get_clnt_odstate(odstate); 1177 dp->dl_type = dl_type; 1178 dp->dl_retries = 1; 1179 dp->dl_recalled = false; 1180 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, 1181 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); 1182 nfsd4_init_cb(&dp->dl_cb_fattr.ncf_getattr, dp->dl_stid.sc_client, 1183 &nfsd4_cb_getattr_ops, NFSPROC4_CLNT_CB_GETATTR); 1184 dp->dl_cb_fattr.ncf_file_modified = false; 1185 dp->dl_cb_fattr.ncf_cb_bmap[0] = FATTR4_WORD0_CHANGE | FATTR4_WORD0_SIZE; 1186 get_nfs4_file(fp); 1187 dp->dl_stid.sc_file = fp; 1188 return dp; 1189 out_dec: 1190 atomic_long_dec(&num_delegations); 1191 return NULL; 1192 } 1193 1194 void 1195 nfs4_put_stid(struct nfs4_stid *s) 1196 { 1197 struct nfs4_file *fp = s->sc_file; 1198 struct nfs4_client *clp = s->sc_client; 1199 1200 might_lock(&clp->cl_lock); 1201 1202 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) { 1203 wake_up_all(&close_wq); 1204 return; 1205 } 1206 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); 1207 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) 1208 atomic_dec(&s->sc_client->cl_admin_revoked); 1209 nfs4_free_cpntf_statelist(clp->net, s); 1210 spin_unlock(&clp->cl_lock); 1211 s->sc_free(s); 1212 if (fp) 1213 put_nfs4_file(fp); 1214 } 1215 1216 void 1217 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) 1218 { 1219 stateid_t *src = &stid->sc_stateid; 1220 1221 spin_lock(&stid->sc_lock); 1222 if (unlikely(++src->si_generation == 0)) 1223 src->si_generation = 1; 1224 memcpy(dst, src, sizeof(*dst)); 1225 spin_unlock(&stid->sc_lock); 1226 } 1227 1228 static void put_deleg_file(struct nfs4_file *fp) 1229 { 1230 struct nfsd_file *nf = NULL; 1231 1232 spin_lock(&fp->fi_lock); 1233 if (--fp->fi_delegees == 0) 1234 swap(nf, fp->fi_deleg_file); 1235 spin_unlock(&fp->fi_lock); 1236 1237 if (nf) 1238 nfsd_file_put(nf); 1239 } 1240 1241 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp) 1242 { 1243 struct nfs4_file *fp = dp->dl_stid.sc_file; 1244 struct nfsd_file *nf = fp->fi_deleg_file; 1245 1246 WARN_ON_ONCE(!fp->fi_delegees); 1247 1248 kernel_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp); 1249 put_deleg_file(fp); 1250 } 1251 1252 static void destroy_unhashed_deleg(struct nfs4_delegation *dp) 1253 { 1254 put_clnt_odstate(dp->dl_clnt_odstate); 1255 nfs4_unlock_deleg_lease(dp); 1256 nfs4_put_stid(&dp->dl_stid); 1257 } 1258 1259 /** 1260 * nfs4_delegation_exists - Discover if this delegation already exists 1261 * @clp: a pointer to the nfs4_client we're granting a delegation to 1262 * @fp: a pointer to the nfs4_file we're granting a delegation on 1263 * 1264 * Return: 1265 * On success: true iff an existing delegation is found 1266 */ 1267 1268 static bool 1269 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp) 1270 { 1271 struct nfs4_delegation *searchdp = NULL; 1272 struct nfs4_client *searchclp = NULL; 1273 1274 lockdep_assert_held(&state_lock); 1275 lockdep_assert_held(&fp->fi_lock); 1276 1277 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { 1278 searchclp = searchdp->dl_stid.sc_client; 1279 if (clp == searchclp) { 1280 return true; 1281 } 1282 } 1283 return false; 1284 } 1285 1286 /** 1287 * hash_delegation_locked - Add a delegation to the appropriate lists 1288 * @dp: a pointer to the nfs4_delegation we are adding. 1289 * @fp: a pointer to the nfs4_file we're granting a delegation on 1290 * 1291 * Return: 1292 * On success: NULL if the delegation was successfully hashed. 1293 * 1294 * On error: -EAGAIN if one was previously granted to this 1295 * nfs4_client for this nfs4_file. Delegation is not hashed. 1296 * 1297 */ 1298 1299 static int 1300 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) 1301 { 1302 struct nfs4_client *clp = dp->dl_stid.sc_client; 1303 1304 lockdep_assert_held(&state_lock); 1305 lockdep_assert_held(&fp->fi_lock); 1306 lockdep_assert_held(&clp->cl_lock); 1307 1308 if (nfs4_delegation_exists(clp, fp)) 1309 return -EAGAIN; 1310 refcount_inc(&dp->dl_stid.sc_count); 1311 dp->dl_stid.sc_type = SC_TYPE_DELEG; 1312 list_add(&dp->dl_perfile, &fp->fi_delegations); 1313 list_add(&dp->dl_perclnt, &clp->cl_delegations); 1314 return 0; 1315 } 1316 1317 static bool delegation_hashed(struct nfs4_delegation *dp) 1318 { 1319 return !(list_empty(&dp->dl_perfile)); 1320 } 1321 1322 static bool 1323 unhash_delegation_locked(struct nfs4_delegation *dp, unsigned short statusmask) 1324 { 1325 struct nfs4_file *fp = dp->dl_stid.sc_file; 1326 1327 lockdep_assert_held(&state_lock); 1328 1329 if (!delegation_hashed(dp)) 1330 return false; 1331 1332 if (statusmask == SC_STATUS_REVOKED && 1333 dp->dl_stid.sc_client->cl_minorversion == 0) 1334 statusmask = SC_STATUS_CLOSED; 1335 dp->dl_stid.sc_status |= statusmask; 1336 if (statusmask & SC_STATUS_ADMIN_REVOKED) 1337 atomic_inc(&dp->dl_stid.sc_client->cl_admin_revoked); 1338 1339 /* Ensure that deleg break won't try to requeue it */ 1340 ++dp->dl_time; 1341 spin_lock(&fp->fi_lock); 1342 list_del_init(&dp->dl_perclnt); 1343 list_del_init(&dp->dl_recall_lru); 1344 list_del_init(&dp->dl_perfile); 1345 spin_unlock(&fp->fi_lock); 1346 return true; 1347 } 1348 1349 static void destroy_delegation(struct nfs4_delegation *dp) 1350 { 1351 bool unhashed; 1352 1353 spin_lock(&state_lock); 1354 unhashed = unhash_delegation_locked(dp, SC_STATUS_CLOSED); 1355 spin_unlock(&state_lock); 1356 if (unhashed) 1357 destroy_unhashed_deleg(dp); 1358 } 1359 1360 static void revoke_delegation(struct nfs4_delegation *dp) 1361 { 1362 struct nfs4_client *clp = dp->dl_stid.sc_client; 1363 1364 WARN_ON(!list_empty(&dp->dl_recall_lru)); 1365 1366 trace_nfsd_stid_revoke(&dp->dl_stid); 1367 1368 if (dp->dl_stid.sc_status & 1369 (SC_STATUS_REVOKED | SC_STATUS_ADMIN_REVOKED)) { 1370 spin_lock(&clp->cl_lock); 1371 refcount_inc(&dp->dl_stid.sc_count); 1372 list_add(&dp->dl_recall_lru, &clp->cl_revoked); 1373 spin_unlock(&clp->cl_lock); 1374 } 1375 destroy_unhashed_deleg(dp); 1376 } 1377 1378 /* 1379 * SETCLIENTID state 1380 */ 1381 1382 static unsigned int clientid_hashval(u32 id) 1383 { 1384 return id & CLIENT_HASH_MASK; 1385 } 1386 1387 static unsigned int clientstr_hashval(struct xdr_netobj name) 1388 { 1389 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK; 1390 } 1391 1392 /* 1393 * A stateid that had a deny mode associated with it is being released 1394 * or downgraded. Recalculate the deny mode on the file. 1395 */ 1396 static void 1397 recalculate_deny_mode(struct nfs4_file *fp) 1398 { 1399 struct nfs4_ol_stateid *stp; 1400 u32 old_deny; 1401 1402 spin_lock(&fp->fi_lock); 1403 old_deny = fp->fi_share_deny; 1404 fp->fi_share_deny = 0; 1405 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) { 1406 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); 1407 if (fp->fi_share_deny == old_deny) 1408 break; 1409 } 1410 spin_unlock(&fp->fi_lock); 1411 } 1412 1413 static void 1414 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) 1415 { 1416 int i; 1417 bool change = false; 1418 1419 for (i = 1; i < 4; i++) { 1420 if ((i & deny) != i) { 1421 change = true; 1422 clear_deny(i, stp); 1423 } 1424 } 1425 1426 /* Recalculate per-file deny mode if there was a change */ 1427 if (change) 1428 recalculate_deny_mode(stp->st_stid.sc_file); 1429 } 1430 1431 /* release all access and file references for a given stateid */ 1432 static void 1433 release_all_access(struct nfs4_ol_stateid *stp) 1434 { 1435 int i; 1436 struct nfs4_file *fp = stp->st_stid.sc_file; 1437 1438 if (fp && stp->st_deny_bmap != 0) 1439 recalculate_deny_mode(fp); 1440 1441 for (i = 1; i < 4; i++) { 1442 if (test_access(i, stp)) 1443 nfs4_file_put_access(stp->st_stid.sc_file, i); 1444 clear_access(i, stp); 1445 } 1446 } 1447 1448 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) 1449 { 1450 kfree(sop->so_owner.data); 1451 sop->so_ops->so_free(sop); 1452 } 1453 1454 static void nfs4_put_stateowner(struct nfs4_stateowner *sop) 1455 { 1456 struct nfs4_client *clp = sop->so_client; 1457 1458 might_lock(&clp->cl_lock); 1459 1460 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) 1461 return; 1462 sop->so_ops->so_unhash(sop); 1463 spin_unlock(&clp->cl_lock); 1464 nfs4_free_stateowner(sop); 1465 } 1466 1467 static bool 1468 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp) 1469 { 1470 return list_empty(&stp->st_perfile); 1471 } 1472 1473 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) 1474 { 1475 struct nfs4_file *fp = stp->st_stid.sc_file; 1476 1477 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); 1478 1479 if (list_empty(&stp->st_perfile)) 1480 return false; 1481 1482 spin_lock(&fp->fi_lock); 1483 list_del_init(&stp->st_perfile); 1484 spin_unlock(&fp->fi_lock); 1485 list_del(&stp->st_perstateowner); 1486 return true; 1487 } 1488 1489 static void nfs4_free_ol_stateid(struct nfs4_stid *stid) 1490 { 1491 struct nfs4_ol_stateid *stp = openlockstateid(stid); 1492 1493 put_clnt_odstate(stp->st_clnt_odstate); 1494 release_all_access(stp); 1495 if (stp->st_stateowner) 1496 nfs4_put_stateowner(stp->st_stateowner); 1497 WARN_ON(!list_empty(&stid->sc_cp_list)); 1498 kmem_cache_free(stateid_slab, stid); 1499 } 1500 1501 static void nfs4_free_lock_stateid(struct nfs4_stid *stid) 1502 { 1503 struct nfs4_ol_stateid *stp = openlockstateid(stid); 1504 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); 1505 struct nfsd_file *nf; 1506 1507 nf = find_any_file(stp->st_stid.sc_file); 1508 if (nf) { 1509 get_file(nf->nf_file); 1510 filp_close(nf->nf_file, (fl_owner_t)lo); 1511 nfsd_file_put(nf); 1512 } 1513 nfs4_free_ol_stateid(stid); 1514 } 1515 1516 /* 1517 * Put the persistent reference to an already unhashed generic stateid, while 1518 * holding the cl_lock. If it's the last reference, then put it onto the 1519 * reaplist for later destruction. 1520 */ 1521 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, 1522 struct list_head *reaplist) 1523 { 1524 struct nfs4_stid *s = &stp->st_stid; 1525 struct nfs4_client *clp = s->sc_client; 1526 1527 lockdep_assert_held(&clp->cl_lock); 1528 1529 WARN_ON_ONCE(!list_empty(&stp->st_locks)); 1530 1531 if (!refcount_dec_and_test(&s->sc_count)) { 1532 wake_up_all(&close_wq); 1533 return; 1534 } 1535 1536 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); 1537 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) 1538 atomic_dec(&s->sc_client->cl_admin_revoked); 1539 list_add(&stp->st_locks, reaplist); 1540 } 1541 1542 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) 1543 { 1544 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); 1545 1546 if (!unhash_ol_stateid(stp)) 1547 return false; 1548 list_del_init(&stp->st_locks); 1549 stp->st_stid.sc_status |= SC_STATUS_CLOSED; 1550 return true; 1551 } 1552 1553 static void release_lock_stateid(struct nfs4_ol_stateid *stp) 1554 { 1555 struct nfs4_client *clp = stp->st_stid.sc_client; 1556 bool unhashed; 1557 1558 spin_lock(&clp->cl_lock); 1559 unhashed = unhash_lock_stateid(stp); 1560 spin_unlock(&clp->cl_lock); 1561 if (unhashed) 1562 nfs4_put_stid(&stp->st_stid); 1563 } 1564 1565 static void unhash_lockowner_locked(struct nfs4_lockowner *lo) 1566 { 1567 struct nfs4_client *clp = lo->lo_owner.so_client; 1568 1569 lockdep_assert_held(&clp->cl_lock); 1570 1571 list_del_init(&lo->lo_owner.so_strhash); 1572 } 1573 1574 /* 1575 * Free a list of generic stateids that were collected earlier after being 1576 * fully unhashed. 1577 */ 1578 static void 1579 free_ol_stateid_reaplist(struct list_head *reaplist) 1580 { 1581 struct nfs4_ol_stateid *stp; 1582 struct nfs4_file *fp; 1583 1584 might_sleep(); 1585 1586 while (!list_empty(reaplist)) { 1587 stp = list_first_entry(reaplist, struct nfs4_ol_stateid, 1588 st_locks); 1589 list_del(&stp->st_locks); 1590 fp = stp->st_stid.sc_file; 1591 stp->st_stid.sc_free(&stp->st_stid); 1592 if (fp) 1593 put_nfs4_file(fp); 1594 } 1595 } 1596 1597 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, 1598 struct list_head *reaplist) 1599 { 1600 struct nfs4_ol_stateid *stp; 1601 1602 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); 1603 1604 while (!list_empty(&open_stp->st_locks)) { 1605 stp = list_entry(open_stp->st_locks.next, 1606 struct nfs4_ol_stateid, st_locks); 1607 unhash_lock_stateid(stp); 1608 put_ol_stateid_locked(stp, reaplist); 1609 } 1610 } 1611 1612 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, 1613 struct list_head *reaplist) 1614 { 1615 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); 1616 1617 if (!unhash_ol_stateid(stp)) 1618 return false; 1619 release_open_stateid_locks(stp, reaplist); 1620 return true; 1621 } 1622 1623 static void release_open_stateid(struct nfs4_ol_stateid *stp) 1624 { 1625 LIST_HEAD(reaplist); 1626 1627 spin_lock(&stp->st_stid.sc_client->cl_lock); 1628 stp->st_stid.sc_status |= SC_STATUS_CLOSED; 1629 if (unhash_open_stateid(stp, &reaplist)) 1630 put_ol_stateid_locked(stp, &reaplist); 1631 spin_unlock(&stp->st_stid.sc_client->cl_lock); 1632 free_ol_stateid_reaplist(&reaplist); 1633 } 1634 1635 static void unhash_openowner_locked(struct nfs4_openowner *oo) 1636 { 1637 struct nfs4_client *clp = oo->oo_owner.so_client; 1638 1639 lockdep_assert_held(&clp->cl_lock); 1640 1641 list_del_init(&oo->oo_owner.so_strhash); 1642 list_del_init(&oo->oo_perclient); 1643 } 1644 1645 static void release_last_closed_stateid(struct nfs4_openowner *oo) 1646 { 1647 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, 1648 nfsd_net_id); 1649 struct nfs4_ol_stateid *s; 1650 1651 spin_lock(&nn->client_lock); 1652 s = oo->oo_last_closed_stid; 1653 if (s) { 1654 list_del_init(&oo->oo_close_lru); 1655 oo->oo_last_closed_stid = NULL; 1656 } 1657 spin_unlock(&nn->client_lock); 1658 if (s) 1659 nfs4_put_stid(&s->st_stid); 1660 } 1661 1662 static void release_openowner(struct nfs4_openowner *oo) 1663 { 1664 struct nfs4_ol_stateid *stp; 1665 struct nfs4_client *clp = oo->oo_owner.so_client; 1666 struct list_head reaplist; 1667 1668 INIT_LIST_HEAD(&reaplist); 1669 1670 spin_lock(&clp->cl_lock); 1671 unhash_openowner_locked(oo); 1672 while (!list_empty(&oo->oo_owner.so_stateids)) { 1673 stp = list_first_entry(&oo->oo_owner.so_stateids, 1674 struct nfs4_ol_stateid, st_perstateowner); 1675 if (unhash_open_stateid(stp, &reaplist)) 1676 put_ol_stateid_locked(stp, &reaplist); 1677 } 1678 spin_unlock(&clp->cl_lock); 1679 free_ol_stateid_reaplist(&reaplist); 1680 release_last_closed_stateid(oo); 1681 nfs4_put_stateowner(&oo->oo_owner); 1682 } 1683 1684 static struct nfs4_stid *find_one_sb_stid(struct nfs4_client *clp, 1685 struct super_block *sb, 1686 unsigned int sc_types) 1687 { 1688 unsigned long id, tmp; 1689 struct nfs4_stid *stid; 1690 1691 spin_lock(&clp->cl_lock); 1692 idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id) 1693 if ((stid->sc_type & sc_types) && 1694 stid->sc_status == 0 && 1695 stid->sc_file->fi_inode->i_sb == sb) { 1696 refcount_inc(&stid->sc_count); 1697 break; 1698 } 1699 spin_unlock(&clp->cl_lock); 1700 return stid; 1701 } 1702 1703 /** 1704 * nfsd4_revoke_states - revoke all nfsv4 states associated with given filesystem 1705 * @net: used to identify instance of nfsd (there is one per net namespace) 1706 * @sb: super_block used to identify target filesystem 1707 * 1708 * All nfs4 states (open, lock, delegation, layout) held by the server instance 1709 * and associated with a file on the given filesystem will be revoked resulting 1710 * in any files being closed and so all references from nfsd to the filesystem 1711 * being released. Thus nfsd will no longer prevent the filesystem from being 1712 * unmounted. 1713 * 1714 * The clients which own the states will subsequently being notified that the 1715 * states have been "admin-revoked". 1716 */ 1717 void nfsd4_revoke_states(struct net *net, struct super_block *sb) 1718 { 1719 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1720 unsigned int idhashval; 1721 unsigned int sc_types; 1722 1723 sc_types = SC_TYPE_OPEN | SC_TYPE_LOCK | SC_TYPE_DELEG | SC_TYPE_LAYOUT; 1724 1725 spin_lock(&nn->client_lock); 1726 for (idhashval = 0; idhashval < CLIENT_HASH_MASK; idhashval++) { 1727 struct list_head *head = &nn->conf_id_hashtbl[idhashval]; 1728 struct nfs4_client *clp; 1729 retry: 1730 list_for_each_entry(clp, head, cl_idhash) { 1731 struct nfs4_stid *stid = find_one_sb_stid(clp, sb, 1732 sc_types); 1733 if (stid) { 1734 struct nfs4_ol_stateid *stp; 1735 struct nfs4_delegation *dp; 1736 struct nfs4_layout_stateid *ls; 1737 1738 spin_unlock(&nn->client_lock); 1739 switch (stid->sc_type) { 1740 case SC_TYPE_OPEN: 1741 stp = openlockstateid(stid); 1742 mutex_lock_nested(&stp->st_mutex, 1743 OPEN_STATEID_MUTEX); 1744 1745 spin_lock(&clp->cl_lock); 1746 if (stid->sc_status == 0) { 1747 stid->sc_status |= 1748 SC_STATUS_ADMIN_REVOKED; 1749 atomic_inc(&clp->cl_admin_revoked); 1750 spin_unlock(&clp->cl_lock); 1751 release_all_access(stp); 1752 } else 1753 spin_unlock(&clp->cl_lock); 1754 mutex_unlock(&stp->st_mutex); 1755 break; 1756 case SC_TYPE_LOCK: 1757 stp = openlockstateid(stid); 1758 mutex_lock_nested(&stp->st_mutex, 1759 LOCK_STATEID_MUTEX); 1760 spin_lock(&clp->cl_lock); 1761 if (stid->sc_status == 0) { 1762 struct nfs4_lockowner *lo = 1763 lockowner(stp->st_stateowner); 1764 struct nfsd_file *nf; 1765 1766 stid->sc_status |= 1767 SC_STATUS_ADMIN_REVOKED; 1768 atomic_inc(&clp->cl_admin_revoked); 1769 spin_unlock(&clp->cl_lock); 1770 nf = find_any_file(stp->st_stid.sc_file); 1771 if (nf) { 1772 get_file(nf->nf_file); 1773 filp_close(nf->nf_file, 1774 (fl_owner_t)lo); 1775 nfsd_file_put(nf); 1776 } 1777 release_all_access(stp); 1778 } else 1779 spin_unlock(&clp->cl_lock); 1780 mutex_unlock(&stp->st_mutex); 1781 break; 1782 case SC_TYPE_DELEG: 1783 dp = delegstateid(stid); 1784 spin_lock(&state_lock); 1785 if (!unhash_delegation_locked( 1786 dp, SC_STATUS_ADMIN_REVOKED)) 1787 dp = NULL; 1788 spin_unlock(&state_lock); 1789 if (dp) 1790 revoke_delegation(dp); 1791 break; 1792 case SC_TYPE_LAYOUT: 1793 ls = layoutstateid(stid); 1794 nfsd4_close_layout(ls); 1795 break; 1796 } 1797 nfs4_put_stid(stid); 1798 spin_lock(&nn->client_lock); 1799 if (clp->cl_minorversion == 0) 1800 /* Allow cleanup after a lease period. 1801 * store_release ensures cleanup will 1802 * see any newly revoked states if it 1803 * sees the time updated. 1804 */ 1805 nn->nfs40_last_revoke = 1806 ktime_get_boottime_seconds(); 1807 goto retry; 1808 } 1809 } 1810 } 1811 spin_unlock(&nn->client_lock); 1812 } 1813 1814 static inline int 1815 hash_sessionid(struct nfs4_sessionid *sessionid) 1816 { 1817 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; 1818 1819 return sid->sequence % SESSION_HASH_SIZE; 1820 } 1821 1822 #ifdef CONFIG_SUNRPC_DEBUG 1823 static inline void 1824 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) 1825 { 1826 u32 *ptr = (u32 *)(&sessionid->data[0]); 1827 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); 1828 } 1829 #else 1830 static inline void 1831 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) 1832 { 1833 } 1834 #endif 1835 1836 /* 1837 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it 1838 * won't be used for replay. 1839 */ 1840 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) 1841 { 1842 struct nfs4_stateowner *so = cstate->replay_owner; 1843 1844 if (nfserr == nfserr_replay_me) 1845 return; 1846 1847 if (!seqid_mutating_err(ntohl(nfserr))) { 1848 nfsd4_cstate_clear_replay(cstate); 1849 return; 1850 } 1851 if (!so) 1852 return; 1853 if (so->so_is_open_owner) 1854 release_last_closed_stateid(openowner(so)); 1855 so->so_seqid++; 1856 return; 1857 } 1858 1859 static void 1860 gen_sessionid(struct nfsd4_session *ses) 1861 { 1862 struct nfs4_client *clp = ses->se_client; 1863 struct nfsd4_sessionid *sid; 1864 1865 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; 1866 sid->clientid = clp->cl_clientid; 1867 sid->sequence = current_sessionid++; 1868 sid->reserved = 0; 1869 } 1870 1871 /* 1872 * The protocol defines ca_maxresponssize_cached to include the size of 1873 * the rpc header, but all we need to cache is the data starting after 1874 * the end of the initial SEQUENCE operation--the rest we regenerate 1875 * each time. Therefore we can advertise a ca_maxresponssize_cached 1876 * value that is the number of bytes in our cache plus a few additional 1877 * bytes. In order to stay on the safe side, and not promise more than 1878 * we can cache, those additional bytes must be the minimum possible: 24 1879 * bytes of rpc header (xid through accept state, with AUTH_NULL 1880 * verifier), 12 for the compound header (with zero-length tag), and 44 1881 * for the SEQUENCE op response: 1882 */ 1883 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) 1884 1885 static void 1886 free_session_slots(struct nfsd4_session *ses) 1887 { 1888 int i; 1889 1890 for (i = 0; i < ses->se_fchannel.maxreqs; i++) { 1891 free_svc_cred(&ses->se_slots[i]->sl_cred); 1892 kfree(ses->se_slots[i]); 1893 } 1894 } 1895 1896 /* 1897 * We don't actually need to cache the rpc and session headers, so we 1898 * can allocate a little less for each slot: 1899 */ 1900 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) 1901 { 1902 u32 size; 1903 1904 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) 1905 size = 0; 1906 else 1907 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; 1908 return size + sizeof(struct nfsd4_slot); 1909 } 1910 1911 /* 1912 * XXX: If we run out of reserved DRC memory we could (up to a point) 1913 * re-negotiate active sessions and reduce their slot usage to make 1914 * room for new connections. For now we just fail the create session. 1915 */ 1916 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) 1917 { 1918 u32 slotsize = slot_bytes(ca); 1919 u32 num = ca->maxreqs; 1920 unsigned long avail, total_avail; 1921 unsigned int scale_factor; 1922 1923 spin_lock(&nfsd_drc_lock); 1924 if (nfsd_drc_max_mem > nfsd_drc_mem_used) 1925 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used; 1926 else 1927 /* We have handed out more space than we chose in 1928 * set_max_drc() to allow. That isn't really a 1929 * problem as long as that doesn't make us think we 1930 * have lots more due to integer overflow. 1931 */ 1932 total_avail = 0; 1933 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail); 1934 /* 1935 * Never use more than a fraction of the remaining memory, 1936 * unless it's the only way to give this client a slot. 1937 * The chosen fraction is either 1/8 or 1/number of threads, 1938 * whichever is smaller. This ensures there are adequate 1939 * slots to support multiple clients per thread. 1940 * Give the client one slot even if that would require 1941 * over-allocation--it is better than failure. 1942 */ 1943 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads); 1944 1945 avail = clamp_t(unsigned long, avail, slotsize, 1946 total_avail/scale_factor); 1947 num = min_t(int, num, avail / slotsize); 1948 num = max_t(int, num, 1); 1949 nfsd_drc_mem_used += num * slotsize; 1950 spin_unlock(&nfsd_drc_lock); 1951 1952 return num; 1953 } 1954 1955 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) 1956 { 1957 int slotsize = slot_bytes(ca); 1958 1959 spin_lock(&nfsd_drc_lock); 1960 nfsd_drc_mem_used -= slotsize * ca->maxreqs; 1961 spin_unlock(&nfsd_drc_lock); 1962 } 1963 1964 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, 1965 struct nfsd4_channel_attrs *battrs) 1966 { 1967 int numslots = fattrs->maxreqs; 1968 int slotsize = slot_bytes(fattrs); 1969 struct nfsd4_session *new; 1970 int i; 1971 1972 BUILD_BUG_ON(struct_size(new, se_slots, NFSD_MAX_SLOTS_PER_SESSION) 1973 > PAGE_SIZE); 1974 1975 new = kzalloc(struct_size(new, se_slots, numslots), GFP_KERNEL); 1976 if (!new) 1977 return NULL; 1978 /* allocate each struct nfsd4_slot and data cache in one piece */ 1979 for (i = 0; i < numslots; i++) { 1980 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); 1981 if (!new->se_slots[i]) 1982 goto out_free; 1983 } 1984 1985 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); 1986 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); 1987 1988 return new; 1989 out_free: 1990 while (i--) 1991 kfree(new->se_slots[i]); 1992 kfree(new); 1993 return NULL; 1994 } 1995 1996 static void free_conn(struct nfsd4_conn *c) 1997 { 1998 svc_xprt_put(c->cn_xprt); 1999 kfree(c); 2000 } 2001 2002 static void nfsd4_conn_lost(struct svc_xpt_user *u) 2003 { 2004 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); 2005 struct nfs4_client *clp = c->cn_session->se_client; 2006 2007 trace_nfsd_cb_lost(clp); 2008 2009 spin_lock(&clp->cl_lock); 2010 if (!list_empty(&c->cn_persession)) { 2011 list_del(&c->cn_persession); 2012 free_conn(c); 2013 } 2014 nfsd4_probe_callback(clp); 2015 spin_unlock(&clp->cl_lock); 2016 } 2017 2018 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags) 2019 { 2020 struct nfsd4_conn *conn; 2021 2022 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); 2023 if (!conn) 2024 return NULL; 2025 svc_xprt_get(rqstp->rq_xprt); 2026 conn->cn_xprt = rqstp->rq_xprt; 2027 conn->cn_flags = flags; 2028 INIT_LIST_HEAD(&conn->cn_xpt_user.list); 2029 return conn; 2030 } 2031 2032 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) 2033 { 2034 conn->cn_session = ses; 2035 list_add(&conn->cn_persession, &ses->se_conns); 2036 } 2037 2038 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) 2039 { 2040 struct nfs4_client *clp = ses->se_client; 2041 2042 spin_lock(&clp->cl_lock); 2043 __nfsd4_hash_conn(conn, ses); 2044 spin_unlock(&clp->cl_lock); 2045 } 2046 2047 static int nfsd4_register_conn(struct nfsd4_conn *conn) 2048 { 2049 conn->cn_xpt_user.callback = nfsd4_conn_lost; 2050 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); 2051 } 2052 2053 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses) 2054 { 2055 int ret; 2056 2057 nfsd4_hash_conn(conn, ses); 2058 ret = nfsd4_register_conn(conn); 2059 if (ret) 2060 /* oops; xprt is already down: */ 2061 nfsd4_conn_lost(&conn->cn_xpt_user); 2062 /* We may have gained or lost a callback channel: */ 2063 nfsd4_probe_callback_sync(ses->se_client); 2064 } 2065 2066 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses) 2067 { 2068 u32 dir = NFS4_CDFC4_FORE; 2069 2070 if (cses->flags & SESSION4_BACK_CHAN) 2071 dir |= NFS4_CDFC4_BACK; 2072 return alloc_conn(rqstp, dir); 2073 } 2074 2075 /* must be called under client_lock */ 2076 static void nfsd4_del_conns(struct nfsd4_session *s) 2077 { 2078 struct nfs4_client *clp = s->se_client; 2079 struct nfsd4_conn *c; 2080 2081 spin_lock(&clp->cl_lock); 2082 while (!list_empty(&s->se_conns)) { 2083 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); 2084 list_del_init(&c->cn_persession); 2085 spin_unlock(&clp->cl_lock); 2086 2087 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); 2088 free_conn(c); 2089 2090 spin_lock(&clp->cl_lock); 2091 } 2092 spin_unlock(&clp->cl_lock); 2093 } 2094 2095 static void __free_session(struct nfsd4_session *ses) 2096 { 2097 free_session_slots(ses); 2098 kfree(ses); 2099 } 2100 2101 static void free_session(struct nfsd4_session *ses) 2102 { 2103 nfsd4_del_conns(ses); 2104 nfsd4_put_drc_mem(&ses->se_fchannel); 2105 __free_session(ses); 2106 } 2107 2108 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses) 2109 { 2110 int idx; 2111 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 2112 2113 new->se_client = clp; 2114 gen_sessionid(new); 2115 2116 INIT_LIST_HEAD(&new->se_conns); 2117 2118 new->se_cb_seq_nr = 1; 2119 new->se_flags = cses->flags; 2120 new->se_cb_prog = cses->callback_prog; 2121 new->se_cb_sec = cses->cb_sec; 2122 atomic_set(&new->se_ref, 0); 2123 idx = hash_sessionid(&new->se_sessionid); 2124 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); 2125 spin_lock(&clp->cl_lock); 2126 list_add(&new->se_perclnt, &clp->cl_sessions); 2127 spin_unlock(&clp->cl_lock); 2128 2129 { 2130 struct sockaddr *sa = svc_addr(rqstp); 2131 /* 2132 * This is a little silly; with sessions there's no real 2133 * use for the callback address. Use the peer address 2134 * as a reasonable default for now, but consider fixing 2135 * the rpc client not to require an address in the 2136 * future: 2137 */ 2138 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); 2139 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); 2140 } 2141 } 2142 2143 /* caller must hold client_lock */ 2144 static struct nfsd4_session * 2145 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) 2146 { 2147 struct nfsd4_session *elem; 2148 int idx; 2149 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2150 2151 lockdep_assert_held(&nn->client_lock); 2152 2153 dump_sessionid(__func__, sessionid); 2154 idx = hash_sessionid(sessionid); 2155 /* Search in the appropriate list */ 2156 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { 2157 if (!memcmp(elem->se_sessionid.data, sessionid->data, 2158 NFS4_MAX_SESSIONID_LEN)) { 2159 return elem; 2160 } 2161 } 2162 2163 dprintk("%s: session not found\n", __func__); 2164 return NULL; 2165 } 2166 2167 static struct nfsd4_session * 2168 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, 2169 __be32 *ret) 2170 { 2171 struct nfsd4_session *session; 2172 __be32 status = nfserr_badsession; 2173 2174 session = __find_in_sessionid_hashtbl(sessionid, net); 2175 if (!session) 2176 goto out; 2177 status = nfsd4_get_session_locked(session); 2178 if (status) 2179 session = NULL; 2180 out: 2181 *ret = status; 2182 return session; 2183 } 2184 2185 /* caller must hold client_lock */ 2186 static void 2187 unhash_session(struct nfsd4_session *ses) 2188 { 2189 struct nfs4_client *clp = ses->se_client; 2190 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2191 2192 lockdep_assert_held(&nn->client_lock); 2193 2194 list_del(&ses->se_hash); 2195 spin_lock(&ses->se_client->cl_lock); 2196 list_del(&ses->se_perclnt); 2197 spin_unlock(&ses->se_client->cl_lock); 2198 } 2199 2200 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ 2201 static int 2202 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) 2203 { 2204 /* 2205 * We're assuming the clid was not given out from a boot 2206 * precisely 2^32 (about 136 years) before this one. That seems 2207 * a safe assumption: 2208 */ 2209 if (clid->cl_boot == (u32)nn->boot_time) 2210 return 0; 2211 trace_nfsd_clid_stale(clid); 2212 return 1; 2213 } 2214 2215 /* 2216 * XXX Should we use a slab cache ? 2217 * This type of memory management is somewhat inefficient, but we use it 2218 * anyway since SETCLIENTID is not a common operation. 2219 */ 2220 static struct nfs4_client *alloc_client(struct xdr_netobj name, 2221 struct nfsd_net *nn) 2222 { 2223 struct nfs4_client *clp; 2224 int i; 2225 2226 if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) { 2227 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); 2228 return NULL; 2229 } 2230 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL); 2231 if (clp == NULL) 2232 return NULL; 2233 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL); 2234 if (clp->cl_name.data == NULL) 2235 goto err_no_name; 2236 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE, 2237 sizeof(struct list_head), 2238 GFP_KERNEL); 2239 if (!clp->cl_ownerstr_hashtbl) 2240 goto err_no_hashtbl; 2241 clp->cl_callback_wq = alloc_ordered_workqueue("nfsd4_callbacks", 0); 2242 if (!clp->cl_callback_wq) 2243 goto err_no_callback_wq; 2244 2245 for (i = 0; i < OWNER_HASH_SIZE; i++) 2246 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); 2247 INIT_LIST_HEAD(&clp->cl_sessions); 2248 idr_init(&clp->cl_stateids); 2249 atomic_set(&clp->cl_rpc_users, 0); 2250 clp->cl_cb_state = NFSD4_CB_UNKNOWN; 2251 clp->cl_state = NFSD4_ACTIVE; 2252 atomic_inc(&nn->nfs4_client_count); 2253 atomic_set(&clp->cl_delegs_in_recall, 0); 2254 INIT_LIST_HEAD(&clp->cl_idhash); 2255 INIT_LIST_HEAD(&clp->cl_openowners); 2256 INIT_LIST_HEAD(&clp->cl_delegations); 2257 INIT_LIST_HEAD(&clp->cl_lru); 2258 INIT_LIST_HEAD(&clp->cl_revoked); 2259 #ifdef CONFIG_NFSD_PNFS 2260 INIT_LIST_HEAD(&clp->cl_lo_states); 2261 #endif 2262 INIT_LIST_HEAD(&clp->async_copies); 2263 spin_lock_init(&clp->async_lock); 2264 spin_lock_init(&clp->cl_lock); 2265 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); 2266 return clp; 2267 err_no_callback_wq: 2268 kfree(clp->cl_ownerstr_hashtbl); 2269 err_no_hashtbl: 2270 kfree(clp->cl_name.data); 2271 err_no_name: 2272 kmem_cache_free(client_slab, clp); 2273 return NULL; 2274 } 2275 2276 static void __free_client(struct kref *k) 2277 { 2278 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref); 2279 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs); 2280 2281 free_svc_cred(&clp->cl_cred); 2282 destroy_workqueue(clp->cl_callback_wq); 2283 kfree(clp->cl_ownerstr_hashtbl); 2284 kfree(clp->cl_name.data); 2285 kfree(clp->cl_nii_domain.data); 2286 kfree(clp->cl_nii_name.data); 2287 idr_destroy(&clp->cl_stateids); 2288 kfree(clp->cl_ra); 2289 kmem_cache_free(client_slab, clp); 2290 } 2291 2292 static void drop_client(struct nfs4_client *clp) 2293 { 2294 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client); 2295 } 2296 2297 static void 2298 free_client(struct nfs4_client *clp) 2299 { 2300 while (!list_empty(&clp->cl_sessions)) { 2301 struct nfsd4_session *ses; 2302 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, 2303 se_perclnt); 2304 list_del(&ses->se_perclnt); 2305 WARN_ON_ONCE(atomic_read(&ses->se_ref)); 2306 free_session(ses); 2307 } 2308 rpc_destroy_wait_queue(&clp->cl_cb_waitq); 2309 if (clp->cl_nfsd_dentry) { 2310 nfsd_client_rmdir(clp->cl_nfsd_dentry); 2311 clp->cl_nfsd_dentry = NULL; 2312 wake_up_all(&expiry_wq); 2313 } 2314 drop_client(clp); 2315 } 2316 2317 /* must be called under the client_lock */ 2318 static void 2319 unhash_client_locked(struct nfs4_client *clp) 2320 { 2321 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2322 struct nfsd4_session *ses; 2323 2324 lockdep_assert_held(&nn->client_lock); 2325 2326 /* Mark the client as expired! */ 2327 clp->cl_time = 0; 2328 /* Make it invisible */ 2329 if (!list_empty(&clp->cl_idhash)) { 2330 list_del_init(&clp->cl_idhash); 2331 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) 2332 rb_erase(&clp->cl_namenode, &nn->conf_name_tree); 2333 else 2334 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); 2335 } 2336 list_del_init(&clp->cl_lru); 2337 spin_lock(&clp->cl_lock); 2338 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) 2339 list_del_init(&ses->se_hash); 2340 spin_unlock(&clp->cl_lock); 2341 } 2342 2343 static void 2344 unhash_client(struct nfs4_client *clp) 2345 { 2346 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2347 2348 spin_lock(&nn->client_lock); 2349 unhash_client_locked(clp); 2350 spin_unlock(&nn->client_lock); 2351 } 2352 2353 static __be32 mark_client_expired_locked(struct nfs4_client *clp) 2354 { 2355 int users = atomic_read(&clp->cl_rpc_users); 2356 2357 trace_nfsd_mark_client_expired(clp, users); 2358 2359 if (users) 2360 return nfserr_jukebox; 2361 unhash_client_locked(clp); 2362 return nfs_ok; 2363 } 2364 2365 static void 2366 __destroy_client(struct nfs4_client *clp) 2367 { 2368 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2369 int i; 2370 struct nfs4_openowner *oo; 2371 struct nfs4_delegation *dp; 2372 struct list_head reaplist; 2373 2374 INIT_LIST_HEAD(&reaplist); 2375 spin_lock(&state_lock); 2376 while (!list_empty(&clp->cl_delegations)) { 2377 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); 2378 unhash_delegation_locked(dp, SC_STATUS_CLOSED); 2379 list_add(&dp->dl_recall_lru, &reaplist); 2380 } 2381 spin_unlock(&state_lock); 2382 while (!list_empty(&reaplist)) { 2383 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); 2384 list_del_init(&dp->dl_recall_lru); 2385 destroy_unhashed_deleg(dp); 2386 } 2387 while (!list_empty(&clp->cl_revoked)) { 2388 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru); 2389 list_del_init(&dp->dl_recall_lru); 2390 nfs4_put_stid(&dp->dl_stid); 2391 } 2392 while (!list_empty(&clp->cl_openowners)) { 2393 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); 2394 nfs4_get_stateowner(&oo->oo_owner); 2395 release_openowner(oo); 2396 } 2397 for (i = 0; i < OWNER_HASH_SIZE; i++) { 2398 struct nfs4_stateowner *so, *tmp; 2399 2400 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i], 2401 so_strhash) { 2402 /* Should be no openowners at this point */ 2403 WARN_ON_ONCE(so->so_is_open_owner); 2404 remove_blocked_locks(lockowner(so)); 2405 } 2406 } 2407 nfsd4_return_all_client_layouts(clp); 2408 nfsd4_shutdown_copy(clp); 2409 nfsd4_shutdown_callback(clp); 2410 if (clp->cl_cb_conn.cb_xprt) 2411 svc_xprt_put(clp->cl_cb_conn.cb_xprt); 2412 atomic_add_unless(&nn->nfs4_client_count, -1, 0); 2413 nfsd4_dec_courtesy_client_count(nn, clp); 2414 free_client(clp); 2415 wake_up_all(&expiry_wq); 2416 } 2417 2418 static void 2419 destroy_client(struct nfs4_client *clp) 2420 { 2421 unhash_client(clp); 2422 __destroy_client(clp); 2423 } 2424 2425 static void inc_reclaim_complete(struct nfs4_client *clp) 2426 { 2427 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2428 2429 if (!nn->track_reclaim_completes) 2430 return; 2431 if (!nfsd4_find_reclaim_client(clp->cl_name, nn)) 2432 return; 2433 if (atomic_inc_return(&nn->nr_reclaim_complete) == 2434 nn->reclaim_str_hashtbl_size) { 2435 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n", 2436 clp->net->ns.inum); 2437 nfsd4_end_grace(nn); 2438 } 2439 } 2440 2441 static void expire_client(struct nfs4_client *clp) 2442 { 2443 unhash_client(clp); 2444 nfsd4_client_record_remove(clp); 2445 __destroy_client(clp); 2446 } 2447 2448 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) 2449 { 2450 memcpy(target->cl_verifier.data, source->data, 2451 sizeof(target->cl_verifier.data)); 2452 } 2453 2454 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) 2455 { 2456 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; 2457 target->cl_clientid.cl_id = source->cl_clientid.cl_id; 2458 } 2459 2460 static int copy_cred(struct svc_cred *target, struct svc_cred *source) 2461 { 2462 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL); 2463 target->cr_raw_principal = kstrdup(source->cr_raw_principal, 2464 GFP_KERNEL); 2465 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL); 2466 if ((source->cr_principal && !target->cr_principal) || 2467 (source->cr_raw_principal && !target->cr_raw_principal) || 2468 (source->cr_targ_princ && !target->cr_targ_princ)) 2469 return -ENOMEM; 2470 2471 target->cr_flavor = source->cr_flavor; 2472 target->cr_uid = source->cr_uid; 2473 target->cr_gid = source->cr_gid; 2474 target->cr_group_info = source->cr_group_info; 2475 get_group_info(target->cr_group_info); 2476 target->cr_gss_mech = source->cr_gss_mech; 2477 if (source->cr_gss_mech) 2478 gss_mech_get(source->cr_gss_mech); 2479 return 0; 2480 } 2481 2482 static int 2483 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) 2484 { 2485 if (o1->len < o2->len) 2486 return -1; 2487 if (o1->len > o2->len) 2488 return 1; 2489 return memcmp(o1->data, o2->data, o1->len); 2490 } 2491 2492 static int 2493 same_verf(nfs4_verifier *v1, nfs4_verifier *v2) 2494 { 2495 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); 2496 } 2497 2498 static int 2499 same_clid(clientid_t *cl1, clientid_t *cl2) 2500 { 2501 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id); 2502 } 2503 2504 static bool groups_equal(struct group_info *g1, struct group_info *g2) 2505 { 2506 int i; 2507 2508 if (g1->ngroups != g2->ngroups) 2509 return false; 2510 for (i=0; i<g1->ngroups; i++) 2511 if (!gid_eq(g1->gid[i], g2->gid[i])) 2512 return false; 2513 return true; 2514 } 2515 2516 /* 2517 * RFC 3530 language requires clid_inuse be returned when the 2518 * "principal" associated with a requests differs from that previously 2519 * used. We use uid, gid's, and gss principal string as our best 2520 * approximation. We also don't want to allow non-gss use of a client 2521 * established using gss: in theory cr_principal should catch that 2522 * change, but in practice cr_principal can be null even in the gss case 2523 * since gssd doesn't always pass down a principal string. 2524 */ 2525 static bool is_gss_cred(struct svc_cred *cr) 2526 { 2527 /* Is cr_flavor one of the gss "pseudoflavors"?: */ 2528 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); 2529 } 2530 2531 2532 static bool 2533 same_creds(struct svc_cred *cr1, struct svc_cred *cr2) 2534 { 2535 if ((is_gss_cred(cr1) != is_gss_cred(cr2)) 2536 || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) 2537 || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) 2538 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) 2539 return false; 2540 /* XXX: check that cr_targ_princ fields match ? */ 2541 if (cr1->cr_principal == cr2->cr_principal) 2542 return true; 2543 if (!cr1->cr_principal || !cr2->cr_principal) 2544 return false; 2545 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); 2546 } 2547 2548 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) 2549 { 2550 struct svc_cred *cr = &rqstp->rq_cred; 2551 u32 service; 2552 2553 if (!cr->cr_gss_mech) 2554 return false; 2555 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); 2556 return service == RPC_GSS_SVC_INTEGRITY || 2557 service == RPC_GSS_SVC_PRIVACY; 2558 } 2559 2560 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) 2561 { 2562 struct svc_cred *cr = &rqstp->rq_cred; 2563 2564 if (!cl->cl_mach_cred) 2565 return true; 2566 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) 2567 return false; 2568 if (!svc_rqst_integrity_protected(rqstp)) 2569 return false; 2570 if (cl->cl_cred.cr_raw_principal) 2571 return 0 == strcmp(cl->cl_cred.cr_raw_principal, 2572 cr->cr_raw_principal); 2573 if (!cr->cr_principal) 2574 return false; 2575 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); 2576 } 2577 2578 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn) 2579 { 2580 __be32 verf[2]; 2581 2582 /* 2583 * This is opaque to client, so no need to byte-swap. Use 2584 * __force to keep sparse happy 2585 */ 2586 verf[0] = (__force __be32)(u32)ktime_get_real_seconds(); 2587 verf[1] = (__force __be32)nn->clverifier_counter++; 2588 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); 2589 } 2590 2591 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) 2592 { 2593 clp->cl_clientid.cl_boot = (u32)nn->boot_time; 2594 clp->cl_clientid.cl_id = nn->clientid_counter++; 2595 gen_confirm(clp, nn); 2596 } 2597 2598 static struct nfs4_stid * 2599 find_stateid_locked(struct nfs4_client *cl, stateid_t *t) 2600 { 2601 struct nfs4_stid *ret; 2602 2603 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); 2604 if (!ret || !ret->sc_type) 2605 return NULL; 2606 return ret; 2607 } 2608 2609 static struct nfs4_stid * 2610 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, 2611 unsigned short typemask, unsigned short ok_states) 2612 { 2613 struct nfs4_stid *s; 2614 2615 spin_lock(&cl->cl_lock); 2616 s = find_stateid_locked(cl, t); 2617 if (s != NULL) { 2618 if ((s->sc_status & ~ok_states) == 0 && 2619 (typemask & s->sc_type)) 2620 refcount_inc(&s->sc_count); 2621 else 2622 s = NULL; 2623 } 2624 spin_unlock(&cl->cl_lock); 2625 return s; 2626 } 2627 2628 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode) 2629 { 2630 struct nfsdfs_client *nc; 2631 nc = get_nfsdfs_client(inode); 2632 if (!nc) 2633 return NULL; 2634 return container_of(nc, struct nfs4_client, cl_nfsdfs); 2635 } 2636 2637 static void seq_quote_mem(struct seq_file *m, char *data, int len) 2638 { 2639 seq_puts(m, "\""); 2640 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\"); 2641 seq_puts(m, "\""); 2642 } 2643 2644 static const char *cb_state2str(int state) 2645 { 2646 switch (state) { 2647 case NFSD4_CB_UP: 2648 return "UP"; 2649 case NFSD4_CB_UNKNOWN: 2650 return "UNKNOWN"; 2651 case NFSD4_CB_DOWN: 2652 return "DOWN"; 2653 case NFSD4_CB_FAULT: 2654 return "FAULT"; 2655 } 2656 return "UNDEFINED"; 2657 } 2658 2659 static int client_info_show(struct seq_file *m, void *v) 2660 { 2661 struct inode *inode = file_inode(m->file); 2662 struct nfs4_client *clp; 2663 u64 clid; 2664 2665 clp = get_nfsdfs_clp(inode); 2666 if (!clp) 2667 return -ENXIO; 2668 memcpy(&clid, &clp->cl_clientid, sizeof(clid)); 2669 seq_printf(m, "clientid: 0x%llx\n", clid); 2670 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr); 2671 2672 if (clp->cl_state == NFSD4_COURTESY) 2673 seq_puts(m, "status: courtesy\n"); 2674 else if (clp->cl_state == NFSD4_EXPIRABLE) 2675 seq_puts(m, "status: expirable\n"); 2676 else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) 2677 seq_puts(m, "status: confirmed\n"); 2678 else 2679 seq_puts(m, "status: unconfirmed\n"); 2680 seq_printf(m, "seconds from last renew: %lld\n", 2681 ktime_get_boottime_seconds() - clp->cl_time); 2682 seq_puts(m, "name: "); 2683 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len); 2684 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion); 2685 if (clp->cl_nii_domain.data) { 2686 seq_puts(m, "Implementation domain: "); 2687 seq_quote_mem(m, clp->cl_nii_domain.data, 2688 clp->cl_nii_domain.len); 2689 seq_puts(m, "\nImplementation name: "); 2690 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len); 2691 seq_printf(m, "\nImplementation time: [%lld, %ld]\n", 2692 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec); 2693 } 2694 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state)); 2695 seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr); 2696 seq_printf(m, "admin-revoked states: %d\n", 2697 atomic_read(&clp->cl_admin_revoked)); 2698 drop_client(clp); 2699 2700 return 0; 2701 } 2702 2703 DEFINE_SHOW_ATTRIBUTE(client_info); 2704 2705 static void *states_start(struct seq_file *s, loff_t *pos) 2706 __acquires(&clp->cl_lock) 2707 { 2708 struct nfs4_client *clp = s->private; 2709 unsigned long id = *pos; 2710 void *ret; 2711 2712 spin_lock(&clp->cl_lock); 2713 ret = idr_get_next_ul(&clp->cl_stateids, &id); 2714 *pos = id; 2715 return ret; 2716 } 2717 2718 static void *states_next(struct seq_file *s, void *v, loff_t *pos) 2719 { 2720 struct nfs4_client *clp = s->private; 2721 unsigned long id = *pos; 2722 void *ret; 2723 2724 id = *pos; 2725 id++; 2726 ret = idr_get_next_ul(&clp->cl_stateids, &id); 2727 *pos = id; 2728 return ret; 2729 } 2730 2731 static void states_stop(struct seq_file *s, void *v) 2732 __releases(&clp->cl_lock) 2733 { 2734 struct nfs4_client *clp = s->private; 2735 2736 spin_unlock(&clp->cl_lock); 2737 } 2738 2739 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f) 2740 { 2741 seq_printf(s, "filename: \"%pD2\"", f->nf_file); 2742 } 2743 2744 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f) 2745 { 2746 struct inode *inode = file_inode(f->nf_file); 2747 2748 seq_printf(s, "superblock: \"%02x:%02x:%ld\"", 2749 MAJOR(inode->i_sb->s_dev), 2750 MINOR(inode->i_sb->s_dev), 2751 inode->i_ino); 2752 } 2753 2754 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo) 2755 { 2756 seq_puts(s, "owner: "); 2757 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len); 2758 } 2759 2760 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid) 2761 { 2762 seq_printf(s, "0x%.8x", stid->si_generation); 2763 seq_printf(s, "%12phN", &stid->si_opaque); 2764 } 2765 2766 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st) 2767 { 2768 struct nfs4_ol_stateid *ols; 2769 struct nfs4_file *nf; 2770 struct nfsd_file *file; 2771 struct nfs4_stateowner *oo; 2772 unsigned int access, deny; 2773 2774 ols = openlockstateid(st); 2775 oo = ols->st_stateowner; 2776 nf = st->sc_file; 2777 2778 seq_puts(s, "- "); 2779 nfs4_show_stateid(s, &st->sc_stateid); 2780 seq_puts(s, ": { type: open, "); 2781 2782 access = bmap_to_share_mode(ols->st_access_bmap); 2783 deny = bmap_to_share_mode(ols->st_deny_bmap); 2784 2785 seq_printf(s, "access: %s%s, ", 2786 access & NFS4_SHARE_ACCESS_READ ? "r" : "-", 2787 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); 2788 seq_printf(s, "deny: %s%s, ", 2789 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-", 2790 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); 2791 2792 spin_lock(&nf->fi_lock); 2793 file = find_any_file_locked(nf); 2794 if (file) { 2795 nfs4_show_superblock(s, file); 2796 seq_puts(s, ", "); 2797 nfs4_show_fname(s, file); 2798 seq_puts(s, ", "); 2799 } 2800 spin_unlock(&nf->fi_lock); 2801 nfs4_show_owner(s, oo); 2802 if (st->sc_status & SC_STATUS_ADMIN_REVOKED) 2803 seq_puts(s, ", admin-revoked"); 2804 seq_puts(s, " }\n"); 2805 return 0; 2806 } 2807 2808 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st) 2809 { 2810 struct nfs4_ol_stateid *ols; 2811 struct nfs4_file *nf; 2812 struct nfsd_file *file; 2813 struct nfs4_stateowner *oo; 2814 2815 ols = openlockstateid(st); 2816 oo = ols->st_stateowner; 2817 nf = st->sc_file; 2818 2819 seq_puts(s, "- "); 2820 nfs4_show_stateid(s, &st->sc_stateid); 2821 seq_puts(s, ": { type: lock, "); 2822 2823 spin_lock(&nf->fi_lock); 2824 file = find_any_file_locked(nf); 2825 if (file) { 2826 /* 2827 * Note: a lock stateid isn't really the same thing as a lock, 2828 * it's the locking state held by one owner on a file, and there 2829 * may be multiple (or no) lock ranges associated with it. 2830 * (Same for the matter is true of open stateids.) 2831 */ 2832 2833 nfs4_show_superblock(s, file); 2834 /* XXX: open stateid? */ 2835 seq_puts(s, ", "); 2836 nfs4_show_fname(s, file); 2837 seq_puts(s, ", "); 2838 } 2839 nfs4_show_owner(s, oo); 2840 if (st->sc_status & SC_STATUS_ADMIN_REVOKED) 2841 seq_puts(s, ", admin-revoked"); 2842 seq_puts(s, " }\n"); 2843 spin_unlock(&nf->fi_lock); 2844 return 0; 2845 } 2846 2847 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st) 2848 { 2849 struct nfs4_delegation *ds; 2850 struct nfs4_file *nf; 2851 struct nfsd_file *file; 2852 2853 ds = delegstateid(st); 2854 nf = st->sc_file; 2855 2856 seq_puts(s, "- "); 2857 nfs4_show_stateid(s, &st->sc_stateid); 2858 seq_puts(s, ": { type: deleg, "); 2859 2860 seq_printf(s, "access: %s", 2861 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w"); 2862 2863 /* XXX: lease time, whether it's being recalled. */ 2864 2865 spin_lock(&nf->fi_lock); 2866 file = nf->fi_deleg_file; 2867 if (file) { 2868 seq_puts(s, ", "); 2869 nfs4_show_superblock(s, file); 2870 seq_puts(s, ", "); 2871 nfs4_show_fname(s, file); 2872 } 2873 spin_unlock(&nf->fi_lock); 2874 if (st->sc_status & SC_STATUS_ADMIN_REVOKED) 2875 seq_puts(s, ", admin-revoked"); 2876 seq_puts(s, " }\n"); 2877 return 0; 2878 } 2879 2880 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st) 2881 { 2882 struct nfs4_layout_stateid *ls; 2883 struct nfsd_file *file; 2884 2885 ls = container_of(st, struct nfs4_layout_stateid, ls_stid); 2886 2887 seq_puts(s, "- "); 2888 nfs4_show_stateid(s, &st->sc_stateid); 2889 seq_puts(s, ": { type: layout"); 2890 2891 /* XXX: What else would be useful? */ 2892 2893 spin_lock(&ls->ls_stid.sc_file->fi_lock); 2894 file = ls->ls_file; 2895 if (file) { 2896 seq_puts(s, ", "); 2897 nfs4_show_superblock(s, file); 2898 seq_puts(s, ", "); 2899 nfs4_show_fname(s, file); 2900 } 2901 spin_unlock(&ls->ls_stid.sc_file->fi_lock); 2902 if (st->sc_status & SC_STATUS_ADMIN_REVOKED) 2903 seq_puts(s, ", admin-revoked"); 2904 seq_puts(s, " }\n"); 2905 2906 return 0; 2907 } 2908 2909 static int states_show(struct seq_file *s, void *v) 2910 { 2911 struct nfs4_stid *st = v; 2912 2913 switch (st->sc_type) { 2914 case SC_TYPE_OPEN: 2915 return nfs4_show_open(s, st); 2916 case SC_TYPE_LOCK: 2917 return nfs4_show_lock(s, st); 2918 case SC_TYPE_DELEG: 2919 return nfs4_show_deleg(s, st); 2920 case SC_TYPE_LAYOUT: 2921 return nfs4_show_layout(s, st); 2922 default: 2923 return 0; /* XXX: or SEQ_SKIP? */ 2924 } 2925 /* XXX: copy stateids? */ 2926 } 2927 2928 static struct seq_operations states_seq_ops = { 2929 .start = states_start, 2930 .next = states_next, 2931 .stop = states_stop, 2932 .show = states_show 2933 }; 2934 2935 static int client_states_open(struct inode *inode, struct file *file) 2936 { 2937 struct seq_file *s; 2938 struct nfs4_client *clp; 2939 int ret; 2940 2941 clp = get_nfsdfs_clp(inode); 2942 if (!clp) 2943 return -ENXIO; 2944 2945 ret = seq_open(file, &states_seq_ops); 2946 if (ret) 2947 return ret; 2948 s = file->private_data; 2949 s->private = clp; 2950 return 0; 2951 } 2952 2953 static int client_opens_release(struct inode *inode, struct file *file) 2954 { 2955 struct seq_file *m = file->private_data; 2956 struct nfs4_client *clp = m->private; 2957 2958 /* XXX: alternatively, we could get/drop in seq start/stop */ 2959 drop_client(clp); 2960 return seq_release(inode, file); 2961 } 2962 2963 static const struct file_operations client_states_fops = { 2964 .open = client_states_open, 2965 .read = seq_read, 2966 .llseek = seq_lseek, 2967 .release = client_opens_release, 2968 }; 2969 2970 /* 2971 * Normally we refuse to destroy clients that are in use, but here the 2972 * administrator is telling us to just do it. We also want to wait 2973 * so the caller has a guarantee that the client's locks are gone by 2974 * the time the write returns: 2975 */ 2976 static void force_expire_client(struct nfs4_client *clp) 2977 { 2978 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2979 bool already_expired; 2980 2981 trace_nfsd_clid_admin_expired(&clp->cl_clientid); 2982 2983 spin_lock(&nn->client_lock); 2984 clp->cl_time = 0; 2985 spin_unlock(&nn->client_lock); 2986 2987 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0); 2988 spin_lock(&nn->client_lock); 2989 already_expired = list_empty(&clp->cl_lru); 2990 if (!already_expired) 2991 unhash_client_locked(clp); 2992 spin_unlock(&nn->client_lock); 2993 2994 if (!already_expired) 2995 expire_client(clp); 2996 else 2997 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL); 2998 } 2999 3000 static ssize_t client_ctl_write(struct file *file, const char __user *buf, 3001 size_t size, loff_t *pos) 3002 { 3003 char *data; 3004 struct nfs4_client *clp; 3005 3006 data = simple_transaction_get(file, buf, size); 3007 if (IS_ERR(data)) 3008 return PTR_ERR(data); 3009 if (size != 7 || 0 != memcmp(data, "expire\n", 7)) 3010 return -EINVAL; 3011 clp = get_nfsdfs_clp(file_inode(file)); 3012 if (!clp) 3013 return -ENXIO; 3014 force_expire_client(clp); 3015 drop_client(clp); 3016 return 7; 3017 } 3018 3019 static const struct file_operations client_ctl_fops = { 3020 .write = client_ctl_write, 3021 .release = simple_transaction_release, 3022 }; 3023 3024 static const struct tree_descr client_files[] = { 3025 [0] = {"info", &client_info_fops, S_IRUSR}, 3026 [1] = {"states", &client_states_fops, S_IRUSR}, 3027 [2] = {"ctl", &client_ctl_fops, S_IWUSR}, 3028 [3] = {""}, 3029 }; 3030 3031 static int 3032 nfsd4_cb_recall_any_done(struct nfsd4_callback *cb, 3033 struct rpc_task *task) 3034 { 3035 trace_nfsd_cb_recall_any_done(cb, task); 3036 switch (task->tk_status) { 3037 case -NFS4ERR_DELAY: 3038 rpc_delay(task, 2 * HZ); 3039 return 0; 3040 default: 3041 return 1; 3042 } 3043 } 3044 3045 static void 3046 nfsd4_cb_recall_any_release(struct nfsd4_callback *cb) 3047 { 3048 struct nfs4_client *clp = cb->cb_clp; 3049 3050 clear_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags); 3051 drop_client(clp); 3052 } 3053 3054 static int 3055 nfsd4_cb_getattr_done(struct nfsd4_callback *cb, struct rpc_task *task) 3056 { 3057 struct nfs4_cb_fattr *ncf = 3058 container_of(cb, struct nfs4_cb_fattr, ncf_getattr); 3059 3060 ncf->ncf_cb_status = task->tk_status; 3061 switch (task->tk_status) { 3062 case -NFS4ERR_DELAY: 3063 rpc_delay(task, 2 * HZ); 3064 return 0; 3065 default: 3066 return 1; 3067 } 3068 } 3069 3070 static void 3071 nfsd4_cb_getattr_release(struct nfsd4_callback *cb) 3072 { 3073 struct nfs4_cb_fattr *ncf = 3074 container_of(cb, struct nfs4_cb_fattr, ncf_getattr); 3075 struct nfs4_delegation *dp = 3076 container_of(ncf, struct nfs4_delegation, dl_cb_fattr); 3077 3078 nfs4_put_stid(&dp->dl_stid); 3079 clear_bit(CB_GETATTR_BUSY, &ncf->ncf_cb_flags); 3080 wake_up_bit(&ncf->ncf_cb_flags, CB_GETATTR_BUSY); 3081 } 3082 3083 static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = { 3084 .done = nfsd4_cb_recall_any_done, 3085 .release = nfsd4_cb_recall_any_release, 3086 }; 3087 3088 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops = { 3089 .done = nfsd4_cb_getattr_done, 3090 .release = nfsd4_cb_getattr_release, 3091 }; 3092 3093 static void nfs4_cb_getattr(struct nfs4_cb_fattr *ncf) 3094 { 3095 struct nfs4_delegation *dp = 3096 container_of(ncf, struct nfs4_delegation, dl_cb_fattr); 3097 3098 if (test_and_set_bit(CB_GETATTR_BUSY, &ncf->ncf_cb_flags)) 3099 return; 3100 /* set to proper status when nfsd4_cb_getattr_done runs */ 3101 ncf->ncf_cb_status = NFS4ERR_IO; 3102 3103 refcount_inc(&dp->dl_stid.sc_count); 3104 nfsd4_run_cb(&ncf->ncf_getattr); 3105 } 3106 3107 static struct nfs4_client *create_client(struct xdr_netobj name, 3108 struct svc_rqst *rqstp, nfs4_verifier *verf) 3109 { 3110 struct nfs4_client *clp; 3111 struct sockaddr *sa = svc_addr(rqstp); 3112 int ret; 3113 struct net *net = SVC_NET(rqstp); 3114 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 3115 struct dentry *dentries[ARRAY_SIZE(client_files)]; 3116 3117 clp = alloc_client(name, nn); 3118 if (clp == NULL) 3119 return NULL; 3120 3121 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); 3122 if (ret) { 3123 free_client(clp); 3124 return NULL; 3125 } 3126 gen_clid(clp, nn); 3127 kref_init(&clp->cl_nfsdfs.cl_ref); 3128 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL); 3129 clp->cl_time = ktime_get_boottime_seconds(); 3130 clear_bit(0, &clp->cl_cb_slot_busy); 3131 copy_verf(clp, verf); 3132 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage)); 3133 clp->cl_cb_session = NULL; 3134 clp->net = net; 3135 clp->cl_nfsd_dentry = nfsd_client_mkdir( 3136 nn, &clp->cl_nfsdfs, 3137 clp->cl_clientid.cl_id - nn->clientid_base, 3138 client_files, dentries); 3139 clp->cl_nfsd_info_dentry = dentries[0]; 3140 if (!clp->cl_nfsd_dentry) { 3141 free_client(clp); 3142 return NULL; 3143 } 3144 clp->cl_ra = kzalloc(sizeof(*clp->cl_ra), GFP_KERNEL); 3145 if (!clp->cl_ra) { 3146 free_client(clp); 3147 return NULL; 3148 } 3149 clp->cl_ra_time = 0; 3150 nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops, 3151 NFSPROC4_CLNT_CB_RECALL_ANY); 3152 return clp; 3153 } 3154 3155 static void 3156 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) 3157 { 3158 struct rb_node **new = &(root->rb_node), *parent = NULL; 3159 struct nfs4_client *clp; 3160 3161 while (*new) { 3162 clp = rb_entry(*new, struct nfs4_client, cl_namenode); 3163 parent = *new; 3164 3165 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) 3166 new = &((*new)->rb_left); 3167 else 3168 new = &((*new)->rb_right); 3169 } 3170 3171 rb_link_node(&new_clp->cl_namenode, parent, new); 3172 rb_insert_color(&new_clp->cl_namenode, root); 3173 } 3174 3175 static struct nfs4_client * 3176 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) 3177 { 3178 int cmp; 3179 struct rb_node *node = root->rb_node; 3180 struct nfs4_client *clp; 3181 3182 while (node) { 3183 clp = rb_entry(node, struct nfs4_client, cl_namenode); 3184 cmp = compare_blob(&clp->cl_name, name); 3185 if (cmp > 0) 3186 node = node->rb_left; 3187 else if (cmp < 0) 3188 node = node->rb_right; 3189 else 3190 return clp; 3191 } 3192 return NULL; 3193 } 3194 3195 static void 3196 add_to_unconfirmed(struct nfs4_client *clp) 3197 { 3198 unsigned int idhashval; 3199 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 3200 3201 lockdep_assert_held(&nn->client_lock); 3202 3203 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); 3204 add_clp_to_name_tree(clp, &nn->unconf_name_tree); 3205 idhashval = clientid_hashval(clp->cl_clientid.cl_id); 3206 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); 3207 renew_client_locked(clp); 3208 } 3209 3210 static void 3211 move_to_confirmed(struct nfs4_client *clp) 3212 { 3213 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); 3214 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 3215 3216 lockdep_assert_held(&nn->client_lock); 3217 3218 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); 3219 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); 3220 add_clp_to_name_tree(clp, &nn->conf_name_tree); 3221 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); 3222 trace_nfsd_clid_confirmed(&clp->cl_clientid); 3223 renew_client_locked(clp); 3224 } 3225 3226 static struct nfs4_client * 3227 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions) 3228 { 3229 struct nfs4_client *clp; 3230 unsigned int idhashval = clientid_hashval(clid->cl_id); 3231 3232 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { 3233 if (same_clid(&clp->cl_clientid, clid)) { 3234 if ((bool)clp->cl_minorversion != sessions) 3235 return NULL; 3236 renew_client_locked(clp); 3237 return clp; 3238 } 3239 } 3240 return NULL; 3241 } 3242 3243 static struct nfs4_client * 3244 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) 3245 { 3246 struct list_head *tbl = nn->conf_id_hashtbl; 3247 3248 lockdep_assert_held(&nn->client_lock); 3249 return find_client_in_id_table(tbl, clid, sessions); 3250 } 3251 3252 static struct nfs4_client * 3253 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) 3254 { 3255 struct list_head *tbl = nn->unconf_id_hashtbl; 3256 3257 lockdep_assert_held(&nn->client_lock); 3258 return find_client_in_id_table(tbl, clid, sessions); 3259 } 3260 3261 static bool clp_used_exchangeid(struct nfs4_client *clp) 3262 { 3263 return clp->cl_exchange_flags != 0; 3264 } 3265 3266 static struct nfs4_client * 3267 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) 3268 { 3269 lockdep_assert_held(&nn->client_lock); 3270 return find_clp_in_name_tree(name, &nn->conf_name_tree); 3271 } 3272 3273 static struct nfs4_client * 3274 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) 3275 { 3276 lockdep_assert_held(&nn->client_lock); 3277 return find_clp_in_name_tree(name, &nn->unconf_name_tree); 3278 } 3279 3280 static void 3281 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp) 3282 { 3283 struct nfs4_cb_conn *conn = &clp->cl_cb_conn; 3284 struct sockaddr *sa = svc_addr(rqstp); 3285 u32 scopeid = rpc_get_scope_id(sa); 3286 unsigned short expected_family; 3287 3288 /* Currently, we only support tcp and tcp6 for the callback channel */ 3289 if (se->se_callback_netid_len == 3 && 3290 !memcmp(se->se_callback_netid_val, "tcp", 3)) 3291 expected_family = AF_INET; 3292 else if (se->se_callback_netid_len == 4 && 3293 !memcmp(se->se_callback_netid_val, "tcp6", 4)) 3294 expected_family = AF_INET6; 3295 else 3296 goto out_err; 3297 3298 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, 3299 se->se_callback_addr_len, 3300 (struct sockaddr *)&conn->cb_addr, 3301 sizeof(conn->cb_addr)); 3302 3303 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) 3304 goto out_err; 3305 3306 if (conn->cb_addr.ss_family == AF_INET6) 3307 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; 3308 3309 conn->cb_prog = se->se_callback_prog; 3310 conn->cb_ident = se->se_callback_ident; 3311 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); 3312 trace_nfsd_cb_args(clp, conn); 3313 return; 3314 out_err: 3315 conn->cb_addr.ss_family = AF_UNSPEC; 3316 conn->cb_addrlen = 0; 3317 trace_nfsd_cb_nodelegs(clp); 3318 return; 3319 } 3320 3321 /* 3322 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. 3323 */ 3324 static void 3325 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) 3326 { 3327 struct xdr_buf *buf = resp->xdr->buf; 3328 struct nfsd4_slot *slot = resp->cstate.slot; 3329 unsigned int base; 3330 3331 dprintk("--> %s slot %p\n", __func__, slot); 3332 3333 slot->sl_flags |= NFSD4_SLOT_INITIALIZED; 3334 slot->sl_opcnt = resp->opcnt; 3335 slot->sl_status = resp->cstate.status; 3336 free_svc_cred(&slot->sl_cred); 3337 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred); 3338 3339 if (!nfsd4_cache_this(resp)) { 3340 slot->sl_flags &= ~NFSD4_SLOT_CACHED; 3341 return; 3342 } 3343 slot->sl_flags |= NFSD4_SLOT_CACHED; 3344 3345 base = resp->cstate.data_offset; 3346 slot->sl_datalen = buf->len - base; 3347 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen)) 3348 WARN(1, "%s: sessions DRC could not cache compound\n", 3349 __func__); 3350 return; 3351 } 3352 3353 /* 3354 * Encode the replay sequence operation from the slot values. 3355 * If cachethis is FALSE encode the uncached rep error on the next 3356 * operation which sets resp->p and increments resp->opcnt for 3357 * nfs4svc_encode_compoundres. 3358 * 3359 */ 3360 static __be32 3361 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, 3362 struct nfsd4_compoundres *resp) 3363 { 3364 struct nfsd4_op *op; 3365 struct nfsd4_slot *slot = resp->cstate.slot; 3366 3367 /* Encode the replayed sequence operation */ 3368 op = &args->ops[resp->opcnt - 1]; 3369 nfsd4_encode_operation(resp, op); 3370 3371 if (slot->sl_flags & NFSD4_SLOT_CACHED) 3372 return op->status; 3373 if (args->opcnt == 1) { 3374 /* 3375 * The original operation wasn't a solo sequence--we 3376 * always cache those--so this retry must not match the 3377 * original: 3378 */ 3379 op->status = nfserr_seq_false_retry; 3380 } else { 3381 op = &args->ops[resp->opcnt++]; 3382 op->status = nfserr_retry_uncached_rep; 3383 nfsd4_encode_operation(resp, op); 3384 } 3385 return op->status; 3386 } 3387 3388 /* 3389 * The sequence operation is not cached because we can use the slot and 3390 * session values. 3391 */ 3392 static __be32 3393 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, 3394 struct nfsd4_sequence *seq) 3395 { 3396 struct nfsd4_slot *slot = resp->cstate.slot; 3397 struct xdr_stream *xdr = resp->xdr; 3398 __be32 *p; 3399 __be32 status; 3400 3401 dprintk("--> %s slot %p\n", __func__, slot); 3402 3403 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); 3404 if (status) 3405 return status; 3406 3407 p = xdr_reserve_space(xdr, slot->sl_datalen); 3408 if (!p) { 3409 WARN_ON_ONCE(1); 3410 return nfserr_serverfault; 3411 } 3412 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); 3413 xdr_commit_encode(xdr); 3414 3415 resp->opcnt = slot->sl_opcnt; 3416 return slot->sl_status; 3417 } 3418 3419 /* 3420 * Set the exchange_id flags returned by the server. 3421 */ 3422 static void 3423 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) 3424 { 3425 #ifdef CONFIG_NFSD_PNFS 3426 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; 3427 #else 3428 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; 3429 #endif 3430 3431 /* Referrals are supported, Migration is not. */ 3432 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; 3433 3434 /* set the wire flags to return to client. */ 3435 clid->flags = new->cl_exchange_flags; 3436 } 3437 3438 static bool client_has_openowners(struct nfs4_client *clp) 3439 { 3440 struct nfs4_openowner *oo; 3441 3442 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { 3443 if (!list_empty(&oo->oo_owner.so_stateids)) 3444 return true; 3445 } 3446 return false; 3447 } 3448 3449 static bool client_has_state(struct nfs4_client *clp) 3450 { 3451 return client_has_openowners(clp) 3452 #ifdef CONFIG_NFSD_PNFS 3453 || !list_empty(&clp->cl_lo_states) 3454 #endif 3455 || !list_empty(&clp->cl_delegations) 3456 || !list_empty(&clp->cl_sessions) 3457 || !list_empty(&clp->async_copies); 3458 } 3459 3460 static __be32 copy_impl_id(struct nfs4_client *clp, 3461 struct nfsd4_exchange_id *exid) 3462 { 3463 if (!exid->nii_domain.data) 3464 return 0; 3465 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL); 3466 if (!clp->cl_nii_domain.data) 3467 return nfserr_jukebox; 3468 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL); 3469 if (!clp->cl_nii_name.data) 3470 return nfserr_jukebox; 3471 clp->cl_nii_time = exid->nii_time; 3472 return 0; 3473 } 3474 3475 __be32 3476 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 3477 union nfsd4_op_u *u) 3478 { 3479 struct nfsd4_exchange_id *exid = &u->exchange_id; 3480 struct nfs4_client *conf, *new; 3481 struct nfs4_client *unconf = NULL; 3482 __be32 status; 3483 char addr_str[INET6_ADDRSTRLEN]; 3484 nfs4_verifier verf = exid->verifier; 3485 struct sockaddr *sa = svc_addr(rqstp); 3486 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; 3487 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 3488 3489 rpc_ntop(sa, addr_str, sizeof(addr_str)); 3490 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " 3491 "ip_addr=%s flags %x, spa_how %u\n", 3492 __func__, rqstp, exid, exid->clname.len, exid->clname.data, 3493 addr_str, exid->flags, exid->spa_how); 3494 3495 if (exid->flags & ~EXCHGID4_FLAG_MASK_A) 3496 return nfserr_inval; 3497 3498 new = create_client(exid->clname, rqstp, &verf); 3499 if (new == NULL) 3500 return nfserr_jukebox; 3501 status = copy_impl_id(new, exid); 3502 if (status) 3503 goto out_nolock; 3504 3505 switch (exid->spa_how) { 3506 case SP4_MACH_CRED: 3507 exid->spo_must_enforce[0] = 0; 3508 exid->spo_must_enforce[1] = ( 3509 1 << (OP_BIND_CONN_TO_SESSION - 32) | 3510 1 << (OP_EXCHANGE_ID - 32) | 3511 1 << (OP_CREATE_SESSION - 32) | 3512 1 << (OP_DESTROY_SESSION - 32) | 3513 1 << (OP_DESTROY_CLIENTID - 32)); 3514 3515 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | 3516 1 << (OP_OPEN_DOWNGRADE) | 3517 1 << (OP_LOCKU) | 3518 1 << (OP_DELEGRETURN)); 3519 3520 exid->spo_must_allow[1] &= ( 3521 1 << (OP_TEST_STATEID - 32) | 3522 1 << (OP_FREE_STATEID - 32)); 3523 if (!svc_rqst_integrity_protected(rqstp)) { 3524 status = nfserr_inval; 3525 goto out_nolock; 3526 } 3527 /* 3528 * Sometimes userspace doesn't give us a principal. 3529 * Which is a bug, really. Anyway, we can't enforce 3530 * MACH_CRED in that case, better to give up now: 3531 */ 3532 if (!new->cl_cred.cr_principal && 3533 !new->cl_cred.cr_raw_principal) { 3534 status = nfserr_serverfault; 3535 goto out_nolock; 3536 } 3537 new->cl_mach_cred = true; 3538 break; 3539 case SP4_NONE: 3540 break; 3541 default: /* checked by xdr code */ 3542 WARN_ON_ONCE(1); 3543 fallthrough; 3544 case SP4_SSV: 3545 status = nfserr_encr_alg_unsupp; 3546 goto out_nolock; 3547 } 3548 3549 /* Cases below refer to rfc 5661 section 18.35.4: */ 3550 spin_lock(&nn->client_lock); 3551 conf = find_confirmed_client_by_name(&exid->clname, nn); 3552 if (conf) { 3553 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); 3554 bool verfs_match = same_verf(&verf, &conf->cl_verifier); 3555 3556 if (update) { 3557 if (!clp_used_exchangeid(conf)) { /* buggy client */ 3558 status = nfserr_inval; 3559 goto out; 3560 } 3561 if (!nfsd4_mach_creds_match(conf, rqstp)) { 3562 status = nfserr_wrong_cred; 3563 goto out; 3564 } 3565 if (!creds_match) { /* case 9 */ 3566 status = nfserr_perm; 3567 goto out; 3568 } 3569 if (!verfs_match) { /* case 8 */ 3570 status = nfserr_not_same; 3571 goto out; 3572 } 3573 /* case 6 */ 3574 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; 3575 trace_nfsd_clid_confirmed_r(conf); 3576 goto out_copy; 3577 } 3578 if (!creds_match) { /* case 3 */ 3579 if (client_has_state(conf)) { 3580 status = nfserr_clid_inuse; 3581 trace_nfsd_clid_cred_mismatch(conf, rqstp); 3582 goto out; 3583 } 3584 goto out_new; 3585 } 3586 if (verfs_match) { /* case 2 */ 3587 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; 3588 trace_nfsd_clid_confirmed_r(conf); 3589 goto out_copy; 3590 } 3591 /* case 5, client reboot */ 3592 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf); 3593 conf = NULL; 3594 goto out_new; 3595 } 3596 3597 if (update) { /* case 7 */ 3598 status = nfserr_noent; 3599 goto out; 3600 } 3601 3602 unconf = find_unconfirmed_client_by_name(&exid->clname, nn); 3603 if (unconf) /* case 4, possible retry or client restart */ 3604 unhash_client_locked(unconf); 3605 3606 /* case 1, new owner ID */ 3607 trace_nfsd_clid_fresh(new); 3608 3609 out_new: 3610 if (conf) { 3611 status = mark_client_expired_locked(conf); 3612 if (status) 3613 goto out; 3614 trace_nfsd_clid_replaced(&conf->cl_clientid); 3615 } 3616 new->cl_minorversion = cstate->minorversion; 3617 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; 3618 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1]; 3619 3620 /* Contrived initial CREATE_SESSION response */ 3621 new->cl_cs_slot.sl_status = nfserr_seq_misordered; 3622 3623 add_to_unconfirmed(new); 3624 swap(new, conf); 3625 out_copy: 3626 exid->clientid.cl_boot = conf->cl_clientid.cl_boot; 3627 exid->clientid.cl_id = conf->cl_clientid.cl_id; 3628 3629 exid->seqid = conf->cl_cs_slot.sl_seqid + 1; 3630 nfsd4_set_ex_flags(conf, exid); 3631 3632 dprintk("nfsd4_exchange_id seqid %d flags %x\n", 3633 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); 3634 status = nfs_ok; 3635 3636 out: 3637 spin_unlock(&nn->client_lock); 3638 out_nolock: 3639 if (new) 3640 expire_client(new); 3641 if (unconf) { 3642 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid); 3643 expire_client(unconf); 3644 } 3645 return status; 3646 } 3647 3648 static __be32 check_slot_seqid(u32 seqid, u32 slot_seqid, bool slot_inuse) 3649 { 3650 /* The slot is in use, and no response has been sent. */ 3651 if (slot_inuse) { 3652 if (seqid == slot_seqid) 3653 return nfserr_jukebox; 3654 else 3655 return nfserr_seq_misordered; 3656 } 3657 /* Note unsigned 32-bit arithmetic handles wraparound: */ 3658 if (likely(seqid == slot_seqid + 1)) 3659 return nfs_ok; 3660 if (seqid == slot_seqid) 3661 return nfserr_replay_cache; 3662 return nfserr_seq_misordered; 3663 } 3664 3665 /* 3666 * Cache the create session result into the create session single DRC 3667 * slot cache by saving the xdr structure. sl_seqid has been set. 3668 * Do this for solo or embedded create session operations. 3669 */ 3670 static void 3671 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, 3672 struct nfsd4_clid_slot *slot, __be32 nfserr) 3673 { 3674 slot->sl_status = nfserr; 3675 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); 3676 } 3677 3678 static __be32 3679 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, 3680 struct nfsd4_clid_slot *slot) 3681 { 3682 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); 3683 return slot->sl_status; 3684 } 3685 3686 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ 3687 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ 3688 1 + /* MIN tag is length with zero, only length */ \ 3689 3 + /* version, opcount, opcode */ \ 3690 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ 3691 /* seqid, slotID, slotID, cache */ \ 3692 4 ) * sizeof(__be32)) 3693 3694 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ 3695 2 + /* verifier: AUTH_NULL, length 0 */\ 3696 1 + /* status */ \ 3697 1 + /* MIN tag is length with zero, only length */ \ 3698 3 + /* opcount, opcode, opstatus*/ \ 3699 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ 3700 /* seqid, slotID, slotID, slotID, status */ \ 3701 5 ) * sizeof(__be32)) 3702 3703 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) 3704 { 3705 u32 maxrpc = nn->nfsd_serv->sv_max_mesg; 3706 3707 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) 3708 return nfserr_toosmall; 3709 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) 3710 return nfserr_toosmall; 3711 ca->headerpadsz = 0; 3712 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); 3713 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); 3714 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); 3715 ca->maxresp_cached = min_t(u32, ca->maxresp_cached, 3716 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); 3717 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); 3718 /* 3719 * Note decreasing slot size below client's request may make it 3720 * difficult for client to function correctly, whereas 3721 * decreasing the number of slots will (just?) affect 3722 * performance. When short on memory we therefore prefer to 3723 * decrease number of slots instead of their size. Clients that 3724 * request larger slots than they need will get poor results: 3725 * Note that we always allow at least one slot, because our 3726 * accounting is soft and provides no guarantees either way. 3727 */ 3728 ca->maxreqs = nfsd4_get_drc_mem(ca, nn); 3729 3730 return nfs_ok; 3731 } 3732 3733 /* 3734 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. 3735 * These are based on similar macros in linux/sunrpc/msg_prot.h . 3736 */ 3737 #define RPC_MAX_HEADER_WITH_AUTH_SYS \ 3738 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) 3739 3740 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ 3741 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) 3742 3743 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ 3744 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) 3745 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ 3746 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ 3747 sizeof(__be32)) 3748 3749 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) 3750 { 3751 ca->headerpadsz = 0; 3752 3753 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) 3754 return nfserr_toosmall; 3755 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) 3756 return nfserr_toosmall; 3757 ca->maxresp_cached = 0; 3758 if (ca->maxops < 2) 3759 return nfserr_toosmall; 3760 3761 return nfs_ok; 3762 } 3763 3764 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) 3765 { 3766 switch (cbs->flavor) { 3767 case RPC_AUTH_NULL: 3768 case RPC_AUTH_UNIX: 3769 return nfs_ok; 3770 default: 3771 /* 3772 * GSS case: the spec doesn't allow us to return this 3773 * error. But it also doesn't allow us not to support 3774 * GSS. 3775 * I'd rather this fail hard than return some error the 3776 * client might think it can already handle: 3777 */ 3778 return nfserr_encr_alg_unsupp; 3779 } 3780 } 3781 3782 __be32 3783 nfsd4_create_session(struct svc_rqst *rqstp, 3784 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 3785 { 3786 struct nfsd4_create_session *cr_ses = &u->create_session; 3787 struct sockaddr *sa = svc_addr(rqstp); 3788 struct nfs4_client *conf, *unconf; 3789 struct nfsd4_clid_slot *cs_slot; 3790 struct nfs4_client *old = NULL; 3791 struct nfsd4_session *new; 3792 struct nfsd4_conn *conn; 3793 __be32 status = 0; 3794 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 3795 3796 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) 3797 return nfserr_inval; 3798 status = nfsd4_check_cb_sec(&cr_ses->cb_sec); 3799 if (status) 3800 return status; 3801 status = check_forechannel_attrs(&cr_ses->fore_channel, nn); 3802 if (status) 3803 return status; 3804 status = check_backchannel_attrs(&cr_ses->back_channel); 3805 if (status) 3806 goto out_release_drc_mem; 3807 status = nfserr_jukebox; 3808 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); 3809 if (!new) 3810 goto out_release_drc_mem; 3811 conn = alloc_conn_from_crses(rqstp, cr_ses); 3812 if (!conn) 3813 goto out_free_session; 3814 3815 spin_lock(&nn->client_lock); 3816 3817 /* RFC 8881 Section 18.36.4 Phase 1: Client record look-up. */ 3818 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); 3819 conf = find_confirmed_client(&cr_ses->clientid, true, nn); 3820 if (!conf && !unconf) { 3821 status = nfserr_stale_clientid; 3822 goto out_free_conn; 3823 } 3824 3825 /* RFC 8881 Section 18.36.4 Phase 2: Sequence ID processing. */ 3826 if (conf) { 3827 cs_slot = &conf->cl_cs_slot; 3828 trace_nfsd_slot_seqid_conf(conf, cr_ses); 3829 } else { 3830 cs_slot = &unconf->cl_cs_slot; 3831 trace_nfsd_slot_seqid_unconf(unconf, cr_ses); 3832 } 3833 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); 3834 switch (status) { 3835 case nfs_ok: 3836 cs_slot->sl_seqid++; 3837 cr_ses->seqid = cs_slot->sl_seqid; 3838 break; 3839 case nfserr_replay_cache: 3840 status = nfsd4_replay_create_session(cr_ses, cs_slot); 3841 fallthrough; 3842 case nfserr_jukebox: 3843 /* The server MUST NOT cache NFS4ERR_DELAY */ 3844 goto out_free_conn; 3845 default: 3846 goto out_cache_error; 3847 } 3848 3849 /* RFC 8881 Section 18.36.4 Phase 3: Client ID confirmation. */ 3850 if (conf) { 3851 status = nfserr_wrong_cred; 3852 if (!nfsd4_mach_creds_match(conf, rqstp)) 3853 goto out_cache_error; 3854 } else { 3855 status = nfserr_clid_inuse; 3856 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || 3857 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { 3858 trace_nfsd_clid_cred_mismatch(unconf, rqstp); 3859 goto out_cache_error; 3860 } 3861 status = nfserr_wrong_cred; 3862 if (!nfsd4_mach_creds_match(unconf, rqstp)) 3863 goto out_cache_error; 3864 old = find_confirmed_client_by_name(&unconf->cl_name, nn); 3865 if (old) { 3866 status = mark_client_expired_locked(old); 3867 if (status) 3868 goto out_expired_error; 3869 trace_nfsd_clid_replaced(&old->cl_clientid); 3870 } 3871 move_to_confirmed(unconf); 3872 conf = unconf; 3873 } 3874 3875 /* RFC 8881 Section 18.36.4 Phase 4: Session creation. */ 3876 status = nfs_ok; 3877 /* Persistent sessions are not supported */ 3878 cr_ses->flags &= ~SESSION4_PERSIST; 3879 /* Upshifting from TCP to RDMA is not supported */ 3880 cr_ses->flags &= ~SESSION4_RDMA; 3881 3882 init_session(rqstp, new, conf, cr_ses); 3883 nfsd4_get_session_locked(new); 3884 3885 memcpy(cr_ses->sessionid.data, new->se_sessionid.data, 3886 NFS4_MAX_SESSIONID_LEN); 3887 3888 /* cache solo and embedded create sessions under the client_lock */ 3889 nfsd4_cache_create_session(cr_ses, cs_slot, status); 3890 spin_unlock(&nn->client_lock); 3891 if (conf == unconf) 3892 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY); 3893 /* init connection and backchannel */ 3894 nfsd4_init_conn(rqstp, conn, new); 3895 nfsd4_put_session(new); 3896 if (old) 3897 expire_client(old); 3898 return status; 3899 3900 out_expired_error: 3901 old = NULL; 3902 /* 3903 * Revert the slot seq_nr change so the server will process 3904 * the client's resend instead of returning a cached response. 3905 */ 3906 if (status == nfserr_jukebox) { 3907 cs_slot->sl_seqid--; 3908 cr_ses->seqid = cs_slot->sl_seqid; 3909 goto out_free_conn; 3910 } 3911 out_cache_error: 3912 nfsd4_cache_create_session(cr_ses, cs_slot, status); 3913 out_free_conn: 3914 spin_unlock(&nn->client_lock); 3915 free_conn(conn); 3916 if (old) 3917 expire_client(old); 3918 out_free_session: 3919 __free_session(new); 3920 out_release_drc_mem: 3921 nfsd4_put_drc_mem(&cr_ses->fore_channel); 3922 return status; 3923 } 3924 3925 static __be32 nfsd4_map_bcts_dir(u32 *dir) 3926 { 3927 switch (*dir) { 3928 case NFS4_CDFC4_FORE: 3929 case NFS4_CDFC4_BACK: 3930 return nfs_ok; 3931 case NFS4_CDFC4_FORE_OR_BOTH: 3932 case NFS4_CDFC4_BACK_OR_BOTH: 3933 *dir = NFS4_CDFC4_BOTH; 3934 return nfs_ok; 3935 } 3936 return nfserr_inval; 3937 } 3938 3939 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, 3940 struct nfsd4_compound_state *cstate, 3941 union nfsd4_op_u *u) 3942 { 3943 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl; 3944 struct nfsd4_session *session = cstate->session; 3945 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 3946 __be32 status; 3947 3948 status = nfsd4_check_cb_sec(&bc->bc_cb_sec); 3949 if (status) 3950 return status; 3951 spin_lock(&nn->client_lock); 3952 session->se_cb_prog = bc->bc_cb_program; 3953 session->se_cb_sec = bc->bc_cb_sec; 3954 spin_unlock(&nn->client_lock); 3955 3956 nfsd4_probe_callback(session->se_client); 3957 3958 return nfs_ok; 3959 } 3960 3961 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) 3962 { 3963 struct nfsd4_conn *c; 3964 3965 list_for_each_entry(c, &s->se_conns, cn_persession) { 3966 if (c->cn_xprt == xpt) { 3967 return c; 3968 } 3969 } 3970 return NULL; 3971 } 3972 3973 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst, 3974 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn) 3975 { 3976 struct nfs4_client *clp = session->se_client; 3977 struct svc_xprt *xpt = rqst->rq_xprt; 3978 struct nfsd4_conn *c; 3979 __be32 status; 3980 3981 /* Following the last paragraph of RFC 5661 Section 18.34.3: */ 3982 spin_lock(&clp->cl_lock); 3983 c = __nfsd4_find_conn(xpt, session); 3984 if (!c) 3985 status = nfserr_noent; 3986 else if (req == c->cn_flags) 3987 status = nfs_ok; 3988 else if (req == NFS4_CDFC4_FORE_OR_BOTH && 3989 c->cn_flags != NFS4_CDFC4_BACK) 3990 status = nfs_ok; 3991 else if (req == NFS4_CDFC4_BACK_OR_BOTH && 3992 c->cn_flags != NFS4_CDFC4_FORE) 3993 status = nfs_ok; 3994 else 3995 status = nfserr_inval; 3996 spin_unlock(&clp->cl_lock); 3997 if (status == nfs_ok && conn) 3998 *conn = c; 3999 return status; 4000 } 4001 4002 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, 4003 struct nfsd4_compound_state *cstate, 4004 union nfsd4_op_u *u) 4005 { 4006 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session; 4007 __be32 status; 4008 struct nfsd4_conn *conn; 4009 struct nfsd4_session *session; 4010 struct net *net = SVC_NET(rqstp); 4011 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 4012 4013 if (!nfsd4_last_compound_op(rqstp)) 4014 return nfserr_not_only_op; 4015 spin_lock(&nn->client_lock); 4016 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); 4017 spin_unlock(&nn->client_lock); 4018 if (!session) 4019 goto out_no_session; 4020 status = nfserr_wrong_cred; 4021 if (!nfsd4_mach_creds_match(session->se_client, rqstp)) 4022 goto out; 4023 status = nfsd4_match_existing_connection(rqstp, session, 4024 bcts->dir, &conn); 4025 if (status == nfs_ok) { 4026 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH || 4027 bcts->dir == NFS4_CDFC4_BACK) 4028 conn->cn_flags |= NFS4_CDFC4_BACK; 4029 nfsd4_probe_callback(session->se_client); 4030 goto out; 4031 } 4032 if (status == nfserr_inval) 4033 goto out; 4034 status = nfsd4_map_bcts_dir(&bcts->dir); 4035 if (status) 4036 goto out; 4037 conn = alloc_conn(rqstp, bcts->dir); 4038 status = nfserr_jukebox; 4039 if (!conn) 4040 goto out; 4041 nfsd4_init_conn(rqstp, conn, session); 4042 status = nfs_ok; 4043 out: 4044 nfsd4_put_session(session); 4045 out_no_session: 4046 return status; 4047 } 4048 4049 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid) 4050 { 4051 if (!cstate->session) 4052 return false; 4053 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid)); 4054 } 4055 4056 __be32 4057 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate, 4058 union nfsd4_op_u *u) 4059 { 4060 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid; 4061 struct nfsd4_session *ses; 4062 __be32 status; 4063 int ref_held_by_me = 0; 4064 struct net *net = SVC_NET(r); 4065 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 4066 4067 status = nfserr_not_only_op; 4068 if (nfsd4_compound_in_session(cstate, sessionid)) { 4069 if (!nfsd4_last_compound_op(r)) 4070 goto out; 4071 ref_held_by_me++; 4072 } 4073 dump_sessionid(__func__, sessionid); 4074 spin_lock(&nn->client_lock); 4075 ses = find_in_sessionid_hashtbl(sessionid, net, &status); 4076 if (!ses) 4077 goto out_client_lock; 4078 status = nfserr_wrong_cred; 4079 if (!nfsd4_mach_creds_match(ses->se_client, r)) 4080 goto out_put_session; 4081 status = mark_session_dead_locked(ses, 1 + ref_held_by_me); 4082 if (status) 4083 goto out_put_session; 4084 unhash_session(ses); 4085 spin_unlock(&nn->client_lock); 4086 4087 nfsd4_probe_callback_sync(ses->se_client); 4088 4089 spin_lock(&nn->client_lock); 4090 status = nfs_ok; 4091 out_put_session: 4092 nfsd4_put_session_locked(ses); 4093 out_client_lock: 4094 spin_unlock(&nn->client_lock); 4095 out: 4096 return status; 4097 } 4098 4099 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses) 4100 { 4101 struct nfs4_client *clp = ses->se_client; 4102 struct nfsd4_conn *c; 4103 __be32 status = nfs_ok; 4104 int ret; 4105 4106 spin_lock(&clp->cl_lock); 4107 c = __nfsd4_find_conn(new->cn_xprt, ses); 4108 if (c) 4109 goto out_free; 4110 status = nfserr_conn_not_bound_to_session; 4111 if (clp->cl_mach_cred) 4112 goto out_free; 4113 __nfsd4_hash_conn(new, ses); 4114 spin_unlock(&clp->cl_lock); 4115 ret = nfsd4_register_conn(new); 4116 if (ret) 4117 /* oops; xprt is already down: */ 4118 nfsd4_conn_lost(&new->cn_xpt_user); 4119 return nfs_ok; 4120 out_free: 4121 spin_unlock(&clp->cl_lock); 4122 free_conn(new); 4123 return status; 4124 } 4125 4126 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) 4127 { 4128 struct nfsd4_compoundargs *args = rqstp->rq_argp; 4129 4130 return args->opcnt > session->se_fchannel.maxops; 4131 } 4132 4133 static bool nfsd4_request_too_big(struct svc_rqst *rqstp, 4134 struct nfsd4_session *session) 4135 { 4136 struct xdr_buf *xb = &rqstp->rq_arg; 4137 4138 return xb->len > session->se_fchannel.maxreq_sz; 4139 } 4140 4141 static bool replay_matches_cache(struct svc_rqst *rqstp, 4142 struct nfsd4_sequence *seq, struct nfsd4_slot *slot) 4143 { 4144 struct nfsd4_compoundargs *argp = rqstp->rq_argp; 4145 4146 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) != 4147 (bool)seq->cachethis) 4148 return false; 4149 /* 4150 * If there's an error then the reply can have fewer ops than 4151 * the call. 4152 */ 4153 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status) 4154 return false; 4155 /* 4156 * But if we cached a reply with *more* ops than the call you're 4157 * sending us now, then this new call is clearly not really a 4158 * replay of the old one: 4159 */ 4160 if (slot->sl_opcnt > argp->opcnt) 4161 return false; 4162 /* This is the only check explicitly called by spec: */ 4163 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred)) 4164 return false; 4165 /* 4166 * There may be more comparisons we could actually do, but the 4167 * spec doesn't require us to catch every case where the calls 4168 * don't match (that would require caching the call as well as 4169 * the reply), so we don't bother. 4170 */ 4171 return true; 4172 } 4173 4174 __be32 4175 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 4176 union nfsd4_op_u *u) 4177 { 4178 struct nfsd4_sequence *seq = &u->sequence; 4179 struct nfsd4_compoundres *resp = rqstp->rq_resp; 4180 struct xdr_stream *xdr = resp->xdr; 4181 struct nfsd4_session *session; 4182 struct nfs4_client *clp; 4183 struct nfsd4_slot *slot; 4184 struct nfsd4_conn *conn; 4185 __be32 status; 4186 int buflen; 4187 struct net *net = SVC_NET(rqstp); 4188 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 4189 4190 if (resp->opcnt != 1) 4191 return nfserr_sequence_pos; 4192 4193 /* 4194 * Will be either used or freed by nfsd4_sequence_check_conn 4195 * below. 4196 */ 4197 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); 4198 if (!conn) 4199 return nfserr_jukebox; 4200 4201 spin_lock(&nn->client_lock); 4202 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); 4203 if (!session) 4204 goto out_no_session; 4205 clp = session->se_client; 4206 4207 status = nfserr_too_many_ops; 4208 if (nfsd4_session_too_many_ops(rqstp, session)) 4209 goto out_put_session; 4210 4211 status = nfserr_req_too_big; 4212 if (nfsd4_request_too_big(rqstp, session)) 4213 goto out_put_session; 4214 4215 status = nfserr_badslot; 4216 if (seq->slotid >= session->se_fchannel.maxreqs) 4217 goto out_put_session; 4218 4219 slot = session->se_slots[seq->slotid]; 4220 dprintk("%s: slotid %d\n", __func__, seq->slotid); 4221 4222 /* We do not negotiate the number of slots yet, so set the 4223 * maxslots to the session maxreqs which is used to encode 4224 * sr_highest_slotid and the sr_target_slot id to maxslots */ 4225 seq->maxslots = session->se_fchannel.maxreqs; 4226 4227 trace_nfsd_slot_seqid_sequence(clp, seq, slot); 4228 status = check_slot_seqid(seq->seqid, slot->sl_seqid, 4229 slot->sl_flags & NFSD4_SLOT_INUSE); 4230 if (status == nfserr_replay_cache) { 4231 status = nfserr_seq_misordered; 4232 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) 4233 goto out_put_session; 4234 status = nfserr_seq_false_retry; 4235 if (!replay_matches_cache(rqstp, seq, slot)) 4236 goto out_put_session; 4237 cstate->slot = slot; 4238 cstate->session = session; 4239 cstate->clp = clp; 4240 /* Return the cached reply status and set cstate->status 4241 * for nfsd4_proc_compound processing */ 4242 status = nfsd4_replay_cache_entry(resp, seq); 4243 cstate->status = nfserr_replay_cache; 4244 goto out; 4245 } 4246 if (status) 4247 goto out_put_session; 4248 4249 status = nfsd4_sequence_check_conn(conn, session); 4250 conn = NULL; 4251 if (status) 4252 goto out_put_session; 4253 4254 buflen = (seq->cachethis) ? 4255 session->se_fchannel.maxresp_cached : 4256 session->se_fchannel.maxresp_sz; 4257 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : 4258 nfserr_rep_too_big; 4259 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) 4260 goto out_put_session; 4261 svc_reserve(rqstp, buflen); 4262 4263 status = nfs_ok; 4264 /* Success! bump slot seqid */ 4265 slot->sl_seqid = seq->seqid; 4266 slot->sl_flags |= NFSD4_SLOT_INUSE; 4267 if (seq->cachethis) 4268 slot->sl_flags |= NFSD4_SLOT_CACHETHIS; 4269 else 4270 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; 4271 4272 cstate->slot = slot; 4273 cstate->session = session; 4274 cstate->clp = clp; 4275 4276 out: 4277 switch (clp->cl_cb_state) { 4278 case NFSD4_CB_DOWN: 4279 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; 4280 break; 4281 case NFSD4_CB_FAULT: 4282 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; 4283 break; 4284 default: 4285 seq->status_flags = 0; 4286 } 4287 if (!list_empty(&clp->cl_revoked)) 4288 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; 4289 if (atomic_read(&clp->cl_admin_revoked)) 4290 seq->status_flags |= SEQ4_STATUS_ADMIN_STATE_REVOKED; 4291 trace_nfsd_seq4_status(rqstp, seq); 4292 out_no_session: 4293 if (conn) 4294 free_conn(conn); 4295 spin_unlock(&nn->client_lock); 4296 return status; 4297 out_put_session: 4298 nfsd4_put_session_locked(session); 4299 goto out_no_session; 4300 } 4301 4302 void 4303 nfsd4_sequence_done(struct nfsd4_compoundres *resp) 4304 { 4305 struct nfsd4_compound_state *cs = &resp->cstate; 4306 4307 if (nfsd4_has_session(cs)) { 4308 if (cs->status != nfserr_replay_cache) { 4309 nfsd4_store_cache_entry(resp); 4310 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; 4311 } 4312 /* Drop session reference that was taken in nfsd4_sequence() */ 4313 nfsd4_put_session(cs->session); 4314 } else if (cs->clp) 4315 put_client_renew(cs->clp); 4316 } 4317 4318 __be32 4319 nfsd4_destroy_clientid(struct svc_rqst *rqstp, 4320 struct nfsd4_compound_state *cstate, 4321 union nfsd4_op_u *u) 4322 { 4323 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid; 4324 struct nfs4_client *conf, *unconf; 4325 struct nfs4_client *clp = NULL; 4326 __be32 status = 0; 4327 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 4328 4329 spin_lock(&nn->client_lock); 4330 unconf = find_unconfirmed_client(&dc->clientid, true, nn); 4331 conf = find_confirmed_client(&dc->clientid, true, nn); 4332 WARN_ON_ONCE(conf && unconf); 4333 4334 if (conf) { 4335 if (client_has_state(conf)) { 4336 status = nfserr_clientid_busy; 4337 goto out; 4338 } 4339 status = mark_client_expired_locked(conf); 4340 if (status) 4341 goto out; 4342 clp = conf; 4343 } else if (unconf) 4344 clp = unconf; 4345 else { 4346 status = nfserr_stale_clientid; 4347 goto out; 4348 } 4349 if (!nfsd4_mach_creds_match(clp, rqstp)) { 4350 clp = NULL; 4351 status = nfserr_wrong_cred; 4352 goto out; 4353 } 4354 trace_nfsd_clid_destroyed(&clp->cl_clientid); 4355 unhash_client_locked(clp); 4356 out: 4357 spin_unlock(&nn->client_lock); 4358 if (clp) 4359 expire_client(clp); 4360 return status; 4361 } 4362 4363 __be32 4364 nfsd4_reclaim_complete(struct svc_rqst *rqstp, 4365 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 4366 { 4367 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete; 4368 struct nfs4_client *clp = cstate->clp; 4369 __be32 status = 0; 4370 4371 if (rc->rca_one_fs) { 4372 if (!cstate->current_fh.fh_dentry) 4373 return nfserr_nofilehandle; 4374 /* 4375 * We don't take advantage of the rca_one_fs case. 4376 * That's OK, it's optional, we can safely ignore it. 4377 */ 4378 return nfs_ok; 4379 } 4380 4381 status = nfserr_complete_already; 4382 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags)) 4383 goto out; 4384 4385 status = nfserr_stale_clientid; 4386 if (is_client_expired(clp)) 4387 /* 4388 * The following error isn't really legal. 4389 * But we only get here if the client just explicitly 4390 * destroyed the client. Surely it no longer cares what 4391 * error it gets back on an operation for the dead 4392 * client. 4393 */ 4394 goto out; 4395 4396 status = nfs_ok; 4397 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid); 4398 nfsd4_client_record_create(clp); 4399 inc_reclaim_complete(clp); 4400 out: 4401 return status; 4402 } 4403 4404 __be32 4405 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 4406 union nfsd4_op_u *u) 4407 { 4408 struct nfsd4_setclientid *setclid = &u->setclientid; 4409 struct xdr_netobj clname = setclid->se_name; 4410 nfs4_verifier clverifier = setclid->se_verf; 4411 struct nfs4_client *conf, *new; 4412 struct nfs4_client *unconf = NULL; 4413 __be32 status; 4414 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 4415 4416 new = create_client(clname, rqstp, &clverifier); 4417 if (new == NULL) 4418 return nfserr_jukebox; 4419 spin_lock(&nn->client_lock); 4420 conf = find_confirmed_client_by_name(&clname, nn); 4421 if (conf && client_has_state(conf)) { 4422 status = nfserr_clid_inuse; 4423 if (clp_used_exchangeid(conf)) 4424 goto out; 4425 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { 4426 trace_nfsd_clid_cred_mismatch(conf, rqstp); 4427 goto out; 4428 } 4429 } 4430 unconf = find_unconfirmed_client_by_name(&clname, nn); 4431 if (unconf) 4432 unhash_client_locked(unconf); 4433 if (conf) { 4434 if (same_verf(&conf->cl_verifier, &clverifier)) { 4435 copy_clid(new, conf); 4436 gen_confirm(new, nn); 4437 } else 4438 trace_nfsd_clid_verf_mismatch(conf, rqstp, 4439 &clverifier); 4440 } else 4441 trace_nfsd_clid_fresh(new); 4442 new->cl_minorversion = 0; 4443 gen_callback(new, setclid, rqstp); 4444 add_to_unconfirmed(new); 4445 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; 4446 setclid->se_clientid.cl_id = new->cl_clientid.cl_id; 4447 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); 4448 new = NULL; 4449 status = nfs_ok; 4450 out: 4451 spin_unlock(&nn->client_lock); 4452 if (new) 4453 free_client(new); 4454 if (unconf) { 4455 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid); 4456 expire_client(unconf); 4457 } 4458 return status; 4459 } 4460 4461 __be32 4462 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, 4463 struct nfsd4_compound_state *cstate, 4464 union nfsd4_op_u *u) 4465 { 4466 struct nfsd4_setclientid_confirm *setclientid_confirm = 4467 &u->setclientid_confirm; 4468 struct nfs4_client *conf, *unconf; 4469 struct nfs4_client *old = NULL; 4470 nfs4_verifier confirm = setclientid_confirm->sc_confirm; 4471 clientid_t * clid = &setclientid_confirm->sc_clientid; 4472 __be32 status; 4473 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 4474 4475 if (STALE_CLIENTID(clid, nn)) 4476 return nfserr_stale_clientid; 4477 4478 spin_lock(&nn->client_lock); 4479 conf = find_confirmed_client(clid, false, nn); 4480 unconf = find_unconfirmed_client(clid, false, nn); 4481 /* 4482 * We try hard to give out unique clientid's, so if we get an 4483 * attempt to confirm the same clientid with a different cred, 4484 * the client may be buggy; this should never happen. 4485 * 4486 * Nevertheless, RFC 7530 recommends INUSE for this case: 4487 */ 4488 status = nfserr_clid_inuse; 4489 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) { 4490 trace_nfsd_clid_cred_mismatch(unconf, rqstp); 4491 goto out; 4492 } 4493 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) { 4494 trace_nfsd_clid_cred_mismatch(conf, rqstp); 4495 goto out; 4496 } 4497 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { 4498 if (conf && same_verf(&confirm, &conf->cl_confirm)) { 4499 status = nfs_ok; 4500 } else 4501 status = nfserr_stale_clientid; 4502 goto out; 4503 } 4504 status = nfs_ok; 4505 if (conf) { 4506 old = unconf; 4507 unhash_client_locked(old); 4508 nfsd4_change_callback(conf, &unconf->cl_cb_conn); 4509 } else { 4510 old = find_confirmed_client_by_name(&unconf->cl_name, nn); 4511 if (old) { 4512 status = nfserr_clid_inuse; 4513 if (client_has_state(old) 4514 && !same_creds(&unconf->cl_cred, 4515 &old->cl_cred)) { 4516 old = NULL; 4517 goto out; 4518 } 4519 status = mark_client_expired_locked(old); 4520 if (status) { 4521 old = NULL; 4522 goto out; 4523 } 4524 trace_nfsd_clid_replaced(&old->cl_clientid); 4525 } 4526 move_to_confirmed(unconf); 4527 conf = unconf; 4528 } 4529 get_client_locked(conf); 4530 spin_unlock(&nn->client_lock); 4531 if (conf == unconf) 4532 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY); 4533 nfsd4_probe_callback(conf); 4534 spin_lock(&nn->client_lock); 4535 put_client_renew_locked(conf); 4536 out: 4537 spin_unlock(&nn->client_lock); 4538 if (old) 4539 expire_client(old); 4540 return status; 4541 } 4542 4543 static struct nfs4_file *nfsd4_alloc_file(void) 4544 { 4545 return kmem_cache_alloc(file_slab, GFP_KERNEL); 4546 } 4547 4548 /* OPEN Share state helper functions */ 4549 4550 static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp) 4551 { 4552 refcount_set(&fp->fi_ref, 1); 4553 spin_lock_init(&fp->fi_lock); 4554 INIT_LIST_HEAD(&fp->fi_stateids); 4555 INIT_LIST_HEAD(&fp->fi_delegations); 4556 INIT_LIST_HEAD(&fp->fi_clnt_odstate); 4557 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle); 4558 fp->fi_deleg_file = NULL; 4559 fp->fi_had_conflict = false; 4560 fp->fi_share_deny = 0; 4561 memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); 4562 memset(fp->fi_access, 0, sizeof(fp->fi_access)); 4563 fp->fi_aliased = false; 4564 fp->fi_inode = d_inode(fh->fh_dentry); 4565 #ifdef CONFIG_NFSD_PNFS 4566 INIT_LIST_HEAD(&fp->fi_lo_states); 4567 atomic_set(&fp->fi_lo_recalls, 0); 4568 #endif 4569 } 4570 4571 void 4572 nfsd4_free_slabs(void) 4573 { 4574 kmem_cache_destroy(client_slab); 4575 kmem_cache_destroy(openowner_slab); 4576 kmem_cache_destroy(lockowner_slab); 4577 kmem_cache_destroy(file_slab); 4578 kmem_cache_destroy(stateid_slab); 4579 kmem_cache_destroy(deleg_slab); 4580 kmem_cache_destroy(odstate_slab); 4581 } 4582 4583 int 4584 nfsd4_init_slabs(void) 4585 { 4586 client_slab = KMEM_CACHE(nfs4_client, 0); 4587 if (client_slab == NULL) 4588 goto out; 4589 openowner_slab = KMEM_CACHE(nfs4_openowner, 0); 4590 if (openowner_slab == NULL) 4591 goto out_free_client_slab; 4592 lockowner_slab = KMEM_CACHE(nfs4_lockowner, 0); 4593 if (lockowner_slab == NULL) 4594 goto out_free_openowner_slab; 4595 file_slab = KMEM_CACHE(nfs4_file, 0); 4596 if (file_slab == NULL) 4597 goto out_free_lockowner_slab; 4598 stateid_slab = KMEM_CACHE(nfs4_ol_stateid, 0); 4599 if (stateid_slab == NULL) 4600 goto out_free_file_slab; 4601 deleg_slab = KMEM_CACHE(nfs4_delegation, 0); 4602 if (deleg_slab == NULL) 4603 goto out_free_stateid_slab; 4604 odstate_slab = KMEM_CACHE(nfs4_clnt_odstate, 0); 4605 if (odstate_slab == NULL) 4606 goto out_free_deleg_slab; 4607 return 0; 4608 4609 out_free_deleg_slab: 4610 kmem_cache_destroy(deleg_slab); 4611 out_free_stateid_slab: 4612 kmem_cache_destroy(stateid_slab); 4613 out_free_file_slab: 4614 kmem_cache_destroy(file_slab); 4615 out_free_lockowner_slab: 4616 kmem_cache_destroy(lockowner_slab); 4617 out_free_openowner_slab: 4618 kmem_cache_destroy(openowner_slab); 4619 out_free_client_slab: 4620 kmem_cache_destroy(client_slab); 4621 out: 4622 return -ENOMEM; 4623 } 4624 4625 static unsigned long 4626 nfsd4_state_shrinker_count(struct shrinker *shrink, struct shrink_control *sc) 4627 { 4628 int count; 4629 struct nfsd_net *nn = shrink->private_data; 4630 4631 count = atomic_read(&nn->nfsd_courtesy_clients); 4632 if (!count) 4633 count = atomic_long_read(&num_delegations); 4634 if (count) 4635 queue_work(laundry_wq, &nn->nfsd_shrinker_work); 4636 return (unsigned long)count; 4637 } 4638 4639 static unsigned long 4640 nfsd4_state_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc) 4641 { 4642 return SHRINK_STOP; 4643 } 4644 4645 void 4646 nfsd4_init_leases_net(struct nfsd_net *nn) 4647 { 4648 struct sysinfo si; 4649 u64 max_clients; 4650 4651 nn->nfsd4_lease = 90; /* default lease time */ 4652 nn->nfsd4_grace = 90; 4653 nn->somebody_reclaimed = false; 4654 nn->track_reclaim_completes = false; 4655 nn->clverifier_counter = get_random_u32(); 4656 nn->clientid_base = get_random_u32(); 4657 nn->clientid_counter = nn->clientid_base + 1; 4658 nn->s2s_cp_cl_id = nn->clientid_counter++; 4659 4660 atomic_set(&nn->nfs4_client_count, 0); 4661 si_meminfo(&si); 4662 max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024); 4663 max_clients *= NFS4_CLIENTS_PER_GB; 4664 nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB); 4665 4666 atomic_set(&nn->nfsd_courtesy_clients, 0); 4667 } 4668 4669 enum rp_lock { 4670 RP_UNLOCKED, 4671 RP_LOCKED, 4672 RP_UNHASHED, 4673 }; 4674 4675 static void init_nfs4_replay(struct nfs4_replay *rp) 4676 { 4677 rp->rp_status = nfserr_serverfault; 4678 rp->rp_buflen = 0; 4679 rp->rp_buf = rp->rp_ibuf; 4680 atomic_set(&rp->rp_locked, RP_UNLOCKED); 4681 } 4682 4683 static int nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, 4684 struct nfs4_stateowner *so) 4685 { 4686 if (!nfsd4_has_session(cstate)) { 4687 wait_var_event(&so->so_replay.rp_locked, 4688 atomic_cmpxchg(&so->so_replay.rp_locked, 4689 RP_UNLOCKED, RP_LOCKED) != RP_LOCKED); 4690 if (atomic_read(&so->so_replay.rp_locked) == RP_UNHASHED) 4691 return -EAGAIN; 4692 cstate->replay_owner = nfs4_get_stateowner(so); 4693 } 4694 return 0; 4695 } 4696 4697 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) 4698 { 4699 struct nfs4_stateowner *so = cstate->replay_owner; 4700 4701 if (so != NULL) { 4702 cstate->replay_owner = NULL; 4703 atomic_set(&so->so_replay.rp_locked, RP_UNLOCKED); 4704 wake_up_var(&so->so_replay.rp_locked); 4705 nfs4_put_stateowner(so); 4706 } 4707 } 4708 4709 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp) 4710 { 4711 struct nfs4_stateowner *sop; 4712 4713 sop = kmem_cache_alloc(slab, GFP_KERNEL); 4714 if (!sop) 4715 return NULL; 4716 4717 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL); 4718 if (!sop->so_owner.data) { 4719 kmem_cache_free(slab, sop); 4720 return NULL; 4721 } 4722 4723 INIT_LIST_HEAD(&sop->so_stateids); 4724 sop->so_client = clp; 4725 init_nfs4_replay(&sop->so_replay); 4726 atomic_set(&sop->so_count, 1); 4727 return sop; 4728 } 4729 4730 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval) 4731 { 4732 lockdep_assert_held(&clp->cl_lock); 4733 4734 list_add(&oo->oo_owner.so_strhash, 4735 &clp->cl_ownerstr_hashtbl[strhashval]); 4736 list_add(&oo->oo_perclient, &clp->cl_openowners); 4737 } 4738 4739 static void nfs4_unhash_openowner(struct nfs4_stateowner *so) 4740 { 4741 unhash_openowner_locked(openowner(so)); 4742 } 4743 4744 static void nfs4_free_openowner(struct nfs4_stateowner *so) 4745 { 4746 struct nfs4_openowner *oo = openowner(so); 4747 4748 kmem_cache_free(openowner_slab, oo); 4749 } 4750 4751 static const struct nfs4_stateowner_operations openowner_ops = { 4752 .so_unhash = nfs4_unhash_openowner, 4753 .so_free = nfs4_free_openowner, 4754 }; 4755 4756 static struct nfs4_ol_stateid * 4757 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) 4758 { 4759 struct nfs4_ol_stateid *local, *ret = NULL; 4760 struct nfs4_openowner *oo = open->op_openowner; 4761 4762 lockdep_assert_held(&fp->fi_lock); 4763 4764 list_for_each_entry(local, &fp->fi_stateids, st_perfile) { 4765 /* ignore lock owners */ 4766 if (local->st_stateowner->so_is_open_owner == 0) 4767 continue; 4768 if (local->st_stateowner != &oo->oo_owner) 4769 continue; 4770 if (local->st_stid.sc_type == SC_TYPE_OPEN && 4771 !local->st_stid.sc_status) { 4772 ret = local; 4773 refcount_inc(&ret->st_stid.sc_count); 4774 break; 4775 } 4776 } 4777 return ret; 4778 } 4779 4780 static void nfsd4_drop_revoked_stid(struct nfs4_stid *s) 4781 __releases(&s->sc_client->cl_lock) 4782 { 4783 struct nfs4_client *cl = s->sc_client; 4784 LIST_HEAD(reaplist); 4785 struct nfs4_ol_stateid *stp; 4786 struct nfs4_delegation *dp; 4787 bool unhashed; 4788 4789 switch (s->sc_type) { 4790 case SC_TYPE_OPEN: 4791 stp = openlockstateid(s); 4792 if (unhash_open_stateid(stp, &reaplist)) 4793 put_ol_stateid_locked(stp, &reaplist); 4794 spin_unlock(&cl->cl_lock); 4795 free_ol_stateid_reaplist(&reaplist); 4796 break; 4797 case SC_TYPE_LOCK: 4798 stp = openlockstateid(s); 4799 unhashed = unhash_lock_stateid(stp); 4800 spin_unlock(&cl->cl_lock); 4801 if (unhashed) 4802 nfs4_put_stid(s); 4803 break; 4804 case SC_TYPE_DELEG: 4805 dp = delegstateid(s); 4806 list_del_init(&dp->dl_recall_lru); 4807 spin_unlock(&cl->cl_lock); 4808 nfs4_put_stid(s); 4809 break; 4810 default: 4811 spin_unlock(&cl->cl_lock); 4812 } 4813 } 4814 4815 static void nfsd40_drop_revoked_stid(struct nfs4_client *cl, 4816 stateid_t *stid) 4817 { 4818 /* NFSv4.0 has no way for the client to tell the server 4819 * that it can forget an admin-revoked stateid. 4820 * So we keep it around until the first time that the 4821 * client uses it, and drop it the first time 4822 * nfserr_admin_revoked is returned. 4823 * For v4.1 and later we wait until explicitly told 4824 * to free the stateid. 4825 */ 4826 if (cl->cl_minorversion == 0) { 4827 struct nfs4_stid *st; 4828 4829 spin_lock(&cl->cl_lock); 4830 st = find_stateid_locked(cl, stid); 4831 if (st) 4832 nfsd4_drop_revoked_stid(st); 4833 else 4834 spin_unlock(&cl->cl_lock); 4835 } 4836 } 4837 4838 static __be32 4839 nfsd4_verify_open_stid(struct nfs4_stid *s) 4840 { 4841 __be32 ret = nfs_ok; 4842 4843 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) 4844 ret = nfserr_admin_revoked; 4845 else if (s->sc_status & SC_STATUS_REVOKED) 4846 ret = nfserr_deleg_revoked; 4847 else if (s->sc_status & SC_STATUS_CLOSED) 4848 ret = nfserr_bad_stateid; 4849 return ret; 4850 } 4851 4852 /* Lock the stateid st_mutex, and deal with races with CLOSE */ 4853 static __be32 4854 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp) 4855 { 4856 __be32 ret; 4857 4858 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX); 4859 ret = nfsd4_verify_open_stid(&stp->st_stid); 4860 if (ret == nfserr_admin_revoked) 4861 nfsd40_drop_revoked_stid(stp->st_stid.sc_client, 4862 &stp->st_stid.sc_stateid); 4863 4864 if (ret != nfs_ok) 4865 mutex_unlock(&stp->st_mutex); 4866 return ret; 4867 } 4868 4869 static struct nfs4_ol_stateid * 4870 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) 4871 { 4872 struct nfs4_ol_stateid *stp; 4873 for (;;) { 4874 spin_lock(&fp->fi_lock); 4875 stp = nfsd4_find_existing_open(fp, open); 4876 spin_unlock(&fp->fi_lock); 4877 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok) 4878 break; 4879 nfs4_put_stid(&stp->st_stid); 4880 } 4881 return stp; 4882 } 4883 4884 static struct nfs4_openowner * 4885 find_or_alloc_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, 4886 struct nfsd4_compound_state *cstate) 4887 { 4888 struct nfs4_client *clp = cstate->clp; 4889 struct nfs4_openowner *oo, *new = NULL; 4890 4891 retry: 4892 spin_lock(&clp->cl_lock); 4893 oo = find_openstateowner_str(strhashval, open, clp); 4894 if (!oo && new) { 4895 hash_openowner(new, clp, strhashval); 4896 spin_unlock(&clp->cl_lock); 4897 return new; 4898 } 4899 spin_unlock(&clp->cl_lock); 4900 4901 if (oo && !(oo->oo_flags & NFS4_OO_CONFIRMED)) { 4902 /* Replace unconfirmed owners without checking for replay. */ 4903 release_openowner(oo); 4904 oo = NULL; 4905 } 4906 if (oo) { 4907 if (new) 4908 nfs4_free_stateowner(&new->oo_owner); 4909 return oo; 4910 } 4911 4912 new = alloc_stateowner(openowner_slab, &open->op_owner, clp); 4913 if (!new) 4914 return NULL; 4915 new->oo_owner.so_ops = &openowner_ops; 4916 new->oo_owner.so_is_open_owner = 1; 4917 new->oo_owner.so_seqid = open->op_seqid; 4918 new->oo_flags = 0; 4919 if (nfsd4_has_session(cstate)) 4920 new->oo_flags |= NFS4_OO_CONFIRMED; 4921 new->oo_time = 0; 4922 new->oo_last_closed_stid = NULL; 4923 INIT_LIST_HEAD(&new->oo_close_lru); 4924 goto retry; 4925 } 4926 4927 static struct nfs4_ol_stateid * 4928 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) 4929 { 4930 4931 struct nfs4_openowner *oo = open->op_openowner; 4932 struct nfs4_ol_stateid *retstp = NULL; 4933 struct nfs4_ol_stateid *stp; 4934 4935 stp = open->op_stp; 4936 /* We are moving these outside of the spinlocks to avoid the warnings */ 4937 mutex_init(&stp->st_mutex); 4938 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); 4939 4940 retry: 4941 spin_lock(&oo->oo_owner.so_client->cl_lock); 4942 spin_lock(&fp->fi_lock); 4943 4944 retstp = nfsd4_find_existing_open(fp, open); 4945 if (retstp) 4946 goto out_unlock; 4947 4948 open->op_stp = NULL; 4949 refcount_inc(&stp->st_stid.sc_count); 4950 stp->st_stid.sc_type = SC_TYPE_OPEN; 4951 INIT_LIST_HEAD(&stp->st_locks); 4952 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); 4953 get_nfs4_file(fp); 4954 stp->st_stid.sc_file = fp; 4955 stp->st_access_bmap = 0; 4956 stp->st_deny_bmap = 0; 4957 stp->st_openstp = NULL; 4958 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); 4959 list_add(&stp->st_perfile, &fp->fi_stateids); 4960 4961 out_unlock: 4962 spin_unlock(&fp->fi_lock); 4963 spin_unlock(&oo->oo_owner.so_client->cl_lock); 4964 if (retstp) { 4965 /* Handle races with CLOSE */ 4966 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { 4967 nfs4_put_stid(&retstp->st_stid); 4968 goto retry; 4969 } 4970 /* To keep mutex tracking happy */ 4971 mutex_unlock(&stp->st_mutex); 4972 stp = retstp; 4973 } 4974 return stp; 4975 } 4976 4977 /* 4978 * In the 4.0 case we need to keep the owners around a little while to handle 4979 * CLOSE replay. We still do need to release any file access that is held by 4980 * them before returning however. 4981 */ 4982 static void 4983 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) 4984 { 4985 struct nfs4_ol_stateid *last; 4986 struct nfs4_openowner *oo = openowner(s->st_stateowner); 4987 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, 4988 nfsd_net_id); 4989 4990 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); 4991 4992 /* 4993 * We know that we hold one reference via nfsd4_close, and another 4994 * "persistent" reference for the client. If the refcount is higher 4995 * than 2, then there are still calls in progress that are using this 4996 * stateid. We can't put the sc_file reference until they are finished. 4997 * Wait for the refcount to drop to 2. Since it has been unhashed, 4998 * there should be no danger of the refcount going back up again at 4999 * this point. 5000 * Some threads with a reference might be waiting for rp_locked, 5001 * so tell them to stop waiting. 5002 */ 5003 atomic_set(&oo->oo_owner.so_replay.rp_locked, RP_UNHASHED); 5004 wake_up_var(&oo->oo_owner.so_replay.rp_locked); 5005 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2); 5006 5007 release_all_access(s); 5008 if (s->st_stid.sc_file) { 5009 put_nfs4_file(s->st_stid.sc_file); 5010 s->st_stid.sc_file = NULL; 5011 } 5012 5013 spin_lock(&nn->client_lock); 5014 last = oo->oo_last_closed_stid; 5015 oo->oo_last_closed_stid = s; 5016 list_move_tail(&oo->oo_close_lru, &nn->close_lru); 5017 oo->oo_time = ktime_get_boottime_seconds(); 5018 spin_unlock(&nn->client_lock); 5019 if (last) 5020 nfs4_put_stid(&last->st_stid); 5021 } 5022 5023 static noinline_for_stack struct nfs4_file * 5024 nfsd4_file_hash_lookup(const struct svc_fh *fhp) 5025 { 5026 struct inode *inode = d_inode(fhp->fh_dentry); 5027 struct rhlist_head *tmp, *list; 5028 struct nfs4_file *fi; 5029 5030 rcu_read_lock(); 5031 list = rhltable_lookup(&nfs4_file_rhltable, &inode, 5032 nfs4_file_rhash_params); 5033 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) { 5034 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) { 5035 if (refcount_inc_not_zero(&fi->fi_ref)) { 5036 rcu_read_unlock(); 5037 return fi; 5038 } 5039 } 5040 } 5041 rcu_read_unlock(); 5042 return NULL; 5043 } 5044 5045 /* 5046 * On hash insertion, identify entries with the same inode but 5047 * distinct filehandles. They will all be on the list returned 5048 * by rhltable_lookup(). 5049 * 5050 * inode->i_lock prevents racing insertions from adding an entry 5051 * for the same inode/fhp pair twice. 5052 */ 5053 static noinline_for_stack struct nfs4_file * 5054 nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp) 5055 { 5056 struct inode *inode = d_inode(fhp->fh_dentry); 5057 struct rhlist_head *tmp, *list; 5058 struct nfs4_file *ret = NULL; 5059 bool alias_found = false; 5060 struct nfs4_file *fi; 5061 int err; 5062 5063 rcu_read_lock(); 5064 spin_lock(&inode->i_lock); 5065 5066 list = rhltable_lookup(&nfs4_file_rhltable, &inode, 5067 nfs4_file_rhash_params); 5068 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) { 5069 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) { 5070 if (refcount_inc_not_zero(&fi->fi_ref)) 5071 ret = fi; 5072 } else 5073 fi->fi_aliased = alias_found = true; 5074 } 5075 if (ret) 5076 goto out_unlock; 5077 5078 nfsd4_file_init(fhp, new); 5079 err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist, 5080 nfs4_file_rhash_params); 5081 if (err) 5082 goto out_unlock; 5083 5084 new->fi_aliased = alias_found; 5085 ret = new; 5086 5087 out_unlock: 5088 spin_unlock(&inode->i_lock); 5089 rcu_read_unlock(); 5090 return ret; 5091 } 5092 5093 static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi) 5094 { 5095 rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist, 5096 nfs4_file_rhash_params); 5097 } 5098 5099 /* 5100 * Called to check deny when READ with all zero stateid or 5101 * WRITE with all zero or all one stateid 5102 */ 5103 static __be32 5104 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) 5105 { 5106 struct nfs4_file *fp; 5107 __be32 ret = nfs_ok; 5108 5109 fp = nfsd4_file_hash_lookup(current_fh); 5110 if (!fp) 5111 return ret; 5112 5113 /* Check for conflicting share reservations */ 5114 spin_lock(&fp->fi_lock); 5115 if (fp->fi_share_deny & deny_type) 5116 ret = nfserr_locked; 5117 spin_unlock(&fp->fi_lock); 5118 put_nfs4_file(fp); 5119 return ret; 5120 } 5121 5122 static bool nfsd4_deleg_present(const struct inode *inode) 5123 { 5124 struct file_lock_context *ctx = locks_inode_context(inode); 5125 5126 return ctx && !list_empty_careful(&ctx->flc_lease); 5127 } 5128 5129 /** 5130 * nfsd_wait_for_delegreturn - wait for delegations to be returned 5131 * @rqstp: the RPC transaction being executed 5132 * @inode: in-core inode of the file being waited for 5133 * 5134 * The timeout prevents deadlock if all nfsd threads happen to be 5135 * tied up waiting for returning delegations. 5136 * 5137 * Return values: 5138 * %true: delegation was returned 5139 * %false: timed out waiting for delegreturn 5140 */ 5141 bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode) 5142 { 5143 long __maybe_unused timeo; 5144 5145 timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode), 5146 NFSD_DELEGRETURN_TIMEOUT); 5147 trace_nfsd_delegret_wakeup(rqstp, inode, timeo); 5148 return timeo > 0; 5149 } 5150 5151 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) 5152 { 5153 struct nfs4_delegation *dp = cb_to_delegation(cb); 5154 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, 5155 nfsd_net_id); 5156 5157 block_delegations(&dp->dl_stid.sc_file->fi_fhandle); 5158 5159 /* 5160 * We can't do this in nfsd_break_deleg_cb because it is 5161 * already holding inode->i_lock. 5162 * 5163 * If the dl_time != 0, then we know that it has already been 5164 * queued for a lease break. Don't queue it again. 5165 */ 5166 spin_lock(&state_lock); 5167 if (delegation_hashed(dp) && dp->dl_time == 0) { 5168 dp->dl_time = ktime_get_boottime_seconds(); 5169 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); 5170 } 5171 spin_unlock(&state_lock); 5172 } 5173 5174 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, 5175 struct rpc_task *task) 5176 { 5177 struct nfs4_delegation *dp = cb_to_delegation(cb); 5178 5179 trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task); 5180 5181 if (dp->dl_stid.sc_status) 5182 /* CLOSED or REVOKED */ 5183 return 1; 5184 5185 switch (task->tk_status) { 5186 case 0: 5187 return 1; 5188 case -NFS4ERR_DELAY: 5189 rpc_delay(task, 2 * HZ); 5190 return 0; 5191 case -EBADHANDLE: 5192 case -NFS4ERR_BAD_STATEID: 5193 /* 5194 * Race: client probably got cb_recall before open reply 5195 * granting delegation. 5196 */ 5197 if (dp->dl_retries--) { 5198 rpc_delay(task, 2 * HZ); 5199 return 0; 5200 } 5201 fallthrough; 5202 default: 5203 return 1; 5204 } 5205 } 5206 5207 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) 5208 { 5209 struct nfs4_delegation *dp = cb_to_delegation(cb); 5210 5211 nfs4_put_stid(&dp->dl_stid); 5212 } 5213 5214 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { 5215 .prepare = nfsd4_cb_recall_prepare, 5216 .done = nfsd4_cb_recall_done, 5217 .release = nfsd4_cb_recall_release, 5218 }; 5219 5220 static void nfsd_break_one_deleg(struct nfs4_delegation *dp) 5221 { 5222 /* 5223 * We're assuming the state code never drops its reference 5224 * without first removing the lease. Since we're in this lease 5225 * callback (and since the lease code is serialized by the 5226 * flc_lock) we know the server hasn't removed the lease yet, and 5227 * we know it's safe to take a reference. 5228 */ 5229 refcount_inc(&dp->dl_stid.sc_count); 5230 WARN_ON_ONCE(!nfsd4_run_cb(&dp->dl_recall)); 5231 } 5232 5233 /* Called from break_lease() with flc_lock held. */ 5234 static bool 5235 nfsd_break_deleg_cb(struct file_lease *fl) 5236 { 5237 struct nfs4_delegation *dp = (struct nfs4_delegation *) fl->c.flc_owner; 5238 struct nfs4_file *fp = dp->dl_stid.sc_file; 5239 struct nfs4_client *clp = dp->dl_stid.sc_client; 5240 struct nfsd_net *nn; 5241 5242 trace_nfsd_cb_recall(&dp->dl_stid); 5243 5244 dp->dl_recalled = true; 5245 atomic_inc(&clp->cl_delegs_in_recall); 5246 if (try_to_expire_client(clp)) { 5247 nn = net_generic(clp->net, nfsd_net_id); 5248 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); 5249 } 5250 5251 /* 5252 * We don't want the locks code to timeout the lease for us; 5253 * we'll remove it ourself if a delegation isn't returned 5254 * in time: 5255 */ 5256 fl->fl_break_time = 0; 5257 5258 fp->fi_had_conflict = true; 5259 nfsd_break_one_deleg(dp); 5260 return false; 5261 } 5262 5263 /** 5264 * nfsd_breaker_owns_lease - Check if lease conflict was resolved 5265 * @fl: Lock state to check 5266 * 5267 * Return values: 5268 * %true: Lease conflict was resolved 5269 * %false: Lease conflict was not resolved. 5270 */ 5271 static bool nfsd_breaker_owns_lease(struct file_lease *fl) 5272 { 5273 struct nfs4_delegation *dl = fl->c.flc_owner; 5274 struct svc_rqst *rqst; 5275 struct nfs4_client *clp; 5276 5277 if (!i_am_nfsd()) 5278 return false; 5279 rqst = kthread_data(current); 5280 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */ 5281 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4) 5282 return false; 5283 clp = *(rqst->rq_lease_breaker); 5284 return dl->dl_stid.sc_client == clp; 5285 } 5286 5287 static int 5288 nfsd_change_deleg_cb(struct file_lease *onlist, int arg, 5289 struct list_head *dispose) 5290 { 5291 struct nfs4_delegation *dp = (struct nfs4_delegation *) onlist->c.flc_owner; 5292 struct nfs4_client *clp = dp->dl_stid.sc_client; 5293 5294 if (arg & F_UNLCK) { 5295 if (dp->dl_recalled) 5296 atomic_dec(&clp->cl_delegs_in_recall); 5297 return lease_modify(onlist, arg, dispose); 5298 } else 5299 return -EAGAIN; 5300 } 5301 5302 static const struct lease_manager_operations nfsd_lease_mng_ops = { 5303 .lm_breaker_owns_lease = nfsd_breaker_owns_lease, 5304 .lm_break = nfsd_break_deleg_cb, 5305 .lm_change = nfsd_change_deleg_cb, 5306 }; 5307 5308 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) 5309 { 5310 if (nfsd4_has_session(cstate)) 5311 return nfs_ok; 5312 if (seqid == so->so_seqid - 1) 5313 return nfserr_replay_me; 5314 if (seqid == so->so_seqid) 5315 return nfs_ok; 5316 return nfserr_bad_seqid; 5317 } 5318 5319 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions, 5320 struct nfsd_net *nn) 5321 { 5322 struct nfs4_client *found; 5323 5324 spin_lock(&nn->client_lock); 5325 found = find_confirmed_client(clid, sessions, nn); 5326 if (found) 5327 atomic_inc(&found->cl_rpc_users); 5328 spin_unlock(&nn->client_lock); 5329 return found; 5330 } 5331 5332 static __be32 set_client(clientid_t *clid, 5333 struct nfsd4_compound_state *cstate, 5334 struct nfsd_net *nn) 5335 { 5336 if (cstate->clp) { 5337 if (!same_clid(&cstate->clp->cl_clientid, clid)) 5338 return nfserr_stale_clientid; 5339 return nfs_ok; 5340 } 5341 if (STALE_CLIENTID(clid, nn)) 5342 return nfserr_stale_clientid; 5343 /* 5344 * We're in the 4.0 case (otherwise the SEQUENCE op would have 5345 * set cstate->clp), so session = false: 5346 */ 5347 cstate->clp = lookup_clientid(clid, false, nn); 5348 if (!cstate->clp) 5349 return nfserr_expired; 5350 return nfs_ok; 5351 } 5352 5353 __be32 5354 nfsd4_process_open1(struct nfsd4_compound_state *cstate, 5355 struct nfsd4_open *open, struct nfsd_net *nn) 5356 { 5357 clientid_t *clientid = &open->op_clientid; 5358 struct nfs4_client *clp = NULL; 5359 unsigned int strhashval; 5360 struct nfs4_openowner *oo = NULL; 5361 __be32 status; 5362 5363 /* 5364 * In case we need it later, after we've already created the 5365 * file and don't want to risk a further failure: 5366 */ 5367 open->op_file = nfsd4_alloc_file(); 5368 if (open->op_file == NULL) 5369 return nfserr_jukebox; 5370 5371 status = set_client(clientid, cstate, nn); 5372 if (status) 5373 return status; 5374 clp = cstate->clp; 5375 5376 strhashval = ownerstr_hashval(&open->op_owner); 5377 retry: 5378 oo = find_or_alloc_open_stateowner(strhashval, open, cstate); 5379 open->op_openowner = oo; 5380 if (!oo) 5381 return nfserr_jukebox; 5382 if (nfsd4_cstate_assign_replay(cstate, &oo->oo_owner) == -EAGAIN) { 5383 nfs4_put_stateowner(&oo->oo_owner); 5384 goto retry; 5385 } 5386 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); 5387 if (status) 5388 return status; 5389 5390 open->op_stp = nfs4_alloc_open_stateid(clp); 5391 if (!open->op_stp) 5392 return nfserr_jukebox; 5393 5394 if (nfsd4_has_session(cstate) && 5395 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { 5396 open->op_odstate = alloc_clnt_odstate(clp); 5397 if (!open->op_odstate) 5398 return nfserr_jukebox; 5399 } 5400 5401 return nfs_ok; 5402 } 5403 5404 static inline __be32 5405 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) 5406 { 5407 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) 5408 return nfserr_openmode; 5409 else 5410 return nfs_ok; 5411 } 5412 5413 static int share_access_to_flags(u32 share_access) 5414 { 5415 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; 5416 } 5417 5418 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, 5419 stateid_t *s) 5420 { 5421 struct nfs4_stid *ret; 5422 5423 ret = find_stateid_by_type(cl, s, SC_TYPE_DELEG, SC_STATUS_REVOKED); 5424 if (!ret) 5425 return NULL; 5426 return delegstateid(ret); 5427 } 5428 5429 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) 5430 { 5431 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || 5432 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; 5433 } 5434 5435 static __be32 5436 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, 5437 struct nfs4_delegation **dp) 5438 { 5439 int flags; 5440 __be32 status = nfserr_bad_stateid; 5441 struct nfs4_delegation *deleg; 5442 5443 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); 5444 if (deleg == NULL) 5445 goto out; 5446 if (deleg->dl_stid.sc_status & SC_STATUS_ADMIN_REVOKED) { 5447 nfs4_put_stid(&deleg->dl_stid); 5448 status = nfserr_admin_revoked; 5449 goto out; 5450 } 5451 if (deleg->dl_stid.sc_status & SC_STATUS_REVOKED) { 5452 nfs4_put_stid(&deleg->dl_stid); 5453 nfsd40_drop_revoked_stid(cl, &open->op_delegate_stateid); 5454 status = nfserr_deleg_revoked; 5455 goto out; 5456 } 5457 flags = share_access_to_flags(open->op_share_access); 5458 status = nfs4_check_delegmode(deleg, flags); 5459 if (status) { 5460 nfs4_put_stid(&deleg->dl_stid); 5461 goto out; 5462 } 5463 *dp = deleg; 5464 out: 5465 if (!nfsd4_is_deleg_cur(open)) 5466 return nfs_ok; 5467 if (status) 5468 return status; 5469 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; 5470 return nfs_ok; 5471 } 5472 5473 static inline int nfs4_access_to_access(u32 nfs4_access) 5474 { 5475 int flags = 0; 5476 5477 if (nfs4_access & NFS4_SHARE_ACCESS_READ) 5478 flags |= NFSD_MAY_READ; 5479 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) 5480 flags |= NFSD_MAY_WRITE; 5481 return flags; 5482 } 5483 5484 static inline __be32 5485 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, 5486 struct nfsd4_open *open) 5487 { 5488 struct iattr iattr = { 5489 .ia_valid = ATTR_SIZE, 5490 .ia_size = 0, 5491 }; 5492 struct nfsd_attrs attrs = { 5493 .na_iattr = &iattr, 5494 }; 5495 if (!open->op_truncate) 5496 return 0; 5497 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) 5498 return nfserr_inval; 5499 return nfsd_setattr(rqstp, fh, &attrs, NULL); 5500 } 5501 5502 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, 5503 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, 5504 struct nfsd4_open *open, bool new_stp) 5505 { 5506 struct nfsd_file *nf = NULL; 5507 __be32 status; 5508 int oflag = nfs4_access_to_omode(open->op_share_access); 5509 int access = nfs4_access_to_access(open->op_share_access); 5510 unsigned char old_access_bmap, old_deny_bmap; 5511 5512 spin_lock(&fp->fi_lock); 5513 5514 /* 5515 * Are we trying to set a deny mode that would conflict with 5516 * current access? 5517 */ 5518 status = nfs4_file_check_deny(fp, open->op_share_deny); 5519 if (status != nfs_ok) { 5520 if (status != nfserr_share_denied) { 5521 spin_unlock(&fp->fi_lock); 5522 goto out; 5523 } 5524 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp, 5525 stp, open->op_share_deny, false)) 5526 status = nfserr_jukebox; 5527 spin_unlock(&fp->fi_lock); 5528 goto out; 5529 } 5530 5531 /* set access to the file */ 5532 status = nfs4_file_get_access(fp, open->op_share_access); 5533 if (status != nfs_ok) { 5534 if (status != nfserr_share_denied) { 5535 spin_unlock(&fp->fi_lock); 5536 goto out; 5537 } 5538 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp, 5539 stp, open->op_share_access, true)) 5540 status = nfserr_jukebox; 5541 spin_unlock(&fp->fi_lock); 5542 goto out; 5543 } 5544 5545 /* Set access bits in stateid */ 5546 old_access_bmap = stp->st_access_bmap; 5547 set_access(open->op_share_access, stp); 5548 5549 /* Set new deny mask */ 5550 old_deny_bmap = stp->st_deny_bmap; 5551 set_deny(open->op_share_deny, stp); 5552 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); 5553 5554 if (!fp->fi_fds[oflag]) { 5555 spin_unlock(&fp->fi_lock); 5556 5557 status = nfsd_file_acquire_opened(rqstp, cur_fh, access, 5558 open->op_filp, &nf); 5559 if (status != nfs_ok) 5560 goto out_put_access; 5561 5562 spin_lock(&fp->fi_lock); 5563 if (!fp->fi_fds[oflag]) { 5564 fp->fi_fds[oflag] = nf; 5565 nf = NULL; 5566 } 5567 } 5568 spin_unlock(&fp->fi_lock); 5569 if (nf) 5570 nfsd_file_put(nf); 5571 5572 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode, 5573 access)); 5574 if (status) 5575 goto out_put_access; 5576 5577 status = nfsd4_truncate(rqstp, cur_fh, open); 5578 if (status) 5579 goto out_put_access; 5580 out: 5581 return status; 5582 out_put_access: 5583 stp->st_access_bmap = old_access_bmap; 5584 nfs4_file_put_access(fp, open->op_share_access); 5585 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); 5586 goto out; 5587 } 5588 5589 static __be32 5590 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, 5591 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, 5592 struct nfsd4_open *open) 5593 { 5594 __be32 status; 5595 unsigned char old_deny_bmap = stp->st_deny_bmap; 5596 5597 if (!test_access(open->op_share_access, stp)) 5598 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false); 5599 5600 /* test and set deny mode */ 5601 spin_lock(&fp->fi_lock); 5602 status = nfs4_file_check_deny(fp, open->op_share_deny); 5603 switch (status) { 5604 case nfs_ok: 5605 set_deny(open->op_share_deny, stp); 5606 fp->fi_share_deny |= 5607 (open->op_share_deny & NFS4_SHARE_DENY_BOTH); 5608 break; 5609 case nfserr_share_denied: 5610 if (nfs4_resolve_deny_conflicts_locked(fp, false, 5611 stp, open->op_share_deny, false)) 5612 status = nfserr_jukebox; 5613 break; 5614 } 5615 spin_unlock(&fp->fi_lock); 5616 5617 if (status != nfs_ok) 5618 return status; 5619 5620 status = nfsd4_truncate(rqstp, cur_fh, open); 5621 if (status != nfs_ok) 5622 reset_union_bmap_deny(old_deny_bmap, stp); 5623 return status; 5624 } 5625 5626 /* Should we give out recallable state?: */ 5627 static bool nfsd4_cb_channel_good(struct nfs4_client *clp) 5628 { 5629 if (clp->cl_cb_state == NFSD4_CB_UP) 5630 return true; 5631 /* 5632 * In the sessions case, since we don't have to establish a 5633 * separate connection for callbacks, we assume it's OK 5634 * until we hear otherwise: 5635 */ 5636 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; 5637 } 5638 5639 static struct file_lease *nfs4_alloc_init_lease(struct nfs4_delegation *dp, 5640 int flag) 5641 { 5642 struct file_lease *fl; 5643 5644 fl = locks_alloc_lease(); 5645 if (!fl) 5646 return NULL; 5647 fl->fl_lmops = &nfsd_lease_mng_ops; 5648 fl->c.flc_flags = FL_DELEG; 5649 fl->c.flc_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; 5650 fl->c.flc_owner = (fl_owner_t)dp; 5651 fl->c.flc_pid = current->tgid; 5652 fl->c.flc_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file; 5653 return fl; 5654 } 5655 5656 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp, 5657 struct nfs4_file *fp) 5658 { 5659 struct nfs4_ol_stateid *st; 5660 struct file *f = fp->fi_deleg_file->nf_file; 5661 struct inode *ino = file_inode(f); 5662 int writes; 5663 5664 writes = atomic_read(&ino->i_writecount); 5665 if (!writes) 5666 return 0; 5667 /* 5668 * There could be multiple filehandles (hence multiple 5669 * nfs4_files) referencing this file, but that's not too 5670 * common; let's just give up in that case rather than 5671 * trying to go look up all the clients using that other 5672 * nfs4_file as well: 5673 */ 5674 if (fp->fi_aliased) 5675 return -EAGAIN; 5676 /* 5677 * If there's a close in progress, make sure that we see it 5678 * clear any fi_fds[] entries before we see it decrement 5679 * i_writecount: 5680 */ 5681 smp_mb__after_atomic(); 5682 5683 if (fp->fi_fds[O_WRONLY]) 5684 writes--; 5685 if (fp->fi_fds[O_RDWR]) 5686 writes--; 5687 if (writes > 0) 5688 return -EAGAIN; /* There may be non-NFSv4 writers */ 5689 /* 5690 * It's possible there are non-NFSv4 write opens in progress, 5691 * but if they haven't incremented i_writecount yet then they 5692 * also haven't called break lease yet; so, they'll break this 5693 * lease soon enough. So, all that's left to check for is NFSv4 5694 * opens: 5695 */ 5696 spin_lock(&fp->fi_lock); 5697 list_for_each_entry(st, &fp->fi_stateids, st_perfile) { 5698 if (st->st_openstp == NULL /* it's an open */ && 5699 access_permit_write(st) && 5700 st->st_stid.sc_client != clp) { 5701 spin_unlock(&fp->fi_lock); 5702 return -EAGAIN; 5703 } 5704 } 5705 spin_unlock(&fp->fi_lock); 5706 /* 5707 * There's a small chance that we could be racing with another 5708 * NFSv4 open. However, any open that hasn't added itself to 5709 * the fi_stateids list also hasn't called break_lease yet; so, 5710 * they'll break this lease soon enough. 5711 */ 5712 return 0; 5713 } 5714 5715 /* 5716 * It's possible that between opening the dentry and setting the delegation, 5717 * that it has been renamed or unlinked. Redo the lookup to verify that this 5718 * hasn't happened. 5719 */ 5720 static int 5721 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp, 5722 struct svc_fh *parent) 5723 { 5724 struct svc_export *exp; 5725 struct dentry *child; 5726 __be32 err; 5727 5728 err = nfsd_lookup_dentry(open->op_rqstp, parent, 5729 open->op_fname, open->op_fnamelen, 5730 &exp, &child); 5731 5732 if (err) 5733 return -EAGAIN; 5734 5735 exp_put(exp); 5736 dput(child); 5737 if (child != file_dentry(fp->fi_deleg_file->nf_file)) 5738 return -EAGAIN; 5739 5740 return 0; 5741 } 5742 5743 /* 5744 * We avoid breaking delegations held by a client due to its own activity, but 5745 * clearing setuid/setgid bits on a write is an implicit activity and the client 5746 * may not notice and continue using the old mode. Avoid giving out a delegation 5747 * on setuid/setgid files when the client is requesting an open for write. 5748 */ 5749 static int 5750 nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf) 5751 { 5752 struct inode *inode = file_inode(nf->nf_file); 5753 5754 if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) && 5755 (inode->i_mode & (S_ISUID|S_ISGID))) 5756 return -EAGAIN; 5757 return 0; 5758 } 5759 5760 static struct nfs4_delegation * 5761 nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, 5762 struct svc_fh *parent) 5763 { 5764 int status = 0; 5765 struct nfs4_client *clp = stp->st_stid.sc_client; 5766 struct nfs4_file *fp = stp->st_stid.sc_file; 5767 struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate; 5768 struct nfs4_delegation *dp; 5769 struct nfsd_file *nf = NULL; 5770 struct file_lease *fl; 5771 u32 dl_type; 5772 5773 /* 5774 * The fi_had_conflict and nfs_get_existing_delegation checks 5775 * here are just optimizations; we'll need to recheck them at 5776 * the end: 5777 */ 5778 if (fp->fi_had_conflict) 5779 return ERR_PTR(-EAGAIN); 5780 5781 /* 5782 * Try for a write delegation first. RFC8881 section 10.4 says: 5783 * 5784 * "An OPEN_DELEGATE_WRITE delegation allows the client to handle, 5785 * on its own, all opens." 5786 * 5787 * Furthermore the client can use a write delegation for most READ 5788 * operations as well, so we require a O_RDWR file here. 5789 * 5790 * Offer a write delegation in the case of a BOTH open, and ensure 5791 * we get the O_RDWR descriptor. 5792 */ 5793 if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) == NFS4_SHARE_ACCESS_BOTH) { 5794 nf = find_rw_file(fp); 5795 dl_type = NFS4_OPEN_DELEGATE_WRITE; 5796 } 5797 5798 /* 5799 * If the file is being opened O_RDONLY or we couldn't get a O_RDWR 5800 * file for some reason, then try for a read delegation instead. 5801 */ 5802 if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) { 5803 nf = find_readable_file(fp); 5804 dl_type = NFS4_OPEN_DELEGATE_READ; 5805 } 5806 5807 if (!nf) 5808 return ERR_PTR(-EAGAIN); 5809 5810 spin_lock(&state_lock); 5811 spin_lock(&fp->fi_lock); 5812 if (nfs4_delegation_exists(clp, fp)) 5813 status = -EAGAIN; 5814 else if (nfsd4_verify_setuid_write(open, nf)) 5815 status = -EAGAIN; 5816 else if (!fp->fi_deleg_file) { 5817 fp->fi_deleg_file = nf; 5818 /* increment early to prevent fi_deleg_file from being 5819 * cleared */ 5820 fp->fi_delegees = 1; 5821 nf = NULL; 5822 } else 5823 fp->fi_delegees++; 5824 spin_unlock(&fp->fi_lock); 5825 spin_unlock(&state_lock); 5826 if (nf) 5827 nfsd_file_put(nf); 5828 if (status) 5829 return ERR_PTR(status); 5830 5831 status = -ENOMEM; 5832 dp = alloc_init_deleg(clp, fp, odstate, dl_type); 5833 if (!dp) 5834 goto out_delegees; 5835 5836 fl = nfs4_alloc_init_lease(dp, dl_type); 5837 if (!fl) 5838 goto out_clnt_odstate; 5839 5840 status = kernel_setlease(fp->fi_deleg_file->nf_file, 5841 fl->c.flc_type, &fl, NULL); 5842 if (fl) 5843 locks_free_lease(fl); 5844 if (status) 5845 goto out_clnt_odstate; 5846 5847 if (parent) { 5848 status = nfsd4_verify_deleg_dentry(open, fp, parent); 5849 if (status) 5850 goto out_unlock; 5851 } 5852 5853 status = nfsd4_check_conflicting_opens(clp, fp); 5854 if (status) 5855 goto out_unlock; 5856 5857 /* 5858 * Now that the deleg is set, check again to ensure that nothing 5859 * raced in and changed the mode while we weren't lookng. 5860 */ 5861 status = nfsd4_verify_setuid_write(open, fp->fi_deleg_file); 5862 if (status) 5863 goto out_unlock; 5864 5865 status = -EAGAIN; 5866 if (fp->fi_had_conflict) 5867 goto out_unlock; 5868 5869 spin_lock(&state_lock); 5870 spin_lock(&clp->cl_lock); 5871 spin_lock(&fp->fi_lock); 5872 status = hash_delegation_locked(dp, fp); 5873 spin_unlock(&fp->fi_lock); 5874 spin_unlock(&clp->cl_lock); 5875 spin_unlock(&state_lock); 5876 5877 if (status) 5878 goto out_unlock; 5879 5880 return dp; 5881 out_unlock: 5882 kernel_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp); 5883 out_clnt_odstate: 5884 put_clnt_odstate(dp->dl_clnt_odstate); 5885 nfs4_put_stid(&dp->dl_stid); 5886 out_delegees: 5887 put_deleg_file(fp); 5888 return ERR_PTR(status); 5889 } 5890 5891 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) 5892 { 5893 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; 5894 if (status == -EAGAIN) 5895 open->op_why_no_deleg = WND4_CONTENTION; 5896 else { 5897 open->op_why_no_deleg = WND4_RESOURCE; 5898 switch (open->op_deleg_want) { 5899 case NFS4_SHARE_WANT_READ_DELEG: 5900 case NFS4_SHARE_WANT_WRITE_DELEG: 5901 case NFS4_SHARE_WANT_ANY_DELEG: 5902 break; 5903 case NFS4_SHARE_WANT_CANCEL: 5904 open->op_why_no_deleg = WND4_CANCELLED; 5905 break; 5906 case NFS4_SHARE_WANT_NO_DELEG: 5907 WARN_ON_ONCE(1); 5908 } 5909 } 5910 } 5911 5912 /* 5913 * The Linux NFS server does not offer write delegations to NFSv4.0 5914 * clients in order to avoid conflicts between write delegations and 5915 * GETATTRs requesting CHANGE or SIZE attributes. 5916 * 5917 * With NFSv4.1 and later minorversions, the SEQUENCE operation that 5918 * begins each COMPOUND contains a client ID. Delegation recall can 5919 * be avoided when the server recognizes the client sending a 5920 * GETATTR also holds write delegation it conflicts with. 5921 * 5922 * However, the NFSv4.0 protocol does not enable a server to 5923 * determine that a GETATTR originated from the client holding the 5924 * conflicting delegation versus coming from some other client. Per 5925 * RFC 7530 Section 16.7.5, the server must recall or send a 5926 * CB_GETATTR even when the GETATTR originates from the client that 5927 * holds the conflicting delegation. 5928 * 5929 * An NFSv4.0 client can trigger a pathological situation if it 5930 * always sends a DELEGRETURN preceded by a conflicting GETATTR in 5931 * the same COMPOUND. COMPOUND execution will always stop at the 5932 * GETATTR and the DELEGRETURN will never get executed. The server 5933 * eventually revokes the delegation, which can result in loss of 5934 * open or lock state. 5935 */ 5936 static void 5937 nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, 5938 struct svc_fh *currentfh) 5939 { 5940 struct nfs4_delegation *dp; 5941 struct nfs4_openowner *oo = openowner(stp->st_stateowner); 5942 struct nfs4_client *clp = stp->st_stid.sc_client; 5943 struct svc_fh *parent = NULL; 5944 int cb_up; 5945 int status = 0; 5946 struct kstat stat; 5947 struct path path; 5948 5949 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); 5950 open->op_recall = false; 5951 switch (open->op_claim_type) { 5952 case NFS4_OPEN_CLAIM_PREVIOUS: 5953 if (!cb_up) 5954 open->op_recall = true; 5955 break; 5956 case NFS4_OPEN_CLAIM_NULL: 5957 parent = currentfh; 5958 fallthrough; 5959 case NFS4_OPEN_CLAIM_FH: 5960 /* 5961 * Let's not give out any delegations till everyone's 5962 * had the chance to reclaim theirs, *and* until 5963 * NLM locks have all been reclaimed: 5964 */ 5965 if (locks_in_grace(clp->net)) 5966 goto out_no_deleg; 5967 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) 5968 goto out_no_deleg; 5969 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE && 5970 !clp->cl_minorversion) 5971 goto out_no_deleg; 5972 break; 5973 default: 5974 goto out_no_deleg; 5975 } 5976 dp = nfs4_set_delegation(open, stp, parent); 5977 if (IS_ERR(dp)) 5978 goto out_no_deleg; 5979 5980 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid)); 5981 5982 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) { 5983 open->op_delegate_type = NFS4_OPEN_DELEGATE_WRITE; 5984 trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid); 5985 path.mnt = currentfh->fh_export->ex_path.mnt; 5986 path.dentry = currentfh->fh_dentry; 5987 if (vfs_getattr(&path, &stat, 5988 (STATX_SIZE | STATX_CTIME | STATX_CHANGE_COOKIE), 5989 AT_STATX_SYNC_AS_STAT)) { 5990 nfs4_put_stid(&dp->dl_stid); 5991 destroy_delegation(dp); 5992 goto out_no_deleg; 5993 } 5994 dp->dl_cb_fattr.ncf_cur_fsize = stat.size; 5995 dp->dl_cb_fattr.ncf_initial_cinfo = 5996 nfsd4_change_attribute(&stat, d_inode(currentfh->fh_dentry)); 5997 } else { 5998 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; 5999 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid); 6000 } 6001 nfs4_put_stid(&dp->dl_stid); 6002 return; 6003 out_no_deleg: 6004 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; 6005 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && 6006 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { 6007 dprintk("NFSD: WARNING: refusing delegation reclaim\n"); 6008 open->op_recall = true; 6009 } 6010 6011 /* 4.1 client asking for a delegation? */ 6012 if (open->op_deleg_want) 6013 nfsd4_open_deleg_none_ext(open, status); 6014 return; 6015 } 6016 6017 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, 6018 struct nfs4_delegation *dp) 6019 { 6020 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && 6021 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { 6022 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; 6023 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; 6024 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && 6025 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { 6026 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; 6027 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; 6028 } 6029 /* Otherwise the client must be confused wanting a delegation 6030 * it already has, therefore we don't return 6031 * NFS4_OPEN_DELEGATE_NONE_EXT and reason. 6032 */ 6033 } 6034 6035 /** 6036 * nfsd4_process_open2 - finish open processing 6037 * @rqstp: the RPC transaction being executed 6038 * @current_fh: NFSv4 COMPOUND's current filehandle 6039 * @open: OPEN arguments 6040 * 6041 * If successful, (1) truncate the file if open->op_truncate was 6042 * set, (2) set open->op_stateid, (3) set open->op_delegation. 6043 * 6044 * Returns %nfs_ok on success; otherwise an nfs4stat value in 6045 * network byte order is returned. 6046 */ 6047 __be32 6048 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) 6049 { 6050 struct nfsd4_compoundres *resp = rqstp->rq_resp; 6051 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; 6052 struct nfs4_file *fp = NULL; 6053 struct nfs4_ol_stateid *stp = NULL; 6054 struct nfs4_delegation *dp = NULL; 6055 __be32 status; 6056 bool new_stp = false; 6057 6058 /* 6059 * Lookup file; if found, lookup stateid and check open request, 6060 * and check for delegations in the process of being recalled. 6061 * If not found, create the nfs4_file struct 6062 */ 6063 fp = nfsd4_file_hash_insert(open->op_file, current_fh); 6064 if (unlikely(!fp)) 6065 return nfserr_jukebox; 6066 if (fp != open->op_file) { 6067 status = nfs4_check_deleg(cl, open, &dp); 6068 if (status) 6069 goto out; 6070 stp = nfsd4_find_and_lock_existing_open(fp, open); 6071 } else { 6072 open->op_file = NULL; 6073 status = nfserr_bad_stateid; 6074 if (nfsd4_is_deleg_cur(open)) 6075 goto out; 6076 } 6077 6078 if (!stp) { 6079 stp = init_open_stateid(fp, open); 6080 if (!open->op_stp) 6081 new_stp = true; 6082 } 6083 6084 /* 6085 * OPEN the file, or upgrade an existing OPEN. 6086 * If truncate fails, the OPEN fails. 6087 * 6088 * stp is already locked. 6089 */ 6090 if (!new_stp) { 6091 /* Stateid was found, this is an OPEN upgrade */ 6092 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); 6093 if (status) { 6094 mutex_unlock(&stp->st_mutex); 6095 goto out; 6096 } 6097 } else { 6098 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true); 6099 if (status) { 6100 release_open_stateid(stp); 6101 mutex_unlock(&stp->st_mutex); 6102 goto out; 6103 } 6104 6105 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, 6106 open->op_odstate); 6107 if (stp->st_clnt_odstate == open->op_odstate) 6108 open->op_odstate = NULL; 6109 } 6110 6111 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); 6112 mutex_unlock(&stp->st_mutex); 6113 6114 if (nfsd4_has_session(&resp->cstate)) { 6115 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { 6116 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; 6117 open->op_why_no_deleg = WND4_NOT_WANTED; 6118 goto nodeleg; 6119 } 6120 } 6121 6122 /* 6123 * Attempt to hand out a delegation. No error return, because the 6124 * OPEN succeeds even if we fail. 6125 */ 6126 nfs4_open_delegation(open, stp, &resp->cstate.current_fh); 6127 nodeleg: 6128 status = nfs_ok; 6129 trace_nfsd_open(&stp->st_stid.sc_stateid); 6130 out: 6131 /* 4.1 client trying to upgrade/downgrade delegation? */ 6132 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && 6133 open->op_deleg_want) 6134 nfsd4_deleg_xgrade_none_ext(open, dp); 6135 6136 if (fp) 6137 put_nfs4_file(fp); 6138 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) 6139 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; 6140 /* 6141 * To finish the open response, we just need to set the rflags. 6142 */ 6143 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; 6144 if (nfsd4_has_session(&resp->cstate)) 6145 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK; 6146 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED)) 6147 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; 6148 6149 if (dp) 6150 nfs4_put_stid(&dp->dl_stid); 6151 if (stp) 6152 nfs4_put_stid(&stp->st_stid); 6153 6154 return status; 6155 } 6156 6157 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, 6158 struct nfsd4_open *open) 6159 { 6160 if (open->op_openowner) 6161 nfs4_put_stateowner(&open->op_openowner->oo_owner); 6162 if (open->op_file) 6163 kmem_cache_free(file_slab, open->op_file); 6164 if (open->op_stp) 6165 nfs4_put_stid(&open->op_stp->st_stid); 6166 if (open->op_odstate) 6167 kmem_cache_free(odstate_slab, open->op_odstate); 6168 } 6169 6170 __be32 6171 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 6172 union nfsd4_op_u *u) 6173 { 6174 clientid_t *clid = &u->renew; 6175 struct nfs4_client *clp; 6176 __be32 status; 6177 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 6178 6179 trace_nfsd_clid_renew(clid); 6180 status = set_client(clid, cstate, nn); 6181 if (status) 6182 return status; 6183 clp = cstate->clp; 6184 if (!list_empty(&clp->cl_delegations) 6185 && clp->cl_cb_state != NFSD4_CB_UP) 6186 return nfserr_cb_path_down; 6187 return nfs_ok; 6188 } 6189 6190 void 6191 nfsd4_end_grace(struct nfsd_net *nn) 6192 { 6193 /* do nothing if grace period already ended */ 6194 if (nn->grace_ended) 6195 return; 6196 6197 trace_nfsd_grace_complete(nn); 6198 nn->grace_ended = true; 6199 /* 6200 * If the server goes down again right now, an NFSv4 6201 * client will still be allowed to reclaim after it comes back up, 6202 * even if it hasn't yet had a chance to reclaim state this time. 6203 * 6204 */ 6205 nfsd4_record_grace_done(nn); 6206 /* 6207 * At this point, NFSv4 clients can still reclaim. But if the 6208 * server crashes, any that have not yet reclaimed will be out 6209 * of luck on the next boot. 6210 * 6211 * (NFSv4.1+ clients are considered to have reclaimed once they 6212 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to 6213 * have reclaimed after their first OPEN.) 6214 */ 6215 locks_end_grace(&nn->nfsd4_manager); 6216 /* 6217 * At this point, and once lockd and/or any other containers 6218 * exit their grace period, further reclaims will fail and 6219 * regular locking can resume. 6220 */ 6221 } 6222 6223 /* 6224 * If we've waited a lease period but there are still clients trying to 6225 * reclaim, wait a little longer to give them a chance to finish. 6226 */ 6227 static bool clients_still_reclaiming(struct nfsd_net *nn) 6228 { 6229 time64_t double_grace_period_end = nn->boot_time + 6230 2 * nn->nfsd4_lease; 6231 6232 if (nn->track_reclaim_completes && 6233 atomic_read(&nn->nr_reclaim_complete) == 6234 nn->reclaim_str_hashtbl_size) 6235 return false; 6236 if (!nn->somebody_reclaimed) 6237 return false; 6238 nn->somebody_reclaimed = false; 6239 /* 6240 * If we've given them *two* lease times to reclaim, and they're 6241 * still not done, give up: 6242 */ 6243 if (ktime_get_boottime_seconds() > double_grace_period_end) 6244 return false; 6245 return true; 6246 } 6247 6248 struct laundry_time { 6249 time64_t cutoff; 6250 time64_t new_timeo; 6251 }; 6252 6253 static bool state_expired(struct laundry_time *lt, time64_t last_refresh) 6254 { 6255 time64_t time_remaining; 6256 6257 if (last_refresh < lt->cutoff) 6258 return true; 6259 time_remaining = last_refresh - lt->cutoff; 6260 lt->new_timeo = min(lt->new_timeo, time_remaining); 6261 return false; 6262 } 6263 6264 #ifdef CONFIG_NFSD_V4_2_INTER_SSC 6265 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn) 6266 { 6267 spin_lock_init(&nn->nfsd_ssc_lock); 6268 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list); 6269 init_waitqueue_head(&nn->nfsd_ssc_waitq); 6270 } 6271 EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work); 6272 6273 /* 6274 * This is called when nfsd is being shutdown, after all inter_ssc 6275 * cleanup were done, to destroy the ssc delayed unmount list. 6276 */ 6277 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn) 6278 { 6279 struct nfsd4_ssc_umount_item *ni = NULL; 6280 struct nfsd4_ssc_umount_item *tmp; 6281 6282 spin_lock(&nn->nfsd_ssc_lock); 6283 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) { 6284 list_del(&ni->nsui_list); 6285 spin_unlock(&nn->nfsd_ssc_lock); 6286 mntput(ni->nsui_vfsmount); 6287 kfree(ni); 6288 spin_lock(&nn->nfsd_ssc_lock); 6289 } 6290 spin_unlock(&nn->nfsd_ssc_lock); 6291 } 6292 6293 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn) 6294 { 6295 bool do_wakeup = false; 6296 struct nfsd4_ssc_umount_item *ni = NULL; 6297 struct nfsd4_ssc_umount_item *tmp; 6298 6299 spin_lock(&nn->nfsd_ssc_lock); 6300 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) { 6301 if (time_after(jiffies, ni->nsui_expire)) { 6302 if (refcount_read(&ni->nsui_refcnt) > 1) 6303 continue; 6304 6305 /* mark being unmount */ 6306 ni->nsui_busy = true; 6307 spin_unlock(&nn->nfsd_ssc_lock); 6308 mntput(ni->nsui_vfsmount); 6309 spin_lock(&nn->nfsd_ssc_lock); 6310 6311 /* waiters need to start from begin of list */ 6312 list_del(&ni->nsui_list); 6313 kfree(ni); 6314 6315 /* wakeup ssc_connect waiters */ 6316 do_wakeup = true; 6317 continue; 6318 } 6319 break; 6320 } 6321 if (do_wakeup) 6322 wake_up_all(&nn->nfsd_ssc_waitq); 6323 spin_unlock(&nn->nfsd_ssc_lock); 6324 } 6325 #endif 6326 6327 /* Check if any lock belonging to this lockowner has any blockers */ 6328 static bool 6329 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo) 6330 { 6331 struct file_lock_context *ctx; 6332 struct nfs4_ol_stateid *stp; 6333 struct nfs4_file *nf; 6334 6335 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) { 6336 nf = stp->st_stid.sc_file; 6337 ctx = locks_inode_context(nf->fi_inode); 6338 if (!ctx) 6339 continue; 6340 if (locks_owner_has_blockers(ctx, lo)) 6341 return true; 6342 } 6343 return false; 6344 } 6345 6346 static bool 6347 nfs4_anylock_blockers(struct nfs4_client *clp) 6348 { 6349 int i; 6350 struct nfs4_stateowner *so; 6351 struct nfs4_lockowner *lo; 6352 6353 if (atomic_read(&clp->cl_delegs_in_recall)) 6354 return true; 6355 spin_lock(&clp->cl_lock); 6356 for (i = 0; i < OWNER_HASH_SIZE; i++) { 6357 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i], 6358 so_strhash) { 6359 if (so->so_is_open_owner) 6360 continue; 6361 lo = lockowner(so); 6362 if (nfs4_lockowner_has_blockers(lo)) { 6363 spin_unlock(&clp->cl_lock); 6364 return true; 6365 } 6366 } 6367 } 6368 spin_unlock(&clp->cl_lock); 6369 return false; 6370 } 6371 6372 static void 6373 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist, 6374 struct laundry_time *lt) 6375 { 6376 unsigned int maxreap, reapcnt = 0; 6377 struct list_head *pos, *next; 6378 struct nfs4_client *clp; 6379 6380 maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ? 6381 NFSD_CLIENT_MAX_TRIM_PER_RUN : 0; 6382 INIT_LIST_HEAD(reaplist); 6383 spin_lock(&nn->client_lock); 6384 list_for_each_safe(pos, next, &nn->client_lru) { 6385 clp = list_entry(pos, struct nfs4_client, cl_lru); 6386 if (clp->cl_state == NFSD4_EXPIRABLE) 6387 goto exp_client; 6388 if (!state_expired(lt, clp->cl_time)) 6389 break; 6390 if (!atomic_read(&clp->cl_rpc_users)) { 6391 if (clp->cl_state == NFSD4_ACTIVE) 6392 atomic_inc(&nn->nfsd_courtesy_clients); 6393 clp->cl_state = NFSD4_COURTESY; 6394 } 6395 if (!client_has_state(clp)) 6396 goto exp_client; 6397 if (!nfs4_anylock_blockers(clp)) 6398 if (reapcnt >= maxreap) 6399 continue; 6400 exp_client: 6401 if (!mark_client_expired_locked(clp)) { 6402 list_add(&clp->cl_lru, reaplist); 6403 reapcnt++; 6404 } 6405 } 6406 spin_unlock(&nn->client_lock); 6407 } 6408 6409 static void 6410 nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn, 6411 struct list_head *reaplist) 6412 { 6413 unsigned int maxreap = 0, reapcnt = 0; 6414 struct list_head *pos, *next; 6415 struct nfs4_client *clp; 6416 6417 maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN; 6418 INIT_LIST_HEAD(reaplist); 6419 6420 spin_lock(&nn->client_lock); 6421 list_for_each_safe(pos, next, &nn->client_lru) { 6422 clp = list_entry(pos, struct nfs4_client, cl_lru); 6423 if (clp->cl_state == NFSD4_ACTIVE) 6424 break; 6425 if (reapcnt >= maxreap) 6426 break; 6427 if (!mark_client_expired_locked(clp)) { 6428 list_add(&clp->cl_lru, reaplist); 6429 reapcnt++; 6430 } 6431 } 6432 spin_unlock(&nn->client_lock); 6433 } 6434 6435 static void 6436 nfs4_process_client_reaplist(struct list_head *reaplist) 6437 { 6438 struct list_head *pos, *next; 6439 struct nfs4_client *clp; 6440 6441 list_for_each_safe(pos, next, reaplist) { 6442 clp = list_entry(pos, struct nfs4_client, cl_lru); 6443 trace_nfsd_clid_purged(&clp->cl_clientid); 6444 list_del_init(&clp->cl_lru); 6445 expire_client(clp); 6446 } 6447 } 6448 6449 static void nfs40_clean_admin_revoked(struct nfsd_net *nn, 6450 struct laundry_time *lt) 6451 { 6452 struct nfs4_client *clp; 6453 6454 spin_lock(&nn->client_lock); 6455 if (nn->nfs40_last_revoke == 0 || 6456 nn->nfs40_last_revoke > lt->cutoff) { 6457 spin_unlock(&nn->client_lock); 6458 return; 6459 } 6460 nn->nfs40_last_revoke = 0; 6461 6462 retry: 6463 list_for_each_entry(clp, &nn->client_lru, cl_lru) { 6464 unsigned long id, tmp; 6465 struct nfs4_stid *stid; 6466 6467 if (atomic_read(&clp->cl_admin_revoked) == 0) 6468 continue; 6469 6470 spin_lock(&clp->cl_lock); 6471 idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id) 6472 if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) { 6473 refcount_inc(&stid->sc_count); 6474 spin_unlock(&nn->client_lock); 6475 /* this function drops ->cl_lock */ 6476 nfsd4_drop_revoked_stid(stid); 6477 nfs4_put_stid(stid); 6478 spin_lock(&nn->client_lock); 6479 goto retry; 6480 } 6481 spin_unlock(&clp->cl_lock); 6482 } 6483 spin_unlock(&nn->client_lock); 6484 } 6485 6486 static time64_t 6487 nfs4_laundromat(struct nfsd_net *nn) 6488 { 6489 struct nfs4_openowner *oo; 6490 struct nfs4_delegation *dp; 6491 struct nfs4_ol_stateid *stp; 6492 struct nfsd4_blocked_lock *nbl; 6493 struct list_head *pos, *next, reaplist; 6494 struct laundry_time lt = { 6495 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease, 6496 .new_timeo = nn->nfsd4_lease 6497 }; 6498 struct nfs4_cpntf_state *cps; 6499 copy_stateid_t *cps_t; 6500 int i; 6501 6502 if (clients_still_reclaiming(nn)) { 6503 lt.new_timeo = 0; 6504 goto out; 6505 } 6506 nfsd4_end_grace(nn); 6507 6508 spin_lock(&nn->s2s_cp_lock); 6509 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) { 6510 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid); 6511 if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID && 6512 state_expired(<, cps->cpntf_time)) 6513 _free_cpntf_state_locked(nn, cps); 6514 } 6515 spin_unlock(&nn->s2s_cp_lock); 6516 nfs4_get_client_reaplist(nn, &reaplist, <); 6517 nfs4_process_client_reaplist(&reaplist); 6518 6519 nfs40_clean_admin_revoked(nn, <); 6520 6521 spin_lock(&state_lock); 6522 list_for_each_safe(pos, next, &nn->del_recall_lru) { 6523 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); 6524 if (!state_expired(<, dp->dl_time)) 6525 break; 6526 unhash_delegation_locked(dp, SC_STATUS_REVOKED); 6527 list_add(&dp->dl_recall_lru, &reaplist); 6528 } 6529 spin_unlock(&state_lock); 6530 while (!list_empty(&reaplist)) { 6531 dp = list_first_entry(&reaplist, struct nfs4_delegation, 6532 dl_recall_lru); 6533 list_del_init(&dp->dl_recall_lru); 6534 revoke_delegation(dp); 6535 } 6536 6537 spin_lock(&nn->client_lock); 6538 while (!list_empty(&nn->close_lru)) { 6539 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, 6540 oo_close_lru); 6541 if (!state_expired(<, oo->oo_time)) 6542 break; 6543 list_del_init(&oo->oo_close_lru); 6544 stp = oo->oo_last_closed_stid; 6545 oo->oo_last_closed_stid = NULL; 6546 spin_unlock(&nn->client_lock); 6547 nfs4_put_stid(&stp->st_stid); 6548 spin_lock(&nn->client_lock); 6549 } 6550 spin_unlock(&nn->client_lock); 6551 6552 /* 6553 * It's possible for a client to try and acquire an already held lock 6554 * that is being held for a long time, and then lose interest in it. 6555 * So, we clean out any un-revisited request after a lease period 6556 * under the assumption that the client is no longer interested. 6557 * 6558 * RFC5661, sec. 9.6 states that the client must not rely on getting 6559 * notifications and must continue to poll for locks, even when the 6560 * server supports them. Thus this shouldn't lead to clients blocking 6561 * indefinitely once the lock does become free. 6562 */ 6563 BUG_ON(!list_empty(&reaplist)); 6564 spin_lock(&nn->blocked_locks_lock); 6565 while (!list_empty(&nn->blocked_locks_lru)) { 6566 nbl = list_first_entry(&nn->blocked_locks_lru, 6567 struct nfsd4_blocked_lock, nbl_lru); 6568 if (!state_expired(<, nbl->nbl_time)) 6569 break; 6570 list_move(&nbl->nbl_lru, &reaplist); 6571 list_del_init(&nbl->nbl_list); 6572 } 6573 spin_unlock(&nn->blocked_locks_lock); 6574 6575 while (!list_empty(&reaplist)) { 6576 nbl = list_first_entry(&reaplist, 6577 struct nfsd4_blocked_lock, nbl_lru); 6578 list_del_init(&nbl->nbl_lru); 6579 free_blocked_lock(nbl); 6580 } 6581 #ifdef CONFIG_NFSD_V4_2_INTER_SSC 6582 /* service the server-to-server copy delayed unmount list */ 6583 nfsd4_ssc_expire_umount(nn); 6584 #endif 6585 if (atomic_long_read(&num_delegations) >= max_delegations) 6586 deleg_reaper(nn); 6587 out: 6588 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); 6589 } 6590 6591 static void laundromat_main(struct work_struct *); 6592 6593 static void 6594 laundromat_main(struct work_struct *laundry) 6595 { 6596 time64_t t; 6597 struct delayed_work *dwork = to_delayed_work(laundry); 6598 struct nfsd_net *nn = container_of(dwork, struct nfsd_net, 6599 laundromat_work); 6600 6601 t = nfs4_laundromat(nn); 6602 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); 6603 } 6604 6605 static void 6606 courtesy_client_reaper(struct nfsd_net *nn) 6607 { 6608 struct list_head reaplist; 6609 6610 nfs4_get_courtesy_client_reaplist(nn, &reaplist); 6611 nfs4_process_client_reaplist(&reaplist); 6612 } 6613 6614 static void 6615 deleg_reaper(struct nfsd_net *nn) 6616 { 6617 struct list_head *pos, *next; 6618 struct nfs4_client *clp; 6619 struct list_head cblist; 6620 6621 INIT_LIST_HEAD(&cblist); 6622 spin_lock(&nn->client_lock); 6623 list_for_each_safe(pos, next, &nn->client_lru) { 6624 clp = list_entry(pos, struct nfs4_client, cl_lru); 6625 if (clp->cl_state != NFSD4_ACTIVE || 6626 list_empty(&clp->cl_delegations) || 6627 atomic_read(&clp->cl_delegs_in_recall) || 6628 test_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags) || 6629 (ktime_get_boottime_seconds() - 6630 clp->cl_ra_time < 5)) { 6631 continue; 6632 } 6633 list_add(&clp->cl_ra_cblist, &cblist); 6634 6635 /* release in nfsd4_cb_recall_any_release */ 6636 kref_get(&clp->cl_nfsdfs.cl_ref); 6637 set_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags); 6638 clp->cl_ra_time = ktime_get_boottime_seconds(); 6639 } 6640 spin_unlock(&nn->client_lock); 6641 6642 while (!list_empty(&cblist)) { 6643 clp = list_first_entry(&cblist, struct nfs4_client, 6644 cl_ra_cblist); 6645 list_del_init(&clp->cl_ra_cblist); 6646 clp->cl_ra->ra_keep = 0; 6647 clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG); 6648 clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG) | 6649 BIT(RCA4_TYPE_MASK_WDATA_DLG); 6650 trace_nfsd_cb_recall_any(clp->cl_ra); 6651 nfsd4_run_cb(&clp->cl_ra->ra_cb); 6652 } 6653 } 6654 6655 static void 6656 nfsd4_state_shrinker_worker(struct work_struct *work) 6657 { 6658 struct nfsd_net *nn = container_of(work, struct nfsd_net, 6659 nfsd_shrinker_work); 6660 6661 courtesy_client_reaper(nn); 6662 deleg_reaper(nn); 6663 } 6664 6665 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp) 6666 { 6667 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) 6668 return nfserr_bad_stateid; 6669 return nfs_ok; 6670 } 6671 6672 static 6673 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) 6674 { 6675 __be32 status = nfserr_openmode; 6676 6677 /* For lock stateid's, we test the parent open, not the lock: */ 6678 if (stp->st_openstp) 6679 stp = stp->st_openstp; 6680 if ((flags & WR_STATE) && !access_permit_write(stp)) 6681 goto out; 6682 if ((flags & RD_STATE) && !access_permit_read(stp)) 6683 goto out; 6684 status = nfs_ok; 6685 out: 6686 return status; 6687 } 6688 6689 static inline __be32 6690 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags) 6691 { 6692 if (ONE_STATEID(stateid) && (flags & RD_STATE)) 6693 return nfs_ok; 6694 else if (opens_in_grace(net)) { 6695 /* Answer in remaining cases depends on existence of 6696 * conflicting state; so we must wait out the grace period. */ 6697 return nfserr_grace; 6698 } else if (flags & WR_STATE) 6699 return nfs4_share_conflict(current_fh, 6700 NFS4_SHARE_DENY_WRITE); 6701 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ 6702 return nfs4_share_conflict(current_fh, 6703 NFS4_SHARE_DENY_READ); 6704 } 6705 6706 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session) 6707 { 6708 /* 6709 * When sessions are used the stateid generation number is ignored 6710 * when it is zero. 6711 */ 6712 if (has_session && in->si_generation == 0) 6713 return nfs_ok; 6714 6715 if (in->si_generation == ref->si_generation) 6716 return nfs_ok; 6717 6718 /* If the client sends us a stateid from the future, it's buggy: */ 6719 if (nfsd4_stateid_generation_after(in, ref)) 6720 return nfserr_bad_stateid; 6721 /* 6722 * However, we could see a stateid from the past, even from a 6723 * non-buggy client. For example, if the client sends a lock 6724 * while some IO is outstanding, the lock may bump si_generation 6725 * while the IO is still in flight. The client could avoid that 6726 * situation by waiting for responses on all the IO requests, 6727 * but better performance may result in retrying IO that 6728 * receives an old_stateid error if requests are rarely 6729 * reordered in flight: 6730 */ 6731 return nfserr_old_stateid; 6732 } 6733 6734 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session) 6735 { 6736 __be32 ret; 6737 6738 spin_lock(&s->sc_lock); 6739 ret = nfsd4_verify_open_stid(s); 6740 if (ret == nfs_ok) 6741 ret = check_stateid_generation(in, &s->sc_stateid, has_session); 6742 spin_unlock(&s->sc_lock); 6743 if (ret == nfserr_admin_revoked) 6744 nfsd40_drop_revoked_stid(s->sc_client, 6745 &s->sc_stateid); 6746 return ret; 6747 } 6748 6749 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) 6750 { 6751 if (ols->st_stateowner->so_is_open_owner && 6752 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) 6753 return nfserr_bad_stateid; 6754 return nfs_ok; 6755 } 6756 6757 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) 6758 { 6759 struct nfs4_stid *s; 6760 __be32 status = nfserr_bad_stateid; 6761 6762 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || 6763 CLOSE_STATEID(stateid)) 6764 return status; 6765 spin_lock(&cl->cl_lock); 6766 s = find_stateid_locked(cl, stateid); 6767 if (!s) 6768 goto out_unlock; 6769 status = nfsd4_stid_check_stateid_generation(stateid, s, 1); 6770 if (status) 6771 goto out_unlock; 6772 status = nfsd4_verify_open_stid(s); 6773 if (status) 6774 goto out_unlock; 6775 6776 switch (s->sc_type) { 6777 case SC_TYPE_DELEG: 6778 status = nfs_ok; 6779 break; 6780 case SC_TYPE_OPEN: 6781 case SC_TYPE_LOCK: 6782 status = nfsd4_check_openowner_confirmed(openlockstateid(s)); 6783 break; 6784 default: 6785 printk("unknown stateid type %x\n", s->sc_type); 6786 status = nfserr_bad_stateid; 6787 } 6788 out_unlock: 6789 spin_unlock(&cl->cl_lock); 6790 if (status == nfserr_admin_revoked) 6791 nfsd40_drop_revoked_stid(cl, stateid); 6792 return status; 6793 } 6794 6795 __be32 6796 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, 6797 stateid_t *stateid, 6798 unsigned short typemask, unsigned short statusmask, 6799 struct nfs4_stid **s, struct nfsd_net *nn) 6800 { 6801 __be32 status; 6802 struct nfs4_stid *stid; 6803 bool return_revoked = false; 6804 6805 /* 6806 * only return revoked delegations if explicitly asked. 6807 * otherwise we report revoked or bad_stateid status. 6808 */ 6809 if (statusmask & SC_STATUS_REVOKED) 6810 return_revoked = true; 6811 if (typemask & SC_TYPE_DELEG) 6812 /* Always allow REVOKED for DELEG so we can 6813 * retturn the appropriate error. 6814 */ 6815 statusmask |= SC_STATUS_REVOKED; 6816 6817 statusmask |= SC_STATUS_ADMIN_REVOKED; 6818 6819 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || 6820 CLOSE_STATEID(stateid)) 6821 return nfserr_bad_stateid; 6822 status = set_client(&stateid->si_opaque.so_clid, cstate, nn); 6823 if (status == nfserr_stale_clientid) { 6824 if (cstate->session) 6825 return nfserr_bad_stateid; 6826 return nfserr_stale_stateid; 6827 } 6828 if (status) 6829 return status; 6830 stid = find_stateid_by_type(cstate->clp, stateid, typemask, statusmask); 6831 if (!stid) 6832 return nfserr_bad_stateid; 6833 if ((stid->sc_status & SC_STATUS_REVOKED) && !return_revoked) { 6834 nfs4_put_stid(stid); 6835 return nfserr_deleg_revoked; 6836 } 6837 if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) { 6838 nfsd40_drop_revoked_stid(cstate->clp, stateid); 6839 nfs4_put_stid(stid); 6840 return nfserr_admin_revoked; 6841 } 6842 *s = stid; 6843 return nfs_ok; 6844 } 6845 6846 static struct nfsd_file * 6847 nfs4_find_file(struct nfs4_stid *s, int flags) 6848 { 6849 struct nfsd_file *ret = NULL; 6850 6851 if (!s || s->sc_status) 6852 return NULL; 6853 6854 switch (s->sc_type) { 6855 case SC_TYPE_DELEG: 6856 spin_lock(&s->sc_file->fi_lock); 6857 ret = nfsd_file_get(s->sc_file->fi_deleg_file); 6858 spin_unlock(&s->sc_file->fi_lock); 6859 break; 6860 case SC_TYPE_OPEN: 6861 case SC_TYPE_LOCK: 6862 if (flags & RD_STATE) 6863 ret = find_readable_file(s->sc_file); 6864 else 6865 ret = find_writeable_file(s->sc_file); 6866 } 6867 6868 return ret; 6869 } 6870 6871 static __be32 6872 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags) 6873 { 6874 __be32 status; 6875 6876 status = nfsd4_check_openowner_confirmed(ols); 6877 if (status) 6878 return status; 6879 return nfs4_check_openmode(ols, flags); 6880 } 6881 6882 static __be32 6883 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s, 6884 struct nfsd_file **nfp, int flags) 6885 { 6886 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; 6887 struct nfsd_file *nf; 6888 __be32 status; 6889 6890 nf = nfs4_find_file(s, flags); 6891 if (nf) { 6892 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, 6893 acc | NFSD_MAY_OWNER_OVERRIDE); 6894 if (status) { 6895 nfsd_file_put(nf); 6896 goto out; 6897 } 6898 } else { 6899 status = nfsd_file_acquire(rqstp, fhp, acc, &nf); 6900 if (status) 6901 return status; 6902 } 6903 *nfp = nf; 6904 out: 6905 return status; 6906 } 6907 static void 6908 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) 6909 { 6910 WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID); 6911 if (!refcount_dec_and_test(&cps->cp_stateid.cs_count)) 6912 return; 6913 list_del(&cps->cp_list); 6914 idr_remove(&nn->s2s_cp_stateids, 6915 cps->cp_stateid.cs_stid.si_opaque.so_id); 6916 kfree(cps); 6917 } 6918 /* 6919 * A READ from an inter server to server COPY will have a 6920 * copy stateid. Look up the copy notify stateid from the 6921 * idr structure and take a reference on it. 6922 */ 6923 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st, 6924 struct nfs4_client *clp, 6925 struct nfs4_cpntf_state **cps) 6926 { 6927 copy_stateid_t *cps_t; 6928 struct nfs4_cpntf_state *state = NULL; 6929 6930 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id) 6931 return nfserr_bad_stateid; 6932 spin_lock(&nn->s2s_cp_lock); 6933 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id); 6934 if (cps_t) { 6935 state = container_of(cps_t, struct nfs4_cpntf_state, 6936 cp_stateid); 6937 if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) { 6938 state = NULL; 6939 goto unlock; 6940 } 6941 if (!clp) 6942 refcount_inc(&state->cp_stateid.cs_count); 6943 else 6944 _free_cpntf_state_locked(nn, state); 6945 } 6946 unlock: 6947 spin_unlock(&nn->s2s_cp_lock); 6948 if (!state) 6949 return nfserr_bad_stateid; 6950 if (!clp) 6951 *cps = state; 6952 return 0; 6953 } 6954 6955 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st, 6956 struct nfs4_stid **stid) 6957 { 6958 __be32 status; 6959 struct nfs4_cpntf_state *cps = NULL; 6960 struct nfs4_client *found; 6961 6962 status = manage_cpntf_state(nn, st, NULL, &cps); 6963 if (status) 6964 return status; 6965 6966 cps->cpntf_time = ktime_get_boottime_seconds(); 6967 6968 status = nfserr_expired; 6969 found = lookup_clientid(&cps->cp_p_clid, true, nn); 6970 if (!found) 6971 goto out; 6972 6973 *stid = find_stateid_by_type(found, &cps->cp_p_stateid, 6974 SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK, 6975 0); 6976 if (*stid) 6977 status = nfs_ok; 6978 else 6979 status = nfserr_bad_stateid; 6980 6981 put_client_renew(found); 6982 out: 6983 nfs4_put_cpntf_state(nn, cps); 6984 return status; 6985 } 6986 6987 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) 6988 { 6989 spin_lock(&nn->s2s_cp_lock); 6990 _free_cpntf_state_locked(nn, cps); 6991 spin_unlock(&nn->s2s_cp_lock); 6992 } 6993 6994 /** 6995 * nfs4_preprocess_stateid_op - find and prep stateid for an operation 6996 * @rqstp: incoming request from client 6997 * @cstate: current compound state 6998 * @fhp: filehandle associated with requested stateid 6999 * @stateid: stateid (provided by client) 7000 * @flags: flags describing type of operation to be done 7001 * @nfp: optional nfsd_file return pointer (may be NULL) 7002 * @cstid: optional returned nfs4_stid pointer (may be NULL) 7003 * 7004 * Given info from the client, look up a nfs4_stid for the operation. On 7005 * success, it returns a reference to the nfs4_stid and/or the nfsd_file 7006 * associated with it. 7007 */ 7008 __be32 7009 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, 7010 struct nfsd4_compound_state *cstate, struct svc_fh *fhp, 7011 stateid_t *stateid, int flags, struct nfsd_file **nfp, 7012 struct nfs4_stid **cstid) 7013 { 7014 struct net *net = SVC_NET(rqstp); 7015 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 7016 struct nfs4_stid *s = NULL; 7017 __be32 status; 7018 7019 if (nfp) 7020 *nfp = NULL; 7021 7022 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { 7023 if (cstid) 7024 status = nfserr_bad_stateid; 7025 else 7026 status = check_special_stateids(net, fhp, stateid, 7027 flags); 7028 goto done; 7029 } 7030 7031 status = nfsd4_lookup_stateid(cstate, stateid, 7032 SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK, 7033 0, &s, nn); 7034 if (status == nfserr_bad_stateid) 7035 status = find_cpntf_state(nn, stateid, &s); 7036 if (status) 7037 return status; 7038 status = nfsd4_stid_check_stateid_generation(stateid, s, 7039 nfsd4_has_session(cstate)); 7040 if (status) 7041 goto out; 7042 7043 switch (s->sc_type) { 7044 case SC_TYPE_DELEG: 7045 status = nfs4_check_delegmode(delegstateid(s), flags); 7046 break; 7047 case SC_TYPE_OPEN: 7048 case SC_TYPE_LOCK: 7049 status = nfs4_check_olstateid(openlockstateid(s), flags); 7050 break; 7051 } 7052 if (status) 7053 goto out; 7054 status = nfs4_check_fh(fhp, s); 7055 7056 done: 7057 if (status == nfs_ok && nfp) 7058 status = nfs4_check_file(rqstp, fhp, s, nfp, flags); 7059 out: 7060 if (s) { 7061 if (!status && cstid) 7062 *cstid = s; 7063 else 7064 nfs4_put_stid(s); 7065 } 7066 return status; 7067 } 7068 7069 /* 7070 * Test if the stateid is valid 7071 */ 7072 __be32 7073 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 7074 union nfsd4_op_u *u) 7075 { 7076 struct nfsd4_test_stateid *test_stateid = &u->test_stateid; 7077 struct nfsd4_test_stateid_id *stateid; 7078 struct nfs4_client *cl = cstate->clp; 7079 7080 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list) 7081 stateid->ts_id_status = 7082 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); 7083 7084 return nfs_ok; 7085 } 7086 7087 static __be32 7088 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) 7089 { 7090 struct nfs4_ol_stateid *stp = openlockstateid(s); 7091 __be32 ret; 7092 7093 ret = nfsd4_lock_ol_stateid(stp); 7094 if (ret) 7095 goto out_put_stid; 7096 7097 ret = check_stateid_generation(stateid, &s->sc_stateid, 1); 7098 if (ret) 7099 goto out; 7100 7101 ret = nfserr_locks_held; 7102 if (check_for_locks(stp->st_stid.sc_file, 7103 lockowner(stp->st_stateowner))) 7104 goto out; 7105 7106 release_lock_stateid(stp); 7107 ret = nfs_ok; 7108 7109 out: 7110 mutex_unlock(&stp->st_mutex); 7111 out_put_stid: 7112 nfs4_put_stid(s); 7113 return ret; 7114 } 7115 7116 __be32 7117 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 7118 union nfsd4_op_u *u) 7119 { 7120 struct nfsd4_free_stateid *free_stateid = &u->free_stateid; 7121 stateid_t *stateid = &free_stateid->fr_stateid; 7122 struct nfs4_stid *s; 7123 struct nfs4_delegation *dp; 7124 struct nfs4_client *cl = cstate->clp; 7125 __be32 ret = nfserr_bad_stateid; 7126 7127 spin_lock(&cl->cl_lock); 7128 s = find_stateid_locked(cl, stateid); 7129 if (!s || s->sc_status & SC_STATUS_CLOSED) 7130 goto out_unlock; 7131 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) { 7132 nfsd4_drop_revoked_stid(s); 7133 ret = nfs_ok; 7134 goto out; 7135 } 7136 spin_lock(&s->sc_lock); 7137 switch (s->sc_type) { 7138 case SC_TYPE_DELEG: 7139 if (s->sc_status & SC_STATUS_REVOKED) { 7140 spin_unlock(&s->sc_lock); 7141 dp = delegstateid(s); 7142 list_del_init(&dp->dl_recall_lru); 7143 spin_unlock(&cl->cl_lock); 7144 nfs4_put_stid(s); 7145 ret = nfs_ok; 7146 goto out; 7147 } 7148 ret = nfserr_locks_held; 7149 break; 7150 case SC_TYPE_OPEN: 7151 ret = check_stateid_generation(stateid, &s->sc_stateid, 1); 7152 if (ret) 7153 break; 7154 ret = nfserr_locks_held; 7155 break; 7156 case SC_TYPE_LOCK: 7157 spin_unlock(&s->sc_lock); 7158 refcount_inc(&s->sc_count); 7159 spin_unlock(&cl->cl_lock); 7160 ret = nfsd4_free_lock_stateid(stateid, s); 7161 goto out; 7162 } 7163 spin_unlock(&s->sc_lock); 7164 out_unlock: 7165 spin_unlock(&cl->cl_lock); 7166 out: 7167 return ret; 7168 } 7169 7170 static inline int 7171 setlkflg (int type) 7172 { 7173 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? 7174 RD_STATE : WR_STATE; 7175 } 7176 7177 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp) 7178 { 7179 struct svc_fh *current_fh = &cstate->current_fh; 7180 struct nfs4_stateowner *sop = stp->st_stateowner; 7181 __be32 status; 7182 7183 status = nfsd4_check_seqid(cstate, sop, seqid); 7184 if (status) 7185 return status; 7186 status = nfsd4_lock_ol_stateid(stp); 7187 if (status != nfs_ok) 7188 return status; 7189 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); 7190 if (status == nfs_ok) 7191 status = nfs4_check_fh(current_fh, &stp->st_stid); 7192 if (status != nfs_ok) 7193 mutex_unlock(&stp->st_mutex); 7194 return status; 7195 } 7196 7197 /** 7198 * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op 7199 * @cstate: compund state 7200 * @seqid: seqid (provided by client) 7201 * @stateid: stateid (provided by client) 7202 * @typemask: mask of allowable types for this operation 7203 * @statusmask: mask of allowed states: 0 or STID_CLOSED 7204 * @stpp: return pointer for the stateid found 7205 * @nn: net namespace for request 7206 * 7207 * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and 7208 * return it in @stpp. On a nfs_ok return, the returned stateid will 7209 * have its st_mutex locked. 7210 */ 7211 static __be32 7212 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, 7213 stateid_t *stateid, 7214 unsigned short typemask, unsigned short statusmask, 7215 struct nfs4_ol_stateid **stpp, 7216 struct nfsd_net *nn) 7217 { 7218 __be32 status; 7219 struct nfs4_stid *s; 7220 struct nfs4_ol_stateid *stp = NULL; 7221 7222 trace_nfsd_preprocess(seqid, stateid); 7223 7224 *stpp = NULL; 7225 retry: 7226 status = nfsd4_lookup_stateid(cstate, stateid, 7227 typemask, statusmask, &s, nn); 7228 if (status) 7229 return status; 7230 stp = openlockstateid(s); 7231 if (nfsd4_cstate_assign_replay(cstate, stp->st_stateowner) == -EAGAIN) { 7232 nfs4_put_stateowner(stp->st_stateowner); 7233 goto retry; 7234 } 7235 7236 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); 7237 if (!status) 7238 *stpp = stp; 7239 else 7240 nfs4_put_stid(&stp->st_stid); 7241 return status; 7242 } 7243 7244 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, 7245 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn) 7246 { 7247 __be32 status; 7248 struct nfs4_openowner *oo; 7249 struct nfs4_ol_stateid *stp; 7250 7251 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, 7252 SC_TYPE_OPEN, 0, &stp, nn); 7253 if (status) 7254 return status; 7255 oo = openowner(stp->st_stateowner); 7256 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { 7257 mutex_unlock(&stp->st_mutex); 7258 nfs4_put_stid(&stp->st_stid); 7259 return nfserr_bad_stateid; 7260 } 7261 *stpp = stp; 7262 return nfs_ok; 7263 } 7264 7265 __be32 7266 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 7267 union nfsd4_op_u *u) 7268 { 7269 struct nfsd4_open_confirm *oc = &u->open_confirm; 7270 __be32 status; 7271 struct nfs4_openowner *oo; 7272 struct nfs4_ol_stateid *stp; 7273 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 7274 7275 dprintk("NFSD: nfsd4_open_confirm on file %pd\n", 7276 cstate->current_fh.fh_dentry); 7277 7278 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); 7279 if (status) 7280 return status; 7281 7282 status = nfs4_preprocess_seqid_op(cstate, 7283 oc->oc_seqid, &oc->oc_req_stateid, 7284 SC_TYPE_OPEN, 0, &stp, nn); 7285 if (status) 7286 goto out; 7287 oo = openowner(stp->st_stateowner); 7288 status = nfserr_bad_stateid; 7289 if (oo->oo_flags & NFS4_OO_CONFIRMED) { 7290 mutex_unlock(&stp->st_mutex); 7291 goto put_stateid; 7292 } 7293 oo->oo_flags |= NFS4_OO_CONFIRMED; 7294 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); 7295 mutex_unlock(&stp->st_mutex); 7296 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid); 7297 nfsd4_client_record_create(oo->oo_owner.so_client); 7298 status = nfs_ok; 7299 put_stateid: 7300 nfs4_put_stid(&stp->st_stid); 7301 out: 7302 nfsd4_bump_seqid(cstate, status); 7303 return status; 7304 } 7305 7306 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access) 7307 { 7308 if (!test_access(access, stp)) 7309 return; 7310 nfs4_file_put_access(stp->st_stid.sc_file, access); 7311 clear_access(access, stp); 7312 } 7313 7314 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) 7315 { 7316 switch (to_access) { 7317 case NFS4_SHARE_ACCESS_READ: 7318 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); 7319 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); 7320 break; 7321 case NFS4_SHARE_ACCESS_WRITE: 7322 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); 7323 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); 7324 break; 7325 case NFS4_SHARE_ACCESS_BOTH: 7326 break; 7327 default: 7328 WARN_ON_ONCE(1); 7329 } 7330 } 7331 7332 __be32 7333 nfsd4_open_downgrade(struct svc_rqst *rqstp, 7334 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 7335 { 7336 struct nfsd4_open_downgrade *od = &u->open_downgrade; 7337 __be32 status; 7338 struct nfs4_ol_stateid *stp; 7339 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 7340 7341 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", 7342 cstate->current_fh.fh_dentry); 7343 7344 /* We don't yet support WANT bits: */ 7345 if (od->od_deleg_want) 7346 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, 7347 od->od_deleg_want); 7348 7349 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, 7350 &od->od_stateid, &stp, nn); 7351 if (status) 7352 goto out; 7353 status = nfserr_inval; 7354 if (!test_access(od->od_share_access, stp)) { 7355 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n", 7356 stp->st_access_bmap, od->od_share_access); 7357 goto put_stateid; 7358 } 7359 if (!test_deny(od->od_share_deny, stp)) { 7360 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n", 7361 stp->st_deny_bmap, od->od_share_deny); 7362 goto put_stateid; 7363 } 7364 nfs4_stateid_downgrade(stp, od->od_share_access); 7365 reset_union_bmap_deny(od->od_share_deny, stp); 7366 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); 7367 status = nfs_ok; 7368 put_stateid: 7369 mutex_unlock(&stp->st_mutex); 7370 nfs4_put_stid(&stp->st_stid); 7371 out: 7372 nfsd4_bump_seqid(cstate, status); 7373 return status; 7374 } 7375 7376 static bool nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) 7377 { 7378 struct nfs4_client *clp = s->st_stid.sc_client; 7379 bool unhashed; 7380 LIST_HEAD(reaplist); 7381 struct nfs4_ol_stateid *stp; 7382 7383 spin_lock(&clp->cl_lock); 7384 unhashed = unhash_open_stateid(s, &reaplist); 7385 7386 if (clp->cl_minorversion) { 7387 if (unhashed) 7388 put_ol_stateid_locked(s, &reaplist); 7389 spin_unlock(&clp->cl_lock); 7390 list_for_each_entry(stp, &reaplist, st_locks) 7391 nfs4_free_cpntf_statelist(clp->net, &stp->st_stid); 7392 free_ol_stateid_reaplist(&reaplist); 7393 return false; 7394 } else { 7395 spin_unlock(&clp->cl_lock); 7396 free_ol_stateid_reaplist(&reaplist); 7397 return unhashed; 7398 } 7399 } 7400 7401 /* 7402 * nfs4_unlock_state() called after encode 7403 */ 7404 __be32 7405 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 7406 union nfsd4_op_u *u) 7407 { 7408 struct nfsd4_close *close = &u->close; 7409 __be32 status; 7410 struct nfs4_ol_stateid *stp; 7411 struct net *net = SVC_NET(rqstp); 7412 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 7413 bool need_move_to_close_list; 7414 7415 dprintk("NFSD: nfsd4_close on file %pd\n", 7416 cstate->current_fh.fh_dentry); 7417 7418 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, 7419 &close->cl_stateid, 7420 SC_TYPE_OPEN, SC_STATUS_CLOSED, 7421 &stp, nn); 7422 nfsd4_bump_seqid(cstate, status); 7423 if (status) 7424 goto out; 7425 7426 spin_lock(&stp->st_stid.sc_client->cl_lock); 7427 stp->st_stid.sc_status |= SC_STATUS_CLOSED; 7428 spin_unlock(&stp->st_stid.sc_client->cl_lock); 7429 7430 /* 7431 * Technically we don't _really_ have to increment or copy it, since 7432 * it should just be gone after this operation and we clobber the 7433 * copied value below, but we continue to do so here just to ensure 7434 * that racing ops see that there was a state change. 7435 */ 7436 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); 7437 7438 need_move_to_close_list = nfsd4_close_open_stateid(stp); 7439 mutex_unlock(&stp->st_mutex); 7440 if (need_move_to_close_list) 7441 move_to_close_lru(stp, net); 7442 7443 /* v4.1+ suggests that we send a special stateid in here, since the 7444 * clients should just ignore this anyway. Since this is not useful 7445 * for v4.0 clients either, we set it to the special close_stateid 7446 * universally. 7447 * 7448 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5 7449 */ 7450 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid)); 7451 7452 /* put reference from nfs4_preprocess_seqid_op */ 7453 nfs4_put_stid(&stp->st_stid); 7454 out: 7455 return status; 7456 } 7457 7458 __be32 7459 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 7460 union nfsd4_op_u *u) 7461 { 7462 struct nfsd4_delegreturn *dr = &u->delegreturn; 7463 struct nfs4_delegation *dp; 7464 stateid_t *stateid = &dr->dr_stateid; 7465 struct nfs4_stid *s; 7466 __be32 status; 7467 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 7468 7469 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) 7470 return status; 7471 7472 status = nfsd4_lookup_stateid(cstate, stateid, SC_TYPE_DELEG, 0, &s, nn); 7473 if (status) 7474 goto out; 7475 dp = delegstateid(s); 7476 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate)); 7477 if (status) 7478 goto put_stateid; 7479 7480 trace_nfsd_deleg_return(stateid); 7481 wake_up_var(d_inode(cstate->current_fh.fh_dentry)); 7482 destroy_delegation(dp); 7483 put_stateid: 7484 nfs4_put_stid(&dp->dl_stid); 7485 out: 7486 return status; 7487 } 7488 7489 /* last octet in a range */ 7490 static inline u64 7491 last_byte_offset(u64 start, u64 len) 7492 { 7493 u64 end; 7494 7495 WARN_ON_ONCE(!len); 7496 end = start + len; 7497 return end > start ? end - 1: NFS4_MAX_UINT64; 7498 } 7499 7500 /* 7501 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that 7502 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th 7503 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit 7504 * locking, this prevents us from being completely protocol-compliant. The 7505 * real solution to this problem is to start using unsigned file offsets in 7506 * the VFS, but this is a very deep change! 7507 */ 7508 static inline void 7509 nfs4_transform_lock_offset(struct file_lock *lock) 7510 { 7511 if (lock->fl_start < 0) 7512 lock->fl_start = OFFSET_MAX; 7513 if (lock->fl_end < 0) 7514 lock->fl_end = OFFSET_MAX; 7515 } 7516 7517 static fl_owner_t 7518 nfsd4_lm_get_owner(fl_owner_t owner) 7519 { 7520 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; 7521 7522 nfs4_get_stateowner(&lo->lo_owner); 7523 return owner; 7524 } 7525 7526 static void 7527 nfsd4_lm_put_owner(fl_owner_t owner) 7528 { 7529 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; 7530 7531 if (lo) 7532 nfs4_put_stateowner(&lo->lo_owner); 7533 } 7534 7535 /* return pointer to struct nfs4_client if client is expirable */ 7536 static bool 7537 nfsd4_lm_lock_expirable(struct file_lock *cfl) 7538 { 7539 struct nfs4_lockowner *lo = (struct nfs4_lockowner *) cfl->c.flc_owner; 7540 struct nfs4_client *clp = lo->lo_owner.so_client; 7541 struct nfsd_net *nn; 7542 7543 if (try_to_expire_client(clp)) { 7544 nn = net_generic(clp->net, nfsd_net_id); 7545 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); 7546 return true; 7547 } 7548 return false; 7549 } 7550 7551 /* schedule laundromat to run immediately and wait for it to complete */ 7552 static void 7553 nfsd4_lm_expire_lock(void) 7554 { 7555 flush_workqueue(laundry_wq); 7556 } 7557 7558 static void 7559 nfsd4_lm_notify(struct file_lock *fl) 7560 { 7561 struct nfs4_lockowner *lo = (struct nfs4_lockowner *) fl->c.flc_owner; 7562 struct net *net = lo->lo_owner.so_client->net; 7563 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 7564 struct nfsd4_blocked_lock *nbl = container_of(fl, 7565 struct nfsd4_blocked_lock, nbl_lock); 7566 bool queue = false; 7567 7568 /* An empty list means that something else is going to be using it */ 7569 spin_lock(&nn->blocked_locks_lock); 7570 if (!list_empty(&nbl->nbl_list)) { 7571 list_del_init(&nbl->nbl_list); 7572 list_del_init(&nbl->nbl_lru); 7573 queue = true; 7574 } 7575 spin_unlock(&nn->blocked_locks_lock); 7576 7577 if (queue) { 7578 trace_nfsd_cb_notify_lock(lo, nbl); 7579 nfsd4_run_cb(&nbl->nbl_cb); 7580 } 7581 } 7582 7583 static const struct lock_manager_operations nfsd_posix_mng_ops = { 7584 .lm_mod_owner = THIS_MODULE, 7585 .lm_notify = nfsd4_lm_notify, 7586 .lm_get_owner = nfsd4_lm_get_owner, 7587 .lm_put_owner = nfsd4_lm_put_owner, 7588 .lm_lock_expirable = nfsd4_lm_lock_expirable, 7589 .lm_expire_lock = nfsd4_lm_expire_lock, 7590 }; 7591 7592 static inline void 7593 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) 7594 { 7595 struct nfs4_lockowner *lo; 7596 7597 if (fl->fl_lmops == &nfsd_posix_mng_ops) { 7598 lo = (struct nfs4_lockowner *) fl->c.flc_owner; 7599 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner, 7600 GFP_KERNEL); 7601 if (!deny->ld_owner.data) 7602 /* We just don't care that much */ 7603 goto nevermind; 7604 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; 7605 } else { 7606 nevermind: 7607 deny->ld_owner.len = 0; 7608 deny->ld_owner.data = NULL; 7609 deny->ld_clientid.cl_boot = 0; 7610 deny->ld_clientid.cl_id = 0; 7611 } 7612 deny->ld_start = fl->fl_start; 7613 deny->ld_length = NFS4_MAX_UINT64; 7614 if (fl->fl_end != NFS4_MAX_UINT64) 7615 deny->ld_length = fl->fl_end - fl->fl_start + 1; 7616 deny->ld_type = NFS4_READ_LT; 7617 if (fl->c.flc_type != F_RDLCK) 7618 deny->ld_type = NFS4_WRITE_LT; 7619 } 7620 7621 static struct nfs4_lockowner * 7622 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) 7623 { 7624 unsigned int strhashval = ownerstr_hashval(owner); 7625 struct nfs4_stateowner *so; 7626 7627 lockdep_assert_held(&clp->cl_lock); 7628 7629 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], 7630 so_strhash) { 7631 if (so->so_is_open_owner) 7632 continue; 7633 if (same_owner_str(so, owner)) 7634 return lockowner(nfs4_get_stateowner(so)); 7635 } 7636 return NULL; 7637 } 7638 7639 static struct nfs4_lockowner * 7640 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) 7641 { 7642 struct nfs4_lockowner *lo; 7643 7644 spin_lock(&clp->cl_lock); 7645 lo = find_lockowner_str_locked(clp, owner); 7646 spin_unlock(&clp->cl_lock); 7647 return lo; 7648 } 7649 7650 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) 7651 { 7652 unhash_lockowner_locked(lockowner(sop)); 7653 } 7654 7655 static void nfs4_free_lockowner(struct nfs4_stateowner *sop) 7656 { 7657 struct nfs4_lockowner *lo = lockowner(sop); 7658 7659 kmem_cache_free(lockowner_slab, lo); 7660 } 7661 7662 static const struct nfs4_stateowner_operations lockowner_ops = { 7663 .so_unhash = nfs4_unhash_lockowner, 7664 .so_free = nfs4_free_lockowner, 7665 }; 7666 7667 /* 7668 * Alloc a lock owner structure. 7669 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has 7670 * occurred. 7671 * 7672 * strhashval = ownerstr_hashval 7673 */ 7674 static struct nfs4_lockowner * 7675 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, 7676 struct nfs4_ol_stateid *open_stp, 7677 struct nfsd4_lock *lock) 7678 { 7679 struct nfs4_lockowner *lo, *ret; 7680 7681 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); 7682 if (!lo) 7683 return NULL; 7684 INIT_LIST_HEAD(&lo->lo_blocked); 7685 INIT_LIST_HEAD(&lo->lo_owner.so_stateids); 7686 lo->lo_owner.so_is_open_owner = 0; 7687 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; 7688 lo->lo_owner.so_ops = &lockowner_ops; 7689 spin_lock(&clp->cl_lock); 7690 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); 7691 if (ret == NULL) { 7692 list_add(&lo->lo_owner.so_strhash, 7693 &clp->cl_ownerstr_hashtbl[strhashval]); 7694 ret = lo; 7695 } else 7696 nfs4_free_stateowner(&lo->lo_owner); 7697 7698 spin_unlock(&clp->cl_lock); 7699 return ret; 7700 } 7701 7702 static struct nfs4_ol_stateid * 7703 find_lock_stateid(const struct nfs4_lockowner *lo, 7704 const struct nfs4_ol_stateid *ost) 7705 { 7706 struct nfs4_ol_stateid *lst; 7707 7708 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock); 7709 7710 /* If ost is not hashed, ost->st_locks will not be valid */ 7711 if (!nfs4_ol_stateid_unhashed(ost)) 7712 list_for_each_entry(lst, &ost->st_locks, st_locks) { 7713 if (lst->st_stateowner == &lo->lo_owner) { 7714 refcount_inc(&lst->st_stid.sc_count); 7715 return lst; 7716 } 7717 } 7718 return NULL; 7719 } 7720 7721 static struct nfs4_ol_stateid * 7722 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, 7723 struct nfs4_file *fp, struct inode *inode, 7724 struct nfs4_ol_stateid *open_stp) 7725 { 7726 struct nfs4_client *clp = lo->lo_owner.so_client; 7727 struct nfs4_ol_stateid *retstp; 7728 7729 mutex_init(&stp->st_mutex); 7730 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); 7731 retry: 7732 spin_lock(&clp->cl_lock); 7733 if (nfs4_ol_stateid_unhashed(open_stp)) 7734 goto out_close; 7735 retstp = find_lock_stateid(lo, open_stp); 7736 if (retstp) 7737 goto out_found; 7738 refcount_inc(&stp->st_stid.sc_count); 7739 stp->st_stid.sc_type = SC_TYPE_LOCK; 7740 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); 7741 get_nfs4_file(fp); 7742 stp->st_stid.sc_file = fp; 7743 stp->st_access_bmap = 0; 7744 stp->st_deny_bmap = open_stp->st_deny_bmap; 7745 stp->st_openstp = open_stp; 7746 spin_lock(&fp->fi_lock); 7747 list_add(&stp->st_locks, &open_stp->st_locks); 7748 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); 7749 list_add(&stp->st_perfile, &fp->fi_stateids); 7750 spin_unlock(&fp->fi_lock); 7751 spin_unlock(&clp->cl_lock); 7752 return stp; 7753 out_found: 7754 spin_unlock(&clp->cl_lock); 7755 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { 7756 nfs4_put_stid(&retstp->st_stid); 7757 goto retry; 7758 } 7759 /* To keep mutex tracking happy */ 7760 mutex_unlock(&stp->st_mutex); 7761 return retstp; 7762 out_close: 7763 spin_unlock(&clp->cl_lock); 7764 mutex_unlock(&stp->st_mutex); 7765 return NULL; 7766 } 7767 7768 static struct nfs4_ol_stateid * 7769 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, 7770 struct inode *inode, struct nfs4_ol_stateid *ost, 7771 bool *new) 7772 { 7773 struct nfs4_stid *ns = NULL; 7774 struct nfs4_ol_stateid *lst; 7775 struct nfs4_openowner *oo = openowner(ost->st_stateowner); 7776 struct nfs4_client *clp = oo->oo_owner.so_client; 7777 7778 *new = false; 7779 spin_lock(&clp->cl_lock); 7780 lst = find_lock_stateid(lo, ost); 7781 spin_unlock(&clp->cl_lock); 7782 if (lst != NULL) { 7783 if (nfsd4_lock_ol_stateid(lst) == nfs_ok) 7784 goto out; 7785 nfs4_put_stid(&lst->st_stid); 7786 } 7787 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid); 7788 if (ns == NULL) 7789 return NULL; 7790 7791 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost); 7792 if (lst == openlockstateid(ns)) 7793 *new = true; 7794 else 7795 nfs4_put_stid(ns); 7796 out: 7797 return lst; 7798 } 7799 7800 static int 7801 check_lock_length(u64 offset, u64 length) 7802 { 7803 return ((length == 0) || ((length != NFS4_MAX_UINT64) && 7804 (length > ~offset))); 7805 } 7806 7807 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access) 7808 { 7809 struct nfs4_file *fp = lock_stp->st_stid.sc_file; 7810 7811 lockdep_assert_held(&fp->fi_lock); 7812 7813 if (test_access(access, lock_stp)) 7814 return; 7815 __nfs4_file_get_access(fp, access); 7816 set_access(access, lock_stp); 7817 } 7818 7819 static __be32 7820 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, 7821 struct nfs4_ol_stateid *ost, 7822 struct nfsd4_lock *lock, 7823 struct nfs4_ol_stateid **plst, bool *new) 7824 { 7825 __be32 status; 7826 struct nfs4_file *fi = ost->st_stid.sc_file; 7827 struct nfs4_openowner *oo = openowner(ost->st_stateowner); 7828 struct nfs4_client *cl = oo->oo_owner.so_client; 7829 struct inode *inode = d_inode(cstate->current_fh.fh_dentry); 7830 struct nfs4_lockowner *lo; 7831 struct nfs4_ol_stateid *lst; 7832 unsigned int strhashval; 7833 7834 lo = find_lockowner_str(cl, &lock->lk_new_owner); 7835 if (!lo) { 7836 strhashval = ownerstr_hashval(&lock->lk_new_owner); 7837 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); 7838 if (lo == NULL) 7839 return nfserr_jukebox; 7840 } else { 7841 /* with an existing lockowner, seqids must be the same */ 7842 status = nfserr_bad_seqid; 7843 if (!cstate->minorversion && 7844 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) 7845 goto out; 7846 } 7847 7848 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); 7849 if (lst == NULL) { 7850 status = nfserr_jukebox; 7851 goto out; 7852 } 7853 7854 status = nfs_ok; 7855 *plst = lst; 7856 out: 7857 nfs4_put_stateowner(&lo->lo_owner); 7858 return status; 7859 } 7860 7861 /* 7862 * LOCK operation 7863 */ 7864 __be32 7865 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 7866 union nfsd4_op_u *u) 7867 { 7868 struct nfsd4_lock *lock = &u->lock; 7869 struct nfs4_openowner *open_sop = NULL; 7870 struct nfs4_lockowner *lock_sop = NULL; 7871 struct nfs4_ol_stateid *lock_stp = NULL; 7872 struct nfs4_ol_stateid *open_stp = NULL; 7873 struct nfs4_file *fp; 7874 struct nfsd_file *nf = NULL; 7875 struct nfsd4_blocked_lock *nbl = NULL; 7876 struct file_lock *file_lock = NULL; 7877 struct file_lock *conflock = NULL; 7878 struct super_block *sb; 7879 __be32 status = 0; 7880 int lkflg; 7881 int err; 7882 bool new = false; 7883 unsigned char type; 7884 unsigned int flags = FL_POSIX; 7885 struct net *net = SVC_NET(rqstp); 7886 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 7887 7888 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", 7889 (long long) lock->lk_offset, 7890 (long long) lock->lk_length); 7891 7892 if (check_lock_length(lock->lk_offset, lock->lk_length)) 7893 return nfserr_inval; 7894 7895 if ((status = fh_verify(rqstp, &cstate->current_fh, 7896 S_IFREG, NFSD_MAY_LOCK))) { 7897 dprintk("NFSD: nfsd4_lock: permission denied!\n"); 7898 return status; 7899 } 7900 sb = cstate->current_fh.fh_dentry->d_sb; 7901 7902 if (lock->lk_is_new) { 7903 if (nfsd4_has_session(cstate)) 7904 /* See rfc 5661 18.10.3: given clientid is ignored: */ 7905 memcpy(&lock->lk_new_clientid, 7906 &cstate->clp->cl_clientid, 7907 sizeof(clientid_t)); 7908 7909 /* validate and update open stateid and open seqid */ 7910 status = nfs4_preprocess_confirmed_seqid_op(cstate, 7911 lock->lk_new_open_seqid, 7912 &lock->lk_new_open_stateid, 7913 &open_stp, nn); 7914 if (status) 7915 goto out; 7916 mutex_unlock(&open_stp->st_mutex); 7917 open_sop = openowner(open_stp->st_stateowner); 7918 status = nfserr_bad_stateid; 7919 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, 7920 &lock->lk_new_clientid)) 7921 goto out; 7922 status = lookup_or_create_lock_state(cstate, open_stp, lock, 7923 &lock_stp, &new); 7924 } else { 7925 status = nfs4_preprocess_seqid_op(cstate, 7926 lock->lk_old_lock_seqid, 7927 &lock->lk_old_lock_stateid, 7928 SC_TYPE_LOCK, 0, &lock_stp, 7929 nn); 7930 } 7931 if (status) 7932 goto out; 7933 lock_sop = lockowner(lock_stp->st_stateowner); 7934 7935 lkflg = setlkflg(lock->lk_type); 7936 status = nfs4_check_openmode(lock_stp, lkflg); 7937 if (status) 7938 goto out; 7939 7940 status = nfserr_grace; 7941 if (locks_in_grace(net) && !lock->lk_reclaim) 7942 goto out; 7943 status = nfserr_no_grace; 7944 if (!locks_in_grace(net) && lock->lk_reclaim) 7945 goto out; 7946 7947 if (lock->lk_reclaim) 7948 flags |= FL_RECLAIM; 7949 7950 fp = lock_stp->st_stid.sc_file; 7951 switch (lock->lk_type) { 7952 case NFS4_READW_LT: 7953 if (nfsd4_has_session(cstate) || 7954 exportfs_lock_op_is_async(sb->s_export_op)) 7955 flags |= FL_SLEEP; 7956 fallthrough; 7957 case NFS4_READ_LT: 7958 spin_lock(&fp->fi_lock); 7959 nf = find_readable_file_locked(fp); 7960 if (nf) 7961 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); 7962 spin_unlock(&fp->fi_lock); 7963 type = F_RDLCK; 7964 break; 7965 case NFS4_WRITEW_LT: 7966 if (nfsd4_has_session(cstate) || 7967 exportfs_lock_op_is_async(sb->s_export_op)) 7968 flags |= FL_SLEEP; 7969 fallthrough; 7970 case NFS4_WRITE_LT: 7971 spin_lock(&fp->fi_lock); 7972 nf = find_writeable_file_locked(fp); 7973 if (nf) 7974 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); 7975 spin_unlock(&fp->fi_lock); 7976 type = F_WRLCK; 7977 break; 7978 default: 7979 status = nfserr_inval; 7980 goto out; 7981 } 7982 7983 if (!nf) { 7984 status = nfserr_openmode; 7985 goto out; 7986 } 7987 7988 /* 7989 * Most filesystems with their own ->lock operations will block 7990 * the nfsd thread waiting to acquire the lock. That leads to 7991 * deadlocks (we don't want every nfsd thread tied up waiting 7992 * for file locks), so don't attempt blocking lock notifications 7993 * on those filesystems: 7994 */ 7995 if (!exportfs_lock_op_is_async(sb->s_export_op)) 7996 flags &= ~FL_SLEEP; 7997 7998 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn); 7999 if (!nbl) { 8000 dprintk("NFSD: %s: unable to allocate block!\n", __func__); 8001 status = nfserr_jukebox; 8002 goto out; 8003 } 8004 8005 file_lock = &nbl->nbl_lock; 8006 file_lock->c.flc_type = type; 8007 file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner)); 8008 file_lock->c.flc_pid = current->tgid; 8009 file_lock->c.flc_file = nf->nf_file; 8010 file_lock->c.flc_flags = flags; 8011 file_lock->fl_lmops = &nfsd_posix_mng_ops; 8012 file_lock->fl_start = lock->lk_offset; 8013 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); 8014 nfs4_transform_lock_offset(file_lock); 8015 8016 conflock = locks_alloc_lock(); 8017 if (!conflock) { 8018 dprintk("NFSD: %s: unable to allocate lock!\n", __func__); 8019 status = nfserr_jukebox; 8020 goto out; 8021 } 8022 8023 if (flags & FL_SLEEP) { 8024 nbl->nbl_time = ktime_get_boottime_seconds(); 8025 spin_lock(&nn->blocked_locks_lock); 8026 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked); 8027 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru); 8028 kref_get(&nbl->nbl_kref); 8029 spin_unlock(&nn->blocked_locks_lock); 8030 } 8031 8032 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock); 8033 switch (err) { 8034 case 0: /* success! */ 8035 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid); 8036 status = 0; 8037 if (lock->lk_reclaim) 8038 nn->somebody_reclaimed = true; 8039 break; 8040 case FILE_LOCK_DEFERRED: 8041 kref_put(&nbl->nbl_kref, free_nbl); 8042 nbl = NULL; 8043 fallthrough; 8044 case -EAGAIN: /* conflock holds conflicting lock */ 8045 status = nfserr_denied; 8046 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); 8047 nfs4_set_lock_denied(conflock, &lock->lk_denied); 8048 break; 8049 case -EDEADLK: 8050 status = nfserr_deadlock; 8051 break; 8052 default: 8053 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err); 8054 status = nfserrno(err); 8055 break; 8056 } 8057 out: 8058 if (nbl) { 8059 /* dequeue it if we queued it before */ 8060 if (flags & FL_SLEEP) { 8061 spin_lock(&nn->blocked_locks_lock); 8062 if (!list_empty(&nbl->nbl_list) && 8063 !list_empty(&nbl->nbl_lru)) { 8064 list_del_init(&nbl->nbl_list); 8065 list_del_init(&nbl->nbl_lru); 8066 kref_put(&nbl->nbl_kref, free_nbl); 8067 } 8068 /* nbl can use one of lists to be linked to reaplist */ 8069 spin_unlock(&nn->blocked_locks_lock); 8070 } 8071 free_blocked_lock(nbl); 8072 } 8073 if (nf) 8074 nfsd_file_put(nf); 8075 if (lock_stp) { 8076 /* Bump seqid manually if the 4.0 replay owner is openowner */ 8077 if (cstate->replay_owner && 8078 cstate->replay_owner != &lock_sop->lo_owner && 8079 seqid_mutating_err(ntohl(status))) 8080 lock_sop->lo_owner.so_seqid++; 8081 8082 /* 8083 * If this is a new, never-before-used stateid, and we are 8084 * returning an error, then just go ahead and release it. 8085 */ 8086 if (status && new) 8087 release_lock_stateid(lock_stp); 8088 8089 mutex_unlock(&lock_stp->st_mutex); 8090 8091 nfs4_put_stid(&lock_stp->st_stid); 8092 } 8093 if (open_stp) 8094 nfs4_put_stid(&open_stp->st_stid); 8095 nfsd4_bump_seqid(cstate, status); 8096 if (conflock) 8097 locks_free_lock(conflock); 8098 return status; 8099 } 8100 8101 void nfsd4_lock_release(union nfsd4_op_u *u) 8102 { 8103 struct nfsd4_lock *lock = &u->lock; 8104 struct nfsd4_lock_denied *deny = &lock->lk_denied; 8105 8106 kfree(deny->ld_owner.data); 8107 } 8108 8109 /* 8110 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, 8111 * so we do a temporary open here just to get an open file to pass to 8112 * vfs_test_lock. 8113 */ 8114 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock) 8115 { 8116 struct nfsd_file *nf; 8117 struct inode *inode; 8118 __be32 err; 8119 8120 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf); 8121 if (err) 8122 return err; 8123 inode = fhp->fh_dentry->d_inode; 8124 inode_lock(inode); /* to block new leases till after test_lock: */ 8125 err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ)); 8126 if (err) 8127 goto out; 8128 lock->c.flc_file = nf->nf_file; 8129 err = nfserrno(vfs_test_lock(nf->nf_file, lock)); 8130 lock->c.flc_file = NULL; 8131 out: 8132 inode_unlock(inode); 8133 nfsd_file_put(nf); 8134 return err; 8135 } 8136 8137 /* 8138 * LOCKT operation 8139 */ 8140 __be32 8141 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 8142 union nfsd4_op_u *u) 8143 { 8144 struct nfsd4_lockt *lockt = &u->lockt; 8145 struct file_lock *file_lock = NULL; 8146 struct nfs4_lockowner *lo = NULL; 8147 __be32 status; 8148 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 8149 8150 if (locks_in_grace(SVC_NET(rqstp))) 8151 return nfserr_grace; 8152 8153 if (check_lock_length(lockt->lt_offset, lockt->lt_length)) 8154 return nfserr_inval; 8155 8156 if (!nfsd4_has_session(cstate)) { 8157 status = set_client(&lockt->lt_clientid, cstate, nn); 8158 if (status) 8159 goto out; 8160 } 8161 8162 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) 8163 goto out; 8164 8165 file_lock = locks_alloc_lock(); 8166 if (!file_lock) { 8167 dprintk("NFSD: %s: unable to allocate lock!\n", __func__); 8168 status = nfserr_jukebox; 8169 goto out; 8170 } 8171 8172 switch (lockt->lt_type) { 8173 case NFS4_READ_LT: 8174 case NFS4_READW_LT: 8175 file_lock->c.flc_type = F_RDLCK; 8176 break; 8177 case NFS4_WRITE_LT: 8178 case NFS4_WRITEW_LT: 8179 file_lock->c.flc_type = F_WRLCK; 8180 break; 8181 default: 8182 dprintk("NFSD: nfs4_lockt: bad lock type!\n"); 8183 status = nfserr_inval; 8184 goto out; 8185 } 8186 8187 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); 8188 if (lo) 8189 file_lock->c.flc_owner = (fl_owner_t)lo; 8190 file_lock->c.flc_pid = current->tgid; 8191 file_lock->c.flc_flags = FL_POSIX; 8192 8193 file_lock->fl_start = lockt->lt_offset; 8194 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length); 8195 8196 nfs4_transform_lock_offset(file_lock); 8197 8198 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); 8199 if (status) 8200 goto out; 8201 8202 if (file_lock->c.flc_type != F_UNLCK) { 8203 status = nfserr_denied; 8204 nfs4_set_lock_denied(file_lock, &lockt->lt_denied); 8205 } 8206 out: 8207 if (lo) 8208 nfs4_put_stateowner(&lo->lo_owner); 8209 if (file_lock) 8210 locks_free_lock(file_lock); 8211 return status; 8212 } 8213 8214 void nfsd4_lockt_release(union nfsd4_op_u *u) 8215 { 8216 struct nfsd4_lockt *lockt = &u->lockt; 8217 struct nfsd4_lock_denied *deny = &lockt->lt_denied; 8218 8219 kfree(deny->ld_owner.data); 8220 } 8221 8222 __be32 8223 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 8224 union nfsd4_op_u *u) 8225 { 8226 struct nfsd4_locku *locku = &u->locku; 8227 struct nfs4_ol_stateid *stp; 8228 struct nfsd_file *nf = NULL; 8229 struct file_lock *file_lock = NULL; 8230 __be32 status; 8231 int err; 8232 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 8233 8234 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", 8235 (long long) locku->lu_offset, 8236 (long long) locku->lu_length); 8237 8238 if (check_lock_length(locku->lu_offset, locku->lu_length)) 8239 return nfserr_inval; 8240 8241 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, 8242 &locku->lu_stateid, SC_TYPE_LOCK, 0, 8243 &stp, nn); 8244 if (status) 8245 goto out; 8246 nf = find_any_file(stp->st_stid.sc_file); 8247 if (!nf) { 8248 status = nfserr_lock_range; 8249 goto put_stateid; 8250 } 8251 file_lock = locks_alloc_lock(); 8252 if (!file_lock) { 8253 dprintk("NFSD: %s: unable to allocate lock!\n", __func__); 8254 status = nfserr_jukebox; 8255 goto put_file; 8256 } 8257 8258 file_lock->c.flc_type = F_UNLCK; 8259 file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner)); 8260 file_lock->c.flc_pid = current->tgid; 8261 file_lock->c.flc_file = nf->nf_file; 8262 file_lock->c.flc_flags = FL_POSIX; 8263 file_lock->fl_lmops = &nfsd_posix_mng_ops; 8264 file_lock->fl_start = locku->lu_offset; 8265 8266 file_lock->fl_end = last_byte_offset(locku->lu_offset, 8267 locku->lu_length); 8268 nfs4_transform_lock_offset(file_lock); 8269 8270 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL); 8271 if (err) { 8272 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); 8273 goto out_nfserr; 8274 } 8275 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); 8276 put_file: 8277 nfsd_file_put(nf); 8278 put_stateid: 8279 mutex_unlock(&stp->st_mutex); 8280 nfs4_put_stid(&stp->st_stid); 8281 out: 8282 nfsd4_bump_seqid(cstate, status); 8283 if (file_lock) 8284 locks_free_lock(file_lock); 8285 return status; 8286 8287 out_nfserr: 8288 status = nfserrno(err); 8289 goto put_file; 8290 } 8291 8292 /* 8293 * returns 8294 * true: locks held by lockowner 8295 * false: no locks held by lockowner 8296 */ 8297 static bool 8298 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) 8299 { 8300 struct file_lock *fl; 8301 int status = false; 8302 struct nfsd_file *nf; 8303 struct inode *inode; 8304 struct file_lock_context *flctx; 8305 8306 spin_lock(&fp->fi_lock); 8307 nf = find_any_file_locked(fp); 8308 if (!nf) { 8309 /* Any valid lock stateid should have some sort of access */ 8310 WARN_ON_ONCE(1); 8311 goto out; 8312 } 8313 8314 inode = file_inode(nf->nf_file); 8315 flctx = locks_inode_context(inode); 8316 8317 if (flctx && !list_empty_careful(&flctx->flc_posix)) { 8318 spin_lock(&flctx->flc_lock); 8319 for_each_file_lock(fl, &flctx->flc_posix) { 8320 if (fl->c.flc_owner == (fl_owner_t)lowner) { 8321 status = true; 8322 break; 8323 } 8324 } 8325 spin_unlock(&flctx->flc_lock); 8326 } 8327 out: 8328 spin_unlock(&fp->fi_lock); 8329 return status; 8330 } 8331 8332 /** 8333 * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations 8334 * @rqstp: RPC transaction 8335 * @cstate: NFSv4 COMPOUND state 8336 * @u: RELEASE_LOCKOWNER arguments 8337 * 8338 * Check if theree are any locks still held and if not - free the lockowner 8339 * and any lock state that is owned. 8340 * 8341 * Return values: 8342 * %nfs_ok: lockowner released or not found 8343 * %nfserr_locks_held: lockowner still in use 8344 * %nfserr_stale_clientid: clientid no longer active 8345 * %nfserr_expired: clientid not recognized 8346 */ 8347 __be32 8348 nfsd4_release_lockowner(struct svc_rqst *rqstp, 8349 struct nfsd4_compound_state *cstate, 8350 union nfsd4_op_u *u) 8351 { 8352 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner; 8353 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 8354 clientid_t *clid = &rlockowner->rl_clientid; 8355 struct nfs4_ol_stateid *stp; 8356 struct nfs4_lockowner *lo; 8357 struct nfs4_client *clp; 8358 LIST_HEAD(reaplist); 8359 __be32 status; 8360 8361 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", 8362 clid->cl_boot, clid->cl_id); 8363 8364 status = set_client(clid, cstate, nn); 8365 if (status) 8366 return status; 8367 clp = cstate->clp; 8368 8369 spin_lock(&clp->cl_lock); 8370 lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner); 8371 if (!lo) { 8372 spin_unlock(&clp->cl_lock); 8373 return nfs_ok; 8374 } 8375 8376 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) { 8377 if (check_for_locks(stp->st_stid.sc_file, lo)) { 8378 spin_unlock(&clp->cl_lock); 8379 nfs4_put_stateowner(&lo->lo_owner); 8380 return nfserr_locks_held; 8381 } 8382 } 8383 unhash_lockowner_locked(lo); 8384 while (!list_empty(&lo->lo_owner.so_stateids)) { 8385 stp = list_first_entry(&lo->lo_owner.so_stateids, 8386 struct nfs4_ol_stateid, 8387 st_perstateowner); 8388 unhash_lock_stateid(stp); 8389 put_ol_stateid_locked(stp, &reaplist); 8390 } 8391 spin_unlock(&clp->cl_lock); 8392 8393 free_ol_stateid_reaplist(&reaplist); 8394 remove_blocked_locks(lo); 8395 nfs4_put_stateowner(&lo->lo_owner); 8396 return nfs_ok; 8397 } 8398 8399 static inline struct nfs4_client_reclaim * 8400 alloc_reclaim(void) 8401 { 8402 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); 8403 } 8404 8405 bool 8406 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn) 8407 { 8408 struct nfs4_client_reclaim *crp; 8409 8410 crp = nfsd4_find_reclaim_client(name, nn); 8411 return (crp && crp->cr_clp); 8412 } 8413 8414 /* 8415 * failure => all reset bets are off, nfserr_no_grace... 8416 * 8417 * The caller is responsible for freeing name.data if NULL is returned (it 8418 * will be freed in nfs4_remove_reclaim_record in the normal case). 8419 */ 8420 struct nfs4_client_reclaim * 8421 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash, 8422 struct nfsd_net *nn) 8423 { 8424 unsigned int strhashval; 8425 struct nfs4_client_reclaim *crp; 8426 8427 crp = alloc_reclaim(); 8428 if (crp) { 8429 strhashval = clientstr_hashval(name); 8430 INIT_LIST_HEAD(&crp->cr_strhash); 8431 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); 8432 crp->cr_name.data = name.data; 8433 crp->cr_name.len = name.len; 8434 crp->cr_princhash.data = princhash.data; 8435 crp->cr_princhash.len = princhash.len; 8436 crp->cr_clp = NULL; 8437 nn->reclaim_str_hashtbl_size++; 8438 } 8439 return crp; 8440 } 8441 8442 void 8443 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn) 8444 { 8445 list_del(&crp->cr_strhash); 8446 kfree(crp->cr_name.data); 8447 kfree(crp->cr_princhash.data); 8448 kfree(crp); 8449 nn->reclaim_str_hashtbl_size--; 8450 } 8451 8452 void 8453 nfs4_release_reclaim(struct nfsd_net *nn) 8454 { 8455 struct nfs4_client_reclaim *crp = NULL; 8456 int i; 8457 8458 for (i = 0; i < CLIENT_HASH_SIZE; i++) { 8459 while (!list_empty(&nn->reclaim_str_hashtbl[i])) { 8460 crp = list_entry(nn->reclaim_str_hashtbl[i].next, 8461 struct nfs4_client_reclaim, cr_strhash); 8462 nfs4_remove_reclaim_record(crp, nn); 8463 } 8464 } 8465 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); 8466 } 8467 8468 /* 8469 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ 8470 struct nfs4_client_reclaim * 8471 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn) 8472 { 8473 unsigned int strhashval; 8474 struct nfs4_client_reclaim *crp = NULL; 8475 8476 strhashval = clientstr_hashval(name); 8477 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { 8478 if (compare_blob(&crp->cr_name, &name) == 0) { 8479 return crp; 8480 } 8481 } 8482 return NULL; 8483 } 8484 8485 __be32 8486 nfs4_check_open_reclaim(struct nfs4_client *clp) 8487 { 8488 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags)) 8489 return nfserr_no_grace; 8490 8491 if (nfsd4_client_record_check(clp)) 8492 return nfserr_reclaim_bad; 8493 8494 return nfs_ok; 8495 } 8496 8497 /* 8498 * Since the lifetime of a delegation isn't limited to that of an open, a 8499 * client may quite reasonably hang on to a delegation as long as it has 8500 * the inode cached. This becomes an obvious problem the first time a 8501 * client's inode cache approaches the size of the server's total memory. 8502 * 8503 * For now we avoid this problem by imposing a hard limit on the number 8504 * of delegations, which varies according to the server's memory size. 8505 */ 8506 static void 8507 set_max_delegations(void) 8508 { 8509 /* 8510 * Allow at most 4 delegations per megabyte of RAM. Quick 8511 * estimates suggest that in the worst case (where every delegation 8512 * is for a different inode), a delegation could take about 1.5K, 8513 * giving a worst case usage of about 6% of memory. 8514 */ 8515 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); 8516 } 8517 8518 static int nfs4_state_create_net(struct net *net) 8519 { 8520 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 8521 int i; 8522 8523 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, 8524 sizeof(struct list_head), 8525 GFP_KERNEL); 8526 if (!nn->conf_id_hashtbl) 8527 goto err; 8528 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, 8529 sizeof(struct list_head), 8530 GFP_KERNEL); 8531 if (!nn->unconf_id_hashtbl) 8532 goto err_unconf_id; 8533 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE, 8534 sizeof(struct list_head), 8535 GFP_KERNEL); 8536 if (!nn->sessionid_hashtbl) 8537 goto err_sessionid; 8538 8539 for (i = 0; i < CLIENT_HASH_SIZE; i++) { 8540 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); 8541 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); 8542 } 8543 for (i = 0; i < SESSION_HASH_SIZE; i++) 8544 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); 8545 nn->conf_name_tree = RB_ROOT; 8546 nn->unconf_name_tree = RB_ROOT; 8547 nn->boot_time = ktime_get_real_seconds(); 8548 nn->grace_ended = false; 8549 nn->nfsd4_manager.block_opens = true; 8550 INIT_LIST_HEAD(&nn->nfsd4_manager.list); 8551 INIT_LIST_HEAD(&nn->client_lru); 8552 INIT_LIST_HEAD(&nn->close_lru); 8553 INIT_LIST_HEAD(&nn->del_recall_lru); 8554 spin_lock_init(&nn->client_lock); 8555 spin_lock_init(&nn->s2s_cp_lock); 8556 idr_init(&nn->s2s_cp_stateids); 8557 8558 spin_lock_init(&nn->blocked_locks_lock); 8559 INIT_LIST_HEAD(&nn->blocked_locks_lru); 8560 8561 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); 8562 INIT_WORK(&nn->nfsd_shrinker_work, nfsd4_state_shrinker_worker); 8563 get_net(net); 8564 8565 nn->nfsd_client_shrinker = shrinker_alloc(0, "nfsd-client"); 8566 if (!nn->nfsd_client_shrinker) 8567 goto err_shrinker; 8568 8569 nn->nfsd_client_shrinker->scan_objects = nfsd4_state_shrinker_scan; 8570 nn->nfsd_client_shrinker->count_objects = nfsd4_state_shrinker_count; 8571 nn->nfsd_client_shrinker->private_data = nn; 8572 8573 shrinker_register(nn->nfsd_client_shrinker); 8574 8575 return 0; 8576 8577 err_shrinker: 8578 put_net(net); 8579 kfree(nn->sessionid_hashtbl); 8580 err_sessionid: 8581 kfree(nn->unconf_id_hashtbl); 8582 err_unconf_id: 8583 kfree(nn->conf_id_hashtbl); 8584 err: 8585 return -ENOMEM; 8586 } 8587 8588 static void 8589 nfs4_state_destroy_net(struct net *net) 8590 { 8591 int i; 8592 struct nfs4_client *clp = NULL; 8593 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 8594 8595 for (i = 0; i < CLIENT_HASH_SIZE; i++) { 8596 while (!list_empty(&nn->conf_id_hashtbl[i])) { 8597 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); 8598 destroy_client(clp); 8599 } 8600 } 8601 8602 WARN_ON(!list_empty(&nn->blocked_locks_lru)); 8603 8604 for (i = 0; i < CLIENT_HASH_SIZE; i++) { 8605 while (!list_empty(&nn->unconf_id_hashtbl[i])) { 8606 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); 8607 destroy_client(clp); 8608 } 8609 } 8610 8611 kfree(nn->sessionid_hashtbl); 8612 kfree(nn->unconf_id_hashtbl); 8613 kfree(nn->conf_id_hashtbl); 8614 put_net(net); 8615 } 8616 8617 int 8618 nfs4_state_start_net(struct net *net) 8619 { 8620 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 8621 int ret; 8622 8623 ret = nfs4_state_create_net(net); 8624 if (ret) 8625 return ret; 8626 locks_start_grace(net, &nn->nfsd4_manager); 8627 nfsd4_client_tracking_init(net); 8628 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0) 8629 goto skip_grace; 8630 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n", 8631 nn->nfsd4_grace, net->ns.inum); 8632 trace_nfsd_grace_start(nn); 8633 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); 8634 return 0; 8635 8636 skip_grace: 8637 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n", 8638 net->ns.inum); 8639 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ); 8640 nfsd4_end_grace(nn); 8641 return 0; 8642 } 8643 8644 /* initialization to perform when the nfsd service is started: */ 8645 8646 int 8647 nfs4_state_start(void) 8648 { 8649 int ret; 8650 8651 ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params); 8652 if (ret) 8653 return ret; 8654 8655 set_max_delegations(); 8656 return 0; 8657 } 8658 8659 void 8660 nfs4_state_shutdown_net(struct net *net) 8661 { 8662 struct nfs4_delegation *dp = NULL; 8663 struct list_head *pos, *next, reaplist; 8664 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 8665 8666 shrinker_free(nn->nfsd_client_shrinker); 8667 cancel_work(&nn->nfsd_shrinker_work); 8668 cancel_delayed_work_sync(&nn->laundromat_work); 8669 locks_end_grace(&nn->nfsd4_manager); 8670 8671 INIT_LIST_HEAD(&reaplist); 8672 spin_lock(&state_lock); 8673 list_for_each_safe(pos, next, &nn->del_recall_lru) { 8674 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); 8675 unhash_delegation_locked(dp, SC_STATUS_CLOSED); 8676 list_add(&dp->dl_recall_lru, &reaplist); 8677 } 8678 spin_unlock(&state_lock); 8679 list_for_each_safe(pos, next, &reaplist) { 8680 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); 8681 list_del_init(&dp->dl_recall_lru); 8682 destroy_unhashed_deleg(dp); 8683 } 8684 8685 nfsd4_client_tracking_exit(net); 8686 nfs4_state_destroy_net(net); 8687 #ifdef CONFIG_NFSD_V4_2_INTER_SSC 8688 nfsd4_ssc_shutdown_umount(nn); 8689 #endif 8690 } 8691 8692 void 8693 nfs4_state_shutdown(void) 8694 { 8695 rhltable_destroy(&nfs4_file_rhltable); 8696 } 8697 8698 static void 8699 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) 8700 { 8701 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) && 8702 CURRENT_STATEID(stateid)) 8703 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); 8704 } 8705 8706 static void 8707 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) 8708 { 8709 if (cstate->minorversion) { 8710 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); 8711 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); 8712 } 8713 } 8714 8715 void 8716 clear_current_stateid(struct nfsd4_compound_state *cstate) 8717 { 8718 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); 8719 } 8720 8721 /* 8722 * functions to set current state id 8723 */ 8724 void 8725 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, 8726 union nfsd4_op_u *u) 8727 { 8728 put_stateid(cstate, &u->open_downgrade.od_stateid); 8729 } 8730 8731 void 8732 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, 8733 union nfsd4_op_u *u) 8734 { 8735 put_stateid(cstate, &u->open.op_stateid); 8736 } 8737 8738 void 8739 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, 8740 union nfsd4_op_u *u) 8741 { 8742 put_stateid(cstate, &u->close.cl_stateid); 8743 } 8744 8745 void 8746 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, 8747 union nfsd4_op_u *u) 8748 { 8749 put_stateid(cstate, &u->lock.lk_resp_stateid); 8750 } 8751 8752 /* 8753 * functions to consume current state id 8754 */ 8755 8756 void 8757 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, 8758 union nfsd4_op_u *u) 8759 { 8760 get_stateid(cstate, &u->open_downgrade.od_stateid); 8761 } 8762 8763 void 8764 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, 8765 union nfsd4_op_u *u) 8766 { 8767 get_stateid(cstate, &u->delegreturn.dr_stateid); 8768 } 8769 8770 void 8771 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, 8772 union nfsd4_op_u *u) 8773 { 8774 get_stateid(cstate, &u->free_stateid.fr_stateid); 8775 } 8776 8777 void 8778 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, 8779 union nfsd4_op_u *u) 8780 { 8781 get_stateid(cstate, &u->setattr.sa_stateid); 8782 } 8783 8784 void 8785 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, 8786 union nfsd4_op_u *u) 8787 { 8788 get_stateid(cstate, &u->close.cl_stateid); 8789 } 8790 8791 void 8792 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, 8793 union nfsd4_op_u *u) 8794 { 8795 get_stateid(cstate, &u->locku.lu_stateid); 8796 } 8797 8798 void 8799 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, 8800 union nfsd4_op_u *u) 8801 { 8802 get_stateid(cstate, &u->read.rd_stateid); 8803 } 8804 8805 void 8806 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, 8807 union nfsd4_op_u *u) 8808 { 8809 get_stateid(cstate, &u->write.wr_stateid); 8810 } 8811 8812 /** 8813 * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict 8814 * @rqstp: RPC transaction context 8815 * @inode: file to be checked for a conflict 8816 * @modified: return true if file was modified 8817 * @size: new size of file if modified is true 8818 * 8819 * This function is called when there is a conflict between a write 8820 * delegation and a change/size GETATTR from another client. The server 8821 * must either use the CB_GETATTR to get the current values of the 8822 * attributes from the client that holds the delegation or recall the 8823 * delegation before replying to the GETATTR. See RFC 8881 section 8824 * 18.7.4. 8825 * 8826 * Returns 0 if there is no conflict; otherwise an nfs_stat 8827 * code is returned. 8828 */ 8829 __be32 8830 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct inode *inode, 8831 bool *modified, u64 *size) 8832 { 8833 __be32 status; 8834 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 8835 struct file_lock_context *ctx; 8836 struct file_lease *fl; 8837 struct nfs4_delegation *dp; 8838 struct iattr attrs; 8839 struct nfs4_cb_fattr *ncf; 8840 8841 *modified = false; 8842 ctx = locks_inode_context(inode); 8843 if (!ctx) 8844 return 0; 8845 spin_lock(&ctx->flc_lock); 8846 for_each_file_lock(fl, &ctx->flc_lease) { 8847 unsigned char type = fl->c.flc_type; 8848 8849 if (fl->c.flc_flags == FL_LAYOUT) 8850 continue; 8851 if (fl->fl_lmops != &nfsd_lease_mng_ops) { 8852 /* 8853 * non-nfs lease, if it's a lease with F_RDLCK then 8854 * we are done; there isn't any write delegation 8855 * on this inode 8856 */ 8857 if (type == F_RDLCK) 8858 break; 8859 goto break_lease; 8860 } 8861 if (type == F_WRLCK) { 8862 dp = fl->c.flc_owner; 8863 if (dp->dl_recall.cb_clp == *(rqstp->rq_lease_breaker)) { 8864 spin_unlock(&ctx->flc_lock); 8865 return 0; 8866 } 8867 break_lease: 8868 nfsd_stats_wdeleg_getattr_inc(nn); 8869 dp = fl->c.flc_owner; 8870 ncf = &dp->dl_cb_fattr; 8871 nfs4_cb_getattr(&dp->dl_cb_fattr); 8872 spin_unlock(&ctx->flc_lock); 8873 wait_on_bit_timeout(&ncf->ncf_cb_flags, CB_GETATTR_BUSY, 8874 TASK_INTERRUPTIBLE, NFSD_CB_GETATTR_TIMEOUT); 8875 if (ncf->ncf_cb_status) { 8876 /* Recall delegation only if client didn't respond */ 8877 status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ)); 8878 if (status != nfserr_jukebox || 8879 !nfsd_wait_for_delegreturn(rqstp, inode)) 8880 return status; 8881 } 8882 if (!ncf->ncf_file_modified && 8883 (ncf->ncf_initial_cinfo != ncf->ncf_cb_change || 8884 ncf->ncf_cur_fsize != ncf->ncf_cb_fsize)) 8885 ncf->ncf_file_modified = true; 8886 if (ncf->ncf_file_modified) { 8887 /* 8888 * Per section 10.4.3 of RFC 8881, the server would 8889 * not update the file's metadata with the client's 8890 * modified size 8891 */ 8892 attrs.ia_mtime = attrs.ia_ctime = current_time(inode); 8893 attrs.ia_valid = ATTR_MTIME | ATTR_CTIME; 8894 setattr_copy(&nop_mnt_idmap, inode, &attrs); 8895 mark_inode_dirty(inode); 8896 ncf->ncf_cur_fsize = ncf->ncf_cb_fsize; 8897 *size = ncf->ncf_cur_fsize; 8898 *modified = true; 8899 } 8900 return 0; 8901 } 8902 break; 8903 } 8904 spin_unlock(&ctx->flc_lock); 8905 return 0; 8906 } 8907