1 /* $NetBSD: ufs_ihash.c,v 1.18 2003/08/07 16:34:45 agc Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ufs_ihash.c 8.7 (Berkeley) 5/17/95
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ufs_ihash.c,v 1.18 2003/08/07 16:34:45 agc Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/vnode.h>
40 #include <sys/malloc.h>
41 #include <sys/proc.h>
42 #include <sys/lock.h>
43
44 #include <ufs/ufs/inode.h>
45 #include <ufs/ufs/ufs_extern.h>
46
47 /*
48 * Structures associated with inode cacheing.
49 */
50 LIST_HEAD(ihashhead, inode) *ihashtbl;
51 u_long ihash; /* size of hash table - 1 */
52 #define INOHASH(device, inum) (((device) + (inum)) & ihash)
53
54 struct lock ufs_hashlock;
55 struct simplelock ufs_ihash_slock;
56
57 /*
58 * Initialize inode hash table.
59 */
60 void
61 ufs_ihashinit()
62 {
63 lockinit(&ufs_hashlock, PINOD, "ufs_hashlock", 0, 0);
64 ihashtbl =
65 hashinit(desiredvnodes, HASH_LIST, M_UFSMNT, M_WAITOK, &ihash);
66 simple_lock_init(&ufs_ihash_slock);
67 }
68
69 /*
70 * Reinitialize inode hash table.
71 */
72
73 void
74 ufs_ihashreinit()
75 {
76 struct inode *ip;
77 struct ihashhead *oldhash, *hash;
78 u_long oldmask, mask, val;
79 int i;
80
81 hash = hashinit(desiredvnodes, HASH_LIST, M_UFSMNT, M_WAITOK, &mask);
82 simple_lock(&ufs_ihash_slock);
83 oldhash = ihashtbl;
84 oldmask = ihash;
85 ihashtbl = hash;
86 ihash = mask;
87 for (i = 0; i <= oldmask; i++) {
88 while ((ip = LIST_FIRST(&oldhash[i])) != NULL) {
89 LIST_REMOVE(ip, i_hash);
90 val = INOHASH(ip->i_dev, ip->i_number);
91 LIST_INSERT_HEAD(&hash[val], ip, i_hash);
92 }
93 }
94 simple_unlock(&ufs_ihash_slock);
95 hashdone(oldhash, M_UFSMNT);
96 }
97
98 /*
99 * Free inode hash table.
100 */
101 void
102 ufs_ihashdone()
103 {
104 hashdone(ihashtbl, M_UFSMNT);
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, return it, even if it is locked.
110 */
111 struct vnode *
112 ufs_ihashlookup(dev, inum)
113 dev_t dev;
114 ino_t inum;
115 {
116 struct inode *ip;
117 struct ihashhead *ipp;
118
119 simple_lock(&ufs_ihash_slock);
120 ipp = &ihashtbl[INOHASH(dev, inum)];
121 LIST_FOREACH(ip, ipp, i_hash) {
122 if (inum == ip->i_number && dev == ip->i_dev)
123 break;
124 }
125 simple_unlock(&ufs_ihash_slock);
126 if (ip)
127 return (ITOV(ip));
128 return (NULLVP);
129 }
130
131 /*
132 * Use the device/inum pair to find the incore inode, and return a pointer
133 * to it. If it is in core, but locked, wait for it.
134 */
135 struct vnode *
136 ufs_ihashget(dev, inum, flags)
137 dev_t dev;
138 ino_t inum;
139 int flags;
140 {
141 struct ihashhead *ipp;
142 struct inode *ip;
143 struct vnode *vp;
144
145 loop:
146 simple_lock(&ufs_ihash_slock);
147 ipp = &ihashtbl[INOHASH(dev, inum)];
148 LIST_FOREACH(ip, ipp, i_hash) {
149 if (inum == ip->i_number && dev == ip->i_dev) {
150 vp = ITOV(ip);
151 simple_lock(&vp->v_interlock);
152 simple_unlock(&ufs_ihash_slock);
153 if (vget(vp, flags | LK_INTERLOCK))
154 goto loop;
155 return (vp);
156 }
157 }
158 simple_unlock(&ufs_ihash_slock);
159 return (NULL);
160 }
161
162 /*
163 * Insert the inode into the hash table, and return it locked.
164 */
165 void
166 ufs_ihashins(ip)
167 struct inode *ip;
168 {
169 struct ihashhead *ipp;
170
171 /* lock the inode, then put it on the appropriate hash list */
172 lockmgr(&ip->i_vnode->v_lock, LK_EXCLUSIVE, NULL);
173
174 simple_lock(&ufs_ihash_slock);
175 ipp = &ihashtbl[INOHASH(ip->i_dev, ip->i_number)];
176 LIST_INSERT_HEAD(ipp, ip, i_hash);
177 simple_unlock(&ufs_ihash_slock);
178 }
179
180 /*
181 * Remove the inode from the hash table.
182 */
183 void
184 ufs_ihashrem(ip)
185 struct inode *ip;
186 {
187 simple_lock(&ufs_ihash_slock);
188 LIST_REMOVE(ip, i_hash);
189 simple_unlock(&ufs_ihash_slock);
190 }
Cache object: e1e98861729b888f20c46619306429f1
|