1 /*	$NetBSD: disksubr.c,v 1.17 2013/01/18 14:04:16 kiyohara Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.17 2013/01/18 14:04:16 kiyohara Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/buf.h>
40 #include <sys/disklabel.h>
41 #include <sys/disk.h>
42 #include <sys/syslog.h>
43 
44 /*
45  * Attempt to read a disk label from a device
46  * using the indicated strategy routine.
47  * The label must be partly set up before this:
48  * secpercyl, secsize and anything required for a block i/o read
49  * operation in the driver's strategy/start routines
50  * must be filled in before calling us.
51  *
52  * If dos partition table requested, attempt to load it and
53  * find disklabel inside a DOS partition. Also, if bad block
54  * table needed, attempt to extract it as well. Return buffer
55  * for use in signalling errors if requested.
56  *
57  * Returns null on success and an error string on failure.
58  */
59 const char *
readdisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,struct cpu_disklabel * osdep)60 readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
61 	      struct cpu_disklabel *osdep)
62 {
63 	struct buf *bp;
64 	struct disklabel *dlp;
65 	struct dkbad *bdp;
66 	const char *msg = NULL;
67 	int i;
68 
69 	/* minimal requirements for archtypal disk label */
70 	if (lp->d_secsize == 0)
71 		lp->d_secsize = DEV_BSIZE;
72 	if (lp->d_secperunit == 0)
73 		lp->d_secperunit = 0x1fffffff;
74 	lp->d_npartitions = RAW_PART + 1;
75 	if (lp->d_partitions[RAW_PART].p_size == 0)
76 		lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
77 	lp->d_partitions[RAW_PART].p_offset = 0;
78 
79 	/* get a buffer and initialize it */
80 	bp = geteblk((int)lp->d_secsize);
81 
82 	/* next, dig out disk label */
83 	bp->b_dev = dev;
84 	bp->b_blkno = LABELSECTOR;
85 	bp->b_cylinder = 0;
86 	bp->b_bcount = lp->d_secsize;
87 	bp->b_flags |= B_READ;
88 	(*strat)(bp);
89 
90 	/* if successful, locate disk label within block and validate */
91 	if (biowait(bp)) {
92 		msg = "disk label I/O error";
93 		goto done;
94 	}
95 
96 	for (dlp = (struct disklabel *)bp->b_data;
97 	    dlp <= (struct disklabel *)((char *)bp->b_data + lp->d_secsize -
98 		sizeof(*dlp));
99 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
100 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
101 			if (msg == NULL)
102 				msg = "no disk label";
103 		} else if (dlp->d_npartitions > MAXPARTITIONS ||
104 			   dkcksum(dlp) != 0)
105 			msg = "disk label corrupted";
106 		else {
107 			*lp = *dlp;
108 			msg = NULL;
109 			break;
110 		}
111 	}
112 
113 	if (msg)
114 		goto done;
115 
116 	/* obtain bad sector table if requested and present */
117 	if (osdep && (lp->d_flags & D_BADSECT)) {
118 		struct dkbad *db;
119 
120 		bdp = &osdep->bad;
121 		i = 0;
122 		do {
123 			/* read a bad sector table */
124 			bp->b_oflags &= ~BO_DONE;
125 			bp->b_flags |= B_READ;
126 			bp->b_blkno = lp->d_secperunit - lp->d_nsectors + i;
127 			if (lp->d_secsize > DEV_BSIZE)
128 				bp->b_blkno *= lp->d_secsize / DEV_BSIZE;
129 			else
130 				bp->b_blkno /= DEV_BSIZE / lp->d_secsize;
131 			bp->b_bcount = lp->d_secsize;
132 			bp->b_cylinder = lp->d_ncylinders - 1;
133 			(*strat)(bp);
134 
135 			/* if successful, validate, otherwise try another */
136 			if (biowait(bp)) {
137 				msg = "bad sector table I/O error";
138 			} else {
139 				db = (struct dkbad *)(bp->b_data);
140 #define DKBAD_MAGIC 0x4321
141 				if (db->bt_mbz == 0
142 					&& db->bt_flag == DKBAD_MAGIC) {
143 					msg = NULL;
144 					*bdp = *db;
145 					break;
146 				} else
147 					msg = "bad sector table corrupted";
148 			}
149 		} while (bp->b_error != 0 && (i += 2) < 10 &&
150 			i < lp->d_nsectors);
151 	}
152 
153 done:
154 	brelse(bp, BC_INVAL);
155 	return (msg);
156 }
157 
158 /*
159  * Check new disk label for sensibility before setting it.
160  */
161 int
setdisklabel(struct disklabel * olp,struct disklabel * nlp,u_long openmask,struct cpu_disklabel * osdep)162 setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask,
163 	     struct cpu_disklabel *osdep)
164 {
165 	int i;
166 	struct partition *opp, *npp;
167 
168 	/* sanity clause */
169 	if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0 ||
170 	    (nlp->d_secsize % DEV_BSIZE) != 0)
171 		return(EINVAL);
172 
173 	/* special case to allow disklabel to be invalidated */
174 	if (nlp->d_magic == 0xffffffff) {
175 		*olp = *nlp;
176 		return (0);
177 	}
178 
179 	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
180 	    dkcksum(nlp) != 0)
181 		return (EINVAL);
182 
183 	while (openmask != 0) {
184 		i = ffs(openmask) - 1;
185 		openmask &= ~(1 << i);
186 		if (nlp->d_npartitions <= i)
187 			return (EBUSY);
188 		opp = &olp->d_partitions[i];
189 		npp = &nlp->d_partitions[i];
190 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
191 			return (EBUSY);
192 		/*
193 		 * Copy internally-set partition information
194 		 * if new label doesn't include it.		XXX
195 		 */
196 		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
197 			npp->p_fstype = opp->p_fstype;
198 			npp->p_fsize = opp->p_fsize;
199 			npp->p_frag = opp->p_frag;
200 			npp->p_cpg = opp->p_cpg;
201 		}
202 	}
203  	nlp->d_checksum = 0;
204  	nlp->d_checksum = dkcksum(nlp);
205 	*olp = *nlp;
206 	return (0);
207 }
208 
209 
210 /*
211  * Write disk label back to device after modification.
212  */
213 int
writedisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,struct cpu_disklabel * osdep)214 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
215 	       struct cpu_disklabel *osdep)
216 {
217 	struct buf *bp;
218 	struct disklabel *dlp;
219 	int error;
220 
221 #ifdef maybe
222 	/* disklabel in appropriate location? */
223 	if (lp->d_partitions[2].p_offset != 0
224 		&& lp->d_partitions[2].p_offset != dospartoff) {
225 		error = EXDEV;
226 		goto done;
227 	}
228 #endif
229 
230 	/* get a buffer and initialize it */
231 	bp = geteblk((int)lp->d_secsize);
232 	bp->b_dev = dev;
233 	bp->b_blkno = LABELSECTOR;
234 	bp->b_cylinder = 0;
235 	bp->b_bcount = lp->d_secsize;
236 	bp->b_flags |= B_READ;
237 	(*strat)(bp);
238 
239 	/* if successful, locate disk label within block and validate */
240 	if ((error = biowait(bp)) != 0)
241 		goto done;
242 	for (dlp = (struct disklabel *)bp->b_data;
243 	    dlp <= (struct disklabel *)((char *)bp->b_data + lp->d_secsize -
244 		sizeof(*dlp));
245 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
246 		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
247 		    dkcksum(dlp) == 0) {
248 			*dlp = *lp;
249 			bp->b_cflags = BC_BUSY;
250 			bp->b_flags = B_WRITE;
251 			CLR(bp->b_oflags, BO_DONE);
252 			(*strat)(bp);
253 			error = biowait(bp);
254 			goto done;
255 		}
256 	}
257 	error = ESRCH;
258 
259 done:
260 	brelse(bp, 0);
261 	return (error);
262 }
263