1 /* $NetBSD: hfs_nhash.c,v 1.8 2008/05/05 17:11:16 ad Exp $ */
4 * Copyright (c) 2005, 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1982, 1986, 1989, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: hfs_nhash.c,v 1.8 2008/05/05 17:11:16 ad Exp $");
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/vmmeter.h>
70 #include <sys/vnode.h>
73 #include <sys/mount.h>
74 #include <sys/namei.h>
76 #include <sys/dirent.h>
77 #include <sys/msgbuf.h>
79 #include <fs/hfs/hfs.h>
81 LIST_HEAD(nhashhead
, hfsnode
) *nhashtbl
;
82 u_long nhash
; /* size of hash table - 1 */
83 #define HNOHASH(device, cnid, fork) (((device) + (cnid) + (fork)) & nhash)
85 kmutex_t hfs_hashlock
;
86 kmutex_t hfs_nhash_lock
;
89 * Initialize hfsnode hash table.
95 nhashtbl
= hashinit(desiredvnodes
, HASH_LIST
, true, &nhash
);
96 mutex_init(&hfs_nhash_lock
, MUTEX_DEFAULT
, IPL_NONE
);
97 mutex_init(&hfs_hashlock
, MUTEX_DEFAULT
, IPL_NONE
);
101 * Free hfsnode hash table.
107 hashdone(nhashtbl
, HASH_LIST
, nhash
);
108 mutex_destroy(&hfs_nhash_lock
);
109 mutex_destroy(&hfs_hashlock
);
113 * Use the device/inum pair to find the incore inode, and return a pointer
114 * to it. If it is in core, but locked, wait for it.
117 hfs_nhashget(dev_t dev
, hfs_cnid_t cnid
, uint8_t fork
, int flags
)
120 struct nhashhead
*hpp
;
124 mutex_enter(&hfs_nhash_lock
);
125 hpp
= &nhashtbl
[HNOHASH(dev
, cnid
, fork
)];
126 LIST_FOREACH(hp
, hpp
, h_hash
) {
127 if (cnid
== hp
->h_rec
.u
.cnid
&& dev
== hp
->h_dev
) {
130 mutex_exit(&hfs_nhash_lock
);
132 mutex_enter(&vp
->v_interlock
);
133 mutex_exit(&hfs_nhash_lock
);
134 if (vget(vp
, flags
| LK_INTERLOCK
))
140 mutex_exit(&hfs_nhash_lock
);
145 * Insert the hfsnode into the hash table, and return it locked.
148 hfs_nhashinsert(struct hfsnode
*hp
)
150 struct nhashhead
*hpp
;
152 /* lock the inode, then put it on the appropriate hash list */
153 vlockmgr(&hp
->h_vnode
->v_lock
, LK_EXCLUSIVE
);
155 mutex_enter(&hfs_nhash_lock
);
156 hpp
= &nhashtbl
[HNOHASH(hp
->h_dev
, hp
->h_rec
.u
.cnid
, hp
->h_fork
)];
157 LIST_INSERT_HEAD(hpp
, hp
, h_hash
);
158 mutex_exit(&hfs_nhash_lock
);
162 * Remove the inode from the hash table.
165 hfs_nhashremove(struct hfsnode
*hp
)
168 mutex_enter(&hfs_nhash_lock
);
169 LIST_REMOVE(hp
, h_hash
);
170 mutex_exit(&hfs_nhash_lock
);