vfs_hash.c revision 245406
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 245406 2013-01-14 05:41:40Z kib $"); 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 38static MALLOC_DEFINE(M_VFS_HASH, "vfs_hash", "VFS hash table"); 39 40static LIST_HEAD(vfs_hash_head, vnode) *vfs_hash_tbl; 41static LIST_HEAD(,vnode) vfs_hash_side; 42static u_long vfs_hash_mask; 43static struct mtx vfs_hash_mtx; 44 45static void 46vfs_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 */ 55SYSINIT(vfs_hash, SI_SUB_VFS, SI_ORDER_SECOND, vfs_hashinit, NULL); 56 57u_int 58vfs_hash_index(struct vnode *vp) 59{ 60 61 return (vp->v_hash + vp->v_mount->mnt_hashseed); 62} 63 64static struct vfs_hash_head * 65vfs_hash_bucket(const struct mount *mp, u_int hash) 66{ 67 68 return (&vfs_hash_tbl[(hash + mp->mnt_hashseed) & vfs_hash_mask]); 69} 70 71int 72vfs_hash_get(const struct mount *mp, u_int hash, int flags, struct thread *td, struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg) 73{ 74 struct vnode *vp; 75 int error; 76 77 while (1) { 78 mtx_lock(&vfs_hash_mtx); 79 LIST_FOREACH(vp, vfs_hash_bucket(mp, hash), v_hashlist) { 80 if (vp->v_hash != hash) 81 continue; 82 if (vp->v_mount != mp) 83 continue; 84 if (fn != NULL && fn(vp, arg)) 85 continue; 86 VI_LOCK(vp); 87 mtx_unlock(&vfs_hash_mtx); 88 error = vget(vp, flags | LK_INTERLOCK, td); 89 if (error == ENOENT && (flags & LK_NOWAIT) == 0) 90 break; 91 if (error) 92 return (error); 93 *vpp = vp; 94 return (0); 95 } 96 if (vp == NULL) { 97 mtx_unlock(&vfs_hash_mtx); 98 *vpp = NULL; 99 return (0); 100 } 101 } 102} 103 104void 105vfs_hash_remove(struct vnode *vp) 106{ 107 108 mtx_lock(&vfs_hash_mtx); 109 LIST_REMOVE(vp, v_hashlist); 110 mtx_unlock(&vfs_hash_mtx); 111} 112 113int 114vfs_hash_insert(struct vnode *vp, u_int hash, int flags, struct thread *td, struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg) 115{ 116 struct vnode *vp2; 117 int error; 118 119 *vpp = NULL; 120 while (1) { 121 mtx_lock(&vfs_hash_mtx); 122 LIST_FOREACH(vp2, 123 vfs_hash_bucket(vp->v_mount, hash), v_hashlist) { 124 if (vp2->v_hash != hash) 125 continue; 126 if (vp2->v_mount != vp->v_mount) 127 continue; 128 if (fn != NULL && fn(vp2, arg)) 129 continue; 130 VI_LOCK(vp2); 131 mtx_unlock(&vfs_hash_mtx); 132 error = vget(vp2, flags | LK_INTERLOCK, td); 133 if (error == ENOENT && (flags & LK_NOWAIT) == 0) 134 break; 135 mtx_lock(&vfs_hash_mtx); 136 LIST_INSERT_HEAD(&vfs_hash_side, vp, v_hashlist); 137 mtx_unlock(&vfs_hash_mtx); 138 vput(vp); 139 if (!error) 140 *vpp = vp2; 141 return (error); 142 } 143 if (vp2 == NULL) 144 break; 145 146 } 147 vp->v_hash = hash; 148 LIST_INSERT_HEAD(vfs_hash_bucket(vp->v_mount, hash), vp, v_hashlist); 149 mtx_unlock(&vfs_hash_mtx); 150 return (0); 151} 152 153void 154vfs_hash_rehash(struct vnode *vp, u_int hash) 155{ 156 157 mtx_lock(&vfs_hash_mtx); 158 LIST_REMOVE(vp, v_hashlist); 159 LIST_INSERT_HEAD(vfs_hash_bucket(vp->v_mount, hash), vp, v_hashlist); 160 vp->v_hash = hash; 161 mtx_unlock(&vfs_hash_mtx); 162} 163