Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/sys/ufs/ufs/ufs_dirhash.c,v rcsdiff: /ftp/cvs/cvsroot/src/sys/ufs/ufs/ufs_dirhash.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.23.2.1 retrieving revision 1.37 diff -u -p -r1.23.2.1 -r1.37 --- src/sys/ufs/ufs/ufs_dirhash.c 2008/06/18 16:33:59 1.23.2.1 +++ src/sys/ufs/ufs/ufs_dirhash.c 2014/12/20 00:28:05 1.37 @@ -1,4 +1,4 @@ -/* $NetBSD: ufs_dirhash.c,v 1.23.2.1 2008/06/18 16:33:59 simonb Exp $ */ +/* $NetBSD: ufs_dirhash.c,v 1.37 2014/12/20 00:28:05 christos Exp $ */ /* * Copyright (c) 2001, 2002 Ian Dowse. All rights reserved. @@ -28,7 +28,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: ufs_dirhash.c,v 1.23.2.1 2008/06/18 16:33:59 simonb Exp $"); +__KERNEL_RCSID(0, "$NetBSD: ufs_dirhash.c,v 1.37 2014/12/20 00:28:05 christos Exp $"); /* * This implements a hash-based lookup scheme for UFS directories. @@ -93,6 +93,8 @@ static TAILQ_HEAD(, dirhash) ufsdirhash_ /* Protects: ufsdirhash_list, `dh_list' field, ufs_dirhashmem. */ static kmutex_t ufsdirhash_lock; +static struct sysctllog *ufsdirhash_sysctl_log; + /* * Locking order: * ufsdirhash_lock @@ -138,13 +140,13 @@ ufsdirhash_build(struct inode *ip) } /* Don't hash removed directories. */ - if (ip->i_ffs_effnlink == 0) + if (ip->i_nlink == 0) return (-1); vp = ip->i_vnode; /* Allocate 50% more entries than this dir size could ever need. */ KASSERT(ip->i_size >= dirblksiz); - nslots = ip->i_size / DIRECTSIZ(1); + nslots = ip->i_size / UFS_DIRECTSIZ(1); nslots = (nslots * 3 + 1) / 2; narrays = howmany(nslots, DH_NBLKOFF); nslots = narrays * DH_NBLKOFF; @@ -237,7 +239,7 @@ ufsdirhash_build(struct inode *ip) slot = WRAPINCR(slot, dh->dh_hlen); dh->dh_hused++; DH_ENTRY(dh, slot) = pos; - ufsdirhash_adjfree(dh, pos, -DIRSIZ(0, ep, needswap), + ufsdirhash_adjfree(dh, pos, -UFS_DIRSIZ(0, ep, needswap), dirblksiz); } pos += ep->d_reclen; @@ -253,6 +255,8 @@ ufsdirhash_build(struct inode *ip) return (0); fail: + ip->i_dirhash = NULL; + DIRHASH_UNLOCK(dh); if (dh->dh_hash != NULL) { for (i = 0; i < narrays; i++) if (dh->dh_hash[i] != NULL) @@ -263,7 +267,6 @@ fail: kmem_free(dh->dh_blkfree, dh->dh_blkfreesz); mutex_destroy(&dh->dh_lock); pool_cache_put(ufsdirhash_cache, dh); - ip->i_dirhash = NULL; atomic_add_int(&ufs_dirhashmem, -memreqd); return (-1); } @@ -280,6 +283,8 @@ ufsdirhash_free(struct inode *ip) if ((dh = ip->i_dirhash) == NULL) return; + ip->i_dirhash = NULL; + if (dh->dh_onlist) { DIRHASHLIST_LOCK(); if (dh->dh_onlist) @@ -300,7 +305,6 @@ ufsdirhash_free(struct inode *ip) } mutex_destroy(&dh->dh_lock); pool_cache_put(ufsdirhash_cache, dh); - ip->i_dirhash = NULL; atomic_add_int(&ufs_dirhashmem, -mem); } @@ -313,7 +317,7 @@ ufsdirhash_free(struct inode *ip) * If successful, the directory offset is stored in *offp, and a * pointer to a struct buf containing the entry is stored in *bpp. If * prevoffp is non-NULL, the offset of the previous entry within - * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry + * the UFS_DIRBLKSIZ-sized block is stored in *prevoffp (if the entry * is the first in a block, the start of the block is used). */ int @@ -413,7 +417,7 @@ restart: brelse(bp, 0); blkoff = offset & ~bmask; if (ufs_blkatoff(vp, (off_t)blkoff, - NULL, &bp, true) != 0) { + NULL, &bp, false) != 0) { DIRHASH_UNLOCK(dh); return (EJUSTRETURN); } @@ -427,7 +431,7 @@ restart: return (EJUSTRETURN); } if (dp->d_namlen == namelen && - bcmp(dp->d_name, name, namelen) == 0) { + memcmp(dp->d_name, name, namelen) == 0) { /* Found. Get the prev offset if needed. */ if (prevoffp != NULL) { if (offset & (dirblksiz - 1)) { @@ -445,7 +449,7 @@ restart: /* Check for sequential access, and update offset. */ if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset) dh->dh_seqopt = 1; - dh->dh_seqoff = offset + DIRSIZ(0, dp, needswap); + dh->dh_seqoff = offset + UFS_DIRSIZ(0, dp, needswap); DIRHASH_UNLOCK(dh); *bpp = bp; @@ -480,7 +484,7 @@ restart: * the offset of the directory entry that begins the free space. * This will either be the offset of an existing entry that has free * space at the end, or the offset of an entry with d_ino == 0 at - * the start of a DIRBLKSIZ block. + * the start of a UFS_DIRBLKSIZ block. * * To use the space, the caller may need to compact existing entries in * the directory. The total number of bytes in all of the entries involved @@ -537,7 +541,7 @@ ufsdirhash_findfree(struct inode *ip, in brelse(bp, 0); return (-1); } - if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp, needswap)) + if (dp->d_ino == 0 || dp->d_reclen > UFS_DIRSIZ(0, dp, needswap)) break; i += dp->d_reclen; dp = (struct direct *)((char *)dp + dp->d_reclen); @@ -554,7 +558,7 @@ ufsdirhash_findfree(struct inode *ip, in while (i < dirblksiz && freebytes < slotneeded) { freebytes += dp->d_reclen; if (dp->d_ino != 0) - freebytes -= DIRSIZ(0, dp, needswap); + freebytes -= UFS_DIRSIZ(0, dp, needswap); if (dp->d_reclen == 0) { DIRHASH_UNLOCK(dh); brelse(bp, 0); @@ -652,7 +656,7 @@ ufsdirhash_add(struct inode *ip, struct DH_ENTRY(dh, slot) = offset; /* Update the per-block summary info. */ - ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp, needswap), dirblksiz); + ufsdirhash_adjfree(dh, offset, -UFS_DIRSIZ(0, dirp, needswap), dirblksiz); DIRHASH_UNLOCK(dh); } @@ -687,7 +691,7 @@ ufsdirhash_remove(struct inode *ip, stru ufsdirhash_delslot(dh, slot); /* Update the per-block summary info. */ - ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp, needswap), dirblksiz); + ufsdirhash_adjfree(dh, offset, UFS_DIRSIZ(0, dirp, needswap), dirblksiz); DIRHASH_UNLOCK(dh); } @@ -721,7 +725,7 @@ ufsdirhash_move(struct inode *ip, struct /* * Inform dirhash that the directory has grown by one block that - * begins at offset (i.e. the new length is offset + DIRBLKSIZ). + * begins at offset (i.e. the new length is offset + UFS_DIRBLKSIZ). */ void ufsdirhash_newblk(struct inode *ip, doff_t offset) @@ -865,7 +869,7 @@ ufsdirhash_checkblock(struct inode *ip, /* Check that the entry exists (will panic if it doesn't). */ ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i); - nfree += dp->d_reclen - DIRSIZ(0, dp, needswap); + nfree += dp->d_reclen - UFS_DIRSIZ(0, dp, needswap); } if (i != dirblksiz) panic("ufsdirhash_checkblock: bad dir end"); @@ -997,7 +1001,7 @@ ufsdirhash_delslot(struct dirhash *dh, i /* * Given a directory entry and its offset, find the offset of the - * previous entry in the same DIRBLKSIZ-sized block. Returns an + * previous entry in the same UFS_DIRBLKSIZ-sized block. Returns an * offset, or -1 if there is no previous entry in the block or some * other problem occurred. */ @@ -1083,83 +1087,80 @@ ufsdirhash_recycle(int wanted) /* Account for the returned memory, and repeat if necessary. */ DIRHASHLIST_LOCK(); - ufs_dirhashmem -= mem; + atomic_add_int(&ufs_dirhashmem, -mem); } /* Success. */ return (0); } -void -ufsdirhash_init() -{ - - mutex_init(&ufsdirhash_lock, MUTEX_DEFAULT, IPL_NONE); - ufsdirhashblk_cache = pool_cache_init(DH_NBLKOFF * sizeof(daddr_t), 0, - 0, 0, "dirhashblk", NULL, IPL_NONE, NULL, NULL, NULL); - ufsdirhash_cache = pool_cache_init(sizeof(struct dirhash), 0, - 0, 0, "dirhash", NULL, IPL_NONE, NULL, NULL, NULL); - TAILQ_INIT(&ufsdirhash_list); -} - -void -ufsdirhash_done(void) -{ - - KASSERT(TAILQ_EMPTY(&ufsdirhash_list)); - pool_cache_destroy(ufsdirhashblk_cache); - pool_cache_destroy(ufsdirhash_cache); - mutex_destroy(&ufsdirhash_lock); -} - -SYSCTL_SETUP(sysctl_vfs_ufs_setup, "sysctl vfs.ufs.dirhash subtree setup") +static void +ufsdirhash_sysctl_init(void) { const struct sysctlnode *rnode, *cnode; - sysctl_createv(clog, 0, NULL, &rnode, - CTLFLAG_PERMANENT, - CTLTYPE_NODE, "vfs", NULL, - NULL, 0, NULL, 0, - CTL_VFS, CTL_EOL); - - sysctl_createv(clog, 0, &rnode, &rnode, + sysctl_createv(&ufsdirhash_sysctl_log, 0, NULL, &rnode, CTLFLAG_PERMANENT, CTLTYPE_NODE, "ufs", SYSCTL_DESCR("ufs"), NULL, 0, NULL, 0, - CTL_CREATE, CTL_EOL); + CTL_VFS, CTL_CREATE, CTL_EOL); - sysctl_createv(clog, 0, &rnode, &rnode, + sysctl_createv(&ufsdirhash_sysctl_log, 0, &rnode, &rnode, CTLFLAG_PERMANENT, CTLTYPE_NODE, "dirhash", SYSCTL_DESCR("dirhash"), NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL); - sysctl_createv(clog, 0, &rnode, &cnode, + sysctl_createv(&ufsdirhash_sysctl_log, 0, &rnode, &cnode, CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "minblocks", SYSCTL_DESCR("minimum hashed directory size in blocks"), NULL, 0, &ufs_dirhashminblks, 0, CTL_CREATE, CTL_EOL); - sysctl_createv(clog, 0, &rnode, &cnode, + sysctl_createv(&ufsdirhash_sysctl_log, 0, &rnode, &cnode, CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "maxmem", SYSCTL_DESCR("maximum dirhash memory usage"), NULL, 0, &ufs_dirhashmaxmem, 0, CTL_CREATE, CTL_EOL); - sysctl_createv(clog, 0, &rnode, &cnode, + sysctl_createv(&ufsdirhash_sysctl_log, 0, &rnode, &cnode, CTLFLAG_PERMANENT|CTLFLAG_READONLY, CTLTYPE_INT, "memused", SYSCTL_DESCR("current dirhash memory usage"), NULL, 0, &ufs_dirhashmem, 0, CTL_CREATE, CTL_EOL); - sysctl_createv(clog, 0, &rnode, &cnode, + sysctl_createv(&ufsdirhash_sysctl_log, 0, &rnode, &cnode, CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "docheck", SYSCTL_DESCR("enable extra sanity checks"), NULL, 0, &ufs_dirhashcheck, 0, CTL_CREATE, CTL_EOL); } + +void +ufsdirhash_init(void) +{ + + mutex_init(&ufsdirhash_lock, MUTEX_DEFAULT, IPL_NONE); + ufsdirhashblk_cache = pool_cache_init(DH_NBLKOFF * sizeof(daddr_t), 0, + 0, 0, "dirhashblk", NULL, IPL_NONE, NULL, NULL, NULL); + ufsdirhash_cache = pool_cache_init(sizeof(struct dirhash), 0, + 0, 0, "dirhash", NULL, IPL_NONE, NULL, NULL, NULL); + TAILQ_INIT(&ufsdirhash_list); + ufsdirhash_sysctl_init(); +} + +void +ufsdirhash_done(void) +{ + + KASSERT(TAILQ_EMPTY(&ufsdirhash_list)); + pool_cache_destroy(ufsdirhashblk_cache); + pool_cache_destroy(ufsdirhash_cache); + mutex_destroy(&ufsdirhash_lock); + sysctl_teardown(&ufsdirhash_sysctl_log); +}