Home | History | Annotate | Line # | Download | only in dev
md.c revision 1.39
      1 /*	$NetBSD: md.c,v 1.39 2004/10/28 07:07:39 yamt 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.39 2004/10/28 07:07:39 yamt 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 };
    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, void *aux)
    163 {
    164 	struct md_softc *sc = (struct md_softc *)self;
    165 
    166 	bufq_alloc(&sc->sc_buflist, BUFQ_FCFS);
    167 
    168 	/* XXX - Could accept aux info here to set the config. */
    169 #ifdef	MEMORY_DISK_HOOKS
    170 	/*
    171 	 * This external function might setup a pre-loaded disk.
    172 	 * All it would need to do is setup the md_conf struct.
    173 	 * See sys/dev/md_root.c for an example.
    174 	 */
    175 	md_attach_hook(sc->sc_dev.dv_unit, &sc->sc_md);
    176 #endif
    177 
    178 	/*
    179 	 * Initialize and attach the disk structure.
    180 	 */
    181 	sc->sc_dkdev.dk_driver = &mddkdriver;
    182 	sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
    183 	disk_attach(&sc->sc_dkdev);
    184 }
    185 
    186 /*
    187  * operational routines:
    188  * open, close, read, write, strategy,
    189  * ioctl, dump, size
    190  */
    191 
    192 #if MEMORY_DISK_SERVER
    193 static int	md_server_loop(struct md_softc *sc);
    194 static int	md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
    195 		    struct proc *proc);
    196 #endif	/* MEMORY_DISK_SERVER */
    197 static int	md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
    198 		    struct proc *proc);
    199 
    200 static int
    201 mdsize(dev_t dev)
    202 {
    203 	int unit;
    204 	struct md_softc *sc;
    205 
    206 	unit = MD_UNIT(dev);
    207 	if (unit >= ramdisk_ndevs)
    208 		return 0;
    209 	sc = ramdisk_devs[unit];
    210 	if (sc == NULL)
    211 		return 0;
    212 
    213 	if (sc->sc_type == MD_UNCONFIGURED)
    214 		return 0;
    215 
    216 	return (sc->sc_size >> DEV_BSHIFT);
    217 }
    218 
    219 static int
    220 mdopen(dev_t dev, int flag, int fmt, struct proc *proc)
    221 {
    222 	int unit;
    223 	struct md_softc *sc;
    224 
    225 	unit = MD_UNIT(dev);
    226 	if (unit >= ramdisk_ndevs)
    227 		return ENXIO;
    228 	sc = ramdisk_devs[unit];
    229 	if (sc == NULL)
    230 		return ENXIO;
    231 
    232 	/*
    233 	 * The raw partition is used for ioctl to configure.
    234 	 */
    235 	if (DISKPART(dev) == RAW_PART)
    236 		return 0;
    237 
    238 #ifdef	MEMORY_DISK_HOOKS
    239 	/* Call the open hook to allow loading the device. */
    240 	md_open_hook(unit, &sc->sc_md);
    241 #endif
    242 
    243 	/*
    244 	 * This is a normal, "slave" device, so
    245 	 * enforce initialized.
    246 	 */
    247 	if (sc->sc_type == MD_UNCONFIGURED)
    248 		return ENXIO;
    249 
    250 	return 0;
    251 }
    252 
    253 static int
    254 mdclose(dev_t dev, int flag, int fmt, struct proc *proc)
    255 {
    256 	int unit;
    257 
    258 	unit = MD_UNIT(dev);
    259 
    260 	if (unit >= ramdisk_ndevs)
    261 		return ENXIO;
    262 
    263 	return 0;
    264 }
    265 
    266 static int
    267 mdread(dev_t dev, struct uio *uio, int flags)
    268 {
    269 	int unit;
    270 	struct md_softc *sc;
    271 
    272 	unit = MD_UNIT(dev);
    273 
    274 	if (unit >= ramdisk_ndevs)
    275 		return ENXIO;
    276 
    277 	sc = ramdisk_devs[unit];
    278 
    279 	if (sc->sc_type == MD_UNCONFIGURED)
    280 		return ENXIO;
    281 
    282 	return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
    283 }
    284 
    285 static int
    286 mdwrite(dev_t dev, struct uio *uio, int flags)
    287 {
    288 	int unit;
    289 	struct md_softc *sc;
    290 
    291 	unit = MD_UNIT(dev);
    292 
    293 	if (unit >= ramdisk_ndevs)
    294 		return ENXIO;
    295 
    296 	sc = ramdisk_devs[unit];
    297 
    298 	if (sc->sc_type == MD_UNCONFIGURED)
    299 		return ENXIO;
    300 
    301 	return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
    302 }
    303 
    304 /*
    305  * Handle I/O requests, either directly, or
    306  * by passing them to the server process.
    307  */
    308 static void
    309 mdstrategy(struct buf *bp)
    310 {
    311 	int unit;
    312 	struct md_softc	*sc;
    313 	caddr_t	addr;
    314 	size_t off, xfer;
    315 
    316 	unit = MD_UNIT(bp->b_dev);
    317 	sc = ramdisk_devs[unit];
    318 
    319 	if (sc->sc_type == MD_UNCONFIGURED) {
    320 		bp->b_error = ENXIO;
    321 		bp->b_flags |= B_ERROR;
    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((caddr_t)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 = 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 		bp->b_flags |= B_ERROR;
    362 		break;
    363 	}
    364  done:
    365 	biodone(bp);
    366 }
    367 
    368 static int
    369 mdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *proc)
    370 {
    371 	int unit;
    372 	struct md_softc *sc;
    373 	struct md_conf *umd;
    374 
    375 	unit = MD_UNIT(dev);
    376 	sc = ramdisk_devs[unit];
    377 
    378 	/* If this is not the raw partition, punt! */
    379 	if (DISKPART(dev) != RAW_PART)
    380 		return ENOTTY;
    381 
    382 	umd = (struct md_conf *)data;
    383 	switch (cmd) {
    384 	case MD_GETCONF:
    385 		*umd = sc->sc_md;
    386 		return 0;
    387 
    388 	case MD_SETCONF:
    389 		/* Can only set it once. */
    390 		if (sc->sc_type != MD_UNCONFIGURED)
    391 			break;
    392 		switch (umd->md_type) {
    393 		case MD_KMEM_ALLOCATED:
    394 			return md_ioctl_kalloc(sc, umd, proc);
    395 #if MEMORY_DISK_SERVER
    396 		case MD_UMEM_SERVER:
    397 			return md_ioctl_server(sc, umd, proc);
    398 #endif	/* MEMORY_DISK_SERVER */
    399 		default:
    400 			break;
    401 		}
    402 		break;
    403 	}
    404 	return EINVAL;
    405 }
    406 
    407 /*
    408  * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
    409  * Just allocate some kernel memory and return.
    410  */
    411 static int
    412 md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd, struct proc *proc)
    413 {
    414 	vaddr_t addr;
    415 	vsize_t size;
    416 
    417 	/* Sanity check the size. */
    418 	size = umd->md_size;
    419 	addr = uvm_km_zalloc(kernel_map, size);
    420 	if (!addr)
    421 		return ENOMEM;
    422 
    423 	/* This unit is now configured. */
    424 	sc->sc_addr = (caddr_t)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, struct proc *proc)
    438 {
    439 	vaddr_t end;
    440 	int error;
    441 
    442 	/* Sanity check addr, size. */
    443 	end = (vaddr_t) (umd->md_addr + umd->md_size);
    444 
    445 	if ((end >= VM_MAXUSER_ADDRESS) ||
    446 		(end < ((vaddr_t) umd->md_addr)) )
    447 		return EINVAL;
    448 
    449 	/* This unit is now configured. */
    450 	sc->sc_addr = umd->md_addr; 	/* user space */
    451 	sc->sc_size = umd->md_size;
    452 	sc->sc_type = MD_UMEM_SERVER;
    453 
    454 	/* Become the server daemon */
    455 	error = md_server_loop(sc);
    456 
    457 	/* This server is now going away! */
    458 	sc->sc_type = MD_UNCONFIGURED;
    459 	sc->sc_addr = 0;
    460 	sc->sc_size = 0;
    461 
    462 	return (error);
    463 }
    464 
    465 static int md_sleep_pri = PWAIT | PCATCH;
    466 
    467 static int
    468 md_server_loop(struct md_softc *sc)
    469 {
    470 	struct buf *bp;
    471 	caddr_t addr;	/* user space address */
    472 	size_t off;	/* offset into "device" */
    473 	size_t xfer;	/* amount to transfer */
    474 	int error;
    475 
    476 	for (;;) {
    477 		/* Wait for some work to arrive. */
    478 		while ((bp = BUFQ_GET(&sc->sc_buflist)) == NULL) {
    479 			error = tsleep((caddr_t)sc, md_sleep_pri, "md_idle", 0);
    480 			if (error)
    481 				return error;
    482 		}
    483 
    484 		/* Do the transfer to/from user space. */
    485 		error = 0;
    486 		bp->b_resid = bp->b_bcount;
    487 		off = (bp->b_blkno << DEV_BSHIFT);
    488 		if (off >= sc->sc_size) {
    489 			if (bp->b_flags & B_READ)
    490 				goto done;	/* EOF (not an error) */
    491 			error = EIO;
    492 			goto done;
    493 		}
    494 		xfer = bp->b_resid;
    495 		if (xfer > (sc->sc_size - off))
    496 			xfer = (sc->sc_size - off);
    497 		addr = sc->sc_addr + off;
    498 		if (bp->b_flags & B_READ)
    499 			error = copyin(addr, bp->b_data, xfer);
    500 		else
    501 			error = copyout(bp->b_data, addr, xfer);
    502 		if (!error)
    503 			bp->b_resid -= xfer;
    504 
    505 	done:
    506 		if (error) {
    507 			bp->b_error = error;
    508 			bp->b_flags |= B_ERROR;
    509 		}
    510 		biodone(bp);
    511 	}
    512 }
    513 #endif	/* MEMORY_DISK_SERVER */
    514