xref: /openbsd-src/sys/ntfs/ntfs_ihash.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: ntfs_ihash.c,v 1.3 2003/06/02 23:28:20 millert Exp $	*/
2 /*	$NetBSD: ntfs_ihash.c,v 1.1 2002/12/23 17:38:32 jdolecek Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1991, 1993, 1995
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)ufs_ihash.c	8.7 (Berkeley) 5/17/95
33  * Id: ntfs_ihash.c,v 1.5 1999/05/12 09:42:58 semenu Exp
34  */
35 
36 #include <sys/cdefs.h>
37 #ifdef __KERNEL_RCSID
38 __KERNEL_RCSID(0, "$NetBSD: ntfs_ihash.c,v 1.1 2002/12/23 17:38:32 jdolecek Exp $");
39 #endif
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/vnode.h>
46 #include <sys/malloc.h>
47 #include <sys/proc.h>
48 #include <sys/mount.h>
49 
50 #if defined(__FreeBSD__) || defined(__NetBSD__)
51 #include <fs/ntfs/ntfs.h>
52 #include <fs/ntfs/ntfs_inode.h>
53 #include <fs/ntfs/ntfs_ihash.h>
54 #else
55 #include <ntfs/ntfs.h>
56 #include <ntfs/ntfs_inode.h>
57 #include <ntfs/ntfs_ihash.h>
58 #endif
59 
60 #ifdef MALLOC_DEFINE
61 MALLOC_DEFINE(M_NTFSNTHASH, "NTFS nthash", "NTFS ntnode hash tables");
62 #endif
63 
64 /*
65  * Structures associated with inode cacheing.
66  */
67 static LIST_HEAD(nthashhead, ntnode) *ntfs_nthashtbl;
68 static u_long	ntfs_nthash;		/* size of hash table - 1 */
69 #define	NTNOHASH(device, inum)	((minor(device) + (inum)) & ntfs_nthash)
70 #ifndef NULL_SIMPLELOCKS
71 static struct simplelock ntfs_nthash_slock;
72 #endif
73 struct lock ntfs_hashlock;
74 
75 /*
76  * Initialize inode hash table.
77  */
78 void
79 ntfs_nthashinit()
80 {
81 	lockinit(&ntfs_hashlock, PINOD, "ntfs_nthashlock", 0, 0);
82 	ntfs_nthashtbl = HASHINIT(desiredvnodes, M_NTFSNTHASH, M_WAITOK,
83 	    &ntfs_nthash);
84 	simple_lock_init(&ntfs_nthash_slock);
85 }
86 
87 #ifdef __NetBSD__
88 /*
89  * Reinitialize inode hash table.
90  */
91 
92 void
93 ntfs_nthashreinit()
94 {
95 	struct ntnode *ip;
96 	struct nthashhead *oldhash, *hash;
97 	u_long oldmask, mask, val;
98 	int i;
99 
100 	hash = HASHINIT(desiredvnodes, M_NTFSNTHASH, M_WAITOK, &mask);
101 
102 	simple_lock(&ntfs_nthash_slock);
103 	oldhash = ntfs_nthashtbl;
104 	oldmask = ntfs_nthash;
105 	ntfs_nthashtbl = hash;
106 	ntfs_nthash = mask;
107 	for (i = 0; i <= oldmask; i++) {
108 		while ((ip = LIST_FIRST(&oldhash[i])) != NULL) {
109 			LIST_REMOVE(ip, i_hash);
110 			val = NTNOHASH(ip->i_dev, ip->i_number);
111 			LIST_INSERT_HEAD(&hash[val], ip, i_hash);
112 		}
113 	}
114 	simple_unlock(&ntfs_nthash_slock);
115 	hashdone(oldhash, M_NTFSNTHASH);
116 }
117 
118 /*
119  * Free the inode hash table. Called from ntfs_done(), only needed
120  * on NetBSD.
121  */
122 void
123 ntfs_nthashdone()
124 {
125 	hashdone(ntfs_nthashtbl, M_NTFSNTHASH);
126 }
127 #endif
128 
129 /*
130  * Use the device/inum pair to find the incore inode, and return a pointer
131  * to it. If it is in core, return it, even if it is locked.
132  */
133 struct ntnode *
134 ntfs_nthashlookup(dev, inum)
135 	dev_t dev;
136 	ino_t inum;
137 {
138 	struct ntnode *ip;
139 	struct nthashhead *ipp;
140 
141 	simple_lock(&ntfs_nthash_slock);
142 	ipp = &ntfs_nthashtbl[NTNOHASH(dev, inum)];
143 	LIST_FOREACH(ip, ipp, i_hash) {
144 		if (inum == ip->i_number && dev == ip->i_dev)
145 			break;
146 	}
147 	simple_unlock(&ntfs_nthash_slock);
148 
149 	return (ip);
150 }
151 
152 /*
153  * Insert the ntnode into the hash table.
154  */
155 void
156 ntfs_nthashins(ip)
157 	struct ntnode *ip;
158 {
159 	struct nthashhead *ipp;
160 
161 	simple_lock(&ntfs_nthash_slock);
162 	ipp = &ntfs_nthashtbl[NTNOHASH(ip->i_dev, ip->i_number)];
163 	LIST_INSERT_HEAD(ipp, ip, i_hash);
164 	ip->i_flag |= IN_HASHED;
165 	simple_unlock(&ntfs_nthash_slock);
166 }
167 
168 /*
169  * Remove the inode from the hash table.
170  */
171 void
172 ntfs_nthashrem(ip)
173 	struct ntnode *ip;
174 {
175 	simple_lock(&ntfs_nthash_slock);
176 	if (ip->i_flag & IN_HASHED) {
177 		ip->i_flag &= ~IN_HASHED;
178 		LIST_REMOVE(ip, i_hash);
179 	}
180 	simple_unlock(&ntfs_nthash_slock);
181 }
182