ufs_dirhash.c revision 184651
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 184651 2008-11-04 18:56:12Z jhb $"); 32116192Sobrien 3379561Siedowse#include "opt_ufs.h" 3479561Siedowse 3579561Siedowse#ifdef UFS_DIRHASH 3679561Siedowse 3779561Siedowse#include <sys/param.h> 3879561Siedowse#include <sys/systm.h> 3979561Siedowse#include <sys/kernel.h> 4084811Sjhb#include <sys/lock.h> 4179561Siedowse#include <sys/mutex.h> 4279561Siedowse#include <sys/malloc.h> 4379561Siedowse#include <sys/fnv_hash.h> 4479561Siedowse#include <sys/proc.h> 4579561Siedowse#include <sys/bio.h> 4679561Siedowse#include <sys/buf.h> 4779561Siedowse#include <sys/vnode.h> 4879561Siedowse#include <sys/mount.h> 49183080Sjhb#include <sys/refcount.h> 5079561Siedowse#include <sys/sysctl.h> 51183080Sjhb#include <sys/sx.h> 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); 184184651Sjhb 185184651Sjhb /* 186184651Sjhb * The DUPOK is to prevent warnings from the 187184651Sjhb * sx_slock() a few lines down which is safe 188184651Sjhb * since the duplicate lock in that case is 189184651Sjhb * the one for this dirhash we are creating 190184651Sjhb * now which has no external references until 191184651Sjhb * after this function returns. 192184651Sjhb */ 193184651Sjhb sx_init_flags(&ndh->dh_lock, "dirhash", SX_DUPOK); 194183080Sjhb sx_xlock(&ndh->dh_lock); 195178110Sjeff } 196178110Sjeff /* 197178110Sjeff * Check i_dirhash. If it's NULL just try to use a 198178110Sjeff * preallocated structure. If none exists loop and try again. 199178110Sjeff */ 200178110Sjeff VI_LOCK(vp); 201178110Sjeff dh = ip->i_dirhash; 202178110Sjeff if (dh == NULL) { 203178110Sjeff ip->i_dirhash = ndh; 204178110Sjeff VI_UNLOCK(vp); 205178110Sjeff if (ndh == NULL) 206178110Sjeff continue; 207178110Sjeff return (ndh); 208178110Sjeff } 209183080Sjhb ufsdirhash_hold(dh); 210183080Sjhb VI_UNLOCK(vp); 211183080Sjhb 212183080Sjhb /* Acquire a shared lock on existing hashes. */ 213183080Sjhb sx_slock(&dh->dh_lock); 214183080Sjhb 215178110Sjeff /* The hash could've been recycled while we were waiting. */ 216183080Sjhb VI_LOCK(vp); 217178110Sjeff if (ip->i_dirhash != dh) { 218183080Sjhb VI_UNLOCK(vp); 219178110Sjeff ufsdirhash_release(dh); 220183080Sjhb ufsdirhash_drop(dh); 221178110Sjeff continue; 222178110Sjeff } 223183080Sjhb VI_UNLOCK(vp); 224183080Sjhb ufsdirhash_drop(dh); 225183080Sjhb 226178110Sjeff /* If the hash is still valid we've succeeded. */ 227178110Sjeff if (dh->dh_hash != NULL) 228178110Sjeff break; 229178110Sjeff /* 230178110Sjeff * If the hash is NULL it has been recycled. Try to upgrade 231183080Sjhb * so we can recreate it. If we fail the upgrade, drop our 232183080Sjhb * lock and try again. 233178110Sjeff */ 234183080Sjhb if (sx_try_upgrade(&dh->dh_lock)) 235178110Sjeff break; 236183080Sjhb sx_sunlock(&dh->dh_lock); 237178110Sjeff } 238178110Sjeff /* Free the preallocated structure if it was not necessary. */ 239178110Sjeff if (ndh) { 240183080Sjhb ufsdirhash_release(ndh); 241183080Sjhb ufsdirhash_drop(ndh); 242178110Sjeff } 243178110Sjeff return (dh); 244178110Sjeff} 245178110Sjeff 246178110Sjeff/* 247178110Sjeff * Acquire an exclusive lock on an existing hash. Requires an exclusive 248178110Sjeff * vnode lock to protect the i_dirhash pointer. hashes that have been 249178110Sjeff * recycled are reclaimed here and NULL is returned. 250178110Sjeff */ 251178110Sjeffstatic struct dirhash * 252178110Sjeffufsdirhash_acquire(struct inode *ip) 253178110Sjeff{ 254178110Sjeff struct dirhash *dh; 255178110Sjeff struct vnode *vp; 256178110Sjeff 257178110Sjeff ASSERT_VOP_ELOCKED(ip->i_vnode, __FUNCTION__); 258178110Sjeff 259178110Sjeff vp = ip->i_vnode; 260178110Sjeff dh = ip->i_dirhash; 261178110Sjeff if (dh == NULL) 262178110Sjeff return (NULL); 263183080Sjhb sx_xlock(&dh->dh_lock); 264178110Sjeff if (dh->dh_hash != NULL) 265178110Sjeff return (dh); 266178110Sjeff ufsdirhash_free_locked(ip); 267178110Sjeff return (NULL); 268178110Sjeff} 269178110Sjeff 270178110Sjeff/* 271178110Sjeff * Acquire exclusively and free the hash pointed to by ip. Works with a 272178110Sjeff * shared or exclusive vnode lock. 273178110Sjeff */ 274178110Sjeffvoid 275178110Sjeffufsdirhash_free(struct inode *ip) 276178110Sjeff{ 277178110Sjeff struct dirhash *dh; 278178110Sjeff struct vnode *vp; 279178110Sjeff 280178110Sjeff vp = ip->i_vnode; 281178110Sjeff for (;;) { 282183080Sjhb /* Grab a reference on this inode's dirhash if it has one. */ 283178110Sjeff VI_LOCK(vp); 284178110Sjeff dh = ip->i_dirhash; 285178110Sjeff if (dh == NULL) { 286178110Sjeff VI_UNLOCK(vp); 287178110Sjeff return; 288178110Sjeff } 289183080Sjhb ufsdirhash_hold(dh); 290183080Sjhb VI_UNLOCK(vp); 291183080Sjhb 292183080Sjhb /* Exclusively lock the dirhash. */ 293183080Sjhb sx_xlock(&dh->dh_lock); 294183080Sjhb 295183080Sjhb /* If this dirhash still belongs to this inode, then free it. */ 296183080Sjhb VI_LOCK(vp); 297183080Sjhb if (ip->i_dirhash == dh) { 298183080Sjhb VI_UNLOCK(vp); 299183080Sjhb ufsdirhash_drop(dh); 300178110Sjeff break; 301183080Sjhb } 302183080Sjhb VI_UNLOCK(vp); 303183080Sjhb 304183080Sjhb /* 305183080Sjhb * This inode's dirhash has changed while we were 306183080Sjhb * waiting for the dirhash lock, so try again. 307183080Sjhb */ 308178110Sjeff ufsdirhash_release(dh); 309183080Sjhb ufsdirhash_drop(dh); 310178110Sjeff } 311178110Sjeff ufsdirhash_free_locked(ip); 312178110Sjeff} 313178110Sjeff 314178110Sjeff/* 31579561Siedowse * Attempt to build up a hash table for the directory contents in 31679561Siedowse * inode 'ip'. Returns 0 on success, or -1 of the operation failed. 31779561Siedowse */ 31879561Siedowseint 31979561Siedowseufsdirhash_build(struct inode *ip) 32079561Siedowse{ 32179561Siedowse struct dirhash *dh; 32279561Siedowse struct buf *bp = NULL; 32379561Siedowse struct direct *ep; 32479561Siedowse struct vnode *vp; 32579561Siedowse doff_t bmask, pos; 32679561Siedowse int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot; 32779561Siedowse 328178110Sjeff /* Take care of a decreased sysctl value. */ 329178110Sjeff while (ufs_dirhashmem > ufs_dirhashmaxmem) 330178110Sjeff if (ufsdirhash_recycle(0) != 0) 331178110Sjeff return (-1); 332178110Sjeff 33379561Siedowse /* Check if we can/should use dirhash. */ 334178110Sjeff if (ip->i_size < ufs_mindirhashsize || OFSFMT(ip->i_vnode) || 335178110Sjeff ip->i_effnlink == 0) { 336178110Sjeff if (ip->i_dirhash) 33779561Siedowse ufsdirhash_free(ip); 338178110Sjeff return (-1); 33979561Siedowse } 340178110Sjeff dh = ufsdirhash_create(ip); 341178110Sjeff if (dh == NULL) 34282364Siedowse return (-1); 343178110Sjeff if (dh->dh_hash != NULL) 344178110Sjeff return (0); 34582364Siedowse 34679561Siedowse vp = ip->i_vnode; 34779561Siedowse /* Allocate 50% more entries than this dir size could ever need. */ 34879561Siedowse KASSERT(ip->i_size >= DIRBLKSIZ, ("ufsdirhash_build size")); 34979561Siedowse nslots = ip->i_size / DIRECTSIZ(1); 35079561Siedowse nslots = (nslots * 3 + 1) / 2; 35179561Siedowse narrays = howmany(nslots, DH_NBLKOFF); 35279561Siedowse nslots = narrays * DH_NBLKOFF; 35379561Siedowse dirblocks = howmany(ip->i_size, DIRBLKSIZ); 35479561Siedowse nblocks = (dirblocks * 3 + 1) / 2; 35579561Siedowse memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) + 35679561Siedowse narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 35779561Siedowse nblocks * sizeof(*dh->dh_blkfree); 358125854Sdwmalone DIRHASHLIST_LOCK(); 35979561Siedowse if (memreqd + ufs_dirhashmem > ufs_dirhashmaxmem) { 360125854Sdwmalone DIRHASHLIST_UNLOCK(); 36179561Siedowse if (memreqd > ufs_dirhashmaxmem / 2) 362178110Sjeff goto fail; 36379561Siedowse /* Try to free some space. */ 36479561Siedowse if (ufsdirhash_recycle(memreqd) != 0) 365178110Sjeff goto fail; 366125854Sdwmalone /* Enough was freed, and list has been locked. */ 36779561Siedowse } 36879561Siedowse ufs_dirhashmem += memreqd; 369125854Sdwmalone DIRHASHLIST_UNLOCK(); 37079561Siedowse 371178110Sjeff /* Initialise the hash table and block statistics. */ 372178110Sjeff dh->dh_memreq = memreqd; 373178110Sjeff dh->dh_narrays = narrays; 374178110Sjeff dh->dh_hlen = nslots; 375178110Sjeff dh->dh_nblk = nblocks; 376178110Sjeff dh->dh_dirblks = dirblocks; 377178110Sjeff for (i = 0; i < DH_NFSTATS; i++) 378178110Sjeff dh->dh_firstfree[i] = -1; 379178110Sjeff dh->dh_firstfree[DH_NFSTATS] = 0; 380178110Sjeff dh->dh_hused = 0; 381178110Sjeff dh->dh_seqopt = 0; 382178110Sjeff dh->dh_seqoff = 0; 383178110Sjeff dh->dh_score = DH_SCOREINIT; 384178110Sjeff 38579561Siedowse /* 38679561Siedowse * Use non-blocking mallocs so that we will revert to a linear 38779561Siedowse * lookup on failure rather than potentially blocking forever. 38879561Siedowse */ 389184205Sdes dh->dh_hash = malloc(narrays * sizeof(dh->dh_hash[0]), 39079561Siedowse M_DIRHASH, M_NOWAIT | M_ZERO); 391178110Sjeff if (dh->dh_hash == NULL) 392178110Sjeff goto fail; 393184205Sdes dh->dh_blkfree = malloc(nblocks * sizeof(dh->dh_blkfree[0]), 39479561Siedowse M_DIRHASH, M_NOWAIT); 395178110Sjeff if (dh->dh_blkfree == NULL) 39679561Siedowse goto fail; 39779561Siedowse for (i = 0; i < narrays; i++) { 398125854Sdwmalone if ((dh->dh_hash[i] = DIRHASH_BLKALLOC_WAITOK()) == NULL) 39979561Siedowse goto fail; 40079561Siedowse for (j = 0; j < DH_NBLKOFF; j++) 40179561Siedowse dh->dh_hash[i][j] = DIRHASH_EMPTY; 40279561Siedowse } 40379561Siedowse for (i = 0; i < dirblocks; i++) 40479561Siedowse dh->dh_blkfree[i] = DIRBLKSIZ / DIRALIGN; 40579561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 40679561Siedowse pos = 0; 40779561Siedowse while (pos < ip->i_size) { 40879561Siedowse /* If necessary, get the next directory block. */ 40979561Siedowse if ((pos & bmask) == 0) { 41079561Siedowse if (bp != NULL) 41179561Siedowse brelse(bp); 41279561Siedowse if (UFS_BLKATOFF(vp, (off_t)pos, NULL, &bp) != 0) 41379561Siedowse goto fail; 41479561Siedowse } 41579561Siedowse 41679561Siedowse /* Add this entry to the hash. */ 41779561Siedowse ep = (struct direct *)((char *)bp->b_data + (pos & bmask)); 41879561Siedowse if (ep->d_reclen == 0 || ep->d_reclen > 41979561Siedowse DIRBLKSIZ - (pos & (DIRBLKSIZ - 1))) { 42079561Siedowse /* Corrupted directory. */ 42179561Siedowse brelse(bp); 42279561Siedowse goto fail; 42379561Siedowse } 42479561Siedowse if (ep->d_ino != 0) { 42579561Siedowse /* Add the entry (simplified ufsdirhash_add). */ 42679561Siedowse slot = ufsdirhash_hash(dh, ep->d_name, ep->d_namlen); 42779561Siedowse while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 42879561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 42979561Siedowse dh->dh_hused++; 43079561Siedowse DH_ENTRY(dh, slot) = pos; 43179561Siedowse ufsdirhash_adjfree(dh, pos, -DIRSIZ(0, ep)); 43279561Siedowse } 43379561Siedowse pos += ep->d_reclen; 43479561Siedowse } 43579561Siedowse 43679561Siedowse if (bp != NULL) 43779561Siedowse brelse(bp); 438125854Sdwmalone DIRHASHLIST_LOCK(); 43979561Siedowse TAILQ_INSERT_TAIL(&ufsdirhash_list, dh, dh_list); 44079561Siedowse dh->dh_onlist = 1; 441125854Sdwmalone DIRHASHLIST_UNLOCK(); 442183080Sjhb sx_downgrade(&dh->dh_lock); 44379561Siedowse return (0); 44479561Siedowse 44579561Siedowsefail: 446178110Sjeff ufsdirhash_free_locked(ip); 44779561Siedowse return (-1); 44879561Siedowse} 44979561Siedowse 45079561Siedowse/* 45179561Siedowse * Free any hash table associated with inode 'ip'. 45279561Siedowse */ 453178110Sjeffstatic void 454178110Sjeffufsdirhash_free_locked(struct inode *ip) 45579561Siedowse{ 45679561Siedowse struct dirhash *dh; 457178110Sjeff struct vnode *vp; 458178110Sjeff int i; 45979561Siedowse 460178110Sjeff DIRHASH_ASSERT_LOCKED(ip->i_dirhash); 461183080Sjhb 462178110Sjeff /* 463178110Sjeff * Clear the pointer in the inode to prevent new threads from 464178110Sjeff * finding the dead structure. 465178110Sjeff */ 466178110Sjeff vp = ip->i_vnode; 467178110Sjeff VI_LOCK(vp); 468178110Sjeff dh = ip->i_dirhash; 469178110Sjeff ip->i_dirhash = NULL; 470178110Sjeff VI_UNLOCK(vp); 471183080Sjhb 472178110Sjeff /* 473183280Sjhb * Remove the hash from the list since we are going to free its 474183280Sjhb * memory. 475183280Sjhb */ 476183280Sjhb DIRHASHLIST_LOCK(); 477183280Sjhb if (dh->dh_onlist) 478183280Sjhb TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 479183280Sjhb ufs_dirhashmem -= dh->dh_memreq; 480183280Sjhb DIRHASHLIST_UNLOCK(); 481183280Sjhb 482183280Sjhb /* 483183080Sjhb * At this point, any waiters for the lock should hold their 484183080Sjhb * own reference on the dirhash structure. They will drop 485183080Sjhb * that reference once they grab the vnode interlock and see 486183080Sjhb * that ip->i_dirhash is NULL. 487178110Sjeff */ 488183080Sjhb sx_xunlock(&dh->dh_lock); 489183080Sjhb 490178110Sjeff /* 491178110Sjeff * Handle partially recycled as well as fully constructed hashes. 492178110Sjeff */ 493178110Sjeff if (dh->dh_hash != NULL) { 494178110Sjeff for (i = 0; i < dh->dh_narrays; i++) 495178110Sjeff if (dh->dh_hash[i] != NULL) 496178110Sjeff DIRHASH_BLKFREE(dh->dh_hash[i]); 497184205Sdes free(dh->dh_hash, M_DIRHASH); 498178110Sjeff if (dh->dh_blkfree != NULL) 499184205Sdes free(dh->dh_blkfree, M_DIRHASH); 500178110Sjeff } 501183080Sjhb 502178110Sjeff /* 503183080Sjhb * Drop the inode's reference to the data structure. 504178110Sjeff */ 505183080Sjhb ufsdirhash_drop(dh); 50679561Siedowse} 50779561Siedowse 50879561Siedowse/* 50979561Siedowse * Find the offset of the specified name within the given inode. 51079561Siedowse * Returns 0 on success, ENOENT if the entry does not exist, or 51179561Siedowse * EJUSTRETURN if the caller should revert to a linear search. 51279561Siedowse * 51379690Siedowse * If successful, the directory offset is stored in *offp, and a 51479690Siedowse * pointer to a struct buf containing the entry is stored in *bpp. If 51579561Siedowse * prevoffp is non-NULL, the offset of the previous entry within 51679561Siedowse * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry 51779561Siedowse * is the first in a block, the start of the block is used). 518178110Sjeff * 519178110Sjeff * Must be called with the hash locked. Returns with the hash unlocked. 52079561Siedowse */ 52179561Siedowseint 52279561Siedowseufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp, 52379690Siedowse struct buf **bpp, doff_t *prevoffp) 52479561Siedowse{ 52579561Siedowse struct dirhash *dh, *dh_next; 52679561Siedowse struct direct *dp; 52779561Siedowse struct vnode *vp; 52879561Siedowse struct buf *bp; 52979561Siedowse doff_t blkoff, bmask, offset, prevoff; 53079561Siedowse int i, slot; 531178110Sjeff int error; 53279561Siedowse 533178110Sjeff dh = ip->i_dirhash; 534178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 535178110Sjeff ("ufsdirhash_lookup: Invalid dirhash %p\n", dh)); 536178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 53779561Siedowse /* 53879561Siedowse * Move this dirhash towards the end of the list if it has a 539178110Sjeff * score higher than the next entry, and acquire the dh_lock. 54079561Siedowse */ 541178110Sjeff DIRHASHLIST_LOCK(); 54279561Siedowse if (TAILQ_NEXT(dh, dh_list) != NULL) { 54379561Siedowse /* 54479561Siedowse * If the new score will be greater than that of the next 54579561Siedowse * entry, then move this entry past it. With both mutexes 54679561Siedowse * held, dh_next won't go away, but its dh_score could 54779561Siedowse * change; that's not important since it is just a hint. 54879561Siedowse */ 549178110Sjeff if ((dh_next = TAILQ_NEXT(dh, dh_list)) != NULL && 55079561Siedowse dh->dh_score >= dh_next->dh_score) { 55179561Siedowse KASSERT(dh->dh_onlist, ("dirhash: not on list")); 55279561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 55379561Siedowse TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh, 55479561Siedowse dh_list); 55579561Siedowse } 55679561Siedowse } 55779561Siedowse /* Update the score. */ 55879561Siedowse if (dh->dh_score < DH_SCOREMAX) 55979561Siedowse dh->dh_score++; 560178110Sjeff DIRHASHLIST_UNLOCK(); 56179561Siedowse 56279561Siedowse vp = ip->i_vnode; 56379561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 56479561Siedowse blkoff = -1; 56579561Siedowse bp = NULL; 56679561Siedowserestart: 56779561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 56879561Siedowse 56979561Siedowse if (dh->dh_seqopt) { 57079561Siedowse /* 57179561Siedowse * Sequential access optimisation. dh_seqoff contains the 57279561Siedowse * offset of the directory entry immediately following 57379561Siedowse * the last entry that was looked up. Check if this offset 57479561Siedowse * appears in the hash chain for the name we are looking for. 57579561Siedowse */ 57679561Siedowse for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY; 57779561Siedowse i = WRAPINCR(i, dh->dh_hlen)) 57886350Siedowse if (offset == dh->dh_seqoff) 57979561Siedowse break; 58079561Siedowse if (offset == dh->dh_seqoff) { 58179561Siedowse /* 58279561Siedowse * We found an entry with the expected offset. This 58379561Siedowse * is probably the entry we want, but if not, the 584149178Siedowse * code below will turn off seqopt and retry. 58579561Siedowse */ 58679561Siedowse slot = i; 58779561Siedowse } else 58879561Siedowse dh->dh_seqopt = 0; 58979561Siedowse } 59079561Siedowse 59179561Siedowse for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY; 59279561Siedowse slot = WRAPINCR(slot, dh->dh_hlen)) { 59379561Siedowse if (offset == DIRHASH_DEL) 59479561Siedowse continue; 59579561Siedowse if (offset < 0 || offset >= ip->i_size) 59679561Siedowse panic("ufsdirhash_lookup: bad offset in hash array"); 59779561Siedowse if ((offset & ~bmask) != blkoff) { 59879561Siedowse if (bp != NULL) 59979561Siedowse brelse(bp); 60079561Siedowse blkoff = offset & ~bmask; 601178110Sjeff if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) { 602178110Sjeff error = EJUSTRETURN; 603178110Sjeff goto fail; 604178110Sjeff } 60579561Siedowse } 60679561Siedowse dp = (struct direct *)(bp->b_data + (offset & bmask)); 60779561Siedowse if (dp->d_reclen == 0 || dp->d_reclen > 60879561Siedowse DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) { 60979561Siedowse /* Corrupted directory. */ 610178110Sjeff error = EJUSTRETURN; 611178110Sjeff goto fail; 61279561Siedowse } 61379561Siedowse if (dp->d_namlen == namelen && 61479561Siedowse bcmp(dp->d_name, name, namelen) == 0) { 61579561Siedowse /* Found. Get the prev offset if needed. */ 61679561Siedowse if (prevoffp != NULL) { 61779561Siedowse if (offset & (DIRBLKSIZ - 1)) { 61879561Siedowse prevoff = ufsdirhash_getprev(dp, 61979561Siedowse offset); 62079561Siedowse if (prevoff == -1) { 621178110Sjeff error = EJUSTRETURN; 622178110Sjeff goto fail; 62379561Siedowse } 62479561Siedowse } else 62579561Siedowse prevoff = offset; 62679561Siedowse *prevoffp = prevoff; 62779561Siedowse } 62879561Siedowse 62979561Siedowse /* Check for sequential access, and update offset. */ 63079561Siedowse if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset) 63179561Siedowse dh->dh_seqopt = 1; 63279561Siedowse dh->dh_seqoff = offset + DIRSIZ(0, dp); 63379690Siedowse *bpp = bp; 63479561Siedowse *offp = offset; 635178110Sjeff ufsdirhash_release(dh); 63679561Siedowse return (0); 63779561Siedowse } 63879561Siedowse 63979561Siedowse /* 64079561Siedowse * When the name doesn't match in the seqopt case, go back 64179561Siedowse * and search normally. 64279561Siedowse */ 64379561Siedowse if (dh->dh_seqopt) { 64479561Siedowse dh->dh_seqopt = 0; 64579561Siedowse goto restart; 64679561Siedowse } 64779561Siedowse } 648178110Sjeff error = ENOENT; 649178110Sjefffail: 650178110Sjeff ufsdirhash_release(dh); 65179561Siedowse if (bp != NULL) 65279561Siedowse brelse(bp); 653178110Sjeff return (error); 65479561Siedowse} 65579561Siedowse 65679561Siedowse/* 65779561Siedowse * Find a directory block with room for 'slotneeded' bytes. Returns 65879561Siedowse * the offset of the directory entry that begins the free space. 65979561Siedowse * This will either be the offset of an existing entry that has free 66079561Siedowse * space at the end, or the offset of an entry with d_ino == 0 at 66179561Siedowse * the start of a DIRBLKSIZ block. 66279561Siedowse * 66379561Siedowse * To use the space, the caller may need to compact existing entries in 66479561Siedowse * the directory. The total number of bytes in all of the entries involved 66579561Siedowse * in the compaction is stored in *slotsize. In other words, all of 66679561Siedowse * the entries that must be compacted are exactly contained in the 66779561Siedowse * region beginning at the returned offset and spanning *slotsize bytes. 66879561Siedowse * 66979561Siedowse * Returns -1 if no space was found, indicating that the directory 67079561Siedowse * must be extended. 67179561Siedowse */ 67279561Siedowsedoff_t 67379561Siedowseufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize) 67479561Siedowse{ 67579561Siedowse struct direct *dp; 67679561Siedowse struct dirhash *dh; 67779561Siedowse struct buf *bp; 67879561Siedowse doff_t pos, slotstart; 67979561Siedowse int dirblock, error, freebytes, i; 68079561Siedowse 681178110Sjeff dh = ip->i_dirhash; 682178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 683178110Sjeff ("ufsdirhash_findfree: Invalid dirhash %p\n", dh)); 684178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 68579561Siedowse 68679561Siedowse /* Find a directory block with the desired free space. */ 68779561Siedowse dirblock = -1; 68879561Siedowse for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++) 68979561Siedowse if ((dirblock = dh->dh_firstfree[i]) != -1) 69079561Siedowse break; 691178110Sjeff if (dirblock == -1) 69279561Siedowse return (-1); 69379561Siedowse 69479561Siedowse KASSERT(dirblock < dh->dh_nblk && 69579561Siedowse dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN), 69679561Siedowse ("ufsdirhash_findfree: bad stats")); 69779561Siedowse pos = dirblock * DIRBLKSIZ; 69879561Siedowse error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp); 69979561Siedowse if (error) 70079561Siedowse return (-1); 70179561Siedowse 70279561Siedowse /* Find the first entry with free space. */ 70379561Siedowse for (i = 0; i < DIRBLKSIZ; ) { 70479561Siedowse if (dp->d_reclen == 0) { 70579561Siedowse brelse(bp); 70679561Siedowse return (-1); 70779561Siedowse } 70879561Siedowse if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp)) 70979561Siedowse break; 71079561Siedowse i += dp->d_reclen; 71179561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 71279561Siedowse } 71379561Siedowse if (i > DIRBLKSIZ) { 71479561Siedowse brelse(bp); 71579561Siedowse return (-1); 71679561Siedowse } 71779561Siedowse slotstart = pos + i; 71879561Siedowse 71979561Siedowse /* Find the range of entries needed to get enough space */ 72079561Siedowse freebytes = 0; 72179561Siedowse while (i < DIRBLKSIZ && freebytes < slotneeded) { 72279561Siedowse freebytes += dp->d_reclen; 72379561Siedowse if (dp->d_ino != 0) 72479561Siedowse freebytes -= DIRSIZ(0, dp); 72579561Siedowse if (dp->d_reclen == 0) { 72679561Siedowse brelse(bp); 72779561Siedowse return (-1); 72879561Siedowse } 72979561Siedowse i += dp->d_reclen; 73079561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 73179561Siedowse } 73279561Siedowse if (i > DIRBLKSIZ) { 73379561Siedowse brelse(bp); 73479561Siedowse return (-1); 73579561Siedowse } 73679561Siedowse if (freebytes < slotneeded) 73779561Siedowse panic("ufsdirhash_findfree: free mismatch"); 73879561Siedowse brelse(bp); 73979561Siedowse *slotsize = pos + i - slotstart; 74079561Siedowse return (slotstart); 74179561Siedowse} 74279561Siedowse 74379561Siedowse/* 74479561Siedowse * Return the start of the unused space at the end of a directory, or 74579561Siedowse * -1 if there are no trailing unused blocks. 74679561Siedowse */ 74779561Siedowsedoff_t 74879561Siedowseufsdirhash_enduseful(struct inode *ip) 74979561Siedowse{ 75079561Siedowse 75179561Siedowse struct dirhash *dh; 75279561Siedowse int i; 75379561Siedowse 754178110Sjeff dh = ip->i_dirhash; 755178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 756178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 757178110Sjeff ("ufsdirhash_enduseful: Invalid dirhash %p\n", dh)); 75879561Siedowse 759178110Sjeff if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN) 76079561Siedowse return (-1); 76179561Siedowse 76279561Siedowse for (i = dh->dh_dirblks - 1; i >= 0; i--) 76379561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 76479561Siedowse break; 765178110Sjeff 76679561Siedowse return ((doff_t)(i + 1) * DIRBLKSIZ); 76779561Siedowse} 76879561Siedowse 76979561Siedowse/* 77079561Siedowse * Insert information into the hash about a new directory entry. dirp 77179561Siedowse * points to a struct direct containing the entry, and offset specifies 77279561Siedowse * the offset of this entry. 77379561Siedowse */ 77479561Siedowsevoid 77579561Siedowseufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset) 77679561Siedowse{ 77779561Siedowse struct dirhash *dh; 77879561Siedowse int slot; 77979561Siedowse 780178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 78179561Siedowse return; 782178110Sjeff 78379561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 78479561Siedowse ("ufsdirhash_add: bad offset")); 78579561Siedowse /* 78679561Siedowse * Normal hash usage is < 66%. If the usage gets too high then 78779561Siedowse * remove the hash entirely and let it be rebuilt later. 78879561Siedowse */ 78979561Siedowse if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) { 790178110Sjeff ufsdirhash_free_locked(ip); 79179561Siedowse return; 79279561Siedowse } 79379561Siedowse 79479561Siedowse /* Find a free hash slot (empty or deleted), and add the entry. */ 79579561Siedowse slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen); 79679561Siedowse while (DH_ENTRY(dh, slot) >= 0) 79779561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 79879561Siedowse if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY) 79979561Siedowse dh->dh_hused++; 80079561Siedowse DH_ENTRY(dh, slot) = offset; 80179561Siedowse 80279561Siedowse /* Update the per-block summary info. */ 80379561Siedowse ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp)); 804178110Sjeff ufsdirhash_release(dh); 80579561Siedowse} 80679561Siedowse 80779561Siedowse/* 80879561Siedowse * Remove the specified directory entry from the hash. The entry to remove 80979561Siedowse * is defined by the name in `dirp', which must exist at the specified 81079561Siedowse * `offset' within the directory. 81179561Siedowse */ 81279561Siedowsevoid 81379561Siedowseufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset) 81479561Siedowse{ 81579561Siedowse struct dirhash *dh; 81679561Siedowse int slot; 81779561Siedowse 818178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 81979561Siedowse return; 82079561Siedowse 82179561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 82279561Siedowse ("ufsdirhash_remove: bad offset")); 82379561Siedowse /* Find the entry */ 82479561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset); 82579561Siedowse 82679561Siedowse /* Remove the hash entry. */ 82779561Siedowse ufsdirhash_delslot(dh, slot); 82879561Siedowse 82979561Siedowse /* Update the per-block summary info. */ 83079561Siedowse ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp)); 831178110Sjeff ufsdirhash_release(dh); 83279561Siedowse} 83379561Siedowse 83479561Siedowse/* 83579561Siedowse * Change the offset associated with a directory entry in the hash. Used 83679561Siedowse * when compacting directory blocks. 83779561Siedowse */ 83879561Siedowsevoid 83979561Siedowseufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff, 84079561Siedowse doff_t newoff) 84179561Siedowse{ 84279561Siedowse struct dirhash *dh; 84379561Siedowse int slot; 84479561Siedowse 845178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 84679561Siedowse return; 84779561Siedowse 84879561Siedowse KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ && 84979561Siedowse newoff < dh->dh_dirblks * DIRBLKSIZ, 85079561Siedowse ("ufsdirhash_move: bad offset")); 85179561Siedowse /* Find the entry, and update the offset. */ 85279561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff); 85379561Siedowse DH_ENTRY(dh, slot) = newoff; 854178110Sjeff ufsdirhash_release(dh); 85579561Siedowse} 85679561Siedowse 85779561Siedowse/* 85879561Siedowse * Inform dirhash that the directory has grown by one block that 85979561Siedowse * begins at offset (i.e. the new length is offset + DIRBLKSIZ). 86079561Siedowse */ 86179561Siedowsevoid 86279561Siedowseufsdirhash_newblk(struct inode *ip, doff_t offset) 86379561Siedowse{ 86479561Siedowse struct dirhash *dh; 86579561Siedowse int block; 86679561Siedowse 867178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 86879561Siedowse return; 86979561Siedowse 87079561Siedowse KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ, 87179561Siedowse ("ufsdirhash_newblk: bad offset")); 87279561Siedowse block = offset / DIRBLKSIZ; 87379561Siedowse if (block >= dh->dh_nblk) { 87479561Siedowse /* Out of space; must rebuild. */ 875178110Sjeff ufsdirhash_free_locked(ip); 87679561Siedowse return; 87779561Siedowse } 87879561Siedowse dh->dh_dirblks = block + 1; 87979561Siedowse 88079561Siedowse /* Account for the new free block. */ 88179561Siedowse dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN; 88279561Siedowse if (dh->dh_firstfree[DH_NFSTATS] == -1) 88379561Siedowse dh->dh_firstfree[DH_NFSTATS] = block; 884178110Sjeff ufsdirhash_release(dh); 88579561Siedowse} 88679561Siedowse 88779561Siedowse/* 88879561Siedowse * Inform dirhash that the directory is being truncated. 88979561Siedowse */ 89079561Siedowsevoid 89179561Siedowseufsdirhash_dirtrunc(struct inode *ip, doff_t offset) 89279561Siedowse{ 89379561Siedowse struct dirhash *dh; 89479561Siedowse int block, i; 89579561Siedowse 896178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 89779561Siedowse return; 89879561Siedowse 89979561Siedowse KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ, 90079561Siedowse ("ufsdirhash_dirtrunc: bad offset")); 90179561Siedowse block = howmany(offset, DIRBLKSIZ); 90279561Siedowse /* 90379561Siedowse * If the directory shrinks to less than 1/8 of dh_nblk blocks 90479561Siedowse * (about 20% of its original size due to the 50% extra added in 90579561Siedowse * ufsdirhash_build) then free it, and let the caller rebuild 90679561Siedowse * if necessary. 90779561Siedowse */ 90879561Siedowse if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) { 909178110Sjeff ufsdirhash_free_locked(ip); 91079561Siedowse return; 91179561Siedowse } 91279561Siedowse 91379561Siedowse /* 91479561Siedowse * Remove any `first free' information pertaining to the 91579561Siedowse * truncated blocks. All blocks we're removing should be 91679561Siedowse * completely unused. 91779561Siedowse */ 91879561Siedowse if (dh->dh_firstfree[DH_NFSTATS] >= block) 91979561Siedowse dh->dh_firstfree[DH_NFSTATS] = -1; 92079561Siedowse for (i = block; i < dh->dh_dirblks; i++) 92179561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 92279561Siedowse panic("ufsdirhash_dirtrunc: blocks in use"); 92379561Siedowse for (i = 0; i < DH_NFSTATS; i++) 92479561Siedowse if (dh->dh_firstfree[i] >= block) 92579561Siedowse panic("ufsdirhash_dirtrunc: first free corrupt"); 92679561Siedowse dh->dh_dirblks = block; 927178110Sjeff ufsdirhash_release(dh); 92879561Siedowse} 92979561Siedowse 93079561Siedowse/* 93179561Siedowse * Debugging function to check that the dirhash information about 93279561Siedowse * a directory block matches its actual contents. Panics if a mismatch 93379561Siedowse * is detected. 93479561Siedowse * 93579561Siedowse * On entry, `buf' should point to the start of an in-core 93679561Siedowse * DIRBLKSIZ-sized directory block, and `offset' should contain the 93779561Siedowse * offset from the start of the directory of that block. 93879561Siedowse */ 93979561Siedowsevoid 94079561Siedowseufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset) 94179561Siedowse{ 94279561Siedowse struct dirhash *dh; 94379561Siedowse struct direct *dp; 94479561Siedowse int block, ffslot, i, nfree; 94579561Siedowse 94679561Siedowse if (!ufs_dirhashcheck) 94779561Siedowse return; 948178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 94979561Siedowse return; 95079561Siedowse 95179561Siedowse block = offset / DIRBLKSIZ; 95279561Siedowse if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks) 95379561Siedowse panic("ufsdirhash_checkblock: bad offset"); 95479561Siedowse 95579561Siedowse nfree = 0; 95679561Siedowse for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) { 95779561Siedowse dp = (struct direct *)(buf + i); 95879561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ) 95979561Siedowse panic("ufsdirhash_checkblock: bad dir"); 96079561Siedowse 96179561Siedowse if (dp->d_ino == 0) { 96280456Siedowse#if 0 96380456Siedowse /* 96480456Siedowse * XXX entries with d_ino == 0 should only occur 96580456Siedowse * at the start of a DIRBLKSIZ block. However the 96680456Siedowse * ufs code is tolerant of such entries at other 96780456Siedowse * offsets, and fsck does not fix them. 96880456Siedowse */ 96979561Siedowse if (i != 0) 97079561Siedowse panic("ufsdirhash_checkblock: bad dir inode"); 97180456Siedowse#endif 97279561Siedowse nfree += dp->d_reclen; 97379561Siedowse continue; 97479561Siedowse } 97579561Siedowse 97679561Siedowse /* Check that the entry exists (will panic if it doesn't). */ 97779561Siedowse ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i); 97879561Siedowse 97979561Siedowse nfree += dp->d_reclen - DIRSIZ(0, dp); 98079561Siedowse } 98179561Siedowse if (i != DIRBLKSIZ) 98279561Siedowse panic("ufsdirhash_checkblock: bad dir end"); 98379561Siedowse 98479561Siedowse if (dh->dh_blkfree[block] * DIRALIGN != nfree) 98579561Siedowse panic("ufsdirhash_checkblock: bad free count"); 98679561Siedowse 98792098Siedowse ffslot = BLKFREE2IDX(nfree / DIRALIGN); 98879561Siedowse for (i = 0; i <= DH_NFSTATS; i++) 98979561Siedowse if (dh->dh_firstfree[i] == block && i != ffslot) 99079561Siedowse panic("ufsdirhash_checkblock: bad first-free"); 99192098Siedowse if (dh->dh_firstfree[ffslot] == -1) 99292098Siedowse panic("ufsdirhash_checkblock: missing first-free entry"); 993178110Sjeff ufsdirhash_release(dh); 99479561Siedowse} 99579561Siedowse 99679561Siedowse/* 99779561Siedowse * Hash the specified filename into a dirhash slot. 99879561Siedowse */ 99979561Siedowsestatic int 100079561Siedowseufsdirhash_hash(struct dirhash *dh, char *name, int namelen) 100179561Siedowse{ 100292807Sdwmalone u_int32_t hash; 100392807Sdwmalone 100492807Sdwmalone /* 1005107868Siedowse * We hash the name and then some other bit of data that is 1006107868Siedowse * invariant over the dirhash's lifetime. Otherwise names 100792807Sdwmalone * differing only in the last byte are placed close to one 100892807Sdwmalone * another in the table, which is bad for linear probing. 100992807Sdwmalone */ 101092807Sdwmalone hash = fnv_32_buf(name, namelen, FNV1_32_INIT); 1011133837Sdwmalone hash = fnv_32_buf(&dh, sizeof(dh), hash); 101292807Sdwmalone return (hash % dh->dh_hlen); 101379561Siedowse} 101479561Siedowse 101579561Siedowse/* 101679561Siedowse * Adjust the number of free bytes in the block containing `offset' 101779561Siedowse * by the value specified by `diff'. 101879561Siedowse * 101979561Siedowse * The caller must ensure we have exclusive access to `dh'; normally 1020178110Sjeff * that means that dh_lock should be held, but this is also called 102179561Siedowse * from ufsdirhash_build() where exclusive access can be assumed. 102279561Siedowse */ 102379561Siedowsestatic void 102479561Siedowseufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff) 102579561Siedowse{ 102679561Siedowse int block, i, nfidx, ofidx; 102779561Siedowse 102879561Siedowse /* Update the per-block summary info. */ 102979561Siedowse block = offset / DIRBLKSIZ; 103079561Siedowse KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks, 103179561Siedowse ("dirhash bad offset")); 103292098Siedowse ofidx = BLKFREE2IDX(dh->dh_blkfree[block]); 103379561Siedowse dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN); 103492098Siedowse nfidx = BLKFREE2IDX(dh->dh_blkfree[block]); 103579561Siedowse 103679561Siedowse /* Update the `first free' list if necessary. */ 103779561Siedowse if (ofidx != nfidx) { 103879561Siedowse /* If removing, scan forward for the next block. */ 103979561Siedowse if (dh->dh_firstfree[ofidx] == block) { 104079561Siedowse for (i = block + 1; i < dh->dh_dirblks; i++) 104192098Siedowse if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx) 104279561Siedowse break; 104379561Siedowse dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1; 104479561Siedowse } 104579561Siedowse 104679561Siedowse /* Make this the new `first free' if necessary */ 104779561Siedowse if (dh->dh_firstfree[nfidx] > block || 104879561Siedowse dh->dh_firstfree[nfidx] == -1) 104979561Siedowse dh->dh_firstfree[nfidx] = block; 105079561Siedowse } 105179561Siedowse} 105279561Siedowse 105379561Siedowse/* 105479561Siedowse * Find the specified name which should have the specified offset. 105579561Siedowse * Returns a slot number, and panics on failure. 105679561Siedowse * 105779561Siedowse * `dh' must be locked on entry and remains so on return. 105879561Siedowse */ 105979561Siedowsestatic int 106079561Siedowseufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset) 106179561Siedowse{ 106279561Siedowse int slot; 106379561Siedowse 1064178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 106579561Siedowse 106679561Siedowse /* Find the entry. */ 106779561Siedowse KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full")); 106879561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 106979561Siedowse while (DH_ENTRY(dh, slot) != offset && 107079561Siedowse DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 107179561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 107279561Siedowse if (DH_ENTRY(dh, slot) != offset) 107379561Siedowse panic("ufsdirhash_findslot: '%.*s' not found", namelen, name); 107479561Siedowse 107579561Siedowse return (slot); 107679561Siedowse} 107779561Siedowse 107879561Siedowse/* 107979561Siedowse * Remove the entry corresponding to the specified slot from the hash array. 108079561Siedowse * 108179561Siedowse * `dh' must be locked on entry and remains so on return. 108279561Siedowse */ 108379561Siedowsestatic void 108479561Siedowseufsdirhash_delslot(struct dirhash *dh, int slot) 108579561Siedowse{ 108679561Siedowse int i; 108779561Siedowse 1088178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 108979561Siedowse 109079561Siedowse /* Mark the entry as deleted. */ 109179561Siedowse DH_ENTRY(dh, slot) = DIRHASH_DEL; 109279561Siedowse 109379561Siedowse /* If this is the end of a chain of DIRHASH_DEL slots, remove them. */ 109479561Siedowse for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; ) 109579561Siedowse i = WRAPINCR(i, dh->dh_hlen); 109679561Siedowse if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) { 109792807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 109892807Sdwmalone while (DH_ENTRY(dh, i) == DIRHASH_DEL) { 109979561Siedowse DH_ENTRY(dh, i) = DIRHASH_EMPTY; 110079561Siedowse dh->dh_hused--; 110192807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 110279561Siedowse } 110379561Siedowse KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen")); 110479561Siedowse } 110579561Siedowse} 110679561Siedowse 110779561Siedowse/* 110879561Siedowse * Given a directory entry and its offset, find the offset of the 110979561Siedowse * previous entry in the same DIRBLKSIZ-sized block. Returns an 111079561Siedowse * offset, or -1 if there is no previous entry in the block or some 111179561Siedowse * other problem occurred. 111279561Siedowse */ 111379561Siedowsestatic doff_t 111479561Siedowseufsdirhash_getprev(struct direct *dirp, doff_t offset) 111579561Siedowse{ 111679561Siedowse struct direct *dp; 111779561Siedowse char *blkbuf; 111879561Siedowse doff_t blkoff, prevoff; 111979561Siedowse int entrypos, i; 112079561Siedowse 112179561Siedowse blkoff = offset & ~(DIRBLKSIZ - 1); /* offset of start of block */ 112279561Siedowse entrypos = offset & (DIRBLKSIZ - 1); /* entry relative to block */ 112379561Siedowse blkbuf = (char *)dirp - entrypos; 112479561Siedowse prevoff = blkoff; 112579561Siedowse 112679561Siedowse /* If `offset' is the start of a block, there is no previous entry. */ 112779561Siedowse if (entrypos == 0) 112879561Siedowse return (-1); 112979561Siedowse 113079561Siedowse /* Scan from the start of the block until we get to the entry. */ 113179561Siedowse for (i = 0; i < entrypos; i += dp->d_reclen) { 113279561Siedowse dp = (struct direct *)(blkbuf + i); 113379561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos) 113479561Siedowse return (-1); /* Corrupted directory. */ 113579561Siedowse prevoff = blkoff + i; 113679561Siedowse } 113779561Siedowse return (prevoff); 113879561Siedowse} 113979561Siedowse 114079561Siedowse/* 114179561Siedowse * Try to free up `wanted' bytes by stealing memory from existing 1142125854Sdwmalone * dirhashes. Returns zero with list locked if successful. 114379561Siedowse */ 114479561Siedowsestatic int 114579561Siedowseufsdirhash_recycle(int wanted) 114679561Siedowse{ 114779561Siedowse struct dirhash *dh; 114879561Siedowse doff_t **hash; 114979561Siedowse u_int8_t *blkfree; 115079561Siedowse int i, mem, narrays; 115179561Siedowse 1152125854Sdwmalone DIRHASHLIST_LOCK(); 1153178110Sjeff dh = TAILQ_FIRST(&ufsdirhash_list); 115479561Siedowse while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) { 1155178110Sjeff /* Decrement the score; only recycle if it becomes zero. */ 1156178110Sjeff if (dh == NULL || --dh->dh_score > 0) { 1157125854Sdwmalone DIRHASHLIST_UNLOCK(); 115879561Siedowse return (-1); 115979561Siedowse } 1160178110Sjeff /* 1161178110Sjeff * If we can't lock it it's in use and we don't want to 1162178110Sjeff * recycle it anyway. 1163178110Sjeff */ 1164183080Sjhb if (!sx_try_xlock(&dh->dh_lock)) { 1165178110Sjeff dh = TAILQ_NEXT(dh, dh_list); 1166178110Sjeff continue; 1167178110Sjeff } 116879561Siedowse KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list")); 116979561Siedowse 117079561Siedowse /* Remove it from the list and detach its memory. */ 117179561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 117279561Siedowse dh->dh_onlist = 0; 117379561Siedowse hash = dh->dh_hash; 117479561Siedowse dh->dh_hash = NULL; 117579561Siedowse blkfree = dh->dh_blkfree; 117679561Siedowse dh->dh_blkfree = NULL; 117779561Siedowse narrays = dh->dh_narrays; 1178178110Sjeff mem = dh->dh_memreq; 1179178110Sjeff dh->dh_memreq = 0; 118079561Siedowse 118179561Siedowse /* Unlock everything, free the detached memory. */ 1182178110Sjeff ufsdirhash_release(dh); 1183125854Sdwmalone DIRHASHLIST_UNLOCK(); 118479561Siedowse for (i = 0; i < narrays; i++) 1185125854Sdwmalone DIRHASH_BLKFREE(hash[i]); 1186184205Sdes free(hash, M_DIRHASH); 1187184205Sdes free(blkfree, M_DIRHASH); 118879561Siedowse 118979561Siedowse /* Account for the returned memory, and repeat if necessary. */ 1190125854Sdwmalone DIRHASHLIST_LOCK(); 119179561Siedowse ufs_dirhashmem -= mem; 1192178110Sjeff dh = TAILQ_FIRST(&ufsdirhash_list); 119379561Siedowse } 1194125854Sdwmalone /* Success; return with list locked. */ 119579561Siedowse return (0); 119679561Siedowse} 119779561Siedowse 119879561Siedowse 119999101Siedowsevoid 120079561Siedowseufsdirhash_init() 120179561Siedowse{ 120296874Siedowse ufsdirhash_zone = uma_zcreate("DIRHASH", DH_NBLKOFF * sizeof(doff_t), 120392768Sjeff NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 120493818Sjhb mtx_init(&ufsdirhash_mtx, "dirhash list", NULL, MTX_DEF); 120579561Siedowse TAILQ_INIT(&ufsdirhash_list); 120679561Siedowse} 120779561Siedowse 120899101Siedowsevoid 120999101Siedowseufsdirhash_uninit() 121099101Siedowse{ 121199101Siedowse KASSERT(TAILQ_EMPTY(&ufsdirhash_list), ("ufsdirhash_uninit")); 121299101Siedowse uma_zdestroy(ufsdirhash_zone); 121399101Siedowse mtx_destroy(&ufsdirhash_mtx); 121499101Siedowse} 121579561Siedowse 121679561Siedowse#endif /* UFS_DIRHASH */ 1217