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