fdfs.c revision 1.1 1 1.1 perseant /* $NetBSD: fdfs.c,v 1.1 2006/03/30 19:10:13 perseant Exp $ */
2 1.1 perseant
3 1.1 perseant /*-
4 1.1 perseant * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 1.1 perseant * All rights reserved.
6 1.1 perseant *
7 1.1 perseant * This code is derived from software contributed to The NetBSD Foundation
8 1.1 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 1.1 perseant *
10 1.1 perseant * Redistribution and use in source and binary forms, with or without
11 1.1 perseant * modification, are permitted provided that the following conditions
12 1.1 perseant * are met:
13 1.1 perseant * 1. Redistributions of source code must retain the above copyright
14 1.1 perseant * notice, this list of conditions and the following disclaimer.
15 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 perseant * notice, this list of conditions and the following disclaimer in the
17 1.1 perseant * documentation and/or other materials provided with the distribution.
18 1.1 perseant * 3. All advertising materials mentioning features or use of this software
19 1.1 perseant * must display the following acknowledgement:
20 1.1 perseant * This product includes software developed by the NetBSD
21 1.1 perseant * Foundation, Inc. and its contributors.
22 1.1 perseant * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 perseant * contributors may be used to endorse or promote products derived
24 1.1 perseant * from this software without specific prior written permission.
25 1.1 perseant *
26 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 perseant * POSSIBILITY OF SUCH DAMAGE.
37 1.1 perseant */
38 1.1 perseant
39 1.1 perseant /*
40 1.1 perseant * Buffer cache routines for a file-descriptor backed filesystem.
41 1.1 perseant * This is part of lfs_cleanerd so there is also a "segment pointer" that
42 1.1 perseant * we can make buffers out of without duplicating memory or reading the data
43 1.1 perseant * again.
44 1.1 perseant */
45 1.1 perseant
46 1.1 perseant #include <err.h>
47 1.1 perseant #include <fcntl.h>
48 1.1 perseant #include <time.h>
49 1.1 perseant #include <stdio.h>
50 1.1 perseant #include <stdlib.h>
51 1.1 perseant #include <string.h>
52 1.1 perseant #include <unistd.h>
53 1.1 perseant
54 1.1 perseant #include <sys/syslog.h>
55 1.1 perseant #include <sys/param.h>
56 1.1 perseant #include <sys/mount.h>
57 1.1 perseant #include <sys/stat.h>
58 1.1 perseant
59 1.1 perseant #include "vnode.h"
60 1.1 perseant #include "bufcache.h"
61 1.1 perseant #include "fdfs.h"
62 1.1 perseant
63 1.1 perseant /*
64 1.1 perseant * Return a "vnode" interface to a given file descriptor.
65 1.1 perseant */
66 1.1 perseant struct uvnode *
67 1.1 perseant fd_vget(int fd, int bsize, int segsize, int nseg)
68 1.1 perseant {
69 1.1 perseant struct fdfs *fs;
70 1.1 perseant struct uvnode *vp;
71 1.1 perseant int i;
72 1.1 perseant
73 1.1 perseant fs = (struct fdfs *)malloc(sizeof(*fs));
74 1.1 perseant if (fs == NULL)
75 1.1 perseant return NULL;
76 1.1 perseant if (segsize > 0) {
77 1.1 perseant fs->fd_bufp = (struct fd_buf *)malloc(nseg *
78 1.1 perseant sizeof(struct fd_buf));
79 1.1 perseant if (fs->fd_bufp == NULL) {
80 1.1 perseant free(fs);
81 1.1 perseant return NULL;
82 1.1 perseant }
83 1.1 perseant for (i = 0; i < nseg; i++) {
84 1.1 perseant fs->fd_bufp[i].start = 0x0;
85 1.1 perseant fs->fd_bufp[i].end = 0x0;
86 1.1 perseant fs->fd_bufp[i].buf = (char *)malloc(segsize);
87 1.1 perseant if (fs->fd_bufp[i].buf == NULL) {
88 1.1 perseant while (--i >= 0)
89 1.1 perseant free(fs->fd_bufp[i].buf);
90 1.1 perseant free(fs->fd_bufp);
91 1.1 perseant free(fs);
92 1.1 perseant return NULL;
93 1.1 perseant }
94 1.1 perseant }
95 1.1 perseant } else
96 1.1 perseant fs->fd_bufp = NULL;
97 1.1 perseant
98 1.1 perseant fs->fd_fd = fd;
99 1.1 perseant fs->fd_bufc = nseg;
100 1.1 perseant fs->fd_bufi = 0;
101 1.1 perseant fs->fd_bsize = bsize;
102 1.1 perseant fs->fd_ssize = segsize;
103 1.1 perseant
104 1.1 perseant vp = (struct uvnode *) malloc(sizeof(*vp));
105 1.1 perseant memset(vp, 0, sizeof(*vp));
106 1.1 perseant vp->v_fd = fd;
107 1.1 perseant vp->v_fs = fs;
108 1.1 perseant vp->v_usecount = 0;
109 1.1 perseant vp->v_strategy_op = fd_vop_strategy;
110 1.1 perseant vp->v_bwrite_op = fd_vop_bwrite;
111 1.1 perseant vp->v_bmap_op = fd_vop_bmap;
112 1.1 perseant LIST_INIT(&vp->v_cleanblkhd);
113 1.1 perseant LIST_INIT(&vp->v_dirtyblkhd);
114 1.1 perseant vp->v_data = NULL;
115 1.1 perseant
116 1.1 perseant return vp;
117 1.1 perseant }
118 1.1 perseant
119 1.1 perseant /*
120 1.1 perseant * Deallocate a vnode.
121 1.1 perseant */
122 1.1 perseant void
123 1.1 perseant fd_reclaim(struct uvnode *vp)
124 1.1 perseant {
125 1.1 perseant int i;
126 1.1 perseant struct ubuf *bp;
127 1.1 perseant struct fdfs *fs;
128 1.1 perseant
129 1.1 perseant LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
130 1.1 perseant bremfree(bp);
131 1.1 perseant buf_destroy(bp);
132 1.1 perseant }
133 1.1 perseant LIST_FOREACH(bp, &vp->v_cleanblkhd, b_vnbufs) {
134 1.1 perseant bremfree(bp);
135 1.1 perseant buf_destroy(bp);
136 1.1 perseant }
137 1.1 perseant
138 1.1 perseant fs = (struct fdfs *)vp->v_fs;
139 1.1 perseant for (i = 0; i < fs->fd_bufc; i++)
140 1.1 perseant free(fs->fd_bufp[i].buf);
141 1.1 perseant free(fs->fd_bufp);
142 1.1 perseant free(fs);
143 1.1 perseant memset(vp, 0, sizeof(vp));
144 1.1 perseant }
145 1.1 perseant
146 1.1 perseant /*
147 1.1 perseant * We won't be using that last segment after all.
148 1.1 perseant */
149 1.1 perseant void
150 1.1 perseant fd_release(struct uvnode *vp)
151 1.1 perseant {
152 1.1 perseant --((struct fdfs *)vp->v_fs)->fd_bufi;
153 1.1 perseant }
154 1.1 perseant
155 1.1 perseant /*
156 1.1 perseant * Reset buffer pointer to first buffer.
157 1.1 perseant */
158 1.1 perseant void
159 1.1 perseant fd_release_all(struct uvnode *vp)
160 1.1 perseant {
161 1.1 perseant ((struct fdfs *)vp->v_fs)->fd_bufi = 0;
162 1.1 perseant }
163 1.1 perseant
164 1.1 perseant /*
165 1.1 perseant * Prepare a segment buffer which we will expect to read from.
166 1.1 perseant */
167 1.1 perseant int
168 1.1 perseant fd_preload(struct uvnode *vp, daddr_t start)
169 1.1 perseant {
170 1.1 perseant struct fdfs *fs = (struct fdfs *)vp->v_fs;
171 1.1 perseant struct fd_buf *t;
172 1.1 perseant int r;
173 1.1 perseant
174 1.1 perseant /* We might need to allocate more buffers. */
175 1.1 perseant if (fs->fd_bufi == fs->fd_bufc) {
176 1.1 perseant fs->fd_bufc = fs->fd_bufi + 1;
177 1.1 perseant syslog(LOG_DEBUG, "increasing number of segment buffers to %d",
178 1.1 perseant fs->fd_bufc);
179 1.1 perseant t = realloc(fs->fd_bufp, fs->fd_bufc * sizeof(struct fd_buf));
180 1.1 perseant if (t == NULL)
181 1.1 perseant return -1;
182 1.1 perseant fs->fd_bufp = t;
183 1.1 perseant fs->fd_bufp[fs->fd_bufi].start = 0x0;
184 1.1 perseant fs->fd_bufp[fs->fd_bufi].end = 0x0;
185 1.1 perseant fs->fd_bufp[fs->fd_bufi].buf = (char *)malloc(fs->fd_ssize);
186 1.1 perseant if (fs->fd_bufp[fs->fd_bufi].buf == NULL) {
187 1.1 perseant --fs->fd_bufc;
188 1.1 perseant free(fs->fd_bufp[fs->fd_bufc].buf);
189 1.1 perseant }
190 1.1 perseant }
191 1.1 perseant
192 1.1 perseant /* Read the current buffer. */
193 1.1 perseant fs->fd_bufp[fs->fd_bufi].start = start;
194 1.1 perseant fs->fd_bufp[fs->fd_bufi].end = start + fs->fd_ssize / fs->fd_bsize;
195 1.1 perseant
196 1.1 perseant if ((r = pread(fs->fd_fd, fs->fd_bufp[fs->fd_bufi].buf,
197 1.1 perseant (size_t)fs->fd_ssize, start * fs->fd_bsize)) < 0) {
198 1.1 perseant syslog(LOG_ERR, "preload to segment buffer %d", fs->fd_bufi);
199 1.1 perseant return r;
200 1.1 perseant }
201 1.1 perseant
202 1.1 perseant fs->fd_bufi = fs->fd_bufi + 1;
203 1.1 perseant return 0;
204 1.1 perseant }
205 1.1 perseant
206 1.1 perseant /*
207 1.1 perseant * Get a pointer to a block contained in one of the segment buffers,
208 1.1 perseant * as if from bread() but avoiding the buffer cache.
209 1.1 perseant */
210 1.1 perseant char *
211 1.1 perseant fd_ptrget(struct uvnode *vp, daddr_t bn)
212 1.1 perseant {
213 1.1 perseant int i;
214 1.1 perseant struct fdfs *fs;
215 1.1 perseant
216 1.1 perseant fs = (struct fdfs *)vp->v_fs;
217 1.1 perseant for (i = 0; i < fs->fd_bufc; i++) {
218 1.1 perseant if (bn >= fs->fd_bufp[i].start && bn < fs->fd_bufp[i].end) {
219 1.1 perseant return fs->fd_bufp[i].buf +
220 1.1 perseant (bn - fs->fd_bufp[i].start) * fs->fd_bsize;
221 1.1 perseant }
222 1.1 perseant }
223 1.1 perseant return NULL;
224 1.1 perseant }
225 1.1 perseant
226 1.1 perseant /*
227 1.1 perseant * Strategy routine. We can read from the segment buffer if requested.
228 1.1 perseant */
229 1.1 perseant int
230 1.1 perseant fd_vop_strategy(struct ubuf * bp)
231 1.1 perseant {
232 1.1 perseant struct fdfs *fs;
233 1.1 perseant char *cp;
234 1.1 perseant int count;
235 1.1 perseant
236 1.1 perseant fs = (struct fdfs *)bp->b_vp->v_fs;
237 1.1 perseant if (bp->b_flags & B_READ) {
238 1.1 perseant if ((cp = fd_ptrget(bp->b_vp, bp->b_blkno)) != NULL) {
239 1.1 perseant free(bp->b_data);
240 1.1 perseant bp->b_data = cp;
241 1.1 perseant bp->b_flags |= (B_DONTFREE | B_DONE);
242 1.1 perseant return 0;
243 1.1 perseant }
244 1.1 perseant count = pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
245 1.1 perseant bp->b_blkno * fs->fd_bsize);
246 1.1 perseant if (count == bp->b_bcount)
247 1.1 perseant bp->b_flags |= B_DONE;
248 1.1 perseant } else {
249 1.1 perseant count = pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
250 1.1 perseant bp->b_blkno * fs->fd_bsize);
251 1.1 perseant if (count == 0) {
252 1.1 perseant perror("pwrite");
253 1.1 perseant return -1;
254 1.1 perseant }
255 1.1 perseant bp->b_flags &= ~B_DELWRI;
256 1.1 perseant reassignbuf(bp, bp->b_vp);
257 1.1 perseant }
258 1.1 perseant return 0;
259 1.1 perseant }
260 1.1 perseant
261 1.1 perseant /*
262 1.1 perseant * Delayed write.
263 1.1 perseant */
264 1.1 perseant int
265 1.1 perseant fd_vop_bwrite(struct ubuf * bp)
266 1.1 perseant {
267 1.1 perseant bp->b_flags |= B_DELWRI;
268 1.1 perseant reassignbuf(bp, bp->b_vp);
269 1.1 perseant brelse(bp);
270 1.1 perseant return 0;
271 1.1 perseant }
272 1.1 perseant
273 1.1 perseant /*
274 1.1 perseant * Map lbn to disk address. Since we are using the file
275 1.1 perseant * descriptor as the "disk", the disk address is meaningless
276 1.1 perseant * and we just return the block address.
277 1.1 perseant */
278 1.1 perseant int
279 1.1 perseant fd_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
280 1.1 perseant {
281 1.1 perseant *daddrp = lbn;
282 1.1 perseant return 0;
283 1.1 perseant }
284