1 /* $NetBSD: efs_ihash.c,v 1.3 2008/01/30 09:50:19 ad Exp $ */
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 * @(#)ufs_ihash.c 8.7 (Berkeley) 5/17/95
35 * This code shamelessly stolen from ufs/ufs/ufs_ihash.c.
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: efs_ihash.c,v 1.3 2008/01/30 09:50:19 ad Exp $");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/vnode.h>
45 #include <sys/mutex.h>
47 #include <miscfs/genfs/genfs_node.h>
49 #include <fs/efs/efs.h>
50 #include <fs/efs/efs_sb.h>
51 #include <fs/efs/efs_dir.h>
52 #include <fs/efs/efs_genfs.h>
53 #include <fs/efs/efs_mount.h>
54 #include <fs/efs/efs_extent.h>
55 #include <fs/efs/efs_dinode.h>
56 #include <fs/efs/efs_inode.h>
57 #include <fs/efs/efs_subr.h>
58 #include <fs/efs/efs_ihash.h>
60 MALLOC_DECLARE(M_EFSINO
);
63 * Structures associated with inode caching.
65 static LIST_HEAD(ihashhead
, efs_inode
) *ihashtbl
;
66 static u_long ihash
; /* size of hash table - 1 */
67 #define INOHASH(device, inum) (((device) + (inum)) & ihash)
69 static kmutex_t efs_ihash_lock
;
70 static kmutex_t efs_hashlock
;
72 MALLOC_DECLARE(M_EFSINO
);
75 * Initialize inode hash table.
81 mutex_init(&efs_hashlock
, MUTEX_DEFAULT
, IPL_NONE
);
82 mutex_init(&efs_ihash_lock
, MUTEX_DEFAULT
, IPL_NONE
);
83 ihashtbl
= hashinit(desiredvnodes
, HASH_LIST
, true, &ihash
);
87 * Reinitialize inode hash table.
93 struct efs_inode
*eip
;
94 struct ihashhead
*oldhash
, *hash
;
95 u_long oldmask
, mask
, val
;
98 hash
= hashinit(desiredvnodes
, HASH_LIST
, true, &mask
);
99 mutex_enter(&efs_ihash_lock
);
104 for (i
= 0; i
<= oldmask
; i
++) {
105 while ((eip
= LIST_FIRST(&oldhash
[i
])) != NULL
) {
106 LIST_REMOVE(eip
, ei_hash
);
107 val
= INOHASH(eip
->ei_dev
, eip
->ei_number
);
108 LIST_INSERT_HEAD(&hash
[val
], eip
, ei_hash
);
111 mutex_exit(&efs_ihash_lock
);
112 hashdone(oldhash
, HASH_LIST
, oldmask
);
116 * Free inode hash table.
122 hashdone(ihashtbl
, HASH_LIST
, ihash
);
123 mutex_destroy(&efs_hashlock
);
124 mutex_destroy(&efs_ihash_lock
);
128 * Use the device/inum pair to find the incore inode, and return a pointer
129 * to it. If it is in core, but locked, wait for it.
132 efs_ihashget(dev_t dev
, ino_t inum
, int flags
)
134 struct ihashhead
*ipp
;
135 struct efs_inode
*eip
;
139 mutex_enter(&efs_ihash_lock
);
140 ipp
= &ihashtbl
[INOHASH(dev
, inum
)];
141 LIST_FOREACH(eip
, ipp
, ei_hash
) {
142 if (inum
== eip
->ei_number
&& dev
== eip
->ei_dev
) {
145 mutex_exit(&efs_ihash_lock
);
147 mutex_enter(&vp
->v_interlock
);
148 mutex_exit(&efs_ihash_lock
);
149 if (vget(vp
, flags
| LK_INTERLOCK
))
155 mutex_exit(&efs_ihash_lock
);
160 * Insert the inode into the hash table, and return it locked.
163 efs_ihashins(struct efs_inode
*eip
)
165 struct ihashhead
*ipp
;
167 KASSERT(mutex_owned(&efs_hashlock
));
169 /* lock the inode, then put it on the appropriate hash list */
170 vlockmgr(&eip
->ei_vp
->v_lock
, LK_EXCLUSIVE
);
172 mutex_enter(&efs_ihash_lock
);
173 ipp
= &ihashtbl
[INOHASH(eip
->ei_dev
, eip
->ei_number
)];
174 LIST_INSERT_HEAD(ipp
, eip
, ei_hash
);
175 mutex_exit(&efs_ihash_lock
);
179 * Remove the inode from the hash table.
182 efs_ihashrem(struct efs_inode
*eip
)
185 mutex_enter(&efs_ihash_lock
);
186 LIST_REMOVE(eip
, ei_hash
);
187 mutex_exit(&efs_ihash_lock
);
191 * Grab the global inode hash lock.
197 mutex_enter(&efs_hashlock
);
201 * Release the global inode hash lock.
204 efs_ihashunlock(void)
207 mutex_exit(&efs_hashlock
);