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

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

CVSweb <webmaster@jp.NetBSD.org>