kernel - Make UFS ihash table per-mount
[dragonfly.git] / sys / vfs / ufs / ufs_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. 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  */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/vnode.h>
42 #include <sys/malloc.h>
43 #include <sys/proc.h>
44 #include <sys/mount.h>
45
46 #include "quota.h"
47 #include "inode.h"
48 #include "ufs_extern.h"
49 #include "ufsmount.h"
50
51 static MALLOC_DEFINE(M_UFSIHASH, "UFS ihash", "UFS Inode hash tables");
52
53 #define INOHASH(ump, inum)      \
54         (&ump->um_ihashtbl[inum & ump->um_ihash])
55
56 /*
57  * Initialize inode hash table.
58  */
59 void
60 ufs_ihashinit(struct ufsmount *ump)
61 {
62         u_long target = desiredvnodes / 4 + 1;
63
64         ump->um_ihash = 16;
65         while (ump->um_ihash < target)
66                 ump->um_ihash <<= 1;
67         ump->um_ihashtbl = kmalloc(sizeof(void *) * ump->um_ihash, M_UFSIHASH,
68                                    M_WAITOK|M_ZERO);
69         --ump->um_ihash;
70 }
71
72 void
73 ufs_ihashuninit(struct ufsmount *ump)
74 {
75         if (ump->um_ihashtbl) {
76                 kfree(ump->um_ihashtbl, M_UFSIHASH);
77                 ump->um_ihashtbl = NULL;
78         }
79 }
80
81 /*
82  * Use the device/inum pair to find the incore inode, and return a pointer
83  * to it. If it is in core, return it, even if it is locked.
84  */
85 struct vnode *
86 ufs_ihashlookup(struct ufsmount *ump, cdev_t dev, ino_t inum)
87 {
88         struct inode *ip = NULL;
89
90         for (ip = *INOHASH(ump, inum); ip; ip = ip->i_next) {
91                 if (inum == ip->i_number && dev == ip->i_dev)
92                         break;
93         }
94         if (ip)
95                 return (ITOV(ip));
96         return (NULLVP);
97 }
98
99 /*
100  * Use the device/inum pair to find the incore inode, and return a pointer
101  * to it. If it is in core, but locked, wait for it.
102  *
103  * This subroutine may block.
104  */
105 struct vnode *
106 ufs_ihashget(struct ufsmount *ump, cdev_t dev, ino_t inum)
107 {
108         struct inode *ip;
109         struct vnode *vp;
110
111 loop:
112         for (ip = *INOHASH(ump, inum); ip; ip = ip->i_next) {
113                 if (inum != ip->i_number || dev != ip->i_dev)
114                         continue;
115                 vp = ITOV(ip);
116                 if (vget(vp, LK_EXCLUSIVE))
117                         goto loop;
118                 /*
119                  * We must check to see if the inode has been ripped
120                  * out from under us after blocking.
121                  */
122                 for (ip = *INOHASH(ump, inum); ip; ip = ip->i_next) {
123                         if (inum == ip->i_number && dev == ip->i_dev)
124                                 break;
125                 }
126                 if (ip == NULL || ITOV(ip) != vp) {
127                         vput(vp);
128                         goto loop;
129                 }
130                 return (vp);
131         }
132         return (NULL);
133 }
134
135 /*
136  * Check to see if an inode is in the hash table.  This is used to interlock
137  * file free operations to ensure that the vnode is not reused due to a
138  * reallocate of its inode number before we have had a chance to recycle it.
139  */
140 int
141 ufs_ihashcheck(struct ufsmount *ump, cdev_t dev, ino_t inum)
142 {
143         struct inode *ip;
144
145         for (ip = *INOHASH(ump, inum); ip; ip = ip->i_next) {
146                 if (inum == ip->i_number && dev == ip->i_dev)
147                         break;
148         }
149         return(ip ? 1 : 0);
150 }
151
152 /*
153  * Insert the inode into the hash table, and return it locked.
154  */
155 int
156 ufs_ihashins(struct ufsmount *ump, struct inode *ip)
157 {
158         struct inode **ipp;
159         struct inode *iq;
160
161         KKASSERT((ip->i_flag & IN_HASHED) == 0);
162         ipp = INOHASH(ump, ip->i_number);
163         while ((iq = *ipp) != NULL) {
164                 if (ip->i_dev == iq->i_dev && ip->i_number == iq->i_number) {
165                         return(EBUSY);
166                 }
167                 ipp = &iq->i_next;
168         }
169         ip->i_next = NULL;
170         *ipp = ip;
171         ip->i_flag |= IN_HASHED;
172         return(0);
173 }
174
175 /*
176  * Remove the inode from the hash table.
177  */
178 void
179 ufs_ihashrem(struct ufsmount *ump, struct inode *ip)
180 {
181         struct inode **ipp;
182         struct inode *iq;
183
184         if (ip->i_flag & IN_HASHED) {
185                 ipp = INOHASH(ump, ip->i_number);
186                 while ((iq = *ipp) != NULL) {
187                         if (ip == iq)
188                                 break;
189                         ipp = &iq->i_next;
190                 }
191                 KKASSERT(ip == iq);
192                 *ipp = ip->i_next;
193                 ip->i_next = NULL;
194                 ip->i_flag &= ~IN_HASHED;
195         }
196 }
197