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

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

CVSweb <webmaster@jp.NetBSD.org>