xref: /freebsd/sys/kern/vfs_hash.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
16c325a2aSPoul-Henning Kamp /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
46c325a2aSPoul-Henning Kamp  * Copyright (c) 2005 Poul-Henning Kamp
56c325a2aSPoul-Henning Kamp  * All rights reserved.
66c325a2aSPoul-Henning Kamp  *
76c325a2aSPoul-Henning Kamp  * Redistribution and use in source and binary forms, with or without
86c325a2aSPoul-Henning Kamp  * modification, are permitted provided that the following conditions
96c325a2aSPoul-Henning Kamp  * are met:
106c325a2aSPoul-Henning Kamp  * 1. Redistributions of source code must retain the above copyright
116c325a2aSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer.
126c325a2aSPoul-Henning Kamp  * 2. Redistributions in binary form must reproduce the above copyright
136c325a2aSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer in the
146c325a2aSPoul-Henning Kamp  *    documentation and/or other materials provided with the distribution.
156c325a2aSPoul-Henning Kamp  *
166c325a2aSPoul-Henning Kamp  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
176c325a2aSPoul-Henning Kamp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
186c325a2aSPoul-Henning Kamp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
196c325a2aSPoul-Henning Kamp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
206c325a2aSPoul-Henning Kamp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
216c325a2aSPoul-Henning Kamp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
226c325a2aSPoul-Henning Kamp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
236c325a2aSPoul-Henning Kamp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
246c325a2aSPoul-Henning Kamp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
256c325a2aSPoul-Henning Kamp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
266c325a2aSPoul-Henning Kamp  * SUCH DAMAGE.
276c325a2aSPoul-Henning Kamp  *
286c325a2aSPoul-Henning Kamp  */
296c325a2aSPoul-Henning Kamp 
306c325a2aSPoul-Henning Kamp #include <sys/param.h>
316c325a2aSPoul-Henning Kamp #include <sys/systm.h>
326c325a2aSPoul-Henning Kamp #include <sys/kernel.h>
336c325a2aSPoul-Henning Kamp #include <sys/malloc.h>
3478bb3c21SPoul-Henning Kamp #include <sys/mount.h>
35af77c1a6SMateusz Guzik #include <sys/rwlock.h>
366c325a2aSPoul-Henning Kamp #include <sys/vnode.h>
376c325a2aSPoul-Henning Kamp 
385bb84bc8SRobert Watson static MALLOC_DEFINE(M_VFS_HASH, "vfs_hash", "VFS hash table");
396c325a2aSPoul-Henning Kamp 
4078bb3c21SPoul-Henning Kamp static LIST_HEAD(vfs_hash_head, vnode)	*vfs_hash_tbl;
4178bb3c21SPoul-Henning Kamp static LIST_HEAD(,vnode)		vfs_hash_side;
426c325a2aSPoul-Henning Kamp static u_long				vfs_hash_mask;
43bb62c418SMateusz Guzik static struct rwlock __exclusive_cache_line vfs_hash_lock;
446c325a2aSPoul-Henning Kamp 
456c325a2aSPoul-Henning Kamp static void
vfs_hashinit(void * dummy __unused)466c325a2aSPoul-Henning Kamp vfs_hashinit(void *dummy __unused)
476c325a2aSPoul-Henning Kamp {
486c325a2aSPoul-Henning Kamp 
496c325a2aSPoul-Henning Kamp 	vfs_hash_tbl = hashinit(desiredvnodes, M_VFS_HASH, &vfs_hash_mask);
50af77c1a6SMateusz Guzik 	rw_init(&vfs_hash_lock, "vfs hash");
5178bb3c21SPoul-Henning Kamp 	LIST_INIT(&vfs_hash_side);
526c325a2aSPoul-Henning Kamp }
536c325a2aSPoul-Henning Kamp 
546c325a2aSPoul-Henning Kamp /* Must be SI_ORDER_SECOND so desiredvnodes is available */
55237fdd78SRobert Watson SYSINIT(vfs_hash, SI_SUB_VFS, SI_ORDER_SECOND, vfs_hashinit, NULL);
566c325a2aSPoul-Henning Kamp 
57f6af8e37SKonstantin Belousov u_int
vfs_hash_index(struct vnode * vp)58f6af8e37SKonstantin Belousov vfs_hash_index(struct vnode *vp)
59f6af8e37SKonstantin Belousov {
60f6af8e37SKonstantin Belousov 
61f6af8e37SKonstantin Belousov 	return (vp->v_hash + vp->v_mount->mnt_hashseed);
62f6af8e37SKonstantin Belousov }
63f6af8e37SKonstantin Belousov 
6478bb3c21SPoul-Henning Kamp static struct vfs_hash_head *
vfs_hash_bucket(const struct mount * mp,u_int hash)657b982bc8SKonstantin Belousov vfs_hash_bucket(const struct mount *mp, u_int hash)
6678bb3c21SPoul-Henning Kamp {
6778bb3c21SPoul-Henning Kamp 
6878bb3c21SPoul-Henning Kamp 	return (&vfs_hash_tbl[(hash + mp->mnt_hashseed) & vfs_hash_mask]);
6978bb3c21SPoul-Henning Kamp }
7078bb3c21SPoul-Henning Kamp 
716c325a2aSPoul-Henning Kamp int
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)72cd85d599SKonstantin Belousov vfs_hash_get(const struct mount *mp, u_int hash, int flags, struct thread *td,
73cd85d599SKonstantin Belousov     struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg)
746c325a2aSPoul-Henning Kamp {
756c325a2aSPoul-Henning Kamp 	struct vnode *vp;
76e3c3248cSMateusz Guzik 	enum vgetstate vs;
776c325a2aSPoul-Henning Kamp 	int error;
786c325a2aSPoul-Henning Kamp 
796c325a2aSPoul-Henning Kamp 	while (1) {
80af77c1a6SMateusz Guzik 		rw_rlock(&vfs_hash_lock);
817b982bc8SKonstantin Belousov 		LIST_FOREACH(vp, vfs_hash_bucket(mp, hash), v_hashlist) {
826c325a2aSPoul-Henning Kamp 			if (vp->v_hash != hash)
836c325a2aSPoul-Henning Kamp 				continue;
846c325a2aSPoul-Henning Kamp 			if (vp->v_mount != mp)
856c325a2aSPoul-Henning Kamp 				continue;
8651f5ce0cSPoul-Henning Kamp 			if (fn != NULL && fn(vp, arg))
8751f5ce0cSPoul-Henning Kamp 				continue;
88e3c3248cSMateusz Guzik 			vs = vget_prep(vp);
89af77c1a6SMateusz Guzik 			rw_runlock(&vfs_hash_lock);
90e3c3248cSMateusz Guzik 			error = vget_finish(vp, flags, vs);
916ff5e2dbSTor Egge 			if (error == ENOENT && (flags & LK_NOWAIT) == 0)
926c325a2aSPoul-Henning Kamp 				break;
937c1e4aabSKonstantin Belousov 			if (error != 0)
946c325a2aSPoul-Henning Kamp 				return (error);
957c1e4aabSKonstantin Belousov 			if (vp->v_hash != hash ||
967c1e4aabSKonstantin Belousov 			    (fn != NULL && fn(vp, arg))) {
977c1e4aabSKonstantin Belousov 				vput(vp);
987c1e4aabSKonstantin Belousov 				/* Restart the bucket walk. */
997c1e4aabSKonstantin Belousov 				break;
1007c1e4aabSKonstantin Belousov 			}
1016c325a2aSPoul-Henning Kamp 			*vpp = vp;
1026c325a2aSPoul-Henning Kamp 			return (0);
1036c325a2aSPoul-Henning Kamp 		}
1046c325a2aSPoul-Henning Kamp 		if (vp == NULL) {
105af77c1a6SMateusz Guzik 			rw_runlock(&vfs_hash_lock);
1066c325a2aSPoul-Henning Kamp 			*vpp = NULL;
1076c325a2aSPoul-Henning Kamp 			return (0);
1086c325a2aSPoul-Henning Kamp 		}
1096c325a2aSPoul-Henning Kamp 	}
1106c325a2aSPoul-Henning Kamp }
1116c325a2aSPoul-Henning Kamp 
1126c325a2aSPoul-Henning Kamp void
vfs_hash_ref(const struct mount * mp,u_int hash,struct thread * td,struct vnode ** vpp,vfs_hash_cmp_t * fn,void * arg)11354a33d2fSKonstantin Belousov vfs_hash_ref(const struct mount *mp, u_int hash, struct thread *td,
11454a33d2fSKonstantin Belousov     struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg)
11554a33d2fSKonstantin Belousov {
11654a33d2fSKonstantin Belousov 	struct vnode *vp;
11754a33d2fSKonstantin Belousov 
11854a33d2fSKonstantin Belousov 	while (1) {
11954a33d2fSKonstantin Belousov 		rw_rlock(&vfs_hash_lock);
12054a33d2fSKonstantin Belousov 		LIST_FOREACH(vp, vfs_hash_bucket(mp, hash), v_hashlist) {
12154a33d2fSKonstantin Belousov 			if (vp->v_hash != hash)
12254a33d2fSKonstantin Belousov 				continue;
12354a33d2fSKonstantin Belousov 			if (vp->v_mount != mp)
12454a33d2fSKonstantin Belousov 				continue;
12554a33d2fSKonstantin Belousov 			if (fn != NULL && fn(vp, arg))
12654a33d2fSKonstantin Belousov 				continue;
12754a33d2fSKonstantin Belousov 			vhold(vp);
12854a33d2fSKonstantin Belousov 			rw_runlock(&vfs_hash_lock);
12954a33d2fSKonstantin Belousov 			vref(vp);
13054a33d2fSKonstantin Belousov 			vdrop(vp);
13154a33d2fSKonstantin Belousov 			*vpp = vp;
13254a33d2fSKonstantin Belousov 			return;
13354a33d2fSKonstantin Belousov 		}
13454a33d2fSKonstantin Belousov 		if (vp == NULL) {
13554a33d2fSKonstantin Belousov 			rw_runlock(&vfs_hash_lock);
13654a33d2fSKonstantin Belousov 			*vpp = NULL;
13754a33d2fSKonstantin Belousov 			return;
13854a33d2fSKonstantin Belousov 		}
13954a33d2fSKonstantin Belousov 	}
14054a33d2fSKonstantin Belousov }
14154a33d2fSKonstantin Belousov 
14254a33d2fSKonstantin Belousov void
vfs_hash_remove(struct vnode * vp)1436c325a2aSPoul-Henning Kamp vfs_hash_remove(struct vnode *vp)
1446c325a2aSPoul-Henning Kamp {
1456c325a2aSPoul-Henning Kamp 
146af77c1a6SMateusz Guzik 	rw_wlock(&vfs_hash_lock);
1476c325a2aSPoul-Henning Kamp 	LIST_REMOVE(vp, v_hashlist);
148af77c1a6SMateusz Guzik 	rw_wunlock(&vfs_hash_lock);
1496c325a2aSPoul-Henning Kamp }
1506c325a2aSPoul-Henning Kamp 
1516c325a2aSPoul-Henning Kamp int
vfs_hash_insert(struct vnode * vp,u_int hash,int flags,struct thread * td,struct vnode ** vpp,vfs_hash_cmp_t * fn,void * arg)152cd85d599SKonstantin Belousov vfs_hash_insert(struct vnode *vp, u_int hash, int flags, struct thread *td,
153cd85d599SKonstantin Belousov     struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg)
1546c325a2aSPoul-Henning Kamp {
1556c325a2aSPoul-Henning Kamp 	struct vnode *vp2;
156e3c3248cSMateusz Guzik 	enum vgetstate vs;
1576c325a2aSPoul-Henning Kamp 	int error;
1586c325a2aSPoul-Henning Kamp 
159e82ef95cSPoul-Henning Kamp 	*vpp = NULL;
1606c325a2aSPoul-Henning Kamp 	while (1) {
161af77c1a6SMateusz Guzik 		rw_wlock(&vfs_hash_lock);
1626c325a2aSPoul-Henning Kamp 		LIST_FOREACH(vp2,
1637b982bc8SKonstantin Belousov 		    vfs_hash_bucket(vp->v_mount, hash), v_hashlist) {
1646c325a2aSPoul-Henning Kamp 			if (vp2->v_hash != hash)
1656c325a2aSPoul-Henning Kamp 				continue;
1666c325a2aSPoul-Henning Kamp 			if (vp2->v_mount != vp->v_mount)
1676c325a2aSPoul-Henning Kamp 				continue;
168a1e1d551SPoul-Henning Kamp 			if (fn != NULL && fn(vp2, arg))
16951f5ce0cSPoul-Henning Kamp 				continue;
170e3c3248cSMateusz Guzik 			vs = vget_prep(vp2);
171af77c1a6SMateusz Guzik 			rw_wunlock(&vfs_hash_lock);
172e3c3248cSMateusz Guzik 			error = vget_finish(vp2, flags, vs);
1736ff5e2dbSTor Egge 			if (error == ENOENT && (flags & LK_NOWAIT) == 0)
1746c325a2aSPoul-Henning Kamp 				break;
175af77c1a6SMateusz Guzik 			rw_wlock(&vfs_hash_lock);
17678bb3c21SPoul-Henning Kamp 			LIST_INSERT_HEAD(&vfs_hash_side, vp, v_hashlist);
177af77c1a6SMateusz Guzik 			rw_wunlock(&vfs_hash_lock);
178a0a36d48SChuck Silvers 			vgone(vp);
17945c26fa2SPoul-Henning Kamp 			vput(vp);
18045c26fa2SPoul-Henning Kamp 			if (!error)
1816c325a2aSPoul-Henning Kamp 				*vpp = vp2;
18245c26fa2SPoul-Henning Kamp 			return (error);
1836c325a2aSPoul-Henning Kamp 		}
1846c325a2aSPoul-Henning Kamp 		if (vp2 == NULL)
1856c325a2aSPoul-Henning Kamp 			break;
1866c325a2aSPoul-Henning Kamp 	}
1876c325a2aSPoul-Henning Kamp 	vp->v_hash = hash;
1887b982bc8SKonstantin Belousov 	LIST_INSERT_HEAD(vfs_hash_bucket(vp->v_mount, hash), vp, v_hashlist);
189af77c1a6SMateusz Guzik 	rw_wunlock(&vfs_hash_lock);
1906c325a2aSPoul-Henning Kamp 	return (0);
1916c325a2aSPoul-Henning Kamp }
1926c325a2aSPoul-Henning Kamp 
1936c325a2aSPoul-Henning Kamp void
vfs_hash_rehash(struct vnode * vp,u_int hash)1946c325a2aSPoul-Henning Kamp vfs_hash_rehash(struct vnode *vp, u_int hash)
1956c325a2aSPoul-Henning Kamp {
196f19063abSKonstantin Belousov 	ASSERT_VOP_ELOCKED(vp, "rehash requires excl lock");
1976c325a2aSPoul-Henning Kamp 
198af77c1a6SMateusz Guzik 	rw_wlock(&vfs_hash_lock);
1996c325a2aSPoul-Henning Kamp 	LIST_REMOVE(vp, v_hashlist);
2007b982bc8SKonstantin Belousov 	LIST_INSERT_HEAD(vfs_hash_bucket(vp->v_mount, hash), vp, v_hashlist);
2016c325a2aSPoul-Henning Kamp 	vp->v_hash = hash;
202af77c1a6SMateusz Guzik 	rw_wunlock(&vfs_hash_lock);
2036c325a2aSPoul-Henning Kamp }
20417518b1aSKirk McKusick 
20517518b1aSKirk McKusick void
vfs_hash_changesize(u_long newmaxvnodes)20669283067SMateusz Guzik vfs_hash_changesize(u_long newmaxvnodes)
20717518b1aSKirk McKusick {
20817518b1aSKirk McKusick 	struct vfs_hash_head *vfs_hash_newtbl, *vfs_hash_oldtbl;
20917518b1aSKirk McKusick 	u_long vfs_hash_newmask, vfs_hash_oldmask;
21017518b1aSKirk McKusick 	struct vnode *vp;
21117518b1aSKirk McKusick 	int i;
21217518b1aSKirk McKusick 
21317518b1aSKirk McKusick 	vfs_hash_newtbl = hashinit(newmaxvnodes, M_VFS_HASH,
21417518b1aSKirk McKusick 		&vfs_hash_newmask);
21517518b1aSKirk McKusick 	/* If same hash table size, nothing to do */
21617518b1aSKirk McKusick 	if (vfs_hash_mask == vfs_hash_newmask) {
21717518b1aSKirk McKusick 		free(vfs_hash_newtbl, M_VFS_HASH);
21817518b1aSKirk McKusick 		return;
21917518b1aSKirk McKusick 	}
22017518b1aSKirk McKusick 	/*
22117518b1aSKirk McKusick 	 * Move everything from the old hash table to the new table.
22217518b1aSKirk McKusick 	 * None of the vnodes in the table can be recycled because to
22317518b1aSKirk McKusick 	 * do so, they have to be removed from the hash table.
22417518b1aSKirk McKusick 	 */
22517518b1aSKirk McKusick 	rw_wlock(&vfs_hash_lock);
22617518b1aSKirk McKusick 	vfs_hash_oldtbl = vfs_hash_tbl;
22717518b1aSKirk McKusick 	vfs_hash_oldmask = vfs_hash_mask;
22817518b1aSKirk McKusick 	vfs_hash_tbl = vfs_hash_newtbl;
22917518b1aSKirk McKusick 	vfs_hash_mask = vfs_hash_newmask;
23017518b1aSKirk McKusick 	for (i = 0; i <= vfs_hash_oldmask; i++) {
23117518b1aSKirk McKusick 		while ((vp = LIST_FIRST(&vfs_hash_oldtbl[i])) != NULL) {
23217518b1aSKirk McKusick 			LIST_REMOVE(vp, v_hashlist);
23317518b1aSKirk McKusick 			LIST_INSERT_HEAD(
23417518b1aSKirk McKusick 			    vfs_hash_bucket(vp->v_mount, vp->v_hash),
23517518b1aSKirk McKusick 			    vp, v_hashlist);
23617518b1aSKirk McKusick 		}
23717518b1aSKirk McKusick 	}
23817518b1aSKirk McKusick 	rw_wunlock(&vfs_hash_lock);
23917518b1aSKirk McKusick 	free(vfs_hash_oldtbl, M_VFS_HASH);
24017518b1aSKirk McKusick }
241