[BACK]Return to ufs_dirhash.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / ufs / ufs

Annotation of src/sys/ufs/ufs/ufs_dirhash.c, Revision 1.24

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

CVSweb <webmaster@jp.NetBSD.org>