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

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

CVSweb <webmaster@jp.NetBSD.org>