Home | History | Annotate | Line # | Download | only in rumpvfs
rumpblk.c revision 1.46.4.3
      1  1.46.4.3    yamt /*	$NetBSD: rumpblk.c,v 1.46.4.3 2014/05/22 11:41:16 yamt 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.1   pooka  */
     38       1.1   pooka 
     39       1.1   pooka #include <sys/cdefs.h>
     40  1.46.4.3    yamt __KERNEL_RCSID(0, "$NetBSD: rumpblk.c,v 1.46.4.3 2014/05/22 11:41:16 yamt Exp $");
     41       1.1   pooka 
     42       1.1   pooka #include <sys/param.h>
     43       1.1   pooka #include <sys/buf.h>
     44       1.1   pooka #include <sys/conf.h>
     45      1.16   pooka #include <sys/condvar.h>
     46       1.1   pooka #include <sys/disklabel.h>
     47      1.18   pooka #include <sys/evcnt.h>
     48       1.1   pooka #include <sys/fcntl.h>
     49       1.1   pooka #include <sys/kmem.h>
     50       1.1   pooka #include <sys/malloc.h>
     51      1.16   pooka #include <sys/queue.h>
     52       1.1   pooka #include <sys/stat.h>
     53  1.46.4.1    yamt #include <sys/cprng.h>
     54       1.1   pooka 
     55       1.1   pooka #include <rump/rumpuser.h>
     56       1.1   pooka 
     57       1.1   pooka #include "rump_private.h"
     58       1.1   pooka #include "rump_vfs_private.h"
     59       1.1   pooka 
     60      1.16   pooka #if 0
     61      1.16   pooka #define DPRINTF(x) printf x
     62      1.16   pooka #else
     63      1.16   pooka #define DPRINTF(x)
     64      1.16   pooka #endif
     65      1.16   pooka 
     66       1.1   pooka #define RUMPBLK_SIZE 16
     67       1.1   pooka static struct rblkdev {
     68       1.1   pooka 	char *rblk_path;
     69       1.1   pooka 	int rblk_fd;
     70      1.45   pooka 	int rblk_mode;
     71  1.46.4.3    yamt 
     72      1.26   pooka 	uint64_t rblk_size;
     73      1.27   pooka 	uint64_t rblk_hostoffset;
     74      1.41   pooka 	uint64_t rblk_hostsize;
     75      1.27   pooka 	int rblk_ftype;
     76      1.16   pooka 
     77      1.31   pooka 	struct disklabel rblk_label;
     78       1.1   pooka } minors[RUMPBLK_SIZE];
     79       1.1   pooka 
     80      1.20   pooka static struct evcnt ev_io_total;
     81      1.20   pooka static struct evcnt ev_io_async;
     82      1.20   pooka 
     83      1.20   pooka static struct evcnt ev_bwrite_total;
     84      1.20   pooka static struct evcnt ev_bwrite_async;
     85      1.20   pooka static struct evcnt ev_bread_total;
     86      1.18   pooka 
     87       1.1   pooka dev_type_open(rumpblk_open);
     88       1.1   pooka dev_type_close(rumpblk_close);
     89       1.1   pooka dev_type_read(rumpblk_read);
     90       1.1   pooka dev_type_write(rumpblk_write);
     91       1.1   pooka dev_type_ioctl(rumpblk_ioctl);
     92       1.1   pooka dev_type_strategy(rumpblk_strategy);
     93       1.3   pooka dev_type_strategy(rumpblk_strategy_fail);
     94       1.1   pooka dev_type_dump(rumpblk_dump);
     95       1.1   pooka dev_type_size(rumpblk_size);
     96       1.1   pooka 
     97       1.1   pooka static const struct bdevsw rumpblk_bdevsw = {
     98  1.46.4.3    yamt 	.d_open = rumpblk_open,
     99  1.46.4.3    yamt 	.d_close = rumpblk_close,
    100  1.46.4.3    yamt 	.d_strategy = rumpblk_strategy,
    101  1.46.4.3    yamt 	.d_ioctl = rumpblk_ioctl,
    102  1.46.4.3    yamt 	.d_dump = nodump,
    103  1.46.4.3    yamt 	.d_psize = nosize,
    104  1.46.4.3    yamt 	.d_flag = D_DISK
    105       1.1   pooka };
    106       1.1   pooka 
    107       1.3   pooka static const struct bdevsw rumpblk_bdevsw_fail = {
    108  1.46.4.3    yamt 	.d_open = rumpblk_open,
    109  1.46.4.3    yamt 	.d_close = rumpblk_close,
    110  1.46.4.3    yamt 	.d_strategy = rumpblk_strategy_fail,
    111  1.46.4.3    yamt 	.d_ioctl = rumpblk_ioctl,
    112  1.46.4.3    yamt 	.d_dump = nodump,
    113  1.46.4.3    yamt 	.d_psize = nosize,
    114  1.46.4.3    yamt 	.d_flag = D_DISK
    115       1.3   pooka };
    116       1.3   pooka 
    117       1.1   pooka static const struct cdevsw rumpblk_cdevsw = {
    118  1.46.4.3    yamt 	.d_open = rumpblk_open,
    119  1.46.4.3    yamt 	.d_close = rumpblk_close,
    120  1.46.4.3    yamt 	.d_read = rumpblk_read,
    121  1.46.4.3    yamt 	.d_write = rumpblk_write,
    122  1.46.4.3    yamt 	.d_ioctl = rumpblk_ioctl,
    123  1.46.4.3    yamt 	.d_stop = nostop,
    124  1.46.4.3    yamt 	.d_tty = notty,
    125  1.46.4.3    yamt 	.d_poll = nopoll,
    126  1.46.4.3    yamt 	.d_mmap = nommap,
    127  1.46.4.3    yamt 	.d_kqfilter = nokqfilter,
    128  1.46.4.3    yamt 	.d_flag = D_DISK
    129       1.1   pooka };
    130       1.1   pooka 
    131      1.45   pooka static int backend_open(struct rblkdev *, const char *);
    132      1.45   pooka static int backend_close(struct rblkdev *);
    133      1.45   pooka 
    134       1.3   pooka /* fail every n out of BLKFAIL_MAX */
    135       1.3   pooka #define BLKFAIL_MAX 10000
    136       1.3   pooka static int blkfail;
    137       1.3   pooka static unsigned randstate;
    138      1.24   pooka static kmutex_t rumpblk_lock;
    139      1.37   pooka static int sectshift = DEV_BSHIFT;
    140       1.1   pooka 
    141      1.31   pooka static void
    142      1.31   pooka makedefaultlabel(struct disklabel *lp, off_t size, int part)
    143      1.31   pooka {
    144      1.31   pooka 	int i;
    145      1.31   pooka 
    146      1.31   pooka 	memset(lp, 0, sizeof(*lp));
    147      1.31   pooka 
    148      1.31   pooka 	lp->d_secperunit = size;
    149      1.37   pooka 	lp->d_secsize = 1 << sectshift;
    150      1.37   pooka 	lp->d_nsectors = size >> sectshift;
    151      1.31   pooka 	lp->d_ntracks = 1;
    152      1.31   pooka 	lp->d_ncylinders = 1;
    153      1.31   pooka 	lp->d_secpercyl = lp->d_nsectors;
    154      1.31   pooka 
    155      1.31   pooka 	/* oh dear oh dear */
    156      1.31   pooka 	strncpy(lp->d_typename, "rumpd", sizeof(lp->d_typename));
    157      1.31   pooka 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    158      1.31   pooka 
    159      1.31   pooka 	lp->d_type = DTYPE_RUMPD;
    160      1.31   pooka 	lp->d_rpm = 11;
    161      1.31   pooka 	lp->d_interleave = 1;
    162      1.31   pooka 	lp->d_flags = 0;
    163      1.31   pooka 
    164      1.31   pooka 	/* XXX: RAW_PART handling? */
    165      1.31   pooka 	for (i = 0; i < part; i++) {
    166      1.31   pooka 		lp->d_partitions[i].p_fstype = FS_UNUSED;
    167      1.31   pooka 	}
    168      1.37   pooka 	lp->d_partitions[part].p_size = size >> sectshift;
    169      1.31   pooka 	lp->d_npartitions = part+1;
    170      1.31   pooka 	/* XXX: file system type? */
    171      1.31   pooka 
    172      1.31   pooka 	lp->d_magic = DISKMAGIC;
    173      1.31   pooka 	lp->d_magic2 = DISKMAGIC;
    174      1.31   pooka 	lp->d_checksum = 0; /* XXX */
    175      1.31   pooka }
    176      1.31   pooka 
    177       1.1   pooka int
    178       1.8  cegger rumpblk_init(void)
    179       1.1   pooka {
    180       1.3   pooka 	char buf[64];
    181      1.38   pooka 	devmajor_t rumpblkmaj = RUMPBLK_DEVMAJOR;
    182      1.19   pooka 	unsigned tmp;
    183  1.46.4.3    yamt 	int i;
    184       1.3   pooka 
    185      1.24   pooka 	mutex_init(&rumpblk_lock, MUTEX_DEFAULT, IPL_NONE);
    186      1.24   pooka 
    187  1.46.4.3    yamt 	if (rumpuser_getparam("RUMP_BLKFAIL", buf, sizeof(buf)) == 0) {
    188       1.3   pooka 		blkfail = strtoul(buf, NULL, 10);
    189       1.3   pooka 		/* fail everything */
    190       1.3   pooka 		if (blkfail > BLKFAIL_MAX)
    191       1.3   pooka 			blkfail = BLKFAIL_MAX;
    192  1.46.4.3    yamt 		if (rumpuser_getparam("RUMP_BLKFAIL_SEED",
    193  1.46.4.3    yamt 		    buf, sizeof(buf)) == 0) {
    194       1.3   pooka 			randstate = strtoul(buf, NULL, 10);
    195       1.3   pooka 		} else {
    196  1.46.4.1    yamt 			randstate = cprng_fast32();
    197       1.3   pooka 		}
    198      1.22   pooka 		printf("rumpblk: FAULT INJECTION ACTIVE! fail %d/%d. "
    199      1.22   pooka 		    "seed %u\n", blkfail, BLKFAIL_MAX, randstate);
    200       1.3   pooka 	} else {
    201       1.3   pooka 		blkfail = 0;
    202       1.3   pooka 	}
    203       1.1   pooka 
    204  1.46.4.3    yamt 	if (rumpuser_getparam("RUMP_BLKSECTSHIFT", buf, sizeof(buf)) == 0) {
    205      1.37   pooka 		printf("rumpblk: ");
    206      1.37   pooka 		tmp = strtoul(buf, NULL, 10);
    207      1.37   pooka 		if (tmp >= DEV_BSHIFT)
    208      1.37   pooka 			sectshift = tmp;
    209      1.37   pooka 		else
    210      1.37   pooka 			printf("RUMP_BLKSECTSHIFT must be least %d (now %d), ",
    211      1.37   pooka 			   DEV_BSHIFT, tmp);
    212      1.37   pooka 		printf("using %d for sector shift (size %d)\n",
    213      1.37   pooka 		    sectshift, 1<<sectshift);
    214      1.37   pooka 	}
    215      1.19   pooka 
    216      1.16   pooka 	memset(minors, 0, sizeof(minors));
    217      1.16   pooka 	for (i = 0; i < RUMPBLK_SIZE; i++) {
    218      1.46   pooka 		minors[i].rblk_fd = -1;
    219      1.16   pooka 	}
    220      1.16   pooka 
    221      1.20   pooka 	evcnt_attach_dynamic(&ev_io_total, EVCNT_TYPE_MISC, NULL,
    222      1.39   pooka 	    "rumpblk", "I/O reqs");
    223      1.20   pooka 	evcnt_attach_dynamic(&ev_io_async, EVCNT_TYPE_MISC, NULL,
    224      1.39   pooka 	    "rumpblk", "async I/O");
    225      1.20   pooka 
    226      1.20   pooka 	evcnt_attach_dynamic(&ev_bread_total, EVCNT_TYPE_MISC, NULL,
    227      1.39   pooka 	    "rumpblk", "bytes read");
    228      1.20   pooka 	evcnt_attach_dynamic(&ev_bwrite_total, EVCNT_TYPE_MISC, NULL,
    229      1.39   pooka 	    "rumpblk", "bytes written");
    230      1.20   pooka 	evcnt_attach_dynamic(&ev_bwrite_async, EVCNT_TYPE_MISC, NULL,
    231      1.39   pooka 	    "rumpblk", "bytes written async");
    232      1.20   pooka 
    233       1.3   pooka 	if (blkfail) {
    234      1.38   pooka 		return devsw_attach("rumpblk",
    235      1.38   pooka 		    &rumpblk_bdevsw_fail, &rumpblkmaj,
    236      1.38   pooka 		    &rumpblk_cdevsw, &rumpblkmaj);
    237       1.3   pooka 	} else {
    238      1.38   pooka 		return devsw_attach("rumpblk",
    239      1.38   pooka 		    &rumpblk_bdevsw, &rumpblkmaj,
    240      1.38   pooka 		    &rumpblk_cdevsw, &rumpblkmaj);
    241       1.3   pooka 	}
    242       1.1   pooka }
    243       1.1   pooka 
    244       1.1   pooka int
    245      1.27   pooka rumpblk_register(const char *path, devminor_t *dmin,
    246      1.27   pooka 	uint64_t offset, uint64_t size)
    247       1.1   pooka {
    248      1.27   pooka 	struct rblkdev *rblk;
    249      1.24   pooka 	uint64_t flen;
    250       1.1   pooka 	size_t len;
    251      1.30   pooka 	int ftype, error, i;
    252      1.24   pooka 
    253      1.27   pooka 	/* devices might not report correct size unless they're open */
    254  1.46.4.3    yamt 	if ((error = rumpuser_getfileinfo(path, &flen, &ftype)) != 0)
    255      1.27   pooka 		return error;
    256      1.27   pooka 
    257      1.24   pooka 	/* verify host file is of supported type */
    258      1.24   pooka 	if (!(ftype == RUMPUSER_FT_REG
    259      1.24   pooka 	   || ftype == RUMPUSER_FT_BLK
    260      1.24   pooka 	   || ftype == RUMPUSER_FT_CHR))
    261      1.24   pooka 		return EINVAL;
    262       1.1   pooka 
    263      1.24   pooka 	mutex_enter(&rumpblk_lock);
    264      1.24   pooka 	for (i = 0; i < RUMPBLK_SIZE; i++) {
    265      1.24   pooka 		if (minors[i].rblk_path&&strcmp(minors[i].rblk_path, path)==0) {
    266      1.24   pooka 			mutex_exit(&rumpblk_lock);
    267      1.24   pooka 			*dmin = i;
    268      1.24   pooka 			return 0;
    269      1.24   pooka 		}
    270      1.24   pooka 	}
    271       1.1   pooka 
    272       1.1   pooka 	for (i = 0; i < RUMPBLK_SIZE; i++)
    273       1.1   pooka 		if (minors[i].rblk_path == NULL)
    274       1.1   pooka 			break;
    275      1.24   pooka 	if (i == RUMPBLK_SIZE) {
    276      1.24   pooka 		mutex_exit(&rumpblk_lock);
    277      1.24   pooka 		return EBUSY;
    278      1.24   pooka 	}
    279       1.1   pooka 
    280      1.27   pooka 	rblk = &minors[i];
    281      1.45   pooka 	rblk->rblk_path = __UNCONST("taken");
    282      1.45   pooka 	mutex_exit(&rumpblk_lock);
    283      1.45   pooka 
    284       1.1   pooka 	len = strlen(path);
    285      1.27   pooka 	rblk->rblk_path = malloc(len + 1, M_TEMP, M_WAITOK);
    286      1.27   pooka 	strcpy(rblk->rblk_path, path);
    287      1.27   pooka 	rblk->rblk_hostoffset = offset;
    288      1.33   pooka 	if (size != RUMPBLK_SIZENOTSET) {
    289      1.27   pooka 		KASSERT(size + offset <= flen);
    290      1.27   pooka 		rblk->rblk_size = size;
    291      1.27   pooka 	} else {
    292      1.27   pooka 		KASSERT(offset < flen);
    293      1.27   pooka 		rblk->rblk_size = flen - offset;
    294      1.27   pooka 	}
    295      1.41   pooka 	rblk->rblk_hostsize = flen;
    296      1.27   pooka 	rblk->rblk_ftype = ftype;
    297      1.31   pooka 	makedefaultlabel(&rblk->rblk_label, rblk->rblk_size, i);
    298      1.45   pooka 
    299      1.45   pooka 	if ((error = backend_open(rblk, path)) != 0) {
    300      1.45   pooka 		memset(&rblk->rblk_label, 0, sizeof(rblk->rblk_label));
    301      1.45   pooka 		free(rblk->rblk_path, M_TEMP);
    302      1.45   pooka 		rblk->rblk_path = NULL;
    303      1.45   pooka 		return error;
    304      1.45   pooka 	}
    305      1.24   pooka 
    306      1.24   pooka 	*dmin = i;
    307      1.24   pooka 	return 0;
    308       1.1   pooka }
    309       1.1   pooka 
    310      1.40   pooka /*
    311      1.40   pooka  * Unregister rumpblk.  It's the callers responsibility to make
    312      1.40   pooka  * sure it's no longer in use.
    313      1.40   pooka  */
    314      1.40   pooka int
    315      1.40   pooka rumpblk_deregister(const char *path)
    316      1.40   pooka {
    317      1.40   pooka 	struct rblkdev *rblk;
    318      1.40   pooka 	int i;
    319      1.40   pooka 
    320      1.40   pooka 	mutex_enter(&rumpblk_lock);
    321      1.40   pooka 	for (i = 0; i < RUMPBLK_SIZE; i++) {
    322      1.40   pooka 		if (minors[i].rblk_path&&strcmp(minors[i].rblk_path, path)==0) {
    323      1.40   pooka 			break;
    324      1.40   pooka 		}
    325      1.40   pooka 	}
    326      1.40   pooka 	mutex_exit(&rumpblk_lock);
    327      1.40   pooka 
    328      1.40   pooka 	if (i == RUMPBLK_SIZE)
    329      1.40   pooka 		return ENOENT;
    330      1.40   pooka 
    331      1.40   pooka 	rblk = &minors[i];
    332      1.45   pooka 	backend_close(rblk);
    333      1.40   pooka 
    334      1.40   pooka 	free(rblk->rblk_path, M_TEMP);
    335      1.45   pooka 	memset(&rblk->rblk_label, 0, sizeof(rblk->rblk_label));
    336      1.40   pooka 	rblk->rblk_path = NULL;
    337      1.40   pooka 
    338      1.40   pooka 	return 0;
    339      1.40   pooka }
    340      1.40   pooka 
    341      1.45   pooka static int
    342      1.45   pooka backend_open(struct rblkdev *rblk, const char *path)
    343       1.1   pooka {
    344       1.1   pooka 	int error, fd;
    345       1.1   pooka 
    346      1.46   pooka 	KASSERT(rblk->rblk_fd == -1);
    347  1.46.4.3    yamt 	error = rumpuser_open(path,
    348  1.46.4.3    yamt 	    RUMPUSER_OPEN_RDWR | RUMPUSER_OPEN_BIO, &fd);
    349      1.45   pooka 	if (error) {
    350  1.46.4.3    yamt 		error = rumpuser_open(path,
    351  1.46.4.3    yamt 		    RUMPUSER_OPEN_RDONLY | RUMPUSER_OPEN_BIO, &fd);
    352      1.45   pooka 		if (error)
    353      1.45   pooka 			return error;
    354      1.45   pooka 		rblk->rblk_mode = FREAD;
    355      1.45   pooka 	} else {
    356      1.45   pooka 		rblk->rblk_mode = FREAD|FWRITE;
    357       1.1   pooka 	}
    358       1.1   pooka 
    359  1.46.4.3    yamt 	rblk->rblk_fd = fd;
    360      1.16   pooka 	KASSERT(rblk->rblk_fd != -1);
    361       1.1   pooka 	return 0;
    362       1.1   pooka }
    363       1.1   pooka 
    364      1.45   pooka static int
    365      1.45   pooka backend_close(struct rblkdev *rblk)
    366       1.1   pooka {
    367       1.1   pooka 
    368  1.46.4.3    yamt 	rumpuser_close(rblk->rblk_fd);
    369       1.1   pooka 	rblk->rblk_fd = -1;
    370      1.45   pooka 
    371      1.45   pooka 	return 0;
    372      1.45   pooka }
    373      1.45   pooka 
    374      1.45   pooka int
    375      1.45   pooka rumpblk_open(dev_t dev, int flag, int fmt, struct lwp *l)
    376      1.45   pooka {
    377      1.45   pooka 	struct rblkdev *rblk = &minors[minor(dev)];
    378      1.45   pooka 
    379      1.45   pooka 	if (rblk->rblk_fd == -1)
    380      1.45   pooka 		return ENXIO;
    381      1.45   pooka 
    382      1.45   pooka 	if (((flag & (FREAD|FWRITE)) & ~rblk->rblk_mode) != 0) {
    383      1.45   pooka 		return EACCES;
    384      1.45   pooka 	}
    385      1.45   pooka 
    386      1.45   pooka 	return 0;
    387      1.45   pooka }
    388      1.45   pooka 
    389      1.45   pooka int
    390      1.45   pooka rumpblk_close(dev_t dev, int flag, int fmt, struct lwp *l)
    391      1.45   pooka {
    392       1.1   pooka 
    393       1.1   pooka 	return 0;
    394       1.1   pooka }
    395       1.1   pooka 
    396       1.1   pooka int
    397       1.1   pooka rumpblk_ioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
    398       1.1   pooka {
    399      1.31   pooka 	devminor_t dmin = minor(dev);
    400      1.31   pooka 	struct rblkdev *rblk = &minors[dmin];
    401      1.31   pooka 	struct partinfo *pi;
    402      1.31   pooka 	int error = 0;
    403      1.31   pooka 
    404      1.31   pooka 	/* well, me should support a few more, but we don't for now */
    405      1.31   pooka 	switch (xfer) {
    406      1.31   pooka 	case DIOCGDINFO:
    407      1.31   pooka 		*(struct disklabel *)addr = rblk->rblk_label;
    408      1.31   pooka 		break;
    409      1.31   pooka 
    410      1.31   pooka 	case DIOCGPART:
    411      1.31   pooka 		pi = addr;
    412      1.31   pooka 		pi->part = &rblk->rblk_label.d_partitions[DISKPART(dmin)];
    413      1.31   pooka 		pi->disklab = &rblk->rblk_label;
    414      1.31   pooka 		break;
    415      1.32   pooka 
    416      1.32   pooka 	/* it's synced enough along the write path */
    417      1.32   pooka 	case DIOCCACHESYNC:
    418      1.32   pooka 		break;
    419      1.32   pooka 
    420      1.31   pooka 	default:
    421      1.31   pooka 		error = ENOTTY;
    422      1.31   pooka 		break;
    423       1.1   pooka 	}
    424       1.1   pooka 
    425      1.31   pooka 	return error;
    426       1.1   pooka }
    427       1.1   pooka 
    428      1.25   pooka static int
    429      1.25   pooka do_physio(dev_t dev, struct uio *uio, int which)
    430      1.25   pooka {
    431      1.25   pooka 	void (*strat)(struct buf *);
    432      1.25   pooka 
    433      1.25   pooka 	if (blkfail)
    434      1.25   pooka 		strat = rumpblk_strategy_fail;
    435      1.25   pooka 	else
    436      1.25   pooka 		strat = rumpblk_strategy;
    437      1.25   pooka 
    438      1.25   pooka 	return physio(strat, NULL, dev, which, minphys, uio);
    439      1.25   pooka }
    440      1.25   pooka 
    441       1.1   pooka int
    442       1.1   pooka rumpblk_read(dev_t dev, struct uio *uio, int flags)
    443       1.1   pooka {
    444       1.1   pooka 
    445      1.25   pooka 	return do_physio(dev, uio, B_READ);
    446       1.1   pooka }
    447       1.1   pooka 
    448       1.1   pooka int
    449       1.1   pooka rumpblk_write(dev_t dev, struct uio *uio, int flags)
    450       1.1   pooka {
    451       1.1   pooka 
    452      1.25   pooka 	return do_physio(dev, uio, B_WRITE);
    453       1.1   pooka }
    454       1.1   pooka 
    455       1.3   pooka static void
    456       1.3   pooka dostrategy(struct buf *bp)
    457       1.1   pooka {
    458       1.1   pooka 	struct rblkdev *rblk = &minors[minor(bp->b_dev)];
    459       1.1   pooka 	off_t off;
    460      1.21   pooka 	int async = bp->b_flags & B_ASYNC;
    461  1.46.4.3    yamt 	int op;
    462       1.1   pooka 
    463      1.44   pooka 	if (bp->b_bcount % (1<<sectshift) != 0) {
    464      1.44   pooka 		rump_biodone(bp, 0, EINVAL);
    465      1.44   pooka 		return;
    466      1.44   pooka 	}
    467      1.44   pooka 
    468      1.20   pooka 	/* collect statistics */
    469      1.20   pooka 	ev_io_total.ev_count++;
    470      1.20   pooka 	if (async)
    471      1.20   pooka 		ev_io_async.ev_count++;
    472      1.20   pooka 	if (BUF_ISWRITE(bp)) {
    473      1.20   pooka 		ev_bwrite_total.ev_count += bp->b_bcount;
    474      1.20   pooka 		if (async)
    475      1.20   pooka 			ev_bwrite_async.ev_count += bp->b_bcount;
    476      1.20   pooka 	} else {
    477      1.20   pooka 		ev_bread_total.ev_count++;
    478      1.20   pooka 	}
    479      1.20   pooka 
    480      1.44   pooka 	/*
    481      1.44   pooka 	 * b_blkno is always in terms of DEV_BSIZE, and since we need
    482      1.44   pooka 	 * to translate to a byte offset for the host read, this
    483      1.44   pooka 	 * calculation does not need sectshift.
    484      1.44   pooka 	 */
    485      1.44   pooka 	off = bp->b_blkno << DEV_BSHIFT;
    486      1.44   pooka 
    487      1.11   pooka 	/*
    488      1.11   pooka 	 * Do bounds checking if we're working on a file.  Otherwise
    489      1.11   pooka 	 * invalid file systems might attempt to read beyond EOF.  This
    490      1.11   pooka 	 * is bad(tm) especially on mmapped images.  This is essentially
    491      1.11   pooka 	 * the kernel bounds_check() routines.
    492      1.11   pooka 	 */
    493      1.27   pooka 	if (off + bp->b_bcount > rblk->rblk_size) {
    494      1.11   pooka 		int64_t sz = rblk->rblk_size - off;
    495      1.11   pooka 
    496      1.11   pooka 		/* EOF */
    497      1.11   pooka 		if (sz == 0) {
    498      1.11   pooka 			rump_biodone(bp, 0, 0);
    499      1.11   pooka 			return;
    500      1.11   pooka 		}
    501      1.11   pooka 		/* beyond EOF ==> error */
    502      1.11   pooka 		if (sz < 0) {
    503      1.11   pooka 			rump_biodone(bp, 0, EINVAL);
    504      1.11   pooka 			return;
    505      1.11   pooka 		}
    506      1.11   pooka 
    507      1.11   pooka 		/* truncate to device size */
    508      1.11   pooka 		bp->b_bcount = sz;
    509      1.11   pooka 	}
    510      1.11   pooka 
    511      1.34   pooka 	off += rblk->rblk_hostoffset;
    512       1.1   pooka 	DPRINTF(("rumpblk_strategy: 0x%x bytes %s off 0x%" PRIx64
    513      1.20   pooka 	    " (0x%" PRIx64 " - 0x%" PRIx64 "), %ssync\n",
    514      1.16   pooka 	    bp->b_bcount, BUF_ISREAD(bp) ? "READ" : "WRITE",
    515      1.20   pooka 	    off, off, (off + bp->b_bcount), async ? "a" : ""));
    516       1.1   pooka 
    517  1.46.4.3    yamt 	op = BUF_ISREAD(bp) ? RUMPUSER_BIO_READ : RUMPUSER_BIO_WRITE;
    518  1.46.4.3    yamt 	if (BUF_ISWRITE(bp) && !async)
    519  1.46.4.3    yamt 		op |= RUMPUSER_BIO_SYNC;
    520       1.1   pooka 
    521  1.46.4.3    yamt 	rumpuser_bio(rblk->rblk_fd, op, bp->b_data, bp->b_bcount, off,
    522  1.46.4.3    yamt 	    rump_biodone, bp);
    523       1.1   pooka }
    524       1.3   pooka 
    525       1.3   pooka void
    526       1.3   pooka rumpblk_strategy(struct buf *bp)
    527       1.3   pooka {
    528       1.3   pooka 
    529       1.3   pooka 	dostrategy(bp);
    530       1.3   pooka }
    531       1.3   pooka 
    532       1.3   pooka /*
    533       1.4   pooka  * Simple random number generator.  This is private so that we can
    534       1.4   pooka  * very repeatedly control which blocks will fail.
    535       1.4   pooka  *
    536       1.3   pooka  * <mlelstv> pooka, rand()
    537       1.3   pooka  * <mlelstv> [paste]
    538       1.3   pooka  */
    539       1.3   pooka static unsigned
    540       1.3   pooka gimmerand(void)
    541       1.3   pooka {
    542       1.3   pooka 
    543       1.3   pooka 	return (randstate = randstate * 1103515245 + 12345) % (0x80000000L);
    544       1.3   pooka }
    545       1.3   pooka 
    546       1.3   pooka /*
    547       1.3   pooka  * Block device with very simple fault injection.  Fails every
    548       1.3   pooka  * n out of BLKFAIL_MAX I/O with EIO.  n is determined by the env
    549       1.3   pooka  * variable RUMP_BLKFAIL.
    550       1.3   pooka  */
    551       1.3   pooka void
    552       1.3   pooka rumpblk_strategy_fail(struct buf *bp)
    553       1.3   pooka {
    554       1.3   pooka 
    555       1.3   pooka 	if (gimmerand() % BLKFAIL_MAX >= blkfail) {
    556       1.3   pooka 		dostrategy(bp);
    557       1.3   pooka 	} else {
    558       1.3   pooka 		printf("block fault injection: failing I/O on block %lld\n",
    559       1.3   pooka 		    (long long)bp->b_blkno);
    560       1.3   pooka 		bp->b_error = EIO;
    561       1.3   pooka 		biodone(bp);
    562       1.3   pooka 	}
    563       1.3   pooka }
    564