rumpblk.c revision 1.2.2.1 1 1.2.2.1 jym /* $NetBSD: rumpblk.c,v 1.2.2.1 2009/05/13 17:22:58 jym 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.2.2.1 jym *
35 1.2.2.1 jym * We provide fault injection. The driver can be made to fail
36 1.2.2.1 jym * I/O occasionally.
37 1.2.2.1 jym *
38 1.2.2.1 jym * The driver also provides an optimization for regular files by
39 1.2.2.1 jym * using memory-mapped I/O. This avoids kernel access for every
40 1.2.2.1 jym * I/O operation. It also gives finer-grained control of how to
41 1.2.2.1 jym * flush data. Additionally, in case the rump kernel dumps core,
42 1.2.2.1 jym * we get way less carnage.
43 1.1 pooka */
44 1.1 pooka
45 1.1 pooka #include <sys/cdefs.h>
46 1.2.2.1 jym __KERNEL_RCSID(0, "$NetBSD: rumpblk.c,v 1.2.2.1 2009/05/13 17:22:58 jym 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.2.2.1 jym #include <sys/condvar.h>
52 1.1 pooka #include <sys/disklabel.h>
53 1.2.2.1 jym #include <sys/evcnt.h>
54 1.1 pooka #include <sys/fcntl.h>
55 1.1 pooka #include <sys/kmem.h>
56 1.1 pooka #include <sys/malloc.h>
57 1.2.2.1 jym #include <sys/queue.h>
58 1.1 pooka #include <sys/stat.h>
59 1.1 pooka
60 1.1 pooka #include <rump/rumpuser.h>
61 1.1 pooka
62 1.1 pooka #include "rump_private.h"
63 1.1 pooka #include "rump_vfs_private.h"
64 1.1 pooka
65 1.2.2.1 jym #if 0
66 1.2.2.1 jym #define DPRINTF(x) printf x
67 1.2.2.1 jym #else
68 1.2.2.1 jym #define DPRINTF(x)
69 1.2.2.1 jym #endif
70 1.2.2.1 jym
71 1.2.2.1 jym /* Default: 16 x 1MB windows */
72 1.2.2.1 jym unsigned memwinsize = (1<<20);
73 1.2.2.1 jym unsigned memwincnt = 16;
74 1.2.2.1 jym
75 1.2.2.1 jym #define STARTWIN(off) ((off) & ~(memwinsize-1))
76 1.2.2.1 jym #define INWIN(win,off) ((win)->win_off == STARTWIN(off))
77 1.2.2.1 jym #define WINSIZE(rblk, win) (MIN((rblk->rblk_size-win->win_off),memwinsize))
78 1.2.2.1 jym #define WINVALID(win) ((win)->win_off != (off_t)-1)
79 1.2.2.1 jym #define WINVALIDATE(win) ((win)->win_off = (off_t)-1)
80 1.2.2.1 jym struct blkwin {
81 1.2.2.1 jym off_t win_off;
82 1.2.2.1 jym void *win_mem;
83 1.2.2.1 jym int win_refcnt;
84 1.2.2.1 jym
85 1.2.2.1 jym TAILQ_ENTRY(blkwin) win_lru;
86 1.2.2.1 jym };
87 1.2.2.1 jym
88 1.1 pooka #define RUMPBLK_SIZE 16
89 1.1 pooka static struct rblkdev {
90 1.1 pooka char *rblk_path;
91 1.1 pooka int rblk_fd;
92 1.2.2.1 jym #ifdef HAS_ODIRECT
93 1.2.2.1 jym int rblk_dfd;
94 1.2.2.1 jym #endif
95 1.2.2.1 jym
96 1.2.2.1 jym /* for mmap */
97 1.2.2.1 jym int rblk_mmflags;
98 1.2.2.1 jym kmutex_t rblk_memmtx;
99 1.2.2.1 jym kcondvar_t rblk_memcv;
100 1.2.2.1 jym TAILQ_HEAD(winlru, blkwin) rblk_lruq;
101 1.2.2.1 jym size_t rblk_size;
102 1.2.2.1 jym bool rblk_waiting;
103 1.1 pooka
104 1.1 pooka struct partition *rblk_curpi;
105 1.1 pooka struct partition rblk_pi;
106 1.1 pooka struct disklabel rblk_dl;
107 1.1 pooka } minors[RUMPBLK_SIZE];
108 1.1 pooka
109 1.2.2.1 jym static struct evcnt ev_io_total;
110 1.2.2.1 jym static struct evcnt ev_io_async;
111 1.2.2.1 jym
112 1.2.2.1 jym static struct evcnt ev_memblk_hits;
113 1.2.2.1 jym static struct evcnt ev_memblk_busy;
114 1.2.2.1 jym
115 1.2.2.1 jym static struct evcnt ev_bwrite_total;
116 1.2.2.1 jym static struct evcnt ev_bwrite_async;
117 1.2.2.1 jym static struct evcnt ev_bread_total;
118 1.2.2.1 jym
119 1.1 pooka dev_type_open(rumpblk_open);
120 1.1 pooka dev_type_close(rumpblk_close);
121 1.1 pooka dev_type_read(rumpblk_read);
122 1.1 pooka dev_type_write(rumpblk_write);
123 1.1 pooka dev_type_ioctl(rumpblk_ioctl);
124 1.1 pooka dev_type_strategy(rumpblk_strategy);
125 1.2.2.1 jym dev_type_strategy(rumpblk_strategy_fail);
126 1.1 pooka dev_type_dump(rumpblk_dump);
127 1.1 pooka dev_type_size(rumpblk_size);
128 1.1 pooka
129 1.1 pooka static const struct bdevsw rumpblk_bdevsw = {
130 1.1 pooka rumpblk_open, rumpblk_close, rumpblk_strategy, rumpblk_ioctl,
131 1.1 pooka nodump, nosize, D_DISK
132 1.1 pooka };
133 1.1 pooka
134 1.2.2.1 jym static const struct bdevsw rumpblk_bdevsw_fail = {
135 1.2.2.1 jym rumpblk_open, rumpblk_close, rumpblk_strategy_fail, rumpblk_ioctl,
136 1.2.2.1 jym nodump, nosize, D_DISK
137 1.2.2.1 jym };
138 1.2.2.1 jym
139 1.1 pooka static const struct cdevsw rumpblk_cdevsw = {
140 1.1 pooka rumpblk_open, rumpblk_close, rumpblk_read, rumpblk_write,
141 1.1 pooka rumpblk_ioctl, nostop, notty, nopoll, nommap, nokqfilter, D_DISK
142 1.1 pooka };
143 1.1 pooka
144 1.2.2.1 jym /* fail every n out of BLKFAIL_MAX */
145 1.2.2.1 jym #define BLKFAIL_MAX 10000
146 1.2.2.1 jym static int blkfail;
147 1.2.2.1 jym static unsigned randstate;
148 1.2.2.1 jym
149 1.2.2.1 jym static struct blkwin *
150 1.2.2.1 jym getwindow(struct rblkdev *rblk, off_t off, int *wsize, int *error)
151 1.2.2.1 jym {
152 1.2.2.1 jym struct blkwin *win;
153 1.2.2.1 jym
154 1.2.2.1 jym mutex_enter(&rblk->rblk_memmtx);
155 1.2.2.1 jym retry:
156 1.2.2.1 jym /* search for window */
157 1.2.2.1 jym TAILQ_FOREACH(win, &rblk->rblk_lruq, win_lru) {
158 1.2.2.1 jym if (INWIN(win, off) && WINVALID(win))
159 1.2.2.1 jym break;
160 1.2.2.1 jym }
161 1.2.2.1 jym
162 1.2.2.1 jym /* found? return */
163 1.2.2.1 jym if (win) {
164 1.2.2.1 jym ev_memblk_hits.ev_count++;
165 1.2.2.1 jym TAILQ_REMOVE(&rblk->rblk_lruq, win, win_lru);
166 1.2.2.1 jym goto good;
167 1.2.2.1 jym }
168 1.2.2.1 jym
169 1.2.2.1 jym /*
170 1.2.2.1 jym * Else, create new window. If the least recently used is not
171 1.2.2.1 jym * currently in use, reuse that. Otherwise we need to wait.
172 1.2.2.1 jym */
173 1.2.2.1 jym win = TAILQ_LAST(&rblk->rblk_lruq, winlru);
174 1.2.2.1 jym if (win->win_refcnt == 0) {
175 1.2.2.1 jym TAILQ_REMOVE(&rblk->rblk_lruq, win, win_lru);
176 1.2.2.1 jym mutex_exit(&rblk->rblk_memmtx);
177 1.2.2.1 jym
178 1.2.2.1 jym if (WINVALID(win)) {
179 1.2.2.1 jym DPRINTF(("win %p, unmap mem %p, off 0x%" PRIx64 "\n",
180 1.2.2.1 jym win, win->win_mem, win->win_off));
181 1.2.2.1 jym rumpuser_unmap(win->win_mem, WINSIZE(rblk, win));
182 1.2.2.1 jym WINVALIDATE(win);
183 1.2.2.1 jym }
184 1.2.2.1 jym
185 1.2.2.1 jym win->win_off = STARTWIN(off);
186 1.2.2.1 jym win->win_mem = rumpuser_filemmap(rblk->rblk_fd, win->win_off,
187 1.2.2.1 jym WINSIZE(rblk, win), rblk->rblk_mmflags, error);
188 1.2.2.1 jym DPRINTF(("win %p, off 0x%" PRIx64 ", mem %p\n",
189 1.2.2.1 jym win, win->win_off, win->win_mem));
190 1.2.2.1 jym
191 1.2.2.1 jym mutex_enter(&rblk->rblk_memmtx);
192 1.2.2.1 jym if (win->win_mem == NULL) {
193 1.2.2.1 jym WINVALIDATE(win);
194 1.2.2.1 jym TAILQ_INSERT_TAIL(&rblk->rblk_lruq, win, win_lru);
195 1.2.2.1 jym mutex_exit(&rblk->rblk_memmtx);
196 1.2.2.1 jym return NULL;
197 1.2.2.1 jym }
198 1.2.2.1 jym } else {
199 1.2.2.1 jym DPRINTF(("memwin wait\n"));
200 1.2.2.1 jym ev_memblk_busy.ev_count++;
201 1.2.2.1 jym
202 1.2.2.1 jym rblk->rblk_waiting = true;
203 1.2.2.1 jym cv_wait(&rblk->rblk_memcv, &rblk->rblk_memmtx);
204 1.2.2.1 jym goto retry;
205 1.2.2.1 jym }
206 1.2.2.1 jym
207 1.2.2.1 jym good:
208 1.2.2.1 jym KASSERT(win);
209 1.2.2.1 jym win->win_refcnt++;
210 1.2.2.1 jym TAILQ_INSERT_HEAD(&rblk->rblk_lruq, win, win_lru);
211 1.2.2.1 jym mutex_exit(&rblk->rblk_memmtx);
212 1.2.2.1 jym *wsize = MIN(*wsize, memwinsize - (off-win->win_off));
213 1.2.2.1 jym KASSERT(*wsize);
214 1.2.2.1 jym
215 1.2.2.1 jym return win;
216 1.2.2.1 jym }
217 1.2.2.1 jym
218 1.2.2.1 jym static void
219 1.2.2.1 jym putwindow(struct rblkdev *rblk, struct blkwin *win)
220 1.2.2.1 jym {
221 1.2.2.1 jym
222 1.2.2.1 jym mutex_enter(&rblk->rblk_memmtx);
223 1.2.2.1 jym if (--win->win_refcnt == 0 && rblk->rblk_waiting) {
224 1.2.2.1 jym rblk->rblk_waiting = false;
225 1.2.2.1 jym cv_signal(&rblk->rblk_memcv);
226 1.2.2.1 jym }
227 1.2.2.1 jym KASSERT(win->win_refcnt >= 0);
228 1.2.2.1 jym mutex_exit(&rblk->rblk_memmtx);
229 1.2.2.1 jym }
230 1.2.2.1 jym
231 1.2.2.1 jym static void
232 1.2.2.1 jym wincleanup(struct rblkdev *rblk)
233 1.2.2.1 jym {
234 1.2.2.1 jym struct blkwin *win;
235 1.2.2.1 jym
236 1.2.2.1 jym while ((win = TAILQ_FIRST(&rblk->rblk_lruq)) != NULL) {
237 1.2.2.1 jym TAILQ_REMOVE(&rblk->rblk_lruq, win, win_lru);
238 1.2.2.1 jym if (WINVALID(win)) {
239 1.2.2.1 jym DPRINTF(("cleanup win %p addr %p\n",
240 1.2.2.1 jym win, win->win_mem));
241 1.2.2.1 jym rumpuser_unmap(win->win_mem, WINSIZE(rblk, win));
242 1.2.2.1 jym }
243 1.2.2.1 jym kmem_free(win, sizeof(*win));
244 1.2.2.1 jym }
245 1.2.2.1 jym rblk->rblk_mmflags = 0;
246 1.2.2.1 jym }
247 1.1 pooka
248 1.1 pooka int
249 1.2.2.1 jym rumpblk_init(void)
250 1.1 pooka {
251 1.2.2.1 jym char buf[64];
252 1.1 pooka int rumpblk = RUMPBLK;
253 1.2.2.1 jym unsigned tmp;
254 1.2.2.1 jym int error, i;
255 1.2.2.1 jym
256 1.2.2.1 jym if (rumpuser_getenv("RUMP_BLKFAIL", buf, sizeof(buf), &error) == 0) {
257 1.2.2.1 jym blkfail = strtoul(buf, NULL, 10);
258 1.2.2.1 jym /* fail everything */
259 1.2.2.1 jym if (blkfail > BLKFAIL_MAX)
260 1.2.2.1 jym blkfail = BLKFAIL_MAX;
261 1.2.2.1 jym if (rumpuser_getenv("RUMP_BLKFAIL_SEED", buf, sizeof(buf),
262 1.2.2.1 jym &error) == 0) {
263 1.2.2.1 jym randstate = strtoul(buf, NULL, 10);
264 1.2.2.1 jym } else {
265 1.2.2.1 jym randstate = arc4random();
266 1.2.2.1 jym }
267 1.2.2.1 jym printf("rumpblk: FAULT INJECTION ACTIVE! fail %d/%d. "
268 1.2.2.1 jym "seed %u\n", blkfail, BLKFAIL_MAX, randstate);
269 1.2.2.1 jym } else {
270 1.2.2.1 jym blkfail = 0;
271 1.2.2.1 jym }
272 1.1 pooka
273 1.2.2.1 jym if (rumpuser_getenv("RUMP_BLKWINSIZE", buf, sizeof(buf), &error) == 0) {
274 1.2.2.1 jym printf("rumpblk: ");
275 1.2.2.1 jym tmp = strtoul(buf, NULL, 10);
276 1.2.2.1 jym if (tmp && !(tmp & (tmp-1)))
277 1.2.2.1 jym memwinsize = tmp;
278 1.2.2.1 jym else
279 1.2.2.1 jym printf("invalid RUMP_BLKWINSIZE %d, ", tmp);
280 1.2.2.1 jym printf("using %d for memwinsize\n", memwinsize);
281 1.2.2.1 jym }
282 1.2.2.1 jym if (rumpuser_getenv("RUMP_BLKWINCOUNT", buf, sizeof(buf), &error) == 0){
283 1.2.2.1 jym printf("rumpblk: ");
284 1.2.2.1 jym tmp = strtoul(buf, NULL, 10);
285 1.2.2.1 jym if (tmp)
286 1.2.2.1 jym memwincnt = tmp;
287 1.2.2.1 jym else
288 1.2.2.1 jym printf("invalid RUMP_BLKWINCOUNT %d, ", tmp);
289 1.2.2.1 jym printf("using %d for memwincount\n", memwincnt);
290 1.2.2.1 jym }
291 1.2.2.1 jym
292 1.2.2.1 jym memset(minors, 0, sizeof(minors));
293 1.2.2.1 jym for (i = 0; i < RUMPBLK_SIZE; i++) {
294 1.2.2.1 jym mutex_init(&minors[i].rblk_memmtx, MUTEX_DEFAULT, IPL_NONE);
295 1.2.2.1 jym cv_init(&minors[i].rblk_memcv, "rblkmcv");
296 1.2.2.1 jym }
297 1.2.2.1 jym
298 1.2.2.1 jym evcnt_attach_dynamic(&ev_io_total, EVCNT_TYPE_MISC, NULL,
299 1.2.2.1 jym "rumpblk", "rumpblk I/O reqs");
300 1.2.2.1 jym evcnt_attach_dynamic(&ev_io_async, EVCNT_TYPE_MISC, NULL,
301 1.2.2.1 jym "rumpblk", "rumpblk async I/O");
302 1.2.2.1 jym
303 1.2.2.1 jym evcnt_attach_dynamic(&ev_bread_total, EVCNT_TYPE_MISC, NULL,
304 1.2.2.1 jym "rumpblk", "rumpblk bytes read");
305 1.2.2.1 jym evcnt_attach_dynamic(&ev_bwrite_total, EVCNT_TYPE_MISC, NULL,
306 1.2.2.1 jym "rumpblk", "rumpblk bytes written");
307 1.2.2.1 jym evcnt_attach_dynamic(&ev_bwrite_async, EVCNT_TYPE_MISC, NULL,
308 1.2.2.1 jym "rumpblk", "rumpblk bytes written async");
309 1.2.2.1 jym
310 1.2.2.1 jym evcnt_attach_dynamic(&ev_memblk_hits, EVCNT_TYPE_MISC, NULL,
311 1.2.2.1 jym "rumpblk", "memblk window hits");
312 1.2.2.1 jym evcnt_attach_dynamic(&ev_memblk_busy, EVCNT_TYPE_MISC, NULL,
313 1.2.2.1 jym "rumpblk", "memblk all windows busy");
314 1.2.2.1 jym
315 1.2.2.1 jym if (blkfail) {
316 1.2.2.1 jym return devsw_attach("rumpblk", &rumpblk_bdevsw_fail, &rumpblk,
317 1.2.2.1 jym &rumpblk_cdevsw, &rumpblk);
318 1.2.2.1 jym } else {
319 1.2.2.1 jym return devsw_attach("rumpblk", &rumpblk_bdevsw, &rumpblk,
320 1.2.2.1 jym &rumpblk_cdevsw, &rumpblk);
321 1.2.2.1 jym }
322 1.1 pooka }
323 1.1 pooka
324 1.1 pooka int
325 1.1 pooka rumpblk_register(const char *path)
326 1.1 pooka {
327 1.1 pooka size_t len;
328 1.1 pooka int i;
329 1.1 pooka
330 1.1 pooka for (i = 0; i < RUMPBLK_SIZE; i++)
331 1.1 pooka if (minors[i].rblk_path && strcmp(minors[i].rblk_path, path)==0)
332 1.1 pooka return i;
333 1.1 pooka
334 1.1 pooka for (i = 0; i < RUMPBLK_SIZE; i++)
335 1.1 pooka if (minors[i].rblk_path == NULL)
336 1.1 pooka break;
337 1.1 pooka if (i == RUMPBLK_SIZE)
338 1.1 pooka return -1;
339 1.1 pooka
340 1.1 pooka len = strlen(path);
341 1.2.2.1 jym minors[i].rblk_path = malloc(len + 1, M_TEMP, M_WAITOK);
342 1.1 pooka strcpy(minors[i].rblk_path, path);
343 1.1 pooka minors[i].rblk_fd = -1;
344 1.1 pooka return i;
345 1.1 pooka }
346 1.1 pooka
347 1.1 pooka int
348 1.1 pooka rumpblk_open(dev_t dev, int flag, int fmt, struct lwp *l)
349 1.1 pooka {
350 1.1 pooka struct rblkdev *rblk = &minors[minor(dev)];
351 1.2.2.1 jym uint64_t fsize;
352 1.2.2.1 jym int ft, dummy;
353 1.1 pooka int error, fd;
354 1.1 pooka
355 1.1 pooka KASSERT(rblk->rblk_fd == -1);
356 1.1 pooka fd = rumpuser_open(rblk->rblk_path, OFLAGS(flag), &error);
357 1.1 pooka if (error)
358 1.1 pooka return error;
359 1.1 pooka
360 1.2.2.1 jym if (rumpuser_getfileinfo(rblk->rblk_path, &fsize, &ft, &error) == -1) {
361 1.2.2.1 jym rumpuser_close(fd, &dummy);
362 1.2.2.1 jym return error;
363 1.2.2.1 jym }
364 1.2.2.1 jym
365 1.2.2.1 jym #ifdef HAS_ODIRECT
366 1.2.2.1 jym rblk->rblk_dfd = rumpuser_open(rblk->rblk_path,
367 1.2.2.1 jym OFLAGS(flag) | O_DIRECT, &error);
368 1.2.2.1 jym if (error)
369 1.2.2.1 jym return error;
370 1.2.2.1 jym #endif
371 1.2.2.1 jym
372 1.2.2.1 jym if (ft == RUMPUSER_FT_REG) {
373 1.2.2.1 jym struct blkwin *win;
374 1.2.2.1 jym int i, winsize;
375 1.2.2.1 jym
376 1.1 pooka /*
377 1.2.2.1 jym * Use mmap to access a regular file. Allocate and
378 1.2.2.1 jym * cache initial windows here. Failure to allocate one
379 1.2.2.1 jym * means fallback to read/write i/o.
380 1.1 pooka */
381 1.1 pooka
382 1.2.2.1 jym rblk->rblk_mmflags = 0;
383 1.2.2.1 jym if (flag & FREAD)
384 1.2.2.1 jym rblk->rblk_mmflags |= RUMPUSER_FILEMMAP_READ;
385 1.2.2.1 jym if (flag & FWRITE) {
386 1.2.2.1 jym rblk->rblk_mmflags |= RUMPUSER_FILEMMAP_WRITE;
387 1.2.2.1 jym rblk->rblk_mmflags |= RUMPUSER_FILEMMAP_SHARED;
388 1.2.2.1 jym }
389 1.1 pooka
390 1.2.2.1 jym TAILQ_INIT(&rblk->rblk_lruq);
391 1.2.2.1 jym rblk->rblk_size = fsize;
392 1.2.2.1 jym rblk->rblk_fd = fd;
393 1.2.2.1 jym
394 1.2.2.1 jym for (i = 0; i < memwincnt && i * memwinsize < fsize; i++) {
395 1.2.2.1 jym win = kmem_zalloc(sizeof(*win), KM_SLEEP);
396 1.2.2.1 jym WINVALIDATE(win);
397 1.2.2.1 jym TAILQ_INSERT_TAIL(&rblk->rblk_lruq, win, win_lru);
398 1.2.2.1 jym
399 1.2.2.1 jym /*
400 1.2.2.1 jym * Allocate first windows. Here we just generally
401 1.2.2.1 jym * make sure a) we can mmap at all b) we have the
402 1.2.2.1 jym * necessary VA available
403 1.2.2.1 jym */
404 1.2.2.1 jym winsize = 1;
405 1.2.2.1 jym win = getwindow(rblk, i*memwinsize, &winsize, &error);
406 1.2.2.1 jym if (win) {
407 1.2.2.1 jym putwindow(rblk, win);
408 1.2.2.1 jym } else {
409 1.2.2.1 jym wincleanup(rblk);
410 1.2.2.1 jym break;
411 1.2.2.1 jym }
412 1.1 pooka }
413 1.2.2.1 jym
414 1.2.2.1 jym memset(&rblk->rblk_dl, 0, sizeof(rblk->rblk_dl));
415 1.2.2.1 jym rblk->rblk_pi.p_size = fsize >> DEV_BSHIFT;
416 1.1 pooka rblk->rblk_dl.d_secsize = DEV_BSIZE;
417 1.1 pooka rblk->rblk_curpi = &rblk->rblk_pi;
418 1.2.2.1 jym } else {
419 1.2.2.1 jym if (rumpuser_ioctl(fd, DIOCGDINFO, &rblk->rblk_dl,
420 1.2.2.1 jym &error) == -1) {
421 1.2.2.1 jym KASSERT(error);
422 1.2.2.1 jym rumpuser_close(fd, &dummy);
423 1.2.2.1 jym return error;
424 1.2.2.1 jym }
425 1.2.2.1 jym
426 1.2.2.1 jym rblk->rblk_fd = fd;
427 1.2.2.1 jym rblk->rblk_curpi = &rblk->rblk_dl.d_partitions[0];
428 1.1 pooka }
429 1.1 pooka
430 1.2.2.1 jym KASSERT(rblk->rblk_fd != -1);
431 1.1 pooka return 0;
432 1.1 pooka }
433 1.1 pooka
434 1.1 pooka int
435 1.1 pooka rumpblk_close(dev_t dev, int flag, int fmt, struct lwp *l)
436 1.1 pooka {
437 1.1 pooka struct rblkdev *rblk = &minors[minor(dev)];
438 1.1 pooka int dummy;
439 1.1 pooka
440 1.2.2.1 jym if (rblk->rblk_mmflags)
441 1.2.2.1 jym wincleanup(rblk);
442 1.2.2.1 jym rumpuser_fsync(rblk->rblk_fd, &dummy);
443 1.1 pooka rumpuser_close(rblk->rblk_fd, &dummy);
444 1.1 pooka rblk->rblk_fd = -1;
445 1.1 pooka
446 1.1 pooka return 0;
447 1.1 pooka }
448 1.1 pooka
449 1.1 pooka int
450 1.1 pooka rumpblk_ioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
451 1.1 pooka {
452 1.1 pooka struct rblkdev *rblk = &minors[minor(dev)];
453 1.1 pooka int rv, error;
454 1.1 pooka
455 1.1 pooka if (xfer == DIOCGPART) {
456 1.1 pooka struct partinfo *pi = (struct partinfo *)addr;
457 1.1 pooka
458 1.1 pooka pi->part = rblk->rblk_curpi;
459 1.1 pooka pi->disklab = &rblk->rblk_dl;
460 1.1 pooka
461 1.1 pooka return 0;
462 1.1 pooka }
463 1.1 pooka
464 1.1 pooka rv = rumpuser_ioctl(rblk->rblk_fd, xfer, addr, &error);
465 1.1 pooka if (rv == -1)
466 1.1 pooka return error;
467 1.1 pooka
468 1.1 pooka return 0;
469 1.1 pooka }
470 1.1 pooka
471 1.1 pooka int
472 1.1 pooka rumpblk_read(dev_t dev, struct uio *uio, int flags)
473 1.1 pooka {
474 1.1 pooka
475 1.1 pooka panic("%s: unimplemented", __func__);
476 1.1 pooka }
477 1.1 pooka
478 1.1 pooka int
479 1.1 pooka rumpblk_write(dev_t dev, struct uio *uio, int flags)
480 1.1 pooka {
481 1.1 pooka
482 1.1 pooka panic("%s: unimplemented", __func__);
483 1.1 pooka }
484 1.1 pooka
485 1.2.2.1 jym static void
486 1.2.2.1 jym dostrategy(struct buf *bp)
487 1.1 pooka {
488 1.1 pooka struct rblkdev *rblk = &minors[minor(bp->b_dev)];
489 1.1 pooka off_t off;
490 1.2.2.1 jym int async = bp->b_flags & B_ASYNC;
491 1.2.2.1 jym int error;
492 1.2.2.1 jym
493 1.2.2.1 jym /* collect statistics */
494 1.2.2.1 jym ev_io_total.ev_count++;
495 1.2.2.1 jym if (async)
496 1.2.2.1 jym ev_io_async.ev_count++;
497 1.2.2.1 jym if (BUF_ISWRITE(bp)) {
498 1.2.2.1 jym ev_bwrite_total.ev_count += bp->b_bcount;
499 1.2.2.1 jym if (async)
500 1.2.2.1 jym ev_bwrite_async.ev_count += bp->b_bcount;
501 1.2.2.1 jym } else {
502 1.2.2.1 jym ev_bread_total.ev_count++;
503 1.2.2.1 jym }
504 1.1 pooka
505 1.1 pooka off = bp->b_blkno << DEV_BSHIFT;
506 1.2.2.1 jym /*
507 1.2.2.1 jym * Do bounds checking if we're working on a file. Otherwise
508 1.2.2.1 jym * invalid file systems might attempt to read beyond EOF. This
509 1.2.2.1 jym * is bad(tm) especially on mmapped images. This is essentially
510 1.2.2.1 jym * the kernel bounds_check() routines.
511 1.2.2.1 jym */
512 1.2.2.1 jym if (rblk->rblk_size && off + bp->b_bcount > rblk->rblk_size) {
513 1.2.2.1 jym int64_t sz = rblk->rblk_size - off;
514 1.2.2.1 jym
515 1.2.2.1 jym /* EOF */
516 1.2.2.1 jym if (sz == 0) {
517 1.2.2.1 jym rump_biodone(bp, 0, 0);
518 1.2.2.1 jym return;
519 1.2.2.1 jym }
520 1.2.2.1 jym /* beyond EOF ==> error */
521 1.2.2.1 jym if (sz < 0) {
522 1.2.2.1 jym rump_biodone(bp, 0, EINVAL);
523 1.2.2.1 jym return;
524 1.2.2.1 jym }
525 1.2.2.1 jym
526 1.2.2.1 jym /* truncate to device size */
527 1.2.2.1 jym bp->b_bcount = sz;
528 1.2.2.1 jym }
529 1.2.2.1 jym
530 1.1 pooka DPRINTF(("rumpblk_strategy: 0x%x bytes %s off 0x%" PRIx64
531 1.2.2.1 jym " (0x%" PRIx64 " - 0x%" PRIx64 "), %ssync\n",
532 1.2.2.1 jym bp->b_bcount, BUF_ISREAD(bp) ? "READ" : "WRITE",
533 1.2.2.1 jym off, off, (off + bp->b_bcount), async ? "a" : ""));
534 1.2.2.1 jym
535 1.2.2.1 jym /* mmap? handle here and return */
536 1.2.2.1 jym if (rblk->rblk_mmflags) {
537 1.2.2.1 jym struct blkwin *win;
538 1.2.2.1 jym int winsize, iodone;
539 1.2.2.1 jym uint8_t *ioaddr, *bufaddr;
540 1.2.2.1 jym
541 1.2.2.1 jym for (iodone = 0; iodone < bp->b_bcount;
542 1.2.2.1 jym iodone += winsize, off += winsize) {
543 1.2.2.1 jym winsize = bp->b_bcount - iodone;
544 1.2.2.1 jym win = getwindow(rblk, off, &winsize, &error);
545 1.2.2.1 jym if (win == NULL) {
546 1.2.2.1 jym rump_biodone(bp, iodone, error);
547 1.2.2.1 jym return;
548 1.2.2.1 jym }
549 1.2.2.1 jym
550 1.2.2.1 jym ioaddr = (uint8_t *)win->win_mem + (off-STARTWIN(off));
551 1.2.2.1 jym bufaddr = (uint8_t *)bp->b_data + iodone;
552 1.2.2.1 jym
553 1.2.2.1 jym DPRINTF(("strat: %p off 0x%" PRIx64
554 1.2.2.1 jym ", ioaddr %p (%p)/buf %p\n", win,
555 1.2.2.1 jym win->win_off, ioaddr, win->win_mem, bufaddr));
556 1.2.2.1 jym if (BUF_ISREAD(bp)) {
557 1.2.2.1 jym memcpy(bufaddr, ioaddr, winsize);
558 1.2.2.1 jym } else {
559 1.2.2.1 jym memcpy(ioaddr, bufaddr, winsize);
560 1.2.2.1 jym }
561 1.2.2.1 jym
562 1.2.2.1 jym /* synchronous write, sync bits back to disk */
563 1.2.2.1 jym if (BUF_ISWRITE(bp) && !async) {
564 1.2.2.1 jym rumpuser_memsync(ioaddr, winsize, &error);
565 1.2.2.1 jym }
566 1.2.2.1 jym putwindow(rblk, win);
567 1.2.2.1 jym }
568 1.2.2.1 jym
569 1.2.2.1 jym rump_biodone(bp, bp->b_bcount, 0);
570 1.2.2.1 jym return;
571 1.2.2.1 jym }
572 1.1 pooka
573 1.1 pooka /*
574 1.1 pooka * Do I/O. We have different paths for async and sync I/O.
575 1.1 pooka * Async I/O is done by passing a request to rumpuser where
576 1.1 pooka * it is executed. The rumpuser routine then calls
577 1.1 pooka * biodone() to signal any waiters in the kernel. I/O's are
578 1.1 pooka * executed in series. Technically executing them in parallel
579 1.1 pooka * would produce better results, but then we'd need either
580 1.1 pooka * more threads or posix aio. Maybe worth investigating
581 1.1 pooka * this later.
582 1.2.2.1 jym *
583 1.2.2.1 jym * Using bufq here might be a good idea.
584 1.1 pooka */
585 1.2.2.1 jym
586 1.2.2.1 jym if (rump_threads) {
587 1.1 pooka struct rumpuser_aio *rua;
588 1.2.2.1 jym int op, fd;
589 1.2.2.1 jym
590 1.2.2.1 jym fd = rblk->rblk_fd;
591 1.2.2.1 jym if (BUF_ISREAD(bp)) {
592 1.2.2.1 jym op = RUA_OP_READ;
593 1.2.2.1 jym } else {
594 1.2.2.1 jym op = RUA_OP_WRITE;
595 1.2.2.1 jym if (!async) {
596 1.2.2.1 jym /* O_DIRECT not fully automatic yet */
597 1.2.2.1 jym #ifdef HAS_ODIRECT
598 1.2.2.1 jym if ((off & (DEV_BSIZE-1)) == 0
599 1.2.2.1 jym && ((intptr_t)bp->b_data&(DEV_BSIZE-1)) == 0
600 1.2.2.1 jym && (bp->b_bcount & (DEV_BSIZE-1)) == 0)
601 1.2.2.1 jym fd = rblk->rblk_dfd;
602 1.2.2.1 jym else
603 1.2.2.1 jym #endif
604 1.2.2.1 jym op |= RUA_OP_SYNC;
605 1.2.2.1 jym }
606 1.2.2.1 jym }
607 1.1 pooka
608 1.1 pooka rumpuser_mutex_enter(&rumpuser_aio_mtx);
609 1.2.2.1 jym while ((rumpuser_aio_head+1) % N_AIOS == rumpuser_aio_tail) {
610 1.2.2.1 jym rumpuser_cv_wait(&rumpuser_aio_cv, &rumpuser_aio_mtx);
611 1.1 pooka }
612 1.1 pooka
613 1.2 pooka rua = &rumpuser_aios[rumpuser_aio_head];
614 1.2 pooka KASSERT(rua->rua_bp == NULL);
615 1.2.2.1 jym rua->rua_fd = fd;
616 1.2 pooka rua->rua_data = bp->b_data;
617 1.2 pooka rua->rua_dlen = bp->b_bcount;
618 1.2 pooka rua->rua_off = off;
619 1.2 pooka rua->rua_bp = bp;
620 1.2.2.1 jym rua->rua_op = op;
621 1.2 pooka
622 1.1 pooka /* insert into queue & signal */
623 1.2.2.1 jym rumpuser_aio_head = (rumpuser_aio_head+1) % N_AIOS;
624 1.1 pooka rumpuser_cv_signal(&rumpuser_aio_cv);
625 1.1 pooka rumpuser_mutex_exit(&rumpuser_aio_mtx);
626 1.1 pooka } else {
627 1.1 pooka if (BUF_ISREAD(bp)) {
628 1.1 pooka rumpuser_read_bio(rblk->rblk_fd, bp->b_data,
629 1.1 pooka bp->b_bcount, off, rump_biodone, bp);
630 1.1 pooka } else {
631 1.1 pooka rumpuser_write_bio(rblk->rblk_fd, bp->b_data,
632 1.1 pooka bp->b_bcount, off, rump_biodone, bp);
633 1.1 pooka }
634 1.2.2.1 jym if (BUF_ISWRITE(bp) && !async)
635 1.2.2.1 jym rumpuser_fsync(rblk->rblk_fd, &error);
636 1.2.2.1 jym }
637 1.2.2.1 jym }
638 1.1 pooka
639 1.2.2.1 jym void
640 1.2.2.1 jym rumpblk_strategy(struct buf *bp)
641 1.2.2.1 jym {
642 1.2.2.1 jym
643 1.2.2.1 jym dostrategy(bp);
644 1.2.2.1 jym }
645 1.2.2.1 jym
646 1.2.2.1 jym /*
647 1.2.2.1 jym * Simple random number generator. This is private so that we can
648 1.2.2.1 jym * very repeatedly control which blocks will fail.
649 1.2.2.1 jym *
650 1.2.2.1 jym * <mlelstv> pooka, rand()
651 1.2.2.1 jym * <mlelstv> [paste]
652 1.2.2.1 jym */
653 1.2.2.1 jym static unsigned
654 1.2.2.1 jym gimmerand(void)
655 1.2.2.1 jym {
656 1.2.2.1 jym
657 1.2.2.1 jym return (randstate = randstate * 1103515245 + 12345) % (0x80000000L);
658 1.2.2.1 jym }
659 1.2.2.1 jym
660 1.2.2.1 jym /*
661 1.2.2.1 jym * Block device with very simple fault injection. Fails every
662 1.2.2.1 jym * n out of BLKFAIL_MAX I/O with EIO. n is determined by the env
663 1.2.2.1 jym * variable RUMP_BLKFAIL.
664 1.2.2.1 jym */
665 1.2.2.1 jym void
666 1.2.2.1 jym rumpblk_strategy_fail(struct buf *bp)
667 1.2.2.1 jym {
668 1.2.2.1 jym
669 1.2.2.1 jym if (gimmerand() % BLKFAIL_MAX >= blkfail) {
670 1.2.2.1 jym dostrategy(bp);
671 1.2.2.1 jym } else {
672 1.2.2.1 jym printf("block fault injection: failing I/O on block %lld\n",
673 1.2.2.1 jym (long long)bp->b_blkno);
674 1.2.2.1 jym bp->b_error = EIO;
675 1.2.2.1 jym biodone(bp);
676 1.1 pooka }
677 1.1 pooka }
678