ufs_dirhash.c revision 92807
179561Siedowse/* 279561Siedowse * Copyright (c) 2001 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 * 2579561Siedowse * $FreeBSD: head/sys/ufs/ufs/ufs_dirhash.c 92807 2002-03-20 17:58:02Z dwmalone $ 2679561Siedowse */ 2779561Siedowse/* 2879561Siedowse * This implements a hash-based lookup scheme for UFS directories. 2979561Siedowse */ 3079561Siedowse 3179561Siedowse#include "opt_ufs.h" 3279561Siedowse 3379561Siedowse#ifdef UFS_DIRHASH 3479561Siedowse 3579561Siedowse#include <sys/param.h> 3679561Siedowse#include <sys/systm.h> 3779561Siedowse#include <sys/kernel.h> 3884811Sjhb#include <sys/lock.h> 3979561Siedowse#include <sys/mutex.h> 4079561Siedowse#include <sys/malloc.h> 4179561Siedowse#include <sys/fnv_hash.h> 4279561Siedowse#include <sys/proc.h> 4379561Siedowse#include <sys/bio.h> 4479561Siedowse#include <sys/buf.h> 4579561Siedowse#include <sys/vnode.h> 4679561Siedowse#include <sys/mount.h> 4779561Siedowse#include <sys/sysctl.h> 4892768Sjeff#include <vm/uma.h> 4979561Siedowse 5079561Siedowse#include <ufs/ufs/quota.h> 5179561Siedowse#include <ufs/ufs/inode.h> 5279561Siedowse#include <ufs/ufs/dir.h> 5379561Siedowse#include <ufs/ufs/dirhash.h> 5479561Siedowse#include <ufs/ufs/extattr.h> 5579561Siedowse#include <ufs/ufs/ufsmount.h> 5679561Siedowse#include <ufs/ufs/ufs_extern.h> 5779561Siedowse 5879561Siedowse#define WRAPINCR(val, limit) (((val) + 1 == (limit)) ? 0 : ((val) + 1)) 5992807Sdwmalone#define WRAPDECR(val, limit) (((val) == 0) ? ((limit) - 1) : ((val) - 1)) 6079561Siedowse#define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0) 6192098Siedowse#define BLKFREE2IDX(n) ((n) > DH_NFSTATS ? DH_NFSTATS : (n)) 6279561Siedowse 6379561Siedowsestatic MALLOC_DEFINE(M_DIRHASH, "UFS dirhash", "UFS directory hash tables"); 6479561Siedowse 6579561SiedowseSYSCTL_NODE(_vfs, OID_AUTO, ufs, CTLFLAG_RD, 0, "UFS filesystem"); 6679561Siedowse 6779561Siedowsestatic int ufs_mindirhashsize = DIRBLKSIZ * 5; 6879561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_minsize, CTLFLAG_RW, 6979561Siedowse &ufs_mindirhashsize, 7079561Siedowse 0, "minimum directory size in bytes for which to use hashed lookup"); 7179561Siedowsestatic int ufs_dirhashmaxmem = 2 * 1024 * 1024; 7279561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_maxmem, CTLFLAG_RW, &ufs_dirhashmaxmem, 7379561Siedowse 0, "maximum allowed dirhash memory usage"); 7479561Siedowsestatic int ufs_dirhashmem; 7579561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_mem, CTLFLAG_RD, &ufs_dirhashmem, 7679561Siedowse 0, "current dirhash memory usage"); 7785512Siedowsestatic int ufs_dirhashcheck = 0; 7879561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_docheck, CTLFLAG_RW, &ufs_dirhashcheck, 7979561Siedowse 0, "enable extra sanity tests"); 8079561Siedowse 8179561Siedowse 8279561Siedowsestatic int ufsdirhash_hash(struct dirhash *dh, char *name, int namelen); 8379561Siedowsestatic void ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff); 8479561Siedowsestatic void ufsdirhash_delslot(struct dirhash *dh, int slot); 8579561Siedowsestatic int ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, 8679561Siedowse doff_t offset); 8779561Siedowsestatic doff_t ufsdirhash_getprev(struct direct *dp, doff_t offset); 8879561Siedowsestatic void ufsdirhash_init(void); 8979561Siedowsestatic int ufsdirhash_recycle(int wanted); 9079561Siedowse 9192768Sjeffstatic uma_zone_t ufsdirhash_zone; 9279561Siedowse 9379561Siedowse/* Dirhash list; recently-used entries are near the tail. */ 9479561Siedowsestatic TAILQ_HEAD(, dirhash) ufsdirhash_list; 9579561Siedowse 9679561Siedowse/* Protects: ufsdirhash_list, `dh_list' field, ufs_dirhashmem. */ 9779561Siedowsestatic struct mtx ufsdirhash_mtx; 9879561Siedowse 9979561Siedowse/* 10079561Siedowse * Locking order: 10179561Siedowse * ufsdirhash_mtx 10279561Siedowse * dh_mtx 10379561Siedowse * 10479561Siedowse * The dh_mtx mutex should be aquired either via the inode lock, or via 10579561Siedowse * ufsdirhash_mtx. Only the owner of the inode may free the associated 10679561Siedowse * dirhash, but anything can steal its memory and set dh_hash to NULL. 10779561Siedowse */ 10879561Siedowse 10979561Siedowse/* 11079561Siedowse * Attempt to build up a hash table for the directory contents in 11179561Siedowse * inode 'ip'. Returns 0 on success, or -1 of the operation failed. 11279561Siedowse */ 11379561Siedowseint 11479561Siedowseufsdirhash_build(struct inode *ip) 11579561Siedowse{ 11679561Siedowse struct dirhash *dh; 11779561Siedowse struct buf *bp = NULL; 11879561Siedowse struct direct *ep; 11979561Siedowse struct vnode *vp; 12079561Siedowse doff_t bmask, pos; 12179561Siedowse int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot; 12279561Siedowse 12379561Siedowse /* Check if we can/should use dirhash. */ 12479561Siedowse if (ip->i_dirhash == NULL) { 12579561Siedowse if (ip->i_size < ufs_mindirhashsize || OFSFMT(ip->i_vnode)) 12679561Siedowse return (-1); 12779561Siedowse } else { 12879561Siedowse /* Hash exists, but sysctls could have changed. */ 12979561Siedowse if (ip->i_size < ufs_mindirhashsize || 13079561Siedowse ufs_dirhashmem > ufs_dirhashmaxmem) { 13179561Siedowse ufsdirhash_free(ip); 13279561Siedowse return (-1); 13379561Siedowse } 13479561Siedowse /* Check if hash exists and is intact (note: unlocked read). */ 13579561Siedowse if (ip->i_dirhash->dh_hash != NULL) 13679561Siedowse return (0); 13779561Siedowse /* Free the old, recycled hash and build a new one. */ 13879561Siedowse ufsdirhash_free(ip); 13979561Siedowse } 14079561Siedowse 14182364Siedowse /* Don't hash removed directories. */ 14282364Siedowse if (ip->i_effnlink == 0) 14382364Siedowse return (-1); 14482364Siedowse 14579561Siedowse vp = ip->i_vnode; 14679561Siedowse /* Allocate 50% more entries than this dir size could ever need. */ 14779561Siedowse KASSERT(ip->i_size >= DIRBLKSIZ, ("ufsdirhash_build size")); 14879561Siedowse nslots = ip->i_size / DIRECTSIZ(1); 14979561Siedowse nslots = (nslots * 3 + 1) / 2; 15079561Siedowse narrays = howmany(nslots, DH_NBLKOFF); 15179561Siedowse nslots = narrays * DH_NBLKOFF; 15279561Siedowse dirblocks = howmany(ip->i_size, DIRBLKSIZ); 15379561Siedowse nblocks = (dirblocks * 3 + 1) / 2; 15479561Siedowse 15579561Siedowse memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) + 15679561Siedowse narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 15779561Siedowse nblocks * sizeof(*dh->dh_blkfree); 15879561Siedowse mtx_lock(&ufsdirhash_mtx); 15979561Siedowse if (memreqd + ufs_dirhashmem > ufs_dirhashmaxmem) { 16079561Siedowse mtx_unlock(&ufsdirhash_mtx); 16179561Siedowse if (memreqd > ufs_dirhashmaxmem / 2) 16279561Siedowse return (-1); 16379561Siedowse 16479561Siedowse /* Try to free some space. */ 16579561Siedowse if (ufsdirhash_recycle(memreqd) != 0) 16679561Siedowse return (-1); 16779561Siedowse /* Enough was freed, and ufsdirhash_mtx has been locked. */ 16879561Siedowse } 16979561Siedowse ufs_dirhashmem += memreqd; 17079561Siedowse mtx_unlock(&ufsdirhash_mtx); 17179561Siedowse 17279561Siedowse /* 17379561Siedowse * Use non-blocking mallocs so that we will revert to a linear 17479561Siedowse * lookup on failure rather than potentially blocking forever. 17579561Siedowse */ 17679561Siedowse MALLOC(dh, struct dirhash *, sizeof *dh, M_DIRHASH, M_NOWAIT | M_ZERO); 17779561Siedowse if (dh == NULL) 17879561Siedowse return (-1); 17979561Siedowse MALLOC(dh->dh_hash, doff_t **, narrays * sizeof(dh->dh_hash[0]), 18079561Siedowse M_DIRHASH, M_NOWAIT | M_ZERO); 18179561Siedowse MALLOC(dh->dh_blkfree, u_int8_t *, nblocks * sizeof(dh->dh_blkfree[0]), 18279561Siedowse M_DIRHASH, M_NOWAIT); 18379561Siedowse if (dh->dh_hash == NULL || dh->dh_blkfree == NULL) 18479561Siedowse goto fail; 18579561Siedowse for (i = 0; i < narrays; i++) { 18692768Sjeff if ((dh->dh_hash[i] = uma_zalloc(ufsdirhash_zone, 18792768Sjeff M_WAITOK)) == NULL) 18879561Siedowse goto fail; 18979561Siedowse for (j = 0; j < DH_NBLKOFF; j++) 19079561Siedowse dh->dh_hash[i][j] = DIRHASH_EMPTY; 19179561Siedowse } 19279561Siedowse 19379561Siedowse /* Initialise the hash table and block statistics. */ 19479561Siedowse mtx_init(&dh->dh_mtx, "dirhash", MTX_DEF); 19579561Siedowse dh->dh_narrays = narrays; 19679561Siedowse dh->dh_hlen = nslots; 19779561Siedowse dh->dh_nblk = nblocks; 19879561Siedowse dh->dh_dirblks = dirblocks; 19979561Siedowse for (i = 0; i < dirblocks; i++) 20079561Siedowse dh->dh_blkfree[i] = DIRBLKSIZ / DIRALIGN; 20179561Siedowse for (i = 0; i < DH_NFSTATS; i++) 20279561Siedowse dh->dh_firstfree[i] = -1; 20379561Siedowse dh->dh_firstfree[DH_NFSTATS] = 0; 20479561Siedowse dh->dh_seqopt = 0; 20579561Siedowse dh->dh_seqoff = 0; 20679561Siedowse dh->dh_score = DH_SCOREINIT; 20779561Siedowse ip->i_dirhash = dh; 20879561Siedowse 20979561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 21079561Siedowse pos = 0; 21179561Siedowse while (pos < ip->i_size) { 21279561Siedowse /* If necessary, get the next directory block. */ 21379561Siedowse if ((pos & bmask) == 0) { 21479561Siedowse if (bp != NULL) 21579561Siedowse brelse(bp); 21679561Siedowse if (UFS_BLKATOFF(vp, (off_t)pos, NULL, &bp) != 0) 21779561Siedowse goto fail; 21879561Siedowse } 21979561Siedowse 22079561Siedowse /* Add this entry to the hash. */ 22179561Siedowse ep = (struct direct *)((char *)bp->b_data + (pos & bmask)); 22279561Siedowse if (ep->d_reclen == 0 || ep->d_reclen > 22379561Siedowse DIRBLKSIZ - (pos & (DIRBLKSIZ - 1))) { 22479561Siedowse /* Corrupted directory. */ 22579561Siedowse brelse(bp); 22679561Siedowse goto fail; 22779561Siedowse } 22879561Siedowse if (ep->d_ino != 0) { 22979561Siedowse /* Add the entry (simplified ufsdirhash_add). */ 23079561Siedowse slot = ufsdirhash_hash(dh, ep->d_name, ep->d_namlen); 23179561Siedowse while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 23279561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 23379561Siedowse dh->dh_hused++; 23479561Siedowse DH_ENTRY(dh, slot) = pos; 23579561Siedowse ufsdirhash_adjfree(dh, pos, -DIRSIZ(0, ep)); 23679561Siedowse } 23779561Siedowse pos += ep->d_reclen; 23879561Siedowse } 23979561Siedowse 24079561Siedowse if (bp != NULL) 24179561Siedowse brelse(bp); 24279561Siedowse mtx_lock(&ufsdirhash_mtx); 24379561Siedowse TAILQ_INSERT_TAIL(&ufsdirhash_list, dh, dh_list); 24479561Siedowse dh->dh_onlist = 1; 24579561Siedowse mtx_unlock(&ufsdirhash_mtx); 24679561Siedowse return (0); 24779561Siedowse 24879561Siedowsefail: 24979561Siedowse if (dh->dh_hash != NULL) { 25079561Siedowse for (i = 0; i < narrays; i++) 25179561Siedowse if (dh->dh_hash[i] != NULL) 25292768Sjeff uma_zfree(ufsdirhash_zone, dh->dh_hash[i]); 25379561Siedowse FREE(dh->dh_hash, M_DIRHASH); 25479561Siedowse } 25579561Siedowse if (dh->dh_blkfree != NULL) 25679561Siedowse FREE(dh->dh_blkfree, M_DIRHASH); 25779561Siedowse FREE(dh, M_DIRHASH); 25879561Siedowse ip->i_dirhash = NULL; 25979561Siedowse mtx_lock(&ufsdirhash_mtx); 26079561Siedowse ufs_dirhashmem -= memreqd; 26179561Siedowse mtx_unlock(&ufsdirhash_mtx); 26279561Siedowse return (-1); 26379561Siedowse} 26479561Siedowse 26579561Siedowse/* 26679561Siedowse * Free any hash table associated with inode 'ip'. 26779561Siedowse */ 26879561Siedowsevoid 26979561Siedowseufsdirhash_free(struct inode *ip) 27079561Siedowse{ 27179561Siedowse struct dirhash *dh; 27279561Siedowse int i, mem; 27379561Siedowse 27479561Siedowse if ((dh = ip->i_dirhash) == NULL) 27579561Siedowse return; 27679561Siedowse mtx_lock(&ufsdirhash_mtx); 27779561Siedowse mtx_lock(&dh->dh_mtx); 27879561Siedowse if (dh->dh_onlist) 27979561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 28079561Siedowse mtx_unlock(&dh->dh_mtx); 28179561Siedowse mtx_unlock(&ufsdirhash_mtx); 28279561Siedowse 28379561Siedowse /* The dirhash pointed to by 'dh' is exclusively ours now. */ 28479561Siedowse 28579561Siedowse mem = sizeof(*dh); 28679561Siedowse if (dh->dh_hash != NULL) { 28779561Siedowse for (i = 0; i < dh->dh_narrays; i++) 28892768Sjeff uma_zfree(ufsdirhash_zone, dh->dh_hash[i]); 28979561Siedowse FREE(dh->dh_hash, M_DIRHASH); 29079561Siedowse FREE(dh->dh_blkfree, M_DIRHASH); 29179561Siedowse mem += dh->dh_narrays * sizeof(*dh->dh_hash) + 29279561Siedowse dh->dh_narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 29379561Siedowse dh->dh_nblk * sizeof(*dh->dh_blkfree); 29479561Siedowse } 29579561Siedowse mtx_destroy(&dh->dh_mtx); 29679561Siedowse FREE(dh, M_DIRHASH); 29779561Siedowse ip->i_dirhash = NULL; 29879561Siedowse 29979561Siedowse mtx_lock(&ufsdirhash_mtx); 30079561Siedowse ufs_dirhashmem -= mem; 30179561Siedowse mtx_unlock(&ufsdirhash_mtx); 30279561Siedowse} 30379561Siedowse 30479561Siedowse/* 30579561Siedowse * Find the offset of the specified name within the given inode. 30679561Siedowse * Returns 0 on success, ENOENT if the entry does not exist, or 30779561Siedowse * EJUSTRETURN if the caller should revert to a linear search. 30879561Siedowse * 30979690Siedowse * If successful, the directory offset is stored in *offp, and a 31079690Siedowse * pointer to a struct buf containing the entry is stored in *bpp. If 31179561Siedowse * prevoffp is non-NULL, the offset of the previous entry within 31279561Siedowse * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry 31379561Siedowse * is the first in a block, the start of the block is used). 31479561Siedowse */ 31579561Siedowseint 31679561Siedowseufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp, 31779690Siedowse struct buf **bpp, doff_t *prevoffp) 31879561Siedowse{ 31979561Siedowse struct dirhash *dh, *dh_next; 32079561Siedowse struct direct *dp; 32179561Siedowse struct vnode *vp; 32279561Siedowse struct buf *bp; 32379561Siedowse doff_t blkoff, bmask, offset, prevoff; 32479561Siedowse int i, slot; 32579561Siedowse 32679561Siedowse if ((dh = ip->i_dirhash) == NULL) 32779561Siedowse return (EJUSTRETURN); 32879561Siedowse /* 32979561Siedowse * Move this dirhash towards the end of the list if it has a 33079561Siedowse * score higher than the next entry, and aquire the dh_mtx. 33179561Siedowse * Optimise the case where it's already the last by performing 33279561Siedowse * an unlocked read of the TAILQ_NEXT pointer. 33379561Siedowse * 33479561Siedowse * In both cases, end up holding just dh_mtx. 33579561Siedowse */ 33679561Siedowse if (TAILQ_NEXT(dh, dh_list) != NULL) { 33779561Siedowse mtx_lock(&ufsdirhash_mtx); 33879561Siedowse mtx_lock(&dh->dh_mtx); 33979561Siedowse /* 34079561Siedowse * If the new score will be greater than that of the next 34179561Siedowse * entry, then move this entry past it. With both mutexes 34279561Siedowse * held, dh_next won't go away, but its dh_score could 34379561Siedowse * change; that's not important since it is just a hint. 34479561Siedowse */ 34579561Siedowse if (dh->dh_hash != NULL && 34679561Siedowse (dh_next = TAILQ_NEXT(dh, dh_list)) != NULL && 34779561Siedowse dh->dh_score >= dh_next->dh_score) { 34879561Siedowse KASSERT(dh->dh_onlist, ("dirhash: not on list")); 34979561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 35079561Siedowse TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh, 35179561Siedowse dh_list); 35279561Siedowse } 35379561Siedowse mtx_unlock(&ufsdirhash_mtx); 35479561Siedowse } else { 35579561Siedowse /* Already the last, though that could change as we wait. */ 35679561Siedowse mtx_lock(&dh->dh_mtx); 35779561Siedowse } 35879561Siedowse if (dh->dh_hash == NULL) { 35979561Siedowse mtx_unlock(&dh->dh_mtx); 36079561Siedowse ufsdirhash_free(ip); 36179561Siedowse return (EJUSTRETURN); 36279561Siedowse } 36379561Siedowse 36479561Siedowse /* Update the score. */ 36579561Siedowse if (dh->dh_score < DH_SCOREMAX) 36679561Siedowse dh->dh_score++; 36779561Siedowse 36879561Siedowse vp = ip->i_vnode; 36979561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 37079561Siedowse blkoff = -1; 37179561Siedowse bp = NULL; 37279561Siedowserestart: 37379561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 37479561Siedowse 37579561Siedowse if (dh->dh_seqopt) { 37679561Siedowse /* 37779561Siedowse * Sequential access optimisation. dh_seqoff contains the 37879561Siedowse * offset of the directory entry immediately following 37979561Siedowse * the last entry that was looked up. Check if this offset 38079561Siedowse * appears in the hash chain for the name we are looking for. 38179561Siedowse */ 38279561Siedowse for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY; 38379561Siedowse i = WRAPINCR(i, dh->dh_hlen)) 38486350Siedowse if (offset == dh->dh_seqoff) 38579561Siedowse break; 38679561Siedowse if (offset == dh->dh_seqoff) { 38779561Siedowse /* 38879561Siedowse * We found an entry with the expected offset. This 38979561Siedowse * is probably the entry we want, but if not, the 39079561Siedowse * code below will turn off seqoff and retry. 39179561Siedowse */ 39279561Siedowse slot = i; 39379561Siedowse } else 39479561Siedowse dh->dh_seqopt = 0; 39579561Siedowse } 39679561Siedowse 39779561Siedowse for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY; 39879561Siedowse slot = WRAPINCR(slot, dh->dh_hlen)) { 39979561Siedowse if (offset == DIRHASH_DEL) 40079561Siedowse continue; 40179561Siedowse mtx_unlock(&dh->dh_mtx); 40279561Siedowse 40379561Siedowse if (offset < 0 || offset >= ip->i_size) 40479561Siedowse panic("ufsdirhash_lookup: bad offset in hash array"); 40579561Siedowse if ((offset & ~bmask) != blkoff) { 40679561Siedowse if (bp != NULL) 40779561Siedowse brelse(bp); 40879561Siedowse blkoff = offset & ~bmask; 40979561Siedowse if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) 41079561Siedowse return (EJUSTRETURN); 41179561Siedowse } 41279561Siedowse dp = (struct direct *)(bp->b_data + (offset & bmask)); 41379561Siedowse if (dp->d_reclen == 0 || dp->d_reclen > 41479561Siedowse DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) { 41579561Siedowse /* Corrupted directory. */ 41679561Siedowse brelse(bp); 41779561Siedowse return (EJUSTRETURN); 41879561Siedowse } 41979561Siedowse if (dp->d_namlen == namelen && 42079561Siedowse bcmp(dp->d_name, name, namelen) == 0) { 42179561Siedowse /* Found. Get the prev offset if needed. */ 42279561Siedowse if (prevoffp != NULL) { 42379561Siedowse if (offset & (DIRBLKSIZ - 1)) { 42479561Siedowse prevoff = ufsdirhash_getprev(dp, 42579561Siedowse offset); 42679561Siedowse if (prevoff == -1) { 42779561Siedowse brelse(bp); 42879561Siedowse return (EJUSTRETURN); 42979561Siedowse } 43079561Siedowse } else 43179561Siedowse prevoff = offset; 43279561Siedowse *prevoffp = prevoff; 43379561Siedowse } 43479561Siedowse 43579561Siedowse /* Check for sequential access, and update offset. */ 43679561Siedowse if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset) 43779561Siedowse dh->dh_seqopt = 1; 43879561Siedowse dh->dh_seqoff = offset + DIRSIZ(0, dp); 43979561Siedowse 44079690Siedowse *bpp = bp; 44179561Siedowse *offp = offset; 44279561Siedowse return (0); 44379561Siedowse } 44479561Siedowse 44579561Siedowse mtx_lock(&dh->dh_mtx); 44679561Siedowse if (dh->dh_hash == NULL) { 44779561Siedowse mtx_unlock(&dh->dh_mtx); 44879561Siedowse if (bp != NULL) 44979561Siedowse brelse(bp); 45079561Siedowse ufsdirhash_free(ip); 45179561Siedowse return (EJUSTRETURN); 45279561Siedowse } 45379561Siedowse /* 45479561Siedowse * When the name doesn't match in the seqopt case, go back 45579561Siedowse * and search normally. 45679561Siedowse */ 45779561Siedowse if (dh->dh_seqopt) { 45879561Siedowse dh->dh_seqopt = 0; 45979561Siedowse goto restart; 46079561Siedowse } 46179561Siedowse } 46279561Siedowse mtx_unlock(&dh->dh_mtx); 46379561Siedowse if (bp != NULL) 46479561Siedowse brelse(bp); 46579561Siedowse return (ENOENT); 46679561Siedowse} 46779561Siedowse 46879561Siedowse/* 46979561Siedowse * Find a directory block with room for 'slotneeded' bytes. Returns 47079561Siedowse * the offset of the directory entry that begins the free space. 47179561Siedowse * This will either be the offset of an existing entry that has free 47279561Siedowse * space at the end, or the offset of an entry with d_ino == 0 at 47379561Siedowse * the start of a DIRBLKSIZ block. 47479561Siedowse * 47579561Siedowse * To use the space, the caller may need to compact existing entries in 47679561Siedowse * the directory. The total number of bytes in all of the entries involved 47779561Siedowse * in the compaction is stored in *slotsize. In other words, all of 47879561Siedowse * the entries that must be compacted are exactly contained in the 47979561Siedowse * region beginning at the returned offset and spanning *slotsize bytes. 48079561Siedowse * 48179561Siedowse * Returns -1 if no space was found, indicating that the directory 48279561Siedowse * must be extended. 48379561Siedowse */ 48479561Siedowsedoff_t 48579561Siedowseufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize) 48679561Siedowse{ 48779561Siedowse struct direct *dp; 48879561Siedowse struct dirhash *dh; 48979561Siedowse struct buf *bp; 49079561Siedowse doff_t pos, slotstart; 49179561Siedowse int dirblock, error, freebytes, i; 49279561Siedowse 49379561Siedowse if ((dh = ip->i_dirhash) == NULL) 49479561Siedowse return (-1); 49579561Siedowse mtx_lock(&dh->dh_mtx); 49679561Siedowse if (dh->dh_hash == NULL) { 49779561Siedowse mtx_unlock(&dh->dh_mtx); 49879561Siedowse ufsdirhash_free(ip); 49979561Siedowse return (-1); 50079561Siedowse } 50179561Siedowse 50279561Siedowse /* Find a directory block with the desired free space. */ 50379561Siedowse dirblock = -1; 50479561Siedowse for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++) 50579561Siedowse if ((dirblock = dh->dh_firstfree[i]) != -1) 50679561Siedowse break; 50779561Siedowse if (dirblock == -1) { 50879561Siedowse mtx_unlock(&dh->dh_mtx); 50979561Siedowse return (-1); 51079561Siedowse } 51179561Siedowse 51279561Siedowse KASSERT(dirblock < dh->dh_nblk && 51379561Siedowse dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN), 51479561Siedowse ("ufsdirhash_findfree: bad stats")); 51579561Siedowse mtx_unlock(&dh->dh_mtx); 51679561Siedowse pos = dirblock * DIRBLKSIZ; 51779561Siedowse error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp); 51879561Siedowse if (error) 51979561Siedowse return (-1); 52079561Siedowse 52179561Siedowse /* Find the first entry with free space. */ 52279561Siedowse for (i = 0; i < DIRBLKSIZ; ) { 52379561Siedowse if (dp->d_reclen == 0) { 52479561Siedowse brelse(bp); 52579561Siedowse return (-1); 52679561Siedowse } 52779561Siedowse if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp)) 52879561Siedowse break; 52979561Siedowse i += dp->d_reclen; 53079561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 53179561Siedowse } 53279561Siedowse if (i > DIRBLKSIZ) { 53379561Siedowse brelse(bp); 53479561Siedowse return (-1); 53579561Siedowse } 53679561Siedowse slotstart = pos + i; 53779561Siedowse 53879561Siedowse /* Find the range of entries needed to get enough space */ 53979561Siedowse freebytes = 0; 54079561Siedowse while (i < DIRBLKSIZ && freebytes < slotneeded) { 54179561Siedowse freebytes += dp->d_reclen; 54279561Siedowse if (dp->d_ino != 0) 54379561Siedowse freebytes -= DIRSIZ(0, dp); 54479561Siedowse if (dp->d_reclen == 0) { 54579561Siedowse brelse(bp); 54679561Siedowse return (-1); 54779561Siedowse } 54879561Siedowse i += dp->d_reclen; 54979561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 55079561Siedowse } 55179561Siedowse if (i > DIRBLKSIZ) { 55279561Siedowse brelse(bp); 55379561Siedowse return (-1); 55479561Siedowse } 55579561Siedowse if (freebytes < slotneeded) 55679561Siedowse panic("ufsdirhash_findfree: free mismatch"); 55779561Siedowse brelse(bp); 55879561Siedowse *slotsize = pos + i - slotstart; 55979561Siedowse return (slotstart); 56079561Siedowse} 56179561Siedowse 56279561Siedowse/* 56379561Siedowse * Return the start of the unused space at the end of a directory, or 56479561Siedowse * -1 if there are no trailing unused blocks. 56579561Siedowse */ 56679561Siedowsedoff_t 56779561Siedowseufsdirhash_enduseful(struct inode *ip) 56879561Siedowse{ 56979561Siedowse 57079561Siedowse struct dirhash *dh; 57179561Siedowse int i; 57279561Siedowse 57379561Siedowse if ((dh = ip->i_dirhash) == NULL) 57479561Siedowse return (-1); 57579561Siedowse mtx_lock(&dh->dh_mtx); 57679561Siedowse if (dh->dh_hash == NULL) { 57779561Siedowse mtx_unlock(&dh->dh_mtx); 57879561Siedowse ufsdirhash_free(ip); 57979561Siedowse return (-1); 58079561Siedowse } 58179561Siedowse 58279561Siedowse if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN) { 58379561Siedowse mtx_unlock(&dh->dh_mtx); 58479561Siedowse return (-1); 58579561Siedowse } 58679561Siedowse 58779561Siedowse for (i = dh->dh_dirblks - 1; i >= 0; i--) 58879561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 58979561Siedowse break; 59079561Siedowse mtx_unlock(&dh->dh_mtx); 59179561Siedowse return ((doff_t)(i + 1) * DIRBLKSIZ); 59279561Siedowse} 59379561Siedowse 59479561Siedowse/* 59579561Siedowse * Insert information into the hash about a new directory entry. dirp 59679561Siedowse * points to a struct direct containing the entry, and offset specifies 59779561Siedowse * the offset of this entry. 59879561Siedowse */ 59979561Siedowsevoid 60079561Siedowseufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset) 60179561Siedowse{ 60279561Siedowse struct dirhash *dh; 60379561Siedowse int slot; 60479561Siedowse 60579561Siedowse if ((dh = ip->i_dirhash) == NULL) 60679561Siedowse return; 60779561Siedowse mtx_lock(&dh->dh_mtx); 60879561Siedowse if (dh->dh_hash == NULL) { 60979561Siedowse mtx_unlock(&dh->dh_mtx); 61079561Siedowse ufsdirhash_free(ip); 61179561Siedowse return; 61279561Siedowse } 61379561Siedowse 61479561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 61579561Siedowse ("ufsdirhash_add: bad offset")); 61679561Siedowse /* 61779561Siedowse * Normal hash usage is < 66%. If the usage gets too high then 61879561Siedowse * remove the hash entirely and let it be rebuilt later. 61979561Siedowse */ 62079561Siedowse if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) { 62179561Siedowse mtx_unlock(&dh->dh_mtx); 62279561Siedowse ufsdirhash_free(ip); 62379561Siedowse return; 62479561Siedowse } 62579561Siedowse 62679561Siedowse /* Find a free hash slot (empty or deleted), and add the entry. */ 62779561Siedowse slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen); 62879561Siedowse while (DH_ENTRY(dh, slot) >= 0) 62979561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 63079561Siedowse if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY) 63179561Siedowse dh->dh_hused++; 63279561Siedowse DH_ENTRY(dh, slot) = offset; 63379561Siedowse 63479561Siedowse /* Update the per-block summary info. */ 63579561Siedowse ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp)); 63679561Siedowse mtx_unlock(&dh->dh_mtx); 63779561Siedowse} 63879561Siedowse 63979561Siedowse/* 64079561Siedowse * Remove the specified directory entry from the hash. The entry to remove 64179561Siedowse * is defined by the name in `dirp', which must exist at the specified 64279561Siedowse * `offset' within the directory. 64379561Siedowse */ 64479561Siedowsevoid 64579561Siedowseufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset) 64679561Siedowse{ 64779561Siedowse struct dirhash *dh; 64879561Siedowse int slot; 64979561Siedowse 65079561Siedowse if ((dh = ip->i_dirhash) == NULL) 65179561Siedowse return; 65279561Siedowse mtx_lock(&dh->dh_mtx); 65379561Siedowse if (dh->dh_hash == NULL) { 65479561Siedowse mtx_unlock(&dh->dh_mtx); 65579561Siedowse ufsdirhash_free(ip); 65679561Siedowse return; 65779561Siedowse } 65879561Siedowse 65979561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 66079561Siedowse ("ufsdirhash_remove: bad offset")); 66179561Siedowse /* Find the entry */ 66279561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset); 66379561Siedowse 66479561Siedowse /* Remove the hash entry. */ 66579561Siedowse ufsdirhash_delslot(dh, slot); 66679561Siedowse 66779561Siedowse /* Update the per-block summary info. */ 66879561Siedowse ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp)); 66979561Siedowse mtx_unlock(&dh->dh_mtx); 67079561Siedowse} 67179561Siedowse 67279561Siedowse/* 67379561Siedowse * Change the offset associated with a directory entry in the hash. Used 67479561Siedowse * when compacting directory blocks. 67579561Siedowse */ 67679561Siedowsevoid 67779561Siedowseufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff, 67879561Siedowse doff_t newoff) 67979561Siedowse{ 68079561Siedowse struct dirhash *dh; 68179561Siedowse int slot; 68279561Siedowse 68379561Siedowse if ((dh = ip->i_dirhash) == NULL) 68479561Siedowse return; 68579561Siedowse mtx_lock(&dh->dh_mtx); 68679561Siedowse if (dh->dh_hash == NULL) { 68779561Siedowse mtx_unlock(&dh->dh_mtx); 68879561Siedowse ufsdirhash_free(ip); 68979561Siedowse return; 69079561Siedowse } 69179561Siedowse 69279561Siedowse KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ && 69379561Siedowse newoff < dh->dh_dirblks * DIRBLKSIZ, 69479561Siedowse ("ufsdirhash_move: bad offset")); 69579561Siedowse /* Find the entry, and update the offset. */ 69679561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff); 69779561Siedowse DH_ENTRY(dh, slot) = newoff; 69879561Siedowse mtx_unlock(&dh->dh_mtx); 69979561Siedowse} 70079561Siedowse 70179561Siedowse/* 70279561Siedowse * Inform dirhash that the directory has grown by one block that 70379561Siedowse * begins at offset (i.e. the new length is offset + DIRBLKSIZ). 70479561Siedowse */ 70579561Siedowsevoid 70679561Siedowseufsdirhash_newblk(struct inode *ip, doff_t offset) 70779561Siedowse{ 70879561Siedowse struct dirhash *dh; 70979561Siedowse int block; 71079561Siedowse 71179561Siedowse if ((dh = ip->i_dirhash) == NULL) 71279561Siedowse return; 71379561Siedowse mtx_lock(&dh->dh_mtx); 71479561Siedowse if (dh->dh_hash == NULL) { 71579561Siedowse mtx_unlock(&dh->dh_mtx); 71679561Siedowse ufsdirhash_free(ip); 71779561Siedowse return; 71879561Siedowse } 71979561Siedowse 72079561Siedowse KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ, 72179561Siedowse ("ufsdirhash_newblk: bad offset")); 72279561Siedowse block = offset / DIRBLKSIZ; 72379561Siedowse if (block >= dh->dh_nblk) { 72479561Siedowse /* Out of space; must rebuild. */ 72579561Siedowse mtx_unlock(&dh->dh_mtx); 72679561Siedowse ufsdirhash_free(ip); 72779561Siedowse return; 72879561Siedowse } 72979561Siedowse dh->dh_dirblks = block + 1; 73079561Siedowse 73179561Siedowse /* Account for the new free block. */ 73279561Siedowse dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN; 73379561Siedowse if (dh->dh_firstfree[DH_NFSTATS] == -1) 73479561Siedowse dh->dh_firstfree[DH_NFSTATS] = block; 73579561Siedowse mtx_unlock(&dh->dh_mtx); 73679561Siedowse} 73779561Siedowse 73879561Siedowse/* 73979561Siedowse * Inform dirhash that the directory is being truncated. 74079561Siedowse */ 74179561Siedowsevoid 74279561Siedowseufsdirhash_dirtrunc(struct inode *ip, doff_t offset) 74379561Siedowse{ 74479561Siedowse struct dirhash *dh; 74579561Siedowse int block, i; 74679561Siedowse 74779561Siedowse if ((dh = ip->i_dirhash) == NULL) 74879561Siedowse return; 74979561Siedowse mtx_lock(&dh->dh_mtx); 75079561Siedowse if (dh->dh_hash == NULL) { 75179561Siedowse mtx_unlock(&dh->dh_mtx); 75279561Siedowse ufsdirhash_free(ip); 75379561Siedowse return; 75479561Siedowse } 75579561Siedowse 75679561Siedowse KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ, 75779561Siedowse ("ufsdirhash_dirtrunc: bad offset")); 75879561Siedowse block = howmany(offset, DIRBLKSIZ); 75979561Siedowse /* 76079561Siedowse * If the directory shrinks to less than 1/8 of dh_nblk blocks 76179561Siedowse * (about 20% of its original size due to the 50% extra added in 76279561Siedowse * ufsdirhash_build) then free it, and let the caller rebuild 76379561Siedowse * if necessary. 76479561Siedowse */ 76579561Siedowse if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) { 76679561Siedowse mtx_unlock(&dh->dh_mtx); 76779561Siedowse ufsdirhash_free(ip); 76879561Siedowse return; 76979561Siedowse } 77079561Siedowse 77179561Siedowse /* 77279561Siedowse * Remove any `first free' information pertaining to the 77379561Siedowse * truncated blocks. All blocks we're removing should be 77479561Siedowse * completely unused. 77579561Siedowse */ 77679561Siedowse if (dh->dh_firstfree[DH_NFSTATS] >= block) 77779561Siedowse dh->dh_firstfree[DH_NFSTATS] = -1; 77879561Siedowse for (i = block; i < dh->dh_dirblks; i++) 77979561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 78079561Siedowse panic("ufsdirhash_dirtrunc: blocks in use"); 78179561Siedowse for (i = 0; i < DH_NFSTATS; i++) 78279561Siedowse if (dh->dh_firstfree[i] >= block) 78379561Siedowse panic("ufsdirhash_dirtrunc: first free corrupt"); 78479561Siedowse dh->dh_dirblks = block; 78579561Siedowse mtx_unlock(&dh->dh_mtx); 78679561Siedowse} 78779561Siedowse 78879561Siedowse/* 78979561Siedowse * Debugging function to check that the dirhash information about 79079561Siedowse * a directory block matches its actual contents. Panics if a mismatch 79179561Siedowse * is detected. 79279561Siedowse * 79379561Siedowse * On entry, `buf' should point to the start of an in-core 79479561Siedowse * DIRBLKSIZ-sized directory block, and `offset' should contain the 79579561Siedowse * offset from the start of the directory of that block. 79679561Siedowse */ 79779561Siedowsevoid 79879561Siedowseufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset) 79979561Siedowse{ 80079561Siedowse struct dirhash *dh; 80179561Siedowse struct direct *dp; 80279561Siedowse int block, ffslot, i, nfree; 80379561Siedowse 80479561Siedowse if (!ufs_dirhashcheck) 80579561Siedowse return; 80679561Siedowse if ((dh = ip->i_dirhash) == NULL) 80779561Siedowse return; 80879561Siedowse mtx_lock(&dh->dh_mtx); 80979561Siedowse if (dh->dh_hash == NULL) { 81079561Siedowse mtx_unlock(&dh->dh_mtx); 81179561Siedowse ufsdirhash_free(ip); 81279561Siedowse return; 81379561Siedowse } 81479561Siedowse 81579561Siedowse block = offset / DIRBLKSIZ; 81679561Siedowse if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks) 81779561Siedowse panic("ufsdirhash_checkblock: bad offset"); 81879561Siedowse 81979561Siedowse nfree = 0; 82079561Siedowse for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) { 82179561Siedowse dp = (struct direct *)(buf + i); 82279561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ) 82379561Siedowse panic("ufsdirhash_checkblock: bad dir"); 82479561Siedowse 82579561Siedowse if (dp->d_ino == 0) { 82680456Siedowse#if 0 82780456Siedowse /* 82880456Siedowse * XXX entries with d_ino == 0 should only occur 82980456Siedowse * at the start of a DIRBLKSIZ block. However the 83080456Siedowse * ufs code is tolerant of such entries at other 83180456Siedowse * offsets, and fsck does not fix them. 83280456Siedowse */ 83379561Siedowse if (i != 0) 83479561Siedowse panic("ufsdirhash_checkblock: bad dir inode"); 83580456Siedowse#endif 83679561Siedowse nfree += dp->d_reclen; 83779561Siedowse continue; 83879561Siedowse } 83979561Siedowse 84079561Siedowse /* Check that the entry exists (will panic if it doesn't). */ 84179561Siedowse ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i); 84279561Siedowse 84379561Siedowse nfree += dp->d_reclen - DIRSIZ(0, dp); 84479561Siedowse } 84579561Siedowse if (i != DIRBLKSIZ) 84679561Siedowse panic("ufsdirhash_checkblock: bad dir end"); 84779561Siedowse 84879561Siedowse if (dh->dh_blkfree[block] * DIRALIGN != nfree) 84979561Siedowse panic("ufsdirhash_checkblock: bad free count"); 85079561Siedowse 85192098Siedowse ffslot = BLKFREE2IDX(nfree / DIRALIGN); 85279561Siedowse for (i = 0; i <= DH_NFSTATS; i++) 85379561Siedowse if (dh->dh_firstfree[i] == block && i != ffslot) 85479561Siedowse panic("ufsdirhash_checkblock: bad first-free"); 85592098Siedowse if (dh->dh_firstfree[ffslot] == -1) 85692098Siedowse panic("ufsdirhash_checkblock: missing first-free entry"); 85779561Siedowse mtx_unlock(&dh->dh_mtx); 85879561Siedowse} 85979561Siedowse 86079561Siedowse/* 86179561Siedowse * Hash the specified filename into a dirhash slot. 86279561Siedowse */ 86379561Siedowsestatic int 86479561Siedowseufsdirhash_hash(struct dirhash *dh, char *name, int namelen) 86579561Siedowse{ 86692807Sdwmalone u_int32_t hash; 86792807Sdwmalone 86892807Sdwmalone /* 86992807Sdwmalone * We hash the name and then some ofther bit of data which is 87092807Sdwmalone * invarient over the dirhash's lifetime. Otherwise names 87192807Sdwmalone * differing only in the last byte are placed close to one 87292807Sdwmalone * another in the table, which is bad for linear probing. 87392807Sdwmalone */ 87492807Sdwmalone hash = fnv_32_buf(name, namelen, FNV1_32_INIT); 87592807Sdwmalone hash = fnv_32_buf(dh, sizeof(dh), hash); 87692807Sdwmalone return (hash % dh->dh_hlen); 87779561Siedowse} 87879561Siedowse 87979561Siedowse/* 88079561Siedowse * Adjust the number of free bytes in the block containing `offset' 88179561Siedowse * by the value specified by `diff'. 88279561Siedowse * 88379561Siedowse * The caller must ensure we have exclusive access to `dh'; normally 88479561Siedowse * that means that dh_mtx should be held, but this is also called 88579561Siedowse * from ufsdirhash_build() where exclusive access can be assumed. 88679561Siedowse */ 88779561Siedowsestatic void 88879561Siedowseufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff) 88979561Siedowse{ 89079561Siedowse int block, i, nfidx, ofidx; 89179561Siedowse 89279561Siedowse /* Update the per-block summary info. */ 89379561Siedowse block = offset / DIRBLKSIZ; 89479561Siedowse KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks, 89579561Siedowse ("dirhash bad offset")); 89692098Siedowse ofidx = BLKFREE2IDX(dh->dh_blkfree[block]); 89779561Siedowse dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN); 89892098Siedowse nfidx = BLKFREE2IDX(dh->dh_blkfree[block]); 89979561Siedowse 90079561Siedowse /* Update the `first free' list if necessary. */ 90179561Siedowse if (ofidx != nfidx) { 90279561Siedowse /* If removing, scan forward for the next block. */ 90379561Siedowse if (dh->dh_firstfree[ofidx] == block) { 90479561Siedowse for (i = block + 1; i < dh->dh_dirblks; i++) 90592098Siedowse if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx) 90679561Siedowse break; 90779561Siedowse dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1; 90879561Siedowse } 90979561Siedowse 91079561Siedowse /* Make this the new `first free' if necessary */ 91179561Siedowse if (dh->dh_firstfree[nfidx] > block || 91279561Siedowse dh->dh_firstfree[nfidx] == -1) 91379561Siedowse dh->dh_firstfree[nfidx] = block; 91479561Siedowse } 91579561Siedowse} 91679561Siedowse 91779561Siedowse/* 91879561Siedowse * Find the specified name which should have the specified offset. 91979561Siedowse * Returns a slot number, and panics on failure. 92079561Siedowse * 92179561Siedowse * `dh' must be locked on entry and remains so on return. 92279561Siedowse */ 92379561Siedowsestatic int 92479561Siedowseufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset) 92579561Siedowse{ 92679561Siedowse int slot; 92779561Siedowse 92879561Siedowse mtx_assert(&dh->dh_mtx, MA_OWNED); 92979561Siedowse 93079561Siedowse /* Find the entry. */ 93179561Siedowse KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full")); 93279561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 93379561Siedowse while (DH_ENTRY(dh, slot) != offset && 93479561Siedowse DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 93579561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 93679561Siedowse if (DH_ENTRY(dh, slot) != offset) 93779561Siedowse panic("ufsdirhash_findslot: '%.*s' not found", namelen, name); 93879561Siedowse 93979561Siedowse return (slot); 94079561Siedowse} 94179561Siedowse 94279561Siedowse/* 94379561Siedowse * Remove the entry corresponding to the specified slot from the hash array. 94479561Siedowse * 94579561Siedowse * `dh' must be locked on entry and remains so on return. 94679561Siedowse */ 94779561Siedowsestatic void 94879561Siedowseufsdirhash_delslot(struct dirhash *dh, int slot) 94979561Siedowse{ 95079561Siedowse int i; 95179561Siedowse 95279561Siedowse mtx_assert(&dh->dh_mtx, MA_OWNED); 95379561Siedowse 95479561Siedowse /* Mark the entry as deleted. */ 95579561Siedowse DH_ENTRY(dh, slot) = DIRHASH_DEL; 95679561Siedowse 95779561Siedowse /* If this is the end of a chain of DIRHASH_DEL slots, remove them. */ 95879561Siedowse for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; ) 95979561Siedowse i = WRAPINCR(i, dh->dh_hlen); 96079561Siedowse if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) { 96192807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 96292807Sdwmalone while (DH_ENTRY(dh, i) == DIRHASH_DEL) { 96379561Siedowse DH_ENTRY(dh, i) = DIRHASH_EMPTY; 96479561Siedowse dh->dh_hused--; 96592807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 96679561Siedowse } 96779561Siedowse KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen")); 96879561Siedowse } 96979561Siedowse} 97079561Siedowse 97179561Siedowse/* 97279561Siedowse * Given a directory entry and its offset, find the offset of the 97379561Siedowse * previous entry in the same DIRBLKSIZ-sized block. Returns an 97479561Siedowse * offset, or -1 if there is no previous entry in the block or some 97579561Siedowse * other problem occurred. 97679561Siedowse */ 97779561Siedowsestatic doff_t 97879561Siedowseufsdirhash_getprev(struct direct *dirp, doff_t offset) 97979561Siedowse{ 98079561Siedowse struct direct *dp; 98179561Siedowse char *blkbuf; 98279561Siedowse doff_t blkoff, prevoff; 98379561Siedowse int entrypos, i; 98479561Siedowse 98579561Siedowse blkoff = offset & ~(DIRBLKSIZ - 1); /* offset of start of block */ 98679561Siedowse entrypos = offset & (DIRBLKSIZ - 1); /* entry relative to block */ 98779561Siedowse blkbuf = (char *)dirp - entrypos; 98879561Siedowse prevoff = blkoff; 98979561Siedowse 99079561Siedowse /* If `offset' is the start of a block, there is no previous entry. */ 99179561Siedowse if (entrypos == 0) 99279561Siedowse return (-1); 99379561Siedowse 99479561Siedowse /* Scan from the start of the block until we get to the entry. */ 99579561Siedowse for (i = 0; i < entrypos; i += dp->d_reclen) { 99679561Siedowse dp = (struct direct *)(blkbuf + i); 99779561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos) 99879561Siedowse return (-1); /* Corrupted directory. */ 99979561Siedowse prevoff = blkoff + i; 100079561Siedowse } 100179561Siedowse return (prevoff); 100279561Siedowse} 100379561Siedowse 100479561Siedowse/* 100579561Siedowse * Try to free up `wanted' bytes by stealing memory from existing 100679561Siedowse * dirhashes. Returns zero with ufsdirhash_mtx locked if successful. 100779561Siedowse */ 100879561Siedowsestatic int 100979561Siedowseufsdirhash_recycle(int wanted) 101079561Siedowse{ 101179561Siedowse struct dirhash *dh; 101279561Siedowse doff_t **hash; 101379561Siedowse u_int8_t *blkfree; 101479561Siedowse int i, mem, narrays; 101579561Siedowse 101679561Siedowse mtx_lock(&ufsdirhash_mtx); 101779561Siedowse while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) { 101879561Siedowse /* Find a dirhash, and lock it. */ 101979561Siedowse if ((dh = TAILQ_FIRST(&ufsdirhash_list)) == NULL) { 102079561Siedowse mtx_unlock(&ufsdirhash_mtx); 102179561Siedowse return (-1); 102279561Siedowse } 102379561Siedowse mtx_lock(&dh->dh_mtx); 102479561Siedowse KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list")); 102579561Siedowse 102679561Siedowse /* Decrement the score; only recycle if it becomes zero. */ 102779561Siedowse if (--dh->dh_score > 0) { 102879561Siedowse mtx_unlock(&dh->dh_mtx); 102979561Siedowse mtx_unlock(&ufsdirhash_mtx); 103079561Siedowse return (-1); 103179561Siedowse } 103279561Siedowse 103379561Siedowse /* Remove it from the list and detach its memory. */ 103479561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 103579561Siedowse dh->dh_onlist = 0; 103679561Siedowse hash = dh->dh_hash; 103779561Siedowse dh->dh_hash = NULL; 103879561Siedowse blkfree = dh->dh_blkfree; 103979561Siedowse dh->dh_blkfree = NULL; 104079561Siedowse narrays = dh->dh_narrays; 104179561Siedowse mem = narrays * sizeof(*dh->dh_hash) + 104279561Siedowse narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 104379561Siedowse dh->dh_nblk * sizeof(*dh->dh_blkfree); 104479561Siedowse 104579561Siedowse /* Unlock everything, free the detached memory. */ 104679561Siedowse mtx_unlock(&dh->dh_mtx); 104779561Siedowse mtx_unlock(&ufsdirhash_mtx); 104879561Siedowse for (i = 0; i < narrays; i++) 104992768Sjeff uma_zfree(ufsdirhash_zone, hash[i]); 105079561Siedowse FREE(hash, M_DIRHASH); 105179561Siedowse FREE(blkfree, M_DIRHASH); 105279561Siedowse 105379561Siedowse /* Account for the returned memory, and repeat if necessary. */ 105479561Siedowse mtx_lock(&ufsdirhash_mtx); 105579561Siedowse ufs_dirhashmem -= mem; 105679561Siedowse } 105779561Siedowse /* Success; return with ufsdirhash_mtx locked. */ 105879561Siedowse return (0); 105979561Siedowse} 106079561Siedowse 106179561Siedowse 106279561Siedowsestatic void 106379561Siedowseufsdirhash_init() 106479561Siedowse{ 106592768Sjeff ufsdirhash_zone = uma_zcreate("DIRHASH", DH_NBLKOFF * sizeof(daddr_t), 106692768Sjeff NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 106779561Siedowse mtx_init(&ufsdirhash_mtx, "dirhash list", MTX_DEF); 106879561Siedowse TAILQ_INIT(&ufsdirhash_list); 106979561Siedowse} 107079561SiedowseSYSINIT(ufsdirhash, SI_SUB_PSEUDO, SI_ORDER_ANY, ufsdirhash_init, NULL) 107179561Siedowse 107279561Siedowse 107379561Siedowse#endif /* UFS_DIRHASH */ 1074