ufs_dirhash.c revision 151897
1139825Simp/*- 2107868Siedowse * Copyright (c) 2001, 2002 Ian Dowse. All rights reserved. 379561Siedowse * 479561Siedowse * Redistribution and use in source and binary forms, with or without 579561Siedowse * modification, are permitted provided that the following conditions 679561Siedowse * are met: 779561Siedowse * 1. Redistributions of source code must retain the above copyright 879561Siedowse * notice, this list of conditions and the following disclaimer. 979561Siedowse * 2. Redistributions in binary form must reproduce the above copyright 1079561Siedowse * notice, this list of conditions and the following disclaimer in the 1179561Siedowse * documentation and/or other materials provided with the distribution. 1279561Siedowse * 1379561Siedowse * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1479561Siedowse * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1579561Siedowse * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1679561Siedowse * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1779561Siedowse * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1879561Siedowse * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1979561Siedowse * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2079561Siedowse * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2179561Siedowse * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2279561Siedowse * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2379561Siedowse * SUCH DAMAGE. 2479561Siedowse */ 25116192Sobrien 2679561Siedowse/* 2779561Siedowse * This implements a hash-based lookup scheme for UFS directories. 2879561Siedowse */ 2979561Siedowse 30116192Sobrien#include <sys/cdefs.h> 31116192Sobrien__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_dirhash.c 151897 2005-10-31 15:41:29Z rwatson $"); 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 65151897Srwatsonstatic MALLOC_DEFINE(M_DIRHASH, "ufs_dirhash", "UFS directory hash tables"); 6679561Siedowse 67141631Sphkstatic SYSCTL_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 } 191149178Siedowse mtx_init(&dh->dh_mtx, "dirhash", NULL, MTX_DEF); 19279561Siedowse MALLOC(dh->dh_hash, doff_t **, narrays * sizeof(dh->dh_hash[0]), 19379561Siedowse M_DIRHASH, M_NOWAIT | M_ZERO); 19479561Siedowse MALLOC(dh->dh_blkfree, u_int8_t *, nblocks * sizeof(dh->dh_blkfree[0]), 19579561Siedowse M_DIRHASH, M_NOWAIT); 19679561Siedowse if (dh->dh_hash == NULL || dh->dh_blkfree == NULL) 19779561Siedowse goto fail; 19879561Siedowse for (i = 0; i < narrays; i++) { 199125854Sdwmalone if ((dh->dh_hash[i] = DIRHASH_BLKALLOC_WAITOK()) == NULL) 20079561Siedowse goto fail; 20179561Siedowse for (j = 0; j < DH_NBLKOFF; j++) 20279561Siedowse dh->dh_hash[i][j] = DIRHASH_EMPTY; 20379561Siedowse } 20479561Siedowse 20579561Siedowse /* Initialise the hash table and block statistics. */ 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); 268149178Siedowse mtx_destroy(&dh->dh_mtx); 26979561Siedowse FREE(dh, M_DIRHASH); 27079561Siedowse ip->i_dirhash = NULL; 271125854Sdwmalone DIRHASHLIST_LOCK(); 27279561Siedowse ufs_dirhashmem -= memreqd; 273125854Sdwmalone DIRHASHLIST_UNLOCK(); 27479561Siedowse return (-1); 27579561Siedowse} 27679561Siedowse 27779561Siedowse/* 27879561Siedowse * Free any hash table associated with inode 'ip'. 27979561Siedowse */ 28079561Siedowsevoid 28179561Siedowseufsdirhash_free(struct inode *ip) 28279561Siedowse{ 28379561Siedowse struct dirhash *dh; 28479561Siedowse int i, mem; 28579561Siedowse 28679561Siedowse if ((dh = ip->i_dirhash) == NULL) 28779561Siedowse return; 288125854Sdwmalone DIRHASHLIST_LOCK(); 289125854Sdwmalone DIRHASH_LOCK(dh); 29079561Siedowse if (dh->dh_onlist) 29179561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 292125854Sdwmalone DIRHASH_UNLOCK(dh); 293125854Sdwmalone DIRHASHLIST_UNLOCK(); 29479561Siedowse 29579561Siedowse /* The dirhash pointed to by 'dh' is exclusively ours now. */ 29679561Siedowse 29779561Siedowse mem = sizeof(*dh); 29879561Siedowse if (dh->dh_hash != NULL) { 29979561Siedowse for (i = 0; i < dh->dh_narrays; i++) 300125854Sdwmalone DIRHASH_BLKFREE(dh->dh_hash[i]); 30179561Siedowse FREE(dh->dh_hash, M_DIRHASH); 30279561Siedowse FREE(dh->dh_blkfree, M_DIRHASH); 30379561Siedowse mem += dh->dh_narrays * sizeof(*dh->dh_hash) + 30479561Siedowse dh->dh_narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 30579561Siedowse dh->dh_nblk * sizeof(*dh->dh_blkfree); 30679561Siedowse } 30779561Siedowse mtx_destroy(&dh->dh_mtx); 30879561Siedowse FREE(dh, M_DIRHASH); 30979561Siedowse ip->i_dirhash = NULL; 31079561Siedowse 311125854Sdwmalone DIRHASHLIST_LOCK(); 31279561Siedowse ufs_dirhashmem -= mem; 313125854Sdwmalone DIRHASHLIST_UNLOCK(); 31479561Siedowse} 31579561Siedowse 31679561Siedowse/* 31779561Siedowse * Find the offset of the specified name within the given inode. 31879561Siedowse * Returns 0 on success, ENOENT if the entry does not exist, or 31979561Siedowse * EJUSTRETURN if the caller should revert to a linear search. 32079561Siedowse * 32179690Siedowse * If successful, the directory offset is stored in *offp, and a 32279690Siedowse * pointer to a struct buf containing the entry is stored in *bpp. If 32379561Siedowse * prevoffp is non-NULL, the offset of the previous entry within 32479561Siedowse * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry 32579561Siedowse * is the first in a block, the start of the block is used). 32679561Siedowse */ 32779561Siedowseint 32879561Siedowseufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp, 32979690Siedowse struct buf **bpp, doff_t *prevoffp) 33079561Siedowse{ 33179561Siedowse struct dirhash *dh, *dh_next; 33279561Siedowse struct direct *dp; 33379561Siedowse struct vnode *vp; 33479561Siedowse struct buf *bp; 33579561Siedowse doff_t blkoff, bmask, offset, prevoff; 33679561Siedowse int i, slot; 33779561Siedowse 33879561Siedowse if ((dh = ip->i_dirhash) == NULL) 33979561Siedowse return (EJUSTRETURN); 34079561Siedowse /* 34179561Siedowse * Move this dirhash towards the end of the list if it has a 342107868Siedowse * score higher than the next entry, and acquire the dh_mtx. 34379561Siedowse * Optimise the case where it's already the last by performing 34479561Siedowse * an unlocked read of the TAILQ_NEXT pointer. 34579561Siedowse * 34679561Siedowse * In both cases, end up holding just dh_mtx. 34779561Siedowse */ 34879561Siedowse if (TAILQ_NEXT(dh, dh_list) != NULL) { 349125854Sdwmalone DIRHASHLIST_LOCK(); 350125854Sdwmalone DIRHASH_LOCK(dh); 35179561Siedowse /* 35279561Siedowse * If the new score will be greater than that of the next 35379561Siedowse * entry, then move this entry past it. With both mutexes 35479561Siedowse * held, dh_next won't go away, but its dh_score could 35579561Siedowse * change; that's not important since it is just a hint. 35679561Siedowse */ 35779561Siedowse if (dh->dh_hash != NULL && 35879561Siedowse (dh_next = TAILQ_NEXT(dh, dh_list)) != NULL && 35979561Siedowse dh->dh_score >= dh_next->dh_score) { 36079561Siedowse KASSERT(dh->dh_onlist, ("dirhash: not on list")); 36179561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 36279561Siedowse TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh, 36379561Siedowse dh_list); 36479561Siedowse } 365125854Sdwmalone DIRHASHLIST_UNLOCK(); 36679561Siedowse } else { 36779561Siedowse /* Already the last, though that could change as we wait. */ 368125854Sdwmalone DIRHASH_LOCK(dh); 36979561Siedowse } 37079561Siedowse if (dh->dh_hash == NULL) { 371125854Sdwmalone DIRHASH_UNLOCK(dh); 37279561Siedowse ufsdirhash_free(ip); 37379561Siedowse return (EJUSTRETURN); 37479561Siedowse } 37579561Siedowse 37679561Siedowse /* Update the score. */ 37779561Siedowse if (dh->dh_score < DH_SCOREMAX) 37879561Siedowse dh->dh_score++; 37979561Siedowse 38079561Siedowse vp = ip->i_vnode; 38179561Siedowse bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 38279561Siedowse blkoff = -1; 38379561Siedowse bp = NULL; 38479561Siedowserestart: 38579561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 38679561Siedowse 38779561Siedowse if (dh->dh_seqopt) { 38879561Siedowse /* 38979561Siedowse * Sequential access optimisation. dh_seqoff contains the 39079561Siedowse * offset of the directory entry immediately following 39179561Siedowse * the last entry that was looked up. Check if this offset 39279561Siedowse * appears in the hash chain for the name we are looking for. 39379561Siedowse */ 39479561Siedowse for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY; 39579561Siedowse i = WRAPINCR(i, dh->dh_hlen)) 39686350Siedowse if (offset == dh->dh_seqoff) 39779561Siedowse break; 39879561Siedowse if (offset == dh->dh_seqoff) { 39979561Siedowse /* 40079561Siedowse * We found an entry with the expected offset. This 40179561Siedowse * is probably the entry we want, but if not, the 402149178Siedowse * code below will turn off seqopt and retry. 40379561Siedowse */ 40479561Siedowse slot = i; 40579561Siedowse } else 40679561Siedowse dh->dh_seqopt = 0; 40779561Siedowse } 40879561Siedowse 40979561Siedowse for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY; 41079561Siedowse slot = WRAPINCR(slot, dh->dh_hlen)) { 41179561Siedowse if (offset == DIRHASH_DEL) 41279561Siedowse continue; 413125854Sdwmalone DIRHASH_UNLOCK(dh); 41479561Siedowse 41579561Siedowse if (offset < 0 || offset >= ip->i_size) 41679561Siedowse panic("ufsdirhash_lookup: bad offset in hash array"); 41779561Siedowse if ((offset & ~bmask) != blkoff) { 41879561Siedowse if (bp != NULL) 41979561Siedowse brelse(bp); 42079561Siedowse blkoff = offset & ~bmask; 42179561Siedowse if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) 42279561Siedowse return (EJUSTRETURN); 42379561Siedowse } 42479561Siedowse dp = (struct direct *)(bp->b_data + (offset & bmask)); 42579561Siedowse if (dp->d_reclen == 0 || dp->d_reclen > 42679561Siedowse DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) { 42779561Siedowse /* Corrupted directory. */ 42879561Siedowse brelse(bp); 42979561Siedowse return (EJUSTRETURN); 43079561Siedowse } 43179561Siedowse if (dp->d_namlen == namelen && 43279561Siedowse bcmp(dp->d_name, name, namelen) == 0) { 43379561Siedowse /* Found. Get the prev offset if needed. */ 43479561Siedowse if (prevoffp != NULL) { 43579561Siedowse if (offset & (DIRBLKSIZ - 1)) { 43679561Siedowse prevoff = ufsdirhash_getprev(dp, 43779561Siedowse offset); 43879561Siedowse if (prevoff == -1) { 43979561Siedowse brelse(bp); 44079561Siedowse return (EJUSTRETURN); 44179561Siedowse } 44279561Siedowse } else 44379561Siedowse prevoff = offset; 44479561Siedowse *prevoffp = prevoff; 44579561Siedowse } 44679561Siedowse 44779561Siedowse /* Check for sequential access, and update offset. */ 44879561Siedowse if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset) 44979561Siedowse dh->dh_seqopt = 1; 45079561Siedowse dh->dh_seqoff = offset + DIRSIZ(0, dp); 45179561Siedowse 45279690Siedowse *bpp = bp; 45379561Siedowse *offp = offset; 45479561Siedowse return (0); 45579561Siedowse } 45679561Siedowse 457125854Sdwmalone DIRHASH_LOCK(dh); 45879561Siedowse if (dh->dh_hash == NULL) { 459125854Sdwmalone DIRHASH_UNLOCK(dh); 46079561Siedowse if (bp != NULL) 46179561Siedowse brelse(bp); 46279561Siedowse ufsdirhash_free(ip); 46379561Siedowse return (EJUSTRETURN); 46479561Siedowse } 46579561Siedowse /* 46679561Siedowse * When the name doesn't match in the seqopt case, go back 46779561Siedowse * and search normally. 46879561Siedowse */ 46979561Siedowse if (dh->dh_seqopt) { 47079561Siedowse dh->dh_seqopt = 0; 47179561Siedowse goto restart; 47279561Siedowse } 47379561Siedowse } 474125854Sdwmalone DIRHASH_UNLOCK(dh); 47579561Siedowse if (bp != NULL) 47679561Siedowse brelse(bp); 47779561Siedowse return (ENOENT); 47879561Siedowse} 47979561Siedowse 48079561Siedowse/* 48179561Siedowse * Find a directory block with room for 'slotneeded' bytes. Returns 48279561Siedowse * the offset of the directory entry that begins the free space. 48379561Siedowse * This will either be the offset of an existing entry that has free 48479561Siedowse * space at the end, or the offset of an entry with d_ino == 0 at 48579561Siedowse * the start of a DIRBLKSIZ block. 48679561Siedowse * 48779561Siedowse * To use the space, the caller may need to compact existing entries in 48879561Siedowse * the directory. The total number of bytes in all of the entries involved 48979561Siedowse * in the compaction is stored in *slotsize. In other words, all of 49079561Siedowse * the entries that must be compacted are exactly contained in the 49179561Siedowse * region beginning at the returned offset and spanning *slotsize bytes. 49279561Siedowse * 49379561Siedowse * Returns -1 if no space was found, indicating that the directory 49479561Siedowse * must be extended. 49579561Siedowse */ 49679561Siedowsedoff_t 49779561Siedowseufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize) 49879561Siedowse{ 49979561Siedowse struct direct *dp; 50079561Siedowse struct dirhash *dh; 50179561Siedowse struct buf *bp; 50279561Siedowse doff_t pos, slotstart; 50379561Siedowse int dirblock, error, freebytes, i; 50479561Siedowse 50579561Siedowse if ((dh = ip->i_dirhash) == NULL) 50679561Siedowse return (-1); 507125854Sdwmalone DIRHASH_LOCK(dh); 50879561Siedowse if (dh->dh_hash == NULL) { 509125854Sdwmalone DIRHASH_UNLOCK(dh); 51079561Siedowse ufsdirhash_free(ip); 51179561Siedowse return (-1); 51279561Siedowse } 51379561Siedowse 51479561Siedowse /* Find a directory block with the desired free space. */ 51579561Siedowse dirblock = -1; 51679561Siedowse for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++) 51779561Siedowse if ((dirblock = dh->dh_firstfree[i]) != -1) 51879561Siedowse break; 51979561Siedowse if (dirblock == -1) { 520125854Sdwmalone DIRHASH_UNLOCK(dh); 52179561Siedowse return (-1); 52279561Siedowse } 52379561Siedowse 52479561Siedowse KASSERT(dirblock < dh->dh_nblk && 52579561Siedowse dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN), 52679561Siedowse ("ufsdirhash_findfree: bad stats")); 527125854Sdwmalone DIRHASH_UNLOCK(dh); 52879561Siedowse pos = dirblock * DIRBLKSIZ; 52979561Siedowse error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp); 53079561Siedowse if (error) 53179561Siedowse return (-1); 53279561Siedowse 53379561Siedowse /* Find the first entry with free space. */ 53479561Siedowse for (i = 0; i < DIRBLKSIZ; ) { 53579561Siedowse if (dp->d_reclen == 0) { 53679561Siedowse brelse(bp); 53779561Siedowse return (-1); 53879561Siedowse } 53979561Siedowse if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp)) 54079561Siedowse break; 54179561Siedowse i += dp->d_reclen; 54279561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 54379561Siedowse } 54479561Siedowse if (i > DIRBLKSIZ) { 54579561Siedowse brelse(bp); 54679561Siedowse return (-1); 54779561Siedowse } 54879561Siedowse slotstart = pos + i; 54979561Siedowse 55079561Siedowse /* Find the range of entries needed to get enough space */ 55179561Siedowse freebytes = 0; 55279561Siedowse while (i < DIRBLKSIZ && freebytes < slotneeded) { 55379561Siedowse freebytes += dp->d_reclen; 55479561Siedowse if (dp->d_ino != 0) 55579561Siedowse freebytes -= DIRSIZ(0, dp); 55679561Siedowse if (dp->d_reclen == 0) { 55779561Siedowse brelse(bp); 55879561Siedowse return (-1); 55979561Siedowse } 56079561Siedowse i += dp->d_reclen; 56179561Siedowse dp = (struct direct *)((char *)dp + dp->d_reclen); 56279561Siedowse } 56379561Siedowse if (i > DIRBLKSIZ) { 56479561Siedowse brelse(bp); 56579561Siedowse return (-1); 56679561Siedowse } 56779561Siedowse if (freebytes < slotneeded) 56879561Siedowse panic("ufsdirhash_findfree: free mismatch"); 56979561Siedowse brelse(bp); 57079561Siedowse *slotsize = pos + i - slotstart; 57179561Siedowse return (slotstart); 57279561Siedowse} 57379561Siedowse 57479561Siedowse/* 57579561Siedowse * Return the start of the unused space at the end of a directory, or 57679561Siedowse * -1 if there are no trailing unused blocks. 57779561Siedowse */ 57879561Siedowsedoff_t 57979561Siedowseufsdirhash_enduseful(struct inode *ip) 58079561Siedowse{ 58179561Siedowse 58279561Siedowse struct dirhash *dh; 58379561Siedowse int i; 58479561Siedowse 58579561Siedowse if ((dh = ip->i_dirhash) == NULL) 58679561Siedowse return (-1); 587125854Sdwmalone DIRHASH_LOCK(dh); 58879561Siedowse if (dh->dh_hash == NULL) { 589125854Sdwmalone DIRHASH_UNLOCK(dh); 59079561Siedowse ufsdirhash_free(ip); 59179561Siedowse return (-1); 59279561Siedowse } 59379561Siedowse 59479561Siedowse if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN) { 595125854Sdwmalone DIRHASH_UNLOCK(dh); 59679561Siedowse return (-1); 59779561Siedowse } 59879561Siedowse 59979561Siedowse for (i = dh->dh_dirblks - 1; i >= 0; i--) 60079561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 60179561Siedowse break; 602125854Sdwmalone DIRHASH_UNLOCK(dh); 60379561Siedowse return ((doff_t)(i + 1) * DIRBLKSIZ); 60479561Siedowse} 60579561Siedowse 60679561Siedowse/* 60779561Siedowse * Insert information into the hash about a new directory entry. dirp 60879561Siedowse * points to a struct direct containing the entry, and offset specifies 60979561Siedowse * the offset of this entry. 61079561Siedowse */ 61179561Siedowsevoid 61279561Siedowseufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset) 61379561Siedowse{ 61479561Siedowse struct dirhash *dh; 61579561Siedowse int slot; 61679561Siedowse 61779561Siedowse if ((dh = ip->i_dirhash) == NULL) 61879561Siedowse return; 619125854Sdwmalone DIRHASH_LOCK(dh); 62079561Siedowse if (dh->dh_hash == NULL) { 621125854Sdwmalone DIRHASH_UNLOCK(dh); 62279561Siedowse ufsdirhash_free(ip); 62379561Siedowse return; 62479561Siedowse } 62579561Siedowse 62679561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 62779561Siedowse ("ufsdirhash_add: bad offset")); 62879561Siedowse /* 62979561Siedowse * Normal hash usage is < 66%. If the usage gets too high then 63079561Siedowse * remove the hash entirely and let it be rebuilt later. 63179561Siedowse */ 63279561Siedowse if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) { 633125854Sdwmalone DIRHASH_UNLOCK(dh); 63479561Siedowse ufsdirhash_free(ip); 63579561Siedowse return; 63679561Siedowse } 63779561Siedowse 63879561Siedowse /* Find a free hash slot (empty or deleted), and add the entry. */ 63979561Siedowse slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen); 64079561Siedowse while (DH_ENTRY(dh, slot) >= 0) 64179561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 64279561Siedowse if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY) 64379561Siedowse dh->dh_hused++; 64479561Siedowse DH_ENTRY(dh, slot) = offset; 64579561Siedowse 64679561Siedowse /* Update the per-block summary info. */ 64779561Siedowse ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp)); 648125854Sdwmalone DIRHASH_UNLOCK(dh); 64979561Siedowse} 65079561Siedowse 65179561Siedowse/* 65279561Siedowse * Remove the specified directory entry from the hash. The entry to remove 65379561Siedowse * is defined by the name in `dirp', which must exist at the specified 65479561Siedowse * `offset' within the directory. 65579561Siedowse */ 65679561Siedowsevoid 65779561Siedowseufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset) 65879561Siedowse{ 65979561Siedowse struct dirhash *dh; 66079561Siedowse int slot; 66179561Siedowse 66279561Siedowse if ((dh = ip->i_dirhash) == NULL) 66379561Siedowse return; 664125854Sdwmalone DIRHASH_LOCK(dh); 66579561Siedowse if (dh->dh_hash == NULL) { 666125854Sdwmalone DIRHASH_UNLOCK(dh); 66779561Siedowse ufsdirhash_free(ip); 66879561Siedowse return; 66979561Siedowse } 67079561Siedowse 67179561Siedowse KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ, 67279561Siedowse ("ufsdirhash_remove: bad offset")); 67379561Siedowse /* Find the entry */ 67479561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset); 67579561Siedowse 67679561Siedowse /* Remove the hash entry. */ 67779561Siedowse ufsdirhash_delslot(dh, slot); 67879561Siedowse 67979561Siedowse /* Update the per-block summary info. */ 68079561Siedowse ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp)); 681125854Sdwmalone DIRHASH_UNLOCK(dh); 68279561Siedowse} 68379561Siedowse 68479561Siedowse/* 68579561Siedowse * Change the offset associated with a directory entry in the hash. Used 68679561Siedowse * when compacting directory blocks. 68779561Siedowse */ 68879561Siedowsevoid 68979561Siedowseufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff, 69079561Siedowse doff_t newoff) 69179561Siedowse{ 69279561Siedowse struct dirhash *dh; 69379561Siedowse int slot; 69479561Siedowse 69579561Siedowse if ((dh = ip->i_dirhash) == NULL) 69679561Siedowse return; 697125854Sdwmalone DIRHASH_LOCK(dh); 69879561Siedowse if (dh->dh_hash == NULL) { 699125854Sdwmalone DIRHASH_UNLOCK(dh); 70079561Siedowse ufsdirhash_free(ip); 70179561Siedowse return; 70279561Siedowse } 70379561Siedowse 70479561Siedowse KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ && 70579561Siedowse newoff < dh->dh_dirblks * DIRBLKSIZ, 70679561Siedowse ("ufsdirhash_move: bad offset")); 70779561Siedowse /* Find the entry, and update the offset. */ 70879561Siedowse slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff); 70979561Siedowse DH_ENTRY(dh, slot) = newoff; 710125854Sdwmalone DIRHASH_UNLOCK(dh); 71179561Siedowse} 71279561Siedowse 71379561Siedowse/* 71479561Siedowse * Inform dirhash that the directory has grown by one block that 71579561Siedowse * begins at offset (i.e. the new length is offset + DIRBLKSIZ). 71679561Siedowse */ 71779561Siedowsevoid 71879561Siedowseufsdirhash_newblk(struct inode *ip, doff_t offset) 71979561Siedowse{ 72079561Siedowse struct dirhash *dh; 72179561Siedowse int block; 72279561Siedowse 72379561Siedowse if ((dh = ip->i_dirhash) == NULL) 72479561Siedowse return; 725125854Sdwmalone DIRHASH_LOCK(dh); 72679561Siedowse if (dh->dh_hash == NULL) { 727125854Sdwmalone DIRHASH_UNLOCK(dh); 72879561Siedowse ufsdirhash_free(ip); 72979561Siedowse return; 73079561Siedowse } 73179561Siedowse 73279561Siedowse KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ, 73379561Siedowse ("ufsdirhash_newblk: bad offset")); 73479561Siedowse block = offset / DIRBLKSIZ; 73579561Siedowse if (block >= dh->dh_nblk) { 73679561Siedowse /* Out of space; must rebuild. */ 737125854Sdwmalone DIRHASH_UNLOCK(dh); 73879561Siedowse ufsdirhash_free(ip); 73979561Siedowse return; 74079561Siedowse } 74179561Siedowse dh->dh_dirblks = block + 1; 74279561Siedowse 74379561Siedowse /* Account for the new free block. */ 74479561Siedowse dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN; 74579561Siedowse if (dh->dh_firstfree[DH_NFSTATS] == -1) 74679561Siedowse dh->dh_firstfree[DH_NFSTATS] = block; 747125854Sdwmalone DIRHASH_UNLOCK(dh); 74879561Siedowse} 74979561Siedowse 75079561Siedowse/* 75179561Siedowse * Inform dirhash that the directory is being truncated. 75279561Siedowse */ 75379561Siedowsevoid 75479561Siedowseufsdirhash_dirtrunc(struct inode *ip, doff_t offset) 75579561Siedowse{ 75679561Siedowse struct dirhash *dh; 75779561Siedowse int block, i; 75879561Siedowse 75979561Siedowse if ((dh = ip->i_dirhash) == NULL) 76079561Siedowse return; 761125854Sdwmalone DIRHASH_LOCK(dh); 76279561Siedowse if (dh->dh_hash == NULL) { 763125854Sdwmalone DIRHASH_UNLOCK(dh); 76479561Siedowse ufsdirhash_free(ip); 76579561Siedowse return; 76679561Siedowse } 76779561Siedowse 76879561Siedowse KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ, 76979561Siedowse ("ufsdirhash_dirtrunc: bad offset")); 77079561Siedowse block = howmany(offset, DIRBLKSIZ); 77179561Siedowse /* 77279561Siedowse * If the directory shrinks to less than 1/8 of dh_nblk blocks 77379561Siedowse * (about 20% of its original size due to the 50% extra added in 77479561Siedowse * ufsdirhash_build) then free it, and let the caller rebuild 77579561Siedowse * if necessary. 77679561Siedowse */ 77779561Siedowse if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) { 778125854Sdwmalone DIRHASH_UNLOCK(dh); 77979561Siedowse ufsdirhash_free(ip); 78079561Siedowse return; 78179561Siedowse } 78279561Siedowse 78379561Siedowse /* 78479561Siedowse * Remove any `first free' information pertaining to the 78579561Siedowse * truncated blocks. All blocks we're removing should be 78679561Siedowse * completely unused. 78779561Siedowse */ 78879561Siedowse if (dh->dh_firstfree[DH_NFSTATS] >= block) 78979561Siedowse dh->dh_firstfree[DH_NFSTATS] = -1; 79079561Siedowse for (i = block; i < dh->dh_dirblks; i++) 79179561Siedowse if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN) 79279561Siedowse panic("ufsdirhash_dirtrunc: blocks in use"); 79379561Siedowse for (i = 0; i < DH_NFSTATS; i++) 79479561Siedowse if (dh->dh_firstfree[i] >= block) 79579561Siedowse panic("ufsdirhash_dirtrunc: first free corrupt"); 79679561Siedowse dh->dh_dirblks = block; 797125854Sdwmalone DIRHASH_UNLOCK(dh); 79879561Siedowse} 79979561Siedowse 80079561Siedowse/* 80179561Siedowse * Debugging function to check that the dirhash information about 80279561Siedowse * a directory block matches its actual contents. Panics if a mismatch 80379561Siedowse * is detected. 80479561Siedowse * 80579561Siedowse * On entry, `buf' should point to the start of an in-core 80679561Siedowse * DIRBLKSIZ-sized directory block, and `offset' should contain the 80779561Siedowse * offset from the start of the directory of that block. 80879561Siedowse */ 80979561Siedowsevoid 81079561Siedowseufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset) 81179561Siedowse{ 81279561Siedowse struct dirhash *dh; 81379561Siedowse struct direct *dp; 81479561Siedowse int block, ffslot, i, nfree; 81579561Siedowse 81679561Siedowse if (!ufs_dirhashcheck) 81779561Siedowse return; 81879561Siedowse if ((dh = ip->i_dirhash) == NULL) 81979561Siedowse return; 820125854Sdwmalone DIRHASH_LOCK(dh); 82179561Siedowse if (dh->dh_hash == NULL) { 822125854Sdwmalone DIRHASH_UNLOCK(dh); 82379561Siedowse ufsdirhash_free(ip); 82479561Siedowse return; 82579561Siedowse } 82679561Siedowse 82779561Siedowse block = offset / DIRBLKSIZ; 82879561Siedowse if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks) 82979561Siedowse panic("ufsdirhash_checkblock: bad offset"); 83079561Siedowse 83179561Siedowse nfree = 0; 83279561Siedowse for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) { 83379561Siedowse dp = (struct direct *)(buf + i); 83479561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ) 83579561Siedowse panic("ufsdirhash_checkblock: bad dir"); 83679561Siedowse 83779561Siedowse if (dp->d_ino == 0) { 83880456Siedowse#if 0 83980456Siedowse /* 84080456Siedowse * XXX entries with d_ino == 0 should only occur 84180456Siedowse * at the start of a DIRBLKSIZ block. However the 84280456Siedowse * ufs code is tolerant of such entries at other 84380456Siedowse * offsets, and fsck does not fix them. 84480456Siedowse */ 84579561Siedowse if (i != 0) 84679561Siedowse panic("ufsdirhash_checkblock: bad dir inode"); 84780456Siedowse#endif 84879561Siedowse nfree += dp->d_reclen; 84979561Siedowse continue; 85079561Siedowse } 85179561Siedowse 85279561Siedowse /* Check that the entry exists (will panic if it doesn't). */ 85379561Siedowse ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i); 85479561Siedowse 85579561Siedowse nfree += dp->d_reclen - DIRSIZ(0, dp); 85679561Siedowse } 85779561Siedowse if (i != DIRBLKSIZ) 85879561Siedowse panic("ufsdirhash_checkblock: bad dir end"); 85979561Siedowse 86079561Siedowse if (dh->dh_blkfree[block] * DIRALIGN != nfree) 86179561Siedowse panic("ufsdirhash_checkblock: bad free count"); 86279561Siedowse 86392098Siedowse ffslot = BLKFREE2IDX(nfree / DIRALIGN); 86479561Siedowse for (i = 0; i <= DH_NFSTATS; i++) 86579561Siedowse if (dh->dh_firstfree[i] == block && i != ffslot) 86679561Siedowse panic("ufsdirhash_checkblock: bad first-free"); 86792098Siedowse if (dh->dh_firstfree[ffslot] == -1) 86892098Siedowse panic("ufsdirhash_checkblock: missing first-free entry"); 869125854Sdwmalone DIRHASH_UNLOCK(dh); 87079561Siedowse} 87179561Siedowse 87279561Siedowse/* 87379561Siedowse * Hash the specified filename into a dirhash slot. 87479561Siedowse */ 87579561Siedowsestatic int 87679561Siedowseufsdirhash_hash(struct dirhash *dh, char *name, int namelen) 87779561Siedowse{ 87892807Sdwmalone u_int32_t hash; 87992807Sdwmalone 88092807Sdwmalone /* 881107868Siedowse * We hash the name and then some other bit of data that is 882107868Siedowse * invariant over the dirhash's lifetime. Otherwise names 88392807Sdwmalone * differing only in the last byte are placed close to one 88492807Sdwmalone * another in the table, which is bad for linear probing. 88592807Sdwmalone */ 88692807Sdwmalone hash = fnv_32_buf(name, namelen, FNV1_32_INIT); 887133837Sdwmalone hash = fnv_32_buf(&dh, sizeof(dh), hash); 88892807Sdwmalone return (hash % dh->dh_hlen); 88979561Siedowse} 89079561Siedowse 89179561Siedowse/* 89279561Siedowse * Adjust the number of free bytes in the block containing `offset' 89379561Siedowse * by the value specified by `diff'. 89479561Siedowse * 89579561Siedowse * The caller must ensure we have exclusive access to `dh'; normally 89679561Siedowse * that means that dh_mtx should be held, but this is also called 89779561Siedowse * from ufsdirhash_build() where exclusive access can be assumed. 89879561Siedowse */ 89979561Siedowsestatic void 90079561Siedowseufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff) 90179561Siedowse{ 90279561Siedowse int block, i, nfidx, ofidx; 90379561Siedowse 90479561Siedowse /* Update the per-block summary info. */ 90579561Siedowse block = offset / DIRBLKSIZ; 90679561Siedowse KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks, 90779561Siedowse ("dirhash bad offset")); 90892098Siedowse ofidx = BLKFREE2IDX(dh->dh_blkfree[block]); 90979561Siedowse dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN); 91092098Siedowse nfidx = BLKFREE2IDX(dh->dh_blkfree[block]); 91179561Siedowse 91279561Siedowse /* Update the `first free' list if necessary. */ 91379561Siedowse if (ofidx != nfidx) { 91479561Siedowse /* If removing, scan forward for the next block. */ 91579561Siedowse if (dh->dh_firstfree[ofidx] == block) { 91679561Siedowse for (i = block + 1; i < dh->dh_dirblks; i++) 91792098Siedowse if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx) 91879561Siedowse break; 91979561Siedowse dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1; 92079561Siedowse } 92179561Siedowse 92279561Siedowse /* Make this the new `first free' if necessary */ 92379561Siedowse if (dh->dh_firstfree[nfidx] > block || 92479561Siedowse dh->dh_firstfree[nfidx] == -1) 92579561Siedowse dh->dh_firstfree[nfidx] = block; 92679561Siedowse } 92779561Siedowse} 92879561Siedowse 92979561Siedowse/* 93079561Siedowse * Find the specified name which should have the specified offset. 93179561Siedowse * Returns a slot number, and panics on failure. 93279561Siedowse * 93379561Siedowse * `dh' must be locked on entry and remains so on return. 93479561Siedowse */ 93579561Siedowsestatic int 93679561Siedowseufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset) 93779561Siedowse{ 93879561Siedowse int slot; 93979561Siedowse 94079561Siedowse mtx_assert(&dh->dh_mtx, MA_OWNED); 94179561Siedowse 94279561Siedowse /* Find the entry. */ 94379561Siedowse KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full")); 94479561Siedowse slot = ufsdirhash_hash(dh, name, namelen); 94579561Siedowse while (DH_ENTRY(dh, slot) != offset && 94679561Siedowse DH_ENTRY(dh, slot) != DIRHASH_EMPTY) 94779561Siedowse slot = WRAPINCR(slot, dh->dh_hlen); 94879561Siedowse if (DH_ENTRY(dh, slot) != offset) 94979561Siedowse panic("ufsdirhash_findslot: '%.*s' not found", namelen, name); 95079561Siedowse 95179561Siedowse return (slot); 95279561Siedowse} 95379561Siedowse 95479561Siedowse/* 95579561Siedowse * Remove the entry corresponding to the specified slot from the hash array. 95679561Siedowse * 95779561Siedowse * `dh' must be locked on entry and remains so on return. 95879561Siedowse */ 95979561Siedowsestatic void 96079561Siedowseufsdirhash_delslot(struct dirhash *dh, int slot) 96179561Siedowse{ 96279561Siedowse int i; 96379561Siedowse 96479561Siedowse mtx_assert(&dh->dh_mtx, MA_OWNED); 96579561Siedowse 96679561Siedowse /* Mark the entry as deleted. */ 96779561Siedowse DH_ENTRY(dh, slot) = DIRHASH_DEL; 96879561Siedowse 96979561Siedowse /* If this is the end of a chain of DIRHASH_DEL slots, remove them. */ 97079561Siedowse for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; ) 97179561Siedowse i = WRAPINCR(i, dh->dh_hlen); 97279561Siedowse if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) { 97392807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 97492807Sdwmalone while (DH_ENTRY(dh, i) == DIRHASH_DEL) { 97579561Siedowse DH_ENTRY(dh, i) = DIRHASH_EMPTY; 97679561Siedowse dh->dh_hused--; 97792807Sdwmalone i = WRAPDECR(i, dh->dh_hlen); 97879561Siedowse } 97979561Siedowse KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen")); 98079561Siedowse } 98179561Siedowse} 98279561Siedowse 98379561Siedowse/* 98479561Siedowse * Given a directory entry and its offset, find the offset of the 98579561Siedowse * previous entry in the same DIRBLKSIZ-sized block. Returns an 98679561Siedowse * offset, or -1 if there is no previous entry in the block or some 98779561Siedowse * other problem occurred. 98879561Siedowse */ 98979561Siedowsestatic doff_t 99079561Siedowseufsdirhash_getprev(struct direct *dirp, doff_t offset) 99179561Siedowse{ 99279561Siedowse struct direct *dp; 99379561Siedowse char *blkbuf; 99479561Siedowse doff_t blkoff, prevoff; 99579561Siedowse int entrypos, i; 99679561Siedowse 99779561Siedowse blkoff = offset & ~(DIRBLKSIZ - 1); /* offset of start of block */ 99879561Siedowse entrypos = offset & (DIRBLKSIZ - 1); /* entry relative to block */ 99979561Siedowse blkbuf = (char *)dirp - entrypos; 100079561Siedowse prevoff = blkoff; 100179561Siedowse 100279561Siedowse /* If `offset' is the start of a block, there is no previous entry. */ 100379561Siedowse if (entrypos == 0) 100479561Siedowse return (-1); 100579561Siedowse 100679561Siedowse /* Scan from the start of the block until we get to the entry. */ 100779561Siedowse for (i = 0; i < entrypos; i += dp->d_reclen) { 100879561Siedowse dp = (struct direct *)(blkbuf + i); 100979561Siedowse if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos) 101079561Siedowse return (-1); /* Corrupted directory. */ 101179561Siedowse prevoff = blkoff + i; 101279561Siedowse } 101379561Siedowse return (prevoff); 101479561Siedowse} 101579561Siedowse 101679561Siedowse/* 101779561Siedowse * Try to free up `wanted' bytes by stealing memory from existing 1018125854Sdwmalone * dirhashes. Returns zero with list locked if successful. 101979561Siedowse */ 102079561Siedowsestatic int 102179561Siedowseufsdirhash_recycle(int wanted) 102279561Siedowse{ 102379561Siedowse struct dirhash *dh; 102479561Siedowse doff_t **hash; 102579561Siedowse u_int8_t *blkfree; 102679561Siedowse int i, mem, narrays; 102779561Siedowse 1028125854Sdwmalone DIRHASHLIST_LOCK(); 102979561Siedowse while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) { 103079561Siedowse /* Find a dirhash, and lock it. */ 103179561Siedowse if ((dh = TAILQ_FIRST(&ufsdirhash_list)) == NULL) { 1032125854Sdwmalone DIRHASHLIST_UNLOCK(); 103379561Siedowse return (-1); 103479561Siedowse } 1035125854Sdwmalone DIRHASH_LOCK(dh); 103679561Siedowse KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list")); 103779561Siedowse 103879561Siedowse /* Decrement the score; only recycle if it becomes zero. */ 103979561Siedowse if (--dh->dh_score > 0) { 1040125854Sdwmalone DIRHASH_UNLOCK(dh); 1041125854Sdwmalone DIRHASHLIST_UNLOCK(); 104279561Siedowse return (-1); 104379561Siedowse } 104479561Siedowse 104579561Siedowse /* Remove it from the list and detach its memory. */ 104679561Siedowse TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); 104779561Siedowse dh->dh_onlist = 0; 104879561Siedowse hash = dh->dh_hash; 104979561Siedowse dh->dh_hash = NULL; 105079561Siedowse blkfree = dh->dh_blkfree; 105179561Siedowse dh->dh_blkfree = NULL; 105279561Siedowse narrays = dh->dh_narrays; 105379561Siedowse mem = narrays * sizeof(*dh->dh_hash) + 105479561Siedowse narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + 105579561Siedowse dh->dh_nblk * sizeof(*dh->dh_blkfree); 105679561Siedowse 105779561Siedowse /* Unlock everything, free the detached memory. */ 1058125854Sdwmalone DIRHASH_UNLOCK(dh); 1059125854Sdwmalone DIRHASHLIST_UNLOCK(); 106079561Siedowse for (i = 0; i < narrays; i++) 1061125854Sdwmalone DIRHASH_BLKFREE(hash[i]); 106279561Siedowse FREE(hash, M_DIRHASH); 106379561Siedowse FREE(blkfree, M_DIRHASH); 106479561Siedowse 106579561Siedowse /* Account for the returned memory, and repeat if necessary. */ 1066125854Sdwmalone DIRHASHLIST_LOCK(); 106779561Siedowse ufs_dirhashmem -= mem; 106879561Siedowse } 1069125854Sdwmalone /* Success; return with list locked. */ 107079561Siedowse return (0); 107179561Siedowse} 107279561Siedowse 107379561Siedowse 107499101Siedowsevoid 107579561Siedowseufsdirhash_init() 107679561Siedowse{ 107796874Siedowse ufsdirhash_zone = uma_zcreate("DIRHASH", DH_NBLKOFF * sizeof(doff_t), 107892768Sjeff NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 107993818Sjhb mtx_init(&ufsdirhash_mtx, "dirhash list", NULL, MTX_DEF); 108079561Siedowse TAILQ_INIT(&ufsdirhash_list); 108179561Siedowse} 108279561Siedowse 108399101Siedowsevoid 108499101Siedowseufsdirhash_uninit() 108599101Siedowse{ 108699101Siedowse KASSERT(TAILQ_EMPTY(&ufsdirhash_list), ("ufsdirhash_uninit")); 108799101Siedowse uma_zdestroy(ufsdirhash_zone); 108899101Siedowse mtx_destroy(&ufsdirhash_mtx); 108999101Siedowse} 109079561Siedowse 109179561Siedowse#endif /* UFS_DIRHASH */ 1092