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/nfsd_ssc.h> 49 50 #include "xdr4.h" 51 #include "nfs4ctl.h" 52 #include "xdr4cb.h" 53 #include "vfs.h" 54 #include "current_stateid.h" 55 #include "stats.h" 56 #include "nfserr.h" 57 58 #include "netns.h" 59 #include "pnfs.h" 60 #include "filecache.h" 61 #include "nfs4xdr_gen.h" 62 #include "trace.h" 63 64 #define NFSDDBG_FACILITY NFSDDBG_PROC 65 66 #define all_ones {{ ~0, ~0}, ~0} 67 static const stateid_t one_stateid = { 68 .si_generation = ~0, 69 .si_opaque = all_ones, 70 }; 71 static const stateid_t zero_stateid = { 72 /* all fields zero */ 73 }; 74 static const stateid_t currentstateid = { 75 .si_generation = 1, 76 }; 77 static const stateid_t close_stateid = { 78 .si_generation = 0xffffffffU, 79 }; 80 81 static u64 current_sessionid = 1; 82 83 bool nfsd_delegts_enabled __read_mostly = true; 84 85 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) 86 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) 87 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) 88 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t))) 89 90 /* forward declarations */ 91 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner); 92 static void nfs4_free_ol_stateid(struct nfs4_stid *stid); 93 static void nfsd4_end_grace(struct nfsd_net *nn); 94 static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps); 95 static void nfsd4_file_hash_remove(struct nfs4_file *fi); 96 static void deleg_reaper(struct nfsd_net *nn, unsigned long backlog); 97 static void nfsd4_drop_revoked_stid(struct nfs4_stid *s) 98 __releases(&s->sc_client->cl_lock); 99 100 static const struct lease_manager_operations nfsd_lease_mng_ops; 101 102 /* Locking: */ 103 104 enum nfsd4_st_mutex_lock_subclass { 105 OPEN_STATEID_MUTEX = 0, 106 LOCK_STATEID_MUTEX = 1, 107 }; 108 109 /* 110 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for 111 * the refcount on the open stateid to drop. 112 */ 113 static DECLARE_WAIT_QUEUE_HEAD(close_wq); 114 115 /* 116 * A waitqueue where a writer to clients/#/ctl destroying a client can 117 * wait for cl_rpc_users to drop to 0 and then for the client to be 118 * unhashed. 119 */ 120 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq); 121 122 static struct kmem_cache *client_slab; 123 static struct kmem_cache *openowner_slab; 124 static struct kmem_cache *lockowner_slab; 125 static struct kmem_cache *file_slab; 126 static struct kmem_cache *stateid_slab; 127 static struct kmem_cache *deleg_slab; 128 static struct kmem_cache *odstate_slab; 129 static struct kmem_cache *async_copy_slab; 130 131 static void free_session(struct nfsd4_session *); 132 133 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops; 134 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops; 135 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops; 136 static const struct nfsd4_callback_ops nfsd4_cb_notify_ops; 137 138 static struct workqueue_struct *laundry_wq; 139 140 int nfsd4_create_laundry_wq(void) 141 { 142 int rc = 0; 143 144 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); 145 if (laundry_wq == NULL) 146 rc = -ENOMEM; 147 return rc; 148 } 149 150 void nfsd4_destroy_laundry_wq(void) 151 { 152 destroy_workqueue(laundry_wq); 153 } 154 155 static bool is_session_dead(struct nfsd4_session *ses) 156 { 157 return ses->se_dead; 158 } 159 160 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) 161 { 162 if (atomic_read(&ses->se_ref) > ref_held_by_me) 163 return nfserr_jukebox; 164 ses->se_dead = true; 165 return nfs_ok; 166 } 167 168 static bool is_client_expired(struct nfs4_client *clp) 169 { 170 return clp->cl_time == 0; 171 } 172 173 static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn, 174 struct nfs4_client *clp) 175 { 176 if (clp->cl_state != NFSD4_ACTIVE) 177 atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0); 178 } 179 180 static __be32 get_client_locked(struct nfs4_client *clp) 181 { 182 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 183 184 lockdep_assert_held(&nn->client_lock); 185 186 if (is_client_expired(clp)) 187 return nfserr_expired; 188 atomic_inc(&clp->cl_rpc_users); 189 nfsd4_dec_courtesy_client_count(nn, clp); 190 clp->cl_state = NFSD4_ACTIVE; 191 return nfs_ok; 192 } 193 194 /* must be called under the client_lock */ 195 static inline void 196 renew_client_locked(struct nfs4_client *clp) 197 { 198 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 199 200 if (is_client_expired(clp)) { 201 WARN_ON(1); 202 printk("%s: client (clientid %08x/%08x) already expired\n", 203 __func__, 204 clp->cl_clientid.cl_boot, 205 clp->cl_clientid.cl_id); 206 return; 207 } 208 209 list_move_tail(&clp->cl_lru, &nn->client_lru); 210 clp->cl_time = ktime_get_boottime_seconds(); 211 nfsd4_dec_courtesy_client_count(nn, clp); 212 clp->cl_state = NFSD4_ACTIVE; 213 } 214 215 /* 216 * Finish a cl_rpc_users unpin with the client_lock held. A 217 * revocation walk clears @renew so the client whose state it is 218 * revoking is not revived; every other caller renews the lease of 219 * a still-active client. 220 */ 221 static void __put_client_locked(struct nfs4_client *clp, bool renew) 222 { 223 if (is_client_expired(clp)) 224 wake_up_all(&expiry_wq); 225 else if (renew) 226 renew_client_locked(clp); 227 } 228 229 static void put_client_renew_locked(struct nfs4_client *clp) 230 { 231 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 232 233 lockdep_assert_held(&nn->client_lock); 234 235 if (atomic_dec_and_test(&clp->cl_rpc_users)) 236 __put_client_locked(clp, true); 237 } 238 239 static void put_client_renew(struct nfs4_client *clp) 240 { 241 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 242 243 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock)) 244 return; 245 __put_client_locked(clp, true); 246 spin_unlock(&nn->client_lock); 247 } 248 249 static void put_client_no_renew_locked(struct nfs4_client *clp) 250 { 251 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 252 253 lockdep_assert_held(&nn->client_lock); 254 255 if (atomic_dec_and_test(&clp->cl_rpc_users)) 256 __put_client_locked(clp, false); 257 } 258 259 static void put_client_no_renew(struct nfs4_client *clp) 260 { 261 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 262 263 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock)) 264 return; 265 __put_client_locked(clp, false); 266 spin_unlock(&nn->client_lock); 267 } 268 269 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) 270 { 271 __be32 status; 272 273 if (is_session_dead(ses)) 274 return nfserr_badsession; 275 status = get_client_locked(ses->se_client); 276 if (status) 277 return status; 278 atomic_inc(&ses->se_ref); 279 return nfs_ok; 280 } 281 282 static void nfsd4_put_session_locked(struct nfsd4_session *ses) 283 { 284 struct nfs4_client *clp = ses->se_client; 285 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 286 287 lockdep_assert_held(&nn->client_lock); 288 289 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) 290 free_session(ses); 291 put_client_renew_locked(clp); 292 } 293 294 static void nfsd4_put_session(struct nfsd4_session *ses) 295 { 296 struct nfs4_client *clp = ses->se_client; 297 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 298 299 spin_lock(&nn->client_lock); 300 nfsd4_put_session_locked(ses); 301 spin_unlock(&nn->client_lock); 302 } 303 304 static struct nfsd4_blocked_lock * 305 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh, 306 struct nfsd_net *nn) 307 { 308 struct nfsd4_blocked_lock *cur, *found = NULL; 309 310 spin_lock(&nn->blocked_locks_lock); 311 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) { 312 if (fh_match(fh, &cur->nbl_fh)) { 313 list_del_init(&cur->nbl_list); 314 WARN_ON(list_empty(&cur->nbl_lru)); 315 list_del_init(&cur->nbl_lru); 316 found = cur; 317 break; 318 } 319 } 320 spin_unlock(&nn->blocked_locks_lock); 321 if (found) 322 locks_delete_block(&found->nbl_lock); 323 return found; 324 } 325 326 static struct nfsd4_blocked_lock * 327 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh, 328 struct nfsd_net *nn) 329 { 330 struct nfsd4_blocked_lock *nbl; 331 332 nbl = find_blocked_lock(lo, fh, nn); 333 if (!nbl) { 334 nbl = kmalloc_obj(*nbl); 335 if (nbl) { 336 INIT_LIST_HEAD(&nbl->nbl_list); 337 INIT_LIST_HEAD(&nbl->nbl_lru); 338 fh_copy_shallow(&nbl->nbl_fh, fh); 339 locks_init_lock(&nbl->nbl_lock); 340 kref_init(&nbl->nbl_kref); 341 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client, 342 &nfsd4_cb_notify_lock_ops, 343 NFSPROC4_CLNT_CB_NOTIFY_LOCK); 344 } 345 } 346 return nbl; 347 } 348 349 static void 350 free_nbl(struct kref *kref) 351 { 352 struct nfsd4_blocked_lock *nbl; 353 354 nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref); 355 locks_release_private(&nbl->nbl_lock); 356 kfree(nbl); 357 } 358 359 static void 360 free_blocked_lock(struct nfsd4_blocked_lock *nbl) 361 { 362 locks_delete_block(&nbl->nbl_lock); 363 kref_put(&nbl->nbl_kref, free_nbl); 364 } 365 366 /* A blocked lock's flc_owner is its nfs4_lockowner. */ 367 static struct nfs4_client * 368 nbl_client(struct nfsd4_blocked_lock *nbl) 369 { 370 struct nfs4_lockowner *lo; 371 372 lo = (struct nfs4_lockowner *)nbl->nbl_lock.c.flc_owner; 373 return lo->lo_owner.so_client; 374 } 375 376 static void 377 remove_blocked_locks(struct nfs4_lockowner *lo) 378 { 379 struct nfs4_client *clp = lo->lo_owner.so_client; 380 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 381 struct nfsd4_blocked_lock *nbl; 382 LIST_HEAD(reaplist); 383 384 /* Dequeue all blocked locks */ 385 spin_lock(&nn->blocked_locks_lock); 386 while (!list_empty(&lo->lo_blocked)) { 387 nbl = list_first_entry(&lo->lo_blocked, 388 struct nfsd4_blocked_lock, 389 nbl_list); 390 list_del_init(&nbl->nbl_list); 391 WARN_ON(list_empty(&nbl->nbl_lru)); 392 list_move(&nbl->nbl_lru, &reaplist); 393 } 394 spin_unlock(&nn->blocked_locks_lock); 395 396 /* Now free them */ 397 while (!list_empty(&reaplist)) { 398 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock, 399 nbl_lru); 400 list_del_init(&nbl->nbl_lru); 401 free_blocked_lock(nbl); 402 } 403 } 404 405 static bool 406 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb) 407 { 408 struct nfsd4_blocked_lock *nbl = container_of(cb, 409 struct nfsd4_blocked_lock, nbl_cb); 410 locks_delete_block(&nbl->nbl_lock); 411 return true; 412 } 413 414 static int 415 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task) 416 { 417 trace_nfsd_cb_notify_lock_done(&zero_stateid, task); 418 419 /* 420 * Since this is just an optimization, we don't try very hard if it 421 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and 422 * just quit trying on anything else. 423 */ 424 switch (task->tk_status) { 425 case -NFS4ERR_DELAY: 426 rpc_delay(task, 1 * HZ); 427 return 0; 428 default: 429 return 1; 430 } 431 } 432 433 static void 434 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb) 435 { 436 struct nfsd4_blocked_lock *nbl = container_of(cb, 437 struct nfsd4_blocked_lock, nbl_cb); 438 439 free_blocked_lock(nbl); 440 } 441 442 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = { 443 .prepare = nfsd4_cb_notify_lock_prepare, 444 .done = nfsd4_cb_notify_lock_done, 445 .release = nfsd4_cb_notify_lock_release, 446 .opcode = OP_CB_NOTIFY_LOCK, 447 }; 448 449 /* 450 * We store the NONE, READ, WRITE, and BOTH bits separately in the 451 * st_{access,deny}_bmap field of the stateid, in order to track not 452 * only what share bits are currently in force, but also what 453 * combinations of share bits previous opens have used. This allows us 454 * to enforce the recommendation in 455 * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that 456 * the server return an error if the client attempt to downgrade to a 457 * combination of share bits not explicable by closing some of its 458 * previous opens. 459 * 460 * This enforcement is arguably incomplete, since we don't keep 461 * track of access/deny bit combinations; so, e.g., we allow: 462 * 463 * OPEN allow read, deny write 464 * OPEN allow both, deny none 465 * DOWNGRADE allow read, deny none 466 * 467 * which we should reject. 468 * 469 * But you could also argue that our current code is already overkill, 470 * since it only exists to return NFS4ERR_INVAL on incorrect client 471 * behavior. 472 */ 473 static unsigned int 474 bmap_to_share_mode(unsigned long bmap) 475 { 476 int i; 477 unsigned int access = 0; 478 479 for (i = 1; i < 4; i++) { 480 if (test_bit(i, &bmap)) 481 access |= i; 482 } 483 return access; 484 } 485 486 /* set share access for a given stateid */ 487 static inline void 488 set_access(u32 access, struct nfs4_ol_stateid *stp) 489 { 490 unsigned char mask = 1 << access; 491 492 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); 493 stp->st_access_bmap |= mask; 494 } 495 496 /* clear share access for a given stateid */ 497 static inline void 498 clear_access(u32 access, struct nfs4_ol_stateid *stp) 499 { 500 unsigned char mask = 1 << access; 501 502 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); 503 stp->st_access_bmap &= ~mask; 504 } 505 506 /* test whether a given stateid has access */ 507 static inline bool 508 test_access(u32 access, struct nfs4_ol_stateid *stp) 509 { 510 unsigned char mask = 1 << access; 511 512 return (bool)(stp->st_access_bmap & mask); 513 } 514 515 /* set share deny for a given stateid */ 516 static inline void 517 set_deny(u32 deny, struct nfs4_ol_stateid *stp) 518 { 519 unsigned char mask = 1 << deny; 520 521 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); 522 stp->st_deny_bmap |= mask; 523 } 524 525 /* clear share deny for a given stateid */ 526 static inline void 527 clear_deny(u32 deny, struct nfs4_ol_stateid *stp) 528 { 529 unsigned char mask = 1 << deny; 530 531 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); 532 stp->st_deny_bmap &= ~mask; 533 } 534 535 /* test whether a given stateid is denying specific access */ 536 static inline bool 537 test_deny(u32 deny, struct nfs4_ol_stateid *stp) 538 { 539 unsigned char mask = 1 << deny; 540 541 return (bool)(stp->st_deny_bmap & mask); 542 } 543 544 static int nfs4_access_to_omode(u32 access) 545 { 546 switch (access & NFS4_SHARE_ACCESS_BOTH) { 547 case NFS4_SHARE_ACCESS_READ: 548 return O_RDONLY; 549 case NFS4_SHARE_ACCESS_WRITE: 550 return O_WRONLY; 551 case NFS4_SHARE_ACCESS_BOTH: 552 return O_RDWR; 553 } 554 WARN_ON_ONCE(1); 555 return O_RDONLY; 556 } 557 558 static inline int 559 access_permit_read(struct nfs4_ol_stateid *stp) 560 { 561 return test_access(NFS4_SHARE_ACCESS_READ, stp) || 562 test_access(NFS4_SHARE_ACCESS_BOTH, stp) || 563 test_access(NFS4_SHARE_ACCESS_WRITE, stp); 564 } 565 566 static inline int 567 access_permit_write(struct nfs4_ol_stateid *stp) 568 { 569 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || 570 test_access(NFS4_SHARE_ACCESS_BOTH, stp); 571 } 572 573 static inline struct nfs4_stateowner * 574 nfs4_get_stateowner(struct nfs4_stateowner *sop) 575 { 576 atomic_inc(&sop->so_count); 577 return sop; 578 } 579 580 static int 581 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) 582 { 583 return (sop->so_owner.len == owner->len) && 584 0 == memcmp(sop->so_owner.data, owner->data, owner->len); 585 } 586 587 static struct nfs4_openowner * 588 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, 589 struct nfs4_client *clp) 590 { 591 struct nfs4_stateowner *so; 592 593 lockdep_assert_held(&clp->cl_lock); 594 595 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], 596 so_strhash) { 597 if (!so->so_is_open_owner) 598 continue; 599 if (same_owner_str(so, &open->op_owner)) 600 return openowner(nfs4_get_stateowner(so)); 601 } 602 return NULL; 603 } 604 605 static inline u32 606 opaque_hashval(const void *ptr, int nbytes) 607 { 608 unsigned char *cptr = (unsigned char *) ptr; 609 610 u32 x = 0; 611 while (nbytes--) { 612 x *= 37; 613 x += *cptr++; 614 } 615 return x; 616 } 617 618 void 619 put_nfs4_file(struct nfs4_file *fi) 620 { 621 if (refcount_dec_and_test(&fi->fi_ref)) { 622 nfsd4_file_hash_remove(fi); 623 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); 624 WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); 625 kfree_rcu(fi, fi_rcu); 626 } 627 } 628 629 static struct nfsd_file * 630 find_writeable_file_locked(struct nfs4_file *f) 631 { 632 struct nfsd_file *ret; 633 634 lockdep_assert_held(&f->fi_lock); 635 636 ret = nfsd_file_get(f->fi_fds[O_WRONLY]); 637 if (!ret) 638 ret = nfsd_file_get(f->fi_fds[O_RDWR]); 639 return ret; 640 } 641 642 static struct nfsd_file * 643 find_writeable_file(struct nfs4_file *f) 644 { 645 struct nfsd_file *ret; 646 647 spin_lock(&f->fi_lock); 648 ret = find_writeable_file_locked(f); 649 spin_unlock(&f->fi_lock); 650 651 return ret; 652 } 653 654 static struct nfsd_file * 655 find_readable_file_locked(struct nfs4_file *f) 656 { 657 struct nfsd_file *ret; 658 659 lockdep_assert_held(&f->fi_lock); 660 661 ret = nfsd_file_get(f->fi_fds[O_RDONLY]); 662 if (!ret) 663 ret = nfsd_file_get(f->fi_fds[O_RDWR]); 664 return ret; 665 } 666 667 static struct nfsd_file * 668 find_readable_file(struct nfs4_file *f) 669 { 670 struct nfsd_file *ret; 671 672 spin_lock(&f->fi_lock); 673 ret = find_readable_file_locked(f); 674 spin_unlock(&f->fi_lock); 675 676 return ret; 677 } 678 679 struct nfsd_file * 680 find_any_file(struct nfs4_file *f) 681 { 682 struct nfsd_file *ret; 683 684 if (!f) 685 return NULL; 686 spin_lock(&f->fi_lock); 687 ret = nfsd_file_get(f->fi_fds[O_RDWR]); 688 if (!ret) { 689 ret = nfsd_file_get(f->fi_fds[O_WRONLY]); 690 if (!ret) 691 ret = nfsd_file_get(f->fi_fds[O_RDONLY]); 692 } 693 spin_unlock(&f->fi_lock); 694 return ret; 695 } 696 697 static struct nfsd_file *find_any_file_locked(struct nfs4_file *f) 698 { 699 lockdep_assert_held(&f->fi_lock); 700 701 if (f->fi_fds[O_RDWR]) 702 return f->fi_fds[O_RDWR]; 703 if (f->fi_fds[O_WRONLY]) 704 return f->fi_fds[O_WRONLY]; 705 if (f->fi_fds[O_RDONLY]) 706 return f->fi_fds[O_RDONLY]; 707 return NULL; 708 } 709 710 static atomic_long_t num_delegations; 711 unsigned long max_delegations; 712 713 /* 714 * Open owner state (share locks) 715 */ 716 717 /* hash tables for lock and open owners */ 718 #define OWNER_HASH_BITS 8 719 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) 720 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) 721 722 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) 723 { 724 unsigned int ret; 725 726 ret = opaque_hashval(ownername->data, ownername->len); 727 return ret & OWNER_HASH_MASK; 728 } 729 730 static struct rhltable nfs4_file_rhltable ____cacheline_aligned_in_smp; 731 732 static const struct rhashtable_params nfs4_file_rhash_params = { 733 .key_len = sizeof_field(struct nfs4_file, fi_inode), 734 .key_offset = offsetof(struct nfs4_file, fi_inode), 735 .head_offset = offsetof(struct nfs4_file, fi_rlist), 736 737 /* 738 * Start with a single page hash table to reduce resizing churn 739 * on light workloads. 740 */ 741 .min_size = 256, 742 .automatic_shrinking = true, 743 }; 744 745 /* 746 * Check if courtesy clients have conflicting access and resolve it if possible 747 * 748 * access: is op_share_access if share_access is true. 749 * Check if access mode, op_share_access, would conflict with 750 * the current deny mode of the file 'fp'. 751 * access: is op_share_deny if share_access is false. 752 * Check if the deny mode, op_share_deny, would conflict with 753 * current access of the file 'fp'. 754 * stp: skip checking this entry. 755 * new_stp: normal open, not open upgrade. 756 * 757 * Function returns: 758 * false - access/deny mode conflict with normal client. 759 * true - no conflict or conflict with courtesy client(s) is resolved. 760 */ 761 static bool 762 nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp, 763 struct nfs4_ol_stateid *stp, u32 access, bool share_access) 764 { 765 struct nfs4_ol_stateid *st; 766 bool resolvable = true; 767 unsigned char bmap; 768 struct nfsd_net *nn; 769 struct nfs4_client *clp; 770 771 lockdep_assert_held(&fp->fi_lock); 772 list_for_each_entry(st, &fp->fi_stateids, st_perfile) { 773 /* ignore lock stateid */ 774 if (st->st_openstp) 775 continue; 776 if (st == stp && new_stp) 777 continue; 778 /* check file access against deny mode or vice versa */ 779 bmap = share_access ? st->st_deny_bmap : st->st_access_bmap; 780 if (!(access & bmap_to_share_mode(bmap))) 781 continue; 782 clp = st->st_stid.sc_client; 783 if (try_to_expire_client(clp)) 784 continue; 785 resolvable = false; 786 break; 787 } 788 if (resolvable) { 789 clp = stp->st_stid.sc_client; 790 nn = net_generic(clp->net, nfsd_net_id); 791 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); 792 } 793 return resolvable; 794 } 795 796 static void 797 __nfs4_file_get_access(struct nfs4_file *fp, u32 access) 798 { 799 lockdep_assert_held(&fp->fi_lock); 800 801 if (access & NFS4_SHARE_ACCESS_WRITE) 802 atomic_inc(&fp->fi_access[O_WRONLY]); 803 if (access & NFS4_SHARE_ACCESS_READ) 804 atomic_inc(&fp->fi_access[O_RDONLY]); 805 } 806 807 static __be32 808 nfs4_file_get_access(struct nfs4_file *fp, u32 access) 809 { 810 lockdep_assert_held(&fp->fi_lock); 811 812 /* Does this access mode make sense? */ 813 if (access & ~NFS4_SHARE_ACCESS_BOTH) 814 return nfserr_inval; 815 816 /* Does it conflict with a deny mode already set? */ 817 if ((access & fp->fi_share_deny) != 0) 818 return nfserr_share_denied; 819 820 __nfs4_file_get_access(fp, access); 821 return nfs_ok; 822 } 823 824 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) 825 { 826 /* Common case is that there is no deny mode. */ 827 if (deny) { 828 /* Does this deny mode make sense? */ 829 if (deny & ~NFS4_SHARE_DENY_BOTH) 830 return nfserr_inval; 831 832 if ((deny & NFS4_SHARE_DENY_READ) && 833 atomic_read(&fp->fi_access[O_RDONLY])) 834 return nfserr_share_denied; 835 836 if ((deny & NFS4_SHARE_DENY_WRITE) && 837 atomic_read(&fp->fi_access[O_WRONLY])) 838 return nfserr_share_denied; 839 } 840 return nfs_ok; 841 } 842 843 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag) 844 { 845 might_lock(&fp->fi_lock); 846 847 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { 848 struct nfsd_file *f1 = NULL; 849 struct nfsd_file *f2 = NULL; 850 851 swap(f1, fp->fi_fds[oflag]); 852 if (atomic_read(&fp->fi_access[1 - oflag]) == 0) 853 swap(f2, fp->fi_fds[O_RDWR]); 854 spin_unlock(&fp->fi_lock); 855 if (f1) 856 nfsd_file_put(f1); 857 if (f2) 858 nfsd_file_put(f2); 859 } 860 } 861 862 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access) 863 { 864 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); 865 866 if (access & NFS4_SHARE_ACCESS_WRITE) 867 __nfs4_file_put_access(fp, O_WRONLY); 868 if (access & NFS4_SHARE_ACCESS_READ) 869 __nfs4_file_put_access(fp, O_RDONLY); 870 } 871 872 /* 873 * Allocate a new open/delegation state counter. This is needed for 874 * pNFS for proper return on close semantics. 875 * 876 * Note that we only allocate it for pNFS-enabled exports, otherwise 877 * all pointers to struct nfs4_clnt_odstate are always NULL. 878 */ 879 static struct nfs4_clnt_odstate * 880 alloc_clnt_odstate(struct nfs4_client *clp) 881 { 882 struct nfs4_clnt_odstate *co; 883 884 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); 885 if (co) { 886 co->co_client = clp; 887 refcount_set(&co->co_odcount, 1); 888 } 889 return co; 890 } 891 892 static void 893 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) 894 { 895 struct nfs4_file *fp = co->co_file; 896 897 lockdep_assert_held(&fp->fi_lock); 898 list_add(&co->co_perfile, &fp->fi_clnt_odstate); 899 } 900 901 static inline void 902 get_clnt_odstate(struct nfs4_clnt_odstate *co) 903 { 904 if (co) 905 refcount_inc(&co->co_odcount); 906 } 907 908 static void 909 put_clnt_odstate(struct nfs4_clnt_odstate *co) 910 { 911 struct nfs4_file *fp; 912 913 if (!co) 914 return; 915 916 fp = co->co_file; 917 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { 918 list_del(&co->co_perfile); 919 spin_unlock(&fp->fi_lock); 920 921 nfsd4_return_all_file_layouts(co->co_client, fp); 922 kmem_cache_free(odstate_slab, co); 923 } 924 } 925 926 static struct nfs4_clnt_odstate * 927 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) 928 { 929 struct nfs4_clnt_odstate *co; 930 struct nfs4_client *cl; 931 932 if (!new) 933 return NULL; 934 935 cl = new->co_client; 936 937 spin_lock(&fp->fi_lock); 938 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { 939 if (co->co_client == cl) { 940 get_clnt_odstate(co); 941 goto out; 942 } 943 } 944 co = new; 945 co->co_file = fp; 946 hash_clnt_odstate_locked(new); 947 out: 948 spin_unlock(&fp->fi_lock); 949 return co; 950 } 951 952 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab, 953 void (*sc_free)(struct nfs4_stid *)) 954 { 955 struct nfs4_stid *stid; 956 int new_id; 957 958 stid = kmem_cache_zalloc(slab, GFP_KERNEL); 959 if (!stid) 960 return NULL; 961 962 idr_preload(GFP_KERNEL); 963 spin_lock(&cl->cl_lock); 964 /* Reserving 0 for start of file in nfsdfs "states" file: */ 965 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT); 966 spin_unlock(&cl->cl_lock); 967 idr_preload_end(); 968 if (new_id < 0) 969 goto out_free; 970 971 stid->sc_free = sc_free; 972 stid->sc_client = cl; 973 stid->sc_stateid.si_opaque.so_id = new_id; 974 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; 975 /* Will be incremented before return to client: */ 976 refcount_set(&stid->sc_count, 1); 977 spin_lock_init(&stid->sc_lock); 978 INIT_LIST_HEAD(&stid->sc_cp_list); 979 980 return stid; 981 out_free: 982 kmem_cache_free(slab, stid); 983 return NULL; 984 } 985 986 /* 987 * Publish a COPY_NOTIFY stateid in nn->s2s_cp_stateids and link it onto the 988 * parent's sc_cp_list. That IDR holds only COPY_NOTIFY stateids. 989 */ 990 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid, 991 struct nfs4_stid *p_stid) 992 { 993 int new_id; 994 995 stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time; 996 stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id; 997 998 idr_preload(GFP_KERNEL); 999 spin_lock(&nn->s2s_cp_lock); 1000 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT); 1001 if (new_id >= 0) { 1002 struct nfs4_cpntf_state *cps = 1003 container_of(stid, struct nfs4_cpntf_state, cp_stateid); 1004 1005 stid->cs_stid.si_opaque.so_id = new_id; 1006 stid->cs_stid.si_generation = 1; 1007 /* 1008 * Set cs_type and link onto sc_cp_list under the same lock 1009 * that installed the IDR entry, so a concurrent 1010 * manage_cpntf_state() sees either no entry or a fully 1011 * linked cp_list. 1012 */ 1013 stid->cs_type = NFS4_COPYNOTIFY_STID; 1014 list_add(&cps->cp_list, &p_stid->sc_cp_list); 1015 } 1016 spin_unlock(&nn->s2s_cp_lock); 1017 idr_preload_end(); 1018 if (new_id < 0) 1019 return 0; 1020 return 1; 1021 } 1022 1023 /* sc_free for a copy offload stateid; runs from nfs4_put_stid(). */ 1024 static void nfsd4_free_async_copy_stid(struct nfs4_stid *stid) 1025 { 1026 struct nfsd4_async_copy *copy = 1027 container_of(stid, struct nfsd4_async_copy, cp_stid); 1028 1029 if (copy->copy_task) 1030 put_task_struct(copy->copy_task); 1031 kfree(copy->cp_copy.cp_src); 1032 kmem_cache_free(async_copy_slab, copy); 1033 } 1034 1035 /* 1036 * Allocate durable async COPY state. The offload stateid is a first-class 1037 * nfs4_stid (SC_TYPE_COPY) in the client's cl_stateids, so it is per-client 1038 * and uses the common refcounting/teardown. find_stateid_locked() hides it; 1039 * OFFLOAD_CANCEL/OFFLOAD_STATUS find it via clp->async_copies. 1040 */ 1041 struct nfsd4_async_copy *nfs4_alloc_copy_stid(struct nfs4_client *clp) 1042 { 1043 struct nfs4_stid *stid; 1044 1045 stid = nfs4_alloc_stid(clp, async_copy_slab, nfsd4_free_async_copy_stid); 1046 if (!stid) 1047 return NULL; 1048 stid->sc_type = SC_TYPE_COPY; 1049 /* RFC 7862 Section 4.8: a copy offload stateid's seqid MUST NOT be 0 */ 1050 stid->sc_stateid.si_generation = 1; 1051 return container_of(stid, struct nfsd4_async_copy, cp_stid); 1052 } 1053 1054 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn, 1055 struct nfs4_stid *p_stid) 1056 { 1057 struct nfs4_cpntf_state *cps; 1058 1059 cps = kzalloc_obj(struct nfs4_cpntf_state); 1060 if (!cps) 1061 return NULL; 1062 /* So a stale list_del_init() before linking is a no-op. */ 1063 INIT_LIST_HEAD(&cps->cp_list); 1064 cps->cpntf_time = ktime_get_boottime_seconds(); 1065 /* 1066 * Fully initialize the entry before nfs4_init_cp_state() publishes it, 1067 * since a concurrent OFFLOAD_CANCEL could then free it. Take an extra 1068 * reference for the caller (dropped with nfs4_put_cpntf_state()). 1069 */ 1070 memcpy(&cps->cp_p_stateid, &p_stid->sc_stateid, sizeof(stateid_t)); 1071 memcpy(&cps->cp_p_clid, &p_stid->sc_client->cl_clientid, 1072 sizeof(clientid_t)); 1073 refcount_set(&cps->cp_stateid.cs_count, 2); 1074 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, p_stid)) 1075 goto out_free; 1076 return cps; 1077 out_free: 1078 kfree(cps); 1079 return NULL; 1080 } 1081 1082 /* 1083 * Drop the parent's reference on an already-unlinked cpntf entry. If a 1084 * concurrent holder still owns a reference, its nfs4_put_cpntf_state() does 1085 * the final free. 1086 * 1087 * nn->s2s_cp_lock must be held. 1088 */ 1089 static void put_cpntf_state_unlinked_locked(struct nfs4_cpntf_state *cps) 1090 { 1091 WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID); 1092 WARN_ON_ONCE(!list_empty(&cps->cp_list)); 1093 1094 if (refcount_dec_and_test(&cps->cp_stateid.cs_count)) 1095 kfree(cps); 1096 } 1097 1098 /* 1099 * Unhash from the IDR and sc_cp_list. Gated on list_empty() to avoid 1100 * evicting a recycled so_id. 1101 */ 1102 static void nfsd4_unhash_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) 1103 { 1104 lockdep_assert_held(&nn->s2s_cp_lock); 1105 1106 if (!list_empty(&cps->cp_list)) { 1107 list_del_init(&cps->cp_list); 1108 idr_remove(&nn->s2s_cp_stateids, cps->cp_stateid.cs_stid.si_opaque.so_id); 1109 } 1110 } 1111 1112 /* 1113 * Revoke a copy-notify stateid: unlink it from the IDR and sc_cp_list first 1114 * so no new finder can discover it, then drop the membership reference. Every 1115 * revoke path (cancel, laundromat, drain) must use this rather than 1116 * _free_cpntf_state_locked(), which unlinks only at refcount zero and so could 1117 * let a second revoke free the entry under a concurrent reader. 1118 * 1119 * nn->s2s_cp_lock must be held. 1120 */ 1121 static void revoke_cpntf_state_locked(struct nfsd_net *nn, 1122 struct nfs4_cpntf_state *cps) 1123 { 1124 nfsd4_unhash_cpntf_state(nn, cps); 1125 put_cpntf_state_unlinked_locked(cps); 1126 } 1127 1128 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid) 1129 { 1130 struct nfs4_cpntf_state *cps, *tmp; 1131 struct nfsd_net *nn; 1132 1133 nn = net_generic(net, nfsd_net_id); 1134 spin_lock(&nn->s2s_cp_lock); 1135 /* 1136 * Revoke unlinks each entry before dropping the parent's reference, so 1137 * the drain terminates in one pass per entry regardless of cs_count; a 1138 * concurrent holder does the final kfree via nfs4_put_cpntf_state(). 1139 */ 1140 list_for_each_entry_safe(cps, tmp, &stid->sc_cp_list, cp_list) 1141 revoke_cpntf_state_locked(nn, cps); 1142 spin_unlock(&nn->s2s_cp_lock); 1143 } 1144 1145 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp) 1146 { 1147 struct nfs4_stid *stid; 1148 1149 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid); 1150 if (!stid) 1151 return NULL; 1152 1153 return openlockstateid(stid); 1154 } 1155 1156 /* 1157 * As the sc_free callback of deleg, this may be called by nfs4_put_stid 1158 * in nfsd_break_one_deleg. 1159 * Considering nfsd_break_one_deleg is called with the flc->flc_lock held, 1160 * this function mustn't ever sleep. 1161 */ 1162 static void nfs4_free_deleg(struct nfs4_stid *stid) 1163 { 1164 struct nfsd_net *nn = net_generic(stid->sc_client->net, nfsd_net_id); 1165 struct nfs4_delegation *dp = delegstateid(stid); 1166 1167 WARN_ON_ONCE(!list_empty(&stid->sc_cp_list)); 1168 WARN_ON_ONCE(!list_empty(&dp->dl_perfile)); 1169 WARN_ON_ONCE(!list_empty(&dp->dl_perclnt)); 1170 WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru)); 1171 /* The list outlives one recall, so ->release() cannot free it. */ 1172 nfsd41_cb_destroy_referring_call_list(&dp->dl_recall); 1173 kmem_cache_free(deleg_slab, stid); 1174 atomic_long_dec(&num_delegations); 1175 atomic_long_dec(&nn->nfsd_delegations); 1176 } 1177 1178 /* 1179 * When we recall a delegation, we should be careful not to hand it 1180 * out again straight away. 1181 * To ensure this we keep a pair of bloom filters ('new' and 'old') 1182 * in which the filehandles of recalled delegations are "stored". 1183 * If a filehandle appear in either filter, a delegation is blocked. 1184 * When a delegation is recalled, the filehandle is stored in the "new" 1185 * filter. 1186 * Every 30 seconds we swap the filters and clear the "new" one, 1187 * unless both are empty of course. This results in delegations for a 1188 * given filehandle being blocked for between 30 and 60 seconds. 1189 * 1190 * Each filter is 256 bits. We hash the filehandle to 32bit and use the 1191 * low 3 bytes as hash-table indices. 1192 * 1193 * 'blocked_delegations_lock', which is always taken in block_delegations(), 1194 * is used to manage concurrent access. Testing does not need the lock 1195 * except when swapping the two filters. 1196 */ 1197 static DEFINE_SPINLOCK(blocked_delegations_lock); 1198 static struct bloom_pair { 1199 int entries, old_entries; 1200 time64_t swap_time; 1201 int new; /* index into 'set' */ 1202 DECLARE_BITMAP(set[2], 256); 1203 } blocked_delegations; 1204 1205 static int delegation_blocked(struct knfsd_fh *fh) 1206 { 1207 u32 hash; 1208 struct bloom_pair *bd = &blocked_delegations; 1209 1210 if (bd->entries == 0) 1211 return 0; 1212 if (ktime_get_seconds() - bd->swap_time > 30) { 1213 spin_lock(&blocked_delegations_lock); 1214 if (ktime_get_seconds() - bd->swap_time > 30) { 1215 bd->entries -= bd->old_entries; 1216 bd->old_entries = bd->entries; 1217 bd->new = 1-bd->new; 1218 memset(bd->set[bd->new], 0, 1219 sizeof(bd->set[0])); 1220 bd->swap_time = ktime_get_seconds(); 1221 } 1222 spin_unlock(&blocked_delegations_lock); 1223 } 1224 hash = jhash(&fh->fh_raw, fh->fh_size, 0); 1225 if (test_bit(hash&255, bd->set[0]) && 1226 test_bit((hash>>8)&255, bd->set[0]) && 1227 test_bit((hash>>16)&255, bd->set[0])) 1228 return 1; 1229 1230 if (test_bit(hash&255, bd->set[1]) && 1231 test_bit((hash>>8)&255, bd->set[1]) && 1232 test_bit((hash>>16)&255, bd->set[1])) 1233 return 1; 1234 1235 return 0; 1236 } 1237 1238 static void block_delegations(struct knfsd_fh *fh) 1239 { 1240 u32 hash; 1241 struct bloom_pair *bd = &blocked_delegations; 1242 1243 hash = jhash(&fh->fh_raw, fh->fh_size, 0); 1244 1245 spin_lock(&blocked_delegations_lock); 1246 __set_bit(hash&255, bd->set[bd->new]); 1247 __set_bit((hash>>8)&255, bd->set[bd->new]); 1248 __set_bit((hash>>16)&255, bd->set[bd->new]); 1249 if (bd->entries == 0) 1250 bd->swap_time = ktime_get_seconds(); 1251 bd->entries += 1; 1252 spin_unlock(&blocked_delegations_lock); 1253 } 1254 1255 static struct nfs4_delegation * 1256 __alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, 1257 struct nfs4_clnt_odstate *odstate, u32 dl_type, 1258 void (*sc_free)(struct nfs4_stid *)) 1259 { 1260 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1261 struct nfs4_delegation *dp; 1262 struct nfs4_stid *stid; 1263 long n; 1264 1265 if (delegation_blocked(&fp->fi_fhandle)) 1266 return NULL; 1267 1268 n = atomic_long_inc_return(&num_delegations); 1269 atomic_long_inc(&nn->nfsd_delegations); 1270 if (n < 0 || n > max_delegations) 1271 goto out_dec; 1272 1273 stid = nfs4_alloc_stid(clp, deleg_slab, sc_free); 1274 if (stid == NULL) 1275 goto out_dec; 1276 1277 /* 1278 * delegation seqid's are never incremented. The 4.1 special 1279 * meaning of seqid 0 isn't meaningful, really, but let's avoid 1280 * 0 anyway just for consistency and use 1. 1281 */ 1282 dp = delegstateid(stid); 1283 dp->dl_stid.sc_stateid.si_generation = 1; 1284 INIT_LIST_HEAD(&dp->dl_perfile); 1285 INIT_LIST_HEAD(&dp->dl_perclnt); 1286 INIT_LIST_HEAD(&dp->dl_recall_lru); 1287 dp->dl_clnt_odstate = odstate; 1288 get_clnt_odstate(odstate); 1289 dp->dl_type = dl_type; 1290 dp->dl_retries = 1; 1291 dp->dl_recalled = false; 1292 dp->dl_recall_rejected = false; 1293 dp->dl_recall_grant.valid = false; 1294 dp->dl_recall_grant.retired_at_send = false; 1295 get_nfs4_file(fp); 1296 dp->dl_stid.sc_file = fp; 1297 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, 1298 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); 1299 return dp; 1300 out_dec: 1301 atomic_long_dec(&num_delegations); 1302 atomic_long_dec(&nn->nfsd_delegations); 1303 return NULL; 1304 } 1305 1306 static struct nfs4_delegation * 1307 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, 1308 struct nfs4_clnt_odstate *odstate, u32 dl_type) 1309 { 1310 struct nfs4_delegation *dp; 1311 1312 dp = __alloc_init_deleg(clp, fp, odstate, dl_type, nfs4_free_deleg); 1313 if (!dp) 1314 return NULL; 1315 1316 nfsd4_init_cb(&dp->dl_cb_fattr.ncf_getattr, dp->dl_stid.sc_client, 1317 &nfsd4_cb_getattr_ops, NFSPROC4_CLNT_CB_GETATTR); 1318 dp->dl_cb_fattr.ncf_file_modified = false; 1319 return dp; 1320 } 1321 1322 static void nfs4_free_dir_deleg(struct nfs4_stid *stid) 1323 { 1324 struct nfs4_delegation *dp = delegstateid(stid); 1325 struct nfsd4_cb_notify *ncn = &dp->dl_cb_notify; 1326 int i; 1327 1328 for (i = 0; i < ncn->ncn_evt_cnt; ++i) 1329 nfsd_notify_event_put(ncn->ncn_evt[i]); 1330 kfree(ncn->ncn_nf); 1331 kfree(ncn->ncn_masks); 1332 for (i = 0; i < NOTIFY4_PAGE_ARRAY_SIZE; i++) { 1333 if (!ncn->ncn_pages[i]) 1334 break; 1335 put_page(ncn->ncn_pages[i]); 1336 } 1337 nfs4_free_deleg(stid); 1338 } 1339 1340 static struct nfs4_delegation * 1341 alloc_init_dir_deleg(struct nfs4_client *clp, struct nfs4_file *fp) 1342 { 1343 struct nfs4_delegation *dp; 1344 struct nfsd4_cb_notify *ncn; 1345 int npages; 1346 1347 dp = __alloc_init_deleg(clp, fp, NULL, NFS4_OPEN_DELEGATE_READ, nfs4_free_dir_deleg); 1348 if (!dp) 1349 return NULL; 1350 1351 ncn = &dp->dl_cb_notify; 1352 1353 npages = alloc_pages_bulk(GFP_KERNEL, NOTIFY4_PAGE_ARRAY_SIZE, ncn->ncn_pages); 1354 if (npages != NOTIFY4_PAGE_ARRAY_SIZE) { 1355 nfs4_put_stid(&dp->dl_stid); 1356 return NULL; 1357 } 1358 1359 ncn->ncn_nf = kzalloc_objs(*ncn->ncn_nf, NOTIFY4_EVENT_QUEUE_SIZE); 1360 if (!ncn->ncn_nf) { 1361 nfs4_put_stid(&dp->dl_stid); 1362 return NULL; 1363 } 1364 ncn->ncn_masks = kcalloc(NOTIFY4_EVENT_QUEUE_SIZE, sizeof(*ncn->ncn_masks), GFP_KERNEL); 1365 if (!ncn->ncn_masks) { 1366 nfs4_put_stid(&dp->dl_stid); 1367 return NULL; 1368 } 1369 spin_lock_init(&ncn->ncn_lock); 1370 nfsd4_init_cb(&ncn->ncn_cb, dp->dl_stid.sc_client, 1371 &nfsd4_cb_notify_ops, NFSPROC4_CLNT_CB_NOTIFY); 1372 return dp; 1373 } 1374 1375 void 1376 nfs4_put_stid(struct nfs4_stid *s) 1377 { 1378 struct nfs4_file *fp = s->sc_file; 1379 struct nfs4_client *clp = s->sc_client; 1380 struct svc_export *exp; 1381 1382 might_lock(&clp->cl_lock); 1383 1384 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) { 1385 wake_up_all(&close_wq); 1386 return; 1387 } 1388 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); 1389 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) 1390 atomic_dec(&s->sc_client->cl_admin_revoked); 1391 /* Read under cl_lock to serialize with drop_stid_export(). */ 1392 exp = s->sc_export; 1393 nfs4_free_cpntf_statelist(clp->net, s); 1394 spin_unlock(&clp->cl_lock); 1395 s->sc_free(s); 1396 if (exp) 1397 exp_put(exp); 1398 if (fp) 1399 put_nfs4_file(fp); 1400 } 1401 1402 void 1403 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) 1404 { 1405 stateid_t *src = &stid->sc_stateid; 1406 1407 spin_lock(&stid->sc_lock); 1408 if (unlikely(++src->si_generation == 0)) 1409 src->si_generation = 1; 1410 memcpy(dst, src, sizeof(*dst)); 1411 spin_unlock(&stid->sc_lock); 1412 } 1413 1414 static void put_deleg_file(struct nfs4_file *fp) 1415 { 1416 struct nfsd_file *rnf = NULL; 1417 struct nfsd_file *nf = NULL; 1418 1419 spin_lock(&fp->fi_lock); 1420 if (--fp->fi_delegees == 0) { 1421 nf = rcu_dereference_protected(fp->fi_deleg_file, 1422 lockdep_is_held(&fp->fi_lock)); 1423 RCU_INIT_POINTER(fp->fi_deleg_file, NULL); 1424 swap(rnf, fp->fi_rdeleg_file); 1425 } 1426 spin_unlock(&fp->fi_lock); 1427 1428 if (nf) 1429 nfsd_file_put(nf); 1430 if (rnf) { 1431 nfsd_file_put(rnf); 1432 nfs4_file_put_access(fp, NFS4_SHARE_ACCESS_READ); 1433 } 1434 } 1435 1436 static void nfsd4_finalize_deleg_timestamps(struct nfs4_delegation *dp, struct file *f) 1437 { 1438 /* don't do anything if FMODE_NOCMTIME isn't set */ 1439 if ((READ_ONCE(f->f_mode) & FMODE_NOCMTIME) == 0) 1440 return; 1441 1442 spin_lock(&f->f_lock); 1443 f->f_mode &= ~FMODE_NOCMTIME; 1444 spin_unlock(&f->f_lock); 1445 1446 /* was it never written? */ 1447 if (!dp->dl_written) 1448 return; 1449 1450 /* did it get a setattr for the timestamps at some point? */ 1451 if (dp->dl_setattr) 1452 return; 1453 1454 /* Stamp everything to "now" */ 1455 nfsd_update_cmtime_attr(f, ATTR_ATIME); 1456 } 1457 1458 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp) 1459 { 1460 struct nfs4_file *fp = dp->dl_stid.sc_file; 1461 struct nfsd_file *nf = rcu_dereference_protected(fp->fi_deleg_file, 1); 1462 1463 WARN_ON_ONCE(!fp->fi_delegees); 1464 1465 nfsd4_finalize_deleg_timestamps(dp, nf->nf_file); 1466 kernel_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp); 1467 nfsd_fsnotify_recalc_mask(nf); 1468 put_deleg_file(fp); 1469 } 1470 1471 static void destroy_unhashed_deleg(struct nfs4_delegation *dp) 1472 { 1473 put_clnt_odstate(dp->dl_clnt_odstate); 1474 nfs4_unlock_deleg_lease(dp); 1475 nfs4_put_stid(&dp->dl_stid); 1476 } 1477 1478 /** 1479 * nfs4_delegation_exists - Discover if this delegation already exists 1480 * @clp: a pointer to the nfs4_client we're granting a delegation to 1481 * @fp: a pointer to the nfs4_file we're granting a delegation on 1482 * 1483 * Return: 1484 * On success: true iff an existing delegation is found 1485 */ 1486 1487 static bool 1488 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp) 1489 { 1490 struct nfs4_delegation *searchdp = NULL; 1491 struct nfs4_client *searchclp = NULL; 1492 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1493 1494 lockdep_assert_held(&nn->deleg_lock); 1495 lockdep_assert_held(&fp->fi_lock); 1496 1497 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { 1498 searchclp = searchdp->dl_stid.sc_client; 1499 if (clp == searchclp) { 1500 return true; 1501 } 1502 } 1503 return false; 1504 } 1505 1506 /** 1507 * hash_delegation_locked - Add a delegation to the appropriate lists 1508 * @dp: a pointer to the nfs4_delegation we are adding. 1509 * @fp: a pointer to the nfs4_file we're granting a delegation on 1510 * 1511 * Return: 1512 * On success: NULL if the delegation was successfully hashed. 1513 * 1514 * On error: -EAGAIN if one was previously granted to this 1515 * nfs4_client for this nfs4_file. Delegation is not hashed. 1516 * 1517 */ 1518 1519 static int 1520 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) 1521 { 1522 struct nfs4_client *clp = dp->dl_stid.sc_client; 1523 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1524 1525 lockdep_assert_held(&nn->deleg_lock); 1526 lockdep_assert_held(&fp->fi_lock); 1527 lockdep_assert_held(&clp->cl_lock); 1528 1529 if (nfs4_delegation_exists(clp, fp)) 1530 return -EAGAIN; 1531 refcount_inc(&dp->dl_stid.sc_count); 1532 dp->dl_stid.sc_type = SC_TYPE_DELEG; 1533 list_add(&dp->dl_perfile, &fp->fi_delegations); 1534 list_add(&dp->dl_perclnt, &clp->cl_delegations); 1535 clp->cl_deleg_count++; 1536 return 0; 1537 } 1538 1539 static bool delegation_hashed(struct nfs4_delegation *dp) 1540 { 1541 return !(list_empty(&dp->dl_perfile)); 1542 } 1543 1544 static bool 1545 unhash_delegation_locked(struct nfs4_delegation *dp, unsigned short statusmask) 1546 { 1547 struct nfs4_file *fp = dp->dl_stid.sc_file; 1548 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, 1549 nfsd_net_id); 1550 1551 lockdep_assert_held(&nn->deleg_lock); 1552 1553 if (!delegation_hashed(dp)) 1554 return false; 1555 1556 if (statusmask == SC_STATUS_REVOKED && 1557 dp->dl_stid.sc_client->cl_minorversion == 0) 1558 statusmask = SC_STATUS_CLOSED; 1559 dp->dl_stid.sc_status |= statusmask; 1560 if (statusmask & SC_STATUS_ADMIN_REVOKED) 1561 atomic_inc(&dp->dl_stid.sc_client->cl_admin_revoked); 1562 1563 /* Ensure that deleg break won't try to requeue it */ 1564 ++dp->dl_time; 1565 spin_lock(&fp->fi_lock); 1566 list_del_init(&dp->dl_perclnt); 1567 dp->dl_stid.sc_client->cl_deleg_count--; 1568 list_del_init(&dp->dl_recall_lru); 1569 list_del_init(&dp->dl_perfile); 1570 spin_unlock(&fp->fi_lock); 1571 return true; 1572 } 1573 1574 static void destroy_delegation(struct nfs4_delegation *dp) 1575 { 1576 bool unhashed; 1577 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, 1578 nfsd_net_id); 1579 1580 spin_lock(&nn->deleg_lock); 1581 unhashed = unhash_delegation_locked(dp, SC_STATUS_CLOSED); 1582 spin_unlock(&nn->deleg_lock); 1583 if (unhashed) 1584 destroy_unhashed_deleg(dp); 1585 } 1586 1587 /** 1588 * revoke_delegation - dispose of a delegation the server has revoked 1589 * @dp: delegation to dispose of 1590 * 1591 * The caller holds a reference on @dp, which this function consumes. 1592 * On NFSv4.1 and newer, @dp's sc_status must already carry 1593 * SC_STATUS_REVOKED or SC_STATUS_ADMIN_REVOKED. 1594 * 1595 * @dp is parked on the client's cl_revoked list to await a FREE_STATEID. 1596 * Where none can arrive, @dp is destroyed here instead: FREE_STATEID has 1597 * already freed it, or the client rejected the recall with 1598 * NFS4ERR_BADHANDLE or NFS4ERR_BAD_STATEID and holds no record of the 1599 * delegation. NFS4ERR_ADMIN_REVOKED still prompts one, so an 1600 * administrative revoke waits on cl_revoked. 1601 * 1602 * Context: Takes and releases the client's cl_lock; may sleep after 1603 * dropping it. 1604 */ 1605 static void revoke_delegation(struct nfs4_delegation *dp) 1606 { 1607 struct nfs4_client *clp = dp->dl_stid.sc_client; 1608 1609 WARN_ON(!list_empty(&dp->dl_recall_lru)); 1610 WARN_ON_ONCE(dp->dl_stid.sc_client->cl_minorversion > 0 && 1611 !(dp->dl_stid.sc_status & 1612 (SC_STATUS_REVOKED | SC_STATUS_ADMIN_REVOKED))); 1613 1614 trace_nfsd_stid_revoke(&dp->dl_stid); 1615 1616 spin_lock(&clp->cl_lock); 1617 if (dp->dl_stid.sc_status & SC_STATUS_FREED) { 1618 list_del_init(&dp->dl_recall_lru); 1619 goto out; 1620 } 1621 if (dp->dl_recall_rejected && 1622 !(dp->dl_stid.sc_status & SC_STATUS_ADMIN_REVOKED)) { 1623 /* 1624 * SC_STATUS_CLOSED, set under cl_lock, makes a racing 1625 * FREE_STATEID bail out rather than drop this reference 1626 * too. The put releases what cl_revoked would have held. 1627 */ 1628 dp->dl_stid.sc_status |= SC_STATUS_CLOSED; 1629 spin_unlock(&clp->cl_lock); 1630 nfs4_put_stid(&dp->dl_stid); 1631 destroy_unhashed_deleg(dp); 1632 return; 1633 } 1634 list_add(&dp->dl_recall_lru, &clp->cl_revoked); 1635 dp->dl_stid.sc_status |= SC_STATUS_FREEABLE; 1636 out: 1637 spin_unlock(&clp->cl_lock); 1638 destroy_unhashed_deleg(dp); 1639 } 1640 1641 /* 1642 * SETCLIENTID state 1643 */ 1644 1645 static unsigned int clientid_hashval(u32 id) 1646 { 1647 return id & CLIENT_HASH_MASK; 1648 } 1649 1650 static unsigned int clientstr_hashval(struct xdr_netobj name) 1651 { 1652 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK; 1653 } 1654 1655 /* 1656 * A stateid that had a deny mode associated with it is being released 1657 * or downgraded. Recalculate the deny mode on the file. 1658 */ 1659 static void 1660 recalculate_deny_mode(struct nfs4_file *fp) 1661 { 1662 struct nfs4_ol_stateid *stp; 1663 u32 old_deny; 1664 1665 spin_lock(&fp->fi_lock); 1666 old_deny = fp->fi_share_deny; 1667 fp->fi_share_deny = 0; 1668 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) { 1669 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); 1670 if (fp->fi_share_deny == old_deny) 1671 break; 1672 } 1673 spin_unlock(&fp->fi_lock); 1674 } 1675 1676 static void 1677 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) 1678 { 1679 int i; 1680 bool change = false; 1681 1682 for (i = 1; i < 4; i++) { 1683 if ((i & deny) != i) { 1684 change = true; 1685 clear_deny(i, stp); 1686 } 1687 } 1688 1689 /* Recalculate per-file deny mode if there was a change */ 1690 if (change) 1691 recalculate_deny_mode(stp->st_stid.sc_file); 1692 } 1693 1694 /* release all access and file references for a given stateid */ 1695 static void 1696 release_all_access(struct nfs4_ol_stateid *stp) 1697 { 1698 int i; 1699 struct nfs4_file *fp = stp->st_stid.sc_file; 1700 1701 if (fp && stp->st_deny_bmap != 0) 1702 recalculate_deny_mode(fp); 1703 1704 for (i = 1; i < 4; i++) { 1705 if (test_access(i, stp)) 1706 nfs4_file_put_access(stp->st_stid.sc_file, i); 1707 clear_access(i, stp); 1708 } 1709 } 1710 1711 /** 1712 * nfs4_replay_free_cache - release dynamically allocated replay buffer 1713 * @rp: replay cache to reset 1714 * 1715 * If @rp->rp_buf points to a kmalloc'd buffer, free it and reset 1716 * rp_buf to the inline rp_ibuf. Always zeroes rp_buflen. 1717 */ 1718 void nfs4_replay_free_cache(struct nfs4_replay *rp) 1719 { 1720 if (rp->rp_buf != rp->rp_ibuf) 1721 kfree(rp->rp_buf); 1722 rp->rp_buf = rp->rp_ibuf; 1723 rp->rp_buflen = 0; 1724 } 1725 1726 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) 1727 { 1728 nfs4_replay_free_cache(&sop->so_replay); 1729 kfree(sop->so_owner.data); 1730 sop->so_ops->so_free(sop); 1731 } 1732 1733 static void nfs4_put_stateowner(struct nfs4_stateowner *sop) 1734 { 1735 struct nfs4_client *clp = sop->so_client; 1736 1737 might_lock(&clp->cl_lock); 1738 1739 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) 1740 return; 1741 sop->so_ops->so_unhash(sop); 1742 spin_unlock(&clp->cl_lock); 1743 nfs4_free_stateowner(sop); 1744 } 1745 1746 static bool 1747 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp) 1748 { 1749 return list_empty(&stp->st_perfile); 1750 } 1751 1752 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) 1753 { 1754 struct nfs4_file *fp = stp->st_stid.sc_file; 1755 1756 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); 1757 1758 if (list_empty(&stp->st_perfile)) 1759 return false; 1760 1761 spin_lock(&fp->fi_lock); 1762 list_del_init(&stp->st_perfile); 1763 spin_unlock(&fp->fi_lock); 1764 list_del(&stp->st_perstateowner); 1765 return true; 1766 } 1767 1768 static void nfs4_free_ol_stateid(struct nfs4_stid *stid) 1769 { 1770 struct nfs4_ol_stateid *stp = openlockstateid(stid); 1771 1772 put_clnt_odstate(stp->st_clnt_odstate); 1773 release_all_access(stp); 1774 if (stp->st_stateowner) 1775 nfs4_put_stateowner(stp->st_stateowner); 1776 if (!list_empty(&stid->sc_cp_list)) 1777 nfs4_free_cpntf_statelist(stid->sc_client->net, stid); 1778 kmem_cache_free(stateid_slab, stid); 1779 } 1780 1781 static void nfs4_free_lock_stateid(struct nfs4_stid *stid) 1782 { 1783 struct nfs4_ol_stateid *stp = openlockstateid(stid); 1784 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); 1785 struct nfsd_file *nf; 1786 1787 nf = find_any_file(stp->st_stid.sc_file); 1788 if (nf) { 1789 get_file(nf->nf_file); 1790 filp_close(nf->nf_file, (fl_owner_t)lo); 1791 nfsd_file_put(nf); 1792 } 1793 nfs4_free_ol_stateid(stid); 1794 } 1795 1796 /* 1797 * Put the persistent reference to an already unhashed generic stateid, while 1798 * holding the cl_lock. If it's the last reference, then put it onto the 1799 * reaplist for later destruction. 1800 */ 1801 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, 1802 struct list_head *reaplist) 1803 { 1804 struct nfs4_stid *s = &stp->st_stid; 1805 struct nfs4_client *clp = s->sc_client; 1806 1807 lockdep_assert_held(&clp->cl_lock); 1808 1809 WARN_ON_ONCE(!list_empty(&stp->st_locks)); 1810 1811 if (!refcount_dec_and_test(&s->sc_count)) { 1812 wake_up_all(&close_wq); 1813 return; 1814 } 1815 1816 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); 1817 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) 1818 atomic_dec(&s->sc_client->cl_admin_revoked); 1819 list_add(&stp->st_locks, reaplist); 1820 } 1821 1822 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) 1823 { 1824 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); 1825 1826 if (!unhash_ol_stateid(stp)) 1827 return false; 1828 list_del_init(&stp->st_locks); 1829 stp->st_stid.sc_status |= SC_STATUS_CLOSED; 1830 return true; 1831 } 1832 1833 static void release_lock_stateid(struct nfs4_ol_stateid *stp) 1834 { 1835 struct nfs4_client *clp = stp->st_stid.sc_client; 1836 bool unhashed; 1837 1838 spin_lock(&clp->cl_lock); 1839 unhashed = unhash_lock_stateid(stp); 1840 spin_unlock(&clp->cl_lock); 1841 if (unhashed) 1842 nfs4_put_stid(&stp->st_stid); 1843 } 1844 1845 static void unhash_lockowner_locked(struct nfs4_lockowner *lo) 1846 { 1847 struct nfs4_client *clp = lo->lo_owner.so_client; 1848 1849 lockdep_assert_held(&clp->cl_lock); 1850 1851 list_del_init(&lo->lo_owner.so_strhash); 1852 } 1853 1854 /* 1855 * Free a list of generic stateids that were collected earlier after being 1856 * fully unhashed. 1857 */ 1858 static void 1859 free_ol_stateid_reaplist(struct list_head *reaplist) 1860 { 1861 struct nfs4_ol_stateid *stp; 1862 struct svc_export *exp; 1863 struct nfs4_file *fp; 1864 1865 might_sleep(); 1866 1867 while (!list_empty(reaplist)) { 1868 stp = list_first_entry(reaplist, struct nfs4_ol_stateid, 1869 st_locks); 1870 list_del(&stp->st_locks); 1871 fp = stp->st_stid.sc_file; 1872 exp = stp->st_stid.sc_export; 1873 stp->st_stid.sc_free(&stp->st_stid); 1874 if (exp) 1875 exp_put(exp); 1876 if (fp) 1877 put_nfs4_file(fp); 1878 } 1879 } 1880 1881 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, 1882 struct list_head *reaplist) 1883 { 1884 struct nfs4_ol_stateid *stp; 1885 1886 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); 1887 1888 while (!list_empty(&open_stp->st_locks)) { 1889 stp = list_entry(open_stp->st_locks.next, 1890 struct nfs4_ol_stateid, st_locks); 1891 unhash_lock_stateid(stp); 1892 put_ol_stateid_locked(stp, reaplist); 1893 } 1894 } 1895 1896 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, 1897 struct list_head *reaplist) 1898 { 1899 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); 1900 1901 if (!unhash_ol_stateid(stp)) 1902 return false; 1903 release_open_stateid_locks(stp, reaplist); 1904 return true; 1905 } 1906 1907 static void release_open_stateid(struct nfs4_ol_stateid *stp) 1908 { 1909 LIST_HEAD(reaplist); 1910 1911 spin_lock(&stp->st_stid.sc_client->cl_lock); 1912 stp->st_stid.sc_status |= SC_STATUS_CLOSED; 1913 if (unhash_open_stateid(stp, &reaplist)) 1914 put_ol_stateid_locked(stp, &reaplist); 1915 spin_unlock(&stp->st_stid.sc_client->cl_lock); 1916 free_ol_stateid_reaplist(&reaplist); 1917 } 1918 1919 static bool nfs4_openowner_unhashed(struct nfs4_openowner *oo) 1920 { 1921 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock); 1922 1923 return list_empty(&oo->oo_owner.so_strhash) && 1924 list_empty(&oo->oo_perclient); 1925 } 1926 1927 static void unhash_openowner_locked(struct nfs4_openowner *oo) 1928 { 1929 struct nfs4_client *clp = oo->oo_owner.so_client; 1930 1931 lockdep_assert_held(&clp->cl_lock); 1932 1933 list_del_init(&oo->oo_owner.so_strhash); 1934 list_del_init(&oo->oo_perclient); 1935 } 1936 1937 static void release_last_closed_stateid(struct nfs4_openowner *oo) 1938 { 1939 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, 1940 nfsd_net_id); 1941 struct nfs4_ol_stateid *s; 1942 1943 spin_lock(&nn->client_lock); 1944 s = oo->oo_last_closed_stid; 1945 if (s) { 1946 list_del_init(&oo->oo_close_lru); 1947 oo->oo_last_closed_stid = NULL; 1948 } 1949 spin_unlock(&nn->client_lock); 1950 if (s) 1951 nfs4_put_stid(&s->st_stid); 1952 } 1953 1954 static void release_openowner(struct nfs4_openowner *oo) 1955 { 1956 struct nfs4_ol_stateid *stp; 1957 struct nfs4_client *clp = oo->oo_owner.so_client; 1958 LIST_HEAD(reaplist); 1959 1960 spin_lock(&clp->cl_lock); 1961 unhash_openowner_locked(oo); 1962 while (!list_empty(&oo->oo_owner.so_stateids)) { 1963 stp = list_first_entry(&oo->oo_owner.so_stateids, 1964 struct nfs4_ol_stateid, st_perstateowner); 1965 if (unhash_open_stateid(stp, &reaplist)) 1966 put_ol_stateid_locked(stp, &reaplist); 1967 } 1968 spin_unlock(&clp->cl_lock); 1969 free_ol_stateid_reaplist(&reaplist); 1970 release_last_closed_stateid(oo); 1971 nfs4_put_stateowner(&oo->oo_owner); 1972 } 1973 1974 static struct nfs4_stid *find_one_sb_stid(struct nfs4_client *clp, 1975 struct super_block *sb, 1976 unsigned int sc_types) 1977 { 1978 unsigned long id = 0; 1979 struct nfs4_stid *stid; 1980 1981 spin_lock(&clp->cl_lock); 1982 while ((stid = idr_get_next_ul(&clp->cl_stateids, &id)) != NULL) { 1983 if ((stid->sc_type & sc_types) && 1984 stid->sc_status == 0 && 1985 stid->sc_file->fi_inode->i_sb == sb) { 1986 refcount_inc(&stid->sc_count); 1987 break; 1988 } 1989 id++; 1990 } 1991 spin_unlock(&clp->cl_lock); 1992 return stid; 1993 } 1994 1995 /* 1996 * Release the export reference an admin-revoked stateid holds, 1997 * so the svc_export (and its vfsmount) is not pinned until the 1998 * client issues FREE_STATEID. sc_export is no longer consulted 1999 * once SC_STATUS_ADMIN_REVOKED is set. 2000 */ 2001 static void drop_stid_export(struct nfs4_client *clp, 2002 struct nfs4_stid *stid) 2003 { 2004 struct svc_export *exp; 2005 2006 spin_lock(&clp->cl_lock); 2007 exp = stid->sc_export; 2008 stid->sc_export = NULL; 2009 spin_unlock(&clp->cl_lock); 2010 if (exp) 2011 exp_put(exp); 2012 } 2013 2014 static void revoke_ol_stid(struct nfs4_client *clp, 2015 struct nfs4_ol_stateid *stp) 2016 { 2017 struct nfs4_stid *stid = &stp->st_stid; 2018 2019 lockdep_assert_held(&stp->st_mutex); 2020 spin_lock(&clp->cl_lock); 2021 if (stid->sc_status == 0) { 2022 stid->sc_status |= SC_STATUS_ADMIN_REVOKED; 2023 atomic_inc(&clp->cl_admin_revoked); 2024 spin_unlock(&clp->cl_lock); 2025 if (stid->sc_type == SC_TYPE_LOCK) { 2026 struct nfs4_lockowner *lo = 2027 lockowner(stp->st_stateowner); 2028 struct nfsd_file *nf; 2029 2030 nf = find_any_file(stp->st_stid.sc_file); 2031 if (nf) { 2032 get_file(nf->nf_file); 2033 filp_close(nf->nf_file, (fl_owner_t)lo); 2034 nfsd_file_put(nf); 2035 } 2036 } 2037 release_all_access(stp); 2038 drop_stid_export(clp, stid); 2039 } else 2040 spin_unlock(&clp->cl_lock); 2041 } 2042 2043 static void revoke_one_stid(struct nfsd_net *nn, struct nfs4_client *clp, 2044 struct nfs4_stid *stid) 2045 { 2046 struct nfs4_ol_stateid *stp; 2047 struct nfs4_delegation *dp; 2048 2049 switch (stid->sc_type) { 2050 case SC_TYPE_OPEN: 2051 stp = openlockstateid(stid); 2052 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); 2053 revoke_ol_stid(clp, stp); 2054 mutex_unlock(&stp->st_mutex); 2055 break; 2056 case SC_TYPE_LOCK: 2057 stp = openlockstateid(stid); 2058 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX); 2059 revoke_ol_stid(clp, stp); 2060 mutex_unlock(&stp->st_mutex); 2061 break; 2062 case SC_TYPE_DELEG: 2063 /* 2064 * Extra reference guards against concurrent FREE_STATEID. 2065 */ 2066 refcount_inc(&stid->sc_count); 2067 dp = delegstateid(stid); 2068 spin_lock(&nn->deleg_lock); 2069 if (!unhash_delegation_locked(dp, SC_STATUS_ADMIN_REVOKED)) 2070 dp = NULL; 2071 spin_unlock(&nn->deleg_lock); 2072 if (dp) { 2073 revoke_delegation(dp); 2074 drop_stid_export(clp, stid); 2075 } else 2076 nfs4_put_stid(stid); 2077 break; 2078 case SC_TYPE_LAYOUT: 2079 spin_lock(&clp->cl_lock); 2080 if (stid->sc_status == 0) { 2081 stid->sc_status |= SC_STATUS_ADMIN_REVOKED; 2082 atomic_inc(&clp->cl_admin_revoked); 2083 } 2084 spin_unlock(&clp->cl_lock); 2085 nfsd4_close_layout(layoutstateid(stid)); 2086 drop_stid_export(clp, stid); 2087 break; 2088 } 2089 } 2090 2091 /** 2092 * nfsd4_revoke_states - revoke all nfsv4 states associated with given filesystem 2093 * @nn: used to identify instance of nfsd (there is one per net namespace) 2094 * @sb: super_block used to identify target filesystem 2095 * 2096 * All nfs4 states (open, lock, delegation, layout) held by the server instance 2097 * and associated with a file on the given filesystem will be revoked resulting 2098 * in any files being closed and so all references from nfsd to the filesystem 2099 * being released. Thus nfsd will no longer prevent the filesystem from being 2100 * unmounted. 2101 * 2102 * The clients which own the states will subsequently be notified that the 2103 * states have been "admin-revoked". 2104 * 2105 * Context: Caller must hold nfsd_mutex with NFSD_NET_UP set. Outside 2106 * that window nn->conf_id_hashtbl is unallocated or freed, 2107 * so the walk would dereference a NULL or dangling pointer. 2108 */ 2109 void nfsd4_revoke_states(struct nfsd_net *nn, struct super_block *sb) 2110 { 2111 unsigned int idhashval; 2112 unsigned int sc_types; 2113 2114 lockdep_assert_held(&nfsd_mutex); 2115 2116 sc_types = SC_TYPE_OPEN | SC_TYPE_LOCK | SC_TYPE_DELEG | SC_TYPE_LAYOUT; 2117 2118 spin_lock(&nn->client_lock); 2119 for (idhashval = 0; idhashval < CLIENT_HASH_SIZE; idhashval++) { 2120 struct list_head *head = &nn->conf_id_hashtbl[idhashval]; 2121 struct nfs4_client *clp; 2122 retry: 2123 list_for_each_entry(clp, head, cl_idhash) { 2124 struct nfs4_stid *stid; 2125 2126 /* 2127 * force_expire_client() ignores cl_rpc_users once 2128 * its wait_event() has passed, so pinning cannot 2129 * keep an already-expiring client alive; the 2130 * expiry path revokes its states instead. 2131 */ 2132 if (is_client_expired(clp)) 2133 continue; 2134 stid = find_one_sb_stid(clp, sb, sc_types); 2135 if (stid) { 2136 atomic_inc(&clp->cl_rpc_users); 2137 spin_unlock(&nn->client_lock); 2138 revoke_one_stid(nn, clp, stid); 2139 nfs4_put_stid(stid); 2140 spin_lock(&nn->client_lock); 2141 if (clp->cl_minorversion == 0) 2142 /* Allow cleanup after a lease period. 2143 * store_release ensures cleanup will 2144 * see any newly revoked states if it 2145 * sees the time updated. 2146 */ 2147 nn->nfs40_last_revoke = 2148 ktime_get_boottime_seconds(); 2149 put_client_no_renew_locked(clp); 2150 goto retry; 2151 } 2152 } 2153 } 2154 spin_unlock(&nn->client_lock); 2155 } 2156 2157 static struct nfs4_stid *find_one_export_stid(struct nfs4_client *clp, 2158 const struct path *path, 2159 unsigned int sc_types) 2160 { 2161 unsigned long id = 0; 2162 struct nfs4_stid *stid; 2163 2164 spin_lock(&clp->cl_lock); 2165 while ((stid = idr_get_next_ul(&clp->cl_stateids, &id)) != NULL) { 2166 if ((stid->sc_type & sc_types) && 2167 stid->sc_status == 0 && 2168 stid->sc_export && 2169 path_equal(&stid->sc_export->ex_path, path)) { 2170 refcount_inc(&stid->sc_count); 2171 break; 2172 } 2173 id++; 2174 } 2175 spin_unlock(&clp->cl_lock); 2176 return stid; 2177 } 2178 2179 /** 2180 * nfsd4_revoke_export_states - revoke nfsv4 states acquired through an export 2181 * @nn: used to identify instance of nfsd (there is one per net namespace) 2182 * @path: export path whose states should be revoked 2183 * 2184 * All nfs4 states (open, lock, delegation, layout) acquired through any 2185 * export matching @path are revoked, regardless of which client holds 2186 * them. Matching is by path identity (dentry + vfsmount), so multiple 2187 * svc_export objects for the same path -- one per auth_domain -- are 2188 * handled correctly. 2189 * 2190 * Userspace (exportfs -u) sends this after removing the last client 2191 * for a path, enabling the underlying filesystem to be unmounted. 2192 * 2193 * Context: Caller must hold nfsd_mutex with NFSD_NET_UP set. Outside 2194 * that window nn->conf_id_hashtbl is unallocated or freed, 2195 * so the walk would dereference a NULL or dangling pointer. 2196 */ 2197 void nfsd4_revoke_export_states(struct nfsd_net *nn, const struct path *path) 2198 { 2199 unsigned int idhashval; 2200 unsigned int sc_types; 2201 2202 lockdep_assert_held(&nfsd_mutex); 2203 2204 sc_types = SC_TYPE_OPEN | SC_TYPE_LOCK | SC_TYPE_DELEG | SC_TYPE_LAYOUT; 2205 2206 spin_lock(&nn->client_lock); 2207 for (idhashval = 0; idhashval < CLIENT_HASH_SIZE; idhashval++) { 2208 struct list_head *head = &nn->conf_id_hashtbl[idhashval]; 2209 struct nfs4_client *clp; 2210 retry: 2211 list_for_each_entry(clp, head, cl_idhash) { 2212 struct nfs4_stid *stid; 2213 2214 /* Skip or pin clp as in nfsd4_revoke_states(). */ 2215 if (is_client_expired(clp)) 2216 continue; 2217 stid = find_one_export_stid(clp, path, sc_types); 2218 if (stid) { 2219 atomic_inc(&clp->cl_rpc_users); 2220 spin_unlock(&nn->client_lock); 2221 revoke_one_stid(nn, clp, stid); 2222 nfs4_put_stid(stid); 2223 spin_lock(&nn->client_lock); 2224 if (clp->cl_minorversion == 0) 2225 nn->nfs40_last_revoke = 2226 ktime_get_boottime_seconds(); 2227 put_client_no_renew_locked(clp); 2228 goto retry; 2229 } 2230 } 2231 } 2232 spin_unlock(&nn->client_lock); 2233 } 2234 2235 static inline int 2236 hash_sessionid(struct nfs4_sessionid *sessionid) 2237 { 2238 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; 2239 2240 return sid->sequence % SESSION_HASH_SIZE; 2241 } 2242 2243 #ifdef CONFIG_SUNRPC_DEBUG 2244 static inline void 2245 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) 2246 { 2247 u32 *ptr = (u32 *)(&sessionid->data[0]); 2248 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); 2249 } 2250 #else 2251 static inline void 2252 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) 2253 { 2254 } 2255 #endif 2256 2257 /* 2258 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it 2259 * won't be used for replay. 2260 */ 2261 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) 2262 { 2263 struct nfs4_stateowner *so = cstate->replay_owner; 2264 2265 if (nfserr == nfserr_replay_me) 2266 return; 2267 2268 if (!seqid_mutating_err(ntohl(nfserr))) { 2269 nfsd4_cstate_clear_replay(cstate); 2270 return; 2271 } 2272 if (!so) 2273 return; 2274 if (so->so_is_open_owner) 2275 release_last_closed_stateid(openowner(so)); 2276 so->so_seqid++; 2277 return; 2278 } 2279 2280 static void 2281 gen_sessionid(struct nfsd4_session *ses) 2282 { 2283 struct nfs4_client *clp = ses->se_client; 2284 struct nfsd4_sessionid *sid; 2285 2286 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; 2287 sid->clientid = clp->cl_clientid; 2288 sid->sequence = current_sessionid++; 2289 sid->reserved = 0; 2290 } 2291 2292 /* 2293 * The protocol defines ca_maxresponssize_cached to include the size of 2294 * the rpc header, but all we need to cache is the data starting after 2295 * the end of the initial SEQUENCE operation--the rest we regenerate 2296 * each time. Therefore we can advertise a ca_maxresponssize_cached 2297 * value that is the number of bytes in our cache plus a few additional 2298 * bytes. In order to stay on the safe side, and not promise more than 2299 * we can cache, those additional bytes must be the minimum possible: 24 2300 * bytes of rpc header (xid through accept state, with AUTH_NULL 2301 * verifier), 12 for the compound header (with zero-length tag), and 44 2302 * for the SEQUENCE op response: 2303 */ 2304 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) 2305 2306 static struct shrinker *nfsd_slot_shrinker; 2307 static DEFINE_SPINLOCK(nfsd_session_list_lock); 2308 static LIST_HEAD(nfsd_session_list); 2309 /* The sum of "target_slots" on every session, slot 0 included. */ 2310 static atomic_t nfsd_total_target_slots = ATOMIC_INIT(0); 2311 /* Session count, subtracted from the sum to exclude slot 0. */ 2312 static atomic_t nfsd_total_sessions = ATOMIC_INIT(0); 2313 2314 static void 2315 free_session_slots(struct nfsd4_session *ses, int from) 2316 { 2317 int i; 2318 2319 if (from >= ses->se_fchannel.maxreqs) 2320 return; 2321 2322 for (i = from; i < ses->se_fchannel.maxreqs; i++) { 2323 struct nfsd4_slot *slot = xa_load(&ses->se_slots, i); 2324 2325 /* 2326 * Save the seqid in case we reactivate this slot. 2327 * This will never require a memory allocation so GFP 2328 * flag is irrelevant 2329 */ 2330 xa_store(&ses->se_slots, i, xa_mk_value(slot->sl_seqid), 0); 2331 free_svc_cred(&slot->sl_cred); 2332 kfree(slot); 2333 } 2334 ses->se_fchannel.maxreqs = from; 2335 if (ses->se_target_maxslots > from) { 2336 int delta = ses->se_target_maxslots - from; 2337 2338 atomic_sub(delta, &nfsd_total_target_slots); 2339 /* Retain one slot so the session can make forward progress. */ 2340 ses->se_target_maxslots = from ?: 1; 2341 } 2342 } 2343 2344 /* 2345 * This interface can be used by a shrinker to reduce the target max-slots 2346 * for a session so that some slots can eventually be freed. 2347 * It uses spin_trylock() as it may be called in a context where another 2348 * spinlock is held that has a dependency on client_lock. As shrinkers are 2349 * best-effort, skipping a session with the client_lock already held has no 2350 * great cost. 2351 */ 2352 static int 2353 reduce_session_slots(struct nfsd4_session *ses, int dec) 2354 { 2355 struct nfsd_net *nn = net_generic(ses->se_client->net, 2356 nfsd_net_id); 2357 int ret = 0; 2358 2359 if (ses->se_target_maxslots <= 1) 2360 return ret; 2361 if (!spin_trylock(&nn->client_lock)) 2362 return ret; 2363 ret = min(dec, ses->se_target_maxslots-1); 2364 ses->se_target_maxslots -= ret; 2365 atomic_sub(ret, &nfsd_total_target_slots); 2366 ses->se_slot_gen += 1; 2367 if (ses->se_slot_gen == 0) { 2368 int i; 2369 ses->se_slot_gen = 1; 2370 for (i = 0; i < ses->se_fchannel.maxreqs; i++) { 2371 struct nfsd4_slot *slot = xa_load(&ses->se_slots, i); 2372 slot->sl_generation = 0; 2373 } 2374 } 2375 spin_unlock(&nn->client_lock); 2376 return ret; 2377 } 2378 2379 static struct nfsd4_slot *nfsd4_alloc_slot(struct nfsd4_channel_attrs *fattrs, 2380 int index, gfp_t gfp) 2381 { 2382 struct nfsd4_slot *slot; 2383 size_t size; 2384 2385 /* 2386 * The RPC and NFS session headers are never saved in 2387 * the slot reply cache buffer. 2388 */ 2389 size = fattrs->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ ? 2390 0 : fattrs->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; 2391 2392 slot = kzalloc_flex(*slot, sl_data, size, gfp); 2393 if (!slot) 2394 return NULL; 2395 slot->sl_index = index; 2396 return slot; 2397 } 2398 2399 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, 2400 struct nfsd4_channel_attrs *battrs) 2401 { 2402 int numslots = fattrs->maxreqs; 2403 struct nfsd4_session *new; 2404 struct nfsd4_slot *slot; 2405 int i; 2406 2407 new = kzalloc_obj(*new); 2408 if (!new) 2409 return NULL; 2410 xa_init(&new->se_slots); 2411 2412 slot = nfsd4_alloc_slot(fattrs, 0, GFP_KERNEL); 2413 if (!slot || xa_is_err(xa_store(&new->se_slots, 0, slot, GFP_KERNEL))) 2414 goto out_free; 2415 2416 for (i = 1; i < numslots; i++) { 2417 const gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN; 2418 slot = nfsd4_alloc_slot(fattrs, i, gfp); 2419 if (!slot) 2420 break; 2421 if (xa_is_err(xa_store(&new->se_slots, i, slot, gfp))) { 2422 kfree(slot); 2423 break; 2424 } 2425 } 2426 fattrs->maxreqs = i; 2427 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); 2428 new->se_target_maxslots = i; 2429 atomic_add(i, &nfsd_total_target_slots); 2430 new->se_cb_slot_avail = ~0U; 2431 new->se_cb_highest_slot = min(battrs->maxreqs - 1, 2432 NFSD_BC_SLOT_TABLE_SIZE - 1); 2433 spin_lock_init(&new->se_lock); 2434 return new; 2435 out_free: 2436 kfree(slot); 2437 xa_destroy(&new->se_slots); 2438 kfree(new); 2439 return NULL; 2440 } 2441 2442 static void free_conn(struct nfsd4_conn *c) 2443 { 2444 svc_xprt_put(c->cn_xprt); 2445 kfree(c); 2446 } 2447 2448 static void nfsd4_conn_lost(struct svc_xpt_user *u) 2449 { 2450 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); 2451 struct nfs4_client *clp = c->cn_session->se_client; 2452 2453 trace_nfsd_cb_lost(clp); 2454 2455 spin_lock(&clp->cl_lock); 2456 if (!list_empty(&c->cn_persession)) { 2457 list_del(&c->cn_persession); 2458 free_conn(c); 2459 } 2460 nfsd4_probe_callback(clp); 2461 spin_unlock(&clp->cl_lock); 2462 } 2463 2464 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags) 2465 { 2466 struct nfsd4_conn *conn; 2467 2468 conn = kmalloc_obj(struct nfsd4_conn); 2469 if (!conn) 2470 return NULL; 2471 svc_xprt_get(rqstp->rq_xprt); 2472 conn->cn_xprt = rqstp->rq_xprt; 2473 conn->cn_flags = flags; 2474 INIT_LIST_HEAD(&conn->cn_xpt_user.list); 2475 return conn; 2476 } 2477 2478 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) 2479 { 2480 conn->cn_session = ses; 2481 list_add(&conn->cn_persession, &ses->se_conns); 2482 } 2483 2484 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) 2485 { 2486 struct nfs4_client *clp = ses->se_client; 2487 2488 spin_lock(&clp->cl_lock); 2489 __nfsd4_hash_conn(conn, ses); 2490 spin_unlock(&clp->cl_lock); 2491 } 2492 2493 static int nfsd4_register_conn(struct nfsd4_conn *conn) 2494 { 2495 conn->cn_xpt_user.callback = nfsd4_conn_lost; 2496 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); 2497 } 2498 2499 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses) 2500 { 2501 int ret; 2502 2503 nfsd4_hash_conn(conn, ses); 2504 ret = nfsd4_register_conn(conn); 2505 if (ret) 2506 /* oops; xprt is already down: */ 2507 nfsd4_conn_lost(&conn->cn_xpt_user); 2508 /* We may have gained or lost a callback channel: */ 2509 nfsd4_probe_callback_sync(ses->se_client); 2510 } 2511 2512 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses) 2513 { 2514 u32 dir = NFS4_CDFC4_FORE; 2515 2516 if (cses->flags & SESSION4_BACK_CHAN) 2517 dir |= NFS4_CDFC4_BACK; 2518 return alloc_conn(rqstp, dir); 2519 } 2520 2521 /* must be called under client_lock */ 2522 static void nfsd4_del_conns(struct nfsd4_session *s) 2523 { 2524 struct nfs4_client *clp = s->se_client; 2525 struct nfsd4_conn *c; 2526 2527 spin_lock(&clp->cl_lock); 2528 while (!list_empty(&s->se_conns)) { 2529 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); 2530 list_del_init(&c->cn_persession); 2531 spin_unlock(&clp->cl_lock); 2532 2533 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); 2534 free_conn(c); 2535 2536 spin_lock(&clp->cl_lock); 2537 } 2538 spin_unlock(&clp->cl_lock); 2539 } 2540 2541 static void __free_session(struct nfsd4_session *ses) 2542 { 2543 free_session_slots(ses, 0); 2544 xa_destroy(&ses->se_slots); 2545 kfree_rcu(ses, rcu_head); 2546 } 2547 2548 static void free_session(struct nfsd4_session *ses) 2549 { 2550 nfsd4_del_conns(ses); 2551 __free_session(ses); 2552 } 2553 2554 /** 2555 * nfsd_slot_shrinker_count - report reclaimable DRC slots 2556 * @s: shrinker descriptor (unused) 2557 * @sc: shrink control (unused) 2558 * 2559 * Return: a positive count of reclaimable slots, or SHRINK_EMPTY when 2560 * there is nothing to reclaim. 2561 */ 2562 static unsigned long 2563 nfsd_slot_shrinker_count(struct shrinker *s, struct shrink_control *sc) 2564 { 2565 int count; 2566 2567 /* 2568 * To prevent session deadlock, one slot of each session (slot 0) 2569 * is not reclaimable while the session is active. Thus the number 2570 * of sessions is subtracted from the total number of target slots. 2571 */ 2572 count = atomic_read(&nfsd_total_target_slots) - 2573 atomic_read(&nfsd_total_sessions); 2574 2575 return count > 0 ? count : SHRINK_EMPTY; 2576 } 2577 2578 /** 2579 * nfsd_slot_shrinker_scan - reclaim DRC slots under memory pressure 2580 * @s: shrinker descriptor (unused) 2581 * @sc: shrink control; @sc->nr_to_scan bounds the sessions visited, 2582 * @sc->nr_scanned reports how many were visited 2583 * 2584 * Return: the number of session slots NFSD will release. 2585 */ 2586 static unsigned long 2587 nfsd_slot_shrinker_scan(struct shrinker *s, struct shrink_control *sc) 2588 { 2589 struct nfsd4_session *ses; 2590 unsigned long scanned = 0; 2591 unsigned long freed = 0; 2592 2593 /* 2594 * Each visited session releases at most one slot. After 2595 * nr_to_scan sessions have been visited, the list head is 2596 * rotated past the last visited session so the next scan 2597 * resumes from there. 2598 */ 2599 spin_lock(&nfsd_session_list_lock); 2600 list_for_each_entry(ses, &nfsd_session_list, se_all_sessions) { 2601 freed += reduce_session_slots(ses, 1); 2602 scanned += 1; 2603 if (scanned >= sc->nr_to_scan) { 2604 /* Move starting point for next scan */ 2605 list_move(&nfsd_session_list, &ses->se_all_sessions); 2606 break; 2607 } 2608 } 2609 spin_unlock(&nfsd_session_list_lock); 2610 sc->nr_scanned = scanned; 2611 return freed; 2612 } 2613 2614 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses) 2615 { 2616 int idx; 2617 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 2618 2619 new->se_client = clp; 2620 gen_sessionid(new); 2621 2622 INIT_LIST_HEAD(&new->se_conns); 2623 2624 atomic_set(&new->se_ref, 0); 2625 new->se_dead = false; 2626 new->se_cb_prog = cses->callback_prog; 2627 new->se_cb_sec = cses->cb_sec; 2628 2629 for (idx = 0; idx < NFSD_BC_SLOT_TABLE_SIZE; ++idx) 2630 new->se_cb_seq_nr[idx] = 1; 2631 2632 idx = hash_sessionid(&new->se_sessionid); 2633 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); 2634 spin_lock(&clp->cl_lock); 2635 list_add(&new->se_perclnt, &clp->cl_sessions); 2636 spin_unlock(&clp->cl_lock); 2637 2638 spin_lock(&nfsd_session_list_lock); 2639 list_add_tail(&new->se_all_sessions, &nfsd_session_list); 2640 atomic_inc(&nfsd_total_sessions); 2641 spin_unlock(&nfsd_session_list_lock); 2642 2643 { 2644 struct sockaddr *sa = svc_addr(rqstp); 2645 /* 2646 * This is a little silly; with sessions there's no real 2647 * use for the callback address. Use the peer address 2648 * as a reasonable default for now, but consider fixing 2649 * the rpc client not to require an address in the 2650 * future: 2651 */ 2652 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); 2653 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); 2654 } 2655 } 2656 2657 /* caller must hold client_lock */ 2658 static struct nfsd4_session * 2659 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) 2660 { 2661 struct nfsd4_session *elem; 2662 int idx; 2663 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2664 2665 lockdep_assert_held(&nn->client_lock); 2666 2667 dump_sessionid(__func__, sessionid); 2668 idx = hash_sessionid(sessionid); 2669 /* Search in the appropriate list */ 2670 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { 2671 if (!memcmp(elem->se_sessionid.data, sessionid->data, 2672 NFS4_MAX_SESSIONID_LEN)) { 2673 return elem; 2674 } 2675 } 2676 2677 dprintk("%s: session not found\n", __func__); 2678 return NULL; 2679 } 2680 2681 static struct nfsd4_session * 2682 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, 2683 __be32 *ret) 2684 { 2685 struct nfsd4_session *session; 2686 __be32 status = nfserr_badsession; 2687 2688 session = __find_in_sessionid_hashtbl(sessionid, net); 2689 if (!session) 2690 goto out; 2691 status = nfsd4_get_session_locked(session); 2692 if (status) 2693 session = NULL; 2694 out: 2695 *ret = status; 2696 return session; 2697 } 2698 2699 /* caller must hold client_lock */ 2700 static void 2701 unhash_session(struct nfsd4_session *ses) 2702 { 2703 struct nfs4_client *clp = ses->se_client; 2704 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2705 2706 lockdep_assert_held(&nn->client_lock); 2707 2708 list_del(&ses->se_hash); 2709 spin_lock(&ses->se_client->cl_lock); 2710 list_del(&ses->se_perclnt); 2711 spin_unlock(&ses->se_client->cl_lock); 2712 spin_lock(&nfsd_session_list_lock); 2713 list_del(&ses->se_all_sessions); 2714 atomic_dec(&nfsd_total_sessions); 2715 spin_unlock(&nfsd_session_list_lock); 2716 } 2717 2718 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ 2719 static int 2720 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) 2721 { 2722 /* 2723 * We're assuming the clid was not given out from a boot 2724 * precisely 2^32 (about 136 years) before this one. That seems 2725 * a safe assumption: 2726 */ 2727 if (clid->cl_boot == (u32)nn->boot_time) 2728 return 0; 2729 trace_nfsd_clid_stale(clid); 2730 return 1; 2731 } 2732 2733 static struct nfs4_client *alloc_client(struct xdr_netobj name, 2734 struct nfsd_net *nn) 2735 { 2736 struct nfs4_client *clp; 2737 int i; 2738 2739 if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients && 2740 atomic_read(&nn->nfsd_courtesy_clients) > 0) 2741 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); 2742 2743 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL); 2744 if (clp == NULL) 2745 return NULL; 2746 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL); 2747 if (clp->cl_name.data == NULL) 2748 goto err_no_name; 2749 clp->cl_ownerstr_hashtbl = kmalloc_objs(struct list_head, 2750 OWNER_HASH_SIZE); 2751 if (!clp->cl_ownerstr_hashtbl) 2752 goto err_no_hashtbl; 2753 clp->cl_callback_wq = alloc_ordered_workqueue("nfsd4_callbacks", 0); 2754 if (!clp->cl_callback_wq) 2755 goto err_no_callback_wq; 2756 2757 for (i = 0; i < OWNER_HASH_SIZE; i++) 2758 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); 2759 INIT_LIST_HEAD(&clp->cl_sessions); 2760 idr_init(&clp->cl_stateids); 2761 atomic_set(&clp->cl_rpc_users, 0); 2762 clp->cl_cb_state = NFSD4_CB_UNKNOWN; 2763 clp->cl_state = NFSD4_ACTIVE; 2764 atomic_inc(&nn->nfs4_client_count); 2765 atomic_set(&clp->cl_delegs_in_recall, 0); 2766 INIT_LIST_HEAD(&clp->cl_idhash); 2767 INIT_LIST_HEAD(&clp->cl_openowners); 2768 INIT_LIST_HEAD(&clp->cl_delegations); 2769 INIT_LIST_HEAD(&clp->cl_lru); 2770 INIT_LIST_HEAD(&clp->cl_revoked); 2771 #ifdef CONFIG_NFSD_PNFS 2772 INIT_LIST_HEAD(&clp->cl_lo_states); 2773 #endif 2774 #ifdef CONFIG_NFSD_SCSILAYOUT 2775 xa_init(&clp->cl_dev_fences); 2776 mutex_init(&clp->cl_fence_mutex); 2777 #endif 2778 INIT_LIST_HEAD(&clp->async_copies); 2779 spin_lock_init(&clp->async_lock); 2780 spin_lock_init(&clp->cl_lock); 2781 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); 2782 return clp; 2783 err_no_callback_wq: 2784 kfree(clp->cl_ownerstr_hashtbl); 2785 err_no_hashtbl: 2786 kfree(clp->cl_name.data); 2787 err_no_name: 2788 kmem_cache_free(client_slab, clp); 2789 return NULL; 2790 } 2791 2792 static void __free_client(struct kref *k) 2793 { 2794 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref); 2795 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs); 2796 2797 free_svc_cred(&clp->cl_cred); 2798 destroy_workqueue(clp->cl_callback_wq); 2799 kfree(clp->cl_ownerstr_hashtbl); 2800 kfree(clp->cl_name.data); 2801 kfree(clp->cl_nii_domain.data); 2802 kfree(clp->cl_nii_name.data); 2803 idr_destroy(&clp->cl_stateids); 2804 kfree(clp->cl_ra); 2805 kmem_cache_free(client_slab, clp); 2806 } 2807 2808 /** 2809 * nfsd4_put_client - release a reference on an nfs4_client 2810 * @clp: the client to be released 2811 * 2812 * When the last reference is released, the client is freed. 2813 */ 2814 void nfsd4_put_client(struct nfs4_client *clp) 2815 { 2816 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client); 2817 } 2818 2819 static void 2820 free_client(struct nfs4_client *clp) 2821 { 2822 LIST_HEAD(reaplist); 2823 2824 /* client_info_show() walks cl_sessions under cl_lock */ 2825 spin_lock(&clp->cl_lock); 2826 list_splice_init(&clp->cl_sessions, &reaplist); 2827 spin_unlock(&clp->cl_lock); 2828 while (!list_empty(&reaplist)) { 2829 struct nfsd4_session *ses; 2830 ses = list_entry(reaplist.next, struct nfsd4_session, 2831 se_perclnt); 2832 list_del(&ses->se_perclnt); 2833 WARN_ON_ONCE(atomic_read(&ses->se_ref)); 2834 free_session(ses); 2835 } 2836 rpc_destroy_wait_queue(&clp->cl_cb_waitq); 2837 if (clp->cl_nfsd_dentry) { 2838 nfsd_client_rmdir(clp->cl_nfsd_dentry); 2839 clp->cl_nfsd_dentry = NULL; 2840 wake_up_all(&expiry_wq); 2841 } 2842 nfsd4_put_client(clp); 2843 } 2844 2845 /* must be called under the client_lock */ 2846 static void 2847 unhash_client_locked(struct nfs4_client *clp) 2848 { 2849 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2850 struct nfsd4_session *ses; 2851 2852 lockdep_assert_held(&nn->client_lock); 2853 2854 /* Mark the client as expired! */ 2855 clp->cl_time = 0; 2856 /* Make it invisible */ 2857 if (!list_empty(&clp->cl_idhash)) { 2858 list_del_init(&clp->cl_idhash); 2859 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) 2860 rb_erase(&clp->cl_namenode, &nn->conf_name_tree); 2861 else 2862 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); 2863 } 2864 list_del_init(&clp->cl_lru); 2865 spin_lock(&clp->cl_lock); 2866 spin_lock(&nfsd_session_list_lock); 2867 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) { 2868 list_del_init(&ses->se_hash); 2869 /* 2870 * unhash_client_locked() can run more than once for a 2871 * client; the session stays on cl_sessions across calls. 2872 * The first pass empties se_all_sessions via 2873 * list_del_init(), so skip the decrement on later passes 2874 * to keep nfsd_total_sessions from being double-counted. 2875 */ 2876 if (!list_empty(&ses->se_all_sessions)) { 2877 list_del_init(&ses->se_all_sessions); 2878 atomic_dec(&nfsd_total_sessions); 2879 } 2880 } 2881 spin_unlock(&nfsd_session_list_lock); 2882 spin_unlock(&clp->cl_lock); 2883 } 2884 2885 static void 2886 unhash_client(struct nfs4_client *clp) 2887 { 2888 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2889 2890 spin_lock(&nn->client_lock); 2891 unhash_client_locked(clp); 2892 spin_unlock(&nn->client_lock); 2893 } 2894 2895 static __be32 mark_client_expired_locked(struct nfs4_client *clp) 2896 { 2897 int users = atomic_read(&clp->cl_rpc_users); 2898 2899 trace_nfsd_mark_client_expired(clp, users); 2900 2901 if (users) 2902 return nfserr_jukebox; 2903 unhash_client_locked(clp); 2904 return nfs_ok; 2905 } 2906 2907 static void 2908 __destroy_client(struct nfs4_client *clp) 2909 { 2910 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2911 int i; 2912 struct nfs4_openowner *oo; 2913 struct nfs4_delegation *dp; 2914 LIST_HEAD(reaplist); 2915 2916 spin_lock(&nn->deleg_lock); 2917 while (!list_empty(&clp->cl_delegations)) { 2918 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); 2919 unhash_delegation_locked(dp, SC_STATUS_CLOSED); 2920 list_add(&dp->dl_recall_lru, &reaplist); 2921 } 2922 spin_unlock(&nn->deleg_lock); 2923 while (!list_empty(&reaplist)) { 2924 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); 2925 list_del_init(&dp->dl_recall_lru); 2926 destroy_unhashed_deleg(dp); 2927 } 2928 /* 2929 * A CB_RECALL reply can release revoked delegations concurrently: 2930 * nfsd4_shutdown_callback() has not run yet. 2931 */ 2932 spin_lock(&clp->cl_lock); 2933 while (!list_empty(&clp->cl_revoked)) { 2934 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru); 2935 /* this function drops ->cl_lock */ 2936 nfsd4_drop_revoked_stid(&dp->dl_stid); 2937 spin_lock(&clp->cl_lock); 2938 } 2939 spin_unlock(&clp->cl_lock); 2940 while (!list_empty(&clp->cl_openowners)) { 2941 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); 2942 nfs4_get_stateowner(&oo->oo_owner); 2943 release_openowner(oo); 2944 } 2945 for (i = 0; i < OWNER_HASH_SIZE; i++) { 2946 struct nfs4_stateowner *so; 2947 2948 spin_lock(&clp->cl_lock); 2949 while (!list_empty(&clp->cl_ownerstr_hashtbl[i])) { 2950 so = list_first_entry(&clp->cl_ownerstr_hashtbl[i], 2951 struct nfs4_stateowner, so_strhash); 2952 /* Should be no openowners at this point */ 2953 WARN_ON_ONCE(so->so_is_open_owner); 2954 nfs4_get_stateowner(so); 2955 unhash_lockowner_locked(lockowner(so)); 2956 spin_unlock(&clp->cl_lock); 2957 2958 remove_blocked_locks(lockowner(so)); 2959 nfs4_put_stateowner(so); 2960 2961 spin_lock(&clp->cl_lock); 2962 } 2963 spin_unlock(&clp->cl_lock); 2964 } 2965 nfsd4_return_all_client_layouts(clp); 2966 nfsd4_shutdown_copy(clp); 2967 nfsd4_shutdown_callback(clp); 2968 if (clp->cl_cb_conn.cb_xprt) 2969 svc_xprt_put(clp->cl_cb_conn.cb_xprt); 2970 atomic_add_unless(&nn->nfs4_client_count, -1, 0); 2971 nfsd4_dec_courtesy_client_count(nn, clp); 2972 #ifdef CONFIG_NFSD_SCSILAYOUT 2973 xa_destroy(&clp->cl_dev_fences); 2974 #endif 2975 free_client(clp); 2976 wake_up_all(&expiry_wq); 2977 } 2978 2979 static void 2980 destroy_client(struct nfs4_client *clp) 2981 { 2982 unhash_client(clp); 2983 __destroy_client(clp); 2984 } 2985 2986 static void inc_reclaim_complete(struct nfs4_client *clp) 2987 { 2988 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2989 2990 if (!test_bit(NFSD_NET_TRACK_RECLAIM_COMPLETES, &nn->flags)) 2991 return; 2992 2993 down_read(&nn->reclaim_str_hashtbl_lock); 2994 if (!nfsd4_find_reclaim_client(clp->cl_name, nn)) { 2995 up_read(&nn->reclaim_str_hashtbl_lock); 2996 return; 2997 } 2998 if (atomic_inc_return(&nn->nr_reclaim_complete) == 2999 nn->reclaim_str_hashtbl_size) { 3000 up_read(&nn->reclaim_str_hashtbl_lock); 3001 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n", 3002 clp->net->ns.inum); 3003 nfsd4_end_grace(nn); 3004 return; 3005 } 3006 up_read(&nn->reclaim_str_hashtbl_lock); 3007 } 3008 3009 static void expire_client(struct nfs4_client *clp) 3010 { 3011 unhash_client(clp); 3012 nfsd4_client_record_remove(clp); 3013 __destroy_client(clp); 3014 } 3015 3016 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) 3017 { 3018 memcpy(target->cl_verifier.data, source->data, 3019 sizeof(target->cl_verifier.data)); 3020 } 3021 3022 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) 3023 { 3024 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; 3025 target->cl_clientid.cl_id = source->cl_clientid.cl_id; 3026 } 3027 3028 static int copy_cred(struct svc_cred *target, struct svc_cred *source) 3029 { 3030 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL); 3031 target->cr_raw_principal = kstrdup(source->cr_raw_principal, 3032 GFP_KERNEL); 3033 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL); 3034 if ((source->cr_principal && !target->cr_principal) || 3035 (source->cr_raw_principal && !target->cr_raw_principal) || 3036 (source->cr_targ_princ && !target->cr_targ_princ)) 3037 return -ENOMEM; 3038 3039 target->cr_flavor = source->cr_flavor; 3040 target->cr_uid = source->cr_uid; 3041 target->cr_gid = source->cr_gid; 3042 target->cr_group_info = source->cr_group_info; 3043 get_group_info(target->cr_group_info); 3044 target->cr_gss_mech = source->cr_gss_mech; 3045 if (source->cr_gss_mech) 3046 gss_mech_get(source->cr_gss_mech); 3047 return 0; 3048 } 3049 3050 static int 3051 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) 3052 { 3053 if (o1->len < o2->len) 3054 return -1; 3055 if (o1->len > o2->len) 3056 return 1; 3057 return memcmp(o1->data, o2->data, o1->len); 3058 } 3059 3060 static int 3061 same_verf(nfs4_verifier *v1, nfs4_verifier *v2) 3062 { 3063 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); 3064 } 3065 3066 static int 3067 same_clid(clientid_t *cl1, clientid_t *cl2) 3068 { 3069 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id); 3070 } 3071 3072 static bool groups_equal(struct group_info *g1, struct group_info *g2) 3073 { 3074 int i; 3075 3076 if (g1->ngroups != g2->ngroups) 3077 return false; 3078 for (i=0; i<g1->ngroups; i++) 3079 if (!gid_eq(g1->gid[i], g2->gid[i])) 3080 return false; 3081 return true; 3082 } 3083 3084 /* 3085 * RFC 3530 language requires clid_inuse be returned when the 3086 * "principal" associated with a requests differs from that previously 3087 * used. We use uid, gid's, and gss principal string as our best 3088 * approximation. We also don't want to allow non-gss use of a client 3089 * established using gss: in theory cr_principal should catch that 3090 * change, but in practice cr_principal can be null even in the gss case 3091 * since gssd doesn't always pass down a principal string. 3092 */ 3093 static bool is_gss_cred(struct svc_cred *cr) 3094 { 3095 /* Is cr_flavor one of the gss "pseudoflavors"?: */ 3096 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); 3097 } 3098 3099 3100 static bool 3101 same_creds(struct svc_cred *cr1, struct svc_cred *cr2) 3102 { 3103 if ((is_gss_cred(cr1) != is_gss_cred(cr2)) 3104 || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) 3105 || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) 3106 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) 3107 return false; 3108 /* XXX: check that cr_targ_princ fields match ? */ 3109 if (cr1->cr_principal == cr2->cr_principal) 3110 return true; 3111 if (!cr1->cr_principal || !cr2->cr_principal) 3112 return false; 3113 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); 3114 } 3115 3116 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) 3117 { 3118 struct svc_cred *cr = &rqstp->rq_cred; 3119 u32 service; 3120 3121 if (!cr->cr_gss_mech) 3122 return false; 3123 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); 3124 return service == RPC_GSS_SVC_INTEGRITY || 3125 service == RPC_GSS_SVC_PRIVACY; 3126 } 3127 3128 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) 3129 { 3130 struct svc_cred *cr = &rqstp->rq_cred; 3131 3132 if (!cl->cl_mach_cred) 3133 return true; 3134 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) 3135 return false; 3136 if (!svc_rqst_integrity_protected(rqstp)) 3137 return false; 3138 if (cl->cl_cred.cr_raw_principal) 3139 return 0 == strcmp(cl->cl_cred.cr_raw_principal, 3140 cr->cr_raw_principal); 3141 if (!cr->cr_principal) 3142 return false; 3143 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); 3144 } 3145 3146 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn) 3147 { 3148 __be32 verf[2]; 3149 3150 /* 3151 * This is opaque to client, so no need to byte-swap. Use 3152 * __force to keep sparse happy 3153 */ 3154 verf[0] = (__force __be32)(u32)ktime_get_real_seconds(); 3155 verf[1] = (__force __be32)nn->clverifier_counter++; 3156 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); 3157 } 3158 3159 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) 3160 { 3161 clp->cl_clientid.cl_boot = (u32)nn->boot_time; 3162 clp->cl_clientid.cl_id = nn->clientid_counter++; 3163 gen_confirm(clp, nn); 3164 } 3165 3166 static struct nfs4_stid * 3167 find_stateid_locked(struct nfs4_client *cl, stateid_t *t) 3168 { 3169 struct nfs4_stid *ret; 3170 3171 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); 3172 if (!ret || !ret->sc_type) 3173 return NULL; 3174 /* 3175 * Copy offload stateids live in cl_stateids only for id allocation and 3176 * refcounting; per RFC 7862 they are not valid targets for generic 3177 * stateid ops (FREE_STATEID, TEST_STATEID, I/O). Hide them so those 3178 * paths return NFS4ERR_BAD_STATEID. 3179 */ 3180 if (ret->sc_type == SC_TYPE_COPY) 3181 return NULL; 3182 return ret; 3183 } 3184 3185 static struct nfs4_stid * 3186 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, 3187 unsigned short typemask, unsigned short ok_states) 3188 { 3189 struct nfs4_stid *s; 3190 3191 spin_lock(&cl->cl_lock); 3192 s = find_stateid_locked(cl, t); 3193 if (s != NULL) { 3194 if ((s->sc_status & ~ok_states) == 0 && 3195 (typemask & s->sc_type)) 3196 refcount_inc(&s->sc_count); 3197 else 3198 s = NULL; 3199 } 3200 spin_unlock(&cl->cl_lock); 3201 return s; 3202 } 3203 3204 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode) 3205 { 3206 struct nfsdfs_client *nc; 3207 nc = get_nfsdfs_client(inode); 3208 if (!nc) 3209 return NULL; 3210 return container_of(nc, struct nfs4_client, cl_nfsdfs); 3211 } 3212 3213 static void seq_quote_mem(struct seq_file *m, char *data, int len) 3214 { 3215 seq_puts(m, "\""); 3216 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\"); 3217 seq_puts(m, "\""); 3218 } 3219 3220 static const char *cb_state2str(int state) 3221 { 3222 switch (state) { 3223 case NFSD4_CB_UP: 3224 return "UP"; 3225 case NFSD4_CB_UNKNOWN: 3226 return "UNKNOWN"; 3227 case NFSD4_CB_DOWN: 3228 return "DOWN"; 3229 case NFSD4_CB_FAULT: 3230 return "FAULT"; 3231 } 3232 return "UNDEFINED"; 3233 } 3234 3235 static int client_info_show(struct seq_file *m, void *v) 3236 { 3237 struct inode *inode = file_inode(m->file); 3238 struct nfsd4_session *ses; 3239 struct nfs4_client *clp; 3240 u64 clid; 3241 3242 clp = get_nfsdfs_clp(inode); 3243 if (!clp) 3244 return -ENXIO; 3245 memcpy(&clid, &clp->cl_clientid, sizeof(clid)); 3246 seq_printf(m, "clientid: 0x%llx\n", clid); 3247 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr); 3248 3249 if (clp->cl_state == NFSD4_COURTESY) 3250 seq_puts(m, "status: courtesy\n"); 3251 else if (clp->cl_state == NFSD4_EXPIRABLE) 3252 seq_puts(m, "status: expirable\n"); 3253 else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) 3254 seq_puts(m, "status: confirmed\n"); 3255 else 3256 seq_puts(m, "status: unconfirmed\n"); 3257 seq_printf(m, "seconds from last renew: %lld\n", 3258 ktime_get_boottime_seconds() - clp->cl_time); 3259 seq_puts(m, "name: "); 3260 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len); 3261 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion); 3262 if (clp->cl_nii_domain.data) { 3263 seq_puts(m, "Implementation domain: "); 3264 seq_quote_mem(m, clp->cl_nii_domain.data, 3265 clp->cl_nii_domain.len); 3266 seq_puts(m, "\nImplementation name: "); 3267 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len); 3268 seq_printf(m, "\nImplementation time: [%lld, %ld]\n", 3269 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec); 3270 } 3271 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state)); 3272 seq_printf(m, "callback address: \"%pISpc\"\n", &clp->cl_cb_conn.cb_addr); 3273 seq_printf(m, "admin-revoked states: %d\n", 3274 atomic_read(&clp->cl_admin_revoked)); 3275 spin_lock(&clp->cl_lock); 3276 seq_printf(m, "session slots:"); 3277 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) 3278 seq_printf(m, " %u", ses->se_fchannel.maxreqs); 3279 seq_printf(m, "\nsession target slots:"); 3280 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) 3281 seq_printf(m, " %u", ses->se_target_maxslots); 3282 spin_unlock(&clp->cl_lock); 3283 seq_puts(m, "\n"); 3284 3285 nfsd4_put_client(clp); 3286 3287 return 0; 3288 } 3289 3290 DEFINE_SHOW_ATTRIBUTE(client_info); 3291 3292 static void *states_start(struct seq_file *s, loff_t *pos) 3293 __acquires(&clp->cl_lock) 3294 { 3295 struct nfs4_client *clp = s->private; 3296 unsigned long id = *pos; 3297 void *ret; 3298 3299 spin_lock(&clp->cl_lock); 3300 ret = idr_get_next_ul(&clp->cl_stateids, &id); 3301 *pos = id; 3302 return ret; 3303 } 3304 3305 static void *states_next(struct seq_file *s, void *v, loff_t *pos) 3306 { 3307 struct nfs4_client *clp = s->private; 3308 unsigned long id = *pos; 3309 void *ret; 3310 3311 id = *pos; 3312 id++; 3313 ret = idr_get_next_ul(&clp->cl_stateids, &id); 3314 *pos = id; 3315 return ret; 3316 } 3317 3318 static void states_stop(struct seq_file *s, void *v) 3319 __releases(&clp->cl_lock) 3320 { 3321 struct nfs4_client *clp = s->private; 3322 3323 spin_unlock(&clp->cl_lock); 3324 } 3325 3326 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f) 3327 { 3328 seq_printf(s, "filename: \"%pD2\"", f->nf_file); 3329 } 3330 3331 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f) 3332 { 3333 struct inode *inode = file_inode(f->nf_file); 3334 3335 seq_printf(s, "superblock: \"%02x:%02x:%llu\"", 3336 MAJOR(inode->i_sb->s_dev), 3337 MINOR(inode->i_sb->s_dev), 3338 inode->i_ino); 3339 } 3340 3341 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo) 3342 { 3343 seq_puts(s, "owner: "); 3344 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len); 3345 } 3346 3347 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid) 3348 { 3349 seq_printf(s, "0x%.8x", stid->si_generation); 3350 seq_printf(s, "%12phN", &stid->si_opaque); 3351 } 3352 3353 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st) 3354 { 3355 struct nfs4_ol_stateid *ols; 3356 struct nfs4_file *nf; 3357 struct nfsd_file *file; 3358 struct nfs4_stateowner *oo; 3359 unsigned int access, deny; 3360 3361 ols = openlockstateid(st); 3362 oo = ols->st_stateowner; 3363 nf = st->sc_file; 3364 3365 seq_puts(s, "- "); 3366 nfs4_show_stateid(s, &st->sc_stateid); 3367 seq_puts(s, ": { type: open, "); 3368 3369 access = bmap_to_share_mode(ols->st_access_bmap); 3370 deny = bmap_to_share_mode(ols->st_deny_bmap); 3371 3372 seq_printf(s, "access: %s%s, ", 3373 access & NFS4_SHARE_ACCESS_READ ? "r" : "-", 3374 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); 3375 seq_printf(s, "deny: %s%s, ", 3376 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-", 3377 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); 3378 3379 if (nf) { 3380 spin_lock(&nf->fi_lock); 3381 file = find_any_file_locked(nf); 3382 if (file) { 3383 nfs4_show_superblock(s, file); 3384 seq_puts(s, ", "); 3385 nfs4_show_fname(s, file); 3386 seq_puts(s, ", "); 3387 } 3388 spin_unlock(&nf->fi_lock); 3389 } else 3390 seq_puts(s, "closed, "); 3391 nfs4_show_owner(s, oo); 3392 if (st->sc_status & SC_STATUS_ADMIN_REVOKED) 3393 seq_puts(s, ", admin-revoked"); 3394 seq_puts(s, " }\n"); 3395 return 0; 3396 } 3397 3398 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st) 3399 { 3400 struct nfs4_ol_stateid *ols; 3401 struct nfs4_file *nf; 3402 struct nfsd_file *file; 3403 struct nfs4_stateowner *oo; 3404 3405 ols = openlockstateid(st); 3406 oo = ols->st_stateowner; 3407 nf = st->sc_file; 3408 3409 seq_puts(s, "- "); 3410 nfs4_show_stateid(s, &st->sc_stateid); 3411 seq_puts(s, ": { type: lock, "); 3412 3413 spin_lock(&nf->fi_lock); 3414 file = find_any_file_locked(nf); 3415 if (file) { 3416 /* 3417 * Note: a lock stateid isn't really the same thing as a lock, 3418 * it's the locking state held by one owner on a file, and there 3419 * may be multiple (or no) lock ranges associated with it. 3420 * (Same for the matter is true of open stateids.) 3421 */ 3422 3423 nfs4_show_superblock(s, file); 3424 /* XXX: open stateid? */ 3425 seq_puts(s, ", "); 3426 nfs4_show_fname(s, file); 3427 seq_puts(s, ", "); 3428 } 3429 nfs4_show_owner(s, oo); 3430 if (st->sc_status & SC_STATUS_ADMIN_REVOKED) 3431 seq_puts(s, ", admin-revoked"); 3432 seq_puts(s, " }\n"); 3433 spin_unlock(&nf->fi_lock); 3434 return 0; 3435 } 3436 3437 static char *nfs4_show_deleg_type(u32 dl_type) 3438 { 3439 switch (dl_type) { 3440 case OPEN_DELEGATE_READ: 3441 return "r"; 3442 case OPEN_DELEGATE_WRITE: 3443 return "w"; 3444 case OPEN_DELEGATE_READ_ATTRS_DELEG: 3445 return "ra"; 3446 case OPEN_DELEGATE_WRITE_ATTRS_DELEG: 3447 return "wa"; 3448 } 3449 return "?"; 3450 } 3451 3452 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st) 3453 { 3454 struct nfs4_delegation *ds; 3455 struct nfs4_file *nf; 3456 struct nfsd_file *file; 3457 3458 ds = delegstateid(st); 3459 nf = st->sc_file; 3460 3461 seq_puts(s, "- "); 3462 nfs4_show_stateid(s, &st->sc_stateid); 3463 seq_puts(s, ": { type: deleg, "); 3464 3465 seq_printf(s, "access: %s", nfs4_show_deleg_type(ds->dl_type)); 3466 3467 /* XXX: lease time, whether it's being recalled. */ 3468 3469 spin_lock(&nf->fi_lock); 3470 file = rcu_dereference_protected(nf->fi_deleg_file, 3471 lockdep_is_held(&nf->fi_lock)); 3472 if (file) { 3473 seq_puts(s, ", "); 3474 nfs4_show_superblock(s, file); 3475 seq_puts(s, ", "); 3476 nfs4_show_fname(s, file); 3477 } 3478 spin_unlock(&nf->fi_lock); 3479 if (st->sc_status & SC_STATUS_ADMIN_REVOKED) 3480 seq_puts(s, ", admin-revoked"); 3481 seq_puts(s, " }\n"); 3482 return 0; 3483 } 3484 3485 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st) 3486 { 3487 struct nfs4_layout_stateid *ls; 3488 struct nfsd_file *file; 3489 3490 ls = container_of(st, struct nfs4_layout_stateid, ls_stid); 3491 3492 seq_puts(s, "- "); 3493 nfs4_show_stateid(s, &st->sc_stateid); 3494 seq_puts(s, ": { type: layout"); 3495 3496 /* XXX: What else would be useful? */ 3497 3498 spin_lock(&ls->ls_stid.sc_file->fi_lock); 3499 file = ls->ls_file; 3500 if (file) { 3501 seq_puts(s, ", "); 3502 nfs4_show_superblock(s, file); 3503 seq_puts(s, ", "); 3504 nfs4_show_fname(s, file); 3505 } 3506 spin_unlock(&ls->ls_stid.sc_file->fi_lock); 3507 if (st->sc_status & SC_STATUS_ADMIN_REVOKED) 3508 seq_puts(s, ", admin-revoked"); 3509 seq_puts(s, " }\n"); 3510 3511 return 0; 3512 } 3513 3514 static int states_show(struct seq_file *s, void *v) 3515 { 3516 struct nfs4_stid *st = v; 3517 3518 switch (st->sc_type) { 3519 case SC_TYPE_OPEN: 3520 return nfs4_show_open(s, st); 3521 case SC_TYPE_LOCK: 3522 return nfs4_show_lock(s, st); 3523 case SC_TYPE_DELEG: 3524 return nfs4_show_deleg(s, st); 3525 case SC_TYPE_LAYOUT: 3526 return nfs4_show_layout(s, st); 3527 default: 3528 return 0; /* XXX: or SEQ_SKIP? */ 3529 } 3530 /* XXX: copy stateids? */ 3531 } 3532 3533 static struct seq_operations states_seq_ops = { 3534 .start = states_start, 3535 .next = states_next, 3536 .stop = states_stop, 3537 .show = states_show 3538 }; 3539 3540 static int client_states_open(struct inode *inode, struct file *file) 3541 { 3542 struct seq_file *s; 3543 struct nfs4_client *clp; 3544 int ret; 3545 3546 clp = get_nfsdfs_clp(inode); 3547 if (!clp) 3548 return -ENXIO; 3549 3550 ret = seq_open(file, &states_seq_ops); 3551 if (ret) { 3552 nfsd4_put_client(clp); 3553 return ret; 3554 } 3555 s = file->private_data; 3556 s->private = clp; 3557 return 0; 3558 } 3559 3560 static int client_opens_release(struct inode *inode, struct file *file) 3561 { 3562 struct seq_file *m = file->private_data; 3563 struct nfs4_client *clp = m->private; 3564 3565 /* XXX: alternatively, we could get/drop in seq start/stop */ 3566 nfsd4_put_client(clp); 3567 return seq_release(inode, file); 3568 } 3569 3570 static const struct file_operations client_states_fops = { 3571 .open = client_states_open, 3572 .read = seq_read, 3573 .llseek = seq_lseek, 3574 .release = client_opens_release, 3575 }; 3576 3577 /* 3578 * Normally we refuse to destroy clients that are in use, but here the 3579 * administrator is telling us to just do it. We also want to wait 3580 * so the caller has a guarantee that the client's locks are gone by 3581 * the time the write returns: 3582 */ 3583 static void force_expire_client(struct nfs4_client *clp) 3584 { 3585 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 3586 bool already_expired; 3587 3588 trace_nfsd_clid_admin_expired(&clp->cl_clientid); 3589 3590 /* 3591 * cl_time is cleared under client_lock before the wait so a 3592 * revocation walk pinning cl_rpc_users under it either skips 3593 * this client or is seen by this wait_event(). 3594 */ 3595 spin_lock(&nn->client_lock); 3596 clp->cl_time = 0; 3597 spin_unlock(&nn->client_lock); 3598 3599 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0); 3600 spin_lock(&nn->client_lock); 3601 already_expired = list_empty(&clp->cl_lru); 3602 if (!already_expired) 3603 unhash_client_locked(clp); 3604 spin_unlock(&nn->client_lock); 3605 3606 if (!already_expired) 3607 expire_client(clp); 3608 else 3609 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL); 3610 } 3611 3612 static ssize_t client_ctl_write(struct file *file, const char __user *buf, 3613 size_t size, loff_t *pos) 3614 { 3615 char *data; 3616 struct nfs4_client *clp; 3617 3618 data = simple_transaction_get(file, buf, size); 3619 if (IS_ERR(data)) 3620 return PTR_ERR(data); 3621 if (size != 7 || 0 != memcmp(data, "expire\n", 7)) 3622 return -EINVAL; 3623 clp = get_nfsdfs_clp(file_inode(file)); 3624 if (!clp) 3625 return -ENXIO; 3626 force_expire_client(clp); 3627 nfsd4_put_client(clp); 3628 return 7; 3629 } 3630 3631 static const struct file_operations client_ctl_fops = { 3632 .write = client_ctl_write, 3633 .release = simple_transaction_release, 3634 }; 3635 3636 static const struct tree_descr client_files[] = { 3637 [0] = {"info", &client_info_fops, S_IRUSR}, 3638 [1] = {"states", &client_states_fops, S_IRUSR}, 3639 [2] = {"ctl", &client_ctl_fops, S_IWUSR}, 3640 [3] = {""}, 3641 }; 3642 3643 static int 3644 nfsd4_cb_recall_any_done(struct nfsd4_callback *cb, 3645 struct rpc_task *task) 3646 { 3647 trace_nfsd_cb_recall_any_done(cb, task); 3648 switch (task->tk_status) { 3649 case -NFS4ERR_DELAY: 3650 rpc_delay(task, 2 * HZ); 3651 return 0; 3652 default: 3653 return 1; 3654 } 3655 } 3656 3657 static void 3658 nfsd4_cb_recall_any_release(struct nfsd4_callback *cb) 3659 { 3660 struct nfs4_client *clp = cb->cb_clp; 3661 3662 nfsd4_put_client(clp); 3663 } 3664 3665 static int 3666 nfsd4_cb_getattr_done(struct nfsd4_callback *cb, struct rpc_task *task) 3667 { 3668 struct nfs4_cb_fattr *ncf = 3669 container_of(cb, struct nfs4_cb_fattr, ncf_getattr); 3670 struct nfs4_delegation *dp = 3671 container_of(ncf, struct nfs4_delegation, dl_cb_fattr); 3672 3673 trace_nfsd_cb_getattr_done(&dp->dl_stid.sc_stateid, task); 3674 ncf->ncf_cb_status = task->tk_status; 3675 switch (task->tk_status) { 3676 case -NFS4ERR_DELAY: 3677 rpc_delay(task, 2 * HZ); 3678 return 0; 3679 default: 3680 return 1; 3681 } 3682 } 3683 3684 static void 3685 nfsd4_cb_getattr_release(struct nfsd4_callback *cb) 3686 { 3687 struct nfs4_cb_fattr *ncf = 3688 container_of(cb, struct nfs4_cb_fattr, ncf_getattr); 3689 struct nfs4_delegation *dp = 3690 container_of(ncf, struct nfs4_delegation, dl_cb_fattr); 3691 3692 nfs4_put_stid(&dp->dl_stid); 3693 } 3694 3695 static void nfsd_break_one_deleg(struct nfs4_delegation *dp) 3696 { 3697 bool queued; 3698 3699 if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &dp->dl_recall.cb_flags)) 3700 return; 3701 3702 /* 3703 * When called from the lease break (nfsd_break_deleg_cb()) the state 3704 * code is serialized by the flc_lock and the lease has not been 3705 * removed yet, so sc_count is known to be nonzero. The CB_NOTIFY 3706 * callback paths reach here from a workqueue without the flc_lock, 3707 * where the delegation may already be unhashed with sc_count at zero. 3708 * Use refcount_inc_not_zero() so both cases are safe, and bail if the 3709 * delegation is already being torn down. 3710 */ 3711 if (!refcount_inc_not_zero(&dp->dl_stid.sc_count)) { 3712 clear_bit(NFSD4_CALLBACK_RUNNING, &dp->dl_recall.cb_flags); 3713 return; 3714 } 3715 queued = nfsd4_run_cb(&dp->dl_recall); 3716 WARN_ON_ONCE(!queued); 3717 if (!queued) { 3718 refcount_dec(&dp->dl_stid.sc_count); 3719 clear_bit(NFSD4_CALLBACK_RUNNING, &dp->dl_recall.cb_flags); 3720 } 3721 } 3722 3723 static bool 3724 nfsd4_cb_notify_prepare(struct nfsd4_callback *cb) 3725 { 3726 struct nfsd4_cb_notify *ncn = container_of(cb, struct nfsd4_cb_notify, ncn_cb); 3727 struct nfs4_delegation *dp = container_of(ncn, struct nfs4_delegation, dl_cb_notify); 3728 struct nfsd_notify_event *events[NOTIFY4_EVENT_QUEUE_SIZE]; 3729 struct xdr_buf xdr = { .buflen = PAGE_SIZE * NOTIFY4_PAGE_ARRAY_SIZE, 3730 .pages = ncn->ncn_pages }; 3731 int limit = NOTIFY4_EVENT_QUEUE_SIZE; 3732 struct xdr_stream stream; 3733 struct nfsd_file *nf; 3734 bool error = false; 3735 int count, i; 3736 3737 /* Save a slot for dir attr update if requested */ 3738 if (dp->dl_notify_mask & BIT(NOTIFY4_CHANGE_DIR_ATTRS)) 3739 --limit; 3740 3741 /* Clear any failure recorded by a previous transmit. */ 3742 ncn->ncn_encode_err = false; 3743 3744 xdr_init_encode_pages(&stream, &xdr); 3745 3746 spin_lock(&ncn->ncn_lock); 3747 count = ncn->ncn_evt_cnt; 3748 3749 /* spurious queueing? */ 3750 if (count == 0) { 3751 spin_unlock(&ncn->ncn_lock); 3752 return false; 3753 } 3754 3755 memcpy(events, ncn->ncn_evt, sizeof(*events) * count); 3756 ncn->ncn_evt_cnt = 0; 3757 spin_unlock(&ncn->ncn_lock); 3758 3759 /* 3760 * We can't keep up! Drop the queued events and recall. The queue must 3761 * be drained here: out_recall leaves ncn_evt_cnt at 0, so the release 3762 * op won't see leftover events and requeue this callback forever. 3763 */ 3764 if (count > limit) { 3765 for (i = 0; i < count; ++i) 3766 nfsd_notify_event_put(events[i]); 3767 goto out_recall; 3768 } 3769 3770 rcu_read_lock(); 3771 nf = nfsd_file_get(rcu_dereference(dp->dl_stid.sc_file->fi_deleg_file)); 3772 rcu_read_unlock(); 3773 if (!nf) { 3774 for (i = 0; i < count; ++i) 3775 nfsd_notify_event_put(events[i]); 3776 goto out_recall; 3777 } 3778 3779 for (i = 0; i < count; ++i) { 3780 struct nfsd_notify_event *nne = events[i]; 3781 3782 if (!error) { 3783 u32 *maskp = &ncn->ncn_masks[i]; 3784 u8 *p; 3785 3786 p = nfsd4_encode_notify_event(&stream, nne, dp, nf, maskp); 3787 if (!p) { 3788 pr_notice("Could not generate CB_NOTIFY from fsnotify mask 0x%x\n", 3789 nne->ne_mask); 3790 error = true; 3791 goto put_event; 3792 } 3793 3794 ncn->ncn_nf[i].notify_mask.count = 1; 3795 ncn->ncn_nf[i].notify_mask.element = maskp; 3796 ncn->ncn_nf[i].notify_vals.data = p; 3797 ncn->ncn_nf[i].notify_vals.len = (u8 *)stream.p - p; 3798 } 3799 put_event: 3800 nfsd_notify_event_put(nne); 3801 } 3802 if (!error && (dp->dl_notify_mask & BIT(NOTIFY4_CHANGE_DIR_ATTRS))) { 3803 u32 *maskp = &ncn->ncn_masks[count]; 3804 u8 *p; 3805 3806 p = nfsd4_encode_dir_attr_change(&stream, dp, nf); 3807 3808 if (IS_ERR(p)) { 3809 /* 3810 * The client asked to be told about dir attr changes 3811 * but the change could not be encoded. RFC 8881 3812 * s10.9.4 requires the server to recall the delegation 3813 * rather than drop a requested notification, so fall 3814 * through to recall. A NULL return instead means there 3815 * were no attributes to report, so omit the event in 3816 * that case. 3817 */ 3818 error = true; 3819 } else if (p) { 3820 *maskp = BIT(NOTIFY4_CHANGE_DIR_ATTRS); 3821 ncn->ncn_nf[count].notify_mask.count = 1; 3822 ncn->ncn_nf[count].notify_mask.element = maskp; 3823 ncn->ncn_nf[count].notify_vals.data = p; 3824 ncn->ncn_nf[count].notify_vals.len = (u8 *)stream.p - p; 3825 ++count; 3826 } 3827 } 3828 if (!error) { 3829 ncn->ncn_nf_cnt = count; 3830 nfsd_file_put(nf); 3831 return true; 3832 } 3833 nfsd_file_put(nf); 3834 out_recall: 3835 nfsd_break_one_deleg(dp); 3836 return false; 3837 } 3838 3839 static int 3840 nfsd4_cb_notify_done(struct nfsd4_callback *cb, 3841 struct rpc_task *task) 3842 { 3843 struct nfsd4_cb_notify *ncn = container_of(cb, struct nfsd4_cb_notify, ncn_cb); 3844 struct nfs4_delegation *dp = container_of(ncn, struct nfs4_delegation, dl_cb_notify); 3845 3846 if (dp->dl_stid.sc_status) 3847 return 1; 3848 3849 /* 3850 * The CB_NOTIFY op overflowed the send buffer and was dropped from the 3851 * compound. The notification is lost, so recall the delegation rather 3852 * than leaving the client unaware of the directory change. 3853 */ 3854 if (ncn->ncn_encode_err) { 3855 nfsd_break_one_deleg(dp); 3856 return 1; 3857 } 3858 3859 switch (task->tk_status) { 3860 case -NFS4ERR_DELAY: 3861 rpc_delay(task, 2 * HZ); 3862 return 0; 3863 default: 3864 /* For any other hard error, recall the deleg */ 3865 nfsd_break_one_deleg(dp); 3866 fallthrough; 3867 case 0: 3868 return 1; 3869 } 3870 } 3871 3872 static void nfsd4_run_cb_notify(struct nfsd4_cb_notify *ncn); 3873 3874 static void 3875 nfsd4_cb_notify_release(struct nfsd4_callback *cb) 3876 { 3877 struct nfsd4_cb_notify *ncn = 3878 container_of(cb, struct nfsd4_cb_notify, ncn_cb); 3879 struct nfs4_delegation *dp = 3880 container_of(ncn, struct nfs4_delegation, dl_cb_notify); 3881 3882 /* 3883 * Drain events that arrived while this callback was in flight, but 3884 * don't requeue against a revoked delegation: there's no point in 3885 * notifying a client that no longer holds it, and doing so can pin the 3886 * stid and spin the workqueue. 3887 */ 3888 if (!dp->dl_stid.sc_status && READ_ONCE(ncn->ncn_evt_cnt) > 0) 3889 nfsd4_run_cb_notify(ncn); 3890 nfs4_put_stid(&dp->dl_stid); 3891 } 3892 3893 static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = { 3894 .done = nfsd4_cb_recall_any_done, 3895 .release = nfsd4_cb_recall_any_release, 3896 .opcode = OP_CB_RECALL_ANY, 3897 }; 3898 3899 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops = { 3900 .done = nfsd4_cb_getattr_done, 3901 .release = nfsd4_cb_getattr_release, 3902 .opcode = OP_CB_GETATTR, 3903 }; 3904 3905 static const struct nfsd4_callback_ops nfsd4_cb_notify_ops = { 3906 .prepare = nfsd4_cb_notify_prepare, 3907 .done = nfsd4_cb_notify_done, 3908 .release = nfsd4_cb_notify_release, 3909 .opcode = OP_CB_NOTIFY, 3910 }; 3911 3912 static void nfs4_cb_getattr(struct nfs4_cb_fattr *ncf) 3913 { 3914 struct nfs4_delegation *dp = 3915 container_of(ncf, struct nfs4_delegation, dl_cb_fattr); 3916 3917 if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &ncf->ncf_getattr.cb_flags)) 3918 return; 3919 3920 /* set to proper status when nfsd4_cb_getattr_done runs */ 3921 ncf->ncf_cb_status = NFS4ERR_IO; 3922 3923 /* ensure that wake_bit is done when RUNNING is cleared */ 3924 set_bit(NFSD4_CALLBACK_WAKE, &ncf->ncf_getattr.cb_flags); 3925 3926 refcount_inc(&dp->dl_stid.sc_count); 3927 nfsd4_run_cb(&ncf->ncf_getattr); 3928 } 3929 3930 static struct nfs4_client *create_client(struct xdr_netobj name, 3931 struct svc_rqst *rqstp, nfs4_verifier *verf) 3932 { 3933 struct nfs4_client *clp; 3934 struct sockaddr *sa = svc_addr(rqstp); 3935 int ret; 3936 struct net *net = SVC_NET(rqstp); 3937 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 3938 struct dentry *dentries[ARRAY_SIZE(client_files)]; 3939 3940 clp = alloc_client(name, nn); 3941 if (clp == NULL) 3942 return NULL; 3943 3944 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); 3945 if (ret) { 3946 free_client(clp); 3947 return NULL; 3948 } 3949 gen_clid(clp, nn); 3950 kref_init(&clp->cl_nfsdfs.cl_ref); 3951 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL); 3952 clp->cl_time = ktime_get_boottime_seconds(); 3953 copy_verf(clp, verf); 3954 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage)); 3955 RCU_INIT_POINTER(clp->cl_cb_session, NULL); 3956 clp->net = net; 3957 clp->cl_nfsd_dentry = nfsd_client_mkdir( 3958 nn, &clp->cl_nfsdfs, 3959 clp->cl_clientid.cl_id - nn->clientid_base, 3960 client_files, dentries); 3961 clp->cl_nfsd_info_dentry = dentries[0]; 3962 if (!clp->cl_nfsd_dentry) { 3963 free_client(clp); 3964 return NULL; 3965 } 3966 clp->cl_ra = kzalloc_obj(*clp->cl_ra); 3967 if (!clp->cl_ra) { 3968 free_client(clp); 3969 return NULL; 3970 } 3971 clp->cl_ra_time = 0; 3972 nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops, 3973 NFSPROC4_CLNT_CB_RECALL_ANY); 3974 return clp; 3975 } 3976 3977 static void 3978 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) 3979 { 3980 struct rb_node **new = &(root->rb_node), *parent = NULL; 3981 struct nfs4_client *clp; 3982 3983 while (*new) { 3984 clp = rb_entry(*new, struct nfs4_client, cl_namenode); 3985 parent = *new; 3986 3987 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) 3988 new = &((*new)->rb_left); 3989 else 3990 new = &((*new)->rb_right); 3991 } 3992 3993 rb_link_node(&new_clp->cl_namenode, parent, new); 3994 rb_insert_color(&new_clp->cl_namenode, root); 3995 } 3996 3997 static struct nfs4_client * 3998 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) 3999 { 4000 int cmp; 4001 struct rb_node *node = root->rb_node; 4002 struct nfs4_client *clp; 4003 4004 while (node) { 4005 clp = rb_entry(node, struct nfs4_client, cl_namenode); 4006 cmp = compare_blob(&clp->cl_name, name); 4007 if (cmp > 0) 4008 node = node->rb_left; 4009 else if (cmp < 0) 4010 node = node->rb_right; 4011 else 4012 return clp; 4013 } 4014 return NULL; 4015 } 4016 4017 static void 4018 add_to_unconfirmed(struct nfs4_client *clp) 4019 { 4020 unsigned int idhashval; 4021 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 4022 4023 lockdep_assert_held(&nn->client_lock); 4024 4025 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); 4026 add_clp_to_name_tree(clp, &nn->unconf_name_tree); 4027 idhashval = clientid_hashval(clp->cl_clientid.cl_id); 4028 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); 4029 renew_client_locked(clp); 4030 } 4031 4032 static void 4033 move_to_confirmed(struct nfs4_client *clp) 4034 { 4035 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); 4036 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 4037 4038 lockdep_assert_held(&nn->client_lock); 4039 4040 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); 4041 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); 4042 add_clp_to_name_tree(clp, &nn->conf_name_tree); 4043 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); 4044 trace_nfsd_clid_confirmed(&clp->cl_clientid); 4045 renew_client_locked(clp); 4046 } 4047 4048 static struct nfs4_client * 4049 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions) 4050 { 4051 struct nfs4_client *clp; 4052 unsigned int idhashval = clientid_hashval(clid->cl_id); 4053 4054 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { 4055 if (same_clid(&clp->cl_clientid, clid)) { 4056 if ((bool)clp->cl_minorversion != sessions) 4057 return NULL; 4058 renew_client_locked(clp); 4059 return clp; 4060 } 4061 } 4062 return NULL; 4063 } 4064 4065 static struct nfs4_client * 4066 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) 4067 { 4068 struct list_head *tbl = nn->conf_id_hashtbl; 4069 4070 lockdep_assert_held(&nn->client_lock); 4071 return find_client_in_id_table(tbl, clid, sessions); 4072 } 4073 4074 static struct nfs4_client * 4075 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) 4076 { 4077 struct list_head *tbl = nn->unconf_id_hashtbl; 4078 4079 lockdep_assert_held(&nn->client_lock); 4080 return find_client_in_id_table(tbl, clid, sessions); 4081 } 4082 4083 static bool clp_used_exchangeid(struct nfs4_client *clp) 4084 { 4085 return clp->cl_exchange_flags != 0; 4086 } 4087 4088 static struct nfs4_client * 4089 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) 4090 { 4091 lockdep_assert_held(&nn->client_lock); 4092 return find_clp_in_name_tree(name, &nn->conf_name_tree); 4093 } 4094 4095 static struct nfs4_client * 4096 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) 4097 { 4098 lockdep_assert_held(&nn->client_lock); 4099 return find_clp_in_name_tree(name, &nn->unconf_name_tree); 4100 } 4101 4102 static void 4103 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp) 4104 { 4105 struct nfs4_cb_conn *conn = &clp->cl_cb_conn; 4106 struct sockaddr *sa = svc_addr(rqstp); 4107 u32 scopeid = rpc_get_scope_id(sa); 4108 unsigned short expected_family; 4109 4110 /* Currently, we only support tcp and tcp6 for the callback channel */ 4111 if (se->se_callback_netid_len == 3 && 4112 !memcmp(se->se_callback_netid_val, "tcp", 3)) 4113 expected_family = AF_INET; 4114 else if (se->se_callback_netid_len == 4 && 4115 !memcmp(se->se_callback_netid_val, "tcp6", 4)) 4116 expected_family = AF_INET6; 4117 else 4118 goto out_err; 4119 4120 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, 4121 se->se_callback_addr_len, 4122 (struct sockaddr *)&conn->cb_addr, 4123 sizeof(conn->cb_addr)); 4124 4125 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) 4126 goto out_err; 4127 4128 if (conn->cb_addr.ss_family == AF_INET6) 4129 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; 4130 4131 conn->cb_prog = se->se_callback_prog; 4132 conn->cb_ident = se->se_callback_ident; 4133 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); 4134 trace_nfsd_cb_args(clp, conn); 4135 return; 4136 out_err: 4137 conn->cb_addr.ss_family = AF_UNSPEC; 4138 conn->cb_addrlen = 0; 4139 trace_nfsd_cb_nodelegs(clp); 4140 return; 4141 } 4142 4143 /* 4144 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. 4145 */ 4146 static void 4147 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) 4148 { 4149 struct xdr_buf *buf = resp->xdr->buf; 4150 struct nfsd4_slot *slot = resp->cstate.slot; 4151 unsigned int base; 4152 4153 /* 4154 * RFC 5661 Section 2.10.6.1.2: 4155 * 4156 * Any time SEQUENCE ... returns an error ... [t]he replier MUST NOT 4157 * modify the reply cache entry for the slot whenever an error is 4158 * returned from SEQUENCE ... 4159 * 4160 * Because nfsd4_store_cache_entry is called only by 4161 * nfsd4_sequence_done(), nfsd4_store_cache_entry() is called only 4162 * when a SEQUENCE operation was part of the COMPOUND. 4163 * nfs41_check_op_ordering() ensures SEQUENCE is the first op. 4164 */ 4165 if (resp->opcnt == 1 && resp->cstate.status != nfs_ok) 4166 return; 4167 4168 slot->sl_flags |= NFSD4_SLOT_INITIALIZED; 4169 slot->sl_opcnt = resp->opcnt; 4170 slot->sl_status = resp->cstate.status; 4171 free_svc_cred(&slot->sl_cred); 4172 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred); 4173 4174 if (!(resp->cstate.slot->sl_flags & NFSD4_SLOT_CACHETHIS)) { 4175 slot->sl_flags &= ~NFSD4_SLOT_CACHED; 4176 return; 4177 } 4178 slot->sl_flags |= NFSD4_SLOT_CACHED; 4179 4180 base = resp->cstate.data_offset; 4181 slot->sl_datalen = buf->len - base; 4182 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen)) 4183 WARN(1, "%s: sessions DRC could not cache compound\n", 4184 __func__); 4185 return; 4186 } 4187 4188 /* 4189 * The sequence operation is not cached because we can use the slot and 4190 * session values. 4191 */ 4192 static __be32 4193 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, 4194 struct nfsd4_sequence *seq) 4195 { 4196 struct nfsd4_compoundargs *args = resp->rqstp->rq_argp; 4197 struct nfsd4_slot *slot = resp->cstate.slot; 4198 struct xdr_stream *xdr = resp->xdr; 4199 __be32 *p; 4200 4201 dprintk("--> %s slot %p\n", __func__, slot); 4202 4203 /* Always encode the SEQUENCE response. */ 4204 nfsd4_encode_operation(resp, &args->ops[0]); 4205 if (args->opcnt == 1) 4206 /* A solo SEQUENCE - nothing was cached */ 4207 return args->ops[0].status; 4208 4209 if (!(slot->sl_flags & NFSD4_SLOT_CACHED)) { 4210 /* We weren't asked to cache this. */ 4211 struct nfsd4_op *op; 4212 4213 op = &args->ops[resp->opcnt++]; 4214 op->status = nfserr_retry_uncached_rep; 4215 nfsd4_encode_operation(resp, op); 4216 return op->status; 4217 } 4218 4219 /* return reply from cache */ 4220 p = xdr_reserve_space(xdr, slot->sl_datalen); 4221 if (!p) { 4222 WARN_ON_ONCE(1); 4223 return nfserr_serverfault; 4224 } 4225 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); 4226 xdr_commit_encode(xdr); 4227 4228 resp->opcnt = slot->sl_opcnt; 4229 return slot->sl_status; 4230 } 4231 4232 /* 4233 * Set the exchange_id flags returned by the server. 4234 */ 4235 static void 4236 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) 4237 { 4238 #ifdef CONFIG_NFSD_PNFS 4239 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; 4240 #else 4241 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; 4242 #endif 4243 4244 /* Referrals are supported, Migration is not. */ 4245 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; 4246 4247 /* set the wire flags to return to client. */ 4248 clid->flags = new->cl_exchange_flags; 4249 } 4250 4251 static bool client_has_openowners(struct nfs4_client *clp) 4252 { 4253 struct nfs4_openowner *oo; 4254 bool found = false; 4255 4256 spin_lock(&clp->cl_lock); 4257 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { 4258 if (!list_empty(&oo->oo_owner.so_stateids)) { 4259 found = true; 4260 break; 4261 } 4262 } 4263 spin_unlock(&clp->cl_lock); 4264 4265 return found; 4266 } 4267 4268 static bool client_has_state(struct nfs4_client *clp) 4269 { 4270 return client_has_openowners(clp) 4271 #ifdef CONFIG_NFSD_PNFS 4272 || !list_empty(&clp->cl_lo_states) 4273 #endif 4274 || !list_empty(&clp->cl_delegations) 4275 || !list_empty(&clp->cl_sessions) 4276 || nfsd4_has_active_async_copies(clp); 4277 } 4278 4279 static __be32 copy_impl_id(struct nfs4_client *clp, 4280 struct nfsd4_exchange_id *exid) 4281 { 4282 if (!exid->nii_domain.data) 4283 return 0; 4284 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL); 4285 if (!clp->cl_nii_domain.data) 4286 return nfserr_jukebox; 4287 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL); 4288 if (!clp->cl_nii_name.data) 4289 return nfserr_jukebox; 4290 clp->cl_nii_time = exid->nii_time; 4291 return 0; 4292 } 4293 4294 __be32 4295 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 4296 union nfsd4_op_u *u) 4297 { 4298 struct nfsd4_exchange_id *exid = &u->exchange_id; 4299 struct nfs4_client *conf, *new; 4300 struct nfs4_client *unconf = NULL; 4301 __be32 status; 4302 char addr_str[INET6_ADDRSTRLEN]; 4303 nfs4_verifier verf = exid->verifier; 4304 struct sockaddr *sa = svc_addr(rqstp); 4305 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; 4306 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 4307 4308 rpc_ntop(sa, addr_str, sizeof(addr_str)); 4309 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " 4310 "ip_addr=%s flags %x, spa_how %u\n", 4311 __func__, rqstp, exid, exid->clname.len, exid->clname.data, 4312 addr_str, exid->flags, exid->spa_how); 4313 4314 exid->server_impl_name = kasprintf(GFP_KERNEL, "%s %s %s %s", 4315 utsname()->sysname, utsname()->release, 4316 utsname()->version, utsname()->machine); 4317 if (!exid->server_impl_name) 4318 return nfserr_jukebox; 4319 4320 if (exid->flags & ~EXCHGID4_FLAG_MASK_A) 4321 return nfserr_inval; 4322 4323 new = create_client(exid->clname, rqstp, &verf); 4324 if (new == NULL) 4325 return nfserr_jukebox; 4326 status = copy_impl_id(new, exid); 4327 if (status) 4328 goto out_nolock; 4329 4330 switch (exid->spa_how) { 4331 case SP4_MACH_CRED: 4332 exid->spo_must_enforce[0] = 0; 4333 exid->spo_must_enforce[1] = ( 4334 1 << (OP_BIND_CONN_TO_SESSION - 32) | 4335 1 << (OP_EXCHANGE_ID - 32) | 4336 1 << (OP_CREATE_SESSION - 32) | 4337 1 << (OP_DESTROY_SESSION - 32) | 4338 1 << (OP_DESTROY_CLIENTID - 32)); 4339 4340 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | 4341 1 << (OP_OPEN_DOWNGRADE) | 4342 1 << (OP_LOCKU) | 4343 1 << (OP_DELEGRETURN)); 4344 4345 exid->spo_must_allow[1] &= ( 4346 1 << (OP_TEST_STATEID - 32) | 4347 1 << (OP_FREE_STATEID - 32)); 4348 if (!svc_rqst_integrity_protected(rqstp)) { 4349 status = nfserr_inval; 4350 goto out_nolock; 4351 } 4352 /* 4353 * Sometimes userspace doesn't give us a principal. 4354 * Which is a bug, really. Anyway, we can't enforce 4355 * MACH_CRED in that case, better to give up now: 4356 */ 4357 if (!new->cl_cred.cr_principal && 4358 !new->cl_cred.cr_raw_principal) { 4359 status = nfserr_serverfault; 4360 goto out_nolock; 4361 } 4362 new->cl_mach_cred = true; 4363 break; 4364 case SP4_NONE: 4365 break; 4366 default: /* checked by xdr code */ 4367 WARN_ON_ONCE(1); 4368 fallthrough; 4369 case SP4_SSV: 4370 status = nfserr_encr_alg_unsupp; 4371 goto out_nolock; 4372 } 4373 4374 /* Cases below refer to rfc 5661 section 18.35.4: */ 4375 spin_lock(&nn->client_lock); 4376 conf = find_confirmed_client_by_name(&exid->clname, nn); 4377 if (conf) { 4378 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); 4379 bool verfs_match = same_verf(&verf, &conf->cl_verifier); 4380 4381 if (update) { 4382 if (!clp_used_exchangeid(conf)) { /* buggy client */ 4383 status = nfserr_inval; 4384 goto out; 4385 } 4386 if (!nfsd4_mach_creds_match(conf, rqstp)) { 4387 status = nfserr_wrong_cred; 4388 goto out; 4389 } 4390 if (!creds_match) { /* case 9 */ 4391 status = nfserr_perm; 4392 goto out; 4393 } 4394 if (!verfs_match) { /* case 8 */ 4395 status = nfserr_not_same; 4396 goto out; 4397 } 4398 /* case 6 */ 4399 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; 4400 trace_nfsd_clid_confirmed_r(conf); 4401 goto out_copy; 4402 } 4403 if (!creds_match) { /* case 3 */ 4404 if (client_has_state(conf)) { 4405 status = nfserr_clid_inuse; 4406 trace_nfsd_clid_cred_mismatch(conf, rqstp); 4407 goto out; 4408 } 4409 goto out_new; 4410 } 4411 if (verfs_match) { /* case 2 */ 4412 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; 4413 trace_nfsd_clid_confirmed_r(conf); 4414 goto out_copy; 4415 } 4416 /* case 5, client reboot */ 4417 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf); 4418 conf = NULL; 4419 goto out_new; 4420 } 4421 4422 if (update) { /* case 7 */ 4423 status = nfserr_noent; 4424 goto out; 4425 } 4426 4427 unconf = find_unconfirmed_client_by_name(&exid->clname, nn); 4428 if (unconf) /* case 4, possible retry or client restart */ 4429 unhash_client_locked(unconf); 4430 4431 /* case 1, new owner ID */ 4432 trace_nfsd_clid_fresh(new); 4433 4434 out_new: 4435 if (conf) { 4436 status = mark_client_expired_locked(conf); 4437 if (status) 4438 goto out; 4439 trace_nfsd_clid_replaced(&conf->cl_clientid); 4440 } 4441 new->cl_minorversion = cstate->minorversion; 4442 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; 4443 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1]; 4444 4445 /* Contrived initial CREATE_SESSION response */ 4446 new->cl_cs_slot.sl_status = nfserr_seq_misordered; 4447 4448 add_to_unconfirmed(new); 4449 swap(new, conf); 4450 out_copy: 4451 exid->clientid.cl_boot = conf->cl_clientid.cl_boot; 4452 exid->clientid.cl_id = conf->cl_clientid.cl_id; 4453 4454 exid->seqid = conf->cl_cs_slot.sl_seqid + 1; 4455 nfsd4_set_ex_flags(conf, exid); 4456 4457 exid->nii_domain.len = sizeof("kernel.org") - 1; 4458 exid->nii_domain.data = "kernel.org"; 4459 4460 /* 4461 * Note that RFC 8881 places no length limit on 4462 * nii_name, but this implementation permits no 4463 * more than NFS4_OPAQUE_LIMIT bytes. 4464 */ 4465 exid->nii_name.len = strlen(exid->server_impl_name); 4466 if (exid->nii_name.len > NFS4_OPAQUE_LIMIT) 4467 exid->nii_name.len = NFS4_OPAQUE_LIMIT; 4468 exid->nii_name.data = exid->server_impl_name; 4469 4470 /* just send zeros - the date is in nii_name */ 4471 exid->nii_time.tv_sec = 0; 4472 exid->nii_time.tv_nsec = 0; 4473 4474 dprintk("nfsd4_exchange_id seqid %d flags %x\n", 4475 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); 4476 status = nfs_ok; 4477 4478 out: 4479 spin_unlock(&nn->client_lock); 4480 out_nolock: 4481 if (new) 4482 expire_client(new); 4483 if (unconf) { 4484 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid); 4485 expire_client(unconf); 4486 } 4487 return status; 4488 } 4489 4490 void 4491 nfsd4_exchange_id_release(union nfsd4_op_u *u) 4492 { 4493 struct nfsd4_exchange_id *exid = &u->exchange_id; 4494 4495 kfree(exid->server_impl_name); 4496 } 4497 4498 static __be32 check_slot_seqid(u32 seqid, u32 slot_seqid, u8 flags) 4499 { 4500 /* The slot is in use, and no response has been sent. */ 4501 if (flags & NFSD4_SLOT_INUSE) { 4502 if (seqid == slot_seqid) 4503 return nfserr_jukebox; 4504 else 4505 return nfserr_seq_misordered; 4506 } 4507 /* Note unsigned 32-bit arithmetic handles wraparound: */ 4508 if (likely(seqid == slot_seqid + 1)) 4509 return nfs_ok; 4510 if ((flags & NFSD4_SLOT_REUSED) && seqid == 1) 4511 return nfs_ok; 4512 if (seqid == slot_seqid) 4513 return nfserr_replay_cache; 4514 return nfserr_seq_misordered; 4515 } 4516 4517 /* 4518 * Cache the create session result into the create session single DRC 4519 * slot cache by saving the xdr structure. sl_seqid has been set. 4520 * Do this for solo or embedded create session operations. 4521 */ 4522 static void 4523 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, 4524 struct nfsd4_clid_slot *slot, __be32 nfserr) 4525 { 4526 slot->sl_status = nfserr; 4527 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); 4528 } 4529 4530 static __be32 4531 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, 4532 struct nfsd4_clid_slot *slot) 4533 { 4534 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); 4535 return slot->sl_status; 4536 } 4537 4538 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ 4539 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ 4540 1 + /* MIN tag is length with zero, only length */ \ 4541 3 + /* version, opcount, opcode */ \ 4542 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ 4543 /* seqid, slotID, slotID, cache */ \ 4544 4 ) * sizeof(__be32)) 4545 4546 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ 4547 2 + /* verifier: AUTH_NULL, length 0 */\ 4548 1 + /* status */ \ 4549 1 + /* MIN tag is length with zero, only length */ \ 4550 3 + /* opcount, opcode, opstatus*/ \ 4551 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ 4552 /* seqid, slotID, slotID, slotID, status */ \ 4553 5 ) * sizeof(__be32)) 4554 4555 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) 4556 { 4557 u32 maxrpc = nn->nfsd_serv->sv_max_mesg; 4558 4559 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) 4560 return nfserr_toosmall; 4561 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) 4562 return nfserr_toosmall; 4563 ca->headerpadsz = 0; 4564 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); 4565 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); 4566 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); 4567 ca->maxresp_cached = min_t(u32, ca->maxresp_cached, 4568 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); 4569 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); 4570 4571 return nfs_ok; 4572 } 4573 4574 /* 4575 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. 4576 * These are based on similar macros in linux/sunrpc/msg_prot.h . 4577 */ 4578 #define RPC_MAX_HEADER_WITH_AUTH_SYS \ 4579 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) 4580 4581 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ 4582 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) 4583 4584 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ 4585 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) 4586 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ 4587 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ 4588 sizeof(__be32)) 4589 4590 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) 4591 { 4592 ca->headerpadsz = 0; 4593 4594 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) 4595 return nfserr_toosmall; 4596 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) 4597 return nfserr_toosmall; 4598 ca->maxresp_cached = 0; 4599 if (ca->maxops < 2) 4600 return nfserr_toosmall; 4601 4602 return nfs_ok; 4603 } 4604 4605 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) 4606 { 4607 switch (cbs->flavor) { 4608 case RPC_AUTH_NULL: 4609 case RPC_AUTH_UNIX: 4610 return nfs_ok; 4611 default: 4612 /* 4613 * GSS case: the spec doesn't allow us to return this 4614 * error. But it also doesn't allow us not to support 4615 * GSS. 4616 * I'd rather this fail hard than return some error the 4617 * client might think it can already handle: 4618 */ 4619 return nfserr_encr_alg_unsupp; 4620 } 4621 } 4622 4623 __be32 4624 nfsd4_create_session(struct svc_rqst *rqstp, 4625 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 4626 { 4627 struct nfsd4_create_session *cr_ses = &u->create_session; 4628 struct sockaddr *sa = svc_addr(rqstp); 4629 struct nfs4_client *conf, *unconf; 4630 struct nfsd4_clid_slot *cs_slot; 4631 struct nfs4_client *old = NULL; 4632 struct nfsd4_session *new; 4633 struct nfsd4_conn *conn; 4634 __be32 status = 0; 4635 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 4636 4637 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) 4638 return nfserr_inval; 4639 status = nfsd4_check_cb_sec(&cr_ses->cb_sec); 4640 if (status) 4641 return status; 4642 status = check_forechannel_attrs(&cr_ses->fore_channel, nn); 4643 if (status) 4644 return status; 4645 status = check_backchannel_attrs(&cr_ses->back_channel); 4646 if (status) 4647 goto out_err; 4648 status = nfserr_jukebox; 4649 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); 4650 if (!new) 4651 goto out_err; 4652 conn = alloc_conn_from_crses(rqstp, cr_ses); 4653 if (!conn) 4654 goto out_free_session; 4655 4656 spin_lock(&nn->client_lock); 4657 4658 /* RFC 8881 Section 18.36.4 Phase 1: Client record look-up. */ 4659 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); 4660 conf = find_confirmed_client(&cr_ses->clientid, true, nn); 4661 if (!conf && !unconf) { 4662 status = nfserr_stale_clientid; 4663 goto out_free_conn; 4664 } 4665 4666 /* RFC 8881 Section 18.36.4 Phase 2: Sequence ID processing. */ 4667 if (conf) { 4668 cs_slot = &conf->cl_cs_slot; 4669 trace_nfsd_slot_seqid_conf(conf, cr_ses); 4670 } else { 4671 cs_slot = &unconf->cl_cs_slot; 4672 trace_nfsd_slot_seqid_unconf(unconf, cr_ses); 4673 } 4674 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); 4675 switch (status) { 4676 case nfs_ok: 4677 cs_slot->sl_seqid++; 4678 cr_ses->seqid = cs_slot->sl_seqid; 4679 break; 4680 case nfserr_replay_cache: 4681 status = nfsd4_replay_create_session(cr_ses, cs_slot); 4682 fallthrough; 4683 case nfserr_jukebox: 4684 /* The server MUST NOT cache NFS4ERR_DELAY */ 4685 goto out_free_conn; 4686 default: 4687 goto out_cache_error; 4688 } 4689 4690 /* RFC 8881 Section 18.36.4 Phase 3: Client ID confirmation. */ 4691 if (conf) { 4692 status = nfserr_wrong_cred; 4693 if (!nfsd4_mach_creds_match(conf, rqstp)) 4694 goto out_cache_error; 4695 } else { 4696 status = nfserr_clid_inuse; 4697 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || 4698 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { 4699 trace_nfsd_clid_cred_mismatch(unconf, rqstp); 4700 goto out_cache_error; 4701 } 4702 status = nfserr_wrong_cred; 4703 if (!nfsd4_mach_creds_match(unconf, rqstp)) 4704 goto out_cache_error; 4705 old = find_confirmed_client_by_name(&unconf->cl_name, nn); 4706 if (old) { 4707 status = mark_client_expired_locked(old); 4708 if (status) 4709 goto out_expired_error; 4710 trace_nfsd_clid_replaced(&old->cl_clientid); 4711 } 4712 move_to_confirmed(unconf); 4713 conf = unconf; 4714 } 4715 4716 /* RFC 8881 Section 18.36.4 Phase 4: Session creation. */ 4717 status = nfs_ok; 4718 /* Persistent sessions are not supported */ 4719 cr_ses->flags &= ~SESSION4_PERSIST; 4720 /* Upshifting from TCP to RDMA is not supported */ 4721 cr_ses->flags &= ~SESSION4_RDMA; 4722 /* Report the correct number of backchannel slots */ 4723 cr_ses->back_channel.maxreqs = new->se_cb_highest_slot + 1; 4724 4725 init_session(rqstp, new, conf, cr_ses); 4726 nfsd4_get_session_locked(new); 4727 4728 memcpy(cr_ses->sessionid.data, new->se_sessionid.data, 4729 NFS4_MAX_SESSIONID_LEN); 4730 4731 /* cache solo and embedded create sessions under the client_lock */ 4732 nfsd4_cache_create_session(cr_ses, cs_slot, status); 4733 spin_unlock(&nn->client_lock); 4734 if (conf == unconf) 4735 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY); 4736 /* init connection and backchannel */ 4737 nfsd4_init_conn(rqstp, conn, new); 4738 nfsd4_put_session(new); 4739 if (old) 4740 expire_client(old); 4741 return status; 4742 4743 out_expired_error: 4744 /* 4745 * Revert the slot seq_nr change so the server will process 4746 * the client's resend instead of returning a cached response. 4747 */ 4748 if (status == nfserr_jukebox) { 4749 cs_slot->sl_seqid--; 4750 cr_ses->seqid = cs_slot->sl_seqid; 4751 goto out_free_conn; 4752 } 4753 out_cache_error: 4754 nfsd4_cache_create_session(cr_ses, cs_slot, status); 4755 out_free_conn: 4756 spin_unlock(&nn->client_lock); 4757 free_conn(conn); 4758 out_free_session: 4759 __free_session(new); 4760 out_err: 4761 return status; 4762 } 4763 4764 static __be32 nfsd4_map_bcts_dir(u32 *dir) 4765 { 4766 switch (*dir) { 4767 case NFS4_CDFC4_FORE: 4768 case NFS4_CDFC4_BACK: 4769 return nfs_ok; 4770 case NFS4_CDFC4_FORE_OR_BOTH: 4771 case NFS4_CDFC4_BACK_OR_BOTH: 4772 *dir = NFS4_CDFC4_BOTH; 4773 return nfs_ok; 4774 } 4775 return nfserr_inval; 4776 } 4777 4778 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, 4779 struct nfsd4_compound_state *cstate, 4780 union nfsd4_op_u *u) 4781 { 4782 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl; 4783 struct nfsd4_session *session = cstate->session; 4784 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 4785 __be32 status; 4786 4787 status = nfsd4_check_cb_sec(&bc->bc_cb_sec); 4788 if (status) 4789 return status; 4790 spin_lock(&nn->client_lock); 4791 session->se_cb_prog = bc->bc_cb_program; 4792 session->se_cb_sec = bc->bc_cb_sec; 4793 spin_unlock(&nn->client_lock); 4794 4795 nfsd4_probe_callback(session->se_client); 4796 4797 return nfs_ok; 4798 } 4799 4800 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) 4801 { 4802 struct nfsd4_conn *c; 4803 4804 list_for_each_entry(c, &s->se_conns, cn_persession) { 4805 if (c->cn_xprt == xpt) { 4806 return c; 4807 } 4808 } 4809 return NULL; 4810 } 4811 4812 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst, 4813 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn) 4814 { 4815 struct nfs4_client *clp = session->se_client; 4816 struct svc_xprt *xpt = rqst->rq_xprt; 4817 struct nfsd4_conn *c; 4818 __be32 status; 4819 4820 /* Following the last paragraph of RFC 5661 Section 18.34.3: */ 4821 spin_lock(&clp->cl_lock); 4822 c = __nfsd4_find_conn(xpt, session); 4823 if (!c) 4824 status = nfserr_noent; 4825 else if (req == c->cn_flags) 4826 status = nfs_ok; 4827 else if (req == NFS4_CDFC4_FORE_OR_BOTH && 4828 c->cn_flags != NFS4_CDFC4_BACK) 4829 status = nfs_ok; 4830 else if (req == NFS4_CDFC4_BACK_OR_BOTH && 4831 c->cn_flags != NFS4_CDFC4_FORE) 4832 status = nfs_ok; 4833 else 4834 status = nfserr_inval; 4835 spin_unlock(&clp->cl_lock); 4836 if (status == nfs_ok && conn) 4837 *conn = c; 4838 return status; 4839 } 4840 4841 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, 4842 struct nfsd4_compound_state *cstate, 4843 union nfsd4_op_u *u) 4844 { 4845 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session; 4846 __be32 status; 4847 struct nfsd4_conn *conn; 4848 struct nfsd4_session *session; 4849 struct net *net = SVC_NET(rqstp); 4850 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 4851 4852 if (!nfsd4_last_compound_op(rqstp)) 4853 return nfserr_not_only_op; 4854 spin_lock(&nn->client_lock); 4855 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); 4856 spin_unlock(&nn->client_lock); 4857 if (!session) 4858 goto out_no_session; 4859 status = nfserr_wrong_cred; 4860 if (!nfsd4_mach_creds_match(session->se_client, rqstp)) 4861 goto out; 4862 status = nfsd4_match_existing_connection(rqstp, session, 4863 bcts->dir, &conn); 4864 if (status == nfs_ok) { 4865 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH || 4866 bcts->dir == NFS4_CDFC4_BACK) 4867 conn->cn_flags |= NFS4_CDFC4_BACK; 4868 nfsd4_probe_callback(session->se_client); 4869 goto out; 4870 } 4871 if (status == nfserr_inval) 4872 goto out; 4873 status = nfsd4_map_bcts_dir(&bcts->dir); 4874 if (status) 4875 goto out; 4876 conn = alloc_conn(rqstp, bcts->dir); 4877 status = nfserr_jukebox; 4878 if (!conn) 4879 goto out; 4880 nfsd4_init_conn(rqstp, conn, session); 4881 status = nfs_ok; 4882 out: 4883 nfsd4_put_session(session); 4884 out_no_session: 4885 return status; 4886 } 4887 4888 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid) 4889 { 4890 if (!cstate->session) 4891 return false; 4892 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid)); 4893 } 4894 4895 __be32 4896 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate, 4897 union nfsd4_op_u *u) 4898 { 4899 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid; 4900 struct nfsd4_session *ses; 4901 __be32 status; 4902 int ref_held_by_me = 0; 4903 struct net *net = SVC_NET(r); 4904 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 4905 4906 status = nfserr_not_only_op; 4907 if (nfsd4_compound_in_session(cstate, sessionid)) { 4908 if (!nfsd4_last_compound_op(r)) 4909 goto out; 4910 ref_held_by_me++; 4911 } 4912 dump_sessionid(__func__, sessionid); 4913 spin_lock(&nn->client_lock); 4914 ses = find_in_sessionid_hashtbl(sessionid, net, &status); 4915 if (!ses) 4916 goto out_client_lock; 4917 status = nfserr_wrong_cred; 4918 if (!nfsd4_mach_creds_match(ses->se_client, r)) 4919 goto out_put_session; 4920 status = mark_session_dead_locked(ses, 1 + ref_held_by_me); 4921 if (status) 4922 goto out_put_session; 4923 unhash_session(ses); 4924 spin_unlock(&nn->client_lock); 4925 4926 nfsd4_probe_callback_sync(ses->se_client); 4927 4928 spin_lock(&nn->client_lock); 4929 status = nfs_ok; 4930 out_put_session: 4931 nfsd4_put_session_locked(ses); 4932 out_client_lock: 4933 spin_unlock(&nn->client_lock); 4934 out: 4935 return status; 4936 } 4937 4938 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses) 4939 { 4940 struct nfs4_client *clp = ses->se_client; 4941 struct nfsd4_conn *c; 4942 __be32 status = nfs_ok; 4943 int ret; 4944 4945 spin_lock(&clp->cl_lock); 4946 c = __nfsd4_find_conn(new->cn_xprt, ses); 4947 if (c) 4948 goto out_free; 4949 status = nfserr_conn_not_bound_to_session; 4950 if (clp->cl_mach_cred) 4951 goto out_free; 4952 __nfsd4_hash_conn(new, ses); 4953 spin_unlock(&clp->cl_lock); 4954 ret = nfsd4_register_conn(new); 4955 if (ret) 4956 /* oops; xprt is already down: */ 4957 nfsd4_conn_lost(&new->cn_xpt_user); 4958 return nfs_ok; 4959 out_free: 4960 spin_unlock(&clp->cl_lock); 4961 free_conn(new); 4962 return status; 4963 } 4964 4965 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) 4966 { 4967 struct nfsd4_compoundargs *args = rqstp->rq_argp; 4968 4969 return args->opcnt > session->se_fchannel.maxops; 4970 } 4971 4972 static bool nfsd4_request_too_big(struct svc_rqst *rqstp, 4973 struct nfsd4_session *session) 4974 { 4975 struct xdr_buf *xb = &rqstp->rq_arg; 4976 4977 return xb->len > session->se_fchannel.maxreq_sz; 4978 } 4979 4980 static bool replay_matches_cache(struct svc_rqst *rqstp, 4981 struct nfsd4_sequence *seq, struct nfsd4_slot *slot) 4982 { 4983 struct nfsd4_compoundargs *argp = rqstp->rq_argp; 4984 4985 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) != 4986 (bool)seq->cachethis) 4987 return false; 4988 /* 4989 * If there's an error then the reply can have fewer ops than 4990 * the call. 4991 */ 4992 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status) 4993 return false; 4994 /* 4995 * But if we cached a reply with *more* ops than the call you're 4996 * sending us now, then this new call is clearly not really a 4997 * replay of the old one: 4998 */ 4999 if (slot->sl_opcnt > argp->opcnt) 5000 return false; 5001 /* This is the only check explicitly called by spec: */ 5002 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred)) 5003 return false; 5004 /* 5005 * There may be more comparisons we could actually do, but the 5006 * spec doesn't require us to catch every case where the calls 5007 * don't match (that would require caching the call as well as 5008 * the reply), so we don't bother. 5009 */ 5010 return true; 5011 } 5012 5013 /* 5014 * Note that the response is constructed here both for the case 5015 * of a new SEQUENCE request and for a replayed SEQUENCE request. 5016 * We do not cache SEQUENCE responses as SEQUENCE is idempotent. 5017 */ 5018 static void nfsd4_construct_sequence_response(struct nfsd4_session *session, 5019 struct nfsd4_sequence *seq) 5020 { 5021 struct nfs4_client *clp = session->se_client; 5022 5023 seq->maxslots_response = max(session->se_target_maxslots, 5024 seq->maxslots); 5025 seq->target_maxslots = session->se_target_maxslots; 5026 5027 switch (clp->cl_cb_state) { 5028 case NFSD4_CB_DOWN: 5029 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; 5030 break; 5031 case NFSD4_CB_FAULT: 5032 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; 5033 break; 5034 default: 5035 seq->status_flags = 0; 5036 } 5037 if (!list_empty(&clp->cl_revoked)) 5038 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; 5039 if (atomic_read(&clp->cl_admin_revoked)) 5040 seq->status_flags |= SEQ4_STATUS_ADMIN_STATE_REVOKED; 5041 } 5042 5043 static bool nfsd4_slots_inuse(struct nfsd4_session *ses, int from) 5044 { 5045 int i; 5046 5047 for (i = from; i < ses->se_fchannel.maxreqs; i++) { 5048 struct nfsd4_slot *slot = xa_load(&ses->se_slots, i); 5049 5050 if (slot->sl_flags & NFSD4_SLOT_INUSE) 5051 return true; 5052 } 5053 return false; 5054 } 5055 5056 __be32 5057 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 5058 union nfsd4_op_u *u) 5059 { 5060 struct nfsd4_compoundargs *args = rqstp->rq_argp; 5061 struct nfsd4_sequence *seq = &u->sequence; 5062 struct nfsd4_compoundres *resp = rqstp->rq_resp; 5063 struct xdr_stream *xdr = resp->xdr; 5064 struct nfsd4_session *session; 5065 struct nfs4_client *clp; 5066 struct nfsd4_slot *slot; 5067 struct nfsd4_conn *conn; 5068 __be32 status; 5069 int buflen; 5070 u32 maxlen, respsize; 5071 struct net *net = SVC_NET(rqstp); 5072 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 5073 5074 if (resp->opcnt != 1) 5075 return nfserr_sequence_pos; 5076 5077 /* 5078 * Will be either used or freed by nfsd4_sequence_check_conn 5079 * below. 5080 */ 5081 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); 5082 if (!conn) 5083 return nfserr_jukebox; 5084 5085 spin_lock(&nn->client_lock); 5086 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); 5087 if (!session) 5088 goto out_no_session; 5089 clp = session->se_client; 5090 5091 status = nfserr_too_many_ops; 5092 if (nfsd4_session_too_many_ops(rqstp, session)) 5093 goto out_put_session; 5094 5095 status = nfserr_req_too_big; 5096 if (nfsd4_request_too_big(rqstp, session)) 5097 goto out_put_session; 5098 5099 status = nfserr_badslot; 5100 if (seq->slotid >= session->se_fchannel.maxreqs) 5101 goto out_put_session; 5102 5103 slot = xa_load(&session->se_slots, seq->slotid); 5104 dprintk("%s: slotid %d\n", __func__, seq->slotid); 5105 5106 trace_nfsd_slot_seqid_sequence(clp, seq, slot); 5107 5108 nfsd4_construct_sequence_response(session, seq); 5109 5110 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_flags); 5111 if (status == nfserr_replay_cache) { 5112 status = nfserr_seq_misordered; 5113 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) 5114 goto out_put_session; 5115 status = nfserr_seq_false_retry; 5116 if (!replay_matches_cache(rqstp, seq, slot)) 5117 goto out_put_session; 5118 cstate->slot = slot; 5119 cstate->session = session; 5120 cstate->clp = clp; 5121 /* Return the cached reply status and set cstate->status 5122 * for nfsd4_proc_compound processing */ 5123 status = nfsd4_replay_cache_entry(resp, seq); 5124 cstate->status = nfserr_replay_cache; 5125 goto out; 5126 } 5127 if (status) 5128 goto out_put_session; 5129 5130 status = nfsd4_sequence_check_conn(conn, session); 5131 conn = NULL; 5132 if (status) 5133 goto out_put_session; 5134 5135 if (session->se_target_maxslots < session->se_fchannel.maxreqs && 5136 slot->sl_generation == session->se_slot_gen && 5137 seq->maxslots <= session->se_target_maxslots && 5138 seq->slotid < session->se_target_maxslots && 5139 !nfsd4_slots_inuse(session, session->se_target_maxslots)) 5140 /* Client acknowledged our reduce maxreqs */ 5141 free_session_slots(session, session->se_target_maxslots); 5142 5143 buflen = (seq->cachethis) ? 5144 session->se_fchannel.maxresp_cached : 5145 session->se_fchannel.maxresp_sz; 5146 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : 5147 nfserr_rep_too_big; 5148 if (buflen < rqstp->rq_auth_slack) 5149 goto out_put_session; 5150 maxlen = buflen - rqstp->rq_auth_slack; 5151 5152 /* 5153 * A SEQUENCE result too large for maxlen never reaches 5154 * nfsd4_encode_sequence(), so cstate.data_offset stays zero and 5155 * the reply cache overruns the slot. 5156 */ 5157 respsize = nfsd4_max_reply(rqstp, &args->ops[0]); 5158 if (!nfsd4_last_compound_op(rqstp)) 5159 respsize += COMPOUND_ERR_SLACK_SPACE; 5160 if (xdr->buf->len + respsize > maxlen) 5161 goto out_put_session; 5162 5163 if (xdr_restrict_buflen(xdr, maxlen)) 5164 goto out_put_session; 5165 svc_reserve_auth(rqstp, buflen); 5166 5167 status = nfs_ok; 5168 /* Success! accept new slot seqid */ 5169 slot->sl_seqid = seq->seqid; 5170 slot->sl_flags &= ~NFSD4_SLOT_REUSED; 5171 slot->sl_flags |= NFSD4_SLOT_INUSE; 5172 slot->sl_generation = session->se_slot_gen; 5173 if (seq->cachethis) 5174 slot->sl_flags |= NFSD4_SLOT_CACHETHIS; 5175 else 5176 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; 5177 5178 cstate->slot = slot; 5179 cstate->slot_owned = true; 5180 cstate->session = session; 5181 cstate->clp = clp; 5182 5183 /* 5184 * If the client ever uses the highest available slot, 5185 * gently try to allocate another 20%. This allows 5186 * fairly quick growth without grossly over-shooting what 5187 * the client might use. 5188 * 5189 * Bound that growth by the service's thread ceiling: 5190 * slots beyond the nfsd thread count cannot raise this 5191 * client's throughput, only deepen its backlog. Cap each 5192 * session independently, since a session cannot use 5193 * another's slots; a shared budget would let idle sessions 5194 * pin an active client small. Compare against the 5195 * configured maximum, not the running thread count, so a 5196 * client resuming from idle can grow back before the pool 5197 * scales up. 5198 */ 5199 if (seq->slotid == session->se_fchannel.maxreqs - 1 && 5200 session->se_target_maxslots >= session->se_fchannel.maxreqs) { 5201 int s = session->se_fchannel.maxreqs; 5202 int ceiling = min_t(int, NFSD_MAX_SLOTS_PER_SESSION, 5203 svc_serv_maxthreads(rqstp->rq_server)); 5204 int cnt = min(DIV_ROUND_UP(s, 5), ceiling - s); 5205 void *prev_slot; 5206 5207 while (cnt-- > 0) { 5208 /* 5209 * GFP_NOWAIT both allows allocation under a 5210 * spinlock, and only succeeds if there is 5211 * plenty of memory. 5212 */ 5213 slot = nfsd4_alloc_slot(&session->se_fchannel, s, 5214 GFP_NOWAIT); 5215 if (!slot) 5216 break; 5217 prev_slot = xa_load(&session->se_slots, s); 5218 if (xa_is_value(prev_slot)) { 5219 slot->sl_seqid = xa_to_value(prev_slot); 5220 slot->sl_flags |= NFSD4_SLOT_REUSED; 5221 } 5222 if (!xa_is_err(xa_store(&session->se_slots, s, slot, 5223 GFP_NOWAIT))) { 5224 s += 1; 5225 session->se_fchannel.maxreqs = s; 5226 atomic_add(s - session->se_target_maxslots, 5227 &nfsd_total_target_slots); 5228 session->se_target_maxslots = s; 5229 } else { 5230 kfree(slot); 5231 break; 5232 } 5233 } 5234 } 5235 5236 out: 5237 trace_nfsd_seq4_status(rqstp, seq); 5238 out_no_session: 5239 if (conn) 5240 free_conn(conn); 5241 spin_unlock(&nn->client_lock); 5242 return status; 5243 out_put_session: 5244 nfsd4_put_session_locked(session); 5245 goto out_no_session; 5246 } 5247 5248 void 5249 nfsd4_sequence_done(struct nfsd4_compoundres *resp) 5250 { 5251 struct nfsd4_compound_state *cs = &resp->cstate; 5252 5253 if (nfsd4_has_session(cs)) { 5254 /* 5255 * Only the request that claimed the slot may update its 5256 * cached reply and clear NFSD4_SLOT_INUSE. 5257 */ 5258 if (cs->slot_owned) { 5259 nfsd4_store_cache_entry(resp); 5260 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; 5261 } 5262 /* Drop session reference that was taken in nfsd4_sequence() */ 5263 nfsd4_put_session(cs->session); 5264 } else if (cs->clp) 5265 put_client_renew(cs->clp); 5266 } 5267 5268 __be32 5269 nfsd4_destroy_clientid(struct svc_rqst *rqstp, 5270 struct nfsd4_compound_state *cstate, 5271 union nfsd4_op_u *u) 5272 { 5273 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid; 5274 struct nfs4_client *conf, *unconf; 5275 struct nfs4_client *clp = NULL; 5276 __be32 status = 0; 5277 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 5278 5279 spin_lock(&nn->client_lock); 5280 unconf = find_unconfirmed_client(&dc->clientid, true, nn); 5281 conf = find_confirmed_client(&dc->clientid, true, nn); 5282 WARN_ON_ONCE(conf && unconf); 5283 5284 if (conf) { 5285 if (client_has_state(conf)) { 5286 status = nfserr_clientid_busy; 5287 goto out; 5288 } 5289 status = mark_client_expired_locked(conf); 5290 if (status) 5291 goto out; 5292 clp = conf; 5293 } else if (unconf) 5294 clp = unconf; 5295 else { 5296 status = nfserr_stale_clientid; 5297 goto out; 5298 } 5299 if (!nfsd4_mach_creds_match(clp, rqstp)) { 5300 clp = NULL; 5301 status = nfserr_wrong_cred; 5302 goto out; 5303 } 5304 trace_nfsd_clid_destroyed(&clp->cl_clientid); 5305 unhash_client_locked(clp); 5306 out: 5307 spin_unlock(&nn->client_lock); 5308 if (clp) 5309 expire_client(clp); 5310 return status; 5311 } 5312 5313 __be32 5314 nfsd4_reclaim_complete(struct svc_rqst *rqstp, 5315 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 5316 { 5317 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete; 5318 struct nfs4_client *clp = cstate->clp; 5319 __be32 status = 0; 5320 5321 if (rc->rca_one_fs) { 5322 if (!cstate->current_fh.fh_dentry) 5323 return nfserr_nofilehandle; 5324 /* 5325 * We don't take advantage of the rca_one_fs case. 5326 * That's OK, it's optional, we can safely ignore it. 5327 */ 5328 return nfs_ok; 5329 } 5330 5331 status = nfserr_complete_already; 5332 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags)) 5333 goto out; 5334 5335 status = nfserr_stale_clientid; 5336 if (is_client_expired(clp)) 5337 /* 5338 * The following error isn't really legal. 5339 * But we only get here if the client just explicitly 5340 * destroyed the client. Surely it no longer cares what 5341 * error it gets back on an operation for the dead 5342 * client. 5343 */ 5344 goto out; 5345 5346 status = nfs_ok; 5347 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid); 5348 nfsd4_client_record_create(clp); 5349 inc_reclaim_complete(clp); 5350 out: 5351 return status; 5352 } 5353 5354 __be32 5355 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 5356 union nfsd4_op_u *u) 5357 { 5358 struct nfsd4_setclientid *setclid = &u->setclientid; 5359 struct xdr_netobj clname = setclid->se_name; 5360 nfs4_verifier clverifier = setclid->se_verf; 5361 struct nfs4_client *conf, *new; 5362 struct nfs4_client *unconf = NULL; 5363 __be32 status; 5364 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 5365 5366 new = create_client(clname, rqstp, &clverifier); 5367 if (new == NULL) 5368 return nfserr_jukebox; 5369 spin_lock(&nn->client_lock); 5370 conf = find_confirmed_client_by_name(&clname, nn); 5371 if (conf && client_has_state(conf)) { 5372 status = nfserr_clid_inuse; 5373 if (clp_used_exchangeid(conf)) 5374 goto out; 5375 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { 5376 trace_nfsd_clid_cred_mismatch(conf, rqstp); 5377 goto out; 5378 } 5379 } 5380 unconf = find_unconfirmed_client_by_name(&clname, nn); 5381 if (unconf) 5382 unhash_client_locked(unconf); 5383 if (conf) { 5384 if (same_verf(&conf->cl_verifier, &clverifier)) { 5385 copy_clid(new, conf); 5386 gen_confirm(new, nn); 5387 } else 5388 trace_nfsd_clid_verf_mismatch(conf, rqstp, 5389 &clverifier); 5390 } else 5391 trace_nfsd_clid_fresh(new); 5392 new->cl_minorversion = 0; 5393 gen_callback(new, setclid, rqstp); 5394 add_to_unconfirmed(new); 5395 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; 5396 setclid->se_clientid.cl_id = new->cl_clientid.cl_id; 5397 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); 5398 new = NULL; 5399 status = nfs_ok; 5400 out: 5401 spin_unlock(&nn->client_lock); 5402 if (new) 5403 free_client(new); 5404 if (unconf) { 5405 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid); 5406 expire_client(unconf); 5407 } 5408 return status; 5409 } 5410 5411 __be32 5412 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, 5413 struct nfsd4_compound_state *cstate, 5414 union nfsd4_op_u *u) 5415 { 5416 struct nfsd4_setclientid_confirm *setclientid_confirm = 5417 &u->setclientid_confirm; 5418 struct nfs4_client *conf, *unconf; 5419 struct nfs4_client *old = NULL; 5420 nfs4_verifier confirm = setclientid_confirm->sc_confirm; 5421 clientid_t * clid = &setclientid_confirm->sc_clientid; 5422 __be32 status; 5423 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 5424 5425 if (STALE_CLIENTID(clid, nn)) 5426 return nfserr_stale_clientid; 5427 5428 spin_lock(&nn->client_lock); 5429 conf = find_confirmed_client(clid, false, nn); 5430 unconf = find_unconfirmed_client(clid, false, nn); 5431 /* 5432 * We try hard to give out unique clientid's, so if we get an 5433 * attempt to confirm the same clientid with a different cred, 5434 * the client may be buggy; this should never happen. 5435 * 5436 * Nevertheless, RFC 7530 recommends INUSE for this case: 5437 */ 5438 status = nfserr_clid_inuse; 5439 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) { 5440 trace_nfsd_clid_cred_mismatch(unconf, rqstp); 5441 goto out; 5442 } 5443 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) { 5444 trace_nfsd_clid_cred_mismatch(conf, rqstp); 5445 goto out; 5446 } 5447 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { 5448 if (conf && same_verf(&confirm, &conf->cl_confirm)) { 5449 status = nfs_ok; 5450 } else 5451 status = nfserr_stale_clientid; 5452 goto out; 5453 } 5454 status = nfs_ok; 5455 if (conf) { 5456 if (get_client_locked(conf) == nfs_ok) { 5457 old = unconf; 5458 unhash_client_locked(old); 5459 nfsd4_change_callback(conf, &unconf->cl_cb_conn); 5460 } else { 5461 conf = NULL; 5462 } 5463 } 5464 5465 if (!conf) { 5466 old = find_confirmed_client_by_name(&unconf->cl_name, nn); 5467 if (old) { 5468 status = nfserr_clid_inuse; 5469 if (client_has_state(old) 5470 && !same_creds(&unconf->cl_cred, 5471 &old->cl_cred)) { 5472 old = NULL; 5473 goto out; 5474 } 5475 status = mark_client_expired_locked(old); 5476 if (status) { 5477 old = NULL; 5478 goto out; 5479 } 5480 trace_nfsd_clid_replaced(&old->cl_clientid); 5481 } 5482 status = get_client_locked(unconf); 5483 if (status != nfs_ok) { 5484 old = NULL; 5485 goto out; 5486 } 5487 move_to_confirmed(unconf); 5488 conf = unconf; 5489 } 5490 spin_unlock(&nn->client_lock); 5491 if (conf == unconf) 5492 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY); 5493 nfsd4_probe_callback(conf); 5494 spin_lock(&nn->client_lock); 5495 put_client_renew_locked(conf); 5496 out: 5497 spin_unlock(&nn->client_lock); 5498 if (old) 5499 expire_client(old); 5500 return status; 5501 } 5502 5503 static struct nfs4_file *nfsd4_alloc_file(void) 5504 { 5505 return kmem_cache_alloc(file_slab, GFP_KERNEL); 5506 } 5507 5508 /* OPEN Share state helper functions */ 5509 5510 static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp) 5511 { 5512 refcount_set(&fp->fi_ref, 1); 5513 spin_lock_init(&fp->fi_lock); 5514 INIT_LIST_HEAD(&fp->fi_stateids); 5515 INIT_LIST_HEAD(&fp->fi_delegations); 5516 INIT_LIST_HEAD(&fp->fi_clnt_odstate); 5517 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle); 5518 RCU_INIT_POINTER(fp->fi_deleg_file, NULL); 5519 fp->fi_rdeleg_file = NULL; 5520 fp->fi_had_conflict = false; 5521 fp->fi_share_deny = 0; 5522 memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); 5523 memset(fp->fi_access, 0, sizeof(fp->fi_access)); 5524 fp->fi_aliased = false; 5525 fp->fi_inode = d_inode(fh->fh_dentry); 5526 #ifdef CONFIG_NFSD_PNFS 5527 INIT_LIST_HEAD(&fp->fi_lo_states); 5528 atomic_set(&fp->fi_lo_recalls, 0); 5529 #endif 5530 } 5531 5532 void 5533 nfsd4_free_slabs(void) 5534 { 5535 kmem_cache_destroy(client_slab); 5536 kmem_cache_destroy(openowner_slab); 5537 kmem_cache_destroy(lockowner_slab); 5538 kmem_cache_destroy(file_slab); 5539 kmem_cache_destroy(stateid_slab); 5540 kmem_cache_destroy(deleg_slab); 5541 kmem_cache_destroy(odstate_slab); 5542 kmem_cache_destroy(async_copy_slab); 5543 } 5544 5545 int 5546 nfsd4_init_slabs(void) 5547 { 5548 client_slab = KMEM_CACHE(nfs4_client, 0); 5549 if (client_slab == NULL) 5550 goto out; 5551 openowner_slab = KMEM_CACHE(nfs4_openowner, 0); 5552 if (openowner_slab == NULL) 5553 goto out_free_client_slab; 5554 lockowner_slab = KMEM_CACHE(nfs4_lockowner, 0); 5555 if (lockowner_slab == NULL) 5556 goto out_free_openowner_slab; 5557 file_slab = KMEM_CACHE(nfs4_file, 0); 5558 if (file_slab == NULL) 5559 goto out_free_lockowner_slab; 5560 stateid_slab = KMEM_CACHE(nfs4_ol_stateid, 0); 5561 if (stateid_slab == NULL) 5562 goto out_free_file_slab; 5563 deleg_slab = KMEM_CACHE(nfs4_delegation, 0); 5564 if (deleg_slab == NULL) 5565 goto out_free_stateid_slab; 5566 odstate_slab = KMEM_CACHE(nfs4_clnt_odstate, 0); 5567 if (odstate_slab == NULL) 5568 goto out_free_deleg_slab; 5569 async_copy_slab = KMEM_CACHE(nfsd4_async_copy, 0); 5570 if (async_copy_slab == NULL) 5571 goto out_free_odstate_slab; 5572 return 0; 5573 5574 out_free_odstate_slab: 5575 kmem_cache_destroy(odstate_slab); 5576 out_free_deleg_slab: 5577 kmem_cache_destroy(deleg_slab); 5578 out_free_stateid_slab: 5579 kmem_cache_destroy(stateid_slab); 5580 out_free_file_slab: 5581 kmem_cache_destroy(file_slab); 5582 out_free_lockowner_slab: 5583 kmem_cache_destroy(lockowner_slab); 5584 out_free_openowner_slab: 5585 kmem_cache_destroy(openowner_slab); 5586 out_free_client_slab: 5587 kmem_cache_destroy(client_slab); 5588 out: 5589 return -ENOMEM; 5590 } 5591 5592 #define NFSD_RECALL_ANY_COOLDOWN_SECS 5 5593 5594 static unsigned long 5595 nfsd4_courtesy_shrinker_count(struct shrinker *shrink, 5596 struct shrink_control *sc) 5597 { 5598 struct nfsd_net *nn = shrink->private_data; 5599 long backlog, count; 5600 5601 count = atomic_read(&nn->nfsd_courtesy_clients); 5602 if (!count) 5603 return 0; 5604 5605 queue_work(laundry_wq, &nn->nfsd_courtesy_work); 5606 5607 /* Work already queued is not available to reclaim again. */ 5608 backlog = atomic_long_read(&nn->nfsd_shrink_backlog); 5609 return count > backlog ? count - backlog : 0; 5610 } 5611 5612 static unsigned long 5613 nfsd4_deleg_shrinker_count(struct shrinker *shrink, struct shrink_control *sc) 5614 { 5615 struct nfsd_net *nn = shrink->private_data; 5616 time64_t elapsed; 5617 long backlog, count; 5618 5619 count = atomic_long_read(&nn->nfsd_delegations); 5620 if (!count) 5621 return 0; 5622 5623 /* 5624 * Delegations the last sweep reached stay unreclaimable until 5625 * deleg_reaper()'s cooldown expires. CB_RECALL_ANY leaves the 5626 * choice of delegations to the client, so there is no return 5627 * to wait on instead. 5628 */ 5629 elapsed = ktime_get_boottime_seconds() - 5630 READ_ONCE(nn->nfsd_last_recall_any); 5631 if (elapsed < NFSD_RECALL_ANY_COOLDOWN_SECS) 5632 return 0; 5633 5634 /* 5635 * Unlike the courtesy shrinker, this one queues no work. 5636 * Nothing is recalled until a scan request arrives. Subtract 5637 * the requests already recorded, or concurrent reclaimers 5638 * each see the whole namespace and stack a scan on top of it. 5639 */ 5640 backlog = atomic_long_read(&nn->nfsd_deleg_backlog); 5641 return count > backlog ? count - backlog : 0; 5642 } 5643 5644 static unsigned long 5645 nfsd4_courtesy_shrinker_scan(struct shrinker *shrink, 5646 struct shrink_control *sc) 5647 { 5648 struct nfsd_net *nn = shrink->private_data; 5649 5650 atomic_long_add(sc->nr_to_scan, &nn->nfsd_shrink_backlog); 5651 queue_work(laundry_wq, &nn->nfsd_courtesy_work); 5652 5653 /* 5654 * The reaper runs from laundry_wq. Report no progress rather 5655 * than claim memory that is not free yet. 5656 */ 5657 return SHRINK_STOP; 5658 } 5659 5660 static unsigned long 5661 nfsd4_deleg_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc) 5662 { 5663 struct nfsd_net *nn = shrink->private_data; 5664 5665 atomic_long_add(sc->nr_to_scan, &nn->nfsd_deleg_backlog); 5666 queue_work(laundry_wq, &nn->nfsd_deleg_work); 5667 5668 /* 5669 * The reaper sends CB_RECALL_ANY, so nothing is free when 5670 * this returns. 5671 */ 5672 return SHRINK_STOP; 5673 } 5674 5675 static struct shrinker * 5676 nfsd4_alloc_state_shrinker(struct nfsd_net *nn, const char *name, 5677 unsigned long (*count)(struct shrinker *, 5678 struct shrink_control *), 5679 unsigned long (*scan)(struct shrinker *, 5680 struct shrink_control *)) 5681 { 5682 struct shrinker *shrink; 5683 5684 shrink = shrinker_alloc(0, "%s:%s", name, nn->nfsd_name); 5685 if (!shrink) 5686 return NULL; 5687 5688 shrink->count_objects = count; 5689 shrink->scan_objects = scan; 5690 shrink->private_data = nn; 5691 5692 shrinker_register(shrink); 5693 return shrink; 5694 } 5695 5696 void 5697 nfsd4_init_leases_net(struct nfsd_net *nn) 5698 { 5699 struct sysinfo si; 5700 u64 max_clients; 5701 5702 nn->nfsd4_lease = 90; /* default lease time */ 5703 nn->nfsd4_grace = 90; 5704 nn->clverifier_counter = get_random_u32(); 5705 nn->clientid_base = get_random_u32(); 5706 nn->clientid_counter = nn->clientid_base + 1; 5707 nn->s2s_cp_cl_id = nn->clientid_counter++; 5708 5709 atomic_set(&nn->nfs4_client_count, 0); 5710 si_meminfo(&si); 5711 max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024); 5712 max_clients *= NFS4_CLIENTS_PER_GB; 5713 nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB); 5714 5715 atomic_set(&nn->nfsd_courtesy_clients, 0); 5716 } 5717 5718 enum rp_lock { 5719 RP_UNLOCKED, 5720 RP_LOCKED, 5721 RP_UNHASHED, 5722 }; 5723 5724 static void init_nfs4_replay(struct nfs4_replay *rp) 5725 { 5726 rp->rp_status = nfserr_serverfault; 5727 rp->rp_buflen = 0; 5728 rp->rp_buf = rp->rp_ibuf; 5729 rp->rp_locked = RP_UNLOCKED; 5730 } 5731 5732 static int nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, 5733 struct nfs4_stateowner *so) 5734 { 5735 if (!nfsd4_has_session(cstate)) { 5736 wait_var_event(&so->so_replay.rp_locked, 5737 cmpxchg(&so->so_replay.rp_locked, 5738 RP_UNLOCKED, RP_LOCKED) != RP_LOCKED); 5739 if (so->so_replay.rp_locked == RP_UNHASHED) 5740 return -EAGAIN; 5741 cstate->replay_owner = nfs4_get_stateowner(so); 5742 } 5743 return 0; 5744 } 5745 5746 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) 5747 { 5748 struct nfs4_stateowner *so = cstate->replay_owner; 5749 5750 if (so != NULL) { 5751 cstate->replay_owner = NULL; 5752 store_release_wake_up(&so->so_replay.rp_locked, RP_UNLOCKED); 5753 nfs4_put_stateowner(so); 5754 } 5755 } 5756 5757 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp) 5758 { 5759 struct nfs4_stateowner *sop; 5760 5761 sop = kmem_cache_alloc(slab, GFP_KERNEL); 5762 if (!sop) 5763 return NULL; 5764 5765 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL); 5766 if (!sop->so_owner.data) { 5767 kmem_cache_free(slab, sop); 5768 return NULL; 5769 } 5770 5771 INIT_LIST_HEAD(&sop->so_stateids); 5772 sop->so_client = clp; 5773 init_nfs4_replay(&sop->so_replay); 5774 atomic_set(&sop->so_count, 1); 5775 return sop; 5776 } 5777 5778 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval) 5779 { 5780 lockdep_assert_held(&clp->cl_lock); 5781 5782 list_add(&oo->oo_owner.so_strhash, 5783 &clp->cl_ownerstr_hashtbl[strhashval]); 5784 list_add(&oo->oo_perclient, &clp->cl_openowners); 5785 } 5786 5787 static void nfs4_unhash_openowner(struct nfs4_stateowner *so) 5788 { 5789 unhash_openowner_locked(openowner(so)); 5790 } 5791 5792 static void nfs4_free_openowner(struct nfs4_stateowner *so) 5793 { 5794 struct nfs4_openowner *oo = openowner(so); 5795 5796 kmem_cache_free(openowner_slab, oo); 5797 } 5798 5799 static const struct nfs4_stateowner_operations openowner_ops = { 5800 .so_unhash = nfs4_unhash_openowner, 5801 .so_free = nfs4_free_openowner, 5802 }; 5803 5804 static struct nfs4_ol_stateid * 5805 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) 5806 { 5807 struct nfs4_ol_stateid *local, *ret = NULL; 5808 struct nfs4_openowner *oo = open->op_openowner; 5809 5810 lockdep_assert_held(&fp->fi_lock); 5811 5812 list_for_each_entry(local, &fp->fi_stateids, st_perfile) { 5813 /* ignore lock owners */ 5814 if (local->st_stateowner->so_is_open_owner == 0) 5815 continue; 5816 if (local->st_stateowner != &oo->oo_owner) 5817 continue; 5818 if (local->st_stid.sc_type == SC_TYPE_OPEN && 5819 !local->st_stid.sc_status) { 5820 ret = local; 5821 refcount_inc(&ret->st_stid.sc_count); 5822 break; 5823 } 5824 } 5825 return ret; 5826 } 5827 5828 static void nfsd4_drop_revoked_stid(struct nfs4_stid *s) 5829 __releases(&s->sc_client->cl_lock) 5830 { 5831 struct nfs4_client *cl = s->sc_client; 5832 LIST_HEAD(reaplist); 5833 struct nfs4_layout_stateid *ls; 5834 struct nfs4_ol_stateid *stp; 5835 struct nfs4_delegation *dp; 5836 bool unhashed; 5837 5838 switch (s->sc_type) { 5839 case SC_TYPE_OPEN: 5840 stp = openlockstateid(s); 5841 if (unhash_open_stateid(stp, &reaplist)) 5842 put_ol_stateid_locked(stp, &reaplist); 5843 spin_unlock(&cl->cl_lock); 5844 free_ol_stateid_reaplist(&reaplist); 5845 break; 5846 case SC_TYPE_LOCK: 5847 stp = openlockstateid(s); 5848 unhashed = unhash_lock_stateid(stp); 5849 spin_unlock(&cl->cl_lock); 5850 if (unhashed) 5851 nfs4_put_stid(s); 5852 break; 5853 case SC_TYPE_DELEG: 5854 dp = delegstateid(s); 5855 list_del_init(&dp->dl_recall_lru); 5856 s->sc_status |= SC_STATUS_FREED; 5857 spin_unlock(&cl->cl_lock); 5858 nfs4_put_stid(s); 5859 break; 5860 case SC_TYPE_LAYOUT: 5861 ls = layoutstateid(s); 5862 list_del_init(&ls->ls_perclnt); 5863 spin_unlock(&cl->cl_lock); 5864 nfs4_put_stid(s); 5865 break; 5866 default: 5867 spin_unlock(&cl->cl_lock); 5868 } 5869 } 5870 5871 static void nfsd40_drop_revoked_stid(struct nfs4_client *cl, 5872 stateid_t *stid) 5873 { 5874 /* NFSv4.0 has no way for the client to tell the server 5875 * that it can forget an admin-revoked stateid. 5876 * So we keep it around until the first time that the 5877 * client uses it, and drop it the first time 5878 * nfserr_admin_revoked is returned. 5879 * For v4.1 and later we wait until explicitly told 5880 * to free the stateid. 5881 */ 5882 if (cl->cl_minorversion == 0) { 5883 struct nfs4_stid *st; 5884 5885 spin_lock(&cl->cl_lock); 5886 st = find_stateid_locked(cl, stid); 5887 if (st) 5888 nfsd4_drop_revoked_stid(st); 5889 else 5890 spin_unlock(&cl->cl_lock); 5891 } 5892 } 5893 5894 static __be32 5895 nfsd4_verify_open_stid(struct nfs4_stid *s) 5896 { 5897 __be32 ret = nfs_ok; 5898 5899 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) 5900 ret = nfserr_admin_revoked; 5901 else if (s->sc_status & SC_STATUS_REVOKED) 5902 ret = nfserr_deleg_revoked; 5903 else if (s->sc_status & SC_STATUS_CLOSED) 5904 ret = nfserr_bad_stateid; 5905 return ret; 5906 } 5907 5908 /* Lock the stateid st_mutex, and deal with races with CLOSE */ 5909 static __be32 5910 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp) 5911 { 5912 __be32 ret; 5913 5914 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX); 5915 ret = nfsd4_verify_open_stid(&stp->st_stid); 5916 if (ret == nfserr_admin_revoked) 5917 nfsd40_drop_revoked_stid(stp->st_stid.sc_client, 5918 &stp->st_stid.sc_stateid); 5919 5920 if (ret != nfs_ok) 5921 mutex_unlock(&stp->st_mutex); 5922 return ret; 5923 } 5924 5925 static struct nfs4_ol_stateid * 5926 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) 5927 { 5928 struct nfs4_ol_stateid *stp; 5929 for (;;) { 5930 spin_lock(&fp->fi_lock); 5931 stp = nfsd4_find_existing_open(fp, open); 5932 spin_unlock(&fp->fi_lock); 5933 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok) 5934 break; 5935 nfs4_put_stid(&stp->st_stid); 5936 } 5937 return stp; 5938 } 5939 5940 static struct nfs4_openowner * 5941 find_or_alloc_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, 5942 struct nfsd4_compound_state *cstate) 5943 { 5944 struct nfs4_client *clp = cstate->clp; 5945 struct nfs4_openowner *oo, *new = NULL; 5946 5947 retry: 5948 spin_lock(&clp->cl_lock); 5949 oo = find_openstateowner_str(strhashval, open, clp); 5950 if (!oo && new) { 5951 hash_openowner(new, clp, strhashval); 5952 spin_unlock(&clp->cl_lock); 5953 return new; 5954 } 5955 spin_unlock(&clp->cl_lock); 5956 5957 if (oo && !(oo->oo_flags & NFS4_OO_CONFIRMED)) { 5958 /* Replace unconfirmed owners without checking for replay. */ 5959 release_openowner(oo); 5960 oo = NULL; 5961 goto retry; 5962 } 5963 if (oo) { 5964 if (new) 5965 nfs4_free_stateowner(&new->oo_owner); 5966 return oo; 5967 } 5968 5969 new = alloc_stateowner(openowner_slab, &open->op_owner, clp); 5970 if (!new) 5971 return NULL; 5972 new->oo_owner.so_ops = &openowner_ops; 5973 new->oo_owner.so_is_open_owner = 1; 5974 new->oo_owner.so_seqid = open->op_seqid; 5975 new->oo_flags = 0; 5976 if (nfsd4_has_session(cstate)) 5977 new->oo_flags |= NFS4_OO_CONFIRMED; 5978 new->oo_time = 0; 5979 new->oo_last_closed_stid = NULL; 5980 INIT_LIST_HEAD(&new->oo_close_lru); 5981 goto retry; 5982 } 5983 5984 static struct nfs4_ol_stateid * 5985 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) 5986 { 5987 5988 struct nfs4_openowner *oo = open->op_openowner; 5989 struct nfs4_ol_stateid *retstp = NULL; 5990 struct nfs4_ol_stateid *stp; 5991 5992 stp = open->op_stp; 5993 /* We are moving these outside of the spinlocks to avoid the warnings */ 5994 mutex_init(&stp->st_mutex); 5995 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); 5996 5997 retry: 5998 spin_lock(&oo->oo_owner.so_client->cl_lock); 5999 spin_lock(&fp->fi_lock); 6000 6001 if (nfs4_openowner_unhashed(oo)) { 6002 mutex_unlock(&stp->st_mutex); 6003 stp = NULL; 6004 goto out_unlock; 6005 } 6006 6007 retstp = nfsd4_find_existing_open(fp, open); 6008 if (retstp) 6009 goto out_unlock; 6010 6011 open->op_stp = NULL; 6012 refcount_inc(&stp->st_stid.sc_count); 6013 stp->st_stid.sc_type = SC_TYPE_OPEN; 6014 INIT_LIST_HEAD(&stp->st_locks); 6015 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); 6016 get_nfs4_file(fp); 6017 stp->st_stid.sc_file = fp; 6018 stp->st_access_bmap = 0; 6019 stp->st_deny_bmap = 0; 6020 stp->st_openstp = NULL; 6021 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); 6022 list_add(&stp->st_perfile, &fp->fi_stateids); 6023 6024 out_unlock: 6025 spin_unlock(&fp->fi_lock); 6026 spin_unlock(&oo->oo_owner.so_client->cl_lock); 6027 if (retstp) { 6028 /* Handle races with CLOSE */ 6029 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { 6030 nfs4_put_stid(&retstp->st_stid); 6031 goto retry; 6032 } 6033 /* To keep mutex tracking happy */ 6034 mutex_unlock(&stp->st_mutex); 6035 stp = retstp; 6036 } 6037 return stp; 6038 } 6039 6040 /* 6041 * In the 4.0 case we need to keep the owners around a little while to handle 6042 * CLOSE replay. We still do need to release any file access that is held by 6043 * them before returning however. 6044 */ 6045 static void 6046 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) 6047 { 6048 struct nfs4_ol_stateid *last; 6049 struct nfs4_openowner *oo = openowner(s->st_stateowner); 6050 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, 6051 nfsd_net_id); 6052 6053 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); 6054 6055 /* 6056 * We know that we hold one reference via nfsd4_close, and another 6057 * "persistent" reference for the client. If the refcount is higher 6058 * than 2, then there are still calls in progress that are using this 6059 * stateid. We can't put the sc_file reference until they are finished. 6060 * Wait for the refcount to drop to 2. Since it has been unhashed, 6061 * there should be no danger of the refcount going back up again at 6062 * this point. 6063 * Some threads with a reference might be waiting for rp_locked, 6064 * so tell them to stop waiting. 6065 */ 6066 store_release_wake_up(&oo->oo_owner.so_replay.rp_locked, RP_UNHASHED); 6067 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2); 6068 6069 release_all_access(s); 6070 if (s->st_stid.sc_file) { 6071 put_nfs4_file(s->st_stid.sc_file); 6072 s->st_stid.sc_file = NULL; 6073 } 6074 6075 spin_lock(&nn->client_lock); 6076 last = oo->oo_last_closed_stid; 6077 oo->oo_last_closed_stid = s; 6078 list_move_tail(&oo->oo_close_lru, &nn->close_lru); 6079 oo->oo_time = ktime_get_boottime_seconds(); 6080 spin_unlock(&nn->client_lock); 6081 if (last) 6082 nfs4_put_stid(&last->st_stid); 6083 } 6084 6085 static noinline_for_stack struct nfs4_file * 6086 nfsd4_file_hash_lookup(const struct svc_fh *fhp) 6087 { 6088 struct inode *inode = d_inode(fhp->fh_dentry); 6089 struct rhlist_head *tmp, *list; 6090 struct nfs4_file *fi; 6091 6092 rcu_read_lock(); 6093 list = rhltable_lookup(&nfs4_file_rhltable, &inode, 6094 nfs4_file_rhash_params); 6095 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) { 6096 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) { 6097 if (refcount_inc_not_zero(&fi->fi_ref)) { 6098 rcu_read_unlock(); 6099 return fi; 6100 } 6101 } 6102 } 6103 rcu_read_unlock(); 6104 return NULL; 6105 } 6106 6107 /* 6108 * On hash insertion, identify entries with the same inode but 6109 * distinct filehandles. They will all be on the list returned 6110 * by rhltable_lookup(). 6111 * 6112 * inode->i_lock prevents racing insertions from adding an entry 6113 * for the same inode/fhp pair twice. 6114 */ 6115 static noinline_for_stack struct nfs4_file * 6116 nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp) 6117 { 6118 struct inode *inode = d_inode(fhp->fh_dentry); 6119 struct rhlist_head *tmp, *list; 6120 struct nfs4_file *ret = NULL; 6121 bool alias_found = false; 6122 struct nfs4_file *fi; 6123 int err; 6124 6125 rcu_read_lock(); 6126 spin_lock(&inode->i_lock); 6127 6128 list = rhltable_lookup(&nfs4_file_rhltable, &inode, 6129 nfs4_file_rhash_params); 6130 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) { 6131 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) { 6132 if (refcount_inc_not_zero(&fi->fi_ref)) 6133 ret = fi; 6134 } else 6135 fi->fi_aliased = alias_found = true; 6136 } 6137 if (ret) 6138 goto out_unlock; 6139 6140 nfsd4_file_init(fhp, new); 6141 err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist, 6142 nfs4_file_rhash_params); 6143 if (err) 6144 goto out_unlock; 6145 6146 new->fi_aliased = alias_found; 6147 ret = new; 6148 6149 out_unlock: 6150 spin_unlock(&inode->i_lock); 6151 rcu_read_unlock(); 6152 return ret; 6153 } 6154 6155 static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi) 6156 { 6157 rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist, 6158 nfs4_file_rhash_params); 6159 } 6160 6161 /* 6162 * Called to check deny when READ with all zero stateid or 6163 * WRITE with all zero or all one stateid 6164 */ 6165 static __be32 6166 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) 6167 { 6168 struct nfs4_file *fp; 6169 __be32 ret = nfs_ok; 6170 6171 fp = nfsd4_file_hash_lookup(current_fh); 6172 if (!fp) 6173 return ret; 6174 6175 /* Check for conflicting share reservations */ 6176 spin_lock(&fp->fi_lock); 6177 if (fp->fi_share_deny & deny_type) 6178 ret = nfserr_locked; 6179 spin_unlock(&fp->fi_lock); 6180 put_nfs4_file(fp); 6181 return ret; 6182 } 6183 6184 static bool nfsd4_deleg_present(const struct inode *inode) 6185 { 6186 struct file_lock_context *ctx = locks_inode_context(inode); 6187 6188 return ctx && !list_empty_careful(&ctx->flc_lease); 6189 } 6190 6191 /** 6192 * nfsd_wait_for_delegreturn - wait for delegations to be returned 6193 * @rqstp: the RPC transaction being executed 6194 * @inode: in-core inode of the file being waited for 6195 * 6196 * The timeout prevents deadlock if all nfsd threads happen to be 6197 * tied up waiting for returning delegations. 6198 * 6199 * Return values: 6200 * %true: delegation was returned 6201 * %false: timed out waiting for delegreturn 6202 */ 6203 bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode) 6204 { 6205 long __maybe_unused timeo; 6206 6207 timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode), 6208 NFSD_DELEGRETURN_TIMEOUT); 6209 trace_nfsd_delegret_wakeup(rqstp, inode, timeo); 6210 return timeo > 0; 6211 } 6212 6213 /* 6214 * gen_sessionid() composes a sessionid from the client's clientid and a 6215 * sequence counter, so the sequence alone identifies the granting session. 6216 */ 6217 static void nfsd4_recall_grant_sessionid(const struct nfs4_delegation *dp, 6218 struct nfsd4_sessionid *sid) 6219 { 6220 sid->clientid = dp->dl_stid.sc_client->cl_clientid; 6221 sid->sequence = dp->dl_recall_grant.sessionid_seq; 6222 sid->reserved = 0; 6223 } 6224 6225 static bool nfsd4_recall_grant_slot_retired(struct nfs4_delegation *dp) 6226 { 6227 struct nfs4_client *clp = dp->dl_stid.sc_client; 6228 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 6229 struct nfsd4_session *ses; 6230 struct nfsd4_sessionid sid; 6231 bool retired = false; 6232 void *entry; 6233 6234 if (!dp->dl_recall_grant.valid) 6235 return false; 6236 6237 nfsd4_recall_grant_sessionid(dp, &sid); 6238 6239 /* 6240 * A missing session does not prove the client saw the grant: a 6241 * DESTROY_SESSION unhashes its own session before the reply to 6242 * that compound is encoded. 6243 */ 6244 spin_lock(&nn->client_lock); 6245 ses = __find_in_sessionid_hashtbl((struct nfs4_sessionid *)&sid, 6246 clp->net); 6247 entry = ses ? xa_load(&ses->se_slots, dp->dl_recall_grant.slotid) : NULL; 6248 if (xa_is_value(entry)) { 6249 /* 6250 * A slot is freed only once the client has acknowledged 6251 * the smaller slot table, which it cannot do while a 6252 * request on that slot is outstanding. 6253 */ 6254 retired = true; 6255 } else if (entry) { 6256 struct nfsd4_slot *slot = entry; 6257 6258 /* 6259 * A reactivated slot was freed and rebuilt, so the same 6260 * acknowledgment applies. The seqid test errs toward 6261 * revoking: a rebuilt slot restarting at seqid 1 matches 6262 * an old grant. 6263 */ 6264 retired = (slot->sl_flags & NFSD4_SLOT_REUSED) || 6265 ((slot->sl_flags & NFSD4_SLOT_INITIALIZED) && 6266 slot->sl_seqid != dp->dl_recall_grant.seqid); 6267 } 6268 spin_unlock(&nn->client_lock); 6269 return retired; 6270 } 6271 6272 /* 6273 * ->prepare does not run on every send: nfsd4_run_cb_work() skips it 6274 * on a requeue, and a retry via rpc_restart_call_prepare() re-enters 6275 * the RPC layer beneath it. The granting request does not change, so 6276 * a send inherits a correct list. Retirement is the one transition 6277 * the list has to follow. 6278 */ 6279 static void nfsd4_refresh_recall_grant(struct nfs4_delegation *dp) 6280 { 6281 dp->dl_recall_grant.retired_at_send = 6282 nfsd4_recall_grant_slot_retired(dp); 6283 if (dp->dl_recall_grant.retired_at_send) 6284 nfsd41_cb_destroy_referring_call_list(&dp->dl_recall); 6285 } 6286 6287 static bool nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) 6288 { 6289 struct nfs4_delegation *dp = cb_to_delegation(cb); 6290 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, 6291 nfsd_net_id); 6292 6293 block_delegations(&dp->dl_stid.sc_file->fi_fhandle); 6294 6295 /* 6296 * We can't do this in nfsd_break_deleg_cb because it is 6297 * already holding inode->i_lock. 6298 * 6299 * If the dl_time != 0, then we know that it has already been 6300 * queued for a lease break. Don't queue it again. 6301 */ 6302 spin_lock(&nn->deleg_lock); 6303 if (delegation_hashed(dp) && dp->dl_time == 0) { 6304 dp->dl_time = ktime_get_boottime_seconds(); 6305 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); 6306 } 6307 spin_unlock(&nn->deleg_lock); 6308 6309 nfsd4_refresh_recall_grant(dp); 6310 6311 if (dp->dl_recall_grant.valid && !dp->dl_recall_grant.retired_at_send) { 6312 struct nfsd4_sessionid sid; 6313 6314 nfsd4_recall_grant_sessionid(dp, &sid); 6315 nfsd41_cb_referring_call(&dp->dl_recall, 6316 (struct nfs4_sessionid *)&sid, 6317 dp->dl_recall_grant.slotid, 6318 dp->dl_recall_grant.seqid); 6319 } 6320 return true; 6321 } 6322 6323 /* 6324 * cl_lock orders this against a laundromat reaping @dp: either 6325 * revoke_delegation() observes dl_recall_rejected and destroys @dp, or 6326 * it reached cl_revoked first and @dp is released here instead. 6327 */ 6328 static void nfsd4_deleg_recall_rejected(struct nfs4_delegation *dp) 6329 { 6330 struct nfs4_client *clp = dp->dl_stid.sc_client; 6331 6332 spin_lock(&clp->cl_lock); 6333 if (dp->dl_stid.sc_status & (SC_STATUS_CLOSED | SC_STATUS_FREED | 6334 SC_STATUS_ADMIN_REVOKED)) { 6335 spin_unlock(&clp->cl_lock); 6336 return; 6337 } 6338 if (dp->dl_stid.sc_status & SC_STATUS_FREEABLE) { 6339 dp->dl_stid.sc_status |= SC_STATUS_CLOSED; 6340 /* this function drops ->cl_lock */ 6341 nfsd4_drop_revoked_stid(&dp->dl_stid); 6342 return; 6343 } 6344 dp->dl_recall_rejected = true; 6345 spin_unlock(&clp->cl_lock); 6346 } 6347 6348 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, 6349 struct rpc_task *task) 6350 { 6351 struct nfs4_delegation *dp = cb_to_delegation(cb); 6352 6353 trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task); 6354 6355 switch (task->tk_status) { 6356 case 0: 6357 return 1; 6358 case -NFS4ERR_DELAY: 6359 if (dp->dl_stid.sc_status) 6360 /* CLOSED or REVOKED */ 6361 return 1; 6362 rpc_delay(task, 2 * HZ); 6363 return 0; 6364 case -EBADHANDLE: 6365 case -NFS4ERR_BAD_STATEID: 6366 /* 6367 * Retirement of the granting slot proves the client saw 6368 * the grant. Trust the rejection only if the slot had 6369 * retired when this recall was sent. 6370 */ 6371 if (dp->dl_recall_grant.retired_at_send) { 6372 nfsd4_deleg_recall_rejected(dp); 6373 return 1; 6374 } 6375 if (!dp->dl_stid.sc_status && dp->dl_retries--) { 6376 nfsd4_refresh_recall_grant(dp); 6377 rpc_delay(task, 2 * HZ); 6378 return 0; 6379 } 6380 return 1; 6381 default: 6382 return 1; 6383 } 6384 } 6385 6386 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) 6387 { 6388 struct nfs4_delegation *dp = cb_to_delegation(cb); 6389 6390 nfs4_put_stid(&dp->dl_stid); 6391 } 6392 6393 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { 6394 .prepare = nfsd4_cb_recall_prepare, 6395 .done = nfsd4_cb_recall_done, 6396 .release = nfsd4_cb_recall_release, 6397 .opcode = OP_CB_RECALL, 6398 }; 6399 6400 /* Called from break_lease() with flc_lock held. */ 6401 static bool 6402 nfsd_break_deleg_cb(struct file_lease *fl) 6403 { 6404 struct nfs4_delegation *dp = (struct nfs4_delegation *) fl->c.flc_owner; 6405 struct nfs4_file *fp = dp->dl_stid.sc_file; 6406 struct nfs4_client *clp = dp->dl_stid.sc_client; 6407 struct nfsd_net *nn; 6408 6409 trace_nfsd_cb_recall(&dp->dl_stid); 6410 6411 dp->dl_recalled = true; 6412 atomic_inc(&clp->cl_delegs_in_recall); 6413 if (try_to_expire_client(clp)) { 6414 nn = net_generic(clp->net, nfsd_net_id); 6415 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); 6416 } 6417 6418 /* 6419 * We don't want the locks code to timeout the lease for us; 6420 * we'll remove it ourself if a delegation isn't returned 6421 * in time: 6422 */ 6423 fl->fl_break_time = 0; 6424 6425 fp->fi_had_conflict = true; 6426 nfsd_break_one_deleg(dp); 6427 return false; 6428 } 6429 6430 /** 6431 * nfsd_breaker_owns_lease - Check if lease conflict was resolved 6432 * @fl: Lock state to check 6433 * 6434 * Return values: 6435 * %true: Lease conflict was resolved 6436 * %false: Lease conflict was not resolved. 6437 */ 6438 static bool nfsd_breaker_owns_lease(struct file_lease *fl) 6439 { 6440 struct nfs4_delegation *dl = fl->c.flc_owner; 6441 struct nfsd_thread_local_info *ntli; 6442 struct svc_rqst *rqst; 6443 struct nfs4_client *clp; 6444 6445 /* Only nfsd leases */ 6446 if (fl->fl_lmops != &nfsd_lease_mng_ops) 6447 return false; 6448 6449 rqst = nfsd_current_rqst(); 6450 if (!nfsd_v4client(rqst)) 6451 return false; 6452 ntli = rqst->rq_private; 6453 clp = *ntli->ntli_lease_breaker; 6454 return dl->dl_stid.sc_client == clp; 6455 } 6456 6457 static int 6458 nfsd_change_deleg_cb(struct file_lease *onlist, int arg, 6459 struct list_head *dispose) 6460 { 6461 struct nfs4_delegation *dp = (struct nfs4_delegation *) onlist->c.flc_owner; 6462 struct nfs4_client *clp = dp->dl_stid.sc_client; 6463 6464 if (arg & F_UNLCK) { 6465 if (dp->dl_recalled) 6466 atomic_dec(&clp->cl_delegs_in_recall); 6467 return lease_modify(onlist, arg, dispose); 6468 } else 6469 return -EAGAIN; 6470 } 6471 6472 /** 6473 * nfsd4_deleg_lm_open_conflict - see if the given file points to an inode that has 6474 * an existing open that would conflict with the 6475 * desired lease. 6476 * @filp: file to check 6477 * @arg: type of lease that we're trying to acquire 6478 * 6479 * The kernel will call into this operation to determine whether there 6480 * are conflicting opens that may prevent the deleg from being granted. 6481 * For nfsd, that check is done at a higher level, so this trivially 6482 * returns 0. 6483 */ 6484 static int 6485 nfsd4_deleg_lm_open_conflict(struct file *filp, int arg) 6486 { 6487 return 0; 6488 } 6489 6490 static const struct lease_manager_operations nfsd_lease_mng_ops = { 6491 .lm_breaker_owns_lease = nfsd_breaker_owns_lease, 6492 .lm_break = nfsd_break_deleg_cb, 6493 .lm_change = nfsd_change_deleg_cb, 6494 .lm_open_conflict = nfsd4_deleg_lm_open_conflict, 6495 }; 6496 6497 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) 6498 { 6499 if (nfsd4_has_session(cstate)) 6500 return nfs_ok; 6501 if (seqid == so->so_seqid - 1) 6502 return nfserr_replay_me; 6503 if (seqid == so->so_seqid) 6504 return nfs_ok; 6505 return nfserr_bad_seqid; 6506 } 6507 6508 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions, 6509 struct nfsd_net *nn) 6510 { 6511 struct nfs4_client *found; 6512 6513 spin_lock(&nn->client_lock); 6514 found = find_confirmed_client(clid, sessions, nn); 6515 if (found) 6516 atomic_inc(&found->cl_rpc_users); 6517 spin_unlock(&nn->client_lock); 6518 return found; 6519 } 6520 6521 static __be32 set_client(clientid_t *clid, 6522 struct nfsd4_compound_state *cstate, 6523 struct nfsd_net *nn) 6524 { 6525 if (cstate->clp) { 6526 if (!same_clid(&cstate->clp->cl_clientid, clid)) 6527 return nfserr_stale_clientid; 6528 return nfs_ok; 6529 } 6530 if (STALE_CLIENTID(clid, nn)) 6531 return nfserr_stale_clientid; 6532 /* 6533 * We're in the 4.0 case (otherwise the SEQUENCE op would have 6534 * set cstate->clp), so session = false: 6535 */ 6536 cstate->clp = lookup_clientid(clid, false, nn); 6537 if (!cstate->clp) 6538 return nfserr_expired; 6539 return nfs_ok; 6540 } 6541 6542 __be32 6543 nfsd4_process_open1(struct nfsd4_compound_state *cstate, 6544 struct nfsd4_open *open, struct nfsd_net *nn) 6545 { 6546 clientid_t *clientid = &open->op_clientid; 6547 struct nfs4_client *clp = NULL; 6548 unsigned int strhashval; 6549 struct nfs4_openowner *oo = NULL; 6550 __be32 status; 6551 6552 /* 6553 * In case we need it later, after we've already created the 6554 * file and don't want to risk a further failure: 6555 */ 6556 open->op_file = nfsd4_alloc_file(); 6557 if (open->op_file == NULL) 6558 return nfserr_jukebox; 6559 6560 status = set_client(clientid, cstate, nn); 6561 if (status) 6562 return status; 6563 clp = cstate->clp; 6564 6565 strhashval = ownerstr_hashval(&open->op_owner); 6566 retry: 6567 oo = find_or_alloc_open_stateowner(strhashval, open, cstate); 6568 open->op_openowner = oo; 6569 if (!oo) 6570 return nfserr_jukebox; 6571 if (nfsd4_cstate_assign_replay(cstate, &oo->oo_owner) == -EAGAIN) { 6572 nfs4_put_stateowner(&oo->oo_owner); 6573 goto retry; 6574 } 6575 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); 6576 if (status) 6577 return status; 6578 6579 open->op_stp = nfs4_alloc_open_stateid(clp); 6580 if (!open->op_stp) 6581 return nfserr_jukebox; 6582 6583 if (nfsd4_has_session(cstate) && 6584 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { 6585 open->op_odstate = alloc_clnt_odstate(clp); 6586 if (!open->op_odstate) 6587 return nfserr_jukebox; 6588 } 6589 6590 return nfs_ok; 6591 } 6592 6593 static inline __be32 6594 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) 6595 { 6596 if (!(flags & RD_STATE) && deleg_is_read(dp->dl_type)) 6597 return nfserr_openmode; 6598 else 6599 return nfs_ok; 6600 } 6601 6602 static int share_access_to_flags(u32 share_access) 6603 { 6604 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; 6605 } 6606 6607 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, 6608 stateid_t *s) 6609 { 6610 struct nfs4_stid *ret; 6611 6612 ret = find_stateid_by_type(cl, s, SC_TYPE_DELEG, SC_STATUS_REVOKED); 6613 if (!ret) 6614 return NULL; 6615 return delegstateid(ret); 6616 } 6617 6618 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) 6619 { 6620 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || 6621 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; 6622 } 6623 6624 static __be32 6625 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, 6626 struct nfs4_delegation **dp) 6627 { 6628 int flags; 6629 __be32 status = nfserr_bad_stateid; 6630 struct nfs4_delegation *deleg; 6631 6632 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); 6633 if (deleg == NULL) 6634 goto out; 6635 if (deleg->dl_stid.sc_status & SC_STATUS_ADMIN_REVOKED) { 6636 nfs4_put_stid(&deleg->dl_stid); 6637 status = nfserr_admin_revoked; 6638 goto out; 6639 } 6640 if (deleg->dl_stid.sc_status & SC_STATUS_REVOKED) { 6641 nfs4_put_stid(&deleg->dl_stid); 6642 nfsd40_drop_revoked_stid(cl, &open->op_delegate_stateid); 6643 status = nfserr_deleg_revoked; 6644 goto out; 6645 } 6646 flags = share_access_to_flags(open->op_share_access); 6647 status = nfs4_check_delegmode(deleg, flags); 6648 if (status) { 6649 nfs4_put_stid(&deleg->dl_stid); 6650 goto out; 6651 } 6652 *dp = deleg; 6653 out: 6654 if (!nfsd4_is_deleg_cur(open)) 6655 return nfs_ok; 6656 if (status) 6657 return status; 6658 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; 6659 return nfs_ok; 6660 } 6661 6662 static inline int nfs4_access_to_access(u32 nfs4_access) 6663 { 6664 int flags = 0; 6665 6666 if (nfs4_access & NFS4_SHARE_ACCESS_READ) 6667 flags |= NFSD_MAY_READ; 6668 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) 6669 flags |= NFSD_MAY_WRITE; 6670 return flags; 6671 } 6672 6673 static inline __be32 6674 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, 6675 struct nfsd4_open *open) 6676 { 6677 struct iattr iattr = { 6678 .ia_valid = ATTR_SIZE, 6679 .ia_size = 0, 6680 }; 6681 struct nfsd_attrs attrs = { 6682 .na_iattr = &iattr, 6683 }; 6684 if (!open->op_truncate) 6685 return 0; 6686 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) 6687 return nfserr_inval; 6688 return nfsd_setattr(rqstp, fh, &attrs, NULL); 6689 } 6690 6691 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, 6692 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, 6693 struct nfsd4_open *open, bool new_stp) 6694 { 6695 struct nfsd_file *nf = NULL; 6696 __be32 status; 6697 int oflag = nfs4_access_to_omode(open->op_share_access); 6698 int access = nfs4_access_to_access(open->op_share_access); 6699 unsigned char old_access_bmap, old_deny_bmap; 6700 6701 spin_lock(&fp->fi_lock); 6702 6703 /* 6704 * Are we trying to set a deny mode that would conflict with 6705 * current access? 6706 */ 6707 status = nfs4_file_check_deny(fp, open->op_share_deny); 6708 if (status != nfs_ok) { 6709 if (status != nfserr_share_denied) { 6710 spin_unlock(&fp->fi_lock); 6711 goto out; 6712 } 6713 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp, 6714 stp, open->op_share_deny, false)) 6715 status = nfserr_jukebox; 6716 spin_unlock(&fp->fi_lock); 6717 goto out; 6718 } 6719 6720 /* set access to the file */ 6721 status = nfs4_file_get_access(fp, open->op_share_access); 6722 if (status != nfs_ok) { 6723 if (status != nfserr_share_denied) { 6724 spin_unlock(&fp->fi_lock); 6725 goto out; 6726 } 6727 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp, 6728 stp, open->op_share_access, true)) 6729 status = nfserr_jukebox; 6730 spin_unlock(&fp->fi_lock); 6731 goto out; 6732 } 6733 6734 /* Set access bits in stateid */ 6735 old_access_bmap = stp->st_access_bmap; 6736 set_access(open->op_share_access, stp); 6737 6738 /* Set new deny mask */ 6739 old_deny_bmap = stp->st_deny_bmap; 6740 set_deny(open->op_share_deny, stp); 6741 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); 6742 6743 if (!fp->fi_fds[oflag]) { 6744 spin_unlock(&fp->fi_lock); 6745 6746 status = nfsd_file_acquire_opened(rqstp, cur_fh, access, 6747 open->op_filp, &nf); 6748 if (status != nfs_ok) 6749 goto out_put_access; 6750 6751 spin_lock(&fp->fi_lock); 6752 if (!fp->fi_fds[oflag]) { 6753 fp->fi_fds[oflag] = nf; 6754 nf = NULL; 6755 } 6756 } 6757 spin_unlock(&fp->fi_lock); 6758 if (nf) 6759 nfsd_file_put(nf); 6760 6761 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode, 6762 access)); 6763 if (status) 6764 goto out_put_access; 6765 6766 status = nfsd4_truncate(rqstp, cur_fh, open); 6767 if (status) 6768 goto out_put_access; 6769 out: 6770 return status; 6771 out_put_access: 6772 stp->st_access_bmap = old_access_bmap; 6773 nfs4_file_put_access(fp, open->op_share_access); 6774 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); 6775 goto out; 6776 } 6777 6778 static __be32 6779 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, 6780 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, 6781 struct nfsd4_open *open) 6782 { 6783 __be32 status; 6784 unsigned char old_deny_bmap = stp->st_deny_bmap; 6785 6786 if (!test_access(open->op_share_access, stp)) 6787 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false); 6788 6789 /* test and set deny mode */ 6790 spin_lock(&fp->fi_lock); 6791 status = nfs4_file_check_deny(fp, open->op_share_deny); 6792 switch (status) { 6793 case nfs_ok: 6794 set_deny(open->op_share_deny, stp); 6795 fp->fi_share_deny |= 6796 (open->op_share_deny & NFS4_SHARE_DENY_BOTH); 6797 break; 6798 case nfserr_share_denied: 6799 if (nfs4_resolve_deny_conflicts_locked(fp, false, 6800 stp, open->op_share_deny, false)) 6801 status = nfserr_jukebox; 6802 break; 6803 } 6804 spin_unlock(&fp->fi_lock); 6805 6806 if (status != nfs_ok) 6807 return status; 6808 6809 status = nfsd4_truncate(rqstp, cur_fh, open); 6810 if (status != nfs_ok) 6811 reset_union_bmap_deny(old_deny_bmap, stp); 6812 return status; 6813 } 6814 6815 /* Should we give out recallable state?: */ 6816 static bool nfsd4_cb_channel_good(struct nfs4_client *clp) 6817 { 6818 if (clp->cl_cb_state == NFSD4_CB_UP) 6819 return true; 6820 /* 6821 * In the sessions case, since we don't have to establish a 6822 * separate connection for callbacks, we assume it's OK 6823 * until we hear otherwise: 6824 */ 6825 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; 6826 } 6827 6828 static unsigned int 6829 nfsd_notify_to_ignore(u32 notify) 6830 { 6831 unsigned int mask = 0; 6832 6833 if (notify & BIT(NOTIFY4_REMOVE_ENTRY)) 6834 mask |= FL_IGN_DIR_DELETE; 6835 if (notify & BIT(NOTIFY4_ADD_ENTRY)) 6836 mask |= FL_IGN_DIR_CREATE; 6837 if (notify & BIT(NOTIFY4_RENAME_ENTRY)) 6838 mask |= FL_IGN_DIR_RENAME; 6839 6840 return mask; 6841 } 6842 6843 static struct file_lease *nfs4_alloc_init_lease(struct nfs4_delegation *dp, u32 notify) 6844 { 6845 struct file_lease *fl; 6846 6847 fl = locks_alloc_lease(); 6848 if (!fl) 6849 return NULL; 6850 fl->fl_lmops = &nfsd_lease_mng_ops; 6851 fl->c.flc_flags = FL_DELEG | nfsd_notify_to_ignore(notify); 6852 fl->c.flc_type = deleg_is_read(dp->dl_type) ? F_RDLCK : F_WRLCK; 6853 fl->c.flc_owner = (fl_owner_t)dp; 6854 fl->c.flc_pid = current->tgid; 6855 fl->c.flc_file = rcu_dereference_protected(dp->dl_stid.sc_file->fi_deleg_file, 1)->nf_file; 6856 return fl; 6857 } 6858 6859 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp, 6860 struct nfs4_file *fp) 6861 { 6862 struct nfs4_ol_stateid *st; 6863 struct file *f = rcu_dereference_protected(fp->fi_deleg_file, 1)->nf_file; 6864 struct inode *ino = file_inode(f); 6865 int writes; 6866 6867 writes = atomic_read(&ino->i_writecount); 6868 if (!writes) 6869 return 0; 6870 /* 6871 * There could be multiple filehandles (hence multiple 6872 * nfs4_files) referencing this file, but that's not too 6873 * common; let's just give up in that case rather than 6874 * trying to go look up all the clients using that other 6875 * nfs4_file as well: 6876 */ 6877 if (fp->fi_aliased) 6878 return -EAGAIN; 6879 /* 6880 * If there's a close in progress, make sure that we see it 6881 * clear any fi_fds[] entries before we see it decrement 6882 * i_writecount: 6883 */ 6884 smp_mb__after_atomic(); 6885 6886 if (fp->fi_fds[O_WRONLY]) 6887 writes--; 6888 if (fp->fi_fds[O_RDWR]) 6889 writes--; 6890 if (writes > 0) 6891 return -EAGAIN; /* There may be non-NFSv4 writers */ 6892 /* 6893 * It's possible there are non-NFSv4 write opens in progress, 6894 * but if they haven't incremented i_writecount yet then they 6895 * also haven't called break lease yet; so, they'll break this 6896 * lease soon enough. So, all that's left to check for is NFSv4 6897 * opens: 6898 */ 6899 spin_lock(&fp->fi_lock); 6900 list_for_each_entry(st, &fp->fi_stateids, st_perfile) { 6901 if (st->st_openstp == NULL /* it's an open */ && 6902 access_permit_write(st) && 6903 st->st_stid.sc_client != clp) { 6904 spin_unlock(&fp->fi_lock); 6905 return -EAGAIN; 6906 } 6907 } 6908 spin_unlock(&fp->fi_lock); 6909 /* 6910 * There's a small chance that we could be racing with another 6911 * NFSv4 open. However, any open that hasn't added itself to 6912 * the fi_stateids list also hasn't called break_lease yet; so, 6913 * they'll break this lease soon enough. 6914 */ 6915 return 0; 6916 } 6917 6918 /* 6919 * It's possible that between opening the dentry and setting the delegation, 6920 * that it has been renamed or unlinked. Redo the lookup to verify that this 6921 * hasn't happened. 6922 */ 6923 static int 6924 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp, 6925 struct svc_fh *parent) 6926 { 6927 struct svc_export *exp; 6928 struct dentry *child; 6929 __be32 err; 6930 6931 err = nfsd_lookup_dentry(open->op_rqstp, parent, 6932 open->op_fname, open->op_fnamelen, 6933 &exp, &child); 6934 6935 if (err) 6936 return -EAGAIN; 6937 6938 exp_put(exp); 6939 dput(child); 6940 if (child != file_dentry(rcu_dereference_protected(fp->fi_deleg_file, 1)->nf_file)) 6941 return -EAGAIN; 6942 6943 return 0; 6944 } 6945 6946 /* 6947 * We avoid breaking delegations held by a client due to its own activity, but 6948 * clearing setuid/setgid bits on a write is an implicit activity and the client 6949 * may not notice and continue using the old mode. Avoid giving out a delegation 6950 * on setuid/setgid files when the client is requesting an open for write. 6951 */ 6952 static int 6953 nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf) 6954 { 6955 struct inode *inode = file_inode(nf->nf_file); 6956 6957 if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) && 6958 (inode->i_mode & (S_ISUID|S_ISGID))) 6959 return -EAGAIN; 6960 return 0; 6961 } 6962 6963 /* 6964 * Timestamp delegation was introduced in RFC7862. Runtime switch for disabling 6965 * this feature is /sys/kernel/debug/nfsd/delegated_timestamps. 6966 */ 6967 static bool nfsd4_want_deleg_timestamps(const struct nfsd4_open *open) 6968 { 6969 if (!nfsd_delegts_enabled) 6970 return false; 6971 return open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_DELEG_TIMESTAMPS; 6972 } 6973 6974 static void 6975 nfs4_delegation_record_grant_slot(struct nfs4_delegation *dp, 6976 const struct nfsd4_compound_state *cstate) 6977 { 6978 const struct nfsd4_sessionid *sid; 6979 6980 if (!cstate->session) 6981 return; 6982 sid = (struct nfsd4_sessionid *)cstate->session->se_sessionid.data; 6983 dp->dl_recall_grant.sessionid_seq = sid->sequence; 6984 dp->dl_recall_grant.slotid = cstate->slot->sl_index; 6985 dp->dl_recall_grant.seqid = cstate->slot->sl_seqid; 6986 dp->dl_recall_grant.valid = true; 6987 } 6988 6989 static struct nfs4_delegation * 6990 nfs4_set_delegation(struct nfsd4_open *open, 6991 const struct nfsd4_compound_state *cstate, 6992 struct nfs4_ol_stateid *stp, struct svc_fh *parent) 6993 { 6994 bool deleg_ts = nfsd4_want_deleg_timestamps(open); 6995 struct nfs4_client *clp = stp->st_stid.sc_client; 6996 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 6997 struct nfs4_file *fp = stp->st_stid.sc_file; 6998 struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate; 6999 struct nfs4_delegation *dp; 7000 struct nfsd_file *nf = NULL; 7001 struct file_lease *fl; 7002 int status = 0; 7003 u32 dl_type; 7004 7005 /* 7006 * The fi_had_conflict and nfs_get_existing_delegation checks 7007 * here are just optimizations; we'll need to recheck them at 7008 * the end: 7009 */ 7010 if (fp->fi_had_conflict) 7011 return ERR_PTR(-EAGAIN); 7012 7013 /* 7014 * Try for a write delegation first. RFC8881 section 10.4 says: 7015 * 7016 * "An OPEN_DELEGATE_WRITE delegation allows the client to handle, 7017 * on its own, all opens." 7018 * 7019 * Furthermore, section 9.1.2 says: 7020 * 7021 * "In the case of READ, the server may perform the corresponding 7022 * check on the access mode, or it may choose to allow READ for 7023 * OPEN4_SHARE_ACCESS_WRITE, to accommodate clients whose WRITE 7024 * implementation may unavoidably do reads (e.g., due to buffer 7025 * cache constraints)." 7026 * 7027 * We choose to offer a write delegation for OPEN with the 7028 * OPEN4_SHARE_ACCESS_WRITE access mode to accommodate such clients. 7029 */ 7030 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) { 7031 nf = find_writeable_file(fp); 7032 dl_type = deleg_ts ? OPEN_DELEGATE_WRITE_ATTRS_DELEG : OPEN_DELEGATE_WRITE; 7033 } 7034 7035 /* 7036 * If the file is being opened O_RDONLY or we couldn't get a O_RDWR 7037 * file for some reason, then try for a read delegation instead. 7038 */ 7039 if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) { 7040 nf = find_readable_file(fp); 7041 dl_type = deleg_ts ? OPEN_DELEGATE_READ_ATTRS_DELEG : OPEN_DELEGATE_READ; 7042 } 7043 7044 if (!nf) 7045 return ERR_PTR(-EAGAIN); 7046 7047 /* 7048 * File delegations and associated locks cannot be recovered if the 7049 * export is from an NFS proxy server. 7050 */ 7051 if (exportfs_cannot_lock(nf->nf_file->f_path.mnt->mnt_sb->s_export_op)) { 7052 nfsd_file_put(nf); 7053 return ERR_PTR(-EOPNOTSUPP); 7054 } 7055 7056 spin_lock(&nn->deleg_lock); 7057 spin_lock(&fp->fi_lock); 7058 if (nfs4_delegation_exists(clp, fp)) 7059 status = -EAGAIN; 7060 else if (nfsd4_verify_setuid_write(open, nf)) 7061 status = -EAGAIN; 7062 else if (!rcu_dereference_protected(fp->fi_deleg_file, 7063 lockdep_is_held(&fp->fi_lock))) { 7064 rcu_assign_pointer(fp->fi_deleg_file, nf); 7065 /* increment early to prevent fi_deleg_file from being 7066 * cleared */ 7067 fp->fi_delegees = 1; 7068 nf = NULL; 7069 } else 7070 fp->fi_delegees++; 7071 spin_unlock(&fp->fi_lock); 7072 spin_unlock(&nn->deleg_lock); 7073 if (nf) 7074 nfsd_file_put(nf); 7075 if (status) 7076 return ERR_PTR(status); 7077 7078 status = -ENOMEM; 7079 dp = alloc_init_deleg(clp, fp, odstate, dl_type); 7080 if (!dp) 7081 goto out_delegees; 7082 7083 /* 7084 * Record the granting slot before kernel_setlease() makes @dp 7085 * visible to lease breakers. A conflicting open can drive 7086 * CB_RECALL to completion from that point on. 7087 */ 7088 nfs4_delegation_record_grant_slot(dp, cstate); 7089 7090 if (stp->st_stid.sc_export) 7091 dp->dl_stid.sc_export = exp_get(stp->st_stid.sc_export); 7092 7093 fl = nfs4_alloc_init_lease(dp, 0); 7094 if (!fl) 7095 goto out_clnt_odstate; 7096 7097 status = kernel_setlease(rcu_dereference_protected(fp->fi_deleg_file, 1)->nf_file, 7098 fl->c.flc_type, &fl, NULL); 7099 if (fl) 7100 locks_free_lease(fl); 7101 if (status) 7102 goto out_clnt_odstate; 7103 7104 if (parent) { 7105 status = nfsd4_verify_deleg_dentry(open, fp, parent); 7106 if (status) 7107 goto out_unlock; 7108 } 7109 7110 status = nfsd4_check_conflicting_opens(clp, fp); 7111 if (status) 7112 goto out_unlock; 7113 7114 /* 7115 * Now that the deleg is set, check again to ensure that nothing 7116 * raced in and changed the mode while we weren't looking. 7117 */ 7118 status = nfsd4_verify_setuid_write(open, rcu_dereference_protected(fp->fi_deleg_file, 1)); 7119 if (status) 7120 goto out_unlock; 7121 7122 status = -EAGAIN; 7123 if (fp->fi_had_conflict) 7124 goto out_unlock; 7125 7126 spin_lock(&nn->deleg_lock); 7127 spin_lock(&clp->cl_lock); 7128 spin_lock(&fp->fi_lock); 7129 status = hash_delegation_locked(dp, fp); 7130 spin_unlock(&fp->fi_lock); 7131 spin_unlock(&clp->cl_lock); 7132 spin_unlock(&nn->deleg_lock); 7133 7134 if (status) 7135 goto out_unlock; 7136 7137 return dp; 7138 out_unlock: 7139 kernel_setlease(rcu_dereference_protected(fp->fi_deleg_file, 1)->nf_file, 7140 F_UNLCK, NULL, (void **)&dp); 7141 out_clnt_odstate: 7142 put_clnt_odstate(dp->dl_clnt_odstate); 7143 nfs4_put_stid(&dp->dl_stid); 7144 out_delegees: 7145 put_deleg_file(fp); 7146 return ERR_PTR(status); 7147 } 7148 7149 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) 7150 { 7151 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT; 7152 if (status == -EAGAIN) 7153 open->op_why_no_deleg = WND4_CONTENTION; 7154 else { 7155 open->op_why_no_deleg = WND4_RESOURCE; 7156 switch (open->op_deleg_want) { 7157 case OPEN4_SHARE_ACCESS_WANT_READ_DELEG: 7158 case OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG: 7159 case OPEN4_SHARE_ACCESS_WANT_ANY_DELEG: 7160 break; 7161 case OPEN4_SHARE_ACCESS_WANT_CANCEL: 7162 open->op_why_no_deleg = WND4_CANCELLED; 7163 break; 7164 case OPEN4_SHARE_ACCESS_WANT_NO_DELEG: 7165 WARN_ON_ONCE(1); 7166 } 7167 } 7168 } 7169 7170 static bool 7171 nfs4_delegation_stat(struct nfs4_delegation *dp, struct svc_fh *currentfh, 7172 struct kstat *stat) 7173 { 7174 struct nfsd_file *nf = find_writeable_file(dp->dl_stid.sc_file); 7175 struct path path; 7176 int rc; 7177 7178 if (!nf) 7179 return false; 7180 7181 path.mnt = currentfh->fh_export->ex_path.mnt; 7182 path.dentry = file_dentry(nf->nf_file); 7183 7184 rc = vfs_getattr(&path, stat, 7185 STATX_MODE | STATX_SIZE | STATX_ATIME | 7186 STATX_MTIME | STATX_CTIME | STATX_CHANGE_COOKIE, 7187 AT_STATX_SYNC_AS_STAT); 7188 7189 nfsd_file_put(nf); 7190 return rc == 0; 7191 } 7192 7193 /* 7194 * Add NFS4_SHARE_ACCESS_READ to the write delegation granted on OPEN 7195 * with NFS4_SHARE_ACCESS_WRITE by allocating separate nfsd_file and 7196 * struct file to be used for read with delegation stateid. 7197 * 7198 */ 7199 static bool 7200 nfsd4_add_rdaccess_to_wrdeleg(struct svc_rqst *rqstp, struct nfsd4_open *open, 7201 struct svc_fh *fh, struct nfs4_ol_stateid *stp) 7202 { 7203 struct nfs4_file *fp; 7204 struct nfsd_file *nf = NULL; 7205 7206 if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) == 7207 NFS4_SHARE_ACCESS_WRITE) { 7208 if (nfsd_file_acquire_opened(rqstp, fh, NFSD_MAY_READ, NULL, &nf)) 7209 return (false); 7210 fp = stp->st_stid.sc_file; 7211 spin_lock(&fp->fi_lock); 7212 if (!fp->fi_fds[O_RDONLY]) { 7213 __nfs4_file_get_access(fp, NFS4_SHARE_ACCESS_READ); 7214 fp->fi_fds[O_RDONLY] = nf; 7215 fp->fi_rdeleg_file = nfsd_file_get(fp->fi_fds[O_RDONLY]); 7216 nf = NULL; 7217 } 7218 spin_unlock(&fp->fi_lock); 7219 if (nf) 7220 nfsd_file_put(nf); 7221 } 7222 return true; 7223 } 7224 7225 /* 7226 * The Linux NFS server does not offer write delegations to NFSv4.0 7227 * clients in order to avoid conflicts between write delegations and 7228 * GETATTRs requesting CHANGE or SIZE attributes. 7229 * 7230 * With NFSv4.1 and later minorversions, the SEQUENCE operation that 7231 * begins each COMPOUND contains a client ID. Delegation recall can 7232 * be avoided when the server recognizes the client sending a 7233 * GETATTR also holds write delegation it conflicts with. 7234 * 7235 * However, the NFSv4.0 protocol does not enable a server to 7236 * determine that a GETATTR originated from the client holding the 7237 * conflicting delegation versus coming from some other client. Per 7238 * RFC 7530 Section 16.7.5, the server must recall or send a 7239 * CB_GETATTR even when the GETATTR originates from the client that 7240 * holds the conflicting delegation. 7241 * 7242 * An NFSv4.0 client can trigger a pathological situation if it 7243 * always sends a DELEGRETURN preceded by a conflicting GETATTR in 7244 * the same COMPOUND. COMPOUND execution will always stop at the 7245 * GETATTR and the DELEGRETURN will never get executed. The server 7246 * eventually revokes the delegation, which can result in loss of 7247 * open or lock state. 7248 */ 7249 static void 7250 nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open, 7251 struct nfs4_ol_stateid *stp, struct svc_fh *currentfh, 7252 struct svc_fh *fh) 7253 { 7254 struct nfsd4_compoundres *resp = rqstp->rq_resp; 7255 struct nfs4_openowner *oo = openowner(stp->st_stateowner); 7256 bool deleg_ts = nfsd4_want_deleg_timestamps(open); 7257 struct nfs4_client *clp = stp->st_stid.sc_client; 7258 struct svc_fh *parent = NULL; 7259 struct nfs4_delegation *dp; 7260 struct kstat stat; 7261 int status = 0; 7262 int cb_up; 7263 7264 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); 7265 open->op_recall = false; 7266 switch (open->op_claim_type) { 7267 case NFS4_OPEN_CLAIM_PREVIOUS: 7268 if (!cb_up) 7269 open->op_recall = true; 7270 break; 7271 case NFS4_OPEN_CLAIM_NULL: 7272 parent = currentfh; 7273 fallthrough; 7274 case NFS4_OPEN_CLAIM_FH: 7275 /* 7276 * Let's not give out any delegations till everyone's 7277 * had the chance to reclaim theirs, *and* until 7278 * NLM locks have all been reclaimed: 7279 */ 7280 if (locks_in_grace(clp->net)) 7281 goto out_no_deleg; 7282 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) 7283 goto out_no_deleg; 7284 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE && 7285 !clp->cl_minorversion) 7286 goto out_no_deleg; 7287 break; 7288 default: 7289 goto out_no_deleg; 7290 } 7291 dp = nfs4_set_delegation(open, &resp->cstate, stp, parent); 7292 if (IS_ERR(dp)) 7293 goto out_no_deleg; 7294 7295 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid)); 7296 7297 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) { 7298 struct file *f; 7299 7300 f = rcu_dereference_protected(dp->dl_stid.sc_file->fi_deleg_file, 1)->nf_file; 7301 if (!nfsd4_add_rdaccess_to_wrdeleg(rqstp, open, fh, stp) || 7302 !nfs4_delegation_stat(dp, currentfh, &stat)) { 7303 nfs4_put_stid(&dp->dl_stid); 7304 destroy_delegation(dp); 7305 goto out_no_deleg; 7306 } 7307 open->op_delegate_type = deleg_ts ? OPEN_DELEGATE_WRITE_ATTRS_DELEG : 7308 OPEN_DELEGATE_WRITE; 7309 dp->dl_cb_fattr.ncf_initial_cinfo = nfsd4_change_attribute(&stat); 7310 dp->dl_atime = stat.atime; 7311 dp->dl_ctime = stat.ctime; 7312 dp->dl_mtime = stat.mtime; 7313 spin_lock(&f->f_lock); 7314 if (deleg_ts) 7315 f->f_mode |= FMODE_NOCMTIME; 7316 spin_unlock(&f->f_lock); 7317 trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid); 7318 } else { 7319 open->op_delegate_type = deleg_ts && nfs4_delegation_stat(dp, currentfh, &stat) ? 7320 OPEN_DELEGATE_READ_ATTRS_DELEG : OPEN_DELEGATE_READ; 7321 dp->dl_atime = stat.atime; 7322 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid); 7323 } 7324 nfs4_put_stid(&dp->dl_stid); 7325 return; 7326 out_no_deleg: 7327 open->op_delegate_type = OPEN_DELEGATE_NONE; 7328 7329 /* 4.1 client asking for a delegation? */ 7330 if (open->op_deleg_want) 7331 nfsd4_open_deleg_none_ext(open, status); 7332 return; 7333 } 7334 7335 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, 7336 struct nfs4_delegation *dp) 7337 { 7338 if (deleg_is_write(dp->dl_type)) { 7339 if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_READ_DELEG) { 7340 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT; 7341 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; 7342 } else if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG) { 7343 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT; 7344 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; 7345 } 7346 } 7347 /* Otherwise the client must be confused wanting a delegation 7348 * it already has, therefore we don't return 7349 * OPEN_DELEGATE_NONE_EXT and reason. 7350 */ 7351 } 7352 7353 /* Are we returning only a delegation stateid? */ 7354 static bool open_xor_delegation(struct nfsd4_open *open) 7355 { 7356 if (!(open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_OPEN_XOR_DELEGATION)) 7357 return false; 7358 /* Did we actually get a delegation? */ 7359 if (!deleg_is_read(open->op_delegate_type) && !deleg_is_write(open->op_delegate_type)) 7360 return false; 7361 return true; 7362 } 7363 7364 /** 7365 * nfsd4_process_open2 - finish open processing 7366 * @rqstp: the RPC transaction being executed 7367 * @current_fh: NFSv4 COMPOUND's current filehandle 7368 * @open: OPEN arguments 7369 * 7370 * If successful, (1) truncate the file if open->op_truncate was 7371 * set, (2) set open->op_stateid, (3) set open->op_delegation. 7372 * 7373 * Returns %nfs_ok on success; otherwise an nfs4stat value in 7374 * network byte order is returned. 7375 */ 7376 __be32 7377 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) 7378 { 7379 struct nfsd4_compoundres *resp = rqstp->rq_resp; 7380 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; 7381 struct nfs4_file *fp = NULL; 7382 struct nfs4_ol_stateid *stp = NULL; 7383 struct nfs4_delegation *dp = NULL; 7384 __be32 status; 7385 bool new_stp = false; 7386 7387 /* 7388 * Lookup file; if found, lookup stateid and check open request, 7389 * and check for delegations in the process of being recalled. 7390 * If not found, create the nfs4_file struct 7391 */ 7392 fp = nfsd4_file_hash_insert(open->op_file, current_fh); 7393 if (unlikely(!fp)) 7394 return nfserr_jukebox; 7395 if (fp != open->op_file) { 7396 status = nfs4_check_deleg(cl, open, &dp); 7397 if (status) 7398 goto out; 7399 if (dp && nfsd4_is_deleg_cur(open) && 7400 (dp->dl_stid.sc_file != fp)) { 7401 /* 7402 * RFC8881 section 8.2.4 mandates the server to return 7403 * NFS4ERR_BAD_STATEID if the selected table entry does 7404 * not match the current filehandle. However returning 7405 * NFS4ERR_BAD_STATEID in the OPEN can cause the client 7406 * to repeatedly retry the operation with the same 7407 * stateid, since the stateid itself is valid. To avoid 7408 * this situation NFSD returns NFS4ERR_INVAL instead. 7409 */ 7410 status = nfserr_inval; 7411 goto out; 7412 } 7413 stp = nfsd4_find_and_lock_existing_open(fp, open); 7414 } else { 7415 open->op_file = NULL; 7416 status = nfserr_bad_stateid; 7417 if (nfsd4_is_deleg_cur(open)) 7418 goto out; 7419 } 7420 7421 if (!stp) { 7422 stp = init_open_stateid(fp, open); 7423 if (!stp) { 7424 status = nfserr_jukebox; 7425 goto out; 7426 } 7427 7428 if (!open->op_stp) { 7429 new_stp = true; 7430 stp->st_stid.sc_export = 7431 exp_get(current_fh->fh_export); 7432 } 7433 } 7434 7435 /* 7436 * OPEN the file, or upgrade an existing OPEN. 7437 * If truncate fails, the OPEN fails. 7438 * 7439 * stp is already locked. 7440 */ 7441 if (!new_stp) { 7442 /* Stateid was found, this is an OPEN upgrade */ 7443 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); 7444 if (status) { 7445 mutex_unlock(&stp->st_mutex); 7446 goto out; 7447 } 7448 } else { 7449 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true); 7450 if (status) { 7451 release_open_stateid(stp); 7452 mutex_unlock(&stp->st_mutex); 7453 goto out; 7454 } 7455 7456 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, 7457 open->op_odstate); 7458 if (stp->st_clnt_odstate == open->op_odstate) 7459 open->op_odstate = NULL; 7460 } 7461 7462 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); 7463 mutex_unlock(&stp->st_mutex); 7464 7465 if (nfsd4_has_session(&resp->cstate)) { 7466 if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_NO_DELEG) { 7467 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT; 7468 open->op_why_no_deleg = WND4_NOT_WANTED; 7469 goto nodeleg; 7470 } 7471 } 7472 7473 /* 7474 * Attempt to hand out a delegation. No error return, because the 7475 * OPEN succeeds even if we fail. 7476 */ 7477 nfs4_open_delegation(rqstp, open, stp, 7478 &resp->cstate.current_fh, current_fh); 7479 7480 /* 7481 * If there is an existing open stateid, it must be updated and 7482 * returned. Only respect WANT_OPEN_XOR_DELEGATION when a new 7483 * open stateid would have to be created. 7484 */ 7485 if (new_stp && open_xor_delegation(open)) { 7486 memcpy(&open->op_stateid, &zero_stateid, sizeof(open->op_stateid)); 7487 open->op_rflags |= OPEN4_RESULT_NO_OPEN_STATEID; 7488 release_open_stateid(stp); 7489 } 7490 nodeleg: 7491 status = nfs_ok; 7492 trace_nfsd_open(&stp->st_stid.sc_stateid); 7493 out: 7494 /* 4.1 client trying to upgrade/downgrade delegation? */ 7495 if (open->op_delegate_type == OPEN_DELEGATE_NONE && dp && 7496 open->op_deleg_want) 7497 nfsd4_deleg_xgrade_none_ext(open, dp); 7498 7499 if (fp) 7500 put_nfs4_file(fp); 7501 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) 7502 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; 7503 /* 7504 * To finish the open response, we just need to set the rflags. 7505 */ 7506 open->op_rflags |= NFS4_OPEN_RESULT_LOCKTYPE_POSIX; 7507 if (nfsd4_has_session(&resp->cstate)) 7508 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK; 7509 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED)) 7510 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; 7511 7512 if (dp) 7513 nfs4_put_stid(&dp->dl_stid); 7514 if (stp) 7515 nfs4_put_stid(&stp->st_stid); 7516 7517 return status; 7518 } 7519 7520 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, 7521 struct nfsd4_open *open) 7522 { 7523 if (open->op_openowner) 7524 nfs4_put_stateowner(&open->op_openowner->oo_owner); 7525 if (open->op_file) 7526 kmem_cache_free(file_slab, open->op_file); 7527 if (open->op_stp) 7528 nfs4_put_stid(&open->op_stp->st_stid); 7529 if (open->op_odstate) 7530 kmem_cache_free(odstate_slab, open->op_odstate); 7531 } 7532 7533 __be32 7534 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 7535 union nfsd4_op_u *u) 7536 { 7537 clientid_t *clid = &u->renew; 7538 struct nfs4_client *clp; 7539 __be32 status; 7540 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 7541 7542 trace_nfsd_clid_renew(clid); 7543 status = set_client(clid, cstate, nn); 7544 if (status) 7545 return status; 7546 clp = cstate->clp; 7547 if (!list_empty(&clp->cl_delegations) 7548 && clp->cl_cb_state != NFSD4_CB_UP) 7549 return nfserr_cb_path_down; 7550 return nfs_ok; 7551 } 7552 7553 static void 7554 nfsd4_end_grace(struct nfsd_net *nn) 7555 { 7556 /* 7557 * nfsd4_end_grace() can be entered concurrently from the 7558 * laundromat workqueue and from an nfsd compound thread 7559 * handling RECLAIM_COMPLETE. Without serialization, both 7560 * callers can observe NFSD_NET_GRACE_ENDED clear and proceed 7561 * into nfsd4_record_grace_done(). For tracking ops whose 7562 * grace_done drains reclaim_str_hashtbl, that results in 7563 * list corruption and a double free of every 7564 * nfs4_client_reclaim entry. Use an atomic test-and-set so 7565 * exactly one caller proceeds. 7566 */ 7567 if (test_and_set_bit(NFSD_NET_GRACE_ENDED, &nn->flags)) 7568 return; 7569 7570 trace_nfsd_grace_complete(nn); 7571 /* 7572 * If the server goes down again right now, an NFSv4 7573 * client will still be allowed to reclaim after it comes back up, 7574 * even if it hasn't yet had a chance to reclaim state this time. 7575 * 7576 */ 7577 nfsd4_record_grace_done(nn); 7578 /* 7579 * At this point, NFSv4 clients can still reclaim. But if the 7580 * server crashes, any that have not yet reclaimed will be out 7581 * of luck on the next boot. 7582 * 7583 * (NFSv4.1+ clients are considered to have reclaimed once they 7584 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to 7585 * have reclaimed after their first OPEN.) 7586 */ 7587 locks_end_grace(&nn->nfsd4_manager); 7588 /* 7589 * At this point, and once lockd and/or any other containers 7590 * exit their grace period, further reclaims will fail and 7591 * regular locking can resume. 7592 */ 7593 } 7594 7595 /** 7596 * nfsd4_force_end_grace - forcibly end the NFSv4 grace period 7597 * @nn: network namespace for the server instance to be updated 7598 * 7599 * Forces bypass of normal grace period completion, then schedules 7600 * the laundromat to end the grace period immediately. Does not wait 7601 * for the grace period to fully terminate before returning. 7602 * 7603 * Return values: 7604 * %true: Grace termination schedule 7605 * %false: No action was taken 7606 */ 7607 bool nfsd4_force_end_grace(struct nfsd_net *nn) 7608 { 7609 if (!nn->client_tracking_ops) 7610 return false; 7611 if (test_bit(NFSD_NET_GRACE_ENDED, &nn->flags)) 7612 return false; 7613 /* laundromat_work must be initialised now, though it might be disabled */ 7614 set_bit(NFSD_NET_GRACE_END_FORCED, &nn->flags); 7615 /* mod_delayed_work() doesn't queue work after 7616 * nfs4_state_shutdown_net() has called disable_delayed_work_sync() 7617 */ 7618 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); 7619 return true; 7620 } 7621 7622 /* 7623 * If we've waited a lease period but there are still clients trying to 7624 * reclaim, wait a little longer to give them a chance to finish. 7625 */ 7626 static bool clients_still_reclaiming(struct nfsd_net *nn) 7627 { 7628 time64_t double_grace_period_end = nn->boot_time_bt + 7629 2 * nn->nfsd4_lease; 7630 7631 if (test_bit(NFSD_NET_GRACE_END_FORCED, &nn->flags)) 7632 return false; 7633 if (test_bit(NFSD_NET_TRACK_RECLAIM_COMPLETES, &nn->flags)) { 7634 int size; 7635 7636 down_read(&nn->reclaim_str_hashtbl_lock); 7637 size = nn->reclaim_str_hashtbl_size; 7638 up_read(&nn->reclaim_str_hashtbl_lock); 7639 if (atomic_read(&nn->nr_reclaim_complete) == size) 7640 return false; 7641 } 7642 if (!test_and_clear_bit(NFSD_NET_SOMEBODY_RECLAIMED, &nn->flags)) 7643 return false; 7644 /* 7645 * If we've given them *two* lease times to reclaim, and they're 7646 * still not done, give up: 7647 */ 7648 if (ktime_get_boottime_seconds() > double_grace_period_end) 7649 return false; 7650 return true; 7651 } 7652 7653 struct laundry_time { 7654 time64_t cutoff; 7655 time64_t new_timeo; 7656 }; 7657 7658 static bool state_expired(struct laundry_time *lt, time64_t last_refresh) 7659 { 7660 time64_t time_remaining; 7661 7662 if (last_refresh < lt->cutoff) 7663 return true; 7664 time_remaining = last_refresh - lt->cutoff; 7665 lt->new_timeo = min(lt->new_timeo, time_remaining); 7666 return false; 7667 } 7668 7669 #ifdef CONFIG_NFSD_V4_2_INTER_SSC 7670 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn) 7671 { 7672 spin_lock_init(&nn->nfsd_ssc_lock); 7673 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list); 7674 init_waitqueue_head(&nn->nfsd_ssc_waitq); 7675 } 7676 7677 /* 7678 * This is called when nfsd is being shutdown, after all inter_ssc 7679 * cleanup were done, to destroy the ssc delayed unmount list. 7680 */ 7681 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn) 7682 { 7683 struct nfsd4_ssc_umount_item *ni = NULL; 7684 struct nfsd4_ssc_umount_item *tmp; 7685 7686 spin_lock(&nn->nfsd_ssc_lock); 7687 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) { 7688 list_del(&ni->nsui_list); 7689 spin_unlock(&nn->nfsd_ssc_lock); 7690 mntput(ni->nsui_vfsmount); 7691 kfree(ni); 7692 spin_lock(&nn->nfsd_ssc_lock); 7693 } 7694 spin_unlock(&nn->nfsd_ssc_lock); 7695 } 7696 7697 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn) 7698 { 7699 bool do_wakeup = false; 7700 struct nfsd4_ssc_umount_item *ni; 7701 7702 restart: 7703 spin_lock(&nn->nfsd_ssc_lock); 7704 list_for_each_entry(ni, &nn->nfsd_ssc_mount_list, nsui_list) { 7705 if (!time_after(jiffies, ni->nsui_expire)) 7706 break; 7707 if (refcount_read(&ni->nsui_refcnt) > 1) 7708 continue; 7709 7710 /* Prevent concurrent setup during unmount */ 7711 ni->nsui_busy = true; 7712 spin_unlock(&nn->nfsd_ssc_lock); 7713 mntput(ni->nsui_vfsmount); 7714 spin_lock(&nn->nfsd_ssc_lock); 7715 7716 /* Force concurrent scanners to restart */ 7717 list_del(&ni->nsui_list); 7718 kfree(ni); 7719 7720 /* wakeup ssc_connect waiters */ 7721 do_wakeup = true; 7722 /* 7723 * Concurrent nfsd4_ssc_cancel_dul() can free any item 7724 * on the list under nfsd_ssc_lock while mntput() runs 7725 * above. Restart from the head; the list is short and 7726 * the expire worker is periodic, so this is cheap. 7727 */ 7728 spin_unlock(&nn->nfsd_ssc_lock); 7729 goto restart; 7730 } 7731 if (do_wakeup) 7732 wake_up_all(&nn->nfsd_ssc_waitq); 7733 spin_unlock(&nn->nfsd_ssc_lock); 7734 } 7735 #endif 7736 7737 /* Check if any lock belonging to this lockowner has any blockers */ 7738 static bool 7739 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo) 7740 { 7741 struct file_lock_context *ctx; 7742 struct nfs4_ol_stateid *stp; 7743 struct nfs4_file *nf; 7744 7745 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) { 7746 nf = stp->st_stid.sc_file; 7747 ctx = locks_inode_context(nf->fi_inode); 7748 if (!ctx) 7749 continue; 7750 if (locks_owner_has_blockers(ctx, lo)) 7751 return true; 7752 } 7753 return false; 7754 } 7755 7756 static bool 7757 nfs4_anylock_blockers(struct nfs4_client *clp) 7758 { 7759 int i; 7760 struct nfs4_stateowner *so; 7761 struct nfs4_lockowner *lo; 7762 7763 if (atomic_read(&clp->cl_delegs_in_recall)) 7764 return true; 7765 spin_lock(&clp->cl_lock); 7766 for (i = 0; i < OWNER_HASH_SIZE; i++) { 7767 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i], 7768 so_strhash) { 7769 if (so->so_is_open_owner) 7770 continue; 7771 lo = lockowner(so); 7772 if (nfs4_lockowner_has_blockers(lo)) { 7773 spin_unlock(&clp->cl_lock); 7774 return true; 7775 } 7776 } 7777 } 7778 spin_unlock(&clp->cl_lock); 7779 return false; 7780 } 7781 7782 static void 7783 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist, 7784 struct laundry_time *lt) 7785 { 7786 unsigned int maxreap, reapcnt = 0; 7787 struct list_head *pos, *next; 7788 struct nfs4_client *clp; 7789 7790 maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ? 7791 NFSD_CLIENT_MAX_TRIM_PER_RUN : 0; 7792 INIT_LIST_HEAD(reaplist); 7793 spin_lock(&nn->client_lock); 7794 list_for_each_safe(pos, next, &nn->client_lru) { 7795 clp = list_entry(pos, struct nfs4_client, cl_lru); 7796 if (clp->cl_state == NFSD4_EXPIRABLE) 7797 goto exp_client; 7798 if (!state_expired(lt, clp->cl_time)) 7799 break; 7800 if (!atomic_read(&clp->cl_rpc_users)) { 7801 if (clp->cl_state == NFSD4_ACTIVE) 7802 atomic_inc(&nn->nfsd_courtesy_clients); 7803 clp->cl_state = NFSD4_COURTESY; 7804 } 7805 if (!client_has_state(clp)) 7806 goto exp_client; 7807 if (!nfs4_anylock_blockers(clp)) 7808 if (reapcnt >= maxreap) 7809 continue; 7810 exp_client: 7811 if (!mark_client_expired_locked(clp)) { 7812 list_add(&clp->cl_lru, reaplist); 7813 reapcnt++; 7814 } 7815 } 7816 spin_unlock(&nn->client_lock); 7817 } 7818 7819 static void 7820 nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn, 7821 struct list_head *reaplist) 7822 { 7823 unsigned int maxreap = 0, reapcnt = 0; 7824 struct list_head *pos, *next; 7825 struct nfs4_client *clp; 7826 7827 maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN; 7828 INIT_LIST_HEAD(reaplist); 7829 7830 spin_lock(&nn->client_lock); 7831 list_for_each_safe(pos, next, &nn->client_lru) { 7832 clp = list_entry(pos, struct nfs4_client, cl_lru); 7833 if (clp->cl_state == NFSD4_ACTIVE) 7834 break; 7835 if (reapcnt >= maxreap) 7836 break; 7837 if (!mark_client_expired_locked(clp)) { 7838 list_add(&clp->cl_lru, reaplist); 7839 reapcnt++; 7840 } 7841 } 7842 spin_unlock(&nn->client_lock); 7843 } 7844 7845 static void 7846 nfs4_process_client_reaplist(struct list_head *reaplist) 7847 { 7848 struct list_head *pos, *next; 7849 struct nfs4_client *clp; 7850 7851 list_for_each_safe(pos, next, reaplist) { 7852 clp = list_entry(pos, struct nfs4_client, cl_lru); 7853 trace_nfsd_clid_purged(&clp->cl_clientid); 7854 list_del_init(&clp->cl_lru); 7855 expire_client(clp); 7856 } 7857 } 7858 7859 static void nfs40_clean_admin_revoked(struct nfsd_net *nn, 7860 struct laundry_time *lt) 7861 { 7862 struct nfs4_client *clp; 7863 7864 spin_lock(&nn->client_lock); 7865 if (nn->nfs40_last_revoke == 0 || 7866 nn->nfs40_last_revoke > lt->cutoff) { 7867 spin_unlock(&nn->client_lock); 7868 return; 7869 } 7870 nn->nfs40_last_revoke = 0; 7871 7872 retry: 7873 list_for_each_entry(clp, &nn->client_lru, cl_lru) { 7874 unsigned long id, tmp; 7875 struct nfs4_stid *stid; 7876 7877 if (atomic_read(&clp->cl_admin_revoked) == 0) 7878 continue; 7879 if (is_client_expired(clp)) 7880 continue; 7881 7882 spin_lock(&clp->cl_lock); 7883 idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id) 7884 if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) { 7885 refcount_inc(&stid->sc_count); 7886 atomic_inc(&clp->cl_rpc_users); 7887 spin_unlock(&nn->client_lock); 7888 /* this function drops ->cl_lock */ 7889 nfsd4_drop_revoked_stid(stid); 7890 nfs4_put_stid(stid); 7891 spin_lock(&nn->client_lock); 7892 put_client_no_renew_locked(clp); 7893 goto retry; 7894 } 7895 spin_unlock(&clp->cl_lock); 7896 } 7897 spin_unlock(&nn->client_lock); 7898 } 7899 7900 static time64_t 7901 nfs4_laundromat(struct nfsd_net *nn) 7902 { 7903 struct nfs4_openowner *oo; 7904 struct nfs4_delegation *dp; 7905 struct nfs4_ol_stateid *stp; 7906 struct nfsd4_blocked_lock *nbl; 7907 struct list_head *pos, *next, reaplist; 7908 struct laundry_time lt = { 7909 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease, 7910 .new_timeo = nn->nfsd4_lease 7911 }; 7912 struct nfs4_cpntf_state *cps; 7913 struct nfs4_client *clp; 7914 copy_stateid_t *cps_t; 7915 long held, host, n; 7916 int i; 7917 7918 if (clients_still_reclaiming(nn)) { 7919 lt.new_timeo = 0; 7920 goto out; 7921 } 7922 nfsd4_end_grace(nn); 7923 7924 spin_lock(&nn->s2s_cp_lock); 7925 /* s2s_cp_stateids holds only COPY_NOTIFY stateids */ 7926 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) { 7927 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid); 7928 if (state_expired(<, cps->cpntf_time)) 7929 revoke_cpntf_state_locked(nn, cps); 7930 } 7931 spin_unlock(&nn->s2s_cp_lock); 7932 nfsd4_async_copy_reaper(nn); 7933 nfs4_get_client_reaplist(nn, &reaplist, <); 7934 nfs4_process_client_reaplist(&reaplist); 7935 7936 nfs40_clean_admin_revoked(nn, <); 7937 7938 spin_lock(&nn->deleg_lock); 7939 list_for_each_safe(pos, next, &nn->del_recall_lru) { 7940 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); 7941 if (!state_expired(<, dp->dl_time)) 7942 break; 7943 clp = dp->dl_stid.sc_client; 7944 spin_lock(&nn->client_lock); 7945 if (is_client_expired(clp)) { 7946 spin_unlock(&nn->client_lock); 7947 continue; 7948 } 7949 /* 7950 * Pin without reviving: get_client_locked() would 7951 * flip a courtesy client back to NFSD4_ACTIVE. 7952 */ 7953 atomic_inc(&clp->cl_rpc_users); 7954 spin_unlock(&nn->client_lock); 7955 refcount_inc(&dp->dl_stid.sc_count); 7956 unhash_delegation_locked(dp, SC_STATUS_REVOKED); 7957 list_add(&dp->dl_recall_lru, &reaplist); 7958 } 7959 spin_unlock(&nn->deleg_lock); 7960 while (!list_empty(&reaplist)) { 7961 dp = list_first_entry(&reaplist, struct nfs4_delegation, 7962 dl_recall_lru); 7963 clp = dp->dl_stid.sc_client; 7964 list_del_init(&dp->dl_recall_lru); 7965 revoke_delegation(dp); 7966 put_client_no_renew(clp); 7967 } 7968 7969 spin_lock(&nn->client_lock); 7970 while (!list_empty(&nn->close_lru)) { 7971 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, 7972 oo_close_lru); 7973 if (!state_expired(<, oo->oo_time)) 7974 break; 7975 list_del_init(&oo->oo_close_lru); 7976 clp = oo->oo_owner.so_client; 7977 if (is_client_expired(clp)) 7978 continue; 7979 stp = oo->oo_last_closed_stid; 7980 oo->oo_last_closed_stid = NULL; 7981 atomic_inc(&clp->cl_rpc_users); 7982 spin_unlock(&nn->client_lock); 7983 nfs4_put_stid(&stp->st_stid); 7984 spin_lock(&nn->client_lock); 7985 put_client_no_renew_locked(clp); 7986 } 7987 spin_unlock(&nn->client_lock); 7988 7989 /* 7990 * It's possible for a client to try and acquire an already held lock 7991 * that is being held for a long time, and then lose interest in it. 7992 * So, we clean out any un-revisited request after a lease period 7993 * under the assumption that the client is no longer interested. 7994 * 7995 * RFC5661, sec. 9.6 states that the client must not rely on getting 7996 * notifications and must continue to poll for locks, even when the 7997 * server supports them. Thus this shouldn't lead to clients blocking 7998 * indefinitely once the lock does become free. 7999 */ 8000 BUG_ON(!list_empty(&reaplist)); 8001 spin_lock(&nn->client_lock); 8002 spin_lock(&nn->blocked_locks_lock); 8003 list_for_each_safe(pos, next, &nn->blocked_locks_lru) { 8004 nbl = list_entry(pos, struct nfsd4_blocked_lock, nbl_lru); 8005 if (!state_expired(<, nbl->nbl_time)) 8006 break; 8007 clp = nbl_client(nbl); 8008 if (is_client_expired(clp)) 8009 continue; 8010 atomic_inc(&clp->cl_rpc_users); 8011 list_move(&nbl->nbl_lru, &reaplist); 8012 list_del_init(&nbl->nbl_list); 8013 } 8014 spin_unlock(&nn->blocked_locks_lock); 8015 spin_unlock(&nn->client_lock); 8016 8017 while (!list_empty(&reaplist)) { 8018 nbl = list_first_entry(&reaplist, 8019 struct nfsd4_blocked_lock, nbl_lru); 8020 clp = nbl_client(nbl); 8021 list_del_init(&nbl->nbl_lru); 8022 free_blocked_lock(nbl); 8023 put_client_no_renew(clp); 8024 } 8025 #ifdef CONFIG_NFSD_V4_2_INTER_SSC 8026 /* service the server-to-server copy delayed unmount list */ 8027 nfsd4_ssc_expire_umount(nn); 8028 #endif 8029 /* 8030 * set_max_delegations() computes a zero max_delegations on a 8031 * server with very little memory. @host is a divisor below. 8032 */ 8033 host = atomic_long_read(&num_delegations); 8034 if (host && host >= max_delegations) { 8035 /* 8036 * max_delegations bounds the host, but the laundromat 8037 * runs once per network namespace. Requesting the whole 8038 * overage in each would multiply the request, so take 8039 * only this namespace's share. 8040 */ 8041 held = atomic_long_read(&nn->nfsd_delegations); 8042 n = host - max_delegations + 1; 8043 deleg_reaper(nn, DIV64_U64_ROUND_UP((u64)n * held, host)); 8044 } 8045 out: 8046 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); 8047 } 8048 8049 static void laundromat_main(struct work_struct *); 8050 8051 static void 8052 laundromat_main(struct work_struct *laundry) 8053 { 8054 time64_t t; 8055 struct delayed_work *dwork = to_delayed_work(laundry); 8056 struct nfsd_net *nn = container_of(dwork, struct nfsd_net, 8057 laundromat_work); 8058 8059 t = nfs4_laundromat(nn); 8060 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); 8061 } 8062 8063 static void 8064 courtesy_client_reaper(struct nfsd_net *nn) 8065 { 8066 struct list_head reaplist; 8067 8068 nfs4_get_courtesy_client_reaplist(nn, &reaplist); 8069 nfs4_process_client_reaplist(&reaplist); 8070 } 8071 8072 /* The two passes in deleg_reaper() must agree on which clients are asked. */ 8073 static bool 8074 deleg_reaper_eligible(const struct nfs4_client *clp, time64_t now) 8075 { 8076 if (clp->cl_minorversion == 0) 8077 return false; 8078 if (clp->cl_state != NFSD4_ACTIVE) 8079 return false; 8080 if (atomic_read(&clp->cl_delegs_in_recall)) 8081 return false; 8082 if (test_bit(NFSD4_CALLBACK_RUNNING, &clp->cl_ra->ra_cb.cb_flags)) 8083 return false; 8084 if (now - clp->cl_ra_time < NFSD_RECALL_ANY_COOLDOWN_SECS) 8085 return false; 8086 if (clp->cl_cb_state != NFSD4_CB_UP) 8087 return false; 8088 return true; 8089 } 8090 8091 static void 8092 deleg_reaper(struct nfsd_net *nn, unsigned long backlog) 8093 { 8094 struct list_head *pos, *next; 8095 struct nfs4_client *clp; 8096 unsigned long remaining, share, total; 8097 unsigned int count; 8098 time64_t now; 8099 8100 /* 8101 * Recalling a delegation before it is needed costs the client 8102 * an OPEN when it next touches the file. Leave 8103 * nfsd_last_recall_any unstamped so the next sweep is not 8104 * delayed. 8105 */ 8106 if (!backlog) 8107 return; 8108 now = ktime_get_boottime_seconds(); 8109 8110 spin_lock(&nn->client_lock); 8111 8112 /* 8113 * Only the clients this sweep asks contribute to the 8114 * apportionment. Dividing the request among holders that are 8115 * skipped under-serves it, and the shortfall goes nowhere: 8116 * nfsd4_deleg_shrinker_worker() has already cleared 8117 * nfsd_deleg_backlog. 8118 */ 8119 total = 0; 8120 list_for_each(pos, &nn->client_lru) { 8121 clp = list_entry(pos, struct nfs4_client, cl_lru); 8122 8123 if (!deleg_reaper_eligible(clp, now)) 8124 continue; 8125 /* 8126 * This read races with hash_delegation_locked() and 8127 * unhash_delegation_locked() on other CPUs. A stale 8128 * count only skews the keep value; the next 8129 * laundromat pass sees a more current one. 8130 */ 8131 total += data_race(READ_ONCE(clp->cl_deleg_count)); 8132 } 8133 if (!total) 8134 goto out; 8135 8136 /* 8137 * Reclaim asks in batches and is not bound by what the count 8138 * callback reported, so the backlog can exceed what these 8139 * clients hold. Cap it to keep each share within the client's 8140 * own count. 8141 */ 8142 backlog = min(backlog, total); 8143 remaining = backlog; 8144 8145 list_for_each_safe(pos, next, &nn->client_lru) { 8146 clp = list_entry(pos, struct nfs4_client, cl_lru); 8147 8148 if (!deleg_reaper_eligible(clp, now)) 8149 continue; 8150 count = data_race(READ_ONCE(clp->cl_deleg_count)); 8151 if (!count) 8152 continue; 8153 if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &clp->cl_ra->ra_cb.cb_flags)) 8154 continue; 8155 8156 /* release in nfsd4_cb_recall_any_release */ 8157 kref_get(&clp->cl_nfsdfs.cl_ref); 8158 clp->cl_ra_time = now; 8159 /* 8160 * Rounding up guarantees every holder gives up at least 8161 * one. The round-up can overshoot @backlog, so stop 8162 * once the request is met. client_lru is ordered by 8163 * last renewal, so the least active clients are asked 8164 * first. 8165 */ 8166 share = DIV64_U64_ROUND_UP((u64)backlog * count, total); 8167 share = min(share, remaining); 8168 remaining -= share; 8169 clp->cl_ra->ra_keep = count - share; 8170 clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG) | 8171 BIT(RCA4_TYPE_MASK_WDATA_DLG) | 8172 BIT(RCA4_TYPE_MASK_DIR_DLG); 8173 trace_nfsd_cb_recall_any(clp->cl_ra); 8174 nfsd4_run_cb(&clp->cl_ra->ra_cb); 8175 if (!remaining) 8176 break; 8177 } 8178 out: 8179 spin_unlock(&nn->client_lock); 8180 8181 /* 8182 * Stamp the sweep even when no recall went out. A sweep that 8183 * found nothing eligible finds nothing on an immediate retry. 8184 */ 8185 WRITE_ONCE(nn->nfsd_last_recall_any, now); 8186 } 8187 8188 static void 8189 nfsd4_courtesy_shrinker_worker(struct work_struct *work) 8190 { 8191 struct nfsd_net *nn = container_of(work, struct nfsd_net, 8192 nfsd_courtesy_work); 8193 long backlog; 8194 8195 /* 8196 * Retire only the requests sampled here, so that requests 8197 * arriving while the reaper runs are still discounted by 8198 * nfsd4_courtesy_shrinker_count(). 8199 */ 8200 backlog = atomic_long_read(&nn->nfsd_shrink_backlog); 8201 courtesy_client_reaper(nn); 8202 atomic_long_sub(backlog, &nn->nfsd_shrink_backlog); 8203 } 8204 8205 static void 8206 nfsd4_deleg_shrinker_worker(struct work_struct *work) 8207 { 8208 struct nfsd_net *nn = container_of(work, struct nfsd_net, 8209 nfsd_deleg_work); 8210 8211 deleg_reaper(nn, atomic_long_xchg(&nn->nfsd_deleg_backlog, 0)); 8212 } 8213 8214 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp) 8215 { 8216 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) 8217 return nfserr_bad_stateid; 8218 return nfs_ok; 8219 } 8220 8221 static 8222 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) 8223 { 8224 __be32 status = nfserr_openmode; 8225 8226 /* For lock stateid's, we test the parent open, not the lock: */ 8227 if (stp->st_openstp) 8228 stp = stp->st_openstp; 8229 if ((flags & WR_STATE) && !access_permit_write(stp)) 8230 goto out; 8231 if ((flags & RD_STATE) && !access_permit_read(stp)) 8232 goto out; 8233 status = nfs_ok; 8234 out: 8235 return status; 8236 } 8237 8238 static inline __be32 8239 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags) 8240 { 8241 if (ONE_STATEID(stateid) && (flags & RD_STATE)) 8242 return nfs_ok; 8243 else if (opens_in_grace(net)) { 8244 /* Answer in remaining cases depends on existence of 8245 * conflicting state; so we must wait out the grace period. */ 8246 return nfserr_grace; 8247 } else if (flags & WR_STATE) 8248 return nfs4_share_conflict(current_fh, 8249 NFS4_SHARE_DENY_WRITE); 8250 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ 8251 return nfs4_share_conflict(current_fh, 8252 NFS4_SHARE_DENY_READ); 8253 } 8254 8255 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session) 8256 { 8257 /* 8258 * When sessions are used the stateid generation number is ignored 8259 * when it is zero. 8260 */ 8261 if (has_session && in->si_generation == 0) 8262 return nfs_ok; 8263 8264 if (in->si_generation == ref->si_generation) 8265 return nfs_ok; 8266 8267 /* If the client sends us a stateid from the future, it's buggy: */ 8268 if (nfsd4_stateid_generation_after(in, ref)) 8269 return nfserr_bad_stateid; 8270 /* 8271 * However, we could see a stateid from the past, even from a 8272 * non-buggy client. For example, if the client sends a lock 8273 * while some IO is outstanding, the lock may bump si_generation 8274 * while the IO is still in flight. The client could avoid that 8275 * situation by waiting for responses on all the IO requests, 8276 * but better performance may result in retrying IO that 8277 * receives an old_stateid error if requests are rarely 8278 * reordered in flight: 8279 */ 8280 return nfserr_old_stateid; 8281 } 8282 8283 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session) 8284 { 8285 __be32 ret; 8286 8287 spin_lock(&s->sc_lock); 8288 ret = nfsd4_verify_open_stid(s); 8289 if (ret == nfs_ok) 8290 ret = check_stateid_generation(in, &s->sc_stateid, has_session); 8291 spin_unlock(&s->sc_lock); 8292 if (ret == nfserr_admin_revoked) 8293 nfsd40_drop_revoked_stid(s->sc_client, 8294 &s->sc_stateid); 8295 return ret; 8296 } 8297 8298 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) 8299 { 8300 if (ols->st_stateowner->so_is_open_owner && 8301 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) 8302 return nfserr_bad_stateid; 8303 return nfs_ok; 8304 } 8305 8306 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) 8307 { 8308 struct nfs4_stid *s; 8309 __be32 status = nfserr_bad_stateid; 8310 8311 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || 8312 CLOSE_STATEID(stateid)) 8313 return status; 8314 spin_lock(&cl->cl_lock); 8315 s = find_stateid_locked(cl, stateid); 8316 if (!s) 8317 goto out_unlock; 8318 status = nfsd4_stid_check_stateid_generation(stateid, s, 1); 8319 if (status) 8320 goto out_unlock; 8321 status = nfsd4_verify_open_stid(s); 8322 if (status) 8323 goto out_unlock; 8324 8325 switch (s->sc_type) { 8326 case SC_TYPE_DELEG: 8327 status = nfs_ok; 8328 break; 8329 case SC_TYPE_OPEN: 8330 case SC_TYPE_LOCK: 8331 status = nfsd4_check_openowner_confirmed(openlockstateid(s)); 8332 break; 8333 default: 8334 printk("unknown stateid type %x\n", s->sc_type); 8335 status = nfserr_bad_stateid; 8336 } 8337 out_unlock: 8338 spin_unlock(&cl->cl_lock); 8339 if (status == nfserr_admin_revoked) 8340 nfsd40_drop_revoked_stid(cl, stateid); 8341 return status; 8342 } 8343 8344 __be32 8345 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, 8346 stateid_t *stateid, 8347 unsigned short typemask, unsigned short statusmask, 8348 struct nfs4_stid **s, struct nfsd_net *nn) 8349 { 8350 __be32 status; 8351 struct nfs4_stid *stid; 8352 bool return_revoked = false; 8353 8354 /* 8355 * only return revoked delegations if explicitly asked. 8356 * otherwise we report revoked or bad_stateid status. 8357 */ 8358 if (statusmask & SC_STATUS_REVOKED) 8359 return_revoked = true; 8360 if (typemask & SC_TYPE_DELEG) 8361 /* Always allow REVOKED for DELEG so we can 8362 * return the appropriate error. 8363 */ 8364 statusmask |= SC_STATUS_REVOKED; 8365 8366 statusmask |= SC_STATUS_ADMIN_REVOKED | SC_STATUS_FREEABLE; 8367 8368 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || 8369 CLOSE_STATEID(stateid)) 8370 return nfserr_bad_stateid; 8371 status = set_client(&stateid->si_opaque.so_clid, cstate, nn); 8372 if (status == nfserr_stale_clientid) { 8373 if (cstate->session) 8374 return nfserr_bad_stateid; 8375 return nfserr_stale_stateid; 8376 } 8377 if (status) 8378 return status; 8379 stid = find_stateid_by_type(cstate->clp, stateid, typemask, statusmask); 8380 if (!stid) 8381 return nfserr_bad_stateid; 8382 if ((stid->sc_status & SC_STATUS_REVOKED) && !return_revoked) { 8383 nfs4_put_stid(stid); 8384 return nfserr_deleg_revoked; 8385 } 8386 if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) { 8387 nfsd40_drop_revoked_stid(cstate->clp, stateid); 8388 nfs4_put_stid(stid); 8389 return nfserr_admin_revoked; 8390 } 8391 *s = stid; 8392 return nfs_ok; 8393 } 8394 8395 static struct nfsd_file * 8396 nfs4_find_file(struct nfs4_stid *s, int flags) 8397 { 8398 struct nfsd_file *ret = NULL; 8399 8400 if (!s || s->sc_status) 8401 return NULL; 8402 8403 switch (s->sc_type) { 8404 case SC_TYPE_DELEG: 8405 case SC_TYPE_OPEN: 8406 case SC_TYPE_LOCK: 8407 if (flags & RD_STATE) 8408 ret = find_readable_file(s->sc_file); 8409 else 8410 ret = find_writeable_file(s->sc_file); 8411 } 8412 8413 return ret; 8414 } 8415 8416 static __be32 8417 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags) 8418 { 8419 __be32 status; 8420 8421 status = nfsd4_check_openowner_confirmed(ols); 8422 if (status) 8423 return status; 8424 return nfs4_check_openmode(ols, flags); 8425 } 8426 8427 static __be32 8428 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s, 8429 struct nfsd_file **nfp, int flags) 8430 { 8431 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; 8432 struct nfsd_file *nf; 8433 __be32 status; 8434 8435 nf = nfs4_find_file(s, flags); 8436 if (nf) { 8437 status = nfsd_permission(&rqstp->rq_cred, 8438 fhp->fh_export, fhp->fh_dentry, 8439 acc | NFSD_MAY_OWNER_OVERRIDE); 8440 if (status) { 8441 nfsd_file_put(nf); 8442 goto out; 8443 } 8444 } else { 8445 status = nfsd_file_acquire(rqstp, fhp, acc, &nf); 8446 if (status) 8447 return status; 8448 } 8449 *nfp = nf; 8450 out: 8451 return status; 8452 } 8453 8454 static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) 8455 { 8456 WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID); 8457 if (refcount_dec_and_test(&cps->cp_stateid.cs_count)) { 8458 nfsd4_unhash_cpntf_state(nn, cps); 8459 kfree(cps); 8460 } 8461 } 8462 /* 8463 * A READ from an inter server to server COPY will have a 8464 * copy stateid. Look up the copy notify stateid from the 8465 * idr structure and take a reference on it. 8466 */ 8467 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st, 8468 struct nfs4_client *clp, 8469 struct nfs4_cpntf_state **cps) 8470 { 8471 copy_stateid_t *cps_t; 8472 struct nfs4_cpntf_state *state = NULL; 8473 8474 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id) 8475 return nfserr_bad_stateid; 8476 spin_lock(&nn->s2s_cp_lock); 8477 /* s2s_cp_stateids holds only COPY_NOTIFY stateids */ 8478 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id); 8479 if (cps_t) { 8480 state = container_of(cps_t, struct nfs4_cpntf_state, 8481 cp_stateid); 8482 if (!clp) { 8483 refcount_inc(&state->cp_stateid.cs_count); 8484 } else if (memcmp(&clp->cl_clientid, &state->cp_p_clid, 8485 sizeof(clientid_t))) { 8486 /* 8487 * OFFLOAD_CANCEL: only the creating client may cancel. 8488 * so_id is guessable, so without this check any client 8489 * could free another's cpntf state. 8490 */ 8491 state = NULL; 8492 goto unlock; 8493 } else { 8494 revoke_cpntf_state_locked(nn, state); 8495 } 8496 } 8497 unlock: 8498 spin_unlock(&nn->s2s_cp_lock); 8499 if (!state) 8500 return nfserr_bad_stateid; 8501 if (!clp) 8502 *cps = state; 8503 return 0; 8504 } 8505 8506 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st, 8507 struct nfs4_stid **stid) 8508 { 8509 __be32 status; 8510 struct nfs4_cpntf_state *cps = NULL; 8511 struct nfs4_client *found; 8512 8513 status = manage_cpntf_state(nn, st, NULL, &cps); 8514 if (status) 8515 return status; 8516 8517 cps->cpntf_time = ktime_get_boottime_seconds(); 8518 8519 status = nfserr_expired; 8520 found = lookup_clientid(&cps->cp_p_clid, true, nn); 8521 if (!found) 8522 goto out; 8523 8524 *stid = find_stateid_by_type(found, &cps->cp_p_stateid, 8525 SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK, 8526 0); 8527 if (*stid) 8528 status = nfs_ok; 8529 else 8530 status = nfserr_bad_stateid; 8531 8532 put_client_renew(found); 8533 out: 8534 nfs4_put_cpntf_state(nn, cps); 8535 return status; 8536 } 8537 8538 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) 8539 { 8540 spin_lock(&nn->s2s_cp_lock); 8541 _free_cpntf_state_locked(nn, cps); 8542 spin_unlock(&nn->s2s_cp_lock); 8543 } 8544 8545 /** 8546 * nfs4_preprocess_stateid_op - find and prep stateid for an operation 8547 * @rqstp: incoming request from client 8548 * @cstate: current compound state 8549 * @fhp: filehandle associated with requested stateid 8550 * @stateid: stateid (provided by client) 8551 * @flags: flags describing type of operation to be done 8552 * @nfp: optional nfsd_file return pointer (may be NULL) 8553 * @cstid: optional returned nfs4_stid pointer (may be NULL) 8554 * 8555 * Given info from the client, look up a nfs4_stid for the operation. On 8556 * success, it returns a reference to the nfs4_stid and/or the nfsd_file 8557 * associated with it. 8558 */ 8559 __be32 8560 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, 8561 struct nfsd4_compound_state *cstate, struct svc_fh *fhp, 8562 stateid_t *stateid, int flags, struct nfsd_file **nfp, 8563 struct nfs4_stid **cstid) 8564 { 8565 struct net *net = SVC_NET(rqstp); 8566 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 8567 struct nfs4_stid *s = NULL; 8568 __be32 status; 8569 8570 if (nfp) 8571 *nfp = NULL; 8572 8573 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { 8574 status = check_special_stateids(net, fhp, stateid, flags); 8575 goto done; 8576 } 8577 8578 status = nfsd4_lookup_stateid(cstate, stateid, 8579 SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK, 8580 0, &s, nn); 8581 if (status == nfserr_bad_stateid) 8582 status = find_cpntf_state(nn, stateid, &s); 8583 if (status) 8584 return status; 8585 status = nfsd4_stid_check_stateid_generation(stateid, s, 8586 nfsd4_has_session(cstate)); 8587 if (status) 8588 goto out; 8589 8590 switch (s->sc_type) { 8591 case SC_TYPE_DELEG: 8592 status = nfs4_check_delegmode(delegstateid(s), flags); 8593 break; 8594 case SC_TYPE_OPEN: 8595 case SC_TYPE_LOCK: 8596 status = nfs4_check_olstateid(openlockstateid(s), flags); 8597 break; 8598 } 8599 if (status) 8600 goto out; 8601 status = nfs4_check_fh(fhp, s); 8602 8603 done: 8604 if (status == nfs_ok && nfp) 8605 status = nfs4_check_file(rqstp, fhp, s, nfp, flags); 8606 out: 8607 if (s) { 8608 if (!status && cstid) 8609 *cstid = s; 8610 else 8611 nfs4_put_stid(s); 8612 } 8613 return status; 8614 } 8615 8616 /* 8617 * Test if the stateid is valid 8618 */ 8619 __be32 8620 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 8621 union nfsd4_op_u *u) 8622 { 8623 struct nfsd4_test_stateid *test_stateid = &u->test_stateid; 8624 struct nfsd4_test_stateid_id *stateid; 8625 struct nfs4_client *cl = cstate->clp; 8626 8627 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list) 8628 stateid->ts_id_status = 8629 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); 8630 8631 return nfs_ok; 8632 } 8633 8634 static __be32 8635 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) 8636 { 8637 struct nfs4_ol_stateid *stp = openlockstateid(s); 8638 __be32 ret; 8639 8640 ret = nfsd4_lock_ol_stateid(stp); 8641 if (ret) 8642 goto out_put_stid; 8643 8644 ret = check_stateid_generation(stateid, &s->sc_stateid, 1); 8645 if (ret) 8646 goto out; 8647 8648 ret = nfserr_locks_held; 8649 if (check_for_locks(stp->st_stid.sc_file, 8650 lockowner(stp->st_stateowner))) 8651 goto out; 8652 8653 release_lock_stateid(stp); 8654 ret = nfs_ok; 8655 8656 out: 8657 mutex_unlock(&stp->st_mutex); 8658 out_put_stid: 8659 nfs4_put_stid(s); 8660 return ret; 8661 } 8662 8663 __be32 8664 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 8665 union nfsd4_op_u *u) 8666 { 8667 struct nfsd4_free_stateid *free_stateid = &u->free_stateid; 8668 stateid_t *stateid = &free_stateid->fr_stateid; 8669 struct nfs4_stid *s; 8670 struct nfs4_delegation *dp; 8671 struct nfs4_client *cl = cstate->clp; 8672 __be32 ret = nfserr_bad_stateid; 8673 8674 spin_lock(&cl->cl_lock); 8675 s = find_stateid_locked(cl, stateid); 8676 if (!s || s->sc_status & SC_STATUS_CLOSED) 8677 goto out_unlock; 8678 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) { 8679 nfsd4_drop_revoked_stid(s); 8680 ret = nfs_ok; 8681 goto out; 8682 } 8683 spin_lock(&s->sc_lock); 8684 switch (s->sc_type) { 8685 case SC_TYPE_DELEG: 8686 if (s->sc_status & SC_STATUS_REVOKED) { 8687 s->sc_status |= SC_STATUS_CLOSED; 8688 spin_unlock(&s->sc_lock); 8689 dp = delegstateid(s); 8690 if (s->sc_status & SC_STATUS_FREEABLE) 8691 list_del_init(&dp->dl_recall_lru); 8692 s->sc_status |= SC_STATUS_FREED; 8693 spin_unlock(&cl->cl_lock); 8694 nfs4_put_stid(s); 8695 ret = nfs_ok; 8696 goto out; 8697 } 8698 ret = nfserr_locks_held; 8699 break; 8700 case SC_TYPE_OPEN: 8701 ret = check_stateid_generation(stateid, &s->sc_stateid, 1); 8702 if (ret) 8703 break; 8704 ret = nfserr_locks_held; 8705 break; 8706 case SC_TYPE_LOCK: 8707 spin_unlock(&s->sc_lock); 8708 refcount_inc(&s->sc_count); 8709 spin_unlock(&cl->cl_lock); 8710 ret = nfsd4_free_lock_stateid(stateid, s); 8711 goto out; 8712 } 8713 spin_unlock(&s->sc_lock); 8714 out_unlock: 8715 spin_unlock(&cl->cl_lock); 8716 out: 8717 return ret; 8718 } 8719 8720 static inline int 8721 setlkflg (int type) 8722 { 8723 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? 8724 RD_STATE : WR_STATE; 8725 } 8726 8727 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp) 8728 { 8729 struct svc_fh *current_fh = &cstate->current_fh; 8730 struct nfs4_stateowner *sop = stp->st_stateowner; 8731 __be32 status; 8732 8733 status = nfsd4_check_seqid(cstate, sop, seqid); 8734 if (status) 8735 return status; 8736 status = nfsd4_lock_ol_stateid(stp); 8737 if (status != nfs_ok) 8738 return status; 8739 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); 8740 if (status == nfs_ok) 8741 status = nfs4_check_fh(current_fh, &stp->st_stid); 8742 if (status != nfs_ok) 8743 mutex_unlock(&stp->st_mutex); 8744 return status; 8745 } 8746 8747 /** 8748 * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op 8749 * @cstate: compund state 8750 * @seqid: seqid (provided by client) 8751 * @stateid: stateid (provided by client) 8752 * @typemask: mask of allowable types for this operation 8753 * @statusmask: mask of allowed states: 0 or STID_CLOSED 8754 * @stpp: return pointer for the stateid found 8755 * @nn: net namespace for request 8756 * 8757 * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and 8758 * return it in @stpp. On a nfs_ok return, the returned stateid will 8759 * have its st_mutex locked. 8760 */ 8761 static __be32 8762 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, 8763 stateid_t *stateid, 8764 unsigned short typemask, unsigned short statusmask, 8765 struct nfs4_ol_stateid **stpp, 8766 struct nfsd_net *nn) 8767 { 8768 __be32 status; 8769 struct nfs4_stid *s; 8770 struct nfs4_ol_stateid *stp = NULL; 8771 8772 trace_nfsd_preprocess(seqid, stateid); 8773 8774 *stpp = NULL; 8775 retry: 8776 status = nfsd4_lookup_stateid(cstate, stateid, 8777 typemask, statusmask, &s, nn); 8778 if (status) 8779 return status; 8780 stp = openlockstateid(s); 8781 if (nfsd4_cstate_assign_replay(cstate, stp->st_stateowner) == -EAGAIN) { 8782 nfs4_put_stid(&stp->st_stid); 8783 goto retry; 8784 } 8785 8786 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); 8787 if (!status) 8788 *stpp = stp; 8789 else 8790 nfs4_put_stid(&stp->st_stid); 8791 return status; 8792 } 8793 8794 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, 8795 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn) 8796 { 8797 __be32 status; 8798 struct nfs4_openowner *oo; 8799 struct nfs4_ol_stateid *stp; 8800 8801 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, 8802 SC_TYPE_OPEN, 0, &stp, nn); 8803 if (status) 8804 return status; 8805 oo = openowner(stp->st_stateowner); 8806 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { 8807 mutex_unlock(&stp->st_mutex); 8808 nfs4_put_stid(&stp->st_stid); 8809 return nfserr_bad_stateid; 8810 } 8811 *stpp = stp; 8812 return nfs_ok; 8813 } 8814 8815 __be32 8816 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 8817 union nfsd4_op_u *u) 8818 { 8819 struct nfsd4_open_confirm *oc = &u->open_confirm; 8820 __be32 status; 8821 struct nfs4_openowner *oo; 8822 struct nfs4_ol_stateid *stp; 8823 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 8824 8825 dprintk("NFSD: nfsd4_open_confirm on file %pd\n", 8826 cstate->current_fh.fh_dentry); 8827 8828 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); 8829 if (status) 8830 return status; 8831 8832 status = nfs4_preprocess_seqid_op(cstate, 8833 oc->oc_seqid, &oc->oc_req_stateid, 8834 SC_TYPE_OPEN, 0, &stp, nn); 8835 if (status) 8836 goto out; 8837 oo = openowner(stp->st_stateowner); 8838 status = nfserr_bad_stateid; 8839 if (oo->oo_flags & NFS4_OO_CONFIRMED) { 8840 mutex_unlock(&stp->st_mutex); 8841 goto put_stateid; 8842 } 8843 oo->oo_flags |= NFS4_OO_CONFIRMED; 8844 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); 8845 mutex_unlock(&stp->st_mutex); 8846 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid); 8847 nfsd4_client_record_create(oo->oo_owner.so_client); 8848 status = nfs_ok; 8849 put_stateid: 8850 nfs4_put_stid(&stp->st_stid); 8851 out: 8852 nfsd4_bump_seqid(cstate, status); 8853 return status; 8854 } 8855 8856 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access) 8857 { 8858 if (!test_access(access, stp)) 8859 return; 8860 nfs4_file_put_access(stp->st_stid.sc_file, access); 8861 clear_access(access, stp); 8862 } 8863 8864 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) 8865 { 8866 switch (to_access) { 8867 case NFS4_SHARE_ACCESS_READ: 8868 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); 8869 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); 8870 break; 8871 case NFS4_SHARE_ACCESS_WRITE: 8872 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); 8873 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); 8874 break; 8875 case NFS4_SHARE_ACCESS_BOTH: 8876 break; 8877 default: 8878 WARN_ON_ONCE(1); 8879 } 8880 } 8881 8882 __be32 8883 nfsd4_open_downgrade(struct svc_rqst *rqstp, 8884 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 8885 { 8886 struct nfsd4_open_downgrade *od = &u->open_downgrade; 8887 __be32 status; 8888 struct nfs4_ol_stateid *stp; 8889 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 8890 8891 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", 8892 cstate->current_fh.fh_dentry); 8893 8894 /* We don't yet support WANT bits: */ 8895 if (od->od_deleg_want) 8896 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, 8897 od->od_deleg_want); 8898 8899 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, 8900 &od->od_stateid, &stp, nn); 8901 if (status) 8902 goto out; 8903 status = nfserr_inval; 8904 if (!test_access(od->od_share_access, stp)) { 8905 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n", 8906 stp->st_access_bmap, od->od_share_access); 8907 goto put_stateid; 8908 } 8909 if (!test_deny(od->od_share_deny, stp)) { 8910 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n", 8911 stp->st_deny_bmap, od->od_share_deny); 8912 goto put_stateid; 8913 } 8914 nfs4_stateid_downgrade(stp, od->od_share_access); 8915 reset_union_bmap_deny(od->od_share_deny, stp); 8916 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); 8917 status = nfs_ok; 8918 put_stateid: 8919 mutex_unlock(&stp->st_mutex); 8920 nfs4_put_stid(&stp->st_stid); 8921 out: 8922 nfsd4_bump_seqid(cstate, status); 8923 return status; 8924 } 8925 8926 static bool nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) 8927 { 8928 struct nfs4_client *clp = s->st_stid.sc_client; 8929 bool unhashed; 8930 LIST_HEAD(reaplist); 8931 struct nfs4_ol_stateid *stp; 8932 8933 spin_lock(&clp->cl_lock); 8934 unhashed = unhash_open_stateid(s, &reaplist); 8935 8936 if (clp->cl_minorversion) { 8937 if (unhashed) 8938 put_ol_stateid_locked(s, &reaplist); 8939 spin_unlock(&clp->cl_lock); 8940 list_for_each_entry(stp, &reaplist, st_locks) 8941 nfs4_free_cpntf_statelist(clp->net, &stp->st_stid); 8942 free_ol_stateid_reaplist(&reaplist); 8943 return false; 8944 } else { 8945 spin_unlock(&clp->cl_lock); 8946 free_ol_stateid_reaplist(&reaplist); 8947 return unhashed; 8948 } 8949 } 8950 8951 /* 8952 * nfs4_unlock_state() called after encode 8953 */ 8954 __be32 8955 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 8956 union nfsd4_op_u *u) 8957 { 8958 struct nfsd4_close *close = &u->close; 8959 __be32 status; 8960 struct nfs4_ol_stateid *stp; 8961 struct net *net = SVC_NET(rqstp); 8962 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 8963 bool need_move_to_close_list; 8964 8965 dprintk("NFSD: nfsd4_close on file %pd\n", 8966 cstate->current_fh.fh_dentry); 8967 8968 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, 8969 &close->cl_stateid, 8970 SC_TYPE_OPEN, SC_STATUS_CLOSED, 8971 &stp, nn); 8972 nfsd4_bump_seqid(cstate, status); 8973 if (status) 8974 goto out; 8975 8976 spin_lock(&stp->st_stid.sc_client->cl_lock); 8977 stp->st_stid.sc_status |= SC_STATUS_CLOSED; 8978 spin_unlock(&stp->st_stid.sc_client->cl_lock); 8979 8980 /* 8981 * Technically we don't _really_ have to increment or copy it, since 8982 * it should just be gone after this operation and we clobber the 8983 * copied value below, but we continue to do so here just to ensure 8984 * that racing ops see that there was a state change. 8985 */ 8986 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); 8987 8988 need_move_to_close_list = nfsd4_close_open_stateid(stp); 8989 mutex_unlock(&stp->st_mutex); 8990 if (need_move_to_close_list) 8991 move_to_close_lru(stp, net); 8992 8993 /* v4.1+ suggests that we send a special stateid in here, since the 8994 * clients should just ignore this anyway. Since this is not useful 8995 * for v4.0 clients either, we set it to the special close_stateid 8996 * universally. 8997 * 8998 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5 8999 */ 9000 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid)); 9001 9002 /* put reference from nfs4_preprocess_seqid_op */ 9003 nfs4_put_stid(&stp->st_stid); 9004 out: 9005 return status; 9006 } 9007 9008 __be32 9009 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 9010 union nfsd4_op_u *u) 9011 { 9012 struct nfsd4_delegreturn *dr = &u->delegreturn; 9013 struct nfs4_delegation *dp; 9014 stateid_t *stateid = &dr->dr_stateid; 9015 struct nfs4_stid *s; 9016 __be32 status; 9017 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 9018 9019 status = fh_verify(rqstp, &cstate->current_fh, 0, 0); 9020 if (status) 9021 return status; 9022 9023 status = nfsd4_lookup_stateid(cstate, stateid, SC_TYPE_DELEG, SC_STATUS_REVOKED, &s, nn); 9024 if (status) 9025 goto out; 9026 dp = delegstateid(s); 9027 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate)); 9028 if (status) 9029 goto put_stateid; 9030 9031 status = nfs4_check_fh(&cstate->current_fh, &dp->dl_stid); 9032 if (status) 9033 goto put_stateid; 9034 9035 trace_nfsd_deleg_return(stateid); 9036 destroy_delegation(dp); 9037 smp_mb__after_atomic(); 9038 wake_up_var(d_inode(cstate->current_fh.fh_dentry)); 9039 put_stateid: 9040 nfs4_put_stid(&dp->dl_stid); 9041 out: 9042 return status; 9043 } 9044 9045 /* last octet in a range */ 9046 static inline u64 9047 last_byte_offset(u64 start, u64 len) 9048 { 9049 u64 end; 9050 9051 WARN_ON_ONCE(!len); 9052 end = start + len; 9053 return end > start ? end - 1: NFS4_MAX_UINT64; 9054 } 9055 9056 /* 9057 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that 9058 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th 9059 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit 9060 * locking, this prevents us from being completely protocol-compliant. The 9061 * real solution to this problem is to start using unsigned file offsets in 9062 * the VFS, but this is a very deep change! 9063 */ 9064 static inline void 9065 nfs4_transform_lock_offset(struct file_lock *lock) 9066 { 9067 if (lock->fl_start < 0) 9068 lock->fl_start = OFFSET_MAX; 9069 if (lock->fl_end < 0) 9070 lock->fl_end = OFFSET_MAX; 9071 } 9072 9073 static fl_owner_t 9074 nfsd4_lm_get_owner(fl_owner_t owner) 9075 { 9076 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; 9077 9078 nfs4_get_stateowner(&lo->lo_owner); 9079 return owner; 9080 } 9081 9082 static void 9083 nfsd4_lm_put_owner(fl_owner_t owner) 9084 { 9085 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; 9086 9087 if (lo) 9088 nfs4_put_stateowner(&lo->lo_owner); 9089 } 9090 9091 /* return pointer to struct nfs4_client if client is expirable */ 9092 static bool 9093 nfsd4_lm_lock_expirable(struct file_lock *cfl) 9094 { 9095 struct nfs4_lockowner *lo = (struct nfs4_lockowner *) cfl->c.flc_owner; 9096 struct nfs4_client *clp = lo->lo_owner.so_client; 9097 struct nfsd_net *nn; 9098 9099 if (try_to_expire_client(clp)) { 9100 nn = net_generic(clp->net, nfsd_net_id); 9101 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); 9102 return true; 9103 } 9104 return false; 9105 } 9106 9107 /* schedule laundromat to run immediately and wait for it to complete */ 9108 static void 9109 nfsd4_lm_expire_lock(void) 9110 { 9111 flush_workqueue(laundry_wq); 9112 } 9113 9114 static void 9115 nfsd4_lm_notify(struct file_lock *fl) 9116 { 9117 struct nfs4_lockowner *lo = (struct nfs4_lockowner *) fl->c.flc_owner; 9118 struct net *net = lo->lo_owner.so_client->net; 9119 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 9120 struct nfsd4_blocked_lock *nbl = container_of(fl, 9121 struct nfsd4_blocked_lock, nbl_lock); 9122 bool queue = false; 9123 9124 /* An empty list means that something else is going to be using it */ 9125 spin_lock(&nn->blocked_locks_lock); 9126 if (!list_empty(&nbl->nbl_list)) { 9127 list_del_init(&nbl->nbl_list); 9128 list_del_init(&nbl->nbl_lru); 9129 queue = true; 9130 } 9131 spin_unlock(&nn->blocked_locks_lock); 9132 9133 if (queue) { 9134 trace_nfsd_cb_notify_lock(lo, nbl); 9135 nfsd4_try_run_cb(&nbl->nbl_cb); 9136 } 9137 } 9138 9139 static const struct lock_manager_operations nfsd_posix_mng_ops = { 9140 .lm_mod_owner = THIS_MODULE, 9141 .lm_notify = nfsd4_lm_notify, 9142 .lm_get_owner = nfsd4_lm_get_owner, 9143 .lm_put_owner = nfsd4_lm_put_owner, 9144 .lm_lock_expirable = nfsd4_lm_lock_expirable, 9145 .lm_expire_lock = nfsd4_lm_expire_lock, 9146 }; 9147 9148 static inline void 9149 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) 9150 { 9151 struct nfs4_lockowner *lo; 9152 9153 if (fl->fl_lmops == &nfsd_posix_mng_ops) { 9154 lo = (struct nfs4_lockowner *) fl->c.flc_owner; 9155 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner, 9156 GFP_KERNEL); 9157 if (!deny->ld_owner.data) 9158 /* We just don't care that much */ 9159 goto nevermind; 9160 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; 9161 } else { 9162 nevermind: 9163 deny->ld_owner.len = 0; 9164 deny->ld_owner.data = NULL; 9165 deny->ld_clientid.cl_boot = 0; 9166 deny->ld_clientid.cl_id = 0; 9167 } 9168 deny->ld_start = fl->fl_start; 9169 deny->ld_length = NFS4_MAX_UINT64; 9170 if (fl->fl_end != NFS4_MAX_UINT64) 9171 deny->ld_length = fl->fl_end - fl->fl_start + 1; 9172 deny->ld_type = NFS4_READ_LT; 9173 if (fl->c.flc_type != F_RDLCK) 9174 deny->ld_type = NFS4_WRITE_LT; 9175 } 9176 9177 static struct nfs4_lockowner * 9178 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) 9179 { 9180 unsigned int strhashval = ownerstr_hashval(owner); 9181 struct nfs4_stateowner *so; 9182 9183 lockdep_assert_held(&clp->cl_lock); 9184 9185 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], 9186 so_strhash) { 9187 if (so->so_is_open_owner) 9188 continue; 9189 if (same_owner_str(so, owner)) 9190 return lockowner(nfs4_get_stateowner(so)); 9191 } 9192 return NULL; 9193 } 9194 9195 static struct nfs4_lockowner * 9196 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) 9197 { 9198 struct nfs4_lockowner *lo; 9199 9200 spin_lock(&clp->cl_lock); 9201 lo = find_lockowner_str_locked(clp, owner); 9202 spin_unlock(&clp->cl_lock); 9203 return lo; 9204 } 9205 9206 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) 9207 { 9208 unhash_lockowner_locked(lockowner(sop)); 9209 } 9210 9211 static void nfs4_free_lockowner(struct nfs4_stateowner *sop) 9212 { 9213 struct nfs4_lockowner *lo = lockowner(sop); 9214 9215 kmem_cache_free(lockowner_slab, lo); 9216 } 9217 9218 static const struct nfs4_stateowner_operations lockowner_ops = { 9219 .so_unhash = nfs4_unhash_lockowner, 9220 .so_free = nfs4_free_lockowner, 9221 }; 9222 9223 /* 9224 * Alloc a lock owner structure. 9225 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has 9226 * occurred. 9227 * 9228 * strhashval = ownerstr_hashval 9229 */ 9230 static struct nfs4_lockowner * 9231 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, 9232 struct nfs4_ol_stateid *open_stp, 9233 struct nfsd4_lock *lock) 9234 { 9235 struct nfs4_lockowner *lo, *ret; 9236 9237 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); 9238 if (!lo) 9239 return NULL; 9240 INIT_LIST_HEAD(&lo->lo_blocked); 9241 INIT_LIST_HEAD(&lo->lo_owner.so_stateids); 9242 lo->lo_owner.so_is_open_owner = 0; 9243 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; 9244 lo->lo_owner.so_ops = &lockowner_ops; 9245 spin_lock(&clp->cl_lock); 9246 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); 9247 if (ret == NULL) { 9248 list_add(&lo->lo_owner.so_strhash, 9249 &clp->cl_ownerstr_hashtbl[strhashval]); 9250 ret = lo; 9251 } else 9252 nfs4_free_stateowner(&lo->lo_owner); 9253 9254 spin_unlock(&clp->cl_lock); 9255 return ret; 9256 } 9257 9258 static struct nfs4_ol_stateid * 9259 find_lock_stateid(const struct nfs4_lockowner *lo, 9260 const struct nfs4_ol_stateid *ost) 9261 { 9262 struct nfs4_ol_stateid *lst; 9263 9264 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock); 9265 9266 /* If ost is not hashed, ost->st_locks will not be valid */ 9267 if (!nfs4_ol_stateid_unhashed(ost)) 9268 list_for_each_entry(lst, &ost->st_locks, st_locks) { 9269 if (lst->st_stateowner == &lo->lo_owner) { 9270 refcount_inc(&lst->st_stid.sc_count); 9271 return lst; 9272 } 9273 } 9274 return NULL; 9275 } 9276 9277 static struct nfs4_ol_stateid * 9278 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, 9279 struct nfs4_file *fp, struct inode *inode, 9280 struct nfs4_ol_stateid *open_stp) 9281 { 9282 struct nfs4_client *clp = lo->lo_owner.so_client; 9283 struct nfs4_ol_stateid *retstp; 9284 9285 mutex_init(&stp->st_mutex); 9286 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); 9287 retry: 9288 spin_lock(&clp->cl_lock); 9289 if (nfs4_ol_stateid_unhashed(open_stp)) 9290 goto out_close; 9291 retstp = find_lock_stateid(lo, open_stp); 9292 if (retstp) 9293 goto out_found; 9294 refcount_inc(&stp->st_stid.sc_count); 9295 stp->st_stid.sc_type = SC_TYPE_LOCK; 9296 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); 9297 get_nfs4_file(fp); 9298 stp->st_stid.sc_file = fp; 9299 if (open_stp->st_stid.sc_export) 9300 stp->st_stid.sc_export = 9301 exp_get(open_stp->st_stid.sc_export); 9302 stp->st_access_bmap = 0; 9303 stp->st_deny_bmap = open_stp->st_deny_bmap; 9304 stp->st_openstp = open_stp; 9305 spin_lock(&fp->fi_lock); 9306 list_add(&stp->st_locks, &open_stp->st_locks); 9307 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); 9308 list_add(&stp->st_perfile, &fp->fi_stateids); 9309 spin_unlock(&fp->fi_lock); 9310 spin_unlock(&clp->cl_lock); 9311 return stp; 9312 out_found: 9313 spin_unlock(&clp->cl_lock); 9314 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { 9315 nfs4_put_stid(&retstp->st_stid); 9316 goto retry; 9317 } 9318 /* To keep mutex tracking happy */ 9319 mutex_unlock(&stp->st_mutex); 9320 return retstp; 9321 out_close: 9322 spin_unlock(&clp->cl_lock); 9323 mutex_unlock(&stp->st_mutex); 9324 return NULL; 9325 } 9326 9327 static struct nfs4_ol_stateid * 9328 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, 9329 struct inode *inode, struct nfs4_ol_stateid *ost, 9330 bool *new) 9331 { 9332 struct nfs4_stid *ns = NULL; 9333 struct nfs4_ol_stateid *lst; 9334 struct nfs4_openowner *oo = openowner(ost->st_stateowner); 9335 struct nfs4_client *clp = oo->oo_owner.so_client; 9336 9337 *new = false; 9338 spin_lock(&clp->cl_lock); 9339 lst = find_lock_stateid(lo, ost); 9340 spin_unlock(&clp->cl_lock); 9341 if (lst != NULL) { 9342 if (nfsd4_lock_ol_stateid(lst) == nfs_ok) 9343 goto out; 9344 nfs4_put_stid(&lst->st_stid); 9345 } 9346 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid); 9347 if (ns == NULL) 9348 return NULL; 9349 9350 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost); 9351 if (lst == openlockstateid(ns)) 9352 *new = true; 9353 else 9354 nfs4_put_stid(ns); 9355 out: 9356 return lst; 9357 } 9358 9359 static int 9360 check_lock_length(u64 offset, u64 length) 9361 { 9362 return ((length == 0) || ((length != NFS4_MAX_UINT64) && 9363 (length > ~offset))); 9364 } 9365 9366 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access) 9367 { 9368 struct nfs4_file *fp = lock_stp->st_stid.sc_file; 9369 9370 lockdep_assert_held(&fp->fi_lock); 9371 9372 if (test_access(access, lock_stp)) 9373 return; 9374 __nfs4_file_get_access(fp, access); 9375 set_access(access, lock_stp); 9376 } 9377 9378 static __be32 9379 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, 9380 struct nfs4_ol_stateid *ost, 9381 struct nfsd4_lock *lock, 9382 struct nfs4_ol_stateid **plst, bool *new) 9383 { 9384 __be32 status; 9385 struct nfs4_file *fi = ost->st_stid.sc_file; 9386 struct nfs4_openowner *oo = openowner(ost->st_stateowner); 9387 struct nfs4_client *cl = oo->oo_owner.so_client; 9388 struct inode *inode = d_inode(cstate->current_fh.fh_dentry); 9389 struct nfs4_lockowner *lo; 9390 struct nfs4_ol_stateid *lst; 9391 unsigned int strhashval; 9392 9393 lo = find_lockowner_str(cl, &lock->lk_new_owner); 9394 if (!lo) { 9395 strhashval = ownerstr_hashval(&lock->lk_new_owner); 9396 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); 9397 if (lo == NULL) 9398 return nfserr_jukebox; 9399 } else { 9400 /* with an existing lockowner, seqids must be the same */ 9401 status = nfserr_bad_seqid; 9402 if (!cstate->minorversion && 9403 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) 9404 goto out; 9405 } 9406 9407 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); 9408 if (lst == NULL) { 9409 status = nfserr_jukebox; 9410 goto out; 9411 } 9412 9413 status = nfs_ok; 9414 *plst = lst; 9415 out: 9416 nfs4_put_stateowner(&lo->lo_owner); 9417 return status; 9418 } 9419 9420 /* 9421 * LOCK operation 9422 */ 9423 __be32 9424 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 9425 union nfsd4_op_u *u) 9426 { 9427 struct nfsd4_lock *lock = &u->lock; 9428 struct nfs4_openowner *open_sop = NULL; 9429 struct nfs4_lockowner *lock_sop = NULL; 9430 struct nfs4_ol_stateid *lock_stp = NULL; 9431 struct nfs4_ol_stateid *open_stp = NULL; 9432 struct nfs4_file *fp; 9433 struct nfsd_file *nf = NULL; 9434 struct nfsd4_blocked_lock *nbl = NULL; 9435 struct file_lock *file_lock = NULL; 9436 struct file_lock *conflock = NULL; 9437 __be32 status = 0; 9438 int lkflg; 9439 int err; 9440 bool new = false; 9441 unsigned char type; 9442 unsigned int flags = FL_POSIX; 9443 struct net *net = SVC_NET(rqstp); 9444 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 9445 9446 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", 9447 (long long) lock->lk_offset, 9448 (long long) lock->lk_length); 9449 9450 if (check_lock_length(lock->lk_offset, lock->lk_length)) 9451 return nfserr_inval; 9452 9453 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); 9454 if (status != nfs_ok) 9455 return status; 9456 if (exportfs_cannot_lock(cstate->current_fh.fh_dentry->d_sb->s_export_op)) { 9457 status = nfserr_notsupp; 9458 goto out; 9459 } 9460 9461 if (lock->lk_is_new) { 9462 if (nfsd4_has_session(cstate)) 9463 /* See rfc 5661 18.10.3: given clientid is ignored: */ 9464 memcpy(&lock->lk_new_clientid, 9465 &cstate->clp->cl_clientid, 9466 sizeof(clientid_t)); 9467 9468 /* validate and update open stateid and open seqid */ 9469 status = nfs4_preprocess_confirmed_seqid_op(cstate, 9470 lock->lk_new_open_seqid, 9471 &lock->lk_new_open_stateid, 9472 &open_stp, nn); 9473 if (status) 9474 goto out; 9475 mutex_unlock(&open_stp->st_mutex); 9476 open_sop = openowner(open_stp->st_stateowner); 9477 status = nfserr_bad_stateid; 9478 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, 9479 &lock->lk_new_clientid)) 9480 goto out; 9481 status = lookup_or_create_lock_state(cstate, open_stp, lock, 9482 &lock_stp, &new); 9483 } else { 9484 status = nfs4_preprocess_seqid_op(cstate, 9485 lock->lk_old_lock_seqid, 9486 &lock->lk_old_lock_stateid, 9487 SC_TYPE_LOCK, 0, &lock_stp, 9488 nn); 9489 } 9490 if (status) 9491 goto out; 9492 lock_sop = lockowner(lock_stp->st_stateowner); 9493 9494 lkflg = setlkflg(lock->lk_type); 9495 status = nfs4_check_openmode(lock_stp, lkflg); 9496 if (status) 9497 goto out; 9498 9499 status = nfserr_grace; 9500 if (locks_in_grace(net) && !lock->lk_reclaim) 9501 goto out; 9502 status = nfserr_no_grace; 9503 if (!locks_in_grace(net) && lock->lk_reclaim) 9504 goto out; 9505 if (lock->lk_reclaim && 9506 test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags)) 9507 goto out; 9508 9509 if (lock->lk_reclaim) 9510 flags |= FL_RECLAIM; 9511 9512 fp = lock_stp->st_stid.sc_file; 9513 switch (lock->lk_type) { 9514 case NFS4_READW_LT: 9515 fallthrough; 9516 case NFS4_READ_LT: 9517 spin_lock(&fp->fi_lock); 9518 nf = find_readable_file_locked(fp); 9519 if (nf) 9520 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); 9521 spin_unlock(&fp->fi_lock); 9522 type = F_RDLCK; 9523 break; 9524 case NFS4_WRITEW_LT: 9525 fallthrough; 9526 case NFS4_WRITE_LT: 9527 spin_lock(&fp->fi_lock); 9528 nf = find_writeable_file_locked(fp); 9529 if (nf) 9530 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); 9531 spin_unlock(&fp->fi_lock); 9532 type = F_WRLCK; 9533 break; 9534 default: 9535 status = nfserr_inval; 9536 goto out; 9537 } 9538 9539 if (!nf) { 9540 status = nfserr_openmode; 9541 goto out; 9542 } 9543 9544 if ((lock->lk_type == NFS4_READW_LT || 9545 lock->lk_type == NFS4_WRITEW_LT) && 9546 nfsd4_has_session(cstate) && 9547 locks_can_async_lock(nf->nf_file->f_op)) 9548 flags |= FL_SLEEP; 9549 9550 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn); 9551 if (!nbl) { 9552 dprintk("NFSD: %s: unable to allocate block!\n", __func__); 9553 status = nfserr_jukebox; 9554 goto out; 9555 } 9556 9557 file_lock = &nbl->nbl_lock; 9558 file_lock->c.flc_type = type; 9559 file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner)); 9560 file_lock->c.flc_pid = current->tgid; 9561 file_lock->c.flc_file = nf->nf_file; 9562 file_lock->c.flc_flags = flags; 9563 file_lock->fl_lmops = &nfsd_posix_mng_ops; 9564 file_lock->fl_start = lock->lk_offset; 9565 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); 9566 nfs4_transform_lock_offset(file_lock); 9567 9568 conflock = locks_alloc_lock(); 9569 if (!conflock) { 9570 dprintk("NFSD: %s: unable to allocate lock!\n", __func__); 9571 status = nfserr_jukebox; 9572 goto out; 9573 } 9574 9575 if (flags & FL_SLEEP) { 9576 nbl->nbl_time = ktime_get_boottime_seconds(); 9577 spin_lock(&nn->blocked_locks_lock); 9578 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked); 9579 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru); 9580 kref_get(&nbl->nbl_kref); 9581 spin_unlock(&nn->blocked_locks_lock); 9582 } 9583 9584 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock); 9585 switch (err) { 9586 case 0: /* success! */ 9587 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid); 9588 status = 0; 9589 if (lock->lk_reclaim) 9590 set_bit(NFSD_NET_SOMEBODY_RECLAIMED, &nn->flags); 9591 break; 9592 case FILE_LOCK_DEFERRED: 9593 kref_put(&nbl->nbl_kref, free_nbl); 9594 nbl = NULL; 9595 fallthrough; 9596 case -EAGAIN: /* conflock holds conflicting lock */ 9597 status = nfserr_denied; 9598 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); 9599 nfs4_set_lock_denied(conflock, &lock->lk_denied); 9600 break; 9601 case -EDEADLK: 9602 status = nfserr_deadlock; 9603 break; 9604 default: 9605 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err); 9606 status = nfserrno(err); 9607 break; 9608 } 9609 out: 9610 if (nbl) { 9611 /* dequeue it if we queued it before */ 9612 if (flags & FL_SLEEP) { 9613 spin_lock(&nn->blocked_locks_lock); 9614 if (!list_empty(&nbl->nbl_list) && 9615 !list_empty(&nbl->nbl_lru)) { 9616 list_del_init(&nbl->nbl_list); 9617 list_del_init(&nbl->nbl_lru); 9618 kref_put(&nbl->nbl_kref, free_nbl); 9619 } 9620 /* nbl can use one of lists to be linked to reaplist */ 9621 spin_unlock(&nn->blocked_locks_lock); 9622 } 9623 free_blocked_lock(nbl); 9624 } 9625 if (nf) 9626 nfsd_file_put(nf); 9627 if (lock_stp) { 9628 /* Bump seqid manually if the 4.0 replay owner is openowner */ 9629 if (cstate->replay_owner && 9630 cstate->replay_owner != &lock_sop->lo_owner && 9631 seqid_mutating_err(ntohl(status))) 9632 lock_sop->lo_owner.so_seqid++; 9633 9634 /* 9635 * If this is a new, never-before-used stateid, and we are 9636 * returning an error, then just go ahead and release it. 9637 */ 9638 if (status && new) 9639 release_lock_stateid(lock_stp); 9640 9641 mutex_unlock(&lock_stp->st_mutex); 9642 9643 nfs4_put_stid(&lock_stp->st_stid); 9644 } 9645 if (open_stp) 9646 nfs4_put_stid(&open_stp->st_stid); 9647 nfsd4_bump_seqid(cstate, status); 9648 if (conflock) 9649 locks_free_lock(conflock); 9650 return status; 9651 } 9652 9653 void nfsd4_lock_release(union nfsd4_op_u *u) 9654 { 9655 struct nfsd4_lock *lock = &u->lock; 9656 struct nfsd4_lock_denied *deny = &lock->lk_denied; 9657 9658 kfree(deny->ld_owner.data); 9659 } 9660 9661 /* 9662 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, 9663 * so we do a temporary open here just to get an open file to pass to 9664 * vfs_test_lock. 9665 */ 9666 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock) 9667 { 9668 struct nfsd_file *nf; 9669 struct inode *inode; 9670 __be32 err; 9671 9672 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf); 9673 if (err) 9674 return err; 9675 inode = fhp->fh_dentry->d_inode; 9676 inode_lock(inode); /* to block new leases till after test_lock: */ 9677 err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ)); 9678 if (err) 9679 goto out; 9680 lock->c.flc_file = nf->nf_file; 9681 err = nfserrno(vfs_test_lock(nf->nf_file, lock)); 9682 lock->c.flc_file = NULL; 9683 out: 9684 inode_unlock(inode); 9685 nfsd_file_put(nf); 9686 return err; 9687 } 9688 9689 /* 9690 * LOCKT operation 9691 */ 9692 __be32 9693 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 9694 union nfsd4_op_u *u) 9695 { 9696 struct nfsd4_lockt *lockt = &u->lockt; 9697 struct file_lock *file_lock = NULL; 9698 struct nfs4_lockowner *lo = NULL; 9699 __be32 status; 9700 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 9701 9702 if (locks_in_grace(SVC_NET(rqstp))) 9703 return nfserr_grace; 9704 9705 if (check_lock_length(lockt->lt_offset, lockt->lt_length)) 9706 return nfserr_inval; 9707 9708 if (!nfsd4_has_session(cstate)) { 9709 status = set_client(&lockt->lt_clientid, cstate, nn); 9710 if (status) 9711 goto out; 9712 } 9713 9714 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) 9715 goto out; 9716 9717 file_lock = locks_alloc_lock(); 9718 if (!file_lock) { 9719 dprintk("NFSD: %s: unable to allocate lock!\n", __func__); 9720 status = nfserr_jukebox; 9721 goto out; 9722 } 9723 9724 switch (lockt->lt_type) { 9725 case NFS4_READ_LT: 9726 case NFS4_READW_LT: 9727 file_lock->c.flc_type = F_RDLCK; 9728 break; 9729 case NFS4_WRITE_LT: 9730 case NFS4_WRITEW_LT: 9731 file_lock->c.flc_type = F_WRLCK; 9732 break; 9733 default: 9734 dprintk("NFSD: nfs4_lockt: bad lock type!\n"); 9735 status = nfserr_inval; 9736 goto out; 9737 } 9738 9739 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); 9740 if (lo) 9741 file_lock->c.flc_owner = (fl_owner_t)lo; 9742 file_lock->c.flc_pid = current->tgid; 9743 file_lock->c.flc_flags = FL_POSIX; 9744 9745 file_lock->fl_start = lockt->lt_offset; 9746 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length); 9747 9748 nfs4_transform_lock_offset(file_lock); 9749 9750 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); 9751 if (status) 9752 goto out; 9753 9754 if (file_lock->c.flc_type != F_UNLCK) { 9755 status = nfserr_denied; 9756 nfs4_set_lock_denied(file_lock, &lockt->lt_denied); 9757 } 9758 out: 9759 if (lo) 9760 nfs4_put_stateowner(&lo->lo_owner); 9761 if (file_lock) 9762 locks_free_lock(file_lock); 9763 return status; 9764 } 9765 9766 void nfsd4_lockt_release(union nfsd4_op_u *u) 9767 { 9768 struct nfsd4_lockt *lockt = &u->lockt; 9769 struct nfsd4_lock_denied *deny = &lockt->lt_denied; 9770 9771 kfree(deny->ld_owner.data); 9772 } 9773 9774 __be32 9775 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 9776 union nfsd4_op_u *u) 9777 { 9778 struct nfsd4_locku *locku = &u->locku; 9779 struct nfs4_ol_stateid *stp; 9780 struct nfsd_file *nf = NULL; 9781 struct file_lock *file_lock = NULL; 9782 __be32 status; 9783 int err; 9784 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 9785 9786 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", 9787 (long long) locku->lu_offset, 9788 (long long) locku->lu_length); 9789 9790 if (check_lock_length(locku->lu_offset, locku->lu_length)) 9791 return nfserr_inval; 9792 9793 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, 9794 &locku->lu_stateid, SC_TYPE_LOCK, 0, 9795 &stp, nn); 9796 if (status) 9797 goto out; 9798 nf = find_any_file(stp->st_stid.sc_file); 9799 if (!nf) { 9800 status = nfserr_lock_range; 9801 goto put_stateid; 9802 } 9803 if (exportfs_cannot_lock(nf->nf_file->f_path.mnt->mnt_sb->s_export_op)) { 9804 status = nfserr_notsupp; 9805 goto put_file; 9806 } 9807 9808 file_lock = locks_alloc_lock(); 9809 if (!file_lock) { 9810 dprintk("NFSD: %s: unable to allocate lock!\n", __func__); 9811 status = nfserr_jukebox; 9812 goto put_file; 9813 } 9814 9815 file_lock->c.flc_type = F_UNLCK; 9816 file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner)); 9817 file_lock->c.flc_pid = current->tgid; 9818 file_lock->c.flc_file = nf->nf_file; 9819 file_lock->c.flc_flags = FL_POSIX; 9820 file_lock->fl_lmops = &nfsd_posix_mng_ops; 9821 file_lock->fl_start = locku->lu_offset; 9822 9823 file_lock->fl_end = last_byte_offset(locku->lu_offset, 9824 locku->lu_length); 9825 nfs4_transform_lock_offset(file_lock); 9826 9827 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL); 9828 if (err) { 9829 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); 9830 goto out_nfserr; 9831 } 9832 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); 9833 put_file: 9834 nfsd_file_put(nf); 9835 put_stateid: 9836 mutex_unlock(&stp->st_mutex); 9837 nfs4_put_stid(&stp->st_stid); 9838 out: 9839 nfsd4_bump_seqid(cstate, status); 9840 if (file_lock) 9841 locks_free_lock(file_lock); 9842 return status; 9843 9844 out_nfserr: 9845 status = nfserrno(err); 9846 goto put_file; 9847 } 9848 9849 /* 9850 * returns 9851 * true: locks held by lockowner 9852 * false: no locks held by lockowner 9853 */ 9854 static bool 9855 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) 9856 { 9857 struct file_lock *fl; 9858 int status = false; 9859 struct nfsd_file *nf; 9860 struct inode *inode; 9861 struct file_lock_context *flctx; 9862 9863 spin_lock(&fp->fi_lock); 9864 nf = find_any_file_locked(fp); 9865 if (!nf) { 9866 /* Any valid lock stateid should have some sort of access */ 9867 WARN_ON_ONCE(1); 9868 goto out; 9869 } 9870 9871 inode = file_inode(nf->nf_file); 9872 flctx = locks_inode_context(inode); 9873 9874 if (flctx && !list_empty_careful(&flctx->flc_posix)) { 9875 spin_lock(&flctx->flc_lock); 9876 for_each_file_lock(fl, &flctx->flc_posix) { 9877 if (fl->c.flc_owner == (fl_owner_t)lowner) { 9878 status = true; 9879 break; 9880 } 9881 } 9882 spin_unlock(&flctx->flc_lock); 9883 } 9884 out: 9885 spin_unlock(&fp->fi_lock); 9886 return status; 9887 } 9888 9889 /** 9890 * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations 9891 * @rqstp: RPC transaction 9892 * @cstate: NFSv4 COMPOUND state 9893 * @u: RELEASE_LOCKOWNER arguments 9894 * 9895 * Check if there are any locks still held and if not, free the lockowner 9896 * and any lock state that is owned. 9897 * 9898 * Return values: 9899 * %nfs_ok: lockowner released or not found 9900 * %nfserr_locks_held: lockowner still in use 9901 * %nfserr_stale_clientid: clientid no longer active 9902 * %nfserr_expired: clientid not recognized 9903 */ 9904 __be32 9905 nfsd4_release_lockowner(struct svc_rqst *rqstp, 9906 struct nfsd4_compound_state *cstate, 9907 union nfsd4_op_u *u) 9908 { 9909 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner; 9910 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 9911 clientid_t *clid = &rlockowner->rl_clientid; 9912 struct nfs4_ol_stateid *stp; 9913 struct nfs4_lockowner *lo; 9914 struct nfs4_client *clp; 9915 LIST_HEAD(reaplist); 9916 __be32 status; 9917 9918 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", 9919 clid->cl_boot, clid->cl_id); 9920 9921 status = set_client(clid, cstate, nn); 9922 if (status) 9923 return status; 9924 clp = cstate->clp; 9925 9926 spin_lock(&clp->cl_lock); 9927 lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner); 9928 if (!lo) { 9929 spin_unlock(&clp->cl_lock); 9930 return nfs_ok; 9931 } 9932 9933 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) { 9934 if (check_for_locks(stp->st_stid.sc_file, lo)) { 9935 spin_unlock(&clp->cl_lock); 9936 nfs4_put_stateowner(&lo->lo_owner); 9937 return nfserr_locks_held; 9938 } 9939 } 9940 unhash_lockowner_locked(lo); 9941 while (!list_empty(&lo->lo_owner.so_stateids)) { 9942 stp = list_first_entry(&lo->lo_owner.so_stateids, 9943 struct nfs4_ol_stateid, 9944 st_perstateowner); 9945 unhash_lock_stateid(stp); 9946 put_ol_stateid_locked(stp, &reaplist); 9947 } 9948 spin_unlock(&clp->cl_lock); 9949 9950 free_ol_stateid_reaplist(&reaplist); 9951 remove_blocked_locks(lo); 9952 nfs4_put_stateowner(&lo->lo_owner); 9953 return nfs_ok; 9954 } 9955 9956 static inline struct nfs4_client_reclaim * 9957 alloc_reclaim(void) 9958 { 9959 return kmalloc_obj(struct nfs4_client_reclaim); 9960 } 9961 9962 bool 9963 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn) 9964 { 9965 struct nfs4_client_reclaim *crp; 9966 bool found; 9967 9968 down_read(&nn->reclaim_str_hashtbl_lock); 9969 crp = nfsd4_find_reclaim_client(name, nn); 9970 found = (crp && crp->cr_clp); 9971 up_read(&nn->reclaim_str_hashtbl_lock); 9972 return found; 9973 } 9974 9975 /* 9976 * failure => all reset bets are off, nfserr_no_grace... 9977 */ 9978 struct nfs4_client_reclaim * 9979 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash, 9980 struct nfsd_net *nn) 9981 { 9982 unsigned int strhashval; 9983 struct nfs4_client_reclaim *crp; 9984 9985 down_write(&nn->reclaim_str_hashtbl_lock); 9986 9987 /* 9988 * A reclaim record for this client name may already exist (for 9989 * example, populated at boot from the recovery directory before 9990 * an in-grace RECLAIM_COMPLETE or an nfsdcld downcall delivers 9991 * the same name). Dedup here so reclaim_str_hashtbl_size stays 9992 * equal to the number of distinct client names; inc_reclaim_complete 9993 * relies on that equality to end the grace period via the fast path. 9994 */ 9995 crp = nfsd4_find_reclaim_client(name, nn); 9996 if (crp) { 9997 if (princhash.len && crp->cr_princhash.len == 0) { 9998 void *pdata = kmemdup(princhash.data, princhash.len, 9999 GFP_KERNEL); 10000 if (pdata) { 10001 crp->cr_princhash.data = pdata; 10002 crp->cr_princhash.len = princhash.len; 10003 } else { 10004 dprintk("%s: failed to allocate memory for princhash.data!\n", 10005 __func__); 10006 crp = NULL; 10007 } 10008 } 10009 up_write(&nn->reclaim_str_hashtbl_lock); 10010 return crp; 10011 } 10012 10013 name.data = kmemdup(name.data, name.len, GFP_KERNEL); 10014 if (!name.data) { 10015 dprintk("%s: failed to allocate memory for name.data!\n", 10016 __func__); 10017 up_write(&nn->reclaim_str_hashtbl_lock); 10018 return NULL; 10019 } 10020 if (princhash.len) { 10021 princhash.data = kmemdup(princhash.data, princhash.len, GFP_KERNEL); 10022 if (!princhash.data) { 10023 dprintk("%s: failed to allocate memory for princhash.data!\n", 10024 __func__); 10025 kfree(name.data); 10026 up_write(&nn->reclaim_str_hashtbl_lock); 10027 return NULL; 10028 } 10029 } else 10030 princhash.data = NULL; 10031 crp = alloc_reclaim(); 10032 if (crp) { 10033 strhashval = clientstr_hashval(name); 10034 INIT_LIST_HEAD(&crp->cr_strhash); 10035 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); 10036 crp->cr_name.data = name.data; 10037 crp->cr_name.len = name.len; 10038 crp->cr_princhash.data = princhash.data; 10039 crp->cr_princhash.len = princhash.len; 10040 crp->cr_clp = NULL; 10041 nn->reclaim_str_hashtbl_size++; 10042 } else { 10043 kfree(name.data); 10044 kfree(princhash.data); 10045 } 10046 up_write(&nn->reclaim_str_hashtbl_lock); 10047 return crp; 10048 } 10049 10050 void 10051 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn) 10052 { 10053 list_del(&crp->cr_strhash); 10054 kfree(crp->cr_name.data); 10055 kfree(crp->cr_princhash.data); 10056 kfree(crp); 10057 nn->reclaim_str_hashtbl_size--; 10058 } 10059 10060 void 10061 nfs4_release_reclaim(struct nfsd_net *nn) 10062 { 10063 struct nfs4_client_reclaim *crp = NULL; 10064 int i; 10065 10066 down_write(&nn->reclaim_str_hashtbl_lock); 10067 for (i = 0; i < CLIENT_HASH_SIZE; i++) { 10068 while (!list_empty(&nn->reclaim_str_hashtbl[i])) { 10069 crp = list_entry(nn->reclaim_str_hashtbl[i].next, 10070 struct nfs4_client_reclaim, cr_strhash); 10071 nfs4_remove_reclaim_record(crp, nn); 10072 } 10073 } 10074 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); 10075 up_write(&nn->reclaim_str_hashtbl_lock); 10076 } 10077 10078 /* 10079 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ 10080 struct nfs4_client_reclaim * 10081 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn) 10082 { 10083 unsigned int strhashval; 10084 struct nfs4_client_reclaim *crp = NULL; 10085 10086 strhashval = clientstr_hashval(name); 10087 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { 10088 if (compare_blob(&crp->cr_name, &name) == 0) { 10089 return crp; 10090 } 10091 } 10092 return NULL; 10093 } 10094 10095 __be32 10096 nfs4_check_open_reclaim(struct nfs4_client *clp) 10097 { 10098 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags)) 10099 return nfserr_no_grace; 10100 10101 if (nfsd4_client_record_check(clp)) 10102 return nfserr_reclaim_bad; 10103 10104 return nfs_ok; 10105 } 10106 10107 /* 10108 * Since the lifetime of a delegation isn't limited to that of an open, a 10109 * client may quite reasonably hang on to a delegation as long as it has 10110 * the inode cached. This becomes an obvious problem the first time a 10111 * client's inode cache approaches the size of the server's total memory. 10112 * 10113 * For now we avoid this problem by imposing a hard limit on the number 10114 * of delegations, which varies according to the server's memory size. 10115 */ 10116 static void 10117 set_max_delegations(void) 10118 { 10119 /* 10120 * Allow at most 4 delegations per megabyte of RAM. Quick 10121 * estimates suggest that in the worst case (where every delegation 10122 * is for a different inode), a delegation could take about 1.5K, 10123 * giving a worst case usage of about 6% of memory. 10124 */ 10125 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); 10126 } 10127 10128 static int nfs4_state_create_net(struct net *net) 10129 { 10130 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 10131 int i; 10132 10133 nn->conf_id_hashtbl = kmalloc_objs(struct list_head, CLIENT_HASH_SIZE); 10134 if (!nn->conf_id_hashtbl) 10135 goto err; 10136 nn->unconf_id_hashtbl = kmalloc_objs(struct list_head, CLIENT_HASH_SIZE); 10137 if (!nn->unconf_id_hashtbl) 10138 goto err_unconf_id; 10139 nn->sessionid_hashtbl = kmalloc_objs(struct list_head, 10140 SESSION_HASH_SIZE); 10141 if (!nn->sessionid_hashtbl) 10142 goto err_sessionid; 10143 10144 for (i = 0; i < CLIENT_HASH_SIZE; i++) { 10145 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); 10146 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); 10147 } 10148 for (i = 0; i < SESSION_HASH_SIZE; i++) 10149 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); 10150 nn->conf_name_tree = RB_ROOT; 10151 nn->unconf_name_tree = RB_ROOT; 10152 nn->boot_time = ktime_get_real_seconds(); 10153 nn->boot_time_bt = ktime_get_boottime_seconds(); 10154 clear_bit(NFSD_NET_GRACE_ENDED, &nn->flags); 10155 clear_bit(NFSD_NET_GRACE_END_FORCED, &nn->flags); 10156 nn->nfsd4_manager.block_opens = true; 10157 INIT_LIST_HEAD(&nn->nfsd4_manager.list); 10158 INIT_LIST_HEAD(&nn->client_lru); 10159 INIT_LIST_HEAD(&nn->close_lru); 10160 INIT_LIST_HEAD(&nn->del_recall_lru); 10161 spin_lock_init(&nn->deleg_lock); 10162 spin_lock_init(&nn->client_lock); 10163 spin_lock_init(&nn->s2s_cp_lock); 10164 idr_init(&nn->s2s_cp_stateids); 10165 atomic_set(&nn->pending_async_copies, 0); 10166 10167 spin_lock_init(&nn->blocked_locks_lock); 10168 INIT_LIST_HEAD(&nn->blocked_locks_lru); 10169 10170 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); 10171 /* Make sure this cannot run until client tracking is initialised */ 10172 disable_delayed_work(&nn->laundromat_work); 10173 INIT_WORK(&nn->nfsd_courtesy_work, nfsd4_courtesy_shrinker_worker); 10174 INIT_WORK(&nn->nfsd_deleg_work, nfsd4_deleg_shrinker_worker); 10175 atomic_long_set(&nn->nfsd_shrink_backlog, 0); 10176 atomic_long_set(&nn->nfsd_deleg_backlog, 0); 10177 nn->nfsd_last_recall_any = 0; 10178 get_net(net); 10179 10180 nn->nfsd_courtesy_shrinker = 10181 nfsd4_alloc_state_shrinker(nn, "nfsd-courtesy", 10182 nfsd4_courtesy_shrinker_count, 10183 nfsd4_courtesy_shrinker_scan); 10184 if (!nn->nfsd_courtesy_shrinker) 10185 goto err_shrinker; 10186 10187 nn->nfsd_deleg_shrinker = 10188 nfsd4_alloc_state_shrinker(nn, "nfsd-delegation", 10189 nfsd4_deleg_shrinker_count, 10190 nfsd4_deleg_shrinker_scan); 10191 if (!nn->nfsd_deleg_shrinker) 10192 goto err_deleg_shrinker; 10193 10194 return 0; 10195 10196 err_deleg_shrinker: 10197 shrinker_free(nn->nfsd_courtesy_shrinker); 10198 err_shrinker: 10199 put_net(net); 10200 kfree(nn->sessionid_hashtbl); 10201 err_sessionid: 10202 kfree(nn->unconf_id_hashtbl); 10203 err_unconf_id: 10204 kfree(nn->conf_id_hashtbl); 10205 err: 10206 return -ENOMEM; 10207 } 10208 10209 static void 10210 nfs4_state_destroy_net(struct net *net) 10211 { 10212 int i; 10213 struct nfs4_client *clp = NULL; 10214 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 10215 10216 for (i = 0; i < CLIENT_HASH_SIZE; i++) { 10217 while (!list_empty(&nn->conf_id_hashtbl[i])) { 10218 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); 10219 destroy_client(clp); 10220 } 10221 } 10222 10223 WARN_ON(!list_empty(&nn->blocked_locks_lru)); 10224 10225 for (i = 0; i < CLIENT_HASH_SIZE; i++) { 10226 while (!list_empty(&nn->unconf_id_hashtbl[i])) { 10227 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); 10228 destroy_client(clp); 10229 } 10230 } 10231 10232 kfree(nn->sessionid_hashtbl); 10233 kfree(nn->unconf_id_hashtbl); 10234 kfree(nn->conf_id_hashtbl); 10235 put_net(net); 10236 } 10237 10238 int 10239 nfs4_state_start_net(struct net *net) 10240 { 10241 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 10242 int ret; 10243 10244 ret = nfs4_state_create_net(net); 10245 if (ret) 10246 return ret; 10247 locks_start_grace(net, &nn->nfsd4_manager); 10248 nfsd4_client_tracking_init(net); 10249 /* safe for laundromat to run now */ 10250 enable_delayed_work(&nn->laundromat_work); 10251 if (test_bit(NFSD_NET_TRACK_RECLAIM_COMPLETES, &nn->flags) && 10252 nn->reclaim_str_hashtbl_size == 0) 10253 goto skip_grace; 10254 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n", 10255 nn->nfsd4_grace, net->ns.inum); 10256 trace_nfsd_grace_start(nn); 10257 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); 10258 return 0; 10259 10260 skip_grace: 10261 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n", 10262 net->ns.inum); 10263 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ); 10264 nfsd4_end_grace(nn); 10265 return 0; 10266 } 10267 10268 /* initialization to perform when the nfsd service is started: */ 10269 int 10270 nfs4_state_start(void) 10271 { 10272 int ret; 10273 10274 ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params); 10275 if (ret) 10276 return ret; 10277 10278 nfsd_slot_shrinker = shrinker_alloc(0, "nfsd-DRC-slot"); 10279 if (!nfsd_slot_shrinker) { 10280 rhltable_destroy(&nfs4_file_rhltable); 10281 return -ENOMEM; 10282 } 10283 nfsd_slot_shrinker->count_objects = nfsd_slot_shrinker_count; 10284 nfsd_slot_shrinker->scan_objects = nfsd_slot_shrinker_scan; 10285 shrinker_register(nfsd_slot_shrinker); 10286 10287 set_max_delegations(); 10288 return 0; 10289 } 10290 10291 void 10292 nfs4_state_shutdown_net(struct net *net) 10293 { 10294 struct nfs4_delegation *dp = NULL; 10295 struct list_head *pos, *next, reaplist; 10296 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 10297 10298 shrinker_free(nn->nfsd_courtesy_shrinker); 10299 shrinker_free(nn->nfsd_deleg_shrinker); 10300 cancel_work_sync(&nn->nfsd_courtesy_work); 10301 cancel_work_sync(&nn->nfsd_deleg_work); 10302 disable_delayed_work_sync(&nn->laundromat_work); 10303 locks_end_grace(&nn->nfsd4_manager); 10304 10305 INIT_LIST_HEAD(&reaplist); 10306 spin_lock(&nn->deleg_lock); 10307 list_for_each_safe(pos, next, &nn->del_recall_lru) { 10308 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); 10309 unhash_delegation_locked(dp, SC_STATUS_CLOSED); 10310 list_add(&dp->dl_recall_lru, &reaplist); 10311 } 10312 spin_unlock(&nn->deleg_lock); 10313 list_for_each_safe(pos, next, &reaplist) { 10314 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); 10315 list_del_init(&dp->dl_recall_lru); 10316 destroy_unhashed_deleg(dp); 10317 } 10318 10319 nfsd4_client_tracking_exit(net); 10320 nfs4_state_destroy_net(net); 10321 #ifdef CONFIG_NFSD_V4_2_INTER_SSC 10322 nfsd4_ssc_shutdown_umount(nn); 10323 #endif 10324 } 10325 10326 void 10327 nfs4_state_shutdown(void) 10328 { 10329 rhltable_destroy(&nfs4_file_rhltable); 10330 shrinker_free(nfsd_slot_shrinker); 10331 } 10332 10333 static void 10334 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) 10335 { 10336 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) && 10337 CURRENT_STATEID(stateid)) 10338 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); 10339 } 10340 10341 static void 10342 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) 10343 { 10344 if (cstate->minorversion) { 10345 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); 10346 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); 10347 } 10348 } 10349 10350 void 10351 clear_current_stateid(struct nfsd4_compound_state *cstate) 10352 { 10353 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); 10354 } 10355 10356 /* 10357 * functions to set current state id 10358 */ 10359 void 10360 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, 10361 union nfsd4_op_u *u) 10362 { 10363 put_stateid(cstate, &u->open_downgrade.od_stateid); 10364 } 10365 10366 void 10367 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, 10368 union nfsd4_op_u *u) 10369 { 10370 put_stateid(cstate, &u->open.op_stateid); 10371 } 10372 10373 void 10374 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, 10375 union nfsd4_op_u *u) 10376 { 10377 put_stateid(cstate, &u->close.cl_stateid); 10378 } 10379 10380 void 10381 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, 10382 union nfsd4_op_u *u) 10383 { 10384 put_stateid(cstate, &u->lock.lk_resp_stateid); 10385 } 10386 10387 /* 10388 * functions to consume current state id 10389 */ 10390 10391 void 10392 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, 10393 union nfsd4_op_u *u) 10394 { 10395 get_stateid(cstate, &u->open_downgrade.od_stateid); 10396 } 10397 10398 void 10399 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, 10400 union nfsd4_op_u *u) 10401 { 10402 get_stateid(cstate, &u->delegreturn.dr_stateid); 10403 } 10404 10405 void 10406 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, 10407 union nfsd4_op_u *u) 10408 { 10409 get_stateid(cstate, &u->free_stateid.fr_stateid); 10410 } 10411 10412 void 10413 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, 10414 union nfsd4_op_u *u) 10415 { 10416 get_stateid(cstate, &u->setattr.sa_stateid); 10417 } 10418 10419 void 10420 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, 10421 union nfsd4_op_u *u) 10422 { 10423 get_stateid(cstate, &u->close.cl_stateid); 10424 } 10425 10426 void 10427 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, 10428 union nfsd4_op_u *u) 10429 { 10430 get_stateid(cstate, &u->locku.lu_stateid); 10431 } 10432 10433 void 10434 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, 10435 union nfsd4_op_u *u) 10436 { 10437 get_stateid(cstate, &u->read.rd_stateid); 10438 } 10439 10440 void 10441 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, 10442 union nfsd4_op_u *u) 10443 { 10444 get_stateid(cstate, &u->write.wr_stateid); 10445 } 10446 10447 /** 10448 * nfsd4_vet_deleg_time - vet and set the timespec for a delegated timestamp update 10449 * @req: timestamp from the client 10450 * @orig: original timestamp in the inode 10451 * @now: current time 10452 * 10453 * Given a timestamp from the client response, check it against the 10454 * current timestamp in the inode and the current time. Returns true 10455 * if the inode's timestamp needs to be updated, and false otherwise. 10456 * @req may also be changed if the timestamp needs to be clamped. 10457 */ 10458 bool nfsd4_vet_deleg_time(struct timespec64 *req, const struct timespec64 *orig, 10459 const struct timespec64 *now) 10460 { 10461 10462 /* 10463 * "When the time presented is before the original time, then the 10464 * update is ignored." Also no need to update if there is no change. 10465 */ 10466 if (timespec64_compare(req, orig) <= 0) 10467 return false; 10468 10469 /* 10470 * "When the time presented is in the future, the server can either 10471 * clamp the new time to the current time, or it may 10472 * return NFS4ERR_DELAY to the client, allowing it to retry." 10473 */ 10474 if (timespec64_compare(req, now) > 0) 10475 *req = *now; 10476 10477 return true; 10478 } 10479 10480 static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation *dp) 10481 { 10482 struct inode *inode = d_inode(dentry); 10483 struct nfs4_cb_fattr *ncf = &dp->dl_cb_fattr; 10484 struct iattr attrs = { }; 10485 int ret; 10486 10487 if (deleg_attrs_deleg(dp->dl_type)) { 10488 struct timespec64 now = current_time(inode); 10489 10490 attrs.ia_atime = ncf->ncf_cb_atime; 10491 attrs.ia_mtime = ncf->ncf_cb_mtime; 10492 10493 if (nfsd4_vet_deleg_time(&attrs.ia_atime, &dp->dl_atime, &now)) 10494 attrs.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET; 10495 10496 if (nfsd4_vet_deleg_time(&attrs.ia_mtime, &dp->dl_mtime, &now)) { 10497 attrs.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET; 10498 attrs.ia_ctime = attrs.ia_mtime; 10499 if (nfsd4_vet_deleg_time(&attrs.ia_ctime, &dp->dl_ctime, &now)) 10500 attrs.ia_valid |= ATTR_CTIME | ATTR_CTIME_SET; 10501 } 10502 } else { 10503 attrs.ia_valid |= ATTR_MTIME | ATTR_CTIME; 10504 } 10505 10506 if (!attrs.ia_valid) 10507 return 0; 10508 10509 attrs.ia_valid |= ATTR_DELEG; 10510 inode_lock(inode); 10511 ret = notify_change(&nop_mnt_idmap, dentry, &attrs, NULL); 10512 inode_unlock(inode); 10513 return ret; 10514 } 10515 10516 /** 10517 * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict 10518 * @rqstp: RPC transaction context 10519 * @dentry: dentry of inode to be checked for a conflict 10520 * @pdp: returned WRITE delegation, if one was found 10521 * 10522 * This function is called when there is a conflict between a write 10523 * delegation and a change/size GETATTR from another client. The server 10524 * must either use the CB_GETATTR to get the current values of the 10525 * attributes from the client that holds the delegation or recall the 10526 * delegation before replying to the GETATTR. See RFC 8881 section 10527 * 18.7.4. 10528 * 10529 * Returns 0 if there is no conflict; otherwise an nfs_stat 10530 * code is returned. If @pdp is set to a non-NULL value, then the 10531 * caller must put the reference. 10532 */ 10533 __be32 10534 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry, 10535 struct nfs4_delegation **pdp) 10536 { 10537 struct nfsd_thread_local_info *ntli = rqstp->rq_private; 10538 struct file_lock_context *ctx; 10539 struct nfs4_delegation *dp = NULL; 10540 struct file_lease *fl; 10541 struct nfs4_cb_fattr *ncf; 10542 struct inode *inode = d_inode(dentry); 10543 __be32 status; 10544 10545 ctx = locks_inode_context(inode); 10546 if (!ctx) 10547 return nfs_ok; 10548 10549 #define NON_NFSD_LEASE ((void *)1) 10550 10551 spin_lock(&ctx->flc_lock); 10552 for_each_file_lock(fl, &ctx->flc_lease) { 10553 if (fl->c.flc_flags == FL_LAYOUT) 10554 continue; 10555 if (fl->c.flc_type == F_WRLCK) { 10556 if (fl->fl_lmops == &nfsd_lease_mng_ops) 10557 dp = fl->c.flc_owner; 10558 else 10559 dp = NON_NFSD_LEASE; 10560 } 10561 break; 10562 } 10563 if (dp == NULL || dp == NON_NFSD_LEASE || 10564 dp->dl_recall.cb_clp == *(ntli->ntli_lease_breaker)) { 10565 spin_unlock(&ctx->flc_lock); 10566 if (dp == NON_NFSD_LEASE) { 10567 status = nfserrno(nfsd_open_break_lease(inode, 10568 NFSD_MAY_READ)); 10569 if (status != nfserr_jukebox || 10570 !nfsd_wait_for_delegreturn(rqstp, inode)) 10571 return status; 10572 } 10573 return 0; 10574 } 10575 10576 refcount_inc(&dp->dl_stid.sc_count); 10577 ncf = &dp->dl_cb_fattr; 10578 nfs4_cb_getattr(&dp->dl_cb_fattr); 10579 spin_unlock(&ctx->flc_lock); 10580 10581 wait_on_bit_timeout(&ncf->ncf_getattr.cb_flags, NFSD4_CALLBACK_RUNNING, 10582 TASK_UNINTERRUPTIBLE, NFSD_CB_GETATTR_TIMEOUT); 10583 if (ncf->ncf_cb_status) { 10584 /* Recall delegation only if client didn't respond */ 10585 status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ)); 10586 if (status != nfserr_jukebox || 10587 !nfsd_wait_for_delegreturn(rqstp, inode)) 10588 goto out_status; 10589 status = nfs_ok; 10590 goto out_status; 10591 } 10592 if (!ncf->ncf_file_modified) { 10593 if (ncf->ncf_initial_cinfo != ncf->ncf_cb_change) 10594 ncf->ncf_file_modified = true; 10595 else if (i_size_read(inode) != ncf->ncf_cb_fsize) 10596 ncf->ncf_file_modified = true; 10597 } 10598 if (ncf->ncf_file_modified) { 10599 int err; 10600 10601 /* 10602 * Per section 10.4.3 of RFC 8881, the server would 10603 * not update the file's metadata with the client's 10604 * modified size 10605 */ 10606 err = cb_getattr_update_times(dentry, dp); 10607 if (err) { 10608 status = nfserrno(err); 10609 goto out_status; 10610 } 10611 ncf->ncf_cur_fsize = ncf->ncf_cb_fsize; 10612 *pdp = dp; 10613 return nfs_ok; 10614 } 10615 status = nfs_ok; 10616 out_status: 10617 nfs4_put_stid(&dp->dl_stid); 10618 return status; 10619 } 10620 10621 #define GDD_WORD0_CHILD_ATTRS (FATTR4_WORD0_TYPE | \ 10622 FATTR4_WORD0_CHANGE | \ 10623 FATTR4_WORD0_SIZE | \ 10624 FATTR4_WORD0_FILEID | \ 10625 FATTR4_WORD0_FILEHANDLE) 10626 10627 #define GDD_WORD1_CHILD_ATTRS (FATTR4_WORD1_MODE | \ 10628 FATTR4_WORD1_NUMLINKS | \ 10629 FATTR4_WORD1_RAWDEV | \ 10630 FATTR4_WORD1_SPACE_USED | \ 10631 FATTR4_WORD1_TIME_ACCESS | \ 10632 FATTR4_WORD1_TIME_METADATA | \ 10633 FATTR4_WORD1_TIME_MODIFY | \ 10634 FATTR4_WORD1_TIME_CREATE) 10635 10636 #define GDD_WORD0_DIR_ATTRS (FATTR4_WORD0_CHANGE | \ 10637 FATTR4_WORD0_SIZE) 10638 10639 #define GDD_WORD1_DIR_ATTRS (FATTR4_WORD1_NUMLINKS | \ 10640 FATTR4_WORD1_SPACE_USED | \ 10641 FATTR4_WORD1_TIME_ACCESS | \ 10642 FATTR4_WORD1_TIME_METADATA | \ 10643 FATTR4_WORD1_TIME_MODIFY) 10644 10645 /** 10646 * nfsd_get_dir_deleg - attempt to get a directory delegation 10647 * @cstate: compound state 10648 * @gdd: GET_DIR_DELEGATION arg/resp structure 10649 * @nf: nfsd_file opened on the directory 10650 * 10651 * Given a GET_DIR_DELEGATION request @gdd, attempt to acquire a delegation 10652 * on the directory to which @nf refers. 10653 */ 10654 struct nfs4_delegation * 10655 nfsd_get_dir_deleg(struct nfsd4_compound_state *cstate, 10656 struct nfsd4_get_dir_delegation *gdd, 10657 struct nfsd_file *nf) 10658 { 10659 struct nfs4_client *clp = cstate->clp; 10660 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 10661 struct nfs4_delegation *dp; 10662 struct file_lease *fl; 10663 struct nfs4_file *fp, *rfp; 10664 int status = 0; 10665 10666 fp = nfsd4_alloc_file(); 10667 if (!fp) 10668 return ERR_PTR(-ENOMEM); 10669 10670 nfsd4_file_init(&cstate->current_fh, fp); 10671 10672 rfp = nfsd4_file_hash_insert(fp, &cstate->current_fh); 10673 if (unlikely(!rfp)) { 10674 put_nfs4_file(fp); 10675 return ERR_PTR(-ENOMEM); 10676 } 10677 10678 if (rfp != fp) { 10679 put_nfs4_file(fp); 10680 fp = rfp; 10681 } 10682 10683 /* if this client already has one, return that it's unavailable */ 10684 spin_lock(&nn->deleg_lock); 10685 spin_lock(&fp->fi_lock); 10686 /* existing delegation? */ 10687 if (nfs4_delegation_exists(clp, fp)) { 10688 status = -EAGAIN; 10689 } else if (!rcu_dereference_protected(fp->fi_deleg_file, 10690 lockdep_is_held(&fp->fi_lock))) { 10691 rcu_assign_pointer(fp->fi_deleg_file, nfsd_file_get(nf)); 10692 fp->fi_delegees = 1; 10693 } else { 10694 ++fp->fi_delegees; 10695 } 10696 spin_unlock(&fp->fi_lock); 10697 spin_unlock(&nn->deleg_lock); 10698 10699 if (status) { 10700 put_nfs4_file(fp); 10701 return ERR_PTR(status); 10702 } 10703 10704 /* Try to set up the lease */ 10705 status = -ENOMEM; 10706 dp = alloc_init_dir_deleg(clp, fp); 10707 if (!dp) 10708 goto out_delegees; 10709 nfs4_delegation_record_grant_slot(dp, cstate); 10710 if (cstate->current_fh.fh_export) 10711 dp->dl_stid.sc_export = 10712 exp_get(cstate->current_fh.fh_export); 10713 10714 /* 10715 * NB: gddr_notification[0] represents the notifications that 10716 * will be granted to the client 10717 */ 10718 dp->dl_notify_mask = gdd->gddr_notification[0]; 10719 dp->dl_child_attrs[0] = gdd->gdda_child_attributes[0] & GDD_WORD0_CHILD_ATTRS; 10720 dp->dl_child_attrs[1] = gdd->gdda_child_attributes[1] & GDD_WORD1_CHILD_ATTRS; 10721 dp->dl_dir_attrs[0] = gdd->gdda_dir_attributes[0] & GDD_WORD0_DIR_ATTRS; 10722 dp->dl_dir_attrs[1] = gdd->gdda_dir_attributes[1] & GDD_WORD1_DIR_ATTRS; 10723 10724 fl = nfs4_alloc_init_lease(dp, dp->dl_notify_mask); 10725 if (!fl) 10726 goto out_put_stid; 10727 10728 status = kernel_setlease(nf->nf_file, 10729 fl->c.flc_type, &fl, NULL); 10730 if (fl) 10731 locks_free_lease(fl); 10732 if (status) 10733 goto out_put_stid; 10734 10735 /* 10736 * Now, try to hash it. This can fail if we race another nfsd task 10737 * trying to set a delegation on the same file. If that happens, 10738 * then just say UNAVAIL. 10739 */ 10740 spin_lock(&nn->deleg_lock); 10741 spin_lock(&clp->cl_lock); 10742 spin_lock(&fp->fi_lock); 10743 status = hash_delegation_locked(dp, fp); 10744 spin_unlock(&fp->fi_lock); 10745 spin_unlock(&clp->cl_lock); 10746 spin_unlock(&nn->deleg_lock); 10747 10748 if (!status) { 10749 put_nfs4_file(fp); 10750 nfsd_fsnotify_recalc_mask(nf); 10751 return dp; 10752 } 10753 10754 /* 10755 * Something failed after the lease was set. Drop the lease and clean 10756 * up the stid. The lease's flc_file is the fi_deleg_file (see 10757 * nfs4_alloc_init_lease()), which is not necessarily this client's 10758 * @nf when an earlier client already holds a delegation on @fp. 10759 * generic_delete_lease() matches on flc_file, so unlock against 10760 * fi_deleg_file or the lease will be leaked (and later freed with the 10761 * stid, leading to a use-after-free when it's eventually broken). 10762 */ 10763 kernel_setlease(rcu_dereference_protected(fp->fi_deleg_file, 1)->nf_file, 10764 F_UNLCK, NULL, (void **)&dp); 10765 nfsd_fsnotify_recalc_mask(nf); 10766 out_put_stid: 10767 nfs4_put_stid(&dp->dl_stid); 10768 out_delegees: 10769 put_deleg_file(fp); 10770 put_nfs4_file(fp); 10771 return ERR_PTR(status); 10772 } 10773 10774 /** 10775 * nfsd_update_cmtime_attr - update file's delegated ctime/mtime, 10776 * and optionally other attributes (ie ATTR_ATIME). 10777 * @f: pointer to an opened file 10778 * @flags: any additional flags that should be updated 10779 * 10780 * Given upon opening a file delegated attributes were issues, update 10781 * @f attributes to current times. 10782 */ 10783 void nfsd_update_cmtime_attr(struct file *f, unsigned int flags) 10784 { 10785 int ret; 10786 struct inode *inode = file_inode(f); 10787 struct iattr attr = { 10788 .ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_DELEG | flags, 10789 }; 10790 10791 inode_lock(inode); 10792 ret = notify_change(&nop_mnt_idmap, f->f_path.dentry, &attr, NULL); 10793 inode_unlock(inode); 10794 if (ret) 10795 pr_notice_ratelimited("nfsd: Unable to update timestamps on " 10796 "inode %02x:%02x:%llu: %d\n", 10797 MAJOR(inode->i_sb->s_dev), 10798 MINOR(inode->i_sb->s_dev), 10799 inode->i_ino, ret); 10800 } 10801 10802 static void 10803 nfsd4_run_cb_notify(struct nfsd4_cb_notify *ncn) 10804 { 10805 struct nfs4_delegation *dp = container_of(ncn, struct nfs4_delegation, dl_cb_notify); 10806 10807 if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &ncn->ncn_cb.cb_flags)) 10808 return; 10809 10810 if (!refcount_inc_not_zero(&dp->dl_stid.sc_count)) 10811 clear_bit(NFSD4_CALLBACK_RUNNING, &ncn->ncn_cb.cb_flags); 10812 else 10813 nfsd4_run_cb(&ncn->ncn_cb); 10814 } 10815 10816 static struct nfsd_notify_event * 10817 alloc_nfsd_notify_event(u32 mask, const struct qstr *q, struct dentry *dentry, 10818 struct inode *target) 10819 { 10820 struct nfsd_notify_event *ne; 10821 struct name_snapshot newname; 10822 u32 newnamelen = 0; 10823 10824 /* 10825 * For a rename, @q is the old name and the live dentry carries the new 10826 * name. Snapshot the new name now, while it is guaranteed to describe 10827 * this event: the dentry can be renamed again before the CB_NOTIFY work 10828 * runs, which would corrupt a late read in nfsd4_encode_notify_event(). 10829 */ 10830 if (mask & FS_RENAME) { 10831 take_dentry_name_snapshot(&newname, dentry); 10832 newnamelen = newname.name.len; 10833 } 10834 10835 ne = kmalloc_flex(*ne, ne_name, 10836 q->len + 1 + (newnamelen ? newnamelen + 1 : 0), 10837 GFP_NOFS); 10838 if (!ne) 10839 goto out; 10840 10841 memcpy(ne->ne_name, q->name, q->len); 10842 ne->ne_name[q->len] = '\0'; 10843 ne->ne_namelen = q->len; 10844 10845 ne->ne_newnamelen = newnamelen; 10846 if (newnamelen) { 10847 char *p = nfsd_notify_event_newname(ne); 10848 10849 memcpy(p, newname.name.name, newnamelen); 10850 p[newnamelen] = '\0'; 10851 } 10852 10853 refcount_set(&ne->ne_ref, 1); 10854 ne->ne_mask = mask; 10855 ne->ne_dentry = dget(dentry); 10856 ne->ne_target = target; 10857 if (ne->ne_target) 10858 ihold(ne->ne_target); 10859 out: 10860 if (mask & FS_RENAME) 10861 release_dentry_name_snapshot(&newname); 10862 return ne; 10863 } 10864 10865 static bool 10866 should_notify_deleg(u32 mask, struct file_lease *fl) 10867 { 10868 /* Don't notify the client generating the event */ 10869 if (nfsd_breaker_owns_lease(fl)) 10870 return false; 10871 10872 /* Skip if this event wasn't ignored by the lease */ 10873 if ((mask & FS_DELETE) && !(fl->c.flc_flags & FL_IGN_DIR_DELETE)) 10874 return false; 10875 if ((mask & FS_CREATE) && !(fl->c.flc_flags & FL_IGN_DIR_CREATE)) 10876 return false; 10877 if ((mask & FS_RENAME) && !(fl->c.flc_flags & FL_IGN_DIR_RENAME)) 10878 return false; 10879 10880 return true; 10881 } 10882 10883 static void 10884 nfsd_recall_all_dir_delegs(const struct inode *dir) 10885 { 10886 struct file_lock_context *ctx = locks_inode_context(dir); 10887 struct file_lock_core *flc; 10888 10889 spin_lock(&ctx->flc_lock); 10890 list_for_each_entry(flc, &ctx->flc_lease, flc_list) { 10891 struct file_lease *fl = container_of(flc, struct file_lease, c); 10892 10893 if (fl->fl_lmops == &nfsd_lease_mng_ops) 10894 nfsd_break_deleg_cb(fl); 10895 } 10896 spin_unlock(&ctx->flc_lock); 10897 } 10898 10899 int 10900 nfsd_handle_dir_event(u32 mask, const struct inode *dir, const void *data, 10901 int data_type, const struct qstr *name) 10902 { 10903 struct dentry *dentry = fsnotify_data_dentry(data, data_type); 10904 struct inode *target = fsnotify_data_rename_target(data, data_type); 10905 struct file_lock_context *ctx; 10906 struct file_lock_core *flc; 10907 struct nfsd_notify_event *evt; 10908 10909 trace_nfsd_handle_dir_event(mask, dir, name); 10910 10911 /* Normalize cross-dir rename events to create/delete */ 10912 if (mask & FS_MOVED_FROM) { 10913 mask &= ~FS_MOVED_FROM; 10914 mask |= FS_DELETE; 10915 } 10916 if (mask & FS_MOVED_TO) { 10917 mask &= ~FS_MOVED_TO; 10918 mask |= FS_CREATE; 10919 } 10920 10921 /* 10922 * FS_RENAME fires on the source directory even for a cross-dir 10923 * rename, where the moved entry now lives under a different parent. 10924 * NOTIFY4_RENAME_ENTRY describes an in-place rename, so reporting it 10925 * here would advertise a name absent from this directory. 10926 */ 10927 if ((mask & FS_RENAME) && dentry && d_inode(dentry->d_parent) != dir) 10928 mask &= ~FS_RENAME; 10929 10930 /* Don't do anything if this is not an expected event */ 10931 if (!(mask & (FS_CREATE|FS_DELETE|FS_RENAME))) 10932 return 0; 10933 10934 ctx = locks_inode_context(dir); 10935 if (!ctx || list_empty(&ctx->flc_lease)) 10936 return 0; 10937 10938 evt = alloc_nfsd_notify_event(mask, name, dentry, target); 10939 if (!evt) { 10940 nfsd_recall_all_dir_delegs(dir); 10941 return 0; 10942 } 10943 10944 spin_lock(&ctx->flc_lock); 10945 list_for_each_entry(flc, &ctx->flc_lease, flc_list) { 10946 struct file_lease *fl = container_of(flc, struct file_lease, c); 10947 struct nfs4_delegation *dp = flc->flc_owner; 10948 struct nfsd4_cb_notify *ncn = &dp->dl_cb_notify; 10949 10950 if (!should_notify_deleg(mask, fl)) 10951 continue; 10952 10953 spin_lock(&ncn->ncn_lock); 10954 if (ncn->ncn_evt_cnt >= NOTIFY4_EVENT_QUEUE_SIZE) { 10955 /* We're generating notifications too fast. Recall. */ 10956 spin_unlock(&ncn->ncn_lock); 10957 nfsd_break_deleg_cb(fl); 10958 continue; 10959 } 10960 ncn->ncn_evt[ncn->ncn_evt_cnt++] = nfsd_notify_event_get(evt); 10961 spin_unlock(&ncn->ncn_lock); 10962 10963 nfsd4_run_cb_notify(ncn); 10964 } 10965 spin_unlock(&ctx->flc_lock); 10966 nfsd_notify_event_put(evt); 10967 return 0; 10968 } 10969