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