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