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