ufs_dirhash.c revision 79690
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 79690 2001-07-13 20:50:38Z 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 * 30179690Siedowse * If successful, the directory offset is stored in *offp, and a 30279690Siedowse * pointer to a struct buf containing the entry is stored in *bpp. If 30379561Siedowse * prevoffp is non-NULL, the offset of the previous entry within 30479561Siedowse * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry 30579561Siedowse * is the first in a block, the start of the block is used). 30679561Siedowse */ 30779561Siedowseint 30879561Siedowseufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp, 30979690Siedowse struct buf **bpp, doff_t *prevoffp) 31079561Siedowse{ 31179561Siedowse struct dirhash *dh, *dh_next; 31279561Siedowse struct direct *dp; 31379561Siedowse struct vnode *vp; 31479561Siedowse struct buf *bp; 31579561Siedowse doff_t blkoff, bmask, offset, prevoff; 31679561Siedowse int i, slot; 31779561Siedowse 31879561Siedowse if ((dh = ip->i_dirhash) == NULL) 31979561Siedowse return (EJUSTRETURN); 32079561Siedowse /* 32179561Siedowse * Move this dirhash towards the end of the list if it has a 32279561Siedowse * score higher than the next entry, and aquire the dh_mtx. 32379561Siedowse * Optimise the case where it's already the last by performing 32479561Siedowse * an unlocked read of the TAILQ_NEXT pointer. 32579561Siedowse * 32679561Siedowse * In both cases, end up holding just dh_mtx. 32779561Siedowse */ 32879561Siedowse if (TAILQ_NEXT(dh, dh_list) != NULL) { 32979561Siedowse mtx_lock(&ufsdirhash_mtx); 33079561Siedowse mtx_lock(&dh->dh_mtx); 33179561Siedowse /* 33279561Siedowse * If the new score will be greater than that of the next 33379561Siedowse * entry, then move this entry past it. With both mutexes 33479561Siedowse * held, dh_next won't go away, but its dh_score could 33579561Siedowse * change; that's not important since it is just a hint. 33679561Siedowse */ 33779561Siedowse if (dh->dh_hash != NULL && 33879561Siedowse (dh_next = TAILQ_NEXT(dh, dh_list)) != NULL && 33979561Siedowse dh->dh_score >= dh_next->dh_score) { 34079561Siedowse KASSERT(dh->dh_onlist, ("dirhash: not on list")); 34179561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 34279561Siedowse TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh, 34379561Siedowse dh_list); 34479561Siedowse } 34579561Siedowse mtx_unlock(&ufsdirhash_mtx); 34679561Siedowse } else { 34779561Siedowse /* Already the last, though that could change as we wait. */ 34879561Siedowse mtx_lock(&dh->dh_mtx); 34979561Siedowse } 35079561Siedowse if (dh->dh_hash == NULL) { 35179561Siedowse mtx_unlock(&dh->dh_mtx); 35279561Siedowse ufsdirhash_free(ip); 35379561Siedowse return (EJUSTRETURN); 35479561Siedowse } 35579561Siedowse 35679561Siedowse /* Update the score. */ 35779561Siedowse if (dh->dh_score < DH_SCOREMAX) 35879561Siedowse dh->dh_score++; 35979561Siedowse 36079561Siedowse vp = ip->i_vnode; 36179561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 36279561Siedowse blkoff = -1; 36379561Siedowse bp = NULL; 36479561Siedowserestart: 36579561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 36679561Siedowse 36779561Siedowse if (dh->dh_seqopt) { 36879561Siedowse /* 36979561Siedowse * Sequential access optimisation. dh_seqoff contains the 37079561Siedowse * offset of the directory entry immediately following 37179561Siedowse * the last entry that was looked up. Check if this offset 37279561Siedowse * appears in the hash chain for the name we are looking for. 37379561Siedowse */ 37479561Siedowse for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY; 37579561Siedowse i = WRAPINCR(i, dh->dh_hlen)) 37679561Siedowse if (offset == dh->dh_seqopt) 37779561Siedowse break; 37879561Siedowse if (offset == dh->dh_seqoff) { 37979561Siedowse /* 38079561Siedowse * We found an entry with the expected offset. This 38179561Siedowse * is probably the entry we want, but if not, the 38279561Siedowse * code below will turn off seqoff and retry. 38379561Siedowse */ 38479561Siedowse slot = i; 38579561Siedowse } else 38679561Siedowse dh->dh_seqopt = 0; 38779561Siedowse } 38879561Siedowse 38979561Siedowse for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY; 39079561Siedowse slot = WRAPINCR(slot, dh->dh_hlen)) { 39179561Siedowse if (offset == DIRHASH_DEL) 39279561Siedowse continue; 39379561Siedowse mtx_unlock(&dh->dh_mtx); 39479561Siedowse 39579561Siedowse if (offset < 0 || offset >= ip->i_size) 39679561Siedowse panic("ufsdirhash_lookup: bad offset in hash array"); 39779561Siedowse if ((offset & ~bmask) != blkoff) { 39879561Siedowse if (bp != NULL) 39979561Siedowse brelse(bp); 40079561Siedowse blkoff = offset & ~bmask; 40179561Siedowse if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) 40279561Siedowse return (EJUSTRETURN); 40379561Siedowse } 40479561Siedowse dp = (struct direct *)(bp->b_data + (offset & bmask)); 40579561Siedowse if (dp->d_reclen == 0 || dp->d_reclen > 40679561Siedowse DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) { 40779561Siedowse /* Corrupted directory. */ 40879561Siedowse brelse(bp); 40979561Siedowse return (EJUSTRETURN); 41079561Siedowse } 41179561Siedowse if (dp->d_namlen == namelen && 41279561Siedowse bcmp(dp->d_name, name, namelen) == 0) { 41379561Siedowse /* Found. Get the prev offset if needed. */ 41479561Siedowse if (prevoffp != NULL) { 41579561Siedowse if (offset & (DIRBLKSIZ - 1)) { 41679561Siedowse prevoff = ufsdirhash_getprev(dp, 41779561Siedowse offset); 41879561Siedowse if (prevoff == -1) { 41979561Siedowse brelse(bp); 42079561Siedowse return (EJUSTRETURN); 42179561Siedowse } 42279561Siedowse } else 42379561Siedowse prevoff = offset; 42479561Siedowse *prevoffp = prevoff; 42579561Siedowse } 42679561Siedowse 42779561Siedowse /* Check for sequential access, and update offset. */ 42879561Siedowse if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset) 42979561Siedowse dh->dh_seqopt = 1; 43079561Siedowse dh->dh_seqoff = offset + DIRSIZ(0, dp); 43179561Siedowse 43279690Siedowse *bpp = bp; 43379561Siedowse *offp = offset; 43479561Siedowse return (0); 43579561Siedowse } 43679561Siedowse 43779561Siedowse mtx_lock(&dh->dh_mtx); 43879561Siedowse if (dh->dh_hash == NULL) { 43979561Siedowse mtx_unlock(&dh->dh_mtx); 44079561Siedowse if (bp != NULL) 44179561Siedowse brelse(bp); 44279561Siedowse ufsdirhash_free(ip); 44379561Siedowse return (EJUSTRETURN); 44479561Siedowse } 44579561Siedowse /* 44679561Siedowse * When the name doesn't match in the seqopt case, go back 44779561Siedowse * and search normally. 44879561Siedowse */ 44979561Siedowse if (dh->dh_seqopt) { 45079561Siedowse dh->dh_seqopt = 0; 45179561Siedowse goto restart; 45279561Siedowse } 45379561Siedowse } 45479561Siedowse mtx_unlock(&dh->dh_mtx); 45579561Siedowse if (bp != NULL) 45679561Siedowse brelse(bp); 45779561Siedowse return (ENOENT); 45879561Siedowse} 45979561Siedowse 46079561Siedowse/* 46179561Siedowse * Find a directory block with room for 'slotneeded' bytes. Returns 46279561Siedowse * the offset of the directory entry that begins the free space. 46379561Siedowse * This will either be the offset of an existing entry that has free 46479561Siedowse * space at the end, or the offset of an entry with d_ino == 0 at 46579561Siedowse * the start of a DIRBLKSIZ block. 46679561Siedowse * 46779561Siedowse * To use the space, the caller may need to compact existing entries in 46879561Siedowse * the directory. The total number of bytes in all of the entries involved 46979561Siedowse * in the compaction is stored in *slotsize. In other words, all of 47079561Siedowse * the entries that must be compacted are exactly contained in the 47179561Siedowse * region beginning at the returned offset and spanning *slotsize bytes. 47279561Siedowse * 47379561Siedowse * Returns -1 if no space was found, indicating that the directory 47479561Siedowse * must be extended. 47579561Siedowse */ 47679561Siedowsedoff_t 47779561Siedowseufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize) 47879561Siedowse{ 47979561Siedowse struct direct *dp; 48079561Siedowse struct dirhash *dh; 48179561Siedowse struct buf *bp; 48279561Siedowse doff_t pos, slotstart; 48379561Siedowse int dirblock, error, freebytes, i; 48479561Siedowse 48579561Siedowse if ((dh = ip->i_dirhash) == NULL) 48679561Siedowse return (-1); 48779561Siedowse mtx_lock(&dh->dh_mtx); 48879561Siedowse if (dh->dh_hash == NULL) { 48979561Siedowse mtx_unlock(&dh->dh_mtx); 49079561Siedowse ufsdirhash_free(ip); 49179561Siedowse return (-1); 49279561Siedowse } 49379561Siedowse 49479561Siedowse /* Find a directory block with the desired free space. */ 49579561Siedowse dirblock = -1; 49679561Siedowse for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++) 49779561Siedowse if ((dirblock = dh->dh_firstfree[i]) != -1) 49879561Siedowse break; 49979561Siedowse if (dirblock == -1) { 50079561Siedowse mtx_unlock(&dh->dh_mtx); 50179561Siedowse return (-1); 50279561Siedowse } 50379561Siedowse 50479561Siedowse KASSERT(dirblock < dh->dh_nblk && 50579561Siedowse dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN), 50679561Siedowse ("ufsdirhash_findfree: bad stats")); 50779561Siedowse mtx_unlock(&dh->dh_mtx); 50879561Siedowse pos = dirblock * DIRBLKSIZ; 50979561Siedowse error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp); 51079561Siedowse if (error) 51179561Siedowse return (-1); 51279561Siedowse 51379561Siedowse /* Find the first entry with free space. */ 51479561Siedowse for (i = 0; i < DIRBLKSIZ; ) { 51579561Siedowse if (dp->d_reclen == 0) { 51679561Siedowse brelse(bp); 51779561Siedowse return (-1); 51879561Siedowse } 51979561Siedowse if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp)) 52079561Siedowse break; 52179561Siedowse i += dp->d_reclen; 52279561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 52379561Siedowse } 52479561Siedowse if (i > DIRBLKSIZ) { 52579561Siedowse brelse(bp); 52679561Siedowse return (-1); 52779561Siedowse } 52879561Siedowse slotstart = pos + i; 52979561Siedowse 53079561Siedowse /* Find the range of entries needed to get enough space */ 53179561Siedowse freebytes = 0; 53279561Siedowse while (i < DIRBLKSIZ && freebytes < slotneeded) { 53379561Siedowse freebytes += dp->d_reclen; 53479561Siedowse if (dp->d_ino != 0) 53579561Siedowse freebytes -= DIRSIZ(0, dp); 53679561Siedowse if (dp->d_reclen == 0) { 53779561Siedowse brelse(bp); 53879561Siedowse return (-1); 53979561Siedowse } 54079561Siedowse i += dp->d_reclen; 54179561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 54279561Siedowse } 54379561Siedowse if (i > DIRBLKSIZ) { 54479561Siedowse brelse(bp); 54579561Siedowse return (-1); 54679561Siedowse } 54779561Siedowse if (freebytes < slotneeded) 54879561Siedowse panic("ufsdirhash_findfree: free mismatch"); 54979561Siedowse brelse(bp); 55079561Siedowse *slotsize = pos + i - slotstart; 55179561Siedowse return (slotstart); 55279561Siedowse} 55379561Siedowse 55479561Siedowse/* 55579561Siedowse * Return the start of the unused space at the end of a directory, or 55679561Siedowse * -1 if there are no trailing unused blocks. 55779561Siedowse */ 55879561Siedowsedoff_t 55979561Siedowseufsdirhash_enduseful(struct inode *ip) 56079561Siedowse{ 56179561Siedowse 56279561Siedowse struct dirhash *dh; 56379561Siedowse int i; 56479561Siedowse 56579561Siedowse if ((dh = ip->i_dirhash) == NULL) 56679561Siedowse return (-1); 56779561Siedowse mtx_lock(&dh->dh_mtx); 56879561Siedowse if (dh->dh_hash == NULL) { 56979561Siedowse mtx_unlock(&dh->dh_mtx); 57079561Siedowse ufsdirhash_free(ip); 57179561Siedowse return (-1); 57279561Siedowse } 57379561Siedowse 57479561Siedowse if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN) { 57579561Siedowse mtx_unlock(&dh->dh_mtx); 57679561Siedowse return (-1); 57779561Siedowse } 57879561Siedowse 57979561Siedowse for (i = dh->dh_dirblks - 1; i >= 0; i--) 58079561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 58179561Siedowse break; 58279561Siedowse mtx_unlock(&dh->dh_mtx); 58379561Siedowse return ((doff_t)(i + 1) * DIRBLKSIZ); 58479561Siedowse} 58579561Siedowse 58679561Siedowse/* 58779561Siedowse * Insert information into the hash about a new directory entry. dirp 58879561Siedowse * points to a struct direct containing the entry, and offset specifies 58979561Siedowse * the offset of this entry. 59079561Siedowse */ 59179561Siedowsevoid 59279561Siedowseufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset) 59379561Siedowse{ 59479561Siedowse struct dirhash *dh; 59579561Siedowse int slot; 59679561Siedowse 59779561Siedowse if ((dh = ip->i_dirhash) == NULL) 59879561Siedowse return; 59979561Siedowse mtx_lock(&dh->dh_mtx); 60079561Siedowse if (dh->dh_hash == NULL) { 60179561Siedowse mtx_unlock(&dh->dh_mtx); 60279561Siedowse ufsdirhash_free(ip); 60379561Siedowse return; 60479561Siedowse } 60579561Siedowse 60679561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 60779561Siedowse ("ufsdirhash_add: bad offset")); 60879561Siedowse /* 60979561Siedowse * Normal hash usage is < 66%. If the usage gets too high then 61079561Siedowse * remove the hash entirely and let it be rebuilt later. 61179561Siedowse */ 61279561Siedowse if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) { 61379561Siedowse mtx_unlock(&dh->dh_mtx); 61479561Siedowse ufsdirhash_free(ip); 61579561Siedowse return; 61679561Siedowse } 61779561Siedowse 61879561Siedowse /* Find a free hash slot (empty or deleted), and add the entry. */ 61979561Siedowse slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen); 62079561Siedowse while (DH_ENTRY(dh, slot) >= 0) 62179561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 62279561Siedowse if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY) 62379561Siedowse dh->dh_hused++; 62479561Siedowse DH_ENTRY(dh, slot) = offset; 62579561Siedowse 62679561Siedowse /* Update the per-block summary info. */ 62779561Siedowse ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp)); 62879561Siedowse mtx_unlock(&dh->dh_mtx); 62979561Siedowse} 63079561Siedowse 63179561Siedowse/* 63279561Siedowse * Remove the specified directory entry from the hash. The entry to remove 63379561Siedowse * is defined by the name in `dirp', which must exist at the specified 63479561Siedowse * `offset' within the directory. 63579561Siedowse */ 63679561Siedowsevoid 63779561Siedowseufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset) 63879561Siedowse{ 63979561Siedowse struct dirhash *dh; 64079561Siedowse int slot; 64179561Siedowse 64279561Siedowse if ((dh = ip->i_dirhash) == NULL) 64379561Siedowse return; 64479561Siedowse mtx_lock(&dh->dh_mtx); 64579561Siedowse if (dh->dh_hash == NULL) { 64679561Siedowse mtx_unlock(&dh->dh_mtx); 64779561Siedowse ufsdirhash_free(ip); 64879561Siedowse return; 64979561Siedowse } 65079561Siedowse 65179561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 65279561Siedowse ("ufsdirhash_remove: bad offset")); 65379561Siedowse /* Find the entry */ 65479561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset); 65579561Siedowse 65679561Siedowse /* Remove the hash entry. */ 65779561Siedowse ufsdirhash_delslot(dh, slot); 65879561Siedowse 65979561Siedowse /* Update the per-block summary info. */ 66079561Siedowse ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp)); 66179561Siedowse mtx_unlock(&dh->dh_mtx); 66279561Siedowse} 66379561Siedowse 66479561Siedowse/* 66579561Siedowse * Change the offset associated with a directory entry in the hash. Used 66679561Siedowse * when compacting directory blocks. 66779561Siedowse */ 66879561Siedowsevoid 66979561Siedowseufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff, 67079561Siedowse doff_t newoff) 67179561Siedowse{ 67279561Siedowse struct dirhash *dh; 67379561Siedowse int slot; 67479561Siedowse 67579561Siedowse if ((dh = ip->i_dirhash) == NULL) 67679561Siedowse return; 67779561Siedowse mtx_lock(&dh->dh_mtx); 67879561Siedowse if (dh->dh_hash == NULL) { 67979561Siedowse mtx_unlock(&dh->dh_mtx); 68079561Siedowse ufsdirhash_free(ip); 68179561Siedowse return; 68279561Siedowse } 68379561Siedowse 68479561Siedowse KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ && 68579561Siedowse newoff < dh->dh_dirblks * DIRBLKSIZ, 68679561Siedowse ("ufsdirhash_move: bad offset")); 68779561Siedowse /* Find the entry, and update the offset. */ 68879561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff); 68979561Siedowse DH_ENTRY(dh, slot) = newoff; 69079561Siedowse mtx_unlock(&dh->dh_mtx); 69179561Siedowse} 69279561Siedowse 69379561Siedowse/* 69479561Siedowse * Inform dirhash that the directory has grown by one block that 69579561Siedowse * begins at offset (i.e. the new length is offset + DIRBLKSIZ). 69679561Siedowse */ 69779561Siedowsevoid 69879561Siedowseufsdirhash_newblk(struct inode *ip, doff_t offset) 69979561Siedowse{ 70079561Siedowse struct dirhash *dh; 70179561Siedowse int block; 70279561Siedowse 70379561Siedowse if ((dh = ip->i_dirhash) == NULL) 70479561Siedowse return; 70579561Siedowse mtx_lock(&dh->dh_mtx); 70679561Siedowse if (dh->dh_hash == NULL) { 70779561Siedowse mtx_unlock(&dh->dh_mtx); 70879561Siedowse ufsdirhash_free(ip); 70979561Siedowse return; 71079561Siedowse } 71179561Siedowse 71279561Siedowse KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ, 71379561Siedowse ("ufsdirhash_newblk: bad offset")); 71479561Siedowse block = offset / DIRBLKSIZ; 71579561Siedowse if (block >= dh->dh_nblk) { 71679561Siedowse /* Out of space; must rebuild. */ 71779561Siedowse mtx_unlock(&dh->dh_mtx); 71879561Siedowse ufsdirhash_free(ip); 71979561Siedowse return; 72079561Siedowse } 72179561Siedowse dh->dh_dirblks = block + 1; 72279561Siedowse 72379561Siedowse /* Account for the new free block. */ 72479561Siedowse dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN; 72579561Siedowse if (dh->dh_firstfree[DH_NFSTATS] == -1) 72679561Siedowse dh->dh_firstfree[DH_NFSTATS] = block; 72779561Siedowse mtx_unlock(&dh->dh_mtx); 72879561Siedowse} 72979561Siedowse 73079561Siedowse/* 73179561Siedowse * Inform dirhash that the directory is being truncated. 73279561Siedowse */ 73379561Siedowsevoid 73479561Siedowseufsdirhash_dirtrunc(struct inode *ip, doff_t offset) 73579561Siedowse{ 73679561Siedowse struct dirhash *dh; 73779561Siedowse int block, i; 73879561Siedowse 73979561Siedowse if ((dh = ip->i_dirhash) == NULL) 74079561Siedowse return; 74179561Siedowse mtx_lock(&dh->dh_mtx); 74279561Siedowse if (dh->dh_hash == NULL) { 74379561Siedowse mtx_unlock(&dh->dh_mtx); 74479561Siedowse ufsdirhash_free(ip); 74579561Siedowse return; 74679561Siedowse } 74779561Siedowse 74879561Siedowse KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ, 74979561Siedowse ("ufsdirhash_dirtrunc: bad offset")); 75079561Siedowse block = howmany(offset, DIRBLKSIZ); 75179561Siedowse /* 75279561Siedowse * If the directory shrinks to less than 1/8 of dh_nblk blocks 75379561Siedowse * (about 20% of its original size due to the 50% extra added in 75479561Siedowse * ufsdirhash_build) then free it, and let the caller rebuild 75579561Siedowse * if necessary. 75679561Siedowse */ 75779561Siedowse if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) { 75879561Siedowse mtx_unlock(&dh->dh_mtx); 75979561Siedowse ufsdirhash_free(ip); 76079561Siedowse return; 76179561Siedowse } 76279561Siedowse 76379561Siedowse /* 76479561Siedowse * Remove any `first free' information pertaining to the 76579561Siedowse * truncated blocks. All blocks we're removing should be 76679561Siedowse * completely unused. 76779561Siedowse */ 76879561Siedowse if (dh->dh_firstfree[DH_NFSTATS] >= block) 76979561Siedowse dh->dh_firstfree[DH_NFSTATS] = -1; 77079561Siedowse for (i = block; i < dh->dh_dirblks; i++) 77179561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 77279561Siedowse panic("ufsdirhash_dirtrunc: blocks in use"); 77379561Siedowse for (i = 0; i < DH_NFSTATS; i++) 77479561Siedowse if (dh->dh_firstfree[i] >= block) 77579561Siedowse panic("ufsdirhash_dirtrunc: first free corrupt"); 77679561Siedowse dh->dh_dirblks = block; 77779561Siedowse mtx_unlock(&dh->dh_mtx); 77879561Siedowse} 77979561Siedowse 78079561Siedowse/* 78179561Siedowse * Debugging function to check that the dirhash information about 78279561Siedowse * a directory block matches its actual contents. Panics if a mismatch 78379561Siedowse * is detected. 78479561Siedowse * 78579561Siedowse * On entry, `buf' should point to the start of an in-core 78679561Siedowse * DIRBLKSIZ-sized directory block, and `offset' should contain the 78779561Siedowse * offset from the start of the directory of that block. 78879561Siedowse */ 78979561Siedowsevoid 79079561Siedowseufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset) 79179561Siedowse{ 79279561Siedowse struct dirhash *dh; 79379561Siedowse struct direct *dp; 79479561Siedowse int block, ffslot, i, nfree; 79579561Siedowse 79679561Siedowse if (!ufs_dirhashcheck) 79779561Siedowse return; 79879561Siedowse if ((dh = ip->i_dirhash) == NULL) 79979561Siedowse return; 80079561Siedowse mtx_lock(&dh->dh_mtx); 80179561Siedowse if (dh->dh_hash == NULL) { 80279561Siedowse mtx_unlock(&dh->dh_mtx); 80379561Siedowse ufsdirhash_free(ip); 80479561Siedowse return; 80579561Siedowse } 80679561Siedowse 80779561Siedowse block = offset / DIRBLKSIZ; 80879561Siedowse if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks) 80979561Siedowse panic("ufsdirhash_checkblock: bad offset"); 81079561Siedowse 81179561Siedowse nfree = 0; 81279561Siedowse for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) { 81379561Siedowse dp = (struct direct *)(buf + i); 81479561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ) 81579561Siedowse panic("ufsdirhash_checkblock: bad dir"); 81679561Siedowse 81779561Siedowse if (dp->d_ino == 0) { 81879561Siedowse if (i != 0) 81979561Siedowse panic("ufsdirhash_checkblock: bad dir inode"); 82079561Siedowse nfree += dp->d_reclen; 82179561Siedowse continue; 82279561Siedowse } 82379561Siedowse 82479561Siedowse /* Check that the entry exists (will panic if it doesn't). */ 82579561Siedowse ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i); 82679561Siedowse 82779561Siedowse nfree += dp->d_reclen - DIRSIZ(0, dp); 82879561Siedowse } 82979561Siedowse if (i != DIRBLKSIZ) 83079561Siedowse panic("ufsdirhash_checkblock: bad dir end"); 83179561Siedowse 83279561Siedowse if (dh->dh_blkfree[block] * DIRALIGN != nfree) 83379561Siedowse panic("ufsdirhash_checkblock: bad free count"); 83479561Siedowse 83579561Siedowse ffslot = nfree / DIRALIGN; 83679561Siedowse if (ffslot > DH_NFSTATS) 83779561Siedowse ffslot = DH_NFSTATS; 83879561Siedowse for (i = 0; i <= DH_NFSTATS; i++) 83979561Siedowse if (dh->dh_firstfree[i] == block && i != ffslot) 84079561Siedowse panic("ufsdirhash_checkblock: bad first-free"); 84179561Siedowse mtx_unlock(&dh->dh_mtx); 84279561Siedowse} 84379561Siedowse 84479561Siedowse/* 84579561Siedowse * Hash the specified filename into a dirhash slot. 84679561Siedowse */ 84779561Siedowsestatic int 84879561Siedowseufsdirhash_hash(struct dirhash *dh, char *name, int namelen) 84979561Siedowse{ 85079561Siedowse return (fnv_32_buf(name, namelen, FNV1_32_INIT) % dh->dh_hlen); 85179561Siedowse} 85279561Siedowse 85379561Siedowse/* 85479561Siedowse * Adjust the number of free bytes in the block containing `offset' 85579561Siedowse * by the value specified by `diff'. 85679561Siedowse * 85779561Siedowse * The caller must ensure we have exclusive access to `dh'; normally 85879561Siedowse * that means that dh_mtx should be held, but this is also called 85979561Siedowse * from ufsdirhash_build() where exclusive access can be assumed. 86079561Siedowse */ 86179561Siedowsestatic void 86279561Siedowseufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff) 86379561Siedowse{ 86479561Siedowse int block, i, nfidx, ofidx; 86579561Siedowse 86679561Siedowse /* Update the per-block summary info. */ 86779561Siedowse block = offset / DIRBLKSIZ; 86879561Siedowse KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks, 86979561Siedowse ("dirhash bad offset")); 87079561Siedowse ofidx = dh->dh_blkfree[block]; 87179561Siedowse if (ofidx > DH_NFSTATS) 87279561Siedowse ofidx = DH_NFSTATS; 87379561Siedowse dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN); 87479561Siedowse nfidx = dh->dh_blkfree[block]; 87579561Siedowse if (nfidx > DH_NFSTATS) 87679561Siedowse nfidx = DH_NFSTATS; 87779561Siedowse 87879561Siedowse /* Update the `first free' list if necessary. */ 87979561Siedowse if (ofidx != nfidx) { 88079561Siedowse /* If removing, scan forward for the next block. */ 88179561Siedowse if (dh->dh_firstfree[ofidx] == block) { 88279561Siedowse for (i = block + 1; i < dh->dh_dirblks; i++) 88379561Siedowse if (dh->dh_blkfree[i] == ofidx) 88479561Siedowse break; 88579561Siedowse dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1; 88679561Siedowse } 88779561Siedowse 88879561Siedowse /* Make this the new `first free' if necessary */ 88979561Siedowse if (dh->dh_firstfree[nfidx] > block || 89079561Siedowse dh->dh_firstfree[nfidx] == -1) 89179561Siedowse dh->dh_firstfree[nfidx] = block; 89279561Siedowse } 89379561Siedowse} 89479561Siedowse 89579561Siedowse/* 89679561Siedowse * Find the specified name which should have the specified offset. 89779561Siedowse * Returns a slot number, and panics on failure. 89879561Siedowse * 89979561Siedowse * `dh' must be locked on entry and remains so on return. 90079561Siedowse */ 90179561Siedowsestatic int 90279561Siedowseufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset) 90379561Siedowse{ 90479561Siedowse int slot; 90579561Siedowse 90679561Siedowse mtx_assert(&dh->dh_mtx, MA_OWNED); 90779561Siedowse 90879561Siedowse /* Find the entry. */ 90979561Siedowse KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full")); 91079561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 91179561Siedowse while (DH_ENTRY(dh, slot) != offset && 91279561Siedowse DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 91379561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 91479561Siedowse if (DH_ENTRY(dh, slot) != offset) 91579561Siedowse panic("ufsdirhash_findslot: '%.*s' not found", namelen, name); 91679561Siedowse 91779561Siedowse return (slot); 91879561Siedowse} 91979561Siedowse 92079561Siedowse/* 92179561Siedowse * Remove the entry corresponding to the specified slot from the hash array. 92279561Siedowse * 92379561Siedowse * `dh' must be locked on entry and remains so on return. 92479561Siedowse */ 92579561Siedowsestatic void 92679561Siedowseufsdirhash_delslot(struct dirhash *dh, int slot) 92779561Siedowse{ 92879561Siedowse int i; 92979561Siedowse 93079561Siedowse mtx_assert(&dh->dh_mtx, MA_OWNED); 93179561Siedowse 93279561Siedowse /* Mark the entry as deleted. */ 93379561Siedowse DH_ENTRY(dh, slot) = DIRHASH_DEL; 93479561Siedowse 93579561Siedowse /* If this is the end of a chain of DIRHASH_DEL slots, remove them. */ 93679561Siedowse for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; ) 93779561Siedowse i = WRAPINCR(i, dh->dh_hlen); 93879561Siedowse if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) { 93979561Siedowse for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; ) { 94079561Siedowse DH_ENTRY(dh, i) = DIRHASH_EMPTY; 94179561Siedowse dh->dh_hused--; 94279561Siedowse i = WRAPINCR(i, dh->dh_hlen); 94379561Siedowse } 94479561Siedowse KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen")); 94579561Siedowse } 94679561Siedowse} 94779561Siedowse 94879561Siedowse/* 94979561Siedowse * Given a directory entry and its offset, find the offset of the 95079561Siedowse * previous entry in the same DIRBLKSIZ-sized block. Returns an 95179561Siedowse * offset, or -1 if there is no previous entry in the block or some 95279561Siedowse * other problem occurred. 95379561Siedowse */ 95479561Siedowsestatic doff_t 95579561Siedowseufsdirhash_getprev(struct direct *dirp, doff_t offset) 95679561Siedowse{ 95779561Siedowse struct direct *dp; 95879561Siedowse char *blkbuf; 95979561Siedowse doff_t blkoff, prevoff; 96079561Siedowse int entrypos, i; 96179561Siedowse 96279561Siedowse blkoff = offset & ~(DIRBLKSIZ - 1); /* offset of start of block */ 96379561Siedowse entrypos = offset & (DIRBLKSIZ - 1); /* entry relative to block */ 96479561Siedowse blkbuf = (char *)dirp - entrypos; 96579561Siedowse prevoff = blkoff; 96679561Siedowse 96779561Siedowse /* If `offset' is the start of a block, there is no previous entry. */ 96879561Siedowse if (entrypos == 0) 96979561Siedowse return (-1); 97079561Siedowse 97179561Siedowse /* Scan from the start of the block until we get to the entry. */ 97279561Siedowse for (i = 0; i < entrypos; i += dp->d_reclen) { 97379561Siedowse dp = (struct direct *)(blkbuf + i); 97479561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos) 97579561Siedowse return (-1); /* Corrupted directory. */ 97679561Siedowse prevoff = blkoff + i; 97779561Siedowse } 97879561Siedowse return (prevoff); 97979561Siedowse} 98079561Siedowse 98179561Siedowse/* 98279561Siedowse * Try to free up `wanted' bytes by stealing memory from existing 98379561Siedowse * dirhashes. Returns zero with ufsdirhash_mtx locked if successful. 98479561Siedowse */ 98579561Siedowsestatic int 98679561Siedowseufsdirhash_recycle(int wanted) 98779561Siedowse{ 98879561Siedowse struct dirhash *dh; 98979561Siedowse doff_t **hash; 99079561Siedowse u_int8_t *blkfree; 99179561Siedowse int i, mem, narrays; 99279561Siedowse 99379561Siedowse mtx_lock(&ufsdirhash_mtx); 99479561Siedowse while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) { 99579561Siedowse /* Find a dirhash, and lock it. */ 99679561Siedowse if ((dh = TAILQ_FIRST(&ufsdirhash_list)) == NULL) { 99779561Siedowse mtx_unlock(&ufsdirhash_mtx); 99879561Siedowse return (-1); 99979561Siedowse } 100079561Siedowse mtx_lock(&dh->dh_mtx); 100179561Siedowse KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list")); 100279561Siedowse 100379561Siedowse /* Decrement the score; only recycle if it becomes zero. */ 100479561Siedowse if (--dh->dh_score > 0) { 100579561Siedowse mtx_unlock(&dh->dh_mtx); 100679561Siedowse mtx_unlock(&ufsdirhash_mtx); 100779561Siedowse return (-1); 100879561Siedowse } 100979561Siedowse 101079561Siedowse /* Remove it from the list and detach its memory. */ 101179561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 101279561Siedowse dh->dh_onlist = 0; 101379561Siedowse hash = dh->dh_hash; 101479561Siedowse dh->dh_hash = NULL; 101579561Siedowse blkfree = dh->dh_blkfree; 101679561Siedowse dh->dh_blkfree = NULL; 101779561Siedowse narrays = dh->dh_narrays; 101879561Siedowse mem = narrays * sizeof(*dh->dh_hash) + 101979561Siedowse narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 102079561Siedowse dh->dh_nblk * sizeof(*dh->dh_blkfree); 102179561Siedowse 102279561Siedowse /* Unlock everything, free the detached memory. */ 102379561Siedowse mtx_unlock(&dh->dh_mtx); 102479561Siedowse mtx_unlock(&ufsdirhash_mtx); 102579561Siedowse for (i = 0; i < narrays; i++) 102679561Siedowse zfree(ufsdirhash_zone, hash[i]); 102779561Siedowse FREE(hash, M_DIRHASH); 102879561Siedowse FREE(blkfree, M_DIRHASH); 102979561Siedowse 103079561Siedowse /* Account for the returned memory, and repeat if necessary. */ 103179561Siedowse mtx_lock(&ufsdirhash_mtx); 103279561Siedowse ufs_dirhashmem -= mem; 103379561Siedowse } 103479561Siedowse /* Success; return with ufsdirhash_mtx locked. */ 103579561Siedowse return (0); 103679561Siedowse} 103779561Siedowse 103879561Siedowse 103979561Siedowsestatic void 104079561Siedowseufsdirhash_init() 104179561Siedowse{ 104279561Siedowse ufsdirhash_zone = zinit("DIRHASH", DH_NBLKOFF * sizeof(daddr_t), 0, 104379561Siedowse 0, 1); 104479561Siedowse mtx_init(&ufsdirhash_mtx, "dirhash list", MTX_DEF); 104579561Siedowse TAILQ_INIT(&ufsdirhash_list); 104679561Siedowse} 104779561SiedowseSYSINIT(ufsdirhash, SI_SUB_PSEUDO, SI_ORDER_ANY, ufsdirhash_init, NULL) 104879561Siedowse 104979561Siedowse 105079561Siedowse#endif /* UFS_DIRHASH */ 1051