Home | History | Annotate | Line # | Download | only in rumpvfs
rumpblk.c revision 1.42.4.1
      1  1.42.4.1  bouyer /*	$NetBSD: rumpblk.c,v 1.42.4.1 2011/02/08 16:20:04 bouyer 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.29   pooka  *
     44      1.29   pooka  * However, it is quite costly in writing large amounts of
     45      1.29   pooka  * file data, since old contents cannot merely be overwritten, but
     46      1.29   pooka  * must be paged in first before replacing (i.e. r/m/w).  Ideally,
     47      1.29   pooka  * we should use directio.  The problem is that directio can fail
     48      1.29   pooka  * silently causing improper file system semantics (i.e. unflushed
     49      1.29   pooka  * data).  Therefore, default to mmap for now.  Even so, directio
     50      1.29   pooka  * _should_ be safe and can be enabled by compiling this module
     51      1.29   pooka  * with -DHAS_DIRECTIO.
     52       1.1   pooka  */
     53       1.1   pooka 
     54       1.1   pooka #include <sys/cdefs.h>
     55  1.42.4.1  bouyer __KERNEL_RCSID(0, "$NetBSD: rumpblk.c,v 1.42.4.1 2011/02/08 16:20:04 bouyer Exp $");
     56       1.1   pooka 
     57       1.1   pooka #include <sys/param.h>
     58       1.1   pooka #include <sys/buf.h>
     59       1.1   pooka #include <sys/conf.h>
     60      1.16   pooka #include <sys/condvar.h>
     61       1.1   pooka #include <sys/disklabel.h>
     62      1.18   pooka #include <sys/evcnt.h>
     63       1.1   pooka #include <sys/fcntl.h>
     64       1.1   pooka #include <sys/kmem.h>
     65       1.1   pooka #include <sys/malloc.h>
     66      1.16   pooka #include <sys/queue.h>
     67       1.1   pooka #include <sys/stat.h>
     68       1.1   pooka 
     69       1.1   pooka #include <rump/rumpuser.h>
     70       1.1   pooka 
     71       1.1   pooka #include "rump_private.h"
     72       1.1   pooka #include "rump_vfs_private.h"
     73       1.1   pooka 
     74  1.42.4.1  bouyer /*
     75  1.42.4.1  bouyer  * O_DIRECT is the fastest alternative, but since it falls back to
     76  1.42.4.1  bouyer  * non-direct writes silently, I am not sure it will always be 100% safe.
     77  1.42.4.1  bouyer  * Use it and play with it, but do that with caution.
     78  1.42.4.1  bouyer  */
     79  1.42.4.1  bouyer #if 0
     80  1.42.4.1  bouyer #define HAS_ODIRECT
     81  1.42.4.1  bouyer #endif
     82  1.42.4.1  bouyer 
     83      1.16   pooka #if 0
     84      1.16   pooka #define DPRINTF(x) printf x
     85      1.16   pooka #else
     86      1.16   pooka #define DPRINTF(x)
     87      1.16   pooka #endif
     88      1.16   pooka 
     89      1.19   pooka /* Default: 16 x 1MB windows */
     90      1.19   pooka unsigned memwinsize = (1<<20);
     91      1.19   pooka unsigned memwincnt = 16;
     92      1.19   pooka 
     93      1.36   pooka #define STARTWIN(off)		((off) & ~((off_t)memwinsize-1))
     94      1.16   pooka #define INWIN(win,off)		((win)->win_off == STARTWIN(off))
     95      1.41   pooka #define WINSIZE(rblk, win)	(MIN((rblk->rblk_hostsize-win->win_off), \
     96      1.41   pooka 				      memwinsize))
     97      1.16   pooka #define WINVALID(win)		((win)->win_off != (off_t)-1)
     98      1.16   pooka #define WINVALIDATE(win)	((win)->win_off = (off_t)-1)
     99      1.16   pooka struct blkwin {
    100      1.16   pooka 	off_t win_off;
    101      1.16   pooka 	void *win_mem;
    102      1.16   pooka 	int win_refcnt;
    103      1.16   pooka 
    104      1.16   pooka 	TAILQ_ENTRY(blkwin) win_lru;
    105      1.16   pooka };
    106      1.16   pooka 
    107       1.1   pooka #define RUMPBLK_SIZE 16
    108       1.1   pooka static struct rblkdev {
    109       1.1   pooka 	char *rblk_path;
    110       1.1   pooka 	int rblk_fd;
    111  1.42.4.1  bouyer 	int rblk_mode;
    112      1.20   pooka #ifdef HAS_ODIRECT
    113      1.20   pooka 	int rblk_dfd;
    114      1.20   pooka #endif
    115      1.26   pooka 	uint64_t rblk_size;
    116      1.27   pooka 	uint64_t rblk_hostoffset;
    117      1.41   pooka 	uint64_t rblk_hostsize;
    118      1.27   pooka 	int rblk_ftype;
    119      1.16   pooka 
    120      1.16   pooka 	/* for mmap */
    121      1.16   pooka 	int rblk_mmflags;
    122      1.16   pooka 	kmutex_t rblk_memmtx;
    123      1.16   pooka 	kcondvar_t rblk_memcv;
    124      1.16   pooka 	TAILQ_HEAD(winlru, blkwin) rblk_lruq;
    125      1.16   pooka 	bool rblk_waiting;
    126       1.1   pooka 
    127      1.31   pooka 	struct disklabel rblk_label;
    128       1.1   pooka } minors[RUMPBLK_SIZE];
    129       1.1   pooka 
    130      1.20   pooka static struct evcnt ev_io_total;
    131      1.20   pooka static struct evcnt ev_io_async;
    132      1.20   pooka 
    133      1.20   pooka static struct evcnt ev_memblk_hits;
    134      1.20   pooka static struct evcnt ev_memblk_busy;
    135      1.20   pooka 
    136      1.20   pooka static struct evcnt ev_bwrite_total;
    137      1.20   pooka static struct evcnt ev_bwrite_async;
    138      1.20   pooka static struct evcnt ev_bread_total;
    139      1.18   pooka 
    140       1.1   pooka dev_type_open(rumpblk_open);
    141       1.1   pooka dev_type_close(rumpblk_close);
    142       1.1   pooka dev_type_read(rumpblk_read);
    143       1.1   pooka dev_type_write(rumpblk_write);
    144       1.1   pooka dev_type_ioctl(rumpblk_ioctl);
    145       1.1   pooka dev_type_strategy(rumpblk_strategy);
    146       1.3   pooka dev_type_strategy(rumpblk_strategy_fail);
    147       1.1   pooka dev_type_dump(rumpblk_dump);
    148       1.1   pooka dev_type_size(rumpblk_size);
    149       1.1   pooka 
    150       1.1   pooka static const struct bdevsw rumpblk_bdevsw = {
    151       1.1   pooka 	rumpblk_open, rumpblk_close, rumpblk_strategy, rumpblk_ioctl,
    152       1.1   pooka 	nodump, nosize, D_DISK
    153       1.1   pooka };
    154       1.1   pooka 
    155       1.3   pooka static const struct bdevsw rumpblk_bdevsw_fail = {
    156       1.3   pooka 	rumpblk_open, rumpblk_close, rumpblk_strategy_fail, rumpblk_ioctl,
    157       1.3   pooka 	nodump, nosize, D_DISK
    158       1.3   pooka };
    159       1.3   pooka 
    160       1.1   pooka static const struct cdevsw rumpblk_cdevsw = {
    161       1.1   pooka 	rumpblk_open, rumpblk_close, rumpblk_read, rumpblk_write,
    162       1.1   pooka 	rumpblk_ioctl, nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    163       1.1   pooka };
    164       1.1   pooka 
    165  1.42.4.1  bouyer static int backend_open(struct rblkdev *, const char *);
    166  1.42.4.1  bouyer static int backend_close(struct rblkdev *);
    167  1.42.4.1  bouyer 
    168       1.3   pooka /* fail every n out of BLKFAIL_MAX */
    169       1.3   pooka #define BLKFAIL_MAX 10000
    170       1.3   pooka static int blkfail;
    171       1.3   pooka static unsigned randstate;
    172      1.24   pooka static kmutex_t rumpblk_lock;
    173      1.37   pooka static int sectshift = DEV_BSHIFT;
    174       1.1   pooka 
    175      1.31   pooka static void
    176      1.31   pooka makedefaultlabel(struct disklabel *lp, off_t size, int part)
    177      1.31   pooka {
    178      1.31   pooka 	int i;
    179      1.31   pooka 
    180      1.31   pooka 	memset(lp, 0, sizeof(*lp));
    181      1.31   pooka 
    182      1.31   pooka 	lp->d_secperunit = size;
    183      1.37   pooka 	lp->d_secsize = 1 << sectshift;
    184      1.37   pooka 	lp->d_nsectors = size >> sectshift;
    185      1.31   pooka 	lp->d_ntracks = 1;
    186      1.31   pooka 	lp->d_ncylinders = 1;
    187      1.31   pooka 	lp->d_secpercyl = lp->d_nsectors;
    188      1.31   pooka 
    189      1.31   pooka 	/* oh dear oh dear */
    190      1.31   pooka 	strncpy(lp->d_typename, "rumpd", sizeof(lp->d_typename));
    191      1.31   pooka 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    192      1.31   pooka 
    193      1.31   pooka 	lp->d_type = DTYPE_RUMPD;
    194      1.31   pooka 	lp->d_rpm = 11;
    195      1.31   pooka 	lp->d_interleave = 1;
    196      1.31   pooka 	lp->d_flags = 0;
    197      1.31   pooka 
    198      1.31   pooka 	/* XXX: RAW_PART handling? */
    199      1.31   pooka 	for (i = 0; i < part; i++) {
    200      1.31   pooka 		lp->d_partitions[i].p_fstype = FS_UNUSED;
    201      1.31   pooka 	}
    202      1.37   pooka 	lp->d_partitions[part].p_size = size >> sectshift;
    203      1.31   pooka 	lp->d_npartitions = part+1;
    204      1.31   pooka 	/* XXX: file system type? */
    205      1.31   pooka 
    206      1.31   pooka 	lp->d_magic = DISKMAGIC;
    207      1.31   pooka 	lp->d_magic2 = DISKMAGIC;
    208      1.31   pooka 	lp->d_checksum = 0; /* XXX */
    209      1.31   pooka }
    210      1.31   pooka 
    211      1.16   pooka static struct blkwin *
    212      1.16   pooka getwindow(struct rblkdev *rblk, off_t off, int *wsize, int *error)
    213      1.16   pooka {
    214      1.16   pooka 	struct blkwin *win;
    215      1.16   pooka 
    216      1.16   pooka 	mutex_enter(&rblk->rblk_memmtx);
    217      1.16   pooka  retry:
    218      1.16   pooka 	/* search for window */
    219      1.16   pooka 	TAILQ_FOREACH(win, &rblk->rblk_lruq, win_lru) {
    220      1.16   pooka 		if (INWIN(win, off) && WINVALID(win))
    221      1.16   pooka 			break;
    222      1.16   pooka 	}
    223      1.16   pooka 
    224      1.16   pooka 	/* found?  return */
    225      1.16   pooka 	if (win) {
    226      1.20   pooka 		ev_memblk_hits.ev_count++;
    227      1.16   pooka 		TAILQ_REMOVE(&rblk->rblk_lruq, win, win_lru);
    228      1.16   pooka 		goto good;
    229      1.16   pooka 	}
    230      1.16   pooka 
    231      1.16   pooka 	/*
    232      1.16   pooka 	 * Else, create new window.  If the least recently used is not
    233      1.16   pooka 	 * currently in use, reuse that.  Otherwise we need to wait.
    234      1.16   pooka 	 */
    235      1.16   pooka 	win = TAILQ_LAST(&rblk->rblk_lruq, winlru);
    236      1.16   pooka 	if (win->win_refcnt == 0) {
    237      1.16   pooka 		TAILQ_REMOVE(&rblk->rblk_lruq, win, win_lru);
    238      1.16   pooka 		mutex_exit(&rblk->rblk_memmtx);
    239      1.16   pooka 
    240      1.16   pooka 		if (WINVALID(win)) {
    241      1.16   pooka 			DPRINTF(("win %p, unmap mem %p, off 0x%" PRIx64 "\n",
    242      1.16   pooka 			    win, win->win_mem, win->win_off));
    243      1.16   pooka 			rumpuser_unmap(win->win_mem, WINSIZE(rblk, win));
    244      1.16   pooka 			WINVALIDATE(win);
    245      1.16   pooka 		}
    246      1.16   pooka 
    247      1.16   pooka 		win->win_off = STARTWIN(off);
    248      1.16   pooka 		win->win_mem = rumpuser_filemmap(rblk->rblk_fd, win->win_off,
    249      1.16   pooka 		    WINSIZE(rblk, win), rblk->rblk_mmflags, error);
    250      1.16   pooka 		DPRINTF(("win %p, off 0x%" PRIx64 ", mem %p\n",
    251      1.16   pooka 		    win, win->win_off, win->win_mem));
    252      1.16   pooka 
    253      1.16   pooka 		mutex_enter(&rblk->rblk_memmtx);
    254      1.16   pooka 		if (win->win_mem == NULL) {
    255      1.16   pooka 			WINVALIDATE(win);
    256      1.16   pooka 			TAILQ_INSERT_TAIL(&rblk->rblk_lruq, win, win_lru);
    257      1.16   pooka 			mutex_exit(&rblk->rblk_memmtx);
    258      1.16   pooka 			return NULL;
    259      1.16   pooka 		}
    260      1.16   pooka 	} else {
    261      1.16   pooka 		DPRINTF(("memwin wait\n"));
    262      1.20   pooka 		ev_memblk_busy.ev_count++;
    263      1.18   pooka 
    264      1.16   pooka 		rblk->rblk_waiting = true;
    265      1.16   pooka 		cv_wait(&rblk->rblk_memcv, &rblk->rblk_memmtx);
    266      1.16   pooka 		goto retry;
    267      1.16   pooka 	}
    268      1.16   pooka 
    269      1.16   pooka  good:
    270      1.16   pooka 	KASSERT(win);
    271      1.16   pooka 	win->win_refcnt++;
    272      1.16   pooka 	TAILQ_INSERT_HEAD(&rblk->rblk_lruq, win, win_lru);
    273      1.16   pooka 	mutex_exit(&rblk->rblk_memmtx);
    274      1.19   pooka 	*wsize = MIN(*wsize, memwinsize - (off-win->win_off));
    275      1.16   pooka 	KASSERT(*wsize);
    276      1.16   pooka 
    277      1.16   pooka 	return win;
    278      1.16   pooka }
    279      1.16   pooka 
    280      1.16   pooka static void
    281      1.16   pooka putwindow(struct rblkdev *rblk, struct blkwin *win)
    282      1.16   pooka {
    283      1.16   pooka 
    284      1.16   pooka 	mutex_enter(&rblk->rblk_memmtx);
    285      1.16   pooka 	if (--win->win_refcnt == 0 && rblk->rblk_waiting) {
    286      1.16   pooka 		rblk->rblk_waiting = false;
    287      1.42   pooka 		cv_broadcast(&rblk->rblk_memcv);
    288      1.16   pooka 	}
    289      1.16   pooka 	KASSERT(win->win_refcnt >= 0);
    290      1.16   pooka 	mutex_exit(&rblk->rblk_memmtx);
    291      1.16   pooka }
    292      1.16   pooka 
    293      1.16   pooka static void
    294      1.16   pooka wincleanup(struct rblkdev *rblk)
    295      1.16   pooka {
    296      1.16   pooka 	struct blkwin *win;
    297      1.16   pooka 
    298      1.16   pooka 	while ((win = TAILQ_FIRST(&rblk->rblk_lruq)) != NULL) {
    299      1.16   pooka 		TAILQ_REMOVE(&rblk->rblk_lruq, win, win_lru);
    300      1.16   pooka 		if (WINVALID(win)) {
    301      1.16   pooka 			DPRINTF(("cleanup win %p addr %p\n",
    302      1.16   pooka 			    win, win->win_mem));
    303      1.16   pooka 			rumpuser_unmap(win->win_mem, WINSIZE(rblk, win));
    304      1.16   pooka 		}
    305      1.16   pooka 		kmem_free(win, sizeof(*win));
    306      1.16   pooka 	}
    307      1.16   pooka 	rblk->rblk_mmflags = 0;
    308      1.16   pooka }
    309      1.16   pooka 
    310       1.1   pooka int
    311       1.8  cegger rumpblk_init(void)
    312       1.1   pooka {
    313       1.3   pooka 	char buf[64];
    314      1.38   pooka 	devmajor_t rumpblkmaj = RUMPBLK_DEVMAJOR;
    315      1.19   pooka 	unsigned tmp;
    316      1.16   pooka 	int error, i;
    317       1.3   pooka 
    318      1.24   pooka 	mutex_init(&rumpblk_lock, MUTEX_DEFAULT, IPL_NONE);
    319      1.24   pooka 
    320       1.3   pooka 	if (rumpuser_getenv("RUMP_BLKFAIL", buf, sizeof(buf), &error) == 0) {
    321       1.3   pooka 		blkfail = strtoul(buf, NULL, 10);
    322       1.3   pooka 		/* fail everything */
    323       1.3   pooka 		if (blkfail > BLKFAIL_MAX)
    324       1.3   pooka 			blkfail = BLKFAIL_MAX;
    325       1.3   pooka 		if (rumpuser_getenv("RUMP_BLKFAIL_SEED", buf, sizeof(buf),
    326       1.3   pooka 		    &error) == 0) {
    327       1.3   pooka 			randstate = strtoul(buf, NULL, 10);
    328       1.3   pooka 		} else {
    329      1.23   pooka 			randstate = arc4random();
    330       1.3   pooka 		}
    331      1.22   pooka 		printf("rumpblk: FAULT INJECTION ACTIVE! fail %d/%d. "
    332      1.22   pooka 		    "seed %u\n", blkfail, BLKFAIL_MAX, randstate);
    333       1.3   pooka 	} else {
    334       1.3   pooka 		blkfail = 0;
    335       1.3   pooka 	}
    336       1.1   pooka 
    337      1.19   pooka 	if (rumpuser_getenv("RUMP_BLKWINSIZE", buf, sizeof(buf), &error) == 0) {
    338      1.19   pooka 		printf("rumpblk: ");
    339      1.19   pooka 		tmp = strtoul(buf, NULL, 10);
    340      1.19   pooka 		if (tmp && !(tmp & (tmp-1)))
    341      1.19   pooka 			memwinsize = tmp;
    342      1.19   pooka 		else
    343      1.19   pooka 			printf("invalid RUMP_BLKWINSIZE %d, ", tmp);
    344      1.19   pooka 		printf("using %d for memwinsize\n", memwinsize);
    345      1.19   pooka 	}
    346      1.19   pooka 	if (rumpuser_getenv("RUMP_BLKWINCOUNT", buf, sizeof(buf), &error) == 0){
    347      1.19   pooka 		printf("rumpblk: ");
    348      1.19   pooka 		tmp = strtoul(buf, NULL, 10);
    349      1.19   pooka 		if (tmp)
    350      1.19   pooka 			memwincnt = tmp;
    351      1.19   pooka 		else
    352      1.19   pooka 			printf("invalid RUMP_BLKWINCOUNT %d, ", tmp);
    353      1.19   pooka 		printf("using %d for memwincount\n", memwincnt);
    354      1.19   pooka 	}
    355      1.37   pooka 	if (rumpuser_getenv("RUMP_BLKSECTSHIFT", buf, sizeof(buf), &error)==0){
    356      1.37   pooka 		printf("rumpblk: ");
    357      1.37   pooka 		tmp = strtoul(buf, NULL, 10);
    358      1.37   pooka 		if (tmp >= DEV_BSHIFT)
    359      1.37   pooka 			sectshift = tmp;
    360      1.37   pooka 		else
    361      1.37   pooka 			printf("RUMP_BLKSECTSHIFT must be least %d (now %d), ",
    362      1.37   pooka 			   DEV_BSHIFT, tmp);
    363      1.37   pooka 		printf("using %d for sector shift (size %d)\n",
    364      1.37   pooka 		    sectshift, 1<<sectshift);
    365      1.37   pooka 	}
    366      1.19   pooka 
    367      1.16   pooka 	memset(minors, 0, sizeof(minors));
    368      1.16   pooka 	for (i = 0; i < RUMPBLK_SIZE; i++) {
    369      1.16   pooka 		mutex_init(&minors[i].rblk_memmtx, MUTEX_DEFAULT, IPL_NONE);
    370      1.16   pooka 		cv_init(&minors[i].rblk_memcv, "rblkmcv");
    371  1.42.4.1  bouyer 		minors[i].rblk_fd = -1;
    372      1.16   pooka 	}
    373      1.16   pooka 
    374      1.20   pooka 	evcnt_attach_dynamic(&ev_io_total, EVCNT_TYPE_MISC, NULL,
    375      1.39   pooka 	    "rumpblk", "I/O reqs");
    376      1.20   pooka 	evcnt_attach_dynamic(&ev_io_async, EVCNT_TYPE_MISC, NULL,
    377      1.39   pooka 	    "rumpblk", "async I/O");
    378      1.20   pooka 
    379      1.20   pooka 	evcnt_attach_dynamic(&ev_bread_total, EVCNT_TYPE_MISC, NULL,
    380      1.39   pooka 	    "rumpblk", "bytes read");
    381      1.20   pooka 	evcnt_attach_dynamic(&ev_bwrite_total, EVCNT_TYPE_MISC, NULL,
    382      1.39   pooka 	    "rumpblk", "bytes written");
    383      1.20   pooka 	evcnt_attach_dynamic(&ev_bwrite_async, EVCNT_TYPE_MISC, NULL,
    384      1.39   pooka 	    "rumpblk", "bytes written async");
    385      1.20   pooka 
    386      1.20   pooka 	evcnt_attach_dynamic(&ev_memblk_hits, EVCNT_TYPE_MISC, NULL,
    387      1.39   pooka 	    "rumpblk", "window hits");
    388      1.20   pooka 	evcnt_attach_dynamic(&ev_memblk_busy, EVCNT_TYPE_MISC, NULL,
    389      1.39   pooka 	    "rumpblk", "all windows busy");
    390      1.18   pooka 
    391       1.3   pooka 	if (blkfail) {
    392      1.38   pooka 		return devsw_attach("rumpblk",
    393      1.38   pooka 		    &rumpblk_bdevsw_fail, &rumpblkmaj,
    394      1.38   pooka 		    &rumpblk_cdevsw, &rumpblkmaj);
    395       1.3   pooka 	} else {
    396      1.38   pooka 		return devsw_attach("rumpblk",
    397      1.38   pooka 		    &rumpblk_bdevsw, &rumpblkmaj,
    398      1.38   pooka 		    &rumpblk_cdevsw, &rumpblkmaj);
    399       1.3   pooka 	}
    400       1.1   pooka }
    401       1.1   pooka 
    402       1.1   pooka int
    403      1.27   pooka rumpblk_register(const char *path, devminor_t *dmin,
    404      1.27   pooka 	uint64_t offset, uint64_t size)
    405       1.1   pooka {
    406      1.27   pooka 	struct rblkdev *rblk;
    407      1.24   pooka 	uint64_t flen;
    408       1.1   pooka 	size_t len;
    409      1.30   pooka 	int ftype, error, i;
    410      1.24   pooka 
    411      1.27   pooka 	/* devices might not report correct size unless they're open */
    412      1.30   pooka 	if (rumpuser_getfileinfo(path, &flen, &ftype, &error) == -1)
    413      1.27   pooka 		return error;
    414      1.27   pooka 
    415      1.24   pooka 	/* verify host file is of supported type */
    416      1.24   pooka 	if (!(ftype == RUMPUSER_FT_REG
    417      1.24   pooka 	   || ftype == RUMPUSER_FT_BLK
    418      1.24   pooka 	   || ftype == RUMPUSER_FT_CHR))
    419      1.24   pooka 		return EINVAL;
    420       1.1   pooka 
    421      1.24   pooka 	mutex_enter(&rumpblk_lock);
    422      1.24   pooka 	for (i = 0; i < RUMPBLK_SIZE; i++) {
    423      1.24   pooka 		if (minors[i].rblk_path&&strcmp(minors[i].rblk_path, path)==0) {
    424      1.24   pooka 			mutex_exit(&rumpblk_lock);
    425      1.24   pooka 			*dmin = i;
    426      1.24   pooka 			return 0;
    427      1.24   pooka 		}
    428      1.24   pooka 	}
    429       1.1   pooka 
    430       1.1   pooka 	for (i = 0; i < RUMPBLK_SIZE; i++)
    431       1.1   pooka 		if (minors[i].rblk_path == NULL)
    432       1.1   pooka 			break;
    433      1.24   pooka 	if (i == RUMPBLK_SIZE) {
    434      1.24   pooka 		mutex_exit(&rumpblk_lock);
    435      1.24   pooka 		return EBUSY;
    436      1.24   pooka 	}
    437       1.1   pooka 
    438      1.27   pooka 	rblk = &minors[i];
    439  1.42.4.1  bouyer 	rblk->rblk_path = __UNCONST("taken");
    440  1.42.4.1  bouyer 	mutex_exit(&rumpblk_lock);
    441  1.42.4.1  bouyer 
    442       1.1   pooka 	len = strlen(path);
    443      1.27   pooka 	rblk->rblk_path = malloc(len + 1, M_TEMP, M_WAITOK);
    444      1.27   pooka 	strcpy(rblk->rblk_path, path);
    445      1.27   pooka 	rblk->rblk_hostoffset = offset;
    446      1.33   pooka 	if (size != RUMPBLK_SIZENOTSET) {
    447      1.27   pooka 		KASSERT(size + offset <= flen);
    448      1.27   pooka 		rblk->rblk_size = size;
    449      1.27   pooka 	} else {
    450      1.27   pooka 		KASSERT(offset < flen);
    451      1.27   pooka 		rblk->rblk_size = flen - offset;
    452      1.27   pooka 	}
    453      1.41   pooka 	rblk->rblk_hostsize = flen;
    454      1.27   pooka 	rblk->rblk_ftype = ftype;
    455      1.31   pooka 	makedefaultlabel(&rblk->rblk_label, rblk->rblk_size, i);
    456  1.42.4.1  bouyer 
    457  1.42.4.1  bouyer 	if ((error = backend_open(rblk, path)) != 0) {
    458  1.42.4.1  bouyer 		memset(&rblk->rblk_label, 0, sizeof(rblk->rblk_label));
    459  1.42.4.1  bouyer 		free(rblk->rblk_path, M_TEMP);
    460  1.42.4.1  bouyer 		rblk->rblk_path = NULL;
    461  1.42.4.1  bouyer 		return error;
    462  1.42.4.1  bouyer 	}
    463      1.24   pooka 
    464      1.24   pooka 	*dmin = i;
    465      1.24   pooka 	return 0;
    466       1.1   pooka }
    467       1.1   pooka 
    468      1.40   pooka /*
    469      1.40   pooka  * Unregister rumpblk.  It's the callers responsibility to make
    470      1.40   pooka  * sure it's no longer in use.
    471      1.40   pooka  */
    472      1.40   pooka int
    473      1.40   pooka rumpblk_deregister(const char *path)
    474      1.40   pooka {
    475      1.40   pooka 	struct rblkdev *rblk;
    476      1.40   pooka 	int i;
    477      1.40   pooka 
    478      1.40   pooka 	mutex_enter(&rumpblk_lock);
    479      1.40   pooka 	for (i = 0; i < RUMPBLK_SIZE; i++) {
    480      1.40   pooka 		if (minors[i].rblk_path&&strcmp(minors[i].rblk_path, path)==0) {
    481      1.40   pooka 			break;
    482      1.40   pooka 		}
    483      1.40   pooka 	}
    484      1.40   pooka 	mutex_exit(&rumpblk_lock);
    485      1.40   pooka 
    486      1.40   pooka 	if (i == RUMPBLK_SIZE)
    487      1.40   pooka 		return ENOENT;
    488      1.40   pooka 
    489      1.40   pooka 	rblk = &minors[i];
    490  1.42.4.1  bouyer 	backend_close(rblk);
    491      1.40   pooka 
    492      1.40   pooka 	wincleanup(rblk);
    493      1.40   pooka 	free(rblk->rblk_path, M_TEMP);
    494      1.40   pooka 	memset(&rblk->rblk_label, 0, sizeof(rblk->rblk_label));
    495  1.42.4.1  bouyer 	rblk->rblk_path = NULL;
    496      1.40   pooka 
    497      1.40   pooka 	return 0;
    498      1.40   pooka }
    499      1.40   pooka 
    500  1.42.4.1  bouyer static int
    501  1.42.4.1  bouyer backend_open(struct rblkdev *rblk, const char *path)
    502       1.1   pooka {
    503       1.1   pooka 	int error, fd;
    504       1.1   pooka 
    505  1.42.4.1  bouyer 	KASSERT(rblk->rblk_fd == -1);
    506  1.42.4.1  bouyer 	fd = rumpuser_open(path, O_RDWR, &error);
    507  1.42.4.1  bouyer 	if (error) {
    508  1.42.4.1  bouyer 		fd = rumpuser_open(path, O_RDONLY, &error);
    509  1.42.4.1  bouyer 		if (error)
    510  1.42.4.1  bouyer 			return error;
    511  1.42.4.1  bouyer 		rblk->rblk_mode = FREAD;
    512      1.27   pooka 
    513  1.42.4.1  bouyer #ifdef HAS_ODIRECT
    514  1.42.4.1  bouyer 		rblk->rblk_dfd = rumpuser_open(path,
    515  1.42.4.1  bouyer 		    O_RDONLY | O_DIRECT, &error);
    516  1.42.4.1  bouyer 		if (error) {
    517  1.42.4.1  bouyer 			close(fd);
    518  1.42.4.1  bouyer 			return error;
    519  1.42.4.1  bouyer 		}
    520  1.42.4.1  bouyer #endif
    521  1.42.4.1  bouyer 	} else {
    522  1.42.4.1  bouyer 		rblk->rblk_mode = FREAD|FWRITE;
    523       1.1   pooka 
    524      1.20   pooka #ifdef HAS_ODIRECT
    525  1.42.4.1  bouyer 		rblk->rblk_dfd = rumpuser_open(path,
    526  1.42.4.1  bouyer 		    O_RDWR | O_DIRECT, &error);
    527  1.42.4.1  bouyer 		if (error) {
    528  1.42.4.1  bouyer 			close(fd);
    529  1.42.4.1  bouyer 			return error;
    530  1.42.4.1  bouyer 		}
    531      1.20   pooka #endif
    532  1.42.4.1  bouyer 	}
    533      1.20   pooka 
    534      1.27   pooka 	if (rblk->rblk_ftype == RUMPUSER_FT_REG) {
    535  1.42.4.1  bouyer 		uint64_t fsize= rblk->rblk_hostsize, off= rblk->rblk_hostoffset;
    536      1.16   pooka 		struct blkwin *win;
    537      1.16   pooka 		int i, winsize;
    538      1.16   pooka 
    539       1.1   pooka 		/*
    540      1.16   pooka 		 * Use mmap to access a regular file.  Allocate and
    541      1.16   pooka 		 * cache initial windows here.  Failure to allocate one
    542      1.16   pooka 		 * means fallback to read/write i/o.
    543       1.1   pooka 		 */
    544       1.9   pooka 
    545      1.16   pooka 		rblk->rblk_mmflags = 0;
    546  1.42.4.1  bouyer 		if (rblk->rblk_mode & FREAD)
    547      1.16   pooka 			rblk->rblk_mmflags |= RUMPUSER_FILEMMAP_READ;
    548  1.42.4.1  bouyer 		if (rblk->rblk_mode & FWRITE) {
    549      1.16   pooka 			rblk->rblk_mmflags |= RUMPUSER_FILEMMAP_WRITE;
    550      1.16   pooka 			rblk->rblk_mmflags |= RUMPUSER_FILEMMAP_SHARED;
    551      1.16   pooka 		}
    552      1.16   pooka 
    553      1.16   pooka 		TAILQ_INIT(&rblk->rblk_lruq);
    554      1.16   pooka 		rblk->rblk_fd = fd;
    555      1.16   pooka 
    556      1.28   pooka 		for (i = 0; i < memwincnt && off + i*memwinsize < fsize; i++) {
    557      1.16   pooka 			win = kmem_zalloc(sizeof(*win), KM_SLEEP);
    558      1.16   pooka 			WINVALIDATE(win);
    559      1.16   pooka 			TAILQ_INSERT_TAIL(&rblk->rblk_lruq, win, win_lru);
    560      1.16   pooka 
    561      1.16   pooka 			/*
    562      1.16   pooka 			 * Allocate first windows.  Here we just generally
    563      1.16   pooka 			 * make sure a) we can mmap at all b) we have the
    564      1.16   pooka 			 * necessary VA available
    565      1.16   pooka 			 */
    566      1.29   pooka 			winsize = memwinsize;
    567      1.28   pooka 			win = getwindow(rblk, off + i*memwinsize, &winsize,
    568      1.28   pooka 			    &error);
    569      1.16   pooka 			if (win) {
    570      1.16   pooka 				putwindow(rblk, win);
    571      1.16   pooka 			} else {
    572      1.16   pooka 				wincleanup(rblk);
    573      1.16   pooka 				break;
    574      1.12   pooka 			}
    575       1.9   pooka 		}
    576      1.35   pooka 	} else {
    577      1.35   pooka 		rblk->rblk_fd = fd;
    578       1.1   pooka 	}
    579       1.1   pooka 
    580      1.16   pooka 	KASSERT(rblk->rblk_fd != -1);
    581       1.1   pooka 	return 0;
    582       1.1   pooka }
    583       1.1   pooka 
    584  1.42.4.1  bouyer static int
    585  1.42.4.1  bouyer backend_close(struct rblkdev *rblk)
    586       1.1   pooka {
    587       1.1   pooka 	int dummy;
    588       1.1   pooka 
    589      1.16   pooka 	if (rblk->rblk_mmflags)
    590      1.16   pooka 		wincleanup(rblk);
    591      1.16   pooka 	rumpuser_fsync(rblk->rblk_fd, &dummy);
    592       1.1   pooka 	rumpuser_close(rblk->rblk_fd, &dummy);
    593       1.1   pooka 	rblk->rblk_fd = -1;
    594  1.42.4.1  bouyer #ifdef HAS_ODIRECT
    595  1.42.4.1  bouyer 	if (rblk->rblk_dfd != -1) {
    596  1.42.4.1  bouyer 		rumpuser_close(rblk->rblk_dfd, &dummy);
    597  1.42.4.1  bouyer 		rblk->rblk_dfd = -1;
    598  1.42.4.1  bouyer 	}
    599  1.42.4.1  bouyer #endif
    600  1.42.4.1  bouyer 
    601  1.42.4.1  bouyer 	return 0;
    602  1.42.4.1  bouyer }
    603  1.42.4.1  bouyer 
    604  1.42.4.1  bouyer int
    605  1.42.4.1  bouyer rumpblk_open(dev_t dev, int flag, int fmt, struct lwp *l)
    606  1.42.4.1  bouyer {
    607  1.42.4.1  bouyer 	struct rblkdev *rblk = &minors[minor(dev)];
    608  1.42.4.1  bouyer 
    609  1.42.4.1  bouyer 	if (rblk->rblk_fd == -1)
    610  1.42.4.1  bouyer 		return ENXIO;
    611  1.42.4.1  bouyer 
    612  1.42.4.1  bouyer 	if (((flag & (FREAD|FWRITE)) & ~rblk->rblk_mode) != 0) {
    613  1.42.4.1  bouyer 		return EACCES;
    614  1.42.4.1  bouyer 	}
    615  1.42.4.1  bouyer 
    616  1.42.4.1  bouyer 	return 0;
    617  1.42.4.1  bouyer }
    618  1.42.4.1  bouyer 
    619  1.42.4.1  bouyer int
    620  1.42.4.1  bouyer rumpblk_close(dev_t dev, int flag, int fmt, struct lwp *l)
    621  1.42.4.1  bouyer {
    622       1.1   pooka 
    623       1.1   pooka 	return 0;
    624       1.1   pooka }
    625       1.1   pooka 
    626       1.1   pooka int
    627       1.1   pooka rumpblk_ioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
    628       1.1   pooka {
    629      1.31   pooka 	devminor_t dmin = minor(dev);
    630      1.31   pooka 	struct rblkdev *rblk = &minors[dmin];
    631      1.31   pooka 	struct partinfo *pi;
    632      1.31   pooka 	int error = 0;
    633      1.31   pooka 
    634      1.31   pooka 	/* well, me should support a few more, but we don't for now */
    635      1.31   pooka 	switch (xfer) {
    636      1.31   pooka 	case DIOCGDINFO:
    637      1.31   pooka 		*(struct disklabel *)addr = rblk->rblk_label;
    638      1.31   pooka 		break;
    639      1.31   pooka 
    640      1.31   pooka 	case DIOCGPART:
    641      1.31   pooka 		pi = addr;
    642      1.31   pooka 		pi->part = &rblk->rblk_label.d_partitions[DISKPART(dmin)];
    643      1.31   pooka 		pi->disklab = &rblk->rblk_label;
    644      1.31   pooka 		break;
    645      1.32   pooka 
    646      1.32   pooka 	/* it's synced enough along the write path */
    647      1.32   pooka 	case DIOCCACHESYNC:
    648      1.32   pooka 		break;
    649      1.32   pooka 
    650      1.31   pooka 	default:
    651      1.31   pooka 		error = ENOTTY;
    652      1.31   pooka 		break;
    653       1.1   pooka 	}
    654       1.1   pooka 
    655      1.31   pooka 	return error;
    656       1.1   pooka }
    657       1.1   pooka 
    658      1.25   pooka static int
    659      1.25   pooka do_physio(dev_t dev, struct uio *uio, int which)
    660      1.25   pooka {
    661      1.25   pooka 	void (*strat)(struct buf *);
    662      1.25   pooka 
    663      1.25   pooka 	if (blkfail)
    664      1.25   pooka 		strat = rumpblk_strategy_fail;
    665      1.25   pooka 	else
    666      1.25   pooka 		strat = rumpblk_strategy;
    667      1.25   pooka 
    668      1.25   pooka 	return physio(strat, NULL, dev, which, minphys, uio);
    669      1.25   pooka }
    670      1.25   pooka 
    671       1.1   pooka int
    672       1.1   pooka rumpblk_read(dev_t dev, struct uio *uio, int flags)
    673       1.1   pooka {
    674       1.1   pooka 
    675      1.25   pooka 	return do_physio(dev, uio, B_READ);
    676       1.1   pooka }
    677       1.1   pooka 
    678       1.1   pooka int
    679       1.1   pooka rumpblk_write(dev_t dev, struct uio *uio, int flags)
    680       1.1   pooka {
    681       1.1   pooka 
    682      1.25   pooka 	return do_physio(dev, uio, B_WRITE);
    683       1.1   pooka }
    684       1.1   pooka 
    685       1.3   pooka static void
    686       1.3   pooka dostrategy(struct buf *bp)
    687       1.1   pooka {
    688       1.1   pooka 	struct rblkdev *rblk = &minors[minor(bp->b_dev)];
    689       1.1   pooka 	off_t off;
    690      1.21   pooka 	int async = bp->b_flags & B_ASYNC;
    691      1.21   pooka 	int error;
    692       1.1   pooka 
    693  1.42.4.1  bouyer 	if (bp->b_bcount % (1<<sectshift) != 0) {
    694  1.42.4.1  bouyer 		rump_biodone(bp, 0, EINVAL);
    695  1.42.4.1  bouyer 		return;
    696  1.42.4.1  bouyer 	}
    697  1.42.4.1  bouyer 
    698      1.20   pooka 	/* collect statistics */
    699      1.20   pooka 	ev_io_total.ev_count++;
    700      1.20   pooka 	if (async)
    701      1.20   pooka 		ev_io_async.ev_count++;
    702      1.20   pooka 	if (BUF_ISWRITE(bp)) {
    703      1.20   pooka 		ev_bwrite_total.ev_count += bp->b_bcount;
    704      1.20   pooka 		if (async)
    705      1.20   pooka 			ev_bwrite_async.ev_count += bp->b_bcount;
    706      1.20   pooka 	} else {
    707      1.20   pooka 		ev_bread_total.ev_count++;
    708      1.20   pooka 	}
    709      1.20   pooka 
    710  1.42.4.1  bouyer 	/*
    711  1.42.4.1  bouyer 	 * b_blkno is always in terms of DEV_BSIZE, and since we need
    712  1.42.4.1  bouyer 	 * to translate to a byte offset for the host read, this
    713  1.42.4.1  bouyer 	 * calculation does not need sectshift.
    714  1.42.4.1  bouyer 	 */
    715  1.42.4.1  bouyer 	off = bp->b_blkno << DEV_BSHIFT;
    716  1.42.4.1  bouyer 
    717      1.11   pooka 	/*
    718      1.11   pooka 	 * Do bounds checking if we're working on a file.  Otherwise
    719      1.11   pooka 	 * invalid file systems might attempt to read beyond EOF.  This
    720      1.11   pooka 	 * is bad(tm) especially on mmapped images.  This is essentially
    721      1.11   pooka 	 * the kernel bounds_check() routines.
    722      1.11   pooka 	 */
    723      1.27   pooka 	if (off + bp->b_bcount > rblk->rblk_size) {
    724      1.11   pooka 		int64_t sz = rblk->rblk_size - off;
    725      1.11   pooka 
    726      1.11   pooka 		/* EOF */
    727      1.11   pooka 		if (sz == 0) {
    728      1.11   pooka 			rump_biodone(bp, 0, 0);
    729      1.11   pooka 			return;
    730      1.11   pooka 		}
    731      1.11   pooka 		/* beyond EOF ==> error */
    732      1.11   pooka 		if (sz < 0) {
    733      1.11   pooka 			rump_biodone(bp, 0, EINVAL);
    734      1.11   pooka 			return;
    735      1.11   pooka 		}
    736      1.11   pooka 
    737      1.11   pooka 		/* truncate to device size */
    738      1.11   pooka 		bp->b_bcount = sz;
    739      1.11   pooka 	}
    740      1.11   pooka 
    741      1.34   pooka 	off += rblk->rblk_hostoffset;
    742       1.1   pooka 	DPRINTF(("rumpblk_strategy: 0x%x bytes %s off 0x%" PRIx64
    743      1.20   pooka 	    " (0x%" PRIx64 " - 0x%" PRIx64 "), %ssync\n",
    744      1.16   pooka 	    bp->b_bcount, BUF_ISREAD(bp) ? "READ" : "WRITE",
    745      1.20   pooka 	    off, off, (off + bp->b_bcount), async ? "a" : ""));
    746       1.1   pooka 
    747      1.16   pooka 	/* mmap?  handle here and return */
    748      1.16   pooka 	if (rblk->rblk_mmflags) {
    749      1.16   pooka 		struct blkwin *win;
    750      1.16   pooka 		int winsize, iodone;
    751      1.16   pooka 		uint8_t *ioaddr, *bufaddr;
    752      1.16   pooka 
    753      1.16   pooka 		for (iodone = 0; iodone < bp->b_bcount;
    754      1.16   pooka 		    iodone += winsize, off += winsize) {
    755      1.16   pooka 			winsize = bp->b_bcount - iodone;
    756      1.16   pooka 			win = getwindow(rblk, off, &winsize, &error);
    757      1.16   pooka 			if (win == NULL) {
    758      1.16   pooka 				rump_biodone(bp, iodone, error);
    759      1.16   pooka 				return;
    760      1.16   pooka 			}
    761      1.16   pooka 
    762      1.16   pooka 			ioaddr = (uint8_t *)win->win_mem + (off-STARTWIN(off));
    763      1.16   pooka 			bufaddr = (uint8_t *)bp->b_data + iodone;
    764      1.11   pooka 
    765      1.16   pooka 			DPRINTF(("strat: %p off 0x%" PRIx64
    766      1.16   pooka 			    ", ioaddr %p (%p)/buf %p\n", win,
    767      1.16   pooka 			    win->win_off, ioaddr, win->win_mem, bufaddr));
    768      1.16   pooka 			if (BUF_ISREAD(bp)) {
    769      1.16   pooka 				memcpy(bufaddr, ioaddr, winsize);
    770      1.16   pooka 			} else {
    771      1.16   pooka 				memcpy(ioaddr, bufaddr, winsize);
    772      1.16   pooka 			}
    773      1.16   pooka 
    774      1.16   pooka 			/* synchronous write, sync bits back to disk */
    775      1.16   pooka 			if (BUF_ISWRITE(bp) && !async) {
    776      1.16   pooka 				rumpuser_memsync(ioaddr, winsize, &error);
    777      1.16   pooka 			}
    778      1.16   pooka 			putwindow(rblk, win);
    779       1.9   pooka 		}
    780       1.9   pooka 
    781       1.9   pooka 		rump_biodone(bp, bp->b_bcount, 0);
    782       1.9   pooka 		return;
    783       1.9   pooka 	}
    784       1.9   pooka 
    785       1.1   pooka 	/*
    786       1.1   pooka 	 * Do I/O.  We have different paths for async and sync I/O.
    787       1.1   pooka 	 * Async I/O is done by passing a request to rumpuser where
    788       1.1   pooka 	 * it is executed.  The rumpuser routine then calls
    789       1.1   pooka 	 * biodone() to signal any waiters in the kernel.  I/O's are
    790       1.1   pooka 	 * executed in series.  Technically executing them in parallel
    791       1.1   pooka 	 * would produce better results, but then we'd need either
    792       1.1   pooka 	 * more threads or posix aio.  Maybe worth investigating
    793       1.1   pooka 	 * this later.
    794      1.14   pooka 	 *
    795      1.14   pooka 	 * Using bufq here might be a good idea.
    796       1.1   pooka 	 */
    797      1.20   pooka 
    798      1.13   pooka 	if (rump_threads) {
    799       1.1   pooka 		struct rumpuser_aio *rua;
    800      1.20   pooka 		int op, fd;
    801      1.20   pooka 
    802      1.20   pooka 		fd = rblk->rblk_fd;
    803      1.20   pooka 		if (BUF_ISREAD(bp)) {
    804      1.20   pooka 			op = RUA_OP_READ;
    805      1.20   pooka 		} else {
    806      1.20   pooka 			op = RUA_OP_WRITE;
    807      1.20   pooka 			if (!async) {
    808      1.20   pooka 				/* O_DIRECT not fully automatic yet */
    809      1.20   pooka #ifdef HAS_ODIRECT
    810      1.37   pooka 				if ((off & ((1<<sectshift)-1)) == 0
    811      1.37   pooka 				    && ((intptr_t)bp->b_data
    812      1.37   pooka 				      & ((1<<sectshift)-1)) == 0
    813      1.37   pooka 				    && (bp->b_bcount & ((1<<sectshift)-1)) == 0)
    814      1.20   pooka 					fd = rblk->rblk_dfd;
    815      1.20   pooka 				else
    816      1.20   pooka #endif
    817      1.20   pooka 					op |= RUA_OP_SYNC;
    818      1.20   pooka 			}
    819      1.20   pooka 		}
    820       1.1   pooka 
    821       1.1   pooka 		rumpuser_mutex_enter(&rumpuser_aio_mtx);
    822      1.20   pooka 		while ((rumpuser_aio_head+1) % N_AIOS == rumpuser_aio_tail) {
    823      1.13   pooka 			rumpuser_cv_wait(&rumpuser_aio_cv, &rumpuser_aio_mtx);
    824      1.20   pooka 		}
    825       1.1   pooka 
    826       1.2   pooka 		rua = &rumpuser_aios[rumpuser_aio_head];
    827       1.2   pooka 		KASSERT(rua->rua_bp == NULL);
    828      1.20   pooka 		rua->rua_fd = fd;
    829       1.2   pooka 		rua->rua_data = bp->b_data;
    830       1.2   pooka 		rua->rua_dlen = bp->b_bcount;
    831       1.2   pooka 		rua->rua_off = off;
    832       1.2   pooka 		rua->rua_bp = bp;
    833      1.20   pooka 		rua->rua_op = op;
    834       1.2   pooka 
    835       1.1   pooka 		/* insert into queue & signal */
    836       1.6   pooka 		rumpuser_aio_head = (rumpuser_aio_head+1) % N_AIOS;
    837       1.1   pooka 		rumpuser_cv_signal(&rumpuser_aio_cv);
    838       1.1   pooka 		rumpuser_mutex_exit(&rumpuser_aio_mtx);
    839       1.1   pooka 	} else {
    840       1.1   pooka 		if (BUF_ISREAD(bp)) {
    841       1.1   pooka 			rumpuser_read_bio(rblk->rblk_fd, bp->b_data,
    842       1.1   pooka 			    bp->b_bcount, off, rump_biodone, bp);
    843       1.1   pooka 		} else {
    844       1.1   pooka 			rumpuser_write_bio(rblk->rblk_fd, bp->b_data,
    845       1.1   pooka 			    bp->b_bcount, off, rump_biodone, bp);
    846       1.1   pooka 		}
    847      1.20   pooka 		if (BUF_ISWRITE(bp) && !async)
    848      1.20   pooka 			rumpuser_fsync(rblk->rblk_fd, &error);
    849       1.1   pooka 	}
    850       1.1   pooka }
    851       1.3   pooka 
    852       1.3   pooka void
    853       1.3   pooka rumpblk_strategy(struct buf *bp)
    854       1.3   pooka {
    855       1.3   pooka 
    856       1.3   pooka 	dostrategy(bp);
    857       1.3   pooka }
    858       1.3   pooka 
    859       1.3   pooka /*
    860       1.4   pooka  * Simple random number generator.  This is private so that we can
    861       1.4   pooka  * very repeatedly control which blocks will fail.
    862       1.4   pooka  *
    863       1.3   pooka  * <mlelstv> pooka, rand()
    864       1.3   pooka  * <mlelstv> [paste]
    865       1.3   pooka  */
    866       1.3   pooka static unsigned
    867       1.3   pooka gimmerand(void)
    868       1.3   pooka {
    869       1.3   pooka 
    870       1.3   pooka 	return (randstate = randstate * 1103515245 + 12345) % (0x80000000L);
    871       1.3   pooka }
    872       1.3   pooka 
    873       1.3   pooka /*
    874       1.3   pooka  * Block device with very simple fault injection.  Fails every
    875       1.3   pooka  * n out of BLKFAIL_MAX I/O with EIO.  n is determined by the env
    876       1.3   pooka  * variable RUMP_BLKFAIL.
    877       1.3   pooka  */
    878       1.3   pooka void
    879       1.3   pooka rumpblk_strategy_fail(struct buf *bp)
    880       1.3   pooka {
    881       1.3   pooka 
    882       1.3   pooka 	if (gimmerand() % BLKFAIL_MAX >= blkfail) {
    883       1.3   pooka 		dostrategy(bp);
    884       1.3   pooka 	} else {
    885       1.3   pooka 		printf("block fault injection: failing I/O on block %lld\n",
    886       1.3   pooka 		    (long long)bp->b_blkno);
    887       1.3   pooka 		bp->b_error = EIO;
    888       1.3   pooka 		biodone(bp);
    889       1.3   pooka 	}
    890       1.3   pooka }
    891