From e1d640230c81b9cd56db8aefc4986c8a29148312 Mon Sep 17 00:00:00 2001 From: matthew Date: Thu, 30 Jun 2011 16:28:05 +0000 Subject: 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 --- sys/kern/subr_disk.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) (limited to 'sys/kern/subr_disk.c') 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. -- cgit v1.2.3-59-g8ed1b