1 /*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993, 1995
3 * The Regents of the University of California. 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 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)ufs_ihash.c 8.7 (Berkeley) 5/17/95
30 * $FreeBSD: src/sys/ufs/ufs/ufs_ihash.c,v 1.20 1999/08/28 00:52:29 peter Exp $
31 */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/vnode.h>
38 #include <sys/malloc.h>
39 #include <sys/proc.h>
40
41 #include <vfs/ext2fs/fs.h>
42 #include <vfs/ext2fs/ext2fs.h>
43 #include <vfs/ext2fs/inode.h>
44 #include <vfs/ext2fs/ext2_dinode.h>
45 #include <vfs/ext2fs/ext2_extern.h>
46
47 static MALLOC_DEFINE(M_EXT2IHASH, "EXT2 ihash", "EXT2 Inode hash tables");
48 /*
49 * Structures associated with inode cacheing.
50 */
51 static struct inode **ext2_ihashtbl;
52 static u_long ext2_ihash; /* size of hash table - 1 */
53 static struct lwkt_token ext2_ihash_token;
54
55 #define INOHASH(device, inum) (&ext2_ihashtbl[(minor(device) + (inum)) & ext2_ihash])
56
57 /*
58 * Initialize inode hash table.
59 */
60 void
ext2_ihashinit(void)61 ext2_ihashinit(void)
62 {
63 ext2_ihash = vfs_inodehashsize();
64 ext2_ihashtbl = malloc(sizeof(void *) * ext2_ihash, M_EXT2IHASH,
65 M_WAITOK | M_ZERO);
66 --ext2_ihash;
67 lwkt_token_init(&ext2_ihash_token, "ext2ihash");
68 }
69
70 void
ext2_ihashuninit(void)71 ext2_ihashuninit(void)
72 {
73 lwkt_gettoken(&ext2_ihash_token);
74 if (ext2_ihashtbl)
75 free(ext2_ihashtbl, M_EXT2IHASH);
76 lwkt_reltoken(&ext2_ihash_token);
77 }
78
79 /*
80 * Use the device/inum pair to find the incore inode, and return a pointer
81 * to it. If it is in core, but locked, wait for it.
82 *
83 * Note that the serializing tokens do not prevent other processes from
84 * playing with the data structure being protected while we are blocked.
85 * They do protect us from preemptive interrupts which might try to
86 * play with the protected data structure.
87 */
88 struct vnode *
ext2_ihashget(cdev_t dev,ino_t inum)89 ext2_ihashget(cdev_t dev, ino_t inum)
90 {
91 struct inode *ip;
92 struct vnode *vp;
93
94 lwkt_gettoken(&ext2_ihash_token);
95 loop:
96 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
97 if (inum != ip->i_number || dev != ip->i_dev)
98 continue;
99 vp = ITOV(ip);
100 if (vget(vp, LK_EXCLUSIVE))
101 goto loop;
102 /*
103 * We must check to see if the inode has been ripped
104 * out from under us after blocking.
105 */
106 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
107 if (inum == ip->i_number && dev == ip->i_dev)
108 break;
109 }
110 if (ip == NULL || ITOV(ip) != vp) {
111 vput(vp);
112 goto loop;
113 }
114 lwkt_reltoken(&ext2_ihash_token);
115 return (vp);
116 }
117 lwkt_reltoken(&ext2_ihash_token);
118 return (NULL);
119 }
120
121 /*
122 * Insert the inode into the hash table, and return it locked.
123 */
124 int
ext2_ihashins(struct inode * ip)125 ext2_ihashins(struct inode *ip)
126 {
127 struct inode **ipp;
128 struct inode *iq;
129
130 KKASSERT((ip->i_flag & IN_HASHED) == 0);
131 lwkt_gettoken(&ext2_ihash_token);
132 ipp = INOHASH(ip->i_dev, ip->i_number);
133 while ((iq = *ipp) != NULL) {
134 if (ip->i_dev == iq->i_dev && ip->i_number == iq->i_number) {
135 lwkt_reltoken(&ext2_ihash_token);
136 return (EBUSY);
137 }
138 ipp = &iq->i_next;
139 }
140 ip->i_next = NULL;
141 *ipp = ip;
142 ip->i_flag |= IN_HASHED;
143 lwkt_reltoken(&ext2_ihash_token);
144 return (0);
145 }
146
147 /*
148 * Remove the inode from the hash table.
149 */
150 void
ext2_ihashrem(struct inode * ip)151 ext2_ihashrem(struct inode *ip)
152 {
153 struct inode **ipp;
154 struct inode *iq;
155
156 lwkt_gettoken(&ext2_ihash_token);
157 if (ip->i_flag & IN_HASHED) {
158 ipp = INOHASH(ip->i_dev, ip->i_number);
159 while ((iq = *ipp) != NULL) {
160 if (ip == iq)
161 break;
162 ipp = &iq->i_next;
163 }
164 KKASSERT(ip == iq);
165 *ipp = ip->i_next;
166 ip->i_next = NULL;
167 ip->i_flag &= ~IN_HASHED;
168 }
169 lwkt_reltoken(&ext2_ihash_token);
170 }
171