xref: /netbsd-src/sbin/fsck_lfs/vnode.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: vnode.c,v 1.8 2007/10/10 20:42:20 ad Exp $ */
2 /*-
3  * Copyright (c) 2003 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Konrad E. Schroder <perseant@hhhh.org>.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the NetBSD
20  *	Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/buf.h>
42 #include <sys/mount.h>
43 #include <sys/queue.h>
44 
45 #include <ufs/ufs/inode.h>
46 #include <ufs/ufs/ufsmount.h>
47 #define vnode uvnode
48 #include <ufs/lfs/lfs.h>
49 #undef vnode
50 
51 #include <assert.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <stdarg.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <util.h>
60 
61 #include "bufcache.h"
62 #include "vnode.h"
63 
64 struct uvnodelst vnodelist;
65 struct uvnodelst getvnodelist[VNODE_HASH_MAX];
66 struct vgrlst    vgrlist;
67 
68 int nvnodes;
69 
70 /* Convert between inode pointers and vnode pointers. */
71 #ifndef VTOI
72 #define	VTOI(vp)	((struct inode *)(vp)->v_data)
73 #endif
74 
75 /*
76  * Raw device uvnode ops
77  */
78 
79 int
80 raw_vop_strategy(struct ubuf * bp)
81 {
82 	if (bp->b_flags & B_READ) {
83 		return pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
84 		    dbtob(bp->b_blkno));
85 	} else {
86 		return pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
87 		    dbtob(bp->b_blkno));
88 	}
89 }
90 
91 int
92 raw_vop_bwrite(struct ubuf * bp)
93 {
94 	bp->b_flags &= ~(B_READ | B_DELWRI | B_DONE | B_ERROR);
95 	raw_vop_strategy(bp);
96 	brelse(bp, 0);
97 	return 0;
98 }
99 
100 int
101 raw_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
102 {
103 	*daddrp = lbn;
104 	return 0;
105 }
106 
107 /* Register a fs-specific vget function */
108 void
109 register_vget(void *fs, struct uvnode *func(void *, ino_t))
110 {
111 	struct vget_reg *vgr;
112 
113 	vgr = emalloc(sizeof(*vgr));
114 	vgr->vgr_fs = fs;
115 	vgr->vgr_func = func;
116 	LIST_INSERT_HEAD(&vgrlist, vgr, vgr_list);
117 }
118 
119 static struct uvnode *
120 VFS_VGET(void *fs, ino_t ino)
121 {
122 	struct vget_reg *vgr;
123 
124 	LIST_FOREACH(vgr, &vgrlist, vgr_list) {
125 		if (vgr->vgr_fs == fs)
126 			return vgr->vgr_func(fs, ino);
127 	}
128 	return NULL;
129 }
130 
131 void
132 vnode_destroy(struct uvnode *tossvp)
133 {
134 	struct ubuf *bp;
135 
136 	--nvnodes;
137 	LIST_REMOVE(tossvp, v_getvnodes);
138 	LIST_REMOVE(tossvp, v_mntvnodes);
139 	while ((bp = LIST_FIRST(&tossvp->v_dirtyblkhd)) != NULL) {
140 		LIST_REMOVE(bp, b_vnbufs);
141 		bremfree(bp);
142 		buf_destroy(bp);
143 	}
144 	while ((bp = LIST_FIRST(&tossvp->v_cleanblkhd)) != NULL) {
145 		LIST_REMOVE(bp, b_vnbufs);
146 		bremfree(bp);
147 		buf_destroy(bp);
148 	}
149 	free(VTOI(tossvp)->inode_ext.lfs);
150 	free(VTOI(tossvp)->i_din.ffs1_din);
151 	memset(VTOI(tossvp), 0, sizeof(struct inode));
152 	free(tossvp->v_data);
153 	memset(tossvp, 0, sizeof(*tossvp));
154 	free(tossvp);
155 }
156 
157 int hits, misses;
158 
159 /*
160  * Find a vnode in the cache; if not present, get it from the
161  * filesystem-specific vget routine.
162  */
163 struct uvnode *
164 vget(void *fs, ino_t ino)
165 {
166 	struct uvnode *vp, *tossvp;
167 	int hash;
168 
169 	/* Look in the uvnode cache */
170 	tossvp = NULL;
171 	hash = ((unsigned long)fs + ino) & (VNODE_HASH_MAX - 1);
172 	LIST_FOREACH(vp, &getvnodelist[hash], v_getvnodes) {
173 		if (vp->v_fs != fs)
174 			continue;
175 		if (VTOI(vp)->i_number == ino) {
176 			/* Move to the front of the list */
177 			LIST_REMOVE(vp, v_getvnodes);
178 			LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes);
179 			++hits;
180 			break;
181 		}
182 		if (LIST_EMPTY(&vp->v_dirtyblkhd) &&
183 		    vp->v_usecount == 0 &&
184 		    !(vp->v_uflag & VU_DIROP))
185 			tossvp = vp;
186 	}
187 	/* Don't let vnode list grow arbitrarily */
188 	if (nvnodes > VNODE_CACHE_SIZE && tossvp) {
189 		vnode_destroy(tossvp);
190 	}
191 	if (vp)
192 		return vp;
193 
194 	++misses;
195 	return VFS_VGET(fs, ino);
196 }
197 
198 void
199 vfs_init(void)
200 {
201 	int i;
202 
203 	nvnodes = 0;
204 	LIST_INIT(&vnodelist);
205 	for (i = 0; i < VNODE_HASH_MAX; i++)
206 		LIST_INIT(&getvnodelist[i]);
207 	LIST_INIT(&vgrlist);
208 }
209