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.1.4.1 retrieving revision 1.24 diff -u -p -r1.1.4.1 -r1.24 --- src/sys/ufs/ufs/ufs_dirhash.c 2005/03/19 08:37:06 1.1.4.1 +++ src/sys/ufs/ufs/ufs_dirhash.c 2008/06/15 21:18:06 1.24 @@ -1,4 +1,4 @@ -/* $NetBSD: ufs_dirhash.c,v 1.1.4.1 2005/03/19 08:37:06 yamt Exp $ */ +/* $NetBSD: ufs_dirhash.c,v 1.24 2008/06/15 21:18:06 skd Exp $ */ /* * Copyright (c) 2001, 2002 Ian Dowse. All rights reserved. @@ -27,16 +27,17 @@ * $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.24 2008/06/15 21:18:06 skd Exp $"); + /* * This implements a hash-based lookup scheme for UFS directories. */ -#ifdef UFS_DIRHASH - #include #include #include -#include +#include #include #include #include @@ -45,8 +46,8 @@ #include #include #include +#include -#include #include #include #include @@ -59,12 +60,10 @@ #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 int ufs_dirhashminblks = 5; -static int ufs_dirhashmaxmem = 2 * 1024 * 1024; -static int ufs_dirhashmem; -static int ufs_dirhashcheck = 0; +static u_int ufs_dirhashminblks = 5; +static u_int ufs_dirhashmaxmem = 2 * 1024 * 1024; +static u_int ufs_dirhashmem; +static u_int ufs_dirhashcheck = 0; static int ufsdirhash_hash(struct dirhash *dh, const char *name, int namelen); static void ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff, @@ -76,19 +75,34 @@ static doff_t ufsdirhash_getprev(struct int dirblksiz); static int ufsdirhash_recycle(int wanted); -POOL_INIT(ufsdirhash_pool, DH_NBLKOFF * sizeof(daddr_t), 0, 0, 0, "ufsdirhash", - &pool_allocator_nointr); +static pool_cache_t ufsdirhashblk_cache; +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() \ + pool_cache_get(ufsdirhashblk_cache, PR_NOWAIT) +#define DIRHASH_BLKFREE(ptr) \ + pool_cache_put(ufsdirhashblk_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. @@ -140,39 +154,38 @@ ufsdirhash_build(struct inode *ip) memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) + narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + nblocks * sizeof(*dh->dh_blkfree); - DIRHASHLIST_LOCK(); - if (memreqd + ufs_dirhashmem > ufs_dirhashmaxmem) { - DIRHASHLIST_UNLOCK(); + + while (atomic_add_int_nv(&ufs_dirhashmem, memreqd) > + ufs_dirhashmaxmem) { + atomic_add_int(&ufs_dirhashmem, -memreqd); if (memreqd > ufs_dirhashmaxmem / 2) return (-1); - /* Try to free some space. */ if (ufsdirhash_recycle(memreqd) != 0) return (-1); - /* Enough was freed, and list has been locked. */ + else + DIRHASHLIST_UNLOCK(); } - ufs_dirhashmem += memreqd; - DIRHASHLIST_UNLOCK(); /* * Use non-blocking mallocs so that we will revert to a linear * lookup on failure rather than potentially blocking forever. */ - MALLOC(dh, struct dirhash *, sizeof *dh, M_DIRHASH, M_NOWAIT | M_ZERO); + dh = pool_cache_get(ufsdirhash_cache, PR_NOWAIT); if (dh == NULL) { - DIRHASHLIST_LOCK(); - ufs_dirhashmem -= memreqd; - DIRHASHLIST_UNLOCK(); + atomic_add_int(&ufs_dirhashmem, -memreqd); return (-1); } - MALLOC(dh->dh_hash, doff_t **, narrays * sizeof(dh->dh_hash[0]), - M_DIRHASH, M_NOWAIT | M_ZERO); - MALLOC(dh->dh_blkfree, u_int8_t *, nblocks * sizeof(dh->dh_blkfree[0]), - M_DIRHASH, M_NOWAIT); + memset(dh, 0, sizeof(*dh)); + mutex_init(&dh->dh_lock, MUTEX_DEFAULT, IPL_NONE); + dh->dh_hashsz = narrays * sizeof(dh->dh_hash[0]); + dh->dh_hash = kmem_zalloc(dh->dh_hashsz, KM_NOSLEEP); + dh->dh_blkfreesz = nblocks * sizeof(dh->dh_blkfree[0]); + dh->dh_blkfree = kmem_zalloc(dh->dh_blkfreesz, KM_NOSLEEP); if (dh->dh_hash == NULL || dh->dh_blkfree == NULL) goto fail; for (i = 0; i < narrays; i++) { - if ((dh->dh_hash[i] = DIRHASH_BLKALLOC_WAITOK()) == NULL) + if ((dh->dh_hash[i] = DIRHASH_BLKALLOC()) == NULL) goto fail; for (j = 0; j < DH_NBLKOFF; j++) dh->dh_hash[i][j] = DIRHASH_EMPTY; @@ -196,11 +209,15 @@ ufsdirhash_build(struct inode *ip) bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; pos = 0; while (pos < ip->i_size) { + if ((curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD) + != 0) { + preempt(); + } /* If necessary, get the next directory block. */ if ((pos & bmask) == 0) { if (bp != NULL) - brelse(bp); - if (VOP_BLKATOFF(vp, (off_t)pos, NULL, &bp) != 0) + brelse(bp, 0); + if (ufs_blkatoff(vp, (off_t)pos, NULL, &bp, false) != 0) goto fail; } @@ -209,7 +226,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) { @@ -226,7 +243,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; @@ -238,15 +255,14 @@ fail: for (i = 0; i < narrays; i++) if (dh->dh_hash[i] != NULL) DIRHASH_BLKFREE(dh->dh_hash[i]); - FREE(dh->dh_hash, M_DIRHASH); + kmem_free(dh->dh_hash, dh->dh_hashsz); } if (dh->dh_blkfree != NULL) - FREE(dh->dh_blkfree, M_DIRHASH); - FREE(dh, M_DIRHASH); + kmem_free(dh->dh_blkfree, dh->dh_blkfreesz); + mutex_destroy(&dh->dh_lock); + pool_cache_put(ufsdirhash_cache, dh); ip->i_dirhash = NULL; - DIRHASHLIST_LOCK(); - ufs_dirhashmem -= memreqd; - DIRHASHLIST_UNLOCK(); + atomic_add_int(&ufs_dirhashmem, -memreqd); return (-1); } @@ -261,31 +277,30 @@ ufsdirhash_free(struct inode *ip) if ((dh = ip->i_dirhash) == NULL) return; - DIRHASHLIST_LOCK(); - DIRHASH_LOCK(dh); - if (dh->dh_onlist) - TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); - DIRHASH_UNLOCK(dh); - DIRHASHLIST_UNLOCK(); - /* The dirhash pointed to by 'dh' is exclusively ours now. */ + if (dh->dh_onlist) { + DIRHASHLIST_LOCK(); + if (dh->dh_onlist) + TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); + DIRHASHLIST_UNLOCK(); + } + /* The dirhash pointed to by 'dh' is exclusively ours now. */ mem = sizeof(*dh); if (dh->dh_hash != NULL) { for (i = 0; i < dh->dh_narrays; i++) DIRHASH_BLKFREE(dh->dh_hash[i]); - FREE(dh->dh_hash, M_DIRHASH); - FREE(dh->dh_blkfree, M_DIRHASH); - mem += dh->dh_narrays * sizeof(*dh->dh_hash) + - dh->dh_narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) + - dh->dh_nblk * sizeof(*dh->dh_blkfree); + kmem_free(dh->dh_hash, dh->dh_hashsz); + kmem_free(dh->dh_blkfree, dh->dh_blkfreesz); + mem += dh->dh_hashsz; + mem += dh->dh_narrays * DH_NBLKOFF * sizeof(**dh->dh_hash); + mem += dh->dh_nblk * sizeof(*dh->dh_blkfree); } - FREE(dh, M_DIRHASH); + mutex_destroy(&dh->dh_lock); + pool_cache_put(ufsdirhash_cache, dh); ip->i_dirhash = NULL; - DIRHASHLIST_LOCK(); - ufs_dirhashmem -= mem; - DIRHASHLIST_UNLOCK(); + atomic_add_int(&ufs_dirhashmem, -mem); } /* @@ -314,13 +329,14 @@ ufsdirhash_lookup(struct inode *ip, cons if ((dh = ip->i_dirhash) == NULL) 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(); @@ -387,22 +403,25 @@ restart: slot = WRAPINCR(slot, dh->dh_hlen)) { if (offset == DIRHASH_DEL) continue; - DIRHASH_UNLOCK(dh); if (offset < 0 || offset >= ip->i_size) panic("ufsdirhash_lookup: bad offset in hash array"); if ((offset & ~bmask) != blkoff) { if (bp != NULL) - brelse(bp); + brelse(bp, 0); blkoff = offset & ~bmask; - if (VOP_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) + if (ufs_blkatoff(vp, (off_t)blkoff, + NULL, &bp, true) != 0) { + DIRHASH_UNLOCK(dh); return (EJUSTRETURN); + } } - dp = (struct direct *)(bp->b_data + (offset & bmask)); + dp = (struct direct *)((char *)bp->b_data + (offset & bmask)); if (dp->d_reclen == 0 || dp->d_reclen > dirblksiz - (offset & (dirblksiz - 1))) { /* Corrupted directory. */ - brelse(bp); + DIRHASH_UNLOCK(dh); + brelse(bp, 0); return (EJUSTRETURN); } if (dp->d_namlen == namelen && @@ -413,7 +432,7 @@ restart: prevoff = ufsdirhash_getprev(dp, offset, dirblksiz); if (prevoff == -1) { - brelse(bp); + brelse(bp, 0); return (EJUSTRETURN); } } else @@ -425,17 +444,17 @@ restart: if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset) dh->dh_seqopt = 1; dh->dh_seqoff = offset + DIRSIZ(0, dp, needswap); + DIRHASH_UNLOCK(dh); *bpp = bp; *offp = offset; return (0); } - DIRHASH_LOCK(dh); if (dh->dh_hash == NULL) { DIRHASH_UNLOCK(dh); if (bp != NULL) - brelse(bp); + brelse(bp, 0); ufsdirhash_free(ip); return (EJUSTRETURN); } @@ -450,7 +469,7 @@ restart: } DIRHASH_UNLOCK(dh); if (bp != NULL) - brelse(bp); + brelse(bp, 0); return (ENOENT); } @@ -483,6 +502,7 @@ ufsdirhash_findfree(struct inode *ip, in if ((dh = ip->i_dirhash) == NULL) return (-1); + DIRHASH_LOCK(dh); if (dh->dh_hash == NULL) { DIRHASH_UNLOCK(dh); @@ -502,15 +522,17 @@ ufsdirhash_findfree(struct inode *ip, in KASSERT(dirblock < dh->dh_nblk && dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN)); - DIRHASH_UNLOCK(dh); pos = dirblock * dirblksiz; - error = VOP_BLKATOFF(ip->i_vnode, (off_t)pos, (void *)&dp, &bp); - if (error) + error = ufs_blkatoff(ip->i_vnode, (off_t)pos, (void *)&dp, &bp, false); + if (error) { + DIRHASH_UNLOCK(dh); return (-1); + } /* Find the first entry with free space. */ for (i = 0; i < dirblksiz; ) { if (dp->d_reclen == 0) { - brelse(bp); + DIRHASH_UNLOCK(dh); + brelse(bp, 0); return (-1); } if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp, needswap)) @@ -519,7 +541,8 @@ ufsdirhash_findfree(struct inode *ip, in dp = (struct direct *)((char *)dp + dp->d_reclen); } if (i > dirblksiz) { - brelse(bp); + DIRHASH_UNLOCK(dh); + brelse(bp, 0); return (-1); } slotstart = pos + i; @@ -531,19 +554,22 @@ ufsdirhash_findfree(struct inode *ip, in if (dp->d_ino != 0) freebytes -= DIRSIZ(0, dp, needswap); if (dp->d_reclen == 0) { - brelse(bp); + DIRHASH_UNLOCK(dh); + brelse(bp, 0); return (-1); } i += dp->d_reclen; dp = (struct direct *)((char *)dp + dp->d_reclen); } if (i > dirblksiz) { - brelse(bp); + DIRHASH_UNLOCK(dh); + brelse(bp, 0); return (-1); } if (freebytes < slotneeded) panic("ufsdirhash_findfree: free mismatch"); - brelse(bp); + DIRHASH_UNLOCK(dh); + brelse(bp, 0); *slotsize = pos + i - slotstart; return (slotstart); } @@ -561,6 +587,7 @@ ufsdirhash_enduseful(struct inode *ip) if ((dh = ip->i_dirhash) == NULL) return (-1); + DIRHASH_LOCK(dh); if (dh->dh_hash == NULL) { DIRHASH_UNLOCK(dh); @@ -595,6 +622,7 @@ ufsdirhash_add(struct inode *ip, struct if ((dh = ip->i_dirhash) == NULL) return; + DIRHASH_LOCK(dh); if (dh->dh_hash == NULL) { DIRHASH_UNLOCK(dh); @@ -641,6 +669,7 @@ ufsdirhash_remove(struct inode *ip, stru if ((dh = ip->i_dirhash) == NULL) return; + DIRHASH_LOCK(dh); if (dh->dh_hash == NULL) { DIRHASH_UNLOCK(dh); @@ -737,6 +766,7 @@ ufsdirhash_dirtrunc(struct inode *ip, do if ((dh = ip->i_dirhash) == NULL) return; + DIRHASH_LOCK(dh); if (dh->dh_hash == NULL) { DIRHASH_UNLOCK(dh); @@ -780,12 +810,12 @@ ufsdirhash_dirtrunc(struct inode *ip, do * a directory block matches its actual contents. Panics if a mismatch * is detected. * - * On entry, `buf' should point to the start of an in-core + * On entry, `sbuf' should point to the start of an in-core * DIRBLKSIZ-sized directory block, and `offset' should contain the * offset from the start of the directory of that block. */ void -ufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset) +ufsdirhash_checkblock(struct inode *ip, char *sbuf, doff_t offset) { struct dirhash *dh; struct direct *dp; @@ -797,6 +827,7 @@ ufsdirhash_checkblock(struct inode *ip, return; if ((dh = ip->i_dirhash) == NULL) return; + DIRHASH_LOCK(dh); if (dh->dh_hash == NULL) { DIRHASH_UNLOCK(dh); @@ -810,7 +841,7 @@ ufsdirhash_checkblock(struct inode *ip, nfree = 0; for (i = 0; i < dirblksiz; i += dp->d_reclen) { - dp = (struct direct *)(buf + i); + dp = (struct direct *)(sbuf + i); if (dp->d_reclen == 0 || i + dp->d_reclen > dirblksiz) panic("ufsdirhash_checkblock: bad dir"); @@ -873,7 +904,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 @@ -881,6 +912,8 @@ ufsdirhash_adjfree(struct dirhash *dh, d { int block, i, nfidx, ofidx; + KASSERT(mutex_owned(&dh->dh_lock)); + /* Update the per-block summary info. */ block = offset / dirblksiz; KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks); @@ -917,6 +950,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); @@ -939,6 +974,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; @@ -1000,6 +1037,7 @@ ufsdirhash_recycle(int wanted) doff_t **hash; u_int8_t *blkfree; int i, mem, narrays; + size_t hashsz, blkfreesz; DIRHASHLIST_LOCK(); while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) { @@ -1022,8 +1060,10 @@ ufsdirhash_recycle(int wanted) TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); dh->dh_onlist = 0; hash = dh->dh_hash; + hashsz = dh->dh_hashsz; dh->dh_hash = NULL; blkfree = dh->dh_blkfree; + blkfreesz = dh->dh_blkfreesz; dh->dh_blkfree = NULL; narrays = dh->dh_narrays; mem = narrays * sizeof(*dh->dh_hash) + @@ -1033,41 +1073,45 @@ ufsdirhash_recycle(int wanted) /* Unlock everything, free the detached memory. */ DIRHASH_UNLOCK(dh); DIRHASHLIST_UNLOCK(); + for (i = 0; i < narrays; i++) DIRHASH_BLKFREE(hash[i]); - FREE(hash, M_DIRHASH); - FREE(blkfree, M_DIRHASH); + kmem_free(hash, hashsz); + kmem_free(blkfree, blkfreesz); /* Account for the returned memory, and repeat if necessary. */ DIRHASHLIST_LOCK(); ufs_dirhashmem -= mem; } - /* Success; return with list locked. */ + /* Success. */ return (0); } 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); + 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() +ufsdirhash_done(void) { + KASSERT(TAILQ_EMPTY(&ufsdirhash_list)); -#ifdef _LKM - pool_destroy(&ufsdirhash_pool); -#endif + 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") { - struct sysctlnode *rnode, *cnode; + const struct sysctlnode *rnode, *cnode; sysctl_createv(clog, 0, NULL, &rnode, CTLFLAG_PERMANENT, @@ -1117,5 +1161,3 @@ SYSCTL_SETUP(sysctl_vfs_ufs_setup, "sysc NULL, 0, &ufs_dirhashcheck, 0, CTL_CREATE, CTL_EOL); } - -#endif /* UFS_DIRHASH */