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

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

CVSweb <webmaster@jp.NetBSD.org>