ufs_dirhash.c revision 92768
11638Srgrimes/*
250476Speter * Copyright (c) 2001 Ian Dowse.  All rights reserved.
31638Srgrimes *
4115103Strhodes * Redistribution and use in source and binary forms, with or without
5115103Strhodes * modification, are permitted provided that the following conditions
6115103Strhodes * are met:
7115103Strhodes * 1. Redistributions of source code must retain the above copyright
83470Srgrimes *    notice, this list of conditions and the following disclaimer.
9115103Strhodes * 2. Redistributions in binary form must reproduce the above copyright
10115103Strhodes *    notice, this list of conditions and the following disclaimer in the
111638Srgrimes *    documentation and/or other materials provided with the distribution.
12115103Strhodes *
13115103Strhodes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14115103Strhodes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15115103Strhodes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16115103Strhodes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17115103Strhodes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18115103Strhodes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19115103Strhodes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20291786Sbdrewery * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2123559Swosch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2223559Swosch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23115103Strhodes * SUCH DAMAGE.
24115103Strhodes *
2523559Swosch * $FreeBSD: head/sys/ufs/ufs/ufs_dirhash.c 92768 2002-03-20 08:48:07Z jeff $
26115103Strhodes */
2723559Swosch/*
2823559Swosch * This implements a hash-based lookup scheme for UFS directories.
2923559Swosch */
30115103Strhodes
31115103Strhodes#include "opt_ufs.h"
32115103Strhodes
3323559Swosch#ifdef UFS_DIRHASH
3423559Swosch
3523559Swosch#include <sys/param.h>
36115103Strhodes#include <sys/systm.h>
37115103Strhodes#include <sys/kernel.h>
3823559Swosch#include <sys/lock.h>
3923559Swosch#include <sys/mutex.h>
40152265Sharti#include <sys/malloc.h>
4123559Swosch#include <sys/fnv_hash.h>
42115103Strhodes#include <sys/proc.h>
43264483Sjmmv#include <sys/bio.h>
44115103Strhodes#include <sys/buf.h>
4523559Swosch#include <sys/vnode.h>
46115103Strhodes#include <sys/mount.h>
4723559Swosch#include <sys/sysctl.h>
48115103Strhodes#include <vm/uma.h>
49115103Strhodes
501638Srgrimes#include <ufs/ufs/quota.h>
51115103Strhodes#include <ufs/ufs/inode.h>
5223578Swosch#include <ufs/ufs/dir.h>
53115103Strhodes#include <ufs/ufs/dirhash.h>
54115103Strhodes#include <ufs/ufs/extattr.h>
55115103Strhodes#include <ufs/ufs/ufsmount.h>
56115103Strhodes#include <ufs/ufs/ufs_extern.h>
57115103Strhodes
58115103Strhodes#define WRAPINCR(val, limit)	(((val) + 1 == (limit)) ? 0 : ((val) + 1))
59115103Strhodes#define OFSFMT(vp)		((vp)->v_mount->mnt_maxsymlinklen <= 0)
60115103Strhodes#define BLKFREE2IDX(n)		((n) > DH_NFSTATS ? DH_NFSTATS : (n))
61115103Strhodes
62115103Strhodesstatic MALLOC_DEFINE(M_DIRHASH, "UFS dirhash", "UFS directory hash tables");
63115103Strhodes
641638SrgrimesSYSCTL_NODE(_vfs, OID_AUTO, ufs, CTLFLAG_RD, 0, "UFS filesystem");
651638Srgrimes
661638Srgrimesstatic int ufs_mindirhashsize = DIRBLKSIZ * 5;
671638SrgrimesSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_minsize, CTLFLAG_RW,
68115103Strhodes    &ufs_mindirhashsize,
691638Srgrimes    0, "minimum directory size in bytes for which to use hashed lookup");
701638Srgrimesstatic int ufs_dirhashmaxmem = 2 * 1024 * 1024;
711638SrgrimesSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_maxmem, CTLFLAG_RW, &ufs_dirhashmaxmem,
721638Srgrimes    0, "maximum allowed dirhash memory usage");
731638Srgrimesstatic int ufs_dirhashmem;
741638SrgrimesSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_mem, CTLFLAG_RD, &ufs_dirhashmem,
751638Srgrimes    0, "current dirhash memory usage");
761638Srgrimesstatic int ufs_dirhashcheck = 0;
771638SrgrimesSYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_docheck, CTLFLAG_RW, &ufs_dirhashcheck,
781638Srgrimes    0, "enable extra sanity tests");
791638Srgrimes
801638Srgrimes
811638Srgrimesstatic int ufsdirhash_hash(struct dirhash *dh, char *name, int namelen);
821638Srgrimesstatic void ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff);
831638Srgrimesstatic void ufsdirhash_delslot(struct dirhash *dh, int slot);
841638Srgrimesstatic int ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen,
851638Srgrimes	   doff_t offset);
861638Srgrimesstatic doff_t ufsdirhash_getprev(struct direct *dp, doff_t offset);
871638Srgrimesstatic void ufsdirhash_init(void);
881638Srgrimesstatic int ufsdirhash_recycle(int wanted);
891638Srgrimes
901638Srgrimesstatic uma_zone_t	ufsdirhash_zone;
911638Srgrimes
921638Srgrimes/* Dirhash list; recently-used entries are near the tail. */
931638Srgrimesstatic TAILQ_HEAD(, dirhash) ufsdirhash_list;
941638Srgrimes
951638Srgrimes/* Protects: ufsdirhash_list, `dh_list' field, ufs_dirhashmem. */
96208420Smaximstatic struct mtx	ufsdirhash_mtx;
97208420Smaxim
98208420Smaxim/*
99208420Smaxim * Locking order:
100208420Smaxim *	ufsdirhash_mtx
101208417Smaxim *	dh_mtx
1021638Srgrimes *
1031638Srgrimes * The dh_mtx mutex should be aquired either via the inode lock, or via
1041638Srgrimes * ufsdirhash_mtx. Only the owner of the inode may free the associated
1051638Srgrimes * dirhash, but anything can steal its memory and set dh_hash to NULL.
1061638Srgrimes */
1071638Srgrimes
1081638Srgrimes/*
1091638Srgrimes * Attempt to build up a hash table for the directory contents in
1101638Srgrimes * inode 'ip'. Returns 0 on success, or -1 of the operation failed.
1111638Srgrimes */
1121638Srgrimesint
1131638Srgrimesufsdirhash_build(struct inode *ip)
1141638Srgrimes{
1151638Srgrimes	struct dirhash *dh;
1161638Srgrimes	struct buf *bp = NULL;
1171638Srgrimes	struct direct *ep;
1181638Srgrimes	struct vnode *vp;
1191638Srgrimes	doff_t bmask, pos;
1201638Srgrimes	int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot;
1211638Srgrimes
1221638Srgrimes	/* Check if we can/should use dirhash. */
1231638Srgrimes	if (ip->i_dirhash == NULL) {
12483075Sru		if (ip->i_size < ufs_mindirhashsize || OFSFMT(ip->i_vnode))
1251638Srgrimes			return (-1);
12683075Sru	} else {
12783075Sru		/* Hash exists, but sysctls could have changed. */
1281638Srgrimes		if (ip->i_size < ufs_mindirhashsize ||
12983075Sru		    ufs_dirhashmem > ufs_dirhashmaxmem) {
13083075Sru			ufsdirhash_free(ip);
13183075Sru			return (-1);
1321638Srgrimes		}
1331638Srgrimes		/* Check if hash exists and is intact (note: unlocked read). */
1341638Srgrimes		if (ip->i_dirhash->dh_hash != NULL)
1351638Srgrimes			return (0);
1361638Srgrimes		/* Free the old, recycled hash and build a new one. */
1371638Srgrimes		ufsdirhash_free(ip);
1381638Srgrimes	}
1391638Srgrimes
1401638Srgrimes	/* Don't hash removed directories. */
1411638Srgrimes	if (ip->i_effnlink == 0)
1421638Srgrimes		return (-1);
1431638Srgrimes
1441638Srgrimes	vp = ip->i_vnode;
1451638Srgrimes	/* Allocate 50% more entries than this dir size could ever need. */
14674942Sru	KASSERT(ip->i_size >= DIRBLKSIZ, ("ufsdirhash_build size"));
1471638Srgrimes	nslots = ip->i_size / DIRECTSIZ(1);
14858494Sru	nslots = (nslots * 3 + 1) / 2;
1491638Srgrimes	narrays = howmany(nslots, DH_NBLKOFF);
1501638Srgrimes	nslots = narrays * DH_NBLKOFF;
1511638Srgrimes	dirblocks = howmany(ip->i_size, DIRBLKSIZ);
15274942Sru	nblocks = (dirblocks * 3 + 1) / 2;
15374942Sru
1541638Srgrimes	memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) +
1551638Srgrimes	    narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) +
1561638Srgrimes	    nblocks * sizeof(*dh->dh_blkfree);
1571638Srgrimes	mtx_lock(&ufsdirhash_mtx);
1581638Srgrimes	if (memreqd + ufs_dirhashmem > ufs_dirhashmaxmem) {
1591638Srgrimes		mtx_unlock(&ufsdirhash_mtx);
1601638Srgrimes		if (memreqd > ufs_dirhashmaxmem / 2)
1611638Srgrimes			return (-1);
1621638Srgrimes
1631638Srgrimes		/* Try to free some space. */
1641638Srgrimes		if (ufsdirhash_recycle(memreqd) != 0)
1651638Srgrimes			return (-1);
1661638Srgrimes		/* Enough was freed, and ufsdirhash_mtx has been locked. */
1671638Srgrimes	}
1681638Srgrimes	ufs_dirhashmem += memreqd;
1691638Srgrimes	mtx_unlock(&ufsdirhash_mtx);
1701638Srgrimes
1711638Srgrimes	/*
1721638Srgrimes	 * Use non-blocking mallocs so that we will revert to a linear
1731638Srgrimes	 * lookup on failure rather than potentially blocking forever.
1741638Srgrimes	 */
1751638Srgrimes	MALLOC(dh, struct dirhash *, sizeof *dh, M_DIRHASH, M_NOWAIT | M_ZERO);
1761638Srgrimes	if (dh == NULL)
1771638Srgrimes		return (-1);
1781638Srgrimes	MALLOC(dh->dh_hash, doff_t **, narrays * sizeof(dh->dh_hash[0]),
1791638Srgrimes	    M_DIRHASH, M_NOWAIT | M_ZERO);
1801638Srgrimes	MALLOC(dh->dh_blkfree, u_int8_t *, nblocks * sizeof(dh->dh_blkfree[0]),
1811638Srgrimes	    M_DIRHASH, M_NOWAIT);
1821638Srgrimes	if (dh->dh_hash == NULL || dh->dh_blkfree == NULL)
1831638Srgrimes		goto fail;
1841638Srgrimes	for (i = 0; i < narrays; i++) {
1851638Srgrimes		if ((dh->dh_hash[i] = uma_zalloc(ufsdirhash_zone,
1861638Srgrimes		    M_WAITOK)) == NULL)
1871638Srgrimes			goto fail;
1881638Srgrimes		for (j = 0; j < DH_NBLKOFF; j++)
1891638Srgrimes			dh->dh_hash[i][j] = DIRHASH_EMPTY;
1901638Srgrimes	}
1911638Srgrimes
192159721Syar	/* Initialise the hash table and block statistics. */
1931638Srgrimes	mtx_init(&dh->dh_mtx, "dirhash", MTX_DEF);
1941638Srgrimes	dh->dh_narrays = narrays;
1951638Srgrimes	dh->dh_hlen = nslots;
1961638Srgrimes	dh->dh_nblk = nblocks;
1971638Srgrimes	dh->dh_dirblks = dirblocks;
1981638Srgrimes	for (i = 0; i < dirblocks; i++)
1991638Srgrimes		dh->dh_blkfree[i] = DIRBLKSIZ / DIRALIGN;
2001638Srgrimes	for (i = 0; i < DH_NFSTATS; i++)
2011638Srgrimes		dh->dh_firstfree[i] = -1;
2021638Srgrimes	dh->dh_firstfree[DH_NFSTATS] = 0;
2031638Srgrimes	dh->dh_seqopt = 0;
2041638Srgrimes	dh->dh_seqoff = 0;
2051638Srgrimes	dh->dh_score = DH_SCOREINIT;
2061638Srgrimes	ip->i_dirhash = dh;
2071638Srgrimes
2081638Srgrimes	bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
2091638Srgrimes	pos = 0;
2101638Srgrimes	while (pos < ip->i_size) {
2111638Srgrimes		/* If necessary, get the next directory block. */
2121638Srgrimes		if ((pos & bmask) == 0) {
2131638Srgrimes			if (bp != NULL)
2141638Srgrimes				brelse(bp);
2151638Srgrimes			if (UFS_BLKATOFF(vp, (off_t)pos, NULL, &bp) != 0)
2161638Srgrimes				goto fail;
2171638Srgrimes		}
21816437Sphk
21916437Sphk		/* Add this entry to the hash. */
22016437Sphk		ep = (struct direct *)((char *)bp->b_data + (pos & bmask));
2211638Srgrimes		if (ep->d_reclen == 0 || ep->d_reclen >
222133653Sru		    DIRBLKSIZ - (pos & (DIRBLKSIZ - 1))) {
2231638Srgrimes			/* Corrupted directory. */
22488055Sru			brelse(bp);
22588055Sru			goto fail;
22688055Sru		}
22788055Sru		if (ep->d_ino != 0) {
22888055Sru			/* Add the entry (simplified ufsdirhash_add). */
2291638Srgrimes			slot = ufsdirhash_hash(dh, ep->d_name, ep->d_namlen);
2301638Srgrimes			while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY)
2311638Srgrimes				slot = WRAPINCR(slot, dh->dh_hlen);
2321638Srgrimes			dh->dh_hused++;
233235928Smarcel			DH_ENTRY(dh, slot) = pos;
2341638Srgrimes			ufsdirhash_adjfree(dh, pos, -DIRSIZ(0, ep));
2351638Srgrimes		}
2361638Srgrimes		pos += ep->d_reclen;
2371638Srgrimes	}
2381638Srgrimes
2391638Srgrimes	if (bp != NULL)
2401638Srgrimes		brelse(bp);
2411638Srgrimes	mtx_lock(&ufsdirhash_mtx);
2421638Srgrimes	TAILQ_INSERT_TAIL(&ufsdirhash_list, dh, dh_list);
2431638Srgrimes	dh->dh_onlist = 1;
24474942Sru	mtx_unlock(&ufsdirhash_mtx);
24574942Sru	return (0);
2461638Srgrimes
2471638Srgrimesfail:
2481638Srgrimes	if (dh->dh_hash != NULL) {
2491638Srgrimes		for (i = 0; i < narrays; i++)
25094424Sru			if (dh->dh_hash[i] != NULL)
25194424Sru				uma_zfree(ufsdirhash_zone, dh->dh_hash[i]);
25294424Sru		FREE(dh->dh_hash, M_DIRHASH);
25394424Sru	}
25494424Sru	if (dh->dh_blkfree != NULL)
255289052Sbdrewery		FREE(dh->dh_blkfree, M_DIRHASH);
256289052Sbdrewery	FREE(dh, M_DIRHASH);
257289052Sbdrewery	ip->i_dirhash = NULL;
258289052Sbdrewery	mtx_lock(&ufsdirhash_mtx);
259289052Sbdrewery	ufs_dirhashmem -= memreqd;
260289052Sbdrewery	mtx_unlock(&ufsdirhash_mtx);
261289052Sbdrewery	return (-1);
262289052Sbdrewery}
263289052Sbdrewery
264289052Sbdrewery/*
265289052Sbdrewery * Free any hash table associated with inode 'ip'.
266289052Sbdrewery */
267289052Sbdreweryvoid
268289052Sbdreweryufsdirhash_free(struct inode *ip)
26975083Sru{
27075083Sru	struct dirhash *dh;
27175083Sru	int i, mem;
27253965Smharo
27394424Sru	if ((dh = ip->i_dirhash) == NULL)
27494424Sru		return;
2751638Srgrimes	mtx_lock(&ufsdirhash_mtx);
2761638Srgrimes	mtx_lock(&dh->dh_mtx);
2771638Srgrimes	if (dh->dh_onlist)
2781638Srgrimes		TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list);
2791638Srgrimes	mtx_unlock(&dh->dh_mtx);
280235928Smarcel	mtx_unlock(&ufsdirhash_mtx);
2811638Srgrimes
28214701Sbde	/* The dirhash pointed to by 'dh' is exclusively ours now. */
28314701Sbde
28414701Sbde	mem = sizeof(*dh);
28514701Sbde	if (dh->dh_hash != NULL) {
2861638Srgrimes		for (i = 0; i < dh->dh_narrays; i++)
28714701Sbde			uma_zfree(ufsdirhash_zone, dh->dh_hash[i]);
28814701Sbde		FREE(dh->dh_hash, M_DIRHASH);
28914701Sbde		FREE(dh->dh_blkfree, M_DIRHASH);
29014701Sbde		mem += dh->dh_narrays * sizeof(*dh->dh_hash) +
29114701Sbde		    dh->dh_narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) +
29214701Sbde		    dh->dh_nblk * sizeof(*dh->dh_blkfree);
29314701Sbde	}
29414701Sbde	mtx_destroy(&dh->dh_mtx);
2951638Srgrimes	FREE(dh, M_DIRHASH);
2961638Srgrimes	ip->i_dirhash = NULL;
297125493Sru
298125493Sru	mtx_lock(&ufsdirhash_mtx);
299125493Sru	ufs_dirhashmem -= mem;
3001638Srgrimes	mtx_unlock(&ufsdirhash_mtx);
3011638Srgrimes}
3021638Srgrimes
3031638Srgrimes/*
3041638Srgrimes * Find the offset of the specified name within the given inode.
30575284Sru * Returns 0 on success, ENOENT if the entry does not exist, or
30675284Sru * EJUSTRETURN if the caller should revert to a linear search.
30775284Sru *
30875284Sru * If successful, the directory offset is stored in *offp, and a
30975284Sru * pointer to a struct buf containing the entry is stored in *bpp. If
31074942Sru * prevoffp is non-NULL, the offset of the previous entry within
31174942Sru * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry
3121638Srgrimes * is the first in a block, the start of the block is used).
3131638Srgrimes */
3141638Srgrimesint
3151638Srgrimesufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp,
3161638Srgrimes    struct buf **bpp, doff_t *prevoffp)
3171638Srgrimes{
3181638Srgrimes	struct dirhash *dh, *dh_next;
3191638Srgrimes	struct direct *dp;
3201638Srgrimes	struct vnode *vp;
3211638Srgrimes	struct buf *bp;
3221638Srgrimes	doff_t blkoff, bmask, offset, prevoff;
323156743Sru	int i, slot;
3241638Srgrimes
3251638Srgrimes	if ((dh = ip->i_dirhash) == NULL)
3261638Srgrimes		return (EJUSTRETURN);
327276486Sngie	/*
3281638Srgrimes	 * Move this dirhash towards the end of the list if it has a
3291638Srgrimes	 * score higher than the next entry, and aquire the dh_mtx.
3301638Srgrimes	 * Optimise the case where it's already the last by performing
3311638Srgrimes	 * an unlocked read of the TAILQ_NEXT pointer.
3321638Srgrimes	 *
3331638Srgrimes	 * In both cases, end up holding just dh_mtx.
3341638Srgrimes	 */
3351638Srgrimes	if (TAILQ_NEXT(dh, dh_list) != NULL) {
3361638Srgrimes		mtx_lock(&ufsdirhash_mtx);
3371638Srgrimes		mtx_lock(&dh->dh_mtx);
3381638Srgrimes		/*
3391638Srgrimes		 * If the new score will be greater than that of the next
3401638Srgrimes		 * entry, then move this entry past it. With both mutexes
3411638Srgrimes		 * held, dh_next won't go away, but its dh_score could
3421638Srgrimes		 * change; that's not important since it is just a hint.
3431638Srgrimes		 */
3441638Srgrimes		if (dh->dh_hash != NULL &&
3451638Srgrimes		    (dh_next = TAILQ_NEXT(dh, dh_list)) != NULL &&
3461638Srgrimes		    dh->dh_score >= dh_next->dh_score) {
3471638Srgrimes			KASSERT(dh->dh_onlist, ("dirhash: not on list"));
3481638Srgrimes			TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list);
3491638Srgrimes			TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh,
3501638Srgrimes			    dh_list);
3511638Srgrimes		}
352281056Sbdrewery		mtx_unlock(&ufsdirhash_mtx);
353281056Sbdrewery	} else {
354281056Sbdrewery		/* Already the last, though that could change as we wait. */
355281056Sbdrewery		mtx_lock(&dh->dh_mtx);
356281056Sbdrewery	}
357281056Sbdrewery	if (dh->dh_hash == NULL) {
358281056Sbdrewery		mtx_unlock(&dh->dh_mtx);
3591638Srgrimes		ufsdirhash_free(ip);
3601638Srgrimes		return (EJUSTRETURN);
3611638Srgrimes	}
3621638Srgrimes
3631638Srgrimes	/* Update the score. */
3641638Srgrimes	if (dh->dh_score < DH_SCOREMAX)
3651638Srgrimes		dh->dh_score++;
3661638Srgrimes
3671638Srgrimes	vp = ip->i_vnode;
3681638Srgrimes	bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
3691638Srgrimes	blkoff = -1;
3701638Srgrimes	bp = NULL;
37174942Srurestart:
3721638Srgrimes	slot = ufsdirhash_hash(dh, name, namelen);
3731638Srgrimes
3741638Srgrimes	if (dh->dh_seqopt) {
3751638Srgrimes		/*
3761638Srgrimes		 * Sequential access optimisation. dh_seqoff contains the
3771638Srgrimes		 * offset of the directory entry immediately following
378235122Sjlh		 * the last entry that was looked up. Check if this offset
379235122Sjlh		 * appears in the hash chain for the name we are looking for.
380235122Sjlh		 */
381235122Sjlh		for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY;
382245561Sbrooks		    i = WRAPINCR(i, dh->dh_hlen))
383245561Sbrooks			if (offset == dh->dh_seqoff)
38474942Sru				break;
38574942Sru		if (offset == dh->dh_seqoff) {
3861638Srgrimes			/*
3871638Srgrimes			 * We found an entry with the expected offset. This
3881638Srgrimes			 * is probably the entry we want, but if not, the
3891638Srgrimes			 * code below will turn off seqoff and retry.
3901638Srgrimes			 */
391264483Sjmmv			slot = i;
392264483Sjmmv		} else
393264483Sjmmv			dh->dh_seqopt = 0;
394264483Sjmmv	}
395264483Sjmmv
396264483Sjmmv	for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY;
397264483Sjmmv	    slot = WRAPINCR(slot, dh->dh_hlen)) {
398264483Sjmmv		if (offset == DIRHASH_DEL)
399264483Sjmmv			continue;
400264483Sjmmv		mtx_unlock(&dh->dh_mtx);
401264483Sjmmv
402264483Sjmmv		if (offset < 0 || offset >= ip->i_size)
403264483Sjmmv			panic("ufsdirhash_lookup: bad offset in hash array");
404264483Sjmmv		if ((offset & ~bmask) != blkoff) {
405264483Sjmmv			if (bp != NULL)
406264483Sjmmv				brelse(bp);
407264483Sjmmv			blkoff = offset & ~bmask;
408264483Sjmmv			if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0)
409264483Sjmmv				return (EJUSTRETURN);
410264483Sjmmv		}
411264483Sjmmv		dp = (struct direct *)(bp->b_data + (offset & bmask));
412264483Sjmmv		if (dp->d_reclen == 0 || dp->d_reclen >
413264483Sjmmv		    DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) {
414264483Sjmmv			/* Corrupted directory. */
415264483Sjmmv			brelse(bp);
416264483Sjmmv			return (EJUSTRETURN);
417264483Sjmmv		}
418264483Sjmmv		if (dp->d_namlen == namelen &&
419264483Sjmmv		    bcmp(dp->d_name, name, namelen) == 0) {
420264483Sjmmv			/* Found. Get the prev offset if needed. */
421264483Sjmmv			if (prevoffp != NULL) {
422264483Sjmmv				if (offset & (DIRBLKSIZ - 1)) {
423264483Sjmmv					prevoff = ufsdirhash_getprev(dp,
424264483Sjmmv					    offset);
425264483Sjmmv					if (prevoff == -1) {
426264483Sjmmv						brelse(bp);
427264483Sjmmv						return (EJUSTRETURN);
428292278Sngie					}
429292278Sngie				} else
430292278Sngie					prevoff = offset;
431264483Sjmmv				*prevoffp = prevoff;
432264483Sjmmv			}
433264483Sjmmv
434292278Sngie			/* Check for sequential access, and update offset. */
435292278Sngie			if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset)
436292278Sngie				dh->dh_seqopt = 1;
437292278Sngie			dh->dh_seqoff = offset + DIRSIZ(0, dp);
438264483Sjmmv
439264483Sjmmv			*bpp = bp;
440264483Sjmmv			*offp = offset;
441264483Sjmmv			return (0);
442264483Sjmmv		}
443264483Sjmmv
444264483Sjmmv		mtx_lock(&dh->dh_mtx);
445292812Sngie		if (dh->dh_hash == NULL) {
446292812Sngie			mtx_unlock(&dh->dh_mtx);
447292812Sngie			if (bp != NULL)
448292812Sngie				brelse(bp);
449264483Sjmmv			ufsdirhash_free(ip);
450264483Sjmmv			return (EJUSTRETURN);
451264483Sjmmv		}
452264483Sjmmv		/*
453264483Sjmmv		 * When the name doesn't match in the seqopt case, go back
454264483Sjmmv		 * and search normally.
455264483Sjmmv		 */
456264483Sjmmv		if (dh->dh_seqopt) {
457264483Sjmmv			dh->dh_seqopt = 0;
458264483Sjmmv			goto restart;
459264483Sjmmv		}
460264483Sjmmv	}
461264483Sjmmv	mtx_unlock(&dh->dh_mtx);
462264483Sjmmv	if (bp != NULL)
463264483Sjmmv		brelse(bp);
464264483Sjmmv	return (ENOENT);
465264483Sjmmv}
466264483Sjmmv
467264483Sjmmv/*
468264483Sjmmv * Find a directory block with room for 'slotneeded' bytes. Returns
469264483Sjmmv * the offset of the directory entry that begins the free space.
470264483Sjmmv * This will either be the offset of an existing entry that has free
471264483Sjmmv * space at the end, or the offset of an entry with d_ino == 0 at
472264483Sjmmv * the start of a DIRBLKSIZ block.
473264483Sjmmv *
474264483Sjmmv * To use the space, the caller may need to compact existing entries in
475264483Sjmmv * the directory. The total number of bytes in all of the entries involved
476264483Sjmmv * in the compaction is stored in *slotsize. In other words, all of
477264483Sjmmv * the entries that must be compacted are exactly contained in the
478264483Sjmmv * region beginning at the returned offset and spanning *slotsize bytes.
479264483Sjmmv *
480264483Sjmmv * Returns -1 if no space was found, indicating that the directory
481264483Sjmmv * must be extended.
482264483Sjmmv */
483264483Sjmmvdoff_t
484264483Sjmmvufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize)
485264483Sjmmv{
486264483Sjmmv	struct direct *dp;
487264483Sjmmv	struct dirhash *dh;
488264483Sjmmv	struct buf *bp;
489264483Sjmmv	doff_t pos, slotstart;
490264483Sjmmv	int dirblock, error, freebytes, i;
491264483Sjmmv
492264483Sjmmv	if ((dh = ip->i_dirhash) == NULL)
493264483Sjmmv		return (-1);
494264483Sjmmv	mtx_lock(&dh->dh_mtx);
495	if (dh->dh_hash == NULL) {
496		mtx_unlock(&dh->dh_mtx);
497		ufsdirhash_free(ip);
498		return (-1);
499	}
500
501	/* Find a directory block with the desired free space. */
502	dirblock = -1;
503	for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++)
504		if ((dirblock = dh->dh_firstfree[i]) != -1)
505			break;
506	if (dirblock == -1) {
507		mtx_unlock(&dh->dh_mtx);
508		return (-1);
509	}
510
511	KASSERT(dirblock < dh->dh_nblk &&
512	    dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN),
513	    ("ufsdirhash_findfree: bad stats"));
514	mtx_unlock(&dh->dh_mtx);
515	pos = dirblock * DIRBLKSIZ;
516	error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp);
517	if (error)
518		return (-1);
519
520	/* Find the first entry with free space. */
521	for (i = 0; i < DIRBLKSIZ; ) {
522		if (dp->d_reclen == 0) {
523			brelse(bp);
524			return (-1);
525		}
526		if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp))
527			break;
528		i += dp->d_reclen;
529		dp = (struct direct *)((char *)dp + dp->d_reclen);
530	}
531	if (i > DIRBLKSIZ) {
532		brelse(bp);
533		return (-1);
534	}
535	slotstart = pos + i;
536
537	/* Find the range of entries needed to get enough space */
538	freebytes = 0;
539	while (i < DIRBLKSIZ && freebytes < slotneeded) {
540		freebytes += dp->d_reclen;
541		if (dp->d_ino != 0)
542			freebytes -= DIRSIZ(0, dp);
543		if (dp->d_reclen == 0) {
544			brelse(bp);
545			return (-1);
546		}
547		i += dp->d_reclen;
548		dp = (struct direct *)((char *)dp + dp->d_reclen);
549	}
550	if (i > DIRBLKSIZ) {
551		brelse(bp);
552		return (-1);
553	}
554	if (freebytes < slotneeded)
555		panic("ufsdirhash_findfree: free mismatch");
556	brelse(bp);
557	*slotsize = pos + i - slotstart;
558	return (slotstart);
559}
560
561/*
562 * Return the start of the unused space at the end of a directory, or
563 * -1 if there are no trailing unused blocks.
564 */
565doff_t
566ufsdirhash_enduseful(struct inode *ip)
567{
568
569	struct dirhash *dh;
570	int i;
571
572	if ((dh = ip->i_dirhash) == NULL)
573		return (-1);
574	mtx_lock(&dh->dh_mtx);
575	if (dh->dh_hash == NULL) {
576		mtx_unlock(&dh->dh_mtx);
577		ufsdirhash_free(ip);
578		return (-1);
579	}
580
581	if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN) {
582		mtx_unlock(&dh->dh_mtx);
583		return (-1);
584	}
585
586	for (i = dh->dh_dirblks - 1; i >= 0; i--)
587		if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN)
588			break;
589	mtx_unlock(&dh->dh_mtx);
590	return ((doff_t)(i + 1) * DIRBLKSIZ);
591}
592
593/*
594 * Insert information into the hash about a new directory entry. dirp
595 * points to a struct direct containing the entry, and offset specifies
596 * the offset of this entry.
597 */
598void
599ufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset)
600{
601	struct dirhash *dh;
602	int slot;
603
604	if ((dh = ip->i_dirhash) == NULL)
605		return;
606	mtx_lock(&dh->dh_mtx);
607	if (dh->dh_hash == NULL) {
608		mtx_unlock(&dh->dh_mtx);
609		ufsdirhash_free(ip);
610		return;
611	}
612
613	KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ,
614	    ("ufsdirhash_add: bad offset"));
615	/*
616	 * Normal hash usage is < 66%. If the usage gets too high then
617	 * remove the hash entirely and let it be rebuilt later.
618	 */
619	if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) {
620		mtx_unlock(&dh->dh_mtx);
621		ufsdirhash_free(ip);
622		return;
623	}
624
625	/* Find a free hash slot (empty or deleted), and add the entry. */
626	slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen);
627	while (DH_ENTRY(dh, slot) >= 0)
628		slot = WRAPINCR(slot, dh->dh_hlen);
629	if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY)
630		dh->dh_hused++;
631	DH_ENTRY(dh, slot) = offset;
632
633	/* Update the per-block summary info. */
634	ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp));
635	mtx_unlock(&dh->dh_mtx);
636}
637
638/*
639 * Remove the specified directory entry from the hash. The entry to remove
640 * is defined by the name in `dirp', which must exist at the specified
641 * `offset' within the directory.
642 */
643void
644ufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset)
645{
646	struct dirhash *dh;
647	int slot;
648
649	if ((dh = ip->i_dirhash) == NULL)
650		return;
651	mtx_lock(&dh->dh_mtx);
652	if (dh->dh_hash == NULL) {
653		mtx_unlock(&dh->dh_mtx);
654		ufsdirhash_free(ip);
655		return;
656	}
657
658	KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ,
659	    ("ufsdirhash_remove: bad offset"));
660	/* Find the entry */
661	slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset);
662
663	/* Remove the hash entry. */
664	ufsdirhash_delslot(dh, slot);
665
666	/* Update the per-block summary info. */
667	ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp));
668	mtx_unlock(&dh->dh_mtx);
669}
670
671/*
672 * Change the offset associated with a directory entry in the hash. Used
673 * when compacting directory blocks.
674 */
675void
676ufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff,
677    doff_t newoff)
678{
679	struct dirhash *dh;
680	int slot;
681
682	if ((dh = ip->i_dirhash) == NULL)
683		return;
684	mtx_lock(&dh->dh_mtx);
685	if (dh->dh_hash == NULL) {
686		mtx_unlock(&dh->dh_mtx);
687		ufsdirhash_free(ip);
688		return;
689	}
690
691	KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ &&
692	    newoff < dh->dh_dirblks * DIRBLKSIZ,
693	    ("ufsdirhash_move: bad offset"));
694	/* Find the entry, and update the offset. */
695	slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff);
696	DH_ENTRY(dh, slot) = newoff;
697	mtx_unlock(&dh->dh_mtx);
698}
699
700/*
701 * Inform dirhash that the directory has grown by one block that
702 * begins at offset (i.e. the new length is offset + DIRBLKSIZ).
703 */
704void
705ufsdirhash_newblk(struct inode *ip, doff_t offset)
706{
707	struct dirhash *dh;
708	int block;
709
710	if ((dh = ip->i_dirhash) == NULL)
711		return;
712	mtx_lock(&dh->dh_mtx);
713	if (dh->dh_hash == NULL) {
714		mtx_unlock(&dh->dh_mtx);
715		ufsdirhash_free(ip);
716		return;
717	}
718
719	KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ,
720	    ("ufsdirhash_newblk: bad offset"));
721	block = offset / DIRBLKSIZ;
722	if (block >= dh->dh_nblk) {
723		/* Out of space; must rebuild. */
724		mtx_unlock(&dh->dh_mtx);
725		ufsdirhash_free(ip);
726		return;
727	}
728	dh->dh_dirblks = block + 1;
729
730	/* Account for the new free block. */
731	dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN;
732	if (dh->dh_firstfree[DH_NFSTATS] == -1)
733		dh->dh_firstfree[DH_NFSTATS] = block;
734	mtx_unlock(&dh->dh_mtx);
735}
736
737/*
738 * Inform dirhash that the directory is being truncated.
739 */
740void
741ufsdirhash_dirtrunc(struct inode *ip, doff_t offset)
742{
743	struct dirhash *dh;
744	int block, i;
745
746	if ((dh = ip->i_dirhash) == NULL)
747		return;
748	mtx_lock(&dh->dh_mtx);
749	if (dh->dh_hash == NULL) {
750		mtx_unlock(&dh->dh_mtx);
751		ufsdirhash_free(ip);
752		return;
753	}
754
755	KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ,
756	    ("ufsdirhash_dirtrunc: bad offset"));
757	block = howmany(offset, DIRBLKSIZ);
758	/*
759	 * If the directory shrinks to less than 1/8 of dh_nblk blocks
760	 * (about 20% of its original size due to the 50% extra added in
761	 * ufsdirhash_build) then free it, and let the caller rebuild
762	 * if necessary.
763	 */
764	if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) {
765		mtx_unlock(&dh->dh_mtx);
766		ufsdirhash_free(ip);
767		return;
768	}
769
770	/*
771	 * Remove any `first free' information pertaining to the
772	 * truncated blocks. All blocks we're removing should be
773	 * completely unused.
774	 */
775	if (dh->dh_firstfree[DH_NFSTATS] >= block)
776		dh->dh_firstfree[DH_NFSTATS] = -1;
777	for (i = block; i < dh->dh_dirblks; i++)
778		if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN)
779			panic("ufsdirhash_dirtrunc: blocks in use");
780	for (i = 0; i < DH_NFSTATS; i++)
781		if (dh->dh_firstfree[i] >= block)
782			panic("ufsdirhash_dirtrunc: first free corrupt");
783	dh->dh_dirblks = block;
784	mtx_unlock(&dh->dh_mtx);
785}
786
787/*
788 * Debugging function to check that the dirhash information about
789 * a directory block matches its actual contents. Panics if a mismatch
790 * is detected.
791 *
792 * On entry, `buf' should point to the start of an in-core
793 * DIRBLKSIZ-sized directory block, and `offset' should contain the
794 * offset from the start of the directory of that block.
795 */
796void
797ufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset)
798{
799	struct dirhash *dh;
800	struct direct *dp;
801	int block, ffslot, i, nfree;
802
803	if (!ufs_dirhashcheck)
804		return;
805	if ((dh = ip->i_dirhash) == NULL)
806		return;
807	mtx_lock(&dh->dh_mtx);
808	if (dh->dh_hash == NULL) {
809		mtx_unlock(&dh->dh_mtx);
810		ufsdirhash_free(ip);
811		return;
812	}
813
814	block = offset / DIRBLKSIZ;
815	if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks)
816		panic("ufsdirhash_checkblock: bad offset");
817
818	nfree = 0;
819	for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) {
820		dp = (struct direct *)(buf + i);
821		if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ)
822			panic("ufsdirhash_checkblock: bad dir");
823
824		if (dp->d_ino == 0) {
825#if 0
826			/*
827			 * XXX entries with d_ino == 0 should only occur
828			 * at the start of a DIRBLKSIZ block. However the
829			 * ufs code is tolerant of such entries at other
830			 * offsets, and fsck does not fix them.
831			 */
832			if (i != 0)
833				panic("ufsdirhash_checkblock: bad dir inode");
834#endif
835			nfree += dp->d_reclen;
836			continue;
837		}
838
839		/* Check that the entry	exists (will panic if it doesn't). */
840		ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i);
841
842		nfree += dp->d_reclen - DIRSIZ(0, dp);
843	}
844	if (i != DIRBLKSIZ)
845		panic("ufsdirhash_checkblock: bad dir end");
846
847	if (dh->dh_blkfree[block] * DIRALIGN != nfree)
848		panic("ufsdirhash_checkblock: bad free count");
849
850	ffslot = BLKFREE2IDX(nfree / DIRALIGN);
851	for (i = 0; i <= DH_NFSTATS; i++)
852		if (dh->dh_firstfree[i] == block && i != ffslot)
853			panic("ufsdirhash_checkblock: bad first-free");
854	if (dh->dh_firstfree[ffslot] == -1)
855		panic("ufsdirhash_checkblock: missing first-free entry");
856	mtx_unlock(&dh->dh_mtx);
857}
858
859/*
860 * Hash the specified filename into a dirhash slot.
861 */
862static int
863ufsdirhash_hash(struct dirhash *dh, char *name, int namelen)
864{
865	return (fnv_32_buf(name, namelen, FNV1_32_INIT) % dh->dh_hlen);
866}
867
868/*
869 * Adjust the number of free bytes in the block containing `offset'
870 * by the value specified by `diff'.
871 *
872 * The caller must ensure we have exclusive access to `dh'; normally
873 * that means that dh_mtx should be held, but this is also called
874 * from ufsdirhash_build() where exclusive access can be assumed.
875 */
876static void
877ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff)
878{
879	int block, i, nfidx, ofidx;
880
881	/* Update the per-block summary info. */
882	block = offset / DIRBLKSIZ;
883	KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks,
884	     ("dirhash bad offset"));
885	ofidx = BLKFREE2IDX(dh->dh_blkfree[block]);
886	dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN);
887	nfidx = BLKFREE2IDX(dh->dh_blkfree[block]);
888
889	/* Update the `first free' list if necessary. */
890	if (ofidx != nfidx) {
891		/* If removing, scan forward for the next block. */
892		if (dh->dh_firstfree[ofidx] == block) {
893			for (i = block + 1; i < dh->dh_dirblks; i++)
894				if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx)
895					break;
896			dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1;
897		}
898
899		/* Make this the new `first free' if necessary */
900		if (dh->dh_firstfree[nfidx] > block ||
901		    dh->dh_firstfree[nfidx] == -1)
902			dh->dh_firstfree[nfidx] = block;
903	}
904}
905
906/*
907 * Find the specified name which should have the specified offset.
908 * Returns a slot number, and panics on failure.
909 *
910 * `dh' must be locked on entry and remains so on return.
911 */
912static int
913ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset)
914{
915	int slot;
916
917	mtx_assert(&dh->dh_mtx, MA_OWNED);
918
919	/* Find the entry. */
920	KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full"));
921	slot = ufsdirhash_hash(dh, name, namelen);
922	while (DH_ENTRY(dh, slot) != offset &&
923	    DH_ENTRY(dh, slot) != DIRHASH_EMPTY)
924		slot = WRAPINCR(slot, dh->dh_hlen);
925	if (DH_ENTRY(dh, slot) != offset)
926		panic("ufsdirhash_findslot: '%.*s' not found", namelen, name);
927
928	return (slot);
929}
930
931/*
932 * Remove the entry corresponding to the specified slot from the hash array.
933 *
934 * `dh' must be locked on entry and remains so on return.
935 */
936static void
937ufsdirhash_delslot(struct dirhash *dh, int slot)
938{
939	int i;
940
941	mtx_assert(&dh->dh_mtx, MA_OWNED);
942
943	/* Mark the entry as deleted. */
944	DH_ENTRY(dh, slot) = DIRHASH_DEL;
945
946	/* If this is the end of a chain of DIRHASH_DEL slots, remove them. */
947	for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; )
948		i = WRAPINCR(i, dh->dh_hlen);
949	if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) {
950		for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; ) {
951			DH_ENTRY(dh, i) = DIRHASH_EMPTY;
952			dh->dh_hused--;
953			i = WRAPINCR(i, dh->dh_hlen);
954		}
955		KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen"));
956	}
957}
958
959/*
960 * Given a directory entry and its offset, find the offset of the
961 * previous entry in the same DIRBLKSIZ-sized block. Returns an
962 * offset, or -1 if there is no previous entry in the block or some
963 * other problem occurred.
964 */
965static doff_t
966ufsdirhash_getprev(struct direct *dirp, doff_t offset)
967{
968	struct direct *dp;
969	char *blkbuf;
970	doff_t blkoff, prevoff;
971	int entrypos, i;
972
973	blkoff = offset & ~(DIRBLKSIZ - 1);	/* offset of start of block */
974	entrypos = offset & (DIRBLKSIZ - 1);	/* entry relative to block */
975	blkbuf = (char *)dirp - entrypos;
976	prevoff = blkoff;
977
978	/* If `offset' is the start of a block, there is no previous entry. */
979	if (entrypos == 0)
980		return (-1);
981
982	/* Scan from the start of the block until we get to the entry. */
983	for (i = 0; i < entrypos; i += dp->d_reclen) {
984		dp = (struct direct *)(blkbuf + i);
985		if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos)
986			return (-1);	/* Corrupted directory. */
987		prevoff = blkoff + i;
988	}
989	return (prevoff);
990}
991
992/*
993 * Try to free up `wanted' bytes by stealing memory from existing
994 * dirhashes. Returns zero with ufsdirhash_mtx locked if successful.
995 */
996static int
997ufsdirhash_recycle(int wanted)
998{
999	struct dirhash *dh;
1000	doff_t **hash;
1001	u_int8_t *blkfree;
1002	int i, mem, narrays;
1003
1004	mtx_lock(&ufsdirhash_mtx);
1005	while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) {
1006		/* Find a dirhash, and lock it. */
1007		if ((dh = TAILQ_FIRST(&ufsdirhash_list)) == NULL) {
1008			mtx_unlock(&ufsdirhash_mtx);
1009			return (-1);
1010		}
1011		mtx_lock(&dh->dh_mtx);
1012		KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list"));
1013
1014		/* Decrement the score; only recycle if it becomes zero. */
1015		if (--dh->dh_score > 0) {
1016			mtx_unlock(&dh->dh_mtx);
1017			mtx_unlock(&ufsdirhash_mtx);
1018			return (-1);
1019		}
1020
1021		/* Remove it from the list and detach its memory. */
1022		TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list);
1023		dh->dh_onlist = 0;
1024		hash = dh->dh_hash;
1025		dh->dh_hash = NULL;
1026		blkfree = dh->dh_blkfree;
1027		dh->dh_blkfree = NULL;
1028		narrays = dh->dh_narrays;
1029		mem = narrays * sizeof(*dh->dh_hash) +
1030		    narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) +
1031		    dh->dh_nblk * sizeof(*dh->dh_blkfree);
1032
1033		/* Unlock everything, free the detached memory. */
1034		mtx_unlock(&dh->dh_mtx);
1035		mtx_unlock(&ufsdirhash_mtx);
1036		for (i = 0; i < narrays; i++)
1037			uma_zfree(ufsdirhash_zone, hash[i]);
1038		FREE(hash, M_DIRHASH);
1039		FREE(blkfree, M_DIRHASH);
1040
1041		/* Account for the returned memory, and repeat if necessary. */
1042		mtx_lock(&ufsdirhash_mtx);
1043		ufs_dirhashmem -= mem;
1044	}
1045	/* Success; return with ufsdirhash_mtx locked. */
1046	return (0);
1047}
1048
1049
1050static void
1051ufsdirhash_init()
1052{
1053	ufsdirhash_zone = uma_zcreate("DIRHASH", DH_NBLKOFF * sizeof(daddr_t),
1054	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1055	mtx_init(&ufsdirhash_mtx, "dirhash list", MTX_DEF);
1056	TAILQ_INIT(&ufsdirhash_list);
1057}
1058SYSINIT(ufsdirhash, SI_SUB_PSEUDO, SI_ORDER_ANY, ufsdirhash_init, NULL)
1059
1060
1061#endif /* UFS_DIRHASH */
1062