1 /* $NetBSD: filecore_node.c,v 1.18 2009/03/14 15:36:21 dsl Exp $ */
4 * Copyright (c) 1982, 1986, 1989, 1994
5 * The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * filecore_node.c 1.0 1998/6/4
36 * Copyright (c) 1998 Andrew McMurry
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * filecore_node.c 1.0 1998/6/4
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: filecore_node.c,v 1.18 2009/03/14 15:36:21 dsl Exp $");
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/mount.h>
78 #include <sys/vnode.h>
79 #include <sys/namei.h>
80 #include <sys/kernel.h>
81 #include <sys/malloc.h>
84 #include <sys/simplelock.h>
86 #include <fs/filecorefs/filecore.h>
87 #include <fs/filecorefs/filecore_extern.h>
88 #include <fs/filecorefs/filecore_node.h>
89 #include <fs/filecorefs/filecore_mount.h>
92 * Structures associated with filecore_node caching.
94 LIST_HEAD(ihashhead
, filecore_node
) *filecorehashtbl
;
96 #define INOHASH(device, inum) (((device) + ((inum)>>12)) & filecorehash)
97 struct simplelock filecore_ihash_slock
;
99 struct pool filecore_node_pool
;
101 extern int prtactive
; /* 1 => print out reclaim of active vnodes */
104 * Initialize hash links for inodes and dnodes.
110 malloc_type_attach(M_FILECOREMNT
);
111 malloc_type_attach(M_FILECORETMP
);
112 pool_init(&filecore_node_pool
, sizeof(struct filecore_node
), 0, 0, 0,
113 "filecrnopl", &pool_allocator_nointr
, IPL_NONE
);
114 filecorehashtbl
= hashinit(desiredvnodes
, HASH_LIST
, true,
116 simple_lock_init(&filecore_ihash_slock
);
120 * Reinitialize inode hash table.
123 filecore_reinit(void)
125 struct filecore_node
*ip
;
126 struct ihashhead
*oldhash
, *hash
;
127 u_long oldmask
, mask
, val
;
130 hash
= hashinit(desiredvnodes
, HASH_LIST
, true, &mask
);
132 simple_lock(&filecore_ihash_slock
);
133 oldhash
= filecorehashtbl
;
134 oldmask
= filecorehash
;
135 filecorehashtbl
= hash
;
137 for (i
= 0; i
<= oldmask
; i
++) {
138 while ((ip
= LIST_FIRST(&oldhash
[i
])) != NULL
) {
139 LIST_REMOVE(ip
, i_hash
);
140 val
= INOHASH(ip
->i_dev
, ip
->i_number
);
141 LIST_INSERT_HEAD(&hash
[val
], ip
, i_hash
);
144 simple_unlock(&filecore_ihash_slock
);
145 hashdone(oldhash
, HASH_LIST
, oldmask
);
149 * Destroy node pool and hash table.
154 hashdone(filecorehashtbl
, HASH_LIST
, filecorehash
);
155 pool_destroy(&filecore_node_pool
);
156 malloc_type_detach(M_FILECORETMP
);
157 malloc_type_detach(M_FILECOREMNT
);
161 * Use the device/inum pair to find the incore inode, and return a pointer
162 * to it. If it is in core, but locked, wait for it.
165 filecore_ihashget(dev_t dev
, ino_t inum
)
167 struct filecore_node
*ip
;
171 simple_lock(&filecore_ihash_slock
);
172 LIST_FOREACH(ip
, &filecorehashtbl
[INOHASH(dev
, inum
)], i_hash
) {
173 if (inum
== ip
->i_number
&& dev
== ip
->i_dev
) {
175 mutex_enter(&vp
->v_interlock
);
176 simple_unlock(&filecore_ihash_slock
);
177 if (vget(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
))
182 simple_unlock(&filecore_ihash_slock
);
187 * Insert the inode into the hash table, and return it locked.
190 filecore_ihashins(struct filecore_node
*ip
)
192 struct ihashhead
*ipp
;
195 simple_lock(&filecore_ihash_slock
);
196 ipp
= &filecorehashtbl
[INOHASH(ip
->i_dev
, ip
->i_number
)];
197 LIST_INSERT_HEAD(ipp
, ip
, i_hash
);
198 simple_unlock(&filecore_ihash_slock
);
201 vlockmgr(&vp
->v_lock
, LK_EXCLUSIVE
);
205 * Remove the inode from the hash table.
208 filecore_ihashrem(struct filecore_node
*ip
)
210 simple_lock(&filecore_ihash_slock
);
211 LIST_REMOVE(ip
, i_hash
);
212 simple_unlock(&filecore_ihash_slock
);
216 * Last reference to an inode, write the inode out and if necessary,
217 * truncate and deallocate the file.
220 filecore_inactive(void *v
)
222 struct vop_inactive_args
/* {
226 struct vnode
*vp
= ap
->a_vp
;
227 struct filecore_node
*ip
= VTOI(vp
);
231 * If we are done with the inode, reclaim it
232 * so that it can be reused immediately.
235 *ap
->a_recycle
= (filecore_staleinode(ip
) != 0);
241 * Reclaim an inode so that it can be used for other purposes.
244 filecore_reclaim(void *v
)
246 struct vop_reclaim_args
/* {
250 struct vnode
*vp
= ap
->a_vp
;
251 struct filecore_node
*ip
= VTOI(vp
);
253 if (prtactive
&& vp
->v_usecount
> 1)
254 vprint("filecore_reclaim: pushing active", vp
);
256 * Remove the inode from its hash chain.
258 filecore_ihashrem(ip
);
260 * Purge old data structures associated with the inode.
267 genfs_node_destroy(vp
);
268 pool_put(&filecore_node_pool
, vp
->v_data
);