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