Home | History | Annotate | Line # | Download | only in dev
md.c revision 1.23
      1 /*	$NetBSD: md.c,v 1.23 2000/06/26 14:21:10 mrg 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 "opt_md.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/kernel.h>
     52 #include <sys/malloc.h>
     53 #include <sys/systm.h>
     54 #include <sys/buf.h>
     55 #include <sys/device.h>
     56 #include <sys/disk.h>
     57 #include <sys/proc.h>
     58 #include <sys/conf.h>
     59 #include <sys/disklabel.h>
     60 
     61 #include <vm/vm.h>
     62 
     63 #include <uvm/uvm_extern.h>
     64 
     65 #include <dev/md.h>
     66 
     67 /*
     68  * By default, include the user-space functionality.
     69  * Use  `options MEMORY_DISK_SERVER=0' to turn it off.
     70  */
     71 #ifndef MEMORY_DISK_SERVER
     72 #define	MEMORY_DISK_SERVER 1
     73 #endif
     74 
     75 /*
     76  * We should use the raw partition for ioctl.
     77  */
     78 #define MD_MAX_UNITS	0x10
     79 #define MD_UNIT(unit)	DISKUNIT(unit)
     80 
     81 /* autoconfig stuff... */
     82 
     83 struct md_softc {
     84 	struct device sc_dev;	/* REQUIRED first entry */
     85 	struct disk sc_dkdev;	/* hook for generic disk handling */
     86 	struct md_conf sc_md;
     87 	struct buf_queue sc_buflist;
     88 };
     89 /* shorthand for fields in sc_md: */
     90 #define sc_addr sc_md.md_addr
     91 #define sc_size sc_md.md_size
     92 #define sc_type sc_md.md_type
     93 
     94 void mdattach __P((int));
     95 static void md_attach __P((struct device *, struct device *, void *));
     96 
     97 void mdstrategy __P((struct buf *bp));
     98 struct dkdriver mddkdriver = { mdstrategy };
     99 
    100 static int   ramdisk_ndevs;
    101 static void *ramdisk_devs[MD_MAX_UNITS];
    102 
    103 /*
    104  * This is called if we are configured as a pseudo-device
    105  */
    106 void
    107 mdattach(n)
    108 	int n;
    109 {
    110 	struct md_softc *sc;
    111 	int i;
    112 
    113 #ifdef	DIAGNOSTIC
    114 	if (ramdisk_ndevs) {
    115 		printf("ramdisk: multiple attach calls?\n");
    116 		return;
    117 	}
    118 #endif
    119 
    120 	/* XXX:  Are we supposed to provide a default? */
    121 	if (n <= 1)
    122 		n = 1;
    123 	if (n > MD_MAX_UNITS)
    124 		n = MD_MAX_UNITS;
    125 	ramdisk_ndevs = n;
    126 
    127 	/* Attach as if by autoconfig. */
    128 	for (i = 0; i < n; i++) {
    129 
    130 		sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
    131 		if (!sc) {
    132 			printf("ramdisk: malloc for attach failed!\n");
    133 			return;
    134 		}
    135 		bzero((caddr_t)sc, sizeof(*sc));
    136 		ramdisk_devs[i] = sc;
    137 		sc->sc_dev.dv_unit = i;
    138 		sprintf(sc->sc_dev.dv_xname, "md%d", i);
    139 		md_attach(NULL, &sc->sc_dev, NULL);
    140 	}
    141 }
    142 
    143 static void
    144 md_attach(parent, self, aux)
    145 	struct device	*parent, *self;
    146 	void		*aux;
    147 {
    148 	struct md_softc *sc = (struct md_softc *)self;
    149 
    150 	BUFQ_INIT(&sc->sc_buflist);
    151 
    152 	/* XXX - Could accept aux info here to set the config. */
    153 #ifdef	MEMORY_DISK_HOOKS
    154 	/*
    155 	 * This external function might setup a pre-loaded disk.
    156 	 * All it would need to do is setup the md_conf struct.
    157 	 * See sys/arch/sun3/dev/md_root.c for an example.
    158 	 */
    159 	md_attach_hook(sc->sc_dev.dv_unit, &sc->sc_md);
    160 #endif
    161 
    162 	/*
    163 	 * Initialize and attach the disk structure.
    164 	 */
    165 	sc->sc_dkdev.dk_driver = &mddkdriver;
    166 	sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
    167 	disk_attach(&sc->sc_dkdev);
    168 }
    169 
    170 /*
    171  * operational routines:
    172  * open, close, read, write, strategy,
    173  * ioctl, dump, size
    174  */
    175 
    176 #if MEMORY_DISK_SERVER
    177 static int md_server_loop __P((struct md_softc *sc));
    178 static int md_ioctl_server __P((struct md_softc *sc,
    179 		struct md_conf *umd, struct proc *proc));
    180 #endif
    181 static int md_ioctl_kalloc __P((struct md_softc *sc,
    182 		struct md_conf *umd, struct proc *proc));
    183 
    184 dev_type_open(mdopen);
    185 dev_type_close(mdclose);
    186 dev_type_read(mdread);
    187 dev_type_write(mdwrite);
    188 dev_type_ioctl(mdioctl);
    189 dev_type_size(mdsize);
    190 dev_type_dump(mddump);
    191 
    192 int
    193 mddump(dev, blkno, va, size)
    194 	dev_t dev;
    195 	daddr_t blkno;
    196 	caddr_t va;
    197 	size_t size;
    198 {
    199 	return ENODEV;
    200 }
    201 
    202 int
    203 mdsize(dev_t dev)
    204 {
    205 	int unit;
    206 	struct md_softc *sc;
    207 
    208 	unit = MD_UNIT(dev);
    209 	if (unit >= ramdisk_ndevs)
    210 		return 0;
    211 	sc = ramdisk_devs[unit];
    212 	if (sc == NULL)
    213 		return 0;
    214 
    215 	if (sc->sc_type == MD_UNCONFIGURED)
    216 		return 0;
    217 
    218 	return (sc->sc_size >> DEV_BSHIFT);
    219 }
    220 
    221 int
    222 mdopen(dev, flag, fmt, proc)
    223 	dev_t dev;
    224 	int flag, fmt;
    225 	struct proc *proc;
    226 {
    227 	int unit;
    228 	struct md_softc *sc;
    229 
    230 	unit = MD_UNIT(dev);
    231 	if (unit >= ramdisk_ndevs)
    232 		return ENXIO;
    233 	sc = ramdisk_devs[unit];
    234 	if (sc == NULL)
    235 		return ENXIO;
    236 
    237 	/*
    238 	 * The raw partition is used for ioctl to configure.
    239 	 */
    240 	if (DISKPART(dev) == RAW_PART)
    241 		return 0;
    242 
    243 #ifdef	MEMORY_DISK_HOOKS
    244 	/* Call the open hook to allow loading the device. */
    245 	md_open_hook(unit, &sc->sc_md);
    246 #endif
    247 
    248 	/*
    249 	 * This is a normal, "slave" device, so
    250 	 * enforce initialized.
    251 	 */
    252 	if (sc->sc_type == MD_UNCONFIGURED)
    253 		return ENXIO;
    254 
    255 	return 0;
    256 }
    257 
    258 int
    259 mdclose(dev, flag, fmt, proc)
    260 	dev_t dev;
    261 	int flag, fmt;
    262 	struct proc *proc;
    263 {
    264 	int unit;
    265 
    266 	unit = MD_UNIT(dev);
    267 
    268 	if (unit >= ramdisk_ndevs)
    269 		return ENXIO;
    270 
    271 	return 0;
    272 }
    273 
    274 int
    275 mdread(dev, uio, flags)
    276 	dev_t dev;
    277 	struct uio *uio;
    278 	int flags;
    279 {
    280 	int unit;
    281 	struct md_softc *sc;
    282 
    283 	unit = MD_UNIT(dev);
    284 
    285 	if (unit >= ramdisk_ndevs)
    286 		return ENXIO;
    287 
    288 	sc = ramdisk_devs[unit];
    289 
    290 	if (sc->sc_type == MD_UNCONFIGURED)
    291 		return ENXIO;
    292 
    293 	return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
    294 }
    295 
    296 int
    297 mdwrite(dev, uio, flags)
    298 	dev_t dev;
    299 	struct uio *uio;
    300 	int flags;
    301 {
    302 	int unit;
    303 	struct md_softc *sc;
    304 
    305 	unit = MD_UNIT(dev);
    306 
    307 	if (unit >= ramdisk_ndevs)
    308 		return ENXIO;
    309 
    310 	sc = ramdisk_devs[unit];
    311 
    312 	if (sc->sc_type == MD_UNCONFIGURED)
    313 		return ENXIO;
    314 
    315 	return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
    316 }
    317 
    318 /*
    319  * Handle I/O requests, either directly, or
    320  * by passing them to the server process.
    321  */
    322 void
    323 mdstrategy(bp)
    324 	struct buf *bp;
    325 {
    326 	int unit;
    327 	struct md_softc	*sc;
    328 	caddr_t	addr;
    329 	size_t off, xfer;
    330 
    331 	unit = MD_UNIT(bp->b_dev);
    332 	sc = ramdisk_devs[unit];
    333 
    334 	if (sc->sc_type == MD_UNCONFIGURED) {
    335 		bp->b_error = ENXIO;
    336 		bp->b_flags |= B_ERROR;
    337 		goto done;
    338 	}
    339 
    340 	switch (sc->sc_type) {
    341 #if MEMORY_DISK_SERVER
    342 	case MD_UMEM_SERVER:
    343 		/* Just add this job to the server's queue. */
    344 		BUFQ_INSERT_TAIL(&sc->sc_buflist, bp);
    345 		if (BUFQ_FIRST(&sc->sc_buflist) == bp) {
    346 			/* server queue was empty. */
    347 			wakeup((caddr_t)sc);
    348 			/* see md_server_loop() */
    349 		}
    350 		/* no biodone in this case */
    351 		return;
    352 #endif	/* MEMORY_DISK_SERVER */
    353 
    354 	case MD_KMEM_FIXED:
    355 	case MD_KMEM_ALLOCATED:
    356 		/* These are in kernel space.  Access directly. */
    357 		bp->b_resid = bp->b_bcount;
    358 		off = (bp->b_blkno << DEV_BSHIFT);
    359 		if (off >= sc->sc_size) {
    360 			if (bp->b_flags & B_READ)
    361 				break;	/* EOF */
    362 			goto set_eio;
    363 		}
    364 		xfer = bp->b_resid;
    365 		if (xfer > (sc->sc_size - off))
    366 			xfer = (sc->sc_size - off);
    367 		addr = sc->sc_addr + off;
    368 		if (bp->b_flags & B_READ)
    369 			bcopy(addr, bp->b_data, xfer);
    370 		else
    371 			bcopy(bp->b_data, addr, xfer);
    372 		bp->b_resid -= xfer;
    373 		break;
    374 
    375 	default:
    376 		bp->b_resid = bp->b_bcount;
    377 	set_eio:
    378 		bp->b_error = EIO;
    379 		bp->b_flags |= B_ERROR;
    380 		break;
    381 	}
    382  done:
    383 	biodone(bp);
    384 }
    385 
    386 int
    387 mdioctl(dev, cmd, data, flag, proc)
    388 	dev_t dev;
    389 	u_long cmd;
    390 	int flag;
    391 	caddr_t data;
    392 	struct proc *proc;
    393 {
    394 	int unit;
    395 	struct md_softc *sc;
    396 	struct md_conf *umd;
    397 
    398 	unit = MD_UNIT(dev);
    399 	sc = ramdisk_devs[unit];
    400 
    401 	/* If this is not the raw partition, punt! */
    402 	if (DISKPART(dev) != RAW_PART)
    403 		return ENOTTY;
    404 
    405 	umd = (struct md_conf *)data;
    406 	switch (cmd) {
    407 	case MD_GETCONF:
    408 		*umd = sc->sc_md;
    409 		return 0;
    410 
    411 	case MD_SETCONF:
    412 		/* Can only set it once. */
    413 		if (sc->sc_type != MD_UNCONFIGURED)
    414 			break;
    415 		switch (umd->md_type) {
    416 		case MD_KMEM_ALLOCATED:
    417 			return md_ioctl_kalloc(sc, umd, proc);
    418 #if MEMORY_DISK_SERVER
    419 		case MD_UMEM_SERVER:
    420 			return md_ioctl_server(sc, umd, proc);
    421 #endif
    422 		default:
    423 			break;
    424 		}
    425 		break;
    426 	}
    427 	return EINVAL;
    428 }
    429 
    430 /*
    431  * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
    432  * Just allocate some kernel memory and return.
    433  */
    434 static int
    435 md_ioctl_kalloc(sc, umd, proc)
    436 	struct md_softc *sc;
    437 	struct md_conf *umd;
    438 	struct proc *proc;
    439 {
    440 	vaddr_t addr;
    441 	vsize_t size;
    442 
    443 	/* Sanity check the size. */
    444 	size = umd->md_size;
    445 	addr = uvm_km_zalloc(kernel_map, size);
    446 	if (!addr)
    447 		return ENOMEM;
    448 
    449 	/* This unit is now configured. */
    450 	sc->sc_addr = (caddr_t)addr; 	/* kernel space */
    451 	sc->sc_size = (size_t)size;
    452 	sc->sc_type = MD_KMEM_ALLOCATED;
    453 	return 0;
    454 }
    455 
    456 #if MEMORY_DISK_SERVER
    457 
    458 /*
    459  * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
    460  * Set config, then become the I/O server for this unit.
    461  */
    462 static int
    463 md_ioctl_server(sc, umd, proc)
    464 	struct md_softc *sc;
    465 	struct md_conf *umd;
    466 	struct proc *proc;
    467 {
    468 	vaddr_t end;
    469 	int error;
    470 
    471 	/* Sanity check addr, size. */
    472 	end = (vaddr_t) (umd->md_addr + umd->md_size);
    473 
    474 	if ((end >= VM_MAXUSER_ADDRESS) ||
    475 		(end < ((vaddr_t) umd->md_addr)) )
    476 		return EINVAL;
    477 
    478 	/* This unit is now configured. */
    479 	sc->sc_addr = umd->md_addr; 	/* user space */
    480 	sc->sc_size = umd->md_size;
    481 	sc->sc_type = MD_UMEM_SERVER;
    482 
    483 	/* Become the server daemon */
    484 	error = md_server_loop(sc);
    485 
    486 	/* This server is now going away! */
    487 	sc->sc_type = MD_UNCONFIGURED;
    488 	sc->sc_addr = 0;
    489 	sc->sc_size = 0;
    490 
    491 	return (error);
    492 }
    493 
    494 int md_sleep_pri = PWAIT | PCATCH;
    495 
    496 static int
    497 md_server_loop(sc)
    498 	struct md_softc *sc;
    499 {
    500 	struct buf *bp;
    501 	caddr_t addr;	/* user space address */
    502 	size_t off;	/* offset into "device" */
    503 	size_t xfer;	/* amount to transfer */
    504 	int error;
    505 
    506 	for (;;) {
    507 		/* Wait for some work to arrive. */
    508 		while ((bp = BUFQ_FIRST(&sc->sc_buflist)) == NULL) {
    509 			error = tsleep((caddr_t)sc, md_sleep_pri, "md_idle", 0);
    510 			if (error)
    511 				return error;
    512 		}
    513 
    514 		/* Unlink buf from head of list. */
    515 		BUFQ_REMOVE(&sc->sc_buflist, bp);
    516 
    517 		/* Do the transfer to/from user space. */
    518 		error = 0;
    519 		bp->b_resid = bp->b_bcount;
    520 		off = (bp->b_blkno << DEV_BSHIFT);
    521 		if (off >= sc->sc_size) {
    522 			if (bp->b_flags & B_READ)
    523 				goto done;	/* EOF (not an error) */
    524 			error = EIO;
    525 			goto done;
    526 		}
    527 		xfer = bp->b_resid;
    528 		if (xfer > (sc->sc_size - off))
    529 			xfer = (sc->sc_size - off);
    530 		addr = sc->sc_addr + off;
    531 		if (bp->b_flags & B_READ)
    532 			error = copyin(addr, bp->b_data, xfer);
    533 		else
    534 			error = copyout(bp->b_data, addr, xfer);
    535 		if (!error)
    536 			bp->b_resid -= xfer;
    537 
    538 	done:
    539 		if (error) {
    540 			bp->b_error = error;
    541 			bp->b_flags |= B_ERROR;
    542 		}
    543 		biodone(bp);
    544 	}
    545 }
    546 #endif	/* MEMORY_DISK_SERVER */
    547