1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T. 28 * All Rights Reserved 29 */ 30 31 #include <sys/param.h> 32 #include <sys/types.h> 33 #include <sys/systm.h> 34 #include <sys/thread.h> 35 #include <sys/t_lock.h> 36 #include <sys/time.h> 37 #include <sys/vnode.h> 38 #include <sys/vfs.h> 39 #include <sys/errno.h> 40 #include <sys/buf.h> 41 #include <sys/stat.h> 42 #include <sys/cred.h> 43 #include <sys/kmem.h> 44 #include <sys/debug.h> 45 #include <sys/dnlc.h> 46 #include <sys/vmsystm.h> 47 #include <sys/flock.h> 48 #include <sys/share.h> 49 #include <sys/cmn_err.h> 50 #include <sys/tiuser.h> 51 #include <sys/sysmacros.h> 52 #include <sys/callb.h> 53 #include <sys/acl.h> 54 #include <sys/kstat.h> 55 #include <sys/signal.h> 56 #include <sys/disp.h> 57 #include <sys/atomic.h> 58 #include <sys/list.h> 59 #include <sys/sdt.h> 60 61 #include <rpc/types.h> 62 #include <rpc/xdr.h> 63 #include <rpc/auth.h> 64 #include <rpc/clnt.h> 65 66 #include <nfs/nfs.h> 67 #include <nfs/nfs_clnt.h> 68 #include <nfs/nfs_acl.h> 69 70 #include <nfs/nfs4.h> 71 #include <nfs/rnode4.h> 72 #include <nfs/nfs4_clnt.h> 73 74 #include <vm/hat.h> 75 #include <vm/as.h> 76 #include <vm/page.h> 77 #include <vm/pvn.h> 78 #include <vm/seg.h> 79 #include <vm/seg_map.h> 80 #include <vm/seg_vn.h> 81 82 #include <sys/ddi.h> 83 84 /* 85 * Arguments to page-flush thread. 86 */ 87 typedef struct { 88 vnode_t *vp; 89 cred_t *cr; 90 } pgflush_t; 91 92 #ifdef DEBUG 93 int nfs4_client_lease_debug; 94 int nfs4_sharedfh_debug; 95 int nfs4_fname_debug; 96 97 /* temporary: panic if v_type is inconsistent with r_attr va_type */ 98 int nfs4_vtype_debug; 99 100 uint_t nfs4_tsd_key; 101 #endif 102 103 static time_t nfs4_client_resumed = 0; 104 static callb_id_t cid = 0; 105 106 static int nfs4renew(nfs4_server_t *); 107 static void nfs4_attrcache_va(vnode_t *, nfs4_ga_res_t *, int); 108 static void nfs4_pgflush_thread(pgflush_t *); 109 110 static boolean_t nfs4_client_cpr_callb(void *, int); 111 112 struct mi4_globals { 113 kmutex_t mig_lock; /* lock protecting mig_list */ 114 list_t mig_list; /* list of NFS v4 mounts in zone */ 115 boolean_t mig_destructor_called; 116 }; 117 118 static zone_key_t mi4_list_key; 119 120 /* 121 * Attributes caching: 122 * 123 * Attributes are cached in the rnode in struct vattr form. 124 * There is a time associated with the cached attributes (r_time_attr_inval) 125 * which tells whether the attributes are valid. The time is initialized 126 * to the difference between current time and the modify time of the vnode 127 * when new attributes are cached. This allows the attributes for 128 * files that have changed recently to be timed out sooner than for files 129 * that have not changed for a long time. There are minimum and maximum 130 * timeout values that can be set per mount point. 131 */ 132 133 /* 134 * If a cache purge is in progress, wait for it to finish. 135 * 136 * The current thread must not be in the middle of an 137 * nfs4_start_op/nfs4_end_op region. Otherwise, there could be a deadlock 138 * between this thread, a recovery thread, and the page flush thread. 139 */ 140 int 141 nfs4_waitfor_purge_complete(vnode_t *vp) 142 { 143 rnode4_t *rp; 144 k_sigset_t smask; 145 146 rp = VTOR4(vp); 147 if ((rp->r_serial != NULL && rp->r_serial != curthread) || 148 ((rp->r_flags & R4PGFLUSH) && rp->r_pgflush != curthread)) { 149 mutex_enter(&rp->r_statelock); 150 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT); 151 while ((rp->r_serial != NULL && rp->r_serial != curthread) || 152 ((rp->r_flags & R4PGFLUSH) && 153 rp->r_pgflush != curthread)) { 154 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 155 sigunintr(&smask); 156 mutex_exit(&rp->r_statelock); 157 return (EINTR); 158 } 159 } 160 sigunintr(&smask); 161 mutex_exit(&rp->r_statelock); 162 } 163 return (0); 164 } 165 166 /* 167 * Validate caches by checking cached attributes. If they have timed out, 168 * then get new attributes from the server. As a side effect, cache 169 * invalidation is done if the attributes have changed. 170 * 171 * If the attributes have not timed out and if there is a cache 172 * invalidation being done by some other thread, then wait until that 173 * thread has completed the cache invalidation. 174 */ 175 int 176 nfs4_validate_caches(vnode_t *vp, cred_t *cr) 177 { 178 int error; 179 nfs4_ga_res_t gar; 180 181 if (ATTRCACHE4_VALID(vp)) { 182 error = nfs4_waitfor_purge_complete(vp); 183 if (error) 184 return (error); 185 return (0); 186 } 187 188 gar.n4g_va.va_mask = AT_ALL; 189 return (nfs4_getattr_otw(vp, &gar, cr, 0)); 190 } 191 192 /* 193 * Fill in attribute from the cache. 194 * If valid, then return 0 to indicate that no error occurred, 195 * otherwise return 1 to indicate that an error occurred. 196 */ 197 static int 198 nfs4_getattr_cache(vnode_t *vp, struct vattr *vap) 199 { 200 rnode4_t *rp; 201 202 rp = VTOR4(vp); 203 mutex_enter(&rp->r_statelock); 204 mutex_enter(&rp->r_statev4_lock); 205 if (ATTRCACHE4_VALID(vp)) { 206 mutex_exit(&rp->r_statev4_lock); 207 /* 208 * Cached attributes are valid 209 */ 210 *vap = rp->r_attr; 211 mutex_exit(&rp->r_statelock); 212 return (0); 213 } 214 mutex_exit(&rp->r_statev4_lock); 215 mutex_exit(&rp->r_statelock); 216 return (1); 217 } 218 219 220 /* 221 * If returned error is ESTALE flush all caches. The nfs4_purge_caches() 222 * call is synchronous because all the pages were invalidated by the 223 * nfs4_invalidate_pages() call. 224 */ 225 void 226 nfs4_purge_stale_fh(int errno, vnode_t *vp, cred_t *cr) 227 { 228 struct rnode4 *rp = VTOR4(vp); 229 230 /* Ensure that the ..._end_op() call has been done */ 231 ASSERT(tsd_get(nfs4_tsd_key) == NULL); 232 233 if (errno != ESTALE) 234 return; 235 236 mutex_enter(&rp->r_statelock); 237 rp->r_flags |= R4STALE; 238 if (!rp->r_error) 239 rp->r_error = errno; 240 mutex_exit(&rp->r_statelock); 241 if (nfs4_has_pages(vp)) 242 nfs4_invalidate_pages(vp, (u_offset_t)0, cr); 243 nfs4_purge_caches(vp, NFS4_PURGE_DNLC, cr, FALSE); 244 } 245 246 /* 247 * Purge all of the various NFS `data' caches. If "asyncpg" is TRUE, the 248 * page purge is done asynchronously. 249 */ 250 void 251 nfs4_purge_caches(vnode_t *vp, int purge_dnlc, cred_t *cr, int asyncpg) 252 { 253 rnode4_t *rp; 254 char *contents; 255 vnode_t *xattr; 256 int size; 257 int pgflush; /* are we the page flush thread? */ 258 259 /* 260 * Purge the DNLC for any entries which refer to this file. 261 */ 262 if (vp->v_count > 1 && 263 (vp->v_type == VDIR || purge_dnlc == NFS4_PURGE_DNLC)) 264 dnlc_purge_vp(vp); 265 266 /* 267 * Clear any readdir state bits and purge the readlink response cache. 268 */ 269 rp = VTOR4(vp); 270 mutex_enter(&rp->r_statelock); 271 rp->r_flags &= ~R4LOOKUP; 272 contents = rp->r_symlink.contents; 273 size = rp->r_symlink.size; 274 rp->r_symlink.contents = NULL; 275 276 xattr = rp->r_xattr_dir; 277 rp->r_xattr_dir = NULL; 278 279 /* 280 * Purge pathconf cache too. 281 */ 282 rp->r_pathconf.pc4_xattr_valid = 0; 283 rp->r_pathconf.pc4_cache_valid = 0; 284 285 pgflush = (curthread == rp->r_pgflush); 286 mutex_exit(&rp->r_statelock); 287 288 if (contents != NULL) { 289 290 kmem_free((void *)contents, size); 291 } 292 293 if (xattr != NULL) 294 VN_RELE(xattr); 295 296 /* 297 * Flush the page cache. If the current thread is the page flush 298 * thread, don't initiate a new page flush. There's no need for 299 * it, and doing it correctly is hard. 300 */ 301 if (nfs4_has_pages(vp) && !pgflush) { 302 if (!asyncpg) { 303 (void) nfs4_waitfor_purge_complete(vp); 304 nfs4_flush_pages(vp, cr); 305 } else { 306 pgflush_t *args; 307 308 /* 309 * We don't hold r_statelock while creating the 310 * thread, in case the call blocks. So we use a 311 * flag to indicate that a page flush thread is 312 * active. 313 */ 314 mutex_enter(&rp->r_statelock); 315 if (rp->r_flags & R4PGFLUSH) { 316 mutex_exit(&rp->r_statelock); 317 } else { 318 rp->r_flags |= R4PGFLUSH; 319 mutex_exit(&rp->r_statelock); 320 321 args = kmem_alloc(sizeof (pgflush_t), 322 KM_SLEEP); 323 args->vp = vp; 324 VN_HOLD(args->vp); 325 args->cr = cr; 326 crhold(args->cr); 327 (void) zthread_create(NULL, 0, 328 nfs4_pgflush_thread, args, 0, 329 minclsyspri); 330 } 331 } 332 } 333 334 /* 335 * Flush the readdir response cache. 336 */ 337 nfs4_purge_rddir_cache(vp); 338 } 339 340 /* 341 * Invalidate all pages for the given file, after writing back the dirty 342 * ones. 343 */ 344 345 void 346 nfs4_flush_pages(vnode_t *vp, cred_t *cr) 347 { 348 int error; 349 rnode4_t *rp = VTOR4(vp); 350 351 error = VOP_PUTPAGE(vp, (u_offset_t)0, 0, B_INVAL, cr, NULL); 352 if (error == ENOSPC || error == EDQUOT) { 353 mutex_enter(&rp->r_statelock); 354 if (!rp->r_error) 355 rp->r_error = error; 356 mutex_exit(&rp->r_statelock); 357 } 358 } 359 360 /* 361 * Page flush thread. 362 */ 363 364 static void 365 nfs4_pgflush_thread(pgflush_t *args) 366 { 367 rnode4_t *rp = VTOR4(args->vp); 368 369 /* remember which thread we are, so we don't deadlock ourselves */ 370 mutex_enter(&rp->r_statelock); 371 ASSERT(rp->r_pgflush == NULL); 372 rp->r_pgflush = curthread; 373 mutex_exit(&rp->r_statelock); 374 375 nfs4_flush_pages(args->vp, args->cr); 376 377 mutex_enter(&rp->r_statelock); 378 rp->r_pgflush = NULL; 379 rp->r_flags &= ~R4PGFLUSH; 380 cv_broadcast(&rp->r_cv); 381 mutex_exit(&rp->r_statelock); 382 383 VN_RELE(args->vp); 384 crfree(args->cr); 385 kmem_free(args, sizeof (pgflush_t)); 386 zthread_exit(); 387 } 388 389 /* 390 * Purge the readdir cache of all entries which are not currently 391 * being filled. 392 */ 393 void 394 nfs4_purge_rddir_cache(vnode_t *vp) 395 { 396 rnode4_t *rp; 397 398 rp = VTOR4(vp); 399 400 mutex_enter(&rp->r_statelock); 401 rp->r_direof = NULL; 402 rp->r_flags &= ~R4LOOKUP; 403 rp->r_flags |= R4READDIRWATTR; 404 rddir4_cache_purge(rp); 405 mutex_exit(&rp->r_statelock); 406 } 407 408 /* 409 * Set attributes cache for given vnode using virtual attributes. There is 410 * no cache validation, but if the attributes are deemed to be stale, they 411 * are ignored. This corresponds to nfs3_attrcache(). 412 * 413 * Set the timeout value on the attribute cache and fill it 414 * with the passed in attributes. 415 */ 416 void 417 nfs4_attrcache_noinval(vnode_t *vp, nfs4_ga_res_t *garp, hrtime_t t) 418 { 419 rnode4_t *rp = VTOR4(vp); 420 421 mutex_enter(&rp->r_statelock); 422 if (rp->r_time_attr_saved <= t) 423 nfs4_attrcache_va(vp, garp, FALSE); 424 mutex_exit(&rp->r_statelock); 425 } 426 427 /* 428 * Use the passed in virtual attributes to check to see whether the 429 * data and metadata caches are valid, cache the new attributes, and 430 * then do the cache invalidation if required. 431 * 432 * The cache validation and caching of the new attributes is done 433 * atomically via the use of the mutex, r_statelock. If required, 434 * the cache invalidation is done atomically w.r.t. the cache 435 * validation and caching of the attributes via the pseudo lock, 436 * r_serial. 437 * 438 * This routine is used to do cache validation and attributes caching 439 * for operations with a single set of post operation attributes. 440 */ 441 442 void 443 nfs4_attr_cache(vnode_t *vp, nfs4_ga_res_t *garp, 444 hrtime_t t, cred_t *cr, int async, 445 change_info4 *cinfo) 446 { 447 rnode4_t *rp; 448 int mtime_changed = 0; 449 int ctime_changed = 0; 450 vsecattr_t *vsp; 451 int was_serial, set_time_cache_inval, recov; 452 vattr_t *vap = &garp->n4g_va; 453 mntinfo4_t *mi = VTOMI4(vp); 454 len_t preattr_rsize; 455 boolean_t writemodify_set = B_FALSE; 456 boolean_t cachepurge_set = B_FALSE; 457 458 ASSERT(mi->mi_vfsp->vfs_dev == garp->n4g_va.va_fsid); 459 460 /* Is curthread the recovery thread? */ 461 mutex_enter(&mi->mi_lock); 462 recov = (VTOMI4(vp)->mi_recovthread == curthread); 463 mutex_exit(&mi->mi_lock); 464 465 rp = VTOR4(vp); 466 mutex_enter(&rp->r_statelock); 467 was_serial = (rp->r_serial == curthread); 468 if (rp->r_serial && !was_serial) { 469 klwp_t *lwp = ttolwp(curthread); 470 471 /* 472 * If we're the recovery thread, then purge current attrs 473 * and bail out to avoid potential deadlock between another 474 * thread caching attrs (r_serial thread), recov thread, 475 * and an async writer thread. 476 */ 477 if (recov) { 478 PURGE_ATTRCACHE4_LOCKED(rp); 479 mutex_exit(&rp->r_statelock); 480 return; 481 } 482 483 if (lwp != NULL) 484 lwp->lwp_nostop++; 485 while (rp->r_serial != NULL) { 486 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 487 mutex_exit(&rp->r_statelock); 488 if (lwp != NULL) 489 lwp->lwp_nostop--; 490 return; 491 } 492 } 493 if (lwp != NULL) 494 lwp->lwp_nostop--; 495 } 496 497 /* 498 * If there is a page flush thread, the current thread needs to 499 * bail out, to prevent a possible deadlock between the current 500 * thread (which might be in a start_op/end_op region), the 501 * recovery thread, and the page flush thread. Expire the 502 * attribute cache, so that any attributes the current thread was 503 * going to set are not lost. 504 */ 505 if ((rp->r_flags & R4PGFLUSH) && rp->r_pgflush != curthread) { 506 PURGE_ATTRCACHE4_LOCKED(rp); 507 mutex_exit(&rp->r_statelock); 508 return; 509 } 510 511 if (rp->r_time_attr_saved > t) { 512 /* 513 * Attributes have been cached since these attributes were 514 * probably made. If there is an inconsistency in what is 515 * cached, mark them invalid. If not, don't act on them. 516 */ 517 if (!CACHE4_VALID(rp, vap->va_mtime, vap->va_size)) 518 PURGE_ATTRCACHE4_LOCKED(rp); 519 mutex_exit(&rp->r_statelock); 520 return; 521 } 522 set_time_cache_inval = 0; 523 if (cinfo) { 524 /* 525 * Only directory modifying callers pass non-NULL cinfo. 526 */ 527 ASSERT(vp->v_type == VDIR); 528 /* 529 * If the cache timeout either doesn't exist or hasn't expired, 530 * and dir didn't changed on server before dirmod op 531 * and dir didn't change after dirmod op but before getattr 532 * then there's a chance that the client's cached data for 533 * this object is current (not stale). No immediate cache 534 * flush is required. 535 * 536 */ 537 if ((! rp->r_time_cache_inval || t < rp->r_time_cache_inval) && 538 cinfo->before == rp->r_change && 539 (garp->n4g_change_valid && 540 cinfo->after == garp->n4g_change)) { 541 542 /* 543 * If atomic isn't set, then the before/after info 544 * cannot be blindly trusted. For this case, we tell 545 * nfs4_attrcache_va to cache the attrs but also 546 * establish an absolute maximum cache timeout. When 547 * the timeout is reached, caches will be flushed. 548 */ 549 if (! cinfo->atomic) 550 set_time_cache_inval = 1; 551 } else { 552 553 /* 554 * We're not sure exactly what changed, but we know 555 * what to do. flush all caches for dir. remove the 556 * attr timeout. 557 * 558 * a) timeout expired. flush all caches. 559 * b) r_change != cinfo.before. flush all caches. 560 * c) r_change == cinfo.before, but cinfo.after != 561 * post-op getattr(change). flush all caches. 562 * d) post-op getattr(change) not provided by server. 563 * flush all caches. 564 */ 565 mtime_changed = 1; 566 ctime_changed = 1; 567 rp->r_time_cache_inval = 0; 568 } 569 } else { 570 /* 571 * Write thread after writing data to file on remote server, 572 * will always set R4WRITEMODIFIED to indicate that file on 573 * remote server was modified with a WRITE operation and would 574 * have marked attribute cache as timed out. If R4WRITEMODIFIED 575 * is set, then do not check for mtime and ctime change. 576 */ 577 if (!(rp->r_flags & R4WRITEMODIFIED)) { 578 if (!CACHE4_VALID(rp, vap->va_mtime, vap->va_size)) 579 mtime_changed = 1; 580 581 if (rp->r_attr.va_ctime.tv_sec != 582 vap->va_ctime.tv_sec || 583 rp->r_attr.va_ctime.tv_nsec != 584 vap->va_ctime.tv_nsec) 585 ctime_changed = 1; 586 } else { 587 writemodify_set = B_TRUE; 588 } 589 } 590 591 preattr_rsize = rp->r_size; 592 593 nfs4_attrcache_va(vp, garp, set_time_cache_inval); 594 595 /* 596 * If we have updated filesize in nfs4_attrcache_va, as soon as we 597 * drop statelock we will be in transition of purging all 598 * our caches and updating them. It is possible for another 599 * thread to pick this new file size and read in zeroed data. 600 * stall other threads till cache purge is complete. 601 */ 602 if ((!cinfo) && (rp->r_size != preattr_rsize)) { 603 /* 604 * If R4WRITEMODIFIED was set and we have updated the file 605 * size, Server's returned file size need not necessarily 606 * be because of this Client's WRITE. We need to purge 607 * all caches. 608 */ 609 if (writemodify_set) 610 mtime_changed = 1; 611 612 if (mtime_changed && !(rp->r_flags & R4INCACHEPURGE)) { 613 rp->r_flags |= R4INCACHEPURGE; 614 cachepurge_set = B_TRUE; 615 } 616 } 617 618 if (!mtime_changed && !ctime_changed) { 619 mutex_exit(&rp->r_statelock); 620 return; 621 } 622 623 rp->r_serial = curthread; 624 625 mutex_exit(&rp->r_statelock); 626 627 /* 628 * If we're the recov thread, then force async nfs4_purge_caches 629 * to avoid potential deadlock. 630 */ 631 if (mtime_changed) 632 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, recov ? 1 : async); 633 634 if ((rp->r_flags & R4INCACHEPURGE) && cachepurge_set) { 635 mutex_enter(&rp->r_statelock); 636 rp->r_flags &= ~R4INCACHEPURGE; 637 cv_broadcast(&rp->r_cv); 638 mutex_exit(&rp->r_statelock); 639 cachepurge_set = B_FALSE; 640 } 641 642 if (ctime_changed) { 643 (void) nfs4_access_purge_rp(rp); 644 if (rp->r_secattr != NULL) { 645 mutex_enter(&rp->r_statelock); 646 vsp = rp->r_secattr; 647 rp->r_secattr = NULL; 648 mutex_exit(&rp->r_statelock); 649 if (vsp != NULL) 650 nfs4_acl_free_cache(vsp); 651 } 652 } 653 654 if (!was_serial) { 655 mutex_enter(&rp->r_statelock); 656 rp->r_serial = NULL; 657 cv_broadcast(&rp->r_cv); 658 mutex_exit(&rp->r_statelock); 659 } 660 } 661 662 /* 663 * Set attributes cache for given vnode using virtual attributes. 664 * 665 * Set the timeout value on the attribute cache and fill it 666 * with the passed in attributes. 667 * 668 * The caller must be holding r_statelock. 669 */ 670 static void 671 nfs4_attrcache_va(vnode_t *vp, nfs4_ga_res_t *garp, int set_cache_timeout) 672 { 673 rnode4_t *rp; 674 mntinfo4_t *mi; 675 hrtime_t delta; 676 hrtime_t now; 677 vattr_t *vap = &garp->n4g_va; 678 679 rp = VTOR4(vp); 680 681 ASSERT(MUTEX_HELD(&rp->r_statelock)); 682 ASSERT(vap->va_mask == AT_ALL); 683 684 /* Switch to master before checking v_flag */ 685 if (IS_SHADOW(vp, rp)) 686 vp = RTOV4(rp); 687 688 now = gethrtime(); 689 690 mi = VTOMI4(vp); 691 692 /* 693 * Only establish a new cache timeout (if requested). Never 694 * extend a timeout. Never clear a timeout. Clearing a timeout 695 * is done by nfs4_update_dircaches (ancestor in our call chain) 696 */ 697 if (set_cache_timeout && ! rp->r_time_cache_inval) 698 rp->r_time_cache_inval = now + mi->mi_acdirmax; 699 700 /* 701 * Delta is the number of nanoseconds that we will 702 * cache the attributes of the file. It is based on 703 * the number of nanoseconds since the last time that 704 * we detected a change. The assumption is that files 705 * that changed recently are likely to change again. 706 * There is a minimum and a maximum for regular files 707 * and for directories which is enforced though. 708 * 709 * Using the time since last change was detected 710 * eliminates direct comparison or calculation 711 * using mixed client and server times. NFS does 712 * not make any assumptions regarding the client 713 * and server clocks being synchronized. 714 */ 715 if (vap->va_mtime.tv_sec != rp->r_attr.va_mtime.tv_sec || 716 vap->va_mtime.tv_nsec != rp->r_attr.va_mtime.tv_nsec || 717 vap->va_size != rp->r_attr.va_size) { 718 rp->r_time_attr_saved = now; 719 } 720 721 if ((mi->mi_flags & MI4_NOAC) || (vp->v_flag & VNOCACHE)) 722 delta = 0; 723 else { 724 delta = now - rp->r_time_attr_saved; 725 if (vp->v_type == VDIR) { 726 if (delta < mi->mi_acdirmin) 727 delta = mi->mi_acdirmin; 728 else if (delta > mi->mi_acdirmax) 729 delta = mi->mi_acdirmax; 730 } else { 731 if (delta < mi->mi_acregmin) 732 delta = mi->mi_acregmin; 733 else if (delta > mi->mi_acregmax) 734 delta = mi->mi_acregmax; 735 } 736 } 737 rp->r_time_attr_inval = now + delta; 738 739 rp->r_attr = *vap; 740 if (garp->n4g_change_valid) 741 rp->r_change = garp->n4g_change; 742 743 /* 744 * The attributes that were returned may be valid and can 745 * be used, but they may not be allowed to be cached. 746 * Reset the timers to cause immediate invalidation and 747 * clear r_change so no VERIFY operations will suceed 748 */ 749 if (garp->n4g_attrwhy == NFS4_GETATTR_NOCACHE_OK) { 750 rp->r_time_attr_inval = now; 751 rp->r_time_attr_saved = now; 752 rp->r_change = 0; 753 } 754 755 /* 756 * If mounted_on_fileid returned AND the object is a stub, 757 * then set object's va_nodeid to the mounted over fid 758 * returned by server. 759 * 760 * If mounted_on_fileid not provided/supported, then 761 * just set it to 0 for now. Eventually it would be 762 * better to set it to a hashed version of FH. This 763 * would probably be good enough to provide a unique 764 * fid/d_ino within a dir. 765 * 766 * We don't need to carry mounted_on_fileid in the 767 * rnode as long as the client never requests fileid 768 * without also requesting mounted_on_fileid. For 769 * now, it stays. 770 */ 771 if (garp->n4g_mon_fid_valid) { 772 rp->r_mntd_fid = garp->n4g_mon_fid; 773 774 if (RP_ISSTUB(rp)) 775 rp->r_attr.va_nodeid = rp->r_mntd_fid; 776 } 777 778 /* 779 * Check to see if there are valid pathconf bits to 780 * cache in the rnode. 781 */ 782 if (garp->n4g_ext_res) { 783 if (garp->n4g_ext_res->n4g_pc4.pc4_cache_valid) { 784 rp->r_pathconf = garp->n4g_ext_res->n4g_pc4; 785 } else { 786 if (garp->n4g_ext_res->n4g_pc4.pc4_xattr_valid) { 787 rp->r_pathconf.pc4_xattr_valid = TRUE; 788 rp->r_pathconf.pc4_xattr_exists = 789 garp->n4g_ext_res->n4g_pc4.pc4_xattr_exists; 790 } 791 } 792 } 793 /* 794 * Update the size of the file if there is no cached data or if 795 * the cached data is clean and there is no data being written 796 * out. 797 */ 798 if (rp->r_size != vap->va_size && 799 (!vn_has_cached_data(vp) || 800 (!(rp->r_flags & R4DIRTY) && rp->r_count == 0))) { 801 rp->r_size = vap->va_size; 802 } 803 nfs_setswaplike(vp, vap); 804 rp->r_flags &= ~R4WRITEMODIFIED; 805 } 806 807 /* 808 * Get attributes over-the-wire and update attributes cache 809 * if no error occurred in the over-the-wire operation. 810 * Return 0 if successful, otherwise error. 811 */ 812 int 813 nfs4_getattr_otw(vnode_t *vp, nfs4_ga_res_t *garp, cred_t *cr, int get_acl) 814 { 815 mntinfo4_t *mi = VTOMI4(vp); 816 hrtime_t t; 817 nfs4_recov_state_t recov_state; 818 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 819 820 recov_state.rs_flags = 0; 821 recov_state.rs_num_retry_despite_err = 0; 822 823 /* Save the original mount point security flavor */ 824 (void) save_mnt_secinfo(mi->mi_curr_serv); 825 826 recov_retry: 827 828 if ((e.error = nfs4_start_fop(mi, vp, NULL, OH_GETATTR, 829 &recov_state, NULL))) { 830 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 831 return (e.error); 832 } 833 834 t = gethrtime(); 835 836 nfs4_getattr_otw_norecovery(vp, garp, &e, cr, get_acl); 837 838 if (nfs4_needs_recovery(&e, FALSE, vp->v_vfsp)) { 839 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 840 NULL, OP_GETATTR, NULL) == FALSE) { 841 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_GETATTR, 842 &recov_state, 1); 843 goto recov_retry; 844 } 845 } 846 847 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_GETATTR, &recov_state, 0); 848 849 if (!e.error) { 850 if (e.stat == NFS4_OK) { 851 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL); 852 } else { 853 e.error = geterrno4(e.stat); 854 855 nfs4_purge_stale_fh(e.error, vp, cr); 856 } 857 } 858 859 /* 860 * If getattr a node that is a stub for a crossed 861 * mount point, keep the original secinfo flavor for 862 * the current file system, not the crossed one. 863 */ 864 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 865 866 return (e.error); 867 } 868 869 /* 870 * Generate a compound to get attributes over-the-wire. 871 */ 872 void 873 nfs4_getattr_otw_norecovery(vnode_t *vp, nfs4_ga_res_t *garp, 874 nfs4_error_t *ep, cred_t *cr, int get_acl) 875 { 876 COMPOUND4args_clnt args; 877 COMPOUND4res_clnt res; 878 int doqueue; 879 rnode4_t *rp = VTOR4(vp); 880 nfs_argop4 argop[2]; 881 882 args.ctag = TAG_GETATTR; 883 884 args.array_len = 2; 885 args.array = argop; 886 887 /* putfh */ 888 argop[0].argop = OP_CPUTFH; 889 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 890 891 /* getattr */ 892 /* 893 * Unlike nfs version 2 and 3, where getattr returns all the 894 * attributes, nfs version 4 returns only the ones explicitly 895 * asked for. This creates problems, as some system functions 896 * (e.g. cache check) require certain attributes and if the 897 * cached node lacks some attributes such as uid/gid, it can 898 * affect system utilities (e.g. "ls") that rely on the information 899 * to be there. This can lead to anything from system crashes to 900 * corrupted information processed by user apps. 901 * So to ensure that all bases are covered, request at least 902 * the AT_ALL attribute mask. 903 */ 904 argop[1].argop = OP_GETATTR; 905 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 906 if (get_acl) 907 argop[1].nfs_argop4_u.opgetattr.attr_request |= FATTR4_ACL_MASK; 908 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 909 910 doqueue = 1; 911 912 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, ep); 913 914 if (ep->error) 915 return; 916 917 if (res.status != NFS4_OK) { 918 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 919 return; 920 } 921 922 *garp = res.array[1].nfs_resop4_u.opgetattr.ga_res; 923 924 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 925 } 926 927 /* 928 * Return either cached or remote attributes. If get remote attr 929 * use them to check and invalidate caches, then cache the new attributes. 930 */ 931 int 932 nfs4getattr(vnode_t *vp, vattr_t *vap, cred_t *cr) 933 { 934 int error; 935 rnode4_t *rp; 936 nfs4_ga_res_t gar; 937 938 ASSERT(nfs4_consistent_type(vp)); 939 940 /* 941 * If we've got cached attributes, we're done, otherwise go 942 * to the server to get attributes, which will update the cache 943 * in the process. Either way, use the cached attributes for 944 * the caller's vattr_t. 945 * 946 * Note that we ignore the gar set by the OTW call: the attr caching 947 * code may make adjustments when storing to the rnode, and we want 948 * to see those changes here. 949 */ 950 rp = VTOR4(vp); 951 error = 0; 952 mutex_enter(&rp->r_statelock); 953 if (!ATTRCACHE4_VALID(vp)) { 954 mutex_exit(&rp->r_statelock); 955 error = nfs4_getattr_otw(vp, &gar, cr, 0); 956 mutex_enter(&rp->r_statelock); 957 } 958 959 if (!error) 960 *vap = rp->r_attr; 961 962 /* Return the client's view of file size */ 963 vap->va_size = rp->r_size; 964 965 mutex_exit(&rp->r_statelock); 966 967 ASSERT(nfs4_consistent_type(vp)); 968 969 return (error); 970 } 971 972 int 973 nfs4_attr_otw(vnode_t *vp, nfs4_tag_type_t tag_type, 974 nfs4_ga_res_t *garp, bitmap4 reqbitmap, cred_t *cr) 975 { 976 COMPOUND4args_clnt args; 977 COMPOUND4res_clnt res; 978 int doqueue; 979 nfs_argop4 argop[2]; 980 mntinfo4_t *mi = VTOMI4(vp); 981 bool_t needrecov = FALSE; 982 nfs4_recov_state_t recov_state; 983 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 984 nfs4_ga_ext_res_t *gerp; 985 986 recov_state.rs_flags = 0; 987 recov_state.rs_num_retry_despite_err = 0; 988 989 recov_retry: 990 args.ctag = tag_type; 991 992 args.array_len = 2; 993 args.array = argop; 994 995 e.error = nfs4_start_fop(mi, vp, NULL, OH_GETATTR, &recov_state, NULL); 996 if (e.error) 997 return (e.error); 998 999 /* putfh */ 1000 argop[0].argop = OP_CPUTFH; 1001 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 1002 1003 /* getattr */ 1004 argop[1].argop = OP_GETATTR; 1005 argop[1].nfs_argop4_u.opgetattr.attr_request = reqbitmap; 1006 argop[1].nfs_argop4_u.opgetattr.mi = mi; 1007 1008 doqueue = 1; 1009 1010 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 1011 "nfs4_attr_otw: %s call, rp %s", needrecov ? "recov" : "first", 1012 rnode4info(VTOR4(vp)))); 1013 1014 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 1015 1016 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 1017 if (!needrecov && e.error) { 1018 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_GETATTR, &recov_state, 1019 needrecov); 1020 return (e.error); 1021 } 1022 1023 if (needrecov) { 1024 bool_t abort; 1025 1026 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1027 "nfs4_attr_otw: initiating recovery\n")); 1028 1029 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 1030 NULL, OP_GETATTR, NULL); 1031 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_GETATTR, &recov_state, 1032 needrecov); 1033 if (!e.error) { 1034 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1035 e.error = geterrno4(res.status); 1036 } 1037 if (abort == FALSE) 1038 goto recov_retry; 1039 return (e.error); 1040 } 1041 1042 if (res.status) { 1043 e.error = geterrno4(res.status); 1044 } else { 1045 gerp = garp->n4g_ext_res; 1046 bcopy(&res.array[1].nfs_resop4_u.opgetattr.ga_res, 1047 garp, sizeof (nfs4_ga_res_t)); 1048 garp->n4g_ext_res = gerp; 1049 if (garp->n4g_ext_res && 1050 res.array[1].nfs_resop4_u.opgetattr.ga_res.n4g_ext_res) 1051 bcopy(res.array[1].nfs_resop4_u.opgetattr. 1052 ga_res.n4g_ext_res, 1053 garp->n4g_ext_res, sizeof (nfs4_ga_ext_res_t)); 1054 } 1055 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1056 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_GETATTR, &recov_state, 1057 needrecov); 1058 return (e.error); 1059 } 1060 1061 /* 1062 * Asynchronous I/O parameters. nfs_async_threads is the high-water mark 1063 * for the demand-based allocation of async threads per-mount. The 1064 * nfs_async_timeout is the amount of time a thread will live after it 1065 * becomes idle, unless new I/O requests are received before the thread 1066 * dies. See nfs4_async_putpage and nfs4_async_start. 1067 */ 1068 1069 static void nfs4_async_start(struct vfs *); 1070 1071 static void 1072 free_async_args4(struct nfs4_async_reqs *args) 1073 { 1074 rnode4_t *rp; 1075 1076 if (args->a_io != NFS4_INACTIVE) { 1077 rp = VTOR4(args->a_vp); 1078 mutex_enter(&rp->r_statelock); 1079 rp->r_count--; 1080 if (args->a_io == NFS4_PUTAPAGE || 1081 args->a_io == NFS4_PAGEIO) 1082 rp->r_awcount--; 1083 cv_broadcast(&rp->r_cv); 1084 mutex_exit(&rp->r_statelock); 1085 VN_RELE(args->a_vp); 1086 } 1087 crfree(args->a_cred); 1088 kmem_free(args, sizeof (*args)); 1089 } 1090 1091 /* 1092 * Cross-zone thread creation and NFS access is disallowed, yet fsflush() and 1093 * pageout(), running in the global zone, have legitimate reasons to do 1094 * VOP_PUTPAGE(B_ASYNC) on other zones' NFS mounts. We avoid the problem by 1095 * use of a a per-mount "asynchronous requests manager thread" which is 1096 * signaled by the various asynchronous work routines when there is 1097 * asynchronous work to be done. It is responsible for creating new 1098 * worker threads if necessary, and notifying existing worker threads 1099 * that there is work to be done. 1100 * 1101 * In other words, it will "take the specifications from the customers and 1102 * give them to the engineers." 1103 * 1104 * Worker threads die off of their own accord if they are no longer 1105 * needed. 1106 * 1107 * This thread is killed when the zone is going away or the filesystem 1108 * is being unmounted. 1109 */ 1110 void 1111 nfs4_async_manager(vfs_t *vfsp) 1112 { 1113 callb_cpr_t cprinfo; 1114 mntinfo4_t *mi; 1115 uint_t max_threads; 1116 1117 mi = VFTOMI4(vfsp); 1118 1119 CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr, 1120 "nfs4_async_manager"); 1121 1122 mutex_enter(&mi->mi_async_lock); 1123 /* 1124 * We want to stash the max number of threads that this mount was 1125 * allowed so we can use it later when the variable is set to zero as 1126 * part of the zone/mount going away. 1127 * 1128 * We want to be able to create at least one thread to handle 1129 * asyncrhonous inactive calls. 1130 */ 1131 max_threads = MAX(mi->mi_max_threads, 1); 1132 mutex_enter(&mi->mi_lock); 1133 /* 1134 * We don't want to wait for mi_max_threads to go to zero, since that 1135 * happens as part of a failed unmount, but this thread should only 1136 * exit when the mount is really going away. 1137 * 1138 * Once MI4_ASYNC_MGR_STOP is set, no more async operations will be 1139 * attempted: the various _async_*() functions know to do things 1140 * inline if mi_max_threads == 0. Henceforth we just drain out the 1141 * outstanding requests. 1142 * 1143 * Note that we still create zthreads even if we notice the zone is 1144 * shutting down (MI4_ASYNC_MGR_STOP is set); this may cause the zone 1145 * shutdown sequence to take slightly longer in some cases, but 1146 * doesn't violate the protocol, as all threads will exit as soon as 1147 * they're done processing the remaining requests. 1148 */ 1149 while (!(mi->mi_flags & MI4_ASYNC_MGR_STOP) || 1150 mi->mi_async_req_count > 0) { 1151 mutex_exit(&mi->mi_lock); 1152 CALLB_CPR_SAFE_BEGIN(&cprinfo); 1153 cv_wait(&mi->mi_async_reqs_cv, &mi->mi_async_lock); 1154 CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock); 1155 while (mi->mi_async_req_count > 0) { 1156 /* 1157 * Paranoia: If the mount started out having 1158 * (mi->mi_max_threads == 0), and the value was 1159 * later changed (via a debugger or somesuch), 1160 * we could be confused since we will think we 1161 * can't create any threads, and the calling 1162 * code (which looks at the current value of 1163 * mi->mi_max_threads, now non-zero) thinks we 1164 * can. 1165 * 1166 * So, because we're paranoid, we create threads 1167 * up to the maximum of the original and the 1168 * current value. This means that future 1169 * (debugger-induced) alterations of 1170 * mi->mi_max_threads are ignored for our 1171 * purposes, but who told them they could change 1172 * random values on a live kernel anyhow? 1173 */ 1174 if (mi->mi_threads < 1175 MAX(mi->mi_max_threads, max_threads)) { 1176 mi->mi_threads++; 1177 mutex_exit(&mi->mi_async_lock); 1178 MI4_HOLD(mi); 1179 VFS_HOLD(vfsp); /* hold for new thread */ 1180 (void) zthread_create(NULL, 0, nfs4_async_start, 1181 vfsp, 0, minclsyspri); 1182 mutex_enter(&mi->mi_async_lock); 1183 } 1184 cv_signal(&mi->mi_async_work_cv); 1185 ASSERT(mi->mi_async_req_count != 0); 1186 mi->mi_async_req_count--; 1187 } 1188 mutex_enter(&mi->mi_lock); 1189 } 1190 mutex_exit(&mi->mi_lock); 1191 1192 NFS4_DEBUG(nfs4_client_zone_debug, (CE_NOTE, 1193 "nfs4_async_manager exiting for vfs %p\n", (void *)mi->mi_vfsp)); 1194 /* 1195 * Let everyone know we're done. 1196 */ 1197 mi->mi_manager_thread = NULL; 1198 /* 1199 * Wake up the inactive thread. 1200 */ 1201 cv_broadcast(&mi->mi_inact_req_cv); 1202 /* 1203 * Wake up anyone sitting in nfs4_async_manager_stop() 1204 */ 1205 cv_broadcast(&mi->mi_async_cv); 1206 /* 1207 * There is no explicit call to mutex_exit(&mi->mi_async_lock) 1208 * since CALLB_CPR_EXIT is actually responsible for releasing 1209 * 'mi_async_lock'. 1210 */ 1211 CALLB_CPR_EXIT(&cprinfo); 1212 VFS_RELE(vfsp); /* release thread's hold */ 1213 MI4_RELE(mi); 1214 zthread_exit(); 1215 } 1216 1217 /* 1218 * Signal (and wait for) the async manager thread to clean up and go away. 1219 */ 1220 void 1221 nfs4_async_manager_stop(vfs_t *vfsp) 1222 { 1223 mntinfo4_t *mi = VFTOMI4(vfsp); 1224 1225 mutex_enter(&mi->mi_async_lock); 1226 mutex_enter(&mi->mi_lock); 1227 mi->mi_flags |= MI4_ASYNC_MGR_STOP; 1228 mutex_exit(&mi->mi_lock); 1229 cv_broadcast(&mi->mi_async_reqs_cv); 1230 /* 1231 * Wait for the async manager thread to die. 1232 */ 1233 while (mi->mi_manager_thread != NULL) 1234 cv_wait(&mi->mi_async_cv, &mi->mi_async_lock); 1235 mutex_exit(&mi->mi_async_lock); 1236 } 1237 1238 int 1239 nfs4_async_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, 1240 struct seg *seg, cred_t *cr, void (*readahead)(vnode_t *, 1241 u_offset_t, caddr_t, struct seg *, cred_t *)) 1242 { 1243 rnode4_t *rp; 1244 mntinfo4_t *mi; 1245 struct nfs4_async_reqs *args; 1246 1247 rp = VTOR4(vp); 1248 ASSERT(rp->r_freef == NULL); 1249 1250 mi = VTOMI4(vp); 1251 1252 /* 1253 * If addr falls in a different segment, don't bother doing readahead. 1254 */ 1255 if (addr >= seg->s_base + seg->s_size) 1256 return (-1); 1257 1258 /* 1259 * If we can't allocate a request structure, punt on the readahead. 1260 */ 1261 if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL) 1262 return (-1); 1263 1264 /* 1265 * If a lock operation is pending, don't initiate any new 1266 * readaheads. Otherwise, bump r_count to indicate the new 1267 * asynchronous I/O. 1268 */ 1269 if (!nfs_rw_tryenter(&rp->r_lkserlock, RW_READER)) { 1270 kmem_free(args, sizeof (*args)); 1271 return (-1); 1272 } 1273 mutex_enter(&rp->r_statelock); 1274 rp->r_count++; 1275 mutex_exit(&rp->r_statelock); 1276 nfs_rw_exit(&rp->r_lkserlock); 1277 1278 args->a_next = NULL; 1279 #ifdef DEBUG 1280 args->a_queuer = curthread; 1281 #endif 1282 VN_HOLD(vp); 1283 args->a_vp = vp; 1284 ASSERT(cr != NULL); 1285 crhold(cr); 1286 args->a_cred = cr; 1287 args->a_io = NFS4_READ_AHEAD; 1288 args->a_nfs4_readahead = readahead; 1289 args->a_nfs4_blkoff = blkoff; 1290 args->a_nfs4_seg = seg; 1291 args->a_nfs4_addr = addr; 1292 1293 mutex_enter(&mi->mi_async_lock); 1294 1295 /* 1296 * If asyncio has been disabled, don't bother readahead. 1297 */ 1298 if (mi->mi_max_threads == 0) { 1299 mutex_exit(&mi->mi_async_lock); 1300 goto noasync; 1301 } 1302 1303 /* 1304 * Link request structure into the async list and 1305 * wakeup async thread to do the i/o. 1306 */ 1307 if (mi->mi_async_reqs[NFS4_READ_AHEAD] == NULL) { 1308 mi->mi_async_reqs[NFS4_READ_AHEAD] = args; 1309 mi->mi_async_tail[NFS4_READ_AHEAD] = args; 1310 } else { 1311 mi->mi_async_tail[NFS4_READ_AHEAD]->a_next = args; 1312 mi->mi_async_tail[NFS4_READ_AHEAD] = args; 1313 } 1314 1315 if (mi->mi_io_kstats) { 1316 mutex_enter(&mi->mi_lock); 1317 kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 1318 mutex_exit(&mi->mi_lock); 1319 } 1320 1321 mi->mi_async_req_count++; 1322 ASSERT(mi->mi_async_req_count != 0); 1323 cv_signal(&mi->mi_async_reqs_cv); 1324 mutex_exit(&mi->mi_async_lock); 1325 return (0); 1326 1327 noasync: 1328 mutex_enter(&rp->r_statelock); 1329 rp->r_count--; 1330 cv_broadcast(&rp->r_cv); 1331 mutex_exit(&rp->r_statelock); 1332 VN_RELE(vp); 1333 crfree(cr); 1334 kmem_free(args, sizeof (*args)); 1335 return (-1); 1336 } 1337 1338 /* 1339 * The async queues for each mounted file system are arranged as a 1340 * set of queues, one for each async i/o type. Requests are taken 1341 * from the queues in a round-robin fashion. A number of consecutive 1342 * requests are taken from each queue before moving on to the next 1343 * queue. This functionality may allow the NFS Version 2 server to do 1344 * write clustering, even if the client is mixing writes and reads 1345 * because it will take multiple write requests from the queue 1346 * before processing any of the other async i/o types. 1347 * 1348 * XXX The nfs4_async_start thread is unsafe in the light of the present 1349 * model defined by cpr to suspend the system. Specifically over the 1350 * wire calls are cpr-unsafe. The thread should be reevaluated in 1351 * case of future updates to the cpr model. 1352 */ 1353 static void 1354 nfs4_async_start(struct vfs *vfsp) 1355 { 1356 struct nfs4_async_reqs *args; 1357 mntinfo4_t *mi = VFTOMI4(vfsp); 1358 clock_t time_left = 1; 1359 callb_cpr_t cprinfo; 1360 int i; 1361 extern int nfs_async_timeout; 1362 1363 /* 1364 * Dynamic initialization of nfs_async_timeout to allow nfs to be 1365 * built in an implementation independent manner. 1366 */ 1367 if (nfs_async_timeout == -1) 1368 nfs_async_timeout = NFS_ASYNC_TIMEOUT; 1369 1370 CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr, "nas"); 1371 1372 mutex_enter(&mi->mi_async_lock); 1373 for (;;) { 1374 /* 1375 * Find the next queue containing an entry. We start 1376 * at the current queue pointer and then round robin 1377 * through all of them until we either find a non-empty 1378 * queue or have looked through all of them. 1379 */ 1380 for (i = 0; i < NFS4_ASYNC_TYPES; i++) { 1381 args = *mi->mi_async_curr; 1382 if (args != NULL) 1383 break; 1384 mi->mi_async_curr++; 1385 if (mi->mi_async_curr == 1386 &mi->mi_async_reqs[NFS4_ASYNC_TYPES]) 1387 mi->mi_async_curr = &mi->mi_async_reqs[0]; 1388 } 1389 /* 1390 * If we didn't find a entry, then block until woken up 1391 * again and then look through the queues again. 1392 */ 1393 if (args == NULL) { 1394 /* 1395 * Exiting is considered to be safe for CPR as well 1396 */ 1397 CALLB_CPR_SAFE_BEGIN(&cprinfo); 1398 1399 /* 1400 * Wakeup thread waiting to unmount the file 1401 * system only if all async threads are inactive. 1402 * 1403 * If we've timed-out and there's nothing to do, 1404 * then get rid of this thread. 1405 */ 1406 if (mi->mi_max_threads == 0 || time_left <= 0) { 1407 if (--mi->mi_threads == 0) 1408 cv_signal(&mi->mi_async_cv); 1409 CALLB_CPR_EXIT(&cprinfo); 1410 VFS_RELE(vfsp); /* release thread's hold */ 1411 MI4_RELE(mi); 1412 zthread_exit(); 1413 /* NOTREACHED */ 1414 } 1415 time_left = cv_reltimedwait(&mi->mi_async_work_cv, 1416 &mi->mi_async_lock, nfs_async_timeout, 1417 TR_CLOCK_TICK); 1418 1419 CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock); 1420 1421 continue; 1422 } else { 1423 time_left = 1; 1424 } 1425 1426 /* 1427 * Remove the request from the async queue and then 1428 * update the current async request queue pointer. If 1429 * the current queue is empty or we have removed enough 1430 * consecutive entries from it, then reset the counter 1431 * for this queue and then move the current pointer to 1432 * the next queue. 1433 */ 1434 *mi->mi_async_curr = args->a_next; 1435 if (*mi->mi_async_curr == NULL || 1436 --mi->mi_async_clusters[args->a_io] == 0) { 1437 mi->mi_async_clusters[args->a_io] = 1438 mi->mi_async_init_clusters; 1439 mi->mi_async_curr++; 1440 if (mi->mi_async_curr == 1441 &mi->mi_async_reqs[NFS4_ASYNC_TYPES]) 1442 mi->mi_async_curr = &mi->mi_async_reqs[0]; 1443 } 1444 1445 if (args->a_io != NFS4_INACTIVE && mi->mi_io_kstats) { 1446 mutex_enter(&mi->mi_lock); 1447 kstat_waitq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 1448 mutex_exit(&mi->mi_lock); 1449 } 1450 1451 mutex_exit(&mi->mi_async_lock); 1452 1453 /* 1454 * Obtain arguments from the async request structure. 1455 */ 1456 if (args->a_io == NFS4_READ_AHEAD && mi->mi_max_threads > 0) { 1457 (*args->a_nfs4_readahead)(args->a_vp, 1458 args->a_nfs4_blkoff, args->a_nfs4_addr, 1459 args->a_nfs4_seg, args->a_cred); 1460 } else if (args->a_io == NFS4_PUTAPAGE) { 1461 (void) (*args->a_nfs4_putapage)(args->a_vp, 1462 args->a_nfs4_pp, args->a_nfs4_off, 1463 args->a_nfs4_len, args->a_nfs4_flags, 1464 args->a_cred); 1465 } else if (args->a_io == NFS4_PAGEIO) { 1466 (void) (*args->a_nfs4_pageio)(args->a_vp, 1467 args->a_nfs4_pp, args->a_nfs4_off, 1468 args->a_nfs4_len, args->a_nfs4_flags, 1469 args->a_cred); 1470 } else if (args->a_io == NFS4_READDIR) { 1471 (void) ((*args->a_nfs4_readdir)(args->a_vp, 1472 args->a_nfs4_rdc, args->a_cred)); 1473 } else if (args->a_io == NFS4_COMMIT) { 1474 (*args->a_nfs4_commit)(args->a_vp, args->a_nfs4_plist, 1475 args->a_nfs4_offset, args->a_nfs4_count, 1476 args->a_cred); 1477 } else if (args->a_io == NFS4_INACTIVE) { 1478 nfs4_inactive_otw(args->a_vp, args->a_cred); 1479 } 1480 1481 /* 1482 * Now, release the vnode and free the credentials 1483 * structure. 1484 */ 1485 free_async_args4(args); 1486 /* 1487 * Reacquire the mutex because it will be needed above. 1488 */ 1489 mutex_enter(&mi->mi_async_lock); 1490 } 1491 } 1492 1493 /* 1494 * nfs4_inactive_thread - look for vnodes that need over-the-wire calls as 1495 * part of VOP_INACTIVE. 1496 */ 1497 1498 void 1499 nfs4_inactive_thread(mntinfo4_t *mi) 1500 { 1501 struct nfs4_async_reqs *args; 1502 callb_cpr_t cprinfo; 1503 vfs_t *vfsp = mi->mi_vfsp; 1504 1505 CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr, 1506 "nfs4_inactive_thread"); 1507 1508 for (;;) { 1509 mutex_enter(&mi->mi_async_lock); 1510 args = mi->mi_async_reqs[NFS4_INACTIVE]; 1511 if (args == NULL) { 1512 mutex_enter(&mi->mi_lock); 1513 /* 1514 * We don't want to exit until the async manager is done 1515 * with its work; hence the check for mi_manager_thread 1516 * being NULL. 1517 * 1518 * The async manager thread will cv_broadcast() on 1519 * mi_inact_req_cv when it's done, at which point we'll 1520 * wake up and exit. 1521 */ 1522 if (mi->mi_manager_thread == NULL) 1523 goto die; 1524 mi->mi_flags |= MI4_INACTIVE_IDLE; 1525 mutex_exit(&mi->mi_lock); 1526 cv_signal(&mi->mi_async_cv); 1527 CALLB_CPR_SAFE_BEGIN(&cprinfo); 1528 cv_wait(&mi->mi_inact_req_cv, &mi->mi_async_lock); 1529 CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock); 1530 mutex_exit(&mi->mi_async_lock); 1531 } else { 1532 mutex_enter(&mi->mi_lock); 1533 mi->mi_flags &= ~MI4_INACTIVE_IDLE; 1534 mutex_exit(&mi->mi_lock); 1535 mi->mi_async_reqs[NFS4_INACTIVE] = args->a_next; 1536 mutex_exit(&mi->mi_async_lock); 1537 nfs4_inactive_otw(args->a_vp, args->a_cred); 1538 crfree(args->a_cred); 1539 kmem_free(args, sizeof (*args)); 1540 } 1541 } 1542 die: 1543 mutex_exit(&mi->mi_lock); 1544 mi->mi_inactive_thread = NULL; 1545 cv_signal(&mi->mi_async_cv); 1546 1547 /* 1548 * There is no explicit call to mutex_exit(&mi->mi_async_lock) since 1549 * CALLB_CPR_EXIT is actually responsible for releasing 'mi_async_lock'. 1550 */ 1551 CALLB_CPR_EXIT(&cprinfo); 1552 1553 NFS4_DEBUG(nfs4_client_zone_debug, (CE_NOTE, 1554 "nfs4_inactive_thread exiting for vfs %p\n", (void *)vfsp)); 1555 1556 MI4_RELE(mi); 1557 zthread_exit(); 1558 /* NOTREACHED */ 1559 } 1560 1561 /* 1562 * nfs_async_stop: 1563 * Wait for all outstanding putpage operations and the inactive thread to 1564 * complete; nfs4_async_stop_sig() without interruptibility. 1565 */ 1566 void 1567 nfs4_async_stop(struct vfs *vfsp) 1568 { 1569 mntinfo4_t *mi = VFTOMI4(vfsp); 1570 1571 /* 1572 * Wait for all outstanding async operations to complete and for 1573 * worker threads to exit. 1574 */ 1575 mutex_enter(&mi->mi_async_lock); 1576 mi->mi_max_threads = 0; 1577 cv_broadcast(&mi->mi_async_work_cv); 1578 while (mi->mi_threads != 0) 1579 cv_wait(&mi->mi_async_cv, &mi->mi_async_lock); 1580 1581 /* 1582 * Wait for the inactive thread to finish doing what it's doing. It 1583 * won't exit until the last reference to the vfs_t goes away. 1584 */ 1585 if (mi->mi_inactive_thread != NULL) { 1586 mutex_enter(&mi->mi_lock); 1587 while (!(mi->mi_flags & MI4_INACTIVE_IDLE) || 1588 (mi->mi_async_reqs[NFS4_INACTIVE] != NULL)) { 1589 mutex_exit(&mi->mi_lock); 1590 cv_wait(&mi->mi_async_cv, &mi->mi_async_lock); 1591 mutex_enter(&mi->mi_lock); 1592 } 1593 mutex_exit(&mi->mi_lock); 1594 } 1595 mutex_exit(&mi->mi_async_lock); 1596 } 1597 1598 /* 1599 * nfs_async_stop_sig: 1600 * Wait for all outstanding putpage operations and the inactive thread to 1601 * complete. If a signal is delivered we will abort and return non-zero; 1602 * otherwise return 0. Since this routine is called from nfs4_unmount, we 1603 * need to make it interruptible. 1604 */ 1605 int 1606 nfs4_async_stop_sig(struct vfs *vfsp) 1607 { 1608 mntinfo4_t *mi = VFTOMI4(vfsp); 1609 ushort_t omax; 1610 bool_t intr = FALSE; 1611 1612 /* 1613 * Wait for all outstanding putpage operations to complete and for 1614 * worker threads to exit. 1615 */ 1616 mutex_enter(&mi->mi_async_lock); 1617 omax = mi->mi_max_threads; 1618 mi->mi_max_threads = 0; 1619 cv_broadcast(&mi->mi_async_work_cv); 1620 while (mi->mi_threads != 0) { 1621 if (!cv_wait_sig(&mi->mi_async_cv, &mi->mi_async_lock)) { 1622 intr = TRUE; 1623 goto interrupted; 1624 } 1625 } 1626 1627 /* 1628 * Wait for the inactive thread to finish doing what it's doing. It 1629 * won't exit until the a last reference to the vfs_t goes away. 1630 */ 1631 if (mi->mi_inactive_thread != NULL) { 1632 mutex_enter(&mi->mi_lock); 1633 while (!(mi->mi_flags & MI4_INACTIVE_IDLE) || 1634 (mi->mi_async_reqs[NFS4_INACTIVE] != NULL)) { 1635 mutex_exit(&mi->mi_lock); 1636 if (!cv_wait_sig(&mi->mi_async_cv, 1637 &mi->mi_async_lock)) { 1638 intr = TRUE; 1639 goto interrupted; 1640 } 1641 mutex_enter(&mi->mi_lock); 1642 } 1643 mutex_exit(&mi->mi_lock); 1644 } 1645 interrupted: 1646 if (intr) 1647 mi->mi_max_threads = omax; 1648 mutex_exit(&mi->mi_async_lock); 1649 1650 return (intr); 1651 } 1652 1653 int 1654 nfs4_async_putapage(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 1655 int flags, cred_t *cr, int (*putapage)(vnode_t *, page_t *, 1656 u_offset_t, size_t, int, cred_t *)) 1657 { 1658 rnode4_t *rp; 1659 mntinfo4_t *mi; 1660 struct nfs4_async_reqs *args; 1661 1662 ASSERT(flags & B_ASYNC); 1663 ASSERT(vp->v_vfsp != NULL); 1664 1665 rp = VTOR4(vp); 1666 ASSERT(rp->r_count > 0); 1667 1668 mi = VTOMI4(vp); 1669 1670 /* 1671 * If we can't allocate a request structure, do the putpage 1672 * operation synchronously in this thread's context. 1673 */ 1674 if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL) 1675 goto noasync; 1676 1677 args->a_next = NULL; 1678 #ifdef DEBUG 1679 args->a_queuer = curthread; 1680 #endif 1681 VN_HOLD(vp); 1682 args->a_vp = vp; 1683 ASSERT(cr != NULL); 1684 crhold(cr); 1685 args->a_cred = cr; 1686 args->a_io = NFS4_PUTAPAGE; 1687 args->a_nfs4_putapage = putapage; 1688 args->a_nfs4_pp = pp; 1689 args->a_nfs4_off = off; 1690 args->a_nfs4_len = (uint_t)len; 1691 args->a_nfs4_flags = flags; 1692 1693 mutex_enter(&mi->mi_async_lock); 1694 1695 /* 1696 * If asyncio has been disabled, then make a synchronous request. 1697 * This check is done a second time in case async io was diabled 1698 * while this thread was blocked waiting for memory pressure to 1699 * reduce or for the queue to drain. 1700 */ 1701 if (mi->mi_max_threads == 0) { 1702 mutex_exit(&mi->mi_async_lock); 1703 1704 VN_RELE(vp); 1705 crfree(cr); 1706 kmem_free(args, sizeof (*args)); 1707 goto noasync; 1708 } 1709 1710 /* 1711 * Link request structure into the async list and 1712 * wakeup async thread to do the i/o. 1713 */ 1714 if (mi->mi_async_reqs[NFS4_PUTAPAGE] == NULL) { 1715 mi->mi_async_reqs[NFS4_PUTAPAGE] = args; 1716 mi->mi_async_tail[NFS4_PUTAPAGE] = args; 1717 } else { 1718 mi->mi_async_tail[NFS4_PUTAPAGE]->a_next = args; 1719 mi->mi_async_tail[NFS4_PUTAPAGE] = args; 1720 } 1721 1722 mutex_enter(&rp->r_statelock); 1723 rp->r_count++; 1724 rp->r_awcount++; 1725 mutex_exit(&rp->r_statelock); 1726 1727 if (mi->mi_io_kstats) { 1728 mutex_enter(&mi->mi_lock); 1729 kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 1730 mutex_exit(&mi->mi_lock); 1731 } 1732 1733 mi->mi_async_req_count++; 1734 ASSERT(mi->mi_async_req_count != 0); 1735 cv_signal(&mi->mi_async_reqs_cv); 1736 mutex_exit(&mi->mi_async_lock); 1737 return (0); 1738 1739 noasync: 1740 1741 if (curproc == proc_pageout || curproc == proc_fsflush || 1742 nfs_zone() == mi->mi_zone) { 1743 /* 1744 * If we get here in the context of the pageout/fsflush, 1745 * or we have run out of memory or we're attempting to 1746 * unmount we refuse to do a sync write, because this may 1747 * hang pageout/fsflush and the machine. In this case, 1748 * we just re-mark the page as dirty and punt on the page. 1749 * 1750 * Make sure B_FORCE isn't set. We can re-mark the 1751 * pages as dirty and unlock the pages in one swoop by 1752 * passing in B_ERROR to pvn_write_done(). However, 1753 * we should make sure B_FORCE isn't set - we don't 1754 * want the page tossed before it gets written out. 1755 */ 1756 if (flags & B_FORCE) 1757 flags &= ~(B_INVAL | B_FORCE); 1758 pvn_write_done(pp, flags | B_ERROR); 1759 return (0); 1760 } 1761 1762 /* 1763 * We'll get here only if (nfs_zone() != mi->mi_zone) 1764 * which means that this was a cross-zone sync putpage. 1765 * 1766 * We pass in B_ERROR to pvn_write_done() to re-mark the pages 1767 * as dirty and unlock them. 1768 * 1769 * We don't want to clear B_FORCE here as the caller presumably 1770 * knows what they're doing if they set it. 1771 */ 1772 pvn_write_done(pp, flags | B_ERROR); 1773 return (EPERM); 1774 } 1775 1776 int 1777 nfs4_async_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 1778 int flags, cred_t *cr, int (*pageio)(vnode_t *, page_t *, u_offset_t, 1779 size_t, int, cred_t *)) 1780 { 1781 rnode4_t *rp; 1782 mntinfo4_t *mi; 1783 struct nfs4_async_reqs *args; 1784 1785 ASSERT(flags & B_ASYNC); 1786 ASSERT(vp->v_vfsp != NULL); 1787 1788 rp = VTOR4(vp); 1789 ASSERT(rp->r_count > 0); 1790 1791 mi = VTOMI4(vp); 1792 1793 /* 1794 * If we can't allocate a request structure, do the pageio 1795 * request synchronously in this thread's context. 1796 */ 1797 if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL) 1798 goto noasync; 1799 1800 args->a_next = NULL; 1801 #ifdef DEBUG 1802 args->a_queuer = curthread; 1803 #endif 1804 VN_HOLD(vp); 1805 args->a_vp = vp; 1806 ASSERT(cr != NULL); 1807 crhold(cr); 1808 args->a_cred = cr; 1809 args->a_io = NFS4_PAGEIO; 1810 args->a_nfs4_pageio = pageio; 1811 args->a_nfs4_pp = pp; 1812 args->a_nfs4_off = io_off; 1813 args->a_nfs4_len = (uint_t)io_len; 1814 args->a_nfs4_flags = flags; 1815 1816 mutex_enter(&mi->mi_async_lock); 1817 1818 /* 1819 * If asyncio has been disabled, then make a synchronous request. 1820 * This check is done a second time in case async io was diabled 1821 * while this thread was blocked waiting for memory pressure to 1822 * reduce or for the queue to drain. 1823 */ 1824 if (mi->mi_max_threads == 0) { 1825 mutex_exit(&mi->mi_async_lock); 1826 1827 VN_RELE(vp); 1828 crfree(cr); 1829 kmem_free(args, sizeof (*args)); 1830 goto noasync; 1831 } 1832 1833 /* 1834 * Link request structure into the async list and 1835 * wakeup async thread to do the i/o. 1836 */ 1837 if (mi->mi_async_reqs[NFS4_PAGEIO] == NULL) { 1838 mi->mi_async_reqs[NFS4_PAGEIO] = args; 1839 mi->mi_async_tail[NFS4_PAGEIO] = args; 1840 } else { 1841 mi->mi_async_tail[NFS4_PAGEIO]->a_next = args; 1842 mi->mi_async_tail[NFS4_PAGEIO] = args; 1843 } 1844 1845 mutex_enter(&rp->r_statelock); 1846 rp->r_count++; 1847 rp->r_awcount++; 1848 mutex_exit(&rp->r_statelock); 1849 1850 if (mi->mi_io_kstats) { 1851 mutex_enter(&mi->mi_lock); 1852 kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 1853 mutex_exit(&mi->mi_lock); 1854 } 1855 1856 mi->mi_async_req_count++; 1857 ASSERT(mi->mi_async_req_count != 0); 1858 cv_signal(&mi->mi_async_reqs_cv); 1859 mutex_exit(&mi->mi_async_lock); 1860 return (0); 1861 1862 noasync: 1863 /* 1864 * If we can't do it ASYNC, for reads we do nothing (but cleanup 1865 * the page list), for writes we do it synchronously, except for 1866 * proc_pageout/proc_fsflush as described below. 1867 */ 1868 if (flags & B_READ) { 1869 pvn_read_done(pp, flags | B_ERROR); 1870 return (0); 1871 } 1872 1873 if (curproc == proc_pageout || curproc == proc_fsflush) { 1874 /* 1875 * If we get here in the context of the pageout/fsflush, 1876 * we refuse to do a sync write, because this may hang 1877 * pageout/fsflush (and the machine). In this case, we just 1878 * re-mark the page as dirty and punt on the page. 1879 * 1880 * Make sure B_FORCE isn't set. We can re-mark the 1881 * pages as dirty and unlock the pages in one swoop by 1882 * passing in B_ERROR to pvn_write_done(). However, 1883 * we should make sure B_FORCE isn't set - we don't 1884 * want the page tossed before it gets written out. 1885 */ 1886 if (flags & B_FORCE) 1887 flags &= ~(B_INVAL | B_FORCE); 1888 pvn_write_done(pp, flags | B_ERROR); 1889 return (0); 1890 } 1891 1892 if (nfs_zone() != mi->mi_zone) { 1893 /* 1894 * So this was a cross-zone sync pageio. We pass in B_ERROR 1895 * to pvn_write_done() to re-mark the pages as dirty and unlock 1896 * them. 1897 * 1898 * We don't want to clear B_FORCE here as the caller presumably 1899 * knows what they're doing if they set it. 1900 */ 1901 pvn_write_done(pp, flags | B_ERROR); 1902 return (EPERM); 1903 } 1904 return ((*pageio)(vp, pp, io_off, io_len, flags, cr)); 1905 } 1906 1907 void 1908 nfs4_async_readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr, 1909 int (*readdir)(vnode_t *, rddir4_cache *, cred_t *)) 1910 { 1911 rnode4_t *rp; 1912 mntinfo4_t *mi; 1913 struct nfs4_async_reqs *args; 1914 1915 rp = VTOR4(vp); 1916 ASSERT(rp->r_freef == NULL); 1917 1918 mi = VTOMI4(vp); 1919 1920 /* 1921 * If we can't allocate a request structure, skip the readdir. 1922 */ 1923 if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL) 1924 goto noasync; 1925 1926 args->a_next = NULL; 1927 #ifdef DEBUG 1928 args->a_queuer = curthread; 1929 #endif 1930 VN_HOLD(vp); 1931 args->a_vp = vp; 1932 ASSERT(cr != NULL); 1933 crhold(cr); 1934 args->a_cred = cr; 1935 args->a_io = NFS4_READDIR; 1936 args->a_nfs4_readdir = readdir; 1937 args->a_nfs4_rdc = rdc; 1938 1939 mutex_enter(&mi->mi_async_lock); 1940 1941 /* 1942 * If asyncio has been disabled, then skip this request 1943 */ 1944 if (mi->mi_max_threads == 0) { 1945 mutex_exit(&mi->mi_async_lock); 1946 1947 VN_RELE(vp); 1948 crfree(cr); 1949 kmem_free(args, sizeof (*args)); 1950 goto noasync; 1951 } 1952 1953 /* 1954 * Link request structure into the async list and 1955 * wakeup async thread to do the i/o. 1956 */ 1957 if (mi->mi_async_reqs[NFS4_READDIR] == NULL) { 1958 mi->mi_async_reqs[NFS4_READDIR] = args; 1959 mi->mi_async_tail[NFS4_READDIR] = args; 1960 } else { 1961 mi->mi_async_tail[NFS4_READDIR]->a_next = args; 1962 mi->mi_async_tail[NFS4_READDIR] = args; 1963 } 1964 1965 mutex_enter(&rp->r_statelock); 1966 rp->r_count++; 1967 mutex_exit(&rp->r_statelock); 1968 1969 if (mi->mi_io_kstats) { 1970 mutex_enter(&mi->mi_lock); 1971 kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 1972 mutex_exit(&mi->mi_lock); 1973 } 1974 1975 mi->mi_async_req_count++; 1976 ASSERT(mi->mi_async_req_count != 0); 1977 cv_signal(&mi->mi_async_reqs_cv); 1978 mutex_exit(&mi->mi_async_lock); 1979 return; 1980 1981 noasync: 1982 mutex_enter(&rp->r_statelock); 1983 rdc->entries = NULL; 1984 /* 1985 * Indicate that no one is trying to fill this entry and 1986 * it still needs to be filled. 1987 */ 1988 rdc->flags &= ~RDDIR; 1989 rdc->flags |= RDDIRREQ; 1990 rddir4_cache_rele(rp, rdc); 1991 mutex_exit(&rp->r_statelock); 1992 } 1993 1994 void 1995 nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 1996 cred_t *cr, void (*commit)(vnode_t *, page_t *, offset3, count3, 1997 cred_t *)) 1998 { 1999 rnode4_t *rp; 2000 mntinfo4_t *mi; 2001 struct nfs4_async_reqs *args; 2002 page_t *pp; 2003 2004 rp = VTOR4(vp); 2005 mi = VTOMI4(vp); 2006 2007 /* 2008 * If we can't allocate a request structure, do the commit 2009 * operation synchronously in this thread's context. 2010 */ 2011 if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL) 2012 goto noasync; 2013 2014 args->a_next = NULL; 2015 #ifdef DEBUG 2016 args->a_queuer = curthread; 2017 #endif 2018 VN_HOLD(vp); 2019 args->a_vp = vp; 2020 ASSERT(cr != NULL); 2021 crhold(cr); 2022 args->a_cred = cr; 2023 args->a_io = NFS4_COMMIT; 2024 args->a_nfs4_commit = commit; 2025 args->a_nfs4_plist = plist; 2026 args->a_nfs4_offset = offset; 2027 args->a_nfs4_count = count; 2028 2029 mutex_enter(&mi->mi_async_lock); 2030 2031 /* 2032 * If asyncio has been disabled, then make a synchronous request. 2033 * This check is done a second time in case async io was diabled 2034 * while this thread was blocked waiting for memory pressure to 2035 * reduce or for the queue to drain. 2036 */ 2037 if (mi->mi_max_threads == 0) { 2038 mutex_exit(&mi->mi_async_lock); 2039 2040 VN_RELE(vp); 2041 crfree(cr); 2042 kmem_free(args, sizeof (*args)); 2043 goto noasync; 2044 } 2045 2046 /* 2047 * Link request structure into the async list and 2048 * wakeup async thread to do the i/o. 2049 */ 2050 if (mi->mi_async_reqs[NFS4_COMMIT] == NULL) { 2051 mi->mi_async_reqs[NFS4_COMMIT] = args; 2052 mi->mi_async_tail[NFS4_COMMIT] = args; 2053 } else { 2054 mi->mi_async_tail[NFS4_COMMIT]->a_next = args; 2055 mi->mi_async_tail[NFS4_COMMIT] = args; 2056 } 2057 2058 mutex_enter(&rp->r_statelock); 2059 rp->r_count++; 2060 mutex_exit(&rp->r_statelock); 2061 2062 if (mi->mi_io_kstats) { 2063 mutex_enter(&mi->mi_lock); 2064 kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 2065 mutex_exit(&mi->mi_lock); 2066 } 2067 2068 mi->mi_async_req_count++; 2069 ASSERT(mi->mi_async_req_count != 0); 2070 cv_signal(&mi->mi_async_reqs_cv); 2071 mutex_exit(&mi->mi_async_lock); 2072 return; 2073 2074 noasync: 2075 if (curproc == proc_pageout || curproc == proc_fsflush || 2076 nfs_zone() != mi->mi_zone) { 2077 while (plist != NULL) { 2078 pp = plist; 2079 page_sub(&plist, pp); 2080 pp->p_fsdata = C_COMMIT; 2081 page_unlock(pp); 2082 } 2083 return; 2084 } 2085 (*commit)(vp, plist, offset, count, cr); 2086 } 2087 2088 /* 2089 * nfs4_async_inactive - hand off a VOP_INACTIVE call to a thread. The 2090 * reference to the vnode is handed over to the thread; the caller should 2091 * no longer refer to the vnode. 2092 * 2093 * Unlike most of the async routines, this handoff is needed for 2094 * correctness reasons, not just performance. So doing operations in the 2095 * context of the current thread is not an option. 2096 */ 2097 void 2098 nfs4_async_inactive(vnode_t *vp, cred_t *cr) 2099 { 2100 mntinfo4_t *mi; 2101 struct nfs4_async_reqs *args; 2102 boolean_t signal_inactive_thread = B_FALSE; 2103 2104 mi = VTOMI4(vp); 2105 2106 args = kmem_alloc(sizeof (*args), KM_SLEEP); 2107 args->a_next = NULL; 2108 #ifdef DEBUG 2109 args->a_queuer = curthread; 2110 #endif 2111 args->a_vp = vp; 2112 ASSERT(cr != NULL); 2113 crhold(cr); 2114 args->a_cred = cr; 2115 args->a_io = NFS4_INACTIVE; 2116 2117 /* 2118 * Note that we don't check mi->mi_max_threads here, since we 2119 * *need* to get rid of this vnode regardless of whether someone 2120 * set nfs4_max_threads to zero in /etc/system. 2121 * 2122 * The manager thread knows about this and is willing to create 2123 * at least one thread to accommodate us. 2124 */ 2125 mutex_enter(&mi->mi_async_lock); 2126 if (mi->mi_inactive_thread == NULL) { 2127 rnode4_t *rp; 2128 vnode_t *unldvp = NULL; 2129 char *unlname; 2130 cred_t *unlcred; 2131 2132 mutex_exit(&mi->mi_async_lock); 2133 /* 2134 * We just need to free up the memory associated with the 2135 * vnode, which can be safely done from within the current 2136 * context. 2137 */ 2138 crfree(cr); /* drop our reference */ 2139 kmem_free(args, sizeof (*args)); 2140 rp = VTOR4(vp); 2141 mutex_enter(&rp->r_statelock); 2142 if (rp->r_unldvp != NULL) { 2143 unldvp = rp->r_unldvp; 2144 rp->r_unldvp = NULL; 2145 unlname = rp->r_unlname; 2146 rp->r_unlname = NULL; 2147 unlcred = rp->r_unlcred; 2148 rp->r_unlcred = NULL; 2149 } 2150 mutex_exit(&rp->r_statelock); 2151 /* 2152 * No need to explicitly throw away any cached pages. The 2153 * eventual r4inactive() will attempt a synchronous 2154 * VOP_PUTPAGE() which will immediately fail since the request 2155 * is coming from the wrong zone, and then will proceed to call 2156 * nfs4_invalidate_pages() which will clean things up for us. 2157 * 2158 * Throw away the delegation here so rp4_addfree()'s attempt to 2159 * return any existing delegations becomes a no-op. 2160 */ 2161 if (rp->r_deleg_type != OPEN_DELEGATE_NONE) { 2162 (void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, 2163 FALSE); 2164 (void) nfs4delegreturn(rp, NFS4_DR_DISCARD); 2165 nfs_rw_exit(&mi->mi_recovlock); 2166 } 2167 nfs4_clear_open_streams(rp); 2168 2169 rp4_addfree(rp, cr); 2170 if (unldvp != NULL) { 2171 kmem_free(unlname, MAXNAMELEN); 2172 VN_RELE(unldvp); 2173 crfree(unlcred); 2174 } 2175 return; 2176 } 2177 2178 if (mi->mi_manager_thread == NULL) { 2179 /* 2180 * We want to talk to the inactive thread. 2181 */ 2182 signal_inactive_thread = B_TRUE; 2183 } 2184 2185 /* 2186 * Enqueue the vnode and wake up either the special thread (empty 2187 * list) or an async thread. 2188 */ 2189 if (mi->mi_async_reqs[NFS4_INACTIVE] == NULL) { 2190 mi->mi_async_reqs[NFS4_INACTIVE] = args; 2191 mi->mi_async_tail[NFS4_INACTIVE] = args; 2192 signal_inactive_thread = B_TRUE; 2193 } else { 2194 mi->mi_async_tail[NFS4_INACTIVE]->a_next = args; 2195 mi->mi_async_tail[NFS4_INACTIVE] = args; 2196 } 2197 if (signal_inactive_thread) { 2198 cv_signal(&mi->mi_inact_req_cv); 2199 } else { 2200 mi->mi_async_req_count++; 2201 ASSERT(mi->mi_async_req_count != 0); 2202 cv_signal(&mi->mi_async_reqs_cv); 2203 } 2204 2205 mutex_exit(&mi->mi_async_lock); 2206 } 2207 2208 int 2209 writerp4(rnode4_t *rp, caddr_t base, int tcount, struct uio *uio, int pgcreated) 2210 { 2211 int pagecreate; 2212 int n; 2213 int saved_n; 2214 caddr_t saved_base; 2215 u_offset_t offset; 2216 int error; 2217 int sm_error; 2218 vnode_t *vp = RTOV(rp); 2219 2220 ASSERT(tcount <= MAXBSIZE && tcount <= uio->uio_resid); 2221 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_WRITER)); 2222 if (!vpm_enable) { 2223 ASSERT(((uintptr_t)base & MAXBOFFSET) + tcount <= MAXBSIZE); 2224 } 2225 2226 /* 2227 * Move bytes in at most PAGESIZE chunks. We must avoid 2228 * spanning pages in uiomove() because page faults may cause 2229 * the cache to be invalidated out from under us. The r_size is not 2230 * updated until after the uiomove. If we push the last page of a 2231 * file before r_size is correct, we will lose the data written past 2232 * the current (and invalid) r_size. 2233 */ 2234 do { 2235 offset = uio->uio_loffset; 2236 pagecreate = 0; 2237 2238 /* 2239 * n is the number of bytes required to satisfy the request 2240 * or the number of bytes to fill out the page. 2241 */ 2242 n = (int)MIN((PAGESIZE - (offset & PAGEOFFSET)), tcount); 2243 2244 /* 2245 * Check to see if we can skip reading in the page 2246 * and just allocate the memory. We can do this 2247 * if we are going to rewrite the entire mapping 2248 * or if we are going to write to or beyond the current 2249 * end of file from the beginning of the mapping. 2250 * 2251 * The read of r_size is now protected by r_statelock. 2252 */ 2253 mutex_enter(&rp->r_statelock); 2254 /* 2255 * When pgcreated is nonzero the caller has already done 2256 * a segmap_getmapflt with forcefault 0 and S_WRITE. With 2257 * segkpm this means we already have at least one page 2258 * created and mapped at base. 2259 */ 2260 pagecreate = pgcreated || 2261 ((offset & PAGEOFFSET) == 0 && 2262 (n == PAGESIZE || ((offset + n) >= rp->r_size))); 2263 2264 mutex_exit(&rp->r_statelock); 2265 2266 if (!vpm_enable && pagecreate) { 2267 /* 2268 * The last argument tells segmap_pagecreate() to 2269 * always lock the page, as opposed to sometimes 2270 * returning with the page locked. This way we avoid a 2271 * fault on the ensuing uiomove(), but also 2272 * more importantly (to fix bug 1094402) we can 2273 * call segmap_fault() to unlock the page in all 2274 * cases. An alternative would be to modify 2275 * segmap_pagecreate() to tell us when it is 2276 * locking a page, but that's a fairly major 2277 * interface change. 2278 */ 2279 if (pgcreated == 0) 2280 (void) segmap_pagecreate(segkmap, base, 2281 (uint_t)n, 1); 2282 saved_base = base; 2283 saved_n = n; 2284 } 2285 2286 /* 2287 * The number of bytes of data in the last page can not 2288 * be accurately be determined while page is being 2289 * uiomove'd to and the size of the file being updated. 2290 * Thus, inform threads which need to know accurately 2291 * how much data is in the last page of the file. They 2292 * will not do the i/o immediately, but will arrange for 2293 * the i/o to happen later when this modify operation 2294 * will have finished. 2295 */ 2296 ASSERT(!(rp->r_flags & R4MODINPROGRESS)); 2297 mutex_enter(&rp->r_statelock); 2298 rp->r_flags |= R4MODINPROGRESS; 2299 rp->r_modaddr = (offset & MAXBMASK); 2300 mutex_exit(&rp->r_statelock); 2301 2302 if (vpm_enable) { 2303 /* 2304 * Copy data. If new pages are created, part of 2305 * the page that is not written will be initizliazed 2306 * with zeros. 2307 */ 2308 error = vpm_data_copy(vp, offset, n, uio, 2309 !pagecreate, NULL, 0, S_WRITE); 2310 } else { 2311 error = uiomove(base, n, UIO_WRITE, uio); 2312 } 2313 2314 /* 2315 * r_size is the maximum number of 2316 * bytes known to be in the file. 2317 * Make sure it is at least as high as the 2318 * first unwritten byte pointed to by uio_loffset. 2319 */ 2320 mutex_enter(&rp->r_statelock); 2321 if (rp->r_size < uio->uio_loffset) 2322 rp->r_size = uio->uio_loffset; 2323 rp->r_flags &= ~R4MODINPROGRESS; 2324 rp->r_flags |= R4DIRTY; 2325 mutex_exit(&rp->r_statelock); 2326 2327 /* n = # of bytes written */ 2328 n = (int)(uio->uio_loffset - offset); 2329 2330 if (!vpm_enable) { 2331 base += n; 2332 } 2333 2334 tcount -= n; 2335 /* 2336 * If we created pages w/o initializing them completely, 2337 * we need to zero the part that wasn't set up. 2338 * This happens on a most EOF write cases and if 2339 * we had some sort of error during the uiomove. 2340 */ 2341 if (!vpm_enable && pagecreate) { 2342 if ((uio->uio_loffset & PAGEOFFSET) || n == 0) 2343 (void) kzero(base, PAGESIZE - n); 2344 2345 if (pgcreated) { 2346 /* 2347 * Caller is responsible for this page, 2348 * it was not created in this loop. 2349 */ 2350 pgcreated = 0; 2351 } else { 2352 /* 2353 * For bug 1094402: segmap_pagecreate locks 2354 * page. Unlock it. This also unlocks the 2355 * pages allocated by page_create_va() in 2356 * segmap_pagecreate(). 2357 */ 2358 sm_error = segmap_fault(kas.a_hat, segkmap, 2359 saved_base, saved_n, 2360 F_SOFTUNLOCK, S_WRITE); 2361 if (error == 0) 2362 error = sm_error; 2363 } 2364 } 2365 } while (tcount > 0 && error == 0); 2366 2367 return (error); 2368 } 2369 2370 int 2371 nfs4_putpages(vnode_t *vp, u_offset_t off, size_t len, int flags, cred_t *cr) 2372 { 2373 rnode4_t *rp; 2374 page_t *pp; 2375 u_offset_t eoff; 2376 u_offset_t io_off; 2377 size_t io_len; 2378 int error; 2379 int rdirty; 2380 int err; 2381 2382 rp = VTOR4(vp); 2383 ASSERT(rp->r_count > 0); 2384 2385 if (!nfs4_has_pages(vp)) 2386 return (0); 2387 2388 ASSERT(vp->v_type != VCHR); 2389 2390 /* 2391 * If R4OUTOFSPACE is set, then all writes turn into B_INVAL 2392 * writes. B_FORCE is set to force the VM system to actually 2393 * invalidate the pages, even if the i/o failed. The pages 2394 * need to get invalidated because they can't be written out 2395 * because there isn't any space left on either the server's 2396 * file system or in the user's disk quota. The B_FREE bit 2397 * is cleared to avoid confusion as to whether this is a 2398 * request to place the page on the freelist or to destroy 2399 * it. 2400 */ 2401 if ((rp->r_flags & R4OUTOFSPACE) || 2402 (vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) 2403 flags = (flags & ~B_FREE) | B_INVAL | B_FORCE; 2404 2405 if (len == 0) { 2406 /* 2407 * If doing a full file synchronous operation, then clear 2408 * the R4DIRTY bit. If a page gets dirtied while the flush 2409 * is happening, then R4DIRTY will get set again. The 2410 * R4DIRTY bit must get cleared before the flush so that 2411 * we don't lose this information. 2412 * 2413 * If there are no full file async write operations 2414 * pending and RDIRTY bit is set, clear it. 2415 */ 2416 if (off == (u_offset_t)0 && 2417 !(flags & B_ASYNC) && 2418 (rp->r_flags & R4DIRTY)) { 2419 mutex_enter(&rp->r_statelock); 2420 rdirty = (rp->r_flags & R4DIRTY); 2421 rp->r_flags &= ~R4DIRTY; 2422 mutex_exit(&rp->r_statelock); 2423 } else if (flags & B_ASYNC && off == (u_offset_t)0) { 2424 mutex_enter(&rp->r_statelock); 2425 if (rp->r_flags & R4DIRTY && rp->r_awcount == 0) { 2426 rdirty = (rp->r_flags & R4DIRTY); 2427 rp->r_flags &= ~R4DIRTY; 2428 } 2429 mutex_exit(&rp->r_statelock); 2430 } else 2431 rdirty = 0; 2432 2433 /* 2434 * Search the entire vp list for pages >= off, and flush 2435 * the dirty pages. 2436 */ 2437 error = pvn_vplist_dirty(vp, off, rp->r_putapage, 2438 flags, cr); 2439 2440 /* 2441 * If an error occurred and the file was marked as dirty 2442 * before and we aren't forcibly invalidating pages, then 2443 * reset the R4DIRTY flag. 2444 */ 2445 if (error && rdirty && 2446 (flags & (B_INVAL | B_FORCE)) != (B_INVAL | B_FORCE)) { 2447 mutex_enter(&rp->r_statelock); 2448 rp->r_flags |= R4DIRTY; 2449 mutex_exit(&rp->r_statelock); 2450 } 2451 } else { 2452 /* 2453 * Do a range from [off...off + len) looking for pages 2454 * to deal with. 2455 */ 2456 error = 0; 2457 io_len = 0; 2458 eoff = off + len; 2459 mutex_enter(&rp->r_statelock); 2460 for (io_off = off; io_off < eoff && io_off < rp->r_size; 2461 io_off += io_len) { 2462 mutex_exit(&rp->r_statelock); 2463 /* 2464 * If we are not invalidating, synchronously 2465 * freeing or writing pages use the routine 2466 * page_lookup_nowait() to prevent reclaiming 2467 * them from the free list. 2468 */ 2469 if ((flags & B_INVAL) || !(flags & B_ASYNC)) { 2470 pp = page_lookup(vp, io_off, 2471 (flags & (B_INVAL | B_FREE)) ? 2472 SE_EXCL : SE_SHARED); 2473 } else { 2474 pp = page_lookup_nowait(vp, io_off, 2475 (flags & B_FREE) ? SE_EXCL : SE_SHARED); 2476 } 2477 2478 if (pp == NULL || !pvn_getdirty(pp, flags)) 2479 io_len = PAGESIZE; 2480 else { 2481 err = (*rp->r_putapage)(vp, pp, &io_off, 2482 &io_len, flags, cr); 2483 if (!error) 2484 error = err; 2485 /* 2486 * "io_off" and "io_len" are returned as 2487 * the range of pages we actually wrote. 2488 * This allows us to skip ahead more quickly 2489 * since several pages may've been dealt 2490 * with by this iteration of the loop. 2491 */ 2492 } 2493 mutex_enter(&rp->r_statelock); 2494 } 2495 mutex_exit(&rp->r_statelock); 2496 } 2497 2498 return (error); 2499 } 2500 2501 void 2502 nfs4_invalidate_pages(vnode_t *vp, u_offset_t off, cred_t *cr) 2503 { 2504 rnode4_t *rp; 2505 2506 rp = VTOR4(vp); 2507 if (IS_SHADOW(vp, rp)) 2508 vp = RTOV4(rp); 2509 mutex_enter(&rp->r_statelock); 2510 while (rp->r_flags & R4TRUNCATE) 2511 cv_wait(&rp->r_cv, &rp->r_statelock); 2512 rp->r_flags |= R4TRUNCATE; 2513 if (off == (u_offset_t)0) { 2514 rp->r_flags &= ~R4DIRTY; 2515 if (!(rp->r_flags & R4STALE)) 2516 rp->r_error = 0; 2517 } 2518 rp->r_truncaddr = off; 2519 mutex_exit(&rp->r_statelock); 2520 (void) pvn_vplist_dirty(vp, off, rp->r_putapage, 2521 B_INVAL | B_TRUNC, cr); 2522 mutex_enter(&rp->r_statelock); 2523 rp->r_flags &= ~R4TRUNCATE; 2524 cv_broadcast(&rp->r_cv); 2525 mutex_exit(&rp->r_statelock); 2526 } 2527 2528 static int 2529 nfs4_mnt_kstat_update(kstat_t *ksp, int rw) 2530 { 2531 mntinfo4_t *mi; 2532 struct mntinfo_kstat *mik; 2533 vfs_t *vfsp; 2534 2535 /* this is a read-only kstat. Bail out on a write */ 2536 if (rw == KSTAT_WRITE) 2537 return (EACCES); 2538 2539 2540 /* 2541 * We don't want to wait here as kstat_chain_lock could be held by 2542 * dounmount(). dounmount() takes vfs_reflock before the chain lock 2543 * and thus could lead to a deadlock. 2544 */ 2545 vfsp = (struct vfs *)ksp->ks_private; 2546 2547 mi = VFTOMI4(vfsp); 2548 mik = (struct mntinfo_kstat *)ksp->ks_data; 2549 2550 (void) strcpy(mik->mik_proto, mi->mi_curr_serv->sv_knconf->knc_proto); 2551 2552 mik->mik_vers = (uint32_t)mi->mi_vers; 2553 mik->mik_flags = mi->mi_flags; 2554 /* 2555 * The sv_secdata holds the flavor the client specifies. 2556 * If the client uses default and a security negotiation 2557 * occurs, sv_currsec will point to the current flavor 2558 * selected from the server flavor list. 2559 * sv_currsec is NULL if no security negotiation takes place. 2560 */ 2561 mik->mik_secmod = mi->mi_curr_serv->sv_currsec ? 2562 mi->mi_curr_serv->sv_currsec->secmod : 2563 mi->mi_curr_serv->sv_secdata->secmod; 2564 mik->mik_curread = (uint32_t)mi->mi_curread; 2565 mik->mik_curwrite = (uint32_t)mi->mi_curwrite; 2566 mik->mik_retrans = mi->mi_retrans; 2567 mik->mik_timeo = mi->mi_timeo; 2568 mik->mik_acregmin = HR2SEC(mi->mi_acregmin); 2569 mik->mik_acregmax = HR2SEC(mi->mi_acregmax); 2570 mik->mik_acdirmin = HR2SEC(mi->mi_acdirmin); 2571 mik->mik_acdirmax = HR2SEC(mi->mi_acdirmax); 2572 mik->mik_noresponse = (uint32_t)mi->mi_noresponse; 2573 mik->mik_failover = (uint32_t)mi->mi_failover; 2574 mik->mik_remap = (uint32_t)mi->mi_remap; 2575 2576 (void) strcpy(mik->mik_curserver, mi->mi_curr_serv->sv_hostname); 2577 2578 return (0); 2579 } 2580 2581 void 2582 nfs4_mnt_kstat_init(struct vfs *vfsp) 2583 { 2584 mntinfo4_t *mi = VFTOMI4(vfsp); 2585 2586 /* 2587 * PSARC 2001/697 Contract Private Interface 2588 * All nfs kstats are under SunMC contract 2589 * Please refer to the PSARC listed above and contact 2590 * SunMC before making any changes! 2591 * 2592 * Changes must be reviewed by Solaris File Sharing 2593 * Changes must be communicated to contract-2001-697@sun.com 2594 * 2595 */ 2596 2597 mi->mi_io_kstats = kstat_create_zone("nfs", getminor(vfsp->vfs_dev), 2598 NULL, "nfs", KSTAT_TYPE_IO, 1, 0, mi->mi_zone->zone_id); 2599 if (mi->mi_io_kstats) { 2600 if (mi->mi_zone->zone_id != GLOBAL_ZONEID) 2601 kstat_zone_add(mi->mi_io_kstats, GLOBAL_ZONEID); 2602 mi->mi_io_kstats->ks_lock = &mi->mi_lock; 2603 kstat_install(mi->mi_io_kstats); 2604 } 2605 2606 if ((mi->mi_ro_kstats = kstat_create_zone("nfs", 2607 getminor(vfsp->vfs_dev), "mntinfo", "misc", KSTAT_TYPE_RAW, 2608 sizeof (struct mntinfo_kstat), 0, mi->mi_zone->zone_id)) != NULL) { 2609 if (mi->mi_zone->zone_id != GLOBAL_ZONEID) 2610 kstat_zone_add(mi->mi_ro_kstats, GLOBAL_ZONEID); 2611 mi->mi_ro_kstats->ks_update = nfs4_mnt_kstat_update; 2612 mi->mi_ro_kstats->ks_private = (void *)vfsp; 2613 kstat_install(mi->mi_ro_kstats); 2614 } 2615 2616 nfs4_mnt_recov_kstat_init(vfsp); 2617 } 2618 2619 void 2620 nfs4_write_error(vnode_t *vp, int error, cred_t *cr) 2621 { 2622 mntinfo4_t *mi; 2623 clock_t now = ddi_get_lbolt(); 2624 2625 mi = VTOMI4(vp); 2626 /* 2627 * In case of forced unmount, do not print any messages 2628 * since it can flood the console with error messages. 2629 */ 2630 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) 2631 return; 2632 2633 /* 2634 * If the mount point is dead, not recoverable, do not 2635 * print error messages that can flood the console. 2636 */ 2637 if (mi->mi_flags & MI4_RECOV_FAIL) 2638 return; 2639 2640 /* 2641 * No use in flooding the console with ENOSPC 2642 * messages from the same file system. 2643 */ 2644 if ((error != ENOSPC && error != EDQUOT) || 2645 now - mi->mi_printftime > 0) { 2646 zoneid_t zoneid = mi->mi_zone->zone_id; 2647 2648 #ifdef DEBUG 2649 nfs_perror(error, "NFS%ld write error on host %s: %m.\n", 2650 mi->mi_vers, VTOR4(vp)->r_server->sv_hostname, NULL); 2651 #else 2652 nfs_perror(error, "NFS write error on host %s: %m.\n", 2653 VTOR4(vp)->r_server->sv_hostname, NULL); 2654 #endif 2655 if (error == ENOSPC || error == EDQUOT) { 2656 zcmn_err(zoneid, CE_CONT, 2657 "^File: userid=%d, groupid=%d\n", 2658 crgetuid(cr), crgetgid(cr)); 2659 if (crgetuid(curthread->t_cred) != crgetuid(cr) || 2660 crgetgid(curthread->t_cred) != crgetgid(cr)) { 2661 zcmn_err(zoneid, CE_CONT, 2662 "^User: userid=%d, groupid=%d\n", 2663 crgetuid(curthread->t_cred), 2664 crgetgid(curthread->t_cred)); 2665 } 2666 mi->mi_printftime = now + 2667 nfs_write_error_interval * hz; 2668 } 2669 sfh4_printfhandle(VTOR4(vp)->r_fh); 2670 #ifdef DEBUG 2671 if (error == EACCES) { 2672 zcmn_err(zoneid, CE_CONT, 2673 "nfs_bio: cred is%s kcred\n", 2674 cr == kcred ? "" : " not"); 2675 } 2676 #endif 2677 } 2678 } 2679 2680 /* 2681 * Return non-zero if the given file can be safely memory mapped. Locks 2682 * are safe if whole-file (length and offset are both zero). 2683 */ 2684 2685 #define SAFE_LOCK(flk) ((flk).l_start == 0 && (flk).l_len == 0) 2686 2687 static int 2688 nfs4_safemap(const vnode_t *vp) 2689 { 2690 locklist_t *llp, *next_llp; 2691 int safe = 1; 2692 rnode4_t *rp = VTOR4(vp); 2693 2694 ASSERT(nfs_rw_lock_held(&rp->r_lkserlock, RW_WRITER)); 2695 2696 NFS4_DEBUG(nfs4_client_map_debug, (CE_NOTE, "nfs4_safemap: " 2697 "vp = %p", (void *)vp)); 2698 2699 /* 2700 * Review all the locks for the vnode, both ones that have been 2701 * acquired and ones that are pending. We assume that 2702 * flk_active_locks_for_vp() has merged any locks that can be 2703 * merged (so that if a process has the entire file locked, it is 2704 * represented as a single lock). 2705 * 2706 * Note that we can't bail out of the loop if we find a non-safe 2707 * lock, because we have to free all the elements in the llp list. 2708 * We might be able to speed up this code slightly by not looking 2709 * at each lock's l_start and l_len fields once we've found a 2710 * non-safe lock. 2711 */ 2712 2713 llp = flk_active_locks_for_vp(vp); 2714 while (llp) { 2715 NFS4_DEBUG(nfs4_client_map_debug, (CE_NOTE, 2716 "nfs4_safemap: active lock (%" PRId64 ", %" PRId64 ")", 2717 llp->ll_flock.l_start, llp->ll_flock.l_len)); 2718 if (!SAFE_LOCK(llp->ll_flock)) { 2719 safe = 0; 2720 NFS4_DEBUG(nfs4_client_map_debug, (CE_NOTE, 2721 "nfs4_safemap: unsafe active lock (%" PRId64 2722 ", %" PRId64 ")", llp->ll_flock.l_start, 2723 llp->ll_flock.l_len)); 2724 } 2725 next_llp = llp->ll_next; 2726 VN_RELE(llp->ll_vp); 2727 kmem_free(llp, sizeof (*llp)); 2728 llp = next_llp; 2729 } 2730 2731 NFS4_DEBUG(nfs4_client_map_debug, (CE_NOTE, "nfs4_safemap: %s", 2732 safe ? "safe" : "unsafe")); 2733 return (safe); 2734 } 2735 2736 /* 2737 * Return whether there is a lost LOCK or LOCKU queued up for the given 2738 * file that would make an mmap request unsafe. cf. nfs4_safemap(). 2739 */ 2740 2741 bool_t 2742 nfs4_map_lost_lock_conflict(vnode_t *vp) 2743 { 2744 bool_t conflict = FALSE; 2745 nfs4_lost_rqst_t *lrp; 2746 mntinfo4_t *mi = VTOMI4(vp); 2747 2748 mutex_enter(&mi->mi_lock); 2749 for (lrp = list_head(&mi->mi_lost_state); lrp != NULL; 2750 lrp = list_next(&mi->mi_lost_state, lrp)) { 2751 if (lrp->lr_op != OP_LOCK && lrp->lr_op != OP_LOCKU) 2752 continue; 2753 ASSERT(lrp->lr_vp != NULL); 2754 if (!VOP_CMP(lrp->lr_vp, vp, NULL)) 2755 continue; /* different file */ 2756 if (!SAFE_LOCK(*lrp->lr_flk)) { 2757 conflict = TRUE; 2758 break; 2759 } 2760 } 2761 2762 mutex_exit(&mi->mi_lock); 2763 return (conflict); 2764 } 2765 2766 /* 2767 * nfs_lockcompletion: 2768 * 2769 * If the vnode has a lock that makes it unsafe to cache the file, mark it 2770 * as non cachable (set VNOCACHE bit). 2771 */ 2772 2773 void 2774 nfs4_lockcompletion(vnode_t *vp, int cmd) 2775 { 2776 rnode4_t *rp = VTOR4(vp); 2777 2778 ASSERT(nfs_rw_lock_held(&rp->r_lkserlock, RW_WRITER)); 2779 ASSERT(!IS_SHADOW(vp, rp)); 2780 2781 if (cmd == F_SETLK || cmd == F_SETLKW) { 2782 2783 if (!nfs4_safemap(vp)) { 2784 mutex_enter(&vp->v_lock); 2785 vp->v_flag |= VNOCACHE; 2786 mutex_exit(&vp->v_lock); 2787 } else { 2788 mutex_enter(&vp->v_lock); 2789 vp->v_flag &= ~VNOCACHE; 2790 mutex_exit(&vp->v_lock); 2791 } 2792 } 2793 /* 2794 * The cached attributes of the file are stale after acquiring 2795 * the lock on the file. They were updated when the file was 2796 * opened, but not updated when the lock was acquired. Therefore the 2797 * cached attributes are invalidated after the lock is obtained. 2798 */ 2799 PURGE_ATTRCACHE4(vp); 2800 } 2801 2802 /* ARGSUSED */ 2803 static void * 2804 nfs4_mi_init(zoneid_t zoneid) 2805 { 2806 struct mi4_globals *mig; 2807 2808 mig = kmem_alloc(sizeof (*mig), KM_SLEEP); 2809 mutex_init(&mig->mig_lock, NULL, MUTEX_DEFAULT, NULL); 2810 list_create(&mig->mig_list, sizeof (mntinfo4_t), 2811 offsetof(mntinfo4_t, mi_zone_node)); 2812 mig->mig_destructor_called = B_FALSE; 2813 return (mig); 2814 } 2815 2816 /* 2817 * Callback routine to tell all NFSv4 mounts in the zone to start tearing down 2818 * state and killing off threads. 2819 */ 2820 /* ARGSUSED */ 2821 static void 2822 nfs4_mi_shutdown(zoneid_t zoneid, void *data) 2823 { 2824 struct mi4_globals *mig = data; 2825 mntinfo4_t *mi; 2826 nfs4_server_t *np; 2827 2828 NFS4_DEBUG(nfs4_client_zone_debug, (CE_NOTE, 2829 "nfs4_mi_shutdown zone %d\n", zoneid)); 2830 ASSERT(mig != NULL); 2831 for (;;) { 2832 mutex_enter(&mig->mig_lock); 2833 mi = list_head(&mig->mig_list); 2834 if (mi == NULL) { 2835 mutex_exit(&mig->mig_lock); 2836 break; 2837 } 2838 2839 NFS4_DEBUG(nfs4_client_zone_debug, (CE_NOTE, 2840 "nfs4_mi_shutdown stopping vfs %p\n", (void *)mi->mi_vfsp)); 2841 /* 2842 * purge the DNLC for this filesystem 2843 */ 2844 (void) dnlc_purge_vfsp(mi->mi_vfsp, 0); 2845 /* 2846 * Tell existing async worker threads to exit. 2847 */ 2848 mutex_enter(&mi->mi_async_lock); 2849 mi->mi_max_threads = 0; 2850 cv_broadcast(&mi->mi_async_work_cv); 2851 /* 2852 * Set the appropriate flags, signal and wait for both the 2853 * async manager and the inactive thread to exit when they're 2854 * done with their current work. 2855 */ 2856 mutex_enter(&mi->mi_lock); 2857 mi->mi_flags |= (MI4_ASYNC_MGR_STOP|MI4_DEAD); 2858 mutex_exit(&mi->mi_lock); 2859 mutex_exit(&mi->mi_async_lock); 2860 if (mi->mi_manager_thread) { 2861 nfs4_async_manager_stop(mi->mi_vfsp); 2862 } 2863 if (mi->mi_inactive_thread) { 2864 mutex_enter(&mi->mi_async_lock); 2865 cv_signal(&mi->mi_inact_req_cv); 2866 /* 2867 * Wait for the inactive thread to exit. 2868 */ 2869 while (mi->mi_inactive_thread != NULL) { 2870 cv_wait(&mi->mi_async_cv, &mi->mi_async_lock); 2871 } 2872 mutex_exit(&mi->mi_async_lock); 2873 } 2874 /* 2875 * Wait for the recovery thread to complete, that is, it will 2876 * signal when it is done using the "mi" structure and about 2877 * to exit 2878 */ 2879 mutex_enter(&mi->mi_lock); 2880 while (mi->mi_in_recovery > 0) 2881 cv_wait(&mi->mi_cv_in_recov, &mi->mi_lock); 2882 mutex_exit(&mi->mi_lock); 2883 /* 2884 * We're done when every mi has been done or the list is empty. 2885 * This one is done, remove it from the list. 2886 */ 2887 list_remove(&mig->mig_list, mi); 2888 mutex_exit(&mig->mig_lock); 2889 zone_rele(mi->mi_zone); 2890 /* 2891 * Release hold on vfs and mi done to prevent race with zone 2892 * shutdown. This releases the hold in nfs4_mi_zonelist_add. 2893 */ 2894 VFS_RELE(mi->mi_vfsp); 2895 MI4_RELE(mi); 2896 } 2897 /* 2898 * Tell each renew thread in the zone to exit 2899 */ 2900 mutex_enter(&nfs4_server_lst_lock); 2901 for (np = nfs4_server_lst.forw; np != &nfs4_server_lst; np = np->forw) { 2902 mutex_enter(&np->s_lock); 2903 if (np->zoneid == zoneid) { 2904 /* 2905 * We add another hold onto the nfs4_server_t 2906 * because this will make sure tha the nfs4_server_t 2907 * stays around until nfs4_callback_fini_zone destroys 2908 * the zone. This way, the renew thread can 2909 * unconditionally release its holds on the 2910 * nfs4_server_t. 2911 */ 2912 np->s_refcnt++; 2913 nfs4_mark_srv_dead(np); 2914 } 2915 mutex_exit(&np->s_lock); 2916 } 2917 mutex_exit(&nfs4_server_lst_lock); 2918 } 2919 2920 static void 2921 nfs4_mi_free_globals(struct mi4_globals *mig) 2922 { 2923 list_destroy(&mig->mig_list); /* makes sure the list is empty */ 2924 mutex_destroy(&mig->mig_lock); 2925 kmem_free(mig, sizeof (*mig)); 2926 } 2927 2928 /* ARGSUSED */ 2929 static void 2930 nfs4_mi_destroy(zoneid_t zoneid, void *data) 2931 { 2932 struct mi4_globals *mig = data; 2933 2934 NFS4_DEBUG(nfs4_client_zone_debug, (CE_NOTE, 2935 "nfs4_mi_destroy zone %d\n", zoneid)); 2936 ASSERT(mig != NULL); 2937 mutex_enter(&mig->mig_lock); 2938 if (list_head(&mig->mig_list) != NULL) { 2939 /* Still waiting for VFS_FREEVFS() */ 2940 mig->mig_destructor_called = B_TRUE; 2941 mutex_exit(&mig->mig_lock); 2942 return; 2943 } 2944 nfs4_mi_free_globals(mig); 2945 } 2946 2947 /* 2948 * Add an NFS mount to the per-zone list of NFS mounts. 2949 */ 2950 void 2951 nfs4_mi_zonelist_add(mntinfo4_t *mi) 2952 { 2953 struct mi4_globals *mig; 2954 2955 mig = zone_getspecific(mi4_list_key, mi->mi_zone); 2956 mutex_enter(&mig->mig_lock); 2957 list_insert_head(&mig->mig_list, mi); 2958 /* 2959 * hold added to eliminate race with zone shutdown -this will be 2960 * released in mi_shutdown 2961 */ 2962 MI4_HOLD(mi); 2963 VFS_HOLD(mi->mi_vfsp); 2964 mutex_exit(&mig->mig_lock); 2965 } 2966 2967 /* 2968 * Remove an NFS mount from the per-zone list of NFS mounts. 2969 */ 2970 int 2971 nfs4_mi_zonelist_remove(mntinfo4_t *mi) 2972 { 2973 struct mi4_globals *mig; 2974 int ret = 0; 2975 2976 mig = zone_getspecific(mi4_list_key, mi->mi_zone); 2977 mutex_enter(&mig->mig_lock); 2978 mutex_enter(&mi->mi_lock); 2979 /* if this mi is marked dead, then the zone already released it */ 2980 if (!(mi->mi_flags & MI4_DEAD)) { 2981 list_remove(&mig->mig_list, mi); 2982 mutex_exit(&mi->mi_lock); 2983 2984 /* release the holds put on in zonelist_add(). */ 2985 VFS_RELE(mi->mi_vfsp); 2986 MI4_RELE(mi); 2987 ret = 1; 2988 } else { 2989 mutex_exit(&mi->mi_lock); 2990 } 2991 2992 /* 2993 * We can be called asynchronously by VFS_FREEVFS() after the zone 2994 * shutdown/destroy callbacks have executed; if so, clean up the zone's 2995 * mi globals. 2996 */ 2997 if (list_head(&mig->mig_list) == NULL && 2998 mig->mig_destructor_called == B_TRUE) { 2999 nfs4_mi_free_globals(mig); 3000 return (ret); 3001 } 3002 mutex_exit(&mig->mig_lock); 3003 return (ret); 3004 } 3005 3006 void 3007 nfs_free_mi4(mntinfo4_t *mi) 3008 { 3009 nfs4_open_owner_t *foop; 3010 nfs4_oo_hash_bucket_t *bucketp; 3011 nfs4_debug_msg_t *msgp; 3012 int i; 3013 servinfo4_t *svp; 3014 3015 /* 3016 * Code introduced here should be carefully evaluated to make 3017 * sure none of the freed resources are accessed either directly 3018 * or indirectly after freeing them. For eg: Introducing calls to 3019 * NFS4_DEBUG that use mntinfo4_t structure member after freeing 3020 * the structure members or other routines calling back into NFS 3021 * accessing freed mntinfo4_t structure member. 3022 */ 3023 mutex_enter(&mi->mi_lock); 3024 ASSERT(mi->mi_recovthread == NULL); 3025 ASSERT(mi->mi_flags & MI4_ASYNC_MGR_STOP); 3026 mutex_exit(&mi->mi_lock); 3027 mutex_enter(&mi->mi_async_lock); 3028 ASSERT(mi->mi_threads == 0); 3029 ASSERT(mi->mi_manager_thread == NULL); 3030 mutex_exit(&mi->mi_async_lock); 3031 if (mi->mi_io_kstats) { 3032 kstat_delete(mi->mi_io_kstats); 3033 mi->mi_io_kstats = NULL; 3034 } 3035 if (mi->mi_ro_kstats) { 3036 kstat_delete(mi->mi_ro_kstats); 3037 mi->mi_ro_kstats = NULL; 3038 } 3039 if (mi->mi_recov_ksp) { 3040 kstat_delete(mi->mi_recov_ksp); 3041 mi->mi_recov_ksp = NULL; 3042 } 3043 mutex_enter(&mi->mi_msg_list_lock); 3044 while (msgp = list_head(&mi->mi_msg_list)) { 3045 list_remove(&mi->mi_msg_list, msgp); 3046 nfs4_free_msg(msgp); 3047 } 3048 mutex_exit(&mi->mi_msg_list_lock); 3049 list_destroy(&mi->mi_msg_list); 3050 if (mi->mi_fname != NULL) 3051 fn_rele(&mi->mi_fname); 3052 if (mi->mi_rootfh != NULL) 3053 sfh4_rele(&mi->mi_rootfh); 3054 if (mi->mi_srvparentfh != NULL) 3055 sfh4_rele(&mi->mi_srvparentfh); 3056 svp = mi->mi_servers; 3057 sv4_free(svp); 3058 mutex_destroy(&mi->mi_lock); 3059 mutex_destroy(&mi->mi_async_lock); 3060 mutex_destroy(&mi->mi_msg_list_lock); 3061 nfs_rw_destroy(&mi->mi_recovlock); 3062 nfs_rw_destroy(&mi->mi_rename_lock); 3063 nfs_rw_destroy(&mi->mi_fh_lock); 3064 cv_destroy(&mi->mi_failover_cv); 3065 cv_destroy(&mi->mi_async_reqs_cv); 3066 cv_destroy(&mi->mi_async_work_cv); 3067 cv_destroy(&mi->mi_async_cv); 3068 cv_destroy(&mi->mi_inact_req_cv); 3069 /* 3070 * Destroy the oo hash lists and mutexes for the cred hash table. 3071 */ 3072 for (i = 0; i < NFS4_NUM_OO_BUCKETS; i++) { 3073 bucketp = &(mi->mi_oo_list[i]); 3074 /* Destroy any remaining open owners on the list */ 3075 foop = list_head(&bucketp->b_oo_hash_list); 3076 while (foop != NULL) { 3077 list_remove(&bucketp->b_oo_hash_list, foop); 3078 nfs4_destroy_open_owner(foop); 3079 foop = list_head(&bucketp->b_oo_hash_list); 3080 } 3081 list_destroy(&bucketp->b_oo_hash_list); 3082 mutex_destroy(&bucketp->b_lock); 3083 } 3084 /* 3085 * Empty and destroy the freed open owner list. 3086 */ 3087 foop = list_head(&mi->mi_foo_list); 3088 while (foop != NULL) { 3089 list_remove(&mi->mi_foo_list, foop); 3090 nfs4_destroy_open_owner(foop); 3091 foop = list_head(&mi->mi_foo_list); 3092 } 3093 list_destroy(&mi->mi_foo_list); 3094 list_destroy(&mi->mi_bseqid_list); 3095 list_destroy(&mi->mi_lost_state); 3096 avl_destroy(&mi->mi_filehandles); 3097 kmem_free(mi, sizeof (*mi)); 3098 } 3099 void 3100 mi_hold(mntinfo4_t *mi) 3101 { 3102 atomic_add_32(&mi->mi_count, 1); 3103 ASSERT(mi->mi_count != 0); 3104 } 3105 3106 void 3107 mi_rele(mntinfo4_t *mi) 3108 { 3109 ASSERT(mi->mi_count != 0); 3110 if (atomic_add_32_nv(&mi->mi_count, -1) == 0) { 3111 nfs_free_mi4(mi); 3112 } 3113 } 3114 3115 vnode_t nfs4_xattr_notsupp_vnode; 3116 3117 void 3118 nfs4_clnt_init(void) 3119 { 3120 nfs4_vnops_init(); 3121 (void) nfs4_rnode_init(); 3122 (void) nfs4_shadow_init(); 3123 (void) nfs4_acache_init(); 3124 (void) nfs4_subr_init(); 3125 nfs4_acl_init(); 3126 nfs_idmap_init(); 3127 nfs4_callback_init(); 3128 nfs4_secinfo_init(); 3129 #ifdef DEBUG 3130 tsd_create(&nfs4_tsd_key, NULL); 3131 #endif 3132 3133 /* 3134 * Add a CPR callback so that we can update client 3135 * lease after a suspend and resume. 3136 */ 3137 cid = callb_add(nfs4_client_cpr_callb, 0, CB_CL_CPR_RPC, "nfs4"); 3138 3139 zone_key_create(&mi4_list_key, nfs4_mi_init, nfs4_mi_shutdown, 3140 nfs4_mi_destroy); 3141 3142 /* 3143 * Initialise the reference count of the notsupp xattr cache vnode to 1 3144 * so that it never goes away (VOP_INACTIVE isn't called on it). 3145 */ 3146 nfs4_xattr_notsupp_vnode.v_count = 1; 3147 } 3148 3149 void 3150 nfs4_clnt_fini(void) 3151 { 3152 (void) zone_key_delete(mi4_list_key); 3153 nfs4_vnops_fini(); 3154 (void) nfs4_rnode_fini(); 3155 (void) nfs4_shadow_fini(); 3156 (void) nfs4_acache_fini(); 3157 (void) nfs4_subr_fini(); 3158 nfs_idmap_fini(); 3159 nfs4_callback_fini(); 3160 nfs4_secinfo_fini(); 3161 #ifdef DEBUG 3162 tsd_destroy(&nfs4_tsd_key); 3163 #endif 3164 if (cid) 3165 (void) callb_delete(cid); 3166 } 3167 3168 /*ARGSUSED*/ 3169 static boolean_t 3170 nfs4_client_cpr_callb(void *arg, int code) 3171 { 3172 /* 3173 * We get called for Suspend and Resume events. 3174 * For the suspend case we simply don't care! 3175 */ 3176 if (code == CB_CODE_CPR_CHKPT) { 3177 return (B_TRUE); 3178 } 3179 3180 /* 3181 * When we get to here we are in the process of 3182 * resuming the system from a previous suspend. 3183 */ 3184 nfs4_client_resumed = gethrestime_sec(); 3185 return (B_TRUE); 3186 } 3187 3188 void 3189 nfs4_renew_lease_thread(nfs4_server_t *sp) 3190 { 3191 int error = 0; 3192 time_t tmp_last_renewal_time, tmp_time, tmp_now_time, kip_secs; 3193 clock_t tick_delay = 0; 3194 clock_t time_left = 0; 3195 callb_cpr_t cpr_info; 3196 kmutex_t cpr_lock; 3197 3198 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3199 "nfs4_renew_lease_thread: acting on sp 0x%p", (void*)sp)); 3200 mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL); 3201 CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr, "nfsv4Lease"); 3202 3203 mutex_enter(&sp->s_lock); 3204 /* sp->s_lease_time is set via a GETATTR */ 3205 sp->last_renewal_time = gethrestime_sec(); 3206 sp->lease_valid = NFS4_LEASE_UNINITIALIZED; 3207 ASSERT(sp->s_refcnt >= 1); 3208 3209 for (;;) { 3210 if (!sp->state_ref_count || 3211 sp->lease_valid != NFS4_LEASE_VALID) { 3212 3213 kip_secs = MAX((sp->s_lease_time >> 1) - 3214 (3 * sp->propagation_delay.tv_sec), 1); 3215 3216 tick_delay = SEC_TO_TICK(kip_secs); 3217 3218 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3219 "nfs4_renew_lease_thread: no renew : thread " 3220 "wait %ld secs", kip_secs)); 3221 3222 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3223 "nfs4_renew_lease_thread: no renew : " 3224 "state_ref_count %d, lease_valid %d", 3225 sp->state_ref_count, sp->lease_valid)); 3226 3227 mutex_enter(&cpr_lock); 3228 CALLB_CPR_SAFE_BEGIN(&cpr_info); 3229 mutex_exit(&cpr_lock); 3230 time_left = cv_reltimedwait(&sp->cv_thread_exit, 3231 &sp->s_lock, tick_delay, TR_CLOCK_TICK); 3232 mutex_enter(&cpr_lock); 3233 CALLB_CPR_SAFE_END(&cpr_info, &cpr_lock); 3234 mutex_exit(&cpr_lock); 3235 3236 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3237 "nfs4_renew_lease_thread: no renew: " 3238 "time left %ld", time_left)); 3239 3240 if (sp->s_thread_exit == NFS4_THREAD_EXIT) 3241 goto die; 3242 continue; 3243 } 3244 3245 tmp_last_renewal_time = sp->last_renewal_time; 3246 3247 tmp_time = gethrestime_sec() - sp->last_renewal_time + 3248 (3 * sp->propagation_delay.tv_sec); 3249 3250 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3251 "nfs4_renew_lease_thread: tmp_time %ld, " 3252 "sp->last_renewal_time %ld", tmp_time, 3253 sp->last_renewal_time)); 3254 3255 kip_secs = MAX((sp->s_lease_time >> 1) - tmp_time, 1); 3256 3257 tick_delay = SEC_TO_TICK(kip_secs); 3258 3259 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3260 "nfs4_renew_lease_thread: valid lease: sleep for %ld " 3261 "secs", kip_secs)); 3262 3263 mutex_enter(&cpr_lock); 3264 CALLB_CPR_SAFE_BEGIN(&cpr_info); 3265 mutex_exit(&cpr_lock); 3266 time_left = cv_reltimedwait(&sp->cv_thread_exit, &sp->s_lock, 3267 tick_delay, TR_CLOCK_TICK); 3268 mutex_enter(&cpr_lock); 3269 CALLB_CPR_SAFE_END(&cpr_info, &cpr_lock); 3270 mutex_exit(&cpr_lock); 3271 3272 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3273 "nfs4_renew_lease_thread: valid lease: time left %ld :" 3274 "sp last_renewal_time %ld, nfs4_client_resumed %ld, " 3275 "tmp_last_renewal_time %ld", time_left, 3276 sp->last_renewal_time, nfs4_client_resumed, 3277 tmp_last_renewal_time)); 3278 3279 if (sp->s_thread_exit == NFS4_THREAD_EXIT) 3280 goto die; 3281 3282 if (tmp_last_renewal_time == sp->last_renewal_time || 3283 (nfs4_client_resumed != 0 && 3284 nfs4_client_resumed > sp->last_renewal_time)) { 3285 /* 3286 * Issue RENEW op since we haven't renewed the lease 3287 * since we slept. 3288 */ 3289 tmp_now_time = gethrestime_sec(); 3290 error = nfs4renew(sp); 3291 /* 3292 * Need to re-acquire sp's lock, nfs4renew() 3293 * relinqueshes it. 3294 */ 3295 mutex_enter(&sp->s_lock); 3296 3297 /* 3298 * See if someone changed s_thread_exit while we gave 3299 * up s_lock. 3300 */ 3301 if (sp->s_thread_exit == NFS4_THREAD_EXIT) 3302 goto die; 3303 3304 if (!error) { 3305 /* 3306 * check to see if we implicitly renewed while 3307 * we waited for a reply for our RENEW call. 3308 */ 3309 if (tmp_last_renewal_time == 3310 sp->last_renewal_time) { 3311 /* no implicit renew came */ 3312 sp->last_renewal_time = tmp_now_time; 3313 } else { 3314 NFS4_DEBUG(nfs4_client_lease_debug, 3315 (CE_NOTE, "renew_thread: did " 3316 "implicit renewal before reply " 3317 "from server for RENEW")); 3318 } 3319 } else { 3320 /* figure out error */ 3321 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3322 "renew_thread: nfs4renew returned error" 3323 " %d", error)); 3324 } 3325 3326 } 3327 } 3328 3329 die: 3330 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3331 "nfs4_renew_lease_thread: thread exiting")); 3332 3333 while (sp->s_otw_call_count != 0) { 3334 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3335 "nfs4_renew_lease_thread: waiting for outstanding " 3336 "otw calls to finish for sp 0x%p, current " 3337 "s_otw_call_count %d", (void *)sp, 3338 sp->s_otw_call_count)); 3339 mutex_enter(&cpr_lock); 3340 CALLB_CPR_SAFE_BEGIN(&cpr_info); 3341 mutex_exit(&cpr_lock); 3342 cv_wait(&sp->s_cv_otw_count, &sp->s_lock); 3343 mutex_enter(&cpr_lock); 3344 CALLB_CPR_SAFE_END(&cpr_info, &cpr_lock); 3345 mutex_exit(&cpr_lock); 3346 } 3347 mutex_exit(&sp->s_lock); 3348 3349 nfs4_server_rele(sp); /* free the thread's reference */ 3350 nfs4_server_rele(sp); /* free the list's reference */ 3351 sp = NULL; 3352 3353 done: 3354 mutex_enter(&cpr_lock); 3355 CALLB_CPR_EXIT(&cpr_info); /* drops cpr_lock */ 3356 mutex_destroy(&cpr_lock); 3357 3358 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3359 "nfs4_renew_lease_thread: renew thread exit officially")); 3360 3361 zthread_exit(); 3362 /* NOT REACHED */ 3363 } 3364 3365 /* 3366 * Send out a RENEW op to the server. 3367 * Assumes sp is locked down. 3368 */ 3369 static int 3370 nfs4renew(nfs4_server_t *sp) 3371 { 3372 COMPOUND4args_clnt args; 3373 COMPOUND4res_clnt res; 3374 nfs_argop4 argop[1]; 3375 int doqueue = 1; 3376 int rpc_error; 3377 cred_t *cr; 3378 mntinfo4_t *mi; 3379 timespec_t prop_time, after_time; 3380 int needrecov = FALSE; 3381 nfs4_recov_state_t recov_state; 3382 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3383 3384 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "nfs4renew")); 3385 3386 recov_state.rs_flags = 0; 3387 recov_state.rs_num_retry_despite_err = 0; 3388 3389 recov_retry: 3390 mi = sp->mntinfo4_list; 3391 VFS_HOLD(mi->mi_vfsp); 3392 mutex_exit(&sp->s_lock); 3393 ASSERT(mi != NULL); 3394 3395 e.error = nfs4_start_op(mi, NULL, NULL, &recov_state); 3396 if (e.error) { 3397 VFS_RELE(mi->mi_vfsp); 3398 return (e.error); 3399 } 3400 3401 /* Check to see if we're dealing with a marked-dead sp */ 3402 mutex_enter(&sp->s_lock); 3403 if (sp->s_thread_exit == NFS4_THREAD_EXIT) { 3404 mutex_exit(&sp->s_lock); 3405 nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov); 3406 VFS_RELE(mi->mi_vfsp); 3407 return (0); 3408 } 3409 3410 /* Make sure mi hasn't changed on us */ 3411 if (mi != sp->mntinfo4_list) { 3412 /* Must drop sp's lock to avoid a recursive mutex enter */ 3413 mutex_exit(&sp->s_lock); 3414 nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov); 3415 VFS_RELE(mi->mi_vfsp); 3416 mutex_enter(&sp->s_lock); 3417 goto recov_retry; 3418 } 3419 mutex_exit(&sp->s_lock); 3420 3421 args.ctag = TAG_RENEW; 3422 3423 args.array_len = 1; 3424 args.array = argop; 3425 3426 argop[0].argop = OP_RENEW; 3427 3428 mutex_enter(&sp->s_lock); 3429 argop[0].nfs_argop4_u.oprenew.clientid = sp->clientid; 3430 cr = sp->s_cred; 3431 crhold(cr); 3432 mutex_exit(&sp->s_lock); 3433 3434 ASSERT(cr != NULL); 3435 3436 /* used to figure out RTT for sp */ 3437 gethrestime(&prop_time); 3438 3439 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 3440 "nfs4renew: %s call, sp 0x%p", needrecov ? "recov" : "first", 3441 (void*)sp)); 3442 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "before: %ld s %ld ns ", 3443 prop_time.tv_sec, prop_time.tv_nsec)); 3444 3445 DTRACE_PROBE2(nfs4__renew__start, nfs4_server_t *, sp, 3446 mntinfo4_t *, mi); 3447 3448 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3449 crfree(cr); 3450 3451 DTRACE_PROBE2(nfs4__renew__end, nfs4_server_t *, sp, 3452 mntinfo4_t *, mi); 3453 3454 gethrestime(&after_time); 3455 3456 mutex_enter(&sp->s_lock); 3457 sp->propagation_delay.tv_sec = 3458 MAX(1, after_time.tv_sec - prop_time.tv_sec); 3459 mutex_exit(&sp->s_lock); 3460 3461 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "after : %ld s %ld ns ", 3462 after_time.tv_sec, after_time.tv_nsec)); 3463 3464 if (e.error == 0 && res.status == NFS4ERR_CB_PATH_DOWN) { 3465 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3466 nfs4_delegreturn_all(sp); 3467 nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov); 3468 VFS_RELE(mi->mi_vfsp); 3469 /* 3470 * If the server returns CB_PATH_DOWN, it has renewed 3471 * the lease and informed us that the callback path is 3472 * down. Since the lease is renewed, just return 0 and 3473 * let the renew thread proceed as normal. 3474 */ 3475 return (0); 3476 } 3477 3478 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3479 if (!needrecov && e.error) { 3480 nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov); 3481 VFS_RELE(mi->mi_vfsp); 3482 return (e.error); 3483 } 3484 3485 rpc_error = e.error; 3486 3487 if (needrecov) { 3488 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3489 "nfs4renew: initiating recovery\n")); 3490 3491 if (nfs4_start_recovery(&e, mi, NULL, NULL, NULL, NULL, 3492 OP_RENEW, NULL) == FALSE) { 3493 nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov); 3494 VFS_RELE(mi->mi_vfsp); 3495 if (!e.error) 3496 (void) xdr_free(xdr_COMPOUND4res_clnt, 3497 (caddr_t)&res); 3498 mutex_enter(&sp->s_lock); 3499 goto recov_retry; 3500 } 3501 /* fall through for res.status case */ 3502 } 3503 3504 if (res.status) { 3505 if (res.status == NFS4ERR_LEASE_MOVED) { 3506 /*EMPTY*/ 3507 /* 3508 * XXX need to try every mntinfo4 in sp->mntinfo4_list 3509 * to renew the lease on that server 3510 */ 3511 } 3512 e.error = geterrno4(res.status); 3513 } 3514 3515 if (!rpc_error) 3516 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3517 3518 nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov); 3519 3520 VFS_RELE(mi->mi_vfsp); 3521 3522 return (e.error); 3523 } 3524 3525 void 3526 nfs4_inc_state_ref_count(mntinfo4_t *mi) 3527 { 3528 nfs4_server_t *sp; 3529 3530 /* this locks down sp if it is found */ 3531 sp = find_nfs4_server(mi); 3532 3533 if (sp != NULL) { 3534 nfs4_inc_state_ref_count_nolock(sp, mi); 3535 mutex_exit(&sp->s_lock); 3536 nfs4_server_rele(sp); 3537 } 3538 } 3539 3540 /* 3541 * Bump the number of OPEN files (ie: those with state) so we know if this 3542 * nfs4_server has any state to maintain a lease for or not. 3543 * 3544 * Also, marks the nfs4_server's lease valid if it hasn't been done so already. 3545 */ 3546 void 3547 nfs4_inc_state_ref_count_nolock(nfs4_server_t *sp, mntinfo4_t *mi) 3548 { 3549 ASSERT(mutex_owned(&sp->s_lock)); 3550 3551 sp->state_ref_count++; 3552 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3553 "nfs4_inc_state_ref_count: state_ref_count now %d", 3554 sp->state_ref_count)); 3555 3556 if (sp->lease_valid == NFS4_LEASE_UNINITIALIZED) 3557 sp->lease_valid = NFS4_LEASE_VALID; 3558 3559 /* 3560 * If this call caused the lease to be marked valid and/or 3561 * took the state_ref_count from 0 to 1, then start the time 3562 * on lease renewal. 3563 */ 3564 if (sp->lease_valid == NFS4_LEASE_VALID && sp->state_ref_count == 1) 3565 sp->last_renewal_time = gethrestime_sec(); 3566 3567 /* update the number of open files for mi */ 3568 mi->mi_open_files++; 3569 } 3570 3571 void 3572 nfs4_dec_state_ref_count(mntinfo4_t *mi) 3573 { 3574 nfs4_server_t *sp; 3575 3576 /* this locks down sp if it is found */ 3577 sp = find_nfs4_server_all(mi, 1); 3578 3579 if (sp != NULL) { 3580 nfs4_dec_state_ref_count_nolock(sp, mi); 3581 mutex_exit(&sp->s_lock); 3582 nfs4_server_rele(sp); 3583 } 3584 } 3585 3586 /* 3587 * Decrement the number of OPEN files (ie: those with state) so we know if 3588 * this nfs4_server has any state to maintain a lease for or not. 3589 */ 3590 void 3591 nfs4_dec_state_ref_count_nolock(nfs4_server_t *sp, mntinfo4_t *mi) 3592 { 3593 ASSERT(mutex_owned(&sp->s_lock)); 3594 ASSERT(sp->state_ref_count != 0); 3595 sp->state_ref_count--; 3596 3597 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3598 "nfs4_dec_state_ref_count: state ref count now %d", 3599 sp->state_ref_count)); 3600 3601 mi->mi_open_files--; 3602 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3603 "nfs4_dec_state_ref_count: mi open files %d, v4 flags 0x%x", 3604 mi->mi_open_files, mi->mi_flags)); 3605 3606 /* We don't have to hold the mi_lock to test mi_flags */ 3607 if (mi->mi_open_files == 0 && 3608 (mi->mi_flags & MI4_REMOVE_ON_LAST_CLOSE)) { 3609 NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, 3610 "nfs4_dec_state_ref_count: remove mntinfo4 %p since " 3611 "we have closed the last open file", (void*)mi)); 3612 nfs4_remove_mi_from_server(mi, sp); 3613 } 3614 } 3615 3616 bool_t 3617 inlease(nfs4_server_t *sp) 3618 { 3619 bool_t result; 3620 3621 ASSERT(mutex_owned(&sp->s_lock)); 3622 3623 if (sp->lease_valid == NFS4_LEASE_VALID && 3624 gethrestime_sec() < sp->last_renewal_time + sp->s_lease_time) 3625 result = TRUE; 3626 else 3627 result = FALSE; 3628 3629 return (result); 3630 } 3631 3632 3633 /* 3634 * Return non-zero if the given nfs4_server_t is going through recovery. 3635 */ 3636 3637 int 3638 nfs4_server_in_recovery(nfs4_server_t *sp) 3639 { 3640 return (nfs_rw_lock_held(&sp->s_recovlock, RW_WRITER)); 3641 } 3642 3643 /* 3644 * Compare two shared filehandle objects. Returns -1, 0, or +1, if the 3645 * first is less than, equal to, or greater than the second. 3646 */ 3647 3648 int 3649 sfh4cmp(const void *p1, const void *p2) 3650 { 3651 const nfs4_sharedfh_t *sfh1 = (const nfs4_sharedfh_t *)p1; 3652 const nfs4_sharedfh_t *sfh2 = (const nfs4_sharedfh_t *)p2; 3653 3654 return (nfs4cmpfh(&sfh1->sfh_fh, &sfh2->sfh_fh)); 3655 } 3656 3657 /* 3658 * Create a table for shared filehandle objects. 3659 */ 3660 3661 void 3662 sfh4_createtab(avl_tree_t *tab) 3663 { 3664 avl_create(tab, sfh4cmp, sizeof (nfs4_sharedfh_t), 3665 offsetof(nfs4_sharedfh_t, sfh_tree)); 3666 } 3667 3668 /* 3669 * Return a shared filehandle object for the given filehandle. The caller 3670 * is responsible for eventually calling sfh4_rele(). 3671 */ 3672 3673 nfs4_sharedfh_t * 3674 sfh4_put(const nfs_fh4 *fh, mntinfo4_t *mi, nfs4_sharedfh_t *key) 3675 { 3676 nfs4_sharedfh_t *sfh, *nsfh; 3677 avl_index_t where; 3678 nfs4_sharedfh_t skey; 3679 3680 if (!key) { 3681 skey.sfh_fh = *fh; 3682 key = &skey; 3683 } 3684 3685 nsfh = kmem_alloc(sizeof (nfs4_sharedfh_t), KM_SLEEP); 3686 nsfh->sfh_fh.nfs_fh4_len = fh->nfs_fh4_len; 3687 /* 3688 * We allocate the largest possible filehandle size because it's 3689 * not that big, and it saves us from possibly having to resize the 3690 * buffer later. 3691 */ 3692 nsfh->sfh_fh.nfs_fh4_val = kmem_alloc(NFS4_FHSIZE, KM_SLEEP); 3693 bcopy(fh->nfs_fh4_val, nsfh->sfh_fh.nfs_fh4_val, fh->nfs_fh4_len); 3694 mutex_init(&nsfh->sfh_lock, NULL, MUTEX_DEFAULT, NULL); 3695 nsfh->sfh_refcnt = 1; 3696 nsfh->sfh_flags = SFH4_IN_TREE; 3697 nsfh->sfh_mi = mi; 3698 NFS4_DEBUG(nfs4_sharedfh_debug, (CE_NOTE, "sfh4_get: new object (%p)", 3699 (void *)nsfh)); 3700 3701 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_WRITER, 0); 3702 sfh = avl_find(&mi->mi_filehandles, key, &where); 3703 if (sfh != NULL) { 3704 mutex_enter(&sfh->sfh_lock); 3705 sfh->sfh_refcnt++; 3706 mutex_exit(&sfh->sfh_lock); 3707 nfs_rw_exit(&mi->mi_fh_lock); 3708 /* free our speculative allocs */ 3709 kmem_free(nsfh->sfh_fh.nfs_fh4_val, NFS4_FHSIZE); 3710 kmem_free(nsfh, sizeof (nfs4_sharedfh_t)); 3711 return (sfh); 3712 } 3713 3714 avl_insert(&mi->mi_filehandles, nsfh, where); 3715 nfs_rw_exit(&mi->mi_fh_lock); 3716 3717 return (nsfh); 3718 } 3719 3720 /* 3721 * Return a shared filehandle object for the given filehandle. The caller 3722 * is responsible for eventually calling sfh4_rele(). 3723 */ 3724 3725 nfs4_sharedfh_t * 3726 sfh4_get(const nfs_fh4 *fh, mntinfo4_t *mi) 3727 { 3728 nfs4_sharedfh_t *sfh; 3729 nfs4_sharedfh_t key; 3730 3731 ASSERT(fh->nfs_fh4_len <= NFS4_FHSIZE); 3732 3733 #ifdef DEBUG 3734 if (nfs4_sharedfh_debug) { 3735 nfs4_fhandle_t fhandle; 3736 3737 fhandle.fh_len = fh->nfs_fh4_len; 3738 bcopy(fh->nfs_fh4_val, fhandle.fh_buf, fhandle.fh_len); 3739 zcmn_err(mi->mi_zone->zone_id, CE_NOTE, "sfh4_get:"); 3740 nfs4_printfhandle(&fhandle); 3741 } 3742 #endif 3743 3744 /* 3745 * If there's already an object for the given filehandle, bump the 3746 * reference count and return it. Otherwise, create a new object 3747 * and add it to the AVL tree. 3748 */ 3749 3750 key.sfh_fh = *fh; 3751 3752 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0); 3753 sfh = avl_find(&mi->mi_filehandles, &key, NULL); 3754 if (sfh != NULL) { 3755 mutex_enter(&sfh->sfh_lock); 3756 sfh->sfh_refcnt++; 3757 NFS4_DEBUG(nfs4_sharedfh_debug, (CE_NOTE, 3758 "sfh4_get: found existing %p, new refcnt=%d", 3759 (void *)sfh, sfh->sfh_refcnt)); 3760 mutex_exit(&sfh->sfh_lock); 3761 nfs_rw_exit(&mi->mi_fh_lock); 3762 return (sfh); 3763 } 3764 nfs_rw_exit(&mi->mi_fh_lock); 3765 3766 return (sfh4_put(fh, mi, &key)); 3767 } 3768 3769 /* 3770 * Get a reference to the given shared filehandle object. 3771 */ 3772 3773 void 3774 sfh4_hold(nfs4_sharedfh_t *sfh) 3775 { 3776 ASSERT(sfh->sfh_refcnt > 0); 3777 3778 mutex_enter(&sfh->sfh_lock); 3779 sfh->sfh_refcnt++; 3780 NFS4_DEBUG(nfs4_sharedfh_debug, 3781 (CE_NOTE, "sfh4_hold %p, new refcnt=%d", 3782 (void *)sfh, sfh->sfh_refcnt)); 3783 mutex_exit(&sfh->sfh_lock); 3784 } 3785 3786 /* 3787 * Release a reference to the given shared filehandle object and null out 3788 * the given pointer. 3789 */ 3790 3791 void 3792 sfh4_rele(nfs4_sharedfh_t **sfhpp) 3793 { 3794 mntinfo4_t *mi; 3795 nfs4_sharedfh_t *sfh = *sfhpp; 3796 3797 ASSERT(sfh->sfh_refcnt > 0); 3798 3799 mutex_enter(&sfh->sfh_lock); 3800 if (sfh->sfh_refcnt > 1) { 3801 sfh->sfh_refcnt--; 3802 NFS4_DEBUG(nfs4_sharedfh_debug, (CE_NOTE, 3803 "sfh4_rele %p, new refcnt=%d", 3804 (void *)sfh, sfh->sfh_refcnt)); 3805 mutex_exit(&sfh->sfh_lock); 3806 goto finish; 3807 } 3808 mutex_exit(&sfh->sfh_lock); 3809 3810 /* 3811 * Possibly the last reference, so get the lock for the table in 3812 * case it's time to remove the object from the table. 3813 */ 3814 mi = sfh->sfh_mi; 3815 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_WRITER, 0); 3816 mutex_enter(&sfh->sfh_lock); 3817 sfh->sfh_refcnt--; 3818 if (sfh->sfh_refcnt > 0) { 3819 NFS4_DEBUG(nfs4_sharedfh_debug, (CE_NOTE, 3820 "sfh4_rele %p, new refcnt=%d", 3821 (void *)sfh, sfh->sfh_refcnt)); 3822 mutex_exit(&sfh->sfh_lock); 3823 nfs_rw_exit(&mi->mi_fh_lock); 3824 goto finish; 3825 } 3826 3827 NFS4_DEBUG(nfs4_sharedfh_debug, (CE_NOTE, 3828 "sfh4_rele %p, last ref", (void *)sfh)); 3829 if (sfh->sfh_flags & SFH4_IN_TREE) { 3830 avl_remove(&mi->mi_filehandles, sfh); 3831 sfh->sfh_flags &= ~SFH4_IN_TREE; 3832 } 3833 mutex_exit(&sfh->sfh_lock); 3834 nfs_rw_exit(&mi->mi_fh_lock); 3835 mutex_destroy(&sfh->sfh_lock); 3836 kmem_free(sfh->sfh_fh.nfs_fh4_val, NFS4_FHSIZE); 3837 kmem_free(sfh, sizeof (nfs4_sharedfh_t)); 3838 3839 finish: 3840 *sfhpp = NULL; 3841 } 3842 3843 /* 3844 * Update the filehandle for the given shared filehandle object. 3845 */ 3846 3847 int nfs4_warn_dupfh = 0; /* if set, always warn about dup fhs below */ 3848 3849 void 3850 sfh4_update(nfs4_sharedfh_t *sfh, const nfs_fh4 *newfh) 3851 { 3852 mntinfo4_t *mi = sfh->sfh_mi; 3853 nfs4_sharedfh_t *dupsfh; 3854 avl_index_t where; 3855 nfs4_sharedfh_t key; 3856 3857 #ifdef DEBUG 3858 mutex_enter(&sfh->sfh_lock); 3859 ASSERT(sfh->sfh_refcnt > 0); 3860 mutex_exit(&sfh->sfh_lock); 3861 #endif 3862 ASSERT(newfh->nfs_fh4_len <= NFS4_FHSIZE); 3863 3864 /* 3865 * The basic plan is to remove the shared filehandle object from 3866 * the table, update it to have the new filehandle, then reinsert 3867 * it. 3868 */ 3869 3870 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_WRITER, 0); 3871 mutex_enter(&sfh->sfh_lock); 3872 if (sfh->sfh_flags & SFH4_IN_TREE) { 3873 avl_remove(&mi->mi_filehandles, sfh); 3874 sfh->sfh_flags &= ~SFH4_IN_TREE; 3875 } 3876 mutex_exit(&sfh->sfh_lock); 3877 sfh->sfh_fh.nfs_fh4_len = newfh->nfs_fh4_len; 3878 bcopy(newfh->nfs_fh4_val, sfh->sfh_fh.nfs_fh4_val, 3879 sfh->sfh_fh.nfs_fh4_len); 3880 3881 /* 3882 * XXX If there is already a shared filehandle object with the new 3883 * filehandle, we're in trouble, because the rnode code assumes 3884 * that there is only one shared filehandle object for a given 3885 * filehandle. So issue a warning (for read-write mounts only) 3886 * and don't try to re-insert the given object into the table. 3887 * Hopefully the given object will quickly go away and everyone 3888 * will use the new object. 3889 */ 3890 key.sfh_fh = *newfh; 3891 dupsfh = avl_find(&mi->mi_filehandles, &key, &where); 3892 if (dupsfh != NULL) { 3893 if (!(mi->mi_vfsp->vfs_flag & VFS_RDONLY) || nfs4_warn_dupfh) { 3894 zcmn_err(mi->mi_zone->zone_id, CE_WARN, "sfh4_update: " 3895 "duplicate filehandle detected"); 3896 sfh4_printfhandle(dupsfh); 3897 } 3898 } else { 3899 avl_insert(&mi->mi_filehandles, sfh, where); 3900 mutex_enter(&sfh->sfh_lock); 3901 sfh->sfh_flags |= SFH4_IN_TREE; 3902 mutex_exit(&sfh->sfh_lock); 3903 } 3904 nfs_rw_exit(&mi->mi_fh_lock); 3905 } 3906 3907 /* 3908 * Copy out the current filehandle for the given shared filehandle object. 3909 */ 3910 3911 void 3912 sfh4_copyval(const nfs4_sharedfh_t *sfh, nfs4_fhandle_t *fhp) 3913 { 3914 mntinfo4_t *mi = sfh->sfh_mi; 3915 3916 ASSERT(sfh->sfh_refcnt > 0); 3917 3918 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0); 3919 fhp->fh_len = sfh->sfh_fh.nfs_fh4_len; 3920 ASSERT(fhp->fh_len <= NFS4_FHSIZE); 3921 bcopy(sfh->sfh_fh.nfs_fh4_val, fhp->fh_buf, fhp->fh_len); 3922 nfs_rw_exit(&mi->mi_fh_lock); 3923 } 3924 3925 /* 3926 * Print out the filehandle for the given shared filehandle object. 3927 */ 3928 3929 void 3930 sfh4_printfhandle(const nfs4_sharedfh_t *sfh) 3931 { 3932 nfs4_fhandle_t fhandle; 3933 3934 sfh4_copyval(sfh, &fhandle); 3935 nfs4_printfhandle(&fhandle); 3936 } 3937 3938 /* 3939 * Compare 2 fnames. Returns -1 if the first is "less" than the second, 0 3940 * if they're the same, +1 if the first is "greater" than the second. The 3941 * caller (or whoever's calling the AVL package) is responsible for 3942 * handling locking issues. 3943 */ 3944 3945 static int 3946 fncmp(const void *p1, const void *p2) 3947 { 3948 const nfs4_fname_t *f1 = p1; 3949 const nfs4_fname_t *f2 = p2; 3950 int res; 3951 3952 res = strcmp(f1->fn_name, f2->fn_name); 3953 /* 3954 * The AVL package wants +/-1, not arbitrary positive or negative 3955 * integers. 3956 */ 3957 if (res > 0) 3958 res = 1; 3959 else if (res < 0) 3960 res = -1; 3961 return (res); 3962 } 3963 3964 /* 3965 * Get or create an fname with the given name, as a child of the given 3966 * fname. The caller is responsible for eventually releasing the reference 3967 * (fn_rele()). parent may be NULL. 3968 */ 3969 3970 nfs4_fname_t * 3971 fn_get(nfs4_fname_t *parent, char *name, nfs4_sharedfh_t *sfh) 3972 { 3973 nfs4_fname_t key; 3974 nfs4_fname_t *fnp; 3975 avl_index_t where; 3976 3977 key.fn_name = name; 3978 3979 /* 3980 * If there's already an fname registered with the given name, bump 3981 * its reference count and return it. Otherwise, create a new one 3982 * and add it to the parent's AVL tree. 3983 * 3984 * fname entries we are looking for should match both name 3985 * and sfh stored in the fname. 3986 */ 3987 again: 3988 if (parent != NULL) { 3989 mutex_enter(&parent->fn_lock); 3990 fnp = avl_find(&parent->fn_children, &key, &where); 3991 if (fnp != NULL) { 3992 /* 3993 * This hold on fnp is released below later, 3994 * in case this is not the fnp we want. 3995 */ 3996 fn_hold(fnp); 3997 3998 if (fnp->fn_sfh == sfh) { 3999 /* 4000 * We have found our entry. 4001 * put an hold and return it. 4002 */ 4003 mutex_exit(&parent->fn_lock); 4004 return (fnp); 4005 } 4006 4007 /* 4008 * We have found an entry that has a mismatching 4009 * fn_sfh. This could be a stale entry due to 4010 * server side rename. We will remove this entry 4011 * and make sure no such entries exist. 4012 */ 4013 mutex_exit(&parent->fn_lock); 4014 mutex_enter(&fnp->fn_lock); 4015 if (fnp->fn_parent == parent) { 4016 /* 4017 * Remove ourselves from parent's 4018 * fn_children tree. 4019 */ 4020 mutex_enter(&parent->fn_lock); 4021 avl_remove(&parent->fn_children, fnp); 4022 mutex_exit(&parent->fn_lock); 4023 fn_rele(&fnp->fn_parent); 4024 } 4025 mutex_exit(&fnp->fn_lock); 4026 fn_rele(&fnp); 4027 goto again; 4028 } 4029 } 4030 4031 fnp = kmem_alloc(sizeof (nfs4_fname_t), KM_SLEEP); 4032 mutex_init(&fnp->fn_lock, NULL, MUTEX_DEFAULT, NULL); 4033 fnp->fn_parent = parent; 4034 if (parent != NULL) 4035 fn_hold(parent); 4036 fnp->fn_len = strlen(name); 4037 ASSERT(fnp->fn_len < MAXNAMELEN); 4038 fnp->fn_name = kmem_alloc(fnp->fn_len + 1, KM_SLEEP); 4039 (void) strcpy(fnp->fn_name, name); 4040 fnp->fn_refcnt = 1; 4041 4042 /* 4043 * This hold on sfh is later released 4044 * when we do the final fn_rele() on this fname. 4045 */ 4046 sfh4_hold(sfh); 4047 fnp->fn_sfh = sfh; 4048 4049 avl_create(&fnp->fn_children, fncmp, sizeof (nfs4_fname_t), 4050 offsetof(nfs4_fname_t, fn_tree)); 4051 NFS4_DEBUG(nfs4_fname_debug, (CE_NOTE, 4052 "fn_get %p:%s, a new nfs4_fname_t!", 4053 (void *)fnp, fnp->fn_name)); 4054 if (parent != NULL) { 4055 avl_insert(&parent->fn_children, fnp, where); 4056 mutex_exit(&parent->fn_lock); 4057 } 4058 4059 return (fnp); 4060 } 4061 4062 void 4063 fn_hold(nfs4_fname_t *fnp) 4064 { 4065 atomic_add_32(&fnp->fn_refcnt, 1); 4066 NFS4_DEBUG(nfs4_fname_debug, (CE_NOTE, 4067 "fn_hold %p:%s, new refcnt=%d", 4068 (void *)fnp, fnp->fn_name, fnp->fn_refcnt)); 4069 } 4070 4071 /* 4072 * Decrement the reference count of the given fname, and destroy it if its 4073 * reference count goes to zero. Nulls out the given pointer. 4074 */ 4075 4076 void 4077 fn_rele(nfs4_fname_t **fnpp) 4078 { 4079 nfs4_fname_t *parent; 4080 uint32_t newref; 4081 nfs4_fname_t *fnp; 4082 4083 recur: 4084 fnp = *fnpp; 4085 *fnpp = NULL; 4086 4087 mutex_enter(&fnp->fn_lock); 4088 parent = fnp->fn_parent; 4089 if (parent != NULL) 4090 mutex_enter(&parent->fn_lock); /* prevent new references */ 4091 newref = atomic_add_32_nv(&fnp->fn_refcnt, -1); 4092 if (newref > 0) { 4093 NFS4_DEBUG(nfs4_fname_debug, (CE_NOTE, 4094 "fn_rele %p:%s, new refcnt=%d", 4095 (void *)fnp, fnp->fn_name, fnp->fn_refcnt)); 4096 if (parent != NULL) 4097 mutex_exit(&parent->fn_lock); 4098 mutex_exit(&fnp->fn_lock); 4099 return; 4100 } 4101 4102 NFS4_DEBUG(nfs4_fname_debug, (CE_NOTE, 4103 "fn_rele %p:%s, last reference, deleting...", 4104 (void *)fnp, fnp->fn_name)); 4105 if (parent != NULL) { 4106 avl_remove(&parent->fn_children, fnp); 4107 mutex_exit(&parent->fn_lock); 4108 } 4109 kmem_free(fnp->fn_name, fnp->fn_len + 1); 4110 sfh4_rele(&fnp->fn_sfh); 4111 mutex_destroy(&fnp->fn_lock); 4112 avl_destroy(&fnp->fn_children); 4113 kmem_free(fnp, sizeof (nfs4_fname_t)); 4114 /* 4115 * Recursivly fn_rele the parent. 4116 * Use goto instead of a recursive call to avoid stack overflow. 4117 */ 4118 if (parent != NULL) { 4119 fnpp = &parent; 4120 goto recur; 4121 } 4122 } 4123 4124 /* 4125 * Returns the single component name of the given fname, in a MAXNAMELEN 4126 * string buffer, which the caller is responsible for freeing. Note that 4127 * the name may become invalid as a result of fn_move(). 4128 */ 4129 4130 char * 4131 fn_name(nfs4_fname_t *fnp) 4132 { 4133 char *name; 4134 4135 ASSERT(fnp->fn_len < MAXNAMELEN); 4136 name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 4137 mutex_enter(&fnp->fn_lock); 4138 (void) strcpy(name, fnp->fn_name); 4139 mutex_exit(&fnp->fn_lock); 4140 4141 return (name); 4142 } 4143 4144 4145 /* 4146 * fn_path_realloc 4147 * 4148 * This function, used only by fn_path, constructs 4149 * a new string which looks like "prepend" + "/" + "current". 4150 * by allocating a new string and freeing the old one. 4151 */ 4152 static void 4153 fn_path_realloc(char **curses, char *prepend) 4154 { 4155 int len, curlen = 0; 4156 char *news; 4157 4158 if (*curses == NULL) { 4159 /* 4160 * Prime the pump, allocate just the 4161 * space for prepend and return that. 4162 */ 4163 len = strlen(prepend) + 1; 4164 news = kmem_alloc(len, KM_SLEEP); 4165 (void) strncpy(news, prepend, len); 4166 } else { 4167 /* 4168 * Allocate the space for a new string 4169 * +1 +1 is for the "/" and the NULL 4170 * byte at the end of it all. 4171 */ 4172 curlen = strlen(*curses); 4173 len = curlen + strlen(prepend) + 1 + 1; 4174 news = kmem_alloc(len, KM_SLEEP); 4175 (void) strncpy(news, prepend, len); 4176 (void) strcat(news, "/"); 4177 (void) strcat(news, *curses); 4178 kmem_free(*curses, curlen + 1); 4179 } 4180 *curses = news; 4181 } 4182 4183 /* 4184 * Returns the path name (starting from the fs root) for the given fname. 4185 * The caller is responsible for freeing. Note that the path may be or 4186 * become invalid as a result of fn_move(). 4187 */ 4188 4189 char * 4190 fn_path(nfs4_fname_t *fnp) 4191 { 4192 char *path; 4193 nfs4_fname_t *nextfnp; 4194 4195 if (fnp == NULL) 4196 return (NULL); 4197 4198 path = NULL; 4199 4200 /* walk up the tree constructing the pathname. */ 4201 4202 fn_hold(fnp); /* adjust for later rele */ 4203 do { 4204 mutex_enter(&fnp->fn_lock); 4205 /* 4206 * Add fn_name in front of the current path 4207 */ 4208 fn_path_realloc(&path, fnp->fn_name); 4209 nextfnp = fnp->fn_parent; 4210 if (nextfnp != NULL) 4211 fn_hold(nextfnp); 4212 mutex_exit(&fnp->fn_lock); 4213 fn_rele(&fnp); 4214 fnp = nextfnp; 4215 } while (fnp != NULL); 4216 4217 return (path); 4218 } 4219 4220 /* 4221 * Return a reference to the parent of the given fname, which the caller is 4222 * responsible for eventually releasing. 4223 */ 4224 4225 nfs4_fname_t * 4226 fn_parent(nfs4_fname_t *fnp) 4227 { 4228 nfs4_fname_t *parent; 4229 4230 mutex_enter(&fnp->fn_lock); 4231 parent = fnp->fn_parent; 4232 if (parent != NULL) 4233 fn_hold(parent); 4234 mutex_exit(&fnp->fn_lock); 4235 4236 return (parent); 4237 } 4238 4239 /* 4240 * Update fnp so that its parent is newparent and its name is newname. 4241 */ 4242 4243 void 4244 fn_move(nfs4_fname_t *fnp, nfs4_fname_t *newparent, char *newname) 4245 { 4246 nfs4_fname_t *parent, *tmpfnp; 4247 ssize_t newlen; 4248 nfs4_fname_t key; 4249 avl_index_t where; 4250 4251 /* 4252 * This assert exists to catch the client trying to rename 4253 * a dir to be a child of itself. This happened at a recent 4254 * bakeoff against a 3rd party (broken) server which allowed 4255 * the rename to succeed. If it trips it means that: 4256 * a) the code in nfs4rename that detects this case is broken 4257 * b) the server is broken (since it allowed the bogus rename) 4258 * 4259 * For non-DEBUG kernels, prepare for a recursive mutex_enter 4260 * panic below from: mutex_enter(&newparent->fn_lock); 4261 */ 4262 ASSERT(fnp != newparent); 4263 4264 /* 4265 * Remove fnp from its current parent, change its name, then add it 4266 * to newparent. It might happen that fnp was replaced by another 4267 * nfs4_fname_t with the same fn_name in parent->fn_children. 4268 * In such case, fnp->fn_parent is NULL and we skip the removal 4269 * of fnp from its current parent. 4270 */ 4271 mutex_enter(&fnp->fn_lock); 4272 parent = fnp->fn_parent; 4273 if (parent != NULL) { 4274 mutex_enter(&parent->fn_lock); 4275 avl_remove(&parent->fn_children, fnp); 4276 mutex_exit(&parent->fn_lock); 4277 fn_rele(&fnp->fn_parent); 4278 } 4279 4280 newlen = strlen(newname); 4281 if (newlen != fnp->fn_len) { 4282 ASSERT(newlen < MAXNAMELEN); 4283 kmem_free(fnp->fn_name, fnp->fn_len + 1); 4284 fnp->fn_name = kmem_alloc(newlen + 1, KM_SLEEP); 4285 fnp->fn_len = newlen; 4286 } 4287 (void) strcpy(fnp->fn_name, newname); 4288 4289 again: 4290 mutex_enter(&newparent->fn_lock); 4291 key.fn_name = fnp->fn_name; 4292 tmpfnp = avl_find(&newparent->fn_children, &key, &where); 4293 if (tmpfnp != NULL) { 4294 /* 4295 * This could be due to a file that was unlinked while 4296 * open, or perhaps the rnode is in the free list. Remove 4297 * it from newparent and let it go away on its own. The 4298 * contorted code is to deal with lock order issues and 4299 * race conditions. 4300 */ 4301 fn_hold(tmpfnp); 4302 mutex_exit(&newparent->fn_lock); 4303 mutex_enter(&tmpfnp->fn_lock); 4304 if (tmpfnp->fn_parent == newparent) { 4305 mutex_enter(&newparent->fn_lock); 4306 avl_remove(&newparent->fn_children, tmpfnp); 4307 mutex_exit(&newparent->fn_lock); 4308 fn_rele(&tmpfnp->fn_parent); 4309 } 4310 mutex_exit(&tmpfnp->fn_lock); 4311 fn_rele(&tmpfnp); 4312 goto again; 4313 } 4314 fnp->fn_parent = newparent; 4315 fn_hold(newparent); 4316 avl_insert(&newparent->fn_children, fnp, where); 4317 mutex_exit(&newparent->fn_lock); 4318 mutex_exit(&fnp->fn_lock); 4319 } 4320 4321 #ifdef DEBUG 4322 /* 4323 * Return non-zero if the type information makes sense for the given vnode. 4324 * Otherwise panic. 4325 */ 4326 int 4327 nfs4_consistent_type(vnode_t *vp) 4328 { 4329 rnode4_t *rp = VTOR4(vp); 4330 4331 if (nfs4_vtype_debug && vp->v_type != VNON && 4332 rp->r_attr.va_type != VNON && vp->v_type != rp->r_attr.va_type) { 4333 cmn_err(CE_PANIC, "vnode %p type mismatch; v_type=%d, " 4334 "rnode attr type=%d", (void *)vp, vp->v_type, 4335 rp->r_attr.va_type); 4336 } 4337 4338 return (1); 4339 } 4340 #endif /* DEBUG */ 4341