xref: /netbsd-src/sys/fs/ntfs/ntfs_ihash.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: ntfs_ihash.c,v 1.10 2012/01/27 19:48:40 para 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.10 2012/01/27 19:48:40 para 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/proc.h>
44 #include <sys/mount.h>
45 #include <sys/mallocvar.h>
46 
47 #include <fs/ntfs/ntfs.h>
48 #include <fs/ntfs/ntfs_inode.h>
49 #include <fs/ntfs/ntfs_ihash.h>
50 
51 /*
52  * Structures associated with inode cacheing.
53  */
54 static LIST_HEAD(nthashhead, ntnode) *ntfs_nthashtbl;
55 static u_long	ntfs_nthash;		/* size of hash table - 1 */
56 #define	NTNOHASH(device, inum)	((minor(device) + (inum)) & ntfs_nthash)
57 static kmutex_t ntfs_nthash_lock;
58 kmutex_t ntfs_hashlock;
59 
60 /*
61  * Initialize inode hash table.
62  */
63 void
64 ntfs_nthashinit(void)
65 {
66 	mutex_init(&ntfs_hashlock, MUTEX_DEFAULT, IPL_NONE);
67 	mutex_init(&ntfs_nthash_lock, MUTEX_DEFAULT, IPL_NONE);
68 	ntfs_nthashtbl = hashinit(desiredvnodes, HASH_LIST, true, &ntfs_nthash);
69 }
70 
71 /*
72  * Reinitialize inode hash table.
73  */
74 
75 void
76 ntfs_nthashreinit(void)
77 {
78 	struct ntnode *ip;
79 	struct nthashhead *oldhash, *hash;
80 	u_long oldmask, mask, val;
81 	int i;
82 
83 	hash = hashinit(desiredvnodes, HASH_LIST, true, &mask);
84 
85 	mutex_enter(&ntfs_nthash_lock);
86 	oldhash = ntfs_nthashtbl;
87 	oldmask = ntfs_nthash;
88 	ntfs_nthashtbl = hash;
89 	ntfs_nthash = mask;
90 	for (i = 0; i <= oldmask; i++) {
91 		while ((ip = LIST_FIRST(&oldhash[i])) != NULL) {
92 			LIST_REMOVE(ip, i_hash);
93 			val = NTNOHASH(ip->i_dev, ip->i_number);
94 			LIST_INSERT_HEAD(&hash[val], ip, i_hash);
95 		}
96 	}
97 	mutex_exit(&ntfs_nthash_lock);
98 	hashdone(oldhash, HASH_LIST, oldmask);
99 }
100 
101 /*
102  * Free the inode hash table. Called from ntfs_done(), only needed
103  * on NetBSD.
104  */
105 void
106 ntfs_nthashdone(void)
107 {
108 	hashdone(ntfs_nthashtbl, HASH_LIST, ntfs_nthash);
109 	mutex_destroy(&ntfs_hashlock);
110 	mutex_destroy(&ntfs_nthash_lock);
111 }
112 
113 /*
114  * Use the device/inum pair to find the incore inode, and return a pointer
115  * to it. If it is in core, return it, even if it is locked.
116  */
117 struct ntnode *
118 ntfs_nthashlookup(dev_t dev, ino_t inum)
119 {
120 	struct ntnode *ip;
121 	struct nthashhead *ipp;
122 
123 	mutex_enter(&ntfs_nthash_lock);
124 	ipp = &ntfs_nthashtbl[NTNOHASH(dev, inum)];
125 	LIST_FOREACH(ip, ipp, i_hash) {
126 		if (inum == ip->i_number && dev == ip->i_dev)
127 			break;
128 	}
129 	mutex_exit(&ntfs_nthash_lock);
130 
131 	return (ip);
132 }
133 
134 /*
135  * Insert the ntnode into the hash table.
136  */
137 void
138 ntfs_nthashins(struct ntnode *ip)
139 {
140 	struct nthashhead *ipp;
141 
142 	mutex_enter(&ntfs_nthash_lock);
143 	ipp = &ntfs_nthashtbl[NTNOHASH(ip->i_dev, ip->i_number)];
144 	LIST_INSERT_HEAD(ipp, ip, i_hash);
145 	ip->i_flag |= IN_HASHED;
146 	mutex_exit(&ntfs_nthash_lock);
147 }
148 
149 /*
150  * Remove the inode from the hash table.
151  */
152 void
153 ntfs_nthashrem(struct ntnode *ip)
154 {
155 	mutex_enter(&ntfs_nthash_lock);
156 	if (ip->i_flag & IN_HASHED) {
157 		ip->i_flag &= ~IN_HASHED;
158 		LIST_REMOVE(ip, i_hash);
159 	}
160 	mutex_exit(&ntfs_nthash_lock);
161 }
162