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 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 29 /* 30 * Portions of this source code were derived from Berkeley 4.3 BSD 31 * under license from the Regents of the University of California. 32 */ 33 34 #pragma ident "%Z%%M% %I% %E% SMI" 35 36 #include <sys/types.h> 37 #include <sys/systm.h> 38 #include <sys/errno.h> 39 #include <sys/kmem.h> 40 #include <sys/buf.h> 41 #include <sys/vnode.h> 42 #include <sys/vfs.h> 43 #include <sys/user.h> 44 #include <sys/callb.h> 45 #include <sys/cpuvar.h> 46 #include <sys/fs/ufs_inode.h> 47 #include <sys/fs/ufs_log.h> 48 #include <sys/fs/ufs_trans.h> 49 #include <sys/fs/ufs_acl.h> 50 #include <sys/fs/ufs_bio.h> 51 #include <sys/fs/ufs_fsdir.h> 52 #include <sys/debug.h> 53 #include <sys/cmn_err.h> 54 #include <sys/sysmacros.h> 55 56 extern pri_t minclsyspri; 57 extern int hash2ints(); 58 extern struct kmem_cache *inode_cache; /* cache of free inodes */ 59 extern int ufs_idle_waiters; 60 extern struct instats ins; 61 62 static void ufs_attr_purge(struct inode *); 63 64 /* 65 * initialize a thread's queue struct 66 */ 67 void 68 ufs_thread_init(struct ufs_q *uq, int lowat) 69 { 70 bzero((caddr_t)uq, sizeof (*uq)); 71 cv_init(&uq->uq_cv, NULL, CV_DEFAULT, NULL); 72 mutex_init(&uq->uq_mutex, NULL, MUTEX_DEFAULT, NULL); 73 uq->uq_lowat = lowat; 74 uq->uq_hiwat = 2 * lowat; 75 uq->uq_threadp = NULL; 76 } 77 78 /* 79 * start a thread for a queue (assumes success) 80 */ 81 void 82 ufs_thread_start(struct ufs_q *uq, void (*func)(), struct vfs *vfsp) 83 { 84 mutex_enter(&uq->uq_mutex); 85 if (uq->uq_threadp == NULL) { 86 uq->uq_threadp = thread_create(NULL, 0, func, vfsp, 0, &p0, 87 TS_RUN, minclsyspri); 88 uq->uq_flags = 0; 89 } 90 mutex_exit(&uq->uq_mutex); 91 } 92 93 /* 94 * wait for the thread to exit 95 */ 96 void 97 ufs_thread_exit(struct ufs_q *uq) 98 { 99 kt_did_t ufs_thread_did = 0; 100 101 mutex_enter(&uq->uq_mutex); 102 uq->uq_flags &= ~(UQ_SUSPEND | UQ_SUSPENDED); 103 if (uq->uq_threadp != NULL) { 104 ufs_thread_did = uq->uq_threadp->t_did; 105 uq->uq_flags |= (UQ_EXIT|UQ_WAIT); 106 cv_broadcast(&uq->uq_cv); 107 } 108 mutex_exit(&uq->uq_mutex); 109 110 /* 111 * It's safe to call thread_join() with an already-gone 112 * t_did, but we have to obtain it before the kernel 113 * thread structure is freed. We do so above under the 114 * protection of the uq_mutex when we're sure the thread 115 * still exists and it's save to de-reference it. 116 * We also have to check if ufs_thread_did is != 0 117 * before calling thread_join() since thread 0 in the system 118 * gets a t_did of 0. 119 */ 120 if (ufs_thread_did) 121 thread_join(ufs_thread_did); 122 } 123 124 /* 125 * wait for a thread to suspend itself on the caller's behalf 126 * the caller is responsible for continuing the thread 127 */ 128 void 129 ufs_thread_suspend(struct ufs_q *uq) 130 { 131 mutex_enter(&uq->uq_mutex); 132 if (uq->uq_threadp != NULL) { 133 /* 134 * wait while another thread is suspending this thread. 135 * no need to do a cv_broadcast(), as whoever suspended 136 * the thread must continue it at some point. 137 */ 138 while ((uq->uq_flags & UQ_SUSPEND) && 139 (uq->uq_threadp != NULL)) { 140 /* 141 * We can't use cv_signal() because if our 142 * signal doesn't happen to hit the desired 143 * thread but instead some other waiter like 144 * ourselves, we'll wait forever for a 145 * response. Well, at least an indeterminate 146 * amount of time until we just happen to get 147 * lucky from whomever did get signalled doing 148 * a cv_signal() of their own. This is an 149 * unfortunate performance lossage. 150 */ 151 uq->uq_flags |= UQ_WAIT; 152 cv_wait(&uq->uq_cv, &uq->uq_mutex); 153 } 154 155 uq->uq_flags |= (UQ_SUSPEND | UQ_WAIT); 156 157 /* 158 * wait for the thread to suspend itself 159 */ 160 if ((uq->uq_flags & UQ_SUSPENDED) == 0 && 161 (uq->uq_threadp != NULL)) { 162 cv_broadcast(&uq->uq_cv); 163 } 164 165 while (((uq->uq_flags & UQ_SUSPENDED) == 0) && 166 (uq->uq_threadp != NULL)) { 167 cv_wait(&uq->uq_cv, &uq->uq_mutex); 168 } 169 } 170 mutex_exit(&uq->uq_mutex); 171 } 172 173 /* 174 * allow a thread to continue from a ufs_thread_suspend() 175 * This thread must be the same as the thread that called 176 * ufs_thread_suspend. 177 */ 178 void 179 ufs_thread_continue(struct ufs_q *uq) 180 { 181 mutex_enter(&uq->uq_mutex); 182 uq->uq_flags &= ~(UQ_SUSPEND | UQ_SUSPENDED); 183 cv_broadcast(&uq->uq_cv); 184 mutex_exit(&uq->uq_mutex); 185 } 186 187 /* 188 * some common code for managing a threads execution 189 * uq is locked at entry and return 190 * may sleep 191 * may exit 192 */ 193 /* 194 * Kind of a hack passing in the callb_cpr_t * here. 195 * It should really be part of the ufs_q structure. 196 * I did not put it in there because we are already in beta 197 * and I was concerned that changing ufs_inode.h to include 198 * callb.h might break something. 199 */ 200 int 201 ufs_thread_run(struct ufs_q *uq, callb_cpr_t *cprinfop) 202 { 203 again: 204 ASSERT(uq->uq_ne >= 0); 205 206 if (uq->uq_flags & UQ_SUSPEND) { 207 uq->uq_flags |= UQ_SUSPENDED; 208 } else if (uq->uq_flags & UQ_EXIT) { 209 /* 210 * exiting; empty the queue (may infinite loop) 211 */ 212 if (uq->uq_ne) 213 return (uq->uq_ne); 214 uq->uq_threadp = NULL; 215 if (uq->uq_flags & UQ_WAIT) { 216 cv_broadcast(&uq->uq_cv); 217 } 218 uq->uq_flags &= ~(UQ_EXIT | UQ_WAIT); 219 CALLB_CPR_EXIT(cprinfop); 220 thread_exit(); 221 } else if (uq->uq_ne >= uq->uq_lowat) { 222 /* 223 * process a block of entries until below high water mark 224 */ 225 return (uq->uq_ne - (uq->uq_lowat >> 1)); 226 } 227 if (uq->uq_flags & UQ_WAIT) { 228 uq->uq_flags &= ~UQ_WAIT; 229 cv_broadcast(&uq->uq_cv); 230 } 231 CALLB_CPR_SAFE_BEGIN(cprinfop); 232 cv_wait(&uq->uq_cv, &uq->uq_mutex); 233 CALLB_CPR_SAFE_END(cprinfop, &uq->uq_mutex); 234 goto again; 235 } 236 237 /* 238 * DELETE INODE 239 * The following routines implement the protocol for freeing the resources 240 * held by an idle and deleted inode. 241 */ 242 void 243 ufs_delete(struct ufsvfs *ufsvfsp, struct inode *ip, int dolockfs) 244 { 245 ushort_t mode; 246 struct vnode *vp = ITOV(ip); 247 struct ulockfs *ulp; 248 int trans_size; 249 int dorwlock = ((ip->i_mode & IFMT) == IFREG); 250 int issync; 251 int err; 252 struct inode *dp; 253 struct ufs_q *delq = &ufsvfsp->vfs_delete; 254 struct ufs_delq_info *delq_info = &ufsvfsp->vfs_delete_info; 255 256 /* 257 * not on a trans device or not part of a transaction 258 */ 259 ASSERT(!TRANS_ISTRANS(ufsvfsp) || 260 ((curthread->t_flag & T_DONTBLOCK) == 0)); 261 262 /* 263 * Ignore if deletes are not allowed (wlock/hlock) 264 */ 265 if (ULOCKFS_IS_NOIDEL(ITOUL(ip))) { 266 mutex_enter(&delq->uq_mutex); 267 delq_info->delq_unreclaimed_blocks -= ip->i_blocks; 268 delq_info->delq_unreclaimed_files--; 269 mutex_exit(&delq->uq_mutex); 270 VN_RELE(vp); 271 return; 272 } 273 274 if ((vp->v_count > 1) || (ip->i_mode == 0)) { 275 mutex_enter(&delq->uq_mutex); 276 delq_info->delq_unreclaimed_blocks -= ip->i_blocks; 277 delq_info->delq_unreclaimed_files--; 278 mutex_exit(&delq->uq_mutex); 279 VN_RELE(vp); 280 return; 281 } 282 /* 283 * If we are called as part of setting a fs lock, then only 284 * do part of the lockfs protocol. In other words, don't hang. 285 */ 286 if (dolockfs) { 287 if (ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_DELETE_MASK)) 288 return; 289 } else { 290 /* 291 * check for recursive VOP call 292 */ 293 if (curthread->t_flag & T_DONTBLOCK) { 294 ulp = NULL; 295 } else { 296 ulp = &ufsvfsp->vfs_ulockfs; 297 curthread->t_flag |= T_DONTBLOCK; 298 } 299 } 300 301 /* 302 * Hold rwlock to synchronize with (nfs) writes 303 */ 304 if (dorwlock) 305 rw_enter(&ip->i_rwlock, RW_WRITER); 306 307 /* 308 * Delete the attribute directory. 309 */ 310 if (ip->i_oeftflag != 0) { 311 TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_REMOVE, 312 trans_size = (int)TOP_REMOVE_SIZE(ip)); 313 rw_enter(&ip->i_contents, RW_WRITER); 314 err = ufs_iget(ip->i_vfs, ip->i_oeftflag, 315 &dp, CRED()); 316 if (err == 0) { 317 rw_enter(&dp->i_rwlock, RW_WRITER); 318 rw_enter(&dp->i_contents, RW_WRITER); 319 dp->i_flag |= IUPD|ICHG; 320 dp->i_seq++; 321 TRANS_INODE(dp->i_ufsvfs, dp); 322 dp->i_nlink -= 2; 323 ufs_setreclaim(dp); 324 /* 325 * Should get rid of any negative cache entries that 326 * might be lingering, as well as ``.'' and 327 * ``..''. If we don't, the VN_RELE() below 328 * won't actually put dp on the delete queue 329 * and it'll hang out until someone forces it 330 * (lockfs -f, umount, ...). The only reliable 331 * way of doing this at the moment is to call 332 * dnlc_purge_vp(ITOV(dp)), which is unacceptably 333 * slow, so we'll just note the problem in this 334 * comment for now. 335 */ 336 dnlc_remove(ITOV(dp), "."); 337 dnlc_remove(ITOV(dp), ".."); 338 ITIMES_NOLOCK(dp); 339 if (!TRANS_ISTRANS(ufsvfsp)) { 340 ufs_iupdat(dp, I_SYNC); 341 } 342 rw_exit(&dp->i_contents); 343 rw_exit(&dp->i_rwlock); 344 VN_RELE(ITOV(dp)); 345 } 346 /* 347 * Clear out attribute pointer 348 */ 349 ip->i_oeftflag = 0; 350 rw_exit(&ip->i_contents); 351 TRANS_END_CSYNC(ufsvfsp, err, issync, 352 TOP_REMOVE, trans_size); 353 dnlc_remove(ITOV(ip), XATTR_DIR_NAME); 354 } 355 356 if ((ip->i_mode & IFMT) == IFATTRDIR) { 357 ufs_attr_purge(ip); 358 } 359 360 (void) TRANS_ITRUNC(ip, (u_offset_t)0, I_FREE | I_ACCT, CRED()); 361 362 /* 363 * the inode's space has been freed; now free the inode 364 */ 365 if (ulp) { 366 trans_size = TOP_IFREE_SIZE(ip); 367 TRANS_BEGIN_ASYNC(ufsvfsp, TOP_IFREE, trans_size); 368 } 369 rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER); 370 rw_enter(&ip->i_contents, RW_WRITER); 371 TRANS_INODE(ufsvfsp, ip); 372 mode = ip->i_mode; 373 ip->i_mode = 0; 374 ip->i_rdev = 0; 375 ip->i_ordev = 0; 376 ip->i_flag |= IMOD; 377 if (ip->i_ufs_acl) { 378 (void) ufs_si_free(ip->i_ufs_acl, vp->v_vfsp, CRED()); 379 ip->i_ufs_acl = NULL; 380 ip->i_shadow = 0; 381 } 382 383 /* 384 * This inode is torn down but still retains it's identity 385 * (inode number). It could get recycled soon so it's best 386 * to clean up the vnode just in case. 387 */ 388 mutex_enter(&vp->v_lock); 389 vn_recycle(vp); 390 mutex_exit(&vp->v_lock); 391 392 /* 393 * free the inode 394 */ 395 ufs_ifree(ip, ip->i_number, mode); 396 /* 397 * release quota resources; can't fail 398 */ 399 (void) chkiq((struct ufsvfs *)vp->v_vfsp->vfs_data, 400 /* change */ -1, ip, (uid_t)ip->i_uid, 0, CRED(), 401 (char **)NULL, (size_t *)NULL); 402 dqrele(ip->i_dquot); 403 ip->i_dquot = NULL; 404 ip->i_flag &= ~(IDEL | IDIRECTIO); 405 ip->i_cflags = 0; 406 if (!TRANS_ISTRANS(ufsvfsp)) { 407 ufs_iupdat(ip, I_SYNC); 408 } else { 409 mutex_enter(&delq->uq_mutex); 410 delq_info->delq_unreclaimed_files--; 411 mutex_exit(&delq->uq_mutex); 412 } 413 rw_exit(&ip->i_contents); 414 rw_exit(&ufsvfsp->vfs_dqrwlock); 415 if (dorwlock) 416 rw_exit(&ip->i_rwlock); 417 VN_RELE(vp); 418 419 /* 420 * End of transaction 421 */ 422 if (ulp) { 423 TRANS_END_ASYNC(ufsvfsp, TOP_IFREE, trans_size); 424 if (dolockfs) 425 ufs_lockfs_end(ulp); 426 else 427 curthread->t_flag &= ~T_DONTBLOCK; 428 } 429 } 430 431 /* 432 * Create the delete thread and init the delq_info for this fs 433 */ 434 void 435 ufs_delete_init(struct ufsvfs *ufsvfsp, int lowat) 436 { 437 struct ufs_delq_info *delq_info = &ufsvfsp->vfs_delete_info; 438 439 ufs_thread_init(&ufsvfsp->vfs_delete, lowat); 440 (void) memset((void *)delq_info, 0, sizeof (*delq_info)); 441 } 442 443 /* 444 * thread that frees up deleted inodes 445 */ 446 void 447 ufs_thread_delete(struct vfs *vfsp) 448 { 449 struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data; 450 struct ufs_q *uq = &ufsvfsp->vfs_delete; 451 struct ufs_delq_info *delq_info = &ufsvfsp->vfs_delete_info; 452 struct inode *ip; 453 long ne; 454 callb_cpr_t cprinfo; 455 456 CALLB_CPR_INIT(&cprinfo, &uq->uq_mutex, callb_generic_cpr, 457 "ufsdelete"); 458 459 mutex_enter(&uq->uq_mutex); 460 again: 461 /* 462 * Sleep until there is work to do. Only do one entry at 463 * a time, to reduce the wait time for checking for a suspend 464 * request. The ?: is for pedantic portability. 465 */ 466 ne = ufs_thread_run(uq, &cprinfo) ? 1 : 0; 467 468 /* 469 * process an entry, if there are any 470 */ 471 if (ne && (ip = uq->uq_ihead)) { 472 /* 473 * process first entry on queue. Assumed conditions are: 474 * ip is held (v_count >= 1) 475 * ip is referenced (i_flag & IREF) 476 * ip is free (i_nlink <= 0) 477 */ 478 if ((uq->uq_ihead = ip->i_freef) == ip) 479 uq->uq_ihead = NULL; 480 ip->i_freef->i_freeb = ip->i_freeb; 481 ip->i_freeb->i_freef = ip->i_freef; 482 ip->i_freef = ip; 483 ip->i_freeb = ip; 484 uq->uq_ne--; 485 mutex_exit(&uq->uq_mutex); 486 ufs_delete(ufsvfsp, ip, 1); 487 mutex_enter(&uq->uq_mutex); 488 } 489 goto again; 490 } 491 492 /* 493 * drain ne entries off the delete queue. As new queue entries may 494 * be added while we're working, ne is interpreted as follows: 495 * 496 * ne > 0 => remove up to ne entries 497 * ne == 0 => remove all entries currently on the queue 498 * ne == -1 => remove entries until the queue is empty 499 */ 500 void 501 ufs_delete_drain(struct vfs *vfsp, int ne, int dolockfs) 502 { 503 struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data; 504 struct ufs_q *uq; 505 struct inode *ip; 506 int drain_cnt = 0; 507 int done; 508 509 /* 510 * if forcibly unmounted; ignore 511 */ 512 if (ufsvfsp == NULL) 513 return; 514 515 uq = &ufsvfsp->vfs_delete; 516 mutex_enter(&uq->uq_mutex); 517 if (ne == 0) 518 drain_cnt = uq->uq_ne; 519 else if (ne > 0) 520 drain_cnt = ne; 521 522 /* 523 * process up to ne entries 524 */ 525 526 done = 0; 527 while (!done && (ip = uq->uq_ihead)) { 528 if (ne != -1) 529 drain_cnt--; 530 if (ne != -1 && drain_cnt == 0) 531 done = 1; 532 if ((uq->uq_ihead = ip->i_freef) == ip) 533 uq->uq_ihead = NULL; 534 ip->i_freef->i_freeb = ip->i_freeb; 535 ip->i_freeb->i_freef = ip->i_freef; 536 ip->i_freef = ip; 537 ip->i_freeb = ip; 538 uq->uq_ne--; 539 mutex_exit(&uq->uq_mutex); 540 ufs_delete(ufsvfsp, ip, dolockfs); 541 mutex_enter(&uq->uq_mutex); 542 } 543 mutex_exit(&uq->uq_mutex); 544 } 545 546 void 547 ufs_sync_with_thread(struct ufs_q *uq) 548 { 549 mutex_enter(&uq->uq_mutex); 550 551 /* 552 * Wake up delete thread to free up space. 553 */ 554 if ((uq->uq_flags & UQ_WAIT) == 0) { 555 uq->uq_flags |= UQ_WAIT; 556 cv_broadcast(&uq->uq_cv); 557 } 558 559 while ((uq->uq_threadp != NULL) && (uq->uq_flags & UQ_WAIT)) { 560 cv_wait(&uq->uq_cv, &uq->uq_mutex); 561 } 562 563 mutex_exit(&uq->uq_mutex); 564 } 565 566 /* 567 * Get rid of everything that's currently in the delete queue, 568 * plus whatever the delete thread is working on at the moment. 569 * 570 * This ability is required for providing true POSIX semantics 571 * regarding close(2), unlink(2), etc, even when logging is enabled. 572 * The standard requires that the released space be immediately 573 * observable (statvfs(2)) and allocatable (e.g., write(2)). 574 */ 575 void 576 ufs_delete_drain_wait(struct ufsvfs *ufsvfsp, int dolockfs) 577 { 578 struct ufs_q *uq = &ufsvfsp->vfs_delete; 579 int error; 580 struct ufs_q *delq = &ufsvfsp->vfs_delete; 581 struct ufs_delq_info *delq_info = &ufsvfsp->vfs_delete_info; 582 583 /* 584 * If there is something on delq or delete thread 585 * working on delq. 586 */ 587 mutex_enter(&delq->uq_mutex); 588 if (delq_info->delq_unreclaimed_files > 0) { 589 mutex_exit(&delq->uq_mutex); 590 (void) ufs_delete_drain(ufsvfsp->vfs_vfs, 0, dolockfs); 591 ufs_sync_with_thread(uq); 592 } else { 593 ASSERT(delq_info->delq_unreclaimed_files == 0); 594 mutex_exit(&delq->uq_mutex); 595 return; 596 } 597 598 /* 599 * Commit any outstanding transactions to make sure 600 * any canceled freed blocks are available for allocation. 601 */ 602 curthread->t_flag |= T_DONTBLOCK; 603 TRANS_BEGIN_SYNC(ufsvfsp, TOP_COMMIT_UPDATE, TOP_COMMIT_SIZE, error); 604 if (!error) { 605 TRANS_END_SYNC(ufsvfsp, error, TOP_COMMIT_UPDATE, 606 TOP_COMMIT_SIZE); 607 } 608 curthread->t_flag &= ~T_DONTBLOCK; 609 } 610 611 /* 612 * Adjust the resource usage in a struct statvfs based on 613 * what's in the delete queue. 614 * 615 * We do not consider the impact of ACLs or extended attributes 616 * that may be deleted as a side-effect of deleting a file. 617 * Those are metadata, and their sizes aren't reflected in the 618 * sizes returned by stat(), so this is not a problem. 619 */ 620 void 621 ufs_delete_adjust_stats(struct ufsvfs *ufsvfsp, struct statvfs64 *sp) 622 { 623 struct ufs_q *uq = &ufsvfsp->vfs_delete; 624 struct ufs_delq_info *delq_info = &ufsvfsp->vfs_delete_info; 625 626 mutex_enter(&uq->uq_mutex); 627 /* 628 * The blocks accounted for in the delete queue info are 629 * counted in DEV_BSIZE chunks, but ufs_statvfs counts in 630 * filesystem fragments, so a conversion is required here. 631 */ 632 sp->f_bfree += dbtofsb(ufsvfsp->vfs_fs, 633 delq_info->delq_unreclaimed_blocks); 634 sp->f_ffree += delq_info->delq_unreclaimed_files; 635 mutex_exit(&uq->uq_mutex); 636 } 637 638 /* 639 * IDLE INODE 640 * The following routines implement the protocol for maintaining an 641 * LRU list of idle inodes and for moving the idle inodes to the 642 * reuse list when the number of allocated inodes exceeds the user 643 * tunable high-water mark (ufs_ninode). 644 */ 645 646 /* 647 * clean an idle inode and move it to the reuse list 648 */ 649 static void 650 ufs_idle_free(struct inode *ip) 651 { 652 int pages; 653 int hno; 654 kmutex_t *ihm; 655 struct ufsvfs *ufsvfsp = ip->i_ufsvfs; 656 struct vnode *vp = ITOV(ip); 657 658 /* 659 * inode is held 660 */ 661 662 /* 663 * remember `pages' for stats below 664 */ 665 pages = (ip->i_mode && vn_has_cached_data(vp) && vp->v_type != VCHR); 666 667 /* 668 * start the dirty pages to disk and then invalidate them 669 * unless the inode is invalid (ISTALE) 670 */ 671 if ((ip->i_flag & ISTALE) == 0) { 672 (void) TRANS_SYNCIP(ip, B_ASYNC, I_ASYNC, TOP_SYNCIP_FREE); 673 (void) TRANS_SYNCIP(ip, 674 (TRANS_ISERROR(ufsvfsp)) ? B_INVAL | B_FORCE : B_INVAL, 675 I_ASYNC, TOP_SYNCIP_FREE); 676 } 677 678 /* 679 * wait for any current ufs_iget to finish and block future ufs_igets 680 */ 681 ASSERT(ip->i_number != 0); 682 hno = INOHASH(ip->i_number); 683 ihm = &ih_lock[hno]; 684 mutex_enter(ihm); 685 686 /* 687 * It must be guaranteed that v_count >= 2, otherwise 688 * something must be wrong with this vnode already. 689 * That is why we use v_count-- instead of VN_RELE(). 690 * Acquire the vnode lock in case another thread is in 691 * VN_RELE(). 692 */ 693 mutex_enter(&vp->v_lock); 694 695 if (vp->v_count < 2) 696 cmn_err(CE_PANIC, 697 "ufs_idle_free: vnode ref count is less than 2"); 698 699 vp->v_count--; 700 if ((vp->v_type != VCHR && vn_has_cached_data(vp)) || 701 vp->v_count != 1 || 702 ip->i_flag & (IMOD|IMODACC|IACC|ICHG|IUPD|IATTCHG)) { 703 /* 704 * Another thread has referenced this inode while 705 * we are trying to free it. Call VN_RELE() to 706 * release our reference. 707 */ 708 mutex_exit(&vp->v_lock); 709 mutex_exit(ihm); 710 VN_RELE(vp); 711 } else { 712 /* 713 * The inode is currently unreferenced and can not 714 * acquire further references because it has no pages 715 * and the hash is locked. Inodes acquire references 716 * via the hash list or via their pages. 717 */ 718 719 mutex_exit(&vp->v_lock); 720 721 /* 722 * remove it from the cache 723 */ 724 remque(ip); 725 mutex_exit(ihm); 726 /* 727 * Stale inodes have no valid ufsvfs 728 */ 729 if ((ip->i_flag & ISTALE) == 0 && ip->i_dquot) { 730 TRANS_DQRELE(ufsvfsp, ip->i_dquot); 731 ip->i_dquot = NULL; 732 } 733 ufs_si_del(ip); 734 if (pages) { 735 CPU_STATS_ADDQ(CPU, sys, ufsipage, 1); 736 } else { 737 CPU_STATS_ADDQ(CPU, sys, ufsinopage, 1); 738 } 739 ASSERT((vp->v_type == VCHR) || !vn_has_cached_data(vp)); 740 741 /* 742 * We had better not have a vnode reference count > 1 743 * at this point, if we do then something is broken as 744 * this inode/vnode acquired a reference underneath of us. 745 */ 746 ASSERT(vp->v_count == 1); 747 748 ufs_free_inode(ip); 749 } 750 } 751 752 /* 753 * this thread processes the global idle queue 754 */ 755 iqhead_t *ufs_junk_iq; 756 iqhead_t *ufs_useful_iq; 757 int ufs_njunk_iq = 0; 758 int ufs_nuseful_iq = 0; 759 int ufs_niqhash; 760 int ufs_iqhashmask; 761 struct ufs_q ufs_idle_q; 762 763 void 764 ufs_thread_idle(void) 765 { 766 callb_cpr_t cprinfo; 767 int i; 768 int ne; 769 770 ufs_niqhash = (ufs_idle_q.uq_lowat >> 1) / IQHASHQLEN; 771 ufs_niqhash = 1 << highbit(ufs_niqhash); /* round up to power of 2 */ 772 ufs_iqhashmask = ufs_niqhash - 1; 773 ufs_junk_iq = kmem_alloc(ufs_niqhash * sizeof (*ufs_junk_iq), 774 KM_SLEEP); 775 ufs_useful_iq = kmem_alloc(ufs_niqhash * sizeof (*ufs_useful_iq), 776 KM_SLEEP); 777 778 /* Initialize hash queue headers */ 779 for (i = 0; i < ufs_niqhash; i++) { 780 ufs_junk_iq[i].i_freef = (inode_t *)&ufs_junk_iq[i]; 781 ufs_junk_iq[i].i_freeb = (inode_t *)&ufs_junk_iq[i]; 782 ufs_useful_iq[i].i_freef = (inode_t *)&ufs_useful_iq[i]; 783 ufs_useful_iq[i].i_freeb = (inode_t *)&ufs_useful_iq[i]; 784 } 785 786 CALLB_CPR_INIT(&cprinfo, &ufs_idle_q.uq_mutex, callb_generic_cpr, 787 "ufsidle"); 788 again: 789 /* 790 * Whenever the idle thread is awakened, it repeatedly gives 791 * back half of the idle queue until the idle queue falls 792 * below lowat. 793 */ 794 mutex_enter(&ufs_idle_q.uq_mutex); 795 if (ufs_idle_q.uq_ne < ufs_idle_q.uq_lowat) { 796 CALLB_CPR_SAFE_BEGIN(&cprinfo); 797 cv_wait(&ufs_idle_q.uq_cv, &ufs_idle_q.uq_mutex); 798 CALLB_CPR_SAFE_END(&cprinfo, &ufs_idle_q.uq_mutex); 799 } 800 mutex_exit(&ufs_idle_q.uq_mutex); 801 802 /* 803 * Give back 1/2 of the idle queue 804 */ 805 ne = ufs_idle_q.uq_ne >> 1; 806 ins.in_tidles.value.ul += ne; 807 ufs_idle_some(ne); 808 goto again; 809 } 810 811 /* 812 * Reclaim callback for ufs inode cache. 813 * Invoked by the kernel memory allocator when memory gets tight. 814 */ 815 /*ARGSUSED*/ 816 void 817 ufs_inode_cache_reclaim(void *cdrarg) 818 { 819 /* 820 * If we are low on memory and the idle queue is over its 821 * halfway mark, then free 50% of the idle q 822 * 823 * We don't free all of the idle inodes because the inodes 824 * for popular NFS files may have been kicked from the dnlc. 825 * The inodes for these files will end up on the idle queue 826 * after every NFS access. 827 * 828 * If we repeatedly push them from the idle queue then 829 * NFS users may be unhappy as an extra buf cache operation 830 * is incurred for every NFS operation to these files. 831 * 832 * It's not common, but I have seen it happen. 833 * 834 */ 835 if (ufs_idle_q.uq_ne < (ufs_idle_q.uq_lowat >> 1)) 836 return; 837 mutex_enter(&ufs_idle_q.uq_mutex); 838 cv_broadcast(&ufs_idle_q.uq_cv); 839 mutex_exit(&ufs_idle_q.uq_mutex); 840 } 841 842 /* 843 * Free up some idle inodes 844 */ 845 void 846 ufs_idle_some(int ne) 847 { 848 int i; 849 struct inode *ip; 850 struct vnode *vp; 851 static int junk_rotor = 0; 852 static int useful_rotor = 0; 853 854 for (i = 0; i < ne; ++i) { 855 mutex_enter(&ufs_idle_q.uq_mutex); 856 857 if (ufs_njunk_iq) { 858 while (ufs_junk_iq[junk_rotor].i_freef == 859 (inode_t *)&ufs_junk_iq[junk_rotor]) { 860 junk_rotor = IQNEXT(junk_rotor); 861 } 862 ip = ufs_junk_iq[junk_rotor].i_freef; 863 ASSERT(ip->i_flag & IJUNKIQ); 864 } else if (ufs_nuseful_iq) { 865 while (ufs_useful_iq[useful_rotor].i_freef == 866 (inode_t *)&ufs_useful_iq[useful_rotor]) { 867 useful_rotor = IQNEXT(useful_rotor); 868 } 869 ip = ufs_useful_iq[useful_rotor].i_freef; 870 ASSERT(!(ip->i_flag & IJUNKIQ)); 871 } else { 872 mutex_exit(&ufs_idle_q.uq_mutex); 873 return; 874 } 875 876 /* 877 * emulate ufs_iget 878 */ 879 vp = ITOV(ip); 880 VN_HOLD(vp); 881 mutex_exit(&ufs_idle_q.uq_mutex); 882 rw_enter(&ip->i_contents, RW_WRITER); 883 /* 884 * VN_RELE should not be called if 885 * ufs_rmidle returns true, as it will 886 * effectively be done in ufs_idle_free. 887 */ 888 if (ufs_rmidle(ip)) { 889 rw_exit(&ip->i_contents); 890 ufs_idle_free(ip); 891 } else { 892 rw_exit(&ip->i_contents); 893 VN_RELE(vp); 894 } 895 } 896 } 897 898 /* 899 * drain entries for vfsp from the idle queue 900 * vfsp == NULL means drain the entire thing 901 */ 902 void 903 ufs_idle_drain(struct vfs *vfsp) 904 { 905 struct inode *ip, *nip; 906 struct inode *ianchor = NULL; 907 int i; 908 909 mutex_enter(&ufs_idle_q.uq_mutex); 910 if (ufs_njunk_iq) { 911 /* for each hash q */ 912 for (i = 0; i < ufs_niqhash; i++) { 913 /* search down the hash q */ 914 for (ip = ufs_junk_iq[i].i_freef; 915 ip != (inode_t *)&ufs_junk_iq[i]; 916 ip = ip->i_freef) { 917 if (ip->i_vfs == vfsp || vfsp == NULL) { 918 /* found a matching entry */ 919 VN_HOLD(ITOV(ip)); 920 mutex_exit(&ufs_idle_q.uq_mutex); 921 rw_enter(&ip->i_contents, RW_WRITER); 922 /* 923 * See comments in ufs_idle_some() 924 * as we will call ufs_idle_free() 925 * after scanning both queues. 926 */ 927 if (ufs_rmidle(ip)) { 928 rw_exit(&ip->i_contents); 929 ip->i_freef = ianchor; 930 ianchor = ip; 931 } else { 932 rw_exit(&ip->i_contents); 933 VN_RELE(ITOV(ip)); 934 } 935 /* restart this hash q */ 936 ip = (inode_t *)&ufs_junk_iq[i]; 937 mutex_enter(&ufs_idle_q.uq_mutex); 938 } 939 } 940 } 941 } 942 if (ufs_nuseful_iq) { 943 /* for each hash q */ 944 for (i = 0; i < ufs_niqhash; i++) { 945 /* search down the hash q */ 946 for (ip = ufs_useful_iq[i].i_freef; 947 ip != (inode_t *)&ufs_useful_iq[i]; 948 ip = ip->i_freef) { 949 if (ip->i_vfs == vfsp || vfsp == NULL) { 950 /* found a matching entry */ 951 VN_HOLD(ITOV(ip)); 952 mutex_exit(&ufs_idle_q.uq_mutex); 953 rw_enter(&ip->i_contents, RW_WRITER); 954 /* 955 * See comments in ufs_idle_some() 956 * as we will call ufs_idle_free() 957 * after scanning both queues. 958 */ 959 if (ufs_rmidle(ip)) { 960 rw_exit(&ip->i_contents); 961 ip->i_freef = ianchor; 962 ianchor = ip; 963 } else { 964 rw_exit(&ip->i_contents); 965 VN_RELE(ITOV(ip)); 966 } 967 /* restart this hash q */ 968 ip = (inode_t *)&ufs_useful_iq[i]; 969 mutex_enter(&ufs_idle_q.uq_mutex); 970 } 971 } 972 } 973 } 974 975 mutex_exit(&ufs_idle_q.uq_mutex); 976 /* no more matching entries, release those we have found (if any) */ 977 for (ip = ianchor; ip; ip = nip) { 978 nip = ip->i_freef; 979 ip->i_freef = ip; 980 ufs_idle_free(ip); 981 } 982 } 983 984 /* 985 * RECLAIM DELETED INODES 986 * The following thread scans the file system once looking for deleted files 987 */ 988 void 989 ufs_thread_reclaim(struct vfs *vfsp) 990 { 991 struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data; 992 struct ufs_q *uq = &ufsvfsp->vfs_reclaim; 993 struct fs *fs = ufsvfsp->vfs_fs; 994 struct buf *bp = 0; 995 int err = 0; 996 daddr_t bno; 997 ino_t ino; 998 struct dinode *dp; 999 struct inode *ip; 1000 callb_cpr_t cprinfo; 1001 1002 CALLB_CPR_INIT(&cprinfo, &uq->uq_mutex, callb_generic_cpr, 1003 "ufsreclaim"); 1004 1005 /* 1006 * mount decided that we don't need a reclaim thread 1007 */ 1008 if ((fs->fs_reclaim & FS_RECLAIMING) == 0) 1009 err++; 1010 1011 /* 1012 * don't reclaim if readonly 1013 */ 1014 if (fs->fs_ronly) 1015 err++; 1016 1017 for (ino = 0; ino < (fs->fs_ncg * fs->fs_ipg) && !err; ++ino) { 1018 1019 /* 1020 * Check whether we are the target of another 1021 * thread having called ufs_thread_exit() or 1022 * ufs_thread_suspend(). 1023 */ 1024 mutex_enter(&uq->uq_mutex); 1025 again: 1026 if (uq->uq_flags & UQ_EXIT) { 1027 err++; 1028 mutex_exit(&uq->uq_mutex); 1029 break; 1030 } else if (uq->uq_flags & UQ_SUSPEND) { 1031 uq->uq_flags |= UQ_SUSPENDED; 1032 /* 1033 * Release the buf before we cv_wait() 1034 * otherwise we may deadlock with the 1035 * thread that called ufs_thread_suspend(). 1036 */ 1037 if (bp) { 1038 brelse(bp); 1039 bp = 0; 1040 } 1041 if (uq->uq_flags & UQ_WAIT) { 1042 uq->uq_flags &= ~UQ_WAIT; 1043 cv_broadcast(&uq->uq_cv); 1044 } 1045 CALLB_CPR_SAFE_BEGIN(&cprinfo); 1046 cv_wait(&uq->uq_cv, &uq->uq_mutex); 1047 CALLB_CPR_SAFE_END(&cprinfo, &uq->uq_mutex); 1048 goto again; 1049 } 1050 mutex_exit(&uq->uq_mutex); 1051 1052 /* 1053 * if we don't already have the buf; get it 1054 */ 1055 bno = fsbtodb(fs, itod(fs, ino)); 1056 if ((bp == 0) || (bp->b_blkno != bno)) { 1057 if (bp) 1058 brelse(bp); 1059 bp = UFS_BREAD(ufsvfsp, 1060 ufsvfsp->vfs_dev, bno, fs->fs_bsize); 1061 bp->b_flags |= B_AGE; 1062 } 1063 if (bp->b_flags & B_ERROR) { 1064 err++; 1065 continue; 1066 } 1067 /* 1068 * nlink <= 0 and mode != 0 means deleted 1069 */ 1070 dp = (struct dinode *)bp->b_un.b_addr + itoo(fs, ino); 1071 if ((dp->di_nlink <= 0) && (dp->di_mode != 0)) { 1072 /* 1073 * can't hold the buf (deadlock) 1074 */ 1075 brelse(bp); 1076 bp = 0; 1077 rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER); 1078 /* 1079 * iget/iput sequence will put inode on ifree 1080 * thread queue if it is idle. This is a nop 1081 * for busy (open, deleted) inodes 1082 */ 1083 if (ufs_iget(vfsp, ino, &ip, CRED())) 1084 err++; 1085 else 1086 VN_RELE(ITOV(ip)); 1087 rw_exit(&ufsvfsp->vfs_dqrwlock); 1088 } 1089 } 1090 1091 if (bp) 1092 brelse(bp); 1093 if (!err) { 1094 /* 1095 * reset the reclaiming-bit 1096 */ 1097 mutex_enter(&ufsvfsp->vfs_lock); 1098 fs->fs_reclaim &= ~FS_RECLAIMING; 1099 mutex_exit(&ufsvfsp->vfs_lock); 1100 TRANS_SBWRITE(ufsvfsp, TOP_SBWRITE_RECLAIM); 1101 } 1102 1103 /* 1104 * exit the reclaim thread 1105 */ 1106 mutex_enter(&uq->uq_mutex); 1107 uq->uq_threadp = NULL; 1108 uq->uq_flags &= ~UQ_WAIT; 1109 cv_broadcast(&uq->uq_cv); 1110 CALLB_CPR_EXIT(&cprinfo); 1111 thread_exit(); 1112 } 1113 /* 1114 * HLOCK FILE SYSTEM 1115 * hlock the file system's whose logs have device errors 1116 */ 1117 struct ufs_q ufs_hlock; 1118 /*ARGSUSED*/ 1119 void 1120 ufs_thread_hlock(void *ignore) 1121 { 1122 int retry; 1123 callb_cpr_t cprinfo; 1124 1125 CALLB_CPR_INIT(&cprinfo, &ufs_hlock.uq_mutex, callb_generic_cpr, 1126 "ufshlock"); 1127 1128 for (;;) { 1129 /* 1130 * sleep until there is work to do 1131 */ 1132 mutex_enter(&ufs_hlock.uq_mutex); 1133 (void) ufs_thread_run(&ufs_hlock, &cprinfo); 1134 ufs_hlock.uq_ne = 0; 1135 mutex_exit(&ufs_hlock.uq_mutex); 1136 /* 1137 * hlock the error'ed fs's 1138 * retry after a bit if another app is doing lockfs stuff 1139 */ 1140 do { 1141 retry = ufs_trans_hlock(); 1142 if (retry) { 1143 mutex_enter(&ufs_hlock.uq_mutex); 1144 CALLB_CPR_SAFE_BEGIN(&cprinfo); 1145 (void) cv_timedwait(&ufs_hlock.uq_cv, 1146 &ufs_hlock.uq_mutex, lbolt + hz); 1147 CALLB_CPR_SAFE_END(&cprinfo, 1148 &ufs_hlock.uq_mutex); 1149 mutex_exit(&ufs_hlock.uq_mutex); 1150 } 1151 } while (retry); 1152 } 1153 } 1154 1155 static void 1156 ufs_attr_purge(struct inode *dp) 1157 { 1158 int err; 1159 int error; 1160 off_t dirsize; /* size of the directory */ 1161 off_t offset; /* offset in the directory */ 1162 int entryoffsetinblk; /* offset of ep in fbp's buffer */ 1163 struct inode *tp; 1164 struct fbuf *fbp; /* pointer to directory block */ 1165 struct direct *ep; /* directory entry */ 1166 int trans_size; 1167 int issync; 1168 struct ufsvfs *ufsvfsp = dp->i_ufsvfs; 1169 1170 rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER); 1171 1172 fbp = NULL; 1173 dirsize = roundup(dp->i_size, DIRBLKSIZ); 1174 offset = 0; 1175 entryoffsetinblk = 0; 1176 1177 /* 1178 * Purge directory cache 1179 */ 1180 1181 dnlc_dir_purge(&dp->i_danchor); 1182 1183 while (offset < dirsize) { 1184 /* 1185 * If offset is on a block boundary, 1186 * read the next directory block. 1187 * Release previous if it exists. 1188 */ 1189 if (blkoff(dp->i_fs, offset) == 0) { 1190 if (fbp != NULL) { 1191 fbrelse(fbp, S_OTHER); 1192 } 1193 1194 err = blkatoff(dp, offset, (char **)0, &fbp); 1195 if (err) { 1196 goto out; 1197 } 1198 entryoffsetinblk = 0; 1199 } 1200 ep = (struct direct *)(fbp->fb_addr + entryoffsetinblk); 1201 if (ep->d_ino == 0 || (ep->d_name[0] == '.' && 1202 ep->d_name[1] == '\0') || 1203 (ep->d_name[0] == '.' && ep->d_name[1] == '.' && 1204 ep->d_name[2] == '\0')) { 1205 1206 entryoffsetinblk += ep->d_reclen; 1207 1208 } else { 1209 1210 if ((err = ufs_iget(dp->i_vfs, ep->d_ino, 1211 &tp, CRED())) != 0) { 1212 goto out; 1213 } 1214 1215 TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_REMOVE, 1216 trans_size = (int)TOP_REMOVE_SIZE(tp)); 1217 1218 /* 1219 * Delete inode. 1220 */ 1221 1222 dnlc_remove(ITOV(dp), ep->d_name); 1223 1224 rw_enter(&tp->i_contents, RW_WRITER); 1225 tp->i_flag |= ICHG; 1226 tp->i_seq++; 1227 TRANS_INODE(tp->i_ufsvfs, tp); 1228 tp->i_nlink--; 1229 ufs_setreclaim(tp); 1230 ITIMES_NOLOCK(tp); 1231 rw_exit(&tp->i_contents); 1232 1233 VN_RELE(ITOV(tp)); 1234 entryoffsetinblk += ep->d_reclen; 1235 TRANS_END_CSYNC(ufsvfsp, error, 1236 issync, TOP_REMOVE, trans_size); 1237 1238 } 1239 offset += ep->d_reclen; 1240 } 1241 1242 if (fbp) { 1243 fbrelse(fbp, S_OTHER); 1244 } 1245 1246 out: 1247 rw_exit(&ufsvfsp->vfs_dqrwlock); 1248 } 1249