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