[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.16

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

CVSweb <webmaster@jp.NetBSD.org>