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: stable/11/sys/ufs/ufs/ufs_dirhash.c 326845 2017-12-14 11:41:12Z kib $"); 32116192Sobrien 3379561Siedowse#include "opt_ufs.h" 3479561Siedowse 3579561Siedowse#ifdef UFS_DIRHASH 3679561Siedowse 3779561Siedowse#include <sys/param.h> 3879561Siedowse#include <sys/systm.h> 3979561Siedowse#include <sys/kernel.h> 4084811Sjhb#include <sys/lock.h> 4179561Siedowse#include <sys/mutex.h> 4279561Siedowse#include <sys/malloc.h> 4379561Siedowse#include <sys/fnv_hash.h> 4479561Siedowse#include <sys/proc.h> 4579561Siedowse#include <sys/bio.h> 4679561Siedowse#include <sys/buf.h> 4779561Siedowse#include <sys/vnode.h> 4879561Siedowse#include <sys/mount.h> 49183080Sjhb#include <sys/refcount.h> 5079561Siedowse#include <sys/sysctl.h> 51183080Sjhb#include <sys/sx.h> 52193375Ssnb#include <sys/eventhandler.h> 53193375Ssnb#include <sys/time.h> 5492768Sjeff#include <vm/uma.h> 5579561Siedowse 5679561Siedowse#include <ufs/ufs/quota.h> 5779561Siedowse#include <ufs/ufs/inode.h> 5879561Siedowse#include <ufs/ufs/dir.h> 5979561Siedowse#include <ufs/ufs/dirhash.h> 6079561Siedowse#include <ufs/ufs/extattr.h> 6179561Siedowse#include <ufs/ufs/ufsmount.h> 6279561Siedowse#include <ufs/ufs/ufs_extern.h> 6379561Siedowse 6479561Siedowse#define WRAPINCR(val, limit) (((val) + 1 == (limit)) ? 0 : ((val) + 1)) 6592807Sdwmalone#define WRAPDECR(val, limit) (((val) == 0) ? ((limit) - 1) : ((val) - 1)) 6679561Siedowse#define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0) 6792098Siedowse#define BLKFREE2IDX(n) ((n) > DH_NFSTATS ? DH_NFSTATS : (n)) 6879561Siedowse 69151897Srwatsonstatic MALLOC_DEFINE(M_DIRHASH, "ufs_dirhash", "UFS directory hash tables"); 7079561Siedowse 7179561Siedowsestatic int ufs_mindirhashsize = DIRBLKSIZ * 5; 7279561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_minsize, CTLFLAG_RW, 7379561Siedowse &ufs_mindirhashsize, 7479561Siedowse 0, "minimum directory size in bytes for which to use hashed lookup"); 75214359Sivorasstatic int ufs_dirhashmaxmem = 2 * 1024 * 1024; /* NOTE: initial value. It is 76214359Sivoras tuned in ufsdirhash_init() */ 7779561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_maxmem, CTLFLAG_RW, &ufs_dirhashmaxmem, 7879561Siedowse 0, "maximum allowed dirhash memory usage"); 7979561Siedowsestatic int ufs_dirhashmem; 8079561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_mem, CTLFLAG_RD, &ufs_dirhashmem, 8179561Siedowse 0, "current dirhash memory usage"); 8285512Siedowsestatic int ufs_dirhashcheck = 0; 8379561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_docheck, CTLFLAG_RW, &ufs_dirhashcheck, 8479561Siedowse 0, "enable extra sanity tests"); 85193375Ssnbstatic int ufs_dirhashlowmemcount = 0; 86193375SsnbSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_lowmemcount, CTLFLAG_RD, 87193375Ssnb &ufs_dirhashlowmemcount, 0, "number of times low memory hook called"); 88274834Sdavidestatic int ufs_dirhashreclaimpercent = 10; 89274834Sdavidestatic int ufsdirhash_set_reclaimpercent(SYSCTL_HANDLER_ARGS); 90274834SdavideSYSCTL_PROC(_vfs_ufs, OID_AUTO, dirhash_reclaimpercent, 91274834Sdavide CTLTYPE_INT | CTLFLAG_RW, 0, 0, ufsdirhash_set_reclaimpercent, "I", 92274834Sdavide "set percentage of dirhash cache to be removed in low VM events"); 9379561Siedowse 9479561Siedowse 9579561Siedowsestatic int ufsdirhash_hash(struct dirhash *dh, char *name, int namelen); 9679561Siedowsestatic void ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff); 9779561Siedowsestatic void ufsdirhash_delslot(struct dirhash *dh, int slot); 9879561Siedowsestatic int ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, 9979561Siedowse doff_t offset); 10079561Siedowsestatic doff_t ufsdirhash_getprev(struct direct *dp, doff_t offset); 10179561Siedowsestatic int ufsdirhash_recycle(int wanted); 102193375Ssnbstatic void ufsdirhash_lowmem(void); 103178110Sjeffstatic void ufsdirhash_free_locked(struct inode *ip); 10479561Siedowse 10592768Sjeffstatic uma_zone_t ufsdirhash_zone; 10679561Siedowse 107125854Sdwmalone#define DIRHASHLIST_LOCK() mtx_lock(&ufsdirhash_mtx) 108125854Sdwmalone#define DIRHASHLIST_UNLOCK() mtx_unlock(&ufsdirhash_mtx) 109125854Sdwmalone#define DIRHASH_BLKALLOC_WAITOK() uma_zalloc(ufsdirhash_zone, M_WAITOK) 110125854Sdwmalone#define DIRHASH_BLKFREE(ptr) uma_zfree(ufsdirhash_zone, (ptr)) 111178110Sjeff#define DIRHASH_ASSERT_LOCKED(dh) \ 112183080Sjhb sx_assert(&(dh)->dh_lock, SA_LOCKED) 113125854Sdwmalone 11479561Siedowse/* Dirhash list; recently-used entries are near the tail. */ 11579561Siedowsestatic TAILQ_HEAD(, dirhash) ufsdirhash_list; 11679561Siedowse 11779561Siedowse/* Protects: ufsdirhash_list, `dh_list' field, ufs_dirhashmem. */ 11879561Siedowsestatic struct mtx ufsdirhash_mtx; 11979561Siedowse 12079561Siedowse/* 121178110Sjeff * Locking: 12279561Siedowse * 123178110Sjeff * The relationship between inode and dirhash is protected either by an 124178110Sjeff * exclusive vnode lock or the vnode interlock where a shared vnode lock 125183080Sjhb * may be used. The dirhash_mtx is acquired after the dirhash lock. To 126183080Sjhb * handle teardown races, code wishing to lock the dirhash for an inode 127183080Sjhb * when using a shared vnode lock must obtain a private reference on the 128183080Sjhb * dirhash while holding the vnode interlock. They can drop it once they 129183080Sjhb * have obtained the dirhash lock and verified that the dirhash wasn't 130183080Sjhb * recycled while they waited for the dirhash lock. 131178110Sjeff * 132178110Sjeff * ufsdirhash_build() acquires a shared lock on the dirhash when it is 133178110Sjeff * successful. This lock is released after a call to ufsdirhash_lookup(). 134178110Sjeff * 135178110Sjeff * Functions requiring exclusive access use ufsdirhash_acquire() which may 136178110Sjeff * free a dirhash structure that was recycled by ufsdirhash_recycle(). 137178110Sjeff * 138178110Sjeff * The dirhash lock may be held across io operations. 139187474Sjhb * 140187474Sjhb * WITNESS reports a lock order reversal between the "bufwait" lock 141187474Sjhb * and the "dirhash" lock. However, this specific reversal will not 142187474Sjhb * cause a deadlock. To get a deadlock, one would have to lock a 143187474Sjhb * buffer followed by the dirhash while a second thread locked a 144187474Sjhb * buffer while holding the dirhash lock. The second order can happen 145187474Sjhb * under a shared or exclusive vnode lock for the associated directory 146187474Sjhb * in lookup(). The first order, however, can only happen under an 147187474Sjhb * exclusive vnode lock (e.g. unlink(), rename(), etc.). Thus, for 148187474Sjhb * a thread to be doing a "bufwait" -> "dirhash" order, it has to hold 149187474Sjhb * an exclusive vnode lock. That exclusive vnode lock will prevent 150187474Sjhb * any other threads from doing a "dirhash" -> "bufwait" order. 15179561Siedowse */ 15279561Siedowse 153183080Sjhbstatic void 154183080Sjhbufsdirhash_hold(struct dirhash *dh) 155183080Sjhb{ 156183080Sjhb 157183080Sjhb refcount_acquire(&dh->dh_refcount); 158183080Sjhb} 159183080Sjhb 160183080Sjhbstatic void 161183080Sjhbufsdirhash_drop(struct dirhash *dh) 162183080Sjhb{ 163183080Sjhb 164183080Sjhb if (refcount_release(&dh->dh_refcount)) { 165183080Sjhb sx_destroy(&dh->dh_lock); 166183080Sjhb free(dh, M_DIRHASH); 167183080Sjhb } 168183080Sjhb} 169183080Sjhb 17079561Siedowse/* 171178110Sjeff * Release the lock on a dirhash. 172178110Sjeff */ 173178110Sjeffstatic void 174178110Sjeffufsdirhash_release(struct dirhash *dh) 175178110Sjeff{ 176178110Sjeff 177183080Sjhb sx_unlock(&dh->dh_lock); 178178110Sjeff} 179178110Sjeff 180178110Sjeff/* 181178110Sjeff * Either acquire an existing hash locked shared or create a new hash and 182178110Sjeff * return it exclusively locked. May return NULL if the allocation fails. 183178110Sjeff * 184178110Sjeff * The vnode interlock is used to protect the i_dirhash pointer from 185178110Sjeff * simultaneous access while only a shared vnode lock is held. 186178110Sjeff */ 187178110Sjeffstatic struct dirhash * 188178110Sjeffufsdirhash_create(struct inode *ip) 189178110Sjeff{ 190178110Sjeff struct dirhash *ndh; 191178110Sjeff struct dirhash *dh; 192178110Sjeff struct vnode *vp; 193326845Skib bool excl; 194178110Sjeff 195178110Sjeff ndh = dh = NULL; 196178110Sjeff vp = ip->i_vnode; 197326845Skib excl = false; 198178110Sjeff for (;;) { 199185102Sjhb /* Racy check for i_dirhash to prefetch a dirhash structure. */ 200178110Sjeff if (ip->i_dirhash == NULL && ndh == NULL) { 201184205Sdes ndh = malloc(sizeof *dh, M_DIRHASH, 202178110Sjeff M_NOWAIT | M_ZERO); 203178110Sjeff if (ndh == NULL) 204178110Sjeff return (NULL); 205183080Sjhb refcount_init(&ndh->dh_refcount, 1); 206184651Sjhb 207184651Sjhb /* 208184651Sjhb * The DUPOK is to prevent warnings from the 209184651Sjhb * sx_slock() a few lines down which is safe 210184651Sjhb * since the duplicate lock in that case is 211184651Sjhb * the one for this dirhash we are creating 212184651Sjhb * now which has no external references until 213184651Sjhb * after this function returns. 214184651Sjhb */ 215184651Sjhb sx_init_flags(&ndh->dh_lock, "dirhash", SX_DUPOK); 216183080Sjhb sx_xlock(&ndh->dh_lock); 217178110Sjeff } 218178110Sjeff /* 219178110Sjeff * Check i_dirhash. If it's NULL just try to use a 220178110Sjeff * preallocated structure. If none exists loop and try again. 221178110Sjeff */ 222178110Sjeff VI_LOCK(vp); 223178110Sjeff dh = ip->i_dirhash; 224178110Sjeff if (dh == NULL) { 225178110Sjeff ip->i_dirhash = ndh; 226178110Sjeff VI_UNLOCK(vp); 227178110Sjeff if (ndh == NULL) 228178110Sjeff continue; 229178110Sjeff return (ndh); 230178110Sjeff } 231183080Sjhb ufsdirhash_hold(dh); 232183080Sjhb VI_UNLOCK(vp); 233183080Sjhb 234326845Skib /* Acquire a lock on existing hashes. */ 235326845Skib if (excl) 236326845Skib sx_xlock(&dh->dh_lock); 237326845Skib else 238326845Skib sx_slock(&dh->dh_lock); 239183080Sjhb 240178110Sjeff /* The hash could've been recycled while we were waiting. */ 241183080Sjhb VI_LOCK(vp); 242178110Sjeff if (ip->i_dirhash != dh) { 243183080Sjhb VI_UNLOCK(vp); 244178110Sjeff ufsdirhash_release(dh); 245183080Sjhb ufsdirhash_drop(dh); 246178110Sjeff continue; 247178110Sjeff } 248183080Sjhb VI_UNLOCK(vp); 249183080Sjhb ufsdirhash_drop(dh); 250183080Sjhb 251178110Sjeff /* If the hash is still valid we've succeeded. */ 252178110Sjeff if (dh->dh_hash != NULL) 253178110Sjeff break; 254178110Sjeff /* 255178110Sjeff * If the hash is NULL it has been recycled. Try to upgrade 256183080Sjhb * so we can recreate it. If we fail the upgrade, drop our 257183080Sjhb * lock and try again. 258178110Sjeff */ 259326845Skib if (excl || sx_try_upgrade(&dh->dh_lock)) 260178110Sjeff break; 261183080Sjhb sx_sunlock(&dh->dh_lock); 262326845Skib excl = true; 263178110Sjeff } 264178110Sjeff /* Free the preallocated structure if it was not necessary. */ 265178110Sjeff if (ndh) { 266183080Sjhb ufsdirhash_release(ndh); 267183080Sjhb ufsdirhash_drop(ndh); 268178110Sjeff } 269178110Sjeff return (dh); 270178110Sjeff} 271178110Sjeff 272178110Sjeff/* 273178110Sjeff * Acquire an exclusive lock on an existing hash. Requires an exclusive 274178110Sjeff * vnode lock to protect the i_dirhash pointer. hashes that have been 275178110Sjeff * recycled are reclaimed here and NULL is returned. 276178110Sjeff */ 277178110Sjeffstatic struct dirhash * 278178110Sjeffufsdirhash_acquire(struct inode *ip) 279178110Sjeff{ 280178110Sjeff struct dirhash *dh; 281178110Sjeff 282178110Sjeff ASSERT_VOP_ELOCKED(ip->i_vnode, __FUNCTION__); 283178110Sjeff 284178110Sjeff dh = ip->i_dirhash; 285178110Sjeff if (dh == NULL) 286178110Sjeff return (NULL); 287183080Sjhb sx_xlock(&dh->dh_lock); 288178110Sjeff if (dh->dh_hash != NULL) 289178110Sjeff return (dh); 290178110Sjeff ufsdirhash_free_locked(ip); 291178110Sjeff return (NULL); 292178110Sjeff} 293178110Sjeff 294178110Sjeff/* 295178110Sjeff * Acquire exclusively and free the hash pointed to by ip. Works with a 296178110Sjeff * shared or exclusive vnode lock. 297178110Sjeff */ 298178110Sjeffvoid 299178110Sjeffufsdirhash_free(struct inode *ip) 300178110Sjeff{ 301178110Sjeff struct dirhash *dh; 302178110Sjeff struct vnode *vp; 303178110Sjeff 304178110Sjeff vp = ip->i_vnode; 305178110Sjeff for (;;) { 306183080Sjhb /* Grab a reference on this inode's dirhash if it has one. */ 307178110Sjeff VI_LOCK(vp); 308178110Sjeff dh = ip->i_dirhash; 309178110Sjeff if (dh == NULL) { 310178110Sjeff VI_UNLOCK(vp); 311178110Sjeff return; 312178110Sjeff } 313183080Sjhb ufsdirhash_hold(dh); 314183080Sjhb VI_UNLOCK(vp); 315183080Sjhb 316183080Sjhb /* Exclusively lock the dirhash. */ 317183080Sjhb sx_xlock(&dh->dh_lock); 318183080Sjhb 319183080Sjhb /* If this dirhash still belongs to this inode, then free it. */ 320183080Sjhb VI_LOCK(vp); 321183080Sjhb if (ip->i_dirhash == dh) { 322183080Sjhb VI_UNLOCK(vp); 323183080Sjhb ufsdirhash_drop(dh); 324178110Sjeff break; 325183080Sjhb } 326183080Sjhb VI_UNLOCK(vp); 327183080Sjhb 328183080Sjhb /* 329183080Sjhb * This inode's dirhash has changed while we were 330183080Sjhb * waiting for the dirhash lock, so try again. 331183080Sjhb */ 332178110Sjeff ufsdirhash_release(dh); 333183080Sjhb ufsdirhash_drop(dh); 334178110Sjeff } 335178110Sjeff ufsdirhash_free_locked(ip); 336178110Sjeff} 337178110Sjeff 338178110Sjeff/* 33979561Siedowse * Attempt to build up a hash table for the directory contents in 34079561Siedowse * inode 'ip'. Returns 0 on success, or -1 of the operation failed. 34179561Siedowse */ 34279561Siedowseint 34379561Siedowseufsdirhash_build(struct inode *ip) 34479561Siedowse{ 34579561Siedowse struct dirhash *dh; 34679561Siedowse struct buf *bp = NULL; 34779561Siedowse struct direct *ep; 34879561Siedowse struct vnode *vp; 34979561Siedowse doff_t bmask, pos; 35079561Siedowse int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot; 35179561Siedowse 352178110Sjeff /* Take care of a decreased sysctl value. */ 353195003Ssnb while (ufs_dirhashmem > ufs_dirhashmaxmem) { 354178110Sjeff if (ufsdirhash_recycle(0) != 0) 355178110Sjeff return (-1); 356195003Ssnb /* Recycled enough memory, so unlock the list. */ 357195003Ssnb DIRHASHLIST_UNLOCK(); 358195003Ssnb } 359178110Sjeff 36079561Siedowse /* Check if we can/should use dirhash. */ 361178110Sjeff if (ip->i_size < ufs_mindirhashsize || OFSFMT(ip->i_vnode) || 362178110Sjeff ip->i_effnlink == 0) { 363178110Sjeff if (ip->i_dirhash) 36479561Siedowse ufsdirhash_free(ip); 365178110Sjeff return (-1); 36679561Siedowse } 367178110Sjeff dh = ufsdirhash_create(ip); 368178110Sjeff if (dh == NULL) 36982364Siedowse return (-1); 370178110Sjeff if (dh->dh_hash != NULL) 371178110Sjeff return (0); 37282364Siedowse 37379561Siedowse vp = ip->i_vnode; 37479561Siedowse /* Allocate 50% more entries than this dir size could ever need. */ 37579561Siedowse KASSERT(ip->i_size >= DIRBLKSIZ, ("ufsdirhash_build size")); 37679561Siedowse nslots = ip->i_size / DIRECTSIZ(1); 37779561Siedowse nslots = (nslots * 3 + 1) / 2; 37879561Siedowse narrays = howmany(nslots, DH_NBLKOFF); 37979561Siedowse nslots = narrays * DH_NBLKOFF; 38079561Siedowse dirblocks = howmany(ip->i_size, DIRBLKSIZ); 38179561Siedowse nblocks = (dirblocks * 3 + 1) / 2; 38279561Siedowse memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) + 38379561Siedowse narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 38479561Siedowse nblocks * sizeof(*dh->dh_blkfree); 385125854Sdwmalone DIRHASHLIST_LOCK(); 38679561Siedowse if (memreqd + ufs_dirhashmem > ufs_dirhashmaxmem) { 387125854Sdwmalone DIRHASHLIST_UNLOCK(); 38879561Siedowse if (memreqd > ufs_dirhashmaxmem / 2) 389178110Sjeff goto fail; 39079561Siedowse /* Try to free some space. */ 39179561Siedowse if (ufsdirhash_recycle(memreqd) != 0) 392178110Sjeff goto fail; 393125854Sdwmalone /* Enough was freed, and list has been locked. */ 39479561Siedowse } 39579561Siedowse ufs_dirhashmem += memreqd; 396125854Sdwmalone DIRHASHLIST_UNLOCK(); 39779561Siedowse 398178110Sjeff /* Initialise the hash table and block statistics. */ 399178110Sjeff dh->dh_memreq = memreqd; 400178110Sjeff dh->dh_narrays = narrays; 401178110Sjeff dh->dh_hlen = nslots; 402178110Sjeff dh->dh_nblk = nblocks; 403178110Sjeff dh->dh_dirblks = dirblocks; 404178110Sjeff for (i = 0; i < DH_NFSTATS; i++) 405178110Sjeff dh->dh_firstfree[i] = -1; 406178110Sjeff dh->dh_firstfree[DH_NFSTATS] = 0; 407178110Sjeff dh->dh_hused = 0; 408219384Sjhb dh->dh_seqoff = -1; 409178110Sjeff dh->dh_score = DH_SCOREINIT; 410193375Ssnb dh->dh_lastused = time_second; 411178110Sjeff 41279561Siedowse /* 41379561Siedowse * Use non-blocking mallocs so that we will revert to a linear 41479561Siedowse * lookup on failure rather than potentially blocking forever. 41579561Siedowse */ 416184205Sdes dh->dh_hash = malloc(narrays * sizeof(dh->dh_hash[0]), 41779561Siedowse M_DIRHASH, M_NOWAIT | M_ZERO); 418178110Sjeff if (dh->dh_hash == NULL) 419178110Sjeff goto fail; 420184205Sdes dh->dh_blkfree = malloc(nblocks * sizeof(dh->dh_blkfree[0]), 42179561Siedowse M_DIRHASH, M_NOWAIT); 422178110Sjeff if (dh->dh_blkfree == NULL) 42379561Siedowse goto fail; 42479561Siedowse for (i = 0; i < narrays; i++) { 425125854Sdwmalone if ((dh->dh_hash[i] = DIRHASH_BLKALLOC_WAITOK()) == NULL) 42679561Siedowse goto fail; 42779561Siedowse for (j = 0; j < DH_NBLKOFF; j++) 42879561Siedowse dh->dh_hash[i][j] = DIRHASH_EMPTY; 42979561Siedowse } 43079561Siedowse for (i = 0; i < dirblocks; i++) 43179561Siedowse dh->dh_blkfree[i] = DIRBLKSIZ / DIRALIGN; 432219388Skib bmask = vp->v_mount->mnt_stat.f_iosize - 1; 43379561Siedowse pos = 0; 43479561Siedowse while (pos < ip->i_size) { 43579561Siedowse /* If necessary, get the next directory block. */ 43679561Siedowse if ((pos & bmask) == 0) { 43779561Siedowse if (bp != NULL) 43879561Siedowse brelse(bp); 43979561Siedowse if (UFS_BLKATOFF(vp, (off_t)pos, NULL, &bp) != 0) 44079561Siedowse goto fail; 44179561Siedowse } 44279561Siedowse 44379561Siedowse /* Add this entry to the hash. */ 44479561Siedowse ep = (struct direct *)((char *)bp->b_data + (pos & bmask)); 44579561Siedowse if (ep->d_reclen == 0 || ep->d_reclen > 44679561Siedowse DIRBLKSIZ - (pos & (DIRBLKSIZ - 1))) { 44779561Siedowse /* Corrupted directory. */ 44879561Siedowse brelse(bp); 44979561Siedowse goto fail; 45079561Siedowse } 45179561Siedowse if (ep->d_ino != 0) { 45279561Siedowse /* Add the entry (simplified ufsdirhash_add). */ 45379561Siedowse slot = ufsdirhash_hash(dh, ep->d_name, ep->d_namlen); 45479561Siedowse while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 45579561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 45679561Siedowse dh->dh_hused++; 45779561Siedowse DH_ENTRY(dh, slot) = pos; 45879561Siedowse ufsdirhash_adjfree(dh, pos, -DIRSIZ(0, ep)); 45979561Siedowse } 46079561Siedowse pos += ep->d_reclen; 46179561Siedowse } 46279561Siedowse 46379561Siedowse if (bp != NULL) 46479561Siedowse brelse(bp); 465125854Sdwmalone DIRHASHLIST_LOCK(); 46679561Siedowse TAILQ_INSERT_TAIL(&ufsdirhash_list, dh, dh_list); 46779561Siedowse dh->dh_onlist = 1; 468125854Sdwmalone DIRHASHLIST_UNLOCK(); 469183080Sjhb sx_downgrade(&dh->dh_lock); 47079561Siedowse return (0); 47179561Siedowse 47279561Siedowsefail: 473178110Sjeff ufsdirhash_free_locked(ip); 47479561Siedowse return (-1); 47579561Siedowse} 47679561Siedowse 47779561Siedowse/* 47879561Siedowse * Free any hash table associated with inode 'ip'. 47979561Siedowse */ 480178110Sjeffstatic void 481178110Sjeffufsdirhash_free_locked(struct inode *ip) 48279561Siedowse{ 48379561Siedowse struct dirhash *dh; 484178110Sjeff struct vnode *vp; 485178110Sjeff int i; 48679561Siedowse 487178110Sjeff DIRHASH_ASSERT_LOCKED(ip->i_dirhash); 488183080Sjhb 489178110Sjeff /* 490178110Sjeff * Clear the pointer in the inode to prevent new threads from 491178110Sjeff * finding the dead structure. 492178110Sjeff */ 493178110Sjeff vp = ip->i_vnode; 494178110Sjeff VI_LOCK(vp); 495178110Sjeff dh = ip->i_dirhash; 496178110Sjeff ip->i_dirhash = NULL; 497178110Sjeff VI_UNLOCK(vp); 498183080Sjhb 499178110Sjeff /* 500183280Sjhb * Remove the hash from the list since we are going to free its 501183280Sjhb * memory. 502183280Sjhb */ 503183280Sjhb DIRHASHLIST_LOCK(); 504183280Sjhb if (dh->dh_onlist) 505183280Sjhb TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 506183280Sjhb ufs_dirhashmem -= dh->dh_memreq; 507183280Sjhb DIRHASHLIST_UNLOCK(); 508183280Sjhb 509183280Sjhb /* 510183080Sjhb * At this point, any waiters for the lock should hold their 511183080Sjhb * own reference on the dirhash structure. They will drop 512183080Sjhb * that reference once they grab the vnode interlock and see 513183080Sjhb * that ip->i_dirhash is NULL. 514178110Sjeff */ 515183080Sjhb sx_xunlock(&dh->dh_lock); 516183080Sjhb 517178110Sjeff /* 518178110Sjeff * Handle partially recycled as well as fully constructed hashes. 519178110Sjeff */ 520178110Sjeff if (dh->dh_hash != NULL) { 521178110Sjeff for (i = 0; i < dh->dh_narrays; i++) 522178110Sjeff if (dh->dh_hash[i] != NULL) 523178110Sjeff DIRHASH_BLKFREE(dh->dh_hash[i]); 524184205Sdes free(dh->dh_hash, M_DIRHASH); 525178110Sjeff if (dh->dh_blkfree != NULL) 526184205Sdes free(dh->dh_blkfree, M_DIRHASH); 527178110Sjeff } 528183080Sjhb 529178110Sjeff /* 530183080Sjhb * Drop the inode's reference to the data structure. 531178110Sjeff */ 532183080Sjhb ufsdirhash_drop(dh); 53379561Siedowse} 53479561Siedowse 53579561Siedowse/* 53679561Siedowse * Find the offset of the specified name within the given inode. 53779561Siedowse * Returns 0 on success, ENOENT if the entry does not exist, or 53879561Siedowse * EJUSTRETURN if the caller should revert to a linear search. 53979561Siedowse * 54079690Siedowse * If successful, the directory offset is stored in *offp, and a 54179690Siedowse * pointer to a struct buf containing the entry is stored in *bpp. If 54279561Siedowse * prevoffp is non-NULL, the offset of the previous entry within 54379561Siedowse * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry 54479561Siedowse * is the first in a block, the start of the block is used). 545178110Sjeff * 546178110Sjeff * Must be called with the hash locked. Returns with the hash unlocked. 54779561Siedowse */ 54879561Siedowseint 54979561Siedowseufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp, 55079690Siedowse struct buf **bpp, doff_t *prevoffp) 55179561Siedowse{ 55279561Siedowse struct dirhash *dh, *dh_next; 55379561Siedowse struct direct *dp; 55479561Siedowse struct vnode *vp; 55579561Siedowse struct buf *bp; 556219384Sjhb doff_t blkoff, bmask, offset, prevoff, seqoff; 55779561Siedowse int i, slot; 558178110Sjeff int error; 55979561Siedowse 560178110Sjeff dh = ip->i_dirhash; 561178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 562178110Sjeff ("ufsdirhash_lookup: Invalid dirhash %p\n", dh)); 563178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 56479561Siedowse /* 56579561Siedowse * Move this dirhash towards the end of the list if it has a 566178110Sjeff * score higher than the next entry, and acquire the dh_lock. 56779561Siedowse */ 568178110Sjeff DIRHASHLIST_LOCK(); 56979561Siedowse if (TAILQ_NEXT(dh, dh_list) != NULL) { 57079561Siedowse /* 57179561Siedowse * If the new score will be greater than that of the next 57279561Siedowse * entry, then move this entry past it. With both mutexes 57379561Siedowse * held, dh_next won't go away, but its dh_score could 57479561Siedowse * change; that's not important since it is just a hint. 57579561Siedowse */ 576178110Sjeff if ((dh_next = TAILQ_NEXT(dh, dh_list)) != NULL && 57779561Siedowse dh->dh_score >= dh_next->dh_score) { 57879561Siedowse KASSERT(dh->dh_onlist, ("dirhash: not on list")); 57979561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 58079561Siedowse TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh, 58179561Siedowse dh_list); 58279561Siedowse } 58379561Siedowse } 58479561Siedowse /* Update the score. */ 58579561Siedowse if (dh->dh_score < DH_SCOREMAX) 58679561Siedowse dh->dh_score++; 587193375Ssnb 588193375Ssnb /* Update last used time. */ 589193375Ssnb dh->dh_lastused = time_second; 590178110Sjeff DIRHASHLIST_UNLOCK(); 59179561Siedowse 59279561Siedowse vp = ip->i_vnode; 593219388Skib bmask = vp->v_mount->mnt_stat.f_iosize - 1; 59479561Siedowse blkoff = -1; 59579561Siedowse bp = NULL; 596219384Sjhb seqoff = dh->dh_seqoff; 59779561Siedowserestart: 59879561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 59979561Siedowse 600219384Sjhb if (seqoff != -1) { 60179561Siedowse /* 602219384Sjhb * Sequential access optimisation. seqoff contains the 60379561Siedowse * offset of the directory entry immediately following 60479561Siedowse * the last entry that was looked up. Check if this offset 60579561Siedowse * appears in the hash chain for the name we are looking for. 60679561Siedowse */ 60779561Siedowse for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY; 60879561Siedowse i = WRAPINCR(i, dh->dh_hlen)) 609219384Sjhb if (offset == seqoff) 61079561Siedowse break; 611219384Sjhb if (offset == seqoff) { 61279561Siedowse /* 61379561Siedowse * We found an entry with the expected offset. This 61479561Siedowse * is probably the entry we want, but if not, the 615219384Sjhb * code below will retry. 61679561Siedowse */ 61779561Siedowse slot = i; 618219384Sjhb } else 619219384Sjhb seqoff = -1; 62079561Siedowse } 62179561Siedowse 62279561Siedowse for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY; 62379561Siedowse slot = WRAPINCR(slot, dh->dh_hlen)) { 62479561Siedowse if (offset == DIRHASH_DEL) 62579561Siedowse continue; 62679561Siedowse if (offset < 0 || offset >= ip->i_size) 62779561Siedowse panic("ufsdirhash_lookup: bad offset in hash array"); 62879561Siedowse if ((offset & ~bmask) != blkoff) { 62979561Siedowse if (bp != NULL) 63079561Siedowse brelse(bp); 63179561Siedowse blkoff = offset & ~bmask; 632178110Sjeff if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) { 633178110Sjeff error = EJUSTRETURN; 634178110Sjeff goto fail; 635178110Sjeff } 63679561Siedowse } 637201717Smckusick KASSERT(bp != NULL, ("no buffer allocated")); 63879561Siedowse dp = (struct direct *)(bp->b_data + (offset & bmask)); 63979561Siedowse if (dp->d_reclen == 0 || dp->d_reclen > 64079561Siedowse DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) { 64179561Siedowse /* Corrupted directory. */ 642178110Sjeff error = EJUSTRETURN; 643178110Sjeff goto fail; 64479561Siedowse } 64579561Siedowse if (dp->d_namlen == namelen && 64679561Siedowse bcmp(dp->d_name, name, namelen) == 0) { 64779561Siedowse /* Found. Get the prev offset if needed. */ 64879561Siedowse if (prevoffp != NULL) { 64979561Siedowse if (offset & (DIRBLKSIZ - 1)) { 65079561Siedowse prevoff = ufsdirhash_getprev(dp, 65179561Siedowse offset); 65279561Siedowse if (prevoff == -1) { 653178110Sjeff error = EJUSTRETURN; 654178110Sjeff goto fail; 65579561Siedowse } 65679561Siedowse } else 65779561Siedowse prevoff = offset; 65879561Siedowse *prevoffp = prevoff; 65979561Siedowse } 66079561Siedowse 661219384Sjhb /* Update offset. */ 66279561Siedowse dh->dh_seqoff = offset + DIRSIZ(0, dp); 66379690Siedowse *bpp = bp; 66479561Siedowse *offp = offset; 665178110Sjeff ufsdirhash_release(dh); 66679561Siedowse return (0); 66779561Siedowse } 66879561Siedowse 66979561Siedowse /* 670219384Sjhb * When the name doesn't match in the sequential 671219384Sjhb * optimization case, go back and search normally. 67279561Siedowse */ 673219384Sjhb if (seqoff != -1) { 674219384Sjhb seqoff = -1; 67579561Siedowse goto restart; 67679561Siedowse } 67779561Siedowse } 678178110Sjeff error = ENOENT; 679178110Sjefffail: 680178110Sjeff ufsdirhash_release(dh); 68179561Siedowse if (bp != NULL) 68279561Siedowse brelse(bp); 683178110Sjeff return (error); 68479561Siedowse} 68579561Siedowse 68679561Siedowse/* 68779561Siedowse * Find a directory block with room for 'slotneeded' bytes. Returns 68879561Siedowse * the offset of the directory entry that begins the free space. 68979561Siedowse * This will either be the offset of an existing entry that has free 69079561Siedowse * space at the end, or the offset of an entry with d_ino == 0 at 69179561Siedowse * the start of a DIRBLKSIZ block. 69279561Siedowse * 69379561Siedowse * To use the space, the caller may need to compact existing entries in 69479561Siedowse * the directory. The total number of bytes in all of the entries involved 69579561Siedowse * in the compaction is stored in *slotsize. In other words, all of 69679561Siedowse * the entries that must be compacted are exactly contained in the 69779561Siedowse * region beginning at the returned offset and spanning *slotsize bytes. 69879561Siedowse * 69979561Siedowse * Returns -1 if no space was found, indicating that the directory 70079561Siedowse * must be extended. 70179561Siedowse */ 70279561Siedowsedoff_t 70379561Siedowseufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize) 70479561Siedowse{ 70579561Siedowse struct direct *dp; 70679561Siedowse struct dirhash *dh; 70779561Siedowse struct buf *bp; 70879561Siedowse doff_t pos, slotstart; 70979561Siedowse int dirblock, error, freebytes, i; 71079561Siedowse 711178110Sjeff dh = ip->i_dirhash; 712178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 713178110Sjeff ("ufsdirhash_findfree: Invalid dirhash %p\n", dh)); 714178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 71579561Siedowse 71679561Siedowse /* Find a directory block with the desired free space. */ 71779561Siedowse dirblock = -1; 71879561Siedowse for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++) 71979561Siedowse if ((dirblock = dh->dh_firstfree[i]) != -1) 72079561Siedowse break; 721178110Sjeff if (dirblock == -1) 72279561Siedowse return (-1); 72379561Siedowse 72479561Siedowse KASSERT(dirblock < dh->dh_nblk && 72579561Siedowse dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN), 72679561Siedowse ("ufsdirhash_findfree: bad stats")); 72779561Siedowse pos = dirblock * DIRBLKSIZ; 72879561Siedowse error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp); 72979561Siedowse if (error) 73079561Siedowse return (-1); 73179561Siedowse 73279561Siedowse /* Find the first entry with free space. */ 73379561Siedowse for (i = 0; i < DIRBLKSIZ; ) { 73479561Siedowse if (dp->d_reclen == 0) { 73579561Siedowse brelse(bp); 73679561Siedowse return (-1); 73779561Siedowse } 73879561Siedowse if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp)) 73979561Siedowse break; 74079561Siedowse i += dp->d_reclen; 74179561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 74279561Siedowse } 74379561Siedowse if (i > DIRBLKSIZ) { 74479561Siedowse brelse(bp); 74579561Siedowse return (-1); 74679561Siedowse } 74779561Siedowse slotstart = pos + i; 74879561Siedowse 74979561Siedowse /* Find the range of entries needed to get enough space */ 75079561Siedowse freebytes = 0; 75179561Siedowse while (i < DIRBLKSIZ && freebytes < slotneeded) { 75279561Siedowse freebytes += dp->d_reclen; 75379561Siedowse if (dp->d_ino != 0) 75479561Siedowse freebytes -= DIRSIZ(0, dp); 75579561Siedowse if (dp->d_reclen == 0) { 75679561Siedowse brelse(bp); 75779561Siedowse return (-1); 75879561Siedowse } 75979561Siedowse i += dp->d_reclen; 76079561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 76179561Siedowse } 76279561Siedowse if (i > DIRBLKSIZ) { 76379561Siedowse brelse(bp); 76479561Siedowse return (-1); 76579561Siedowse } 76679561Siedowse if (freebytes < slotneeded) 76779561Siedowse panic("ufsdirhash_findfree: free mismatch"); 76879561Siedowse brelse(bp); 76979561Siedowse *slotsize = pos + i - slotstart; 77079561Siedowse return (slotstart); 77179561Siedowse} 77279561Siedowse 77379561Siedowse/* 77479561Siedowse * Return the start of the unused space at the end of a directory, or 77579561Siedowse * -1 if there are no trailing unused blocks. 77679561Siedowse */ 77779561Siedowsedoff_t 77879561Siedowseufsdirhash_enduseful(struct inode *ip) 77979561Siedowse{ 78079561Siedowse 78179561Siedowse struct dirhash *dh; 78279561Siedowse int i; 78379561Siedowse 784178110Sjeff dh = ip->i_dirhash; 785178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 786178110Sjeff KASSERT(dh != NULL && dh->dh_hash != NULL, 787178110Sjeff ("ufsdirhash_enduseful: Invalid dirhash %p\n", dh)); 78879561Siedowse 789178110Sjeff if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN) 79079561Siedowse return (-1); 79179561Siedowse 79279561Siedowse for (i = dh->dh_dirblks - 1; i >= 0; i--) 79379561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 79479561Siedowse break; 795178110Sjeff 79679561Siedowse return ((doff_t)(i + 1) * DIRBLKSIZ); 79779561Siedowse} 79879561Siedowse 79979561Siedowse/* 80079561Siedowse * Insert information into the hash about a new directory entry. dirp 80179561Siedowse * points to a struct direct containing the entry, and offset specifies 80279561Siedowse * the offset of this entry. 80379561Siedowse */ 80479561Siedowsevoid 80579561Siedowseufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset) 80679561Siedowse{ 80779561Siedowse struct dirhash *dh; 80879561Siedowse int slot; 80979561Siedowse 810178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 81179561Siedowse return; 812178110Sjeff 81379561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 81479561Siedowse ("ufsdirhash_add: bad offset")); 81579561Siedowse /* 81679561Siedowse * Normal hash usage is < 66%. If the usage gets too high then 81779561Siedowse * remove the hash entirely and let it be rebuilt later. 81879561Siedowse */ 81979561Siedowse if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) { 820178110Sjeff ufsdirhash_free_locked(ip); 82179561Siedowse return; 82279561Siedowse } 82379561Siedowse 82479561Siedowse /* Find a free hash slot (empty or deleted), and add the entry. */ 82579561Siedowse slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen); 82679561Siedowse while (DH_ENTRY(dh, slot) >= 0) 82779561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 82879561Siedowse if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY) 82979561Siedowse dh->dh_hused++; 83079561Siedowse DH_ENTRY(dh, slot) = offset; 83179561Siedowse 832193375Ssnb /* Update last used time. */ 833193375Ssnb dh->dh_lastused = time_second; 834193375Ssnb 83579561Siedowse /* Update the per-block summary info. */ 83679561Siedowse ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp)); 837178110Sjeff ufsdirhash_release(dh); 83879561Siedowse} 83979561Siedowse 84079561Siedowse/* 84179561Siedowse * Remove the specified directory entry from the hash. The entry to remove 84279561Siedowse * is defined by the name in `dirp', which must exist at the specified 84379561Siedowse * `offset' within the directory. 84479561Siedowse */ 84579561Siedowsevoid 84679561Siedowseufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset) 84779561Siedowse{ 84879561Siedowse struct dirhash *dh; 84979561Siedowse int slot; 85079561Siedowse 851178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 85279561Siedowse return; 85379561Siedowse 85479561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 85579561Siedowse ("ufsdirhash_remove: bad offset")); 85679561Siedowse /* Find the entry */ 85779561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset); 85879561Siedowse 85979561Siedowse /* Remove the hash entry. */ 86079561Siedowse ufsdirhash_delslot(dh, slot); 86179561Siedowse 86279561Siedowse /* Update the per-block summary info. */ 86379561Siedowse ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp)); 864178110Sjeff ufsdirhash_release(dh); 86579561Siedowse} 86679561Siedowse 86779561Siedowse/* 86879561Siedowse * Change the offset associated with a directory entry in the hash. Used 86979561Siedowse * when compacting directory blocks. 87079561Siedowse */ 87179561Siedowsevoid 87279561Siedowseufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff, 87379561Siedowse doff_t newoff) 87479561Siedowse{ 87579561Siedowse struct dirhash *dh; 87679561Siedowse int slot; 87779561Siedowse 878178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 87979561Siedowse return; 88079561Siedowse 88179561Siedowse KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ && 88279561Siedowse newoff < dh->dh_dirblks * DIRBLKSIZ, 88379561Siedowse ("ufsdirhash_move: bad offset")); 88479561Siedowse /* Find the entry, and update the offset. */ 88579561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff); 88679561Siedowse DH_ENTRY(dh, slot) = newoff; 887178110Sjeff ufsdirhash_release(dh); 88879561Siedowse} 88979561Siedowse 89079561Siedowse/* 89179561Siedowse * Inform dirhash that the directory has grown by one block that 89279561Siedowse * begins at offset (i.e. the new length is offset + DIRBLKSIZ). 89379561Siedowse */ 89479561Siedowsevoid 89579561Siedowseufsdirhash_newblk(struct inode *ip, doff_t offset) 89679561Siedowse{ 89779561Siedowse struct dirhash *dh; 89879561Siedowse int block; 89979561Siedowse 900178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 90179561Siedowse return; 90279561Siedowse 90379561Siedowse KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ, 90479561Siedowse ("ufsdirhash_newblk: bad offset")); 90579561Siedowse block = offset / DIRBLKSIZ; 90679561Siedowse if (block >= dh->dh_nblk) { 90779561Siedowse /* Out of space; must rebuild. */ 908178110Sjeff ufsdirhash_free_locked(ip); 90979561Siedowse return; 91079561Siedowse } 91179561Siedowse dh->dh_dirblks = block + 1; 91279561Siedowse 91379561Siedowse /* Account for the new free block. */ 91479561Siedowse dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN; 91579561Siedowse if (dh->dh_firstfree[DH_NFSTATS] == -1) 91679561Siedowse dh->dh_firstfree[DH_NFSTATS] = block; 917178110Sjeff ufsdirhash_release(dh); 91879561Siedowse} 91979561Siedowse 92079561Siedowse/* 92179561Siedowse * Inform dirhash that the directory is being truncated. 92279561Siedowse */ 92379561Siedowsevoid 92479561Siedowseufsdirhash_dirtrunc(struct inode *ip, doff_t offset) 92579561Siedowse{ 92679561Siedowse struct dirhash *dh; 92779561Siedowse int block, i; 92879561Siedowse 929178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 93079561Siedowse return; 93179561Siedowse 93279561Siedowse KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ, 93379561Siedowse ("ufsdirhash_dirtrunc: bad offset")); 93479561Siedowse block = howmany(offset, DIRBLKSIZ); 93579561Siedowse /* 93679561Siedowse * If the directory shrinks to less than 1/8 of dh_nblk blocks 93779561Siedowse * (about 20% of its original size due to the 50% extra added in 93879561Siedowse * ufsdirhash_build) then free it, and let the caller rebuild 93979561Siedowse * if necessary. 94079561Siedowse */ 94179561Siedowse if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) { 942178110Sjeff ufsdirhash_free_locked(ip); 94379561Siedowse return; 94479561Siedowse } 94579561Siedowse 94679561Siedowse /* 94779561Siedowse * Remove any `first free' information pertaining to the 94879561Siedowse * truncated blocks. All blocks we're removing should be 94979561Siedowse * completely unused. 95079561Siedowse */ 95179561Siedowse if (dh->dh_firstfree[DH_NFSTATS] >= block) 95279561Siedowse dh->dh_firstfree[DH_NFSTATS] = -1; 95379561Siedowse for (i = block; i < dh->dh_dirblks; i++) 95479561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 95579561Siedowse panic("ufsdirhash_dirtrunc: blocks in use"); 95679561Siedowse for (i = 0; i < DH_NFSTATS; i++) 95779561Siedowse if (dh->dh_firstfree[i] >= block) 95879561Siedowse panic("ufsdirhash_dirtrunc: first free corrupt"); 95979561Siedowse dh->dh_dirblks = block; 960178110Sjeff ufsdirhash_release(dh); 96179561Siedowse} 96279561Siedowse 96379561Siedowse/* 96479561Siedowse * Debugging function to check that the dirhash information about 96579561Siedowse * a directory block matches its actual contents. Panics if a mismatch 96679561Siedowse * is detected. 96779561Siedowse * 96879561Siedowse * On entry, `buf' should point to the start of an in-core 96979561Siedowse * DIRBLKSIZ-sized directory block, and `offset' should contain the 97079561Siedowse * offset from the start of the directory of that block. 97179561Siedowse */ 97279561Siedowsevoid 97379561Siedowseufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset) 97479561Siedowse{ 97579561Siedowse struct dirhash *dh; 97679561Siedowse struct direct *dp; 97779561Siedowse int block, ffslot, i, nfree; 97879561Siedowse 97979561Siedowse if (!ufs_dirhashcheck) 98079561Siedowse return; 981178110Sjeff if ((dh = ufsdirhash_acquire(ip)) == NULL) 98279561Siedowse return; 98379561Siedowse 98479561Siedowse block = offset / DIRBLKSIZ; 98579561Siedowse if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks) 98679561Siedowse panic("ufsdirhash_checkblock: bad offset"); 98779561Siedowse 98879561Siedowse nfree = 0; 98979561Siedowse for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) { 99079561Siedowse dp = (struct direct *)(buf + i); 99179561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ) 99279561Siedowse panic("ufsdirhash_checkblock: bad dir"); 99379561Siedowse 99479561Siedowse if (dp->d_ino == 0) { 99580456Siedowse#if 0 99680456Siedowse /* 99780456Siedowse * XXX entries with d_ino == 0 should only occur 99880456Siedowse * at the start of a DIRBLKSIZ block. However the 99980456Siedowse * ufs code is tolerant of such entries at other 100080456Siedowse * offsets, and fsck does not fix them. 100180456Siedowse */ 100279561Siedowse if (i != 0) 100379561Siedowse panic("ufsdirhash_checkblock: bad dir inode"); 100480456Siedowse#endif 100579561Siedowse nfree += dp->d_reclen; 100679561Siedowse continue; 100779561Siedowse } 100879561Siedowse 100979561Siedowse /* Check that the entry exists (will panic if it doesn't). */ 101079561Siedowse ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i); 101179561Siedowse 101279561Siedowse nfree += dp->d_reclen - DIRSIZ(0, dp); 101379561Siedowse } 101479561Siedowse if (i != DIRBLKSIZ) 101579561Siedowse panic("ufsdirhash_checkblock: bad dir end"); 101679561Siedowse 101779561Siedowse if (dh->dh_blkfree[block] * DIRALIGN != nfree) 101879561Siedowse panic("ufsdirhash_checkblock: bad free count"); 101979561Siedowse 102092098Siedowse ffslot = BLKFREE2IDX(nfree / DIRALIGN); 102179561Siedowse for (i = 0; i <= DH_NFSTATS; i++) 102279561Siedowse if (dh->dh_firstfree[i] == block && i != ffslot) 102379561Siedowse panic("ufsdirhash_checkblock: bad first-free"); 102492098Siedowse if (dh->dh_firstfree[ffslot] == -1) 102592098Siedowse panic("ufsdirhash_checkblock: missing first-free entry"); 1026178110Sjeff ufsdirhash_release(dh); 102779561Siedowse} 102879561Siedowse 102979561Siedowse/* 103079561Siedowse * Hash the specified filename into a dirhash slot. 103179561Siedowse */ 103279561Siedowsestatic int 103379561Siedowseufsdirhash_hash(struct dirhash *dh, char *name, int namelen) 103479561Siedowse{ 103592807Sdwmalone u_int32_t hash; 103692807Sdwmalone 103792807Sdwmalone /* 1038107868Siedowse * We hash the name and then some other bit of data that is 1039107868Siedowse * invariant over the dirhash's lifetime. Otherwise names 104092807Sdwmalone * differing only in the last byte are placed close to one 104192807Sdwmalone * another in the table, which is bad for linear probing. 104292807Sdwmalone */ 104392807Sdwmalone hash = fnv_32_buf(name, namelen, FNV1_32_INIT); 1044133837Sdwmalone hash = fnv_32_buf(&dh, sizeof(dh), hash); 104592807Sdwmalone return (hash % dh->dh_hlen); 104679561Siedowse} 104779561Siedowse 104879561Siedowse/* 104979561Siedowse * Adjust the number of free bytes in the block containing `offset' 105079561Siedowse * by the value specified by `diff'. 105179561Siedowse * 105279561Siedowse * The caller must ensure we have exclusive access to `dh'; normally 1053178110Sjeff * that means that dh_lock should be held, but this is also called 105479561Siedowse * from ufsdirhash_build() where exclusive access can be assumed. 105579561Siedowse */ 105679561Siedowsestatic void 105779561Siedowseufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff) 105879561Siedowse{ 105979561Siedowse int block, i, nfidx, ofidx; 106079561Siedowse 106179561Siedowse /* Update the per-block summary info. */ 106279561Siedowse block = offset / DIRBLKSIZ; 106379561Siedowse KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks, 106479561Siedowse ("dirhash bad offset")); 106592098Siedowse ofidx = BLKFREE2IDX(dh->dh_blkfree[block]); 106679561Siedowse dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN); 106792098Siedowse nfidx = BLKFREE2IDX(dh->dh_blkfree[block]); 106879561Siedowse 106979561Siedowse /* Update the `first free' list if necessary. */ 107079561Siedowse if (ofidx != nfidx) { 107179561Siedowse /* If removing, scan forward for the next block. */ 107279561Siedowse if (dh->dh_firstfree[ofidx] == block) { 107379561Siedowse for (i = block + 1; i < dh->dh_dirblks; i++) 107492098Siedowse if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx) 107579561Siedowse break; 107679561Siedowse dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1; 107779561Siedowse } 107879561Siedowse 107979561Siedowse /* Make this the new `first free' if necessary */ 108079561Siedowse if (dh->dh_firstfree[nfidx] > block || 108179561Siedowse dh->dh_firstfree[nfidx] == -1) 108279561Siedowse dh->dh_firstfree[nfidx] = block; 108379561Siedowse } 108479561Siedowse} 108579561Siedowse 108679561Siedowse/* 108779561Siedowse * Find the specified name which should have the specified offset. 108879561Siedowse * Returns a slot number, and panics on failure. 108979561Siedowse * 109079561Siedowse * `dh' must be locked on entry and remains so on return. 109179561Siedowse */ 109279561Siedowsestatic int 109379561Siedowseufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset) 109479561Siedowse{ 109579561Siedowse int slot; 109679561Siedowse 1097178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 109879561Siedowse 109979561Siedowse /* Find the entry. */ 110079561Siedowse KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full")); 110179561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 110279561Siedowse while (DH_ENTRY(dh, slot) != offset && 110379561Siedowse DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 110479561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 110579561Siedowse if (DH_ENTRY(dh, slot) != offset) 110679561Siedowse panic("ufsdirhash_findslot: '%.*s' not found", namelen, name); 110779561Siedowse 110879561Siedowse return (slot); 110979561Siedowse} 111079561Siedowse 111179561Siedowse/* 111279561Siedowse * Remove the entry corresponding to the specified slot from the hash array. 111379561Siedowse * 111479561Siedowse * `dh' must be locked on entry and remains so on return. 111579561Siedowse */ 111679561Siedowsestatic void 111779561Siedowseufsdirhash_delslot(struct dirhash *dh, int slot) 111879561Siedowse{ 111979561Siedowse int i; 112079561Siedowse 1121178110Sjeff DIRHASH_ASSERT_LOCKED(dh); 112279561Siedowse 112379561Siedowse /* Mark the entry as deleted. */ 112479561Siedowse DH_ENTRY(dh, slot) = DIRHASH_DEL; 112579561Siedowse 112679561Siedowse /* If this is the end of a chain of DIRHASH_DEL slots, remove them. */ 112779561Siedowse for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; ) 112879561Siedowse i = WRAPINCR(i, dh->dh_hlen); 112979561Siedowse if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) { 113092807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 113192807Sdwmalone while (DH_ENTRY(dh, i) == DIRHASH_DEL) { 113279561Siedowse DH_ENTRY(dh, i) = DIRHASH_EMPTY; 113379561Siedowse dh->dh_hused--; 113492807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 113579561Siedowse } 113679561Siedowse KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen")); 113779561Siedowse } 113879561Siedowse} 113979561Siedowse 114079561Siedowse/* 114179561Siedowse * Given a directory entry and its offset, find the offset of the 114279561Siedowse * previous entry in the same DIRBLKSIZ-sized block. Returns an 114379561Siedowse * offset, or -1 if there is no previous entry in the block or some 114479561Siedowse * other problem occurred. 114579561Siedowse */ 114679561Siedowsestatic doff_t 114779561Siedowseufsdirhash_getprev(struct direct *dirp, doff_t offset) 114879561Siedowse{ 114979561Siedowse struct direct *dp; 115079561Siedowse char *blkbuf; 115179561Siedowse doff_t blkoff, prevoff; 115279561Siedowse int entrypos, i; 115379561Siedowse 1154298433Spfg blkoff = rounddown2(offset, DIRBLKSIZ); /* offset of start of block */ 115579561Siedowse entrypos = offset & (DIRBLKSIZ - 1); /* entry relative to block */ 115679561Siedowse blkbuf = (char *)dirp - entrypos; 115779561Siedowse prevoff = blkoff; 115879561Siedowse 115979561Siedowse /* If `offset' is the start of a block, there is no previous entry. */ 116079561Siedowse if (entrypos == 0) 116179561Siedowse return (-1); 116279561Siedowse 116379561Siedowse /* Scan from the start of the block until we get to the entry. */ 116479561Siedowse for (i = 0; i < entrypos; i += dp->d_reclen) { 116579561Siedowse dp = (struct direct *)(blkbuf + i); 116679561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos) 116779561Siedowse return (-1); /* Corrupted directory. */ 116879561Siedowse prevoff = blkoff + i; 116979561Siedowse } 117079561Siedowse return (prevoff); 117179561Siedowse} 117279561Siedowse 117379561Siedowse/* 1174193375Ssnb * Delete the given dirhash and reclaim its memory. Assumes that 1175193375Ssnb * ufsdirhash_list is locked, and leaves it locked. Also assumes 1176193375Ssnb * that dh is locked. Returns the amount of memory freed. 1177193375Ssnb */ 1178193375Ssnbstatic int 1179193375Ssnbufsdirhash_destroy(struct dirhash *dh) 1180193375Ssnb{ 1181193375Ssnb doff_t **hash; 1182193375Ssnb u_int8_t *blkfree; 1183193375Ssnb int i, mem, narrays; 1184193375Ssnb 1185193375Ssnb KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list")); 1186193375Ssnb 1187193375Ssnb /* Remove it from the list and detach its memory. */ 1188193375Ssnb TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 1189193375Ssnb dh->dh_onlist = 0; 1190193375Ssnb hash = dh->dh_hash; 1191193375Ssnb dh->dh_hash = NULL; 1192193375Ssnb blkfree = dh->dh_blkfree; 1193193375Ssnb dh->dh_blkfree = NULL; 1194193375Ssnb narrays = dh->dh_narrays; 1195193375Ssnb mem = dh->dh_memreq; 1196193375Ssnb dh->dh_memreq = 0; 1197193375Ssnb 1198194387Ssnb /* Unlock dirhash and free the detached memory. */ 1199193375Ssnb ufsdirhash_release(dh); 1200193375Ssnb for (i = 0; i < narrays; i++) 1201193375Ssnb DIRHASH_BLKFREE(hash[i]); 1202193375Ssnb free(hash, M_DIRHASH); 1203193375Ssnb free(blkfree, M_DIRHASH); 1204193375Ssnb 1205193375Ssnb /* Account for the returned memory. */ 1206193375Ssnb ufs_dirhashmem -= mem; 1207193375Ssnb 1208193375Ssnb return (mem); 1209193375Ssnb} 1210193375Ssnb 1211193375Ssnb/* 121279561Siedowse * Try to free up `wanted' bytes by stealing memory from existing 1213125854Sdwmalone * dirhashes. Returns zero with list locked if successful. 121479561Siedowse */ 121579561Siedowsestatic int 121679561Siedowseufsdirhash_recycle(int wanted) 121779561Siedowse{ 121879561Siedowse struct dirhash *dh; 121979561Siedowse 1220125854Sdwmalone DIRHASHLIST_LOCK(); 1221178110Sjeff dh = TAILQ_FIRST(&ufsdirhash_list); 122279561Siedowse while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) { 1223178110Sjeff /* Decrement the score; only recycle if it becomes zero. */ 1224178110Sjeff if (dh == NULL || --dh->dh_score > 0) { 1225125854Sdwmalone DIRHASHLIST_UNLOCK(); 122679561Siedowse return (-1); 122779561Siedowse } 1228178110Sjeff /* 1229178110Sjeff * If we can't lock it it's in use and we don't want to 1230178110Sjeff * recycle it anyway. 1231178110Sjeff */ 1232183080Sjhb if (!sx_try_xlock(&dh->dh_lock)) { 1233178110Sjeff dh = TAILQ_NEXT(dh, dh_list); 1234178110Sjeff continue; 1235178110Sjeff } 123679561Siedowse 1237193375Ssnb ufsdirhash_destroy(dh); 123879561Siedowse 1239193375Ssnb /* Repeat if necessary. */ 1240178110Sjeff dh = TAILQ_FIRST(&ufsdirhash_list); 124179561Siedowse } 1242125854Sdwmalone /* Success; return with list locked. */ 124379561Siedowse return (0); 124479561Siedowse} 124579561Siedowse 1246193375Ssnb/* 1247193375Ssnb * Callback that frees some dirhashes when the system is low on virtual memory. 1248193375Ssnb */ 1249193375Ssnbstatic void 1250193375Ssnbufsdirhash_lowmem() 1251193375Ssnb{ 1252194387Ssnb struct dirhash *dh, *dh_temp; 1253270588Sdavide int memfreed, memwanted; 125479561Siedowse 1255193375Ssnb ufs_dirhashlowmemcount++; 1256270588Sdavide memfreed = 0; 1257274835Sdavide memwanted = ufs_dirhashmem * ufs_dirhashreclaimpercent / 100; 1258193375Ssnb 1259193375Ssnb DIRHASHLIST_LOCK(); 1260270588Sdavide 1261270588Sdavide /* 1262270588Sdavide * Reclaim up to memwanted from the oldest dirhashes. This will allow 1263270588Sdavide * us to make some progress when the system is running out of memory 1264270588Sdavide * without compromising the dinamicity of maximum age. If the situation 1265270588Sdavide * does not improve lowmem will be eventually retriggered and free some 1266270588Sdavide * other entry in the cache. The entries on the head of the list should 1267270588Sdavide * be the oldest. If during list traversal we can't get a lock on the 1268270588Sdavide * dirhash, it will be skipped. 1269193375Ssnb */ 1270194387Ssnb TAILQ_FOREACH_SAFE(dh, &ufsdirhash_list, dh_list, dh_temp) { 1271270588Sdavide if (sx_try_xlock(&dh->dh_lock)) 1272193375Ssnb memfreed += ufsdirhash_destroy(dh); 1273270588Sdavide if (memfreed >= memwanted) 1274270588Sdavide break; 1275193375Ssnb } 1276193375Ssnb DIRHASHLIST_UNLOCK(); 1277193375Ssnb} 1278193375Ssnb 1279274834Sdavidestatic int 1280274834Sdavideufsdirhash_set_reclaimpercent(SYSCTL_HANDLER_ARGS) 1281274834Sdavide{ 1282274834Sdavide int error, v; 1283193375Ssnb 1284274834Sdavide v = ufs_dirhashreclaimpercent; 1285274834Sdavide error = sysctl_handle_int(oidp, &v, v, req); 1286274834Sdavide if (error) 1287274834Sdavide return (error); 1288274834Sdavide if (req->newptr == NULL) 1289274834Sdavide return (error); 1290274834Sdavide if (v == ufs_dirhashreclaimpercent) 1291274834Sdavide return (0); 1292274834Sdavide 1293274834Sdavide /* Refuse invalid percentages */ 1294274834Sdavide if (v < 0 || v > 100) 1295274834Sdavide return (EINVAL); 1296274834Sdavide ufs_dirhashreclaimpercent = v; 1297274834Sdavide return (0); 1298274834Sdavide} 1299274834Sdavide 130099101Siedowsevoid 130179561Siedowseufsdirhash_init() 130279561Siedowse{ 1303214359Sivoras ufs_dirhashmaxmem = lmax(roundup(hibufspace / 64, PAGE_SIZE), 1304214359Sivoras 2 * 1024 * 1024); 1305214359Sivoras 130696874Siedowse ufsdirhash_zone = uma_zcreate("DIRHASH", DH_NBLKOFF * sizeof(doff_t), 130792768Sjeff NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 130893818Sjhb mtx_init(&ufsdirhash_mtx, "dirhash list", NULL, MTX_DEF); 130979561Siedowse TAILQ_INIT(&ufsdirhash_list); 1310193375Ssnb 1311193375Ssnb /* Register a callback function to handle low memory signals */ 1312193375Ssnb EVENTHANDLER_REGISTER(vm_lowmem, ufsdirhash_lowmem, NULL, 1313193375Ssnb EVENTHANDLER_PRI_FIRST); 131479561Siedowse} 131579561Siedowse 131699101Siedowsevoid 131799101Siedowseufsdirhash_uninit() 131899101Siedowse{ 131999101Siedowse KASSERT(TAILQ_EMPTY(&ufsdirhash_list), ("ufsdirhash_uninit")); 132099101Siedowse uma_zdestroy(ufsdirhash_zone); 132199101Siedowse mtx_destroy(&ufsdirhash_mtx); 132299101Siedowse} 132379561Siedowse 132479561Siedowse#endif /* UFS_DIRHASH */ 1325