Home | History | Annotate | Line # | Download | only in dev
md.c revision 1.4
      1 /*	$NetBSD: md.c,v 1.4 1996/01/07 22:03:31 thorpej 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-puspose RAM-disk.
     36  * See ramdisk.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 RAM" code goes
     45  * to the authors of the MFS implementation.
     46  */
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/buf.h>
     51 #include <sys/device.h>
     52 #include <sys/disk.h>
     53 
     54 #include <vm/vm.h>
     55 #include <vm/vm_kern.h>
     56 /* Don't want all those other VM headers... */
     57 extern vm_offset_t	 kmem_alloc __P((vm_map_t, vm_size_t));
     58 
     59 #include <dev/ramdisk.h>
     60 
     61 /*
     62  * By default, include the user-space functionality.
     63  * Use:  option RAMDISK_SERVER=0 to turn it off.
     64  */
     65 #ifndef RAMDISK_SERVER
     66 #define	RAMDISK_SERVER 1
     67 #endif
     68 
     69 /*
     70  * XXX: the "control" unit is (base unit + 16).
     71  * We should just use the cdev as the "control", but
     72  * that interferes with the security stuff preventing
     73  * simulatneous use of raw and block devices.
     74  *
     75  * XXX Assumption: 16 RAM-disks are enough!
     76  */
     77 #define RD_IS_CTRL(unit) (unit & 0x10)
     78 #define RD_UNIT(unit)    (unit &  0xF)
     79 
     80 /*
     81  * XXX -  This is just for a sanity check.  Only
     82  * applies to kernel-space RAM disk allocations.
     83  */
     84 #define RD_KMEM_MAX_SIZE	0x100000	/* 1MB */
     85 
     86 /* autoconfig stuff... */
     87 
     88 struct rd_softc {
     89 	struct device sc_dev;	/* REQUIRED first entry */
     90 	struct disk sc_dkdev;	/* hook for generic disk handling */
     91 	struct rd_conf sc_rd;
     92 	struct buf *sc_buflist;
     93 	int sc_flags;
     94 };
     95 /* shorthand for fields in sc_rd: */
     96 #define sc_addr sc_rd.rd_addr
     97 #define sc_size sc_rd.rd_size
     98 #define sc_type sc_rd.rd_type
     99 /* flags */
    100 #define RD_ISOPEN	0x01
    101 #define RD_SERVED	0x02
    102 
    103 static int  rd_match (struct device *, void *self, void *);
    104 static void rd_attach(struct device *, struct device *self, void *);
    105 
    106 struct cfdriver rdcd = {
    107 	NULL, "rd", rd_match, rd_attach,
    108 	DV_DULL, sizeof(struct rd_softc), NULL, 0 };
    109 
    110 void rdstrategy __P((struct buf *bp));
    111 
    112 struct dkdriver rddkdriver = { rdstrategy };
    113 
    114 static int
    115 rd_match(parent, self, aux)
    116 	struct device	*parent;
    117 	void	*self;
    118 	void	*aux;
    119 {
    120 	return(1);
    121 }
    122 
    123 static void
    124 rd_attach(parent, self, aux)
    125 	struct device	*parent, *self;
    126 	void		*aux;
    127 {
    128 	struct rd_softc *sc = (struct rd_softc *)self;
    129 
    130 	/* XXX - Could accept aux info here to set the config. */
    131 #ifdef	RAMDISK_HOOKS
    132 	/*
    133 	 * This external function might setup a pre-loaded disk.
    134 	 * All it would need to do is setup the rd_conf struct.
    135 	 * See sys/arch/sun3/dev/rd_root.c for an example.
    136 	 */
    137 	rd_attach_hook(sc->sc_dev.dv_unit, &sc->sc_rd);
    138 #endif
    139 	printf("\n");
    140 
    141 	/*
    142 	 * Initialize and attach the disk structure.
    143 	 */
    144 	bzero(&sc->sc_dkdev, sizeof(sc->sc_dkdev));
    145 	sc->sc_dkdev.dk_driver = &rddkdriver;
    146 	sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
    147 	disk_attach(&sc->sc_dkdev);
    148 }
    149 
    150 /*
    151  * operational routines:
    152  * open, close, read, write, strategy,
    153  * ioctl, dump, size
    154  */
    155 
    156 #if RAMDISK_SERVER
    157 static int rd_server_loop __P((struct rd_softc *sc));
    158 static int rd_ioctl_server __P((struct rd_softc *sc,
    159 		struct rd_conf *urd, struct proc *proc));
    160 #endif
    161 
    162 int rddump(dev, blkno, va, size)
    163 	dev_t dev;
    164 	daddr_t blkno;
    165 	caddr_t va;
    166 	size_t size;
    167 {
    168 	return ENODEV;
    169 }
    170 
    171 int rdsize(dev_t dev)
    172 {
    173 	int unit;
    174 	struct rd_softc *sc;
    175 
    176 	/* Disallow control units. */
    177 	unit = minor(dev);
    178 	if (unit >= rdcd.cd_ndevs)
    179 		return 0;
    180 	sc = rdcd.cd_devs[unit];
    181 	if (sc == NULL)
    182 		return 0;
    183 
    184 	if (sc->sc_type == RD_UNCONFIGURED)
    185 		return 0;
    186 
    187 	return (sc->sc_size >> DEV_BSHIFT);
    188 }
    189 
    190 int rdopen(dev, flag, fmt, proc)
    191 	dev_t   dev;
    192 	int     flag, fmt;
    193 	struct proc *proc;
    194 {
    195 	int md, unit;
    196 	struct rd_softc *sc;
    197 
    198 	md = minor(dev);
    199 	unit = RD_UNIT(md);
    200 	if (unit >= rdcd.cd_ndevs)
    201 		return ENXIO;
    202 	sc = rdcd.cd_devs[unit];
    203 	if (sc == NULL)
    204 		return ENXIO;
    205 
    206 	/*
    207 	 * The control device is not exclusive, and can
    208 	 * open uninitialized units (so you can setconf).
    209 	 */
    210 	if (RD_IS_CTRL(md))
    211 		return 0;
    212 
    213 #ifdef	RAMDISK_HOOKS
    214 	/* Call the open hook to allow loading the device. */
    215 	rd_open_hook(unit, &sc->sc_rd);
    216 #endif
    217 
    218 	/*
    219 	 * This is a normal, "slave" device, so
    220 	 * enforce initialized, exclusive open.
    221 	 */
    222 	if (sc->sc_type == RD_UNCONFIGURED)
    223 		return ENXIO;
    224 	if (sc->sc_flags & RD_ISOPEN)
    225 		return EBUSY;
    226 
    227 	return 0;
    228 }
    229 
    230 int rdclose(dev, flag, fmt, proc)
    231 	dev_t   dev;
    232 	int     flag, fmt;
    233 	struct proc *proc;
    234 {
    235 	int md, unit;
    236 	struct rd_softc *sc;
    237 
    238 	md = minor(dev);
    239 	unit = RD_UNIT(md);
    240 	sc = rdcd.cd_devs[unit];
    241 
    242 	if (RD_IS_CTRL(md))
    243 		return 0;
    244 
    245 	/* Normal device. */
    246 	sc->sc_flags = 0;
    247 
    248 	return 0;
    249 }
    250 
    251 int
    252 rdread(dev, uio)
    253 	dev_t		dev;
    254 	struct uio	*uio;
    255 {
    256 	return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
    257 }
    258 
    259 int
    260 rdwrite(dev, uio)
    261 	dev_t		dev;
    262 	struct uio	*uio;
    263 {
    264 	return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
    265 }
    266 
    267 /*
    268  * Handle I/O requests, either directly, or
    269  * by passing them to the server process.
    270  */
    271 void
    272 rdstrategy(bp)
    273 	struct buf *bp;
    274 {
    275 	int md, unit;
    276 	struct rd_softc *sc;
    277 	caddr_t addr;
    278 	size_t  off, xfer;
    279 
    280 	md = minor(bp->b_dev);
    281 	unit = RD_UNIT(md);
    282 	sc = rdcd.cd_devs[unit];
    283 
    284 	switch (sc->sc_type) {
    285 #if RAMDISK_SERVER
    286 	case RD_UMEM_SERVER:
    287 		/* Just add this job to the server's queue. */
    288 		bp->b_actf = sc->sc_buflist;
    289 		sc->sc_buflist = bp;
    290 		if (bp->b_actf == NULL) {
    291 			/* server queue was empty. */
    292 			wakeup((caddr_t)sc);
    293 			/* see rd_server_loop() */
    294 		}
    295 		/* no biodone in this case */
    296 		return;
    297 #endif	/* RAMDISK_SERVER */
    298 
    299 	case RD_KMEM_FIXED:
    300 	case RD_KMEM_ALLOCATED:
    301 		/* These are in kernel space.  Access directly. */
    302 		bp->b_resid = bp->b_bcount;
    303 		off = (bp->b_blkno << DEV_BSHIFT);
    304 		if (off >= sc->sc_size) {
    305 			if (bp->b_flags & B_READ)
    306 				break;	/* EOF */
    307 			goto set_eio;
    308 		}
    309 		xfer = bp->b_resid;
    310 		if (xfer > (sc->sc_size - off))
    311 			xfer = (sc->sc_size - off);
    312 		addr = sc->sc_addr + off;
    313 		if (bp->b_flags & B_READ)
    314 			bcopy(addr, bp->b_data, xfer);
    315 		else
    316 			bcopy(bp->b_data, addr, xfer);
    317 		bp->b_resid -= xfer;
    318 		break;
    319 
    320 	default:
    321 		bp->b_resid = bp->b_bcount;
    322 	set_eio:
    323 		bp->b_error = EIO;
    324 		bp->b_flags |= B_ERROR;
    325 		break;
    326 	}
    327 	biodone(bp);
    328 }
    329 
    330 int
    331 rdioctl(dev, cmd, data, flag, proc)
    332 	dev_t	dev;
    333 	u_long	cmd;
    334 	int		flag;
    335 	caddr_t	data;
    336 	struct proc	*proc;
    337 {
    338 	int md, unit;
    339 	struct rd_softc *sc;
    340 	struct rd_conf *urd;
    341 
    342 	md = minor(dev);
    343 	unit = RD_UNIT(md);
    344 	sc = rdcd.cd_devs[unit];
    345 
    346 	/* If this is not the control device, punt! */
    347 	if (RD_IS_CTRL(md) == 0)
    348 		return ENOTTY;
    349 
    350 	urd = (struct rd_conf *)data;
    351 	switch (cmd) {
    352 	case RD_GETCONF:
    353 		*urd = sc->sc_rd;
    354 		return 0;
    355 
    356 	case RD_SETCONF:
    357 		/* Can only set it once. */
    358 		if (sc->sc_type != RD_UNCONFIGURED)
    359 			break;
    360 		switch (urd->rd_type) {
    361 		case RD_KMEM_ALLOCATED:
    362 			return rd_ioctl_kalloc(sc, urd, proc);
    363 #if RAMDISK_SERVER
    364 		case RD_UMEM_SERVER:
    365 			return rd_ioctl_server(sc, urd, proc);
    366 #endif
    367 		default:
    368 			break;
    369 		}
    370 		break;
    371 	}
    372 	return EINVAL;
    373 }
    374 
    375 /*
    376  * Handle ioctl RD_SETCONF for (sc_type == RD_KMEM_ALLOCATED)
    377  * Just allocate some kernel memory and return.
    378  */
    379 int
    380 rd_ioctl_kalloc(sc, urd, proc)
    381 	struct rd_softc *sc;
    382 	struct rd_conf *urd;
    383 	struct proc	*proc;
    384 {
    385 	vm_offset_t addr;
    386 	vm_size_t  size;
    387 
    388 	/* Sanity check the size. */
    389 	size = urd->rd_size;
    390 	if (size > RD_KMEM_MAX_SIZE)
    391 		return EINVAL;
    392 	addr = kmem_alloc(kernel_map, size);
    393 	if (!addr)
    394 		return ENOMEM;
    395 
    396 	/* This unit is now configured. */
    397 	sc->sc_addr = (caddr_t)addr; 	/* kernel space */
    398 	sc->sc_size = (size_t)size;
    399 	sc->sc_type = RD_KMEM_ALLOCATED;
    400 	return 0;
    401 }
    402 
    403 #if RAMDISK_SERVER
    404 
    405 /*
    406  * Handle ioctl RD_SETCONF for (sc_type == RD_UMEM_SERVER)
    407  * Set config, then become the I/O server for this unit.
    408  */
    409 int
    410 rd_ioctl_server(sc, urd, proc)
    411 	struct rd_softc *sc;
    412 	struct rd_conf *urd;
    413 	struct proc	*proc;
    414 {
    415 	vm_offset_t end;
    416 	int error;
    417 
    418 	/* Sanity check addr, size. */
    419 	end = (vm_offset_t) (urd->rd_addr + urd->rd_size);
    420 
    421 	if ((end >= VM_MAXUSER_ADDRESS) ||
    422 		(end < ((vm_offset_t) urd->rd_addr)) )
    423 		return EINVAL;
    424 
    425 	/* This unit is now configured. */
    426 	sc->sc_addr = urd->rd_addr; 	/* user space */
    427 	sc->sc_size = urd->rd_size;
    428 	sc->sc_type = RD_UMEM_SERVER;
    429 
    430 	/* Become the server daemon */
    431 	error = rd_server_loop(sc);
    432 
    433 	/* This server is now going away! */
    434 	sc->sc_type = RD_UNCONFIGURED;
    435 	sc->sc_addr = 0;
    436 	sc->sc_size = 0;
    437 
    438 	return (error);
    439 }
    440 
    441 int	rd_sleep_pri = PWAIT | PCATCH;
    442 
    443 static int
    444 rd_server_loop(sc)
    445 	struct rd_softc *sc;
    446 {
    447 	struct buf *bp;
    448 	caddr_t addr;	/* user space address */
    449 	size_t  off;	/* offset into "device" */
    450 	size_t  xfer;	/* amount to transfer */
    451 	int error;
    452 
    453 	for (;;) {
    454 		/* Wait for some work to arrive. */
    455 		while (sc->sc_buflist == NULL) {
    456 			error = tsleep((caddr_t)sc, rd_sleep_pri, "rd_idle", 0);
    457 			if (error)
    458 				return error;
    459 		}
    460 
    461 		/* Unlink buf from head of list. */
    462 		bp = sc->sc_buflist;
    463 		sc->sc_buflist = bp->b_actf;
    464 		bp->b_actf = NULL;
    465 
    466 		/* Do the transfer to/from user space. */
    467 		error = 0;
    468 		bp->b_resid = bp->b_bcount;
    469 		off = (bp->b_blkno << DEV_BSHIFT);
    470 		if (off >= sc->sc_size) {
    471 			if (bp->b_flags & B_READ)
    472 				goto done;	/* EOF (not an error) */
    473 			error = EIO;
    474 			goto done;
    475 		}
    476 		xfer = bp->b_resid;
    477 		if (xfer > (sc->sc_size - off))
    478 			xfer = (sc->sc_size - off);
    479 		addr = sc->sc_addr + off;
    480 		if (bp->b_flags & B_READ)
    481 			error = copyin(addr, bp->b_data, xfer);
    482 		else
    483 			error = copyout(bp->b_data, addr, xfer);
    484 		if (!error)
    485 			bp->b_resid -= xfer;
    486 
    487 	done:
    488 		if (error) {
    489 			bp->b_error = error;
    490 			bp->b_flags |= B_ERROR;
    491 		}
    492 		biodone(bp);
    493 	}
    494 }
    495 
    496 #endif	/* RAMDISK_SERVER */
    497