Home | History | Annotate | Line # | Download | only in dev
md.c revision 1.78.6.6
      1 /*	$NetBSD: md.c,v 1.78.6.6 2017/05/17 01:44:17 pgoyette 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.78.6.6 2017/05/17 01:44:17 pgoyette 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 	DEVSW_MODULE_INIT
    110 	.d_open = mdopen,
    111 	.d_close = mdclose,
    112 	.d_strategy = mdstrategy,
    113 	.d_ioctl = mdioctl,
    114 	.d_dump = nodump,
    115 	.d_psize = mdsize,
    116 	.d_discard = nodiscard,
    117 	.d_flag = D_DISK | D_MPSAFE
    118 };
    119 
    120 const struct cdevsw md_cdevsw = {
    121 	DEVSW_MODULE_INIT
    122 	.d_open = mdopen,
    123 	.d_close = mdclose,
    124 	.d_read = mdread,
    125 	.d_write = mdwrite,
    126 	.d_ioctl = mdioctl,
    127 	.d_stop = nostop,
    128 	.d_tty = notty,
    129 	.d_poll = nopoll,
    130 	.d_mmap = nommap,
    131 	.d_kqfilter = nokqfilter,
    132 	.d_discard = nodiscard,
    133 	.d_flag = D_DISK
    134 };
    135 
    136 static struct dkdriver mddkdriver = {
    137 	.d_strategy = mdstrategy
    138 };
    139 
    140 extern struct cfdriver md_cd;
    141 CFATTACH_DECL3_NEW(md, sizeof(struct md_softc),
    142 	0, md_attach, md_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
    143 
    144 static kmutex_t md_device_lock;		/* Protect unit creation / deletion. */
    145 extern size_t md_root_size;
    146 
    147 static void md_set_disklabel(struct md_softc *);
    148 
    149 /*
    150  * This is called if we are configured as a pseudo-device
    151  */
    152 void
    153 mdattach(int n)
    154 {
    155 
    156 	mutex_init(&md_device_lock, MUTEX_DEFAULT, IPL_NONE);
    157 	if (config_cfattach_attach(md_cd.cd_name, &md_ca)) {
    158 		aprint_error("%s: cfattach_attach failed\n", md_cd.cd_name);
    159 		return;
    160 	}
    161 }
    162 
    163 static void
    164 md_attach(device_t parent, device_t self, void *aux)
    165 {
    166 	struct md_softc *sc = device_private(self);
    167 
    168 	sc->sc_dev = self;
    169 	sc->sc_type = MD_UNCONFIGURED;
    170 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    171 	cv_init(&sc->sc_cv, "mdidle");
    172 	bufq_alloc(&sc->sc_buflist, "fcfs", 0);
    173 
    174 	/* XXX - Could accept aux info here to set the config. */
    175 #ifdef	MEMORY_DISK_HOOKS
    176 	/*
    177 	 * This external function might setup a pre-loaded disk.
    178 	 * All it would need to do is setup the md_conf struct.
    179 	 * See sys/dev/md_root.c for an example.
    180 	 */
    181 	md_attach_hook(device_unit(self), &sc->sc_md);
    182 #endif
    183 
    184 	/*
    185 	 * Initialize and attach the disk structure.
    186 	 */
    187 	disk_init(&sc->sc_dkdev, device_xname(self), &mddkdriver);
    188 	disk_attach(&sc->sc_dkdev);
    189 
    190 	if (sc->sc_type != MD_UNCONFIGURED)
    191 		md_set_disklabel(sc);
    192 
    193 	if (!pmf_device_register(self, NULL, NULL))
    194 		aprint_error_dev(self, "couldn't establish power handler\n");
    195 }
    196 
    197 /*
    198  * Caller must hold a reference to the device's localcount.
    199  */
    200 static int
    201 md_detach(device_t self, int flags)
    202 {
    203 	struct md_softc *sc = device_private(self);
    204 	int rc;
    205 
    206 	rc = 0;
    207 	mutex_enter(&sc->sc_dkdev.dk_openlock);
    208 	if (sc->sc_dkdev.dk_openmask == 0 && sc->sc_type == MD_UNCONFIGURED)
    209 		;	/* nothing to do */
    210 	else if ((flags & DETACH_FORCE) == 0)
    211 		rc = EBUSY;
    212 	mutex_exit(&sc->sc_dkdev.dk_openlock);
    213 
    214 	if (rc != 0)
    215 		return rc;
    216 
    217 	pmf_device_deregister(self);
    218 	disk_detach(&sc->sc_dkdev);
    219 	disk_destroy(&sc->sc_dkdev);
    220 	bufq_free(sc->sc_buflist);
    221 	mutex_destroy(&sc->sc_lock);
    222 	cv_destroy(&sc->sc_cv);
    223 	return 0;
    224 }
    225 
    226 /*
    227  * operational routines:
    228  * open, close, read, write, strategy,
    229  * ioctl, dump, size
    230  */
    231 
    232 #if MEMORY_DISK_SERVER
    233 static int	md_server_loop(struct md_softc *sc);
    234 static int	md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
    235 		    struct lwp *l);
    236 #endif	/* MEMORY_DISK_SERVER */
    237 static int	md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
    238 		    struct lwp *l);
    239 
    240 static int
    241 mdsize(dev_t dev)
    242 {
    243 	struct md_softc *sc;
    244 	int res;
    245 
    246 	sc = device_lookup_private_acquire(&md_cd, MD_UNIT(dev));
    247 	if (sc == NULL)
    248 		return 0;
    249 
    250 	mutex_enter(&sc->sc_lock);
    251 	if (sc->sc_type == MD_UNCONFIGURED)
    252 		res = 0;
    253 	else
    254 		res = sc->sc_size >> DEV_BSHIFT;
    255 	mutex_exit(&sc->sc_lock);
    256 
    257 	device_release(sc->sc_dev);
    258 	return res;
    259 }
    260 
    261 static int
    262 mdopen(dev_t dev, int flag, int fmt, struct lwp *l)
    263 {
    264 	device_t self;
    265 	int unit;
    266 	int part = DISKPART(dev);
    267 	int pmask = 1 << part;
    268 	cfdata_t cf;
    269 	struct md_softc *sc;
    270 	struct disk *dk;
    271 #ifdef	MEMORY_DISK_HOOKS
    272 	bool configured;
    273 #endif
    274 
    275 	mutex_enter(&md_device_lock);
    276 	unit = MD_UNIT(dev);
    277 	sc = device_lookup_private_acquire(&md_cd, unit);
    278 	if (sc == NULL) {
    279 		if (part != RAW_PART) {
    280 			mutex_exit(&md_device_lock);
    281 			return ENXIO;
    282 		}
    283 		cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
    284 		cf->cf_name = md_cd.cd_name;
    285 		cf->cf_atname = md_cd.cd_name;
    286 		cf->cf_unit = unit;
    287 		cf->cf_fstate = FSTATE_STAR;
    288 		self = config_attach_pseudo(cf);
    289 		sc = device_private(self);
    290 		if (sc == NULL) {
    291 			mutex_exit(&md_device_lock);
    292 			device_release(self);
    293 			return ENOMEM;
    294 		}
    295 	}
    296 
    297 	dk = &sc->sc_dkdev;
    298 
    299 	/*
    300 	 * The raw partition is used for ioctl to configure.
    301 	 */
    302 	if (part == RAW_PART)
    303 		goto ok;
    304 
    305 #ifdef	MEMORY_DISK_HOOKS
    306 	/* Call the open hook to allow loading the device. */
    307 	configured = (sc->sc_type != MD_UNCONFIGURED);
    308 	md_open_hook(unit, &sc->sc_md);
    309 	/* initialize disklabel if the device is configured in open hook */
    310 	if (!configured && sc->sc_type != MD_UNCONFIGURED)
    311 		md_set_disklabel(sc);
    312 #endif
    313 
    314 	/*
    315 	 * This is a normal, "slave" device, so
    316 	 * enforce initialized.
    317 	 */
    318 	if (sc->sc_type == MD_UNCONFIGURED) {
    319 		mutex_exit(&md_device_lock);
    320 		device_release(sc->sc_dev);
    321 		return ENXIO;
    322 	}
    323 
    324  ok:
    325 	/* XXX duplicates code in dk_open().  Call dk_open(), instead? */
    326 	mutex_enter(&dk->dk_openlock);
    327 	/* Mark our unit as open. */
    328 	switch (fmt) {
    329 	case S_IFCHR:
    330 		dk->dk_copenmask |= pmask;
    331 		break;
    332 	case S_IFBLK:
    333 		dk->dk_bopenmask |= pmask;
    334 		break;
    335 	}
    336 
    337 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    338 
    339 	mutex_exit(&dk->dk_openlock);
    340 	mutex_exit(&md_device_lock);
    341 	device_release(sc->sc_dev);
    342 	return 0;
    343 }
    344 
    345 static int
    346 mdclose(dev_t dev, int flag, int fmt, struct lwp *l)
    347 {
    348 	int part = DISKPART(dev);
    349 	int pmask = 1 << part;
    350 	int error;
    351 	cfdata_t cf;
    352 	struct md_softc *sc;
    353 	struct disk *dk;
    354 
    355 	sc = device_lookup_private_acquire(&md_cd, MD_UNIT(dev));
    356 	if (sc == NULL)
    357 		return ENXIO;
    358 
    359 	dk = &sc->sc_dkdev;
    360 
    361 	mutex_enter(&dk->dk_openlock);
    362 
    363 	switch (fmt) {
    364 	case S_IFCHR:
    365 		dk->dk_copenmask &= ~pmask;
    366 		break;
    367 	case S_IFBLK:
    368 		dk->dk_bopenmask &= ~pmask;
    369 		break;
    370 	}
    371 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    372 	if (dk->dk_openmask != 0) {
    373 		mutex_exit(&dk->dk_openlock);
    374 		device_release(sc->sc_dev);
    375 		return 0;
    376 	}
    377 
    378 	mutex_exit(&dk->dk_openlock);
    379 
    380 	mutex_enter(&md_device_lock);
    381 	cf = device_cfdata(sc->sc_dev);
    382 	error = config_detach_release(sc->sc_dev, DETACH_QUIET);
    383 	if (! error)
    384 		free(cf, M_DEVBUF);
    385 	mutex_exit(&md_device_lock);
    386 
    387 	return error;
    388 }
    389 
    390 static int
    391 mdread(dev_t dev, struct uio *uio, int flags)
    392 {
    393 	struct md_softc *sc;
    394 	int error;
    395 
    396 	sc = device_lookup_private_acquire(&md_cd, MD_UNIT(dev));
    397 
    398 	if (sc == NULL || sc->sc_type == MD_UNCONFIGURED) {
    399 		if (sc != NULL)
    400 			device_release(sc->sc_dev);
    401 		return ENXIO;
    402 	}
    403 
    404 	error = (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
    405 	device_release(sc->sc_dev);
    406 	return error;
    407 }
    408 
    409 static int
    410 mdwrite(dev_t dev, struct uio *uio, int flags)
    411 {
    412 	struct md_softc *sc;
    413 	int error;
    414 
    415 	sc = device_lookup_private_acquire(&md_cd, MD_UNIT(dev));
    416 
    417 	if (sc == NULL || sc->sc_type == MD_UNCONFIGURED) {
    418 		if (sc != NULL) {
    419 			device_release(sc->sc_dev);
    420 		}
    421 		return ENXIO;
    422 	}
    423 
    424 	error = physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio);
    425 
    426 	device_release(sc->sc_dev);
    427 	return error;
    428 }
    429 
    430 /*
    431  * Handle I/O requests, either directly, or
    432  * by passing them to the server process.
    433  */
    434 static void
    435 mdstrategy(struct buf *bp)
    436 {
    437 	struct md_softc	*sc;
    438 	void *	addr;
    439 	size_t off, xfer;
    440 	bool is_read;
    441 
    442 	sc = device_lookup_private_acquire(&md_cd, MD_UNIT(bp->b_dev));
    443 	if (sc == NULL || sc->sc_type == MD_UNCONFIGURED) {
    444 		bp->b_error = ENXIO;
    445 		goto done;
    446 	}
    447 
    448 	mutex_enter(&sc->sc_lock);
    449 
    450 	switch (sc->sc_type) {
    451 #if MEMORY_DISK_SERVER
    452 	case MD_UMEM_SERVER:
    453 		/* Just add this job to the server's queue. */
    454 		bufq_put(sc->sc_buflist, bp);
    455 		cv_signal(&sc->sc_cv);
    456 		mutex_exit(&sc->sc_lock);
    457 		/* see md_server_loop() */
    458 		/* no biodone in this case */
    459 		device_release(sc->sc_dev);
    460 		return;
    461 #endif	/* MEMORY_DISK_SERVER */
    462 
    463 	case MD_KMEM_FIXED:
    464 	case MD_KMEM_ALLOCATED:
    465 		/* These are in kernel space.  Access directly. */
    466 		is_read = ((bp->b_flags & B_READ) == B_READ);
    467 		bp->b_resid = bp->b_bcount;
    468 		off = (bp->b_blkno << DEV_BSHIFT);
    469 		if (off >= sc->sc_size) {
    470 			if (is_read)
    471 				break;	/* EOF */
    472 			goto set_eio;
    473 		}
    474 		xfer = bp->b_resid;
    475 		if (xfer > (sc->sc_size - off))
    476 			xfer = (sc->sc_size - off);
    477 		addr = (char *)sc->sc_addr + off;
    478 		disk_busy(&sc->sc_dkdev);
    479 		if (is_read)
    480 			memcpy(bp->b_data, addr, xfer);
    481 		else
    482 			memcpy(addr, bp->b_data, xfer);
    483 		disk_unbusy(&sc->sc_dkdev, xfer, is_read);
    484 		bp->b_resid -= xfer;
    485 		break;
    486 
    487 	default:
    488 		bp->b_resid = bp->b_bcount;
    489 	set_eio:
    490 		bp->b_error = EIO;
    491 		break;
    492 	}
    493 	mutex_exit(&sc->sc_lock);
    494 
    495  done:
    496 	biodone(bp);
    497 	if (sc != NULL)
    498 		device_release(sc->sc_dev);
    499 }
    500 
    501 static int
    502 mdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    503 {
    504 	struct md_softc *sc;
    505 	struct md_conf *umd;
    506 	int error;
    507 
    508 	sc = device_lookup_private_acquire(&md_cd, MD_UNIT(dev));
    509 	if (sc == NULL)
    510 		return ENXIO;
    511 
    512 	mutex_enter(&sc->sc_lock);
    513 	if (sc->sc_type != MD_UNCONFIGURED) {
    514 		error = disk_ioctl(&sc->sc_dkdev, dev, cmd, data, flag, l);
    515 		if (error != EPASSTHROUGH) {
    516 			mutex_exit(&sc->sc_lock);
    517 			device_release(sc->sc_dev);
    518 			return 0;
    519 		}
    520 	}
    521 
    522 	/* If this is not the raw partition, punt! */
    523 	if (DISKPART(dev) != RAW_PART) {
    524 		mutex_exit(&sc->sc_lock);
    525 		device_release(sc->sc_dev);
    526 		return ENOTTY;
    527 	}
    528 
    529 	umd = (struct md_conf *)data;
    530 	error = EINVAL;
    531 	switch (cmd) {
    532 	case MD_GETCONF:
    533 		*umd = sc->sc_md;
    534 		error = 0;
    535 		break;
    536 
    537 	case MD_SETCONF:
    538 		/* Can only set it once. */
    539 		if (sc->sc_type != MD_UNCONFIGURED)
    540 			break;
    541 		switch (umd->md_type) {
    542 		case MD_KMEM_ALLOCATED:
    543 			error = md_ioctl_kalloc(sc, umd, l);
    544 			break;
    545 #if MEMORY_DISK_SERVER
    546 		case MD_UMEM_SERVER:
    547 			error = md_ioctl_server(sc, umd, l);
    548 			break;
    549 #endif	/* MEMORY_DISK_SERVER */
    550 		default:
    551 			break;
    552 		}
    553 		break;
    554 	}
    555 	mutex_exit(&sc->sc_lock);
    556 	device_release(sc->sc_dev);
    557 	return error;
    558 }
    559 
    560 static void
    561 md_set_disklabel(struct md_softc *sc)
    562 {
    563 	struct disk_geom *dg = &sc->sc_dkdev.dk_geom;
    564 	struct disklabel *lp = sc->sc_dkdev.dk_label;
    565 	struct partition *pp;
    566 
    567 	memset(lp, 0, sizeof(*lp));
    568 
    569 	lp->d_secsize = DEV_BSIZE;
    570 	lp->d_secperunit = sc->sc_size / DEV_BSIZE;
    571 	if (lp->d_secperunit >= (32*64)) {
    572 		lp->d_nsectors = 32;
    573 		lp->d_ntracks = 64;
    574 		lp->d_ncylinders = lp->d_secperunit / (32*64);
    575 	} else {
    576 		lp->d_nsectors = 1;
    577 		lp->d_ntracks = 1;
    578 		lp->d_ncylinders = lp->d_secperunit;
    579 	}
    580 	lp->d_secpercyl = lp->d_ntracks*lp->d_nsectors;
    581 
    582 	strncpy(lp->d_typename, md_cd.cd_name, sizeof(lp->d_typename));
    583 	lp->d_type = DKTYPE_MD;
    584 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    585 	lp->d_rpm = 3600;
    586 	lp->d_interleave = 1;
    587 	lp->d_flags = 0;
    588 
    589 	pp = &lp->d_partitions[0];
    590 	pp->p_offset = 0;
    591 	pp->p_size = lp->d_secperunit;
    592 	pp->p_fstype = FS_BSDFFS;
    593 
    594 	pp = &lp->d_partitions[RAW_PART];
    595 	pp->p_offset = 0;
    596 	pp->p_size = lp->d_secperunit;
    597 	pp->p_fstype = FS_UNUSED;
    598 
    599 	lp->d_npartitions = RAW_PART+1;
    600 	lp->d_magic = DISKMAGIC;
    601 	lp->d_magic2 = DISKMAGIC;
    602 	lp->d_checksum = dkcksum(lp);
    603 
    604 	memset(dg, 0, sizeof(*dg));
    605 
    606 	dg->dg_secsize = lp->d_secsize;
    607 	dg->dg_secperunit = lp->d_secperunit;
    608 	dg->dg_nsectors = lp->d_nsectors;
    609 	dg->dg_ntracks = lp->d_ntracks = 64;;
    610 	dg->dg_ncylinders = lp->d_ncylinders;
    611 
    612 	disk_set_info(sc->sc_dev, &sc->sc_dkdev, NULL);
    613 }
    614 
    615 /*
    616  * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
    617  * Just allocate some kernel memory and return.
    618  */
    619 static int
    620 md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
    621     struct lwp *l)
    622 {
    623 	vaddr_t addr;
    624 	vsize_t size;
    625 
    626 	mutex_exit(&sc->sc_lock);
    627 
    628 	/* Sanity check the size. */
    629 	size = umd->md_size;
    630 	addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
    631 
    632 	mutex_enter(&sc->sc_lock);
    633 
    634 	if (!addr)
    635 		return ENOMEM;
    636 
    637 	/* If another thread beat us to configure this unit:  fail. */
    638 	if (sc->sc_type != MD_UNCONFIGURED) {
    639 		uvm_km_free(kernel_map, addr, size, UVM_KMF_WIRED);
    640 		return EINVAL;
    641 	}
    642 
    643 	/* This unit is now configured. */
    644 	sc->sc_addr = (void *)addr; 	/* kernel space */
    645 	sc->sc_size = (size_t)size;
    646 	sc->sc_type = MD_KMEM_ALLOCATED;
    647 	md_set_disklabel(sc);
    648 	return 0;
    649 }
    650 
    651 #if MEMORY_DISK_SERVER
    652 
    653 /*
    654  * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
    655  * Set config, then become the I/O server for this unit.
    656  */
    657 static int
    658 md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
    659     struct lwp *l)
    660 {
    661 	vaddr_t end;
    662 	int error;
    663 
    664 	KASSERT(mutex_owned(&sc->sc_lock));
    665 
    666 	/* Sanity check addr, size. */
    667 	end = (vaddr_t) ((char *)umd->md_addr + umd->md_size);
    668 
    669 	if ((end >= VM_MAXUSER_ADDRESS) ||
    670 		(end < ((vaddr_t) umd->md_addr)) )
    671 		return EINVAL;
    672 
    673 	/* This unit is now configured. */
    674 	sc->sc_addr = umd->md_addr; 	/* user space */
    675 	sc->sc_size = umd->md_size;
    676 	sc->sc_type = MD_UMEM_SERVER;
    677 	md_set_disklabel(sc);
    678 
    679 	/* Become the server daemon */
    680 	error = md_server_loop(sc);
    681 
    682 	/* This server is now going away! */
    683 	sc->sc_type = MD_UNCONFIGURED;
    684 	sc->sc_addr = 0;
    685 	sc->sc_size = 0;
    686 
    687 	return (error);
    688 }
    689 
    690 static int
    691 md_server_loop(struct md_softc *sc)
    692 {
    693 	struct buf *bp;
    694 	void *addr;	/* user space address */
    695 	size_t off;	/* offset into "device" */
    696 	size_t xfer;	/* amount to transfer */
    697 	int error;
    698 	bool is_read;
    699 
    700 	KASSERT(mutex_owned(&sc->sc_lock));
    701 
    702 	for (;;) {
    703 		/* Wait for some work to arrive. */
    704 		while ((bp = bufq_get(sc->sc_buflist)) == NULL) {
    705 			error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
    706 			if (error)
    707 				return error;
    708 		}
    709 
    710 		/* Do the transfer to/from user space. */
    711 		mutex_exit(&sc->sc_lock);
    712 		error = 0;
    713 		is_read = ((bp->b_flags & B_READ) == B_READ);
    714 		bp->b_resid = bp->b_bcount;
    715 		off = (bp->b_blkno << DEV_BSHIFT);
    716 		if (off >= sc->sc_size) {
    717 			if (is_read)
    718 				goto done;	/* EOF (not an error) */
    719 			error = EIO;
    720 			goto done;
    721 		}
    722 		xfer = bp->b_resid;
    723 		if (xfer > (sc->sc_size - off))
    724 			xfer = (sc->sc_size - off);
    725 		addr = (char *)sc->sc_addr + off;
    726 		disk_busy(&sc->sc_dkdev);
    727 		if (is_read)
    728 			error = copyin(addr, bp->b_data, xfer);
    729 		else
    730 			error = copyout(bp->b_data, addr, xfer);
    731 		disk_unbusy(&sc->sc_dkdev, (error ? 0 : xfer), is_read);
    732 		if (!error)
    733 			bp->b_resid -= xfer;
    734 
    735 	done:
    736 		if (error) {
    737 			bp->b_error = error;
    738 		}
    739 		biodone(bp);
    740 		mutex_enter(&sc->sc_lock);
    741 	}
    742 }
    743 #endif	/* MEMORY_DISK_SERVER */
    744