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