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

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

CVSweb <webmaster@jp.NetBSD.org>