rumpblk.c revision 1.16 1 1.16 pooka /* $NetBSD: rumpblk.c,v 1.16 2009/04/06 20:40:33 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2009 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by the
7 1.1 pooka * Finnish Cultural Foundation.
8 1.1 pooka *
9 1.1 pooka * Redistribution and use in source and binary forms, with or without
10 1.1 pooka * modification, are permitted provided that the following conditions
11 1.1 pooka * are met:
12 1.1 pooka * 1. Redistributions of source code must retain the above copyright
13 1.1 pooka * notice, this list of conditions and the following disclaimer.
14 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 pooka * notice, this list of conditions and the following disclaimer in the
16 1.1 pooka * documentation and/or other materials provided with the distribution.
17 1.1 pooka *
18 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 pooka * SUCH DAMAGE.
29 1.1 pooka */
30 1.1 pooka
31 1.1 pooka /*
32 1.1 pooka * Block device emulation. Presents a block device interface and
33 1.1 pooka * uses rumpuser system calls to satisfy I/O requests.
34 1.16 pooka *
35 1.16 pooka * We provide fault injection. The driver can be made to fail
36 1.16 pooka * I/O occasionally.
37 1.16 pooka *
38 1.16 pooka * The driver also provides an optimization for regular files by
39 1.16 pooka * using memory-mapped I/O. This avoids kernel access for every
40 1.16 pooka * I/O operation. It also gives finer-grained control of how to
41 1.16 pooka * flush data. Additionally, in case the rump kernel dumps core,
42 1.16 pooka * we get way less carnage.
43 1.1 pooka */
44 1.1 pooka
45 1.1 pooka #include <sys/cdefs.h>
46 1.16 pooka __KERNEL_RCSID(0, "$NetBSD: rumpblk.c,v 1.16 2009/04/06 20:40:33 pooka Exp $");
47 1.1 pooka
48 1.1 pooka #include <sys/param.h>
49 1.1 pooka #include <sys/buf.h>
50 1.1 pooka #include <sys/conf.h>
51 1.16 pooka #include <sys/condvar.h>
52 1.1 pooka #include <sys/disklabel.h>
53 1.1 pooka #include <sys/fcntl.h>
54 1.1 pooka #include <sys/kmem.h>
55 1.1 pooka #include <sys/malloc.h>
56 1.16 pooka #include <sys/queue.h>
57 1.1 pooka #include <sys/stat.h>
58 1.1 pooka
59 1.1 pooka #include <rump/rumpuser.h>
60 1.1 pooka
61 1.1 pooka #include "rump_private.h"
62 1.1 pooka #include "rump_vfs_private.h"
63 1.1 pooka
64 1.16 pooka #if 0
65 1.16 pooka #define DPRINTF(x) printf x
66 1.16 pooka #else
67 1.16 pooka #define DPRINTF(x)
68 1.16 pooka #endif
69 1.16 pooka
70 1.16 pooka #define MEMWINSIZE (1<<20) /* 1MB */
71 1.16 pooka #define MEMWINCOUNT 16 /* max 16 windows == 16 megs of memory */
72 1.16 pooka #define STARTWIN(off) ((off) & ~(MEMWINSIZE-1))
73 1.16 pooka #define INWIN(win,off) ((win)->win_off == STARTWIN(off))
74 1.16 pooka #define WINSIZE(rblk, win) (MIN((rblk->rblk_size-win->win_off),MEMWINSIZE))
75 1.16 pooka #define WINVALID(win) ((win)->win_off != (off_t)-1)
76 1.16 pooka #define WINVALIDATE(win) ((win)->win_off = (off_t)-1)
77 1.16 pooka struct blkwin {
78 1.16 pooka off_t win_off;
79 1.16 pooka void *win_mem;
80 1.16 pooka int win_refcnt;
81 1.16 pooka
82 1.16 pooka TAILQ_ENTRY(blkwin) win_lru;
83 1.16 pooka };
84 1.16 pooka
85 1.1 pooka #define RUMPBLK_SIZE 16
86 1.1 pooka static struct rblkdev {
87 1.1 pooka char *rblk_path;
88 1.1 pooka int rblk_fd;
89 1.16 pooka
90 1.16 pooka /* for mmap */
91 1.16 pooka int rblk_mmflags;
92 1.16 pooka kmutex_t rblk_memmtx;
93 1.16 pooka kcondvar_t rblk_memcv;
94 1.16 pooka TAILQ_HEAD(winlru, blkwin) rblk_lruq;
95 1.16 pooka size_t rblk_size;
96 1.16 pooka bool rblk_waiting;
97 1.1 pooka
98 1.1 pooka struct partition *rblk_curpi;
99 1.1 pooka struct partition rblk_pi;
100 1.1 pooka struct disklabel rblk_dl;
101 1.1 pooka } minors[RUMPBLK_SIZE];
102 1.1 pooka
103 1.1 pooka dev_type_open(rumpblk_open);
104 1.1 pooka dev_type_close(rumpblk_close);
105 1.1 pooka dev_type_read(rumpblk_read);
106 1.1 pooka dev_type_write(rumpblk_write);
107 1.1 pooka dev_type_ioctl(rumpblk_ioctl);
108 1.1 pooka dev_type_strategy(rumpblk_strategy);
109 1.3 pooka dev_type_strategy(rumpblk_strategy_fail);
110 1.1 pooka dev_type_dump(rumpblk_dump);
111 1.1 pooka dev_type_size(rumpblk_size);
112 1.1 pooka
113 1.1 pooka static const struct bdevsw rumpblk_bdevsw = {
114 1.1 pooka rumpblk_open, rumpblk_close, rumpblk_strategy, rumpblk_ioctl,
115 1.1 pooka nodump, nosize, D_DISK
116 1.1 pooka };
117 1.1 pooka
118 1.3 pooka static const struct bdevsw rumpblk_bdevsw_fail = {
119 1.3 pooka rumpblk_open, rumpblk_close, rumpblk_strategy_fail, rumpblk_ioctl,
120 1.3 pooka nodump, nosize, D_DISK
121 1.3 pooka };
122 1.3 pooka
123 1.1 pooka static const struct cdevsw rumpblk_cdevsw = {
124 1.1 pooka rumpblk_open, rumpblk_close, rumpblk_read, rumpblk_write,
125 1.1 pooka rumpblk_ioctl, nostop, notty, nopoll, nommap, nokqfilter, D_DISK
126 1.1 pooka };
127 1.1 pooka
128 1.3 pooka /* fail every n out of BLKFAIL_MAX */
129 1.3 pooka #define BLKFAIL_MAX 10000
130 1.3 pooka static int blkfail;
131 1.3 pooka static unsigned randstate;
132 1.1 pooka
133 1.16 pooka static struct blkwin *
134 1.16 pooka getwindow(struct rblkdev *rblk, off_t off, int *wsize, int *error)
135 1.16 pooka {
136 1.16 pooka struct blkwin *win;
137 1.16 pooka
138 1.16 pooka mutex_enter(&rblk->rblk_memmtx);
139 1.16 pooka retry:
140 1.16 pooka /* search for window */
141 1.16 pooka TAILQ_FOREACH(win, &rblk->rblk_lruq, win_lru) {
142 1.16 pooka if (INWIN(win, off) && WINVALID(win))
143 1.16 pooka break;
144 1.16 pooka }
145 1.16 pooka
146 1.16 pooka /* found? return */
147 1.16 pooka if (win) {
148 1.16 pooka TAILQ_REMOVE(&rblk->rblk_lruq, win, win_lru);
149 1.16 pooka goto good;
150 1.16 pooka }
151 1.16 pooka
152 1.16 pooka /*
153 1.16 pooka * Else, create new window. If the least recently used is not
154 1.16 pooka * currently in use, reuse that. Otherwise we need to wait.
155 1.16 pooka */
156 1.16 pooka win = TAILQ_LAST(&rblk->rblk_lruq, winlru);
157 1.16 pooka if (win->win_refcnt == 0) {
158 1.16 pooka TAILQ_REMOVE(&rblk->rblk_lruq, win, win_lru);
159 1.16 pooka mutex_exit(&rblk->rblk_memmtx);
160 1.16 pooka
161 1.16 pooka if (WINVALID(win)) {
162 1.16 pooka DPRINTF(("win %p, unmap mem %p, off 0x%" PRIx64 "\n",
163 1.16 pooka win, win->win_mem, win->win_off));
164 1.16 pooka rumpuser_unmap(win->win_mem, WINSIZE(rblk, win));
165 1.16 pooka WINVALIDATE(win);
166 1.16 pooka }
167 1.16 pooka
168 1.16 pooka win->win_off = STARTWIN(off);
169 1.16 pooka win->win_mem = rumpuser_filemmap(rblk->rblk_fd, win->win_off,
170 1.16 pooka WINSIZE(rblk, win), rblk->rblk_mmflags, error);
171 1.16 pooka DPRINTF(("win %p, off 0x%" PRIx64 ", mem %p\n",
172 1.16 pooka win, win->win_off, win->win_mem));
173 1.16 pooka
174 1.16 pooka mutex_enter(&rblk->rblk_memmtx);
175 1.16 pooka if (win->win_mem == NULL) {
176 1.16 pooka WINVALIDATE(win);
177 1.16 pooka TAILQ_INSERT_TAIL(&rblk->rblk_lruq, win, win_lru);
178 1.16 pooka mutex_exit(&rblk->rblk_memmtx);
179 1.16 pooka return NULL;
180 1.16 pooka }
181 1.16 pooka } else {
182 1.16 pooka DPRINTF(("memwin wait\n"));
183 1.16 pooka rblk->rblk_waiting = true;
184 1.16 pooka cv_wait(&rblk->rblk_memcv, &rblk->rblk_memmtx);
185 1.16 pooka goto retry;
186 1.16 pooka }
187 1.16 pooka
188 1.16 pooka good:
189 1.16 pooka KASSERT(win);
190 1.16 pooka win->win_refcnt++;
191 1.16 pooka TAILQ_INSERT_HEAD(&rblk->rblk_lruq, win, win_lru);
192 1.16 pooka mutex_exit(&rblk->rblk_memmtx);
193 1.16 pooka *wsize = MIN(*wsize, MEMWINSIZE - (off-win->win_off));
194 1.16 pooka KASSERT(*wsize);
195 1.16 pooka
196 1.16 pooka return win;
197 1.16 pooka }
198 1.16 pooka
199 1.16 pooka static void
200 1.16 pooka putwindow(struct rblkdev *rblk, struct blkwin *win)
201 1.16 pooka {
202 1.16 pooka
203 1.16 pooka mutex_enter(&rblk->rblk_memmtx);
204 1.16 pooka if (--win->win_refcnt == 0 && rblk->rblk_waiting) {
205 1.16 pooka rblk->rblk_waiting = false;
206 1.16 pooka cv_signal(&rblk->rblk_memcv);
207 1.16 pooka }
208 1.16 pooka KASSERT(win->win_refcnt >= 0);
209 1.16 pooka mutex_exit(&rblk->rblk_memmtx);
210 1.16 pooka }
211 1.16 pooka
212 1.16 pooka static void
213 1.16 pooka wincleanup(struct rblkdev *rblk)
214 1.16 pooka {
215 1.16 pooka struct blkwin *win;
216 1.16 pooka
217 1.16 pooka while ((win = TAILQ_FIRST(&rblk->rblk_lruq)) != NULL) {
218 1.16 pooka TAILQ_REMOVE(&rblk->rblk_lruq, win, win_lru);
219 1.16 pooka if (WINVALID(win)) {
220 1.16 pooka DPRINTF(("cleanup win %p addr %p\n",
221 1.16 pooka win, win->win_mem));
222 1.16 pooka rumpuser_unmap(win->win_mem, WINSIZE(rblk, win));
223 1.16 pooka }
224 1.16 pooka kmem_free(win, sizeof(*win));
225 1.16 pooka }
226 1.16 pooka rblk->rblk_mmflags = 0;
227 1.16 pooka }
228 1.16 pooka
229 1.1 pooka int
230 1.8 cegger rumpblk_init(void)
231 1.1 pooka {
232 1.3 pooka char buf[64];
233 1.1 pooka int rumpblk = RUMPBLK;
234 1.16 pooka int error, i;
235 1.3 pooka
236 1.3 pooka if (rumpuser_getenv("RUMP_BLKFAIL", buf, sizeof(buf), &error) == 0) {
237 1.3 pooka blkfail = strtoul(buf, NULL, 10);
238 1.3 pooka /* fail everything */
239 1.3 pooka if (blkfail > BLKFAIL_MAX)
240 1.3 pooka blkfail = BLKFAIL_MAX;
241 1.3 pooka if (rumpuser_getenv("RUMP_BLKFAIL_SEED", buf, sizeof(buf),
242 1.3 pooka &error) == 0) {
243 1.3 pooka randstate = strtoul(buf, NULL, 10);
244 1.3 pooka } else {
245 1.3 pooka randstate = arc4random(); /* XXX: not enough entropy */
246 1.3 pooka }
247 1.3 pooka printf("rumpblk: FAULT INJECTION ACTIVE! every %d out of"
248 1.3 pooka " %d I/O will fail. key %u\n", blkfail, BLKFAIL_MAX,
249 1.3 pooka randstate);
250 1.3 pooka } else {
251 1.3 pooka blkfail = 0;
252 1.3 pooka }
253 1.1 pooka
254 1.16 pooka memset(minors, 0, sizeof(minors));
255 1.16 pooka for (i = 0; i < RUMPBLK_SIZE; i++) {
256 1.16 pooka mutex_init(&minors[i].rblk_memmtx, MUTEX_DEFAULT, IPL_NONE);
257 1.16 pooka cv_init(&minors[i].rblk_memcv, "rblkmcv");
258 1.16 pooka }
259 1.16 pooka
260 1.3 pooka if (blkfail) {
261 1.3 pooka return devsw_attach("rumpblk", &rumpblk_bdevsw_fail, &rumpblk,
262 1.3 pooka &rumpblk_cdevsw, &rumpblk);
263 1.3 pooka } else {
264 1.3 pooka return devsw_attach("rumpblk", &rumpblk_bdevsw, &rumpblk,
265 1.3 pooka &rumpblk_cdevsw, &rumpblk);
266 1.3 pooka }
267 1.1 pooka }
268 1.1 pooka
269 1.1 pooka int
270 1.1 pooka rumpblk_register(const char *path)
271 1.1 pooka {
272 1.1 pooka size_t len;
273 1.1 pooka int i;
274 1.1 pooka
275 1.1 pooka for (i = 0; i < RUMPBLK_SIZE; i++)
276 1.15 pooka if (minors[i].rblk_path && strcmp(minors[i].rblk_path, path)==0)
277 1.1 pooka return i;
278 1.1 pooka
279 1.1 pooka for (i = 0; i < RUMPBLK_SIZE; i++)
280 1.1 pooka if (minors[i].rblk_path == NULL)
281 1.1 pooka break;
282 1.1 pooka if (i == RUMPBLK_SIZE)
283 1.1 pooka return -1;
284 1.1 pooka
285 1.1 pooka len = strlen(path);
286 1.10 uebayasi minors[i].rblk_path = malloc(len + 1, M_TEMP, M_WAITOK);
287 1.1 pooka strcpy(minors[i].rblk_path, path);
288 1.1 pooka minors[i].rblk_fd = -1;
289 1.1 pooka return i;
290 1.1 pooka }
291 1.1 pooka
292 1.1 pooka int
293 1.1 pooka rumpblk_open(dev_t dev, int flag, int fmt, struct lwp *l)
294 1.1 pooka {
295 1.1 pooka struct rblkdev *rblk = &minors[minor(dev)];
296 1.5 pooka uint64_t fsize;
297 1.9 pooka int ft, dummy;
298 1.1 pooka int error, fd;
299 1.1 pooka
300 1.1 pooka KASSERT(rblk->rblk_fd == -1);
301 1.1 pooka fd = rumpuser_open(rblk->rblk_path, OFLAGS(flag), &error);
302 1.1 pooka if (error)
303 1.1 pooka return error;
304 1.1 pooka
305 1.9 pooka if (rumpuser_getfileinfo(rblk->rblk_path, &fsize, &ft, &error) == -1) {
306 1.9 pooka rumpuser_close(fd, &dummy);
307 1.9 pooka return error;
308 1.9 pooka }
309 1.9 pooka
310 1.9 pooka if (ft == RUMPUSER_FT_REG) {
311 1.16 pooka struct blkwin *win;
312 1.16 pooka int i, winsize;
313 1.16 pooka
314 1.1 pooka /*
315 1.16 pooka * Use mmap to access a regular file. Allocate and
316 1.16 pooka * cache initial windows here. Failure to allocate one
317 1.16 pooka * means fallback to read/write i/o.
318 1.1 pooka */
319 1.9 pooka
320 1.16 pooka rblk->rblk_mmflags = 0;
321 1.16 pooka if (flag & FREAD)
322 1.16 pooka rblk->rblk_mmflags |= RUMPUSER_FILEMMAP_READ;
323 1.16 pooka if (flag & FWRITE) {
324 1.16 pooka rblk->rblk_mmflags |= RUMPUSER_FILEMMAP_WRITE;
325 1.16 pooka rblk->rblk_mmflags |= RUMPUSER_FILEMMAP_SHARED;
326 1.16 pooka }
327 1.16 pooka
328 1.16 pooka TAILQ_INIT(&rblk->rblk_lruq);
329 1.16 pooka rblk->rblk_size = fsize;
330 1.16 pooka rblk->rblk_fd = fd;
331 1.16 pooka
332 1.16 pooka for (i = 0; i < MEMWINCOUNT && i * MEMWINSIZE < fsize; i++) {
333 1.16 pooka win = kmem_zalloc(sizeof(*win), KM_SLEEP);
334 1.16 pooka WINVALIDATE(win);
335 1.16 pooka TAILQ_INSERT_TAIL(&rblk->rblk_lruq, win, win_lru);
336 1.16 pooka
337 1.16 pooka /*
338 1.16 pooka * Allocate first windows. Here we just generally
339 1.16 pooka * make sure a) we can mmap at all b) we have the
340 1.16 pooka * necessary VA available
341 1.16 pooka */
342 1.16 pooka winsize = 1;
343 1.16 pooka win = getwindow(rblk, i*MEMWINSIZE, &winsize, &error);
344 1.16 pooka if (win) {
345 1.16 pooka putwindow(rblk, win);
346 1.16 pooka } else {
347 1.16 pooka wincleanup(rblk);
348 1.16 pooka break;
349 1.12 pooka }
350 1.9 pooka }
351 1.9 pooka
352 1.1 pooka memset(&rblk->rblk_dl, 0, sizeof(rblk->rblk_dl));
353 1.9 pooka rblk->rblk_pi.p_size = fsize >> DEV_BSHIFT;
354 1.9 pooka rblk->rblk_dl.d_secsize = DEV_BSIZE;
355 1.9 pooka rblk->rblk_curpi = &rblk->rblk_pi;
356 1.9 pooka } else {
357 1.13 pooka if (rumpuser_ioctl(fd, DIOCGDINFO, &rblk->rblk_dl,
358 1.13 pooka &error) != -1) {
359 1.1 pooka rumpuser_close(fd, &dummy);
360 1.1 pooka return error;
361 1.1 pooka }
362 1.9 pooka
363 1.16 pooka rblk->rblk_fd = fd;
364 1.9 pooka rblk->rblk_curpi = &rblk->rblk_dl.d_partitions[0];
365 1.1 pooka }
366 1.1 pooka
367 1.16 pooka KASSERT(rblk->rblk_fd != -1);
368 1.1 pooka return 0;
369 1.1 pooka }
370 1.1 pooka
371 1.1 pooka int
372 1.1 pooka rumpblk_close(dev_t dev, int flag, int fmt, struct lwp *l)
373 1.1 pooka {
374 1.1 pooka struct rblkdev *rblk = &minors[minor(dev)];
375 1.1 pooka int dummy;
376 1.1 pooka
377 1.16 pooka if (rblk->rblk_mmflags)
378 1.16 pooka wincleanup(rblk);
379 1.16 pooka rumpuser_fsync(rblk->rblk_fd, &dummy);
380 1.1 pooka rumpuser_close(rblk->rblk_fd, &dummy);
381 1.1 pooka rblk->rblk_fd = -1;
382 1.1 pooka
383 1.1 pooka return 0;
384 1.1 pooka }
385 1.1 pooka
386 1.1 pooka int
387 1.1 pooka rumpblk_ioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
388 1.1 pooka {
389 1.1 pooka struct rblkdev *rblk = &minors[minor(dev)];
390 1.1 pooka int rv, error;
391 1.1 pooka
392 1.1 pooka if (xfer == DIOCGPART) {
393 1.1 pooka struct partinfo *pi = (struct partinfo *)addr;
394 1.1 pooka
395 1.1 pooka pi->part = rblk->rblk_curpi;
396 1.1 pooka pi->disklab = &rblk->rblk_dl;
397 1.1 pooka
398 1.1 pooka return 0;
399 1.1 pooka }
400 1.1 pooka
401 1.1 pooka rv = rumpuser_ioctl(rblk->rblk_fd, xfer, addr, &error);
402 1.1 pooka if (rv == -1)
403 1.1 pooka return error;
404 1.1 pooka
405 1.1 pooka return 0;
406 1.1 pooka }
407 1.1 pooka
408 1.1 pooka int
409 1.1 pooka rumpblk_read(dev_t dev, struct uio *uio, int flags)
410 1.1 pooka {
411 1.1 pooka
412 1.1 pooka panic("%s: unimplemented", __func__);
413 1.1 pooka }
414 1.1 pooka
415 1.1 pooka int
416 1.1 pooka rumpblk_write(dev_t dev, struct uio *uio, int flags)
417 1.1 pooka {
418 1.1 pooka
419 1.1 pooka panic("%s: unimplemented", __func__);
420 1.1 pooka }
421 1.1 pooka
422 1.3 pooka static void
423 1.3 pooka dostrategy(struct buf *bp)
424 1.1 pooka {
425 1.1 pooka struct rblkdev *rblk = &minors[minor(bp->b_dev)];
426 1.1 pooka off_t off;
427 1.9 pooka int async, error;
428 1.1 pooka
429 1.1 pooka off = bp->b_blkno << DEV_BSHIFT;
430 1.11 pooka /*
431 1.11 pooka * Do bounds checking if we're working on a file. Otherwise
432 1.11 pooka * invalid file systems might attempt to read beyond EOF. This
433 1.11 pooka * is bad(tm) especially on mmapped images. This is essentially
434 1.11 pooka * the kernel bounds_check() routines.
435 1.11 pooka */
436 1.11 pooka if (rblk->rblk_size && off + bp->b_bcount > rblk->rblk_size) {
437 1.11 pooka int64_t sz = rblk->rblk_size - off;
438 1.11 pooka
439 1.11 pooka /* EOF */
440 1.11 pooka if (sz == 0) {
441 1.11 pooka rump_biodone(bp, 0, 0);
442 1.11 pooka return;
443 1.11 pooka }
444 1.11 pooka /* beyond EOF ==> error */
445 1.11 pooka if (sz < 0) {
446 1.11 pooka rump_biodone(bp, 0, EINVAL);
447 1.11 pooka return;
448 1.11 pooka }
449 1.11 pooka
450 1.11 pooka /* truncate to device size */
451 1.11 pooka bp->b_bcount = sz;
452 1.11 pooka }
453 1.11 pooka
454 1.9 pooka async = bp->b_flags & B_ASYNC;
455 1.1 pooka DPRINTF(("rumpblk_strategy: 0x%x bytes %s off 0x%" PRIx64
456 1.16 pooka " (0x%" PRIx64 " - 0x%" PRIx64 ")\n",
457 1.16 pooka bp->b_bcount, BUF_ISREAD(bp) ? "READ" : "WRITE",
458 1.1 pooka off, off, (off + bp->b_bcount)));
459 1.1 pooka
460 1.16 pooka /* mmap? handle here and return */
461 1.16 pooka if (rblk->rblk_mmflags) {
462 1.16 pooka struct blkwin *win;
463 1.16 pooka int winsize, iodone;
464 1.16 pooka uint8_t *ioaddr, *bufaddr;
465 1.16 pooka
466 1.16 pooka for (iodone = 0; iodone < bp->b_bcount;
467 1.16 pooka iodone += winsize, off += winsize) {
468 1.16 pooka winsize = bp->b_bcount - iodone;
469 1.16 pooka win = getwindow(rblk, off, &winsize, &error);
470 1.16 pooka if (win == NULL) {
471 1.16 pooka rump_biodone(bp, iodone, error);
472 1.16 pooka return;
473 1.16 pooka }
474 1.16 pooka
475 1.16 pooka ioaddr = (uint8_t *)win->win_mem + (off-STARTWIN(off));
476 1.16 pooka bufaddr = (uint8_t *)bp->b_data + iodone;
477 1.11 pooka
478 1.16 pooka DPRINTF(("strat: %p off 0x%" PRIx64
479 1.16 pooka ", ioaddr %p (%p)/buf %p\n", win,
480 1.16 pooka win->win_off, ioaddr, win->win_mem, bufaddr));
481 1.16 pooka if (BUF_ISREAD(bp)) {
482 1.16 pooka memcpy(bufaddr, ioaddr, winsize);
483 1.16 pooka } else {
484 1.16 pooka memcpy(ioaddr, bufaddr, winsize);
485 1.16 pooka }
486 1.16 pooka
487 1.16 pooka /* synchronous write, sync bits back to disk */
488 1.16 pooka if (BUF_ISWRITE(bp) && !async) {
489 1.16 pooka rumpuser_memsync(ioaddr, winsize, &error);
490 1.16 pooka }
491 1.16 pooka putwindow(rblk, win);
492 1.9 pooka }
493 1.9 pooka
494 1.9 pooka rump_biodone(bp, bp->b_bcount, 0);
495 1.9 pooka return;
496 1.9 pooka }
497 1.9 pooka
498 1.1 pooka /*
499 1.1 pooka * Do I/O. We have different paths for async and sync I/O.
500 1.1 pooka * Async I/O is done by passing a request to rumpuser where
501 1.1 pooka * it is executed. The rumpuser routine then calls
502 1.1 pooka * biodone() to signal any waiters in the kernel. I/O's are
503 1.1 pooka * executed in series. Technically executing them in parallel
504 1.1 pooka * would produce better results, but then we'd need either
505 1.1 pooka * more threads or posix aio. Maybe worth investigating
506 1.1 pooka * this later.
507 1.14 pooka *
508 1.14 pooka * Using bufq here might be a good idea.
509 1.1 pooka */
510 1.13 pooka if (rump_threads) {
511 1.1 pooka struct rumpuser_aio *rua;
512 1.1 pooka
513 1.1 pooka rumpuser_mutex_enter(&rumpuser_aio_mtx);
514 1.13 pooka while ((rumpuser_aio_head+1) % N_AIOS == rumpuser_aio_tail)
515 1.13 pooka rumpuser_cv_wait(&rumpuser_aio_cv, &rumpuser_aio_mtx);
516 1.1 pooka
517 1.2 pooka rua = &rumpuser_aios[rumpuser_aio_head];
518 1.2 pooka KASSERT(rua->rua_bp == NULL);
519 1.2 pooka rua->rua_fd = rblk->rblk_fd;
520 1.2 pooka rua->rua_data = bp->b_data;
521 1.2 pooka rua->rua_dlen = bp->b_bcount;
522 1.2 pooka rua->rua_off = off;
523 1.2 pooka rua->rua_bp = bp;
524 1.2 pooka rua->rua_op = BUF_ISREAD(bp);
525 1.2 pooka
526 1.1 pooka /* insert into queue & signal */
527 1.6 pooka rumpuser_aio_head = (rumpuser_aio_head+1) % N_AIOS;
528 1.1 pooka rumpuser_cv_signal(&rumpuser_aio_cv);
529 1.1 pooka rumpuser_mutex_exit(&rumpuser_aio_mtx);
530 1.13 pooka
531 1.13 pooka /* make sure non-async writes end up on backing media */
532 1.13 pooka if (BUF_ISWRITE(bp) && !async) {
533 1.13 pooka biowait(bp);
534 1.13 pooka rumpuser_fsync(rblk->rblk_fd, &error);
535 1.13 pooka }
536 1.1 pooka } else {
537 1.1 pooka if (BUF_ISREAD(bp)) {
538 1.1 pooka rumpuser_read_bio(rblk->rblk_fd, bp->b_data,
539 1.1 pooka bp->b_bcount, off, rump_biodone, bp);
540 1.1 pooka } else {
541 1.1 pooka rumpuser_write_bio(rblk->rblk_fd, bp->b_data,
542 1.1 pooka bp->b_bcount, off, rump_biodone, bp);
543 1.1 pooka }
544 1.1 pooka if (!async) {
545 1.1 pooka if (BUF_ISWRITE(bp))
546 1.1 pooka rumpuser_fsync(rblk->rblk_fd, &error);
547 1.1 pooka }
548 1.1 pooka }
549 1.1 pooka }
550 1.3 pooka
551 1.3 pooka void
552 1.3 pooka rumpblk_strategy(struct buf *bp)
553 1.3 pooka {
554 1.3 pooka
555 1.3 pooka dostrategy(bp);
556 1.3 pooka }
557 1.3 pooka
558 1.3 pooka /*
559 1.4 pooka * Simple random number generator. This is private so that we can
560 1.4 pooka * very repeatedly control which blocks will fail.
561 1.4 pooka *
562 1.3 pooka * <mlelstv> pooka, rand()
563 1.3 pooka * <mlelstv> [paste]
564 1.3 pooka */
565 1.3 pooka static unsigned
566 1.3 pooka gimmerand(void)
567 1.3 pooka {
568 1.3 pooka
569 1.3 pooka return (randstate = randstate * 1103515245 + 12345) % (0x80000000L);
570 1.3 pooka }
571 1.3 pooka
572 1.3 pooka /*
573 1.3 pooka * Block device with very simple fault injection. Fails every
574 1.3 pooka * n out of BLKFAIL_MAX I/O with EIO. n is determined by the env
575 1.3 pooka * variable RUMP_BLKFAIL.
576 1.3 pooka */
577 1.3 pooka void
578 1.3 pooka rumpblk_strategy_fail(struct buf *bp)
579 1.3 pooka {
580 1.3 pooka
581 1.3 pooka if (gimmerand() % BLKFAIL_MAX >= blkfail) {
582 1.3 pooka dostrategy(bp);
583 1.3 pooka } else {
584 1.3 pooka printf("block fault injection: failing I/O on block %lld\n",
585 1.3 pooka (long long)bp->b_blkno);
586 1.3 pooka bp->b_error = EIO;
587 1.3 pooka biodone(bp);
588 1.3 pooka }
589 1.3 pooka }
590