xref: /netbsd-src/sbin/fsck_lfs/vnode.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /* $NetBSD: vnode.c,v 1.2 2003/04/02 10:39:28 fvdl 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 
60 #include "bufcache.h"
61 #include "vnode.h"
62 
63 struct uvnodelst vnodelist;
64 struct uvnodelst getvnodelist;
65 struct vgrlst    vgrlist;
66 
67 int nvnodes;
68 
69 /* Convert between inode pointers and vnode pointers. */
70 #ifndef VTOI
71 #define	VTOI(vp)	((struct inode *)(vp)->v_data)
72 #endif
73 
74 /*
75  * Raw device uvnode ops
76  */
77 
78 int
79 raw_vop_strategy(struct ubuf * bp)
80 {
81 	if (bp->b_flags & B_READ) {
82 		return pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
83 		    dbtob(bp->b_blkno));
84 	} else {
85 		return pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
86 		    dbtob(bp->b_blkno));
87 	}
88 }
89 
90 int
91 raw_vop_bwrite(struct ubuf * bp)
92 {
93 	bp->b_flags &= ~(B_READ | B_DELWRI | B_DONE | B_ERROR);
94 	raw_vop_strategy(bp);
95 	brelse(bp);
96 	return 0;
97 }
98 
99 int
100 raw_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
101 {
102 	*daddrp = lbn;
103 	return 0;
104 }
105 
106 /* Register a fs-specific vget function */
107 void
108 register_vget(void *fs, struct uvnode *func(void *, ino_t))
109 {
110 	struct vget_reg *vgr;
111 
112 	vgr = (struct vget_reg *)malloc(sizeof(*vgr));
113 	vgr->vgr_fs = fs;
114 	vgr->vgr_func = func;
115 	LIST_INSERT_HEAD(&vgrlist, vgr, vgr_list);
116 }
117 
118 static struct uvnode *
119 VFS_VGET(void *fs, ino_t ino)
120 {
121 	struct vget_reg *vgr;
122 
123 	LIST_FOREACH(vgr, &vgrlist, vgr_list) {
124 		if (vgr->vgr_fs == fs)
125 			return vgr->vgr_func(fs, ino);
126 	}
127 	return NULL;
128 }
129 
130 void
131 vnode_destroy(struct uvnode *tossvp)
132 {
133 	struct ubuf *bp;
134 
135 	--nvnodes;
136 	LIST_REMOVE(tossvp, v_getvnodes);
137 	LIST_REMOVE(tossvp, v_mntvnodes);
138 	LIST_FOREACH(bp, &tossvp->v_dirtyblkhd, b_vnbufs) {
139 		bremfree(bp);
140 		buf_destroy(bp);
141 	}
142 	LIST_FOREACH(bp, &tossvp->v_cleanblkhd, b_vnbufs) {
143 		bremfree(bp);
144 		buf_destroy(bp);
145 	}
146 	free(VTOI(tossvp)->inode_ext.lfs);
147 	free(VTOI(tossvp)->i_din.ffs1_din);
148 	memset(VTOI(tossvp), 0, sizeof(struct inode));
149 	free(tossvp->v_data);
150 	memset(tossvp, 0, sizeof(*tossvp));
151 	free(tossvp);
152 }
153 
154 /*
155  * Find a vnode in the cache; if not present, get it from the
156  * filesystem-specific vget routine.
157  */
158 struct uvnode *
159 vget(void *fs, ino_t ino)
160 {
161 	struct uvnode *vp, *tossvp;
162 
163 	/* Look in the uvnode cache */
164 	tossvp = NULL;
165 	LIST_FOREACH(vp, &getvnodelist, v_getvnodes) {
166 		if (vp->v_fs != fs)
167 			continue;
168 		if (VTOI(vp)->i_number == ino) {
169 			LIST_REMOVE(vp, v_getvnodes);
170 			LIST_INSERT_HEAD(&getvnodelist, vp, v_getvnodes);
171 			break;
172 		}
173 		if (LIST_EMPTY(&vp->v_dirtyblkhd) &&
174 		    vp->v_usecount == 0 &&
175 		    !(vp->v_flag & VDIROP))
176 			tossvp = vp;
177 	}
178 	/* Don't let vnode list grow arbitrarily */
179 	if (nvnodes > VNODE_CACHE_SIZE && tossvp) {
180 		vnode_destroy(tossvp);
181 	}
182 	if (vp)
183 		return vp;
184 
185 	return VFS_VGET(fs, ino);
186 }
187 
188 void
189 vfs_init(void)
190 {
191 	nvnodes = 0;
192 	LIST_INIT(&vnodelist);
193 	LIST_INIT(&getvnodelist);
194 	LIST_INIT(&vgrlist);
195 }
196