NetBSD/sys/dev/md.c

548 lines
12 KiB
C
Raw Normal View History

/* $NetBSD: md.c,v 1.14 1997/08/01 19:44:11 leo Exp $ */
1995-10-09 02:30:57 +03:00
/*
* Copyright (c) 1995 Gordon W. Ross, Leo Weppelman.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* 4. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by
* Gordon W. Ross and Leo Weppelman.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
1996-12-29 02:09:26 +03:00
* This implements a general-purpose memory-disk.
1997-05-24 03:44:34 +04:00
* See md.h for notes on the config types.
1995-10-09 02:30:57 +03:00
*
* Note that this driver provides the same functionality
* as the MFS filesystem hack, but this is better because
* you can use this for any filesystem type you'd like!
*
* Credit for most of the kmem ramdisk code goes to:
* Leo Weppelman (atari) and Phil Nelson (pc532)
1996-12-29 02:09:26 +03:00
* Credit for the ideas behind the "user space memory" code goes
1995-10-09 02:30:57 +03:00
* to the authors of the MFS implementation.
*/
#include <sys/param.h>
1996-03-23 02:02:02 +03:00
#include <sys/kernel.h>
#include <sys/malloc.h>
1995-10-09 02:30:57 +03:00
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/disk.h>
1996-04-12 12:30:09 +04:00
#include <sys/proc.h>
#include <sys/conf.h>
#include <sys/disklabel.h>
1995-10-09 02:30:57 +03:00
#include <vm/vm.h>
#include <vm/vm_kern.h>
1996-12-29 02:09:26 +03:00
#include <vm/vm_extern.h>
1995-10-09 02:30:57 +03:00
1996-12-29 02:09:26 +03:00
#include <dev/md.h>
1995-10-09 02:30:57 +03:00
/*
* By default, include the user-space functionality.
1996-12-29 02:09:26 +03:00
* Use `options MEMORY_DISK_SERVER=0' to turn it off.
1995-10-09 02:30:57 +03:00
*/
1996-12-29 02:09:26 +03:00
#ifndef MEMORY_DISK_SERVER
#define MEMORY_DISK_SERVER 1
1995-10-09 02:30:57 +03:00
#endif
/*
* XXX: the "control" unit is (base unit + 16).
* We should just use the cdev as the "control", but
* that interferes with the security stuff preventing
* simulatneous use of raw and block devices.
*
1996-12-29 02:09:26 +03:00
* XXX Assumption: 16 memory-disks are enough!
1995-10-09 02:30:57 +03:00
*/
1996-12-29 02:09:26 +03:00
#define MD_MAX_UNITS 0x10
#define MD_IS_CTRL(unit) (unit & 0x10)
#define MD_UNIT(unit) (unit & 0xF)
1995-10-09 02:30:57 +03:00
/* autoconfig stuff... */
1996-12-29 02:09:26 +03:00
struct md_softc {
1995-10-09 02:30:57 +03:00
struct device sc_dev; /* REQUIRED first entry */
struct disk sc_dkdev; /* hook for generic disk handling */
1996-12-29 02:09:26 +03:00
struct md_conf sc_md;
1995-10-09 02:30:57 +03:00
struct buf *sc_buflist;
int sc_flags;
};
1996-12-29 02:09:26 +03:00
/* shorthand for fields in sc_md: */
#define sc_addr sc_md.md_addr
#define sc_size sc_md.md_size
#define sc_type sc_md.md_type
1995-10-09 02:30:57 +03:00
/* flags */
1996-12-29 02:09:26 +03:00
#define MD_ISOPEN 0x01
#define MD_SERVED 0x02
1995-10-09 02:30:57 +03:00
1996-12-29 02:09:26 +03:00
void mdattach __P((int));
static void md_attach __P((struct device *, struct device *, void *));
1996-03-23 02:02:02 +03:00
/*
* Some ports (like i386) use a swapgeneric that wants to
1996-12-29 02:09:26 +03:00
* snoop around in this md_cd structure. It is preserved
1996-03-23 02:02:02 +03:00
* (for now) to remain compatible with such practice.
* XXX - that practice is questionable...
*/
1996-12-29 02:09:26 +03:00
struct cfdriver md_cd = {
NULL, "md", DV_DULL, NULL, 0
};
1995-10-09 02:30:57 +03:00
1996-12-29 02:09:26 +03:00
void mdstrategy __P((struct buf *bp));
struct dkdriver mddkdriver = { mdstrategy };
1996-03-23 02:02:02 +03:00
static int ramdisk_ndevs;
1996-12-29 02:09:26 +03:00
static void *ramdisk_devs[MD_MAX_UNITS];
1996-03-23 02:02:02 +03:00
/*
* This is called if we are configured as a pseudo-device
*/
void
1996-12-29 02:09:26 +03:00
mdattach(n)
1996-03-23 02:02:02 +03:00
int n;
1995-10-09 02:30:57 +03:00
{
1996-12-29 02:09:26 +03:00
struct md_softc *sc;
1996-03-23 02:02:02 +03:00
int i;
#ifdef DIAGNOSTIC
if (ramdisk_ndevs) {
1996-10-13 05:37:04 +04:00
printf("ramdisk: multiple attach calls?\n");
1996-03-23 02:02:02 +03:00
return;
}
#endif
1996-03-23 02:02:02 +03:00
/* XXX: Are we supposed to provide a default? */
if (n <= 1)
n = 1;
1996-12-29 02:09:26 +03:00
if (n > MD_MAX_UNITS)
n = MD_MAX_UNITS;
1996-03-23 02:02:02 +03:00
ramdisk_ndevs = n;
1996-12-29 02:09:26 +03:00
/* XXX: Fake-up md_cd (see above) */
md_cd.cd_ndevs = ramdisk_ndevs;
md_cd.cd_devs = ramdisk_devs;
1996-03-23 02:02:02 +03:00
/* Attach as if by autoconfig. */
for (i = 0; i < n; i++) {
sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
1996-03-23 02:02:02 +03:00
if (!sc) {
1996-10-13 05:37:04 +04:00
printf("ramdisk: malloc for attach failed!\n");
1996-03-23 02:02:02 +03:00
return;
}
bzero((caddr_t)sc, sizeof(*sc));
ramdisk_devs[i] = sc;
sc->sc_dev.dv_unit = i;
1996-12-29 02:09:26 +03:00
sprintf(sc->sc_dev.dv_xname, "md%d", i);
md_attach(NULL, &sc->sc_dev, NULL);
1996-03-23 02:02:02 +03:00
}
1995-10-09 02:30:57 +03:00
}
static void
1996-12-29 02:09:26 +03:00
md_attach(parent, self, aux)
1995-10-09 02:30:57 +03:00
struct device *parent, *self;
void *aux;
{
1996-12-29 02:09:26 +03:00
struct md_softc *sc = (struct md_softc *)self;
1995-10-09 02:30:57 +03:00
/* XXX - Could accept aux info here to set the config. */
1996-12-29 02:09:26 +03:00
#ifdef MEMORY_DISK_HOOKS
1995-10-09 02:30:57 +03:00
/*
* This external function might setup a pre-loaded disk.
1996-12-29 02:09:26 +03:00
* All it would need to do is setup the md_conf struct.
* See sys/arch/sun3/dev/md_root.c for an example.
1995-10-09 02:30:57 +03:00
*/
1996-12-29 02:09:26 +03:00
md_attach_hook(sc->sc_dev.dv_unit, &sc->sc_md);
1995-10-09 02:30:57 +03:00
#endif
/*
* Initialize and attach the disk structure.
*/
1996-12-29 02:09:26 +03:00
sc->sc_dkdev.dk_driver = &mddkdriver;
sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
disk_attach(&sc->sc_dkdev);
1995-10-09 02:30:57 +03:00
}
/*
* operational routines:
* open, close, read, write, strategy,
* ioctl, dump, size
*/
1996-12-29 02:09:26 +03:00
#if MEMORY_DISK_SERVER
static int md_server_loop __P((struct md_softc *sc));
static int md_ioctl_server __P((struct md_softc *sc,
struct md_conf *umd, struct proc *proc));
1995-10-09 02:30:57 +03:00
#endif
1996-12-29 02:09:26 +03:00
static int md_ioctl_kalloc __P((struct md_softc *sc,
struct md_conf *umd, struct proc *proc));
dev_type_open(mdopen);
dev_type_close(mdclose);
dev_type_read(mdread);
dev_type_write(mdwrite);
dev_type_ioctl(mdioctl);
dev_type_size(mdsize);
dev_type_dump(mddump);
int mddump(dev, blkno, va, size)
1995-10-09 02:30:57 +03:00
dev_t dev;
daddr_t blkno;
caddr_t va;
size_t size;
{
return ENODEV;
}
1996-12-29 02:09:26 +03:00
int mdsize(dev_t dev)
1995-10-09 02:30:57 +03:00
{
int unit;
1996-12-29 02:09:26 +03:00
struct md_softc *sc;
1995-10-09 02:30:57 +03:00
/* Disallow control units. */
unit = DISKUNIT(dev);
1996-03-23 02:02:02 +03:00
if (unit >= ramdisk_ndevs)
1995-10-09 02:30:57 +03:00
return 0;
1996-03-23 02:02:02 +03:00
sc = ramdisk_devs[unit];
1995-10-09 02:30:57 +03:00
if (sc == NULL)
return 0;
1996-12-29 02:09:26 +03:00
if (sc->sc_type == MD_UNCONFIGURED)
1995-10-09 02:30:57 +03:00
return 0;
return (sc->sc_size >> DEV_BSHIFT);
}
1996-04-12 12:30:09 +04:00
int
1996-12-29 02:09:26 +03:00
mdopen(dev, flag, fmt, proc)
1995-10-09 02:30:57 +03:00
dev_t dev;
int flag, fmt;
struct proc *proc;
{
int md, unit;
1996-12-29 02:09:26 +03:00
struct md_softc *sc;
1995-10-09 02:30:57 +03:00
md = DISKUNIT(dev);
1996-12-29 02:09:26 +03:00
unit = MD_UNIT(md);
1996-03-23 02:02:02 +03:00
if (unit >= ramdisk_ndevs)
1995-10-09 02:30:57 +03:00
return ENXIO;
1996-03-23 02:02:02 +03:00
sc = ramdisk_devs[unit];
1995-10-09 02:30:57 +03:00
if (sc == NULL)
return ENXIO;
/*
* The control device is not exclusive, and can
* open uninitialized units (so you can setconf).
*/
1996-12-29 02:09:26 +03:00
if (MD_IS_CTRL(md))
1995-10-09 02:30:57 +03:00
return 0;
1996-12-29 02:09:26 +03:00
#ifdef MEMORY_DISK_HOOKS
1995-10-09 02:30:57 +03:00
/* Call the open hook to allow loading the device. */
1996-12-29 02:09:26 +03:00
md_open_hook(unit, &sc->sc_md);
1995-10-09 02:30:57 +03:00
#endif
/*
* This is a normal, "slave" device, so
* enforce initialized, exclusive open.
*/
1996-12-29 02:09:26 +03:00
if (sc->sc_type == MD_UNCONFIGURED)
1995-10-09 02:30:57 +03:00
return ENXIO;
1996-12-29 02:09:26 +03:00
if (sc->sc_flags & MD_ISOPEN)
1995-10-09 02:30:57 +03:00
return EBUSY;
return 0;
}
1996-04-12 12:30:09 +04:00
int
1996-12-29 02:09:26 +03:00
mdclose(dev, flag, fmt, proc)
1995-10-09 02:30:57 +03:00
dev_t dev;
int flag, fmt;
struct proc *proc;
{
int md, unit;
1996-12-29 02:09:26 +03:00
struct md_softc *sc;
1995-10-09 02:30:57 +03:00
md = DISKUNIT(dev);
1996-12-29 02:09:26 +03:00
unit = MD_UNIT(md);
1996-03-23 02:02:02 +03:00
sc = ramdisk_devs[unit];
1995-10-09 02:30:57 +03:00
1996-12-29 02:09:26 +03:00
if (MD_IS_CTRL(md))
1995-10-09 02:30:57 +03:00
return 0;
/* Normal device. */
sc->sc_flags = 0;
return 0;
}
int
1996-12-29 02:09:26 +03:00
mdread(dev, uio, flags)
1995-10-09 02:30:57 +03:00
dev_t dev;
struct uio *uio;
1996-04-12 12:30:09 +04:00
int flags;
1995-10-09 02:30:57 +03:00
{
1996-12-29 02:09:26 +03:00
return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
1995-10-09 02:30:57 +03:00
}
int
1996-12-29 02:09:26 +03:00
mdwrite(dev, uio, flags)
1995-10-09 02:30:57 +03:00
dev_t dev;
struct uio *uio;
1996-04-12 12:30:09 +04:00
int flags;
1995-10-09 02:30:57 +03:00
{
1996-12-29 02:09:26 +03:00
return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
1995-10-09 02:30:57 +03:00
}
/*
* Handle I/O requests, either directly, or
* by passing them to the server process.
*/
void
1996-12-29 02:09:26 +03:00
mdstrategy(bp)
1995-10-09 02:30:57 +03:00
struct buf *bp;
{
1996-12-29 02:09:26 +03:00
int md, unit;
struct md_softc *sc;
caddr_t addr;
size_t off, xfer;
1995-10-09 02:30:57 +03:00
md = DISKUNIT(bp->b_dev);
1996-12-29 02:09:26 +03:00
unit = MD_UNIT(md);
1996-03-23 02:02:02 +03:00
sc = ramdisk_devs[unit];
1995-10-09 02:30:57 +03:00
switch (sc->sc_type) {
1996-12-29 02:09:26 +03:00
#if MEMORY_DISK_SERVER
case MD_UMEM_SERVER:
1995-10-09 02:30:57 +03:00
/* Just add this job to the server's queue. */
bp->b_actf = sc->sc_buflist;
sc->sc_buflist = bp;
if (bp->b_actf == NULL) {
/* server queue was empty. */
wakeup((caddr_t)sc);
1996-12-29 02:09:26 +03:00
/* see md_server_loop() */
1995-10-09 02:30:57 +03:00
}
/* no biodone in this case */
return;
1996-12-29 02:09:26 +03:00
#endif /* MEMORY_DISK_SERVER */
1995-10-09 02:30:57 +03:00
1996-12-29 02:09:26 +03:00
case MD_KMEM_FIXED:
case MD_KMEM_ALLOCATED:
1995-10-09 02:30:57 +03:00
/* These are in kernel space. Access directly. */
bp->b_resid = bp->b_bcount;
off = (bp->b_blkno << DEV_BSHIFT);
if (off >= sc->sc_size) {
if (bp->b_flags & B_READ)
break; /* EOF */
goto set_eio;
}
xfer = bp->b_resid;
if (xfer > (sc->sc_size - off))
xfer = (sc->sc_size - off);
addr = sc->sc_addr + off;
if (bp->b_flags & B_READ)
bcopy(addr, bp->b_data, xfer);
else
bcopy(bp->b_data, addr, xfer);
bp->b_resid -= xfer;
break;
default:
bp->b_resid = bp->b_bcount;
set_eio:
bp->b_error = EIO;
bp->b_flags |= B_ERROR;
break;
}
biodone(bp);
}
int
1996-12-29 02:09:26 +03:00
mdioctl(dev, cmd, data, flag, proc)
dev_t dev;
u_long cmd;
1995-10-09 02:30:57 +03:00
int flag;
1996-12-29 02:09:26 +03:00
caddr_t data;
1995-10-09 02:30:57 +03:00
struct proc *proc;
{
1996-12-29 02:09:26 +03:00
int md, unit;
struct md_softc *sc;
struct md_conf *umd;
1995-10-09 02:30:57 +03:00
md = DISKUNIT(dev);
1996-12-29 02:09:26 +03:00
unit = MD_UNIT(md);
1996-03-23 02:02:02 +03:00
sc = ramdisk_devs[unit];
1995-10-09 02:30:57 +03:00
/* If this is not the control device, punt! */
1996-12-29 02:09:26 +03:00
if (MD_IS_CTRL(md) == 0)
1995-10-09 02:30:57 +03:00
return ENOTTY;
1996-12-29 02:09:26 +03:00
umd = (struct md_conf *)data;
1995-10-09 02:30:57 +03:00
switch (cmd) {
1996-12-29 02:09:26 +03:00
case MD_GETCONF:
*umd = sc->sc_md;
1995-10-09 02:30:57 +03:00
return 0;
1996-12-29 02:09:26 +03:00
case MD_SETCONF:
1995-10-09 02:30:57 +03:00
/* Can only set it once. */
1996-12-29 02:09:26 +03:00
if (sc->sc_type != MD_UNCONFIGURED)
1995-10-09 02:30:57 +03:00
break;
1996-12-29 02:09:26 +03:00
switch (umd->md_type) {
case MD_KMEM_ALLOCATED:
return md_ioctl_kalloc(sc, umd, proc);
#if MEMORY_DISK_SERVER
case MD_UMEM_SERVER:
return md_ioctl_server(sc, umd, proc);
1995-10-09 02:30:57 +03:00
#endif
default:
break;
}
break;
}
return EINVAL;
}
/*
1996-12-29 02:09:26 +03:00
* Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
1995-10-09 02:30:57 +03:00
* Just allocate some kernel memory and return.
*/
1996-04-12 12:30:09 +04:00
static int
1996-12-29 02:09:26 +03:00
md_ioctl_kalloc(sc, umd, proc)
struct md_softc *sc;
struct md_conf *umd;
1995-10-09 02:30:57 +03:00
struct proc *proc;
{
vm_offset_t addr;
vm_size_t size;
/* Sanity check the size. */
1996-12-29 02:09:26 +03:00
size = umd->md_size;
1995-10-09 02:30:57 +03:00
addr = kmem_alloc(kernel_map, size);
if (!addr)
return ENOMEM;
/* This unit is now configured. */
sc->sc_addr = (caddr_t)addr; /* kernel space */
sc->sc_size = (size_t)size;
1996-12-29 02:09:26 +03:00
sc->sc_type = MD_KMEM_ALLOCATED;
1995-10-09 02:30:57 +03:00
return 0;
}
1996-12-29 02:09:26 +03:00
#if MEMORY_DISK_SERVER
1995-10-09 02:30:57 +03:00
/*
1996-12-29 02:09:26 +03:00
* Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
1995-10-09 02:30:57 +03:00
* Set config, then become the I/O server for this unit.
*/
1996-04-12 12:30:09 +04:00
static int
1996-12-29 02:09:26 +03:00
md_ioctl_server(sc, umd, proc)
struct md_softc *sc;
struct md_conf *umd;
1995-10-09 02:30:57 +03:00
struct proc *proc;
{
vm_offset_t end;
int error;
/* Sanity check addr, size. */
1996-12-29 02:09:26 +03:00
end = (vm_offset_t) (umd->md_addr + umd->md_size);
1995-10-09 02:30:57 +03:00
if ((end >= VM_MAXUSER_ADDRESS) ||
1996-12-29 02:09:26 +03:00
(end < ((vm_offset_t) umd->md_addr)) )
1995-10-09 02:30:57 +03:00
return EINVAL;
/* This unit is now configured. */
1996-12-29 02:09:26 +03:00
sc->sc_addr = umd->md_addr; /* user space */
sc->sc_size = umd->md_size;
sc->sc_type = MD_UMEM_SERVER;
1995-10-09 02:30:57 +03:00
/* Become the server daemon */
1996-12-29 02:09:26 +03:00
error = md_server_loop(sc);
1995-10-09 02:30:57 +03:00
/* This server is now going away! */
1996-12-29 02:09:26 +03:00
sc->sc_type = MD_UNCONFIGURED;
1995-10-09 02:30:57 +03:00
sc->sc_addr = 0;
sc->sc_size = 0;
return (error);
}
1996-12-29 02:09:26 +03:00
int md_sleep_pri = PWAIT | PCATCH;
1995-10-09 02:30:57 +03:00
static int
1996-12-29 02:09:26 +03:00
md_server_loop(sc)
struct md_softc *sc;
1995-10-09 02:30:57 +03:00
{
struct buf *bp;
caddr_t addr; /* user space address */
size_t off; /* offset into "device" */
size_t xfer; /* amount to transfer */
int error;
for (;;) {
/* Wait for some work to arrive. */
while (sc->sc_buflist == NULL) {
1996-12-29 02:09:26 +03:00
error = tsleep((caddr_t)sc, md_sleep_pri, "md_idle", 0);
1995-10-09 02:30:57 +03:00
if (error)
return error;
}
/* Unlink buf from head of list. */
bp = sc->sc_buflist;
sc->sc_buflist = bp->b_actf;
bp->b_actf = NULL;
/* Do the transfer to/from user space. */
error = 0;
bp->b_resid = bp->b_bcount;
off = (bp->b_blkno << DEV_BSHIFT);
if (off >= sc->sc_size) {
if (bp->b_flags & B_READ)
goto done; /* EOF (not an error) */
error = EIO;
goto done;
}
xfer = bp->b_resid;
if (xfer > (sc->sc_size - off))
xfer = (sc->sc_size - off);
addr = sc->sc_addr + off;
if (bp->b_flags & B_READ)
error = copyin(addr, bp->b_data, xfer);
else
error = copyout(bp->b_data, addr, xfer);
if (!error)
bp->b_resid -= xfer;
done:
if (error) {
bp->b_error = error;
bp->b_flags |= B_ERROR;
}
biodone(bp);
}
}
1996-12-29 02:09:26 +03:00
#endif /* MEMORY_DISK_SERVER */