Home | History | Annotate | Line # | Download | only in lfs
ulfs_dirhash.c revision 1.6.2.4
      1  1.6.2.2       tls /*	$NetBSD: ulfs_dirhash.c,v 1.6.2.4 2017/12/03 11:39:22 jdolecek Exp $	*/
      2  1.6.2.4  jdolecek /*  from NetBSD: ufs_dirhash.c,v 1.37 2014/12/20 00:28:05 christos Exp  */
      3  1.6.2.2       tls 
      4  1.6.2.2       tls /*
      5  1.6.2.2       tls  * Copyright (c) 2001, 2002 Ian Dowse.  All rights reserved.
      6  1.6.2.2       tls  *
      7  1.6.2.2       tls  * Redistribution and use in source and binary forms, with or without
      8  1.6.2.2       tls  * modification, are permitted provided that the following conditions
      9  1.6.2.2       tls  * are met:
     10  1.6.2.2       tls  * 1. Redistributions of source code must retain the above copyright
     11  1.6.2.2       tls  *    notice, this list of conditions and the following disclaimer.
     12  1.6.2.2       tls  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.6.2.2       tls  *    notice, this list of conditions and the following disclaimer in the
     14  1.6.2.2       tls  *    documentation and/or other materials provided with the distribution.
     15  1.6.2.2       tls  *
     16  1.6.2.2       tls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.6.2.2       tls  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.6.2.2       tls  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.6.2.2       tls  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.6.2.2       tls  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.6.2.2       tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.6.2.2       tls  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.6.2.2       tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.6.2.2       tls  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.6.2.2       tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.6.2.2       tls  * SUCH DAMAGE.
     27  1.6.2.2       tls  *
     28  1.6.2.2       tls  * $FreeBSD: src/sys/ufs/ufs/ufs_dirhash.c,v 1.3.2.8 2004/12/08 11:54:13 dwmalone Exp $
     29  1.6.2.2       tls  */
     30  1.6.2.2       tls 
     31  1.6.2.2       tls #include <sys/cdefs.h>
     32  1.6.2.2       tls __KERNEL_RCSID(0, "$NetBSD: ulfs_dirhash.c,v 1.6.2.4 2017/12/03 11:39:22 jdolecek Exp $");
     33  1.6.2.2       tls 
     34  1.6.2.2       tls /*
     35  1.6.2.2       tls  * This implements a hash-based lookup scheme for ULFS directories.
     36  1.6.2.2       tls  */
     37  1.6.2.2       tls 
     38  1.6.2.2       tls #include <sys/param.h>
     39  1.6.2.2       tls #include <sys/systm.h>
     40  1.6.2.2       tls #include <sys/kernel.h>
     41  1.6.2.2       tls #include <sys/kmem.h>
     42  1.6.2.2       tls #include <sys/types.h>
     43  1.6.2.2       tls #include <sys/hash.h>
     44  1.6.2.2       tls #include <sys/proc.h>
     45  1.6.2.2       tls #include <sys/buf.h>
     46  1.6.2.2       tls #include <sys/vnode.h>
     47  1.6.2.2       tls #include <sys/mount.h>
     48  1.6.2.2       tls #include <sys/pool.h>
     49  1.6.2.2       tls #include <sys/sysctl.h>
     50  1.6.2.2       tls #include <sys/atomic.h>
     51  1.6.2.2       tls 
     52  1.6.2.4  jdolecek #include <ufs/lfs/lfs.h>
     53  1.6.2.4  jdolecek #include <ufs/lfs/lfs_accessors.h>
     54  1.6.2.2       tls #include <ufs/lfs/ulfs_inode.h>
     55  1.6.2.2       tls #include <ufs/lfs/ulfs_dirhash.h>
     56  1.6.2.2       tls #include <ufs/lfs/ulfsmount.h>
     57  1.6.2.2       tls #include <ufs/lfs/ulfs_bswap.h>
     58  1.6.2.2       tls #include <ufs/lfs/ulfs_extern.h>
     59  1.6.2.2       tls 
     60  1.6.2.2       tls #define WRAPINCR(val, limit)	(((val) + 1 == (limit)) ? 0 : ((val) + 1))
     61  1.6.2.2       tls #define WRAPDECR(val, limit)	(((val) == 0) ? ((limit) - 1) : ((val) - 1))
     62  1.6.2.3       tls #define OFSFMT(ip)		((ip)->i_lfs->um_maxsymlinklen <= 0)
     63  1.6.2.2       tls #define BLKFREE2IDX(n)		((n) > DH_NFSTATS ? DH_NFSTATS : (n))
     64  1.6.2.2       tls 
     65  1.6.2.2       tls static u_int ulfs_dirhashminblks = 5;
     66  1.6.2.2       tls static u_int ulfs_dirhashmaxmem = 2 * 1024 * 1024;
     67  1.6.2.2       tls static u_int ulfs_dirhashmem;
     68  1.6.2.2       tls static u_int ulfs_dirhashcheck = 0;
     69  1.6.2.2       tls 
     70  1.6.2.2       tls static int ulfsdirhash_hash(struct dirhash *dh, const char *name, int namelen);
     71  1.6.2.2       tls static void ulfsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff,
     72  1.6.2.2       tls 	   int dirblksiz);
     73  1.6.2.2       tls static void ulfsdirhash_delslot(struct dirhash *dh, int slot);
     74  1.6.2.2       tls static int ulfsdirhash_findslot(struct dirhash *dh, const char *name,
     75  1.6.2.2       tls 	   int namelen, doff_t offset);
     76  1.6.2.4  jdolecek static doff_t ulfsdirhash_getprev(struct lfs *fs, LFS_DIRHEADER *dp,
     77  1.6.2.4  jdolecek 	   doff_t offset, int dirblksiz);
     78  1.6.2.2       tls static int ulfsdirhash_recycle(int wanted);
     79  1.6.2.2       tls 
     80  1.6.2.2       tls static pool_cache_t ulfsdirhashblk_cache;
     81  1.6.2.2       tls static pool_cache_t ulfsdirhash_cache;
     82  1.6.2.2       tls 
     83  1.6.2.2       tls #define DIRHASHLIST_LOCK()		mutex_enter(&ulfsdirhash_lock)
     84  1.6.2.2       tls #define DIRHASHLIST_UNLOCK()		mutex_exit(&ulfsdirhash_lock)
     85  1.6.2.2       tls #define DIRHASH_LOCK(dh)		mutex_enter(&(dh)->dh_lock)
     86  1.6.2.2       tls #define DIRHASH_UNLOCK(dh)		mutex_exit(&(dh)->dh_lock)
     87  1.6.2.2       tls #define DIRHASH_BLKALLOC()		\
     88  1.6.2.2       tls     pool_cache_get(ulfsdirhashblk_cache, PR_NOWAIT)
     89  1.6.2.2       tls #define DIRHASH_BLKFREE(ptr)		\
     90  1.6.2.2       tls     pool_cache_put(ulfsdirhashblk_cache, ptr)
     91  1.6.2.2       tls 
     92  1.6.2.2       tls /* Dirhash list; recently-used entries are near the tail. */
     93  1.6.2.2       tls static TAILQ_HEAD(, dirhash) ulfsdirhash_list;
     94  1.6.2.2       tls 
     95  1.6.2.2       tls /* Protects: ulfsdirhash_list, `dh_list' field, ulfs_dirhashmem. */
     96  1.6.2.2       tls static kmutex_t ulfsdirhash_lock;
     97  1.6.2.2       tls 
     98  1.6.2.2       tls static struct sysctllog *ulfsdirhash_sysctl_log;
     99  1.6.2.2       tls 
    100  1.6.2.2       tls /*
    101  1.6.2.2       tls  * Locking order:
    102  1.6.2.2       tls  *	ulfsdirhash_lock
    103  1.6.2.2       tls  *	dh_lock
    104  1.6.2.2       tls  *
    105  1.6.2.2       tls  * The dh_lock mutex should be acquired either via the inode lock, or via
    106  1.6.2.2       tls  * ulfsdirhash_lock. Only the owner of the inode may free the associated
    107  1.6.2.2       tls  * dirhash, but anything can steal its memory and set dh_hash to NULL.
    108  1.6.2.2       tls  */
    109  1.6.2.2       tls 
    110  1.6.2.2       tls /*
    111  1.6.2.2       tls  * Attempt to build up a hash table for the directory contents in
    112  1.6.2.2       tls  * inode 'ip'. Returns 0 on success, or -1 of the operation failed.
    113  1.6.2.2       tls  */
    114  1.6.2.2       tls int
    115  1.6.2.2       tls ulfsdirhash_build(struct inode *ip)
    116  1.6.2.2       tls {
    117  1.6.2.4  jdolecek 	struct lfs *fs = ip->i_lfs;
    118  1.6.2.2       tls 	struct dirhash *dh;
    119  1.6.2.2       tls 	struct buf *bp = NULL;
    120  1.6.2.4  jdolecek 	LFS_DIRHEADER *ep;
    121  1.6.2.2       tls 	struct vnode *vp;
    122  1.6.2.2       tls 	doff_t bmask, pos;
    123  1.6.2.2       tls 	int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot;
    124  1.6.2.3       tls 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    125  1.6.2.2       tls 
    126  1.6.2.2       tls 	/* Check if we can/should use dirhash. */
    127  1.6.2.2       tls 	if (ip->i_dirhash == NULL) {
    128  1.6.2.2       tls 		if (ip->i_size < (ulfs_dirhashminblks * dirblksiz) || OFSFMT(ip))
    129  1.6.2.2       tls 			return (-1);
    130  1.6.2.2       tls 	} else {
    131  1.6.2.2       tls 		/* Hash exists, but sysctls could have changed. */
    132  1.6.2.2       tls 		if (ip->i_size < (ulfs_dirhashminblks * dirblksiz) ||
    133  1.6.2.2       tls 		    ulfs_dirhashmem > ulfs_dirhashmaxmem) {
    134  1.6.2.2       tls 			ulfsdirhash_free(ip);
    135  1.6.2.2       tls 			return (-1);
    136  1.6.2.2       tls 		}
    137  1.6.2.2       tls 		/* Check if hash exists and is intact (note: unlocked read). */
    138  1.6.2.2       tls 		if (ip->i_dirhash->dh_hash != NULL)
    139  1.6.2.2       tls 			return (0);
    140  1.6.2.2       tls 		/* Free the old, recycled hash and build a new one. */
    141  1.6.2.2       tls 		ulfsdirhash_free(ip);
    142  1.6.2.2       tls 	}
    143  1.6.2.2       tls 
    144  1.6.2.2       tls 	/* Don't hash removed directories. */
    145  1.6.2.2       tls 	if (ip->i_nlink == 0)
    146  1.6.2.2       tls 		return (-1);
    147  1.6.2.2       tls 
    148  1.6.2.2       tls 	vp = ip->i_vnode;
    149  1.6.2.2       tls 	/* Allocate 50% more entries than this dir size could ever need. */
    150  1.6.2.2       tls 	KASSERT(ip->i_size >= dirblksiz);
    151  1.6.2.4  jdolecek 	nslots = ip->i_size / LFS_DIRECTSIZ(fs, 1);
    152  1.6.2.2       tls 	nslots = (nslots * 3 + 1) / 2;
    153  1.6.2.2       tls 	narrays = howmany(nslots, DH_NBLKOFF);
    154  1.6.2.2       tls 	nslots = narrays * DH_NBLKOFF;
    155  1.6.2.2       tls 	dirblocks = howmany(ip->i_size, dirblksiz);
    156  1.6.2.2       tls 	nblocks = (dirblocks * 3 + 1) / 2;
    157  1.6.2.2       tls 
    158  1.6.2.2       tls 	memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) +
    159  1.6.2.2       tls 	    narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) +
    160  1.6.2.2       tls 	    nblocks * sizeof(*dh->dh_blkfree);
    161  1.6.2.2       tls 
    162  1.6.2.2       tls 	while (atomic_add_int_nv(&ulfs_dirhashmem, memreqd) >
    163  1.6.2.2       tls 	    ulfs_dirhashmaxmem) {
    164  1.6.2.2       tls 		atomic_add_int(&ulfs_dirhashmem, -memreqd);
    165  1.6.2.2       tls 		if (memreqd > ulfs_dirhashmaxmem / 2)
    166  1.6.2.2       tls 			return (-1);
    167  1.6.2.2       tls 		/* Try to free some space. */
    168  1.6.2.2       tls 		if (ulfsdirhash_recycle(memreqd) != 0)
    169  1.6.2.2       tls 			return (-1);
    170  1.6.2.2       tls 	        else
    171  1.6.2.2       tls 		    	DIRHASHLIST_UNLOCK();
    172  1.6.2.2       tls 	}
    173  1.6.2.2       tls 
    174  1.6.2.2       tls 	/*
    175  1.6.2.2       tls 	 * Use non-blocking mallocs so that we will revert to a linear
    176  1.6.2.2       tls 	 * lookup on failure rather than potentially blocking forever.
    177  1.6.2.2       tls 	 */
    178  1.6.2.2       tls 	dh = pool_cache_get(ulfsdirhash_cache, PR_NOWAIT);
    179  1.6.2.2       tls 	if (dh == NULL) {
    180  1.6.2.2       tls 		atomic_add_int(&ulfs_dirhashmem, -memreqd);
    181  1.6.2.2       tls 		return (-1);
    182  1.6.2.2       tls 	}
    183  1.6.2.2       tls 	memset(dh, 0, sizeof(*dh));
    184  1.6.2.2       tls 	mutex_init(&dh->dh_lock, MUTEX_DEFAULT, IPL_NONE);
    185  1.6.2.2       tls 	DIRHASH_LOCK(dh);
    186  1.6.2.2       tls 	dh->dh_hashsz = narrays * sizeof(dh->dh_hash[0]);
    187  1.6.2.2       tls 	dh->dh_hash = kmem_zalloc(dh->dh_hashsz, KM_NOSLEEP);
    188  1.6.2.2       tls 	dh->dh_blkfreesz = nblocks * sizeof(dh->dh_blkfree[0]);
    189  1.6.2.2       tls 	dh->dh_blkfree = kmem_zalloc(dh->dh_blkfreesz, KM_NOSLEEP);
    190  1.6.2.2       tls 	if (dh->dh_hash == NULL || dh->dh_blkfree == NULL)
    191  1.6.2.2       tls 		goto fail;
    192  1.6.2.2       tls 	for (i = 0; i < narrays; i++) {
    193  1.6.2.2       tls 		if ((dh->dh_hash[i] = DIRHASH_BLKALLOC()) == NULL)
    194  1.6.2.2       tls 			goto fail;
    195  1.6.2.2       tls 		for (j = 0; j < DH_NBLKOFF; j++)
    196  1.6.2.2       tls 			dh->dh_hash[i][j] = DIRHASH_EMPTY;
    197  1.6.2.2       tls 	}
    198  1.6.2.2       tls 
    199  1.6.2.2       tls 	/* Initialise the hash table and block statistics. */
    200  1.6.2.2       tls 	dh->dh_narrays = narrays;
    201  1.6.2.2       tls 	dh->dh_hlen = nslots;
    202  1.6.2.2       tls 	dh->dh_nblk = nblocks;
    203  1.6.2.2       tls 	dh->dh_dirblks = dirblocks;
    204  1.6.2.2       tls 	for (i = 0; i < dirblocks; i++)
    205  1.6.2.2       tls 		dh->dh_blkfree[i] = dirblksiz / DIRALIGN;
    206  1.6.2.2       tls 	for (i = 0; i < DH_NFSTATS; i++)
    207  1.6.2.2       tls 		dh->dh_firstfree[i] = -1;
    208  1.6.2.2       tls 	dh->dh_firstfree[DH_NFSTATS] = 0;
    209  1.6.2.2       tls 	dh->dh_seqopt = 0;
    210  1.6.2.2       tls 	dh->dh_seqoff = 0;
    211  1.6.2.2       tls 	dh->dh_score = DH_SCOREINIT;
    212  1.6.2.2       tls 	ip->i_dirhash = dh;
    213  1.6.2.2       tls 
    214  1.6.2.2       tls 	bmask = VFSTOULFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
    215  1.6.2.2       tls 	pos = 0;
    216  1.6.2.2       tls 	while (pos < ip->i_size) {
    217  1.6.2.2       tls 		if ((curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    218  1.6.2.2       tls 		    != 0) {
    219  1.6.2.2       tls 			preempt();
    220  1.6.2.2       tls 		}
    221  1.6.2.2       tls 		/* If necessary, get the next directory block. */
    222  1.6.2.2       tls 		if ((pos & bmask) == 0) {
    223  1.6.2.2       tls 			if (bp != NULL)
    224  1.6.2.2       tls 				brelse(bp, 0);
    225  1.6.2.2       tls 			if (ulfs_blkatoff(vp, (off_t)pos, NULL, &bp, false) != 0)
    226  1.6.2.2       tls 				goto fail;
    227  1.6.2.2       tls 		}
    228  1.6.2.2       tls 
    229  1.6.2.2       tls 		/* Add this entry to the hash. */
    230  1.6.2.4  jdolecek 		ep = (LFS_DIRHEADER *)((char *)bp->b_data + (pos & bmask));
    231  1.6.2.4  jdolecek 		if (lfs_dir_getreclen(fs, ep) == 0 || lfs_dir_getreclen(fs, ep) >
    232  1.6.2.2       tls 		    dirblksiz - (pos & (dirblksiz - 1))) {
    233  1.6.2.2       tls 			/* Corrupted directory. */
    234  1.6.2.2       tls 			brelse(bp, 0);
    235  1.6.2.2       tls 			goto fail;
    236  1.6.2.2       tls 		}
    237  1.6.2.4  jdolecek 		if (lfs_dir_getino(fs, ep) != 0) {
    238  1.6.2.2       tls 			/* Add the entry (simplified ulfsdirhash_add). */
    239  1.6.2.4  jdolecek 			slot = ulfsdirhash_hash(dh, lfs_dir_nameptr(fs, ep),
    240  1.6.2.4  jdolecek 						lfs_dir_getnamlen(fs, ep));
    241  1.6.2.2       tls 			while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY)
    242  1.6.2.2       tls 				slot = WRAPINCR(slot, dh->dh_hlen);
    243  1.6.2.2       tls 			dh->dh_hused++;
    244  1.6.2.2       tls 			DH_ENTRY(dh, slot) = pos;
    245  1.6.2.4  jdolecek 			ulfsdirhash_adjfree(dh, pos, -LFS_DIRSIZ(fs, ep),
    246  1.6.2.2       tls 			    dirblksiz);
    247  1.6.2.2       tls 		}
    248  1.6.2.4  jdolecek 		pos += lfs_dir_getreclen(fs, ep);
    249  1.6.2.2       tls 	}
    250  1.6.2.2       tls 
    251  1.6.2.2       tls 	if (bp != NULL)
    252  1.6.2.2       tls 		brelse(bp, 0);
    253  1.6.2.2       tls 	DIRHASHLIST_LOCK();
    254  1.6.2.2       tls 	TAILQ_INSERT_TAIL(&ulfsdirhash_list, dh, dh_list);
    255  1.6.2.2       tls 	dh->dh_onlist = 1;
    256  1.6.2.2       tls 	DIRHASH_UNLOCK(dh);
    257  1.6.2.2       tls 	DIRHASHLIST_UNLOCK();
    258  1.6.2.2       tls 	return (0);
    259  1.6.2.2       tls 
    260  1.6.2.2       tls fail:
    261  1.6.2.4  jdolecek 	ip->i_dirhash = NULL;
    262  1.6.2.2       tls 	DIRHASH_UNLOCK(dh);
    263  1.6.2.2       tls 	if (dh->dh_hash != NULL) {
    264  1.6.2.2       tls 		for (i = 0; i < narrays; i++)
    265  1.6.2.2       tls 			if (dh->dh_hash[i] != NULL)
    266  1.6.2.2       tls 				DIRHASH_BLKFREE(dh->dh_hash[i]);
    267  1.6.2.2       tls 		kmem_free(dh->dh_hash, dh->dh_hashsz);
    268  1.6.2.2       tls 	}
    269  1.6.2.2       tls 	if (dh->dh_blkfree != NULL)
    270  1.6.2.2       tls 		kmem_free(dh->dh_blkfree, dh->dh_blkfreesz);
    271  1.6.2.2       tls 	mutex_destroy(&dh->dh_lock);
    272  1.6.2.2       tls 	pool_cache_put(ulfsdirhash_cache, dh);
    273  1.6.2.2       tls 	atomic_add_int(&ulfs_dirhashmem, -memreqd);
    274  1.6.2.2       tls 	return (-1);
    275  1.6.2.2       tls }
    276  1.6.2.2       tls 
    277  1.6.2.2       tls /*
    278  1.6.2.2       tls  * Free any hash table associated with inode 'ip'.
    279  1.6.2.2       tls  */
    280  1.6.2.2       tls void
    281  1.6.2.2       tls ulfsdirhash_free(struct inode *ip)
    282  1.6.2.2       tls {
    283  1.6.2.2       tls 	struct dirhash *dh;
    284  1.6.2.2       tls 	int i, mem;
    285  1.6.2.2       tls 
    286  1.6.2.2       tls 	if ((dh = ip->i_dirhash) == NULL)
    287  1.6.2.2       tls 		return;
    288  1.6.2.2       tls 
    289  1.6.2.4  jdolecek 	ip->i_dirhash = NULL;
    290  1.6.2.4  jdolecek 
    291  1.6.2.2       tls 	if (dh->dh_onlist) {
    292  1.6.2.2       tls 		DIRHASHLIST_LOCK();
    293  1.6.2.2       tls 		if (dh->dh_onlist)
    294  1.6.2.2       tls 			TAILQ_REMOVE(&ulfsdirhash_list, dh, dh_list);
    295  1.6.2.2       tls 		DIRHASHLIST_UNLOCK();
    296  1.6.2.2       tls 	}
    297  1.6.2.2       tls 
    298  1.6.2.2       tls 	/* The dirhash pointed to by 'dh' is exclusively ours now. */
    299  1.6.2.2       tls 	mem = sizeof(*dh);
    300  1.6.2.2       tls 	if (dh->dh_hash != NULL) {
    301  1.6.2.2       tls 		for (i = 0; i < dh->dh_narrays; i++)
    302  1.6.2.2       tls 			DIRHASH_BLKFREE(dh->dh_hash[i]);
    303  1.6.2.2       tls 		kmem_free(dh->dh_hash, dh->dh_hashsz);
    304  1.6.2.2       tls 		kmem_free(dh->dh_blkfree, dh->dh_blkfreesz);
    305  1.6.2.2       tls 		mem += dh->dh_hashsz;
    306  1.6.2.2       tls 		mem += dh->dh_narrays * DH_NBLKOFF * sizeof(**dh->dh_hash);
    307  1.6.2.2       tls 		mem += dh->dh_nblk * sizeof(*dh->dh_blkfree);
    308  1.6.2.2       tls 	}
    309  1.6.2.2       tls 	mutex_destroy(&dh->dh_lock);
    310  1.6.2.2       tls 	pool_cache_put(ulfsdirhash_cache, dh);
    311  1.6.2.2       tls 
    312  1.6.2.2       tls 	atomic_add_int(&ulfs_dirhashmem, -mem);
    313  1.6.2.2       tls }
    314  1.6.2.2       tls 
    315  1.6.2.2       tls /*
    316  1.6.2.2       tls  * Find the offset of the specified name within the given inode.
    317  1.6.2.2       tls  * Returns 0 on success, ENOENT if the entry does not exist, or
    318  1.6.2.2       tls  * EJUSTRETURN if the caller should revert to a linear search.
    319  1.6.2.2       tls  *
    320  1.6.2.2       tls  * If successful, the directory offset is stored in *offp, and a
    321  1.6.2.2       tls  * pointer to a struct buf containing the entry is stored in *bpp. If
    322  1.6.2.2       tls  * prevoffp is non-NULL, the offset of the previous entry within
    323  1.6.2.2       tls  * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry
    324  1.6.2.2       tls  * is the first in a block, the start of the block is used).
    325  1.6.2.2       tls  */
    326  1.6.2.2       tls int
    327  1.6.2.2       tls ulfsdirhash_lookup(struct inode *ip, const char *name, int namelen, doff_t *offp,
    328  1.6.2.2       tls     struct buf **bpp, doff_t *prevoffp)
    329  1.6.2.2       tls {
    330  1.6.2.4  jdolecek 	struct lfs *fs = ip->i_lfs;
    331  1.6.2.2       tls 	struct dirhash *dh, *dh_next;
    332  1.6.2.4  jdolecek 	LFS_DIRHEADER *dp;
    333  1.6.2.2       tls 	struct vnode *vp;
    334  1.6.2.2       tls 	struct buf *bp;
    335  1.6.2.2       tls 	doff_t blkoff, bmask, offset, prevoff;
    336  1.6.2.2       tls 	int i, slot;
    337  1.6.2.3       tls 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    338  1.6.2.2       tls 
    339  1.6.2.2       tls 	if ((dh = ip->i_dirhash) == NULL)
    340  1.6.2.2       tls 		return (EJUSTRETURN);
    341  1.6.2.2       tls 
    342  1.6.2.2       tls 	/*
    343  1.6.2.2       tls 	 * Move this dirhash towards the end of the list if it has a
    344  1.6.2.2       tls 	 * score higher than the next entry, and acquire the dh_lock.
    345  1.6.2.2       tls 	 * Optimise the case where it's already the last by performing
    346  1.6.2.2       tls 	 * an unlocked read of the TAILQ_NEXT pointer.
    347  1.6.2.2       tls 	 *
    348  1.6.2.2       tls 	 * In both cases, end up holding just dh_lock.
    349  1.6.2.2       tls 	 */
    350  1.6.2.2       tls 	if (TAILQ_NEXT(dh, dh_list) != NULL) {
    351  1.6.2.2       tls 		DIRHASHLIST_LOCK();
    352  1.6.2.2       tls 		DIRHASH_LOCK(dh);
    353  1.6.2.2       tls 		/*
    354  1.6.2.2       tls 		 * If the new score will be greater than that of the next
    355  1.6.2.2       tls 		 * entry, then move this entry past it. With both mutexes
    356  1.6.2.2       tls 		 * held, dh_next won't go away, but its dh_score could
    357  1.6.2.2       tls 		 * change; that's not important since it is just a hint.
    358  1.6.2.2       tls 		 */
    359  1.6.2.2       tls 		if (dh->dh_hash != NULL &&
    360  1.6.2.2       tls 		    (dh_next = TAILQ_NEXT(dh, dh_list)) != NULL &&
    361  1.6.2.2       tls 		    dh->dh_score >= dh_next->dh_score) {
    362  1.6.2.2       tls 			KASSERT(dh->dh_onlist);
    363  1.6.2.2       tls 			TAILQ_REMOVE(&ulfsdirhash_list, dh, dh_list);
    364  1.6.2.2       tls 			TAILQ_INSERT_AFTER(&ulfsdirhash_list, dh_next, dh,
    365  1.6.2.2       tls 			    dh_list);
    366  1.6.2.2       tls 		}
    367  1.6.2.2       tls 		DIRHASHLIST_UNLOCK();
    368  1.6.2.2       tls 	} else {
    369  1.6.2.2       tls 		/* Already the last, though that could change as we wait. */
    370  1.6.2.2       tls 		DIRHASH_LOCK(dh);
    371  1.6.2.2       tls 	}
    372  1.6.2.2       tls 	if (dh->dh_hash == NULL) {
    373  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    374  1.6.2.2       tls 		ulfsdirhash_free(ip);
    375  1.6.2.2       tls 		return (EJUSTRETURN);
    376  1.6.2.2       tls 	}
    377  1.6.2.2       tls 
    378  1.6.2.2       tls 	/* Update the score. */
    379  1.6.2.2       tls 	if (dh->dh_score < DH_SCOREMAX)
    380  1.6.2.2       tls 		dh->dh_score++;
    381  1.6.2.2       tls 
    382  1.6.2.2       tls 	vp = ip->i_vnode;
    383  1.6.2.2       tls 	bmask = VFSTOULFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
    384  1.6.2.2       tls 	blkoff = -1;
    385  1.6.2.2       tls 	bp = NULL;
    386  1.6.2.2       tls restart:
    387  1.6.2.2       tls 	slot = ulfsdirhash_hash(dh, name, namelen);
    388  1.6.2.2       tls 
    389  1.6.2.2       tls 	if (dh->dh_seqopt) {
    390  1.6.2.2       tls 		/*
    391  1.6.2.2       tls 		 * Sequential access optimisation. dh_seqoff contains the
    392  1.6.2.2       tls 		 * offset of the directory entry immediately following
    393  1.6.2.2       tls 		 * the last entry that was looked up. Check if this offset
    394  1.6.2.2       tls 		 * appears in the hash chain for the name we are looking for.
    395  1.6.2.2       tls 		 */
    396  1.6.2.2       tls 		for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY;
    397  1.6.2.2       tls 		    i = WRAPINCR(i, dh->dh_hlen))
    398  1.6.2.2       tls 			if (offset == dh->dh_seqoff)
    399  1.6.2.2       tls 				break;
    400  1.6.2.2       tls 		if (offset == dh->dh_seqoff) {
    401  1.6.2.2       tls 			/*
    402  1.6.2.2       tls 			 * We found an entry with the expected offset. This
    403  1.6.2.2       tls 			 * is probably the entry we want, but if not, the
    404  1.6.2.2       tls 			 * code below will turn off seqoff and retry.
    405  1.6.2.2       tls 			 */
    406  1.6.2.2       tls 			slot = i;
    407  1.6.2.2       tls 		} else
    408  1.6.2.2       tls 			dh->dh_seqopt = 0;
    409  1.6.2.2       tls 	}
    410  1.6.2.2       tls 
    411  1.6.2.2       tls 	for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY;
    412  1.6.2.2       tls 	    slot = WRAPINCR(slot, dh->dh_hlen)) {
    413  1.6.2.2       tls 		if (offset == DIRHASH_DEL)
    414  1.6.2.2       tls 			continue;
    415  1.6.2.2       tls 
    416  1.6.2.2       tls 		if (offset < 0 || offset >= ip->i_size)
    417  1.6.2.2       tls 			panic("ulfsdirhash_lookup: bad offset in hash array");
    418  1.6.2.2       tls 		if ((offset & ~bmask) != blkoff) {
    419  1.6.2.2       tls 			if (bp != NULL)
    420  1.6.2.2       tls 				brelse(bp, 0);
    421  1.6.2.2       tls 			blkoff = offset & ~bmask;
    422  1.6.2.2       tls 			if (ulfs_blkatoff(vp, (off_t)blkoff,
    423  1.6.2.2       tls 			    NULL, &bp, false) != 0) {
    424  1.6.2.2       tls 				DIRHASH_UNLOCK(dh);
    425  1.6.2.2       tls 				return (EJUSTRETURN);
    426  1.6.2.2       tls 			}
    427  1.6.2.2       tls 		}
    428  1.6.2.4  jdolecek 		dp = (LFS_DIRHEADER *)((char *)bp->b_data + (offset & bmask));
    429  1.6.2.4  jdolecek 		if (lfs_dir_getreclen(fs, dp) == 0 || lfs_dir_getreclen(fs, dp) >
    430  1.6.2.2       tls 		    dirblksiz - (offset & (dirblksiz - 1))) {
    431  1.6.2.2       tls 			/* Corrupted directory. */
    432  1.6.2.2       tls 			DIRHASH_UNLOCK(dh);
    433  1.6.2.2       tls 			brelse(bp, 0);
    434  1.6.2.2       tls 			return (EJUSTRETURN);
    435  1.6.2.2       tls 		}
    436  1.6.2.4  jdolecek 		if (lfs_dir_getnamlen(fs, dp) == namelen &&
    437  1.6.2.4  jdolecek 		    memcmp(lfs_dir_nameptr(fs, dp), name, namelen) == 0) {
    438  1.6.2.2       tls 			/* Found. Get the prev offset if needed. */
    439  1.6.2.2       tls 			if (prevoffp != NULL) {
    440  1.6.2.2       tls 				if (offset & (dirblksiz - 1)) {
    441  1.6.2.4  jdolecek 					prevoff = ulfsdirhash_getprev(fs, dp,
    442  1.6.2.2       tls 					    offset, dirblksiz);
    443  1.6.2.2       tls 					if (prevoff == -1) {
    444  1.6.2.2       tls 						brelse(bp, 0);
    445  1.6.2.2       tls 						return (EJUSTRETURN);
    446  1.6.2.2       tls 					}
    447  1.6.2.2       tls 				} else
    448  1.6.2.2       tls 					prevoff = offset;
    449  1.6.2.2       tls 				*prevoffp = prevoff;
    450  1.6.2.2       tls 			}
    451  1.6.2.2       tls 
    452  1.6.2.2       tls 			/* Check for sequential access, and update offset. */
    453  1.6.2.2       tls 			if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset)
    454  1.6.2.2       tls 				dh->dh_seqopt = 1;
    455  1.6.2.4  jdolecek 			dh->dh_seqoff = offset + LFS_DIRSIZ(fs, dp);
    456  1.6.2.2       tls 			DIRHASH_UNLOCK(dh);
    457  1.6.2.2       tls 
    458  1.6.2.2       tls 			*bpp = bp;
    459  1.6.2.2       tls 			*offp = offset;
    460  1.6.2.2       tls 			return (0);
    461  1.6.2.2       tls 		}
    462  1.6.2.2       tls 
    463  1.6.2.2       tls 		if (dh->dh_hash == NULL) {
    464  1.6.2.2       tls 			DIRHASH_UNLOCK(dh);
    465  1.6.2.2       tls 			if (bp != NULL)
    466  1.6.2.2       tls 				brelse(bp, 0);
    467  1.6.2.2       tls 			ulfsdirhash_free(ip);
    468  1.6.2.2       tls 			return (EJUSTRETURN);
    469  1.6.2.2       tls 		}
    470  1.6.2.2       tls 		/*
    471  1.6.2.2       tls 		 * When the name doesn't match in the seqopt case, go back
    472  1.6.2.2       tls 		 * and search normally.
    473  1.6.2.2       tls 		 */
    474  1.6.2.2       tls 		if (dh->dh_seqopt) {
    475  1.6.2.2       tls 			dh->dh_seqopt = 0;
    476  1.6.2.2       tls 			goto restart;
    477  1.6.2.2       tls 		}
    478  1.6.2.2       tls 	}
    479  1.6.2.2       tls 	DIRHASH_UNLOCK(dh);
    480  1.6.2.2       tls 	if (bp != NULL)
    481  1.6.2.2       tls 		brelse(bp, 0);
    482  1.6.2.2       tls 	return (ENOENT);
    483  1.6.2.2       tls }
    484  1.6.2.2       tls 
    485  1.6.2.2       tls /*
    486  1.6.2.2       tls  * Find a directory block with room for 'slotneeded' bytes. Returns
    487  1.6.2.2       tls  * the offset of the directory entry that begins the free space.
    488  1.6.2.2       tls  * This will either be the offset of an existing entry that has free
    489  1.6.2.2       tls  * space at the end, or the offset of an entry with d_ino == 0 at
    490  1.6.2.2       tls  * the start of a DIRBLKSIZ block.
    491  1.6.2.2       tls  *
    492  1.6.2.2       tls  * To use the space, the caller may need to compact existing entries in
    493  1.6.2.2       tls  * the directory. The total number of bytes in all of the entries involved
    494  1.6.2.2       tls  * in the compaction is stored in *slotsize. In other words, all of
    495  1.6.2.2       tls  * the entries that must be compacted are exactly contained in the
    496  1.6.2.2       tls  * region beginning at the returned offset and spanning *slotsize bytes.
    497  1.6.2.2       tls  *
    498  1.6.2.2       tls  * Returns -1 if no space was found, indicating that the directory
    499  1.6.2.2       tls  * must be extended.
    500  1.6.2.2       tls  */
    501  1.6.2.2       tls doff_t
    502  1.6.2.2       tls ulfsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize)
    503  1.6.2.2       tls {
    504  1.6.2.4  jdolecek 	struct lfs *fs = ip->i_lfs;
    505  1.6.2.4  jdolecek 	LFS_DIRHEADER *dp;
    506  1.6.2.2       tls 	struct dirhash *dh;
    507  1.6.2.2       tls 	struct buf *bp;
    508  1.6.2.2       tls 	doff_t pos, slotstart;
    509  1.6.2.2       tls 	int dirblock, error, freebytes, i;
    510  1.6.2.3       tls 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    511  1.6.2.2       tls 
    512  1.6.2.2       tls 	if ((dh = ip->i_dirhash) == NULL)
    513  1.6.2.2       tls 		return (-1);
    514  1.6.2.2       tls 
    515  1.6.2.2       tls 	DIRHASH_LOCK(dh);
    516  1.6.2.2       tls 	if (dh->dh_hash == NULL) {
    517  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    518  1.6.2.2       tls 		ulfsdirhash_free(ip);
    519  1.6.2.2       tls 		return (-1);
    520  1.6.2.2       tls 	}
    521  1.6.2.2       tls 
    522  1.6.2.2       tls 	/* Find a directory block with the desired free space. */
    523  1.6.2.2       tls 	dirblock = -1;
    524  1.6.2.2       tls 	for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++)
    525  1.6.2.2       tls 		if ((dirblock = dh->dh_firstfree[i]) != -1)
    526  1.6.2.2       tls 			break;
    527  1.6.2.2       tls 	if (dirblock == -1) {
    528  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    529  1.6.2.2       tls 		return (-1);
    530  1.6.2.2       tls 	}
    531  1.6.2.2       tls 
    532  1.6.2.2       tls 	KASSERT(dirblock < dh->dh_nblk &&
    533  1.6.2.2       tls 	    dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN));
    534  1.6.2.2       tls 	pos = dirblock * dirblksiz;
    535  1.6.2.2       tls 	error = ulfs_blkatoff(ip->i_vnode, (off_t)pos, (void *)&dp, &bp, false);
    536  1.6.2.2       tls 	if (error) {
    537  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    538  1.6.2.2       tls 		return (-1);
    539  1.6.2.2       tls 	}
    540  1.6.2.2       tls 	/* Find the first entry with free space. */
    541  1.6.2.2       tls 	for (i = 0; i < dirblksiz; ) {
    542  1.6.2.4  jdolecek 		if (lfs_dir_getreclen(fs, dp) == 0) {
    543  1.6.2.2       tls 			DIRHASH_UNLOCK(dh);
    544  1.6.2.2       tls 			brelse(bp, 0);
    545  1.6.2.2       tls 			return (-1);
    546  1.6.2.2       tls 		}
    547  1.6.2.4  jdolecek 		if (lfs_dir_getino(fs, dp) == 0 || lfs_dir_getreclen(fs, dp) > LFS_DIRSIZ(fs, dp))
    548  1.6.2.2       tls 			break;
    549  1.6.2.4  jdolecek 		i += lfs_dir_getreclen(fs, dp);
    550  1.6.2.4  jdolecek 		dp = LFS_NEXTDIR(fs, dp);
    551  1.6.2.2       tls 	}
    552  1.6.2.2       tls 	if (i > dirblksiz) {
    553  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    554  1.6.2.2       tls 		brelse(bp, 0);
    555  1.6.2.2       tls 		return (-1);
    556  1.6.2.2       tls 	}
    557  1.6.2.2       tls 	slotstart = pos + i;
    558  1.6.2.2       tls 
    559  1.6.2.2       tls 	/* Find the range of entries needed to get enough space */
    560  1.6.2.2       tls 	freebytes = 0;
    561  1.6.2.2       tls 	while (i < dirblksiz && freebytes < slotneeded) {
    562  1.6.2.4  jdolecek 		freebytes += lfs_dir_getreclen(fs, dp);
    563  1.6.2.4  jdolecek 		if (lfs_dir_getino(fs, dp) != 0)
    564  1.6.2.4  jdolecek 			freebytes -= LFS_DIRSIZ(fs, dp);
    565  1.6.2.4  jdolecek 		if (lfs_dir_getreclen(fs, dp) == 0) {
    566  1.6.2.2       tls 			DIRHASH_UNLOCK(dh);
    567  1.6.2.2       tls 			brelse(bp, 0);
    568  1.6.2.2       tls 			return (-1);
    569  1.6.2.2       tls 		}
    570  1.6.2.4  jdolecek 		i += lfs_dir_getreclen(fs, dp);
    571  1.6.2.4  jdolecek 		dp = LFS_NEXTDIR(fs, dp);
    572  1.6.2.2       tls 	}
    573  1.6.2.2       tls 	if (i > dirblksiz) {
    574  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    575  1.6.2.2       tls 		brelse(bp, 0);
    576  1.6.2.2       tls 		return (-1);
    577  1.6.2.2       tls 	}
    578  1.6.2.2       tls 	if (freebytes < slotneeded)
    579  1.6.2.2       tls 		panic("ulfsdirhash_findfree: free mismatch");
    580  1.6.2.2       tls 	DIRHASH_UNLOCK(dh);
    581  1.6.2.2       tls 	brelse(bp, 0);
    582  1.6.2.2       tls 	*slotsize = pos + i - slotstart;
    583  1.6.2.2       tls 	return (slotstart);
    584  1.6.2.2       tls }
    585  1.6.2.2       tls 
    586  1.6.2.2       tls /*
    587  1.6.2.2       tls  * Return the start of the unused space at the end of a directory, or
    588  1.6.2.2       tls  * -1 if there are no trailing unused blocks.
    589  1.6.2.2       tls  */
    590  1.6.2.2       tls doff_t
    591  1.6.2.2       tls ulfsdirhash_enduseful(struct inode *ip)
    592  1.6.2.2       tls {
    593  1.6.2.2       tls 	struct dirhash *dh;
    594  1.6.2.2       tls 	int i;
    595  1.6.2.3       tls 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    596  1.6.2.2       tls 
    597  1.6.2.2       tls 	if ((dh = ip->i_dirhash) == NULL)
    598  1.6.2.2       tls 		return (-1);
    599  1.6.2.2       tls 
    600  1.6.2.2       tls 	DIRHASH_LOCK(dh);
    601  1.6.2.2       tls 	if (dh->dh_hash == NULL) {
    602  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    603  1.6.2.2       tls 		ulfsdirhash_free(ip);
    604  1.6.2.2       tls 		return (-1);
    605  1.6.2.2       tls 	}
    606  1.6.2.2       tls 
    607  1.6.2.2       tls 	if (dh->dh_blkfree[dh->dh_dirblks - 1] != dirblksiz / DIRALIGN) {
    608  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    609  1.6.2.2       tls 		return (-1);
    610  1.6.2.2       tls 	}
    611  1.6.2.2       tls 
    612  1.6.2.2       tls 	for (i = dh->dh_dirblks - 1; i >= 0; i--)
    613  1.6.2.2       tls 		if (dh->dh_blkfree[i] != dirblksiz / DIRALIGN)
    614  1.6.2.2       tls 			break;
    615  1.6.2.2       tls 	DIRHASH_UNLOCK(dh);
    616  1.6.2.2       tls 	return ((doff_t)(i + 1) * dirblksiz);
    617  1.6.2.2       tls }
    618  1.6.2.2       tls 
    619  1.6.2.2       tls /*
    620  1.6.2.2       tls  * Insert information into the hash about a new directory entry. dirp
    621  1.6.2.2       tls  * points to a struct lfs_direct containing the entry, and offset specifies
    622  1.6.2.2       tls  * the offset of this entry.
    623  1.6.2.2       tls  */
    624  1.6.2.2       tls void
    625  1.6.2.4  jdolecek ulfsdirhash_add(struct inode *ip, LFS_DIRHEADER *dirp, doff_t offset)
    626  1.6.2.2       tls {
    627  1.6.2.4  jdolecek 	struct lfs *fs = ip->i_lfs;
    628  1.6.2.2       tls 	struct dirhash *dh;
    629  1.6.2.2       tls 	int slot;
    630  1.6.2.3       tls 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    631  1.6.2.2       tls 
    632  1.6.2.2       tls 	if ((dh = ip->i_dirhash) == NULL)
    633  1.6.2.2       tls 		return;
    634  1.6.2.2       tls 
    635  1.6.2.2       tls 	DIRHASH_LOCK(dh);
    636  1.6.2.2       tls 	if (dh->dh_hash == NULL) {
    637  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    638  1.6.2.2       tls 		ulfsdirhash_free(ip);
    639  1.6.2.2       tls 		return;
    640  1.6.2.2       tls 	}
    641  1.6.2.2       tls 
    642  1.6.2.2       tls 	KASSERT(offset < dh->dh_dirblks * dirblksiz);
    643  1.6.2.2       tls 	/*
    644  1.6.2.2       tls 	 * Normal hash usage is < 66%. If the usage gets too high then
    645  1.6.2.2       tls 	 * remove the hash entirely and let it be rebuilt later.
    646  1.6.2.2       tls 	 */
    647  1.6.2.2       tls 	if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) {
    648  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    649  1.6.2.2       tls 		ulfsdirhash_free(ip);
    650  1.6.2.2       tls 		return;
    651  1.6.2.2       tls 	}
    652  1.6.2.2       tls 
    653  1.6.2.2       tls 	/* Find a free hash slot (empty or deleted), and add the entry. */
    654  1.6.2.4  jdolecek 	slot = ulfsdirhash_hash(dh, lfs_dir_nameptr(fs, dirp),
    655  1.6.2.4  jdolecek 				lfs_dir_getnamlen(fs, dirp));
    656  1.6.2.2       tls 	while (DH_ENTRY(dh, slot) >= 0)
    657  1.6.2.2       tls 		slot = WRAPINCR(slot, dh->dh_hlen);
    658  1.6.2.2       tls 	if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY)
    659  1.6.2.2       tls 		dh->dh_hused++;
    660  1.6.2.2       tls 	DH_ENTRY(dh, slot) = offset;
    661  1.6.2.2       tls 
    662  1.6.2.2       tls 	/* Update the per-block summary info. */
    663  1.6.2.4  jdolecek 	ulfsdirhash_adjfree(dh, offset, -LFS_DIRSIZ(fs, dirp), dirblksiz);
    664  1.6.2.2       tls 	DIRHASH_UNLOCK(dh);
    665  1.6.2.2       tls }
    666  1.6.2.2       tls 
    667  1.6.2.2       tls /*
    668  1.6.2.2       tls  * Remove the specified directory entry from the hash. The entry to remove
    669  1.6.2.2       tls  * is defined by the name in `dirp', which must exist at the specified
    670  1.6.2.2       tls  * `offset' within the directory.
    671  1.6.2.2       tls  */
    672  1.6.2.2       tls void
    673  1.6.2.4  jdolecek ulfsdirhash_remove(struct inode *ip, LFS_DIRHEADER *dirp, doff_t offset)
    674  1.6.2.2       tls {
    675  1.6.2.4  jdolecek 	struct lfs *fs = ip->i_lfs;
    676  1.6.2.2       tls 	struct dirhash *dh;
    677  1.6.2.2       tls 	int slot;
    678  1.6.2.3       tls 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    679  1.6.2.2       tls 
    680  1.6.2.2       tls 	if ((dh = ip->i_dirhash) == NULL)
    681  1.6.2.2       tls 		return;
    682  1.6.2.2       tls 
    683  1.6.2.2       tls 	DIRHASH_LOCK(dh);
    684  1.6.2.2       tls 	if (dh->dh_hash == NULL) {
    685  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    686  1.6.2.2       tls 		ulfsdirhash_free(ip);
    687  1.6.2.2       tls 		return;
    688  1.6.2.2       tls 	}
    689  1.6.2.2       tls 
    690  1.6.2.2       tls 	KASSERT(offset < dh->dh_dirblks * dirblksiz);
    691  1.6.2.2       tls 	/* Find the entry */
    692  1.6.2.4  jdolecek 	slot = ulfsdirhash_findslot(dh, lfs_dir_nameptr(fs, dirp),
    693  1.6.2.4  jdolecek 				    lfs_dir_getnamlen(fs, dirp), offset);
    694  1.6.2.2       tls 
    695  1.6.2.2       tls 	/* Remove the hash entry. */
    696  1.6.2.2       tls 	ulfsdirhash_delslot(dh, slot);
    697  1.6.2.2       tls 
    698  1.6.2.2       tls 	/* Update the per-block summary info. */
    699  1.6.2.4  jdolecek 	ulfsdirhash_adjfree(dh, offset, LFS_DIRSIZ(fs, dirp), dirblksiz);
    700  1.6.2.2       tls 	DIRHASH_UNLOCK(dh);
    701  1.6.2.2       tls }
    702  1.6.2.2       tls 
    703  1.6.2.2       tls /*
    704  1.6.2.2       tls  * Change the offset associated with a directory entry in the hash. Used
    705  1.6.2.2       tls  * when compacting directory blocks.
    706  1.6.2.2       tls  */
    707  1.6.2.2       tls void
    708  1.6.2.4  jdolecek ulfsdirhash_move(struct inode *ip, LFS_DIRHEADER *dirp, doff_t oldoff,
    709  1.6.2.2       tls     doff_t newoff)
    710  1.6.2.2       tls {
    711  1.6.2.4  jdolecek 	struct lfs *fs = ip->i_lfs;
    712  1.6.2.2       tls 	struct dirhash *dh;
    713  1.6.2.2       tls 	int slot;
    714  1.6.2.2       tls 
    715  1.6.2.2       tls 	if ((dh = ip->i_dirhash) == NULL)
    716  1.6.2.2       tls 		return;
    717  1.6.2.2       tls 	DIRHASH_LOCK(dh);
    718  1.6.2.2       tls 	if (dh->dh_hash == NULL) {
    719  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    720  1.6.2.2       tls 		ulfsdirhash_free(ip);
    721  1.6.2.2       tls 		return;
    722  1.6.2.2       tls 	}
    723  1.6.2.2       tls 
    724  1.6.2.3       tls 	KASSERT(oldoff < dh->dh_dirblks * ip->i_lfs->um_dirblksiz &&
    725  1.6.2.3       tls 	    newoff < dh->dh_dirblks * ip->i_lfs->um_dirblksiz);
    726  1.6.2.2       tls 	/* Find the entry, and update the offset. */
    727  1.6.2.4  jdolecek 	slot = ulfsdirhash_findslot(dh, lfs_dir_nameptr(fs, dirp),
    728  1.6.2.4  jdolecek 				    lfs_dir_getnamlen(fs, dirp), oldoff);
    729  1.6.2.2       tls 	DH_ENTRY(dh, slot) = newoff;
    730  1.6.2.2       tls 	DIRHASH_UNLOCK(dh);
    731  1.6.2.2       tls }
    732  1.6.2.2       tls 
    733  1.6.2.2       tls /*
    734  1.6.2.2       tls  * Inform dirhash that the directory has grown by one block that
    735  1.6.2.2       tls  * begins at offset (i.e. the new length is offset + DIRBLKSIZ).
    736  1.6.2.2       tls  */
    737  1.6.2.2       tls void
    738  1.6.2.2       tls ulfsdirhash_newblk(struct inode *ip, doff_t offset)
    739  1.6.2.2       tls {
    740  1.6.2.2       tls 	struct dirhash *dh;
    741  1.6.2.2       tls 	int block;
    742  1.6.2.3       tls 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    743  1.6.2.2       tls 
    744  1.6.2.2       tls 	if ((dh = ip->i_dirhash) == NULL)
    745  1.6.2.2       tls 		return;
    746  1.6.2.2       tls 	DIRHASH_LOCK(dh);
    747  1.6.2.2       tls 	if (dh->dh_hash == NULL) {
    748  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    749  1.6.2.2       tls 		ulfsdirhash_free(ip);
    750  1.6.2.2       tls 		return;
    751  1.6.2.2       tls 	}
    752  1.6.2.2       tls 
    753  1.6.2.2       tls 	KASSERT(offset == dh->dh_dirblks * dirblksiz);
    754  1.6.2.2       tls 	block = offset / dirblksiz;
    755  1.6.2.2       tls 	if (block >= dh->dh_nblk) {
    756  1.6.2.2       tls 		/* Out of space; must rebuild. */
    757  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    758  1.6.2.2       tls 		ulfsdirhash_free(ip);
    759  1.6.2.2       tls 		return;
    760  1.6.2.2       tls 	}
    761  1.6.2.2       tls 	dh->dh_dirblks = block + 1;
    762  1.6.2.2       tls 
    763  1.6.2.2       tls 	/* Account for the new free block. */
    764  1.6.2.2       tls 	dh->dh_blkfree[block] = dirblksiz / DIRALIGN;
    765  1.6.2.2       tls 	if (dh->dh_firstfree[DH_NFSTATS] == -1)
    766  1.6.2.2       tls 		dh->dh_firstfree[DH_NFSTATS] = block;
    767  1.6.2.2       tls 	DIRHASH_UNLOCK(dh);
    768  1.6.2.2       tls }
    769  1.6.2.2       tls 
    770  1.6.2.2       tls /*
    771  1.6.2.2       tls  * Inform dirhash that the directory is being truncated.
    772  1.6.2.2       tls  */
    773  1.6.2.2       tls void
    774  1.6.2.2       tls ulfsdirhash_dirtrunc(struct inode *ip, doff_t offset)
    775  1.6.2.2       tls {
    776  1.6.2.2       tls 	struct dirhash *dh;
    777  1.6.2.2       tls 	int block, i;
    778  1.6.2.3       tls 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    779  1.6.2.2       tls 
    780  1.6.2.2       tls 	if ((dh = ip->i_dirhash) == NULL)
    781  1.6.2.2       tls 		return;
    782  1.6.2.2       tls 
    783  1.6.2.2       tls 	DIRHASH_LOCK(dh);
    784  1.6.2.2       tls 	if (dh->dh_hash == NULL) {
    785  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    786  1.6.2.2       tls 		ulfsdirhash_free(ip);
    787  1.6.2.2       tls 		return;
    788  1.6.2.2       tls 	}
    789  1.6.2.2       tls 
    790  1.6.2.2       tls 	KASSERT(offset <= dh->dh_dirblks * dirblksiz);
    791  1.6.2.2       tls 	block = howmany(offset, dirblksiz);
    792  1.6.2.2       tls 	/*
    793  1.6.2.2       tls 	 * If the directory shrinks to less than 1/8 of dh_nblk blocks
    794  1.6.2.2       tls 	 * (about 20% of its original size due to the 50% extra added in
    795  1.6.2.2       tls 	 * ulfsdirhash_build) then free it, and let the caller rebuild
    796  1.6.2.2       tls 	 * if necessary.
    797  1.6.2.2       tls 	 */
    798  1.6.2.2       tls 	if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) {
    799  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    800  1.6.2.2       tls 		ulfsdirhash_free(ip);
    801  1.6.2.2       tls 		return;
    802  1.6.2.2       tls 	}
    803  1.6.2.2       tls 
    804  1.6.2.2       tls 	/*
    805  1.6.2.2       tls 	 * Remove any `first free' information pertaining to the
    806  1.6.2.2       tls 	 * truncated blocks. All blocks we're removing should be
    807  1.6.2.2       tls 	 * completely unused.
    808  1.6.2.2       tls 	 */
    809  1.6.2.2       tls 	if (dh->dh_firstfree[DH_NFSTATS] >= block)
    810  1.6.2.2       tls 		dh->dh_firstfree[DH_NFSTATS] = -1;
    811  1.6.2.2       tls 	for (i = block; i < dh->dh_dirblks; i++)
    812  1.6.2.2       tls 		if (dh->dh_blkfree[i] != dirblksiz / DIRALIGN)
    813  1.6.2.2       tls 			panic("ulfsdirhash_dirtrunc: blocks in use");
    814  1.6.2.2       tls 	for (i = 0; i < DH_NFSTATS; i++)
    815  1.6.2.2       tls 		if (dh->dh_firstfree[i] >= block)
    816  1.6.2.2       tls 			panic("ulfsdirhash_dirtrunc: first free corrupt");
    817  1.6.2.2       tls 	dh->dh_dirblks = block;
    818  1.6.2.2       tls 	DIRHASH_UNLOCK(dh);
    819  1.6.2.2       tls }
    820  1.6.2.2       tls 
    821  1.6.2.2       tls /*
    822  1.6.2.2       tls  * Debugging function to check that the dirhash information about
    823  1.6.2.2       tls  * a directory block matches its actual contents. Panics if a mismatch
    824  1.6.2.2       tls  * is detected.
    825  1.6.2.2       tls  *
    826  1.6.2.2       tls  * On entry, `sbuf' should point to the start of an in-core
    827  1.6.2.2       tls  * DIRBLKSIZ-sized directory block, and `offset' should contain the
    828  1.6.2.2       tls  * offset from the start of the directory of that block.
    829  1.6.2.2       tls  */
    830  1.6.2.2       tls void
    831  1.6.2.2       tls ulfsdirhash_checkblock(struct inode *ip, char *sbuf, doff_t offset)
    832  1.6.2.2       tls {
    833  1.6.2.4  jdolecek 	struct lfs *fs = ip->i_lfs;
    834  1.6.2.2       tls 	struct dirhash *dh;
    835  1.6.2.4  jdolecek 	LFS_DIRHEADER *dp;
    836  1.6.2.2       tls 	int block, ffslot, i, nfree;
    837  1.6.2.3       tls 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    838  1.6.2.2       tls 
    839  1.6.2.2       tls 	if (!ulfs_dirhashcheck)
    840  1.6.2.2       tls 		return;
    841  1.6.2.2       tls 	if ((dh = ip->i_dirhash) == NULL)
    842  1.6.2.2       tls 		return;
    843  1.6.2.2       tls 
    844  1.6.2.2       tls 	DIRHASH_LOCK(dh);
    845  1.6.2.2       tls 	if (dh->dh_hash == NULL) {
    846  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
    847  1.6.2.2       tls 		ulfsdirhash_free(ip);
    848  1.6.2.2       tls 		return;
    849  1.6.2.2       tls 	}
    850  1.6.2.2       tls 
    851  1.6.2.2       tls 	block = offset / dirblksiz;
    852  1.6.2.2       tls 	if ((offset & (dirblksiz - 1)) != 0 || block >= dh->dh_dirblks)
    853  1.6.2.2       tls 		panic("ulfsdirhash_checkblock: bad offset");
    854  1.6.2.2       tls 
    855  1.6.2.2       tls 	nfree = 0;
    856  1.6.2.4  jdolecek 	for (i = 0; i < dirblksiz; i += lfs_dir_getreclen(fs, dp)) {
    857  1.6.2.4  jdolecek 		dp = (LFS_DIRHEADER *)(sbuf + i);
    858  1.6.2.4  jdolecek 		if (lfs_dir_getreclen(fs, dp) == 0 || i + lfs_dir_getreclen(fs, dp) > dirblksiz)
    859  1.6.2.2       tls 			panic("ulfsdirhash_checkblock: bad dir");
    860  1.6.2.2       tls 
    861  1.6.2.4  jdolecek 		if (lfs_dir_getino(fs, dp) == 0) {
    862  1.6.2.2       tls #if 0
    863  1.6.2.2       tls 			/*
    864  1.6.2.2       tls 			 * XXX entries with d_ino == 0 should only occur
    865  1.6.2.2       tls 			 * at the start of a DIRBLKSIZ block. However the
    866  1.6.2.2       tls 			 * ulfs code is tolerant of such entries at other
    867  1.6.2.2       tls 			 * offsets, and fsck does not fix them.
    868  1.6.2.2       tls 			 */
    869  1.6.2.2       tls 			if (i != 0)
    870  1.6.2.2       tls 				panic("ulfsdirhash_checkblock: bad dir inode");
    871  1.6.2.2       tls #endif
    872  1.6.2.4  jdolecek 			nfree += lfs_dir_getreclen(fs, dp);
    873  1.6.2.2       tls 			continue;
    874  1.6.2.2       tls 		}
    875  1.6.2.2       tls 
    876  1.6.2.2       tls 		/* Check that the entry	exists (will panic if it doesn't). */
    877  1.6.2.4  jdolecek 		ulfsdirhash_findslot(dh, lfs_dir_nameptr(fs, dp),
    878  1.6.2.4  jdolecek 				     lfs_dir_getnamlen(fs, dp),
    879  1.6.2.4  jdolecek 				     offset + i);
    880  1.6.2.2       tls 
    881  1.6.2.4  jdolecek 		nfree += lfs_dir_getreclen(fs, dp) - LFS_DIRSIZ(fs, dp);
    882  1.6.2.2       tls 	}
    883  1.6.2.2       tls 	if (i != dirblksiz)
    884  1.6.2.2       tls 		panic("ulfsdirhash_checkblock: bad dir end");
    885  1.6.2.2       tls 
    886  1.6.2.2       tls 	if (dh->dh_blkfree[block] * DIRALIGN != nfree)
    887  1.6.2.2       tls 		panic("ulfsdirhash_checkblock: bad free count");
    888  1.6.2.2       tls 
    889  1.6.2.2       tls 	ffslot = BLKFREE2IDX(nfree / DIRALIGN);
    890  1.6.2.2       tls 	for (i = 0; i <= DH_NFSTATS; i++)
    891  1.6.2.2       tls 		if (dh->dh_firstfree[i] == block && i != ffslot)
    892  1.6.2.2       tls 			panic("ulfsdirhash_checkblock: bad first-free");
    893  1.6.2.2       tls 	if (dh->dh_firstfree[ffslot] == -1)
    894  1.6.2.2       tls 		panic("ulfsdirhash_checkblock: missing first-free entry");
    895  1.6.2.2       tls 	DIRHASH_UNLOCK(dh);
    896  1.6.2.2       tls }
    897  1.6.2.2       tls 
    898  1.6.2.2       tls /*
    899  1.6.2.2       tls  * Hash the specified filename into a dirhash slot.
    900  1.6.2.2       tls  */
    901  1.6.2.2       tls static int
    902  1.6.2.2       tls ulfsdirhash_hash(struct dirhash *dh, const char *name, int namelen)
    903  1.6.2.2       tls {
    904  1.6.2.2       tls 	u_int32_t hash;
    905  1.6.2.2       tls 
    906  1.6.2.2       tls 	/*
    907  1.6.2.2       tls 	 * We hash the name and then some other bit of data that is
    908  1.6.2.2       tls 	 * invariant over the dirhash's lifetime. Otherwise names
    909  1.6.2.2       tls 	 * differing only in the last byte are placed close to one
    910  1.6.2.2       tls 	 * another in the table, which is bad for linear probing.
    911  1.6.2.2       tls 	 */
    912  1.6.2.2       tls 	hash = hash32_buf(name, namelen, HASH32_BUF_INIT);
    913  1.6.2.2       tls 	hash = hash32_buf(&dh, sizeof(dh), hash);
    914  1.6.2.2       tls 	return (hash % dh->dh_hlen);
    915  1.6.2.2       tls }
    916  1.6.2.2       tls 
    917  1.6.2.2       tls /*
    918  1.6.2.2       tls  * Adjust the number of free bytes in the block containing `offset'
    919  1.6.2.2       tls  * by the value specified by `diff'.
    920  1.6.2.2       tls  *
    921  1.6.2.2       tls  * The caller must ensure we have exclusive access to `dh'; normally
    922  1.6.2.2       tls  * that means that dh_lock should be held, but this is also called
    923  1.6.2.2       tls  * from ulfsdirhash_build() where exclusive access can be assumed.
    924  1.6.2.2       tls  */
    925  1.6.2.2       tls static void
    926  1.6.2.2       tls ulfsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff, int dirblksiz)
    927  1.6.2.2       tls {
    928  1.6.2.2       tls 	int block, i, nfidx, ofidx;
    929  1.6.2.2       tls 
    930  1.6.2.2       tls 	KASSERT(mutex_owned(&dh->dh_lock));
    931  1.6.2.2       tls 
    932  1.6.2.2       tls 	/* Update the per-block summary info. */
    933  1.6.2.2       tls 	block = offset / dirblksiz;
    934  1.6.2.2       tls 	KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks);
    935  1.6.2.2       tls 	ofidx = BLKFREE2IDX(dh->dh_blkfree[block]);
    936  1.6.2.2       tls 	dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN);
    937  1.6.2.2       tls 	nfidx = BLKFREE2IDX(dh->dh_blkfree[block]);
    938  1.6.2.2       tls 
    939  1.6.2.2       tls 	/* Update the `first free' list if necessary. */
    940  1.6.2.2       tls 	if (ofidx != nfidx) {
    941  1.6.2.2       tls 		/* If removing, scan forward for the next block. */
    942  1.6.2.2       tls 		if (dh->dh_firstfree[ofidx] == block) {
    943  1.6.2.2       tls 			for (i = block + 1; i < dh->dh_dirblks; i++)
    944  1.6.2.2       tls 				if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx)
    945  1.6.2.2       tls 					break;
    946  1.6.2.2       tls 			dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1;
    947  1.6.2.2       tls 		}
    948  1.6.2.2       tls 
    949  1.6.2.2       tls 		/* Make this the new `first free' if necessary */
    950  1.6.2.2       tls 		if (dh->dh_firstfree[nfidx] > block ||
    951  1.6.2.2       tls 		    dh->dh_firstfree[nfidx] == -1)
    952  1.6.2.2       tls 			dh->dh_firstfree[nfidx] = block;
    953  1.6.2.2       tls 	}
    954  1.6.2.2       tls }
    955  1.6.2.2       tls 
    956  1.6.2.2       tls /*
    957  1.6.2.2       tls  * Find the specified name which should have the specified offset.
    958  1.6.2.2       tls  * Returns a slot number, and panics on failure.
    959  1.6.2.2       tls  *
    960  1.6.2.2       tls  * `dh' must be locked on entry and remains so on return.
    961  1.6.2.2       tls  */
    962  1.6.2.2       tls static int
    963  1.6.2.2       tls ulfsdirhash_findslot(struct dirhash *dh, const char *name, int namelen,
    964  1.6.2.2       tls     doff_t offset)
    965  1.6.2.2       tls {
    966  1.6.2.2       tls 	int slot;
    967  1.6.2.2       tls 
    968  1.6.2.2       tls 	KASSERT(mutex_owned(&dh->dh_lock));
    969  1.6.2.2       tls 
    970  1.6.2.2       tls 	/* Find the entry. */
    971  1.6.2.2       tls 	KASSERT(dh->dh_hused < dh->dh_hlen);
    972  1.6.2.2       tls 	slot = ulfsdirhash_hash(dh, name, namelen);
    973  1.6.2.2       tls 	while (DH_ENTRY(dh, slot) != offset &&
    974  1.6.2.2       tls 	    DH_ENTRY(dh, slot) != DIRHASH_EMPTY)
    975  1.6.2.2       tls 		slot = WRAPINCR(slot, dh->dh_hlen);
    976  1.6.2.2       tls 	if (DH_ENTRY(dh, slot) != offset)
    977  1.6.2.2       tls 		panic("ulfsdirhash_findslot: '%.*s' not found", namelen, name);
    978  1.6.2.2       tls 
    979  1.6.2.2       tls 	return (slot);
    980  1.6.2.2       tls }
    981  1.6.2.2       tls 
    982  1.6.2.2       tls /*
    983  1.6.2.2       tls  * Remove the entry corresponding to the specified slot from the hash array.
    984  1.6.2.2       tls  *
    985  1.6.2.2       tls  * `dh' must be locked on entry and remains so on return.
    986  1.6.2.2       tls  */
    987  1.6.2.2       tls static void
    988  1.6.2.2       tls ulfsdirhash_delslot(struct dirhash *dh, int slot)
    989  1.6.2.2       tls {
    990  1.6.2.2       tls 	int i;
    991  1.6.2.2       tls 
    992  1.6.2.2       tls 	KASSERT(mutex_owned(&dh->dh_lock));
    993  1.6.2.2       tls 
    994  1.6.2.2       tls 	/* Mark the entry as deleted. */
    995  1.6.2.2       tls 	DH_ENTRY(dh, slot) = DIRHASH_DEL;
    996  1.6.2.2       tls 
    997  1.6.2.2       tls 	/* If this is the end of a chain of DIRHASH_DEL slots, remove them. */
    998  1.6.2.2       tls 	for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; )
    999  1.6.2.2       tls 		i = WRAPINCR(i, dh->dh_hlen);
   1000  1.6.2.2       tls 	if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) {
   1001  1.6.2.2       tls 		i = WRAPDECR(i, dh->dh_hlen);
   1002  1.6.2.2       tls 		while (DH_ENTRY(dh, i) == DIRHASH_DEL) {
   1003  1.6.2.2       tls 			DH_ENTRY(dh, i) = DIRHASH_EMPTY;
   1004  1.6.2.2       tls 			dh->dh_hused--;
   1005  1.6.2.2       tls 			i = WRAPDECR(i, dh->dh_hlen);
   1006  1.6.2.2       tls 		}
   1007  1.6.2.2       tls 		KASSERT(dh->dh_hused >= 0);
   1008  1.6.2.2       tls 	}
   1009  1.6.2.2       tls }
   1010  1.6.2.2       tls 
   1011  1.6.2.2       tls /*
   1012  1.6.2.2       tls  * Given a directory entry and its offset, find the offset of the
   1013  1.6.2.2       tls  * previous entry in the same DIRBLKSIZ-sized block. Returns an
   1014  1.6.2.2       tls  * offset, or -1 if there is no previous entry in the block or some
   1015  1.6.2.2       tls  * other problem occurred.
   1016  1.6.2.2       tls  */
   1017  1.6.2.2       tls static doff_t
   1018  1.6.2.4  jdolecek ulfsdirhash_getprev(struct lfs *fs, LFS_DIRHEADER *dirp,
   1019  1.6.2.4  jdolecek 		doff_t offset, int dirblksiz)
   1020  1.6.2.2       tls {
   1021  1.6.2.4  jdolecek 	LFS_DIRHEADER *dp;
   1022  1.6.2.2       tls 	char *blkbuf;
   1023  1.6.2.2       tls 	doff_t blkoff, prevoff;
   1024  1.6.2.2       tls 	int entrypos, i;
   1025  1.6.2.4  jdolecek 	unsigned reclen;
   1026  1.6.2.2       tls 
   1027  1.6.2.2       tls 	blkoff = offset & ~(dirblksiz - 1);	/* offset of start of block */
   1028  1.6.2.2       tls 	entrypos = offset & (dirblksiz - 1);	/* entry relative to block */
   1029  1.6.2.2       tls 	blkbuf = (char *)dirp - entrypos;
   1030  1.6.2.2       tls 	prevoff = blkoff;
   1031  1.6.2.2       tls 
   1032  1.6.2.2       tls 	/* If `offset' is the start of a block, there is no previous entry. */
   1033  1.6.2.2       tls 	if (entrypos == 0)
   1034  1.6.2.2       tls 		return (-1);
   1035  1.6.2.2       tls 
   1036  1.6.2.2       tls 	/* Scan from the start of the block until we get to the entry. */
   1037  1.6.2.4  jdolecek 	for (i = 0; i < entrypos; i += reclen) {
   1038  1.6.2.4  jdolecek 		dp = (LFS_DIRHEADER *)(blkbuf + i);
   1039  1.6.2.4  jdolecek 		reclen = lfs_dir_getreclen(fs, dp);
   1040  1.6.2.4  jdolecek 		if (reclen == 0 || i + reclen > entrypos)
   1041  1.6.2.2       tls 			return (-1);	/* Corrupted directory. */
   1042  1.6.2.2       tls 		prevoff = blkoff + i;
   1043  1.6.2.2       tls 	}
   1044  1.6.2.2       tls 	return (prevoff);
   1045  1.6.2.2       tls }
   1046  1.6.2.2       tls 
   1047  1.6.2.2       tls /*
   1048  1.6.2.2       tls  * Try to free up `wanted' bytes by stealing memory from existing
   1049  1.6.2.2       tls  * dirhashes. Returns zero with list locked if successful.
   1050  1.6.2.2       tls  */
   1051  1.6.2.2       tls static int
   1052  1.6.2.2       tls ulfsdirhash_recycle(int wanted)
   1053  1.6.2.2       tls {
   1054  1.6.2.2       tls 	struct dirhash *dh;
   1055  1.6.2.2       tls 	doff_t **hash;
   1056  1.6.2.2       tls 	u_int8_t *blkfree;
   1057  1.6.2.2       tls 	int i, mem, narrays;
   1058  1.6.2.2       tls 	size_t hashsz, blkfreesz;
   1059  1.6.2.2       tls 
   1060  1.6.2.2       tls 	DIRHASHLIST_LOCK();
   1061  1.6.2.2       tls 	while (wanted + ulfs_dirhashmem > ulfs_dirhashmaxmem) {
   1062  1.6.2.2       tls 		/* Find a dirhash, and lock it. */
   1063  1.6.2.2       tls 		if ((dh = TAILQ_FIRST(&ulfsdirhash_list)) == NULL) {
   1064  1.6.2.2       tls 			DIRHASHLIST_UNLOCK();
   1065  1.6.2.2       tls 			return (-1);
   1066  1.6.2.2       tls 		}
   1067  1.6.2.2       tls 		DIRHASH_LOCK(dh);
   1068  1.6.2.2       tls 		KASSERT(dh->dh_hash != NULL);
   1069  1.6.2.2       tls 
   1070  1.6.2.2       tls 		/* Decrement the score; only recycle if it becomes zero. */
   1071  1.6.2.2       tls 		if (--dh->dh_score > 0) {
   1072  1.6.2.2       tls 			DIRHASH_UNLOCK(dh);
   1073  1.6.2.2       tls 			DIRHASHLIST_UNLOCK();
   1074  1.6.2.2       tls 			return (-1);
   1075  1.6.2.2       tls 		}
   1076  1.6.2.2       tls 
   1077  1.6.2.2       tls 		/* Remove it from the list and detach its memory. */
   1078  1.6.2.2       tls 		TAILQ_REMOVE(&ulfsdirhash_list, dh, dh_list);
   1079  1.6.2.2       tls 		dh->dh_onlist = 0;
   1080  1.6.2.2       tls 		hash = dh->dh_hash;
   1081  1.6.2.2       tls 		hashsz = dh->dh_hashsz;
   1082  1.6.2.2       tls 		dh->dh_hash = NULL;
   1083  1.6.2.2       tls 		blkfree = dh->dh_blkfree;
   1084  1.6.2.2       tls 		blkfreesz = dh->dh_blkfreesz;
   1085  1.6.2.2       tls 		dh->dh_blkfree = NULL;
   1086  1.6.2.2       tls 		narrays = dh->dh_narrays;
   1087  1.6.2.2       tls 		mem = narrays * sizeof(*dh->dh_hash) +
   1088  1.6.2.2       tls 		    narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) +
   1089  1.6.2.2       tls 		    dh->dh_nblk * sizeof(*dh->dh_blkfree);
   1090  1.6.2.2       tls 
   1091  1.6.2.2       tls 		/* Unlock everything, free the detached memory. */
   1092  1.6.2.2       tls 		DIRHASH_UNLOCK(dh);
   1093  1.6.2.2       tls 		DIRHASHLIST_UNLOCK();
   1094  1.6.2.2       tls 
   1095  1.6.2.2       tls 		for (i = 0; i < narrays; i++)
   1096  1.6.2.2       tls 			DIRHASH_BLKFREE(hash[i]);
   1097  1.6.2.2       tls 		kmem_free(hash, hashsz);
   1098  1.6.2.2       tls 		kmem_free(blkfree, blkfreesz);
   1099  1.6.2.2       tls 
   1100  1.6.2.2       tls 		/* Account for the returned memory, and repeat if necessary. */
   1101  1.6.2.2       tls 		DIRHASHLIST_LOCK();
   1102  1.6.2.2       tls 		atomic_add_int(&ulfs_dirhashmem, -mem);
   1103  1.6.2.2       tls 	}
   1104  1.6.2.2       tls 	/* Success. */
   1105  1.6.2.2       tls 	return (0);
   1106  1.6.2.2       tls }
   1107  1.6.2.2       tls 
   1108  1.6.2.2       tls static void
   1109  1.6.2.2       tls ulfsdirhash_sysctl_init(void)
   1110  1.6.2.2       tls {
   1111  1.6.2.2       tls 	const struct sysctlnode *rnode, *cnode;
   1112  1.6.2.2       tls 
   1113  1.6.2.2       tls 	sysctl_createv(&ulfsdirhash_sysctl_log, 0, NULL, &rnode,
   1114  1.6.2.2       tls 		       CTLFLAG_PERMANENT,
   1115  1.6.2.2       tls 		       CTLTYPE_NODE, "ulfs",
   1116  1.6.2.2       tls 		       SYSCTL_DESCR("ulfs"),
   1117  1.6.2.2       tls 		       NULL, 0, NULL, 0,
   1118  1.6.2.3       tls 		       CTL_VFS, CTL_CREATE, CTL_EOL);
   1119  1.6.2.2       tls 
   1120  1.6.2.2       tls 	sysctl_createv(&ulfsdirhash_sysctl_log, 0, &rnode, &rnode,
   1121  1.6.2.2       tls 		       CTLFLAG_PERMANENT,
   1122  1.6.2.2       tls 		       CTLTYPE_NODE, "dirhash",
   1123  1.6.2.2       tls 		       SYSCTL_DESCR("dirhash"),
   1124  1.6.2.2       tls 		       NULL, 0, NULL, 0,
   1125  1.6.2.2       tls 		       CTL_CREATE, CTL_EOL);
   1126  1.6.2.2       tls 
   1127  1.6.2.2       tls 	sysctl_createv(&ulfsdirhash_sysctl_log, 0, &rnode, &cnode,
   1128  1.6.2.2       tls 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1129  1.6.2.2       tls 		       CTLTYPE_INT, "minblocks",
   1130  1.6.2.2       tls 		       SYSCTL_DESCR("minimum hashed directory size in blocks"),
   1131  1.6.2.2       tls 		       NULL, 0, &ulfs_dirhashminblks, 0,
   1132  1.6.2.2       tls 		       CTL_CREATE, CTL_EOL);
   1133  1.6.2.2       tls 
   1134  1.6.2.2       tls 	sysctl_createv(&ulfsdirhash_sysctl_log, 0, &rnode, &cnode,
   1135  1.6.2.2       tls 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1136  1.6.2.2       tls 		       CTLTYPE_INT, "maxmem",
   1137  1.6.2.2       tls 		       SYSCTL_DESCR("maximum dirhash memory usage"),
   1138  1.6.2.2       tls 		       NULL, 0, &ulfs_dirhashmaxmem, 0,
   1139  1.6.2.2       tls 		       CTL_CREATE, CTL_EOL);
   1140  1.6.2.2       tls 
   1141  1.6.2.2       tls 	sysctl_createv(&ulfsdirhash_sysctl_log, 0, &rnode, &cnode,
   1142  1.6.2.2       tls 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   1143  1.6.2.2       tls 		       CTLTYPE_INT, "memused",
   1144  1.6.2.2       tls 		       SYSCTL_DESCR("current dirhash memory usage"),
   1145  1.6.2.2       tls 		       NULL, 0, &ulfs_dirhashmem, 0,
   1146  1.6.2.2       tls 		       CTL_CREATE, CTL_EOL);
   1147  1.6.2.2       tls 
   1148  1.6.2.2       tls 	sysctl_createv(&ulfsdirhash_sysctl_log, 0, &rnode, &cnode,
   1149  1.6.2.2       tls 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1150  1.6.2.2       tls 		       CTLTYPE_INT, "docheck",
   1151  1.6.2.2       tls 		       SYSCTL_DESCR("enable extra sanity checks"),
   1152  1.6.2.2       tls 		       NULL, 0, &ulfs_dirhashcheck, 0,
   1153  1.6.2.2       tls 		       CTL_CREATE, CTL_EOL);
   1154  1.6.2.2       tls }
   1155  1.6.2.2       tls 
   1156  1.6.2.2       tls void
   1157  1.6.2.2       tls ulfsdirhash_init(void)
   1158  1.6.2.2       tls {
   1159  1.6.2.2       tls 
   1160  1.6.2.2       tls 	mutex_init(&ulfsdirhash_lock, MUTEX_DEFAULT, IPL_NONE);
   1161  1.6.2.2       tls 	ulfsdirhashblk_cache = pool_cache_init(DH_NBLKOFF * sizeof(daddr_t), 0,
   1162  1.6.2.2       tls 	    0, 0, "dirhashblk", NULL, IPL_NONE, NULL, NULL, NULL);
   1163  1.6.2.2       tls 	ulfsdirhash_cache = pool_cache_init(sizeof(struct dirhash), 0,
   1164  1.6.2.2       tls 	    0, 0, "dirhash", NULL, IPL_NONE, NULL, NULL, NULL);
   1165  1.6.2.2       tls 	TAILQ_INIT(&ulfsdirhash_list);
   1166  1.6.2.2       tls 	ulfsdirhash_sysctl_init();
   1167  1.6.2.2       tls }
   1168  1.6.2.2       tls 
   1169  1.6.2.2       tls void
   1170  1.6.2.2       tls ulfsdirhash_done(void)
   1171  1.6.2.2       tls {
   1172  1.6.2.2       tls 
   1173  1.6.2.2       tls 	KASSERT(TAILQ_EMPTY(&ulfsdirhash_list));
   1174  1.6.2.2       tls 	pool_cache_destroy(ulfsdirhashblk_cache);
   1175  1.6.2.2       tls 	pool_cache_destroy(ulfsdirhash_cache);
   1176  1.6.2.2       tls 	mutex_destroy(&ulfsdirhash_lock);
   1177  1.6.2.2       tls 	sysctl_teardown(&ulfsdirhash_sysctl_log);
   1178  1.6.2.2       tls }
   1179