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

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

CVSweb <webmaster@jp.NetBSD.org>