Home | History | Annotate | Line # | Download | only in dev
md.c revision 1.80
      1 /*	$NetBSD: md.c,v 1.80 2018/03/03 19:26:12 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Gordon W. Ross, Leo Weppelman.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * This implements a general-purpose memory-disk.
     30  * See md.h for notes on the config types.
     31  *
     32  * Note that this driver provides the same functionality
     33  * as the MFS filesystem hack, but this is better because
     34  * you can use this for any filesystem type you'd like!
     35  *
     36  * Credit for most of the kmem ramdisk code goes to:
     37  *   Leo Weppelman (atari) and Phil Nelson (pc532)
     38  * Credit for the ideas behind the "user space memory" code goes
     39  * to the authors of the MFS implementation.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: md.c,v 1.80 2018/03/03 19:26:12 christos Exp $");
     44 
     45 #ifdef _KERNEL_OPT
     46 #include "opt_md.h"
     47 #else
     48 #define MEMORY_DISK_SERVER 1
     49 #endif
     50 
     51 #include <sys/param.h>
     52 #include <sys/kernel.h>
     53 #include <sys/malloc.h>
     54 #include <sys/systm.h>
     55 #include <sys/buf.h>
     56 #include <sys/bufq.h>
     57 #include <sys/device.h>
     58 #include <sys/disk.h>
     59 #include <sys/stat.h>
     60 #include <sys/proc.h>
     61 #include <sys/conf.h>
     62 #include <sys/disklabel.h>
     63 
     64 #include <uvm/uvm_extern.h>
     65 
     66 #include <dev/md.h>
     67 
     68 #include "ioconf.h"
     69 /*
     70  * The user-space functionality is included by default.
     71  * Use  `options MEMORY_DISK_SERVER=0' to turn it off.
     72  */
     73 #ifndef MEMORY_DISK_SERVER
     74 #error MEMORY_DISK_SERVER should be defined by opt_md.h
     75 #endif	/* MEMORY_DISK_SERVER */
     76 
     77 /*
     78  * We should use the raw partition for ioctl.
     79  */
     80 #define MD_UNIT(unit)	DISKUNIT(unit)
     81 
     82 /* autoconfig stuff... */
     83 
     84 struct md_softc {
     85 	device_t sc_dev;	/* Self. */
     86 	struct disk sc_dkdev;	/* hook for generic disk handling */
     87 	struct md_conf sc_md;
     88 	kmutex_t sc_lock;	/* Protect self. */
     89 	kcondvar_t sc_cv;	/* Wait here for work. */
     90 	struct bufq_state *sc_buflist;
     91 };
     92 /* shorthand for fields in sc_md: */
     93 #define sc_addr sc_md.md_addr
     94 #define sc_size sc_md.md_size
     95 #define sc_type sc_md.md_type
     96 
     97 static void	md_attach(device_t, device_t, void *);
     98 static int	md_detach(device_t, int);
     99 
    100 static dev_type_open(mdopen);
    101 static dev_type_close(mdclose);
    102 static dev_type_read(mdread);
    103 static dev_type_write(mdwrite);
    104 static dev_type_ioctl(mdioctl);
    105 static dev_type_strategy(mdstrategy);
    106 static dev_type_size(mdsize);
    107 
    108 const struct bdevsw md_bdevsw = {
    109 	.d_open = mdopen,
    110 	.d_close = mdclose,
    111 	.d_strategy = mdstrategy,
    112 	.d_ioctl = mdioctl,
    113 	.d_dump = nodump,
    114 	.d_psize = mdsize,
    115 	.d_discard = nodiscard,
    116 	.d_flag = D_DISK | D_MPSAFE
    117 };
    118 
    119 const struct cdevsw md_cdevsw = {
    120 	.d_open = mdopen,
    121 	.d_close = mdclose,
    122 	.d_read = mdread,
    123 	.d_write = mdwrite,
    124 	.d_ioctl = mdioctl,
    125 	.d_stop = nostop,
    126 	.d_tty = notty,
    127 	.d_poll = nopoll,
    128 	.d_mmap = nommap,
    129 	.d_kqfilter = nokqfilter,
    130 	.d_discard = nodiscard,
    131 	.d_flag = D_DISK
    132 };
    133 
    134 static struct dkdriver mddkdriver = {
    135 	.d_strategy = mdstrategy
    136 };
    137 
    138 CFATTACH_DECL3_NEW(md, sizeof(struct md_softc),
    139 	0, md_attach, md_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
    140 
    141 static kmutex_t md_device_lock;		/* Protect unit creation / deletion. */
    142 extern size_t md_root_size;
    143 
    144 static void md_set_disklabel(struct md_softc *);
    145 
    146 /*
    147  * This is called if we are configured as a pseudo-device
    148  */
    149 void
    150 mdattach(int n)
    151 {
    152 
    153 	mutex_init(&md_device_lock, MUTEX_DEFAULT, IPL_NONE);
    154 	if (config_cfattach_attach(md_cd.cd_name, &md_ca)) {
    155 		aprint_error("%s: cfattach_attach failed\n", md_cd.cd_name);
    156 		return;
    157 	}
    158 }
    159 
    160 static void
    161 md_attach(device_t parent, device_t self, void *aux)
    162 {
    163 	struct md_softc *sc = device_private(self);
    164 
    165 	sc->sc_dev = self;
    166 	sc->sc_type = MD_UNCONFIGURED;
    167 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    168 	cv_init(&sc->sc_cv, "mdidle");
    169 	bufq_alloc(&sc->sc_buflist, "fcfs", 0);
    170 
    171 	/* XXX - Could accept aux info here to set the config. */
    172 #ifdef	MEMORY_DISK_HOOKS
    173 	/*
    174 	 * This external function might setup a pre-loaded disk.
    175 	 * All it would need to do is setup the md_conf struct.
    176 	 * See sys/dev/md_root.c for an example.
    177 	 */
    178 	md_attach_hook(device_unit(self), &sc->sc_md);
    179 #endif
    180 
    181 	/*
    182 	 * Initialize and attach the disk structure.
    183 	 */
    184 	disk_init(&sc->sc_dkdev, device_xname(self), &mddkdriver);
    185 	disk_attach(&sc->sc_dkdev);
    186 
    187 	if (sc->sc_type != MD_UNCONFIGURED)
    188 		md_set_disklabel(sc);
    189 
    190 	if (!pmf_device_register(self, NULL, NULL))
    191 		aprint_error_dev(self, "couldn't establish power handler\n");
    192 }
    193 
    194 static int
    195 md_detach(device_t self, int flags)
    196 {
    197 	struct md_softc *sc = device_private(self);
    198 	int rc;
    199 
    200 	rc = 0;
    201 	mutex_enter(&sc->sc_dkdev.dk_openlock);
    202 	if (sc->sc_dkdev.dk_openmask == 0 && sc->sc_type == MD_UNCONFIGURED)
    203 		;	/* nothing to do */
    204 	else if ((flags & DETACH_FORCE) == 0)
    205 		rc = EBUSY;
    206 	mutex_exit(&sc->sc_dkdev.dk_openlock);
    207 
    208 	if (rc != 0)
    209 		return rc;
    210 
    211 	pmf_device_deregister(self);
    212 	disk_detach(&sc->sc_dkdev);
    213 	disk_destroy(&sc->sc_dkdev);
    214 	bufq_free(sc->sc_buflist);
    215 	mutex_destroy(&sc->sc_lock);
    216 	cv_destroy(&sc->sc_cv);
    217 	return 0;
    218 }
    219 
    220 /*
    221  * operational routines:
    222  * open, close, read, write, strategy,
    223  * ioctl, dump, size
    224  */
    225 
    226 #if MEMORY_DISK_SERVER
    227 static int	md_server_loop(struct md_softc *sc);
    228 static int	md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
    229 		    struct lwp *l);
    230 #endif	/* MEMORY_DISK_SERVER */
    231 static int	md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
    232 		    struct lwp *l);
    233 
    234 static int
    235 mdsize(dev_t dev)
    236 {
    237 	struct md_softc *sc;
    238 	int res;
    239 
    240 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
    241 	if (sc == NULL)
    242 		return 0;
    243 
    244 	mutex_enter(&sc->sc_lock);
    245 	if (sc->sc_type == MD_UNCONFIGURED)
    246 		res = 0;
    247 	else
    248 		res = sc->sc_size >> DEV_BSHIFT;
    249 	mutex_exit(&sc->sc_lock);
    250 
    251 	return res;
    252 }
    253 
    254 static int
    255 mdopen(dev_t dev, int flag, int fmt, struct lwp *l)
    256 {
    257 	int unit;
    258 	int part = DISKPART(dev);
    259 	int pmask = 1 << part;
    260 	cfdata_t cf;
    261 	struct md_softc *sc;
    262 	struct disk *dk;
    263 #ifdef	MEMORY_DISK_HOOKS
    264 	bool configured;
    265 #endif
    266 
    267 	mutex_enter(&md_device_lock);
    268 	unit = MD_UNIT(dev);
    269 	sc = device_lookup_private(&md_cd, unit);
    270 	if (sc == NULL) {
    271 		if (part != RAW_PART) {
    272 			mutex_exit(&md_device_lock);
    273 			return ENXIO;
    274 		}
    275 		cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
    276 		cf->cf_name = md_cd.cd_name;
    277 		cf->cf_atname = md_cd.cd_name;
    278 		cf->cf_unit = unit;
    279 		cf->cf_fstate = FSTATE_STAR;
    280 		sc = device_private(config_attach_pseudo(cf));
    281 		if (sc == NULL) {
    282 			mutex_exit(&md_device_lock);
    283 			return ENOMEM;
    284 		}
    285 	}
    286 
    287 	dk = &sc->sc_dkdev;
    288 
    289 	/*
    290 	 * The raw partition is used for ioctl to configure.
    291 	 */
    292 	if (part == RAW_PART)
    293 		goto ok;
    294 
    295 #ifdef	MEMORY_DISK_HOOKS
    296 	/* Call the open hook to allow loading the device. */
    297 	configured = (sc->sc_type != MD_UNCONFIGURED);
    298 	md_open_hook(unit, &sc->sc_md);
    299 	/* initialize disklabel if the device is configured in open hook */
    300 	if (!configured && sc->sc_type != MD_UNCONFIGURED)
    301 		md_set_disklabel(sc);
    302 #endif
    303 
    304 	/*
    305 	 * This is a normal, "slave" device, so
    306 	 * enforce initialized.
    307 	 */
    308 	if (sc->sc_type == MD_UNCONFIGURED) {
    309 		mutex_exit(&md_device_lock);
    310 		return ENXIO;
    311 	}
    312 
    313 ok:
    314 	/* XXX duplicates code in dk_open().  Call dk_open(), instead? */
    315 	mutex_enter(&dk->dk_openlock);
    316 	/* Mark our unit as open. */
    317 	switch (fmt) {
    318 	case S_IFCHR:
    319 		dk->dk_copenmask |= pmask;
    320 		break;
    321 	case S_IFBLK:
    322 		dk->dk_bopenmask |= pmask;
    323 		break;
    324 	}
    325 
    326 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    327 
    328 	mutex_exit(&dk->dk_openlock);
    329 	mutex_exit(&md_device_lock);
    330 	return 0;
    331 }
    332 
    333 static int
    334 mdclose(dev_t dev, int flag, int fmt, struct lwp *l)
    335 {
    336 	int part = DISKPART(dev);
    337 	int pmask = 1 << part;
    338 	int error;
    339 	cfdata_t cf;
    340 	struct md_softc *sc;
    341 	struct disk *dk;
    342 
    343 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
    344 	if (sc == NULL)
    345 		return ENXIO;
    346 
    347 	dk = &sc->sc_dkdev;
    348 
    349 	mutex_enter(&dk->dk_openlock);
    350 
    351 	switch (fmt) {
    352 	case S_IFCHR:
    353 		dk->dk_copenmask &= ~pmask;
    354 		break;
    355 	case S_IFBLK:
    356 		dk->dk_bopenmask &= ~pmask;
    357 		break;
    358 	}
    359 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    360 	if (dk->dk_openmask != 0) {
    361 		mutex_exit(&dk->dk_openlock);
    362 		return 0;
    363 	}
    364 
    365 	mutex_exit(&dk->dk_openlock);
    366 
    367 	mutex_enter(&md_device_lock);
    368 	cf = device_cfdata(sc->sc_dev);
    369 	error = config_detach(sc->sc_dev, DETACH_QUIET);
    370 	if (! error)
    371 		free(cf, M_DEVBUF);
    372 	mutex_exit(&md_device_lock);
    373 	return error;
    374 }
    375 
    376 static int
    377 mdread(dev_t dev, struct uio *uio, int flags)
    378 {
    379 	struct md_softc *sc;
    380 
    381 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
    382 
    383 	if (sc == NULL || sc->sc_type == MD_UNCONFIGURED)
    384 		return ENXIO;
    385 
    386 	return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
    387 }
    388 
    389 static int
    390 mdwrite(dev_t dev, struct uio *uio, int flags)
    391 {
    392 	struct md_softc *sc;
    393 
    394 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
    395 
    396 	if (sc == NULL || sc->sc_type == MD_UNCONFIGURED)
    397 		return ENXIO;
    398 
    399 	return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
    400 }
    401 
    402 /*
    403  * Handle I/O requests, either directly, or
    404  * by passing them to the server process.
    405  */
    406 static void
    407 mdstrategy(struct buf *bp)
    408 {
    409 	struct md_softc	*sc;
    410 	void *	addr;
    411 	size_t off, xfer;
    412 	bool is_read;
    413 
    414 	sc = device_lookup_private(&md_cd, MD_UNIT(bp->b_dev));
    415 
    416 	if (sc == NULL || sc->sc_type == MD_UNCONFIGURED) {
    417 		bp->b_error = ENXIO;
    418 		goto done;
    419 	}
    420 
    421 	mutex_enter(&sc->sc_lock);
    422 
    423 	switch (sc->sc_type) {
    424 #if MEMORY_DISK_SERVER
    425 	case MD_UMEM_SERVER:
    426 		/* Just add this job to the server's queue. */
    427 		bufq_put(sc->sc_buflist, bp);
    428 		cv_signal(&sc->sc_cv);
    429 		mutex_exit(&sc->sc_lock);
    430 		/* see md_server_loop() */
    431 		/* no biodone in this case */
    432 		return;
    433 #endif	/* MEMORY_DISK_SERVER */
    434 
    435 	case MD_KMEM_FIXED:
    436 	case MD_KMEM_ALLOCATED:
    437 		/* These are in kernel space.  Access directly. */
    438 		is_read = ((bp->b_flags & B_READ) == B_READ);
    439 		bp->b_resid = bp->b_bcount;
    440 		off = (bp->b_blkno << DEV_BSHIFT);
    441 		if (off >= sc->sc_size) {
    442 			if (is_read)
    443 				break;	/* EOF */
    444 			goto set_eio;
    445 		}
    446 		xfer = bp->b_resid;
    447 		if (xfer > (sc->sc_size - off))
    448 			xfer = (sc->sc_size - off);
    449 		addr = (char *)sc->sc_addr + off;
    450 		disk_busy(&sc->sc_dkdev);
    451 		if (is_read)
    452 			memcpy(bp->b_data, addr, xfer);
    453 		else
    454 			memcpy(addr, bp->b_data, xfer);
    455 		disk_unbusy(&sc->sc_dkdev, xfer, is_read);
    456 		bp->b_resid -= xfer;
    457 		break;
    458 
    459 	default:
    460 		bp->b_resid = bp->b_bcount;
    461 	set_eio:
    462 		bp->b_error = EIO;
    463 		break;
    464 	}
    465 	mutex_exit(&sc->sc_lock);
    466 
    467  done:
    468 
    469 	biodone(bp);
    470 }
    471 
    472 static int
    473 mdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    474 {
    475 	struct md_softc *sc;
    476 	struct md_conf *umd;
    477 	int error;
    478 
    479 	if ((sc = device_lookup_private(&md_cd, MD_UNIT(dev))) == NULL)
    480 		return ENXIO;
    481 
    482 	mutex_enter(&sc->sc_lock);
    483 	if (sc->sc_type != MD_UNCONFIGURED) {
    484 		error = disk_ioctl(&sc->sc_dkdev, dev, cmd, data, flag, l);
    485 		if (error != EPASSTHROUGH) {
    486 			mutex_exit(&sc->sc_lock);
    487 			return 0;
    488 		}
    489 	}
    490 
    491 	/* If this is not the raw partition, punt! */
    492 	if (DISKPART(dev) != RAW_PART) {
    493 		mutex_exit(&sc->sc_lock);
    494 		return ENOTTY;
    495 	}
    496 
    497 	umd = (struct md_conf *)data;
    498 	error = EINVAL;
    499 	switch (cmd) {
    500 	case MD_GETCONF:
    501 		*umd = sc->sc_md;
    502 		error = 0;
    503 		break;
    504 
    505 	case MD_SETCONF:
    506 		/* Can only set it once. */
    507 		if (sc->sc_type != MD_UNCONFIGURED)
    508 			break;
    509 		switch (umd->md_type) {
    510 		case MD_KMEM_ALLOCATED:
    511 			error = md_ioctl_kalloc(sc, umd, l);
    512 			break;
    513 #if MEMORY_DISK_SERVER
    514 		case MD_UMEM_SERVER:
    515 			error = md_ioctl_server(sc, umd, l);
    516 			break;
    517 #endif	/* MEMORY_DISK_SERVER */
    518 		default:
    519 			break;
    520 		}
    521 		break;
    522 	}
    523 	mutex_exit(&sc->sc_lock);
    524 	return error;
    525 }
    526 
    527 static void
    528 md_set_disklabel(struct md_softc *sc)
    529 {
    530 	struct disk_geom *dg = &sc->sc_dkdev.dk_geom;
    531 	struct disklabel *lp = sc->sc_dkdev.dk_label;
    532 	struct partition *pp;
    533 
    534 	memset(lp, 0, sizeof(*lp));
    535 
    536 	lp->d_secsize = DEV_BSIZE;
    537 	lp->d_secperunit = sc->sc_size / DEV_BSIZE;
    538 	if (lp->d_secperunit >= (32*64)) {
    539 		lp->d_nsectors = 32;
    540 		lp->d_ntracks = 64;
    541 		lp->d_ncylinders = lp->d_secperunit / (32*64);
    542 	} else {
    543 		lp->d_nsectors = 1;
    544 		lp->d_ntracks = 1;
    545 		lp->d_ncylinders = lp->d_secperunit;
    546 	}
    547 	lp->d_secpercyl = lp->d_ntracks*lp->d_nsectors;
    548 
    549 	strncpy(lp->d_typename, md_cd.cd_name, sizeof(lp->d_typename));
    550 	lp->d_type = DKTYPE_MD;
    551 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    552 	lp->d_rpm = 3600;
    553 	lp->d_interleave = 1;
    554 	lp->d_flags = 0;
    555 
    556 	pp = &lp->d_partitions[0];
    557 	pp->p_offset = 0;
    558 	pp->p_size = lp->d_secperunit;
    559 	pp->p_fstype = FS_BSDFFS;
    560 
    561 	pp = &lp->d_partitions[RAW_PART];
    562 	pp->p_offset = 0;
    563 	pp->p_size = lp->d_secperunit;
    564 	pp->p_fstype = FS_UNUSED;
    565 
    566 	lp->d_npartitions = RAW_PART+1;
    567 	lp->d_magic = DISKMAGIC;
    568 	lp->d_magic2 = DISKMAGIC;
    569 	lp->d_checksum = dkcksum(lp);
    570 
    571 	memset(dg, 0, sizeof(*dg));
    572 
    573 	dg->dg_secsize = lp->d_secsize;
    574 	dg->dg_secperunit = lp->d_secperunit;
    575 	dg->dg_nsectors = lp->d_nsectors;
    576 	dg->dg_ntracks = lp->d_ntracks = 64;;
    577 	dg->dg_ncylinders = lp->d_ncylinders;
    578 
    579 	disk_set_info(sc->sc_dev, &sc->sc_dkdev, NULL);
    580 }
    581 
    582 /*
    583  * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
    584  * Just allocate some kernel memory and return.
    585  */
    586 static int
    587 md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
    588     struct lwp *l)
    589 {
    590 	vaddr_t addr;
    591 	vsize_t size;
    592 
    593 	mutex_exit(&sc->sc_lock);
    594 
    595 	/* Sanity check the size. */
    596 	size = umd->md_size;
    597 	addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
    598 
    599 	mutex_enter(&sc->sc_lock);
    600 
    601 	if (!addr)
    602 		return ENOMEM;
    603 
    604 	/* If another thread beat us to configure this unit:  fail. */
    605 	if (sc->sc_type != MD_UNCONFIGURED) {
    606 		uvm_km_free(kernel_map, addr, size, UVM_KMF_WIRED);
    607 		return EINVAL;
    608 	}
    609 
    610 	/* This unit is now configured. */
    611 	sc->sc_addr = (void *)addr; 	/* kernel space */
    612 	sc->sc_size = (size_t)size;
    613 	sc->sc_type = MD_KMEM_ALLOCATED;
    614 	md_set_disklabel(sc);
    615 	return 0;
    616 }
    617 
    618 #if MEMORY_DISK_SERVER
    619 
    620 /*
    621  * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
    622  * Set config, then become the I/O server for this unit.
    623  */
    624 static int
    625 md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
    626     struct lwp *l)
    627 {
    628 	vaddr_t end;
    629 	int error;
    630 
    631 	KASSERT(mutex_owned(&sc->sc_lock));
    632 
    633 	/* Sanity check addr, size. */
    634 	end = (vaddr_t) ((char *)umd->md_addr + umd->md_size);
    635 
    636 	if (
    637 #ifndef _RUMPKERNEL
    638 	    /*
    639 	     * On some architectures (e.g. powerpc) rump kernel provides
    640 	     * "safe" low defaults which make this test fail since malloc
    641 	     * does return higher addresses than the "safe" default.
    642 	     */
    643 	    (end >= VM_MAXUSER_ADDRESS) ||
    644 #endif
    645 	    (end < ((vaddr_t) umd->md_addr)))
    646 		return EINVAL;
    647 
    648 	/* This unit is now configured. */
    649 	sc->sc_addr = umd->md_addr; 	/* user space */
    650 	sc->sc_size = umd->md_size;
    651 	sc->sc_type = MD_UMEM_SERVER;
    652 	md_set_disklabel(sc);
    653 
    654 	/* Become the server daemon */
    655 	error = md_server_loop(sc);
    656 
    657 	/* This server is now going away! */
    658 	sc->sc_type = MD_UNCONFIGURED;
    659 	sc->sc_addr = 0;
    660 	sc->sc_size = 0;
    661 
    662 	return (error);
    663 }
    664 
    665 static int
    666 md_server_loop(struct md_softc *sc)
    667 {
    668 	struct buf *bp;
    669 	void *addr;	/* user space address */
    670 	size_t off;	/* offset into "device" */
    671 	size_t xfer;	/* amount to transfer */
    672 	int error;
    673 	bool is_read;
    674 
    675 	KASSERT(mutex_owned(&sc->sc_lock));
    676 
    677 	for (;;) {
    678 		/* Wait for some work to arrive. */
    679 		while ((bp = bufq_get(sc->sc_buflist)) == NULL) {
    680 			error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
    681 			if (error)
    682 				return error;
    683 		}
    684 
    685 		/* Do the transfer to/from user space. */
    686 		mutex_exit(&sc->sc_lock);
    687 		error = 0;
    688 		is_read = ((bp->b_flags & B_READ) == B_READ);
    689 		bp->b_resid = bp->b_bcount;
    690 		off = (bp->b_blkno << DEV_BSHIFT);
    691 		if (off >= sc->sc_size) {
    692 			if (is_read)
    693 				goto done;	/* EOF (not an error) */
    694 			error = EIO;
    695 			goto done;
    696 		}
    697 		xfer = bp->b_resid;
    698 		if (xfer > (sc->sc_size - off))
    699 			xfer = (sc->sc_size - off);
    700 		addr = (char *)sc->sc_addr + off;
    701 		disk_busy(&sc->sc_dkdev);
    702 		if (is_read)
    703 			error = copyin(addr, bp->b_data, xfer);
    704 		else
    705 			error = copyout(bp->b_data, addr, xfer);
    706 		disk_unbusy(&sc->sc_dkdev, (error ? 0 : xfer), is_read);
    707 		if (!error)
    708 			bp->b_resid -= xfer;
    709 
    710 	done:
    711 		if (error) {
    712 			bp->b_error = error;
    713 		}
    714 		biodone(bp);
    715 		mutex_enter(&sc->sc_lock);
    716 	}
    717 }
    718 #endif	/* MEMORY_DISK_SERVER */
    719