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