ufs_dirhash.c revision 125854
179561Siedowse/* 2107868Siedowse * Copyright (c) 2001, 2002 Ian Dowse. All rights reserved. 379561Siedowse * 479561Siedowse * Redistribution and use in source and binary forms, with or without 579561Siedowse * modification, are permitted provided that the following conditions 679561Siedowse * are met: 779561Siedowse * 1. Redistributions of source code must retain the above copyright 879561Siedowse * notice, this list of conditions and the following disclaimer. 979561Siedowse * 2. Redistributions in binary form must reproduce the above copyright 1079561Siedowse * notice, this list of conditions and the following disclaimer in the 1179561Siedowse * documentation and/or other materials provided with the distribution. 1279561Siedowse * 1379561Siedowse * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1479561Siedowse * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1579561Siedowse * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1679561Siedowse * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1779561Siedowse * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1879561Siedowse * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1979561Siedowse * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2079561Siedowse * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2179561Siedowse * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2279561Siedowse * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2379561Siedowse * SUCH DAMAGE. 2479561Siedowse */ 25116192Sobrien 2679561Siedowse/* 2779561Siedowse * This implements a hash-based lookup scheme for UFS directories. 2879561Siedowse */ 2979561Siedowse 30116192Sobrien#include <sys/cdefs.h> 31116192Sobrien__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_dirhash.c 125854 2004-02-15 21:39:35Z dwmalone $"); 32116192Sobrien 3379561Siedowse#include "opt_ufs.h" 3479561Siedowse 3579561Siedowse#ifdef UFS_DIRHASH 3679561Siedowse 3779561Siedowse#include <sys/param.h> 3879561Siedowse#include <sys/systm.h> 3979561Siedowse#include <sys/kernel.h> 4084811Sjhb#include <sys/lock.h> 4179561Siedowse#include <sys/mutex.h> 4279561Siedowse#include <sys/malloc.h> 4379561Siedowse#include <sys/fnv_hash.h> 4479561Siedowse#include <sys/proc.h> 4579561Siedowse#include <sys/bio.h> 4679561Siedowse#include <sys/buf.h> 4779561Siedowse#include <sys/vnode.h> 4879561Siedowse#include <sys/mount.h> 4979561Siedowse#include <sys/sysctl.h> 5092768Sjeff#include <vm/uma.h> 5179561Siedowse 5279561Siedowse#include <ufs/ufs/quota.h> 5379561Siedowse#include <ufs/ufs/inode.h> 5479561Siedowse#include <ufs/ufs/dir.h> 5579561Siedowse#include <ufs/ufs/dirhash.h> 5679561Siedowse#include <ufs/ufs/extattr.h> 5779561Siedowse#include <ufs/ufs/ufsmount.h> 5879561Siedowse#include <ufs/ufs/ufs_extern.h> 5979561Siedowse 6079561Siedowse#define WRAPINCR(val, limit) (((val) + 1 == (limit)) ? 0 : ((val) + 1)) 6192807Sdwmalone#define WRAPDECR(val, limit) (((val) == 0) ? ((limit) - 1) : ((val) - 1)) 6279561Siedowse#define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0) 6392098Siedowse#define BLKFREE2IDX(n) ((n) > DH_NFSTATS ? DH_NFSTATS : (n)) 6479561Siedowse 6579561Siedowsestatic MALLOC_DEFINE(M_DIRHASH, "UFS dirhash", "UFS directory hash tables"); 6679561Siedowse 6779561SiedowseSYSCTL_NODE(_vfs, OID_AUTO, ufs, CTLFLAG_RD, 0, "UFS filesystem"); 6879561Siedowse 6979561Siedowsestatic int ufs_mindirhashsize = DIRBLKSIZ * 5; 7079561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_minsize, CTLFLAG_RW, 7179561Siedowse &ufs_mindirhashsize, 7279561Siedowse 0, "minimum directory size in bytes for which to use hashed lookup"); 7379561Siedowsestatic int ufs_dirhashmaxmem = 2 * 1024 * 1024; 7479561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_maxmem, CTLFLAG_RW, &ufs_dirhashmaxmem, 7579561Siedowse 0, "maximum allowed dirhash memory usage"); 7679561Siedowsestatic int ufs_dirhashmem; 7779561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_mem, CTLFLAG_RD, &ufs_dirhashmem, 7879561Siedowse 0, "current dirhash memory usage"); 7985512Siedowsestatic int ufs_dirhashcheck = 0; 8079561SiedowseSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_docheck, CTLFLAG_RW, &ufs_dirhashcheck, 8179561Siedowse 0, "enable extra sanity tests"); 8279561Siedowse 8379561Siedowse 8479561Siedowsestatic int ufsdirhash_hash(struct dirhash *dh, char *name, int namelen); 8579561Siedowsestatic void ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff); 8679561Siedowsestatic void ufsdirhash_delslot(struct dirhash *dh, int slot); 8779561Siedowsestatic int ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, 8879561Siedowse doff_t offset); 8979561Siedowsestatic doff_t ufsdirhash_getprev(struct direct *dp, doff_t offset); 9079561Siedowsestatic int ufsdirhash_recycle(int wanted); 9179561Siedowse 9292768Sjeffstatic uma_zone_t ufsdirhash_zone; 9379561Siedowse 94125854Sdwmalone#define DIRHASHLIST_LOCK() mtx_lock(&ufsdirhash_mtx) 95125854Sdwmalone#define DIRHASHLIST_UNLOCK() mtx_unlock(&ufsdirhash_mtx) 96125854Sdwmalone#define DIRHASH_LOCK(dh) mtx_lock(&(dh)->dh_mtx) 97125854Sdwmalone#define DIRHASH_UNLOCK(dh) mtx_unlock(&(dh)->dh_mtx) 98125854Sdwmalone#define DIRHASH_BLKALLOC_WAITOK() uma_zalloc(ufsdirhash_zone, M_WAITOK) 99125854Sdwmalone#define DIRHASH_BLKFREE(ptr) uma_zfree(ufsdirhash_zone, (ptr)) 100125854Sdwmalone 10179561Siedowse/* Dirhash list; recently-used entries are near the tail. */ 10279561Siedowsestatic TAILQ_HEAD(, dirhash) ufsdirhash_list; 10379561Siedowse 10479561Siedowse/* Protects: ufsdirhash_list, `dh_list' field, ufs_dirhashmem. */ 10579561Siedowsestatic struct mtx ufsdirhash_mtx; 10679561Siedowse 10779561Siedowse/* 10879561Siedowse * Locking order: 10979561Siedowse * ufsdirhash_mtx 11079561Siedowse * dh_mtx 11179561Siedowse * 112107868Siedowse * The dh_mtx mutex should be acquired either via the inode lock, or via 11379561Siedowse * ufsdirhash_mtx. Only the owner of the inode may free the associated 11479561Siedowse * dirhash, but anything can steal its memory and set dh_hash to NULL. 11579561Siedowse */ 11679561Siedowse 11779561Siedowse/* 11879561Siedowse * Attempt to build up a hash table for the directory contents in 11979561Siedowse * inode 'ip'. Returns 0 on success, or -1 of the operation failed. 12079561Siedowse */ 12179561Siedowseint 12279561Siedowseufsdirhash_build(struct inode *ip) 12379561Siedowse{ 12479561Siedowse struct dirhash *dh; 12579561Siedowse struct buf *bp = NULL; 12679561Siedowse struct direct *ep; 12779561Siedowse struct vnode *vp; 12879561Siedowse doff_t bmask, pos; 12979561Siedowse int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot; 13079561Siedowse 13179561Siedowse /* Check if we can/should use dirhash. */ 13279561Siedowse if (ip->i_dirhash == NULL) { 13379561Siedowse if (ip->i_size < ufs_mindirhashsize || OFSFMT(ip->i_vnode)) 13479561Siedowse return (-1); 13579561Siedowse } else { 13679561Siedowse /* Hash exists, but sysctls could have changed. */ 13779561Siedowse if (ip->i_size < ufs_mindirhashsize || 13879561Siedowse ufs_dirhashmem > ufs_dirhashmaxmem) { 13979561Siedowse ufsdirhash_free(ip); 14079561Siedowse return (-1); 14179561Siedowse } 14279561Siedowse /* Check if hash exists and is intact (note: unlocked read). */ 14379561Siedowse if (ip->i_dirhash->dh_hash != NULL) 14479561Siedowse return (0); 14579561Siedowse /* Free the old, recycled hash and build a new one. */ 14679561Siedowse ufsdirhash_free(ip); 14779561Siedowse } 14879561Siedowse 14982364Siedowse /* Don't hash removed directories. */ 15082364Siedowse if (ip->i_effnlink == 0) 15182364Siedowse return (-1); 15282364Siedowse 15379561Siedowse vp = ip->i_vnode; 15479561Siedowse /* Allocate 50% more entries than this dir size could ever need. */ 15579561Siedowse KASSERT(ip->i_size >= DIRBLKSIZ, ("ufsdirhash_build size")); 15679561Siedowse nslots = ip->i_size / DIRECTSIZ(1); 15779561Siedowse nslots = (nslots * 3 + 1) / 2; 15879561Siedowse narrays = howmany(nslots, DH_NBLKOFF); 15979561Siedowse nslots = narrays * DH_NBLKOFF; 16079561Siedowse dirblocks = howmany(ip->i_size, DIRBLKSIZ); 16179561Siedowse nblocks = (dirblocks * 3 + 1) / 2; 16279561Siedowse 16379561Siedowse memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) + 16479561Siedowse narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 16579561Siedowse nblocks * sizeof(*dh->dh_blkfree); 166125854Sdwmalone DIRHASHLIST_LOCK(); 16779561Siedowse if (memreqd + ufs_dirhashmem > ufs_dirhashmaxmem) { 168125854Sdwmalone DIRHASHLIST_UNLOCK(); 16979561Siedowse if (memreqd > ufs_dirhashmaxmem / 2) 17079561Siedowse return (-1); 17179561Siedowse 17279561Siedowse /* Try to free some space. */ 17379561Siedowse if (ufsdirhash_recycle(memreqd) != 0) 17479561Siedowse return (-1); 175125854Sdwmalone /* Enough was freed, and list has been locked. */ 17679561Siedowse } 17779561Siedowse ufs_dirhashmem += memreqd; 178125854Sdwmalone DIRHASHLIST_UNLOCK(); 17979561Siedowse 18079561Siedowse /* 18179561Siedowse * Use non-blocking mallocs so that we will revert to a linear 18279561Siedowse * lookup on failure rather than potentially blocking forever. 18379561Siedowse */ 18479561Siedowse MALLOC(dh, struct dirhash *, sizeof *dh, M_DIRHASH, M_NOWAIT | M_ZERO); 185107868Siedowse if (dh == NULL) { 186125854Sdwmalone DIRHASHLIST_LOCK(); 187107868Siedowse ufs_dirhashmem -= memreqd; 188125854Sdwmalone DIRHASHLIST_UNLOCK(); 18979561Siedowse return (-1); 190107868Siedowse } 19179561Siedowse MALLOC(dh->dh_hash, doff_t **, narrays * sizeof(dh->dh_hash[0]), 19279561Siedowse M_DIRHASH, M_NOWAIT | M_ZERO); 19379561Siedowse MALLOC(dh->dh_blkfree, u_int8_t *, nblocks * sizeof(dh->dh_blkfree[0]), 19479561Siedowse M_DIRHASH, M_NOWAIT); 19579561Siedowse if (dh->dh_hash == NULL || dh->dh_blkfree == NULL) 19679561Siedowse goto fail; 19779561Siedowse for (i = 0; i < narrays; i++) { 198125854Sdwmalone if ((dh->dh_hash[i] = DIRHASH_BLKALLOC_WAITOK()) == NULL) 19979561Siedowse goto fail; 20079561Siedowse for (j = 0; j < DH_NBLKOFF; j++) 20179561Siedowse dh->dh_hash[i][j] = DIRHASH_EMPTY; 20279561Siedowse } 20379561Siedowse 20479561Siedowse /* Initialise the hash table and block statistics. */ 20593818Sjhb mtx_init(&dh->dh_mtx, "dirhash", NULL, MTX_DEF); 20679561Siedowse dh->dh_narrays = narrays; 20779561Siedowse dh->dh_hlen = nslots; 20879561Siedowse dh->dh_nblk = nblocks; 20979561Siedowse dh->dh_dirblks = dirblocks; 21079561Siedowse for (i = 0; i < dirblocks; i++) 21179561Siedowse dh->dh_blkfree[i] = DIRBLKSIZ / DIRALIGN; 21279561Siedowse for (i = 0; i < DH_NFSTATS; i++) 21379561Siedowse dh->dh_firstfree[i] = -1; 21479561Siedowse dh->dh_firstfree[DH_NFSTATS] = 0; 21579561Siedowse dh->dh_seqopt = 0; 21679561Siedowse dh->dh_seqoff = 0; 21779561Siedowse dh->dh_score = DH_SCOREINIT; 21879561Siedowse ip->i_dirhash = dh; 21979561Siedowse 22079561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 22179561Siedowse pos = 0; 22279561Siedowse while (pos < ip->i_size) { 22379561Siedowse /* If necessary, get the next directory block. */ 22479561Siedowse if ((pos & bmask) == 0) { 22579561Siedowse if (bp != NULL) 22679561Siedowse brelse(bp); 22779561Siedowse if (UFS_BLKATOFF(vp, (off_t)pos, NULL, &bp) != 0) 22879561Siedowse goto fail; 22979561Siedowse } 23079561Siedowse 23179561Siedowse /* Add this entry to the hash. */ 23279561Siedowse ep = (struct direct *)((char *)bp->b_data + (pos & bmask)); 23379561Siedowse if (ep->d_reclen == 0 || ep->d_reclen > 23479561Siedowse DIRBLKSIZ - (pos & (DIRBLKSIZ - 1))) { 23579561Siedowse /* Corrupted directory. */ 23679561Siedowse brelse(bp); 23779561Siedowse goto fail; 23879561Siedowse } 23979561Siedowse if (ep->d_ino != 0) { 24079561Siedowse /* Add the entry (simplified ufsdirhash_add). */ 24179561Siedowse slot = ufsdirhash_hash(dh, ep->d_name, ep->d_namlen); 24279561Siedowse while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 24379561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 24479561Siedowse dh->dh_hused++; 24579561Siedowse DH_ENTRY(dh, slot) = pos; 24679561Siedowse ufsdirhash_adjfree(dh, pos, -DIRSIZ(0, ep)); 24779561Siedowse } 24879561Siedowse pos += ep->d_reclen; 24979561Siedowse } 25079561Siedowse 25179561Siedowse if (bp != NULL) 25279561Siedowse brelse(bp); 253125854Sdwmalone DIRHASHLIST_LOCK(); 25479561Siedowse TAILQ_INSERT_TAIL(&ufsdirhash_list, dh, dh_list); 25579561Siedowse dh->dh_onlist = 1; 256125854Sdwmalone DIRHASHLIST_UNLOCK(); 25779561Siedowse return (0); 25879561Siedowse 25979561Siedowsefail: 26079561Siedowse if (dh->dh_hash != NULL) { 26179561Siedowse for (i = 0; i < narrays; i++) 26279561Siedowse if (dh->dh_hash[i] != NULL) 263125854Sdwmalone DIRHASH_BLKFREE(dh->dh_hash[i]); 26479561Siedowse FREE(dh->dh_hash, M_DIRHASH); 26579561Siedowse } 26679561Siedowse if (dh->dh_blkfree != NULL) 26779561Siedowse FREE(dh->dh_blkfree, M_DIRHASH); 26879561Siedowse FREE(dh, M_DIRHASH); 26979561Siedowse ip->i_dirhash = NULL; 270125854Sdwmalone DIRHASHLIST_LOCK(); 27179561Siedowse ufs_dirhashmem -= memreqd; 272125854Sdwmalone DIRHASHLIST_UNLOCK(); 27379561Siedowse return (-1); 27479561Siedowse} 27579561Siedowse 27679561Siedowse/* 27779561Siedowse * Free any hash table associated with inode 'ip'. 27879561Siedowse */ 27979561Siedowsevoid 28079561Siedowseufsdirhash_free(struct inode *ip) 28179561Siedowse{ 28279561Siedowse struct dirhash *dh; 28379561Siedowse int i, mem; 28479561Siedowse 28579561Siedowse if ((dh = ip->i_dirhash) == NULL) 28679561Siedowse return; 287125854Sdwmalone DIRHASHLIST_LOCK(); 288125854Sdwmalone DIRHASH_LOCK(dh); 28979561Siedowse if (dh->dh_onlist) 29079561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 291125854Sdwmalone DIRHASH_UNLOCK(dh); 292125854Sdwmalone DIRHASHLIST_UNLOCK(); 29379561Siedowse 29479561Siedowse /* The dirhash pointed to by 'dh' is exclusively ours now. */ 29579561Siedowse 29679561Siedowse mem = sizeof(*dh); 29779561Siedowse if (dh->dh_hash != NULL) { 29879561Siedowse for (i = 0; i < dh->dh_narrays; i++) 299125854Sdwmalone DIRHASH_BLKFREE(dh->dh_hash[i]); 30079561Siedowse FREE(dh->dh_hash, M_DIRHASH); 30179561Siedowse FREE(dh->dh_blkfree, M_DIRHASH); 30279561Siedowse mem += dh->dh_narrays * sizeof(*dh->dh_hash) + 30379561Siedowse dh->dh_narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 30479561Siedowse dh->dh_nblk * sizeof(*dh->dh_blkfree); 30579561Siedowse } 30679561Siedowse mtx_destroy(&dh->dh_mtx); 30779561Siedowse FREE(dh, M_DIRHASH); 30879561Siedowse ip->i_dirhash = NULL; 30979561Siedowse 310125854Sdwmalone DIRHASHLIST_LOCK(); 31179561Siedowse ufs_dirhashmem -= mem; 312125854Sdwmalone DIRHASHLIST_UNLOCK(); 31379561Siedowse} 31479561Siedowse 31579561Siedowse/* 31679561Siedowse * Find the offset of the specified name within the given inode. 31779561Siedowse * Returns 0 on success, ENOENT if the entry does not exist, or 31879561Siedowse * EJUSTRETURN if the caller should revert to a linear search. 31979561Siedowse * 32079690Siedowse * If successful, the directory offset is stored in *offp, and a 32179690Siedowse * pointer to a struct buf containing the entry is stored in *bpp. If 32279561Siedowse * prevoffp is non-NULL, the offset of the previous entry within 32379561Siedowse * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry 32479561Siedowse * is the first in a block, the start of the block is used). 32579561Siedowse */ 32679561Siedowseint 32779561Siedowseufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp, 32879690Siedowse struct buf **bpp, doff_t *prevoffp) 32979561Siedowse{ 33079561Siedowse struct dirhash *dh, *dh_next; 33179561Siedowse struct direct *dp; 33279561Siedowse struct vnode *vp; 33379561Siedowse struct buf *bp; 33479561Siedowse doff_t blkoff, bmask, offset, prevoff; 33579561Siedowse int i, slot; 33679561Siedowse 33779561Siedowse if ((dh = ip->i_dirhash) == NULL) 33879561Siedowse return (EJUSTRETURN); 33979561Siedowse /* 34079561Siedowse * Move this dirhash towards the end of the list if it has a 341107868Siedowse * score higher than the next entry, and acquire the dh_mtx. 34279561Siedowse * Optimise the case where it's already the last by performing 34379561Siedowse * an unlocked read of the TAILQ_NEXT pointer. 34479561Siedowse * 34579561Siedowse * In both cases, end up holding just dh_mtx. 34679561Siedowse */ 34779561Siedowse if (TAILQ_NEXT(dh, dh_list) != NULL) { 348125854Sdwmalone DIRHASHLIST_LOCK(); 349125854Sdwmalone DIRHASH_LOCK(dh); 35079561Siedowse /* 35179561Siedowse * If the new score will be greater than that of the next 35279561Siedowse * entry, then move this entry past it. With both mutexes 35379561Siedowse * held, dh_next won't go away, but its dh_score could 35479561Siedowse * change; that's not important since it is just a hint. 35579561Siedowse */ 35679561Siedowse if (dh->dh_hash != NULL && 35779561Siedowse (dh_next = TAILQ_NEXT(dh, dh_list)) != NULL && 35879561Siedowse dh->dh_score >= dh_next->dh_score) { 35979561Siedowse KASSERT(dh->dh_onlist, ("dirhash: not on list")); 36079561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 36179561Siedowse TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh, 36279561Siedowse dh_list); 36379561Siedowse } 364125854Sdwmalone DIRHASHLIST_UNLOCK(); 36579561Siedowse } else { 36679561Siedowse /* Already the last, though that could change as we wait. */ 367125854Sdwmalone DIRHASH_LOCK(dh); 36879561Siedowse } 36979561Siedowse if (dh->dh_hash == NULL) { 370125854Sdwmalone DIRHASH_UNLOCK(dh); 37179561Siedowse ufsdirhash_free(ip); 37279561Siedowse return (EJUSTRETURN); 37379561Siedowse } 37479561Siedowse 37579561Siedowse /* Update the score. */ 37679561Siedowse if (dh->dh_score < DH_SCOREMAX) 37779561Siedowse dh->dh_score++; 37879561Siedowse 37979561Siedowse vp = ip->i_vnode; 38079561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 38179561Siedowse blkoff = -1; 38279561Siedowse bp = NULL; 38379561Siedowserestart: 38479561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 38579561Siedowse 38679561Siedowse if (dh->dh_seqopt) { 38779561Siedowse /* 38879561Siedowse * Sequential access optimisation. dh_seqoff contains the 38979561Siedowse * offset of the directory entry immediately following 39079561Siedowse * the last entry that was looked up. Check if this offset 39179561Siedowse * appears in the hash chain for the name we are looking for. 39279561Siedowse */ 39379561Siedowse for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY; 39479561Siedowse i = WRAPINCR(i, dh->dh_hlen)) 39586350Siedowse if (offset == dh->dh_seqoff) 39679561Siedowse break; 39779561Siedowse if (offset == dh->dh_seqoff) { 39879561Siedowse /* 39979561Siedowse * We found an entry with the expected offset. This 40079561Siedowse * is probably the entry we want, but if not, the 40179561Siedowse * code below will turn off seqoff and retry. 40279561Siedowse */ 40379561Siedowse slot = i; 40479561Siedowse } else 40579561Siedowse dh->dh_seqopt = 0; 40679561Siedowse } 40779561Siedowse 40879561Siedowse for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY; 40979561Siedowse slot = WRAPINCR(slot, dh->dh_hlen)) { 41079561Siedowse if (offset == DIRHASH_DEL) 41179561Siedowse continue; 412125854Sdwmalone DIRHASH_UNLOCK(dh); 41379561Siedowse 41479561Siedowse if (offset < 0 || offset >= ip->i_size) 41579561Siedowse panic("ufsdirhash_lookup: bad offset in hash array"); 41679561Siedowse if ((offset & ~bmask) != blkoff) { 41779561Siedowse if (bp != NULL) 41879561Siedowse brelse(bp); 41979561Siedowse blkoff = offset & ~bmask; 42079561Siedowse if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) 42179561Siedowse return (EJUSTRETURN); 42279561Siedowse } 42379561Siedowse dp = (struct direct *)(bp->b_data + (offset & bmask)); 42479561Siedowse if (dp->d_reclen == 0 || dp->d_reclen > 42579561Siedowse DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) { 42679561Siedowse /* Corrupted directory. */ 42779561Siedowse brelse(bp); 42879561Siedowse return (EJUSTRETURN); 42979561Siedowse } 43079561Siedowse if (dp->d_namlen == namelen && 43179561Siedowse bcmp(dp->d_name, name, namelen) == 0) { 43279561Siedowse /* Found. Get the prev offset if needed. */ 43379561Siedowse if (prevoffp != NULL) { 43479561Siedowse if (offset & (DIRBLKSIZ - 1)) { 43579561Siedowse prevoff = ufsdirhash_getprev(dp, 43679561Siedowse offset); 43779561Siedowse if (prevoff == -1) { 43879561Siedowse brelse(bp); 43979561Siedowse return (EJUSTRETURN); 44079561Siedowse } 44179561Siedowse } else 44279561Siedowse prevoff = offset; 44379561Siedowse *prevoffp = prevoff; 44479561Siedowse } 44579561Siedowse 44679561Siedowse /* Check for sequential access, and update offset. */ 44779561Siedowse if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset) 44879561Siedowse dh->dh_seqopt = 1; 44979561Siedowse dh->dh_seqoff = offset + DIRSIZ(0, dp); 45079561Siedowse 45179690Siedowse *bpp = bp; 45279561Siedowse *offp = offset; 45379561Siedowse return (0); 45479561Siedowse } 45579561Siedowse 456125854Sdwmalone DIRHASH_LOCK(dh); 45779561Siedowse if (dh->dh_hash == NULL) { 458125854Sdwmalone DIRHASH_UNLOCK(dh); 45979561Siedowse if (bp != NULL) 46079561Siedowse brelse(bp); 46179561Siedowse ufsdirhash_free(ip); 46279561Siedowse return (EJUSTRETURN); 46379561Siedowse } 46479561Siedowse /* 46579561Siedowse * When the name doesn't match in the seqopt case, go back 46679561Siedowse * and search normally. 46779561Siedowse */ 46879561Siedowse if (dh->dh_seqopt) { 46979561Siedowse dh->dh_seqopt = 0; 47079561Siedowse goto restart; 47179561Siedowse } 47279561Siedowse } 473125854Sdwmalone DIRHASH_UNLOCK(dh); 47479561Siedowse if (bp != NULL) 47579561Siedowse brelse(bp); 47679561Siedowse return (ENOENT); 47779561Siedowse} 47879561Siedowse 47979561Siedowse/* 48079561Siedowse * Find a directory block with room for 'slotneeded' bytes. Returns 48179561Siedowse * the offset of the directory entry that begins the free space. 48279561Siedowse * This will either be the offset of an existing entry that has free 48379561Siedowse * space at the end, or the offset of an entry with d_ino == 0 at 48479561Siedowse * the start of a DIRBLKSIZ block. 48579561Siedowse * 48679561Siedowse * To use the space, the caller may need to compact existing entries in 48779561Siedowse * the directory. The total number of bytes in all of the entries involved 48879561Siedowse * in the compaction is stored in *slotsize. In other words, all of 48979561Siedowse * the entries that must be compacted are exactly contained in the 49079561Siedowse * region beginning at the returned offset and spanning *slotsize bytes. 49179561Siedowse * 49279561Siedowse * Returns -1 if no space was found, indicating that the directory 49379561Siedowse * must be extended. 49479561Siedowse */ 49579561Siedowsedoff_t 49679561Siedowseufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize) 49779561Siedowse{ 49879561Siedowse struct direct *dp; 49979561Siedowse struct dirhash *dh; 50079561Siedowse struct buf *bp; 50179561Siedowse doff_t pos, slotstart; 50279561Siedowse int dirblock, error, freebytes, i; 50379561Siedowse 50479561Siedowse if ((dh = ip->i_dirhash) == NULL) 50579561Siedowse return (-1); 506125854Sdwmalone DIRHASH_LOCK(dh); 50779561Siedowse if (dh->dh_hash == NULL) { 508125854Sdwmalone DIRHASH_UNLOCK(dh); 50979561Siedowse ufsdirhash_free(ip); 51079561Siedowse return (-1); 51179561Siedowse } 51279561Siedowse 51379561Siedowse /* Find a directory block with the desired free space. */ 51479561Siedowse dirblock = -1; 51579561Siedowse for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++) 51679561Siedowse if ((dirblock = dh->dh_firstfree[i]) != -1) 51779561Siedowse break; 51879561Siedowse if (dirblock == -1) { 519125854Sdwmalone DIRHASH_UNLOCK(dh); 52079561Siedowse return (-1); 52179561Siedowse } 52279561Siedowse 52379561Siedowse KASSERT(dirblock < dh->dh_nblk && 52479561Siedowse dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN), 52579561Siedowse ("ufsdirhash_findfree: bad stats")); 526125854Sdwmalone DIRHASH_UNLOCK(dh); 52779561Siedowse pos = dirblock * DIRBLKSIZ; 52879561Siedowse error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp); 52979561Siedowse if (error) 53079561Siedowse return (-1); 53179561Siedowse 53279561Siedowse /* Find the first entry with free space. */ 53379561Siedowse for (i = 0; i < DIRBLKSIZ; ) { 53479561Siedowse if (dp->d_reclen == 0) { 53579561Siedowse brelse(bp); 53679561Siedowse return (-1); 53779561Siedowse } 53879561Siedowse if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp)) 53979561Siedowse break; 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 slotstart = pos + i; 54879561Siedowse 54979561Siedowse /* Find the range of entries needed to get enough space */ 55079561Siedowse freebytes = 0; 55179561Siedowse while (i < DIRBLKSIZ && freebytes < slotneeded) { 55279561Siedowse freebytes += dp->d_reclen; 55379561Siedowse if (dp->d_ino != 0) 55479561Siedowse freebytes -= DIRSIZ(0, dp); 55579561Siedowse if (dp->d_reclen == 0) { 55679561Siedowse brelse(bp); 55779561Siedowse return (-1); 55879561Siedowse } 55979561Siedowse i += dp->d_reclen; 56079561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 56179561Siedowse } 56279561Siedowse if (i > DIRBLKSIZ) { 56379561Siedowse brelse(bp); 56479561Siedowse return (-1); 56579561Siedowse } 56679561Siedowse if (freebytes < slotneeded) 56779561Siedowse panic("ufsdirhash_findfree: free mismatch"); 56879561Siedowse brelse(bp); 56979561Siedowse *slotsize = pos + i - slotstart; 57079561Siedowse return (slotstart); 57179561Siedowse} 57279561Siedowse 57379561Siedowse/* 57479561Siedowse * Return the start of the unused space at the end of a directory, or 57579561Siedowse * -1 if there are no trailing unused blocks. 57679561Siedowse */ 57779561Siedowsedoff_t 57879561Siedowseufsdirhash_enduseful(struct inode *ip) 57979561Siedowse{ 58079561Siedowse 58179561Siedowse struct dirhash *dh; 58279561Siedowse int i; 58379561Siedowse 58479561Siedowse if ((dh = ip->i_dirhash) == NULL) 58579561Siedowse return (-1); 586125854Sdwmalone DIRHASH_LOCK(dh); 58779561Siedowse if (dh->dh_hash == NULL) { 588125854Sdwmalone DIRHASH_UNLOCK(dh); 58979561Siedowse ufsdirhash_free(ip); 59079561Siedowse return (-1); 59179561Siedowse } 59279561Siedowse 59379561Siedowse if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN) { 594125854Sdwmalone DIRHASH_UNLOCK(dh); 59579561Siedowse return (-1); 59679561Siedowse } 59779561Siedowse 59879561Siedowse for (i = dh->dh_dirblks - 1; i >= 0; i--) 59979561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 60079561Siedowse break; 601125854Sdwmalone DIRHASH_UNLOCK(dh); 60279561Siedowse return ((doff_t)(i + 1) * DIRBLKSIZ); 60379561Siedowse} 60479561Siedowse 60579561Siedowse/* 60679561Siedowse * Insert information into the hash about a new directory entry. dirp 60779561Siedowse * points to a struct direct containing the entry, and offset specifies 60879561Siedowse * the offset of this entry. 60979561Siedowse */ 61079561Siedowsevoid 61179561Siedowseufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset) 61279561Siedowse{ 61379561Siedowse struct dirhash *dh; 61479561Siedowse int slot; 61579561Siedowse 61679561Siedowse if ((dh = ip->i_dirhash) == NULL) 61779561Siedowse return; 618125854Sdwmalone DIRHASH_LOCK(dh); 61979561Siedowse if (dh->dh_hash == NULL) { 620125854Sdwmalone DIRHASH_UNLOCK(dh); 62179561Siedowse ufsdirhash_free(ip); 62279561Siedowse return; 62379561Siedowse } 62479561Siedowse 62579561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 62679561Siedowse ("ufsdirhash_add: bad offset")); 62779561Siedowse /* 62879561Siedowse * Normal hash usage is < 66%. If the usage gets too high then 62979561Siedowse * remove the hash entirely and let it be rebuilt later. 63079561Siedowse */ 63179561Siedowse if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) { 632125854Sdwmalone DIRHASH_UNLOCK(dh); 63379561Siedowse ufsdirhash_free(ip); 63479561Siedowse return; 63579561Siedowse } 63679561Siedowse 63779561Siedowse /* Find a free hash slot (empty or deleted), and add the entry. */ 63879561Siedowse slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen); 63979561Siedowse while (DH_ENTRY(dh, slot) >= 0) 64079561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 64179561Siedowse if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY) 64279561Siedowse dh->dh_hused++; 64379561Siedowse DH_ENTRY(dh, slot) = offset; 64479561Siedowse 64579561Siedowse /* Update the per-block summary info. */ 64679561Siedowse ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp)); 647125854Sdwmalone DIRHASH_UNLOCK(dh); 64879561Siedowse} 64979561Siedowse 65079561Siedowse/* 65179561Siedowse * Remove the specified directory entry from the hash. The entry to remove 65279561Siedowse * is defined by the name in `dirp', which must exist at the specified 65379561Siedowse * `offset' within the directory. 65479561Siedowse */ 65579561Siedowsevoid 65679561Siedowseufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset) 65779561Siedowse{ 65879561Siedowse struct dirhash *dh; 65979561Siedowse int slot; 66079561Siedowse 66179561Siedowse if ((dh = ip->i_dirhash) == NULL) 66279561Siedowse return; 663125854Sdwmalone DIRHASH_LOCK(dh); 66479561Siedowse if (dh->dh_hash == NULL) { 665125854Sdwmalone DIRHASH_UNLOCK(dh); 66679561Siedowse ufsdirhash_free(ip); 66779561Siedowse return; 66879561Siedowse } 66979561Siedowse 67079561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 67179561Siedowse ("ufsdirhash_remove: bad offset")); 67279561Siedowse /* Find the entry */ 67379561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset); 67479561Siedowse 67579561Siedowse /* Remove the hash entry. */ 67679561Siedowse ufsdirhash_delslot(dh, slot); 67779561Siedowse 67879561Siedowse /* Update the per-block summary info. */ 67979561Siedowse ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp)); 680125854Sdwmalone DIRHASH_UNLOCK(dh); 68179561Siedowse} 68279561Siedowse 68379561Siedowse/* 68479561Siedowse * Change the offset associated with a directory entry in the hash. Used 68579561Siedowse * when compacting directory blocks. 68679561Siedowse */ 68779561Siedowsevoid 68879561Siedowseufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff, 68979561Siedowse doff_t newoff) 69079561Siedowse{ 69179561Siedowse struct dirhash *dh; 69279561Siedowse int slot; 69379561Siedowse 69479561Siedowse if ((dh = ip->i_dirhash) == NULL) 69579561Siedowse return; 696125854Sdwmalone DIRHASH_LOCK(dh); 69779561Siedowse if (dh->dh_hash == NULL) { 698125854Sdwmalone DIRHASH_UNLOCK(dh); 69979561Siedowse ufsdirhash_free(ip); 70079561Siedowse return; 70179561Siedowse } 70279561Siedowse 70379561Siedowse KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ && 70479561Siedowse newoff < dh->dh_dirblks * DIRBLKSIZ, 70579561Siedowse ("ufsdirhash_move: bad offset")); 70679561Siedowse /* Find the entry, and update the offset. */ 70779561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff); 70879561Siedowse DH_ENTRY(dh, slot) = newoff; 709125854Sdwmalone DIRHASH_UNLOCK(dh); 71079561Siedowse} 71179561Siedowse 71279561Siedowse/* 71379561Siedowse * Inform dirhash that the directory has grown by one block that 71479561Siedowse * begins at offset (i.e. the new length is offset + DIRBLKSIZ). 71579561Siedowse */ 71679561Siedowsevoid 71779561Siedowseufsdirhash_newblk(struct inode *ip, doff_t offset) 71879561Siedowse{ 71979561Siedowse struct dirhash *dh; 72079561Siedowse int block; 72179561Siedowse 72279561Siedowse if ((dh = ip->i_dirhash) == NULL) 72379561Siedowse return; 724125854Sdwmalone DIRHASH_LOCK(dh); 72579561Siedowse if (dh->dh_hash == NULL) { 726125854Sdwmalone DIRHASH_UNLOCK(dh); 72779561Siedowse ufsdirhash_free(ip); 72879561Siedowse return; 72979561Siedowse } 73079561Siedowse 73179561Siedowse KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ, 73279561Siedowse ("ufsdirhash_newblk: bad offset")); 73379561Siedowse block = offset / DIRBLKSIZ; 73479561Siedowse if (block >= dh->dh_nblk) { 73579561Siedowse /* Out of space; must rebuild. */ 736125854Sdwmalone DIRHASH_UNLOCK(dh); 73779561Siedowse ufsdirhash_free(ip); 73879561Siedowse return; 73979561Siedowse } 74079561Siedowse dh->dh_dirblks = block + 1; 74179561Siedowse 74279561Siedowse /* Account for the new free block. */ 74379561Siedowse dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN; 74479561Siedowse if (dh->dh_firstfree[DH_NFSTATS] == -1) 74579561Siedowse dh->dh_firstfree[DH_NFSTATS] = block; 746125854Sdwmalone DIRHASH_UNLOCK(dh); 74779561Siedowse} 74879561Siedowse 74979561Siedowse/* 75079561Siedowse * Inform dirhash that the directory is being truncated. 75179561Siedowse */ 75279561Siedowsevoid 75379561Siedowseufsdirhash_dirtrunc(struct inode *ip, doff_t offset) 75479561Siedowse{ 75579561Siedowse struct dirhash *dh; 75679561Siedowse int block, i; 75779561Siedowse 75879561Siedowse if ((dh = ip->i_dirhash) == NULL) 75979561Siedowse return; 760125854Sdwmalone DIRHASH_LOCK(dh); 76179561Siedowse if (dh->dh_hash == NULL) { 762125854Sdwmalone DIRHASH_UNLOCK(dh); 76379561Siedowse ufsdirhash_free(ip); 76479561Siedowse return; 76579561Siedowse } 76679561Siedowse 76779561Siedowse KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ, 76879561Siedowse ("ufsdirhash_dirtrunc: bad offset")); 76979561Siedowse block = howmany(offset, DIRBLKSIZ); 77079561Siedowse /* 77179561Siedowse * If the directory shrinks to less than 1/8 of dh_nblk blocks 77279561Siedowse * (about 20% of its original size due to the 50% extra added in 77379561Siedowse * ufsdirhash_build) then free it, and let the caller rebuild 77479561Siedowse * if necessary. 77579561Siedowse */ 77679561Siedowse if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) { 777125854Sdwmalone DIRHASH_UNLOCK(dh); 77879561Siedowse ufsdirhash_free(ip); 77979561Siedowse return; 78079561Siedowse } 78179561Siedowse 78279561Siedowse /* 78379561Siedowse * Remove any `first free' information pertaining to the 78479561Siedowse * truncated blocks. All blocks we're removing should be 78579561Siedowse * completely unused. 78679561Siedowse */ 78779561Siedowse if (dh->dh_firstfree[DH_NFSTATS] >= block) 78879561Siedowse dh->dh_firstfree[DH_NFSTATS] = -1; 78979561Siedowse for (i = block; i < dh->dh_dirblks; i++) 79079561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 79179561Siedowse panic("ufsdirhash_dirtrunc: blocks in use"); 79279561Siedowse for (i = 0; i < DH_NFSTATS; i++) 79379561Siedowse if (dh->dh_firstfree[i] >= block) 79479561Siedowse panic("ufsdirhash_dirtrunc: first free corrupt"); 79579561Siedowse dh->dh_dirblks = block; 796125854Sdwmalone DIRHASH_UNLOCK(dh); 79779561Siedowse} 79879561Siedowse 79979561Siedowse/* 80079561Siedowse * Debugging function to check that the dirhash information about 80179561Siedowse * a directory block matches its actual contents. Panics if a mismatch 80279561Siedowse * is detected. 80379561Siedowse * 80479561Siedowse * On entry, `buf' should point to the start of an in-core 80579561Siedowse * DIRBLKSIZ-sized directory block, and `offset' should contain the 80679561Siedowse * offset from the start of the directory of that block. 80779561Siedowse */ 80879561Siedowsevoid 80979561Siedowseufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset) 81079561Siedowse{ 81179561Siedowse struct dirhash *dh; 81279561Siedowse struct direct *dp; 81379561Siedowse int block, ffslot, i, nfree; 81479561Siedowse 81579561Siedowse if (!ufs_dirhashcheck) 81679561Siedowse return; 81779561Siedowse if ((dh = ip->i_dirhash) == NULL) 81879561Siedowse return; 819125854Sdwmalone DIRHASH_LOCK(dh); 82079561Siedowse if (dh->dh_hash == NULL) { 821125854Sdwmalone DIRHASH_UNLOCK(dh); 82279561Siedowse ufsdirhash_free(ip); 82379561Siedowse return; 82479561Siedowse } 82579561Siedowse 82679561Siedowse block = offset / DIRBLKSIZ; 82779561Siedowse if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks) 82879561Siedowse panic("ufsdirhash_checkblock: bad offset"); 82979561Siedowse 83079561Siedowse nfree = 0; 83179561Siedowse for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) { 83279561Siedowse dp = (struct direct *)(buf + i); 83379561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ) 83479561Siedowse panic("ufsdirhash_checkblock: bad dir"); 83579561Siedowse 83679561Siedowse if (dp->d_ino == 0) { 83780456Siedowse#if 0 83880456Siedowse /* 83980456Siedowse * XXX entries with d_ino == 0 should only occur 84080456Siedowse * at the start of a DIRBLKSIZ block. However the 84180456Siedowse * ufs code is tolerant of such entries at other 84280456Siedowse * offsets, and fsck does not fix them. 84380456Siedowse */ 84479561Siedowse if (i != 0) 84579561Siedowse panic("ufsdirhash_checkblock: bad dir inode"); 84680456Siedowse#endif 84779561Siedowse nfree += dp->d_reclen; 84879561Siedowse continue; 84979561Siedowse } 85079561Siedowse 85179561Siedowse /* Check that the entry exists (will panic if it doesn't). */ 85279561Siedowse ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i); 85379561Siedowse 85479561Siedowse nfree += dp->d_reclen - DIRSIZ(0, dp); 85579561Siedowse } 85679561Siedowse if (i != DIRBLKSIZ) 85779561Siedowse panic("ufsdirhash_checkblock: bad dir end"); 85879561Siedowse 85979561Siedowse if (dh->dh_blkfree[block] * DIRALIGN != nfree) 86079561Siedowse panic("ufsdirhash_checkblock: bad free count"); 86179561Siedowse 86292098Siedowse ffslot = BLKFREE2IDX(nfree / DIRALIGN); 86379561Siedowse for (i = 0; i <= DH_NFSTATS; i++) 86479561Siedowse if (dh->dh_firstfree[i] == block && i != ffslot) 86579561Siedowse panic("ufsdirhash_checkblock: bad first-free"); 86692098Siedowse if (dh->dh_firstfree[ffslot] == -1) 86792098Siedowse panic("ufsdirhash_checkblock: missing first-free entry"); 868125854Sdwmalone DIRHASH_UNLOCK(dh); 86979561Siedowse} 87079561Siedowse 87179561Siedowse/* 87279561Siedowse * Hash the specified filename into a dirhash slot. 87379561Siedowse */ 87479561Siedowsestatic int 87579561Siedowseufsdirhash_hash(struct dirhash *dh, char *name, int namelen) 87679561Siedowse{ 87792807Sdwmalone u_int32_t hash; 87892807Sdwmalone 87992807Sdwmalone /* 880107868Siedowse * We hash the name and then some other bit of data that is 881107868Siedowse * invariant over the dirhash's lifetime. Otherwise names 88292807Sdwmalone * differing only in the last byte are placed close to one 88392807Sdwmalone * another in the table, which is bad for linear probing. 88492807Sdwmalone */ 88592807Sdwmalone hash = fnv_32_buf(name, namelen, FNV1_32_INIT); 88692807Sdwmalone hash = fnv_32_buf(dh, sizeof(dh), hash); 88792807Sdwmalone return (hash % dh->dh_hlen); 88879561Siedowse} 88979561Siedowse 89079561Siedowse/* 89179561Siedowse * Adjust the number of free bytes in the block containing `offset' 89279561Siedowse * by the value specified by `diff'. 89379561Siedowse * 89479561Siedowse * The caller must ensure we have exclusive access to `dh'; normally 89579561Siedowse * that means that dh_mtx should be held, but this is also called 89679561Siedowse * from ufsdirhash_build() where exclusive access can be assumed. 89779561Siedowse */ 89879561Siedowsestatic void 89979561Siedowseufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff) 90079561Siedowse{ 90179561Siedowse int block, i, nfidx, ofidx; 90279561Siedowse 90379561Siedowse /* Update the per-block summary info. */ 90479561Siedowse block = offset / DIRBLKSIZ; 90579561Siedowse KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks, 90679561Siedowse ("dirhash bad offset")); 90792098Siedowse ofidx = BLKFREE2IDX(dh->dh_blkfree[block]); 90879561Siedowse dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN); 90992098Siedowse nfidx = BLKFREE2IDX(dh->dh_blkfree[block]); 91079561Siedowse 91179561Siedowse /* Update the `first free' list if necessary. */ 91279561Siedowse if (ofidx != nfidx) { 91379561Siedowse /* If removing, scan forward for the next block. */ 91479561Siedowse if (dh->dh_firstfree[ofidx] == block) { 91579561Siedowse for (i = block + 1; i < dh->dh_dirblks; i++) 91692098Siedowse if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx) 91779561Siedowse break; 91879561Siedowse dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1; 91979561Siedowse } 92079561Siedowse 92179561Siedowse /* Make this the new `first free' if necessary */ 92279561Siedowse if (dh->dh_firstfree[nfidx] > block || 92379561Siedowse dh->dh_firstfree[nfidx] == -1) 92479561Siedowse dh->dh_firstfree[nfidx] = block; 92579561Siedowse } 92679561Siedowse} 92779561Siedowse 92879561Siedowse/* 92979561Siedowse * Find the specified name which should have the specified offset. 93079561Siedowse * Returns a slot number, and panics on failure. 93179561Siedowse * 93279561Siedowse * `dh' must be locked on entry and remains so on return. 93379561Siedowse */ 93479561Siedowsestatic int 93579561Siedowseufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset) 93679561Siedowse{ 93779561Siedowse int slot; 93879561Siedowse 93979561Siedowse mtx_assert(&dh->dh_mtx, MA_OWNED); 94079561Siedowse 94179561Siedowse /* Find the entry. */ 94279561Siedowse KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full")); 94379561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 94479561Siedowse while (DH_ENTRY(dh, slot) != offset && 94579561Siedowse DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 94679561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 94779561Siedowse if (DH_ENTRY(dh, slot) != offset) 94879561Siedowse panic("ufsdirhash_findslot: '%.*s' not found", namelen, name); 94979561Siedowse 95079561Siedowse return (slot); 95179561Siedowse} 95279561Siedowse 95379561Siedowse/* 95479561Siedowse * Remove the entry corresponding to the specified slot from the hash array. 95579561Siedowse * 95679561Siedowse * `dh' must be locked on entry and remains so on return. 95779561Siedowse */ 95879561Siedowsestatic void 95979561Siedowseufsdirhash_delslot(struct dirhash *dh, int slot) 96079561Siedowse{ 96179561Siedowse int i; 96279561Siedowse 96379561Siedowse mtx_assert(&dh->dh_mtx, MA_OWNED); 96479561Siedowse 96579561Siedowse /* Mark the entry as deleted. */ 96679561Siedowse DH_ENTRY(dh, slot) = DIRHASH_DEL; 96779561Siedowse 96879561Siedowse /* If this is the end of a chain of DIRHASH_DEL slots, remove them. */ 96979561Siedowse for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; ) 97079561Siedowse i = WRAPINCR(i, dh->dh_hlen); 97179561Siedowse if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) { 97292807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 97392807Sdwmalone while (DH_ENTRY(dh, i) == DIRHASH_DEL) { 97479561Siedowse DH_ENTRY(dh, i) = DIRHASH_EMPTY; 97579561Siedowse dh->dh_hused--; 97692807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 97779561Siedowse } 97879561Siedowse KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen")); 97979561Siedowse } 98079561Siedowse} 98179561Siedowse 98279561Siedowse/* 98379561Siedowse * Given a directory entry and its offset, find the offset of the 98479561Siedowse * previous entry in the same DIRBLKSIZ-sized block. Returns an 98579561Siedowse * offset, or -1 if there is no previous entry in the block or some 98679561Siedowse * other problem occurred. 98779561Siedowse */ 98879561Siedowsestatic doff_t 98979561Siedowseufsdirhash_getprev(struct direct *dirp, doff_t offset) 99079561Siedowse{ 99179561Siedowse struct direct *dp; 99279561Siedowse char *blkbuf; 99379561Siedowse doff_t blkoff, prevoff; 99479561Siedowse int entrypos, i; 99579561Siedowse 99679561Siedowse blkoff = offset & ~(DIRBLKSIZ - 1); /* offset of start of block */ 99779561Siedowse entrypos = offset & (DIRBLKSIZ - 1); /* entry relative to block */ 99879561Siedowse blkbuf = (char *)dirp - entrypos; 99979561Siedowse prevoff = blkoff; 100079561Siedowse 100179561Siedowse /* If `offset' is the start of a block, there is no previous entry. */ 100279561Siedowse if (entrypos == 0) 100379561Siedowse return (-1); 100479561Siedowse 100579561Siedowse /* Scan from the start of the block until we get to the entry. */ 100679561Siedowse for (i = 0; i < entrypos; i += dp->d_reclen) { 100779561Siedowse dp = (struct direct *)(blkbuf + i); 100879561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos) 100979561Siedowse return (-1); /* Corrupted directory. */ 101079561Siedowse prevoff = blkoff + i; 101179561Siedowse } 101279561Siedowse return (prevoff); 101379561Siedowse} 101479561Siedowse 101579561Siedowse/* 101679561Siedowse * Try to free up `wanted' bytes by stealing memory from existing 1017125854Sdwmalone * dirhashes. Returns zero with list locked if successful. 101879561Siedowse */ 101979561Siedowsestatic int 102079561Siedowseufsdirhash_recycle(int wanted) 102179561Siedowse{ 102279561Siedowse struct dirhash *dh; 102379561Siedowse doff_t **hash; 102479561Siedowse u_int8_t *blkfree; 102579561Siedowse int i, mem, narrays; 102679561Siedowse 1027125854Sdwmalone DIRHASHLIST_LOCK(); 102879561Siedowse while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) { 102979561Siedowse /* Find a dirhash, and lock it. */ 103079561Siedowse if ((dh = TAILQ_FIRST(&ufsdirhash_list)) == NULL) { 1031125854Sdwmalone DIRHASHLIST_UNLOCK(); 103279561Siedowse return (-1); 103379561Siedowse } 1034125854Sdwmalone DIRHASH_LOCK(dh); 103579561Siedowse KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list")); 103679561Siedowse 103779561Siedowse /* Decrement the score; only recycle if it becomes zero. */ 103879561Siedowse if (--dh->dh_score > 0) { 1039125854Sdwmalone DIRHASH_UNLOCK(dh); 1040125854Sdwmalone DIRHASHLIST_UNLOCK(); 104179561Siedowse return (-1); 104279561Siedowse } 104379561Siedowse 104479561Siedowse /* Remove it from the list and detach its memory. */ 104579561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 104679561Siedowse dh->dh_onlist = 0; 104779561Siedowse hash = dh->dh_hash; 104879561Siedowse dh->dh_hash = NULL; 104979561Siedowse blkfree = dh->dh_blkfree; 105079561Siedowse dh->dh_blkfree = NULL; 105179561Siedowse narrays = dh->dh_narrays; 105279561Siedowse mem = narrays * sizeof(*dh->dh_hash) + 105379561Siedowse narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 105479561Siedowse dh->dh_nblk * sizeof(*dh->dh_blkfree); 105579561Siedowse 105679561Siedowse /* Unlock everything, free the detached memory. */ 1057125854Sdwmalone DIRHASH_UNLOCK(dh); 1058125854Sdwmalone DIRHASHLIST_UNLOCK(); 105979561Siedowse for (i = 0; i < narrays; i++) 1060125854Sdwmalone DIRHASH_BLKFREE(hash[i]); 106179561Siedowse FREE(hash, M_DIRHASH); 106279561Siedowse FREE(blkfree, M_DIRHASH); 106379561Siedowse 106479561Siedowse /* Account for the returned memory, and repeat if necessary. */ 1065125854Sdwmalone DIRHASHLIST_LOCK(); 106679561Siedowse ufs_dirhashmem -= mem; 106779561Siedowse } 1068125854Sdwmalone /* Success; return with list locked. */ 106979561Siedowse return (0); 107079561Siedowse} 107179561Siedowse 107279561Siedowse 107399101Siedowsevoid 107479561Siedowseufsdirhash_init() 107579561Siedowse{ 107696874Siedowse ufsdirhash_zone = uma_zcreate("DIRHASH", DH_NBLKOFF * sizeof(doff_t), 107792768Sjeff NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 107893818Sjhb mtx_init(&ufsdirhash_mtx, "dirhash list", NULL, MTX_DEF); 107979561Siedowse TAILQ_INIT(&ufsdirhash_list); 108079561Siedowse} 108179561Siedowse 108299101Siedowsevoid 108399101Siedowseufsdirhash_uninit() 108499101Siedowse{ 108599101Siedowse KASSERT(TAILQ_EMPTY(&ufsdirhash_list), ("ufsdirhash_uninit")); 108699101Siedowse uma_zdestroy(ufsdirhash_zone); 108799101Siedowse mtx_destroy(&ufsdirhash_mtx); 108899101Siedowse} 108979561Siedowse 109079561Siedowse#endif /* UFS_DIRHASH */ 1091