Home | History | Annotate | Line # | Download | only in dev
md.c revision 1.47
      1 /*	$NetBSD: md.c,v 1.47 2006/11/16 01:32:45 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  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  * 4. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed by
     20  *			Gordon W. Ross and Leo Weppelman.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * This implements a general-purpose memory-disk.
     36  * See md.h for notes on the config types.
     37  *
     38  * Note that this driver provides the same functionality
     39  * as the MFS filesystem hack, but this is better because
     40  * you can use this for any filesystem type you'd like!
     41  *
     42  * Credit for most of the kmem ramdisk code goes to:
     43  *   Leo Weppelman (atari) and Phil Nelson (pc532)
     44  * Credit for the ideas behind the "user space memory" code goes
     45  * to the authors of the MFS implementation.
     46  */
     47 
     48 #include <sys/cdefs.h>
     49 __KERNEL_RCSID(0, "$NetBSD: md.c,v 1.47 2006/11/16 01:32:45 christos Exp $");
     50 
     51 #include "opt_md.h"
     52 
     53 #include <sys/param.h>
     54 #include <sys/kernel.h>
     55 #include <sys/malloc.h>
     56 #include <sys/systm.h>
     57 #include <sys/buf.h>
     58 #include <sys/bufq.h>
     59 #include <sys/device.h>
     60 #include <sys/disk.h>
     61 #include <sys/proc.h>
     62 #include <sys/conf.h>
     63 #include <sys/disklabel.h>
     64 
     65 #include <uvm/uvm_extern.h>
     66 
     67 #include <dev/md.h>
     68 
     69 /*
     70  * By default, include the user-space functionality.
     71  * Use  `options MEMORY_DISK_SERVER=0' to turn it off.
     72  */
     73 #ifndef MEMORY_DISK_SERVER
     74 #define	MEMORY_DISK_SERVER 1
     75 #endif	/* MEMORY_DISK_SERVER */
     76 
     77 /*
     78  * We should use the raw partition for ioctl.
     79  */
     80 #define MD_MAX_UNITS	0x10
     81 #define MD_UNIT(unit)	DISKUNIT(unit)
     82 
     83 /* autoconfig stuff... */
     84 
     85 struct md_softc {
     86 	struct device sc_dev;	/* REQUIRED first entry */
     87 	struct disk sc_dkdev;	/* hook for generic disk handling */
     88 	struct md_conf sc_md;
     89 	struct bufq_state *sc_buflist;
     90 };
     91 /* shorthand for fields in sc_md: */
     92 #define sc_addr sc_md.md_addr
     93 #define sc_size sc_md.md_size
     94 #define sc_type sc_md.md_type
     95 
     96 void	mdattach(int);
     97 
     98 static void	md_attach(struct device *, struct device *, void *);
     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 	mdopen, mdclose, mdstrategy, mdioctl, nodump, mdsize, D_DISK
    110 };
    111 
    112 const struct cdevsw md_cdevsw = {
    113 	mdopen, mdclose, mdread, mdwrite, mdioctl,
    114 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    115 };
    116 
    117 static struct dkdriver mddkdriver = { mdstrategy, NULL };
    118 
    119 static int   ramdisk_ndevs;
    120 static void *ramdisk_devs[MD_MAX_UNITS];
    121 
    122 /*
    123  * This is called if we are configured as a pseudo-device
    124  */
    125 void
    126 mdattach(int n)
    127 {
    128 	struct md_softc *sc;
    129 	int i;
    130 
    131 #ifdef	DIAGNOSTIC
    132 	if (ramdisk_ndevs) {
    133 		aprint_error("ramdisk: multiple attach calls?\n");
    134 		return;
    135 	}
    136 #endif
    137 
    138 	/* XXX:  Are we supposed to provide a default? */
    139 	if (n <= 1)
    140 		n = 1;
    141 	if (n > MD_MAX_UNITS)
    142 		n = MD_MAX_UNITS;
    143 	ramdisk_ndevs = n;
    144 
    145 	/* Attach as if by autoconfig. */
    146 	for (i = 0; i < n; i++) {
    147 
    148 		sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT|M_ZERO);
    149 		if (!sc) {
    150 			aprint_error("ramdisk: malloc for attach failed!\n");
    151 			return;
    152 		}
    153 		ramdisk_devs[i] = sc;
    154 		sc->sc_dev.dv_unit = i;
    155 		snprintf(sc->sc_dev.dv_xname, sizeof(sc->sc_dev.dv_xname),
    156 		    "md%d", i);
    157 		md_attach(NULL, &sc->sc_dev, NULL);
    158 	}
    159 }
    160 
    161 static void
    162 md_attach(struct device *parent, struct device *self,
    163     void *aux)
    164 {
    165 	struct md_softc *sc = (struct md_softc *)self;
    166 
    167 	bufq_alloc(&sc->sc_buflist, "fcfs", 0);
    168 
    169 	/* XXX - Could accept aux info here to set the config. */
    170 #ifdef	MEMORY_DISK_HOOKS
    171 	/*
    172 	 * This external function might setup a pre-loaded disk.
    173 	 * All it would need to do is setup the md_conf struct.
    174 	 * See sys/dev/md_root.c for an example.
    175 	 */
    176 	md_attach_hook(device_unit(&sc->sc_dev), &sc->sc_md);
    177 #endif
    178 
    179 	/*
    180 	 * Initialize and attach the disk structure.
    181 	 */
    182 	sc->sc_dkdev.dk_driver = &mddkdriver;
    183 	sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
    184 	disk_attach(&sc->sc_dkdev);
    185 }
    186 
    187 /*
    188  * operational routines:
    189  * open, close, read, write, strategy,
    190  * ioctl, dump, size
    191  */
    192 
    193 #if MEMORY_DISK_SERVER
    194 static int	md_server_loop(struct md_softc *sc);
    195 static int	md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
    196 		    struct lwp *l);
    197 #endif	/* MEMORY_DISK_SERVER */
    198 static int	md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
    199 		    struct lwp *l);
    200 
    201 static int
    202 mdsize(dev_t dev)
    203 {
    204 	int unit;
    205 	struct md_softc *sc;
    206 
    207 	unit = MD_UNIT(dev);
    208 	if (unit >= ramdisk_ndevs)
    209 		return 0;
    210 	sc = ramdisk_devs[unit];
    211 	if (sc == NULL)
    212 		return 0;
    213 
    214 	if (sc->sc_type == MD_UNCONFIGURED)
    215 		return 0;
    216 
    217 	return (sc->sc_size >> DEV_BSHIFT);
    218 }
    219 
    220 static int
    221 mdopen(dev_t dev, int flag, int fmt, struct lwp *l)
    222 {
    223 	int unit;
    224 	struct md_softc *sc;
    225 
    226 	unit = MD_UNIT(dev);
    227 	if (unit >= ramdisk_ndevs)
    228 		return ENXIO;
    229 	sc = ramdisk_devs[unit];
    230 	if (sc == NULL)
    231 		return ENXIO;
    232 
    233 	/*
    234 	 * The raw partition is used for ioctl to configure.
    235 	 */
    236 	if (DISKPART(dev) == RAW_PART)
    237 		return 0;
    238 
    239 #ifdef	MEMORY_DISK_HOOKS
    240 	/* Call the open hook to allow loading the device. */
    241 	md_open_hook(unit, &sc->sc_md);
    242 #endif
    243 
    244 	/*
    245 	 * This is a normal, "slave" device, so
    246 	 * enforce initialized.
    247 	 */
    248 	if (sc->sc_type == MD_UNCONFIGURED)
    249 		return ENXIO;
    250 
    251 	return 0;
    252 }
    253 
    254 static int
    255 mdclose(dev_t dev, int flag, int fmt, struct lwp *l)
    256 {
    257 	int unit;
    258 
    259 	unit = MD_UNIT(dev);
    260 
    261 	if (unit >= ramdisk_ndevs)
    262 		return ENXIO;
    263 
    264 	return 0;
    265 }
    266 
    267 static int
    268 mdread(dev_t dev, struct uio *uio, int flags)
    269 {
    270 	int unit;
    271 	struct md_softc *sc;
    272 
    273 	unit = MD_UNIT(dev);
    274 
    275 	if (unit >= ramdisk_ndevs)
    276 		return ENXIO;
    277 
    278 	sc = ramdisk_devs[unit];
    279 
    280 	if (sc->sc_type == MD_UNCONFIGURED)
    281 		return ENXIO;
    282 
    283 	return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
    284 }
    285 
    286 static int
    287 mdwrite(dev_t dev, struct uio *uio, int flags)
    288 {
    289 	int unit;
    290 	struct md_softc *sc;
    291 
    292 	unit = MD_UNIT(dev);
    293 
    294 	if (unit >= ramdisk_ndevs)
    295 		return ENXIO;
    296 
    297 	sc = ramdisk_devs[unit];
    298 
    299 	if (sc->sc_type == MD_UNCONFIGURED)
    300 		return ENXIO;
    301 
    302 	return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
    303 }
    304 
    305 /*
    306  * Handle I/O requests, either directly, or
    307  * by passing them to the server process.
    308  */
    309 static void
    310 mdstrategy(struct buf *bp)
    311 {
    312 	int unit;
    313 	struct md_softc	*sc;
    314 	caddr_t	addr;
    315 	size_t off, xfer;
    316 
    317 	unit = MD_UNIT(bp->b_dev);
    318 	sc = ramdisk_devs[unit];
    319 
    320 	if (sc->sc_type == MD_UNCONFIGURED) {
    321 		bp->b_error = ENXIO;
    322 		bp->b_flags |= B_ERROR;
    323 		goto done;
    324 	}
    325 
    326 	switch (sc->sc_type) {
    327 #if MEMORY_DISK_SERVER
    328 	case MD_UMEM_SERVER:
    329 		/* Just add this job to the server's queue. */
    330 		BUFQ_PUT(sc->sc_buflist, bp);
    331 		wakeup((caddr_t)sc);
    332 		/* see md_server_loop() */
    333 		/* no biodone in this case */
    334 		return;
    335 #endif	/* MEMORY_DISK_SERVER */
    336 
    337 	case MD_KMEM_FIXED:
    338 	case MD_KMEM_ALLOCATED:
    339 		/* These are in kernel space.  Access directly. */
    340 		bp->b_resid = bp->b_bcount;
    341 		off = (bp->b_blkno << DEV_BSHIFT);
    342 		if (off >= sc->sc_size) {
    343 			if (bp->b_flags & B_READ)
    344 				break;	/* EOF */
    345 			goto set_eio;
    346 		}
    347 		xfer = bp->b_resid;
    348 		if (xfer > (sc->sc_size - off))
    349 			xfer = (sc->sc_size - off);
    350 		addr = sc->sc_addr + off;
    351 		if (bp->b_flags & B_READ)
    352 			memcpy(bp->b_data, addr, xfer);
    353 		else
    354 			memcpy(addr, bp->b_data, xfer);
    355 		bp->b_resid -= xfer;
    356 		break;
    357 
    358 	default:
    359 		bp->b_resid = bp->b_bcount;
    360 	set_eio:
    361 		bp->b_error = EIO;
    362 		bp->b_flags |= B_ERROR;
    363 		break;
    364 	}
    365  done:
    366 	biodone(bp);
    367 }
    368 
    369 static int
    370 mdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    371 {
    372 	int unit;
    373 	struct md_softc *sc;
    374 	struct md_conf *umd;
    375 
    376 	unit = MD_UNIT(dev);
    377 	sc = ramdisk_devs[unit];
    378 
    379 	/* If this is not the raw partition, punt! */
    380 	if (DISKPART(dev) != RAW_PART)
    381 		return ENOTTY;
    382 
    383 	umd = (struct md_conf *)data;
    384 	switch (cmd) {
    385 	case MD_GETCONF:
    386 		*umd = sc->sc_md;
    387 		return 0;
    388 
    389 	case MD_SETCONF:
    390 		/* Can only set it once. */
    391 		if (sc->sc_type != MD_UNCONFIGURED)
    392 			break;
    393 		switch (umd->md_type) {
    394 		case MD_KMEM_ALLOCATED:
    395 			return md_ioctl_kalloc(sc, umd, l);
    396 #if MEMORY_DISK_SERVER
    397 		case MD_UMEM_SERVER:
    398 			return md_ioctl_server(sc, umd, l);
    399 #endif	/* MEMORY_DISK_SERVER */
    400 		default:
    401 			break;
    402 		}
    403 		break;
    404 	}
    405 	return EINVAL;
    406 }
    407 
    408 /*
    409  * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
    410  * Just allocate some kernel memory and return.
    411  */
    412 static int
    413 md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
    414     struct lwp *l)
    415 {
    416 	vaddr_t addr;
    417 	vsize_t size;
    418 
    419 	/* Sanity check the size. */
    420 	size = umd->md_size;
    421 	addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
    422 	if (!addr)
    423 		return ENOMEM;
    424 
    425 	/* This unit is now configured. */
    426 	sc->sc_addr = (caddr_t)addr; 	/* kernel space */
    427 	sc->sc_size = (size_t)size;
    428 	sc->sc_type = MD_KMEM_ALLOCATED;
    429 	return 0;
    430 }
    431 
    432 #if MEMORY_DISK_SERVER
    433 
    434 /*
    435  * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
    436  * Set config, then become the I/O server for this unit.
    437  */
    438 static int
    439 md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
    440     struct lwp *l)
    441 {
    442 	vaddr_t end;
    443 	int error;
    444 
    445 	/* Sanity check addr, size. */
    446 	end = (vaddr_t) (umd->md_addr + umd->md_size);
    447 
    448 	if ((end >= VM_MAXUSER_ADDRESS) ||
    449 		(end < ((vaddr_t) umd->md_addr)) )
    450 		return EINVAL;
    451 
    452 	/* This unit is now configured. */
    453 	sc->sc_addr = umd->md_addr; 	/* user space */
    454 	sc->sc_size = umd->md_size;
    455 	sc->sc_type = MD_UMEM_SERVER;
    456 
    457 	/* Become the server daemon */
    458 	error = md_server_loop(sc);
    459 
    460 	/* This server is now going away! */
    461 	sc->sc_type = MD_UNCONFIGURED;
    462 	sc->sc_addr = 0;
    463 	sc->sc_size = 0;
    464 
    465 	return (error);
    466 }
    467 
    468 static int md_sleep_pri = PWAIT | PCATCH;
    469 
    470 static int
    471 md_server_loop(struct md_softc *sc)
    472 {
    473 	struct buf *bp;
    474 	caddr_t addr;	/* user space address */
    475 	size_t off;	/* offset into "device" */
    476 	size_t xfer;	/* amount to transfer */
    477 	int error;
    478 
    479 	for (;;) {
    480 		/* Wait for some work to arrive. */
    481 		while ((bp = BUFQ_GET(sc->sc_buflist)) == NULL) {
    482 			error = tsleep((caddr_t)sc, md_sleep_pri, "md_idle", 0);
    483 			if (error)
    484 				return error;
    485 		}
    486 
    487 		/* Do the transfer to/from user space. */
    488 		error = 0;
    489 		bp->b_resid = bp->b_bcount;
    490 		off = (bp->b_blkno << DEV_BSHIFT);
    491 		if (off >= sc->sc_size) {
    492 			if (bp->b_flags & B_READ)
    493 				goto done;	/* EOF (not an error) */
    494 			error = EIO;
    495 			goto done;
    496 		}
    497 		xfer = bp->b_resid;
    498 		if (xfer > (sc->sc_size - off))
    499 			xfer = (sc->sc_size - off);
    500 		addr = sc->sc_addr + off;
    501 		if (bp->b_flags & B_READ)
    502 			error = copyin(addr, bp->b_data, xfer);
    503 		else
    504 			error = copyout(bp->b_data, addr, xfer);
    505 		if (!error)
    506 			bp->b_resid -= xfer;
    507 
    508 	done:
    509 		if (error) {
    510 			bp->b_error = error;
    511 			bp->b_flags |= B_ERROR;
    512 		}
    513 		biodone(bp);
    514 	}
    515 }
    516 #endif	/* MEMORY_DISK_SERVER */
    517