xref: /dragonfly/sys/kern/subr_disklabel32.c (revision c69bf40f)
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/proc.h>
94 #include <sys/sysctl.h>
95 #include <sys/buf.h>
96 #include <sys/conf.h>
97 #include <sys/disklabel.h>
98 #include <sys/disklabel32.h>
99 #include <sys/diskslice.h>
100 #include <sys/disk.h>
101 #include <sys/dtype.h>		/* DTYPE_* constants */
102 #include <machine/md_var.h>
103 #include <sys/ctype.h>
104 #include <sys/syslog.h>
105 #include <sys/device.h>
106 #include <sys/msgport.h>
107 #include <sys/msgport2.h>
108 #include <sys/buf2.h>
109 
110 #include <vfs/ufs/dinode.h>	/* XXX used only for fs.h */
111 #include <vfs/ufs/fs.h>		/* XXX used only to get BBSIZE/SBSIZE */
112 
113 static void partition_info(const char *sname, int part, struct partition32 *pp);
114 static void slice_info(const char *sname, struct diskslice *sp);
115 static const char *l32_fixlabel(const char *sname, struct diskslice *sp,
116 				disklabel_t lpx, int writeflag);
117 
118 /*
119  * Retrieve the partition start and extent, in blocks.  Return 0 on success,
120  * EINVAL on error.
121  */
122 static int
123 l32_getpartbounds(struct diskslices *ssp, disklabel_t lp, u_int32_t part,
124 		  u_int64_t *start, u_int64_t *blocks)
125 {
126 	struct partition32 *pp;
127 
128 	if (part >= lp.lab32->d_npartitions)
129 		return (EINVAL);
130 	pp = &lp.lab32->d_partitions[part];
131 	*start = pp->p_offset;
132 	*blocks = pp->p_size;
133 	return(0);
134 }
135 
136 static void
137 l32_loadpartinfo(disklabel_t lp, u_int32_t part, struct partinfo *dpart)
138 {
139 	struct partition32 *pp;
140 	const size_t uuid_size = sizeof(struct uuid);
141 
142 	bzero(&dpart->fstype_uuid, uuid_size);
143 	bzero(&dpart->storage_uuid, uuid_size);
144 	if (part < lp.lab32->d_npartitions) {
145 		pp = &lp.lab32->d_partitions[part];
146 		dpart->fstype = pp->p_fstype;
147 	} else {
148 		dpart->fstype = 0;
149 	}
150 }
151 
152 static u_int32_t
153 l32_getnumparts(disklabel_t lp)
154 {
155 	return(lp.lab32->d_npartitions);
156 }
157 
158 static void
159 l32_freedisklabel(disklabel_t *lpp)
160 {
161 	kfree((*lpp).lab32, M_DEVBUF);
162 	(*lpp).lab32 = NULL;
163 }
164 
165 /*
166  * Attempt to read a disk label from a device.
167  *
168  * Returns NULL on sucess, and an error string on failure
169  */
170 static const char *
171 l32_readdisklabel(cdev_t dev, struct diskslice *sp, disklabel_t *lpp,
172 		struct disk_info *info)
173 {
174 	disklabel_t lpx;
175 	struct buf *bp;
176 	struct disklabel32 *dlp;
177 	const char *msg = NULL;
178 	int secsize = info->d_media_blksize;
179 
180 	bp = getpbuf_mem(NULL);
181 	KKASSERT(secsize <= bp->b_bufsize);
182 	bp->b_bio1.bio_offset = (off_t)LABELSECTOR32 * secsize;
183 	bp->b_bio1.bio_done = biodone_sync;
184 	bp->b_bio1.bio_flags |= BIO_SYNC;
185 	bp->b_bcount = secsize;
186 	bp->b_flags &= ~B_INVAL;
187 	bp->b_cmd = BUF_CMD_READ;
188 	bp->b_flags |= B_FAILONDIS;
189 	dev_dstrategy(dev, &bp->b_bio1);
190 	if (biowait(&bp->b_bio1, "labrd"))
191 		msg = "I/O error";
192 	else for (dlp = (struct disklabel32 *)bp->b_data;
193 	    dlp <= (struct disklabel32 *)((char *)bp->b_data +
194 	    secsize - sizeof(*dlp));
195 	    dlp = (struct disklabel32 *)((char *)dlp + sizeof(long))) {
196 		if (dlp->d_magic != DISKMAGIC32 ||
197 		    dlp->d_magic2 != DISKMAGIC32) {
198 			/*
199 			 * NOTE! dsreadandsetlabel() does a strcmp() on
200 			 * this string.
201 			 */
202 			if (msg == NULL)
203 				msg = "no disk label";
204 		} else if (dlp->d_npartitions > MAXPARTITIONS32 ||
205 			   dkcksum32(dlp) != 0) {
206 			msg = "disk label corrupted";
207 		} else {
208 			lpx.lab32 = dlp;
209 			msg = l32_fixlabel(NULL, sp, lpx, FALSE);
210 			if (msg == NULL) {
211 				(*lpp).lab32 = kmalloc(sizeof(*dlp),
212 						       M_DEVBUF, M_WAITOK|M_ZERO);
213 				*(*lpp).lab32 = *dlp;
214 			}
215 			break;
216 		}
217 	}
218 	bp->b_flags |= B_INVAL | B_AGE;
219 	relpbuf(bp, NULL);
220 
221 	return (msg);
222 }
223 
224 /*
225  * Check new disk label for sensibility before setting it.
226  */
227 static int
228 l32_setdisklabel(disklabel_t olpx, disklabel_t nlpx, struct diskslices *ssp,
229 		 struct diskslice *sp, u_int32_t *openmask)
230 {
231 	struct disklabel32 *olp, *nlp;
232 	struct partition32 *opp, *npp;
233 	int part;
234 	int i;
235 
236 	olp = olpx.lab32;
237 	nlp = nlpx.lab32;
238 
239 	/*
240 	 * Check it is actually a disklabel we are looking at.
241 	 */
242 	if (nlp->d_magic != DISKMAGIC32 || nlp->d_magic2 != DISKMAGIC32 ||
243 	    dkcksum32(nlp) != 0)
244 		return (EINVAL);
245 
246 	/*
247 	 * For each partition that we think is open, check the new disklabel
248 	 * for compatibility.  Ignore special partitions (>= 128).
249 	 */
250 	i = 0;
251 	while (i < 128) {
252 		if (openmask[i >> 5] == 0) {
253 			i += 32;
254 			continue;
255 		}
256 		if ((openmask[i >> 5] & (1 << (i & 31))) == 0) {
257 			++i;
258 			continue;
259 		}
260 		if (nlp->d_npartitions <= i)
261 			return (EBUSY);
262 		opp = &olp->d_partitions[i];
263 		npp = &nlp->d_partitions[i];
264 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
265 			return (EBUSY);
266 		/*
267 		 * Copy internally-set partition information
268 		 * if new label doesn't include it.		XXX
269 		 * (If we are using it then we had better stay the same type)
270 		 * This is possibly dubious, as someone else noted (XXX)
271 		 */
272 		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
273 			npp->p_fstype = opp->p_fstype;
274 			npp->p_fsize = opp->p_fsize;
275 			npp->p_frag = opp->p_frag;
276 			npp->p_cpg = opp->p_cpg;
277 		}
278 		++i;
279 	}
280  	nlp->d_checksum = 0;
281  	nlp->d_checksum = dkcksum32(nlp);
282 	*olp = *nlp;
283 
284 	if (olp->d_partitions[RAW_PART].p_offset)
285 		return (EXDEV);
286 	if (olp->d_secperunit > sp->ds_size)
287 		return (ENOSPC);
288 	for (part = 0; part < olp->d_npartitions; ++part) {
289 		if (olp->d_partitions[part].p_size > sp->ds_size)
290 			return(ENOSPC);
291 	}
292 	return (0);
293 }
294 
295 /*
296  * Write disk label back to device after modification.
297  */
298 static int
299 l32_writedisklabel(cdev_t dev, struct diskslices *ssp, struct diskslice *sp,
300 		   disklabel_t lpx)
301 {
302 	struct disklabel32 *lp;
303 	struct disklabel32 *dlp;
304 	struct buf *bp;
305 	const char *msg;
306 	int error = 0;
307 
308 	lp = lpx.lab32;
309 
310 	if (lp->d_partitions[RAW_PART].p_offset != 0)
311 		return (EXDEV);			/* not quite right */
312 
313 	bp = getpbuf_mem(NULL);
314 	KKASSERT((int)lp->d_secsize <= bp->b_bufsize);
315 	bp->b_bio1.bio_offset = (off_t)LABELSECTOR32 * lp->d_secsize;
316 	bp->b_bio1.bio_done = biodone_sync;
317 	bp->b_bio1.bio_flags |= BIO_SYNC;
318 	bp->b_bcount = lp->d_secsize;
319 	bp->b_flags |= B_FAILONDIS;
320 
321 #if 1
322 	/*
323 	 * We read the label first to see if it's there,
324 	 * in which case we will put ours at the same offset into the block..
325 	 * (I think this is stupid [Julian])
326 	 * Note that you can't write a label out over a corrupted label!
327 	 * (also stupid.. how do you write the first one? by raw writes?)
328 	 */
329 	bp->b_flags &= ~B_INVAL;
330 	bp->b_cmd = BUF_CMD_READ;
331 	KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
332 	dev_dstrategy(dev, &bp->b_bio1);
333 	error = biowait(&bp->b_bio1, "labrd");
334 	if (error)
335 		goto done;
336 	for (dlp = (struct disklabel32 *)bp->b_data;
337 	    dlp <= (struct disklabel32 *)
338 	      ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
339 	    dlp = (struct disklabel32 *)((char *)dlp + sizeof(long))) {
340 		if (dlp->d_magic == DISKMAGIC32 &&
341 		    dlp->d_magic2 == DISKMAGIC32 && dkcksum32(dlp) == 0) {
342 			*dlp = *lp;
343 			lpx.lab32 = dlp;
344 			msg = l32_fixlabel(NULL, sp, lpx, TRUE);
345 			if (msg) {
346 				error = EINVAL;
347 			} else {
348 				bp->b_cmd = BUF_CMD_WRITE;
349 				bp->b_bio1.bio_done = biodone_sync;
350 				bp->b_bio1.bio_flags |= BIO_SYNC;
351 				KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
352 				dev_dstrategy(dev, &bp->b_bio1);
353 				error = biowait(&bp->b_bio1, "labwr");
354 			}
355 			goto done;
356 		}
357 	}
358 	error = ESRCH;
359 done:
360 #else
361 	bzero(bp->b_data, lp->d_secsize);
362 	dlp = (struct disklabel32 *)bp->b_data;
363 	*dlp = *lp;
364 	bp->b_flags &= ~B_INVAL;
365 	bp->b_cmd = BUF_CMD_WRITE;
366 	bp->b_bio1.bio_done = biodone_sync;
367 	bp->b_bio1.bio_flags |= BIO_SYNC;
368 	BUF_STRATEGY(bp, 1);
369 	error = biowait(&bp->b_bio1, "labwr");
370 #endif
371 	bp->b_flags |= B_INVAL | B_AGE;
372 	relpbuf(bp, NULL);
373 
374 	return (error);
375 }
376 
377 /*
378  * Create a disklabel based on a disk_info structure, initializing
379  * the appropriate fields and creating a raw partition that covers the
380  * whole disk.
381  *
382  * If a diskslice is passed, the label is truncated to the slice
383  */
384 static disklabel_t
385 l32_clone_label(struct disk_info *info, struct diskslice *sp)
386 {
387 	struct disklabel32 *lp;
388 	disklabel_t res;
389 
390 	lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK | M_ZERO);
391 	lp->d_nsectors = info->d_secpertrack;
392 	lp->d_ntracks = info->d_nheads;
393 	lp->d_secpercyl = info->d_secpercyl;
394 	lp->d_secsize = info->d_media_blksize;
395 
396 	if (sp)
397 		lp->d_secperunit = (u_int)sp->ds_size;
398 	else
399 		lp->d_secperunit = (u_int)info->d_media_blocks;
400 
401 	if (lp->d_typename[0] == '\0')
402 		strncpy(lp->d_typename, "amnesiac", sizeof(lp->d_typename));
403 	if (lp->d_packname[0] == '\0')
404 		strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
405 	if (lp->d_nsectors == 0)
406 		lp->d_nsectors = 32;
407 	if (lp->d_ntracks == 0)
408 		lp->d_ntracks = 64;
409 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
410 	lp->d_ncylinders = lp->d_secperunit / lp->d_secpercyl;
411 	if (lp->d_rpm == 0)
412 		lp->d_rpm = 3600;
413 	if (lp->d_interleave == 0)
414 		lp->d_interleave = 1;
415 	if (lp->d_npartitions < RAW_PART + 1)
416 		lp->d_npartitions = MAXPARTITIONS32;
417 	if (lp->d_bbsize == 0)
418 		lp->d_bbsize = BBSIZE;
419 	if (lp->d_sbsize == 0)
420 		lp->d_sbsize = SBSIZE;
421 
422 	/*
423 	 * Used by various devices to create a compatibility slice which
424 	 * allows us to mount root from devices which do not have a
425 	 * disklabel.  Particularly: CDs.
426 	 */
427 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
428 	if (info->d_dsflags & DSO_COMPATPARTA) {
429 		lp->d_partitions[0].p_size = lp->d_secperunit;
430 		lp->d_partitions[0].p_fstype = FS_OTHER;
431 	}
432 	lp->d_magic = DISKMAGIC32;
433 	lp->d_magic2 = DISKMAGIC32;
434 	lp->d_checksum = dkcksum32(lp);
435 	res.lab32 = lp;
436 	return (res);
437 }
438 
439 static void
440 l32_makevirginlabel(disklabel_t lpx, struct diskslices *ssp,
441 		    struct diskslice *sp, struct disk_info *info)
442 {
443 	struct disklabel32 *lp = lpx.lab32;
444 	struct partition32 *pp;
445 	disklabel_t template;
446 
447 	template = l32_clone_label(info, NULL);
448 	bcopy(template.opaque, lp, sizeof(struct disklabel32));
449 
450 	lp->d_magic = DISKMAGIC32;
451 	lp->d_magic2 = DISKMAGIC32;
452 
453 	lp->d_npartitions = MAXPARTITIONS32;
454 	if (lp->d_interleave == 0)
455 		lp->d_interleave = 1;
456 	if (lp->d_rpm == 0)
457 		lp->d_rpm = 3600;
458 	if (lp->d_nsectors == 0)	/* sectors per track */
459 		lp->d_nsectors = 32;
460 	if (lp->d_ntracks == 0)		/* heads */
461 		lp->d_ntracks = 64;
462 	lp->d_ncylinders = 0;
463 	lp->d_bbsize = BBSIZE;
464 	lp->d_sbsize = SBSIZE;
465 
466 	/*
467 	 * If the slice or GPT partition is really small we could
468 	 * wind up with an absurd calculation for ncylinders.
469 	 */
470 	while (lp->d_ncylinders < 4) {
471 		if (lp->d_ntracks > 1)
472 			lp->d_ntracks >>= 1;
473 		else if (lp->d_nsectors > 1)
474 			lp->d_nsectors >>= 1;
475 		else
476 			break;
477 		lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
478 		lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
479 	}
480 
481 	/*
482 	 * Set or Modify the partition sizes to accomodate the slice,
483 	 * since we started with a copy of the virgin label stored
484 	 * in the whole-disk-slice and we are probably not a
485 	 * whole-disk slice.
486 	 */
487 	lp->d_secperunit = sp->ds_size;
488 	pp = &lp->d_partitions[RAW_PART];
489 	pp->p_offset = 0;
490 	pp->p_size = lp->d_secperunit;
491 	if (info->d_dsflags & DSO_COMPATPARTA) {
492 		pp = &lp->d_partitions[0];
493 		pp->p_offset = 0;
494 		pp->p_size = lp->d_secperunit;
495 		pp->p_fstype = FS_OTHER;
496 	}
497 	lp->d_checksum = 0;
498 	lp->d_checksum = dkcksum32(lp);
499 
500 	kfree(template.opaque, M_DEVBUF);
501 }
502 
503 static const char *
504 l32_fixlabel(const char *sname, struct diskslice *sp,
505 	     disklabel_t lpx, int writeflag)
506 {
507 	struct disklabel32 *lp;
508 	struct partition32 *pp;
509 	u_int64_t start;
510 	u_int64_t end;
511 	u_int64_t offset;
512 	int part;
513 	int warned;
514 
515 	lp = lpx.lab32;
516 
517 	/* These errors "can't happen" so don't bother reporting details. */
518 	if (lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32)
519 		return ("fixlabel: invalid magic");
520 	if (dkcksum32(lp) != 0)
521 		return ("fixlabel: invalid checksum");
522 
523 	pp = &lp->d_partitions[RAW_PART];
524 
525 	/*
526 	 * What a mess.  For ages old backwards compatibility the disklabel
527 	 * on-disk stores absolute offsets instead of slice-relative offsets.
528 	 * So fix it up when reading, writing, or snooping.
529 	 *
530 	 * The in-core label is always slice-relative.
531 	 */
532 	if (writeflag) {
533 		start = 0;
534 		offset = sp->ds_offset;
535 	} else {
536 		start = sp->ds_offset;
537 		offset = -sp->ds_offset;
538 	}
539 	if (pp->p_offset != start) {
540 		if (sname != NULL) {
541 			kprintf(
542 "%s: rejecting BSD label: raw partition offset != slice offset\n",
543 			       sname);
544 			slice_info(sname, sp);
545 			partition_info(sname, RAW_PART, pp);
546 		}
547 		return ("fixlabel: raw partition offset != slice offset");
548 	}
549 	if (pp->p_size != sp->ds_size) {
550 		if (sname != NULL) {
551 			kprintf("%s: raw partition size != slice size\n", sname);
552 			slice_info(sname, sp);
553 			partition_info(sname, RAW_PART, pp);
554 		}
555 		if (pp->p_size > sp->ds_size) {
556 			if (sname == NULL)
557 				return ("fixlabel: raw partition size > slice size");
558 			kprintf("%s: truncating raw partition\n", sname);
559 			pp->p_size = sp->ds_size;
560 		}
561 	}
562 	end = start + sp->ds_size;
563 	if (start > end)
564 		return ("fixlabel: slice wraps");
565 	if (lp->d_secpercyl <= 0)
566 		return ("fixlabel: d_secpercyl <= 0");
567 	pp -= RAW_PART;
568 	warned = FALSE;
569 	for (part = 0; part < lp->d_npartitions; part++, pp++) {
570 		if (pp->p_offset != 0 || pp->p_size != 0) {
571 			if (pp->p_offset < start
572 			    || pp->p_offset + pp->p_size > end
573 			    || pp->p_offset + pp->p_size < pp->p_offset) {
574 				if (sname != NULL) {
575 					kprintf(
576 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
577 					       sname);
578 					if (!warned) {
579 						slice_info(sname, sp);
580 						warned = TRUE;
581 					}
582 					partition_info(sname, part, pp);
583 				}
584 				/* XXX else silently discard junk. */
585 				bzero(pp, sizeof *pp);
586 			} else {
587 				pp->p_offset += offset;
588 			}
589 		}
590 	}
591 	lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
592 	lp->d_secperunit = sp->ds_size;
593  	lp->d_checksum = 0;
594  	lp->d_checksum = dkcksum32(lp);
595 	return (NULL);
596 }
597 
598 /*
599  * Set the number of blocks at the beginning of the slice which have
600  * been reserved for label operations.  This area will be write-protected
601  * when accessed via the slice.
602  */
603 static void
604 l32_adjust_label_reserved(struct diskslices *ssp, int slice,
605 			  struct diskslice *sp)
606 {
607 	/*struct disklabel32 *lp = sp->ds_label.lab32;*/
608 	sp->ds_reserved = SBSIZE / ssp->dss_secsize;
609 }
610 
611 static void
612 partition_info(const char *sname, int part, struct partition32 *pp)
613 {
614 	kprintf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
615 		(u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
616 		(u_long)pp->p_size);
617 }
618 
619 static void
620 slice_info(const char *sname, struct diskslice *sp)
621 {
622 	kprintf("%s: start %llu, end %llu, size %llu\n", sname,
623 	       (long long)sp->ds_offset,
624 	       (long long)sp->ds_offset + sp->ds_size - 1,
625 	       (long long)sp->ds_size);
626 }
627 
628 struct disklabel_ops disklabel32_ops = {
629 	.labelsize = sizeof(struct disklabel32),
630 	.op_readdisklabel = l32_readdisklabel,
631 	.op_setdisklabel = l32_setdisklabel,
632 	.op_writedisklabel = l32_writedisklabel,
633 	.op_clone_label = l32_clone_label,
634 	.op_adjust_label_reserved = l32_adjust_label_reserved,
635 	.op_getpartbounds = l32_getpartbounds,
636 	.op_loadpartinfo = l32_loadpartinfo,
637 	.op_getnumparts = l32_getnumparts,
638 	.op_makevirginlabel = l32_makevirginlabel,
639 	.op_freedisklabel = l32_freedisklabel
640 };
641 
642