Home | History | Annotate | Line # | Download | only in rumpvfs
rumpblk.c revision 1.32
      1  1.32   pooka /*	$NetBSD: rumpblk.c,v 1.32 2009/11/25 15:01:28 pooka Exp $	*/
      2   1.1   pooka 
      3   1.1   pooka /*
      4   1.1   pooka  * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
      5   1.1   pooka  *
      6   1.1   pooka  * Development of this software was supported by the
      7   1.1   pooka  * Finnish Cultural Foundation.
      8   1.1   pooka  *
      9   1.1   pooka  * Redistribution and use in source and binary forms, with or without
     10   1.1   pooka  * modification, are permitted provided that the following conditions
     11   1.1   pooka  * are met:
     12   1.1   pooka  * 1. Redistributions of source code must retain the above copyright
     13   1.1   pooka  *    notice, this list of conditions and the following disclaimer.
     14   1.1   pooka  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1   pooka  *    notice, this list of conditions and the following disclaimer in the
     16   1.1   pooka  *    documentation and/or other materials provided with the distribution.
     17   1.1   pooka  *
     18   1.1   pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19   1.1   pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20   1.1   pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21   1.1   pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22   1.1   pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23   1.1   pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24   1.1   pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25   1.1   pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26   1.1   pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1   pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1   pooka  * SUCH DAMAGE.
     29   1.1   pooka  */
     30   1.1   pooka 
     31   1.1   pooka /*
     32   1.1   pooka  * Block device emulation.  Presents a block device interface and
     33   1.1   pooka  * uses rumpuser system calls to satisfy I/O requests.
     34  1.16   pooka  *
     35  1.16   pooka  * We provide fault injection.  The driver can be made to fail
     36  1.16   pooka  * I/O occasionally.
     37  1.16   pooka  *
     38  1.16   pooka  * The driver also provides an optimization for regular files by
     39  1.16   pooka  * using memory-mapped I/O.  This avoids kernel access for every
     40  1.16   pooka  * I/O operation.  It also gives finer-grained control of how to
     41  1.16   pooka  * flush data.  Additionally, in case the rump kernel dumps core,
     42  1.16   pooka  * we get way less carnage.
     43  1.29   pooka  *
     44  1.29   pooka  * However, it is quite costly in writing large amounts of
     45  1.29   pooka  * file data, since old contents cannot merely be overwritten, but
     46  1.29   pooka  * must be paged in first before replacing (i.e. r/m/w).  Ideally,
     47  1.29   pooka  * we should use directio.  The problem is that directio can fail
     48  1.29   pooka  * silently causing improper file system semantics (i.e. unflushed
     49  1.29   pooka  * data).  Therefore, default to mmap for now.  Even so, directio
     50  1.29   pooka  * _should_ be safe and can be enabled by compiling this module
     51  1.29   pooka  * with -DHAS_DIRECTIO.
     52   1.1   pooka  */
     53   1.1   pooka 
     54   1.1   pooka #include <sys/cdefs.h>
     55  1.32   pooka __KERNEL_RCSID(0, "$NetBSD: rumpblk.c,v 1.32 2009/11/25 15:01:28 pooka Exp $");
     56   1.1   pooka 
     57   1.1   pooka #include <sys/param.h>
     58   1.1   pooka #include <sys/buf.h>
     59   1.1   pooka #include <sys/conf.h>
     60  1.16   pooka #include <sys/condvar.h>
     61   1.1   pooka #include <sys/disklabel.h>
     62  1.18   pooka #include <sys/evcnt.h>
     63   1.1   pooka #include <sys/fcntl.h>
     64   1.1   pooka #include <sys/kmem.h>
     65   1.1   pooka #include <sys/malloc.h>
     66  1.16   pooka #include <sys/queue.h>
     67   1.1   pooka #include <sys/stat.h>
     68   1.1   pooka 
     69   1.1   pooka #include <rump/rumpuser.h>
     70   1.1   pooka 
     71   1.1   pooka #include "rump_private.h"
     72   1.1   pooka #include "rump_vfs_private.h"
     73   1.1   pooka 
     74  1.16   pooka #if 0
     75  1.16   pooka #define DPRINTF(x) printf x
     76  1.16   pooka #else
     77  1.16   pooka #define DPRINTF(x)
     78  1.16   pooka #endif
     79  1.16   pooka 
     80  1.19   pooka /* Default: 16 x 1MB windows */
     81  1.19   pooka unsigned memwinsize = (1<<20);
     82  1.19   pooka unsigned memwincnt = 16;
     83  1.19   pooka 
     84  1.19   pooka #define STARTWIN(off)		((off) & ~(memwinsize-1))
     85  1.16   pooka #define INWIN(win,off)		((win)->win_off == STARTWIN(off))
     86  1.19   pooka #define WINSIZE(rblk, win)	(MIN((rblk->rblk_size-win->win_off),memwinsize))
     87  1.16   pooka #define WINVALID(win)		((win)->win_off != (off_t)-1)
     88  1.16   pooka #define WINVALIDATE(win)	((win)->win_off = (off_t)-1)
     89  1.16   pooka struct blkwin {
     90  1.16   pooka 	off_t win_off;
     91  1.16   pooka 	void *win_mem;
     92  1.16   pooka 	int win_refcnt;
     93  1.16   pooka 
     94  1.16   pooka 	TAILQ_ENTRY(blkwin) win_lru;
     95  1.16   pooka };
     96  1.16   pooka 
     97   1.1   pooka #define RUMPBLK_SIZE 16
     98   1.1   pooka static struct rblkdev {
     99   1.1   pooka 	char *rblk_path;
    100   1.1   pooka 	int rblk_fd;
    101  1.25   pooka 	int rblk_opencnt;
    102  1.20   pooka #ifdef HAS_ODIRECT
    103  1.20   pooka 	int rblk_dfd;
    104  1.20   pooka #endif
    105  1.26   pooka 	uint64_t rblk_size;
    106  1.27   pooka 	uint64_t rblk_hostoffset;
    107  1.27   pooka 	int rblk_ftype;
    108  1.16   pooka 
    109  1.16   pooka 	/* for mmap */
    110  1.16   pooka 	int rblk_mmflags;
    111  1.16   pooka 	kmutex_t rblk_memmtx;
    112  1.16   pooka 	kcondvar_t rblk_memcv;
    113  1.16   pooka 	TAILQ_HEAD(winlru, blkwin) rblk_lruq;
    114  1.16   pooka 	bool rblk_waiting;
    115   1.1   pooka 
    116  1.31   pooka 	struct disklabel rblk_label;
    117   1.1   pooka } minors[RUMPBLK_SIZE];
    118   1.1   pooka 
    119  1.20   pooka static struct evcnt ev_io_total;
    120  1.20   pooka static struct evcnt ev_io_async;
    121  1.20   pooka 
    122  1.20   pooka static struct evcnt ev_memblk_hits;
    123  1.20   pooka static struct evcnt ev_memblk_busy;
    124  1.20   pooka 
    125  1.20   pooka static struct evcnt ev_bwrite_total;
    126  1.20   pooka static struct evcnt ev_bwrite_async;
    127  1.20   pooka static struct evcnt ev_bread_total;
    128  1.18   pooka 
    129   1.1   pooka dev_type_open(rumpblk_open);
    130   1.1   pooka dev_type_close(rumpblk_close);
    131   1.1   pooka dev_type_read(rumpblk_read);
    132   1.1   pooka dev_type_write(rumpblk_write);
    133   1.1   pooka dev_type_ioctl(rumpblk_ioctl);
    134   1.1   pooka dev_type_strategy(rumpblk_strategy);
    135   1.3   pooka dev_type_strategy(rumpblk_strategy_fail);
    136   1.1   pooka dev_type_dump(rumpblk_dump);
    137   1.1   pooka dev_type_size(rumpblk_size);
    138   1.1   pooka 
    139   1.1   pooka static const struct bdevsw rumpblk_bdevsw = {
    140   1.1   pooka 	rumpblk_open, rumpblk_close, rumpblk_strategy, rumpblk_ioctl,
    141   1.1   pooka 	nodump, nosize, D_DISK
    142   1.1   pooka };
    143   1.1   pooka 
    144   1.3   pooka static const struct bdevsw rumpblk_bdevsw_fail = {
    145   1.3   pooka 	rumpblk_open, rumpblk_close, rumpblk_strategy_fail, rumpblk_ioctl,
    146   1.3   pooka 	nodump, nosize, D_DISK
    147   1.3   pooka };
    148   1.3   pooka 
    149   1.1   pooka static const struct cdevsw rumpblk_cdevsw = {
    150   1.1   pooka 	rumpblk_open, rumpblk_close, rumpblk_read, rumpblk_write,
    151   1.1   pooka 	rumpblk_ioctl, nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    152   1.1   pooka };
    153   1.1   pooka 
    154   1.3   pooka /* fail every n out of BLKFAIL_MAX */
    155   1.3   pooka #define BLKFAIL_MAX 10000
    156   1.3   pooka static int blkfail;
    157   1.3   pooka static unsigned randstate;
    158  1.24   pooka static kmutex_t rumpblk_lock;
    159   1.1   pooka 
    160  1.31   pooka static void
    161  1.31   pooka makedefaultlabel(struct disklabel *lp, off_t size, int part)
    162  1.31   pooka {
    163  1.31   pooka 	int i;
    164  1.31   pooka 
    165  1.31   pooka 	memset(lp, 0, sizeof(*lp));
    166  1.31   pooka 
    167  1.31   pooka 	lp->d_secperunit = size;
    168  1.31   pooka 	lp->d_secsize = DEV_BSIZE;
    169  1.31   pooka 	lp->d_nsectors = size >> DEV_BSHIFT;
    170  1.31   pooka 	lp->d_ntracks = 1;
    171  1.31   pooka 	lp->d_ncylinders = 1;
    172  1.31   pooka 	lp->d_secpercyl = lp->d_nsectors;
    173  1.31   pooka 
    174  1.31   pooka 	/* oh dear oh dear */
    175  1.31   pooka 	strncpy(lp->d_typename, "rumpd", sizeof(lp->d_typename));
    176  1.31   pooka 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    177  1.31   pooka 
    178  1.31   pooka 	lp->d_type = DTYPE_RUMPD;
    179  1.31   pooka 	lp->d_rpm = 11;
    180  1.31   pooka 	lp->d_interleave = 1;
    181  1.31   pooka 	lp->d_flags = 0;
    182  1.31   pooka 
    183  1.31   pooka 	/* XXX: RAW_PART handling? */
    184  1.31   pooka 	for (i = 0; i < part; i++) {
    185  1.31   pooka 		lp->d_partitions[i].p_fstype = FS_UNUSED;
    186  1.31   pooka 	}
    187  1.32   pooka 	lp->d_partitions[part].p_size = size >> DEV_BSHIFT;
    188  1.31   pooka 	lp->d_npartitions = part+1;
    189  1.31   pooka 	/* XXX: file system type? */
    190  1.31   pooka 
    191  1.31   pooka 	lp->d_magic = DISKMAGIC;
    192  1.31   pooka 	lp->d_magic2 = DISKMAGIC;
    193  1.31   pooka 	lp->d_checksum = 0; /* XXX */
    194  1.31   pooka }
    195  1.31   pooka 
    196  1.16   pooka static struct blkwin *
    197  1.16   pooka getwindow(struct rblkdev *rblk, off_t off, int *wsize, int *error)
    198  1.16   pooka {
    199  1.16   pooka 	struct blkwin *win;
    200  1.16   pooka 
    201  1.16   pooka 	mutex_enter(&rblk->rblk_memmtx);
    202  1.16   pooka  retry:
    203  1.16   pooka 	/* search for window */
    204  1.16   pooka 	TAILQ_FOREACH(win, &rblk->rblk_lruq, win_lru) {
    205  1.16   pooka 		if (INWIN(win, off) && WINVALID(win))
    206  1.16   pooka 			break;
    207  1.16   pooka 	}
    208  1.16   pooka 
    209  1.16   pooka 	/* found?  return */
    210  1.16   pooka 	if (win) {
    211  1.20   pooka 		ev_memblk_hits.ev_count++;
    212  1.16   pooka 		TAILQ_REMOVE(&rblk->rblk_lruq, win, win_lru);
    213  1.16   pooka 		goto good;
    214  1.16   pooka 	}
    215  1.16   pooka 
    216  1.16   pooka 	/*
    217  1.16   pooka 	 * Else, create new window.  If the least recently used is not
    218  1.16   pooka 	 * currently in use, reuse that.  Otherwise we need to wait.
    219  1.16   pooka 	 */
    220  1.16   pooka 	win = TAILQ_LAST(&rblk->rblk_lruq, winlru);
    221  1.16   pooka 	if (win->win_refcnt == 0) {
    222  1.16   pooka 		TAILQ_REMOVE(&rblk->rblk_lruq, win, win_lru);
    223  1.16   pooka 		mutex_exit(&rblk->rblk_memmtx);
    224  1.16   pooka 
    225  1.16   pooka 		if (WINVALID(win)) {
    226  1.16   pooka 			DPRINTF(("win %p, unmap mem %p, off 0x%" PRIx64 "\n",
    227  1.16   pooka 			    win, win->win_mem, win->win_off));
    228  1.16   pooka 			rumpuser_unmap(win->win_mem, WINSIZE(rblk, win));
    229  1.16   pooka 			WINVALIDATE(win);
    230  1.16   pooka 		}
    231  1.16   pooka 
    232  1.16   pooka 		win->win_off = STARTWIN(off);
    233  1.16   pooka 		win->win_mem = rumpuser_filemmap(rblk->rblk_fd, win->win_off,
    234  1.16   pooka 		    WINSIZE(rblk, win), rblk->rblk_mmflags, error);
    235  1.16   pooka 		DPRINTF(("win %p, off 0x%" PRIx64 ", mem %p\n",
    236  1.16   pooka 		    win, win->win_off, win->win_mem));
    237  1.16   pooka 
    238  1.16   pooka 		mutex_enter(&rblk->rblk_memmtx);
    239  1.16   pooka 		if (win->win_mem == NULL) {
    240  1.16   pooka 			WINVALIDATE(win);
    241  1.16   pooka 			TAILQ_INSERT_TAIL(&rblk->rblk_lruq, win, win_lru);
    242  1.16   pooka 			mutex_exit(&rblk->rblk_memmtx);
    243  1.16   pooka 			return NULL;
    244  1.16   pooka 		}
    245  1.16   pooka 	} else {
    246  1.16   pooka 		DPRINTF(("memwin wait\n"));
    247  1.20   pooka 		ev_memblk_busy.ev_count++;
    248  1.18   pooka 
    249  1.16   pooka 		rblk->rblk_waiting = true;
    250  1.16   pooka 		cv_wait(&rblk->rblk_memcv, &rblk->rblk_memmtx);
    251  1.16   pooka 		goto retry;
    252  1.16   pooka 	}
    253  1.16   pooka 
    254  1.16   pooka  good:
    255  1.16   pooka 	KASSERT(win);
    256  1.16   pooka 	win->win_refcnt++;
    257  1.16   pooka 	TAILQ_INSERT_HEAD(&rblk->rblk_lruq, win, win_lru);
    258  1.16   pooka 	mutex_exit(&rblk->rblk_memmtx);
    259  1.19   pooka 	*wsize = MIN(*wsize, memwinsize - (off-win->win_off));
    260  1.16   pooka 	KASSERT(*wsize);
    261  1.16   pooka 
    262  1.16   pooka 	return win;
    263  1.16   pooka }
    264  1.16   pooka 
    265  1.16   pooka static void
    266  1.16   pooka putwindow(struct rblkdev *rblk, struct blkwin *win)
    267  1.16   pooka {
    268  1.16   pooka 
    269  1.16   pooka 	mutex_enter(&rblk->rblk_memmtx);
    270  1.16   pooka 	if (--win->win_refcnt == 0 && rblk->rblk_waiting) {
    271  1.16   pooka 		rblk->rblk_waiting = false;
    272  1.16   pooka 		cv_signal(&rblk->rblk_memcv);
    273  1.16   pooka 	}
    274  1.16   pooka 	KASSERT(win->win_refcnt >= 0);
    275  1.16   pooka 	mutex_exit(&rblk->rblk_memmtx);
    276  1.16   pooka }
    277  1.16   pooka 
    278  1.16   pooka static void
    279  1.16   pooka wincleanup(struct rblkdev *rblk)
    280  1.16   pooka {
    281  1.16   pooka 	struct blkwin *win;
    282  1.16   pooka 
    283  1.16   pooka 	while ((win = TAILQ_FIRST(&rblk->rblk_lruq)) != NULL) {
    284  1.16   pooka 		TAILQ_REMOVE(&rblk->rblk_lruq, win, win_lru);
    285  1.16   pooka 		if (WINVALID(win)) {
    286  1.16   pooka 			DPRINTF(("cleanup win %p addr %p\n",
    287  1.16   pooka 			    win, win->win_mem));
    288  1.16   pooka 			rumpuser_unmap(win->win_mem, WINSIZE(rblk, win));
    289  1.16   pooka 		}
    290  1.16   pooka 		kmem_free(win, sizeof(*win));
    291  1.16   pooka 	}
    292  1.16   pooka 	rblk->rblk_mmflags = 0;
    293  1.16   pooka }
    294  1.16   pooka 
    295   1.1   pooka int
    296   1.8  cegger rumpblk_init(void)
    297   1.1   pooka {
    298   1.3   pooka 	char buf[64];
    299   1.1   pooka 	int rumpblk = RUMPBLK;
    300  1.19   pooka 	unsigned tmp;
    301  1.16   pooka 	int error, i;
    302   1.3   pooka 
    303  1.24   pooka 	mutex_init(&rumpblk_lock, MUTEX_DEFAULT, IPL_NONE);
    304  1.24   pooka 
    305   1.3   pooka 	if (rumpuser_getenv("RUMP_BLKFAIL", buf, sizeof(buf), &error) == 0) {
    306   1.3   pooka 		blkfail = strtoul(buf, NULL, 10);
    307   1.3   pooka 		/* fail everything */
    308   1.3   pooka 		if (blkfail > BLKFAIL_MAX)
    309   1.3   pooka 			blkfail = BLKFAIL_MAX;
    310   1.3   pooka 		if (rumpuser_getenv("RUMP_BLKFAIL_SEED", buf, sizeof(buf),
    311   1.3   pooka 		    &error) == 0) {
    312   1.3   pooka 			randstate = strtoul(buf, NULL, 10);
    313   1.3   pooka 		} else {
    314  1.23   pooka 			randstate = arc4random();
    315   1.3   pooka 		}
    316  1.22   pooka 		printf("rumpblk: FAULT INJECTION ACTIVE! fail %d/%d. "
    317  1.22   pooka 		    "seed %u\n", blkfail, BLKFAIL_MAX, randstate);
    318   1.3   pooka 	} else {
    319   1.3   pooka 		blkfail = 0;
    320   1.3   pooka 	}
    321   1.1   pooka 
    322  1.19   pooka 	if (rumpuser_getenv("RUMP_BLKWINSIZE", buf, sizeof(buf), &error) == 0) {
    323  1.19   pooka 		printf("rumpblk: ");
    324  1.19   pooka 		tmp = strtoul(buf, NULL, 10);
    325  1.19   pooka 		if (tmp && !(tmp & (tmp-1)))
    326  1.19   pooka 			memwinsize = tmp;
    327  1.19   pooka 		else
    328  1.19   pooka 			printf("invalid RUMP_BLKWINSIZE %d, ", tmp);
    329  1.19   pooka 		printf("using %d for memwinsize\n", memwinsize);
    330  1.19   pooka 	}
    331  1.19   pooka 	if (rumpuser_getenv("RUMP_BLKWINCOUNT", buf, sizeof(buf), &error) == 0){
    332  1.19   pooka 		printf("rumpblk: ");
    333  1.19   pooka 		tmp = strtoul(buf, NULL, 10);
    334  1.19   pooka 		if (tmp)
    335  1.19   pooka 			memwincnt = tmp;
    336  1.19   pooka 		else
    337  1.19   pooka 			printf("invalid RUMP_BLKWINCOUNT %d, ", tmp);
    338  1.19   pooka 		printf("using %d for memwincount\n", memwincnt);
    339  1.19   pooka 	}
    340  1.19   pooka 
    341  1.16   pooka 	memset(minors, 0, sizeof(minors));
    342  1.16   pooka 	for (i = 0; i < RUMPBLK_SIZE; i++) {
    343  1.16   pooka 		mutex_init(&minors[i].rblk_memmtx, MUTEX_DEFAULT, IPL_NONE);
    344  1.16   pooka 		cv_init(&minors[i].rblk_memcv, "rblkmcv");
    345  1.16   pooka 	}
    346  1.16   pooka 
    347  1.20   pooka 	evcnt_attach_dynamic(&ev_io_total, EVCNT_TYPE_MISC, NULL,
    348  1.20   pooka 	    "rumpblk", "rumpblk I/O reqs");
    349  1.20   pooka 	evcnt_attach_dynamic(&ev_io_async, EVCNT_TYPE_MISC, NULL,
    350  1.20   pooka 	    "rumpblk", "rumpblk async I/O");
    351  1.20   pooka 
    352  1.20   pooka 	evcnt_attach_dynamic(&ev_bread_total, EVCNT_TYPE_MISC, NULL,
    353  1.20   pooka 	    "rumpblk", "rumpblk bytes read");
    354  1.20   pooka 	evcnt_attach_dynamic(&ev_bwrite_total, EVCNT_TYPE_MISC, NULL,
    355  1.20   pooka 	    "rumpblk", "rumpblk bytes written");
    356  1.20   pooka 	evcnt_attach_dynamic(&ev_bwrite_async, EVCNT_TYPE_MISC, NULL,
    357  1.20   pooka 	    "rumpblk", "rumpblk bytes written async");
    358  1.20   pooka 
    359  1.20   pooka 	evcnt_attach_dynamic(&ev_memblk_hits, EVCNT_TYPE_MISC, NULL,
    360  1.18   pooka 	    "rumpblk", "memblk window hits");
    361  1.20   pooka 	evcnt_attach_dynamic(&ev_memblk_busy, EVCNT_TYPE_MISC, NULL,
    362  1.18   pooka 	    "rumpblk", "memblk all windows busy");
    363  1.18   pooka 
    364   1.3   pooka 	if (blkfail) {
    365   1.3   pooka 		return devsw_attach("rumpblk", &rumpblk_bdevsw_fail, &rumpblk,
    366   1.3   pooka 		    &rumpblk_cdevsw, &rumpblk);
    367   1.3   pooka 	} else {
    368   1.3   pooka 		return devsw_attach("rumpblk", &rumpblk_bdevsw, &rumpblk,
    369   1.3   pooka 		    &rumpblk_cdevsw, &rumpblk);
    370   1.3   pooka 	}
    371   1.1   pooka }
    372   1.1   pooka 
    373  1.24   pooka /* XXX: no deregister */
    374   1.1   pooka int
    375  1.27   pooka rumpblk_register(const char *path, devminor_t *dmin,
    376  1.27   pooka 	uint64_t offset, uint64_t size)
    377   1.1   pooka {
    378  1.27   pooka 	struct rblkdev *rblk;
    379  1.24   pooka 	uint64_t flen;
    380   1.1   pooka 	size_t len;
    381  1.30   pooka 	int ftype, error, i;
    382  1.24   pooka 
    383  1.27   pooka 	/* devices might not report correct size unless they're open */
    384  1.30   pooka 	if (rumpuser_getfileinfo(path, &flen, &ftype, &error) == -1)
    385  1.27   pooka 		return error;
    386  1.27   pooka 
    387  1.24   pooka 	/* verify host file is of supported type */
    388  1.24   pooka 	if (!(ftype == RUMPUSER_FT_REG
    389  1.24   pooka 	   || ftype == RUMPUSER_FT_BLK
    390  1.24   pooka 	   || ftype == RUMPUSER_FT_CHR))
    391  1.24   pooka 		return EINVAL;
    392   1.1   pooka 
    393  1.24   pooka 	mutex_enter(&rumpblk_lock);
    394  1.24   pooka 	for (i = 0; i < RUMPBLK_SIZE; i++) {
    395  1.24   pooka 		if (minors[i].rblk_path&&strcmp(minors[i].rblk_path, path)==0) {
    396  1.24   pooka 			mutex_exit(&rumpblk_lock);
    397  1.24   pooka 			*dmin = i;
    398  1.24   pooka 			return 0;
    399  1.24   pooka 		}
    400  1.24   pooka 	}
    401   1.1   pooka 
    402   1.1   pooka 	for (i = 0; i < RUMPBLK_SIZE; i++)
    403   1.1   pooka 		if (minors[i].rblk_path == NULL)
    404   1.1   pooka 			break;
    405  1.24   pooka 	if (i == RUMPBLK_SIZE) {
    406  1.24   pooka 		mutex_exit(&rumpblk_lock);
    407  1.24   pooka 		return EBUSY;
    408  1.24   pooka 	}
    409   1.1   pooka 
    410  1.27   pooka 	rblk = &minors[i];
    411   1.1   pooka 	len = strlen(path);
    412  1.27   pooka 	rblk->rblk_path = malloc(len + 1, M_TEMP, M_WAITOK);
    413  1.27   pooka 	strcpy(rblk->rblk_path, path);
    414  1.27   pooka 	rblk->rblk_fd = -1;
    415  1.27   pooka 	rblk->rblk_hostoffset = offset;
    416  1.27   pooka 	if (size == RUMPBLK_SIZENOTSET) {
    417  1.27   pooka 		KASSERT(size + offset <= flen);
    418  1.27   pooka 		rblk->rblk_size = size;
    419  1.27   pooka 	} else {
    420  1.27   pooka 		KASSERT(offset < flen);
    421  1.27   pooka 		rblk->rblk_size = flen - offset;
    422  1.27   pooka 	}
    423  1.27   pooka 	rblk->rblk_ftype = ftype;
    424  1.31   pooka 	makedefaultlabel(&rblk->rblk_label, rblk->rblk_size, i);
    425  1.24   pooka 	mutex_exit(&rumpblk_lock);
    426  1.24   pooka 
    427  1.24   pooka 	*dmin = i;
    428  1.24   pooka 	return 0;
    429   1.1   pooka }
    430   1.1   pooka 
    431   1.1   pooka int
    432   1.1   pooka rumpblk_open(dev_t dev, int flag, int fmt, struct lwp *l)
    433   1.1   pooka {
    434   1.1   pooka 	struct rblkdev *rblk = &minors[minor(dev)];
    435   1.1   pooka 	int error, fd;
    436   1.1   pooka 
    437  1.27   pooka 	if (rblk->rblk_path == NULL)
    438  1.27   pooka 		return ENXIO;
    439  1.27   pooka 
    440  1.25   pooka 	if (rblk->rblk_fd != -1)
    441  1.25   pooka 		return 0; /* XXX: refcount, open mode */
    442   1.1   pooka 	fd = rumpuser_open(rblk->rblk_path, OFLAGS(flag), &error);
    443   1.1   pooka 	if (error)
    444   1.1   pooka 		return error;
    445   1.1   pooka 
    446  1.20   pooka #ifdef HAS_ODIRECT
    447  1.20   pooka 	rblk->rblk_dfd = rumpuser_open(rblk->rblk_path,
    448  1.20   pooka 	    OFLAGS(flag) | O_DIRECT, &error);
    449  1.20   pooka 	if (error)
    450  1.20   pooka 		return error;
    451  1.20   pooka #endif
    452  1.20   pooka 
    453  1.27   pooka 	if (rblk->rblk_ftype == RUMPUSER_FT_REG) {
    454  1.30   pooka 		uint64_t fsize = rblk->rblk_size, off = rblk->rblk_hostoffset;
    455  1.16   pooka 		struct blkwin *win;
    456  1.16   pooka 		int i, winsize;
    457  1.16   pooka 
    458   1.1   pooka 		/*
    459  1.16   pooka 		 * Use mmap to access a regular file.  Allocate and
    460  1.16   pooka 		 * cache initial windows here.  Failure to allocate one
    461  1.16   pooka 		 * means fallback to read/write i/o.
    462   1.1   pooka 		 */
    463   1.9   pooka 
    464  1.16   pooka 		rblk->rblk_mmflags = 0;
    465  1.16   pooka 		if (flag & FREAD)
    466  1.16   pooka 			rblk->rblk_mmflags |= RUMPUSER_FILEMMAP_READ;
    467  1.16   pooka 		if (flag & FWRITE) {
    468  1.16   pooka 			rblk->rblk_mmflags |= RUMPUSER_FILEMMAP_WRITE;
    469  1.16   pooka 			rblk->rblk_mmflags |= RUMPUSER_FILEMMAP_SHARED;
    470  1.16   pooka 		}
    471  1.16   pooka 
    472  1.16   pooka 		TAILQ_INIT(&rblk->rblk_lruq);
    473  1.16   pooka 		rblk->rblk_fd = fd;
    474  1.16   pooka 
    475  1.28   pooka 		for (i = 0; i < memwincnt && off + i*memwinsize < fsize; i++) {
    476  1.16   pooka 			win = kmem_zalloc(sizeof(*win), KM_SLEEP);
    477  1.16   pooka 			WINVALIDATE(win);
    478  1.16   pooka 			TAILQ_INSERT_TAIL(&rblk->rblk_lruq, win, win_lru);
    479  1.16   pooka 
    480  1.16   pooka 			/*
    481  1.16   pooka 			 * Allocate first windows.  Here we just generally
    482  1.16   pooka 			 * make sure a) we can mmap at all b) we have the
    483  1.16   pooka 			 * necessary VA available
    484  1.16   pooka 			 */
    485  1.29   pooka 			winsize = memwinsize;
    486  1.28   pooka 			win = getwindow(rblk, off + i*memwinsize, &winsize,
    487  1.28   pooka 			    &error);
    488  1.16   pooka 			if (win) {
    489  1.16   pooka 				putwindow(rblk, win);
    490  1.16   pooka 			} else {
    491  1.16   pooka 				wincleanup(rblk);
    492  1.16   pooka 				break;
    493  1.12   pooka 			}
    494   1.9   pooka 		}
    495   1.1   pooka 	}
    496   1.1   pooka 
    497  1.16   pooka 	KASSERT(rblk->rblk_fd != -1);
    498   1.1   pooka 	return 0;
    499   1.1   pooka }
    500   1.1   pooka 
    501   1.1   pooka int
    502   1.1   pooka rumpblk_close(dev_t dev, int flag, int fmt, struct lwp *l)
    503   1.1   pooka {
    504   1.1   pooka 	struct rblkdev *rblk = &minors[minor(dev)];
    505   1.1   pooka 	int dummy;
    506   1.1   pooka 
    507  1.16   pooka 	if (rblk->rblk_mmflags)
    508  1.16   pooka 		wincleanup(rblk);
    509  1.16   pooka 	rumpuser_fsync(rblk->rblk_fd, &dummy);
    510   1.1   pooka 	rumpuser_close(rblk->rblk_fd, &dummy);
    511   1.1   pooka 	rblk->rblk_fd = -1;
    512   1.1   pooka 
    513   1.1   pooka 	return 0;
    514   1.1   pooka }
    515   1.1   pooka 
    516   1.1   pooka int
    517   1.1   pooka rumpblk_ioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
    518   1.1   pooka {
    519  1.31   pooka 	devminor_t dmin = minor(dev);
    520  1.31   pooka 	struct rblkdev *rblk = &minors[dmin];
    521  1.31   pooka 	struct partinfo *pi;
    522  1.31   pooka 	int error = 0;
    523  1.31   pooka 
    524  1.31   pooka 	/* well, me should support a few more, but we don't for now */
    525  1.31   pooka 	switch (xfer) {
    526  1.31   pooka 	case DIOCGDINFO:
    527  1.31   pooka 		*(struct disklabel *)addr = rblk->rblk_label;
    528  1.31   pooka 		break;
    529  1.31   pooka 
    530  1.31   pooka 	case DIOCGPART:
    531  1.31   pooka 		pi = addr;
    532  1.31   pooka 		pi->part = &rblk->rblk_label.d_partitions[DISKPART(dmin)];
    533  1.31   pooka 		pi->disklab = &rblk->rblk_label;
    534  1.31   pooka 		break;
    535  1.32   pooka 
    536  1.32   pooka 	/* it's synced enough along the write path */
    537  1.32   pooka 	case DIOCCACHESYNC:
    538  1.32   pooka 		break;
    539  1.32   pooka 
    540  1.31   pooka 	default:
    541  1.31   pooka 		error = ENOTTY;
    542  1.31   pooka 		break;
    543   1.1   pooka 	}
    544   1.1   pooka 
    545  1.31   pooka 	return error;
    546   1.1   pooka }
    547   1.1   pooka 
    548  1.25   pooka static int
    549  1.25   pooka do_physio(dev_t dev, struct uio *uio, int which)
    550  1.25   pooka {
    551  1.25   pooka 	void (*strat)(struct buf *);
    552  1.25   pooka 
    553  1.25   pooka 	if (blkfail)
    554  1.25   pooka 		strat = rumpblk_strategy_fail;
    555  1.25   pooka 	else
    556  1.25   pooka 		strat = rumpblk_strategy;
    557  1.25   pooka 
    558  1.25   pooka 	return physio(strat, NULL, dev, which, minphys, uio);
    559  1.25   pooka }
    560  1.25   pooka 
    561   1.1   pooka int
    562   1.1   pooka rumpblk_read(dev_t dev, struct uio *uio, int flags)
    563   1.1   pooka {
    564   1.1   pooka 
    565  1.25   pooka 	return do_physio(dev, uio, B_READ);
    566   1.1   pooka }
    567   1.1   pooka 
    568   1.1   pooka int
    569   1.1   pooka rumpblk_write(dev_t dev, struct uio *uio, int flags)
    570   1.1   pooka {
    571   1.1   pooka 
    572  1.25   pooka 	return do_physio(dev, uio, B_WRITE);
    573   1.1   pooka }
    574   1.1   pooka 
    575   1.3   pooka static void
    576   1.3   pooka dostrategy(struct buf *bp)
    577   1.1   pooka {
    578   1.1   pooka 	struct rblkdev *rblk = &minors[minor(bp->b_dev)];
    579   1.1   pooka 	off_t off;
    580  1.21   pooka 	int async = bp->b_flags & B_ASYNC;
    581  1.21   pooka 	int error;
    582   1.1   pooka 
    583  1.20   pooka 	/* collect statistics */
    584  1.20   pooka 	ev_io_total.ev_count++;
    585  1.20   pooka 	if (async)
    586  1.20   pooka 		ev_io_async.ev_count++;
    587  1.20   pooka 	if (BUF_ISWRITE(bp)) {
    588  1.20   pooka 		ev_bwrite_total.ev_count += bp->b_bcount;
    589  1.20   pooka 		if (async)
    590  1.20   pooka 			ev_bwrite_async.ev_count += bp->b_bcount;
    591  1.20   pooka 	} else {
    592  1.20   pooka 		ev_bread_total.ev_count++;
    593  1.20   pooka 	}
    594  1.20   pooka 
    595   1.1   pooka 	off = bp->b_blkno << DEV_BSHIFT;
    596  1.27   pooka 	off += rblk->rblk_hostoffset;
    597  1.11   pooka 	/*
    598  1.11   pooka 	 * Do bounds checking if we're working on a file.  Otherwise
    599  1.11   pooka 	 * invalid file systems might attempt to read beyond EOF.  This
    600  1.11   pooka 	 * is bad(tm) especially on mmapped images.  This is essentially
    601  1.11   pooka 	 * the kernel bounds_check() routines.
    602  1.11   pooka 	 */
    603  1.27   pooka 	if (off + bp->b_bcount > rblk->rblk_size) {
    604  1.11   pooka 		int64_t sz = rblk->rblk_size - off;
    605  1.11   pooka 
    606  1.11   pooka 		/* EOF */
    607  1.11   pooka 		if (sz == 0) {
    608  1.11   pooka 			rump_biodone(bp, 0, 0);
    609  1.11   pooka 			return;
    610  1.11   pooka 		}
    611  1.11   pooka 		/* beyond EOF ==> error */
    612  1.11   pooka 		if (sz < 0) {
    613  1.11   pooka 			rump_biodone(bp, 0, EINVAL);
    614  1.11   pooka 			return;
    615  1.11   pooka 		}
    616  1.11   pooka 
    617  1.11   pooka 		/* truncate to device size */
    618  1.11   pooka 		bp->b_bcount = sz;
    619  1.11   pooka 	}
    620  1.11   pooka 
    621   1.1   pooka 	DPRINTF(("rumpblk_strategy: 0x%x bytes %s off 0x%" PRIx64
    622  1.20   pooka 	    " (0x%" PRIx64 " - 0x%" PRIx64 "), %ssync\n",
    623  1.16   pooka 	    bp->b_bcount, BUF_ISREAD(bp) ? "READ" : "WRITE",
    624  1.20   pooka 	    off, off, (off + bp->b_bcount), async ? "a" : ""));
    625   1.1   pooka 
    626  1.16   pooka 	/* mmap?  handle here and return */
    627  1.16   pooka 	if (rblk->rblk_mmflags) {
    628  1.16   pooka 		struct blkwin *win;
    629  1.16   pooka 		int winsize, iodone;
    630  1.16   pooka 		uint8_t *ioaddr, *bufaddr;
    631  1.16   pooka 
    632  1.16   pooka 		for (iodone = 0; iodone < bp->b_bcount;
    633  1.16   pooka 		    iodone += winsize, off += winsize) {
    634  1.16   pooka 			winsize = bp->b_bcount - iodone;
    635  1.16   pooka 			win = getwindow(rblk, off, &winsize, &error);
    636  1.16   pooka 			if (win == NULL) {
    637  1.16   pooka 				rump_biodone(bp, iodone, error);
    638  1.16   pooka 				return;
    639  1.16   pooka 			}
    640  1.16   pooka 
    641  1.16   pooka 			ioaddr = (uint8_t *)win->win_mem + (off-STARTWIN(off));
    642  1.16   pooka 			bufaddr = (uint8_t *)bp->b_data + iodone;
    643  1.11   pooka 
    644  1.16   pooka 			DPRINTF(("strat: %p off 0x%" PRIx64
    645  1.16   pooka 			    ", ioaddr %p (%p)/buf %p\n", win,
    646  1.16   pooka 			    win->win_off, ioaddr, win->win_mem, bufaddr));
    647  1.16   pooka 			if (BUF_ISREAD(bp)) {
    648  1.16   pooka 				memcpy(bufaddr, ioaddr, winsize);
    649  1.16   pooka 			} else {
    650  1.16   pooka 				memcpy(ioaddr, bufaddr, winsize);
    651  1.16   pooka 			}
    652  1.16   pooka 
    653  1.16   pooka 			/* synchronous write, sync bits back to disk */
    654  1.16   pooka 			if (BUF_ISWRITE(bp) && !async) {
    655  1.16   pooka 				rumpuser_memsync(ioaddr, winsize, &error);
    656  1.16   pooka 			}
    657  1.16   pooka 			putwindow(rblk, win);
    658   1.9   pooka 		}
    659   1.9   pooka 
    660   1.9   pooka 		rump_biodone(bp, bp->b_bcount, 0);
    661   1.9   pooka 		return;
    662   1.9   pooka 	}
    663   1.9   pooka 
    664   1.1   pooka 	/*
    665   1.1   pooka 	 * Do I/O.  We have different paths for async and sync I/O.
    666   1.1   pooka 	 * Async I/O is done by passing a request to rumpuser where
    667   1.1   pooka 	 * it is executed.  The rumpuser routine then calls
    668   1.1   pooka 	 * biodone() to signal any waiters in the kernel.  I/O's are
    669   1.1   pooka 	 * executed in series.  Technically executing them in parallel
    670   1.1   pooka 	 * would produce better results, but then we'd need either
    671   1.1   pooka 	 * more threads or posix aio.  Maybe worth investigating
    672   1.1   pooka 	 * this later.
    673  1.14   pooka 	 *
    674  1.14   pooka 	 * Using bufq here might be a good idea.
    675   1.1   pooka 	 */
    676  1.20   pooka 
    677  1.13   pooka 	if (rump_threads) {
    678   1.1   pooka 		struct rumpuser_aio *rua;
    679  1.20   pooka 		int op, fd;
    680  1.20   pooka 
    681  1.20   pooka 		fd = rblk->rblk_fd;
    682  1.20   pooka 		if (BUF_ISREAD(bp)) {
    683  1.20   pooka 			op = RUA_OP_READ;
    684  1.20   pooka 		} else {
    685  1.20   pooka 			op = RUA_OP_WRITE;
    686  1.20   pooka 			if (!async) {
    687  1.20   pooka 				/* O_DIRECT not fully automatic yet */
    688  1.20   pooka #ifdef HAS_ODIRECT
    689  1.20   pooka 				if ((off & (DEV_BSIZE-1)) == 0
    690  1.20   pooka 				    && ((intptr_t)bp->b_data&(DEV_BSIZE-1)) == 0
    691  1.20   pooka 				    && (bp->b_bcount & (DEV_BSIZE-1)) == 0)
    692  1.20   pooka 					fd = rblk->rblk_dfd;
    693  1.20   pooka 				else
    694  1.20   pooka #endif
    695  1.20   pooka 					op |= RUA_OP_SYNC;
    696  1.20   pooka 			}
    697  1.20   pooka 		}
    698   1.1   pooka 
    699   1.1   pooka 		rumpuser_mutex_enter(&rumpuser_aio_mtx);
    700  1.20   pooka 		while ((rumpuser_aio_head+1) % N_AIOS == rumpuser_aio_tail) {
    701  1.13   pooka 			rumpuser_cv_wait(&rumpuser_aio_cv, &rumpuser_aio_mtx);
    702  1.20   pooka 		}
    703   1.1   pooka 
    704   1.2   pooka 		rua = &rumpuser_aios[rumpuser_aio_head];
    705   1.2   pooka 		KASSERT(rua->rua_bp == NULL);
    706  1.20   pooka 		rua->rua_fd = fd;
    707   1.2   pooka 		rua->rua_data = bp->b_data;
    708   1.2   pooka 		rua->rua_dlen = bp->b_bcount;
    709   1.2   pooka 		rua->rua_off = off;
    710   1.2   pooka 		rua->rua_bp = bp;
    711  1.20   pooka 		rua->rua_op = op;
    712   1.2   pooka 
    713   1.1   pooka 		/* insert into queue & signal */
    714   1.6   pooka 		rumpuser_aio_head = (rumpuser_aio_head+1) % N_AIOS;
    715   1.1   pooka 		rumpuser_cv_signal(&rumpuser_aio_cv);
    716   1.1   pooka 		rumpuser_mutex_exit(&rumpuser_aio_mtx);
    717   1.1   pooka 	} else {
    718   1.1   pooka 		if (BUF_ISREAD(bp)) {
    719   1.1   pooka 			rumpuser_read_bio(rblk->rblk_fd, bp->b_data,
    720   1.1   pooka 			    bp->b_bcount, off, rump_biodone, bp);
    721   1.1   pooka 		} else {
    722   1.1   pooka 			rumpuser_write_bio(rblk->rblk_fd, bp->b_data,
    723   1.1   pooka 			    bp->b_bcount, off, rump_biodone, bp);
    724   1.1   pooka 		}
    725  1.20   pooka 		if (BUF_ISWRITE(bp) && !async)
    726  1.20   pooka 			rumpuser_fsync(rblk->rblk_fd, &error);
    727   1.1   pooka 	}
    728   1.1   pooka }
    729   1.3   pooka 
    730   1.3   pooka void
    731   1.3   pooka rumpblk_strategy(struct buf *bp)
    732   1.3   pooka {
    733   1.3   pooka 
    734   1.3   pooka 	dostrategy(bp);
    735   1.3   pooka }
    736   1.3   pooka 
    737   1.3   pooka /*
    738   1.4   pooka  * Simple random number generator.  This is private so that we can
    739   1.4   pooka  * very repeatedly control which blocks will fail.
    740   1.4   pooka  *
    741   1.3   pooka  * <mlelstv> pooka, rand()
    742   1.3   pooka  * <mlelstv> [paste]
    743   1.3   pooka  */
    744   1.3   pooka static unsigned
    745   1.3   pooka gimmerand(void)
    746   1.3   pooka {
    747   1.3   pooka 
    748   1.3   pooka 	return (randstate = randstate * 1103515245 + 12345) % (0x80000000L);
    749   1.3   pooka }
    750   1.3   pooka 
    751   1.3   pooka /*
    752   1.3   pooka  * Block device with very simple fault injection.  Fails every
    753   1.3   pooka  * n out of BLKFAIL_MAX I/O with EIO.  n is determined by the env
    754   1.3   pooka  * variable RUMP_BLKFAIL.
    755   1.3   pooka  */
    756   1.3   pooka void
    757   1.3   pooka rumpblk_strategy_fail(struct buf *bp)
    758   1.3   pooka {
    759   1.3   pooka 
    760   1.3   pooka 	if (gimmerand() % BLKFAIL_MAX >= blkfail) {
    761   1.3   pooka 		dostrategy(bp);
    762   1.3   pooka 	} else {
    763   1.3   pooka 		printf("block fault injection: failing I/O on block %lld\n",
    764   1.3   pooka 		    (long long)bp->b_blkno);
    765   1.3   pooka 		bp->b_error = EIO;
    766   1.3   pooka 		biodone(bp);
    767   1.3   pooka 	}
    768   1.3   pooka }
    769