xref: /dragonfly/sys/kern/subr_disklabel32.c (revision 7ff0fc30)
1 /*
2  * Copyright (c) 2003-2007 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * ----------------------------------------------------------------------------
35  * "THE BEER-WARE LICENSE" (Revision 42):
36  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
37  * can do whatever you want with this stuff. If we meet some day, and you think
38  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
39  * ----------------------------------------------------------------------------
40  *
41  * Copyright (c) 1982, 1986, 1988, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  * (c) UNIX System Laboratories, Inc.
44  * All or some portions of this file are derived from material licensed
45  * to the University of California by American Telephone and Telegraph
46  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47  * the permission of UNIX System Laboratories, Inc.
48  *
49  * Copyright (c) 1994 Bruce D. Evans.
50  * All rights reserved.
51  *
52  * Copyright (c) 1990 The Regents of the University of California.
53  * All rights reserved.
54  *
55  * This code is derived from software contributed to Berkeley by
56  * William Jolitz.
57  *
58  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
59  * All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  * 1. Redistributions of source code must retain the above copyright
65  *    notice, this list of conditions and the following disclaimer.
66  * 2. Redistributions in binary form must reproduce the above copyright
67  *    notice, this list of conditions and the following disclaimer in the
68  *    documentation and/or other materials provided with the distribution.
69  * 3. Neither the name of the University nor the names of its contributors
70  *    may be used to endorse or promote products derived from this software
71  *    without specific prior written permission.
72  *
73  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
74  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
77  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83  * SUCH DAMAGE.
84  *
85  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
86  * $FreeBSD: src/sys/kern/subr_disk.c,v 1.20.2.6 2001/10/05 07:14:57 peter Exp $
87  * $FreeBSD: src/sys/ufs/ufs/ufs_disksubr.c,v 1.44.2.3 2001/03/05 05:42:19 obrien Exp $
88  */
89 
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/kernel.h>
93 #include <sys/malloc.h>
94 #include <sys/proc.h>
95 #include <sys/sysctl.h>
96 #include <sys/buf.h>
97 #include <sys/conf.h>
98 #include <sys/disklabel.h>
99 #include <sys/disklabel32.h>
100 #include <sys/diskslice.h>
101 #include <sys/disk.h>
102 #include <sys/dtype.h>		/* DTYPE_* constants */
103 #include <machine/md_var.h>
104 #include <sys/ctype.h>
105 #include <sys/syslog.h>
106 #include <sys/device.h>
107 #include <sys/msgport.h>
108 #include <sys/msgport2.h>
109 #include <sys/buf2.h>
110 
111 #include <vfs/ufs/dinode.h>	/* XXX used only for fs.h */
112 #include <vfs/ufs/fs.h>		/* XXX used only to get BBSIZE/SBSIZE */
113 
114 static void partition_info(const char *sname, int part, struct partition32 *pp);
115 static void slice_info(const char *sname, struct diskslice *sp);
116 static const char *l32_fixlabel(const char *sname, struct diskslice *sp,
117 				disklabel_t lpx, int writeflag);
118 
119 /*
120  * Retrieve the partition start and extent, in blocks.  Return 0 on success,
121  * EINVAL on error.
122  */
123 static int
124 l32_getpartbounds(struct diskslices *ssp, disklabel_t lp, u_int32_t part,
125 		  u_int64_t *start, u_int64_t *blocks)
126 {
127 	struct partition32 *pp;
128 
129 	if (part >= lp.lab32->d_npartitions)
130 		return (EINVAL);
131 	pp = &lp.lab32->d_partitions[part];
132 	*start = pp->p_offset;
133 	*blocks = pp->p_size;
134 	return(0);
135 }
136 
137 static void
138 l32_loadpartinfo(disklabel_t lp, u_int32_t part, struct partinfo *dpart)
139 {
140 	struct partition32 *pp;
141 	const size_t uuid_size = sizeof(struct uuid);
142 
143 	bzero(&dpart->fstype_uuid, uuid_size);
144 	bzero(&dpart->storage_uuid, uuid_size);
145 	if (part < lp.lab32->d_npartitions) {
146 		pp = &lp.lab32->d_partitions[part];
147 		dpart->fstype = pp->p_fstype;
148 	} else {
149 		dpart->fstype = 0;
150 	}
151 }
152 
153 static u_int32_t
154 l32_getnumparts(disklabel_t lp)
155 {
156 	return(lp.lab32->d_npartitions);
157 }
158 
159 static int
160 l32_getpackname(disklabel_t lp, char *buf, size_t bytes)
161 {
162 	size_t slen;
163 
164 	if (lp.lab32->d_packname[0] == 0) {
165 		buf[0] = 0;
166 		return -1;
167 	}
168 	slen = strnlen(lp.lab32->d_packname, sizeof(lp.lab32->d_packname));
169 	if (slen >= bytes)
170 		slen = bytes - 1;
171 	bcopy(lp.lab32->d_packname, buf, slen);
172 	buf[slen] = 0;
173 
174 	return 0;
175 }
176 
177 static void
178 l32_freedisklabel(disklabel_t *lpp)
179 {
180 	kfree((*lpp).lab32, M_DEVBUF);
181 	(*lpp).lab32 = NULL;
182 }
183 
184 /*
185  * Attempt to read a disk label from a device.
186  *
187  * Returns NULL on sucess, and an error string on failure
188  */
189 static const char *
190 l32_readdisklabel(cdev_t dev, struct diskslice *sp, disklabel_t *lpp,
191 		struct disk_info *info)
192 {
193 	disklabel_t lpx;
194 	struct buf *bp;
195 	struct disklabel32 *dlp;
196 	const char *msg = NULL;
197 	int secsize = info->d_media_blksize;
198 
199 	bp = getpbuf_mem(NULL);
200 	KKASSERT(secsize <= bp->b_bufsize);
201 	bp->b_bio1.bio_offset = (off_t)LABELSECTOR32 * secsize;
202 	bp->b_bio1.bio_done = biodone_sync;
203 	bp->b_bio1.bio_flags |= BIO_SYNC;
204 	bp->b_bcount = secsize;
205 	bp->b_flags &= ~B_INVAL;
206 	bp->b_cmd = BUF_CMD_READ;
207 	bp->b_flags |= B_FAILONDIS;
208 	dev_dstrategy(dev, &bp->b_bio1);
209 	if (biowait(&bp->b_bio1, "labrd"))
210 		msg = "I/O error";
211 	else for (dlp = (struct disklabel32 *)bp->b_data;
212 	    dlp <= (struct disklabel32 *)((char *)bp->b_data +
213 	    secsize - sizeof(*dlp));
214 	    dlp = (struct disklabel32 *)((char *)dlp + sizeof(long))) {
215 		if (dlp->d_magic != DISKMAGIC32 ||
216 		    dlp->d_magic2 != DISKMAGIC32) {
217 			/*
218 			 * NOTE! dsreadandsetlabel() does a strcmp() on
219 			 * this string.
220 			 */
221 			if (msg == NULL)
222 				msg = "no disk label";
223 		} else if (dlp->d_npartitions > MAXPARTITIONS32 ||
224 			   dkcksum32(dlp) != 0) {
225 			msg = "disk label corrupted";
226 		} else {
227 			lpx.lab32 = dlp;
228 			msg = l32_fixlabel(NULL, sp, lpx, FALSE);
229 			if (msg == NULL) {
230 				(*lpp).lab32 = kmalloc(sizeof(*dlp),
231 						       M_DEVBUF, M_WAITOK|M_ZERO);
232 				*(*lpp).lab32 = *dlp;
233 			}
234 			break;
235 		}
236 	}
237 	bp->b_flags |= B_INVAL | B_AGE;
238 	relpbuf(bp, NULL);
239 
240 	return (msg);
241 }
242 
243 /*
244  * Check new disk label for sensibility before setting it.
245  */
246 static int
247 l32_setdisklabel(disklabel_t olpx, disklabel_t nlpx, struct diskslices *ssp,
248 		 struct diskslice *sp, u_int32_t *openmask)
249 {
250 	struct disklabel32 *olp, *nlp;
251 	struct partition32 *opp, *npp;
252 	int part;
253 	int i;
254 
255 	olp = olpx.lab32;
256 	nlp = nlpx.lab32;
257 
258 	/*
259 	 * Check it is actually a disklabel we are looking at.
260 	 */
261 	if (nlp->d_magic != DISKMAGIC32 || nlp->d_magic2 != DISKMAGIC32 ||
262 	    dkcksum32(nlp) != 0)
263 		return (EINVAL);
264 
265 	/*
266 	 * For each partition that we think is open, check the new disklabel
267 	 * for compatibility.  Ignore special partitions (>= 128).
268 	 */
269 	i = 0;
270 	while (i < 128) {
271 		if (openmask[i >> 5] == 0) {
272 			i += 32;
273 			continue;
274 		}
275 		if ((openmask[i >> 5] & (1 << (i & 31))) == 0) {
276 			++i;
277 			continue;
278 		}
279 		if (nlp->d_npartitions <= i)
280 			return (EBUSY);
281 		opp = &olp->d_partitions[i];
282 		npp = &nlp->d_partitions[i];
283 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
284 			return (EBUSY);
285 		/*
286 		 * Copy internally-set partition information
287 		 * if new label doesn't include it.		XXX
288 		 * (If we are using it then we had better stay the same type)
289 		 * This is possibly dubious, as someone else noted (XXX)
290 		 */
291 		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
292 			npp->p_fstype = opp->p_fstype;
293 			npp->p_fsize = opp->p_fsize;
294 			npp->p_frag = opp->p_frag;
295 			npp->p_cpg = opp->p_cpg;
296 		}
297 		++i;
298 	}
299  	nlp->d_checksum = 0;
300  	nlp->d_checksum = dkcksum32(nlp);
301 	*olp = *nlp;
302 
303 	if (olp->d_partitions[RAW_PART].p_offset)
304 		return (EXDEV);
305 	if (olp->d_secperunit > sp->ds_size)
306 		return (ENOSPC);
307 	for (part = 0; part < olp->d_npartitions; ++part) {
308 		if (olp->d_partitions[part].p_size > sp->ds_size)
309 			return(ENOSPC);
310 	}
311 	return (0);
312 }
313 
314 /*
315  * Write disk label back to device after modification.
316  */
317 static int
318 l32_writedisklabel(cdev_t dev, struct diskslices *ssp, struct diskslice *sp,
319 		   disklabel_t lpx)
320 {
321 	struct disklabel32 *lp;
322 	struct disklabel32 *dlp;
323 	struct buf *bp;
324 	const char *msg;
325 	int error = 0;
326 
327 	lp = lpx.lab32;
328 
329 	if (lp->d_partitions[RAW_PART].p_offset != 0)
330 		return (EXDEV);			/* not quite right */
331 
332 	bp = getpbuf_mem(NULL);
333 	KKASSERT((int)lp->d_secsize <= bp->b_bufsize);
334 	bp->b_bio1.bio_offset = (off_t)LABELSECTOR32 * lp->d_secsize;
335 	bp->b_bio1.bio_done = biodone_sync;
336 	bp->b_bio1.bio_flags |= BIO_SYNC;
337 	bp->b_bcount = lp->d_secsize;
338 	bp->b_flags |= B_FAILONDIS;
339 
340 #if 1
341 	/*
342 	 * We read the label first to see if it's there,
343 	 * in which case we will put ours at the same offset into the block..
344 	 * (I think this is stupid [Julian])
345 	 * Note that you can't write a label out over a corrupted label!
346 	 * (also stupid.. how do you write the first one? by raw writes?)
347 	 */
348 	bp->b_flags &= ~B_INVAL;
349 	bp->b_cmd = BUF_CMD_READ;
350 	KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
351 	dev_dstrategy(dev, &bp->b_bio1);
352 	error = biowait(&bp->b_bio1, "labrd");
353 	if (error)
354 		goto done;
355 	for (dlp = (struct disklabel32 *)bp->b_data;
356 	    dlp <= (struct disklabel32 *)
357 	      ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
358 	    dlp = (struct disklabel32 *)((char *)dlp + sizeof(long))) {
359 		if (dlp->d_magic == DISKMAGIC32 &&
360 		    dlp->d_magic2 == DISKMAGIC32 && dkcksum32(dlp) == 0) {
361 			*dlp = *lp;
362 			lpx.lab32 = dlp;
363 			msg = l32_fixlabel(NULL, sp, lpx, TRUE);
364 			if (msg) {
365 				error = EINVAL;
366 			} else {
367 				bp->b_cmd = BUF_CMD_WRITE;
368 				bp->b_bio1.bio_done = biodone_sync;
369 				bp->b_bio1.bio_flags |= BIO_SYNC;
370 				KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
371 				dev_dstrategy(dev, &bp->b_bio1);
372 				error = biowait(&bp->b_bio1, "labwr");
373 			}
374 			goto done;
375 		}
376 	}
377 	error = ESRCH;
378 done:
379 #else
380 	bzero(bp->b_data, lp->d_secsize);
381 	dlp = (struct disklabel32 *)bp->b_data;
382 	*dlp = *lp;
383 	bp->b_flags &= ~B_INVAL;
384 	bp->b_cmd = BUF_CMD_WRITE;
385 	bp->b_bio1.bio_done = biodone_sync;
386 	bp->b_bio1.bio_flags |= BIO_SYNC;
387 	BUF_STRATEGY(bp, 1);
388 	error = biowait(&bp->b_bio1, "labwr");
389 #endif
390 	bp->b_flags |= B_INVAL | B_AGE;
391 	relpbuf(bp, NULL);
392 
393 	return (error);
394 }
395 
396 /*
397  * Create a disklabel based on a disk_info structure, initializing
398  * the appropriate fields and creating a raw partition that covers the
399  * whole disk.
400  *
401  * If a diskslice is passed, the label is truncated to the slice
402  */
403 static disklabel_t
404 l32_clone_label(struct disk_info *info, struct diskslice *sp)
405 {
406 	struct disklabel32 *lp;
407 	disklabel_t res;
408 
409 	lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK | M_ZERO);
410 	lp->d_nsectors = info->d_secpertrack;
411 	lp->d_ntracks = info->d_nheads;
412 	lp->d_secpercyl = info->d_secpercyl;
413 	lp->d_secsize = info->d_media_blksize;
414 
415 	if (sp)
416 		lp->d_secperunit = (u_int)sp->ds_size;
417 	else
418 		lp->d_secperunit = (u_int)info->d_media_blocks;
419 
420 	if (lp->d_typename[0] == '\0')
421 		strncpy(lp->d_typename, "amnesiac", sizeof(lp->d_typename));
422 	if (lp->d_packname[0] == '\0')
423 		strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
424 	if (lp->d_nsectors == 0)
425 		lp->d_nsectors = 32;
426 	if (lp->d_ntracks == 0)
427 		lp->d_ntracks = 64;
428 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
429 	lp->d_ncylinders = lp->d_secperunit / lp->d_secpercyl;
430 	if (lp->d_rpm == 0)
431 		lp->d_rpm = 3600;
432 	if (lp->d_interleave == 0)
433 		lp->d_interleave = 1;
434 	if (lp->d_npartitions < RAW_PART + 1)
435 		lp->d_npartitions = MAXPARTITIONS32;
436 	if (lp->d_bbsize == 0)
437 		lp->d_bbsize = BBSIZE;
438 	if (lp->d_sbsize == 0)
439 		lp->d_sbsize = SBSIZE;
440 
441 	/*
442 	 * Used by various devices to create a compatibility slice which
443 	 * allows us to mount root from devices which do not have a
444 	 * disklabel.  Particularly: CDs.
445 	 */
446 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
447 	if (info->d_dsflags & DSO_COMPATPARTA) {
448 		lp->d_partitions[0].p_size = lp->d_secperunit;
449 		lp->d_partitions[0].p_fstype = FS_OTHER;
450 	}
451 	lp->d_magic = DISKMAGIC32;
452 	lp->d_magic2 = DISKMAGIC32;
453 	lp->d_checksum = dkcksum32(lp);
454 	res.lab32 = lp;
455 	return (res);
456 }
457 
458 static void
459 l32_makevirginlabel(disklabel_t lpx, struct diskslices *ssp,
460 		    struct diskslice *sp, struct disk_info *info)
461 {
462 	struct disklabel32 *lp = lpx.lab32;
463 	struct partition32 *pp;
464 	disklabel_t template;
465 
466 	template = l32_clone_label(info, NULL);
467 	bcopy(template.opaque, lp, sizeof(struct disklabel32));
468 
469 	lp->d_magic = DISKMAGIC32;
470 	lp->d_magic2 = DISKMAGIC32;
471 
472 	lp->d_npartitions = MAXPARTITIONS32;
473 	if (lp->d_interleave == 0)
474 		lp->d_interleave = 1;
475 	if (lp->d_rpm == 0)
476 		lp->d_rpm = 3600;
477 	if (lp->d_nsectors == 0)	/* sectors per track */
478 		lp->d_nsectors = 32;
479 	if (lp->d_ntracks == 0)		/* heads */
480 		lp->d_ntracks = 64;
481 	lp->d_ncylinders = 0;
482 	lp->d_bbsize = BBSIZE;
483 	lp->d_sbsize = SBSIZE;
484 
485 	/*
486 	 * If the slice or GPT partition is really small we could
487 	 * wind up with an absurd calculation for ncylinders.
488 	 */
489 	while (lp->d_ncylinders < 4) {
490 		if (lp->d_ntracks > 1)
491 			lp->d_ntracks >>= 1;
492 		else if (lp->d_nsectors > 1)
493 			lp->d_nsectors >>= 1;
494 		else
495 			break;
496 		lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
497 		lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
498 	}
499 
500 	/*
501 	 * Set or Modify the partition sizes to accomodate the slice,
502 	 * since we started with a copy of the virgin label stored
503 	 * in the whole-disk-slice and we are probably not a
504 	 * whole-disk slice.
505 	 */
506 	lp->d_secperunit = sp->ds_size;
507 	pp = &lp->d_partitions[RAW_PART];
508 	pp->p_offset = 0;
509 	pp->p_size = lp->d_secperunit;
510 	if (info->d_dsflags & DSO_COMPATPARTA) {
511 		pp = &lp->d_partitions[0];
512 		pp->p_offset = 0;
513 		pp->p_size = lp->d_secperunit;
514 		pp->p_fstype = FS_OTHER;
515 	}
516 	lp->d_checksum = 0;
517 	lp->d_checksum = dkcksum32(lp);
518 
519 	kfree(template.opaque, M_DEVBUF);
520 }
521 
522 static const char *
523 l32_fixlabel(const char *sname, struct diskslice *sp,
524 	     disklabel_t lpx, int writeflag)
525 {
526 	struct disklabel32 *lp;
527 	struct partition32 *pp;
528 	u_int64_t start;
529 	u_int64_t end;
530 	u_int64_t offset;
531 	int part;
532 	int warned;
533 
534 	lp = lpx.lab32;
535 
536 	/* These errors "can't happen" so don't bother reporting details. */
537 	if (lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32)
538 		return ("fixlabel: invalid magic");
539 	if (dkcksum32(lp) != 0)
540 		return ("fixlabel: invalid checksum");
541 
542 	pp = &lp->d_partitions[RAW_PART];
543 
544 	/*
545 	 * What a mess.  For ages old backwards compatibility the disklabel
546 	 * on-disk stores absolute offsets instead of slice-relative offsets.
547 	 * So fix it up when reading, writing, or snooping.
548 	 *
549 	 * The in-core label is always slice-relative.
550 	 */
551 	if (writeflag) {
552 		start = 0;
553 		offset = sp->ds_offset;
554 	} else {
555 		start = sp->ds_offset;
556 		offset = -sp->ds_offset;
557 	}
558 	if (pp->p_offset != start) {
559 		if (sname != NULL) {
560 			kprintf(
561 "%s: rejecting BSD label: raw partition offset != slice offset\n",
562 			       sname);
563 			slice_info(sname, sp);
564 			partition_info(sname, RAW_PART, pp);
565 		}
566 		return ("fixlabel: raw partition offset != slice offset");
567 	}
568 	if (pp->p_size != sp->ds_size) {
569 		if (sname != NULL) {
570 			kprintf("%s: raw partition size != slice size\n", sname);
571 			slice_info(sname, sp);
572 			partition_info(sname, RAW_PART, pp);
573 		}
574 		if (pp->p_size > sp->ds_size) {
575 			if (sname == NULL)
576 				return ("fixlabel: raw partition size > slice size");
577 			kprintf("%s: truncating raw partition\n", sname);
578 			pp->p_size = sp->ds_size;
579 		}
580 	}
581 	end = start + sp->ds_size;
582 	if (start > end)
583 		return ("fixlabel: slice wraps");
584 	if (lp->d_secpercyl <= 0)
585 		return ("fixlabel: d_secpercyl <= 0");
586 	pp -= RAW_PART;
587 	warned = FALSE;
588 	for (part = 0; part < lp->d_npartitions; part++, pp++) {
589 		if (pp->p_offset != 0 || pp->p_size != 0) {
590 			if (pp->p_offset < start
591 			    || pp->p_offset + pp->p_size > end
592 			    || pp->p_offset + pp->p_size < pp->p_offset) {
593 				if (sname != NULL) {
594 					kprintf(
595 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
596 					       sname);
597 					if (!warned) {
598 						slice_info(sname, sp);
599 						warned = TRUE;
600 					}
601 					partition_info(sname, part, pp);
602 				}
603 				/* XXX else silently discard junk. */
604 				bzero(pp, sizeof *pp);
605 			} else {
606 				pp->p_offset += offset;
607 			}
608 		}
609 	}
610 	lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
611 	lp->d_secperunit = sp->ds_size;
612  	lp->d_checksum = 0;
613  	lp->d_checksum = dkcksum32(lp);
614 	return (NULL);
615 }
616 
617 /*
618  * Set the number of blocks at the beginning of the slice which have
619  * been reserved for label operations.  This area will be write-protected
620  * when accessed via the slice.
621  */
622 static void
623 l32_adjust_label_reserved(struct diskslices *ssp, int slice,
624 			  struct diskslice *sp)
625 {
626 	/*struct disklabel32 *lp = sp->ds_label.lab32;*/
627 	sp->ds_reserved = SBSIZE / ssp->dss_secsize;
628 }
629 
630 static void
631 partition_info(const char *sname, int part, struct partition32 *pp)
632 {
633 	kprintf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
634 		(u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
635 		(u_long)pp->p_size);
636 }
637 
638 static void
639 slice_info(const char *sname, struct diskslice *sp)
640 {
641 	kprintf("%s: start %llu, end %llu, size %llu\n", sname,
642 	       (long long)sp->ds_offset,
643 	       (long long)sp->ds_offset + sp->ds_size - 1,
644 	       (long long)sp->ds_size);
645 }
646 
647 struct disklabel_ops disklabel32_ops = {
648 	.labelsize = sizeof(struct disklabel32),
649 	.op_readdisklabel = l32_readdisklabel,
650 	.op_setdisklabel = l32_setdisklabel,
651 	.op_writedisklabel = l32_writedisklabel,
652 	.op_clone_label = l32_clone_label,
653 	.op_adjust_label_reserved = l32_adjust_label_reserved,
654 	.op_getpartbounds = l32_getpartbounds,
655 	.op_loadpartinfo = l32_loadpartinfo,
656 	.op_getnumparts = l32_getnumparts,
657 	.op_makevirginlabel = l32_makevirginlabel,
658 	.op_getpackname = l32_getpackname,
659 	.op_freedisklabel = l32_freedisklabel
660 };
661 
662