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

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

CVSweb <webmaster@jp.NetBSD.org>