summaryrefslogtreecommitdiffstats
path: root/sys/kern/subr_disk.c
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/kern/subr_disk.c
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/kern/subr_disk.c')
-rw-r--r--sys/kern/subr_disk.c59
1 files changed, 58 insertions, 1 deletions
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.