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