vfs_hash.c revision 143663
1/*- 2 * Copyright (c) 2005 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/kern/vfs_hash.c 143663 2005-03-15 20:00:03Z phk $"); 30 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/kernel.h> 34#include <sys/malloc.h> 35#include <sys/vnode.h> 36 37static MALLOC_DEFINE(M_VFS_HASH, "VFS hash", "VFS hash table"); 38 39static LIST_HEAD(vfs_hashhead, vnode) *vfs_hash_tbl; 40static u_long vfs_hash_mask; 41static struct mtx vfs_hash_mtx; 42 43static void 44vfs_hashinit(void *dummy __unused) 45{ 46 47 vfs_hash_tbl = hashinit(desiredvnodes, M_VFS_HASH, &vfs_hash_mask); 48 mtx_init(&vfs_hash_mtx, "vfs hash", NULL, MTX_DEF); 49} 50 51/* Must be SI_ORDER_SECOND so desiredvnodes is available */ 52SYSINIT(vfs_hash, SI_SUB_VFS, SI_ORDER_SECOND, vfs_hashinit, NULL) 53 54int 55vfs_hash_get(struct mount *mp, u_int hash, int flags, struct thread *td, struct vnode **vpp) 56{ 57 struct vnode *vp; 58 int error; 59 60 while (1) { 61 mtx_lock(&vfs_hash_mtx); 62 LIST_FOREACH(vp, 63 &vfs_hash_tbl[hash & vfs_hash_mask], v_hashlist) { 64 if (vp->v_hash != hash) 65 continue; 66 if (vp->v_mount != mp) 67 continue; 68 VI_LOCK(vp); 69 mtx_unlock(&vfs_hash_mtx); 70 error = vget(vp, flags | LK_INTERLOCK, td); 71 if (error == ENOENT) 72 break; 73 if (error) 74 return (error); 75 *vpp = vp; 76 return (0); 77 } 78 if (vp == NULL) { 79 mtx_unlock(&vfs_hash_mtx); 80 *vpp = NULL; 81 return (0); 82 } 83 } 84} 85 86void 87vfs_hash_remove(struct vnode *vp) 88{ 89 90 mtx_lock(&vfs_hash_mtx); 91 VI_LOCK(vp); 92 VNASSERT(vp->v_iflag & VI_HASHED, vp, ("vfs_hash_remove: not hashed")); 93 LIST_REMOVE(vp, v_hashlist); 94 vp->v_iflag &= ~VI_HASHED; 95 VI_UNLOCK(vp); 96 mtx_unlock(&vfs_hash_mtx); 97} 98 99int 100vfs_hash_insert(struct vnode *vp, u_int hash, int flags, struct thread *td, struct vnode **vpp) 101{ 102 struct vnode *vp2; 103 int error; 104 105 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL, td); 106 *vpp = NULL; 107 while (1) { 108 mtx_lock(&vfs_hash_mtx); 109 LIST_FOREACH(vp2, 110 &vfs_hash_tbl[hash & vfs_hash_mask], v_hashlist) { 111 if (vp2->v_hash != hash) 112 continue; 113 if (vp2->v_mount != vp->v_mount) 114 continue; 115 VI_LOCK(vp2); 116 mtx_unlock(&vfs_hash_mtx); 117 error = vget(vp2, flags | LK_INTERLOCK, td); 118 if (error == ENOENT) 119 break; 120 vput(vp); 121 if (!error) 122 *vpp = vp2; 123 return (error); 124 } 125 if (vp2 == NULL) 126 break; 127 128 } 129 VI_LOCK(vp); 130 VNASSERT(!(vp->v_iflag & VI_HASHED), vp, 131 ("vfs_hash_insert: already hashed")); 132 vp->v_hash = hash; 133 LIST_INSERT_HEAD(&vfs_hash_tbl[hash & vfs_hash_mask], vp, v_hashlist); 134 vp->v_iflag |= VI_HASHED; 135 VI_UNLOCK(vp); 136 mtx_unlock(&vfs_hash_mtx); 137 return (0); 138} 139 140void 141vfs_hash_rehash(struct vnode *vp, u_int hash) 142{ 143 144 mtx_lock(&vfs_hash_mtx); 145 VNASSERT(vp->v_iflag & VI_HASHED, vp, ("vfs_hash_rehash: not hashed")); 146 LIST_REMOVE(vp, v_hashlist); 147 LIST_INSERT_HEAD(&vfs_hash_tbl[hash & vfs_hash_mask], vp, v_hashlist); 148 vp->v_hash = hash; 149 mtx_unlock(&vfs_hash_mtx); 150} 151