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