xref: /dragonfly/sys/vfs/hpfs/hpfs_hash.c (revision 9dbf638f)
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/fs/hpfs/hpfs_hash.c,v 1.1 1999/12/09 19:09:58 semenu Exp $
35  * $DragonFly: src/sys/vfs/hpfs/hpfs_hash.c,v 1.7 2003/08/15 07:26:15 dillon Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/vnode.h>
43 #include <sys/mount.h>
44 #include <sys/malloc.h>
45 #include <sys/proc.h>
46 
47 #include "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 #ifndef NULL_SIMPLELOCKS
58 static struct lwkt_token hpfs_hphash_token;
59 #endif
60 struct lock hpfs_hphash_lock;
61 
62 /*
63  * Initialize inode hash table.
64  */
65 void
66 hpfs_hphashinit()
67 {
68 
69 	lockinit (&hpfs_hphash_lock, 0, "hpfs_hphashlock", 0, 0);
70 	hpfs_hphashtbl = HASHINIT(desiredvnodes, M_HPFSHASH, M_WAITOK,
71 	    &hpfs_hphash);
72 	lwkt_inittoken(&hpfs_hphash_token);
73 }
74 
75 /*
76  * Use the device/inum pair to find the incore inode, and return a pointer
77  * to it. If it is in core, return it, even if it is locked.
78  */
79 struct hpfsnode *
80 hpfs_hphashlookup(dev, ino)
81 	dev_t dev;
82 	lsn_t ino;
83 {
84 	struct hpfsnode *hp;
85 
86 	lwkt_gettoken(&hpfs_hphash_token);
87 	for (hp = HPNOHASH(dev, ino)->lh_first; hp; hp = hp->h_hash.le_next)
88 		if (ino == hp->h_no && dev == hp->h_dev)
89 			break;
90 	lwkt_reltoken(&hpfs_hphash_token);
91 
92 	return (hp);
93 }
94 
95 #if 0
96 struct hpfsnode *
97 hpfs_hphashget(dev, ino)
98 	dev_t dev;
99 	lsn_t ino;
100 {
101 	struct hpfsnode *hp;
102 
103 loop:
104 	lwkt_gettoken(&hpfs_hphash_token);
105 	for (hp = HPNOHASH(dev, ino)->lh_first; hp; hp = hp->h_hash.le_next) {
106 		if (ino == hp->h_no && dev == hp->h_dev) {
107 			LOCKMGR(&hp->h_intlock, LK_EXCLUSIVE | LK_INTERLOCK, &hpfs_hphash_token, NULL);
108 			return (hp);
109 		}
110 	}
111 	lwkt_reltoken(&hpfs_hphash_token);
112 	return (hp);
113 }
114 #endif
115 
116 struct vnode *
117 hpfs_hphashvget(dev, ino, td)
118 	dev_t dev;
119 	lsn_t ino;
120 	struct thread *td;
121 {
122 	struct hpfsnode *hp;
123 	struct vnode *vp;
124 	int gen;
125 
126 	gen = lwkt_gettoken(&hpfs_hphash_token);
127 loop:
128 	for (hp = HPNOHASH(dev, ino)->lh_first; hp; hp = hp->h_hash.le_next) {
129 		if (ino == hp->h_no && dev == hp->h_dev) {
130 			vp = HPTOV(hp);
131 			lwkt_gettoken (&vp->v_interlock);
132 			if (lwkt_gentoken(&hpfs_hphash_token, &gen))
133 				goto loop;
134 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
135 				gen = lwkt_regettoken(&hpfs_hphash_token);
136 				goto loop;
137 			}
138 			lwkt_reltoken(&hpfs_hphash_token);
139 			return (vp);
140 		}
141 	}
142 	lwkt_reltoken(&hpfs_hphash_token);
143 	return (NULLVP);
144 }
145 
146 /*
147  * Insert the hpfsnode into the hash table.
148  */
149 void
150 hpfs_hphashins(hp)
151 	struct hpfsnode *hp;
152 {
153 	struct hphashhead *hpp;
154 
155 	lwkt_gettoken(&hpfs_hphash_token);
156 	hpp = HPNOHASH(hp->h_dev, hp->h_no);
157 	hp->h_flag |= H_HASHED;
158 	LIST_INSERT_HEAD(hpp, hp, h_hash);
159 	lwkt_reltoken(&hpfs_hphash_token);
160 }
161 
162 /*
163  * Remove the inode from the hash table.
164  */
165 void
166 hpfs_hphashrem(hp)
167 	struct hpfsnode *hp;
168 {
169 	lwkt_gettoken(&hpfs_hphash_token);
170 	if (hp->h_flag & H_HASHED) {
171 		hp->h_flag &= ~H_HASHED;
172 		LIST_REMOVE(hp, h_hash);
173 #ifdef DIAGNOSTIC
174 		hp->h_hash.le_next = NULL;
175 		hp->h_hash.le_prev = NULL;
176 #endif
177 	}
178 	lwkt_reltoken(&hpfs_hphash_token);
179 }
180