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