ufs_dirhash.c revision 219384
1139825Simp/*- 2107868Siedowse * Copyright (c) 2001, 2002 Ian Dowse. All rights reserved. 379561Siedowse * 479561Siedowse * Redistribution and use in source and binary forms, with or without 579561Siedowse * modification, are permitted provided that the following conditions 679561Siedowse * are met: 779561Siedowse * 1. Redistributions of source code must retain the above copyright 879561Siedowse * notice, this list of conditions and the following disclaimer. 979561Siedowse * 2. Redistributions in binary form must reproduce the above copyright 1079561Siedowse * notice, this list of conditions and the following disclaimer in the 1179561Siedowse * documentation and/or other materials provided with the distribution. 1279561Siedowse * 1379561Siedowse * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1479561Siedowse * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1579561Siedowse * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1679561Siedowse * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1779561Siedowse * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1879561Siedowse * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1979561Siedowse * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2079561Siedowse * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2179561Siedowse * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2279561Siedowse * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2379561Siedowse * SUCH DAMAGE. 2479561Siedowse */ 25116192Sobrien 2679561Siedowse/* 2779561Siedowse * This implements a hash-based lookup scheme for UFS directories. 2879561Siedowse */ 2979561Siedowse 30116192Sobrien#include <sys/cdefs.h> 31116192Sobrien__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_dirhash.c 219384 2011-03-07 18:33:29Z jhb $"); 32116192Sobrien 3379561Siedowse#include "opt_ufs.h" 3479561Siedowse 3579561Siedowse#ifdef UFS_DIRHASH 3679561Siedowse 3779561Siedowse#include <sys/param.h> 3879561Siedowse#include <sys/systm.h> 3979561Siedowse#include <sys/kernel.h> 4084811Sjhb#include <sys/lock.h> 4179561Siedowse#include <sys/mutex.h> 4279561Siedowse#include <sys/malloc.h> 4379561Siedowse#include <sys/fnv_hash.h> 4479561Siedowse#include <sys/proc.h> 4579561Siedowse#include <sys/bio.h> 4679561Siedowse#include <sys/buf.h> 4779561Siedowse#include <sys/vnode.h> 4879561Siedowse#include <sys/mount.h> 49183080Sjhb#include <sys/refcount.h> 5079561Siedowse#include <sys/sysctl.h> 51183080Sjhb#include <sys/sx.h> 52193375Ssnb#include <sys/eventhandler.h> 53193375Ssnb#include <sys/time.h> 5492768Sjeff#include <vm/uma.h> 5579561Siedowse 5679561Siedowse#include <ufs/ufs/quota.h> 5779561Siedowse#include <ufs/ufs/inode.h> 5879561Siedowse#include <ufs/ufs/dir.h> 5979561Siedowse#include <ufs/ufs/dirhash.h> 6079561Siedowse#include <ufs/ufs/extattr.h> 6179561Siedowse#include <ufs/ufs/ufsmount.h> 6279561Siedowse#include <ufs/ufs/ufs_extern.h> 6379561Siedowse 6479561Siedowse#define WRAPINCR(val, limit) (((val) + 1 == (limit)) ? 0 : ((val) + 1)) 6592807Sdwmalone#define WRAPDECR(val, limit) (((val) == 0) ? ((limit) - 1) : ((val) - 1)) 6679561Siedowse#define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0) 6792098Siedowse#define BLKFREE2IDX(n) ((n) > DH_NFSTATS ? DH_NFSTATS : (n)) 6879561Siedowse 69151897Srwatsonstatic MALLOC_DEFINE(M_DIRHASH, "ufs_dirhash", "UFS directory hash tables"); 7079561Siedowse 7179561Siedowsestatic int ufs_mindirhashsize = DIRBLKSIZ * 5; 7279561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_minsize, CTLFLAG_RW, 7379561Siedowse &ufs_mindirhashsize, 7479561Siedowse 0, "minimum directory size in bytes for which to use hashed lookup"); 75214359Sivorasstatic int ufs_dirhashmaxmem = 2 * 1024 * 1024; /* NOTE: initial value. It is 76214359Sivoras tuned in ufsdirhash_init() */ 7779561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_maxmem, CTLFLAG_RW, &ufs_dirhashmaxmem, 7879561Siedowse 0, "maximum allowed dirhash memory usage"); 7979561Siedowsestatic int ufs_dirhashmem; 8079561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_mem, CTLFLAG_RD, &ufs_dirhashmem, 8179561Siedowse 0, "current dirhash memory usage"); 8285512Siedowsestatic int ufs_dirhashcheck = 0; 8379561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_docheck, CTLFLAG_RW, &ufs_dirhashcheck, 8479561Siedowse 0, "enable extra sanity tests"); 85193375Ssnbstatic int ufs_dirhashlowmemcount = 0; 86193375SsnbSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_lowmemcount, CTLFLAG_RD, 87193375Ssnb &ufs_dirhashlowmemcount, 0, "number of times low memory hook called"); 88193375Ssnbstatic int ufs_dirhashreclaimage = 5; 89193375SsnbSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_reclaimage, CTLFLAG_RW, 90193375Ssnb &ufs_dirhashreclaimage, 0, 91193375Ssnb "max time in seconds of hash inactivity before deletion in low VM events"); 9279561Siedowse 9379561Siedowse 9479561Siedowsestatic int ufsdirhash_hash(struct dirhash *dh, char *name, int namelen); 9579561Siedowsestatic void ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff); 9679561Siedowsestatic void ufsdirhash_delslot(struct dirhash *dh, int slot); 9779561Siedowsestatic int ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, 9879561Siedowse doff_t offset); 9979561Siedowsestatic doff_t ufsdirhash_getprev(struct direct *dp, doff_t offset); 10079561Siedowsestatic int ufsdirhash_recycle(int wanted); 101193375Ssnbstatic void ufsdirhash_lowmem(void); 102178110Sjeffstatic void ufsdirhash_free_locked(struct inode *ip); 10379561Siedowse 10492768Sjeffstatic uma_zone_t ufsdirhash_zone; 10579561Siedowse 106125854Sdwmalone#define DIRHASHLIST_LOCK() mtx_lock(&ufsdirhash_mtx) 107125854Sdwmalone#define DIRHASHLIST_UNLOCK() mtx_unlock(&ufsdirhash_mtx) 108125854Sdwmalone#define DIRHASH_BLKALLOC_WAITOK() uma_zalloc(ufsdirhash_zone, M_WAITOK) 109125854Sdwmalone#define DIRHASH_BLKFREE(ptr) uma_zfree(ufsdirhash_zone, (ptr)) 110178110Sjeff#define DIRHASH_ASSERT_LOCKED(dh) \ 111183080Sjhb sx_assert(&(dh)->dh_lock, SA_LOCKED) 112125854Sdwmalone 11379561Siedowse/* Dirhash list; recently-used entries are near the tail. */ 11479561Siedowsestatic TAILQ_HEAD(, dirhash) ufsdirhash_list; 11579561Siedowse 11679561Siedowse/* Protects: ufsdirhash_list, `dh_list' field, ufs_dirhashmem. */ 11779561Siedowsestatic struct mtx ufsdirhash_mtx; 11879561Siedowse 11979561Siedowse/* 120178110Sjeff * Locking: 12179561Siedowse * 122178110Sjeff * The relationship between inode and dirhash is protected either by an 123178110Sjeff * exclusive vnode lock or the vnode interlock where a shared vnode lock 124183080Sjhb * may be used. The dirhash_mtx is acquired after the dirhash lock. To 125183080Sjhb * handle teardown races, code wishing to lock the dirhash for an inode 126183080Sjhb * when using a shared vnode lock must obtain a private reference on the 127183080Sjhb * dirhash while holding the vnode interlock. They can drop it once they 128183080Sjhb * have obtained the dirhash lock and verified that the dirhash wasn't 129183080Sjhb * recycled while they waited for the dirhash lock. 130178110Sjeff * 131178110Sjeff * ufsdirhash_build() acquires a shared lock on the dirhash when it is 132178110Sjeff * successful. This lock is released after a call to ufsdirhash_lookup(). 133178110Sjeff * 134178110Sjeff * Functions requiring exclusive access use ufsdirhash_acquire() which may 135178110Sjeff * free a dirhash structure that was recycled by ufsdirhash_recycle(). 136178110Sjeff * 137178110Sjeff * The dirhash lock may be held across io operations. 138187474Sjhb * 139187474Sjhb * WITNESS reports a lock order reversal between the "bufwait" lock 140187474Sjhb * and the "dirhash" lock. However, this specific reversal will not 141187474Sjhb * cause a deadlock. To get a deadlock, one would have to lock a 142187474Sjhb * buffer followed by the dirhash while a second thread locked a 143187474Sjhb * buffer while holding the dirhash lock. The second order can happen 144187474Sjhb * under a shared or exclusive vnode lock for the associated directory 145187474Sjhb * in lookup(). The first order, however, can only happen under an 146187474Sjhb * exclusive vnode lock (e.g. unlink(), rename(), etc.). Thus, for 147187474Sjhb * a thread to be doing a "bufwait" -> "dirhash" order, it has to hold 148187474Sjhb * an exclusive vnode lock. That exclusive vnode lock will prevent 149187474Sjhb * any other threads from doing a "dirhash" -> "bufwait" order. 15079561Siedowse */ 15179561Siedowse 152183080Sjhbstatic void 153183080Sjhbufsdirhash_hold(struct dirhash *dh) 154183080Sjhb{ 155183080Sjhb 156183080Sjhb refcount_acquire(&dh->dh_refcount); 157183080Sjhb} 158183080Sjhb 159183080Sjhbstatic void 160183080Sjhbufsdirhash_drop(struct dirhash *dh) 161183080Sjhb{ 162183080Sjhb 163183080Sjhb if (refcount_release(&dh->dh_refcount)) { 164183080Sjhb sx_destroy(&dh->dh_lock); 165183080Sjhb free(dh, M_DIRHASH); 166183080Sjhb } 167183080Sjhb} 168183080Sjhb 16979561Siedowse/* 170178110Sjeff * Release the lock on a dirhash. 171178110Sjeff */ 172178110Sjeffstatic void 173178110Sjeffufsdirhash_release(struct dirhash *dh) 174178110Sjeff{ 175178110Sjeff 176183080Sjhb sx_unlock(&dh->dh_lock); 177178110Sjeff} 178178110Sjeff 179178110Sjeff/* 180178110Sjeff * Either acquire an existing hash locked shared or create a new hash and 181178110Sjeff * return it exclusively locked. May return NULL if the allocation fails. 182178110Sjeff * 183178110Sjeff * The vnode interlock is used to protect the i_dirhash pointer from 184178110Sjeff * simultaneous access while only a shared vnode lock is held. 185178110Sjeff */ 186178110Sjeffstatic struct dirhash * 187178110Sjeffufsdirhash_create(struct inode *ip) 188178110Sjeff{ 189178110Sjeff struct dirhash *ndh; 190178110Sjeff struct dirhash *dh; 191178110Sjeff struct vnode *vp; 192178110Sjeff int error; 193178110Sjeff 194178110Sjeff error = 0; 195178110Sjeff ndh = dh = NULL; 196178110Sjeff vp = ip->i_vnode; 197178110Sjeff for (;;) { 198185102Sjhb /* Racy check for i_dirhash to prefetch a dirhash structure. */ 199178110Sjeff if (ip->i_dirhash == NULL && ndh == NULL) { 200184205Sdes ndh = malloc(sizeof *dh, M_DIRHASH, 201178110Sjeff M_NOWAIT | M_ZERO); 202178110Sjeff if (ndh == NULL) 203178110Sjeff return (NULL); 204183080Sjhb refcount_init(&ndh->dh_refcount, 1); 205184651Sjhb 206184651Sjhb /* 207184651Sjhb * The DUPOK is to prevent warnings from the 208184651Sjhb * sx_slock() a few lines down which is safe 209184651Sjhb * since the duplicate lock in that case is 210184651Sjhb * the one for this dirhash we are creating 211184651Sjhb * now which has no external references until 212184651Sjhb * after this function returns. 213184651Sjhb */ 214184651Sjhb sx_init_flags(&ndh->dh_lock, "dirhash", SX_DUPOK); 215183080Sjhb sx_xlock(&ndh->dh_lock); 216178110Sjeff } 217178110Sjeff /* 218178110Sjeff * Check i_dirhash. If it's NULL just try to use a 219178110Sjeff * preallocated structure. If none exists loop and try again. 220178110Sjeff */ 221178110Sjeff VI_LOCK(vp); 222178110Sjeff dh = ip->i_dirhash; 223178110Sjeff if (dh == NULL) { 224178110Sjeff ip->i_dirhash = ndh; 225178110Sjeff VI_UNLOCK(vp); 226178110Sjeff if (ndh == NULL) 227178110Sjeff continue; 228178110Sjeff return (ndh); 229178110Sjeff } 230183080Sjhb ufsdirhash_hold(dh); 231183080Sjhb VI_UNLOCK(vp); 232183080Sjhb 233183080Sjhb /* Acquire a shared lock on existing hashes. */ 234183080Sjhb sx_slock(&dh->dh_lock); 235183080Sjhb 236178110Sjeff /* The hash could've been recycled while we were waiting. */ 237183080Sjhb VI_LOCK(vp); 238178110Sjeff if (ip->i_dirhash != dh) { 239183080Sjhb VI_UNLOCK(vp); 240178110Sjeff ufsdirhash_release(dh); 241183080Sjhb ufsdirhash_drop(dh); 242178110Sjeff continue; 243178110Sjeff } 244183080Sjhb VI_UNLOCK(vp); 245183080Sjhb ufsdirhash_drop(dh); 246183080Sjhb 247178110Sjeff /* If the hash is still valid we've succeeded. */ 248178110Sjeff if (dh->dh_hash != NULL) 249178110Sjeff break; 250178110Sjeff /* 251178110Sjeff * If the hash is NULL it has been recycled. Try to upgrade 252183080Sjhb * so we can recreate it. If we fail the upgrade, drop our 253183080Sjhb * lock and try again. 254178110Sjeff */ 255183080Sjhb if (sx_try_upgrade(&dh->dh_lock)) 256178110Sjeff break; 257183080Sjhb sx_sunlock(&dh->dh_lock); 258178110Sjeff } 259178110Sjeff /* Free the preallocated structure if it was not necessary. */ 260178110Sjeff if (ndh) { 261183080Sjhb ufsdirhash_release(ndh); 262183080Sjhb ufsdirhash_drop(ndh); 263178110Sjeff } 264178110Sjeff return (dh); 265178110Sjeff} 266178110Sjeff 267178110Sjeff/* 268178110Sjeff * Acquire an exclusive lock on an existing hash. Requires an exclusive 269178110Sjeff * vnode lock to protect the i_dirhash pointer. hashes that have been 270178110Sjeff * recycled are reclaimed here and NULL is returned. 271178110Sjeff */ 272178110Sjeffstatic struct dirhash * 273178110Sjeffufsdirhash_acquire(struct inode *ip) 274178110Sjeff{ 275178110Sjeff struct dirhash *dh; 276178110Sjeff struct vnode *vp; 277178110Sjeff 278178110Sjeff ASSERT_VOP_ELOCKED(ip->i_vnode, __FUNCTION__); 279178110Sjeff 280178110Sjeff vp = ip->i_vnode; 281178110Sjeff dh = ip->i_dirhash; 282178110Sjeff if (dh == NULL) 283178110Sjeff return (NULL); 284183080Sjhb sx_xlock(&dh->dh_lock); 285178110Sjeff if (dh->dh_hash != NULL) 286178110Sjeff return (dh); 287178110Sjeff ufsdirhash_free_locked(ip); 288178110Sjeff return (NULL); 289178110Sjeff} 290178110Sjeff 291178110Sjeff/* 292178110Sjeff * Acquire exclusively and free the hash pointed to by ip. Works with a 293178110Sjeff * shared or exclusive vnode lock. 294178110Sjeff */ 295178110Sjeffvoid 296178110Sjeffufsdirhash_free(struct inode *ip) 297178110Sjeff{ 298178110Sjeff struct dirhash *dh; 299178110Sjeff struct vnode *vp; 300178110Sjeff 301178110Sjeff vp = ip->i_vnode; 302178110Sjeff for (;;) { 303183080Sjhb /* Grab a reference on this inode's dirhash if it has one. */ 304178110Sjeff VI_LOCK(vp); 305178110Sjeff dh = ip->i_dirhash; 306178110Sjeff if (dh == NULL) { 307178110Sjeff VI_UNLOCK(vp); 308178110Sjeff return; 309178110Sjeff } 310183080Sjhb ufsdirhash_hold(dh); 311183080Sjhb VI_UNLOCK(vp); 312183080Sjhb 313183080Sjhb /* Exclusively lock the dirhash. */ 314183080Sjhb sx_xlock(&dh->dh_lock); 315183080Sjhb 316183080Sjhb /* If this dirhash still belongs to this inode, then free it. */ 317183080Sjhb VI_LOCK(vp); 318183080Sjhb if (ip->i_dirhash == dh) { 319183080Sjhb VI_UNLOCK(vp); 320183080Sjhb ufsdirhash_drop(dh); 321178110Sjeff break; 322183080Sjhb } 323183080Sjhb VI_UNLOCK(vp); 324183080Sjhb 325183080Sjhb /* 326183080Sjhb * This inode's dirhash has changed while we were 327183080Sjhb * waiting for the dirhash lock, so try again. 328183080Sjhb */ 329178110Sjeff ufsdirhash_release(dh); 330183080Sjhb ufsdirhash_drop(dh); 331178110Sjeff } 332178110Sjeff ufsdirhash_free_locked(ip); 333178110Sjeff} 334178110Sjeff 335178110Sjeff/* 33679561Siedowse * Attempt to build up a hash table for the directory contents in 33779561Siedowse * inode 'ip'. Returns 0 on success, or -1 of the operation failed. 33879561Siedowse */ 33979561Siedowseint 34079561Siedowseufsdirhash_build(struct inode *ip) 34179561Siedowse{ 34279561Siedowse struct dirhash *dh; 34379561Siedowse struct buf *bp = NULL; 34479561Siedowse struct direct *ep; 34579561Siedowse struct vnode *vp; 34679561Siedowse doff_t bmask, pos; 34779561Siedowse int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot; 34879561Siedowse 349178110Sjeff /* Take care of a decreased sysctl value. */ 350195003Ssnb while (ufs_dirhashmem > ufs_dirhashmaxmem) { 351178110Sjeff if (ufsdirhash_recycle(0) != 0) 352178110Sjeff return (-1); 353195003Ssnb /* Recycled enough memory, so unlock the list. */ 354195003Ssnb DIRHASHLIST_UNLOCK(); 355195003Ssnb } 356178110Sjeff 35779561Siedowse /* Check if we can/should use dirhash. */ 358178110Sjeff if (ip->i_size < ufs_mindirhashsize || OFSFMT(ip->i_vnode) || 359178110Sjeff ip->i_effnlink == 0) { 360178110Sjeff if (ip->i_dirhash) 36179561Siedowse ufsdirhash_free(ip); 362178110Sjeff return (-1); 36379561Siedowse } 364178110Sjeff dh = ufsdirhash_create(ip); 365178110Sjeff if (dh == NULL) 36682364Siedowse return (-1); 367178110Sjeff if (dh->dh_hash != NULL) 368178110Sjeff return (0); 36982364Siedowse 37079561Siedowse vp = ip->i_vnode; 37179561Siedowse /* Allocate 50% more entries than this dir size could ever need. */ 37279561Siedowse KASSERT(ip->i_size >= DIRBLKSIZ, ("ufsdirhash_build size")); 37379561Siedowse nslots = ip->i_size / DIRECTSIZ(1); 37479561Siedowse nslots = (nslots * 3 + 1) / 2; 37579561Siedowse narrays = howmany(nslots, DH_NBLKOFF); 37679561Siedowse nslots = narrays * DH_NBLKOFF; 37779561Siedowse dirblocks = howmany(ip->i_size, DIRBLKSIZ); 37879561Siedowse nblocks = (dirblocks * 3 + 1) / 2; 37979561Siedowse memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) + 38079561Siedowse narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 38179561Siedowse nblocks * sizeof(*dh->dh_blkfree); 382125854Sdwmalone DIRHASHLIST_LOCK(); 38379561Siedowse if (memreqd + ufs_dirhashmem > ufs_dirhashmaxmem) { 384125854Sdwmalone DIRHASHLIST_UNLOCK(); 38579561Siedowse if (memreqd > ufs_dirhashmaxmem / 2) 386178110Sjeff goto fail; 38779561Siedowse /* Try to free some space. */ 38879561Siedowse if (ufsdirhash_recycle(memreqd) != 0) 389178110Sjeff goto fail; 390125854Sdwmalone /* Enough was freed, and list has been locked. */ 39179561Siedowse } 39279561Siedowse ufs_dirhashmem += memreqd; 393125854Sdwmalone DIRHASHLIST_UNLOCK(); 39479561Siedowse 395178110Sjeff /* Initialise the hash table and block statistics. */ 396178110Sjeff dh->dh_memreq = memreqd; 397178110Sjeff dh->dh_narrays = narrays; 398178110Sjeff dh->dh_hlen = nslots; 399178110Sjeff dh->dh_nblk = nblocks; 400178110Sjeff dh->dh_dirblks = dirblocks; 401178110Sjeff for (i = 0; i < DH_NFSTATS; i++) 402178110Sjeff dh->dh_firstfree[i] = -1; 403178110Sjeff dh->dh_firstfree[DH_NFSTATS] = 0; 404178110Sjeff dh->dh_hused = 0; 405219384Sjhb dh->dh_seqoff = -1; 406178110Sjeff dh->dh_score = DH_SCOREINIT; 407193375Ssnb dh->dh_lastused = time_second; 408178110Sjeff 40979561Siedowse /* 41079561Siedowse * Use non-blocking mallocs so that we will revert to a linear 41179561Siedowse * lookup on failure rather than potentially blocking forever. 41279561Siedowse */ 413184205Sdes dh->dh_hash = malloc(narrays * sizeof(dh->dh_hash[0]), 41479561Siedowse M_DIRHASH, M_NOWAIT | M_ZERO); 415178110Sjeff if (dh->dh_hash == NULL) 416178110Sjeff goto fail; 417184205Sdes dh->dh_blkfree = malloc(nblocks * sizeof(dh->dh_blkfree[0]), 41879561Siedowse M_DIRHASH, M_NOWAIT); 419178110Sjeff if (dh->dh_blkfree == NULL) 42079561Siedowse goto fail; 42179561Siedowse for (i = 0; i < narrays; i++) { 422125854Sdwmalone if ((dh->dh_hash[i] = DIRHASH_BLKALLOC_WAITOK()) == NULL) 42379561Siedowse goto fail; 42479561Siedowse for (j = 0; j < DH_NBLKOFF; j++) 42579561Siedowse dh->dh_hash[i][j] = DIRHASH_EMPTY; 42679561Siedowse } 42779561Siedowse for (i = 0; i < dirblocks; i++) 42879561Siedowse dh->dh_blkfree[i] = DIRBLKSIZ / DIRALIGN; 42979561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 43079561Siedowse pos = 0; 43179561Siedowse while (pos < ip->i_size) { 43279561Siedowse /* If necessary, get the next directory block. */ 43379561Siedowse if ((pos & bmask) == 0) { 43479561Siedowse if (bp != NULL) 43579561Siedowse brelse(bp); 43679561Siedowse if (UFS_BLKATOFF(vp, (off_t)pos, NULL, &bp) != 0) 43779561Siedowse goto fail; 43879561Siedowse } 43979561Siedowse 44079561Siedowse /* Add this entry to the hash. */ 44179561Siedowse ep = (struct direct *)((char *)bp->b_data + (pos & bmask)); 44279561Siedowse if (ep->d_reclen == 0 || ep->d_reclen > 44379561Siedowse DIRBLKSIZ - (pos & (DIRBLKSIZ - 1))) { 44479561Siedowse /* Corrupted directory. */ 44579561Siedowse brelse(bp); 44679561Siedowse goto fail; 44779561Siedowse } 44879561Siedowse if (ep->d_ino != 0) { 44979561Siedowse /* Add the entry (simplified ufsdirhash_add). */ 45079561Siedowse slot = ufsdirhash_hash(dh, ep->d_name, ep->d_namlen); 45179561Siedowse while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 45279561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 45379561Siedowse dh->dh_hused++; 45479561Siedowse DH_ENTRY(dh, slot) = pos; 45579561Siedowse ufsdirhash_adjfree(dh, pos, -DIRSIZ(0, ep)); 45679561Siedowse } 45779561Siedowse pos += ep->d_reclen; 45879561Siedowse } 45979561Siedowse 46079561Siedowse if (bp != NULL) 46179561Siedowse brelse(bp); 462125854Sdwmalone DIRHASHLIST_LOCK(); 46379561Siedowse TAILQ_INSERT_TAIL(&ufsdirhash_list, dh, dh_list); 46479561Siedowse dh->dh_onlist = 1; 465125854Sdwmalone DIRHASHLIST_UNLOCK(); 466183080Sjhb sx_downgrade(&dh->dh_lock); 46779561Siedowse return (0); 46879561Siedowse 46979561Siedowsefail: 470178110Sjeff ufsdirhash_free_locked(ip); 47179561Siedowse return (-1); 47279561Siedowse} 47379561Siedowse 47479561Siedowse/* 47579561Siedowse * Free any hash table associated with inode 'ip'. 47679561Siedowse */ 477178110Sjeffstatic void 478178110Sjeffufsdirhash_free_locked(struct inode *ip) 47979561Siedowse{ 48079561Siedowse struct dirhash *dh; 481178110Sjeff struct vnode *vp; 482178110Sjeff int i; 48379561Siedowse 484178110Sjeff DIRHASH_ASSERT_LOCKED(ip->i_dirhash); 485183080Sjhb 486178110Sjeff /* 487178110Sjeff * Clear the pointer in the inode to prevent new threads from 488178110Sjeff * finding the dead structure. 489178110Sjeff */ 490178110Sjeff vp = ip->i_vnode; 491178110Sjeff VI_LOCK(vp); 492178110Sjeff dh = ip->i_dirhash; 493178110Sjeff ip->i_dirhash = NULL; 494178110Sjeff VI_UNLOCK(vp); 495183080Sjhb 496178110Sjeff /* 497183280Sjhb * Remove the hash from the list since we are going to free its 498183280Sjhb * memory. 499183280Sjhb */ 500183280Sjhb DIRHASHLIST_LOCK(); 501183280Sjhb if (dh->dh_onlist) 502183280Sjhb TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 503183280Sjhb ufs_dirhashmem -= dh->dh_memreq; 504183280Sjhb DIRHASHLIST_UNLOCK(); 505183280Sjhb 506183280Sjhb /* 507183080Sjhb * At this point, any waiters for the lock should hold their 508183080Sjhb * own reference on the dirhash structure. They will drop 509183080Sjhb * that reference once they grab the vnode interlock and see 510183080Sjhb * that ip->i_dirhash is NULL. 511178110Sjeff */ 512183080Sjhb sx_xunlock(&dh->dh_lock); 513183080Sjhb 514178110Sjeff /* 515178110Sjeff * Handle partially recycled as well as fully constructed hashes. 516178110Sjeff */ 517178110Sjeff if (dh->dh_hash != NULL) { 518178110Sjeff for (i = 0; i < dh->dh_narrays; i++) 519178110Sjeff if (dh->dh_hash[i] != NULL) 520178110Sjeff DIRHASH_BLKFREE(dh->dh_hash[i]); 521184205Sdes free(dh->dh_hash, M_DIRHASH); 522178110Sjeff if (dh->dh_blkfree != NULL) 523184205Sdes free(dh->dh_blkfree, M_DIRHASH); 524178110Sjeff } 525183080Sjhb 526178110Sjeff /* 527183080Sjhb * Drop the inode's reference to the data structure. 528178110Sjeff */ 529183080Sjhb ufsdirhash_drop(dh); 53079561Siedowse} 53179561Siedowse 53279561Siedowse/* 53379561Siedowse * Find the offset of the specified name within the given inode. 53479561Siedowse * Returns 0 on success, ENOENT if the entry does not exist, or 53579561Siedowse * EJUSTRETURN if the caller should revert to a linear search. 53679561Siedowse * 53779690Siedowse * If successful, the directory offset is stored in *offp, and a 53879690Siedowse * pointer to a struct buf containing the entry is stored in *bpp. If 53979561Siedowse * prevoffp is non-NULL, the offset of the previous entry within 54079561Siedowse * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry 54179561Siedowse * is the first in a block, the start of the block is used). 542178110Sjeff * 543178110Sjeff * Must be called with the hash locked. Returns with the hash unlocked. 54479561Siedowse */ 54579561Siedowseint 54679561Siedowseufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp, 54779690Siedowse struct buf **bpp, doff_t *prevoffp) 54879561Siedowse{ 54979561Siedowse struct dirhash *dh, *dh_next; 55079561Siedowse struct direct *dp; 55179561Siedowse struct vnode *vp; 55279561Siedowse struct buf *bp; 553219384Sjhb doff_t blkoff, bmask, offset, prevoff, seqoff; 55479561Siedowse int i, slot; 555178110Sjeff int error; 55679561Siedowse 557178110Sjeff dh = ip->i_dirhash; 558178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 559178110Sjeff ("ufsdirhash_lookup: Invalid dirhash %p\n", dh)); 560178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 56179561Siedowse /* 56279561Siedowse * Move this dirhash towards the end of the list if it has a 563178110Sjeff * score higher than the next entry, and acquire the dh_lock. 56479561Siedowse */ 565178110Sjeff DIRHASHLIST_LOCK(); 56679561Siedowse if (TAILQ_NEXT(dh, dh_list) != NULL) { 56779561Siedowse /* 56879561Siedowse * If the new score will be greater than that of the next 56979561Siedowse * entry, then move this entry past it. With both mutexes 57079561Siedowse * held, dh_next won't go away, but its dh_score could 57179561Siedowse * change; that's not important since it is just a hint. 57279561Siedowse */ 573178110Sjeff if ((dh_next = TAILQ_NEXT(dh, dh_list)) != NULL && 57479561Siedowse dh->dh_score >= dh_next->dh_score) { 57579561Siedowse KASSERT(dh->dh_onlist, ("dirhash: not on list")); 57679561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 57779561Siedowse TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh, 57879561Siedowse dh_list); 57979561Siedowse } 58079561Siedowse } 58179561Siedowse /* Update the score. */ 58279561Siedowse if (dh->dh_score < DH_SCOREMAX) 58379561Siedowse dh->dh_score++; 584193375Ssnb 585193375Ssnb /* Update last used time. */ 586193375Ssnb dh->dh_lastused = time_second; 587178110Sjeff DIRHASHLIST_UNLOCK(); 58879561Siedowse 58979561Siedowse vp = ip->i_vnode; 59079561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 59179561Siedowse blkoff = -1; 59279561Siedowse bp = NULL; 593219384Sjhb seqoff = dh->dh_seqoff; 59479561Siedowserestart: 59579561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 59679561Siedowse 597219384Sjhb if (seqoff != -1) { 59879561Siedowse /* 599219384Sjhb * Sequential access optimisation. seqoff contains the 60079561Siedowse * offset of the directory entry immediately following 60179561Siedowse * the last entry that was looked up. Check if this offset 60279561Siedowse * appears in the hash chain for the name we are looking for. 60379561Siedowse */ 60479561Siedowse for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY; 60579561Siedowse i = WRAPINCR(i, dh->dh_hlen)) 606219384Sjhb if (offset == seqoff) 60779561Siedowse break; 608219384Sjhb if (offset == seqoff) { 60979561Siedowse /* 61079561Siedowse * We found an entry with the expected offset. This 61179561Siedowse * is probably the entry we want, but if not, the 612219384Sjhb * code below will retry. 61379561Siedowse */ 61479561Siedowse slot = i; 615219384Sjhb } else 616219384Sjhb seqoff = -1; 61779561Siedowse } 61879561Siedowse 61979561Siedowse for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY; 62079561Siedowse slot = WRAPINCR(slot, dh->dh_hlen)) { 62179561Siedowse if (offset == DIRHASH_DEL) 62279561Siedowse continue; 62379561Siedowse if (offset < 0 || offset >= ip->i_size) 62479561Siedowse panic("ufsdirhash_lookup: bad offset in hash array"); 62579561Siedowse if ((offset & ~bmask) != blkoff) { 62679561Siedowse if (bp != NULL) 62779561Siedowse brelse(bp); 62879561Siedowse blkoff = offset & ~bmask; 629178110Sjeff if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) { 630178110Sjeff error = EJUSTRETURN; 631178110Sjeff goto fail; 632178110Sjeff } 63379561Siedowse } 634201717Smckusick KASSERT(bp != NULL, ("no buffer allocated")); 63579561Siedowse dp = (struct direct *)(bp->b_data + (offset & bmask)); 63679561Siedowse if (dp->d_reclen == 0 || dp->d_reclen > 63779561Siedowse DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) { 63879561Siedowse /* Corrupted directory. */ 639178110Sjeff error = EJUSTRETURN; 640178110Sjeff goto fail; 64179561Siedowse } 64279561Siedowse if (dp->d_namlen == namelen && 64379561Siedowse bcmp(dp->d_name, name, namelen) == 0) { 64479561Siedowse /* Found. Get the prev offset if needed. */ 64579561Siedowse if (prevoffp != NULL) { 64679561Siedowse if (offset & (DIRBLKSIZ - 1)) { 64779561Siedowse prevoff = ufsdirhash_getprev(dp, 64879561Siedowse offset); 64979561Siedowse if (prevoff == -1) { 650178110Sjeff error = EJUSTRETURN; 651178110Sjeff goto fail; 65279561Siedowse } 65379561Siedowse } else 65479561Siedowse prevoff = offset; 65579561Siedowse *prevoffp = prevoff; 65679561Siedowse } 65779561Siedowse 658219384Sjhb /* Update offset. */ 65979561Siedowse dh->dh_seqoff = offset + DIRSIZ(0, dp); 66079690Siedowse *bpp = bp; 66179561Siedowse *offp = offset; 662178110Sjeff ufsdirhash_release(dh); 66379561Siedowse return (0); 66479561Siedowse } 66579561Siedowse 66679561Siedowse /* 667219384Sjhb * When the name doesn't match in the sequential 668219384Sjhb * optimization case, go back and search normally. 66979561Siedowse */ 670219384Sjhb if (seqoff != -1) { 671219384Sjhb seqoff = -1; 67279561Siedowse goto restart; 67379561Siedowse } 67479561Siedowse } 675178110Sjeff error = ENOENT; 676178110Sjefffail: 677178110Sjeff ufsdirhash_release(dh); 67879561Siedowse if (bp != NULL) 67979561Siedowse brelse(bp); 680178110Sjeff return (error); 68179561Siedowse} 68279561Siedowse 68379561Siedowse/* 68479561Siedowse * Find a directory block with room for 'slotneeded' bytes. Returns 68579561Siedowse * the offset of the directory entry that begins the free space. 68679561Siedowse * This will either be the offset of an existing entry that has free 68779561Siedowse * space at the end, or the offset of an entry with d_ino == 0 at 68879561Siedowse * the start of a DIRBLKSIZ block. 68979561Siedowse * 69079561Siedowse * To use the space, the caller may need to compact existing entries in 69179561Siedowse * the directory. The total number of bytes in all of the entries involved 69279561Siedowse * in the compaction is stored in *slotsize. In other words, all of 69379561Siedowse * the entries that must be compacted are exactly contained in the 69479561Siedowse * region beginning at the returned offset and spanning *slotsize bytes. 69579561Siedowse * 69679561Siedowse * Returns -1 if no space was found, indicating that the directory 69779561Siedowse * must be extended. 69879561Siedowse */ 69979561Siedowsedoff_t 70079561Siedowseufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize) 70179561Siedowse{ 70279561Siedowse struct direct *dp; 70379561Siedowse struct dirhash *dh; 70479561Siedowse struct buf *bp; 70579561Siedowse doff_t pos, slotstart; 70679561Siedowse int dirblock, error, freebytes, i; 70779561Siedowse 708178110Sjeff dh = ip->i_dirhash; 709178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 710178110Sjeff ("ufsdirhash_findfree: Invalid dirhash %p\n", dh)); 711178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 71279561Siedowse 71379561Siedowse /* Find a directory block with the desired free space. */ 71479561Siedowse dirblock = -1; 71579561Siedowse for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++) 71679561Siedowse if ((dirblock = dh->dh_firstfree[i]) != -1) 71779561Siedowse break; 718178110Sjeff if (dirblock == -1) 71979561Siedowse return (-1); 72079561Siedowse 72179561Siedowse KASSERT(dirblock < dh->dh_nblk && 72279561Siedowse dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN), 72379561Siedowse ("ufsdirhash_findfree: bad stats")); 72479561Siedowse pos = dirblock * DIRBLKSIZ; 72579561Siedowse error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp); 72679561Siedowse if (error) 72779561Siedowse return (-1); 72879561Siedowse 72979561Siedowse /* Find the first entry with free space. */ 73079561Siedowse for (i = 0; i < DIRBLKSIZ; ) { 73179561Siedowse if (dp->d_reclen == 0) { 73279561Siedowse brelse(bp); 73379561Siedowse return (-1); 73479561Siedowse } 73579561Siedowse if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp)) 73679561Siedowse break; 73779561Siedowse i += dp->d_reclen; 73879561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 73979561Siedowse } 74079561Siedowse if (i > DIRBLKSIZ) { 74179561Siedowse brelse(bp); 74279561Siedowse return (-1); 74379561Siedowse } 74479561Siedowse slotstart = pos + i; 74579561Siedowse 74679561Siedowse /* Find the range of entries needed to get enough space */ 74779561Siedowse freebytes = 0; 74879561Siedowse while (i < DIRBLKSIZ && freebytes < slotneeded) { 74979561Siedowse freebytes += dp->d_reclen; 75079561Siedowse if (dp->d_ino != 0) 75179561Siedowse freebytes -= DIRSIZ(0, dp); 75279561Siedowse if (dp->d_reclen == 0) { 75379561Siedowse brelse(bp); 75479561Siedowse return (-1); 75579561Siedowse } 75679561Siedowse i += dp->d_reclen; 75779561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 75879561Siedowse } 75979561Siedowse if (i > DIRBLKSIZ) { 76079561Siedowse brelse(bp); 76179561Siedowse return (-1); 76279561Siedowse } 76379561Siedowse if (freebytes < slotneeded) 76479561Siedowse panic("ufsdirhash_findfree: free mismatch"); 76579561Siedowse brelse(bp); 76679561Siedowse *slotsize = pos + i - slotstart; 76779561Siedowse return (slotstart); 76879561Siedowse} 76979561Siedowse 77079561Siedowse/* 77179561Siedowse * Return the start of the unused space at the end of a directory, or 77279561Siedowse * -1 if there are no trailing unused blocks. 77379561Siedowse */ 77479561Siedowsedoff_t 77579561Siedowseufsdirhash_enduseful(struct inode *ip) 77679561Siedowse{ 77779561Siedowse 77879561Siedowse struct dirhash *dh; 77979561Siedowse int i; 78079561Siedowse 781178110Sjeff dh = ip->i_dirhash; 782178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 783178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 784178110Sjeff ("ufsdirhash_enduseful: Invalid dirhash %p\n", dh)); 78579561Siedowse 786178110Sjeff if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN) 78779561Siedowse return (-1); 78879561Siedowse 78979561Siedowse for (i = dh->dh_dirblks - 1; i >= 0; i--) 79079561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 79179561Siedowse break; 792178110Sjeff 79379561Siedowse return ((doff_t)(i + 1) * DIRBLKSIZ); 79479561Siedowse} 79579561Siedowse 79679561Siedowse/* 79779561Siedowse * Insert information into the hash about a new directory entry. dirp 79879561Siedowse * points to a struct direct containing the entry, and offset specifies 79979561Siedowse * the offset of this entry. 80079561Siedowse */ 80179561Siedowsevoid 80279561Siedowseufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset) 80379561Siedowse{ 80479561Siedowse struct dirhash *dh; 80579561Siedowse int slot; 80679561Siedowse 807178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 80879561Siedowse return; 809178110Sjeff 81079561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 81179561Siedowse ("ufsdirhash_add: bad offset")); 81279561Siedowse /* 81379561Siedowse * Normal hash usage is < 66%. If the usage gets too high then 81479561Siedowse * remove the hash entirely and let it be rebuilt later. 81579561Siedowse */ 81679561Siedowse if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) { 817178110Sjeff ufsdirhash_free_locked(ip); 81879561Siedowse return; 81979561Siedowse } 82079561Siedowse 82179561Siedowse /* Find a free hash slot (empty or deleted), and add the entry. */ 82279561Siedowse slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen); 82379561Siedowse while (DH_ENTRY(dh, slot) >= 0) 82479561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 82579561Siedowse if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY) 82679561Siedowse dh->dh_hused++; 82779561Siedowse DH_ENTRY(dh, slot) = offset; 82879561Siedowse 829193375Ssnb /* Update last used time. */ 830193375Ssnb dh->dh_lastused = time_second; 831193375Ssnb 83279561Siedowse /* Update the per-block summary info. */ 83379561Siedowse ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp)); 834178110Sjeff ufsdirhash_release(dh); 83579561Siedowse} 83679561Siedowse 83779561Siedowse/* 83879561Siedowse * Remove the specified directory entry from the hash. The entry to remove 83979561Siedowse * is defined by the name in `dirp', which must exist at the specified 84079561Siedowse * `offset' within the directory. 84179561Siedowse */ 84279561Siedowsevoid 84379561Siedowseufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset) 84479561Siedowse{ 84579561Siedowse struct dirhash *dh; 84679561Siedowse int slot; 84779561Siedowse 848178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 84979561Siedowse return; 85079561Siedowse 85179561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 85279561Siedowse ("ufsdirhash_remove: bad offset")); 85379561Siedowse /* Find the entry */ 85479561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset); 85579561Siedowse 85679561Siedowse /* Remove the hash entry. */ 85779561Siedowse ufsdirhash_delslot(dh, slot); 85879561Siedowse 85979561Siedowse /* Update the per-block summary info. */ 86079561Siedowse ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp)); 861178110Sjeff ufsdirhash_release(dh); 86279561Siedowse} 86379561Siedowse 86479561Siedowse/* 86579561Siedowse * Change the offset associated with a directory entry in the hash. Used 86679561Siedowse * when compacting directory blocks. 86779561Siedowse */ 86879561Siedowsevoid 86979561Siedowseufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff, 87079561Siedowse doff_t newoff) 87179561Siedowse{ 87279561Siedowse struct dirhash *dh; 87379561Siedowse int slot; 87479561Siedowse 875178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 87679561Siedowse return; 87779561Siedowse 87879561Siedowse KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ && 87979561Siedowse newoff < dh->dh_dirblks * DIRBLKSIZ, 88079561Siedowse ("ufsdirhash_move: bad offset")); 88179561Siedowse /* Find the entry, and update the offset. */ 88279561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff); 88379561Siedowse DH_ENTRY(dh, slot) = newoff; 884178110Sjeff ufsdirhash_release(dh); 88579561Siedowse} 88679561Siedowse 88779561Siedowse/* 88879561Siedowse * Inform dirhash that the directory has grown by one block that 88979561Siedowse * begins at offset (i.e. the new length is offset + DIRBLKSIZ). 89079561Siedowse */ 89179561Siedowsevoid 89279561Siedowseufsdirhash_newblk(struct inode *ip, doff_t offset) 89379561Siedowse{ 89479561Siedowse struct dirhash *dh; 89579561Siedowse int block; 89679561Siedowse 897178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 89879561Siedowse return; 89979561Siedowse 90079561Siedowse KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ, 90179561Siedowse ("ufsdirhash_newblk: bad offset")); 90279561Siedowse block = offset / DIRBLKSIZ; 90379561Siedowse if (block >= dh->dh_nblk) { 90479561Siedowse /* Out of space; must rebuild. */ 905178110Sjeff ufsdirhash_free_locked(ip); 90679561Siedowse return; 90779561Siedowse } 90879561Siedowse dh->dh_dirblks = block + 1; 90979561Siedowse 91079561Siedowse /* Account for the new free block. */ 91179561Siedowse dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN; 91279561Siedowse if (dh->dh_firstfree[DH_NFSTATS] == -1) 91379561Siedowse dh->dh_firstfree[DH_NFSTATS] = block; 914178110Sjeff ufsdirhash_release(dh); 91579561Siedowse} 91679561Siedowse 91779561Siedowse/* 91879561Siedowse * Inform dirhash that the directory is being truncated. 91979561Siedowse */ 92079561Siedowsevoid 92179561Siedowseufsdirhash_dirtrunc(struct inode *ip, doff_t offset) 92279561Siedowse{ 92379561Siedowse struct dirhash *dh; 92479561Siedowse int block, i; 92579561Siedowse 926178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 92779561Siedowse return; 92879561Siedowse 92979561Siedowse KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ, 93079561Siedowse ("ufsdirhash_dirtrunc: bad offset")); 93179561Siedowse block = howmany(offset, DIRBLKSIZ); 93279561Siedowse /* 93379561Siedowse * If the directory shrinks to less than 1/8 of dh_nblk blocks 93479561Siedowse * (about 20% of its original size due to the 50% extra added in 93579561Siedowse * ufsdirhash_build) then free it, and let the caller rebuild 93679561Siedowse * if necessary. 93779561Siedowse */ 93879561Siedowse if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) { 939178110Sjeff ufsdirhash_free_locked(ip); 94079561Siedowse return; 94179561Siedowse } 94279561Siedowse 94379561Siedowse /* 94479561Siedowse * Remove any `first free' information pertaining to the 94579561Siedowse * truncated blocks. All blocks we're removing should be 94679561Siedowse * completely unused. 94779561Siedowse */ 94879561Siedowse if (dh->dh_firstfree[DH_NFSTATS] >= block) 94979561Siedowse dh->dh_firstfree[DH_NFSTATS] = -1; 95079561Siedowse for (i = block; i < dh->dh_dirblks; i++) 95179561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 95279561Siedowse panic("ufsdirhash_dirtrunc: blocks in use"); 95379561Siedowse for (i = 0; i < DH_NFSTATS; i++) 95479561Siedowse if (dh->dh_firstfree[i] >= block) 95579561Siedowse panic("ufsdirhash_dirtrunc: first free corrupt"); 95679561Siedowse dh->dh_dirblks = block; 957178110Sjeff ufsdirhash_release(dh); 95879561Siedowse} 95979561Siedowse 96079561Siedowse/* 96179561Siedowse * Debugging function to check that the dirhash information about 96279561Siedowse * a directory block matches its actual contents. Panics if a mismatch 96379561Siedowse * is detected. 96479561Siedowse * 96579561Siedowse * On entry, `buf' should point to the start of an in-core 96679561Siedowse * DIRBLKSIZ-sized directory block, and `offset' should contain the 96779561Siedowse * offset from the start of the directory of that block. 96879561Siedowse */ 96979561Siedowsevoid 97079561Siedowseufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset) 97179561Siedowse{ 97279561Siedowse struct dirhash *dh; 97379561Siedowse struct direct *dp; 97479561Siedowse int block, ffslot, i, nfree; 97579561Siedowse 97679561Siedowse if (!ufs_dirhashcheck) 97779561Siedowse return; 978178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 97979561Siedowse return; 98079561Siedowse 98179561Siedowse block = offset / DIRBLKSIZ; 98279561Siedowse if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks) 98379561Siedowse panic("ufsdirhash_checkblock: bad offset"); 98479561Siedowse 98579561Siedowse nfree = 0; 98679561Siedowse for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) { 98779561Siedowse dp = (struct direct *)(buf + i); 98879561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ) 98979561Siedowse panic("ufsdirhash_checkblock: bad dir"); 99079561Siedowse 99179561Siedowse if (dp->d_ino == 0) { 99280456Siedowse#if 0 99380456Siedowse /* 99480456Siedowse * XXX entries with d_ino == 0 should only occur 99580456Siedowse * at the start of a DIRBLKSIZ block. However the 99680456Siedowse * ufs code is tolerant of such entries at other 99780456Siedowse * offsets, and fsck does not fix them. 99880456Siedowse */ 99979561Siedowse if (i != 0) 100079561Siedowse panic("ufsdirhash_checkblock: bad dir inode"); 100180456Siedowse#endif 100279561Siedowse nfree += dp->d_reclen; 100379561Siedowse continue; 100479561Siedowse } 100579561Siedowse 100679561Siedowse /* Check that the entry exists (will panic if it doesn't). */ 100779561Siedowse ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i); 100879561Siedowse 100979561Siedowse nfree += dp->d_reclen - DIRSIZ(0, dp); 101079561Siedowse } 101179561Siedowse if (i != DIRBLKSIZ) 101279561Siedowse panic("ufsdirhash_checkblock: bad dir end"); 101379561Siedowse 101479561Siedowse if (dh->dh_blkfree[block] * DIRALIGN != nfree) 101579561Siedowse panic("ufsdirhash_checkblock: bad free count"); 101679561Siedowse 101792098Siedowse ffslot = BLKFREE2IDX(nfree / DIRALIGN); 101879561Siedowse for (i = 0; i <= DH_NFSTATS; i++) 101979561Siedowse if (dh->dh_firstfree[i] == block && i != ffslot) 102079561Siedowse panic("ufsdirhash_checkblock: bad first-free"); 102192098Siedowse if (dh->dh_firstfree[ffslot] == -1) 102292098Siedowse panic("ufsdirhash_checkblock: missing first-free entry"); 1023178110Sjeff ufsdirhash_release(dh); 102479561Siedowse} 102579561Siedowse 102679561Siedowse/* 102779561Siedowse * Hash the specified filename into a dirhash slot. 102879561Siedowse */ 102979561Siedowsestatic int 103079561Siedowseufsdirhash_hash(struct dirhash *dh, char *name, int namelen) 103179561Siedowse{ 103292807Sdwmalone u_int32_t hash; 103392807Sdwmalone 103492807Sdwmalone /* 1035107868Siedowse * We hash the name and then some other bit of data that is 1036107868Siedowse * invariant over the dirhash's lifetime. Otherwise names 103792807Sdwmalone * differing only in the last byte are placed close to one 103892807Sdwmalone * another in the table, which is bad for linear probing. 103992807Sdwmalone */ 104092807Sdwmalone hash = fnv_32_buf(name, namelen, FNV1_32_INIT); 1041133837Sdwmalone hash = fnv_32_buf(&dh, sizeof(dh), hash); 104292807Sdwmalone return (hash % dh->dh_hlen); 104379561Siedowse} 104479561Siedowse 104579561Siedowse/* 104679561Siedowse * Adjust the number of free bytes in the block containing `offset' 104779561Siedowse * by the value specified by `diff'. 104879561Siedowse * 104979561Siedowse * The caller must ensure we have exclusive access to `dh'; normally 1050178110Sjeff * that means that dh_lock should be held, but this is also called 105179561Siedowse * from ufsdirhash_build() where exclusive access can be assumed. 105279561Siedowse */ 105379561Siedowsestatic void 105479561Siedowseufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff) 105579561Siedowse{ 105679561Siedowse int block, i, nfidx, ofidx; 105779561Siedowse 105879561Siedowse /* Update the per-block summary info. */ 105979561Siedowse block = offset / DIRBLKSIZ; 106079561Siedowse KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks, 106179561Siedowse ("dirhash bad offset")); 106292098Siedowse ofidx = BLKFREE2IDX(dh->dh_blkfree[block]); 106379561Siedowse dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN); 106492098Siedowse nfidx = BLKFREE2IDX(dh->dh_blkfree[block]); 106579561Siedowse 106679561Siedowse /* Update the `first free' list if necessary. */ 106779561Siedowse if (ofidx != nfidx) { 106879561Siedowse /* If removing, scan forward for the next block. */ 106979561Siedowse if (dh->dh_firstfree[ofidx] == block) { 107079561Siedowse for (i = block + 1; i < dh->dh_dirblks; i++) 107192098Siedowse if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx) 107279561Siedowse break; 107379561Siedowse dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1; 107479561Siedowse } 107579561Siedowse 107679561Siedowse /* Make this the new `first free' if necessary */ 107779561Siedowse if (dh->dh_firstfree[nfidx] > block || 107879561Siedowse dh->dh_firstfree[nfidx] == -1) 107979561Siedowse dh->dh_firstfree[nfidx] = block; 108079561Siedowse } 108179561Siedowse} 108279561Siedowse 108379561Siedowse/* 108479561Siedowse * Find the specified name which should have the specified offset. 108579561Siedowse * Returns a slot number, and panics on failure. 108679561Siedowse * 108779561Siedowse * `dh' must be locked on entry and remains so on return. 108879561Siedowse */ 108979561Siedowsestatic int 109079561Siedowseufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset) 109179561Siedowse{ 109279561Siedowse int slot; 109379561Siedowse 1094178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 109579561Siedowse 109679561Siedowse /* Find the entry. */ 109779561Siedowse KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full")); 109879561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 109979561Siedowse while (DH_ENTRY(dh, slot) != offset && 110079561Siedowse DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 110179561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 110279561Siedowse if (DH_ENTRY(dh, slot) != offset) 110379561Siedowse panic("ufsdirhash_findslot: '%.*s' not found", namelen, name); 110479561Siedowse 110579561Siedowse return (slot); 110679561Siedowse} 110779561Siedowse 110879561Siedowse/* 110979561Siedowse * Remove the entry corresponding to the specified slot from the hash array. 111079561Siedowse * 111179561Siedowse * `dh' must be locked on entry and remains so on return. 111279561Siedowse */ 111379561Siedowsestatic void 111479561Siedowseufsdirhash_delslot(struct dirhash *dh, int slot) 111579561Siedowse{ 111679561Siedowse int i; 111779561Siedowse 1118178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 111979561Siedowse 112079561Siedowse /* Mark the entry as deleted. */ 112179561Siedowse DH_ENTRY(dh, slot) = DIRHASH_DEL; 112279561Siedowse 112379561Siedowse /* If this is the end of a chain of DIRHASH_DEL slots, remove them. */ 112479561Siedowse for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; ) 112579561Siedowse i = WRAPINCR(i, dh->dh_hlen); 112679561Siedowse if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) { 112792807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 112892807Sdwmalone while (DH_ENTRY(dh, i) == DIRHASH_DEL) { 112979561Siedowse DH_ENTRY(dh, i) = DIRHASH_EMPTY; 113079561Siedowse dh->dh_hused--; 113192807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 113279561Siedowse } 113379561Siedowse KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen")); 113479561Siedowse } 113579561Siedowse} 113679561Siedowse 113779561Siedowse/* 113879561Siedowse * Given a directory entry and its offset, find the offset of the 113979561Siedowse * previous entry in the same DIRBLKSIZ-sized block. Returns an 114079561Siedowse * offset, or -1 if there is no previous entry in the block or some 114179561Siedowse * other problem occurred. 114279561Siedowse */ 114379561Siedowsestatic doff_t 114479561Siedowseufsdirhash_getprev(struct direct *dirp, doff_t offset) 114579561Siedowse{ 114679561Siedowse struct direct *dp; 114779561Siedowse char *blkbuf; 114879561Siedowse doff_t blkoff, prevoff; 114979561Siedowse int entrypos, i; 115079561Siedowse 115179561Siedowse blkoff = offset & ~(DIRBLKSIZ - 1); /* offset of start of block */ 115279561Siedowse entrypos = offset & (DIRBLKSIZ - 1); /* entry relative to block */ 115379561Siedowse blkbuf = (char *)dirp - entrypos; 115479561Siedowse prevoff = blkoff; 115579561Siedowse 115679561Siedowse /* If `offset' is the start of a block, there is no previous entry. */ 115779561Siedowse if (entrypos == 0) 115879561Siedowse return (-1); 115979561Siedowse 116079561Siedowse /* Scan from the start of the block until we get to the entry. */ 116179561Siedowse for (i = 0; i < entrypos; i += dp->d_reclen) { 116279561Siedowse dp = (struct direct *)(blkbuf + i); 116379561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos) 116479561Siedowse return (-1); /* Corrupted directory. */ 116579561Siedowse prevoff = blkoff + i; 116679561Siedowse } 116779561Siedowse return (prevoff); 116879561Siedowse} 116979561Siedowse 117079561Siedowse/* 1171193375Ssnb * Delete the given dirhash and reclaim its memory. Assumes that 1172193375Ssnb * ufsdirhash_list is locked, and leaves it locked. Also assumes 1173193375Ssnb * that dh is locked. Returns the amount of memory freed. 1174193375Ssnb */ 1175193375Ssnbstatic int 1176193375Ssnbufsdirhash_destroy(struct dirhash *dh) 1177193375Ssnb{ 1178193375Ssnb doff_t **hash; 1179193375Ssnb u_int8_t *blkfree; 1180193375Ssnb int i, mem, narrays; 1181193375Ssnb 1182193375Ssnb KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list")); 1183193375Ssnb 1184193375Ssnb /* Remove it from the list and detach its memory. */ 1185193375Ssnb TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 1186193375Ssnb dh->dh_onlist = 0; 1187193375Ssnb hash = dh->dh_hash; 1188193375Ssnb dh->dh_hash = NULL; 1189193375Ssnb blkfree = dh->dh_blkfree; 1190193375Ssnb dh->dh_blkfree = NULL; 1191193375Ssnb narrays = dh->dh_narrays; 1192193375Ssnb mem = dh->dh_memreq; 1193193375Ssnb dh->dh_memreq = 0; 1194193375Ssnb 1195194387Ssnb /* Unlock dirhash and free the detached memory. */ 1196193375Ssnb ufsdirhash_release(dh); 1197193375Ssnb for (i = 0; i < narrays; i++) 1198193375Ssnb DIRHASH_BLKFREE(hash[i]); 1199193375Ssnb free(hash, M_DIRHASH); 1200193375Ssnb free(blkfree, M_DIRHASH); 1201193375Ssnb 1202193375Ssnb /* Account for the returned memory. */ 1203193375Ssnb ufs_dirhashmem -= mem; 1204193375Ssnb 1205193375Ssnb return (mem); 1206193375Ssnb} 1207193375Ssnb 1208193375Ssnb/* 120979561Siedowse * Try to free up `wanted' bytes by stealing memory from existing 1210125854Sdwmalone * dirhashes. Returns zero with list locked if successful. 121179561Siedowse */ 121279561Siedowsestatic int 121379561Siedowseufsdirhash_recycle(int wanted) 121479561Siedowse{ 121579561Siedowse struct dirhash *dh; 121679561Siedowse 1217125854Sdwmalone DIRHASHLIST_LOCK(); 1218178110Sjeff dh = TAILQ_FIRST(&ufsdirhash_list); 121979561Siedowse while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) { 1220178110Sjeff /* Decrement the score; only recycle if it becomes zero. */ 1221178110Sjeff if (dh == NULL || --dh->dh_score > 0) { 1222125854Sdwmalone DIRHASHLIST_UNLOCK(); 122379561Siedowse return (-1); 122479561Siedowse } 1225178110Sjeff /* 1226178110Sjeff * If we can't lock it it's in use and we don't want to 1227178110Sjeff * recycle it anyway. 1228178110Sjeff */ 1229183080Sjhb if (!sx_try_xlock(&dh->dh_lock)) { 1230178110Sjeff dh = TAILQ_NEXT(dh, dh_list); 1231178110Sjeff continue; 1232178110Sjeff } 123379561Siedowse 1234193375Ssnb ufsdirhash_destroy(dh); 123579561Siedowse 1236193375Ssnb /* Repeat if necessary. */ 1237178110Sjeff dh = TAILQ_FIRST(&ufsdirhash_list); 123879561Siedowse } 1239125854Sdwmalone /* Success; return with list locked. */ 124079561Siedowse return (0); 124179561Siedowse} 124279561Siedowse 1243193375Ssnb/* 1244193375Ssnb * Callback that frees some dirhashes when the system is low on virtual memory. 1245193375Ssnb */ 1246193375Ssnbstatic void 1247193375Ssnbufsdirhash_lowmem() 1248193375Ssnb{ 1249194387Ssnb struct dirhash *dh, *dh_temp; 1250193375Ssnb int memfreed = 0; 1251193375Ssnb /* XXX: this 10% may need to be adjusted */ 1252193375Ssnb int memwanted = ufs_dirhashmem / 10; 125379561Siedowse 1254193375Ssnb ufs_dirhashlowmemcount++; 1255193375Ssnb 1256193375Ssnb DIRHASHLIST_LOCK(); 1257193375Ssnb /* 1258193375Ssnb * Delete dirhashes not used for more than ufs_dirhashreclaimage 1259193375Ssnb * seconds. If we can't get a lock on the dirhash, it will be skipped. 1260193375Ssnb */ 1261194387Ssnb TAILQ_FOREACH_SAFE(dh, &ufsdirhash_list, dh_list, dh_temp) { 1262193375Ssnb if (!sx_try_xlock(&dh->dh_lock)) 1263193375Ssnb continue; 1264193375Ssnb if (time_second - dh->dh_lastused > ufs_dirhashreclaimage) 1265193375Ssnb memfreed += ufsdirhash_destroy(dh); 1266193375Ssnb /* Unlock if we didn't delete the dirhash */ 1267193375Ssnb else 1268193375Ssnb ufsdirhash_release(dh); 1269193375Ssnb } 1270193375Ssnb 1271193375Ssnb /* 1272193375Ssnb * If not enough memory was freed, keep deleting hashes from the head 1273193375Ssnb * of the dirhash list. The ones closest to the head should be the 1274193375Ssnb * oldest. 1275193375Ssnb */ 1276194387Ssnb if (memfreed < memwanted) { 1277194387Ssnb TAILQ_FOREACH_SAFE(dh, &ufsdirhash_list, dh_list, dh_temp) { 1278194387Ssnb if (!sx_try_xlock(&dh->dh_lock)) 1279194387Ssnb continue; 1280194387Ssnb memfreed += ufsdirhash_destroy(dh); 1281194387Ssnb if (memfreed >= memwanted) 1282194387Ssnb break; 1283194387Ssnb } 1284193375Ssnb } 1285193375Ssnb DIRHASHLIST_UNLOCK(); 1286193375Ssnb} 1287193375Ssnb 1288193375Ssnb 128999101Siedowsevoid 129079561Siedowseufsdirhash_init() 129179561Siedowse{ 1292214359Sivoras ufs_dirhashmaxmem = lmax(roundup(hibufspace / 64, PAGE_SIZE), 1293214359Sivoras 2 * 1024 * 1024); 1294214359Sivoras 129596874Siedowse ufsdirhash_zone = uma_zcreate("DIRHASH", DH_NBLKOFF * sizeof(doff_t), 129692768Sjeff NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 129793818Sjhb mtx_init(&ufsdirhash_mtx, "dirhash list", NULL, MTX_DEF); 129879561Siedowse TAILQ_INIT(&ufsdirhash_list); 1299193375Ssnb 1300193375Ssnb /* Register a callback function to handle low memory signals */ 1301193375Ssnb EVENTHANDLER_REGISTER(vm_lowmem, ufsdirhash_lowmem, NULL, 1302193375Ssnb EVENTHANDLER_PRI_FIRST); 130379561Siedowse} 130479561Siedowse 130599101Siedowsevoid 130699101Siedowseufsdirhash_uninit() 130799101Siedowse{ 130899101Siedowse KASSERT(TAILQ_EMPTY(&ufsdirhash_list), ("ufsdirhash_uninit")); 130999101Siedowse uma_zdestroy(ufsdirhash_zone); 131099101Siedowse mtx_destroy(&ufsdirhash_mtx); 131199101Siedowse} 131279561Siedowse 131379561Siedowse#endif /* UFS_DIRHASH */ 1314