ufs_dirhash.c revision 194387
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 194387 2009-06-17 18:55:29Z snb $"); 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 71141631Sphkstatic SYSCTL_NODE(_vfs, OID_AUTO, ufs, CTLFLAG_RD, 0, "UFS filesystem"); 7279561Siedowse 7379561Siedowsestatic int ufs_mindirhashsize = DIRBLKSIZ * 5; 7479561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_minsize, CTLFLAG_RW, 7579561Siedowse &ufs_mindirhashsize, 7679561Siedowse 0, "minimum directory size in bytes for which to use hashed lookup"); 7779561Siedowsestatic int ufs_dirhashmaxmem = 2 * 1024 * 1024; 7879561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_maxmem, CTLFLAG_RW, &ufs_dirhashmaxmem, 7979561Siedowse 0, "maximum allowed dirhash memory usage"); 8079561Siedowsestatic int ufs_dirhashmem; 8179561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_mem, CTLFLAG_RD, &ufs_dirhashmem, 8279561Siedowse 0, "current dirhash memory usage"); 8385512Siedowsestatic int ufs_dirhashcheck = 0; 8479561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_docheck, CTLFLAG_RW, &ufs_dirhashcheck, 8579561Siedowse 0, "enable extra sanity tests"); 86193375Ssnbstatic int ufs_dirhashlowmemcount = 0; 87193375SsnbSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_lowmemcount, CTLFLAG_RD, 88193375Ssnb &ufs_dirhashlowmemcount, 0, "number of times low memory hook called"); 89193375Ssnbstatic int ufs_dirhashreclaimage = 5; 90193375SsnbSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_reclaimage, CTLFLAG_RW, 91193375Ssnb &ufs_dirhashreclaimage, 0, 92193375Ssnb "max time in seconds of hash inactivity before deletion in low VM events"); 9379561Siedowse 9479561Siedowse 9579561Siedowsestatic int ufsdirhash_hash(struct dirhash *dh, char *name, int namelen); 9679561Siedowsestatic void ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff); 9779561Siedowsestatic void ufsdirhash_delslot(struct dirhash *dh, int slot); 9879561Siedowsestatic int ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, 9979561Siedowse doff_t offset); 10079561Siedowsestatic doff_t ufsdirhash_getprev(struct direct *dp, doff_t offset); 10179561Siedowsestatic int ufsdirhash_recycle(int wanted); 102193375Ssnbstatic void ufsdirhash_lowmem(void); 103178110Sjeffstatic void ufsdirhash_free_locked(struct inode *ip); 10479561Siedowse 10592768Sjeffstatic uma_zone_t ufsdirhash_zone; 10679561Siedowse 107125854Sdwmalone#define DIRHASHLIST_LOCK() mtx_lock(&ufsdirhash_mtx) 108125854Sdwmalone#define DIRHASHLIST_UNLOCK() mtx_unlock(&ufsdirhash_mtx) 109125854Sdwmalone#define DIRHASH_BLKALLOC_WAITOK() uma_zalloc(ufsdirhash_zone, M_WAITOK) 110125854Sdwmalone#define DIRHASH_BLKFREE(ptr) uma_zfree(ufsdirhash_zone, (ptr)) 111178110Sjeff#define DIRHASH_ASSERT_LOCKED(dh) \ 112183080Sjhb sx_assert(&(dh)->dh_lock, SA_LOCKED) 113125854Sdwmalone 11479561Siedowse/* Dirhash list; recently-used entries are near the tail. */ 11579561Siedowsestatic TAILQ_HEAD(, dirhash) ufsdirhash_list; 11679561Siedowse 11779561Siedowse/* Protects: ufsdirhash_list, `dh_list' field, ufs_dirhashmem. */ 11879561Siedowsestatic struct mtx ufsdirhash_mtx; 11979561Siedowse 12079561Siedowse/* 121178110Sjeff * Locking: 12279561Siedowse * 123178110Sjeff * The relationship between inode and dirhash is protected either by an 124178110Sjeff * exclusive vnode lock or the vnode interlock where a shared vnode lock 125183080Sjhb * may be used. The dirhash_mtx is acquired after the dirhash lock. To 126183080Sjhb * handle teardown races, code wishing to lock the dirhash for an inode 127183080Sjhb * when using a shared vnode lock must obtain a private reference on the 128183080Sjhb * dirhash while holding the vnode interlock. They can drop it once they 129183080Sjhb * have obtained the dirhash lock and verified that the dirhash wasn't 130183080Sjhb * recycled while they waited for the dirhash lock. 131178110Sjeff * 132178110Sjeff * ufsdirhash_build() acquires a shared lock on the dirhash when it is 133178110Sjeff * successful. This lock is released after a call to ufsdirhash_lookup(). 134178110Sjeff * 135178110Sjeff * Functions requiring exclusive access use ufsdirhash_acquire() which may 136178110Sjeff * free a dirhash structure that was recycled by ufsdirhash_recycle(). 137178110Sjeff * 138178110Sjeff * The dirhash lock may be held across io operations. 139187474Sjhb * 140187474Sjhb * WITNESS reports a lock order reversal between the "bufwait" lock 141187474Sjhb * and the "dirhash" lock. However, this specific reversal will not 142187474Sjhb * cause a deadlock. To get a deadlock, one would have to lock a 143187474Sjhb * buffer followed by the dirhash while a second thread locked a 144187474Sjhb * buffer while holding the dirhash lock. The second order can happen 145187474Sjhb * under a shared or exclusive vnode lock for the associated directory 146187474Sjhb * in lookup(). The first order, however, can only happen under an 147187474Sjhb * exclusive vnode lock (e.g. unlink(), rename(), etc.). Thus, for 148187474Sjhb * a thread to be doing a "bufwait" -> "dirhash" order, it has to hold 149187474Sjhb * an exclusive vnode lock. That exclusive vnode lock will prevent 150187474Sjhb * any other threads from doing a "dirhash" -> "bufwait" order. 15179561Siedowse */ 15279561Siedowse 153183080Sjhbstatic void 154183080Sjhbufsdirhash_hold(struct dirhash *dh) 155183080Sjhb{ 156183080Sjhb 157183080Sjhb refcount_acquire(&dh->dh_refcount); 158183080Sjhb} 159183080Sjhb 160183080Sjhbstatic void 161183080Sjhbufsdirhash_drop(struct dirhash *dh) 162183080Sjhb{ 163183080Sjhb 164183080Sjhb if (refcount_release(&dh->dh_refcount)) { 165183080Sjhb sx_destroy(&dh->dh_lock); 166183080Sjhb free(dh, M_DIRHASH); 167183080Sjhb } 168183080Sjhb} 169183080Sjhb 17079561Siedowse/* 171178110Sjeff * Release the lock on a dirhash. 172178110Sjeff */ 173178110Sjeffstatic void 174178110Sjeffufsdirhash_release(struct dirhash *dh) 175178110Sjeff{ 176178110Sjeff 177183080Sjhb sx_unlock(&dh->dh_lock); 178178110Sjeff} 179178110Sjeff 180178110Sjeff/* 181178110Sjeff * Either acquire an existing hash locked shared or create a new hash and 182178110Sjeff * return it exclusively locked. May return NULL if the allocation fails. 183178110Sjeff * 184178110Sjeff * The vnode interlock is used to protect the i_dirhash pointer from 185178110Sjeff * simultaneous access while only a shared vnode lock is held. 186178110Sjeff */ 187178110Sjeffstatic struct dirhash * 188178110Sjeffufsdirhash_create(struct inode *ip) 189178110Sjeff{ 190178110Sjeff struct dirhash *ndh; 191178110Sjeff struct dirhash *dh; 192178110Sjeff struct vnode *vp; 193178110Sjeff int error; 194178110Sjeff 195178110Sjeff error = 0; 196178110Sjeff ndh = dh = NULL; 197178110Sjeff vp = ip->i_vnode; 198178110Sjeff for (;;) { 199185102Sjhb /* Racy check for i_dirhash to prefetch a dirhash structure. */ 200178110Sjeff if (ip->i_dirhash == NULL && ndh == NULL) { 201184205Sdes ndh = malloc(sizeof *dh, M_DIRHASH, 202178110Sjeff M_NOWAIT | M_ZERO); 203178110Sjeff if (ndh == NULL) 204178110Sjeff return (NULL); 205183080Sjhb refcount_init(&ndh->dh_refcount, 1); 206184651Sjhb 207184651Sjhb /* 208184651Sjhb * The DUPOK is to prevent warnings from the 209184651Sjhb * sx_slock() a few lines down which is safe 210184651Sjhb * since the duplicate lock in that case is 211184651Sjhb * the one for this dirhash we are creating 212184651Sjhb * now which has no external references until 213184651Sjhb * after this function returns. 214184651Sjhb */ 215184651Sjhb sx_init_flags(&ndh->dh_lock, "dirhash", SX_DUPOK); 216183080Sjhb sx_xlock(&ndh->dh_lock); 217178110Sjeff } 218178110Sjeff /* 219178110Sjeff * Check i_dirhash. If it's NULL just try to use a 220178110Sjeff * preallocated structure. If none exists loop and try again. 221178110Sjeff */ 222178110Sjeff VI_LOCK(vp); 223178110Sjeff dh = ip->i_dirhash; 224178110Sjeff if (dh == NULL) { 225178110Sjeff ip->i_dirhash = ndh; 226178110Sjeff VI_UNLOCK(vp); 227178110Sjeff if (ndh == NULL) 228178110Sjeff continue; 229178110Sjeff return (ndh); 230178110Sjeff } 231183080Sjhb ufsdirhash_hold(dh); 232183080Sjhb VI_UNLOCK(vp); 233183080Sjhb 234183080Sjhb /* Acquire a shared lock on existing hashes. */ 235183080Sjhb sx_slock(&dh->dh_lock); 236183080Sjhb 237178110Sjeff /* The hash could've been recycled while we were waiting. */ 238183080Sjhb VI_LOCK(vp); 239178110Sjeff if (ip->i_dirhash != dh) { 240183080Sjhb VI_UNLOCK(vp); 241178110Sjeff ufsdirhash_release(dh); 242183080Sjhb ufsdirhash_drop(dh); 243178110Sjeff continue; 244178110Sjeff } 245183080Sjhb VI_UNLOCK(vp); 246183080Sjhb ufsdirhash_drop(dh); 247183080Sjhb 248178110Sjeff /* If the hash is still valid we've succeeded. */ 249178110Sjeff if (dh->dh_hash != NULL) 250178110Sjeff break; 251178110Sjeff /* 252178110Sjeff * If the hash is NULL it has been recycled. Try to upgrade 253183080Sjhb * so we can recreate it. If we fail the upgrade, drop our 254183080Sjhb * lock and try again. 255178110Sjeff */ 256183080Sjhb if (sx_try_upgrade(&dh->dh_lock)) 257178110Sjeff break; 258183080Sjhb sx_sunlock(&dh->dh_lock); 259178110Sjeff } 260178110Sjeff /* Free the preallocated structure if it was not necessary. */ 261178110Sjeff if (ndh) { 262183080Sjhb ufsdirhash_release(ndh); 263183080Sjhb ufsdirhash_drop(ndh); 264178110Sjeff } 265178110Sjeff return (dh); 266178110Sjeff} 267178110Sjeff 268178110Sjeff/* 269178110Sjeff * Acquire an exclusive lock on an existing hash. Requires an exclusive 270178110Sjeff * vnode lock to protect the i_dirhash pointer. hashes that have been 271178110Sjeff * recycled are reclaimed here and NULL is returned. 272178110Sjeff */ 273178110Sjeffstatic struct dirhash * 274178110Sjeffufsdirhash_acquire(struct inode *ip) 275178110Sjeff{ 276178110Sjeff struct dirhash *dh; 277178110Sjeff struct vnode *vp; 278178110Sjeff 279178110Sjeff ASSERT_VOP_ELOCKED(ip->i_vnode, __FUNCTION__); 280178110Sjeff 281178110Sjeff vp = ip->i_vnode; 282178110Sjeff dh = ip->i_dirhash; 283178110Sjeff if (dh == NULL) 284178110Sjeff return (NULL); 285183080Sjhb sx_xlock(&dh->dh_lock); 286178110Sjeff if (dh->dh_hash != NULL) 287178110Sjeff return (dh); 288178110Sjeff ufsdirhash_free_locked(ip); 289178110Sjeff return (NULL); 290178110Sjeff} 291178110Sjeff 292178110Sjeff/* 293178110Sjeff * Acquire exclusively and free the hash pointed to by ip. Works with a 294178110Sjeff * shared or exclusive vnode lock. 295178110Sjeff */ 296178110Sjeffvoid 297178110Sjeffufsdirhash_free(struct inode *ip) 298178110Sjeff{ 299178110Sjeff struct dirhash *dh; 300178110Sjeff struct vnode *vp; 301178110Sjeff 302178110Sjeff vp = ip->i_vnode; 303178110Sjeff for (;;) { 304183080Sjhb /* Grab a reference on this inode's dirhash if it has one. */ 305178110Sjeff VI_LOCK(vp); 306178110Sjeff dh = ip->i_dirhash; 307178110Sjeff if (dh == NULL) { 308178110Sjeff VI_UNLOCK(vp); 309178110Sjeff return; 310178110Sjeff } 311183080Sjhb ufsdirhash_hold(dh); 312183080Sjhb VI_UNLOCK(vp); 313183080Sjhb 314183080Sjhb /* Exclusively lock the dirhash. */ 315183080Sjhb sx_xlock(&dh->dh_lock); 316183080Sjhb 317183080Sjhb /* If this dirhash still belongs to this inode, then free it. */ 318183080Sjhb VI_LOCK(vp); 319183080Sjhb if (ip->i_dirhash == dh) { 320183080Sjhb VI_UNLOCK(vp); 321183080Sjhb ufsdirhash_drop(dh); 322178110Sjeff break; 323183080Sjhb } 324183080Sjhb VI_UNLOCK(vp); 325183080Sjhb 326183080Sjhb /* 327183080Sjhb * This inode's dirhash has changed while we were 328183080Sjhb * waiting for the dirhash lock, so try again. 329183080Sjhb */ 330178110Sjeff ufsdirhash_release(dh); 331183080Sjhb ufsdirhash_drop(dh); 332178110Sjeff } 333178110Sjeff ufsdirhash_free_locked(ip); 334178110Sjeff} 335178110Sjeff 336178110Sjeff/* 33779561Siedowse * Attempt to build up a hash table for the directory contents in 33879561Siedowse * inode 'ip'. Returns 0 on success, or -1 of the operation failed. 33979561Siedowse */ 34079561Siedowseint 34179561Siedowseufsdirhash_build(struct inode *ip) 34279561Siedowse{ 34379561Siedowse struct dirhash *dh; 34479561Siedowse struct buf *bp = NULL; 34579561Siedowse struct direct *ep; 34679561Siedowse struct vnode *vp; 34779561Siedowse doff_t bmask, pos; 34879561Siedowse int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot; 34979561Siedowse 350178110Sjeff /* Take care of a decreased sysctl value. */ 351178110Sjeff while (ufs_dirhashmem > ufs_dirhashmaxmem) 352178110Sjeff if (ufsdirhash_recycle(0) != 0) 353178110Sjeff return (-1); 354178110Sjeff 35579561Siedowse /* Check if we can/should use dirhash. */ 356178110Sjeff if (ip->i_size < ufs_mindirhashsize || OFSFMT(ip->i_vnode) || 357178110Sjeff ip->i_effnlink == 0) { 358178110Sjeff if (ip->i_dirhash) 35979561Siedowse ufsdirhash_free(ip); 360178110Sjeff return (-1); 36179561Siedowse } 362178110Sjeff dh = ufsdirhash_create(ip); 363178110Sjeff if (dh == NULL) 36482364Siedowse return (-1); 365178110Sjeff if (dh->dh_hash != NULL) 366178110Sjeff return (0); 36782364Siedowse 36879561Siedowse vp = ip->i_vnode; 36979561Siedowse /* Allocate 50% more entries than this dir size could ever need. */ 37079561Siedowse KASSERT(ip->i_size >= DIRBLKSIZ, ("ufsdirhash_build size")); 37179561Siedowse nslots = ip->i_size / DIRECTSIZ(1); 37279561Siedowse nslots = (nslots * 3 + 1) / 2; 37379561Siedowse narrays = howmany(nslots, DH_NBLKOFF); 37479561Siedowse nslots = narrays * DH_NBLKOFF; 37579561Siedowse dirblocks = howmany(ip->i_size, DIRBLKSIZ); 37679561Siedowse nblocks = (dirblocks * 3 + 1) / 2; 37779561Siedowse memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) + 37879561Siedowse narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 37979561Siedowse nblocks * sizeof(*dh->dh_blkfree); 380125854Sdwmalone DIRHASHLIST_LOCK(); 38179561Siedowse if (memreqd + ufs_dirhashmem > ufs_dirhashmaxmem) { 382125854Sdwmalone DIRHASHLIST_UNLOCK(); 38379561Siedowse if (memreqd > ufs_dirhashmaxmem / 2) 384178110Sjeff goto fail; 38579561Siedowse /* Try to free some space. */ 38679561Siedowse if (ufsdirhash_recycle(memreqd) != 0) 387178110Sjeff goto fail; 388125854Sdwmalone /* Enough was freed, and list has been locked. */ 38979561Siedowse } 39079561Siedowse ufs_dirhashmem += memreqd; 391125854Sdwmalone DIRHASHLIST_UNLOCK(); 39279561Siedowse 393178110Sjeff /* Initialise the hash table and block statistics. */ 394178110Sjeff dh->dh_memreq = memreqd; 395178110Sjeff dh->dh_narrays = narrays; 396178110Sjeff dh->dh_hlen = nslots; 397178110Sjeff dh->dh_nblk = nblocks; 398178110Sjeff dh->dh_dirblks = dirblocks; 399178110Sjeff for (i = 0; i < DH_NFSTATS; i++) 400178110Sjeff dh->dh_firstfree[i] = -1; 401178110Sjeff dh->dh_firstfree[DH_NFSTATS] = 0; 402178110Sjeff dh->dh_hused = 0; 403178110Sjeff dh->dh_seqopt = 0; 404178110Sjeff dh->dh_seqoff = 0; 405178110Sjeff dh->dh_score = DH_SCOREINIT; 406193375Ssnb dh->dh_lastused = time_second; 407178110Sjeff 40879561Siedowse /* 40979561Siedowse * Use non-blocking mallocs so that we will revert to a linear 41079561Siedowse * lookup on failure rather than potentially blocking forever. 41179561Siedowse */ 412184205Sdes dh->dh_hash = malloc(narrays * sizeof(dh->dh_hash[0]), 41379561Siedowse M_DIRHASH, M_NOWAIT | M_ZERO); 414178110Sjeff if (dh->dh_hash == NULL) 415178110Sjeff goto fail; 416184205Sdes dh->dh_blkfree = malloc(nblocks * sizeof(dh->dh_blkfree[0]), 41779561Siedowse M_DIRHASH, M_NOWAIT); 418178110Sjeff if (dh->dh_blkfree == NULL) 41979561Siedowse goto fail; 42079561Siedowse for (i = 0; i < narrays; i++) { 421125854Sdwmalone if ((dh->dh_hash[i] = DIRHASH_BLKALLOC_WAITOK()) == NULL) 42279561Siedowse goto fail; 42379561Siedowse for (j = 0; j < DH_NBLKOFF; j++) 42479561Siedowse dh->dh_hash[i][j] = DIRHASH_EMPTY; 42579561Siedowse } 42679561Siedowse for (i = 0; i < dirblocks; i++) 42779561Siedowse dh->dh_blkfree[i] = DIRBLKSIZ / DIRALIGN; 42879561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 42979561Siedowse pos = 0; 43079561Siedowse while (pos < ip->i_size) { 43179561Siedowse /* If necessary, get the next directory block. */ 43279561Siedowse if ((pos & bmask) == 0) { 43379561Siedowse if (bp != NULL) 43479561Siedowse brelse(bp); 43579561Siedowse if (UFS_BLKATOFF(vp, (off_t)pos, NULL, &bp) != 0) 43679561Siedowse goto fail; 43779561Siedowse } 43879561Siedowse 43979561Siedowse /* Add this entry to the hash. */ 44079561Siedowse ep = (struct direct *)((char *)bp->b_data + (pos & bmask)); 44179561Siedowse if (ep->d_reclen == 0 || ep->d_reclen > 44279561Siedowse DIRBLKSIZ - (pos & (DIRBLKSIZ - 1))) { 44379561Siedowse /* Corrupted directory. */ 44479561Siedowse brelse(bp); 44579561Siedowse goto fail; 44679561Siedowse } 44779561Siedowse if (ep->d_ino != 0) { 44879561Siedowse /* Add the entry (simplified ufsdirhash_add). */ 44979561Siedowse slot = ufsdirhash_hash(dh, ep->d_name, ep->d_namlen); 45079561Siedowse while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 45179561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 45279561Siedowse dh->dh_hused++; 45379561Siedowse DH_ENTRY(dh, slot) = pos; 45479561Siedowse ufsdirhash_adjfree(dh, pos, -DIRSIZ(0, ep)); 45579561Siedowse } 45679561Siedowse pos += ep->d_reclen; 45779561Siedowse } 45879561Siedowse 45979561Siedowse if (bp != NULL) 46079561Siedowse brelse(bp); 461125854Sdwmalone DIRHASHLIST_LOCK(); 46279561Siedowse TAILQ_INSERT_TAIL(&ufsdirhash_list, dh, dh_list); 46379561Siedowse dh->dh_onlist = 1; 464125854Sdwmalone DIRHASHLIST_UNLOCK(); 465183080Sjhb sx_downgrade(&dh->dh_lock); 46679561Siedowse return (0); 46779561Siedowse 46879561Siedowsefail: 469178110Sjeff ufsdirhash_free_locked(ip); 47079561Siedowse return (-1); 47179561Siedowse} 47279561Siedowse 47379561Siedowse/* 47479561Siedowse * Free any hash table associated with inode 'ip'. 47579561Siedowse */ 476178110Sjeffstatic void 477178110Sjeffufsdirhash_free_locked(struct inode *ip) 47879561Siedowse{ 47979561Siedowse struct dirhash *dh; 480178110Sjeff struct vnode *vp; 481178110Sjeff int i; 48279561Siedowse 483178110Sjeff DIRHASH_ASSERT_LOCKED(ip->i_dirhash); 484183080Sjhb 485178110Sjeff /* 486178110Sjeff * Clear the pointer in the inode to prevent new threads from 487178110Sjeff * finding the dead structure. 488178110Sjeff */ 489178110Sjeff vp = ip->i_vnode; 490178110Sjeff VI_LOCK(vp); 491178110Sjeff dh = ip->i_dirhash; 492178110Sjeff ip->i_dirhash = NULL; 493178110Sjeff VI_UNLOCK(vp); 494183080Sjhb 495178110Sjeff /* 496183280Sjhb * Remove the hash from the list since we are going to free its 497183280Sjhb * memory. 498183280Sjhb */ 499183280Sjhb DIRHASHLIST_LOCK(); 500183280Sjhb if (dh->dh_onlist) 501183280Sjhb TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 502183280Sjhb ufs_dirhashmem -= dh->dh_memreq; 503183280Sjhb DIRHASHLIST_UNLOCK(); 504183280Sjhb 505183280Sjhb /* 506183080Sjhb * At this point, any waiters for the lock should hold their 507183080Sjhb * own reference on the dirhash structure. They will drop 508183080Sjhb * that reference once they grab the vnode interlock and see 509183080Sjhb * that ip->i_dirhash is NULL. 510178110Sjeff */ 511183080Sjhb sx_xunlock(&dh->dh_lock); 512183080Sjhb 513178110Sjeff /* 514178110Sjeff * Handle partially recycled as well as fully constructed hashes. 515178110Sjeff */ 516178110Sjeff if (dh->dh_hash != NULL) { 517178110Sjeff for (i = 0; i < dh->dh_narrays; i++) 518178110Sjeff if (dh->dh_hash[i] != NULL) 519178110Sjeff DIRHASH_BLKFREE(dh->dh_hash[i]); 520184205Sdes free(dh->dh_hash, M_DIRHASH); 521178110Sjeff if (dh->dh_blkfree != NULL) 522184205Sdes free(dh->dh_blkfree, M_DIRHASH); 523178110Sjeff } 524183080Sjhb 525178110Sjeff /* 526183080Sjhb * Drop the inode's reference to the data structure. 527178110Sjeff */ 528183080Sjhb ufsdirhash_drop(dh); 52979561Siedowse} 53079561Siedowse 53179561Siedowse/* 53279561Siedowse * Find the offset of the specified name within the given inode. 53379561Siedowse * Returns 0 on success, ENOENT if the entry does not exist, or 53479561Siedowse * EJUSTRETURN if the caller should revert to a linear search. 53579561Siedowse * 53679690Siedowse * If successful, the directory offset is stored in *offp, and a 53779690Siedowse * pointer to a struct buf containing the entry is stored in *bpp. If 53879561Siedowse * prevoffp is non-NULL, the offset of the previous entry within 53979561Siedowse * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry 54079561Siedowse * is the first in a block, the start of the block is used). 541178110Sjeff * 542178110Sjeff * Must be called with the hash locked. Returns with the hash unlocked. 54379561Siedowse */ 54479561Siedowseint 54579561Siedowseufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp, 54679690Siedowse struct buf **bpp, doff_t *prevoffp) 54779561Siedowse{ 54879561Siedowse struct dirhash *dh, *dh_next; 54979561Siedowse struct direct *dp; 55079561Siedowse struct vnode *vp; 55179561Siedowse struct buf *bp; 55279561Siedowse doff_t blkoff, bmask, offset, prevoff; 55379561Siedowse int i, slot; 554178110Sjeff int error; 55579561Siedowse 556178110Sjeff dh = ip->i_dirhash; 557178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 558178110Sjeff ("ufsdirhash_lookup: Invalid dirhash %p\n", dh)); 559178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 56079561Siedowse /* 56179561Siedowse * Move this dirhash towards the end of the list if it has a 562178110Sjeff * score higher than the next entry, and acquire the dh_lock. 56379561Siedowse */ 564178110Sjeff DIRHASHLIST_LOCK(); 56579561Siedowse if (TAILQ_NEXT(dh, dh_list) != NULL) { 56679561Siedowse /* 56779561Siedowse * If the new score will be greater than that of the next 56879561Siedowse * entry, then move this entry past it. With both mutexes 56979561Siedowse * held, dh_next won't go away, but its dh_score could 57079561Siedowse * change; that's not important since it is just a hint. 57179561Siedowse */ 572178110Sjeff if ((dh_next = TAILQ_NEXT(dh, dh_list)) != NULL && 57379561Siedowse dh->dh_score >= dh_next->dh_score) { 57479561Siedowse KASSERT(dh->dh_onlist, ("dirhash: not on list")); 57579561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 57679561Siedowse TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh, 57779561Siedowse dh_list); 57879561Siedowse } 57979561Siedowse } 58079561Siedowse /* Update the score. */ 58179561Siedowse if (dh->dh_score < DH_SCOREMAX) 58279561Siedowse dh->dh_score++; 583193375Ssnb 584193375Ssnb /* Update last used time. */ 585193375Ssnb dh->dh_lastused = time_second; 586178110Sjeff DIRHASHLIST_UNLOCK(); 58779561Siedowse 58879561Siedowse vp = ip->i_vnode; 58979561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 59079561Siedowse blkoff = -1; 59179561Siedowse bp = NULL; 59279561Siedowserestart: 59379561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 59479561Siedowse 59579561Siedowse if (dh->dh_seqopt) { 59679561Siedowse /* 59779561Siedowse * Sequential access optimisation. dh_seqoff contains the 59879561Siedowse * offset of the directory entry immediately following 59979561Siedowse * the last entry that was looked up. Check if this offset 60079561Siedowse * appears in the hash chain for the name we are looking for. 60179561Siedowse */ 60279561Siedowse for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY; 60379561Siedowse i = WRAPINCR(i, dh->dh_hlen)) 60486350Siedowse if (offset == dh->dh_seqoff) 60579561Siedowse break; 60679561Siedowse if (offset == dh->dh_seqoff) { 60779561Siedowse /* 60879561Siedowse * We found an entry with the expected offset. This 60979561Siedowse * is probably the entry we want, but if not, the 610149178Siedowse * code below will turn off seqopt and retry. 61179561Siedowse */ 61279561Siedowse slot = i; 61379561Siedowse } else 61479561Siedowse dh->dh_seqopt = 0; 61579561Siedowse } 61679561Siedowse 61779561Siedowse for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY; 61879561Siedowse slot = WRAPINCR(slot, dh->dh_hlen)) { 61979561Siedowse if (offset == DIRHASH_DEL) 62079561Siedowse continue; 62179561Siedowse if (offset < 0 || offset >= ip->i_size) 62279561Siedowse panic("ufsdirhash_lookup: bad offset in hash array"); 62379561Siedowse if ((offset & ~bmask) != blkoff) { 62479561Siedowse if (bp != NULL) 62579561Siedowse brelse(bp); 62679561Siedowse blkoff = offset & ~bmask; 627178110Sjeff if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) { 628178110Sjeff error = EJUSTRETURN; 629178110Sjeff goto fail; 630178110Sjeff } 63179561Siedowse } 63279561Siedowse dp = (struct direct *)(bp->b_data + (offset & bmask)); 63379561Siedowse if (dp->d_reclen == 0 || dp->d_reclen > 63479561Siedowse DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) { 63579561Siedowse /* Corrupted directory. */ 636178110Sjeff error = EJUSTRETURN; 637178110Sjeff goto fail; 63879561Siedowse } 63979561Siedowse if (dp->d_namlen == namelen && 64079561Siedowse bcmp(dp->d_name, name, namelen) == 0) { 64179561Siedowse /* Found. Get the prev offset if needed. */ 64279561Siedowse if (prevoffp != NULL) { 64379561Siedowse if (offset & (DIRBLKSIZ - 1)) { 64479561Siedowse prevoff = ufsdirhash_getprev(dp, 64579561Siedowse offset); 64679561Siedowse if (prevoff == -1) { 647178110Sjeff error = EJUSTRETURN; 648178110Sjeff goto fail; 64979561Siedowse } 65079561Siedowse } else 65179561Siedowse prevoff = offset; 65279561Siedowse *prevoffp = prevoff; 65379561Siedowse } 65479561Siedowse 65579561Siedowse /* Check for sequential access, and update offset. */ 65679561Siedowse if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset) 65779561Siedowse dh->dh_seqopt = 1; 65879561Siedowse dh->dh_seqoff = offset + DIRSIZ(0, dp); 65979690Siedowse *bpp = bp; 66079561Siedowse *offp = offset; 661178110Sjeff ufsdirhash_release(dh); 66279561Siedowse return (0); 66379561Siedowse } 66479561Siedowse 66579561Siedowse /* 66679561Siedowse * When the name doesn't match in the seqopt case, go back 66779561Siedowse * and search normally. 66879561Siedowse */ 66979561Siedowse if (dh->dh_seqopt) { 67079561Siedowse dh->dh_seqopt = 0; 67179561Siedowse goto restart; 67279561Siedowse } 67379561Siedowse } 674178110Sjeff error = ENOENT; 675178110Sjefffail: 676178110Sjeff ufsdirhash_release(dh); 67779561Siedowse if (bp != NULL) 67879561Siedowse brelse(bp); 679178110Sjeff return (error); 68079561Siedowse} 68179561Siedowse 68279561Siedowse/* 68379561Siedowse * Find a directory block with room for 'slotneeded' bytes. Returns 68479561Siedowse * the offset of the directory entry that begins the free space. 68579561Siedowse * This will either be the offset of an existing entry that has free 68679561Siedowse * space at the end, or the offset of an entry with d_ino == 0 at 68779561Siedowse * the start of a DIRBLKSIZ block. 68879561Siedowse * 68979561Siedowse * To use the space, the caller may need to compact existing entries in 69079561Siedowse * the directory. The total number of bytes in all of the entries involved 69179561Siedowse * in the compaction is stored in *slotsize. In other words, all of 69279561Siedowse * the entries that must be compacted are exactly contained in the 69379561Siedowse * region beginning at the returned offset and spanning *slotsize bytes. 69479561Siedowse * 69579561Siedowse * Returns -1 if no space was found, indicating that the directory 69679561Siedowse * must be extended. 69779561Siedowse */ 69879561Siedowsedoff_t 69979561Siedowseufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize) 70079561Siedowse{ 70179561Siedowse struct direct *dp; 70279561Siedowse struct dirhash *dh; 70379561Siedowse struct buf *bp; 70479561Siedowse doff_t pos, slotstart; 70579561Siedowse int dirblock, error, freebytes, i; 70679561Siedowse 707178110Sjeff dh = ip->i_dirhash; 708178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 709178110Sjeff ("ufsdirhash_findfree: Invalid dirhash %p\n", dh)); 710178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 71179561Siedowse 71279561Siedowse /* Find a directory block with the desired free space. */ 71379561Siedowse dirblock = -1; 71479561Siedowse for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++) 71579561Siedowse if ((dirblock = dh->dh_firstfree[i]) != -1) 71679561Siedowse break; 717178110Sjeff if (dirblock == -1) 71879561Siedowse return (-1); 71979561Siedowse 72079561Siedowse KASSERT(dirblock < dh->dh_nblk && 72179561Siedowse dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN), 72279561Siedowse ("ufsdirhash_findfree: bad stats")); 72379561Siedowse pos = dirblock * DIRBLKSIZ; 72479561Siedowse error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp); 72579561Siedowse if (error) 72679561Siedowse return (-1); 72779561Siedowse 72879561Siedowse /* Find the first entry with free space. */ 72979561Siedowse for (i = 0; i < DIRBLKSIZ; ) { 73079561Siedowse if (dp->d_reclen == 0) { 73179561Siedowse brelse(bp); 73279561Siedowse return (-1); 73379561Siedowse } 73479561Siedowse if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp)) 73579561Siedowse break; 73679561Siedowse i += dp->d_reclen; 73779561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 73879561Siedowse } 73979561Siedowse if (i > DIRBLKSIZ) { 74079561Siedowse brelse(bp); 74179561Siedowse return (-1); 74279561Siedowse } 74379561Siedowse slotstart = pos + i; 74479561Siedowse 74579561Siedowse /* Find the range of entries needed to get enough space */ 74679561Siedowse freebytes = 0; 74779561Siedowse while (i < DIRBLKSIZ && freebytes < slotneeded) { 74879561Siedowse freebytes += dp->d_reclen; 74979561Siedowse if (dp->d_ino != 0) 75079561Siedowse freebytes -= DIRSIZ(0, dp); 75179561Siedowse if (dp->d_reclen == 0) { 75279561Siedowse brelse(bp); 75379561Siedowse return (-1); 75479561Siedowse } 75579561Siedowse i += dp->d_reclen; 75679561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 75779561Siedowse } 75879561Siedowse if (i > DIRBLKSIZ) { 75979561Siedowse brelse(bp); 76079561Siedowse return (-1); 76179561Siedowse } 76279561Siedowse if (freebytes < slotneeded) 76379561Siedowse panic("ufsdirhash_findfree: free mismatch"); 76479561Siedowse brelse(bp); 76579561Siedowse *slotsize = pos + i - slotstart; 76679561Siedowse return (slotstart); 76779561Siedowse} 76879561Siedowse 76979561Siedowse/* 77079561Siedowse * Return the start of the unused space at the end of a directory, or 77179561Siedowse * -1 if there are no trailing unused blocks. 77279561Siedowse */ 77379561Siedowsedoff_t 77479561Siedowseufsdirhash_enduseful(struct inode *ip) 77579561Siedowse{ 77679561Siedowse 77779561Siedowse struct dirhash *dh; 77879561Siedowse int i; 77979561Siedowse 780178110Sjeff dh = ip->i_dirhash; 781178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 782178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 783178110Sjeff ("ufsdirhash_enduseful: Invalid dirhash %p\n", dh)); 78479561Siedowse 785178110Sjeff if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN) 78679561Siedowse return (-1); 78779561Siedowse 78879561Siedowse for (i = dh->dh_dirblks - 1; i >= 0; i--) 78979561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 79079561Siedowse break; 791178110Sjeff 79279561Siedowse return ((doff_t)(i + 1) * DIRBLKSIZ); 79379561Siedowse} 79479561Siedowse 79579561Siedowse/* 79679561Siedowse * Insert information into the hash about a new directory entry. dirp 79779561Siedowse * points to a struct direct containing the entry, and offset specifies 79879561Siedowse * the offset of this entry. 79979561Siedowse */ 80079561Siedowsevoid 80179561Siedowseufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset) 80279561Siedowse{ 80379561Siedowse struct dirhash *dh; 80479561Siedowse int slot; 80579561Siedowse 806178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 80779561Siedowse return; 808178110Sjeff 80979561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 81079561Siedowse ("ufsdirhash_add: bad offset")); 81179561Siedowse /* 81279561Siedowse * Normal hash usage is < 66%. If the usage gets too high then 81379561Siedowse * remove the hash entirely and let it be rebuilt later. 81479561Siedowse */ 81579561Siedowse if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) { 816178110Sjeff ufsdirhash_free_locked(ip); 81779561Siedowse return; 81879561Siedowse } 81979561Siedowse 82079561Siedowse /* Find a free hash slot (empty or deleted), and add the entry. */ 82179561Siedowse slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen); 82279561Siedowse while (DH_ENTRY(dh, slot) >= 0) 82379561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 82479561Siedowse if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY) 82579561Siedowse dh->dh_hused++; 82679561Siedowse DH_ENTRY(dh, slot) = offset; 82779561Siedowse 828193375Ssnb /* Update last used time. */ 829193375Ssnb dh->dh_lastused = time_second; 830193375Ssnb 83179561Siedowse /* Update the per-block summary info. */ 83279561Siedowse ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp)); 833178110Sjeff ufsdirhash_release(dh); 83479561Siedowse} 83579561Siedowse 83679561Siedowse/* 83779561Siedowse * Remove the specified directory entry from the hash. The entry to remove 83879561Siedowse * is defined by the name in `dirp', which must exist at the specified 83979561Siedowse * `offset' within the directory. 84079561Siedowse */ 84179561Siedowsevoid 84279561Siedowseufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset) 84379561Siedowse{ 84479561Siedowse struct dirhash *dh; 84579561Siedowse int slot; 84679561Siedowse 847178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 84879561Siedowse return; 84979561Siedowse 85079561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 85179561Siedowse ("ufsdirhash_remove: bad offset")); 85279561Siedowse /* Find the entry */ 85379561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset); 85479561Siedowse 85579561Siedowse /* Remove the hash entry. */ 85679561Siedowse ufsdirhash_delslot(dh, slot); 85779561Siedowse 85879561Siedowse /* Update the per-block summary info. */ 85979561Siedowse ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp)); 860178110Sjeff ufsdirhash_release(dh); 86179561Siedowse} 86279561Siedowse 86379561Siedowse/* 86479561Siedowse * Change the offset associated with a directory entry in the hash. Used 86579561Siedowse * when compacting directory blocks. 86679561Siedowse */ 86779561Siedowsevoid 86879561Siedowseufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff, 86979561Siedowse doff_t newoff) 87079561Siedowse{ 87179561Siedowse struct dirhash *dh; 87279561Siedowse int slot; 87379561Siedowse 874178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 87579561Siedowse return; 87679561Siedowse 87779561Siedowse KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ && 87879561Siedowse newoff < dh->dh_dirblks * DIRBLKSIZ, 87979561Siedowse ("ufsdirhash_move: bad offset")); 88079561Siedowse /* Find the entry, and update the offset. */ 88179561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff); 88279561Siedowse DH_ENTRY(dh, slot) = newoff; 883178110Sjeff ufsdirhash_release(dh); 88479561Siedowse} 88579561Siedowse 88679561Siedowse/* 88779561Siedowse * Inform dirhash that the directory has grown by one block that 88879561Siedowse * begins at offset (i.e. the new length is offset + DIRBLKSIZ). 88979561Siedowse */ 89079561Siedowsevoid 89179561Siedowseufsdirhash_newblk(struct inode *ip, doff_t offset) 89279561Siedowse{ 89379561Siedowse struct dirhash *dh; 89479561Siedowse int block; 89579561Siedowse 896178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 89779561Siedowse return; 89879561Siedowse 89979561Siedowse KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ, 90079561Siedowse ("ufsdirhash_newblk: bad offset")); 90179561Siedowse block = offset / DIRBLKSIZ; 90279561Siedowse if (block >= dh->dh_nblk) { 90379561Siedowse /* Out of space; must rebuild. */ 904178110Sjeff ufsdirhash_free_locked(ip); 90579561Siedowse return; 90679561Siedowse } 90779561Siedowse dh->dh_dirblks = block + 1; 90879561Siedowse 90979561Siedowse /* Account for the new free block. */ 91079561Siedowse dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN; 91179561Siedowse if (dh->dh_firstfree[DH_NFSTATS] == -1) 91279561Siedowse dh->dh_firstfree[DH_NFSTATS] = block; 913178110Sjeff ufsdirhash_release(dh); 91479561Siedowse} 91579561Siedowse 91679561Siedowse/* 91779561Siedowse * Inform dirhash that the directory is being truncated. 91879561Siedowse */ 91979561Siedowsevoid 92079561Siedowseufsdirhash_dirtrunc(struct inode *ip, doff_t offset) 92179561Siedowse{ 92279561Siedowse struct dirhash *dh; 92379561Siedowse int block, i; 92479561Siedowse 925178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 92679561Siedowse return; 92779561Siedowse 92879561Siedowse KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ, 92979561Siedowse ("ufsdirhash_dirtrunc: bad offset")); 93079561Siedowse block = howmany(offset, DIRBLKSIZ); 93179561Siedowse /* 93279561Siedowse * If the directory shrinks to less than 1/8 of dh_nblk blocks 93379561Siedowse * (about 20% of its original size due to the 50% extra added in 93479561Siedowse * ufsdirhash_build) then free it, and let the caller rebuild 93579561Siedowse * if necessary. 93679561Siedowse */ 93779561Siedowse if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) { 938178110Sjeff ufsdirhash_free_locked(ip); 93979561Siedowse return; 94079561Siedowse } 94179561Siedowse 94279561Siedowse /* 94379561Siedowse * Remove any `first free' information pertaining to the 94479561Siedowse * truncated blocks. All blocks we're removing should be 94579561Siedowse * completely unused. 94679561Siedowse */ 94779561Siedowse if (dh->dh_firstfree[DH_NFSTATS] >= block) 94879561Siedowse dh->dh_firstfree[DH_NFSTATS] = -1; 94979561Siedowse for (i = block; i < dh->dh_dirblks; i++) 95079561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 95179561Siedowse panic("ufsdirhash_dirtrunc: blocks in use"); 95279561Siedowse for (i = 0; i < DH_NFSTATS; i++) 95379561Siedowse if (dh->dh_firstfree[i] >= block) 95479561Siedowse panic("ufsdirhash_dirtrunc: first free corrupt"); 95579561Siedowse dh->dh_dirblks = block; 956178110Sjeff ufsdirhash_release(dh); 95779561Siedowse} 95879561Siedowse 95979561Siedowse/* 96079561Siedowse * Debugging function to check that the dirhash information about 96179561Siedowse * a directory block matches its actual contents. Panics if a mismatch 96279561Siedowse * is detected. 96379561Siedowse * 96479561Siedowse * On entry, `buf' should point to the start of an in-core 96579561Siedowse * DIRBLKSIZ-sized directory block, and `offset' should contain the 96679561Siedowse * offset from the start of the directory of that block. 96779561Siedowse */ 96879561Siedowsevoid 96979561Siedowseufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset) 97079561Siedowse{ 97179561Siedowse struct dirhash *dh; 97279561Siedowse struct direct *dp; 97379561Siedowse int block, ffslot, i, nfree; 97479561Siedowse 97579561Siedowse if (!ufs_dirhashcheck) 97679561Siedowse return; 977178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 97879561Siedowse return; 97979561Siedowse 98079561Siedowse block = offset / DIRBLKSIZ; 98179561Siedowse if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks) 98279561Siedowse panic("ufsdirhash_checkblock: bad offset"); 98379561Siedowse 98479561Siedowse nfree = 0; 98579561Siedowse for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) { 98679561Siedowse dp = (struct direct *)(buf + i); 98779561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ) 98879561Siedowse panic("ufsdirhash_checkblock: bad dir"); 98979561Siedowse 99079561Siedowse if (dp->d_ino == 0) { 99180456Siedowse#if 0 99280456Siedowse /* 99380456Siedowse * XXX entries with d_ino == 0 should only occur 99480456Siedowse * at the start of a DIRBLKSIZ block. However the 99580456Siedowse * ufs code is tolerant of such entries at other 99680456Siedowse * offsets, and fsck does not fix them. 99780456Siedowse */ 99879561Siedowse if (i != 0) 99979561Siedowse panic("ufsdirhash_checkblock: bad dir inode"); 100080456Siedowse#endif 100179561Siedowse nfree += dp->d_reclen; 100279561Siedowse continue; 100379561Siedowse } 100479561Siedowse 100579561Siedowse /* Check that the entry exists (will panic if it doesn't). */ 100679561Siedowse ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i); 100779561Siedowse 100879561Siedowse nfree += dp->d_reclen - DIRSIZ(0, dp); 100979561Siedowse } 101079561Siedowse if (i != DIRBLKSIZ) 101179561Siedowse panic("ufsdirhash_checkblock: bad dir end"); 101279561Siedowse 101379561Siedowse if (dh->dh_blkfree[block] * DIRALIGN != nfree) 101479561Siedowse panic("ufsdirhash_checkblock: bad free count"); 101579561Siedowse 101692098Siedowse ffslot = BLKFREE2IDX(nfree / DIRALIGN); 101779561Siedowse for (i = 0; i <= DH_NFSTATS; i++) 101879561Siedowse if (dh->dh_firstfree[i] == block && i != ffslot) 101979561Siedowse panic("ufsdirhash_checkblock: bad first-free"); 102092098Siedowse if (dh->dh_firstfree[ffslot] == -1) 102192098Siedowse panic("ufsdirhash_checkblock: missing first-free entry"); 1022178110Sjeff ufsdirhash_release(dh); 102379561Siedowse} 102479561Siedowse 102579561Siedowse/* 102679561Siedowse * Hash the specified filename into a dirhash slot. 102779561Siedowse */ 102879561Siedowsestatic int 102979561Siedowseufsdirhash_hash(struct dirhash *dh, char *name, int namelen) 103079561Siedowse{ 103192807Sdwmalone u_int32_t hash; 103292807Sdwmalone 103392807Sdwmalone /* 1034107868Siedowse * We hash the name and then some other bit of data that is 1035107868Siedowse * invariant over the dirhash's lifetime. Otherwise names 103692807Sdwmalone * differing only in the last byte are placed close to one 103792807Sdwmalone * another in the table, which is bad for linear probing. 103892807Sdwmalone */ 103992807Sdwmalone hash = fnv_32_buf(name, namelen, FNV1_32_INIT); 1040133837Sdwmalone hash = fnv_32_buf(&dh, sizeof(dh), hash); 104192807Sdwmalone return (hash % dh->dh_hlen); 104279561Siedowse} 104379561Siedowse 104479561Siedowse/* 104579561Siedowse * Adjust the number of free bytes in the block containing `offset' 104679561Siedowse * by the value specified by `diff'. 104779561Siedowse * 104879561Siedowse * The caller must ensure we have exclusive access to `dh'; normally 1049178110Sjeff * that means that dh_lock should be held, but this is also called 105079561Siedowse * from ufsdirhash_build() where exclusive access can be assumed. 105179561Siedowse */ 105279561Siedowsestatic void 105379561Siedowseufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff) 105479561Siedowse{ 105579561Siedowse int block, i, nfidx, ofidx; 105679561Siedowse 105779561Siedowse /* Update the per-block summary info. */ 105879561Siedowse block = offset / DIRBLKSIZ; 105979561Siedowse KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks, 106079561Siedowse ("dirhash bad offset")); 106192098Siedowse ofidx = BLKFREE2IDX(dh->dh_blkfree[block]); 106279561Siedowse dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN); 106392098Siedowse nfidx = BLKFREE2IDX(dh->dh_blkfree[block]); 106479561Siedowse 106579561Siedowse /* Update the `first free' list if necessary. */ 106679561Siedowse if (ofidx != nfidx) { 106779561Siedowse /* If removing, scan forward for the next block. */ 106879561Siedowse if (dh->dh_firstfree[ofidx] == block) { 106979561Siedowse for (i = block + 1; i < dh->dh_dirblks; i++) 107092098Siedowse if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx) 107179561Siedowse break; 107279561Siedowse dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1; 107379561Siedowse } 107479561Siedowse 107579561Siedowse /* Make this the new `first free' if necessary */ 107679561Siedowse if (dh->dh_firstfree[nfidx] > block || 107779561Siedowse dh->dh_firstfree[nfidx] == -1) 107879561Siedowse dh->dh_firstfree[nfidx] = block; 107979561Siedowse } 108079561Siedowse} 108179561Siedowse 108279561Siedowse/* 108379561Siedowse * Find the specified name which should have the specified offset. 108479561Siedowse * Returns a slot number, and panics on failure. 108579561Siedowse * 108679561Siedowse * `dh' must be locked on entry and remains so on return. 108779561Siedowse */ 108879561Siedowsestatic int 108979561Siedowseufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset) 109079561Siedowse{ 109179561Siedowse int slot; 109279561Siedowse 1093178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 109479561Siedowse 109579561Siedowse /* Find the entry. */ 109679561Siedowse KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full")); 109779561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 109879561Siedowse while (DH_ENTRY(dh, slot) != offset && 109979561Siedowse DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 110079561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 110179561Siedowse if (DH_ENTRY(dh, slot) != offset) 110279561Siedowse panic("ufsdirhash_findslot: '%.*s' not found", namelen, name); 110379561Siedowse 110479561Siedowse return (slot); 110579561Siedowse} 110679561Siedowse 110779561Siedowse/* 110879561Siedowse * Remove the entry corresponding to the specified slot from the hash array. 110979561Siedowse * 111079561Siedowse * `dh' must be locked on entry and remains so on return. 111179561Siedowse */ 111279561Siedowsestatic void 111379561Siedowseufsdirhash_delslot(struct dirhash *dh, int slot) 111479561Siedowse{ 111579561Siedowse int i; 111679561Siedowse 1117178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 111879561Siedowse 111979561Siedowse /* Mark the entry as deleted. */ 112079561Siedowse DH_ENTRY(dh, slot) = DIRHASH_DEL; 112179561Siedowse 112279561Siedowse /* If this is the end of a chain of DIRHASH_DEL slots, remove them. */ 112379561Siedowse for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; ) 112479561Siedowse i = WRAPINCR(i, dh->dh_hlen); 112579561Siedowse if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) { 112692807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 112792807Sdwmalone while (DH_ENTRY(dh, i) == DIRHASH_DEL) { 112879561Siedowse DH_ENTRY(dh, i) = DIRHASH_EMPTY; 112979561Siedowse dh->dh_hused--; 113092807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 113179561Siedowse } 113279561Siedowse KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen")); 113379561Siedowse } 113479561Siedowse} 113579561Siedowse 113679561Siedowse/* 113779561Siedowse * Given a directory entry and its offset, find the offset of the 113879561Siedowse * previous entry in the same DIRBLKSIZ-sized block. Returns an 113979561Siedowse * offset, or -1 if there is no previous entry in the block or some 114079561Siedowse * other problem occurred. 114179561Siedowse */ 114279561Siedowsestatic doff_t 114379561Siedowseufsdirhash_getprev(struct direct *dirp, doff_t offset) 114479561Siedowse{ 114579561Siedowse struct direct *dp; 114679561Siedowse char *blkbuf; 114779561Siedowse doff_t blkoff, prevoff; 114879561Siedowse int entrypos, i; 114979561Siedowse 115079561Siedowse blkoff = offset & ~(DIRBLKSIZ - 1); /* offset of start of block */ 115179561Siedowse entrypos = offset & (DIRBLKSIZ - 1); /* entry relative to block */ 115279561Siedowse blkbuf = (char *)dirp - entrypos; 115379561Siedowse prevoff = blkoff; 115479561Siedowse 115579561Siedowse /* If `offset' is the start of a block, there is no previous entry. */ 115679561Siedowse if (entrypos == 0) 115779561Siedowse return (-1); 115879561Siedowse 115979561Siedowse /* Scan from the start of the block until we get to the entry. */ 116079561Siedowse for (i = 0; i < entrypos; i += dp->d_reclen) { 116179561Siedowse dp = (struct direct *)(blkbuf + i); 116279561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos) 116379561Siedowse return (-1); /* Corrupted directory. */ 116479561Siedowse prevoff = blkoff + i; 116579561Siedowse } 116679561Siedowse return (prevoff); 116779561Siedowse} 116879561Siedowse 116979561Siedowse/* 1170193375Ssnb * Delete the given dirhash and reclaim its memory. Assumes that 1171193375Ssnb * ufsdirhash_list is locked, and leaves it locked. Also assumes 1172193375Ssnb * that dh is locked. Returns the amount of memory freed. 1173193375Ssnb */ 1174193375Ssnbstatic int 1175193375Ssnbufsdirhash_destroy(struct dirhash *dh) 1176193375Ssnb{ 1177193375Ssnb doff_t **hash; 1178193375Ssnb u_int8_t *blkfree; 1179193375Ssnb int i, mem, narrays; 1180193375Ssnb 1181193375Ssnb KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list")); 1182193375Ssnb 1183193375Ssnb /* Remove it from the list and detach its memory. */ 1184193375Ssnb TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 1185193375Ssnb dh->dh_onlist = 0; 1186193375Ssnb hash = dh->dh_hash; 1187193375Ssnb dh->dh_hash = NULL; 1188193375Ssnb blkfree = dh->dh_blkfree; 1189193375Ssnb dh->dh_blkfree = NULL; 1190193375Ssnb narrays = dh->dh_narrays; 1191193375Ssnb mem = dh->dh_memreq; 1192193375Ssnb dh->dh_memreq = 0; 1193193375Ssnb 1194194387Ssnb /* Unlock dirhash and free the detached memory. */ 1195193375Ssnb ufsdirhash_release(dh); 1196193375Ssnb for (i = 0; i < narrays; i++) 1197193375Ssnb DIRHASH_BLKFREE(hash[i]); 1198193375Ssnb free(hash, M_DIRHASH); 1199193375Ssnb free(blkfree, M_DIRHASH); 1200193375Ssnb 1201193375Ssnb /* Account for the returned memory. */ 1202193375Ssnb ufs_dirhashmem -= mem; 1203193375Ssnb 1204193375Ssnb return (mem); 1205193375Ssnb} 1206193375Ssnb 1207193375Ssnb/* 120879561Siedowse * Try to free up `wanted' bytes by stealing memory from existing 1209125854Sdwmalone * dirhashes. Returns zero with list locked if successful. 121079561Siedowse */ 121179561Siedowsestatic int 121279561Siedowseufsdirhash_recycle(int wanted) 121379561Siedowse{ 121479561Siedowse struct dirhash *dh; 121579561Siedowse 1216125854Sdwmalone DIRHASHLIST_LOCK(); 1217178110Sjeff dh = TAILQ_FIRST(&ufsdirhash_list); 121879561Siedowse while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) { 1219178110Sjeff /* Decrement the score; only recycle if it becomes zero. */ 1220178110Sjeff if (dh == NULL || --dh->dh_score > 0) { 1221125854Sdwmalone DIRHASHLIST_UNLOCK(); 122279561Siedowse return (-1); 122379561Siedowse } 1224178110Sjeff /* 1225178110Sjeff * If we can't lock it it's in use and we don't want to 1226178110Sjeff * recycle it anyway. 1227178110Sjeff */ 1228183080Sjhb if (!sx_try_xlock(&dh->dh_lock)) { 1229178110Sjeff dh = TAILQ_NEXT(dh, dh_list); 1230178110Sjeff continue; 1231178110Sjeff } 123279561Siedowse 1233193375Ssnb ufsdirhash_destroy(dh); 123479561Siedowse 1235193375Ssnb /* Repeat if necessary. */ 1236178110Sjeff dh = TAILQ_FIRST(&ufsdirhash_list); 123779561Siedowse } 1238125854Sdwmalone /* Success; return with list locked. */ 123979561Siedowse return (0); 124079561Siedowse} 124179561Siedowse 1242193375Ssnb/* 1243193375Ssnb * Callback that frees some dirhashes when the system is low on virtual memory. 1244193375Ssnb */ 1245193375Ssnbstatic void 1246193375Ssnbufsdirhash_lowmem() 1247193375Ssnb{ 1248194387Ssnb struct dirhash *dh, *dh_temp; 1249193375Ssnb int memfreed = 0; 1250193375Ssnb /* XXX: this 10% may need to be adjusted */ 1251193375Ssnb int memwanted = ufs_dirhashmem / 10; 125279561Siedowse 1253193375Ssnb ufs_dirhashlowmemcount++; 1254193375Ssnb 1255193375Ssnb DIRHASHLIST_LOCK(); 1256193375Ssnb /* 1257193375Ssnb * Delete dirhashes not used for more than ufs_dirhashreclaimage 1258193375Ssnb * seconds. If we can't get a lock on the dirhash, it will be skipped. 1259193375Ssnb */ 1260194387Ssnb TAILQ_FOREACH_SAFE(dh, &ufsdirhash_list, dh_list, dh_temp) { 1261193375Ssnb if (!sx_try_xlock(&dh->dh_lock)) 1262193375Ssnb continue; 1263193375Ssnb if (time_second - dh->dh_lastused > ufs_dirhashreclaimage) 1264193375Ssnb memfreed += ufsdirhash_destroy(dh); 1265193375Ssnb /* Unlock if we didn't delete the dirhash */ 1266193375Ssnb else 1267193375Ssnb ufsdirhash_release(dh); 1268193375Ssnb } 1269193375Ssnb 1270193375Ssnb /* 1271193375Ssnb * If not enough memory was freed, keep deleting hashes from the head 1272193375Ssnb * of the dirhash list. The ones closest to the head should be the 1273193375Ssnb * oldest. 1274193375Ssnb */ 1275194387Ssnb if (memfreed < memwanted) { 1276194387Ssnb TAILQ_FOREACH_SAFE(dh, &ufsdirhash_list, dh_list, dh_temp) { 1277194387Ssnb if (!sx_try_xlock(&dh->dh_lock)) 1278194387Ssnb continue; 1279194387Ssnb memfreed += ufsdirhash_destroy(dh); 1280194387Ssnb if (memfreed >= memwanted) 1281194387Ssnb break; 1282194387Ssnb } 1283193375Ssnb } 1284193375Ssnb DIRHASHLIST_UNLOCK(); 1285193375Ssnb} 1286193375Ssnb 1287193375Ssnb 128899101Siedowsevoid 128979561Siedowseufsdirhash_init() 129079561Siedowse{ 129196874Siedowse ufsdirhash_zone = uma_zcreate("DIRHASH", DH_NBLKOFF * sizeof(doff_t), 129292768Sjeff NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 129393818Sjhb mtx_init(&ufsdirhash_mtx, "dirhash list", NULL, MTX_DEF); 129479561Siedowse TAILQ_INIT(&ufsdirhash_list); 1295193375Ssnb 1296193375Ssnb /* Register a callback function to handle low memory signals */ 1297193375Ssnb EVENTHANDLER_REGISTER(vm_lowmem, ufsdirhash_lowmem, NULL, 1298193375Ssnb EVENTHANDLER_PRI_FIRST); 129979561Siedowse} 130079561Siedowse 130199101Siedowsevoid 130299101Siedowseufsdirhash_uninit() 130399101Siedowse{ 130499101Siedowse KASSERT(TAILQ_EMPTY(&ufsdirhash_list), ("ufsdirhash_uninit")); 130599101Siedowse uma_zdestroy(ufsdirhash_zone); 130699101Siedowse mtx_destroy(&ufsdirhash_mtx); 130799101Siedowse} 130879561Siedowse 130979561Siedowse#endif /* UFS_DIRHASH */ 1310