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.12.2.1 retrieving revision 1.21 diff -u -p -r1.12.2.1 -r1.21 --- src/sys/ufs/ufs/ufs_dirhash.c 2007/03/12 06:01:10 1.12.2.1 +++ src/sys/ufs/ufs/ufs_dirhash.c 2008/01/03 19:28:50 1.21 @@ -1,4 +1,4 @@ -/* $NetBSD: ufs_dirhash.c,v 1.12.2.1 2007/03/12 06:01:10 rmind Exp $ */ +/* $NetBSD: ufs_dirhash.c,v 1.21 2008/01/03 19:28:50 ad Exp $ */ /* * Copyright (c) 2001, 2002 Ian Dowse. All rights reserved. @@ -27,6 +27,9 @@ * $FreeBSD: src/sys/ufs/ufs/ufs_dirhash.c,v 1.3.2.8 2004/12/08 11:54:13 dwmalone Exp $ */ +#include +__KERNEL_RCSID(0, "$NetBSD: ufs_dirhash.c,v 1.21 2008/01/03 19:28:50 ad Exp $"); + /* * This implements a hash-based lookup scheme for UFS directories. */ @@ -44,7 +47,6 @@ #include #include -#include #include #include #include @@ -57,7 +59,7 @@ #define OFSFMT(ip) ((ip)->i_ump->um_maxsymlinklen <= 0) #define BLKFREE2IDX(n) ((n) > DH_NFSTATS ? DH_NFSTATS : (n)) -static MALLOC_DEFINE(M_DIRHASH, "UFS dirhash", "UFS directory hash tables"); +static MALLOC_JUSTDEFINE(M_DIRHASH, "UFS dirhash", "UFS directory hash tables"); static int ufs_dirhashminblks = 5; static int ufs_dirhashmaxmem = 2 * 1024 * 1024; @@ -74,19 +76,33 @@ static doff_t ufsdirhash_getprev(struct int dirblksiz); static int ufsdirhash_recycle(int wanted); -static POOL_INIT(ufsdirhash_pool, DH_NBLKOFF * sizeof(daddr_t), 0, 0, 0, - "ufsdirhash", &pool_allocator_nointr); +static pool_cache_t ufsdirhash_cache; -#define DIRHASHLIST_LOCK() do { } while (0) -#define DIRHASHLIST_UNLOCK() do { } while (0) -#define DIRHASH_LOCK(dh) do { } while (0) -#define DIRHASH_UNLOCK(dh) do { } while (0) -#define DIRHASH_BLKALLOC_WAITOK() pool_get(&ufsdirhash_pool, PR_WAITOK) -#define DIRHASH_BLKFREE(ptr) pool_put(&ufsdirhash_pool, ptr) +#define DIRHASHLIST_LOCK() mutex_enter(&ufsdirhash_lock) +#define DIRHASHLIST_UNLOCK() mutex_exit(&ufsdirhash_lock) +#define DIRHASH_LOCK(dh) mutex_enter(&(dh)->dh_lock) +#define DIRHASH_UNLOCK(dh) mutex_exit(&(dh)->dh_lock) +#define DIRHASH_BLKALLOC_WAITOK() \ + pool_cache_get(ufsdirhash_cache, PR_WAITOK) +#define DIRHASH_BLKFREE(ptr) \ + pool_cache_put(ufsdirhash_cache, ptr) /* Dirhash list; recently-used entries are near the tail. */ static TAILQ_HEAD(, dirhash) ufsdirhash_list; +/* Protects: ufsdirhash_list, `dh_list' field, ufs_dirhashmem. */ +static kmutex_t ufsdirhash_lock; + +/* + * Locking order: + * ufsdirhash_lock + * dh_lock + * + * The dh_lock mutex should be acquired either via the inode lock, or via + * ufsdirhash_lock. Only the owner of the inode may free the associated + * dirhash, but anything can steal its memory and set dh_hash to NULL. + */ + /* * Attempt to build up a hash table for the directory contents in * inode 'ip'. Returns 0 on success, or -1 of the operation failed. @@ -163,6 +179,7 @@ ufsdirhash_build(struct inode *ip) DIRHASHLIST_UNLOCK(); return (-1); } + mutex_init(&dh->dh_lock, MUTEX_DEFAULT, IPL_NONE); dh->dh_hash = (doff_t **)malloc(narrays * sizeof(dh->dh_hash[0]), M_DIRHASH, M_NOWAIT | M_ZERO); dh->dh_blkfree = (u_int8_t *)malloc(nblocks * sizeof(dh->dh_blkfree[0]), @@ -201,7 +218,7 @@ ufsdirhash_build(struct inode *ip) /* If necessary, get the next directory block. */ if ((pos & bmask) == 0) { if (bp != NULL) - brelse(bp); + brelse(bp, 0); if (ufs_blkatoff(vp, (off_t)pos, NULL, &bp) != 0) goto fail; } @@ -211,7 +228,7 @@ ufsdirhash_build(struct inode *ip) if (ep->d_reclen == 0 || ep->d_reclen > dirblksiz - (pos & (dirblksiz - 1))) { /* Corrupted directory. */ - brelse(bp); + brelse(bp, 0); goto fail; } if (ep->d_ino != 0) { @@ -228,7 +245,7 @@ ufsdirhash_build(struct inode *ip) } if (bp != NULL) - brelse(bp); + brelse(bp, 0); DIRHASHLIST_LOCK(); TAILQ_INSERT_TAIL(&ufsdirhash_list, dh, dh_list); dh->dh_onlist = 1; @@ -244,6 +261,7 @@ fail: } if (dh->dh_blkfree != NULL) FREE(dh->dh_blkfree, M_DIRHASH); + mutex_destroy(&dh->dh_lock); FREE(dh, M_DIRHASH); ip->i_dirhash = NULL; DIRHASHLIST_LOCK(); @@ -282,6 +300,7 @@ ufsdirhash_free(struct inode *ip) dh->dh_narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + dh->dh_nblk * sizeof(*dh->dh_blkfree); } + mutex_destroy(&dh->dh_lock); FREE(dh, M_DIRHASH); ip->i_dirhash = NULL; @@ -318,11 +337,11 @@ ufsdirhash_lookup(struct inode *ip, cons return (EJUSTRETURN); /* * Move this dirhash towards the end of the list if it has a - * score higher than the next entry, and acquire the dh_mtx. + * score higher than the next entry, and acquire the dh_lock. * Optimise the case where it's already the last by performing * an unlocked read of the TAILQ_NEXT pointer. * - * In both cases, end up holding just dh_mtx. + * In both cases, end up holding just dh_lock. */ if (TAILQ_NEXT(dh, dh_list) != NULL) { DIRHASHLIST_LOCK(); @@ -395,7 +414,7 @@ restart: panic("ufsdirhash_lookup: bad offset in hash array"); if ((offset & ~bmask) != blkoff) { if (bp != NULL) - brelse(bp); + brelse(bp, 0); blkoff = offset & ~bmask; if (ufs_blkatoff(vp, (off_t)blkoff, NULL, &bp) != 0) return (EJUSTRETURN); @@ -404,7 +423,7 @@ restart: if (dp->d_reclen == 0 || dp->d_reclen > dirblksiz - (offset & (dirblksiz - 1))) { /* Corrupted directory. */ - brelse(bp); + brelse(bp, 0); return (EJUSTRETURN); } if (dp->d_namlen == namelen && @@ -415,7 +434,7 @@ restart: prevoff = ufsdirhash_getprev(dp, offset, dirblksiz); if (prevoff == -1) { - brelse(bp); + brelse(bp, 0); return (EJUSTRETURN); } } else @@ -437,7 +456,7 @@ restart: if (dh->dh_hash == NULL) { DIRHASH_UNLOCK(dh); if (bp != NULL) - brelse(bp); + brelse(bp, 0); ufsdirhash_free(ip); return (EJUSTRETURN); } @@ -452,7 +471,7 @@ restart: } DIRHASH_UNLOCK(dh); if (bp != NULL) - brelse(bp); + brelse(bp, 0); return (ENOENT); } @@ -512,7 +531,7 @@ ufsdirhash_findfree(struct inode *ip, in /* Find the first entry with free space. */ for (i = 0; i < dirblksiz; ) { if (dp->d_reclen == 0) { - brelse(bp); + brelse(bp, 0); return (-1); } if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp, needswap)) @@ -521,7 +540,7 @@ ufsdirhash_findfree(struct inode *ip, in dp = (struct direct *)((char *)dp + dp->d_reclen); } if (i > dirblksiz) { - brelse(bp); + brelse(bp, 0); return (-1); } slotstart = pos + i; @@ -533,19 +552,19 @@ ufsdirhash_findfree(struct inode *ip, in if (dp->d_ino != 0) freebytes -= DIRSIZ(0, dp, needswap); if (dp->d_reclen == 0) { - brelse(bp); + brelse(bp, 0); return (-1); } i += dp->d_reclen; dp = (struct direct *)((char *)dp + dp->d_reclen); } if (i > dirblksiz) { - brelse(bp); + brelse(bp, 0); return (-1); } if (freebytes < slotneeded) panic("ufsdirhash_findfree: free mismatch"); - brelse(bp); + brelse(bp, 0); *slotsize = pos + i - slotstart; return (slotstart); } @@ -875,7 +894,7 @@ ufsdirhash_hash(struct dirhash *dh, cons * by the value specified by `diff'. * * The caller must ensure we have exclusive access to `dh'; normally - * that means that dh_mtx should be held, but this is also called + * that means that dh_lock should be held, but this is also called * from ufsdirhash_build() where exclusive access can be assumed. */ static void @@ -919,6 +938,8 @@ ufsdirhash_findslot(struct dirhash *dh, { int slot; + KASSERT(mutex_owned(&dh->dh_lock)); + /* Find the entry. */ KASSERT(dh->dh_hused < dh->dh_hlen); slot = ufsdirhash_hash(dh, name, namelen); @@ -941,6 +962,8 @@ ufsdirhash_delslot(struct dirhash *dh, i { int i; + KASSERT(mutex_owned(&dh->dh_lock)); + /* Mark the entry as deleted. */ DH_ENTRY(dh, slot) = DIRHASH_DEL; @@ -1051,20 +1074,22 @@ ufsdirhash_recycle(int wanted) void ufsdirhash_init() { -#ifdef _LKM - pool_init(&ufsdirhash_pool, DH_NBLKOFF * sizeof(daddr_t), 0, 0, 0, - "ufsdirhash", &pool_allocator_nointr); -#endif + + mutex_init(&ufsdirhash_lock, MUTEX_DEFAULT, IPL_NONE); + malloc_type_attach(M_DIRHASH); + ufsdirhash_cache = pool_cache_init(DH_NBLKOFF * sizeof(daddr_t), 0, + 0, 0, "ufsdirhash", NULL, IPL_NONE, NULL, NULL, NULL); TAILQ_INIT(&ufsdirhash_list); } void ufsdirhash_done(void) { + KASSERT(TAILQ_EMPTY(&ufsdirhash_list)); -#ifdef _LKM - pool_destroy(&ufsdirhash_pool); -#endif + pool_cache_destroy(ufsdirhash_cache); + malloc_type_detach(M_DIRHASH); + mutex_destroy(&ufsdirhash_lock); } SYSCTL_SETUP(sysctl_vfs_ufs_setup, "sysctl vfs.ufs.dirhash subtree setup")