summaryrefslogtreecommitdiffstats
path: root/sys
diff options
context:
space:
mode:
authormatthew <matthew@openbsd.org>2011-06-30 16:28:05 +0000
committermatthew <matthew@openbsd.org>2011-06-30 16:28:05 +0000
commite1d640230c81b9cd56db8aefc4986c8a29148312 (patch)
tree76e6272d686a8def9bfa59b8712ffcea2d2ab196 /sys
parentfinish ansi in uvm. ok ariane oga (diff)
downloadwireguard-openbsd-e1d640230c81b9cd56db8aefc4986c8a29148312.tar.xz
wireguard-openbsd-e1d640230c81b9cd56db8aefc4986c8a29148312.zip
Refactor some common open/close/detach disk driver code into
subr_disk.c. For now just the MI disk drivers. ok deraadt@, krw@; jsing@ liked the approach too
Diffstat (limited to 'sys')
-rw-r--r--sys/dev/ata/wd.c43
-rw-r--r--sys/dev/ccd.c43
-rw-r--r--sys/dev/rd.c41
-rw-r--r--sys/dev/vnd.c41
-rw-r--r--sys/kern/subr_disk.c59
-rw-r--r--sys/scsi/cd.c40
-rw-r--r--sys/scsi/sd.c41
-rw-r--r--sys/sys/disk.h5
8 files changed, 90 insertions, 223 deletions
diff --git a/sys/dev/ata/wd.c b/sys/dev/ata/wd.c
index 953a9a6ea89..e3499ca6d44 100644
--- a/sys/dev/ata/wd.c
+++ b/sys/dev/ata/wd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: wd.c,v 1.106 2011/06/20 06:45:07 matthew Exp $ */
+/* $OpenBSD: wd.c,v 1.107 2011/06/30 16:28:05 matthew Exp $ */
/* $NetBSD: wd.c,v 1.193 1999/02/28 17:15:27 explorer Exp $ */
/*
@@ -370,21 +370,12 @@ int
wddetach(struct device *self, int flags)
{
struct wd_softc *sc = (struct wd_softc *)self;
- int bmaj, cmaj, mn;
timeout_del(&sc->sc_restart_timeout);
bufq_drain(&sc->sc_bufq);
- /* Locate the lowest minor number to be detached. */
- mn = DISKMINOR(self->dv_unit, 0);
-
- for (bmaj = 0; bmaj < nblkdev; bmaj++)
- if (bdevsw[bmaj].d_open == wdopen)
- vdevgone(bmaj, mn, mn + MAXPARTITIONS - 1, VBLK);
- for (cmaj = 0; cmaj < nchrdev; cmaj++)
- if (cdevsw[cmaj].d_open == wdopen)
- vdevgone(cmaj, mn, mn + MAXPARTITIONS - 1, VCHR);
+ disk_gone(wdopen, self->dv_unit);
/* Get rid of the shutdown hook. */
if (sc->sc_sdhook != NULL)
@@ -686,25 +677,8 @@ wdopen(dev_t dev, int flag, int fmt, struct proc *p)
part = DISKPART(dev);
- /* Check that the partition exists. */
- if (part != RAW_PART &&
- (part >= wd->sc_dk.dk_label->d_npartitions ||
- wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
- error = ENXIO;
+ if ((error = disk_openpart(&wd->sc_dk, part, fmt, 1)) != 0)
goto bad;
- }
-
- /* Insure only one open at a time. */
- switch (fmt) {
- case S_IFCHR:
- wd->sc_dk.dk_copenmask |= (1 << part);
- break;
- case S_IFBLK:
- wd->sc_dk.dk_bopenmask |= (1 << part);
- break;
- }
- wd->sc_dk.dk_openmask =
- wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
disk_unlock(&wd->sc_dk);
device_unref(&wd->sc_dev);
@@ -735,16 +709,7 @@ wdclose(dev_t dev, int flag, int fmt, struct proc *p)
disk_lock_nointr(&wd->sc_dk);
- switch (fmt) {
- case S_IFCHR:
- wd->sc_dk.dk_copenmask &= ~(1 << part);
- break;
- case S_IFBLK:
- wd->sc_dk.dk_bopenmask &= ~(1 << part);
- break;
- }
- wd->sc_dk.dk_openmask =
- wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
+ disk_closepart(&wd->sc_dk, part, fmt);
if (wd->sc_dk.dk_openmask == 0) {
wd_flushcache(wd, 0);
diff --git a/sys/dev/ccd.c b/sys/dev/ccd.c
index cd517730e7f..dd678022a24 100644
--- a/sys/dev/ccd.c
+++ b/sys/dev/ccd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ccd.c,v 1.95 2011/06/05 18:40:33 matthew Exp $ */
+/* $OpenBSD: ccd.c,v 1.96 2011/06/30 16:28:05 matthew Exp $ */
/* $NetBSD: ccd.c,v 1.33 1996/05/05 04:21:14 thorpej Exp $ */
/*-
@@ -553,7 +553,7 @@ ccdopen(dev_t dev, int flags, int fmt, struct proc *p)
int unit = DISKUNIT(dev);
struct ccd_softc *cs;
struct disklabel *lp;
- int error = 0, part, pmask;
+ int error = 0, part;
CCD_DPRINTF(CCDB_FOLLOW, ("ccdopen(%x, %x)\n", dev, flags));
@@ -567,7 +567,6 @@ ccdopen(dev_t dev, int flags, int fmt, struct proc *p)
lp = cs->sc_dkdev.dk_label;
part = DISKPART(dev);
- pmask = (1 << part);
/*
* If we're initialized, check to see if there are any other
@@ -577,30 +576,9 @@ ccdopen(dev_t dev, int flags, int fmt, struct proc *p)
if ((cs->sc_flags & CCDF_INITED) && (cs->sc_dkdev.dk_openmask == 0))
ccdgetdisklabel(dev, cs, lp, 0);
- /* Check that the partition exists. */
- if (part != RAW_PART) {
- if (((cs->sc_flags & CCDF_INITED) == 0) ||
- ((part >= lp->d_npartitions) ||
- (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
- error = ENXIO;
- goto done;
- }
- }
-
- /* Prevent our unit from being unconfigured while open. */
- switch (fmt) {
- case S_IFCHR:
- cs->sc_dkdev.dk_copenmask |= pmask;
- break;
-
- case S_IFBLK:
- cs->sc_dkdev.dk_bopenmask |= pmask;
- break;
- }
- cs->sc_dkdev.dk_openmask =
- cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
+ error = disk_openpart(&cs->sc_dkdev, part, fmt,
+ (cs->sc_flags & CCDF_INITED) != 0);
- done:
ccdunlock(cs);
return (error);
}
@@ -624,18 +602,7 @@ ccdclose(dev_t dev, int flags, int fmt, struct proc *p)
part = DISKPART(dev);
- /* ...that much closer to allowing unconfiguration... */
- switch (fmt) {
- case S_IFCHR:
- cs->sc_dkdev.dk_copenmask &= ~(1 << part);
- break;
-
- case S_IFBLK:
- cs->sc_dkdev.dk_bopenmask &= ~(1 << part);
- break;
- }
- cs->sc_dkdev.dk_openmask =
- cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
+ disk_closepart(&cs->sc_dkdev, part, fmt);
ccdunlock(cs);
return (0);
diff --git a/sys/dev/rd.c b/sys/dev/rd.c
index c6a318e5aee..dccbd679e32 100644
--- a/sys/dev/rd.c
+++ b/sys/dev/rd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rd.c,v 1.3 2011/06/23 17:06:07 matthew Exp $ */
+/* $OpenBSD: rd.c,v 1.4 2011/06/30 16:28:05 matthew Exp $ */
/*
* Copyright (c) 2011 Matthew Dempsky <matthew@dempsky.org>
@@ -136,17 +136,8 @@ int
rd_detach(struct device *self, int flags)
{
struct rd_softc *sc = (struct rd_softc *)self;
- int bmaj, cmaj, mn;
- /* Locate the lowest minor number to be detached. */
- mn = DISKMINOR(self->dv_unit, 0);
-
- for (bmaj = 0; bmaj < nblkdev; bmaj++)
- if (bdevsw[bmaj].d_open == rdopen)
- vdevgone(bmaj, mn, mn + MAXPARTITIONS - 1, VBLK);
- for (cmaj = 0; cmaj < nchrdev; cmaj++)
- if (cdevsw[cmaj].d_open == rdopen)
- vdevgone(cmaj, mn, mn + MAXPARTITIONS - 1, VCHR);
+ disk_gone(rdopen, self->dv_unit);
/* Detach disk. */
disk_detach(&sc->sc_dk);
@@ -178,23 +169,7 @@ rdopen(dev_t dev, int flag, int fmt, struct proc *p)
goto unlock;
}
- /* Check that the partition exists. */
- if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions ||
- sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
- error = ENXIO;
- goto unlock;
- }
-
- /* Ensure the partition doesn't get changed under our feet. */
- switch (fmt) {
- case S_IFCHR:
- sc->sc_dk.dk_copenmask |= (1 << part);
- break;
- case S_IFBLK:
- sc->sc_dk.dk_bopenmask |= (1 << part);
- break;
- }
- sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
+ error = disk_openpart(&sc->sc_dk, part, fmt, 1);
unlock:
disk_unlock(&sc->sc_dk);
@@ -218,15 +193,7 @@ rdclose(dev_t dev, int flag, int fmt, struct proc *p)
disk_lock_nointr(&sc->sc_dk);
- switch (fmt) {
- case S_IFCHR:
- sc->sc_dk.dk_copenmask &= ~(1 << part);
- break;
- case S_IFBLK:
- sc->sc_dk.dk_bopenmask &= ~(1 << part);
- break;
- }
- sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
+ disk_closepart(&sc->sc_dk, part, fmt);
disk_unlock(&sc->sc_dk);
device_unref(&sc->sc_dev);
diff --git a/sys/dev/vnd.c b/sys/dev/vnd.c
index 385ea386994..cb17d88f6fe 100644
--- a/sys/dev/vnd.c
+++ b/sys/dev/vnd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vnd.c,v 1.133 2011/06/21 01:47:15 deraadt Exp $ */
+/* $OpenBSD: vnd.c,v 1.134 2011/06/30 16:28:05 matthew Exp $ */
/* $NetBSD: vnd.c,v 1.26 1996/03/30 23:06:11 christos Exp $ */
/*
@@ -181,7 +181,7 @@ vndopen(dev_t dev, int flags, int mode, struct proc *p)
{
int unit = vndunit(dev);
struct vnd_softc *sc;
- int error = 0, part, pmask;
+ int error = 0, part;
DNPRINTF(VDB_FOLLOW, "vndopen(%x, %x, %x, %p)\n", dev, flags, mode, p);
@@ -204,29 +204,9 @@ vndopen(dev_t dev, int flags, int mode, struct proc *p)
}
part = DISKPART(dev);
- pmask = 1 << part;
-
- /* Check that the partition exists. */
- if (part != RAW_PART &&
- ((sc->sc_flags & VNF_HAVELABEL) == 0 ||
- part >= sc->sc_dk.dk_label->d_npartitions ||
- sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
- error = ENXIO;
- goto bad;
- }
- /* Prevent our unit from being unconfigured while open. */
- switch (mode) {
- case S_IFCHR:
- sc->sc_dk.dk_copenmask |= pmask;
- break;
-
- case S_IFBLK:
- sc->sc_dk.dk_bopenmask |= pmask;
- break;
- }
- sc->sc_dk.dk_openmask =
- sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
+ error = disk_openpart(&sc->sc_dk, part, mode,
+ (sc->sc_flags & VNF_HAVELABEL) != 0);
bad:
disk_unlock(&sc->sc_dk);
@@ -280,18 +260,7 @@ vndclose(dev_t dev, int flags, int mode, struct proc *p)
part = DISKPART(dev);
- /* ...that much closer to allowing unconfiguration... */
- switch (mode) {
- case S_IFCHR:
- sc->sc_dk.dk_copenmask &= ~(1 << part);
- break;
-
- case S_IFBLK:
- sc->sc_dk.dk_bopenmask &= ~(1 << part);
- break;
- }
- sc->sc_dk.dk_openmask =
- sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
+ disk_closepart(&sc->sc_dk, part, mode);
disk_unlock(&sc->sc_dk);
return (0);
diff --git a/sys/kern/subr_disk.c b/sys/kern/subr_disk.c
index 2ddf1747d95..dd6f19b826d 100644
--- a/sys/kern/subr_disk.c
+++ b/sys/kern/subr_disk.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: subr_disk.c,v 1.126 2011/06/19 04:53:17 matthew Exp $ */
+/* $OpenBSD: subr_disk.c,v 1.127 2011/06/30 16:28:05 matthew Exp $ */
/* $NetBSD: subr_disk.c,v 1.17 1996/03/16 23:17:08 christos Exp $ */
/*
@@ -896,6 +896,63 @@ disk_detach(struct disk *diskp)
panic("disk_detach: disk_count < 0");
}
+int
+disk_openpart(struct disk *dk, int part, int fmt, int haslabel)
+{
+ KASSERT(part >= 0 && part < MAXPARTITIONS);
+
+ /* Unless opening the raw partition, check that the partition exists. */
+ if (part != RAW_PART && (!haslabel ||
+ part >= dk->dk_label->d_npartitions ||
+ dk->dk_label->d_partitions[part].p_fstype == FS_UNUSED))
+ return (ENXIO);
+
+ /* Ensure the partition doesn't get changed under our feet. */
+ switch (fmt) {
+ case S_IFCHR:
+ dk->dk_copenmask |= (1 << part);
+ break;
+ case S_IFBLK:
+ dk->dk_bopenmask |= (1 << part);
+ break;
+ }
+ dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
+
+ return (0);
+}
+
+void
+disk_closepart(struct disk *dk, int part, int fmt)
+{
+ KASSERT(part >= 0 && part < MAXPARTITIONS);
+
+ switch (fmt) {
+ case S_IFCHR:
+ dk->dk_copenmask &= ~(1 << part);
+ break;
+ case S_IFBLK:
+ dk->dk_bopenmask &= ~(1 << part);
+ break;
+ }
+ dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
+}
+
+void
+disk_gone(int (*open)(dev_t, int, int, struct proc *), int unit)
+{
+ int bmaj, cmaj, mn;
+
+ /* Locate the lowest minor number to be detached. */
+ mn = DISKMINOR(unit, 0);
+
+ for (bmaj = 0; bmaj < nblkdev; bmaj++)
+ if (bdevsw[bmaj].d_open == open)
+ vdevgone(bmaj, mn, mn + MAXPARTITIONS - 1, VBLK);
+ for (cmaj = 0; cmaj < nchrdev; cmaj++)
+ if (cdevsw[cmaj].d_open == open)
+ vdevgone(cmaj, mn, mn + MAXPARTITIONS - 1, VCHR);
+}
+
/*
* Increment a disk's busy counter. If the counter is going from
* 0 to 1, set the timestamp.
diff --git a/sys/scsi/cd.c b/sys/scsi/cd.c
index f0f61b20703..7c2e5f1d9e0 100644
--- a/sys/scsi/cd.c
+++ b/sys/scsi/cd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cd.c,v 1.204 2011/06/19 04:55:34 deraadt Exp $ */
+/* $OpenBSD: cd.c,v 1.205 2011/06/30 16:28:05 matthew Exp $ */
/* $NetBSD: cd.c,v 1.100 1997/04/02 02:29:30 mycroft Exp $ */
/*
@@ -264,19 +264,10 @@ int
cddetach(struct device *self, int flags)
{
struct cd_softc *sc = (struct cd_softc *)self;
- int bmaj, cmaj, mn;
bufq_drain(&sc->sc_bufq);
- /* Locate the lowest minor number to be detached. */
- mn = DISKMINOR(self->dv_unit, 0);
-
- for (bmaj = 0; bmaj < nblkdev; bmaj++)
- if (bdevsw[bmaj].d_open == cdopen)
- vdevgone(bmaj, mn, mn + MAXPARTITIONS - 1, VBLK);
- for (cmaj = 0; cmaj < nchrdev; cmaj++)
- if (cdevsw[cmaj].d_open == cdopen)
- vdevgone(cmaj, mn, mn + MAXPARTITIONS - 1, VCHR);
+ disk_gone(cdopen, self->dv_unit);
/* Detach disk. */
bufq_destroy(&sc->sc_bufq);
@@ -379,23 +370,10 @@ cdopen(dev_t dev, int flag, int fmt, struct proc *p)
SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel fabricated\n"));
}
- /* Check that the partition exists. */
- if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions ||
- sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
- error = ENXIO;
+out:
+ if ((error = disk_openpart(&sc->sc_dk, part, fmt, 1)) != 0)
goto bad;
- }
-out: /* Insure only one open at a time. */
- switch (fmt) {
- case S_IFCHR:
- sc->sc_dk.dk_copenmask |= (1 << part);
- break;
- case S_IFBLK:
- sc->sc_dk.dk_bopenmask |= (1 << part);
- break;
- }
- sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
sc_link->flags |= SDEV_OPEN;
SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
@@ -433,15 +411,7 @@ cdclose(dev_t dev, int flag, int fmt, struct proc *p)
disk_lock_nointr(&sc->sc_dk);
- switch (fmt) {
- case S_IFCHR:
- sc->sc_dk.dk_copenmask &= ~(1 << part);
- break;
- case S_IFBLK:
- sc->sc_dk.dk_bopenmask &= ~(1 << part);
- break;
- }
- sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
+ disk_closepart(&sc->sc_dk, part, fmt);
if (sc->sc_dk.dk_openmask == 0) {
/* XXXX Must wait for I/O to complete! */
diff --git a/sys/scsi/sd.c b/sys/scsi/sd.c
index b3228c478a7..58ca2ff5950 100644
--- a/sys/scsi/sd.c
+++ b/sys/scsi/sd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sd.c,v 1.230 2011/06/19 04:55:34 deraadt Exp $ */
+/* $OpenBSD: sd.c,v 1.231 2011/06/30 16:28:05 matthew Exp $ */
/* $NetBSD: sd.c,v 1.111 1997/04/02 02:29:41 mycroft Exp $ */
/*-
@@ -297,19 +297,10 @@ int
sddetach(struct device *self, int flags)
{
struct sd_softc *sc = (struct sd_softc *)self;
- int bmaj, cmaj, mn;
bufq_drain(&sc->sc_bufq);
- /* Locate the lowest minor number to be detached. */
- mn = DISKMINOR(self->dv_unit, 0);
-
- for (bmaj = 0; bmaj < nblkdev; bmaj++)
- if (bdevsw[bmaj].d_open == sdopen)
- vdevgone(bmaj, mn, mn + MAXPARTITIONS - 1, VBLK);
- for (cmaj = 0; cmaj < nchrdev; cmaj++)
- if (cdevsw[cmaj].d_open == sdopen)
- vdevgone(cmaj, mn, mn + MAXPARTITIONS - 1, VCHR);
+ disk_gone(sdopen, self->dv_unit);
/* Get rid of the shutdown hook. */
if (sc->sc_sdhook != NULL)
@@ -428,24 +419,10 @@ sdopen(dev_t dev, int flag, int fmt, struct proc *p)
SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded\n"));
}
- /* Check that the partition exists. */
- if (part != RAW_PART &&
- (part >= sc->sc_dk.dk_label->d_npartitions ||
- sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
- error = ENXIO;
+out:
+ if ((error = disk_openpart(&sc->sc_dk, part, fmt, 1)) != 0)
goto bad;
- }
-out: /* Insure only one open at a time. */
- switch (fmt) {
- case S_IFCHR:
- sc->sc_dk.dk_copenmask |= (1 << part);
- break;
- case S_IFBLK:
- sc->sc_dk.dk_bopenmask |= (1 << part);
- break;
- }
- sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
/* It's OK to fall through because dk_openmask is now non-zero. */
@@ -483,15 +460,7 @@ sdclose(dev_t dev, int flag, int fmt, struct proc *p)
disk_lock_nointr(&sc->sc_dk);
- switch (fmt) {
- case S_IFCHR:
- sc->sc_dk.dk_copenmask &= ~(1 << part);
- break;
- case S_IFBLK:
- sc->sc_dk.dk_bopenmask &= ~(1 << part);
- break;
- }
- sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
+ disk_closepart(&sc->sc_dk, part, fmt);
if (sc->sc_dk.dk_openmask == 0) {
if ((sc->flags & SDF_DIRTY) != 0)
diff --git a/sys/sys/disk.h b/sys/sys/disk.h
index b4b02a50d9f..5766fffd1b9 100644
--- a/sys/sys/disk.h
+++ b/sys/sys/disk.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: disk.h,v 1.30 2011/06/19 04:51:06 matthew Exp $ */
+/* $OpenBSD: disk.h,v 1.31 2011/06/30 16:28:05 matthew Exp $ */
/* $NetBSD: disk.h,v 1.11 1996/04/28 20:22:50 thorpej Exp $ */
/*
@@ -154,6 +154,9 @@ void disk_init(void);
int disk_construct(struct disk *);
void disk_attach(struct device *, struct disk *);
void disk_detach(struct disk *);
+int disk_openpart(struct disk *, int, int, int);
+void disk_closepart(struct disk *, int, int);
+void disk_gone(int (*)(dev_t, int, int, struct proc *), int);
void disk_busy(struct disk *);
void disk_unbusy(struct disk *, long, int);