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