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

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

CVSweb <webmaster@jp.NetBSD.org>