Annotation of sys/arch/aviion/aviion/disksubr.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: disksubr.c,v 1.39 2007/06/20 18:15:45 deraadt Exp $ */
2: /* $NetBSD: disksubr.c,v 1.21 1996/05/03 19:42:03 christos Exp $ */
3:
4: /*
5: * Copyright (c) 1996 Theo de Raadt
6: * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
7: * All rights reserved.
8: *
9: * Redistribution and use in source and binary forms, with or without
10: * modification, are permitted provided that the following conditions
11: * are met:
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: * 2. Redistributions in binary form must reproduce the above copyright
15: * notice, this list of conditions and the following disclaimer in the
16: * documentation and/or other materials provided with the distribution.
17: * 3. Neither the name of the University nor the names of its contributors
18: * may be used to endorse or promote products derived from this software
19: * without specific prior written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31: * SUCH DAMAGE.
32: */
33:
34: #include <sys/param.h>
35: #include <sys/systm.h>
36: #include <sys/buf.h>
37: #include <sys/disklabel.h>
38: #include <sys/disk.h>
39:
40: /*
41: * Attempt to read a disk label from a device
42: * using the indicated strategy routine.
43: * The label must be partly set up before this:
44: * secpercyl, secsize and anything required for a block i/o read
45: * operation in the driver's strategy/start routines
46: * must be filled in before calling us.
47: *
48: * If dos partition table requested, attempt to load it and
49: * find disklabel inside a DOS partition. Return buffer
50: * for use in signalling errors if requested.
51: *
52: * We would like to check if each MBR has a valid DOSMBR_SIGNATURE, but
53: * we cannot because it doesn't always exist. So.. we assume the
54: * MBR is valid.
55: *
56: * Returns null on success and an error string on failure.
57: */
58: char *
59: readdisklabel(dev_t dev, void (*strat)(struct buf *),
60: struct disklabel *lp, int spoofonly)
61: {
62: struct buf *bp = NULL;
63: char *msg;
64:
65: if ((msg = initdisklabel(lp)))
66: goto done;
67:
68: /* get a buffer and initialize it */
69: bp = geteblk((int)lp->d_secsize);
70: bp->b_dev = dev;
71:
72: msg = readdoslabel(bp, strat, lp, NULL, spoofonly);
73: if (msg == NULL)
74: goto done;
75:
76: #if defined(CD9660)
77: if (iso_disklabelspoof(dev, strat, lp) == 0) {
78: msg = NULL;
79: goto done;
80: }
81: #endif
82: #if defined(UDF)
83: if (udf_disklabelspoof(dev, strat, lp) == 0) {
84: msg = NULL;
85: goto done;
86: }
87: #endif
88:
89: done:
90: if (bp) {
91: bp->b_flags |= B_INVAL;
92: brelse(bp);
93: }
94: return (msg);
95: }
96:
97: /*
98: * Write disk label back to device after modification.
99: */
100: int
101: writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp)
102: {
103: int error = EIO, partoff = -1, cyl = 0;
104: struct disklabel *dlp;
105: struct buf *bp = NULL;
106:
107: /* get a buffer and initialize it */
108: bp = geteblk((int)lp->d_secsize);
109: bp->b_dev = dev;
110:
111: if (readdoslabel(bp, strat, lp, &partoff, 1) != NULL)
112: goto done;
113:
114: /* Read it in, slap the new label in, and write it back out */
115: bp->b_blkno = partoff + LABELSECTOR;
116: bp->b_bcount = lp->d_secsize;
117: bp->b_flags = B_BUSY | B_READ;
118: (*strat)(bp);
119: if ((error = biowait(bp)) != 0)
120: goto done;
121:
122: dlp = (struct disklabel *)(bp->b_data + LABELOFFSET);
123: *dlp = *lp;
124: bp->b_flags = B_BUSY | B_WRITE;
125: (*strat)(bp);
126: error = biowait(bp);
127:
128: done:
129: if (bp) {
130: bp->b_flags |= B_INVAL;
131: brelse(bp);
132: }
133: return (error);
134: }
CVSweb