ufs_dirhash.c revision 194387
1/*- 2 * Copyright (c) 2001, 2002 Ian Dowse. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26/* 27 * This implements a hash-based lookup scheme for UFS directories. 28 */ 29 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_dirhash.c 194387 2009-06-17 18:55:29Z snb $"); 32 33#include "opt_ufs.h" 34 35#ifdef UFS_DIRHASH 36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/kernel.h> 40#include <sys/lock.h> 41#include <sys/mutex.h> 42#include <sys/malloc.h> 43#include <sys/fnv_hash.h> 44#include <sys/proc.h> 45#include <sys/bio.h> 46#include <sys/buf.h> 47#include <sys/vnode.h> 48#include <sys/mount.h> 49#include <sys/refcount.h> 50#include <sys/sysctl.h> 51#include <sys/sx.h> 52#include <sys/eventhandler.h> 53#include <sys/time.h> 54#include <vm/uma.h> 55 56#include <ufs/ufs/quota.h> 57#include <ufs/ufs/inode.h> 58#include <ufs/ufs/dir.h> 59#include <ufs/ufs/dirhash.h> 60#include <ufs/ufs/extattr.h> 61#include <ufs/ufs/ufsmount.h> 62#include <ufs/ufs/ufs_extern.h> 63 64#define WRAPINCR(val, limit) (((val) + 1 == (limit)) ? 0 : ((val) + 1)) 65#define WRAPDECR(val, limit) (((val) == 0) ? ((limit) - 1) : ((val) - 1)) 66#define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0) 67#define BLKFREE2IDX(n) ((n) > DH_NFSTATS ? DH_NFSTATS : (n)) 68 69static MALLOC_DEFINE(M_DIRHASH, "ufs_dirhash", "UFS directory hash tables"); 70 71static SYSCTL_NODE(_vfs, OID_AUTO, ufs, CTLFLAG_RD, 0, "UFS filesystem"); 72 73static int ufs_mindirhashsize = DIRBLKSIZ * 5; 74SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_minsize, CTLFLAG_RW, 75 &ufs_mindirhashsize, 76 0, "minimum directory size in bytes for which to use hashed lookup"); 77static int ufs_dirhashmaxmem = 2 * 1024 * 1024; 78SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_maxmem, CTLFLAG_RW, &ufs_dirhashmaxmem, 79 0, "maximum allowed dirhash memory usage"); 80static int ufs_dirhashmem; 81SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_mem, CTLFLAG_RD, &ufs_dirhashmem, 82 0, "current dirhash memory usage"); 83static int ufs_dirhashcheck = 0; 84SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_docheck, CTLFLAG_RW, &ufs_dirhashcheck, 85 0, "enable extra sanity tests"); 86static int ufs_dirhashlowmemcount = 0; 87SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_lowmemcount, CTLFLAG_RD, 88 &ufs_dirhashlowmemcount, 0, "number of times low memory hook called"); 89static int ufs_dirhashreclaimage = 5; 90SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_reclaimage, CTLFLAG_RW, 91 &ufs_dirhashreclaimage, 0, 92 "max time in seconds of hash inactivity before deletion in low VM events"); 93 94 95static int ufsdirhash_hash(struct dirhash *dh, char *name, int namelen); 96static void ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff); 97static void ufsdirhash_delslot(struct dirhash *dh, int slot); 98static int ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, 99 doff_t offset); 100static doff_t ufsdirhash_getprev(struct direct *dp, doff_t offset); 101static int ufsdirhash_recycle(int wanted); 102static void ufsdirhash_lowmem(void); 103static void ufsdirhash_free_locked(struct inode *ip); 104 105static uma_zone_t ufsdirhash_zone; 106 107#define DIRHASHLIST_LOCK() mtx_lock(&ufsdirhash_mtx) 108#define DIRHASHLIST_UNLOCK() mtx_unlock(&ufsdirhash_mtx) 109#define DIRHASH_BLKALLOC_WAITOK() uma_zalloc(ufsdirhash_zone, M_WAITOK) 110#define DIRHASH_BLKFREE(ptr) uma_zfree(ufsdirhash_zone, (ptr)) 111#define DIRHASH_ASSERT_LOCKED(dh) \ 112 sx_assert(&(dh)->dh_lock, SA_LOCKED) 113 114/* Dirhash list; recently-used entries are near the tail. */ 115static TAILQ_HEAD(, dirhash) ufsdirhash_list; 116 117/* Protects: ufsdirhash_list, `dh_list' field, ufs_dirhashmem. */ 118static struct mtx ufsdirhash_mtx; 119 120/* 121 * Locking: 122 * 123 * The relationship between inode and dirhash is protected either by an 124 * exclusive vnode lock or the vnode interlock where a shared vnode lock 125 * may be used. The dirhash_mtx is acquired after the dirhash lock. To 126 * handle teardown races, code wishing to lock the dirhash for an inode 127 * when using a shared vnode lock must obtain a private reference on the 128 * dirhash while holding the vnode interlock. They can drop it once they 129 * have obtained the dirhash lock and verified that the dirhash wasn't 130 * recycled while they waited for the dirhash lock. 131 * 132 * ufsdirhash_build() acquires a shared lock on the dirhash when it is 133 * successful. This lock is released after a call to ufsdirhash_lookup(). 134 * 135 * Functions requiring exclusive access use ufsdirhash_acquire() which may 136 * free a dirhash structure that was recycled by ufsdirhash_recycle(). 137 * 138 * The dirhash lock may be held across io operations. 139 * 140 * WITNESS reports a lock order reversal between the "bufwait" lock 141 * and the "dirhash" lock. However, this specific reversal will not 142 * cause a deadlock. To get a deadlock, one would have to lock a 143 * buffer followed by the dirhash while a second thread locked a 144 * buffer while holding the dirhash lock. The second order can happen 145 * under a shared or exclusive vnode lock for the associated directory 146 * in lookup(). The first order, however, can only happen under an 147 * exclusive vnode lock (e.g. unlink(), rename(), etc.). Thus, for 148 * a thread to be doing a "bufwait" -> "dirhash" order, it has to hold 149 * an exclusive vnode lock. That exclusive vnode lock will prevent 150 * any other threads from doing a "dirhash" -> "bufwait" order. 151 */ 152 153static void 154ufsdirhash_hold(struct dirhash *dh) 155{ 156 157 refcount_acquire(&dh->dh_refcount); 158} 159 160static void 161ufsdirhash_drop(struct dirhash *dh) 162{ 163 164 if (refcount_release(&dh->dh_refcount)) { 165 sx_destroy(&dh->dh_lock); 166 free(dh, M_DIRHASH); 167 } 168} 169 170/* 171 * Release the lock on a dirhash. 172 */ 173static void 174ufsdirhash_release(struct dirhash *dh) 175{ 176 177 sx_unlock(&dh->dh_lock); 178} 179 180/* 181 * Either acquire an existing hash locked shared or create a new hash and 182 * return it exclusively locked. May return NULL if the allocation fails. 183 * 184 * The vnode interlock is used to protect the i_dirhash pointer from 185 * simultaneous access while only a shared vnode lock is held. 186 */ 187static struct dirhash * 188ufsdirhash_create(struct inode *ip) 189{ 190 struct dirhash *ndh; 191 struct dirhash *dh; 192 struct vnode *vp; 193 int error; 194 195 error = 0; 196 ndh = dh = NULL; 197 vp = ip->i_vnode; 198 for (;;) { 199 /* Racy check for i_dirhash to prefetch a dirhash structure. */ 200 if (ip->i_dirhash == NULL && ndh == NULL) { 201 ndh = malloc(sizeof *dh, M_DIRHASH, 202 M_NOWAIT | M_ZERO); 203 if (ndh == NULL) 204 return (NULL); 205 refcount_init(&ndh->dh_refcount, 1); 206 207 /* 208 * The DUPOK is to prevent warnings from the 209 * sx_slock() a few lines down which is safe 210 * since the duplicate lock in that case is 211 * the one for this dirhash we are creating 212 * now which has no external references until 213 * after this function returns. 214 */ 215 sx_init_flags(&ndh->dh_lock, "dirhash", SX_DUPOK); 216 sx_xlock(&ndh->dh_lock); 217 } 218 /* 219 * Check i_dirhash. If it's NULL just try to use a 220 * preallocated structure. If none exists loop and try again. 221 */ 222 VI_LOCK(vp); 223 dh = ip->i_dirhash; 224 if (dh == NULL) { 225 ip->i_dirhash = ndh; 226 VI_UNLOCK(vp); 227 if (ndh == NULL) 228 continue; 229 return (ndh); 230 } 231 ufsdirhash_hold(dh); 232 VI_UNLOCK(vp); 233 234 /* Acquire a shared lock on existing hashes. */ 235 sx_slock(&dh->dh_lock); 236 237 /* The hash could've been recycled while we were waiting. */ 238 VI_LOCK(vp); 239 if (ip->i_dirhash != dh) { 240 VI_UNLOCK(vp); 241 ufsdirhash_release(dh); 242 ufsdirhash_drop(dh); 243 continue; 244 } 245 VI_UNLOCK(vp); 246 ufsdirhash_drop(dh); 247 248 /* If the hash is still valid we've succeeded. */ 249 if (dh->dh_hash != NULL) 250 break; 251 /* 252 * If the hash is NULL it has been recycled. Try to upgrade 253 * so we can recreate it. If we fail the upgrade, drop our 254 * lock and try again. 255 */ 256 if (sx_try_upgrade(&dh->dh_lock)) 257 break; 258 sx_sunlock(&dh->dh_lock); 259 } 260 /* Free the preallocated structure if it was not necessary. */ 261 if (ndh) { 262 ufsdirhash_release(ndh); 263 ufsdirhash_drop(ndh); 264 } 265 return (dh); 266} 267 268/* 269 * Acquire an exclusive lock on an existing hash. Requires an exclusive 270 * vnode lock to protect the i_dirhash pointer. hashes that have been 271 * recycled are reclaimed here and NULL is returned. 272 */ 273static struct dirhash * 274ufsdirhash_acquire(struct inode *ip) 275{ 276 struct dirhash *dh; 277 struct vnode *vp; 278 279 ASSERT_VOP_ELOCKED(ip->i_vnode, __FUNCTION__); 280 281 vp = ip->i_vnode; 282 dh = ip->i_dirhash; 283 if (dh == NULL) 284 return (NULL); 285 sx_xlock(&dh->dh_lock); 286 if (dh->dh_hash != NULL) 287 return (dh); 288 ufsdirhash_free_locked(ip); 289 return (NULL); 290} 291 292/* 293 * Acquire exclusively and free the hash pointed to by ip. Works with a 294 * shared or exclusive vnode lock. 295 */ 296void 297ufsdirhash_free(struct inode *ip) 298{ 299 struct dirhash *dh; 300 struct vnode *vp; 301 302 vp = ip->i_vnode; 303 for (;;) { 304 /* Grab a reference on this inode's dirhash if it has one. */ 305 VI_LOCK(vp); 306 dh = ip->i_dirhash; 307 if (dh == NULL) { 308 VI_UNLOCK(vp); 309 return; 310 } 311 ufsdirhash_hold(dh); 312 VI_UNLOCK(vp); 313 314 /* Exclusively lock the dirhash. */ 315 sx_xlock(&dh->dh_lock); 316 317 /* If this dirhash still belongs to this inode, then free it. */ 318 VI_LOCK(vp); 319 if (ip->i_dirhash == dh) { 320 VI_UNLOCK(vp); 321 ufsdirhash_drop(dh); 322 break; 323 } 324 VI_UNLOCK(vp); 325 326 /* 327 * This inode's dirhash has changed while we were 328 * waiting for the dirhash lock, so try again. 329 */ 330 ufsdirhash_release(dh); 331 ufsdirhash_drop(dh); 332 } 333 ufsdirhash_free_locked(ip); 334} 335 336/* 337 * Attempt to build up a hash table for the directory contents in 338 * inode 'ip'. Returns 0 on success, or -1 of the operation failed. 339 */ 340int 341ufsdirhash_build(struct inode *ip) 342{ 343 struct dirhash *dh; 344 struct buf *bp = NULL; 345 struct direct *ep; 346 struct vnode *vp; 347 doff_t bmask, pos; 348 int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot; 349 350 /* Take care of a decreased sysctl value. */ 351 while (ufs_dirhashmem > ufs_dirhashmaxmem) 352 if (ufsdirhash_recycle(0) != 0) 353 return (-1); 354 355 /* Check if we can/should use dirhash. */ 356 if (ip->i_size < ufs_mindirhashsize || OFSFMT(ip->i_vnode) || 357 ip->i_effnlink == 0) { 358 if (ip->i_dirhash) 359 ufsdirhash_free(ip); 360 return (-1); 361 } 362 dh = ufsdirhash_create(ip); 363 if (dh == NULL) 364 return (-1); 365 if (dh->dh_hash != NULL) 366 return (0); 367 368 vp = ip->i_vnode; 369 /* Allocate 50% more entries than this dir size could ever need. */ 370 KASSERT(ip->i_size >= DIRBLKSIZ, ("ufsdirhash_build size")); 371 nslots = ip->i_size / DIRECTSIZ(1); 372 nslots = (nslots * 3 + 1) / 2; 373 narrays = howmany(nslots, DH_NBLKOFF); 374 nslots = narrays * DH_NBLKOFF; 375 dirblocks = howmany(ip->i_size, DIRBLKSIZ); 376 nblocks = (dirblocks * 3 + 1) / 2; 377 memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) + 378 narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 379 nblocks * sizeof(*dh->dh_blkfree); 380 DIRHASHLIST_LOCK(); 381 if (memreqd + ufs_dirhashmem > ufs_dirhashmaxmem) { 382 DIRHASHLIST_UNLOCK(); 383 if (memreqd > ufs_dirhashmaxmem / 2) 384 goto fail; 385 /* Try to free some space. */ 386 if (ufsdirhash_recycle(memreqd) != 0) 387 goto fail; 388 /* Enough was freed, and list has been locked. */ 389 } 390 ufs_dirhashmem += memreqd; 391 DIRHASHLIST_UNLOCK(); 392 393 /* Initialise the hash table and block statistics. */ 394 dh->dh_memreq = memreqd; 395 dh->dh_narrays = narrays; 396 dh->dh_hlen = nslots; 397 dh->dh_nblk = nblocks; 398 dh->dh_dirblks = dirblocks; 399 for (i = 0; i < DH_NFSTATS; i++) 400 dh->dh_firstfree[i] = -1; 401 dh->dh_firstfree[DH_NFSTATS] = 0; 402 dh->dh_hused = 0; 403 dh->dh_seqopt = 0; 404 dh->dh_seqoff = 0; 405 dh->dh_score = DH_SCOREINIT; 406 dh->dh_lastused = time_second; 407 408 /* 409 * Use non-blocking mallocs so that we will revert to a linear 410 * lookup on failure rather than potentially blocking forever. 411 */ 412 dh->dh_hash = malloc(narrays * sizeof(dh->dh_hash[0]), 413 M_DIRHASH, M_NOWAIT | M_ZERO); 414 if (dh->dh_hash == NULL) 415 goto fail; 416 dh->dh_blkfree = malloc(nblocks * sizeof(dh->dh_blkfree[0]), 417 M_DIRHASH, M_NOWAIT); 418 if (dh->dh_blkfree == NULL) 419 goto fail; 420 for (i = 0; i < narrays; i++) { 421 if ((dh->dh_hash[i] = DIRHASH_BLKALLOC_WAITOK()) == NULL) 422 goto fail; 423 for (j = 0; j < DH_NBLKOFF; j++) 424 dh->dh_hash[i][j] = DIRHASH_EMPTY; 425 } 426 for (i = 0; i < dirblocks; i++) 427 dh->dh_blkfree[i] = DIRBLKSIZ / DIRALIGN; 428 bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 429 pos = 0; 430 while (pos < ip->i_size) { 431 /* If necessary, get the next directory block. */ 432 if ((pos & bmask) == 0) { 433 if (bp != NULL) 434 brelse(bp); 435 if (UFS_BLKATOFF(vp, (off_t)pos, NULL, &bp) != 0) 436 goto fail; 437 } 438 439 /* Add this entry to the hash. */ 440 ep = (struct direct *)((char *)bp->b_data + (pos & bmask)); 441 if (ep->d_reclen == 0 || ep->d_reclen > 442 DIRBLKSIZ - (pos & (DIRBLKSIZ - 1))) { 443 /* Corrupted directory. */ 444 brelse(bp); 445 goto fail; 446 } 447 if (ep->d_ino != 0) { 448 /* Add the entry (simplified ufsdirhash_add). */ 449 slot = ufsdirhash_hash(dh, ep->d_name, ep->d_namlen); 450 while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 451 slot = WRAPINCR(slot, dh->dh_hlen); 452 dh->dh_hused++; 453 DH_ENTRY(dh, slot) = pos; 454 ufsdirhash_adjfree(dh, pos, -DIRSIZ(0, ep)); 455 } 456 pos += ep->d_reclen; 457 } 458 459 if (bp != NULL) 460 brelse(bp); 461 DIRHASHLIST_LOCK(); 462 TAILQ_INSERT_TAIL(&ufsdirhash_list, dh, dh_list); 463 dh->dh_onlist = 1; 464 DIRHASHLIST_UNLOCK(); 465 sx_downgrade(&dh->dh_lock); 466 return (0); 467 468fail: 469 ufsdirhash_free_locked(ip); 470 return (-1); 471} 472 473/* 474 * Free any hash table associated with inode 'ip'. 475 */ 476static void 477ufsdirhash_free_locked(struct inode *ip) 478{ 479 struct dirhash *dh; 480 struct vnode *vp; 481 int i; 482 483 DIRHASH_ASSERT_LOCKED(ip->i_dirhash); 484 485 /* 486 * Clear the pointer in the inode to prevent new threads from 487 * finding the dead structure. 488 */ 489 vp = ip->i_vnode; 490 VI_LOCK(vp); 491 dh = ip->i_dirhash; 492 ip->i_dirhash = NULL; 493 VI_UNLOCK(vp); 494 495 /* 496 * Remove the hash from the list since we are going to free its 497 * memory. 498 */ 499 DIRHASHLIST_LOCK(); 500 if (dh->dh_onlist) 501 TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 502 ufs_dirhashmem -= dh->dh_memreq; 503 DIRHASHLIST_UNLOCK(); 504 505 /* 506 * At this point, any waiters for the lock should hold their 507 * own reference on the dirhash structure. They will drop 508 * that reference once they grab the vnode interlock and see 509 * that ip->i_dirhash is NULL. 510 */ 511 sx_xunlock(&dh->dh_lock); 512 513 /* 514 * Handle partially recycled as well as fully constructed hashes. 515 */ 516 if (dh->dh_hash != NULL) { 517 for (i = 0; i < dh->dh_narrays; i++) 518 if (dh->dh_hash[i] != NULL) 519 DIRHASH_BLKFREE(dh->dh_hash[i]); 520 free(dh->dh_hash, M_DIRHASH); 521 if (dh->dh_blkfree != NULL) 522 free(dh->dh_blkfree, M_DIRHASH); 523 } 524 525 /* 526 * Drop the inode's reference to the data structure. 527 */ 528 ufsdirhash_drop(dh); 529} 530 531/* 532 * Find the offset of the specified name within the given inode. 533 * Returns 0 on success, ENOENT if the entry does not exist, or 534 * EJUSTRETURN if the caller should revert to a linear search. 535 * 536 * If successful, the directory offset is stored in *offp, and a 537 * pointer to a struct buf containing the entry is stored in *bpp. If 538 * prevoffp is non-NULL, the offset of the previous entry within 539 * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry 540 * is the first in a block, the start of the block is used). 541 * 542 * Must be called with the hash locked. Returns with the hash unlocked. 543 */ 544int 545ufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp, 546 struct buf **bpp, doff_t *prevoffp) 547{ 548 struct dirhash *dh, *dh_next; 549 struct direct *dp; 550 struct vnode *vp; 551 struct buf *bp; 552 doff_t blkoff, bmask, offset, prevoff; 553 int i, slot; 554 int error; 555 556 dh = ip->i_dirhash; 557 KASSERT(dh != NULL && dh->dh_hash != NULL, 558 ("ufsdirhash_lookup: Invalid dirhash %p\n", dh)); 559 DIRHASH_ASSERT_LOCKED(dh); 560 /* 561 * Move this dirhash towards the end of the list if it has a 562 * score higher than the next entry, and acquire the dh_lock. 563 */ 564 DIRHASHLIST_LOCK(); 565 if (TAILQ_NEXT(dh, dh_list) != NULL) { 566 /* 567 * If the new score will be greater than that of the next 568 * entry, then move this entry past it. With both mutexes 569 * held, dh_next won't go away, but its dh_score could 570 * change; that's not important since it is just a hint. 571 */ 572 if ((dh_next = TAILQ_NEXT(dh, dh_list)) != NULL && 573 dh->dh_score >= dh_next->dh_score) { 574 KASSERT(dh->dh_onlist, ("dirhash: not on list")); 575 TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 576 TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh, 577 dh_list); 578 } 579 } 580 /* Update the score. */ 581 if (dh->dh_score < DH_SCOREMAX) 582 dh->dh_score++; 583 584 /* Update last used time. */ 585 dh->dh_lastused = time_second; 586 DIRHASHLIST_UNLOCK(); 587 588 vp = ip->i_vnode; 589 bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 590 blkoff = -1; 591 bp = NULL; 592restart: 593 slot = ufsdirhash_hash(dh, name, namelen); 594 595 if (dh->dh_seqopt) { 596 /* 597 * Sequential access optimisation. dh_seqoff contains the 598 * offset of the directory entry immediately following 599 * the last entry that was looked up. Check if this offset 600 * appears in the hash chain for the name we are looking for. 601 */ 602 for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY; 603 i = WRAPINCR(i, dh->dh_hlen)) 604 if (offset == dh->dh_seqoff) 605 break; 606 if (offset == dh->dh_seqoff) { 607 /* 608 * We found an entry with the expected offset. This 609 * is probably the entry we want, but if not, the 610 * code below will turn off seqopt and retry. 611 */ 612 slot = i; 613 } else 614 dh->dh_seqopt = 0; 615 } 616 617 for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY; 618 slot = WRAPINCR(slot, dh->dh_hlen)) { 619 if (offset == DIRHASH_DEL) 620 continue; 621 if (offset < 0 || offset >= ip->i_size) 622 panic("ufsdirhash_lookup: bad offset in hash array"); 623 if ((offset & ~bmask) != blkoff) { 624 if (bp != NULL) 625 brelse(bp); 626 blkoff = offset & ~bmask; 627 if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) { 628 error = EJUSTRETURN; 629 goto fail; 630 } 631 } 632 dp = (struct direct *)(bp->b_data + (offset & bmask)); 633 if (dp->d_reclen == 0 || dp->d_reclen > 634 DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) { 635 /* Corrupted directory. */ 636 error = EJUSTRETURN; 637 goto fail; 638 } 639 if (dp->d_namlen == namelen && 640 bcmp(dp->d_name, name, namelen) == 0) { 641 /* Found. Get the prev offset if needed. */ 642 if (prevoffp != NULL) { 643 if (offset & (DIRBLKSIZ - 1)) { 644 prevoff = ufsdirhash_getprev(dp, 645 offset); 646 if (prevoff == -1) { 647 error = EJUSTRETURN; 648 goto fail; 649 } 650 } else 651 prevoff = offset; 652 *prevoffp = prevoff; 653 } 654 655 /* Check for sequential access, and update offset. */ 656 if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset) 657 dh->dh_seqopt = 1; 658 dh->dh_seqoff = offset + DIRSIZ(0, dp); 659 *bpp = bp; 660 *offp = offset; 661 ufsdirhash_release(dh); 662 return (0); 663 } 664 665 /* 666 * When the name doesn't match in the seqopt case, go back 667 * and search normally. 668 */ 669 if (dh->dh_seqopt) { 670 dh->dh_seqopt = 0; 671 goto restart; 672 } 673 } 674 error = ENOENT; 675fail: 676 ufsdirhash_release(dh); 677 if (bp != NULL) 678 brelse(bp); 679 return (error); 680} 681 682/* 683 * Find a directory block with room for 'slotneeded' bytes. Returns 684 * the offset of the directory entry that begins the free space. 685 * This will either be the offset of an existing entry that has free 686 * space at the end, or the offset of an entry with d_ino == 0 at 687 * the start of a DIRBLKSIZ block. 688 * 689 * To use the space, the caller may need to compact existing entries in 690 * the directory. The total number of bytes in all of the entries involved 691 * in the compaction is stored in *slotsize. In other words, all of 692 * the entries that must be compacted are exactly contained in the 693 * region beginning at the returned offset and spanning *slotsize bytes. 694 * 695 * Returns -1 if no space was found, indicating that the directory 696 * must be extended. 697 */ 698doff_t 699ufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize) 700{ 701 struct direct *dp; 702 struct dirhash *dh; 703 struct buf *bp; 704 doff_t pos, slotstart; 705 int dirblock, error, freebytes, i; 706 707 dh = ip->i_dirhash; 708 KASSERT(dh != NULL && dh->dh_hash != NULL, 709 ("ufsdirhash_findfree: Invalid dirhash %p\n", dh)); 710 DIRHASH_ASSERT_LOCKED(dh); 711 712 /* Find a directory block with the desired free space. */ 713 dirblock = -1; 714 for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++) 715 if ((dirblock = dh->dh_firstfree[i]) != -1) 716 break; 717 if (dirblock == -1) 718 return (-1); 719 720 KASSERT(dirblock < dh->dh_nblk && 721 dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN), 722 ("ufsdirhash_findfree: bad stats")); 723 pos = dirblock * DIRBLKSIZ; 724 error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp); 725 if (error) 726 return (-1); 727 728 /* Find the first entry with free space. */ 729 for (i = 0; i < DIRBLKSIZ; ) { 730 if (dp->d_reclen == 0) { 731 brelse(bp); 732 return (-1); 733 } 734 if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp)) 735 break; 736 i += dp->d_reclen; 737 dp = (struct direct *)((char *)dp + dp->d_reclen); 738 } 739 if (i > DIRBLKSIZ) { 740 brelse(bp); 741 return (-1); 742 } 743 slotstart = pos + i; 744 745 /* Find the range of entries needed to get enough space */ 746 freebytes = 0; 747 while (i < DIRBLKSIZ && freebytes < slotneeded) { 748 freebytes += dp->d_reclen; 749 if (dp->d_ino != 0) 750 freebytes -= DIRSIZ(0, dp); 751 if (dp->d_reclen == 0) { 752 brelse(bp); 753 return (-1); 754 } 755 i += dp->d_reclen; 756 dp = (struct direct *)((char *)dp + dp->d_reclen); 757 } 758 if (i > DIRBLKSIZ) { 759 brelse(bp); 760 return (-1); 761 } 762 if (freebytes < slotneeded) 763 panic("ufsdirhash_findfree: free mismatch"); 764 brelse(bp); 765 *slotsize = pos + i - slotstart; 766 return (slotstart); 767} 768 769/* 770 * Return the start of the unused space at the end of a directory, or 771 * -1 if there are no trailing unused blocks. 772 */ 773doff_t 774ufsdirhash_enduseful(struct inode *ip) 775{ 776 777 struct dirhash *dh; 778 int i; 779 780 dh = ip->i_dirhash; 781 DIRHASH_ASSERT_LOCKED(dh); 782 KASSERT(dh != NULL && dh->dh_hash != NULL, 783 ("ufsdirhash_enduseful: Invalid dirhash %p\n", dh)); 784 785 if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN) 786 return (-1); 787 788 for (i = dh->dh_dirblks - 1; i >= 0; i--) 789 if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 790 break; 791 792 return ((doff_t)(i + 1) * DIRBLKSIZ); 793} 794 795/* 796 * Insert information into the hash about a new directory entry. dirp 797 * points to a struct direct containing the entry, and offset specifies 798 * the offset of this entry. 799 */ 800void 801ufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset) 802{ 803 struct dirhash *dh; 804 int slot; 805 806 if ((dh = ufsdirhash_acquire(ip)) == NULL) 807 return; 808 809 KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 810 ("ufsdirhash_add: bad offset")); 811 /* 812 * Normal hash usage is < 66%. If the usage gets too high then 813 * remove the hash entirely and let it be rebuilt later. 814 */ 815 if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) { 816 ufsdirhash_free_locked(ip); 817 return; 818 } 819 820 /* Find a free hash slot (empty or deleted), and add the entry. */ 821 slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen); 822 while (DH_ENTRY(dh, slot) >= 0) 823 slot = WRAPINCR(slot, dh->dh_hlen); 824 if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY) 825 dh->dh_hused++; 826 DH_ENTRY(dh, slot) = offset; 827 828 /* Update last used time. */ 829 dh->dh_lastused = time_second; 830 831 /* Update the per-block summary info. */ 832 ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp)); 833 ufsdirhash_release(dh); 834} 835 836/* 837 * Remove the specified directory entry from the hash. The entry to remove 838 * is defined by the name in `dirp', which must exist at the specified 839 * `offset' within the directory. 840 */ 841void 842ufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset) 843{ 844 struct dirhash *dh; 845 int slot; 846 847 if ((dh = ufsdirhash_acquire(ip)) == NULL) 848 return; 849 850 KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 851 ("ufsdirhash_remove: bad offset")); 852 /* Find the entry */ 853 slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset); 854 855 /* Remove the hash entry. */ 856 ufsdirhash_delslot(dh, slot); 857 858 /* Update the per-block summary info. */ 859 ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp)); 860 ufsdirhash_release(dh); 861} 862 863/* 864 * Change the offset associated with a directory entry in the hash. Used 865 * when compacting directory blocks. 866 */ 867void 868ufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff, 869 doff_t newoff) 870{ 871 struct dirhash *dh; 872 int slot; 873 874 if ((dh = ufsdirhash_acquire(ip)) == NULL) 875 return; 876 877 KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ && 878 newoff < dh->dh_dirblks * DIRBLKSIZ, 879 ("ufsdirhash_move: bad offset")); 880 /* Find the entry, and update the offset. */ 881 slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff); 882 DH_ENTRY(dh, slot) = newoff; 883 ufsdirhash_release(dh); 884} 885 886/* 887 * Inform dirhash that the directory has grown by one block that 888 * begins at offset (i.e. the new length is offset + DIRBLKSIZ). 889 */ 890void 891ufsdirhash_newblk(struct inode *ip, doff_t offset) 892{ 893 struct dirhash *dh; 894 int block; 895 896 if ((dh = ufsdirhash_acquire(ip)) == NULL) 897 return; 898 899 KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ, 900 ("ufsdirhash_newblk: bad offset")); 901 block = offset / DIRBLKSIZ; 902 if (block >= dh->dh_nblk) { 903 /* Out of space; must rebuild. */ 904 ufsdirhash_free_locked(ip); 905 return; 906 } 907 dh->dh_dirblks = block + 1; 908 909 /* Account for the new free block. */ 910 dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN; 911 if (dh->dh_firstfree[DH_NFSTATS] == -1) 912 dh->dh_firstfree[DH_NFSTATS] = block; 913 ufsdirhash_release(dh); 914} 915 916/* 917 * Inform dirhash that the directory is being truncated. 918 */ 919void 920ufsdirhash_dirtrunc(struct inode *ip, doff_t offset) 921{ 922 struct dirhash *dh; 923 int block, i; 924 925 if ((dh = ufsdirhash_acquire(ip)) == NULL) 926 return; 927 928 KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ, 929 ("ufsdirhash_dirtrunc: bad offset")); 930 block = howmany(offset, DIRBLKSIZ); 931 /* 932 * If the directory shrinks to less than 1/8 of dh_nblk blocks 933 * (about 20% of its original size due to the 50% extra added in 934 * ufsdirhash_build) then free it, and let the caller rebuild 935 * if necessary. 936 */ 937 if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) { 938 ufsdirhash_free_locked(ip); 939 return; 940 } 941 942 /* 943 * Remove any `first free' information pertaining to the 944 * truncated blocks. All blocks we're removing should be 945 * completely unused. 946 */ 947 if (dh->dh_firstfree[DH_NFSTATS] >= block) 948 dh->dh_firstfree[DH_NFSTATS] = -1; 949 for (i = block; i < dh->dh_dirblks; i++) 950 if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 951 panic("ufsdirhash_dirtrunc: blocks in use"); 952 for (i = 0; i < DH_NFSTATS; i++) 953 if (dh->dh_firstfree[i] >= block) 954 panic("ufsdirhash_dirtrunc: first free corrupt"); 955 dh->dh_dirblks = block; 956 ufsdirhash_release(dh); 957} 958 959/* 960 * Debugging function to check that the dirhash information about 961 * a directory block matches its actual contents. Panics if a mismatch 962 * is detected. 963 * 964 * On entry, `buf' should point to the start of an in-core 965 * DIRBLKSIZ-sized directory block, and `offset' should contain the 966 * offset from the start of the directory of that block. 967 */ 968void 969ufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset) 970{ 971 struct dirhash *dh; 972 struct direct *dp; 973 int block, ffslot, i, nfree; 974 975 if (!ufs_dirhashcheck) 976 return; 977 if ((dh = ufsdirhash_acquire(ip)) == NULL) 978 return; 979 980 block = offset / DIRBLKSIZ; 981 if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks) 982 panic("ufsdirhash_checkblock: bad offset"); 983 984 nfree = 0; 985 for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) { 986 dp = (struct direct *)(buf + i); 987 if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ) 988 panic("ufsdirhash_checkblock: bad dir"); 989 990 if (dp->d_ino == 0) { 991#if 0 992 /* 993 * XXX entries with d_ino == 0 should only occur 994 * at the start of a DIRBLKSIZ block. However the 995 * ufs code is tolerant of such entries at other 996 * offsets, and fsck does not fix them. 997 */ 998 if (i != 0) 999 panic("ufsdirhash_checkblock: bad dir inode"); 1000#endif 1001 nfree += dp->d_reclen; 1002 continue; 1003 } 1004 1005 /* Check that the entry exists (will panic if it doesn't). */ 1006 ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i); 1007 1008 nfree += dp->d_reclen - DIRSIZ(0, dp); 1009 } 1010 if (i != DIRBLKSIZ) 1011 panic("ufsdirhash_checkblock: bad dir end"); 1012 1013 if (dh->dh_blkfree[block] * DIRALIGN != nfree) 1014 panic("ufsdirhash_checkblock: bad free count"); 1015 1016 ffslot = BLKFREE2IDX(nfree / DIRALIGN); 1017 for (i = 0; i <= DH_NFSTATS; i++) 1018 if (dh->dh_firstfree[i] == block && i != ffslot) 1019 panic("ufsdirhash_checkblock: bad first-free"); 1020 if (dh->dh_firstfree[ffslot] == -1) 1021 panic("ufsdirhash_checkblock: missing first-free entry"); 1022 ufsdirhash_release(dh); 1023} 1024 1025/* 1026 * Hash the specified filename into a dirhash slot. 1027 */ 1028static int 1029ufsdirhash_hash(struct dirhash *dh, char *name, int namelen) 1030{ 1031 u_int32_t hash; 1032 1033 /* 1034 * We hash the name and then some other bit of data that is 1035 * invariant over the dirhash's lifetime. Otherwise names 1036 * differing only in the last byte are placed close to one 1037 * another in the table, which is bad for linear probing. 1038 */ 1039 hash = fnv_32_buf(name, namelen, FNV1_32_INIT); 1040 hash = fnv_32_buf(&dh, sizeof(dh), hash); 1041 return (hash % dh->dh_hlen); 1042} 1043 1044/* 1045 * Adjust the number of free bytes in the block containing `offset' 1046 * by the value specified by `diff'. 1047 * 1048 * The caller must ensure we have exclusive access to `dh'; normally 1049 * that means that dh_lock should be held, but this is also called 1050 * from ufsdirhash_build() where exclusive access can be assumed. 1051 */ 1052static void 1053ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff) 1054{ 1055 int block, i, nfidx, ofidx; 1056 1057 /* Update the per-block summary info. */ 1058 block = offset / DIRBLKSIZ; 1059 KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks, 1060 ("dirhash bad offset")); 1061 ofidx = BLKFREE2IDX(dh->dh_blkfree[block]); 1062 dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN); 1063 nfidx = BLKFREE2IDX(dh->dh_blkfree[block]); 1064 1065 /* Update the `first free' list if necessary. */ 1066 if (ofidx != nfidx) { 1067 /* If removing, scan forward for the next block. */ 1068 if (dh->dh_firstfree[ofidx] == block) { 1069 for (i = block + 1; i < dh->dh_dirblks; i++) 1070 if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx) 1071 break; 1072 dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1; 1073 } 1074 1075 /* Make this the new `first free' if necessary */ 1076 if (dh->dh_firstfree[nfidx] > block || 1077 dh->dh_firstfree[nfidx] == -1) 1078 dh->dh_firstfree[nfidx] = block; 1079 } 1080} 1081 1082/* 1083 * Find the specified name which should have the specified offset. 1084 * Returns a slot number, and panics on failure. 1085 * 1086 * `dh' must be locked on entry and remains so on return. 1087 */ 1088static int 1089ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset) 1090{ 1091 int slot; 1092 1093 DIRHASH_ASSERT_LOCKED(dh); 1094 1095 /* Find the entry. */ 1096 KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full")); 1097 slot = ufsdirhash_hash(dh, name, namelen); 1098 while (DH_ENTRY(dh, slot) != offset && 1099 DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 1100 slot = WRAPINCR(slot, dh->dh_hlen); 1101 if (DH_ENTRY(dh, slot) != offset) 1102 panic("ufsdirhash_findslot: '%.*s' not found", namelen, name); 1103 1104 return (slot); 1105} 1106 1107/* 1108 * Remove the entry corresponding to the specified slot from the hash array. 1109 * 1110 * `dh' must be locked on entry and remains so on return. 1111 */ 1112static void 1113ufsdirhash_delslot(struct dirhash *dh, int slot) 1114{ 1115 int i; 1116 1117 DIRHASH_ASSERT_LOCKED(dh); 1118 1119 /* Mark the entry as deleted. */ 1120 DH_ENTRY(dh, slot) = DIRHASH_DEL; 1121 1122 /* If this is the end of a chain of DIRHASH_DEL slots, remove them. */ 1123 for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; ) 1124 i = WRAPINCR(i, dh->dh_hlen); 1125 if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) { 1126 i = WRAPDECR(i, dh->dh_hlen); 1127 while (DH_ENTRY(dh, i) == DIRHASH_DEL) { 1128 DH_ENTRY(dh, i) = DIRHASH_EMPTY; 1129 dh->dh_hused--; 1130 i = WRAPDECR(i, dh->dh_hlen); 1131 } 1132 KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen")); 1133 } 1134} 1135 1136/* 1137 * Given a directory entry and its offset, find the offset of the 1138 * previous entry in the same DIRBLKSIZ-sized block. Returns an 1139 * offset, or -1 if there is no previous entry in the block or some 1140 * other problem occurred. 1141 */ 1142static doff_t 1143ufsdirhash_getprev(struct direct *dirp, doff_t offset) 1144{ 1145 struct direct *dp; 1146 char *blkbuf; 1147 doff_t blkoff, prevoff; 1148 int entrypos, i; 1149 1150 blkoff = offset & ~(DIRBLKSIZ - 1); /* offset of start of block */ 1151 entrypos = offset & (DIRBLKSIZ - 1); /* entry relative to block */ 1152 blkbuf = (char *)dirp - entrypos; 1153 prevoff = blkoff; 1154 1155 /* If `offset' is the start of a block, there is no previous entry. */ 1156 if (entrypos == 0) 1157 return (-1); 1158 1159 /* Scan from the start of the block until we get to the entry. */ 1160 for (i = 0; i < entrypos; i += dp->d_reclen) { 1161 dp = (struct direct *)(blkbuf + i); 1162 if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos) 1163 return (-1); /* Corrupted directory. */ 1164 prevoff = blkoff + i; 1165 } 1166 return (prevoff); 1167} 1168 1169/* 1170 * Delete the given dirhash and reclaim its memory. Assumes that 1171 * ufsdirhash_list is locked, and leaves it locked. Also assumes 1172 * that dh is locked. Returns the amount of memory freed. 1173 */ 1174static int 1175ufsdirhash_destroy(struct dirhash *dh) 1176{ 1177 doff_t **hash; 1178 u_int8_t *blkfree; 1179 int i, mem, narrays; 1180 1181 KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list")); 1182 1183 /* Remove it from the list and detach its memory. */ 1184 TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 1185 dh->dh_onlist = 0; 1186 hash = dh->dh_hash; 1187 dh->dh_hash = NULL; 1188 blkfree = dh->dh_blkfree; 1189 dh->dh_blkfree = NULL; 1190 narrays = dh->dh_narrays; 1191 mem = dh->dh_memreq; 1192 dh->dh_memreq = 0; 1193 1194 /* Unlock dirhash and free the detached memory. */ 1195 ufsdirhash_release(dh); 1196 for (i = 0; i < narrays; i++) 1197 DIRHASH_BLKFREE(hash[i]); 1198 free(hash, M_DIRHASH); 1199 free(blkfree, M_DIRHASH); 1200 1201 /* Account for the returned memory. */ 1202 ufs_dirhashmem -= mem; 1203 1204 return (mem); 1205} 1206 1207/* 1208 * Try to free up `wanted' bytes by stealing memory from existing 1209 * dirhashes. Returns zero with list locked if successful. 1210 */ 1211static int 1212ufsdirhash_recycle(int wanted) 1213{ 1214 struct dirhash *dh; 1215 1216 DIRHASHLIST_LOCK(); 1217 dh = TAILQ_FIRST(&ufsdirhash_list); 1218 while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) { 1219 /* Decrement the score; only recycle if it becomes zero. */ 1220 if (dh == NULL || --dh->dh_score > 0) { 1221 DIRHASHLIST_UNLOCK(); 1222 return (-1); 1223 } 1224 /* 1225 * If we can't lock it it's in use and we don't want to 1226 * recycle it anyway. 1227 */ 1228 if (!sx_try_xlock(&dh->dh_lock)) { 1229 dh = TAILQ_NEXT(dh, dh_list); 1230 continue; 1231 } 1232 1233 ufsdirhash_destroy(dh); 1234 1235 /* Repeat if necessary. */ 1236 dh = TAILQ_FIRST(&ufsdirhash_list); 1237 } 1238 /* Success; return with list locked. */ 1239 return (0); 1240} 1241 1242/* 1243 * Callback that frees some dirhashes when the system is low on virtual memory. 1244 */ 1245static void 1246ufsdirhash_lowmem() 1247{ 1248 struct dirhash *dh, *dh_temp; 1249 int memfreed = 0; 1250 /* XXX: this 10% may need to be adjusted */ 1251 int memwanted = ufs_dirhashmem / 10; 1252 1253 ufs_dirhashlowmemcount++; 1254 1255 DIRHASHLIST_LOCK(); 1256 /* 1257 * Delete dirhashes not used for more than ufs_dirhashreclaimage 1258 * seconds. If we can't get a lock on the dirhash, it will be skipped. 1259 */ 1260 TAILQ_FOREACH_SAFE(dh, &ufsdirhash_list, dh_list, dh_temp) { 1261 if (!sx_try_xlock(&dh->dh_lock)) 1262 continue; 1263 if (time_second - dh->dh_lastused > ufs_dirhashreclaimage) 1264 memfreed += ufsdirhash_destroy(dh); 1265 /* Unlock if we didn't delete the dirhash */ 1266 else 1267 ufsdirhash_release(dh); 1268 } 1269 1270 /* 1271 * If not enough memory was freed, keep deleting hashes from the head 1272 * of the dirhash list. The ones closest to the head should be the 1273 * oldest. 1274 */ 1275 if (memfreed < memwanted) { 1276 TAILQ_FOREACH_SAFE(dh, &ufsdirhash_list, dh_list, dh_temp) { 1277 if (!sx_try_xlock(&dh->dh_lock)) 1278 continue; 1279 memfreed += ufsdirhash_destroy(dh); 1280 if (memfreed >= memwanted) 1281 break; 1282 } 1283 } 1284 DIRHASHLIST_UNLOCK(); 1285} 1286 1287 1288void 1289ufsdirhash_init() 1290{ 1291 ufsdirhash_zone = uma_zcreate("DIRHASH", DH_NBLKOFF * sizeof(doff_t), 1292 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1293 mtx_init(&ufsdirhash_mtx, "dirhash list", NULL, MTX_DEF); 1294 TAILQ_INIT(&ufsdirhash_list); 1295 1296 /* Register a callback function to handle low memory signals */ 1297 EVENTHANDLER_REGISTER(vm_lowmem, ufsdirhash_lowmem, NULL, 1298 EVENTHANDLER_PRI_FIRST); 1299} 1300 1301void 1302ufsdirhash_uninit() 1303{ 1304 KASSERT(TAILQ_EMPTY(&ufsdirhash_list), ("ufsdirhash_uninit")); 1305 uma_zdestroy(ufsdirhash_zone); 1306 mtx_destroy(&ufsdirhash_mtx); 1307} 1308 1309#endif /* UFS_DIRHASH */ 1310