2 * Copyright (c) 1982, 1986, 1989, 1991, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)ufs_ihash.c 8.7 (Berkeley) 5/17/95
34 * $FreeBSD: src/sys/ufs/ufs/ufs_ihash.c,v 1.20 1999/08/28 00:52:29 peter Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/vnode.h>
42 #include <sys/malloc.h>
48 #include "ext2_extern.h"
50 static MALLOC_DEFINE(M_EXT2IHASH, "EXT2 ihash", "EXT2 Inode hash tables");
52 * Structures associated with inode cacheing.
54 static struct inode **ext2_ihashtbl;
55 static u_long ext2_ihash; /* size of hash table - 1 */
56 static struct lwkt_token ext2_ihash_token;
58 #define INOHASH(device, inum) (&ext2_ihashtbl[(minor(device) + (inum)) & ext2_ihash])
61 * Initialize inode hash table.
67 while (ext2_ihash < desiredvnodes)
69 ext2_ihashtbl = kmalloc(sizeof(void *) * ext2_ihash, M_EXT2IHASH, M_WAITOK|M_ZERO);
71 lwkt_token_init(&ext2_ihash_token, "ext2ihash");
75 ext2_uninit(struct vfsconf *vfc)
77 lwkt_gettoken(&ext2_ihash_token);
79 kfree(ext2_ihashtbl, M_EXT2IHASH);
80 lwkt_reltoken(&ext2_ihash_token);
85 * Use the device/inum pair to find the incore inode, and return a pointer
86 * to it. If it is in core, return it, even if it is locked.
89 ext2_ihashlookup(cdev_t dev, ino_t inum)
93 lwkt_gettoken(&ext2_ihash_token);
94 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
95 if (inum == ip->i_number && dev == ip->i_dev)
98 lwkt_reltoken(&ext2_ihash_token);
105 * Use the device/inum pair to find the incore inode, and return a pointer
106 * to it. If it is in core, but locked, wait for it.
108 * Note that the serializing tokens do not prevent other processes from
109 * playing with the data structure being protected while we are blocked.
110 * They do protect us from preemptive interrupts which might try to
111 * play with the protected data structure.
114 ext2_ihashget(cdev_t dev, ino_t inum)
119 lwkt_gettoken(&ext2_ihash_token);
121 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
122 if (inum != ip->i_number || dev != ip->i_dev)
125 if (vget(vp, LK_EXCLUSIVE))
128 * We must check to see if the inode has been ripped
129 * out from under us after blocking.
131 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
132 if (inum == ip->i_number && dev == ip->i_dev)
135 if (ip == NULL || ITOV(ip) != vp) {
139 lwkt_reltoken(&ext2_ihash_token);
142 lwkt_reltoken(&ext2_ihash_token);
147 * Check to see if an inode is in the hash table. This is used to interlock
148 * file free operations to ensure that the vnode is not reused due to a
149 * reallocate of its inode number before we have had a chance to recycle it.
152 ext2_ihashcheck(cdev_t dev, ino_t inum)
156 lwkt_gettoken(&ext2_ihash_token);
157 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
158 if (inum == ip->i_number && dev == ip->i_dev)
161 lwkt_reltoken(&ext2_ihash_token);
166 * Insert the inode into the hash table, and return it locked.
169 ext2_ihashins(struct inode *ip)
174 KKASSERT((ip->i_flag & IN_HASHED) == 0);
175 lwkt_gettoken(&ext2_ihash_token);
176 ipp = INOHASH(ip->i_dev, ip->i_number);
177 while ((iq = *ipp) != NULL) {
178 if (ip->i_dev == iq->i_dev && ip->i_number == iq->i_number) {
179 lwkt_reltoken(&ext2_ihash_token);
186 ip->i_flag |= IN_HASHED;
187 lwkt_reltoken(&ext2_ihash_token);
192 * Remove the inode from the hash table.
195 ext2_ihashrem(struct inode *ip)
200 lwkt_gettoken(&ext2_ihash_token);
201 if (ip->i_flag & IN_HASHED) {
202 ipp = INOHASH(ip->i_dev, ip->i_number);
203 while ((iq = *ipp) != NULL) {
211 ip->i_flag &= ~IN_HASHED;
213 lwkt_reltoken(&ext2_ihash_token);