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