Add micro "disk" layer which should enable us to pull all the slice/label

stuff out of the device drivers.
This commit is contained in:
Poul-Henning Kamp 1999-08-29 13:28:55 +00:00
parent 27108a1511
commit da9e4f5550
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=50565
5 changed files with 225 additions and 0 deletions

View File

@ -326,6 +326,7 @@ kern/md5c.c standard
kern/subr_autoconf.c standard
kern/subr_bus.c standard
kern/subr_devstat.c standard
kern/subr_disk.c standard
kern/subr_diskslice.c standard
kern/subr_dkbad.c standard
kern/subr_eventhandler.c standard

183
sys/kern/subr_disk.c Normal file
View File

@ -0,0 +1,183 @@
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
* ----------------------------------------------------------------------------
*
* $FreeBSD$
*
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/buf.h>
#include <sys/conf.h>
#include <sys/disk.h>
#include <sys/malloc.h>
MALLOC_DEFINE(M_DISK, "disk", "disk data");
static d_strategy_t diskstrategy;
static d_open_t diskopen;
static d_close_t diskclose;
static d_ioctl_t diskioctl;
static d_psize_t diskpsize;
static struct cdevsw disk_cdevsw = {
/* open */ diskopen,
/* close */ diskclose,
/* read */ physread,
/* write */ physwrite,
/* ioctl */ diskioctl,
/* stop */ nostop,
/* reset */ noreset,
/* devtotty */ nodevtotty,
/* poll */ nopoll,
/* mmap */ nommap,
/* strategy */ diskstrategy,
/* name */ "disk",
/* parms */ noparms,
/* maj */ -1,
/* dump */ nodump,
/* psize */ diskpsize,
/* flags */ D_DISK,
/* maxio */ 0,
/* bmaj */ -1,
};
dev_t
disk_create(int unit, struct disk *dp, struct cdevsw *cdevsw)
{
dev_t dev;
struct cdevsw *cds;
dp->d_devsw = cdevsw;
dev = makedev(cdevsw->d_maj, 0);
cds = devsw(dev);
if (!cds) {
/* Build the "real" cdevsw */
MALLOC(cds, struct cdevsw *, sizeof(*cds), M_DISK, M_WAITOK);
*cds = disk_cdevsw;
cds->d_name = dp->d_devsw->d_name;
cds->d_maj = dp->d_devsw->d_maj;
cds->d_bmaj = dp->d_devsw->d_bmaj;
cds->d_flags = dp->d_devsw->d_flags;
cdevsw_add(cds);
}
printf("Creating DISK %s%d\n", cds->d_name, unit);
dev = make_dev(cds, dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART),
0, 0, 0, "r%s%d", cds->d_name, unit);
dev->si_disk = dp;
dp->d_dev = dev;
return (dev);
}
void
disk_delete(dev_t dev)
{
return;
}
static int
diskopen(dev_t dev, int oflags, int devtype, struct proc *p)
{
dev_t pdev;
struct disk *dp;
int error;
pdev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), RAW_PART);
dp = pdev->si_disk;
dev->si_disk = pdev->si_disk;
dev->si_drv1 = pdev->si_drv1;
dev->si_drv2 = pdev->si_drv2;
if (!dp)
return (ENXIO);
dev->si_disk = dp;
if (!dp->d_opencount++) {
error = dp->d_devsw->d_open(dev, oflags, devtype, p);
if (error)
return(error);
}
error = dsopen(dev, devtype, 0, &dp->d_slice, &dp->d_label);
if (error && !--dp->d_opencount)
dp->d_devsw->d_close(dev, oflags, devtype, p);
return(error);
}
static int
diskclose(dev_t dev, int fflag, int devtype, struct proc *p)
{
struct disk *dp;
int error;
error = 0;
dp = dev->si_disk;
dsclose(dev, devtype, dp->d_slice);
if (!--dp->d_opencount)
error = dp->d_devsw->d_close(dev, fflag, devtype, p);
return (error);
}
static void
diskstrategy(struct buf *bp)
{
dev_t pdev;
struct disk *dp;
dp = bp->b_dev->si_disk;
if (!dp) {
pdev = dkmodpart(dkmodslice(bp->b_dev, WHOLE_DISK_SLICE), RAW_PART);
dp = pdev->si_disk;
bp->b_dev->si_drv1 = pdev->si_drv1;
bp->b_dev->si_drv2 = pdev->si_drv2;
/* XXX: don't set bp->b_dev->si_disk (?) */
} else {
pdev = dp->d_dev;
}
if (!dp) {
bp->b_error = ENXIO;
bp->b_flags |= B_ERROR;
biodone(bp);
return;
}
if (dscheck(bp, dp->d_slice) < 0) {
biodone(bp);
return;
}
dp->d_devsw->d_strategy(bp);
return;
}
static int
diskioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
{
struct disk *dp;
int error;
dp = dev->si_disk;
error = dsioctl(dev, cmd, data, fflag, &dp->d_slice);
if (error == ENOIOCTL)
error = dp->d_devsw->d_ioctl(dev, cmd, data, fflag, p);
return (error);
}
static int
diskpsize(dev_t dev)
{
struct disk *dp;
dp = dev->si_disk;
return (dssize(dev, &dp->d_slice));
}

View File

@ -47,6 +47,7 @@
#define SPECNAMELEN 15
struct tty;
struct disk;
struct vnode;
struct specinfo {
@ -65,6 +66,7 @@ struct specinfo {
struct tty *__sit_tty;
} __si_tty;
struct {
struct disk *__sid_disk;
struct mount *__sid_mountpoint;
int __sid_bsize_phys; /* min physical block size */
int __sid_bsize_best; /* optimal block size */
@ -74,6 +76,7 @@ struct specinfo {
};
#define si_tty_tty __si_u.__si_tty.__sit_tty
#define si_disk __si_u.__si_disk.__sid_disk
#define si_mountpoint __si_u.__si_disk.__sid_mountpoint
#define si_bsize_phys __si_u.__si_disk.__sid_bsize_phys
#define si_bsize_best __si_u.__si_disk.__sid_bsize_best

35
sys/sys/disk.h Normal file
View File

@ -0,0 +1,35 @@
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
* ----------------------------------------------------------------------------
*
* $FreeBSD$
*
*/
#ifndef _SYS_DISK_H_
#define _SYS_DISK_H_
#ifndef _SYS_DISKSLICE_H_
#include <sys/diskslice.h>
#endif /* _SYS_DISKSLICE_H_ */
#ifndef _SYS_DISKLABEL
#include <sys/disklabel.h>
#endif /* _SYS_DISKLABEL */
struct disk {
int d_opencount;
struct cdevsw *d_devsw;
dev_t d_dev;
struct diskslices *d_slice;
struct disklabel d_label;
};
dev_t disk_create __P((int unit, struct disk *disk, struct cdevsw *cdevsw));
void disk_delete __P((dev_t dev));
#endif /* _SYS_DISK_H_ */

View File

@ -47,6 +47,7 @@
#define SPECNAMELEN 15
struct tty;
struct disk;
struct vnode;
struct specinfo {
@ -65,6 +66,7 @@ struct specinfo {
struct tty *__sit_tty;
} __si_tty;
struct {
struct disk *__sid_disk;
struct mount *__sid_mountpoint;
int __sid_bsize_phys; /* min physical block size */
int __sid_bsize_best; /* optimal block size */
@ -74,6 +76,7 @@ struct specinfo {
};
#define si_tty_tty __si_u.__si_tty.__sit_tty
#define si_disk __si_u.__si_disk.__sid_disk
#define si_mountpoint __si_u.__si_disk.__sid_mountpoint
#define si_bsize_phys __si_u.__si_disk.__sid_bsize_phys
#define si_bsize_best __si_u.__si_disk.__sid_bsize_best