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