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$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/malloc.h> 35 #include <sys/mount.h> 36 #include <sys/vnode.h> 37 38 static MALLOC_DEFINE(M_VFS_HASH, "vfs_hash", "VFS hash table"); 39 40 static LIST_HEAD(vfs_hash_head, vnode) *vfs_hash_tbl; 41 static LIST_HEAD(,vnode) vfs_hash_side; 42 static u_long vfs_hash_mask; 43 static struct mtx vfs_hash_mtx; 44 45 static void 46 vfs_hashinit(void *dummy __unused) 47 { 48 49 vfs_hash_tbl = hashinit(desiredvnodes, M_VFS_HASH, &vfs_hash_mask); 50 mtx_init(&vfs_hash_mtx, "vfs hash", NULL, MTX_DEF); 51 LIST_INIT(&vfs_hash_side); 52 } 53 54 /* Must be SI_ORDER_SECOND so desiredvnodes is available */ 55 SYSINIT(vfs_hash, SI_SUB_VFS, SI_ORDER_SECOND, vfs_hashinit, NULL); 56 57 static struct vfs_hash_head * 58 vfs_hash_index(const struct mount *mp, u_int hash) 59 { 60 61 return(&vfs_hash_tbl[(hash + mp->mnt_hashseed) & vfs_hash_mask]); 62 } 63 64 int 65 vfs_hash_get(const struct mount *mp, u_int hash, int flags, struct thread *td, struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg) 66 { 67 struct vnode *vp; 68 int error; 69 70 while (1) { 71 mtx_lock(&vfs_hash_mtx); 72 LIST_FOREACH(vp, vfs_hash_index(mp, hash), v_hashlist) { 73 if (vp->v_hash != hash) 74 continue; 75 if (vp->v_mount != mp) 76 continue; 77 if (fn != NULL && fn(vp, arg)) 78 continue; 79 VI_LOCK(vp); 80 mtx_unlock(&vfs_hash_mtx); 81 error = vget(vp, flags | LK_INTERLOCK, td); 82 if (error == ENOENT && (flags & LK_NOWAIT) == 0) 83 break; 84 if (error) 85 return (error); 86 *vpp = vp; 87 return (0); 88 } 89 if (vp == NULL) { 90 mtx_unlock(&vfs_hash_mtx); 91 *vpp = NULL; 92 return (0); 93 } 94 } 95 } 96 97 void 98 vfs_hash_remove(struct vnode *vp) 99 { 100 101 mtx_lock(&vfs_hash_mtx); 102 LIST_REMOVE(vp, v_hashlist); 103 mtx_unlock(&vfs_hash_mtx); 104 } 105 106 int 107 vfs_hash_insert(struct vnode *vp, u_int hash, int flags, struct thread *td, struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg) 108 { 109 struct vnode *vp2; 110 int error; 111 112 *vpp = NULL; 113 while (1) { 114 mtx_lock(&vfs_hash_mtx); 115 LIST_FOREACH(vp2, 116 vfs_hash_index(vp->v_mount, hash), v_hashlist) { 117 if (vp2->v_hash != hash) 118 continue; 119 if (vp2->v_mount != vp->v_mount) 120 continue; 121 if (fn != NULL && fn(vp2, arg)) 122 continue; 123 VI_LOCK(vp2); 124 mtx_unlock(&vfs_hash_mtx); 125 error = vget(vp2, flags | LK_INTERLOCK, td); 126 if (error == ENOENT && (flags & LK_NOWAIT) == 0) 127 break; 128 mtx_lock(&vfs_hash_mtx); 129 LIST_INSERT_HEAD(&vfs_hash_side, vp, v_hashlist); 130 mtx_unlock(&vfs_hash_mtx); 131 vput(vp); 132 if (!error) 133 *vpp = vp2; 134 return (error); 135 } 136 if (vp2 == NULL) 137 break; 138 139 } 140 vp->v_hash = hash; 141 LIST_INSERT_HEAD(vfs_hash_index(vp->v_mount, hash), vp, v_hashlist); 142 mtx_unlock(&vfs_hash_mtx); 143 return (0); 144 } 145 146 void 147 vfs_hash_rehash(struct vnode *vp, u_int hash) 148 { 149 150 mtx_lock(&vfs_hash_mtx); 151 LIST_REMOVE(vp, v_hashlist); 152 LIST_INSERT_HEAD(vfs_hash_index(vp->v_mount, hash), vp, v_hashlist); 153 vp->v_hash = hash; 154 mtx_unlock(&vfs_hash_mtx); 155 } 156