The thread/proc pointer argument in the VFS subsystem originally existed
[dragonfly.git] / sys / vfs / ufs / ufs_ihash.c
... / ...
CommitLineData
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 $
35 * $DragonFly: src/sys/vfs/ufs/ufs_ihash.c,v 1.17 2006/05/06 02:43:14 dillon Exp $
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 "inode.h"
48#include "ufs_extern.h"
49
50static MALLOC_DEFINE(M_UFSIHASH, "UFS ihash", "UFS Inode hash tables");
51/*
52 * Structures associated with inode cacheing.
53 */
54static struct inode **ihashtbl;
55static u_long ihash; /* size of hash table - 1 */
56static struct lwkt_token ufs_ihash_token;
57
58#define INOHASH(device, inum) (&ihashtbl[(minor(device) + (inum)) & ihash])
59
60/*
61 * Initialize inode hash table.
62 */
63void
64ufs_ihashinit(void)
65{
66 ihash = 16;
67 while (ihash < desiredvnodes)
68 ihash <<= 1;
69 ihashtbl = malloc(sizeof(void *) * ihash, M_UFSIHASH, M_WAITOK|M_ZERO);
70 --ihash;
71 lwkt_token_init(&ufs_ihash_token);
72}
73
74int
75ufs_uninit(struct vfsconf *vfc)
76{
77 lwkt_tokref ilock;
78
79 lwkt_gettoken(&ilock, &ufs_ihash_token);
80 if (ihashtbl)
81 free(ihashtbl, M_UFSIHASH);
82 lwkt_reltoken(&ilock);
83
84 return (0);
85}
86/*
87 * Use the device/inum pair to find the incore inode, and return a pointer
88 * to it. If it is in core, return it, even if it is locked.
89 */
90struct vnode *
91ufs_ihashlookup(dev_t dev, ino_t inum)
92{
93 struct inode *ip;
94 lwkt_tokref ilock;
95
96 lwkt_gettoken(&ilock, &ufs_ihash_token);
97 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
98 if (inum == ip->i_number && dev == ip->i_dev)
99 break;
100 }
101 lwkt_reltoken(&ilock);
102 if (ip)
103 return (ITOV(ip));
104 return (NULLVP);
105}
106
107/*
108 * Use the device/inum pair to find the incore inode, and return a pointer
109 * to it. If it is in core, but locked, wait for it.
110 *
111 * Note that the serializing tokens do not prevent other processes from
112 * playing with the data structure being protected while we are blocked.
113 * They do protect us from preemptive interrupts which might try to
114 * play with the protected data structure.
115 */
116struct vnode *
117ufs_ihashget(dev_t dev, ino_t inum)
118{
119 lwkt_tokref ilock;
120 struct inode *ip;
121 struct vnode *vp;
122
123 lwkt_gettoken(&ilock, &ufs_ihash_token);
124loop:
125 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
126 if (inum != ip->i_number || dev != ip->i_dev)
127 continue;
128 vp = ITOV(ip);
129 if (vget(vp, LK_EXCLUSIVE))
130 goto loop;
131 /*
132 * We must check to see if the inode has been ripped
133 * out from under us after blocking.
134 */
135 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
136 if (inum == ip->i_number && dev == ip->i_dev)
137 break;
138 }
139 if (ip == NULL || ITOV(ip) != vp) {
140 vput(vp);
141 goto loop;
142 }
143 lwkt_reltoken(&ilock);
144 return (vp);
145 }
146 lwkt_reltoken(&ilock);
147 return (NULL);
148}
149
150/*
151 * Check to see if an inode is in the hash table. This is used to interlock
152 * file free operations to ensure that the vnode is not reused due to a
153 * reallocate of its inode number before we have had a chance to recycle it.
154 */
155int
156ufs_ihashcheck(dev_t dev, ino_t inum)
157{
158 lwkt_tokref ilock;
159 struct inode *ip;
160
161 lwkt_gettoken(&ilock, &ufs_ihash_token);
162 for (ip = *INOHASH(dev, inum); ip; ip = ip->i_next) {
163 if (inum == ip->i_number && dev == ip->i_dev) {
164 if (ip->i_vnode) {
165 printf("conflict with vnode %p", ip->i_vnode);
166 if (TAILQ_FIRST(&ip->i_vnode->v_namecache))
167 printf(" ncp %s", TAILQ_FIRST(&ip->i_vnode->v_namecache)->nc_name);
168 printf("\n");
169 }
170 break;
171 }
172 }
173 lwkt_reltoken(&ilock);
174 return(ip ? 1 : 0);
175}
176
177/*
178 * Insert the inode into the hash table, and return it locked.
179 */
180int
181ufs_ihashins(struct inode *ip)
182{
183 struct inode **ipp;
184 struct inode *iq;
185 lwkt_tokref ilock;
186
187 KKASSERT((ip->i_flag & IN_HASHED) == 0);
188 lwkt_gettoken(&ilock, &ufs_ihash_token);
189 ipp = INOHASH(ip->i_dev, ip->i_number);
190 while ((iq = *ipp) != NULL) {
191 if (ip->i_dev == iq->i_dev && ip->i_number == iq->i_number) {
192 lwkt_reltoken(&ilock);
193 return(EBUSY);
194 }
195 ipp = &iq->i_next;
196 }
197 ip->i_next = NULL;
198 *ipp = ip;
199 ip->i_flag |= IN_HASHED;
200 lwkt_reltoken(&ilock);
201 return(0);
202}
203
204/*
205 * Remove the inode from the hash table.
206 */
207void
208ufs_ihashrem(struct inode *ip)
209{
210 lwkt_tokref ilock;
211 struct inode **ipp;
212 struct inode *iq;
213
214 lwkt_gettoken(&ilock, &ufs_ihash_token);
215 if (ip->i_flag & IN_HASHED) {
216 ipp = INOHASH(ip->i_dev, ip->i_number);
217 while ((iq = *ipp) != NULL) {
218 if (ip == iq)
219 break;
220 ipp = &iq->i_next;
221 }
222 KKASSERT(ip == iq);
223 *ipp = ip->i_next;
224 ip->i_next = NULL;
225 ip->i_flag &= ~IN_HASHED;
226 }
227 lwkt_reltoken(&ilock);
228}
229