kernel - lwkt_token revamp
[dragonfly.git] / sys / vfs / ufs / ufs_ihash.c
CommitLineData
984263bc
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/ufs/ufs_ihash.c,v 1.20 2006/10/14 16:26:40 dillon Exp $
984263bc
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
1f2de5d4
MD
46#include "quota.h"
47#include "inode.h"
48#include "ufs_extern.h"
984263bc
MD
49
50static MALLOC_DEFINE(M_UFSIHASH, "UFS ihash", "UFS Inode hash tables");
51/*
52 * Structures associated with inode cacheing.
53 */
3446c007 54static struct inode **ihashtbl;
984263bc 55static u_long ihash; /* size of hash table - 1 */
8a8d5d85 56static struct lwkt_token ufs_ihash_token;
984263bc 57
3446c007
MD
58#define INOHASH(device, inum) (&ihashtbl[(minor(device) + (inum)) & ihash])
59
984263bc
MD
60/*
61 * Initialize inode hash table.
62 */
63void
0973c589 64ufs_ihashinit(void)
984263bc 65{
3446c007
MD
66 ihash = 16;
67 while (ihash < desiredvnodes)
68 ihash <<= 1;
efda3bd0 69 ihashtbl = kmalloc(sizeof(void *) * ihash, M_UFSIHASH, M_WAITOK|M_ZERO);
3446c007 70 --ihash;
3b998fa9 71 lwkt_token_init(&ufs_ihash_token, 1);
984263bc
MD
72}
73
43c45e8f
HP
74int
75ufs_uninit(struct vfsconf *vfc)
76{
3b998fa9 77 lwkt_gettoken(&ufs_ihash_token);
43c45e8f 78 if (ihashtbl)
efda3bd0 79 kfree(ihashtbl, M_UFSIHASH);
3b998fa9 80 lwkt_reltoken(&ufs_ihash_token);
43c45e8f
HP
81
82 return (0);
83}
984263bc
MD
84/*
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.
87 */
88struct vnode *
b13267a5 89ufs_ihashlookup(cdev_t dev, ino_t inum)
984263bc
MD
90{
91 struct inode *ip;
92
3b998fa9 93 lwkt_gettoken(&ufs_ihash_token);
3446c007 94 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
984263bc
MD
95 if (inum == ip->i_number && dev == ip->i_dev)
96 break;
1bfd75da 97 }
3b998fa9 98 lwkt_reltoken(&ufs_ihash_token);
984263bc
MD
99 if (ip)
100 return (ITOV(ip));
101 return (NULLVP);
102}
103
104/*
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.
4954c633 107 *
41a01a4d
MD
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.
984263bc
MD
112 */
113struct vnode *
b13267a5 114ufs_ihashget(cdev_t dev, ino_t inum)
984263bc 115{
984263bc
MD
116 struct inode *ip;
117 struct vnode *vp;
118
3b998fa9 119 lwkt_gettoken(&ufs_ihash_token);
984263bc 120loop:
3446c007 121 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
41a01a4d
MD
122 if (inum != ip->i_number || dev != ip->i_dev)
123 continue;
124 vp = ITOV(ip);
87de5057 125 if (vget(vp, LK_EXCLUSIVE))
5fd012e0 126 goto loop;
41a01a4d
MD
127 /*
128 * We must check to see if the inode has been ripped
129 * out from under us after blocking.
130 */
3446c007 131 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
41a01a4d
MD
132 if (inum == ip->i_number && dev == ip->i_dev)
133 break;
984263bc 134 }
41a01a4d 135 if (ip == NULL || ITOV(ip) != vp) {
5fd012e0 136 vput(vp);
41a01a4d
MD
137 goto loop;
138 }
3b998fa9 139 lwkt_reltoken(&ufs_ihash_token);
41a01a4d 140 return (vp);
984263bc 141 }
3b998fa9 142 lwkt_reltoken(&ufs_ihash_token);
984263bc
MD
143 return (NULL);
144}
145
146/*
b1f7dd27
MD
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.
150 */
151int
b13267a5 152ufs_ihashcheck(cdev_t dev, ino_t inum)
b1f7dd27 153{
b1f7dd27
MD
154 struct inode *ip;
155
3b998fa9 156 lwkt_gettoken(&ufs_ihash_token);
b1f7dd27 157 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
f3dc9d06 158 if (inum == ip->i_number && dev == ip->i_dev)
b1f7dd27 159 break;
b1f7dd27 160 }
3b998fa9 161 lwkt_reltoken(&ufs_ihash_token);
b1f7dd27
MD
162 return(ip ? 1 : 0);
163}
164
165/*
984263bc
MD
166 * Insert the inode into the hash table, and return it locked.
167 */
3446c007 168int
dadab5e9 169ufs_ihashins(struct inode *ip)
984263bc 170{
3446c007
MD
171 struct inode **ipp;
172 struct inode *iq;
984263bc 173
3446c007 174 KKASSERT((ip->i_flag & IN_HASHED) == 0);
3b998fa9 175 lwkt_gettoken(&ufs_ihash_token);
984263bc 176 ipp = INOHASH(ip->i_dev, ip->i_number);
3446c007
MD
177 while ((iq = *ipp) != NULL) {
178 if (ip->i_dev == iq->i_dev && ip->i_number == iq->i_number) {
3b998fa9 179 lwkt_reltoken(&ufs_ihash_token);
3446c007
MD
180 return(EBUSY);
181 }
182 ipp = &iq->i_next;
183 }
184 ip->i_next = NULL;
185 *ipp = ip;
984263bc 186 ip->i_flag |= IN_HASHED;
3b998fa9 187 lwkt_reltoken(&ufs_ihash_token);
3446c007 188 return(0);
984263bc
MD
189}
190
191/*
192 * Remove the inode from the hash table.
193 */
194void
0973c589 195ufs_ihashrem(struct inode *ip)
984263bc 196{
3446c007
MD
197 struct inode **ipp;
198 struct inode *iq;
41a01a4d 199
3b998fa9 200 lwkt_gettoken(&ufs_ihash_token);
984263bc 201 if (ip->i_flag & IN_HASHED) {
3446c007
MD
202 ipp = INOHASH(ip->i_dev, ip->i_number);
203 while ((iq = *ipp) != NULL) {
204 if (ip == iq)
205 break;
206 ipp = &iq->i_next;
207 }
208 KKASSERT(ip == iq);
209 *ipp = ip->i_next;
210 ip->i_next = NULL;
984263bc 211 ip->i_flag &= ~IN_HASHED;
984263bc 212 }
3b998fa9 213 lwkt_reltoken(&ufs_ihash_token);
984263bc 214}
3446c007 215