Skip to content

Commit

Permalink
Convert vfs hash lock from a mutex to an rwlock.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjguzik committed Dec 30, 2014
1 parent e9ea040 commit 09f7a46
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions sys/kern/vfs_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,22 @@ __FBSDID("$FreeBSD$");
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/rwlock.h>
#include <sys/vnode.h>

static MALLOC_DEFINE(M_VFS_HASH, "vfs_hash", "VFS hash table");

static LIST_HEAD(vfs_hash_head, vnode) *vfs_hash_tbl;
static LIST_HEAD(,vnode) vfs_hash_side;
static u_long vfs_hash_mask;
static struct mtx vfs_hash_mtx;
static struct rwlock vfs_hash_lock;

static void
vfs_hashinit(void *dummy __unused)
{

vfs_hash_tbl = hashinit(desiredvnodes, M_VFS_HASH, &vfs_hash_mask);
mtx_init(&vfs_hash_mtx, "vfs hash", NULL, MTX_DEF);
rw_init(&vfs_hash_lock, "vfs hash");
LIST_INIT(&vfs_hash_side);
}

Expand Down Expand Up @@ -75,7 +76,7 @@ vfs_hash_get(const struct mount *mp, u_int hash, int flags, struct thread *td, s
int error;

while (1) {
mtx_lock(&vfs_hash_mtx);
rw_rlock(&vfs_hash_lock);
LIST_FOREACH(vp, vfs_hash_bucket(mp, hash), v_hashlist) {
if (vp->v_hash != hash)
continue;
Expand All @@ -84,7 +85,7 @@ vfs_hash_get(const struct mount *mp, u_int hash, int flags, struct thread *td, s
if (fn != NULL && fn(vp, arg))
continue;
VI_LOCK(vp);
mtx_unlock(&vfs_hash_mtx);
rw_runlock(&vfs_hash_lock);
error = vget(vp, flags | LK_INTERLOCK, td);
if (error == ENOENT && (flags & LK_NOWAIT) == 0)
break;
Expand All @@ -94,7 +95,7 @@ vfs_hash_get(const struct mount *mp, u_int hash, int flags, struct thread *td, s
return (0);
}
if (vp == NULL) {
mtx_unlock(&vfs_hash_mtx);
rw_runlock(&vfs_hash_lock);
*vpp = NULL;
return (0);
}
Expand All @@ -105,9 +106,9 @@ void
vfs_hash_remove(struct vnode *vp)
{

mtx_lock(&vfs_hash_mtx);
rw_wlock(&vfs_hash_lock);
LIST_REMOVE(vp, v_hashlist);
mtx_unlock(&vfs_hash_mtx);
rw_wunlock(&vfs_hash_lock);
}

int
Expand All @@ -118,7 +119,7 @@ vfs_hash_insert(struct vnode *vp, u_int hash, int flags, struct thread *td, stru

*vpp = NULL;
while (1) {
mtx_lock(&vfs_hash_mtx);
rw_wlock(&vfs_hash_lock);
LIST_FOREACH(vp2,
vfs_hash_bucket(vp->v_mount, hash), v_hashlist) {
if (vp2->v_hash != hash)
Expand All @@ -128,13 +129,13 @@ vfs_hash_insert(struct vnode *vp, u_int hash, int flags, struct thread *td, stru
if (fn != NULL && fn(vp2, arg))
continue;
VI_LOCK(vp2);
mtx_unlock(&vfs_hash_mtx);
rw_wunlock(&vfs_hash_lock);
error = vget(vp2, flags | LK_INTERLOCK, td);
if (error == ENOENT && (flags & LK_NOWAIT) == 0)
break;
mtx_lock(&vfs_hash_mtx);
rw_wlock(&vfs_hash_lock);
LIST_INSERT_HEAD(&vfs_hash_side, vp, v_hashlist);
mtx_unlock(&vfs_hash_mtx);
rw_wunlock(&vfs_hash_lock);
vput(vp);
if (!error)
*vpp = vp2;
Expand All @@ -146,17 +147,17 @@ vfs_hash_insert(struct vnode *vp, u_int hash, int flags, struct thread *td, stru
}
vp->v_hash = hash;
LIST_INSERT_HEAD(vfs_hash_bucket(vp->v_mount, hash), vp, v_hashlist);
mtx_unlock(&vfs_hash_mtx);
rw_wunlock(&vfs_hash_lock);
return (0);
}

void
vfs_hash_rehash(struct vnode *vp, u_int hash)
{

mtx_lock(&vfs_hash_mtx);
rw_wlock(&vfs_hash_lock);
LIST_REMOVE(vp, v_hashlist);
LIST_INSERT_HEAD(vfs_hash_bucket(vp->v_mount, hash), vp, v_hashlist);
vp->v_hash = hash;
mtx_unlock(&vfs_hash_mtx);
rw_wunlock(&vfs_hash_lock);
}

0 comments on commit 09f7a46

Please sign in to comment.