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