ufs_dirhash.c revision 184205
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 184205 2008-10-23 15:53:51Z des $"); 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> 5292768Sjeff#include <vm/uma.h> 5379561Siedowse 5479561Siedowse#include <ufs/ufs/quota.h> 5579561Siedowse#include <ufs/ufs/inode.h> 5679561Siedowse#include <ufs/ufs/dir.h> 5779561Siedowse#include <ufs/ufs/dirhash.h> 5879561Siedowse#include <ufs/ufs/extattr.h> 5979561Siedowse#include <ufs/ufs/ufsmount.h> 6079561Siedowse#include <ufs/ufs/ufs_extern.h> 6179561Siedowse 6279561Siedowse#define WRAPINCR(val, limit) (((val) + 1 == (limit)) ? 0 : ((val) + 1)) 6392807Sdwmalone#define WRAPDECR(val, limit) (((val) == 0) ? ((limit) - 1) : ((val) - 1)) 6479561Siedowse#define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0) 6592098Siedowse#define BLKFREE2IDX(n) ((n) > DH_NFSTATS ? DH_NFSTATS : (n)) 6679561Siedowse 67151897Srwatsonstatic MALLOC_DEFINE(M_DIRHASH, "ufs_dirhash", "UFS directory hash tables"); 6879561Siedowse 69141631Sphkstatic SYSCTL_NODE(_vfs, OID_AUTO, ufs, CTLFLAG_RD, 0, "UFS filesystem"); 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"); 7579561Siedowsestatic int ufs_dirhashmaxmem = 2 * 1024 * 1024; 7679561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_maxmem, CTLFLAG_RW, &ufs_dirhashmaxmem, 7779561Siedowse 0, "maximum allowed dirhash memory usage"); 7879561Siedowsestatic int ufs_dirhashmem; 7979561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_mem, CTLFLAG_RD, &ufs_dirhashmem, 8079561Siedowse 0, "current dirhash memory usage"); 8185512Siedowsestatic int ufs_dirhashcheck = 0; 8279561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_docheck, CTLFLAG_RW, &ufs_dirhashcheck, 8379561Siedowse 0, "enable extra sanity tests"); 8479561Siedowse 8579561Siedowse 8679561Siedowsestatic int ufsdirhash_hash(struct dirhash *dh, char *name, int namelen); 8779561Siedowsestatic void ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff); 8879561Siedowsestatic void ufsdirhash_delslot(struct dirhash *dh, int slot); 8979561Siedowsestatic int ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, 9079561Siedowse doff_t offset); 9179561Siedowsestatic doff_t ufsdirhash_getprev(struct direct *dp, doff_t offset); 9279561Siedowsestatic int ufsdirhash_recycle(int wanted); 93178110Sjeffstatic void ufsdirhash_free_locked(struct inode *ip); 9479561Siedowse 9592768Sjeffstatic uma_zone_t ufsdirhash_zone; 9679561Siedowse 97125854Sdwmalone#define DIRHASHLIST_LOCK() mtx_lock(&ufsdirhash_mtx) 98125854Sdwmalone#define DIRHASHLIST_UNLOCK() mtx_unlock(&ufsdirhash_mtx) 99125854Sdwmalone#define DIRHASH_BLKALLOC_WAITOK() uma_zalloc(ufsdirhash_zone, M_WAITOK) 100125854Sdwmalone#define DIRHASH_BLKFREE(ptr) uma_zfree(ufsdirhash_zone, (ptr)) 101178110Sjeff#define DIRHASH_ASSERT_LOCKED(dh) \ 102183080Sjhb sx_assert(&(dh)->dh_lock, SA_LOCKED) 103125854Sdwmalone 10479561Siedowse/* Dirhash list; recently-used entries are near the tail. */ 10579561Siedowsestatic TAILQ_HEAD(, dirhash) ufsdirhash_list; 10679561Siedowse 10779561Siedowse/* Protects: ufsdirhash_list, `dh_list' field, ufs_dirhashmem. */ 10879561Siedowsestatic struct mtx ufsdirhash_mtx; 10979561Siedowse 11079561Siedowse/* 111178110Sjeff * Locking: 11279561Siedowse * 113178110Sjeff * The relationship between inode and dirhash is protected either by an 114178110Sjeff * exclusive vnode lock or the vnode interlock where a shared vnode lock 115183080Sjhb * may be used. The dirhash_mtx is acquired after the dirhash lock. To 116183080Sjhb * handle teardown races, code wishing to lock the dirhash for an inode 117183080Sjhb * when using a shared vnode lock must obtain a private reference on the 118183080Sjhb * dirhash while holding the vnode interlock. They can drop it once they 119183080Sjhb * have obtained the dirhash lock and verified that the dirhash wasn't 120183080Sjhb * recycled while they waited for the dirhash lock. 121178110Sjeff * 122178110Sjeff * ufsdirhash_build() acquires a shared lock on the dirhash when it is 123178110Sjeff * successful. This lock is released after a call to ufsdirhash_lookup(). 124178110Sjeff * 125178110Sjeff * Functions requiring exclusive access use ufsdirhash_acquire() which may 126178110Sjeff * free a dirhash structure that was recycled by ufsdirhash_recycle(). 127178110Sjeff * 128178110Sjeff * The dirhash lock may be held across io operations. 12979561Siedowse */ 13079561Siedowse 131183080Sjhbstatic void 132183080Sjhbufsdirhash_hold(struct dirhash *dh) 133183080Sjhb{ 134183080Sjhb 135183080Sjhb refcount_acquire(&dh->dh_refcount); 136183080Sjhb} 137183080Sjhb 138183080Sjhbstatic void 139183080Sjhbufsdirhash_drop(struct dirhash *dh) 140183080Sjhb{ 141183080Sjhb 142183080Sjhb if (refcount_release(&dh->dh_refcount)) { 143183080Sjhb sx_destroy(&dh->dh_lock); 144183080Sjhb free(dh, M_DIRHASH); 145183080Sjhb } 146183080Sjhb} 147183080Sjhb 14879561Siedowse/* 149178110Sjeff * Release the lock on a dirhash. 150178110Sjeff */ 151178110Sjeffstatic void 152178110Sjeffufsdirhash_release(struct dirhash *dh) 153178110Sjeff{ 154178110Sjeff 155183080Sjhb sx_unlock(&dh->dh_lock); 156178110Sjeff} 157178110Sjeff 158178110Sjeff/* 159178110Sjeff * Either acquire an existing hash locked shared or create a new hash and 160178110Sjeff * return it exclusively locked. May return NULL if the allocation fails. 161178110Sjeff * 162178110Sjeff * The vnode interlock is used to protect the i_dirhash pointer from 163178110Sjeff * simultaneous access while only a shared vnode lock is held. 164178110Sjeff */ 165178110Sjeffstatic struct dirhash * 166178110Sjeffufsdirhash_create(struct inode *ip) 167178110Sjeff{ 168178110Sjeff struct dirhash *ndh; 169178110Sjeff struct dirhash *dh; 170178110Sjeff struct vnode *vp; 171178110Sjeff int error; 172178110Sjeff 173178110Sjeff error = 0; 174178110Sjeff ndh = dh = NULL; 175178110Sjeff vp = ip->i_vnode; 176178110Sjeff for (;;) { 177178110Sjeff /* Racy check for i_dirhash to prefetch an dirhash structure. */ 178178110Sjeff if (ip->i_dirhash == NULL && ndh == NULL) { 179184205Sdes ndh = malloc(sizeof *dh, M_DIRHASH, 180178110Sjeff M_NOWAIT | M_ZERO); 181178110Sjeff if (ndh == NULL) 182178110Sjeff return (NULL); 183183080Sjhb refcount_init(&ndh->dh_refcount, 1); 184183080Sjhb sx_init(&ndh->dh_lock, "dirhash"); 185183080Sjhb sx_xlock(&ndh->dh_lock); 186178110Sjeff } 187178110Sjeff /* 188178110Sjeff * Check i_dirhash. If it's NULL just try to use a 189178110Sjeff * preallocated structure. If none exists loop and try again. 190178110Sjeff */ 191178110Sjeff VI_LOCK(vp); 192178110Sjeff dh = ip->i_dirhash; 193178110Sjeff if (dh == NULL) { 194178110Sjeff ip->i_dirhash = ndh; 195178110Sjeff VI_UNLOCK(vp); 196178110Sjeff if (ndh == NULL) 197178110Sjeff continue; 198178110Sjeff return (ndh); 199178110Sjeff } 200183080Sjhb ufsdirhash_hold(dh); 201183080Sjhb VI_UNLOCK(vp); 202183080Sjhb 203183080Sjhb /* Acquire a shared lock on existing hashes. */ 204183080Sjhb sx_slock(&dh->dh_lock); 205183080Sjhb 206178110Sjeff /* The hash could've been recycled while we were waiting. */ 207183080Sjhb VI_LOCK(vp); 208178110Sjeff if (ip->i_dirhash != dh) { 209183080Sjhb VI_UNLOCK(vp); 210178110Sjeff ufsdirhash_release(dh); 211183080Sjhb ufsdirhash_drop(dh); 212178110Sjeff continue; 213178110Sjeff } 214183080Sjhb VI_UNLOCK(vp); 215183080Sjhb ufsdirhash_drop(dh); 216183080Sjhb 217178110Sjeff /* If the hash is still valid we've succeeded. */ 218178110Sjeff if (dh->dh_hash != NULL) 219178110Sjeff break; 220178110Sjeff /* 221178110Sjeff * If the hash is NULL it has been recycled. Try to upgrade 222183080Sjhb * so we can recreate it. If we fail the upgrade, drop our 223183080Sjhb * lock and try again. 224178110Sjeff */ 225183080Sjhb if (sx_try_upgrade(&dh->dh_lock)) 226178110Sjeff break; 227183080Sjhb sx_sunlock(&dh->dh_lock); 228178110Sjeff } 229178110Sjeff /* Free the preallocated structure if it was not necessary. */ 230178110Sjeff if (ndh) { 231183080Sjhb ufsdirhash_release(ndh); 232183080Sjhb ufsdirhash_drop(ndh); 233178110Sjeff } 234178110Sjeff return (dh); 235178110Sjeff} 236178110Sjeff 237178110Sjeff/* 238178110Sjeff * Acquire an exclusive lock on an existing hash. Requires an exclusive 239178110Sjeff * vnode lock to protect the i_dirhash pointer. hashes that have been 240178110Sjeff * recycled are reclaimed here and NULL is returned. 241178110Sjeff */ 242178110Sjeffstatic struct dirhash * 243178110Sjeffufsdirhash_acquire(struct inode *ip) 244178110Sjeff{ 245178110Sjeff struct dirhash *dh; 246178110Sjeff struct vnode *vp; 247178110Sjeff 248178110Sjeff ASSERT_VOP_ELOCKED(ip->i_vnode, __FUNCTION__); 249178110Sjeff 250178110Sjeff vp = ip->i_vnode; 251178110Sjeff dh = ip->i_dirhash; 252178110Sjeff if (dh == NULL) 253178110Sjeff return (NULL); 254183080Sjhb sx_xlock(&dh->dh_lock); 255178110Sjeff if (dh->dh_hash != NULL) 256178110Sjeff return (dh); 257178110Sjeff ufsdirhash_free_locked(ip); 258178110Sjeff return (NULL); 259178110Sjeff} 260178110Sjeff 261178110Sjeff/* 262178110Sjeff * Acquire exclusively and free the hash pointed to by ip. Works with a 263178110Sjeff * shared or exclusive vnode lock. 264178110Sjeff */ 265178110Sjeffvoid 266178110Sjeffufsdirhash_free(struct inode *ip) 267178110Sjeff{ 268178110Sjeff struct dirhash *dh; 269178110Sjeff struct vnode *vp; 270178110Sjeff 271178110Sjeff vp = ip->i_vnode; 272178110Sjeff for (;;) { 273183080Sjhb /* Grab a reference on this inode's dirhash if it has one. */ 274178110Sjeff VI_LOCK(vp); 275178110Sjeff dh = ip->i_dirhash; 276178110Sjeff if (dh == NULL) { 277178110Sjeff VI_UNLOCK(vp); 278178110Sjeff return; 279178110Sjeff } 280183080Sjhb ufsdirhash_hold(dh); 281183080Sjhb VI_UNLOCK(vp); 282183080Sjhb 283183080Sjhb /* Exclusively lock the dirhash. */ 284183080Sjhb sx_xlock(&dh->dh_lock); 285183080Sjhb 286183080Sjhb /* If this dirhash still belongs to this inode, then free it. */ 287183080Sjhb VI_LOCK(vp); 288183080Sjhb if (ip->i_dirhash == dh) { 289183080Sjhb VI_UNLOCK(vp); 290183080Sjhb ufsdirhash_drop(dh); 291178110Sjeff break; 292183080Sjhb } 293183080Sjhb VI_UNLOCK(vp); 294183080Sjhb 295183080Sjhb /* 296183080Sjhb * This inode's dirhash has changed while we were 297183080Sjhb * waiting for the dirhash lock, so try again. 298183080Sjhb */ 299178110Sjeff ufsdirhash_release(dh); 300183080Sjhb ufsdirhash_drop(dh); 301178110Sjeff } 302178110Sjeff ufsdirhash_free_locked(ip); 303178110Sjeff} 304178110Sjeff 305178110Sjeff/* 30679561Siedowse * Attempt to build up a hash table for the directory contents in 30779561Siedowse * inode 'ip'. Returns 0 on success, or -1 of the operation failed. 30879561Siedowse */ 30979561Siedowseint 31079561Siedowseufsdirhash_build(struct inode *ip) 31179561Siedowse{ 31279561Siedowse struct dirhash *dh; 31379561Siedowse struct buf *bp = NULL; 31479561Siedowse struct direct *ep; 31579561Siedowse struct vnode *vp; 31679561Siedowse doff_t bmask, pos; 31779561Siedowse int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot; 31879561Siedowse 319178110Sjeff /* Take care of a decreased sysctl value. */ 320178110Sjeff while (ufs_dirhashmem > ufs_dirhashmaxmem) 321178110Sjeff if (ufsdirhash_recycle(0) != 0) 322178110Sjeff return (-1); 323178110Sjeff 32479561Siedowse /* Check if we can/should use dirhash. */ 325178110Sjeff if (ip->i_size < ufs_mindirhashsize || OFSFMT(ip->i_vnode) || 326178110Sjeff ip->i_effnlink == 0) { 327178110Sjeff if (ip->i_dirhash) 32879561Siedowse ufsdirhash_free(ip); 329178110Sjeff return (-1); 33079561Siedowse } 331178110Sjeff dh = ufsdirhash_create(ip); 332178110Sjeff if (dh == NULL) 33382364Siedowse return (-1); 334178110Sjeff if (dh->dh_hash != NULL) 335178110Sjeff return (0); 33682364Siedowse 33779561Siedowse vp = ip->i_vnode; 33879561Siedowse /* Allocate 50% more entries than this dir size could ever need. */ 33979561Siedowse KASSERT(ip->i_size >= DIRBLKSIZ, ("ufsdirhash_build size")); 34079561Siedowse nslots = ip->i_size / DIRECTSIZ(1); 34179561Siedowse nslots = (nslots * 3 + 1) / 2; 34279561Siedowse narrays = howmany(nslots, DH_NBLKOFF); 34379561Siedowse nslots = narrays * DH_NBLKOFF; 34479561Siedowse dirblocks = howmany(ip->i_size, DIRBLKSIZ); 34579561Siedowse nblocks = (dirblocks * 3 + 1) / 2; 34679561Siedowse memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) + 34779561Siedowse narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 34879561Siedowse nblocks * sizeof(*dh->dh_blkfree); 349125854Sdwmalone DIRHASHLIST_LOCK(); 35079561Siedowse if (memreqd + ufs_dirhashmem > ufs_dirhashmaxmem) { 351125854Sdwmalone DIRHASHLIST_UNLOCK(); 35279561Siedowse if (memreqd > ufs_dirhashmaxmem / 2) 353178110Sjeff goto fail; 35479561Siedowse /* Try to free some space. */ 35579561Siedowse if (ufsdirhash_recycle(memreqd) != 0) 356178110Sjeff goto fail; 357125854Sdwmalone /* Enough was freed, and list has been locked. */ 35879561Siedowse } 35979561Siedowse ufs_dirhashmem += memreqd; 360125854Sdwmalone DIRHASHLIST_UNLOCK(); 36179561Siedowse 362178110Sjeff /* Initialise the hash table and block statistics. */ 363178110Sjeff dh->dh_memreq = memreqd; 364178110Sjeff dh->dh_narrays = narrays; 365178110Sjeff dh->dh_hlen = nslots; 366178110Sjeff dh->dh_nblk = nblocks; 367178110Sjeff dh->dh_dirblks = dirblocks; 368178110Sjeff for (i = 0; i < DH_NFSTATS; i++) 369178110Sjeff dh->dh_firstfree[i] = -1; 370178110Sjeff dh->dh_firstfree[DH_NFSTATS] = 0; 371178110Sjeff dh->dh_hused = 0; 372178110Sjeff dh->dh_seqopt = 0; 373178110Sjeff dh->dh_seqoff = 0; 374178110Sjeff dh->dh_score = DH_SCOREINIT; 375178110Sjeff 37679561Siedowse /* 37779561Siedowse * Use non-blocking mallocs so that we will revert to a linear 37879561Siedowse * lookup on failure rather than potentially blocking forever. 37979561Siedowse */ 380184205Sdes dh->dh_hash = malloc(narrays * sizeof(dh->dh_hash[0]), 38179561Siedowse M_DIRHASH, M_NOWAIT | M_ZERO); 382178110Sjeff if (dh->dh_hash == NULL) 383178110Sjeff goto fail; 384184205Sdes dh->dh_blkfree = malloc(nblocks * sizeof(dh->dh_blkfree[0]), 38579561Siedowse M_DIRHASH, M_NOWAIT); 386178110Sjeff if (dh->dh_blkfree == NULL) 38779561Siedowse goto fail; 38879561Siedowse for (i = 0; i < narrays; i++) { 389125854Sdwmalone if ((dh->dh_hash[i] = DIRHASH_BLKALLOC_WAITOK()) == NULL) 39079561Siedowse goto fail; 39179561Siedowse for (j = 0; j < DH_NBLKOFF; j++) 39279561Siedowse dh->dh_hash[i][j] = DIRHASH_EMPTY; 39379561Siedowse } 39479561Siedowse for (i = 0; i < dirblocks; i++) 39579561Siedowse dh->dh_blkfree[i] = DIRBLKSIZ / DIRALIGN; 39679561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 39779561Siedowse pos = 0; 39879561Siedowse while (pos < ip->i_size) { 39979561Siedowse /* If necessary, get the next directory block. */ 40079561Siedowse if ((pos & bmask) == 0) { 40179561Siedowse if (bp != NULL) 40279561Siedowse brelse(bp); 40379561Siedowse if (UFS_BLKATOFF(vp, (off_t)pos, NULL, &bp) != 0) 40479561Siedowse goto fail; 40579561Siedowse } 40679561Siedowse 40779561Siedowse /* Add this entry to the hash. */ 40879561Siedowse ep = (struct direct *)((char *)bp->b_data + (pos & bmask)); 40979561Siedowse if (ep->d_reclen == 0 || ep->d_reclen > 41079561Siedowse DIRBLKSIZ - (pos & (DIRBLKSIZ - 1))) { 41179561Siedowse /* Corrupted directory. */ 41279561Siedowse brelse(bp); 41379561Siedowse goto fail; 41479561Siedowse } 41579561Siedowse if (ep->d_ino != 0) { 41679561Siedowse /* Add the entry (simplified ufsdirhash_add). */ 41779561Siedowse slot = ufsdirhash_hash(dh, ep->d_name, ep->d_namlen); 41879561Siedowse while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 41979561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 42079561Siedowse dh->dh_hused++; 42179561Siedowse DH_ENTRY(dh, slot) = pos; 42279561Siedowse ufsdirhash_adjfree(dh, pos, -DIRSIZ(0, ep)); 42379561Siedowse } 42479561Siedowse pos += ep->d_reclen; 42579561Siedowse } 42679561Siedowse 42779561Siedowse if (bp != NULL) 42879561Siedowse brelse(bp); 429125854Sdwmalone DIRHASHLIST_LOCK(); 43079561Siedowse TAILQ_INSERT_TAIL(&ufsdirhash_list, dh, dh_list); 43179561Siedowse dh->dh_onlist = 1; 432125854Sdwmalone DIRHASHLIST_UNLOCK(); 433183080Sjhb sx_downgrade(&dh->dh_lock); 43479561Siedowse return (0); 43579561Siedowse 43679561Siedowsefail: 437178110Sjeff ufsdirhash_free_locked(ip); 43879561Siedowse return (-1); 43979561Siedowse} 44079561Siedowse 44179561Siedowse/* 44279561Siedowse * Free any hash table associated with inode 'ip'. 44379561Siedowse */ 444178110Sjeffstatic void 445178110Sjeffufsdirhash_free_locked(struct inode *ip) 44679561Siedowse{ 44779561Siedowse struct dirhash *dh; 448178110Sjeff struct vnode *vp; 449178110Sjeff int i; 45079561Siedowse 451178110Sjeff DIRHASH_ASSERT_LOCKED(ip->i_dirhash); 452183080Sjhb 453178110Sjeff /* 454178110Sjeff * Clear the pointer in the inode to prevent new threads from 455178110Sjeff * finding the dead structure. 456178110Sjeff */ 457178110Sjeff vp = ip->i_vnode; 458178110Sjeff VI_LOCK(vp); 459178110Sjeff dh = ip->i_dirhash; 460178110Sjeff ip->i_dirhash = NULL; 461178110Sjeff VI_UNLOCK(vp); 462183080Sjhb 463178110Sjeff /* 464183280Sjhb * Remove the hash from the list since we are going to free its 465183280Sjhb * memory. 466183280Sjhb */ 467183280Sjhb DIRHASHLIST_LOCK(); 468183280Sjhb if (dh->dh_onlist) 469183280Sjhb TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 470183280Sjhb ufs_dirhashmem -= dh->dh_memreq; 471183280Sjhb DIRHASHLIST_UNLOCK(); 472183280Sjhb 473183280Sjhb /* 474183080Sjhb * At this point, any waiters for the lock should hold their 475183080Sjhb * own reference on the dirhash structure. They will drop 476183080Sjhb * that reference once they grab the vnode interlock and see 477183080Sjhb * that ip->i_dirhash is NULL. 478178110Sjeff */ 479183080Sjhb sx_xunlock(&dh->dh_lock); 480183080Sjhb 481178110Sjeff /* 482178110Sjeff * Handle partially recycled as well as fully constructed hashes. 483178110Sjeff */ 484178110Sjeff if (dh->dh_hash != NULL) { 485178110Sjeff for (i = 0; i < dh->dh_narrays; i++) 486178110Sjeff if (dh->dh_hash[i] != NULL) 487178110Sjeff DIRHASH_BLKFREE(dh->dh_hash[i]); 488184205Sdes free(dh->dh_hash, M_DIRHASH); 489178110Sjeff if (dh->dh_blkfree != NULL) 490184205Sdes free(dh->dh_blkfree, M_DIRHASH); 491178110Sjeff } 492183080Sjhb 493178110Sjeff /* 494183080Sjhb * Drop the inode's reference to the data structure. 495178110Sjeff */ 496183080Sjhb ufsdirhash_drop(dh); 49779561Siedowse} 49879561Siedowse 49979561Siedowse/* 50079561Siedowse * Find the offset of the specified name within the given inode. 50179561Siedowse * Returns 0 on success, ENOENT if the entry does not exist, or 50279561Siedowse * EJUSTRETURN if the caller should revert to a linear search. 50379561Siedowse * 50479690Siedowse * If successful, the directory offset is stored in *offp, and a 50579690Siedowse * pointer to a struct buf containing the entry is stored in *bpp. If 50679561Siedowse * prevoffp is non-NULL, the offset of the previous entry within 50779561Siedowse * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry 50879561Siedowse * is the first in a block, the start of the block is used). 509178110Sjeff * 510178110Sjeff * Must be called with the hash locked. Returns with the hash unlocked. 51179561Siedowse */ 51279561Siedowseint 51379561Siedowseufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp, 51479690Siedowse struct buf **bpp, doff_t *prevoffp) 51579561Siedowse{ 51679561Siedowse struct dirhash *dh, *dh_next; 51779561Siedowse struct direct *dp; 51879561Siedowse struct vnode *vp; 51979561Siedowse struct buf *bp; 52079561Siedowse doff_t blkoff, bmask, offset, prevoff; 52179561Siedowse int i, slot; 522178110Sjeff int error; 52379561Siedowse 524178110Sjeff dh = ip->i_dirhash; 525178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 526178110Sjeff ("ufsdirhash_lookup: Invalid dirhash %p\n", dh)); 527178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 52879561Siedowse /* 52979561Siedowse * Move this dirhash towards the end of the list if it has a 530178110Sjeff * score higher than the next entry, and acquire the dh_lock. 53179561Siedowse */ 532178110Sjeff DIRHASHLIST_LOCK(); 53379561Siedowse if (TAILQ_NEXT(dh, dh_list) != NULL) { 53479561Siedowse /* 53579561Siedowse * If the new score will be greater than that of the next 53679561Siedowse * entry, then move this entry past it. With both mutexes 53779561Siedowse * held, dh_next won't go away, but its dh_score could 53879561Siedowse * change; that's not important since it is just a hint. 53979561Siedowse */ 540178110Sjeff if ((dh_next = TAILQ_NEXT(dh, dh_list)) != NULL && 54179561Siedowse dh->dh_score >= dh_next->dh_score) { 54279561Siedowse KASSERT(dh->dh_onlist, ("dirhash: not on list")); 54379561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 54479561Siedowse TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh, 54579561Siedowse dh_list); 54679561Siedowse } 54779561Siedowse } 54879561Siedowse /* Update the score. */ 54979561Siedowse if (dh->dh_score < DH_SCOREMAX) 55079561Siedowse dh->dh_score++; 551178110Sjeff DIRHASHLIST_UNLOCK(); 55279561Siedowse 55379561Siedowse vp = ip->i_vnode; 55479561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 55579561Siedowse blkoff = -1; 55679561Siedowse bp = NULL; 55779561Siedowserestart: 55879561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 55979561Siedowse 56079561Siedowse if (dh->dh_seqopt) { 56179561Siedowse /* 56279561Siedowse * Sequential access optimisation. dh_seqoff contains the 56379561Siedowse * offset of the directory entry immediately following 56479561Siedowse * the last entry that was looked up. Check if this offset 56579561Siedowse * appears in the hash chain for the name we are looking for. 56679561Siedowse */ 56779561Siedowse for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY; 56879561Siedowse i = WRAPINCR(i, dh->dh_hlen)) 56986350Siedowse if (offset == dh->dh_seqoff) 57079561Siedowse break; 57179561Siedowse if (offset == dh->dh_seqoff) { 57279561Siedowse /* 57379561Siedowse * We found an entry with the expected offset. This 57479561Siedowse * is probably the entry we want, but if not, the 575149178Siedowse * code below will turn off seqopt and retry. 57679561Siedowse */ 57779561Siedowse slot = i; 57879561Siedowse } else 57979561Siedowse dh->dh_seqopt = 0; 58079561Siedowse } 58179561Siedowse 58279561Siedowse for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY; 58379561Siedowse slot = WRAPINCR(slot, dh->dh_hlen)) { 58479561Siedowse if (offset == DIRHASH_DEL) 58579561Siedowse continue; 58679561Siedowse if (offset < 0 || offset >= ip->i_size) 58779561Siedowse panic("ufsdirhash_lookup: bad offset in hash array"); 58879561Siedowse if ((offset & ~bmask) != blkoff) { 58979561Siedowse if (bp != NULL) 59079561Siedowse brelse(bp); 59179561Siedowse blkoff = offset & ~bmask; 592178110Sjeff if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) { 593178110Sjeff error = EJUSTRETURN; 594178110Sjeff goto fail; 595178110Sjeff } 59679561Siedowse } 59779561Siedowse dp = (struct direct *)(bp->b_data + (offset & bmask)); 59879561Siedowse if (dp->d_reclen == 0 || dp->d_reclen > 59979561Siedowse DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) { 60079561Siedowse /* Corrupted directory. */ 601178110Sjeff error = EJUSTRETURN; 602178110Sjeff goto fail; 60379561Siedowse } 60479561Siedowse if (dp->d_namlen == namelen && 60579561Siedowse bcmp(dp->d_name, name, namelen) == 0) { 60679561Siedowse /* Found. Get the prev offset if needed. */ 60779561Siedowse if (prevoffp != NULL) { 60879561Siedowse if (offset & (DIRBLKSIZ - 1)) { 60979561Siedowse prevoff = ufsdirhash_getprev(dp, 61079561Siedowse offset); 61179561Siedowse if (prevoff == -1) { 612178110Sjeff error = EJUSTRETURN; 613178110Sjeff goto fail; 61479561Siedowse } 61579561Siedowse } else 61679561Siedowse prevoff = offset; 61779561Siedowse *prevoffp = prevoff; 61879561Siedowse } 61979561Siedowse 62079561Siedowse /* Check for sequential access, and update offset. */ 62179561Siedowse if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset) 62279561Siedowse dh->dh_seqopt = 1; 62379561Siedowse dh->dh_seqoff = offset + DIRSIZ(0, dp); 62479690Siedowse *bpp = bp; 62579561Siedowse *offp = offset; 626178110Sjeff ufsdirhash_release(dh); 62779561Siedowse return (0); 62879561Siedowse } 62979561Siedowse 63079561Siedowse /* 63179561Siedowse * When the name doesn't match in the seqopt case, go back 63279561Siedowse * and search normally. 63379561Siedowse */ 63479561Siedowse if (dh->dh_seqopt) { 63579561Siedowse dh->dh_seqopt = 0; 63679561Siedowse goto restart; 63779561Siedowse } 63879561Siedowse } 639178110Sjeff error = ENOENT; 640178110Sjefffail: 641178110Sjeff ufsdirhash_release(dh); 64279561Siedowse if (bp != NULL) 64379561Siedowse brelse(bp); 644178110Sjeff return (error); 64579561Siedowse} 64679561Siedowse 64779561Siedowse/* 64879561Siedowse * Find a directory block with room for 'slotneeded' bytes. Returns 64979561Siedowse * the offset of the directory entry that begins the free space. 65079561Siedowse * This will either be the offset of an existing entry that has free 65179561Siedowse * space at the end, or the offset of an entry with d_ino == 0 at 65279561Siedowse * the start of a DIRBLKSIZ block. 65379561Siedowse * 65479561Siedowse * To use the space, the caller may need to compact existing entries in 65579561Siedowse * the directory. The total number of bytes in all of the entries involved 65679561Siedowse * in the compaction is stored in *slotsize. In other words, all of 65779561Siedowse * the entries that must be compacted are exactly contained in the 65879561Siedowse * region beginning at the returned offset and spanning *slotsize bytes. 65979561Siedowse * 66079561Siedowse * Returns -1 if no space was found, indicating that the directory 66179561Siedowse * must be extended. 66279561Siedowse */ 66379561Siedowsedoff_t 66479561Siedowseufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize) 66579561Siedowse{ 66679561Siedowse struct direct *dp; 66779561Siedowse struct dirhash *dh; 66879561Siedowse struct buf *bp; 66979561Siedowse doff_t pos, slotstart; 67079561Siedowse int dirblock, error, freebytes, i; 67179561Siedowse 672178110Sjeff dh = ip->i_dirhash; 673178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 674178110Sjeff ("ufsdirhash_findfree: Invalid dirhash %p\n", dh)); 675178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 67679561Siedowse 67779561Siedowse /* Find a directory block with the desired free space. */ 67879561Siedowse dirblock = -1; 67979561Siedowse for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++) 68079561Siedowse if ((dirblock = dh->dh_firstfree[i]) != -1) 68179561Siedowse break; 682178110Sjeff if (dirblock == -1) 68379561Siedowse return (-1); 68479561Siedowse 68579561Siedowse KASSERT(dirblock < dh->dh_nblk && 68679561Siedowse dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN), 68779561Siedowse ("ufsdirhash_findfree: bad stats")); 68879561Siedowse pos = dirblock * DIRBLKSIZ; 68979561Siedowse error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp); 69079561Siedowse if (error) 69179561Siedowse return (-1); 69279561Siedowse 69379561Siedowse /* Find the first entry with free space. */ 69479561Siedowse for (i = 0; i < DIRBLKSIZ; ) { 69579561Siedowse if (dp->d_reclen == 0) { 69679561Siedowse brelse(bp); 69779561Siedowse return (-1); 69879561Siedowse } 69979561Siedowse if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp)) 70079561Siedowse break; 70179561Siedowse i += dp->d_reclen; 70279561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 70379561Siedowse } 70479561Siedowse if (i > DIRBLKSIZ) { 70579561Siedowse brelse(bp); 70679561Siedowse return (-1); 70779561Siedowse } 70879561Siedowse slotstart = pos + i; 70979561Siedowse 71079561Siedowse /* Find the range of entries needed to get enough space */ 71179561Siedowse freebytes = 0; 71279561Siedowse while (i < DIRBLKSIZ && freebytes < slotneeded) { 71379561Siedowse freebytes += dp->d_reclen; 71479561Siedowse if (dp->d_ino != 0) 71579561Siedowse freebytes -= DIRSIZ(0, dp); 71679561Siedowse if (dp->d_reclen == 0) { 71779561Siedowse brelse(bp); 71879561Siedowse return (-1); 71979561Siedowse } 72079561Siedowse i += dp->d_reclen; 72179561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 72279561Siedowse } 72379561Siedowse if (i > DIRBLKSIZ) { 72479561Siedowse brelse(bp); 72579561Siedowse return (-1); 72679561Siedowse } 72779561Siedowse if (freebytes < slotneeded) 72879561Siedowse panic("ufsdirhash_findfree: free mismatch"); 72979561Siedowse brelse(bp); 73079561Siedowse *slotsize = pos + i - slotstart; 73179561Siedowse return (slotstart); 73279561Siedowse} 73379561Siedowse 73479561Siedowse/* 73579561Siedowse * Return the start of the unused space at the end of a directory, or 73679561Siedowse * -1 if there are no trailing unused blocks. 73779561Siedowse */ 73879561Siedowsedoff_t 73979561Siedowseufsdirhash_enduseful(struct inode *ip) 74079561Siedowse{ 74179561Siedowse 74279561Siedowse struct dirhash *dh; 74379561Siedowse int i; 74479561Siedowse 745178110Sjeff dh = ip->i_dirhash; 746178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 747178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 748178110Sjeff ("ufsdirhash_enduseful: Invalid dirhash %p\n", dh)); 74979561Siedowse 750178110Sjeff if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN) 75179561Siedowse return (-1); 75279561Siedowse 75379561Siedowse for (i = dh->dh_dirblks - 1; i >= 0; i--) 75479561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 75579561Siedowse break; 756178110Sjeff 75779561Siedowse return ((doff_t)(i + 1) * DIRBLKSIZ); 75879561Siedowse} 75979561Siedowse 76079561Siedowse/* 76179561Siedowse * Insert information into the hash about a new directory entry. dirp 76279561Siedowse * points to a struct direct containing the entry, and offset specifies 76379561Siedowse * the offset of this entry. 76479561Siedowse */ 76579561Siedowsevoid 76679561Siedowseufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset) 76779561Siedowse{ 76879561Siedowse struct dirhash *dh; 76979561Siedowse int slot; 77079561Siedowse 771178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 77279561Siedowse return; 773178110Sjeff 77479561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 77579561Siedowse ("ufsdirhash_add: bad offset")); 77679561Siedowse /* 77779561Siedowse * Normal hash usage is < 66%. If the usage gets too high then 77879561Siedowse * remove the hash entirely and let it be rebuilt later. 77979561Siedowse */ 78079561Siedowse if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) { 781178110Sjeff ufsdirhash_free_locked(ip); 78279561Siedowse return; 78379561Siedowse } 78479561Siedowse 78579561Siedowse /* Find a free hash slot (empty or deleted), and add the entry. */ 78679561Siedowse slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen); 78779561Siedowse while (DH_ENTRY(dh, slot) >= 0) 78879561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 78979561Siedowse if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY) 79079561Siedowse dh->dh_hused++; 79179561Siedowse DH_ENTRY(dh, slot) = offset; 79279561Siedowse 79379561Siedowse /* Update the per-block summary info. */ 79479561Siedowse ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp)); 795178110Sjeff ufsdirhash_release(dh); 79679561Siedowse} 79779561Siedowse 79879561Siedowse/* 79979561Siedowse * Remove the specified directory entry from the hash. The entry to remove 80079561Siedowse * is defined by the name in `dirp', which must exist at the specified 80179561Siedowse * `offset' within the directory. 80279561Siedowse */ 80379561Siedowsevoid 80479561Siedowseufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset) 80579561Siedowse{ 80679561Siedowse struct dirhash *dh; 80779561Siedowse int slot; 80879561Siedowse 809178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 81079561Siedowse return; 81179561Siedowse 81279561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 81379561Siedowse ("ufsdirhash_remove: bad offset")); 81479561Siedowse /* Find the entry */ 81579561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset); 81679561Siedowse 81779561Siedowse /* Remove the hash entry. */ 81879561Siedowse ufsdirhash_delslot(dh, slot); 81979561Siedowse 82079561Siedowse /* Update the per-block summary info. */ 82179561Siedowse ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp)); 822178110Sjeff ufsdirhash_release(dh); 82379561Siedowse} 82479561Siedowse 82579561Siedowse/* 82679561Siedowse * Change the offset associated with a directory entry in the hash. Used 82779561Siedowse * when compacting directory blocks. 82879561Siedowse */ 82979561Siedowsevoid 83079561Siedowseufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff, 83179561Siedowse doff_t newoff) 83279561Siedowse{ 83379561Siedowse struct dirhash *dh; 83479561Siedowse int slot; 83579561Siedowse 836178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 83779561Siedowse return; 83879561Siedowse 83979561Siedowse KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ && 84079561Siedowse newoff < dh->dh_dirblks * DIRBLKSIZ, 84179561Siedowse ("ufsdirhash_move: bad offset")); 84279561Siedowse /* Find the entry, and update the offset. */ 84379561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff); 84479561Siedowse DH_ENTRY(dh, slot) = newoff; 845178110Sjeff ufsdirhash_release(dh); 84679561Siedowse} 84779561Siedowse 84879561Siedowse/* 84979561Siedowse * Inform dirhash that the directory has grown by one block that 85079561Siedowse * begins at offset (i.e. the new length is offset + DIRBLKSIZ). 85179561Siedowse */ 85279561Siedowsevoid 85379561Siedowseufsdirhash_newblk(struct inode *ip, doff_t offset) 85479561Siedowse{ 85579561Siedowse struct dirhash *dh; 85679561Siedowse int block; 85779561Siedowse 858178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 85979561Siedowse return; 86079561Siedowse 86179561Siedowse KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ, 86279561Siedowse ("ufsdirhash_newblk: bad offset")); 86379561Siedowse block = offset / DIRBLKSIZ; 86479561Siedowse if (block >= dh->dh_nblk) { 86579561Siedowse /* Out of space; must rebuild. */ 866178110Sjeff ufsdirhash_free_locked(ip); 86779561Siedowse return; 86879561Siedowse } 86979561Siedowse dh->dh_dirblks = block + 1; 87079561Siedowse 87179561Siedowse /* Account for the new free block. */ 87279561Siedowse dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN; 87379561Siedowse if (dh->dh_firstfree[DH_NFSTATS] == -1) 87479561Siedowse dh->dh_firstfree[DH_NFSTATS] = block; 875178110Sjeff ufsdirhash_release(dh); 87679561Siedowse} 87779561Siedowse 87879561Siedowse/* 87979561Siedowse * Inform dirhash that the directory is being truncated. 88079561Siedowse */ 88179561Siedowsevoid 88279561Siedowseufsdirhash_dirtrunc(struct inode *ip, doff_t offset) 88379561Siedowse{ 88479561Siedowse struct dirhash *dh; 88579561Siedowse int block, i; 88679561Siedowse 887178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 88879561Siedowse return; 88979561Siedowse 89079561Siedowse KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ, 89179561Siedowse ("ufsdirhash_dirtrunc: bad offset")); 89279561Siedowse block = howmany(offset, DIRBLKSIZ); 89379561Siedowse /* 89479561Siedowse * If the directory shrinks to less than 1/8 of dh_nblk blocks 89579561Siedowse * (about 20% of its original size due to the 50% extra added in 89679561Siedowse * ufsdirhash_build) then free it, and let the caller rebuild 89779561Siedowse * if necessary. 89879561Siedowse */ 89979561Siedowse if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) { 900178110Sjeff ufsdirhash_free_locked(ip); 90179561Siedowse return; 90279561Siedowse } 90379561Siedowse 90479561Siedowse /* 90579561Siedowse * Remove any `first free' information pertaining to the 90679561Siedowse * truncated blocks. All blocks we're removing should be 90779561Siedowse * completely unused. 90879561Siedowse */ 90979561Siedowse if (dh->dh_firstfree[DH_NFSTATS] >= block) 91079561Siedowse dh->dh_firstfree[DH_NFSTATS] = -1; 91179561Siedowse for (i = block; i < dh->dh_dirblks; i++) 91279561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 91379561Siedowse panic("ufsdirhash_dirtrunc: blocks in use"); 91479561Siedowse for (i = 0; i < DH_NFSTATS; i++) 91579561Siedowse if (dh->dh_firstfree[i] >= block) 91679561Siedowse panic("ufsdirhash_dirtrunc: first free corrupt"); 91779561Siedowse dh->dh_dirblks = block; 918178110Sjeff ufsdirhash_release(dh); 91979561Siedowse} 92079561Siedowse 92179561Siedowse/* 92279561Siedowse * Debugging function to check that the dirhash information about 92379561Siedowse * a directory block matches its actual contents. Panics if a mismatch 92479561Siedowse * is detected. 92579561Siedowse * 92679561Siedowse * On entry, `buf' should point to the start of an in-core 92779561Siedowse * DIRBLKSIZ-sized directory block, and `offset' should contain the 92879561Siedowse * offset from the start of the directory of that block. 92979561Siedowse */ 93079561Siedowsevoid 93179561Siedowseufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset) 93279561Siedowse{ 93379561Siedowse struct dirhash *dh; 93479561Siedowse struct direct *dp; 93579561Siedowse int block, ffslot, i, nfree; 93679561Siedowse 93779561Siedowse if (!ufs_dirhashcheck) 93879561Siedowse return; 939178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 94079561Siedowse return; 94179561Siedowse 94279561Siedowse block = offset / DIRBLKSIZ; 94379561Siedowse if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks) 94479561Siedowse panic("ufsdirhash_checkblock: bad offset"); 94579561Siedowse 94679561Siedowse nfree = 0; 94779561Siedowse for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) { 94879561Siedowse dp = (struct direct *)(buf + i); 94979561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ) 95079561Siedowse panic("ufsdirhash_checkblock: bad dir"); 95179561Siedowse 95279561Siedowse if (dp->d_ino == 0) { 95380456Siedowse#if 0 95480456Siedowse /* 95580456Siedowse * XXX entries with d_ino == 0 should only occur 95680456Siedowse * at the start of a DIRBLKSIZ block. However the 95780456Siedowse * ufs code is tolerant of such entries at other 95880456Siedowse * offsets, and fsck does not fix them. 95980456Siedowse */ 96079561Siedowse if (i != 0) 96179561Siedowse panic("ufsdirhash_checkblock: bad dir inode"); 96280456Siedowse#endif 96379561Siedowse nfree += dp->d_reclen; 96479561Siedowse continue; 96579561Siedowse } 96679561Siedowse 96779561Siedowse /* Check that the entry exists (will panic if it doesn't). */ 96879561Siedowse ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i); 96979561Siedowse 97079561Siedowse nfree += dp->d_reclen - DIRSIZ(0, dp); 97179561Siedowse } 97279561Siedowse if (i != DIRBLKSIZ) 97379561Siedowse panic("ufsdirhash_checkblock: bad dir end"); 97479561Siedowse 97579561Siedowse if (dh->dh_blkfree[block] * DIRALIGN != nfree) 97679561Siedowse panic("ufsdirhash_checkblock: bad free count"); 97779561Siedowse 97892098Siedowse ffslot = BLKFREE2IDX(nfree / DIRALIGN); 97979561Siedowse for (i = 0; i <= DH_NFSTATS; i++) 98079561Siedowse if (dh->dh_firstfree[i] == block && i != ffslot) 98179561Siedowse panic("ufsdirhash_checkblock: bad first-free"); 98292098Siedowse if (dh->dh_firstfree[ffslot] == -1) 98392098Siedowse panic("ufsdirhash_checkblock: missing first-free entry"); 984178110Sjeff ufsdirhash_release(dh); 98579561Siedowse} 98679561Siedowse 98779561Siedowse/* 98879561Siedowse * Hash the specified filename into a dirhash slot. 98979561Siedowse */ 99079561Siedowsestatic int 99179561Siedowseufsdirhash_hash(struct dirhash *dh, char *name, int namelen) 99279561Siedowse{ 99392807Sdwmalone u_int32_t hash; 99492807Sdwmalone 99592807Sdwmalone /* 996107868Siedowse * We hash the name and then some other bit of data that is 997107868Siedowse * invariant over the dirhash's lifetime. Otherwise names 99892807Sdwmalone * differing only in the last byte are placed close to one 99992807Sdwmalone * another in the table, which is bad for linear probing. 100092807Sdwmalone */ 100192807Sdwmalone hash = fnv_32_buf(name, namelen, FNV1_32_INIT); 1002133837Sdwmalone hash = fnv_32_buf(&dh, sizeof(dh), hash); 100392807Sdwmalone return (hash % dh->dh_hlen); 100479561Siedowse} 100579561Siedowse 100679561Siedowse/* 100779561Siedowse * Adjust the number of free bytes in the block containing `offset' 100879561Siedowse * by the value specified by `diff'. 100979561Siedowse * 101079561Siedowse * The caller must ensure we have exclusive access to `dh'; normally 1011178110Sjeff * that means that dh_lock should be held, but this is also called 101279561Siedowse * from ufsdirhash_build() where exclusive access can be assumed. 101379561Siedowse */ 101479561Siedowsestatic void 101579561Siedowseufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff) 101679561Siedowse{ 101779561Siedowse int block, i, nfidx, ofidx; 101879561Siedowse 101979561Siedowse /* Update the per-block summary info. */ 102079561Siedowse block = offset / DIRBLKSIZ; 102179561Siedowse KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks, 102279561Siedowse ("dirhash bad offset")); 102392098Siedowse ofidx = BLKFREE2IDX(dh->dh_blkfree[block]); 102479561Siedowse dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN); 102592098Siedowse nfidx = BLKFREE2IDX(dh->dh_blkfree[block]); 102679561Siedowse 102779561Siedowse /* Update the `first free' list if necessary. */ 102879561Siedowse if (ofidx != nfidx) { 102979561Siedowse /* If removing, scan forward for the next block. */ 103079561Siedowse if (dh->dh_firstfree[ofidx] == block) { 103179561Siedowse for (i = block + 1; i < dh->dh_dirblks; i++) 103292098Siedowse if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx) 103379561Siedowse break; 103479561Siedowse dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1; 103579561Siedowse } 103679561Siedowse 103779561Siedowse /* Make this the new `first free' if necessary */ 103879561Siedowse if (dh->dh_firstfree[nfidx] > block || 103979561Siedowse dh->dh_firstfree[nfidx] == -1) 104079561Siedowse dh->dh_firstfree[nfidx] = block; 104179561Siedowse } 104279561Siedowse} 104379561Siedowse 104479561Siedowse/* 104579561Siedowse * Find the specified name which should have the specified offset. 104679561Siedowse * Returns a slot number, and panics on failure. 104779561Siedowse * 104879561Siedowse * `dh' must be locked on entry and remains so on return. 104979561Siedowse */ 105079561Siedowsestatic int 105179561Siedowseufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset) 105279561Siedowse{ 105379561Siedowse int slot; 105479561Siedowse 1055178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 105679561Siedowse 105779561Siedowse /* Find the entry. */ 105879561Siedowse KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full")); 105979561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 106079561Siedowse while (DH_ENTRY(dh, slot) != offset && 106179561Siedowse DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 106279561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 106379561Siedowse if (DH_ENTRY(dh, slot) != offset) 106479561Siedowse panic("ufsdirhash_findslot: '%.*s' not found", namelen, name); 106579561Siedowse 106679561Siedowse return (slot); 106779561Siedowse} 106879561Siedowse 106979561Siedowse/* 107079561Siedowse * Remove the entry corresponding to the specified slot from the hash array. 107179561Siedowse * 107279561Siedowse * `dh' must be locked on entry and remains so on return. 107379561Siedowse */ 107479561Siedowsestatic void 107579561Siedowseufsdirhash_delslot(struct dirhash *dh, int slot) 107679561Siedowse{ 107779561Siedowse int i; 107879561Siedowse 1079178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 108079561Siedowse 108179561Siedowse /* Mark the entry as deleted. */ 108279561Siedowse DH_ENTRY(dh, slot) = DIRHASH_DEL; 108379561Siedowse 108479561Siedowse /* If this is the end of a chain of DIRHASH_DEL slots, remove them. */ 108579561Siedowse for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; ) 108679561Siedowse i = WRAPINCR(i, dh->dh_hlen); 108779561Siedowse if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) { 108892807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 108992807Sdwmalone while (DH_ENTRY(dh, i) == DIRHASH_DEL) { 109079561Siedowse DH_ENTRY(dh, i) = DIRHASH_EMPTY; 109179561Siedowse dh->dh_hused--; 109292807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 109379561Siedowse } 109479561Siedowse KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen")); 109579561Siedowse } 109679561Siedowse} 109779561Siedowse 109879561Siedowse/* 109979561Siedowse * Given a directory entry and its offset, find the offset of the 110079561Siedowse * previous entry in the same DIRBLKSIZ-sized block. Returns an 110179561Siedowse * offset, or -1 if there is no previous entry in the block or some 110279561Siedowse * other problem occurred. 110379561Siedowse */ 110479561Siedowsestatic doff_t 110579561Siedowseufsdirhash_getprev(struct direct *dirp, doff_t offset) 110679561Siedowse{ 110779561Siedowse struct direct *dp; 110879561Siedowse char *blkbuf; 110979561Siedowse doff_t blkoff, prevoff; 111079561Siedowse int entrypos, i; 111179561Siedowse 111279561Siedowse blkoff = offset & ~(DIRBLKSIZ - 1); /* offset of start of block */ 111379561Siedowse entrypos = offset & (DIRBLKSIZ - 1); /* entry relative to block */ 111479561Siedowse blkbuf = (char *)dirp - entrypos; 111579561Siedowse prevoff = blkoff; 111679561Siedowse 111779561Siedowse /* If `offset' is the start of a block, there is no previous entry. */ 111879561Siedowse if (entrypos == 0) 111979561Siedowse return (-1); 112079561Siedowse 112179561Siedowse /* Scan from the start of the block until we get to the entry. */ 112279561Siedowse for (i = 0; i < entrypos; i += dp->d_reclen) { 112379561Siedowse dp = (struct direct *)(blkbuf + i); 112479561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos) 112579561Siedowse return (-1); /* Corrupted directory. */ 112679561Siedowse prevoff = blkoff + i; 112779561Siedowse } 112879561Siedowse return (prevoff); 112979561Siedowse} 113079561Siedowse 113179561Siedowse/* 113279561Siedowse * Try to free up `wanted' bytes by stealing memory from existing 1133125854Sdwmalone * dirhashes. Returns zero with list locked if successful. 113479561Siedowse */ 113579561Siedowsestatic int 113679561Siedowseufsdirhash_recycle(int wanted) 113779561Siedowse{ 113879561Siedowse struct dirhash *dh; 113979561Siedowse doff_t **hash; 114079561Siedowse u_int8_t *blkfree; 114179561Siedowse int i, mem, narrays; 114279561Siedowse 1143125854Sdwmalone DIRHASHLIST_LOCK(); 1144178110Sjeff dh = TAILQ_FIRST(&ufsdirhash_list); 114579561Siedowse while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) { 1146178110Sjeff /* Decrement the score; only recycle if it becomes zero. */ 1147178110Sjeff if (dh == NULL || --dh->dh_score > 0) { 1148125854Sdwmalone DIRHASHLIST_UNLOCK(); 114979561Siedowse return (-1); 115079561Siedowse } 1151178110Sjeff /* 1152178110Sjeff * If we can't lock it it's in use and we don't want to 1153178110Sjeff * recycle it anyway. 1154178110Sjeff */ 1155183080Sjhb if (!sx_try_xlock(&dh->dh_lock)) { 1156178110Sjeff dh = TAILQ_NEXT(dh, dh_list); 1157178110Sjeff continue; 1158178110Sjeff } 115979561Siedowse KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list")); 116079561Siedowse 116179561Siedowse /* Remove it from the list and detach its memory. */ 116279561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 116379561Siedowse dh->dh_onlist = 0; 116479561Siedowse hash = dh->dh_hash; 116579561Siedowse dh->dh_hash = NULL; 116679561Siedowse blkfree = dh->dh_blkfree; 116779561Siedowse dh->dh_blkfree = NULL; 116879561Siedowse narrays = dh->dh_narrays; 1169178110Sjeff mem = dh->dh_memreq; 1170178110Sjeff dh->dh_memreq = 0; 117179561Siedowse 117279561Siedowse /* Unlock everything, free the detached memory. */ 1173178110Sjeff ufsdirhash_release(dh); 1174125854Sdwmalone DIRHASHLIST_UNLOCK(); 117579561Siedowse for (i = 0; i < narrays; i++) 1176125854Sdwmalone DIRHASH_BLKFREE(hash[i]); 1177184205Sdes free(hash, M_DIRHASH); 1178184205Sdes free(blkfree, M_DIRHASH); 117979561Siedowse 118079561Siedowse /* Account for the returned memory, and repeat if necessary. */ 1181125854Sdwmalone DIRHASHLIST_LOCK(); 118279561Siedowse ufs_dirhashmem -= mem; 1183178110Sjeff dh = TAILQ_FIRST(&ufsdirhash_list); 118479561Siedowse } 1185125854Sdwmalone /* Success; return with list locked. */ 118679561Siedowse return (0); 118779561Siedowse} 118879561Siedowse 118979561Siedowse 119099101Siedowsevoid 119179561Siedowseufsdirhash_init() 119279561Siedowse{ 119396874Siedowse ufsdirhash_zone = uma_zcreate("DIRHASH", DH_NBLKOFF * sizeof(doff_t), 119492768Sjeff NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 119593818Sjhb mtx_init(&ufsdirhash_mtx, "dirhash list", NULL, MTX_DEF); 119679561Siedowse TAILQ_INIT(&ufsdirhash_list); 119779561Siedowse} 119879561Siedowse 119999101Siedowsevoid 120099101Siedowseufsdirhash_uninit() 120199101Siedowse{ 120299101Siedowse KASSERT(TAILQ_EMPTY(&ufsdirhash_list), ("ufsdirhash_uninit")); 120399101Siedowse uma_zdestroy(ufsdirhash_zone); 120499101Siedowse mtx_destroy(&ufsdirhash_mtx); 120599101Siedowse} 120679561Siedowse 120779561Siedowse#endif /* UFS_DIRHASH */ 1208