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

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

CVSweb <webmaster@jp.NetBSD.org>