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