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: releng/5.0/sys/fs/hpfs/hpfs_hash.c 93818 2002-04-04 21:03:38Z jhb $
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/mount.h>
43 #include <sys/malloc.h>
44 #include <sys/proc.h>
45 #include <sys/mutex.h>
46
47 #include <fs/hpfs/hpfs.h>
48
49 MALLOC_DEFINE(M_HPFSHASH, "HPFS hash", "HPFS node hash tables");
50
51 /*
52 * Structures associated with hpfsnode cacheing.
53 */
54 static LIST_HEAD(hphashhead, hpfsnode) *hpfs_hphashtbl;
55 static u_long hpfs_hphash; /* size of hash table - 1 */
56 #define HPNOHASH(dev, lsn) (&hpfs_hphashtbl[(minor(dev) + (lsn)) & hpfs_hphash])
57 static struct mtx hpfs_hphash_mtx;
58 struct lock hpfs_hphash_lock;
59
60 /*
61 * Initialize inode hash table.
62 */
63 void
64 hpfs_hphashinit()
65 {
66
67 lockinit (&hpfs_hphash_lock, PINOD, "hpfs_hphashlock", 0, 0);
68 hpfs_hphashtbl = hashinit(desiredvnodes, M_HPFSHASH, &hpfs_hphash);
69 mtx_init(&hpfs_hphash_mtx, "hpfs hphash", NULL, MTX_DEF);
70 }
71
72 /*
73 * Destroy inode hash table.
74 */
75 void
76 hpfs_hphashdestroy(void)
77 {
78
79 lockdestroy(&hpfs_hphash_lock);
80 mtx_destroy(&hpfs_hphash_mtx);
81 }
82
83 /*
84 * Use the device/inum pair to find the incore inode, and return a pointer
85 * to it. If it is in core, return it, even if it is locked.
86 */
87 struct hpfsnode *
88 hpfs_hphashlookup(dev, ino)
89 dev_t dev;
90 lsn_t ino;
91 {
92 struct hpfsnode *hp;
93
94 mtx_lock(&hpfs_hphash_mtx);
95 LIST_FOREACH(hp, HPNOHASH(dev, ino), h_hash)
96 if (ino == hp->h_no && dev == hp->h_dev)
97 break;
98 mtx_unlock(&hpfs_hphash_mtx);
99
100 return (hp);
101 }
102
103 #if 0
104 struct hpfsnode *
105 hpfs_hphashget(dev, ino)
106 dev_t dev;
107 lsn_t ino;
108 {
109 struct hpfsnode *hp;
110
111 loop:
112 mtx_lock(&hpfs_hphash_mtx);
113 LIST_FOREACH(hp, HPNOHASH(dev, ino), h_hash) {
114 if (ino == hp->h_no && dev == hp->h_dev) {
115 lockmgr(&hp->h_intlock, LK_EXCLUSIVE | LK_INTERLOCK,
116 &hpfs_hphash_slock, NULL);
117 return (hp);
118 }
119 }
120 mtx_unlock(&hpfs_hphash_mtx);
121 return (hp);
122 }
123 #endif
124
125 int
126 hpfs_hphashvget(dev, ino, flags, vpp, td)
127 dev_t dev;
128 lsn_t ino;
129 int flags;
130 struct vnode **vpp;
131 struct thread *td;
132 {
133 struct hpfsnode *hp;
134 struct vnode *vp;
135 int error;
136
137 *vpp = NULLVP;
138 loop:
139 mtx_lock(&hpfs_hphash_mtx);
140 LIST_FOREACH(hp, HPNOHASH(dev, ino), h_hash) {
141 if (ino == hp->h_no && dev == hp->h_dev) {
142 vp = HPTOV(hp);
143 mtx_lock(&vp->v_interlock);
144 mtx_unlock(&hpfs_hphash_mtx);
145 error = vget(vp, flags | LK_INTERLOCK, td);
146 if (error == ENOENT)
147 goto loop;
148 if (error)
149 return (error);
150 *vpp = vp;
151 return (0);
152 }
153 }
154 mtx_unlock(&hpfs_hphash_mtx);
155 return (0);
156 }
157
158 /*
159 * Insert the hpfsnode into the hash table.
160 */
161 void
162 hpfs_hphashins(hp)
163 struct hpfsnode *hp;
164 {
165 struct hphashhead *hpp;
166
167 mtx_lock(&hpfs_hphash_mtx);
168 hpp = HPNOHASH(hp->h_dev, hp->h_no);
169 hp->h_flag |= H_HASHED;
170 LIST_INSERT_HEAD(hpp, hp, h_hash);
171 mtx_unlock(&hpfs_hphash_mtx);
172 }
173
174 /*
175 * Remove the inode from the hash table.
176 */
177 void
178 hpfs_hphashrem(hp)
179 struct hpfsnode *hp;
180 {
181 mtx_lock(&hpfs_hphash_mtx);
182 if (hp->h_flag & H_HASHED) {
183 hp->h_flag &= ~H_HASHED;
184 LIST_REMOVE(hp, h_hash);
185 }
186 mtx_unlock(&hpfs_hphash_mtx);
187 }
Cache object: c8fe2e2126ed17232ccfdd670733d782
|