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