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