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