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