filecore_node.c revision 1.1.4.2 1 /* $NetBSD: filecore_node.c,v 1.1.4.2 2004/08/03 10:52:24 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 1982, 1986, 1989, 1994
5 * The Regents of the University of California.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
19 *
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
30 * SUCH DAMAGE.
31 *
32 * filecore_node.c 1.0 1998/6/4
33 */
34
35 /*-
36 * Copyright (c) 1998 Andrew McMurry
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
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.
53 *
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
64 * SUCH DAMAGE.
65 *
66 * filecore_node.c 1.0 1998/6/4
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: filecore_node.c,v 1.1.4.2 2004/08/03 10:52:24 skrll Exp $");
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/mount.h>
75 #include <sys/proc.h>
76 #include <sys/file.h>
77 #include <sys/buf.h>
78 #include <sys/vnode.h>
79 #include <sys/namei.h>
80 #include <sys/kernel.h>
81 #include <sys/malloc.h>
82 #include <sys/pool.h>
83 #include <sys/stat.h>
84
85 #include <fs/filecorefs/filecore.h>
86 #include <fs/filecorefs/filecore_extern.h>
87 #include <fs/filecorefs/filecore_node.h>
88 #include <fs/filecorefs/filecore_mount.h>
89
90 /*
91 * Structures associated with filecore_node caching.
92 */
93 LIST_HEAD(ihashhead, filecore_node) *filecorehashtbl;
94 u_long filecorehash;
95 #define INOHASH(device, inum) (((device) + ((inum)>>12)) & filecorehash)
96 struct simplelock filecore_ihash_slock;
97
98 POOL_INIT(filecore_node_pool, sizeof(struct filecore_node), 0, 0, 0,
99 "filecrnopl", &pool_allocator_nointr);
100
101 extern int prtactive; /* 1 => print out reclaim of active vnodes */
102
103 /*
104 * Initialize hash links for inodes and dnodes.
105 */
106 void
107 filecore_init()
108 {
109 #ifdef _LKM
110 malloc_type_attach(M_FILECOREMNT);
111 pool_init(&filecore_node_pool, sizeof(struct filecore_node), 0, 0, 0,
112 "filecrnopl", &pool_allocator_nointr);
113 #endif
114 filecorehashtbl = hashinit(desiredvnodes, HASH_LIST, M_FILECOREMNT,
115 M_WAITOK, &filecorehash);
116 simple_lock_init(&filecore_ihash_slock);
117 }
118
119 /*
120 * Reinitialize inode hash table.
121 */
122 void
123 filecore_reinit()
124 {
125 struct filecore_node *ip;
126 struct ihashhead *oldhash, *hash;
127 u_long oldmask, mask, val;
128 int i;
129
130 hash = hashinit(desiredvnodes, HASH_LIST, M_FILECOREMNT, M_WAITOK,
131 &mask);
132
133 simple_lock(&filecore_ihash_slock);
134 oldhash = filecorehashtbl;
135 oldmask = filecorehash;
136 filecorehashtbl = hash;
137 filecorehash = mask;
138 for (i = 0; i <= oldmask; i++) {
139 while ((ip = LIST_FIRST(&oldhash[i])) != NULL) {
140 LIST_REMOVE(ip, i_hash);
141 val = INOHASH(ip->i_dev, ip->i_number);
142 LIST_INSERT_HEAD(&hash[val], ip, i_hash);
143 }
144 }
145 simple_unlock(&filecore_ihash_slock);
146 hashdone(oldhash, M_FILECOREMNT);
147 }
148
149 /*
150 * Destroy node pool and hash table.
151 */
152 void
153 filecore_done()
154 {
155 hashdone(filecorehashtbl, M_FILECOREMNT);
156 #ifdef _LKM
157 pool_destroy(&filecore_node_pool);
158 malloc_type_detach(M_FILECOREMNT);
159 #endif
160 }
161
162 /*
163 * Use the device/inum pair to find the incore inode, and return a pointer
164 * to it. If it is in core, but locked, wait for it.
165 */
166 struct vnode *
167 filecore_ihashget(dev, inum, l)
168 dev_t dev;
169 ino_t inum;
170 struct lwp *l;
171 {
172 struct filecore_node *ip;
173 struct vnode *vp;
174
175 loop:
176 simple_lock(&filecore_ihash_slock);
177 LIST_FOREACH(ip, &filecorehashtbl[INOHASH(dev, inum)], i_hash) {
178 if (inum == ip->i_number && dev == ip->i_dev) {
179 vp = ITOV(ip);
180 simple_lock(&vp->v_interlock);
181 simple_unlock(&filecore_ihash_slock);
182 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, l))
183 goto loop;
184 return (vp);
185 }
186 }
187 simple_unlock(&filecore_ihash_slock);
188 return (NULL);
189 }
190
191 /*
192 * Insert the inode into the hash table, and return it locked.
193 */
194 void
195 filecore_ihashins(ip)
196 struct filecore_node *ip;
197 {
198 struct ihashhead *ipp;
199 struct vnode *vp;
200
201 simple_lock(&filecore_ihash_slock);
202 ipp = &filecorehashtbl[INOHASH(ip->i_dev, ip->i_number)];
203 LIST_INSERT_HEAD(ipp, ip, i_hash);
204 simple_unlock(&filecore_ihash_slock);
205
206 vp = ip->i_vnode;
207 lockmgr(&vp->v_lock, LK_EXCLUSIVE, &vp->v_interlock);
208 }
209
210 /*
211 * Remove the inode from the hash table.
212 */
213 void
214 filecore_ihashrem(ip)
215 struct filecore_node *ip;
216 {
217 simple_lock(&filecore_ihash_slock);
218 LIST_REMOVE(ip, i_hash);
219 simple_unlock(&filecore_ihash_slock);
220 }
221
222 /*
223 * Last reference to an inode, write the inode out and if necessary,
224 * truncate and deallocate the file.
225 */
226 int
227 filecore_inactive(v)
228 void *v;
229 {
230 struct vop_inactive_args /* {
231 struct vnode *a_vp;
232 struct proc *a_p;
233 } */ *ap = v;
234 struct vnode *vp = ap->a_vp;
235 struct lwp *l = ap->a_l;
236 struct filecore_node *ip = VTOI(vp);
237 int error = 0;
238
239 if (prtactive && vp->v_usecount != 0)
240 vprint("filecore_inactive: pushing active", vp);
241
242 ip->i_flag = 0;
243 VOP_UNLOCK(vp, 0);
244 /*
245 * If we are done with the inode, reclaim it
246 * so that it can be reused immediately.
247 */
248 if (filecore_staleinode(ip))
249 vrecycle(vp, (struct simplelock *)0, l);
250 return error;
251 }
252
253 /*
254 * Reclaim an inode so that it can be used for other purposes.
255 */
256 int
257 filecore_reclaim(v)
258 void *v;
259 {
260 struct vop_reclaim_args /* {
261 struct vnode *a_vp;
262 struct proc *a_p;
263 } */ *ap = v;
264 struct vnode *vp = ap->a_vp;
265 struct filecore_node *ip = VTOI(vp);
266
267 if (prtactive && vp->v_usecount != 0)
268 vprint("filecore_reclaim: pushing active", vp);
269 /*
270 * Remove the inode from its hash chain.
271 */
272 filecore_ihashrem(ip);
273 /*
274 * Purge old data structures associated with the inode.
275 */
276 cache_purge(vp);
277 if (ip->i_devvp) {
278 vrele(ip->i_devvp);
279 ip->i_devvp = 0;
280 }
281 pool_put(&filecore_node_pool, vp->v_data);
282 vp->v_data = NULL;
283 return (0);
284 }
285