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