1999-08-29 13:28:55 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "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>
|
1999-09-09 19:08:44 +00:00
|
|
|
#include <sys/sysctl.h>
|
1999-08-29 13:28:55 +00:00
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/disk.h>
|
|
|
|
#include <sys/malloc.h>
|
1999-09-01 05:38:09 +00:00
|
|
|
#include <machine/md_var.h>
|
1999-08-29 13:28:55 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
dev_t
|
1999-09-12 20:40:29 +00:00
|
|
|
disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, struct cdevsw *proto)
|
1999-08-29 13:28:55 +00:00
|
|
|
{
|
|
|
|
dev_t dev;
|
1999-09-12 09:16:00 +00:00
|
|
|
|
|
|
|
bzero(dp, sizeof(*dp));
|
|
|
|
|
1999-08-29 13:28:55 +00:00
|
|
|
dev = makedev(cdevsw->d_maj, 0);
|
1999-09-12 09:16:00 +00:00
|
|
|
if (!devsw(dev)) {
|
1999-09-12 20:40:29 +00:00
|
|
|
*proto = *cdevsw;
|
|
|
|
proto->d_open = diskopen;
|
|
|
|
proto->d_close = diskclose;
|
|
|
|
proto->d_ioctl = diskioctl;
|
|
|
|
proto->d_strategy = diskstrategy;
|
|
|
|
proto->d_psize = diskpsize;
|
|
|
|
cdevsw_add(proto);
|
1999-08-29 13:28:55 +00:00
|
|
|
}
|
|
|
|
|
1999-11-19 23:34:01 +00:00
|
|
|
if (bootverbose)
|
|
|
|
printf("Creating DISK %s%d\n", cdevsw->d_name, unit);
|
1999-09-13 18:20:21 +00:00
|
|
|
dev = make_dev(proto, dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART),
|
1999-09-12 09:16:00 +00:00
|
|
|
0, 0, 0, "r%s%d", cdevsw->d_name, unit);
|
1999-08-29 13:28:55 +00:00
|
|
|
|
|
|
|
dev->si_disk = dp;
|
|
|
|
dp->d_dev = dev;
|
1999-11-06 10:25:58 +00:00
|
|
|
dp->d_dsflags = flags;
|
1999-09-12 20:40:29 +00:00
|
|
|
dp->d_devsw = cdevsw;
|
1999-08-29 13:28:55 +00:00
|
|
|
return (dev);
|
|
|
|
}
|
|
|
|
|
1999-09-01 05:38:09 +00:00
|
|
|
int
|
|
|
|
disk_dumpcheck(dev_t dev, u_int *count, u_int *blkno, u_int *secsize)
|
|
|
|
{
|
|
|
|
struct disk *dp;
|
|
|
|
struct disklabel *dl;
|
|
|
|
u_int boff;
|
|
|
|
|
|
|
|
dp = dev->si_disk;
|
|
|
|
if (!dp)
|
|
|
|
return (ENXIO);
|
|
|
|
if (!dp->d_slice)
|
|
|
|
return (ENXIO);
|
|
|
|
dl = dsgetlabel(dev, dp->d_slice);
|
|
|
|
if (!dl)
|
|
|
|
return (ENXIO);
|
|
|
|
*count = (u_long)Maxmem * PAGE_SIZE / dl->d_secsize;
|
|
|
|
if (dumplo < 0 ||
|
|
|
|
(dumplo + *count > dl->d_partitions[dkpart(dev)].p_size))
|
|
|
|
return (EINVAL);
|
|
|
|
boff = dl->d_partitions[dkpart(dev)].p_offset +
|
|
|
|
dp->d_slice->dss_slices[dkslice(dev)].ds_offset;
|
|
|
|
*blkno = boff + dumplo;
|
|
|
|
*secsize = dl->d_secsize;
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
disk_invalidate (struct disk *disk)
|
|
|
|
{
|
|
|
|
dsgone(&disk->d_slice);
|
|
|
|
}
|
|
|
|
|
1999-08-29 13:28:55 +00:00
|
|
|
void
|
2000-01-28 20:49:43 +00:00
|
|
|
disk_destroy(dev_t dev)
|
1999-08-29 13:28:55 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-09-01 05:38:09 +00:00
|
|
|
/*
|
|
|
|
* The cdevsw functions
|
|
|
|
*/
|
|
|
|
|
1999-08-29 13:28:55 +00:00
|
|
|
static int
|
|
|
|
diskopen(dev_t dev, int oflags, int devtype, struct proc *p)
|
|
|
|
{
|
|
|
|
dev_t pdev;
|
|
|
|
struct disk *dp;
|
|
|
|
int error;
|
|
|
|
|
1999-09-01 05:38:09 +00:00
|
|
|
error = 0;
|
1999-08-29 13:28:55 +00:00
|
|
|
pdev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), RAW_PART);
|
1999-09-01 05:38:09 +00:00
|
|
|
|
1999-08-29 13:28:55 +00:00
|
|
|
dp = pdev->si_disk;
|
|
|
|
if (!dp)
|
|
|
|
return (ENXIO);
|
1999-09-01 05:38:09 +00:00
|
|
|
|
1999-11-06 10:25:58 +00:00
|
|
|
while (dp->d_flags & DISKFLAG_LOCK) {
|
|
|
|
dp->d_flags |= DISKFLAG_WANTED;
|
1999-12-19 12:36:41 +00:00
|
|
|
error = tsleep(dp, PRIBIO | PCATCH, "diskopen", hz);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
1999-11-06 10:25:58 +00:00
|
|
|
}
|
|
|
|
dp->d_flags |= DISKFLAG_LOCK;
|
|
|
|
|
1999-10-02 11:17:54 +00:00
|
|
|
if (!dsisopen(dp->d_slice)) {
|
1999-10-02 20:21:49 +00:00
|
|
|
if (!pdev->si_iosize_max)
|
|
|
|
pdev->si_iosize_max = dev->si_iosize_max;
|
1999-09-30 21:14:28 +00:00
|
|
|
error = dp->d_devsw->d_open(pdev, oflags, devtype, p);
|
1999-10-02 11:17:54 +00:00
|
|
|
}
|
1999-09-30 21:14:28 +00:00
|
|
|
|
|
|
|
/* Inherit properties from the whole/raw dev_t */
|
|
|
|
dev->si_disk = pdev->si_disk;
|
1999-09-01 05:38:09 +00:00
|
|
|
dev->si_drv1 = pdev->si_drv1;
|
|
|
|
dev->si_drv2 = pdev->si_drv2;
|
1999-09-30 21:14:28 +00:00
|
|
|
dev->si_iosize_max = pdev->si_iosize_max;
|
|
|
|
dev->si_bsize_phys = pdev->si_bsize_phys;
|
|
|
|
dev->si_bsize_best = pdev->si_bsize_best;
|
1999-08-29 13:28:55 +00:00
|
|
|
|
1999-09-01 05:38:09 +00:00
|
|
|
if (error)
|
1999-11-06 10:25:58 +00:00
|
|
|
goto out;
|
1999-09-01 05:38:09 +00:00
|
|
|
|
1999-11-06 10:25:58 +00:00
|
|
|
error = dsopen(dev, devtype, dp->d_dsflags, &dp->d_slice, &dp->d_label);
|
1999-08-29 13:28:55 +00:00
|
|
|
|
1999-09-01 05:38:09 +00:00
|
|
|
if (!dsisopen(dp->d_slice))
|
1999-10-04 09:28:36 +00:00
|
|
|
dp->d_devsw->d_close(pdev, oflags, devtype, p);
|
1999-11-06 10:25:58 +00:00
|
|
|
out:
|
|
|
|
dp->d_flags &= ~DISKFLAG_LOCK;
|
|
|
|
if (dp->d_flags & DISKFLAG_WANTED) {
|
|
|
|
dp->d_flags &= ~DISKFLAG_WANTED;
|
|
|
|
wakeup(dp);
|
|
|
|
}
|
1999-09-01 05:38:09 +00:00
|
|
|
|
1999-08-29 13:28:55 +00:00
|
|
|
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;
|
1999-09-30 05:29:59 +00:00
|
|
|
dsclose(dev, devtype, dp->d_slice);
|
1999-09-30 21:14:28 +00:00
|
|
|
if (!dsisopen(dp->d_slice)) {
|
1999-10-04 09:28:36 +00:00
|
|
|
error = dp->d_devsw->d_close(dp->d_dev, fflag, devtype, p);
|
1999-09-30 21:14:28 +00:00
|
|
|
}
|
1999-08-29 13:28:55 +00:00
|
|
|
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);
|
1999-10-02 11:17:54 +00:00
|
|
|
dp = bp->b_dev->si_disk = pdev->si_disk;
|
1999-08-29 13:28:55 +00:00
|
|
|
bp->b_dev->si_drv1 = pdev->si_drv1;
|
|
|
|
bp->b_dev->si_drv2 = pdev->si_drv2;
|
1999-10-02 11:17:54 +00:00
|
|
|
bp->b_dev->si_iosize_max = pdev->si_iosize_max;
|
|
|
|
bp->b_dev->si_bsize_phys = pdev->si_bsize_phys;
|
|
|
|
bp->b_dev->si_bsize_best = pdev->si_bsize_best;
|
1999-08-29 13:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!dp) {
|
|
|
|
bp->b_error = ENXIO;
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-01-10 12:21:39 +00:00
|
|
|
if (dscheck(bp, dp->d_slice) <= 0) {
|
1999-08-29 13:28:55 +00:00
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-09-12 20:40:29 +00:00
|
|
|
dp->d_devsw->d_strategy(bp);
|
1999-08-29 13:28:55 +00:00
|
|
|
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)
|
1999-09-12 20:40:29 +00:00
|
|
|
error = dp->d_devsw->d_ioctl(dev, cmd, data, fflag, p);
|
1999-08-29 13:28:55 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
diskpsize(dev_t dev)
|
|
|
|
{
|
|
|
|
struct disk *dp;
|
1999-09-01 05:38:09 +00:00
|
|
|
dev_t pdev;
|
1999-08-29 13:28:55 +00:00
|
|
|
|
|
|
|
dp = dev->si_disk;
|
1999-09-01 05:38:09 +00:00
|
|
|
if (!dp) {
|
|
|
|
pdev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), RAW_PART);
|
|
|
|
dp = pdev->si_disk;
|
|
|
|
dev->si_drv1 = pdev->si_drv1;
|
|
|
|
dev->si_drv2 = pdev->si_drv2;
|
|
|
|
/* XXX: don't set bp->b_dev->si_disk (?) */
|
|
|
|
}
|
1999-08-29 13:28:55 +00:00
|
|
|
return (dssize(dev, &dp->d_slice));
|
|
|
|
}
|
1999-09-09 19:08:44 +00:00
|
|
|
|
|
|
|
SYSCTL_DECL(_debug_sizeof);
|
|
|
|
|
|
|
|
SYSCTL_INT(_debug_sizeof, OID_AUTO, disklabel, CTLFLAG_RD,
|
|
|
|
0, sizeof(struct disklabel), "sizeof(struct disklabel)");
|
|
|
|
|
|
|
|
SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD,
|
|
|
|
0, sizeof(struct diskslices), "sizeof(struct diskslices)");
|
|
|
|
|
|
|
|
SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD,
|
|
|
|
0, sizeof(struct disk), "sizeof(struct disk)");
|