Home | History | Annotate | Line # | Download | only in dev
md.c revision 1.48.2.2
      1 /*	$NetBSD: md.c,v 1.48.2.2 2007/08/19 19:24:21 ad 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.48.2.2 2007/08/19 19:24:21 ad 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 	void *	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 		goto done;
    323 	}
    324 
    325 	switch (sc->sc_type) {
    326 #if MEMORY_DISK_SERVER
    327 	case MD_UMEM_SERVER:
    328 		/* Just add this job to the server's queue. */
    329 		BUFQ_PUT(sc->sc_buflist, bp);
    330 		wakeup((void *)sc);
    331 		/* see md_server_loop() */
    332 		/* no biodone in this case */
    333 		return;
    334 #endif	/* MEMORY_DISK_SERVER */
    335 
    336 	case MD_KMEM_FIXED:
    337 	case MD_KMEM_ALLOCATED:
    338 		/* These are in kernel space.  Access directly. */
    339 		bp->b_resid = bp->b_bcount;
    340 		off = (bp->b_blkno << DEV_BSHIFT);
    341 		if (off >= sc->sc_size) {
    342 			if (bp->b_flags & B_READ)
    343 				break;	/* EOF */
    344 			goto set_eio;
    345 		}
    346 		xfer = bp->b_resid;
    347 		if (xfer > (sc->sc_size - off))
    348 			xfer = (sc->sc_size - off);
    349 		addr = (char *)sc->sc_addr + off;
    350 		if (bp->b_flags & B_READ)
    351 			memcpy(bp->b_data, addr, xfer);
    352 		else
    353 			memcpy(addr, bp->b_data, xfer);
    354 		bp->b_resid -= xfer;
    355 		break;
    356 
    357 	default:
    358 		bp->b_resid = bp->b_bcount;
    359 	set_eio:
    360 		bp->b_error = EIO;
    361 		break;
    362 	}
    363  done:
    364 	biodone(bp);
    365 }
    366 
    367 static int
    368 mdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    369 {
    370 	int unit;
    371 	struct md_softc *sc;
    372 	struct md_conf *umd;
    373 
    374 	unit = MD_UNIT(dev);
    375 	sc = ramdisk_devs[unit];
    376 
    377 	/* If this is not the raw partition, punt! */
    378 	if (DISKPART(dev) != RAW_PART)
    379 		return ENOTTY;
    380 
    381 	umd = (struct md_conf *)data;
    382 	switch (cmd) {
    383 	case MD_GETCONF:
    384 		*umd = sc->sc_md;
    385 		return 0;
    386 
    387 	case MD_SETCONF:
    388 		/* Can only set it once. */
    389 		if (sc->sc_type != MD_UNCONFIGURED)
    390 			break;
    391 		switch (umd->md_type) {
    392 		case MD_KMEM_ALLOCATED:
    393 			return md_ioctl_kalloc(sc, umd, l);
    394 #if MEMORY_DISK_SERVER
    395 		case MD_UMEM_SERVER:
    396 			return md_ioctl_server(sc, umd, l);
    397 #endif	/* MEMORY_DISK_SERVER */
    398 		default:
    399 			break;
    400 		}
    401 		break;
    402 	}
    403 	return EINVAL;
    404 }
    405 
    406 /*
    407  * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
    408  * Just allocate some kernel memory and return.
    409  */
    410 static int
    411 md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
    412     struct lwp *l)
    413 {
    414 	vaddr_t addr;
    415 	vsize_t size;
    416 
    417 	/* Sanity check the size. */
    418 	size = umd->md_size;
    419 	addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
    420 	if (!addr)
    421 		return ENOMEM;
    422 
    423 	/* This unit is now configured. */
    424 	sc->sc_addr = (void *)addr; 	/* kernel space */
    425 	sc->sc_size = (size_t)size;
    426 	sc->sc_type = MD_KMEM_ALLOCATED;
    427 	return 0;
    428 }
    429 
    430 #if MEMORY_DISK_SERVER
    431 
    432 /*
    433  * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
    434  * Set config, then become the I/O server for this unit.
    435  */
    436 static int
    437 md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
    438     struct lwp *l)
    439 {
    440 	vaddr_t end;
    441 	int error;
    442 
    443 	/* Sanity check addr, size. */
    444 	end = (vaddr_t) ((char *)umd->md_addr + umd->md_size);
    445 
    446 	if ((end >= VM_MAXUSER_ADDRESS) ||
    447 		(end < ((vaddr_t) umd->md_addr)) )
    448 		return EINVAL;
    449 
    450 	/* This unit is now configured. */
    451 	sc->sc_addr = umd->md_addr; 	/* user space */
    452 	sc->sc_size = umd->md_size;
    453 	sc->sc_type = MD_UMEM_SERVER;
    454 
    455 	/* Become the server daemon */
    456 	error = md_server_loop(sc);
    457 
    458 	/* This server is now going away! */
    459 	sc->sc_type = MD_UNCONFIGURED;
    460 	sc->sc_addr = 0;
    461 	sc->sc_size = 0;
    462 
    463 	return (error);
    464 }
    465 
    466 static int md_sleep_pri = PWAIT | PCATCH;
    467 
    468 static int
    469 md_server_loop(struct md_softc *sc)
    470 {
    471 	struct buf *bp;
    472 	void *addr;	/* user space address */
    473 	size_t off;	/* offset into "device" */
    474 	size_t xfer;	/* amount to transfer */
    475 	int error;
    476 
    477 	for (;;) {
    478 		/* Wait for some work to arrive. */
    479 		while ((bp = BUFQ_GET(sc->sc_buflist)) == NULL) {
    480 			error = tsleep((void *)sc, md_sleep_pri, "md_idle", 0);
    481 			if (error)
    482 				return error;
    483 		}
    484 
    485 		/* Do the transfer to/from user space. */
    486 		error = 0;
    487 		bp->b_resid = bp->b_bcount;
    488 		off = (bp->b_blkno << DEV_BSHIFT);
    489 		if (off >= sc->sc_size) {
    490 			if (bp->b_flags & B_READ)
    491 				goto done;	/* EOF (not an error) */
    492 			error = EIO;
    493 			goto done;
    494 		}
    495 		xfer = bp->b_resid;
    496 		if (xfer > (sc->sc_size - off))
    497 			xfer = (sc->sc_size - off);
    498 		addr = (char *)sc->sc_addr + off;
    499 		if (bp->b_flags & B_READ)
    500 			error = copyin(addr, bp->b_data, xfer);
    501 		else
    502 			error = copyout(bp->b_data, addr, xfer);
    503 		if (!error)
    504 			bp->b_resid -= xfer;
    505 
    506 	done:
    507 		bp->b_error = error;
    508 		biodone(bp);
    509 	}
    510 }
    511 #endif	/* MEMORY_DISK_SERVER */
    512