diff options
author | Kenneth R Westerback <krw@cvs.openbsd.org> | 2015-08-31 15:47:52 +0000 |
---|---|---|
committer | Kenneth R Westerback <krw@cvs.openbsd.org> | 2015-08-31 15:47:52 +0000 |
commit | d2a4baea36d7e707a34651f34a92fc8da7340eff (patch) | |
tree | 9a82058b5b10c99f4bcfac71cbcc32ab6dfbbc8a /sys | |
parent | d3c11f3bb1146acfc735d258c38aed6046da58bb (diff) |
Abstract 5 identical code blocks into a readdisksector() function.
Cleaner, clearer and less error prone.
Tested by bmercer@ as part of a larger diff, of which this is the
last part.
reads ok to jsing@ kettenis@. ok deraadt@.
Diffstat (limited to 'sys')
-rw-r--r-- | sys/kern/subr_disk.c | 73 |
1 files changed, 31 insertions, 42 deletions
diff --git a/sys/kern/subr_disk.c b/sys/kern/subr_disk.c index b0ba7504eba..847fd1f7898 100644 --- a/sys/kern/subr_disk.c +++ b/sys/kern/subr_disk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: subr_disk.c,v 1.196 2015/08/31 14:02:36 krw Exp $ */ +/* $OpenBSD: subr_disk.c,v 1.197 2015/08/31 15:47:51 krw Exp $ */ /* $NetBSD: subr_disk.c,v 1.17 1996/03/16 23:17:08 christos Exp $ */ /* @@ -103,6 +103,9 @@ struct disk_attach_task { void disk_attach_callback(void *); +int readdisksector(struct buf *, void (*)(struct buf *), struct disklabel *, + u_int64_t); + /* * Compute checksum for disk label. */ @@ -291,6 +294,24 @@ checkdisklabel(void *rlp, struct disklabel *lp, u_int64_t boundstart, } /* + * Read a disk sector. + */ +int +readdisksector(struct buf *bp, void (*strat)(struct buf *), + struct disklabel *lp, u_int64_t sector) +{ + bp->b_blkno = DL_SECTOBLK(lp, sector); + bp->b_bcount = lp->d_secsize; + bp->b_error = 0; + CLR(bp->b_flags, B_READ | B_WRITE | B_DONE | B_ERROR); + SET(bp->b_flags, B_BUSY | B_READ | B_RAW); + + (*strat)(bp); + + return (biowait(bp)); +} + +/* * If dos partition table requested, attempt to load it and * find disklabel inside a DOS partition. Return buffer * for use in signalling errors if requested. @@ -328,13 +349,7 @@ readdoslabel(struct buf *bp, void (*strat)(struct buf *), sector = extoff; /* read MBR/EBR */ - bp->b_blkno = DL_SECTOBLK(lp, sector); - bp->b_bcount = lp->d_secsize; - bp->b_error = 0; /* B_ERROR and b_error may have stale data. */ - CLR(bp->b_flags, B_READ | B_WRITE | B_DONE | B_ERROR); - SET(bp->b_flags, B_BUSY | B_READ | B_RAW); - (*strat)(bp); - error = biowait(bp); + error = readdisksector(bp, strat, lp, sector); if (error) { /*wrong*/ if (partoffp) /*wrong*/ *partoffp = -1; @@ -526,14 +541,9 @@ notfat: if (spoofonly) return (0); - bp->b_blkno = DL_SECTOBLK(lp, dospartoff + DL_BLKTOSEC(lp, - DOS_LABELSECTOR)); - bp->b_bcount = lp->d_secsize; - bp->b_error = 0; /* B_ERROR and b_error may have stale data. */ - CLR(bp->b_flags, B_READ | B_WRITE | B_DONE | B_ERROR); - SET(bp->b_flags, B_BUSY | B_READ | B_RAW); - (*strat)(bp); - if (biowait(bp)) + error = readdisksector(bp, strat, lp, dospartoff + + DL_BLKTOSEC(lp, DOS_LABELSECTOR)); + if (error) return (bp->b_error); offset = DL_BLKOFFSET(lp, DOS_LABELSECTOR); @@ -652,15 +662,7 @@ readgptlabel(struct buf *bp, void (*strat)(struct buf *), uint32_t ghpartnum; uint32_t ghpartspersec; - /* read header record */ - bp->b_blkno = DL_SECTOBLK(lp, sector); - bp->b_bcount = lp->d_secsize; - bp->b_error = 0; /* B_ERROR and b_error may have stale data. */ - CLR(bp->b_flags, B_READ | B_WRITE | B_DONE | B_ERROR); - SET(bp->b_flags, B_BUSY | B_READ | B_RAW); - (*strat)(bp); - error = biowait(bp); - + error = readdisksector(bp, strat, lp, sector); if (error) { DPRINTF("error reading from disk\n"); /*wrong*/ if (partoffp) @@ -739,15 +741,7 @@ readgptlabel(struct buf *bp, void (*strat)(struct buf *), */ sector = letoh64(gh.gh_part_lba); for (i = 0; i < ghpartnum / ghpartspersec; i++, sector++) { - /* read partition record */ - bp->b_blkno = DL_SECTOBLK(lp, sector); - bp->b_bcount = lp->d_secsize; - /* B_ERROR and b_error may have stale data. */ - bp->b_error = 0; - CLR(bp->b_flags, B_READ | B_WRITE | B_DONE | B_ERROR); - SET(bp->b_flags, B_BUSY | B_READ | B_RAW); - (*strat)(bp); - error = biowait(bp); + error = readdisksector(bp, strat, lp, sector); if (error) { /*wrong*/ if (partoffp) /*wrong*/ *partoffp = -1; @@ -840,14 +834,9 @@ readgptlabel(struct buf *bp, void (*strat)(struct buf *), if (spoofonly) return (0); - bp->b_blkno = DL_SECTOBLK(lp, gptpartoff + DL_BLKTOSEC(lp, - DOS_LABELSECTOR)); - bp->b_bcount = lp->d_secsize; - bp->b_error = 0; /* B_ERROR and b_error may have stale data. */ - CLR(bp->b_flags, B_READ | B_WRITE | B_DONE | B_ERROR); - SET(bp->b_flags, B_BUSY | B_READ | B_RAW); - (*strat)(bp); - if (biowait(bp)) + error = readdisksector(bp, strat, lp, gptpartoff + + DL_BLKTOSEC(lp, DOS_LABELSECTOR)); + if (error) return (bp->b_error); offset = DL_BLKOFFSET(lp, DOS_LABELSECTOR); |