#include <sys/cdefs.h>
#include "opt_ufs.h"
#ifdef UFS_DIRHASH
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/malloc.h>
#include <sys/fnv_hash.h>
#include <sys/proc.h>
#include <sys/bio.h>
#include <sys/buf.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/refcount.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <sys/eventhandler.h>
#include <sys/time.h>
#include <vm/uma.h>
#include <ufs/ufs/quota.h>
#include <ufs/ufs/inode.h>
#include <ufs/ufs/dir.h>
#include <ufs/ufs/dirhash.h>
#include <ufs/ufs/extattr.h>
#include <ufs/ufs/ufsmount.h>
#include <ufs/ufs/ufs_extern.h>
#define WRAPINCR(val, limit) (((val) + 1 == (limit)) ? 0 : ((val) + 1))
#define WRAPDECR(val, limit) (((val) == 0) ? ((limit) - 1) : ((val) - 1))
#define BLKFREE2IDX(n) ((n) > DH_NFSTATS ? DH_NFSTATS : (n))
static MALLOC_DEFINE(M_DIRHASH, "ufs_dirhash", "UFS directory hash tables");
static int ufs_mindirhashsize = DIRBLKSIZ * 5;
SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_minsize, CTLFLAG_RW,
&ufs_mindirhashsize,
0, "minimum directory size in bytes for which to use hashed lookup");
static int ufs_dirhashmaxmem = 2 * 1024 * 1024;
SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_maxmem, CTLFLAG_RW, &ufs_dirhashmaxmem,
0, "maximum allowed dirhash memory usage");
static int ufs_dirhashmem;
SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_mem, CTLFLAG_RD, &ufs_dirhashmem,
0, "current dirhash memory usage");
static int ufs_dirhashcheck = 0;
SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_docheck, CTLFLAG_RW, &ufs_dirhashcheck,
0, "enable extra sanity tests");
static int ufs_dirhashlowmemcount = 0;
SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_lowmemcount, CTLFLAG_RD,
&ufs_dirhashlowmemcount, 0, "number of times low memory hook called");
static int ufs_dirhashreclaimpercent = 10;
static int ufsdirhash_set_reclaimpercent(SYSCTL_HANDLER_ARGS);
SYSCTL_PROC(_vfs_ufs, OID_AUTO, dirhash_reclaimpercent,
CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
0, 0, ufsdirhash_set_reclaimpercent, "I",
"set percentage of dirhash cache to be removed in low VM events");
static int ufsdirhash_hash(struct dirhash *dh, char *name, int namelen);
static void ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff);
static void ufsdirhash_delslot(struct dirhash *dh, int slot);
static int ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen,
doff_t offset);
static doff_t ufsdirhash_getprev(struct direct *dp, doff_t offset);
static int ufsdirhash_recycle(int wanted);
static void ufsdirhash_lowmem(void *, int);
static void ufsdirhash_free_locked(struct inode *ip);
static uma_zone_t ufsdirhash_zone;
#define DIRHASHLIST_LOCK() mtx_lock(&ufsdirhash_mtx)
#define DIRHASHLIST_UNLOCK() mtx_unlock(&ufsdirhash_mtx)
#define DIRHASH_BLKALLOC() uma_zalloc(ufsdirhash_zone, M_NOWAIT)
#define DIRHASH_BLKFREE(ptr) uma_zfree(ufsdirhash_zone, (ptr))
#define DIRHASH_ASSERT_LOCKED(dh) \
sx_assert(&(dh)->dh_lock, SA_LOCKED)
static TAILQ_HEAD(, dirhash) ufsdirhash_list;
static struct mtx ufsdirhash_mtx;
static void
ufsdirhash_hold(struct dirhash *dh)
{
refcount_acquire(&dh->dh_refcount);
}
static void
ufsdirhash_drop(struct dirhash *dh)
{
if (refcount_release(&dh->dh_refcount)) {
sx_destroy(&dh->dh_lock);
free(dh, M_DIRHASH);
}
}
static void
ufsdirhash_release(struct dirhash *dh)
{
sx_unlock(&dh->dh_lock);
}
static struct dirhash *
ufsdirhash_create(struct inode *ip)
{
struct dirhash *ndh;
struct dirhash *dh;
struct vnode *vp;
bool excl;
ndh = dh = NULL;
vp = ip->i_vnode;
excl = false;
for (;;) {
if (ip->i_dirhash == NULL && ndh == NULL) {
ndh = malloc(sizeof *dh, M_DIRHASH,
M_NOWAIT | M_ZERO);
if (ndh == NULL)
return (NULL);
refcount_init(&ndh->dh_refcount, 1);
sx_init_flags(&ndh->dh_lock, "dirhash", SX_DUPOK);
sx_xlock(&ndh->dh_lock);
}
VI_LOCK(vp);
dh = ip->i_dirhash;
if (dh == NULL) {
ip->i_dirhash = ndh;
VI_UNLOCK(vp);
if (ndh == NULL)
continue;
return (ndh);
}
ufsdirhash_hold(dh);
VI_UNLOCK(vp);
if (excl)
sx_xlock(&dh->dh_lock);
else
sx_slock(&dh->dh_lock);
VI_LOCK(vp);
if (ip->i_dirhash != dh) {
VI_UNLOCK(vp);
ufsdirhash_release(dh);
ufsdirhash_drop(dh);
continue;
}
VI_UNLOCK(vp);
ufsdirhash_drop(dh);
if (dh->dh_hash != NULL)
break;
if (excl || sx_try_upgrade(&dh->dh_lock))
break;
sx_sunlock(&dh->dh_lock);
excl = true;
}
if (ndh) {
ufsdirhash_release(ndh);
ufsdirhash_drop(ndh);
}
return (dh);
}
static struct dirhash *
ufsdirhash_acquire(struct inode *ip)
{
struct dirhash *dh;
ASSERT_VOP_ELOCKED(ip->i_vnode, __FUNCTION__);
dh = ip->i_dirhash;
if (dh == NULL)
return (NULL);
sx_xlock(&dh->dh_lock);
if (dh->dh_hash != NULL)
return (dh);
ufsdirhash_free_locked(ip);
return (NULL);
}
void
ufsdirhash_free(struct inode *ip)
{
struct dirhash *dh;
struct vnode *vp;
vp = ip->i_vnode;
for (;;) {
VI_LOCK(vp);
dh = ip->i_dirhash;
if (dh == NULL) {
VI_UNLOCK(vp);
return;
}
ufsdirhash_hold(dh);
VI_UNLOCK(vp);
sx_xlock(&dh->dh_lock);
VI_LOCK(vp);
if (ip->i_dirhash == dh) {
VI_UNLOCK(vp);
ufsdirhash_drop(dh);
break;
}
VI_UNLOCK(vp);
ufsdirhash_release(dh);
ufsdirhash_drop(dh);
}
ufsdirhash_free_locked(ip);
}
int
ufsdirhash_build(struct inode *ip)
{
struct dirhash *dh;
struct buf *bp = NULL;
struct direct *ep;
struct vnode *vp;
doff_t bmask, pos;
uint64_t dirblocks, i, narrays, nblocks, nslots;
int j, memreqd, slot;
while (ufs_dirhashmem > ufs_dirhashmaxmem) {
if (ufsdirhash_recycle(0) != 0)
return (-1);
DIRHASHLIST_UNLOCK();
}
if (ip->i_size < ufs_mindirhashsize || OFSFMT(ip->i_vnode) ||
ip->i_effnlink == 0) {
if (ip->i_dirhash)
ufsdirhash_free(ip);
return (-1);
}
dh = ufsdirhash_create(ip);
if (dh == NULL)
return (-1);
if (dh->dh_hash != NULL)
return (0);
vp = ip->i_vnode;
KASSERT(ip->i_size >= DIRBLKSIZ, ("ufsdirhash_build size"));
nslots = ip->i_size / DIRECTSIZ(1);
nslots = (nslots * 3 + 1) / 2;
narrays = howmany(nslots, DH_NBLKOFF);
nslots = narrays * DH_NBLKOFF;
dirblocks = howmany(ip->i_size, DIRBLKSIZ);
nblocks = (dirblocks * 3 + 1) / 2;
memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) +
narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) +
nblocks * sizeof(*dh->dh_blkfree);
DIRHASHLIST_LOCK();
if (memreqd + ufs_dirhashmem > ufs_dirhashmaxmem) {
DIRHASHLIST_UNLOCK();
if (memreqd > ufs_dirhashmaxmem / 2)
goto fail;
if (ufsdirhash_recycle(memreqd) != 0)
goto fail;
}
ufs_dirhashmem += memreqd;
DIRHASHLIST_UNLOCK();
dh->dh_memreq = memreqd;
dh->dh_narrays = narrays;
dh->dh_hlen = nslots;
dh->dh_nblk = nblocks;
dh->dh_dirblks = dirblocks;
for (i = 0; i < DH_NFSTATS; i++)
dh->dh_firstfree[i] = -1;
dh->dh_firstfree[DH_NFSTATS] = 0;
dh->dh_hused = 0;
dh->dh_seqoff = -1;
dh->dh_score = DH_SCOREINIT;
dh->dh_lastused = time_second;
dh->dh_hash = malloc(narrays * sizeof(dh->dh_hash[0]),
M_DIRHASH, M_NOWAIT | M_ZERO);
if (dh->dh_hash == NULL)
goto fail;
dh->dh_blkfree = malloc(nblocks * sizeof(dh->dh_blkfree[0]),
M_DIRHASH, M_NOWAIT);
if (dh->dh_blkfree == NULL)
goto fail;
for (i = 0; i < narrays; i++) {
if ((dh->dh_hash[i] = DIRHASH_BLKALLOC()) == NULL)
goto fail;
for (j = 0; j < DH_NBLKOFF; j++)
dh->dh_hash[i][j] = DIRHASH_EMPTY;
}
for (i = 0; i < dirblocks; i++)
dh->dh_blkfree[i] = DIRBLKSIZ / DIRALIGN;
bmask = vp->v_mount->mnt_stat.f_iosize - 1;
pos = 0;
while (pos < ip->i_size) {
if ((pos & bmask) == 0) {
if (bp != NULL)
brelse(bp);
if (UFS_BLKATOFF(vp, (off_t)pos, NULL, &bp) != 0)
goto fail;
}
ep = (struct direct *)((char *)bp->b_data + (pos & bmask));
if (ep->d_reclen == 0 || ep->d_reclen >
DIRBLKSIZ - (pos & (DIRBLKSIZ - 1))) {
brelse(bp);
goto fail;
}
if (ep->d_ino != 0) {
slot = ufsdirhash_hash(dh, ep->d_name, ep->d_namlen);
while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY)
slot = WRAPINCR(slot, dh->dh_hlen);
dh->dh_hused++;
DH_ENTRY(dh, slot) = pos;
ufsdirhash_adjfree(dh, pos, -DIRSIZ(0, ep));
}
pos += ep->d_reclen;
}
if (bp != NULL)
brelse(bp);
DIRHASHLIST_LOCK();
TAILQ_INSERT_TAIL(&ufsdirhash_list, dh, dh_list);
dh->dh_onlist = 1;
DIRHASHLIST_UNLOCK();
sx_downgrade(&dh->dh_lock);
return (0);
fail:
ufsdirhash_free_locked(ip);
return (-1);
}
static void
ufsdirhash_free_locked(struct inode *ip)
{
struct dirhash *dh;
struct vnode *vp;
int i;
DIRHASH_ASSERT_LOCKED(ip->i_dirhash);
vp = ip->i_vnode;
VI_LOCK(vp);
dh = ip->i_dirhash;
ip->i_dirhash = NULL;
VI_UNLOCK(vp);
DIRHASHLIST_LOCK();
if (dh->dh_onlist)
TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list);
ufs_dirhashmem -= dh->dh_memreq;
DIRHASHLIST_UNLOCK();
sx_xunlock(&dh->dh_lock);
if (dh->dh_hash != NULL) {
for (i = 0; i < dh->dh_narrays; i++)
if (dh->dh_hash[i] != NULL)
DIRHASH_BLKFREE(dh->dh_hash[i]);
free(dh->dh_hash, M_DIRHASH);
if (dh->dh_blkfree != NULL)
free(dh->dh_blkfree, M_DIRHASH);
}
ufsdirhash_drop(dh);
}
int
ufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp,
struct buf **bpp, doff_t *prevoffp)
{
struct dirhash *dh, *dh_next;
struct direct *dp;
struct vnode *vp;
struct buf *bp;
doff_t blkoff, bmask, offset, prevoff, seqoff;
int i, slot;
int error;
dh = ip->i_dirhash;
KASSERT(dh != NULL && dh->dh_hash != NULL,
("ufsdirhash_lookup: Invalid dirhash %p\n", dh));
DIRHASH_ASSERT_LOCKED(dh);
DIRHASHLIST_LOCK();
if (TAILQ_NEXT(dh, dh_list) != NULL) {
if ((dh_next = TAILQ_NEXT(dh, dh_list)) != NULL &&
dh->dh_score >= dh_next->dh_score) {
KASSERT(dh->dh_onlist, ("dirhash: not on list"));
TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list);
TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh,
dh_list);
}
}
if (dh->dh_score < DH_SCOREMAX)
dh->dh_score++;
dh->dh_lastused = time_second;
DIRHASHLIST_UNLOCK();
vp = ip->i_vnode;
bmask = vp->v_mount->mnt_stat.f_iosize - 1;
blkoff = -1;
bp = NULL;
seqoff = dh->dh_seqoff;
restart:
slot = ufsdirhash_hash(dh, name, namelen);
if (seqoff != -1) {
for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY;
i = WRAPINCR(i, dh->dh_hlen))
if (offset == seqoff)
break;
if (offset == seqoff) {
slot = i;
} else
seqoff = -1;
}
for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY;
slot = WRAPINCR(slot, dh->dh_hlen)) {
if (offset == DIRHASH_DEL)
continue;
if (offset < 0 || offset >= ip->i_size)
panic("ufsdirhash_lookup: bad offset in hash array");
if ((offset & ~bmask) != blkoff) {
if (bp != NULL)
brelse(bp);
blkoff = offset & ~bmask;
if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) {
error = EJUSTRETURN;
goto fail;
}
}
KASSERT(bp != NULL, ("no buffer allocated"));
dp = (struct direct *)(bp->b_data + (offset & bmask));
if (dp->d_reclen == 0 || dp->d_reclen >
DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) {
error = EJUSTRETURN;
goto fail;
}
if (dp->d_namlen == namelen &&
bcmp(dp->d_name, name, namelen) == 0) {
if (prevoffp != NULL) {
if (offset & (DIRBLKSIZ - 1)) {
prevoff = ufsdirhash_getprev(dp,
offset);
if (prevoff == -1) {
error = EJUSTRETURN;
goto fail;
}
} else
prevoff = offset;
*prevoffp = prevoff;
}
dh->dh_seqoff = offset + DIRSIZ(0, dp);
*bpp = bp;
*offp = offset;
ufsdirhash_release(dh);
return (0);
}
if (seqoff != -1) {
seqoff = -1;
goto restart;
}
}
error = ENOENT;
fail:
ufsdirhash_release(dh);
if (bp != NULL)
brelse(bp);
return (error);
}
doff_t
ufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize)
{
struct direct *dp;
struct dirhash *dh;
struct buf *bp;
doff_t pos, slotstart;
int dirblock, error, freebytes, i;
dh = ip->i_dirhash;
KASSERT(dh != NULL && dh->dh_hash != NULL,
("ufsdirhash_findfree: Invalid dirhash %p\n", dh));
DIRHASH_ASSERT_LOCKED(dh);
dirblock = -1;
for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++)
if ((dirblock = dh->dh_firstfree[i]) != -1)
break;
if (dirblock == -1)
return (-1);
KASSERT(dirblock < dh->dh_nblk &&
dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN),
("ufsdirhash_findfree: bad stats"));
pos = dirblock * DIRBLKSIZ;
error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp);
if (error)
return (-1);
for (i = 0; i < DIRBLKSIZ; ) {
if (dp->d_reclen == 0) {
brelse(bp);
return (-1);
}
if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp))
break;
i += dp->d_reclen;
dp = (struct direct *)((char *)dp + dp->d_reclen);
}
if (i > DIRBLKSIZ) {
brelse(bp);
return (-1);
}
slotstart = pos + i;
freebytes = 0;
while (i < DIRBLKSIZ && freebytes < slotneeded) {
freebytes += dp->d_reclen;
if (dp->d_ino != 0)
freebytes -= DIRSIZ(0, dp);
if (dp->d_reclen == 0) {
brelse(bp);
return (-1);
}
i += dp->d_reclen;
dp = (struct direct *)((char *)dp + dp->d_reclen);
}
if (i > DIRBLKSIZ) {
brelse(bp);
return (-1);
}
if (freebytes < slotneeded)
panic("ufsdirhash_findfree: free mismatch");
brelse(bp);
*slotsize = pos + i - slotstart;
return (slotstart);
}
doff_t
ufsdirhash_enduseful(struct inode *ip)
{
struct dirhash *dh;
int i;
dh = ip->i_dirhash;
DIRHASH_ASSERT_LOCKED(dh);
KASSERT(dh != NULL && dh->dh_hash != NULL,
("ufsdirhash_enduseful: Invalid dirhash %p\n", dh));
if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN)
return (-1);
for (i = dh->dh_dirblks - 1; i >= 0; i--)
if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN)
break;
return ((doff_t)(i + 1) * DIRBLKSIZ);
}
void
ufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset)
{
struct dirhash *dh;
int slot;
if ((dh = ufsdirhash_acquire(ip)) == NULL)
return;
KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ,
("ufsdirhash_add: bad offset"));
if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) {
ufsdirhash_free_locked(ip);
return;
}
slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen);
while (DH_ENTRY(dh, slot) >= 0)
slot = WRAPINCR(slot, dh->dh_hlen);
if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY)
dh->dh_hused++;
DH_ENTRY(dh, slot) = offset;
dh->dh_lastused = time_second;
ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp));
ufsdirhash_release(dh);
}
void
ufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset)
{
struct dirhash *dh;
int slot;
if ((dh = ufsdirhash_acquire(ip)) == NULL)
return;
KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ,
("ufsdirhash_remove: bad offset"));
slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset);
ufsdirhash_delslot(dh, slot);
ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp));
ufsdirhash_release(dh);
}
void
ufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff,
doff_t newoff)
{
struct dirhash *dh;
int slot;
if ((dh = ufsdirhash_acquire(ip)) == NULL)
return;
KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ &&
newoff < dh->dh_dirblks * DIRBLKSIZ,
("ufsdirhash_move: bad offset"));
slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff);
DH_ENTRY(dh, slot) = newoff;
ufsdirhash_release(dh);
}
void
ufsdirhash_newblk(struct inode *ip, doff_t offset)
{
struct dirhash *dh;
int block;
if ((dh = ufsdirhash_acquire(ip)) == NULL)
return;
KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ,
("ufsdirhash_newblk: bad offset"));
block = offset / DIRBLKSIZ;
if (block >= dh->dh_nblk) {
ufsdirhash_free_locked(ip);
return;
}
dh->dh_dirblks = block + 1;
dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN;
if (dh->dh_firstfree[DH_NFSTATS] == -1)
dh->dh_firstfree[DH_NFSTATS] = block;
ufsdirhash_release(dh);
}
void
ufsdirhash_dirtrunc(struct inode *ip, doff_t offset)
{
struct dirhash *dh;
int block, i;
if ((dh = ufsdirhash_acquire(ip)) == NULL)
return;
KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ,
("ufsdirhash_dirtrunc: bad offset"));
block = howmany(offset, DIRBLKSIZ);
if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) {
ufsdirhash_free_locked(ip);
return;
}
if (dh->dh_firstfree[DH_NFSTATS] >= block)
dh->dh_firstfree[DH_NFSTATS] = -1;
for (i = block; i < dh->dh_dirblks; i++)
if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN)
panic("ufsdirhash_dirtrunc: blocks in use");
for (i = 0; i < DH_NFSTATS; i++)
if (dh->dh_firstfree[i] >= block)
panic("ufsdirhash_dirtrunc: first free corrupt");
dh->dh_dirblks = block;
ufsdirhash_release(dh);
}
void
ufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset)
{
struct dirhash *dh;
struct direct *dp;
int block, ffslot, i, nfree;
if (!ufs_dirhashcheck)
return;
if ((dh = ufsdirhash_acquire(ip)) == NULL)
return;
block = offset / DIRBLKSIZ;
if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks)
panic("ufsdirhash_checkblock: bad offset");
nfree = 0;
for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) {
dp = (struct direct *)(buf + i);
if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ)
panic("ufsdirhash_checkblock: bad dir");
if (dp->d_ino == 0) {
#if 0
if (i != 0)
panic("ufsdirhash_checkblock: bad dir inode");
#endif
nfree += dp->d_reclen;
continue;
}
ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i);
nfree += dp->d_reclen - DIRSIZ(0, dp);
}
if (i != DIRBLKSIZ)
panic("ufsdirhash_checkblock: bad dir end");
if (dh->dh_blkfree[block] * DIRALIGN != nfree)
panic("ufsdirhash_checkblock: bad free count");
ffslot = BLKFREE2IDX(nfree / DIRALIGN);
for (i = 0; i <= DH_NFSTATS; i++)
if (dh->dh_firstfree[i] == block && i != ffslot)
panic("ufsdirhash_checkblock: bad first-free");
if (dh->dh_firstfree[ffslot] == -1)
panic("ufsdirhash_checkblock: missing first-free entry");
ufsdirhash_release(dh);
}
static int
ufsdirhash_hash(struct dirhash *dh, char *name, int namelen)
{
uint32_t hash;
hash = fnv_32_buf(name, namelen, FNV1_32_INIT);
hash = fnv_32_buf(&dh, sizeof(dh), hash);
return (hash % dh->dh_hlen);
}
static void
ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff)
{
int block, i, nfidx, ofidx;
block = offset / DIRBLKSIZ;
KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks,
("dirhash bad offset"));
ofidx = BLKFREE2IDX(dh->dh_blkfree[block]);
dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN);
nfidx = BLKFREE2IDX(dh->dh_blkfree[block]);
if (ofidx != nfidx) {
if (dh->dh_firstfree[ofidx] == block) {
for (i = block + 1; i < dh->dh_dirblks; i++)
if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx)
break;
dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1;
}
if (dh->dh_firstfree[nfidx] > block ||
dh->dh_firstfree[nfidx] == -1)
dh->dh_firstfree[nfidx] = block;
}
}
static int
ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset)
{
int slot;
DIRHASH_ASSERT_LOCKED(dh);
KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full"));
slot = ufsdirhash_hash(dh, name, namelen);
while (DH_ENTRY(dh, slot) != offset &&
DH_ENTRY(dh, slot) != DIRHASH_EMPTY)
slot = WRAPINCR(slot, dh->dh_hlen);
if (DH_ENTRY(dh, slot) != offset)
panic("ufsdirhash_findslot: '%.*s' not found", namelen, name);
return (slot);
}
static void
ufsdirhash_delslot(struct dirhash *dh, int slot)
{
int i;
DIRHASH_ASSERT_LOCKED(dh);
DH_ENTRY(dh, slot) = DIRHASH_DEL;
for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; )
i = WRAPINCR(i, dh->dh_hlen);
if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) {
i = WRAPDECR(i, dh->dh_hlen);
while (DH_ENTRY(dh, i) == DIRHASH_DEL) {
DH_ENTRY(dh, i) = DIRHASH_EMPTY;
dh->dh_hused--;
i = WRAPDECR(i, dh->dh_hlen);
}
KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen"));
}
}
static doff_t
ufsdirhash_getprev(struct direct *dirp, doff_t offset)
{
struct direct *dp;
char *blkbuf;
doff_t blkoff, prevoff;
int entrypos, i;
blkoff = rounddown2(offset, DIRBLKSIZ);
entrypos = offset & (DIRBLKSIZ - 1);
blkbuf = (char *)dirp - entrypos;
prevoff = blkoff;
if (entrypos == 0)
return (-1);
for (i = 0; i < entrypos; i += dp->d_reclen) {
dp = (struct direct *)(blkbuf + i);
if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos)
return (-1);
prevoff = blkoff + i;
}
return (prevoff);
}
static int
ufsdirhash_destroy(struct dirhash *dh)
{
doff_t **hash;
uint8_t *blkfree;
int i, mem, narrays;
KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list"));
TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list);
dh->dh_onlist = 0;
hash = dh->dh_hash;
dh->dh_hash = NULL;
blkfree = dh->dh_blkfree;
dh->dh_blkfree = NULL;
narrays = dh->dh_narrays;
mem = dh->dh_memreq;
dh->dh_memreq = 0;
ufsdirhash_release(dh);
for (i = 0; i < narrays; i++)
DIRHASH_BLKFREE(hash[i]);
free(hash, M_DIRHASH);
free(blkfree, M_DIRHASH);
ufs_dirhashmem -= mem;
return (mem);
}
static int
ufsdirhash_recycle(int wanted)
{
struct dirhash *dh;
DIRHASHLIST_LOCK();
dh = TAILQ_FIRST(&ufsdirhash_list);
while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) {
if (dh == NULL || --dh->dh_score > 0) {
DIRHASHLIST_UNLOCK();
return (-1);
}
if (!sx_try_xlock(&dh->dh_lock)) {
dh = TAILQ_NEXT(dh, dh_list);
continue;
}
ufsdirhash_destroy(dh);
dh = TAILQ_FIRST(&ufsdirhash_list);
}
return (0);
}
static void
ufsdirhash_lowmem(void *arg __unused, int flags __unused)
{
struct dirhash *dh, *dh_temp;
int memfreed, memwanted;
ufs_dirhashlowmemcount++;
memfreed = 0;
memwanted = ufs_dirhashmem * ufs_dirhashreclaimpercent / 100;
DIRHASHLIST_LOCK();
TAILQ_FOREACH_SAFE(dh, &ufsdirhash_list, dh_list, dh_temp) {
if (sx_try_xlock(&dh->dh_lock))
memfreed += ufsdirhash_destroy(dh);
if (memfreed >= memwanted)
break;
}
DIRHASHLIST_UNLOCK();
}
static int
ufsdirhash_set_reclaimpercent(SYSCTL_HANDLER_ARGS)
{
int error, v;
v = ufs_dirhashreclaimpercent;
error = sysctl_handle_int(oidp, &v, v, req);
if (error)
return (error);
if (req->newptr == NULL)
return (error);
if (v == ufs_dirhashreclaimpercent)
return (0);
if (v < 0 || v > 100)
return (EINVAL);
ufs_dirhashreclaimpercent = v;
return (0);
}
void
ufsdirhash_init(void)
{
ufs_dirhashmaxmem = lmax(roundup(hibufspace / 64, PAGE_SIZE),
2 * 1024 * 1024);
ufsdirhash_zone = uma_zcreate("DIRHASH", DH_NBLKOFF * sizeof(doff_t),
NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
mtx_init(&ufsdirhash_mtx, "dirhash list", NULL, MTX_DEF);
TAILQ_INIT(&ufsdirhash_list);
EVENTHANDLER_REGISTER(vm_lowmem, ufsdirhash_lowmem, NULL,
EVENTHANDLER_PRI_FIRST);
}
void
ufsdirhash_uninit(void)
{
KASSERT(TAILQ_EMPTY(&ufsdirhash_list), ("ufsdirhash_uninit"));
uma_zdestroy(ufsdirhash_zone);
mtx_destroy(&ufsdirhash_mtx);
}
#endif