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

1.21.8.1! yamt        1: /*     $NetBSD: ufs_dirhash.c,v 1.21 2008/01/03 19:28:50 ad Exp $      */
1.1       rumble      2:
                      3: /*
                      4:  * Copyright (c) 2001, 2002 Ian Dowse.  All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     16:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     17:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     18:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     19:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     20:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     21:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     22:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     23:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     24:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     25:  * SUCH DAMAGE.
                     26:  *
                     27:  * $FreeBSD: src/sys/ufs/ufs/ufs_dirhash.c,v 1.3.2.8 2004/12/08 11:54:13 dwmalone Exp $
                     28:  */
                     29:
1.19      rumble     30: #include <sys/cdefs.h>
1.21.8.1! yamt       31: __KERNEL_RCSID(0, "$NetBSD: ufs_dirhash.c,v 1.21 2008/01/03 19:28:50 ad Exp $");
1.19      rumble     32:
1.1       rumble     33: /*
                     34:  * This implements a hash-based lookup scheme for UFS directories.
                     35:  */
                     36:
                     37: #include <sys/param.h>
                     38: #include <sys/systm.h>
                     39: #include <sys/kernel.h>
                     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.21      ad         79: static pool_cache_t ufsdirhash_cache;
1.1       rumble     80:
1.16      ad         81: #define DIRHASHLIST_LOCK()             mutex_enter(&ufsdirhash_lock)
                     82: #define DIRHASHLIST_UNLOCK()           mutex_exit(&ufsdirhash_lock)
                     83: #define DIRHASH_LOCK(dh)               mutex_enter(&(dh)->dh_lock)
                     84: #define DIRHASH_UNLOCK(dh)             mutex_exit(&(dh)->dh_lock)
1.21      ad         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.20      ad        221:                                brelse(bp, 0);
1.21.8.1! yamt      222:                        if (ufs_blkatoff(vp, (off_t)pos, NULL, &bp, false) != 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.20      ad        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.20      ad        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.20      ad        417:                                brelse(bp, 0);
1.1       rumble    418:                        blkoff = offset & ~bmask;
1.21.8.1! yamt      419:                        if (ufs_blkatoff(vp, (off_t)blkoff,
        !           420:                            NULL, &bp, true) != 0)
1.1       rumble    421:                                return (EJUSTRETURN);
                    422:                }
1.13      christos  423:                dp = (struct direct *)((char *)bp->b_data + (offset & bmask));
1.1       rumble    424:                if (dp->d_reclen == 0 || dp->d_reclen >
                    425:                    dirblksiz - (offset & (dirblksiz - 1))) {
                    426:                        /* Corrupted directory. */
1.20      ad        427:                        brelse(bp, 0);
1.1       rumble    428:                        return (EJUSTRETURN);
                    429:                }
                    430:                if (dp->d_namlen == namelen &&
                    431:                    bcmp(dp->d_name, name, namelen) == 0) {
                    432:                        /* Found. Get the prev offset if needed. */
                    433:                        if (prevoffp != NULL) {
                    434:                                if (offset & (dirblksiz - 1)) {
                    435:                                        prevoff = ufsdirhash_getprev(dp,
                    436:                                            offset, dirblksiz);
                    437:                                        if (prevoff == -1) {
1.20      ad        438:                                                brelse(bp, 0);
1.1       rumble    439:                                                return (EJUSTRETURN);
                    440:                                        }
                    441:                                } else
                    442:                                        prevoff = offset;
                    443:                                *prevoffp = prevoff;
                    444:                        }
                    445:
                    446:                        /* Check for sequential access, and update offset. */
                    447:                        if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset)
                    448:                                dh->dh_seqopt = 1;
                    449:                        dh->dh_seqoff = offset + DIRSIZ(0, dp, needswap);
                    450:
                    451:                        *bpp = bp;
                    452:                        *offp = offset;
                    453:                        return (0);
                    454:                }
                    455:
                    456:                DIRHASH_LOCK(dh);
                    457:                if (dh->dh_hash == NULL) {
                    458:                        DIRHASH_UNLOCK(dh);
                    459:                        if (bp != NULL)
1.20      ad        460:                                brelse(bp, 0);
1.1       rumble    461:                        ufsdirhash_free(ip);
                    462:                        return (EJUSTRETURN);
                    463:                }
                    464:                /*
                    465:                 * When the name doesn't match in the seqopt case, go back
                    466:                 * and search normally.
                    467:                 */
                    468:                if (dh->dh_seqopt) {
                    469:                        dh->dh_seqopt = 0;
                    470:                        goto restart;
                    471:                }
                    472:        }
                    473:        DIRHASH_UNLOCK(dh);
                    474:        if (bp != NULL)
1.20      ad        475:                brelse(bp, 0);
1.1       rumble    476:        return (ENOENT);
                    477: }
                    478:
                    479: /*
                    480:  * Find a directory block with room for 'slotneeded' bytes. Returns
                    481:  * the offset of the directory entry that begins the free space.
                    482:  * This will either be the offset of an existing entry that has free
                    483:  * space at the end, or the offset of an entry with d_ino == 0 at
                    484:  * the start of a DIRBLKSIZ block.
                    485:  *
                    486:  * To use the space, the caller may need to compact existing entries in
                    487:  * the directory. The total number of bytes in all of the entries involved
                    488:  * in the compaction is stored in *slotsize. In other words, all of
                    489:  * the entries that must be compacted are exactly contained in the
                    490:  * region beginning at the returned offset and spanning *slotsize bytes.
                    491:  *
                    492:  * Returns -1 if no space was found, indicating that the directory
                    493:  * must be extended.
                    494:  */
                    495: doff_t
                    496: ufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize)
                    497: {
                    498:        struct direct *dp;
                    499:        struct dirhash *dh;
                    500:        struct buf *bp;
                    501:        doff_t pos, slotstart;
                    502:        int dirblock, error, freebytes, i;
1.2       perry     503:        const int needswap = UFS_MPNEEDSWAP(ip->i_ump);
1.1       rumble    504:        int dirblksiz = ip->i_ump->um_dirblksiz;
                    505:
                    506:        if ((dh = ip->i_dirhash) == NULL)
                    507:                return (-1);
                    508:        DIRHASH_LOCK(dh);
                    509:        if (dh->dh_hash == NULL) {
                    510:                DIRHASH_UNLOCK(dh);
                    511:                ufsdirhash_free(ip);
                    512:                return (-1);
                    513:        }
                    514:
                    515:        /* Find a directory block with the desired free space. */
                    516:        dirblock = -1;
                    517:        for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++)
                    518:                if ((dirblock = dh->dh_firstfree[i]) != -1)
                    519:                        break;
                    520:        if (dirblock == -1) {
                    521:                DIRHASH_UNLOCK(dh);
                    522:                return (-1);
                    523:        }
                    524:
                    525:        KASSERT(dirblock < dh->dh_nblk &&
                    526:            dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN));
                    527:        DIRHASH_UNLOCK(dh);
                    528:        pos = dirblock * dirblksiz;
1.21.8.1! yamt      529:        error = ufs_blkatoff(ip->i_vnode, (off_t)pos, (void *)&dp, &bp, false);
1.1       rumble    530:        if (error)
                    531:                return (-1);
                    532:        /* Find the first entry with free space. */
                    533:        for (i = 0; i < dirblksiz; ) {
                    534:                if (dp->d_reclen == 0) {
1.20      ad        535:                        brelse(bp, 0);
1.1       rumble    536:                        return (-1);
                    537:                }
                    538:                if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp, needswap))
                    539:                        break;
                    540:                i += dp->d_reclen;
                    541:                dp = (struct direct *)((char *)dp + dp->d_reclen);
                    542:        }
                    543:        if (i > dirblksiz) {
1.20      ad        544:                brelse(bp, 0);
1.1       rumble    545:                return (-1);
                    546:        }
                    547:        slotstart = pos + i;
                    548:
                    549:        /* Find the range of entries needed to get enough space */
                    550:        freebytes = 0;
                    551:        while (i < dirblksiz && freebytes < slotneeded) {
                    552:                freebytes += dp->d_reclen;
                    553:                if (dp->d_ino != 0)
                    554:                        freebytes -= DIRSIZ(0, dp, needswap);
                    555:                if (dp->d_reclen == 0) {
1.20      ad        556:                        brelse(bp, 0);
1.1       rumble    557:                        return (-1);
                    558:                }
                    559:                i += dp->d_reclen;
                    560:                dp = (struct direct *)((char *)dp + dp->d_reclen);
                    561:        }
                    562:        if (i > dirblksiz) {
1.20      ad        563:                brelse(bp, 0);
1.1       rumble    564:                return (-1);
                    565:        }
                    566:        if (freebytes < slotneeded)
                    567:                panic("ufsdirhash_findfree: free mismatch");
1.20      ad        568:        brelse(bp, 0);
1.1       rumble    569:        *slotsize = pos + i - slotstart;
                    570:        return (slotstart);
                    571: }
                    572:
                    573: /*
                    574:  * Return the start of the unused space at the end of a directory, or
                    575:  * -1 if there are no trailing unused blocks.
                    576:  */
                    577: doff_t
                    578: ufsdirhash_enduseful(struct inode *ip)
                    579: {
                    580:        struct dirhash *dh;
                    581:        int i;
                    582:        int dirblksiz = ip->i_ump->um_dirblksiz;
                    583:
                    584:        if ((dh = ip->i_dirhash) == NULL)
                    585:                return (-1);
                    586:        DIRHASH_LOCK(dh);
                    587:        if (dh->dh_hash == NULL) {
                    588:                DIRHASH_UNLOCK(dh);
                    589:                ufsdirhash_free(ip);
                    590:                return (-1);
                    591:        }
                    592:
                    593:        if (dh->dh_blkfree[dh->dh_dirblks - 1] != dirblksiz / DIRALIGN) {
                    594:                DIRHASH_UNLOCK(dh);
                    595:                return (-1);
                    596:        }
                    597:
                    598:        for (i = dh->dh_dirblks - 1; i >= 0; i--)
                    599:                if (dh->dh_blkfree[i] != dirblksiz / DIRALIGN)
                    600:                        break;
                    601:        DIRHASH_UNLOCK(dh);
                    602:        return ((doff_t)(i + 1) * dirblksiz);
                    603: }
                    604:
                    605: /*
                    606:  * Insert information into the hash about a new directory entry. dirp
                    607:  * points to a struct direct containing the entry, and offset specifies
                    608:  * the offset of this entry.
                    609:  */
                    610: void
                    611: ufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset)
                    612: {
                    613:        struct dirhash *dh;
                    614:        int slot;
                    615:        const int needswap = UFS_MPNEEDSWAP(ip->i_ump);
                    616:        int dirblksiz = ip->i_ump->um_dirblksiz;
                    617:
                    618:        if ((dh = ip->i_dirhash) == NULL)
                    619:                return;
                    620:        DIRHASH_LOCK(dh);
                    621:        if (dh->dh_hash == NULL) {
                    622:                DIRHASH_UNLOCK(dh);
                    623:                ufsdirhash_free(ip);
                    624:                return;
                    625:        }
                    626:
                    627:        KASSERT(offset < dh->dh_dirblks * dirblksiz);
                    628:        /*
                    629:         * Normal hash usage is < 66%. If the usage gets too high then
                    630:         * remove the hash entirely and let it be rebuilt later.
                    631:         */
                    632:        if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) {
                    633:                DIRHASH_UNLOCK(dh);
                    634:                ufsdirhash_free(ip);
                    635:                return;
                    636:        }
                    637:
                    638:        /* Find a free hash slot (empty or deleted), and add the entry. */
                    639:        slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen);
                    640:        while (DH_ENTRY(dh, slot) >= 0)
                    641:                slot = WRAPINCR(slot, dh->dh_hlen);
                    642:        if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY)
                    643:                dh->dh_hused++;
                    644:        DH_ENTRY(dh, slot) = offset;
                    645:
                    646:        /* Update the per-block summary info. */
                    647:        ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp, needswap), dirblksiz);
                    648:        DIRHASH_UNLOCK(dh);
                    649: }
                    650:
                    651: /*
                    652:  * Remove the specified directory entry from the hash. The entry to remove
                    653:  * is defined by the name in `dirp', which must exist at the specified
                    654:  * `offset' within the directory.
                    655:  */
                    656: void
                    657: ufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset)
                    658: {
                    659:        struct dirhash *dh;
                    660:        int slot;
                    661:        const int needswap = UFS_MPNEEDSWAP(ip->i_ump);
                    662:        int dirblksiz = ip->i_ump->um_dirblksiz;
                    663:
                    664:        if ((dh = ip->i_dirhash) == NULL)
                    665:                return;
                    666:        DIRHASH_LOCK(dh);
                    667:        if (dh->dh_hash == NULL) {
                    668:                DIRHASH_UNLOCK(dh);
                    669:                ufsdirhash_free(ip);
                    670:                return;
                    671:        }
                    672:
                    673:        KASSERT(offset < dh->dh_dirblks * dirblksiz);
                    674:        /* Find the entry */
                    675:        slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset);
                    676:
                    677:        /* Remove the hash entry. */
                    678:        ufsdirhash_delslot(dh, slot);
                    679:
                    680:        /* Update the per-block summary info. */
                    681:        ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp, needswap), dirblksiz);
                    682:        DIRHASH_UNLOCK(dh);
                    683: }
                    684:
                    685: /*
                    686:  * Change the offset associated with a directory entry in the hash. Used
                    687:  * when compacting directory blocks.
                    688:  */
                    689: void
                    690: ufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff,
                    691:     doff_t newoff)
                    692: {
                    693:        struct dirhash *dh;
                    694:        int slot;
                    695:
                    696:        if ((dh = ip->i_dirhash) == NULL)
                    697:                return;
                    698:        DIRHASH_LOCK(dh);
                    699:        if (dh->dh_hash == NULL) {
                    700:                DIRHASH_UNLOCK(dh);
                    701:                ufsdirhash_free(ip);
                    702:                return;
                    703:        }
                    704:
                    705:        KASSERT(oldoff < dh->dh_dirblks * ip->i_ump->um_dirblksiz &&
                    706:            newoff < dh->dh_dirblks * ip->i_ump->um_dirblksiz);
                    707:        /* Find the entry, and update the offset. */
                    708:        slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff);
                    709:        DH_ENTRY(dh, slot) = newoff;
                    710:        DIRHASH_UNLOCK(dh);
                    711: }
                    712:
                    713: /*
                    714:  * Inform dirhash that the directory has grown by one block that
                    715:  * begins at offset (i.e. the new length is offset + DIRBLKSIZ).
                    716:  */
                    717: void
                    718: ufsdirhash_newblk(struct inode *ip, doff_t offset)
                    719: {
                    720:        struct dirhash *dh;
                    721:        int block;
                    722:        int dirblksiz = ip->i_ump->um_dirblksiz;
                    723:
                    724:        if ((dh = ip->i_dirhash) == NULL)
                    725:                return;
                    726:        DIRHASH_LOCK(dh);
                    727:        if (dh->dh_hash == NULL) {
                    728:                DIRHASH_UNLOCK(dh);
                    729:                ufsdirhash_free(ip);
                    730:                return;
                    731:        }
                    732:
                    733:        KASSERT(offset == dh->dh_dirblks * dirblksiz);
                    734:        block = offset / dirblksiz;
                    735:        if (block >= dh->dh_nblk) {
                    736:                /* Out of space; must rebuild. */
                    737:                DIRHASH_UNLOCK(dh);
                    738:                ufsdirhash_free(ip);
                    739:                return;
                    740:        }
                    741:        dh->dh_dirblks = block + 1;
                    742:
                    743:        /* Account for the new free block. */
                    744:        dh->dh_blkfree[block] = dirblksiz / DIRALIGN;
                    745:        if (dh->dh_firstfree[DH_NFSTATS] == -1)
                    746:                dh->dh_firstfree[DH_NFSTATS] = block;
                    747:        DIRHASH_UNLOCK(dh);
                    748: }
                    749:
                    750: /*
                    751:  * Inform dirhash that the directory is being truncated.
                    752:  */
                    753: void
                    754: ufsdirhash_dirtrunc(struct inode *ip, doff_t offset)
                    755: {
                    756:        struct dirhash *dh;
                    757:        int block, i;
                    758:        int dirblksiz = ip->i_ump->um_dirblksiz;
                    759:
                    760:        if ((dh = ip->i_dirhash) == NULL)
                    761:                return;
                    762:        DIRHASH_LOCK(dh);
                    763:        if (dh->dh_hash == NULL) {
                    764:                DIRHASH_UNLOCK(dh);
                    765:                ufsdirhash_free(ip);
                    766:                return;
                    767:        }
                    768:
                    769:        KASSERT(offset <= dh->dh_dirblks * dirblksiz);
                    770:        block = howmany(offset, dirblksiz);
                    771:        /*
                    772:         * If the directory shrinks to less than 1/8 of dh_nblk blocks
                    773:         * (about 20% of its original size due to the 50% extra added in
                    774:         * ufsdirhash_build) then free it, and let the caller rebuild
                    775:         * if necessary.
                    776:         */
                    777:        if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) {
                    778:                DIRHASH_UNLOCK(dh);
                    779:                ufsdirhash_free(ip);
                    780:                return;
                    781:        }
                    782:
                    783:        /*
                    784:         * Remove any `first free' information pertaining to the
                    785:         * truncated blocks. All blocks we're removing should be
                    786:         * completely unused.
                    787:         */
                    788:        if (dh->dh_firstfree[DH_NFSTATS] >= block)
                    789:                dh->dh_firstfree[DH_NFSTATS] = -1;
                    790:        for (i = block; i < dh->dh_dirblks; i++)
                    791:                if (dh->dh_blkfree[i] != dirblksiz / DIRALIGN)
                    792:                        panic("ufsdirhash_dirtrunc: blocks in use");
                    793:        for (i = 0; i < DH_NFSTATS; i++)
                    794:                if (dh->dh_firstfree[i] >= block)
                    795:                        panic("ufsdirhash_dirtrunc: first free corrupt");
                    796:        dh->dh_dirblks = block;
                    797:        DIRHASH_UNLOCK(dh);
                    798: }
                    799:
                    800: /*
                    801:  * Debugging function to check that the dirhash information about
                    802:  * a directory block matches its actual contents. Panics if a mismatch
                    803:  * is detected.
                    804:  *
1.3       christos  805:  * On entry, `sbuf' should point to the start of an in-core
1.1       rumble    806:  * DIRBLKSIZ-sized directory block, and `offset' should contain the
                    807:  * offset from the start of the directory of that block.
                    808:  */
                    809: void
1.3       christos  810: ufsdirhash_checkblock(struct inode *ip, char *sbuf, doff_t offset)
1.1       rumble    811: {
                    812:        struct dirhash *dh;
                    813:        struct direct *dp;
                    814:        int block, ffslot, i, nfree;
                    815:        const int needswap = UFS_MPNEEDSWAP(ip->i_ump);
                    816:        int dirblksiz = ip->i_ump->um_dirblksiz;
                    817:
                    818:        if (!ufs_dirhashcheck)
                    819:                return;
                    820:        if ((dh = ip->i_dirhash) == NULL)
                    821:                return;
                    822:        DIRHASH_LOCK(dh);
                    823:        if (dh->dh_hash == NULL) {
                    824:                DIRHASH_UNLOCK(dh);
                    825:                ufsdirhash_free(ip);
                    826:                return;
                    827:        }
                    828:
                    829:        block = offset / dirblksiz;
                    830:        if ((offset & (dirblksiz - 1)) != 0 || block >= dh->dh_dirblks)
                    831:                panic("ufsdirhash_checkblock: bad offset");
                    832:
                    833:        nfree = 0;
                    834:        for (i = 0; i < dirblksiz; i += dp->d_reclen) {
1.3       christos  835:                dp = (struct direct *)(sbuf + i);
1.1       rumble    836:                if (dp->d_reclen == 0 || i + dp->d_reclen > dirblksiz)
                    837:                        panic("ufsdirhash_checkblock: bad dir");
                    838:
                    839:                if (dp->d_ino == 0) {
                    840: #if 0
                    841:                        /*
                    842:                         * XXX entries with d_ino == 0 should only occur
                    843:                         * at the start of a DIRBLKSIZ block. However the
                    844:                         * ufs code is tolerant of such entries at other
                    845:                         * offsets, and fsck does not fix them.
                    846:                         */
                    847:                        if (i != 0)
                    848:                                panic("ufsdirhash_checkblock: bad dir inode");
                    849: #endif
                    850:                        nfree += dp->d_reclen;
                    851:                        continue;
                    852:                }
                    853:
                    854:                /* Check that the entry exists (will panic if it doesn't). */
                    855:                ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i);
                    856:
                    857:                nfree += dp->d_reclen - DIRSIZ(0, dp, needswap);
                    858:        }
                    859:        if (i != dirblksiz)
                    860:                panic("ufsdirhash_checkblock: bad dir end");
                    861:
                    862:        if (dh->dh_blkfree[block] * DIRALIGN != nfree)
                    863:                panic("ufsdirhash_checkblock: bad free count");
                    864:
                    865:        ffslot = BLKFREE2IDX(nfree / DIRALIGN);
                    866:        for (i = 0; i <= DH_NFSTATS; i++)
                    867:                if (dh->dh_firstfree[i] == block && i != ffslot)
                    868:                        panic("ufsdirhash_checkblock: bad first-free");
                    869:        if (dh->dh_firstfree[ffslot] == -1)
                    870:                panic("ufsdirhash_checkblock: missing first-free entry");
                    871:        DIRHASH_UNLOCK(dh);
                    872: }
                    873:
                    874: /*
                    875:  * Hash the specified filename into a dirhash slot.
                    876:  */
                    877: static int
                    878: ufsdirhash_hash(struct dirhash *dh, const char *name, int namelen)
                    879: {
                    880:        u_int32_t hash;
                    881:
                    882:        /*
                    883:         * We hash the name and then some other bit of data that is
                    884:         * invariant over the dirhash's lifetime. Otherwise names
                    885:         * differing only in the last byte are placed close to one
                    886:         * another in the table, which is bad for linear probing.
                    887:         */
                    888:        hash = hash32_buf(name, namelen, HASH32_BUF_INIT);
                    889:        hash = hash32_buf(&dh, sizeof(dh), hash);
                    890:        return (hash % dh->dh_hlen);
                    891: }
                    892:
                    893: /*
                    894:  * Adjust the number of free bytes in the block containing `offset'
                    895:  * by the value specified by `diff'.
                    896:  *
                    897:  * The caller must ensure we have exclusive access to `dh'; normally
1.16      ad        898:  * that means that dh_lock should be held, but this is also called
1.1       rumble    899:  * from ufsdirhash_build() where exclusive access can be assumed.
                    900:  */
                    901: static void
                    902: ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff, int dirblksiz)
                    903: {
                    904:        int block, i, nfidx, ofidx;
                    905:
                    906:        /* Update the per-block summary info. */
                    907:        block = offset / dirblksiz;
                    908:        KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks);
                    909:        ofidx = BLKFREE2IDX(dh->dh_blkfree[block]);
                    910:        dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN);
                    911:        nfidx = BLKFREE2IDX(dh->dh_blkfree[block]);
                    912:
                    913:        /* Update the `first free' list if necessary. */
                    914:        if (ofidx != nfidx) {
                    915:                /* If removing, scan forward for the next block. */
                    916:                if (dh->dh_firstfree[ofidx] == block) {
                    917:                        for (i = block + 1; i < dh->dh_dirblks; i++)
                    918:                                if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx)
                    919:                                        break;
                    920:                        dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1;
                    921:                }
                    922:
                    923:                /* Make this the new `first free' if necessary */
                    924:                if (dh->dh_firstfree[nfidx] > block ||
                    925:                    dh->dh_firstfree[nfidx] == -1)
                    926:                        dh->dh_firstfree[nfidx] = block;
                    927:        }
                    928: }
                    929:
                    930: /*
                    931:  * Find the specified name which should have the specified offset.
                    932:  * Returns a slot number, and panics on failure.
                    933:  *
                    934:  * `dh' must be locked on entry and remains so on return.
                    935:  */
                    936: static int
                    937: ufsdirhash_findslot(struct dirhash *dh, const char *name, int namelen,
                    938:     doff_t offset)
                    939: {
                    940:        int slot;
                    941:
1.16      ad        942:        KASSERT(mutex_owned(&dh->dh_lock));
                    943:
1.1       rumble    944:        /* Find the entry. */
                    945:        KASSERT(dh->dh_hused < dh->dh_hlen);
                    946:        slot = ufsdirhash_hash(dh, name, namelen);
                    947:        while (DH_ENTRY(dh, slot) != offset &&
                    948:            DH_ENTRY(dh, slot) != DIRHASH_EMPTY)
                    949:                slot = WRAPINCR(slot, dh->dh_hlen);
                    950:        if (DH_ENTRY(dh, slot) != offset)
                    951:                panic("ufsdirhash_findslot: '%.*s' not found", namelen, name);
                    952:
                    953:        return (slot);
                    954: }
                    955:
                    956: /*
                    957:  * Remove the entry corresponding to the specified slot from the hash array.
                    958:  *
                    959:  * `dh' must be locked on entry and remains so on return.
                    960:  */
                    961: static void
                    962: ufsdirhash_delslot(struct dirhash *dh, int slot)
                    963: {
                    964:        int i;
                    965:
1.16      ad        966:        KASSERT(mutex_owned(&dh->dh_lock));
                    967:
1.1       rumble    968:        /* Mark the entry as deleted. */
                    969:        DH_ENTRY(dh, slot) = DIRHASH_DEL;
                    970:
                    971:        /* If this is the end of a chain of DIRHASH_DEL slots, remove them. */
                    972:        for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; )
                    973:                i = WRAPINCR(i, dh->dh_hlen);
                    974:        if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) {
                    975:                i = WRAPDECR(i, dh->dh_hlen);
                    976:                while (DH_ENTRY(dh, i) == DIRHASH_DEL) {
                    977:                        DH_ENTRY(dh, i) = DIRHASH_EMPTY;
                    978:                        dh->dh_hused--;
                    979:                        i = WRAPDECR(i, dh->dh_hlen);
                    980:                }
                    981:                KASSERT(dh->dh_hused >= 0);
                    982:        }
                    983: }
                    984:
                    985: /*
                    986:  * Given a directory entry and its offset, find the offset of the
                    987:  * previous entry in the same DIRBLKSIZ-sized block. Returns an
                    988:  * offset, or -1 if there is no previous entry in the block or some
                    989:  * other problem occurred.
                    990:  */
                    991: static doff_t
                    992: ufsdirhash_getprev(struct direct *dirp, doff_t offset, int dirblksiz)
                    993: {
                    994:        struct direct *dp;
                    995:        char *blkbuf;
                    996:        doff_t blkoff, prevoff;
                    997:        int entrypos, i;
                    998:
                    999:        blkoff = offset & ~(dirblksiz - 1);     /* offset of start of block */
                   1000:        entrypos = offset & (dirblksiz - 1);    /* entry relative to block */
                   1001:        blkbuf = (char *)dirp - entrypos;
                   1002:        prevoff = blkoff;
                   1003:
                   1004:        /* If `offset' is the start of a block, there is no previous entry. */
                   1005:        if (entrypos == 0)
                   1006:                return (-1);
                   1007:
                   1008:        /* Scan from the start of the block until we get to the entry. */
                   1009:        for (i = 0; i < entrypos; i += dp->d_reclen) {
                   1010:                dp = (struct direct *)(blkbuf + i);
                   1011:                if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos)
                   1012:                        return (-1);    /* Corrupted directory. */
                   1013:                prevoff = blkoff + i;
                   1014:        }
                   1015:        return (prevoff);
                   1016: }
                   1017:
                   1018: /*
                   1019:  * Try to free up `wanted' bytes by stealing memory from existing
                   1020:  * dirhashes. Returns zero with list locked if successful.
                   1021:  */
                   1022: static int
                   1023: ufsdirhash_recycle(int wanted)
                   1024: {
                   1025:        struct dirhash *dh;
                   1026:        doff_t **hash;
                   1027:        u_int8_t *blkfree;
                   1028:        int i, mem, narrays;
                   1029:
                   1030:        DIRHASHLIST_LOCK();
                   1031:        while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) {
                   1032:                /* Find a dirhash, and lock it. */
                   1033:                if ((dh = TAILQ_FIRST(&ufsdirhash_list)) == NULL) {
                   1034:                        DIRHASHLIST_UNLOCK();
                   1035:                        return (-1);
                   1036:                }
                   1037:                DIRHASH_LOCK(dh);
                   1038:                KASSERT(dh->dh_hash != NULL);
                   1039:
                   1040:                /* Decrement the score; only recycle if it becomes zero. */
                   1041:                if (--dh->dh_score > 0) {
                   1042:                        DIRHASH_UNLOCK(dh);
                   1043:                        DIRHASHLIST_UNLOCK();
                   1044:                        return (-1);
                   1045:                }
                   1046:
                   1047:                /* Remove it from the list and detach its memory. */
                   1048:                TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list);
                   1049:                dh->dh_onlist = 0;
                   1050:                hash = dh->dh_hash;
                   1051:                dh->dh_hash = NULL;
                   1052:                blkfree = dh->dh_blkfree;
                   1053:                dh->dh_blkfree = NULL;
                   1054:                narrays = dh->dh_narrays;
                   1055:                mem = narrays * sizeof(*dh->dh_hash) +
                   1056:                    narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) +
                   1057:                    dh->dh_nblk * sizeof(*dh->dh_blkfree);
                   1058:
                   1059:                /* Unlock everything, free the detached memory. */
                   1060:                DIRHASH_UNLOCK(dh);
                   1061:                DIRHASHLIST_UNLOCK();
                   1062:                for (i = 0; i < narrays; i++)
                   1063:                        DIRHASH_BLKFREE(hash[i]);
                   1064:                FREE(hash, M_DIRHASH);
                   1065:                FREE(blkfree, M_DIRHASH);
                   1066:
                   1067:                /* Account for the returned memory, and repeat if necessary. */
                   1068:                DIRHASHLIST_LOCK();
                   1069:                ufs_dirhashmem -= mem;
                   1070:        }
                   1071:        /* Success; return with list locked. */
                   1072:        return (0);
                   1073: }
                   1074:
                   1075: void
                   1076: ufsdirhash_init()
                   1077: {
1.15      pooka    1078:
1.17      ad       1079:        mutex_init(&ufsdirhash_lock, MUTEX_DEFAULT, IPL_NONE);
1.15      pooka    1080:        malloc_type_attach(M_DIRHASH);
1.21      ad       1081:        ufsdirhash_cache = pool_cache_init(DH_NBLKOFF * sizeof(daddr_t), 0,
                   1082:            0, 0, "ufsdirhash", NULL, IPL_NONE, NULL, NULL, NULL);
1.1       rumble   1083:        TAILQ_INIT(&ufsdirhash_list);
                   1084: }
                   1085:
                   1086: void
1.5       thorpej  1087: ufsdirhash_done(void)
1.1       rumble   1088: {
1.15      pooka    1089:
1.1       rumble   1090:        KASSERT(TAILQ_EMPTY(&ufsdirhash_list));
1.21      ad       1091:        pool_cache_destroy(ufsdirhash_cache);
1.15      pooka    1092:        malloc_type_detach(M_DIRHASH);
1.17      ad       1093:        mutex_destroy(&ufsdirhash_lock);
1.1       rumble   1094: }
                   1095:
                   1096: SYSCTL_SETUP(sysctl_vfs_ufs_setup, "sysctl vfs.ufs.dirhash subtree setup")
                   1097: {
1.4       atatat   1098:        const struct sysctlnode *rnode, *cnode;
1.1       rumble   1099:
                   1100:        sysctl_createv(clog, 0, NULL, &rnode,
                   1101:                       CTLFLAG_PERMANENT,
                   1102:                       CTLTYPE_NODE, "vfs", NULL,
                   1103:                       NULL, 0, NULL, 0,
                   1104:                       CTL_VFS, CTL_EOL);
                   1105:
                   1106:        sysctl_createv(clog, 0, &rnode, &rnode,
                   1107:                       CTLFLAG_PERMANENT,
                   1108:                       CTLTYPE_NODE, "ufs",
                   1109:                       SYSCTL_DESCR("ufs"),
                   1110:                       NULL, 0, NULL, 0,
                   1111:                       CTL_CREATE, CTL_EOL);
                   1112:
                   1113:        sysctl_createv(clog, 0, &rnode, &rnode,
                   1114:                       CTLFLAG_PERMANENT,
                   1115:                       CTLTYPE_NODE, "dirhash",
                   1116:                       SYSCTL_DESCR("dirhash"),
                   1117:                       NULL, 0, NULL, 0,
                   1118:                       CTL_CREATE, CTL_EOL);
                   1119:
                   1120:        sysctl_createv(clog, 0, &rnode, &cnode,
                   1121:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1122:                       CTLTYPE_INT, "minblocks",
                   1123:                       SYSCTL_DESCR("minimum hashed directory size in blocks"),
                   1124:                       NULL, 0, &ufs_dirhashminblks, 0,
                   1125:                       CTL_CREATE, CTL_EOL);
                   1126:
                   1127:        sysctl_createv(clog, 0, &rnode, &cnode,
                   1128:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1129:                       CTLTYPE_INT, "maxmem",
                   1130:                       SYSCTL_DESCR("maximum dirhash memory usage"),
                   1131:                       NULL, 0, &ufs_dirhashmaxmem, 0,
                   1132:                       CTL_CREATE, CTL_EOL);
                   1133:
                   1134:        sysctl_createv(clog, 0, &rnode, &cnode,
                   1135:                       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
                   1136:                       CTLTYPE_INT, "memused",
                   1137:                       SYSCTL_DESCR("current dirhash memory usage"),
                   1138:                       NULL, 0, &ufs_dirhashmem, 0,
                   1139:                       CTL_CREATE, CTL_EOL);
                   1140:
                   1141:        sysctl_createv(clog, 0, &rnode, &cnode,
                   1142:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1143:                       CTLTYPE_INT, "docheck",
                   1144:                       SYSCTL_DESCR("enable extra sanity checks"),
                   1145:                       NULL, 0, &ufs_dirhashcheck, 0,
                   1146:                       CTL_CREATE, CTL_EOL);
                   1147: }

CVSweb <webmaster@jp.NetBSD.org>