kernel - lwkt_token revamp
[dragonfly.git] / sys / vfs / gnu / ext2fs / ext2_ihash.c
CommitLineData
1f1db49f
MD
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. 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.
20 *
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
31 * SUCH DAMAGE.
32 *
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 $
f3dc9d06 35 * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_ihash.c,v 1.5 2006/10/14 16:26:38 dillon Exp $
1f1db49f
MD
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/lock.h>
42#include <sys/vnode.h>
43#include <sys/malloc.h>
44#include <sys/proc.h>
45
46#include "quota.h"
47#include "dinode.h"
48#include "inode.h"
49#include "ext2_extern.h"
50
51static MALLOC_DEFINE(M_EXT2IHASH, "EXT2 ihash", "EXT2 Inode hash tables");
52/*
53 * Structures associated with inode cacheing.
54 */
55static struct inode **ext2_ihashtbl;
56static u_long ext2_ihash; /* size of hash table - 1 */
57static struct lwkt_token ext2_ihash_token;
58
59#define INOHASH(device, inum) (&ext2_ihashtbl[(minor(device) + (inum)) & ext2_ihash])
60
61/*
62 * Initialize inode hash table.
63 */
64void
65ext2_ihashinit(void)
66{
67 ext2_ihash = 16;
68 while (ext2_ihash < desiredvnodes)
69 ext2_ihash <<= 1;
efda3bd0 70 ext2_ihashtbl = kmalloc(sizeof(void *) * ext2_ihash, M_EXT2IHASH, M_WAITOK|M_ZERO);
1f1db49f 71 --ext2_ihash;
3b998fa9 72 lwkt_token_init(&ext2_ihash_token, 1);
1f1db49f
MD
73}
74
75int
76ext2_uninit(struct vfsconf *vfc)
77{
3b998fa9 78 lwkt_gettoken(&ext2_ihash_token);
1f1db49f 79 if (ext2_ihashtbl)
efda3bd0 80 kfree(ext2_ihashtbl, M_EXT2IHASH);
3b998fa9 81 lwkt_reltoken(&ext2_ihash_token);
1f1db49f
MD
82
83 return (0);
84}
85/*
86 * Use the device/inum pair to find the incore inode, and return a pointer
87 * to it. If it is in core, return it, even if it is locked.
88 */
89struct vnode *
b13267a5 90ext2_ihashlookup(cdev_t dev, ino_t inum)
1f1db49f
MD
91{
92 struct inode *ip;
1f1db49f 93
3b998fa9 94 lwkt_gettoken(&ext2_ihash_token);
1f1db49f
MD
95 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
96 if (inum == ip->i_number && dev == ip->i_dev)
97 break;
98 }
3b998fa9 99 lwkt_reltoken(&ext2_ihash_token);
1f1db49f
MD
100 if (ip)
101 return (ITOV(ip));
102 return (NULLVP);
103}
104
105/*
106 * Use the device/inum pair to find the incore inode, and return a pointer
107 * to it. If it is in core, but locked, wait for it.
108 *
109 * Note that the serializing tokens do not prevent other processes from
110 * playing with the data structure being protected while we are blocked.
111 * They do protect us from preemptive interrupts which might try to
112 * play with the protected data structure.
113 */
114struct vnode *
b13267a5 115ext2_ihashget(cdev_t dev, ino_t inum)
1f1db49f 116{
1f1db49f
MD
117 struct inode *ip;
118 struct vnode *vp;
119
3b998fa9 120 lwkt_gettoken(&ext2_ihash_token);
1f1db49f
MD
121loop:
122 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
123 if (inum != ip->i_number || dev != ip->i_dev)
124 continue;
125 vp = ITOV(ip);
87de5057 126 if (vget(vp, LK_EXCLUSIVE))
1f1db49f
MD
127 goto loop;
128 /*
129 * We must check to see if the inode has been ripped
130 * out from under us after blocking.
131 */
132 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
133 if (inum == ip->i_number && dev == ip->i_dev)
134 break;
135 }
136 if (ip == NULL || ITOV(ip) != vp) {
137 vput(vp);
138 goto loop;
139 }
3b998fa9 140 lwkt_reltoken(&ext2_ihash_token);
1f1db49f
MD
141 return (vp);
142 }
3b998fa9 143 lwkt_reltoken(&ext2_ihash_token);
1f1db49f
MD
144 return (NULL);
145}
146
147/*
148 * Check to see if an inode is in the hash table. This is used to interlock
149 * file free operations to ensure that the vnode is not reused due to a
150 * reallocate of its inode number before we have had a chance to recycle it.
151 */
152int
b13267a5 153ext2_ihashcheck(cdev_t dev, ino_t inum)
1f1db49f 154{
1f1db49f
MD
155 struct inode *ip;
156
3b998fa9 157 lwkt_gettoken(&ext2_ihash_token);
1f1db49f 158 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
f3dc9d06 159 if (inum == ip->i_number && dev == ip->i_dev)
1f1db49f 160 break;
1f1db49f 161 }
3b998fa9 162 lwkt_reltoken(&ext2_ihash_token);
1f1db49f
MD
163 return(ip ? 1 : 0);
164}
165
166/*
167 * Insert the inode into the hash table, and return it locked.
168 */
169int
170ext2_ihashins(struct inode *ip)
171{
172 struct inode **ipp;
173 struct inode *iq;
1f1db49f
MD
174
175 KKASSERT((ip->i_flag & IN_HASHED) == 0);
3b998fa9 176 lwkt_gettoken(&ext2_ihash_token);
1f1db49f
MD
177 ipp = INOHASH(ip->i_dev, ip->i_number);
178 while ((iq = *ipp) != NULL) {
179 if (ip->i_dev == iq->i_dev && ip->i_number == iq->i_number) {
3b998fa9 180 lwkt_reltoken(&ext2_ihash_token);
1f1db49f
MD
181 return(EBUSY);
182 }
183 ipp = &iq->i_next;
184 }
185 ip->i_next = NULL;
186 *ipp = ip;
187 ip->i_flag |= IN_HASHED;
3b998fa9 188 lwkt_reltoken(&ext2_ihash_token);
1f1db49f
MD
189 return(0);
190}
191
192/*
193 * Remove the inode from the hash table.
194 */
195void
196ext2_ihashrem(struct inode *ip)
197{
1f1db49f
MD
198 struct inode **ipp;
199 struct inode *iq;
200
3b998fa9 201 lwkt_gettoken(&ext2_ihash_token);
1f1db49f
MD
202 if (ip->i_flag & IN_HASHED) {
203 ipp = INOHASH(ip->i_dev, ip->i_number);
204 while ((iq = *ipp) != NULL) {
205 if (ip == iq)
206 break;
207 ipp = &iq->i_next;
208 }
209 KKASSERT(ip == iq);
210 *ipp = ip->i_next;
211 ip->i_next = NULL;
212 ip->i_flag &= ~IN_HASHED;
213 }
3b998fa9 214 lwkt_reltoken(&ext2_ihash_token);
1f1db49f
MD
215}
216