DragonFlyBSD Kernel Audit
sys/kern/subr_disklabel32.c
← back
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/*
 * Copyright (c) 2003-2007 The DragonFly Project.  All rights reserved.
 *
 * This code is derived from software contributed to The DragonFly Project
 * by Matthew Dillon <dillon@backplane.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name of The DragonFly Project nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific, prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
 * ----------------------------------------------------------------------------
 *
 * Copyright (c) 1982, 1986, 1988, 1993
 *	The Regents of the University of California.  All rights reserved.
 * (c) UNIX System Laboratories, Inc.
 * All or some portions of this file are derived from material licensed
 * to the University of California by American Telephone and Telegraph
 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
 * the permission of UNIX System Laboratories, Inc.
 *
 * Copyright (c) 1994 Bruce D. Evans.
 * All rights reserved.
 *
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * William Jolitz.
 *
 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
 * $FreeBSD: src/sys/kern/subr_disk.c,v 1.20.2.6 2001/10/05 07:14:57 peter Exp $
 * $FreeBSD: src/sys/ufs/ufs/ufs_disksubr.c,v 1.44.2.3 2001/03/05 05:42:19 obrien Exp $
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <sys/buf.h>
#include <sys/conf.h>
#include <sys/disklabel.h>
#include <sys/disklabel32.h>
#include <sys/diskslice.h>
#include <sys/disk.h>
#include <sys/dtype.h>		/* DTYPE_* constants */
#include <machine/md_var.h>
#include <sys/ctype.h>
#include <sys/syslog.h>
#include <sys/device.h>
#include <sys/msgport.h>
#include <sys/msgport2.h>
#include <sys/buf2.h>

#include <vfs/ufs/dinode.h>	/* XXX used only for fs.h */
#include <vfs/ufs/fs.h>		/* XXX used only to get BBSIZE/SBSIZE */

static void partition_info(const char *sname, int part, struct partition32 *pp);
static void slice_info(const char *sname, struct diskslice *sp);
static const char *l32_fixlabel(const char *sname, struct diskslice *sp,
				disklabel_t lpx, int writeflag);

/*
 * Retrieve the partition start and extent, in blocks.  Return 0 on success,
 * EINVAL on error.
 */
static int
l32_getpartbounds(struct diskslices *ssp, disklabel_t lp, u_int32_t part,
		  u_int64_t *start, u_int64_t *blocks)
{
	struct partition32 *pp;

	if (part >= lp.lab32->d_npartitions)
		return (EINVAL);
	pp = &lp.lab32->d_partitions[part];
	*start = pp->p_offset;
	*blocks = pp->p_size;
	return(0);
}

static void
l32_loadpartinfo(disklabel_t lp, u_int32_t part, struct partinfo *dpart)
{
	struct partition32 *pp;
	const size_t uuid_size = sizeof(struct uuid);

	bzero(&dpart->fstype_uuid, uuid_size);
	bzero(&dpart->storage_uuid, uuid_size);
	if (part < lp.lab32->d_npartitions) {
		pp = &lp.lab32->d_partitions[part];
		dpart->fstype = pp->p_fstype;
	} else {
		dpart->fstype = 0;
	}
}

static u_int32_t
l32_getnumparts(disklabel_t lp)
{
	return(lp.lab32->d_npartitions);
}

static int
l32_getpackname(disklabel_t lp, char *buf, size_t bytes)
{
	size_t slen;

	if (lp.lab32->d_packname[0] == 0) {
		buf[0] = 0;
		return -1;
	}
	slen = strnlen(lp.lab32->d_packname, sizeof(lp.lab32->d_packname));
	if (slen >= bytes)
		slen = bytes - 1;
	bcopy(lp.lab32->d_packname, buf, slen);
	buf[slen] = 0;

	return 0;
}

static void
l32_freedisklabel(disklabel_t *lpp)
{
	kfree((*lpp).lab32, M_DEVBUF);
	(*lpp).lab32 = NULL;
}

/*
 * Attempt to read a disk label from a device.
 *
 * Returns NULL on sucess, and an error string on failure
 */
static const char *
l32_readdisklabel(cdev_t dev, struct diskslice *sp, disklabel_t *lpp,
		struct disk_info *info)
{
	disklabel_t lpx;
	struct buf *bp;
	struct disklabel32 *dlp;
	const char *msg = NULL;
	int secsize = info->d_media_blksize;

	bp = getpbuf_mem(NULL);
	KKASSERT(secsize <= bp->b_bufsize);
	bp->b_bio1.bio_offset = (off_t)LABELSECTOR32 * secsize;
	bp->b_bio1.bio_done = biodone_sync;
	bp->b_bio1.bio_flags |= BIO_SYNC;
	bp->b_bcount = secsize;
	bp->b_flags &= ~B_INVAL;
	bp->b_cmd = BUF_CMD_READ;
	bp->b_flags |= B_FAILONDIS;
	dev_dstrategy(dev, &bp->b_bio1);
	if (biowait(&bp->b_bio1, "labrd")) {
		msg = "I/O error";
	} else {
		for (dlp = (struct disklabel32 *)bp->b_data;
		     dlp <= (struct disklabel32 *)
		         ((char *)bp->b_data + secsize - sizeof(*dlp));
		     dlp = (struct disklabel32 *)((char *)dlp + sizeof(long)))
		{
			if (dlp->d_magic != DISKMAGIC32 ||
			    dlp->d_magic2 != DISKMAGIC32) {
				/*
				 * NOTE! dsreadandsetlabel() does a strcmp() on
				 * this string.
				 */
				if (msg == NULL)
					msg = "no disk label";
			} else if (dlp->d_npartitions > MAXPARTITIONS32 ||
				   dkcksum32(dlp) != 0) {
				msg = "disk label corrupted";
			} else {
				lpx.lab32 = dlp;
				msg = l32_fixlabel(NULL, sp, lpx, FALSE);
				if (msg == NULL) {
					(*lpp).lab32 = kmalloc(sizeof(*dlp),
					    M_DEVBUF, M_WAITOK | M_ZERO);
					*(*lpp).lab32 = *dlp;
				}
				break;
			}
		}
	}
	bp->b_flags |= B_INVAL | B_AGE;
	relpbuf(bp, NULL);

	return (msg);
}

/*
 * Check new disk label for sensibility before setting it.
 */
static int
l32_setdisklabel(disklabel_t olpx, disklabel_t nlpx, struct diskslices *ssp,
		 struct diskslice *sp, u_int32_t *openmask)
{
	struct disklabel32 *olp, *nlp;
	struct partition32 *opp, *npp;
	int part;
	int i;

	olp = olpx.lab32;
	nlp = nlpx.lab32;

	/*
	 * Check it is actually a disklabel we are looking at.
	 */
	if (nlp->d_magic != DISKMAGIC32 || nlp->d_magic2 != DISKMAGIC32 ||
	    dkcksum32(nlp) != 0)
		return (EINVAL);

	/*
	 * For each partition that we think is open, check the new disklabel
	 * for compatibility.  Ignore special partitions (>= 128).
	 */
	i = 0;
	while (i < 128) {
		if (openmask[i >> 5] == 0) {
			i += 32;
			continue;
		}
		if ((openmask[i >> 5] & (1 << (i & 31))) == 0) {
			++i;
			continue;
		}
		if (nlp->d_npartitions <= i)
			return (EBUSY);
		opp = &olp->d_partitions[i];
		npp = &nlp->d_partitions[i];
		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
			return (EBUSY);
		/*
		 * Copy internally-set partition information
		 * if new label doesn't include it.		XXX
		 * (If we are using it then we had better stay the same type)
		 * This is possibly dubious, as someone else noted (XXX)
		 */
		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
			npp->p_fstype = opp->p_fstype;
			npp->p_fsize = opp->p_fsize;
			npp->p_frag = opp->p_frag;
			npp->p_cpg = opp->p_cpg;
		}
		++i;
	}
 	nlp->d_checksum = 0;
 	nlp->d_checksum = dkcksum32(nlp);
	*olp = *nlp;

	if (olp->d_partitions[RAW_PART].p_offset)
		return (EXDEV);
	if (olp->d_secperunit > sp->ds_size)
		return (ENOSPC);
	for (part = 0; part < olp->d_npartitions; ++part) {
		if (olp->d_partitions[part].p_size > sp->ds_size)
			return(ENOSPC);
	}
	return (0);
}

/*
 * Write disk label back to device after modification.
 */
static int
l32_writedisklabel(cdev_t dev, struct diskslices *ssp, struct diskslice *sp,
		   disklabel_t lpx)
{
	struct disklabel32 *lp;
	struct disklabel32 *dlp;
	struct buf *bp;
	const char *msg;
	int error = 0;

	lp = lpx.lab32;

	if (lp->d_partitions[RAW_PART].p_offset != 0)
		return (EXDEV);			/* not quite right */

	bp = getpbuf_mem(NULL);
	KKASSERT((int)lp->d_secsize <= bp->b_bufsize);
	bp->b_bio1.bio_offset = (off_t)LABELSECTOR32 * lp->d_secsize;
	bp->b_bio1.bio_done = biodone_sync;
	bp->b_bio1.bio_flags |= BIO_SYNC;
	bp->b_bcount = lp->d_secsize;
	bp->b_flags |= B_FAILONDIS;

#if 1
	/*
	 * We read the label first to see if it's there,
	 * in which case we will put ours at the same offset into the block..
	 * (I think this is stupid [Julian])
	 * Note that you can't write a label out over a corrupted label!
	 * (also stupid.. how do you write the first one? by raw writes?)
	 */
	bp->b_flags &= ~B_INVAL;
	bp->b_cmd = BUF_CMD_READ;
	KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
	dev_dstrategy(dev, &bp->b_bio1);
	error = biowait(&bp->b_bio1, "labrd");
	if (error)
		goto done;
	for (dlp = (struct disklabel32 *)bp->b_data;
	     dlp <= (struct disklabel32 *)
	         ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
	     dlp = (struct disklabel32 *)((char *)dlp + sizeof(long)))
	{
		if (dlp->d_magic == DISKMAGIC32 &&
		    dlp->d_magic2 == DISKMAGIC32 && dkcksum32(dlp) == 0) {
			*dlp = *lp;
			lpx.lab32 = dlp;
			msg = l32_fixlabel(NULL, sp, lpx, TRUE);
			if (msg) {
				error = EINVAL;
			} else {
				bp->b_cmd = BUF_CMD_WRITE;
				bp->b_bio1.bio_done = biodone_sync;
				bp->b_bio1.bio_flags |= BIO_SYNC;
				KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
				dev_dstrategy(dev, &bp->b_bio1);
				error = biowait(&bp->b_bio1, "labwr");
			}
			goto done;
		}
	}
	error = ESRCH;
done:
#else
	bzero(bp->b_data, lp->d_secsize);
	dlp = (struct disklabel32 *)bp->b_data;
	*dlp = *lp;
	bp->b_flags &= ~B_INVAL;
	bp->b_cmd = BUF_CMD_WRITE;
	bp->b_bio1.bio_done = biodone_sync;
	bp->b_bio1.bio_flags |= BIO_SYNC;
	BUF_STRATEGY(bp, 1);
	error = biowait(&bp->b_bio1, "labwr");
#endif
	bp->b_flags |= B_INVAL | B_AGE;
	relpbuf(bp, NULL);

	return (error);
}

/*
 * Create a disklabel based on a disk_info structure, initializing
 * the appropriate fields and creating a raw partition that covers the
 * whole disk.
 *
 * If a diskslice is passed, the label is truncated to the slice
 */
static disklabel_t
l32_clone_label(struct disk_info *info, struct diskslice *sp)
{
	struct disklabel32 *lp;
	disklabel_t res;

	lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK | M_ZERO);
	lp->d_nsectors = info->d_secpertrack;
	lp->d_ntracks = info->d_nheads;
	lp->d_secpercyl = info->d_secpercyl;
	lp->d_secsize = info->d_media_blksize;

	if (sp)
		lp->d_secperunit = (u_int)sp->ds_size;
	else
		lp->d_secperunit = (u_int)info->d_media_blocks;

	if (lp->d_typename[0] == '\0')
		strncpy(lp->d_typename, "amnesiac", sizeof(lp->d_typename));
	if (lp->d_packname[0] == '\0')
		strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
	if (lp->d_nsectors == 0)
		lp->d_nsectors = 32;
	if (lp->d_ntracks == 0)
		lp->d_ntracks = 64;
	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
	lp->d_ncylinders = lp->d_secperunit / lp->d_secpercyl;
	if (lp->d_rpm == 0)
		lp->d_rpm = 3600;
	if (lp->d_interleave == 0)
		lp->d_interleave = 1;
	if (lp->d_npartitions < RAW_PART + 1)
		lp->d_npartitions = MAXPARTITIONS32;
	if (lp->d_bbsize == 0)
		lp->d_bbsize = BBSIZE;
	if (lp->d_sbsize == 0)
		lp->d_sbsize = SBSIZE;

	/*
	 * Used by various devices to create a compatibility slice which
	 * allows us to mount root from devices which do not have a
	 * disklabel.  Particularly: CDs.
	 */
	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
	if (info->d_dsflags & DSO_COMPATPARTA) {
		lp->d_partitions[0].p_size = lp->d_secperunit;
		lp->d_partitions[0].p_fstype = FS_OTHER;
	}
	lp->d_magic = DISKMAGIC32;
	lp->d_magic2 = DISKMAGIC32;
	lp->d_checksum = dkcksum32(lp);
	res.lab32 = lp;
	return (res);
}

static void
l32_makevirginlabel(disklabel_t lpx, struct diskslices *ssp,
		    struct diskslice *sp, struct disk_info *info)
{
	struct disklabel32 *lp = lpx.lab32;
	struct partition32 *pp;
	disklabel_t template;

	template = l32_clone_label(info, NULL);
	bcopy(template.opaque, lp, sizeof(struct disklabel32));

	lp->d_magic = DISKMAGIC32;
	lp->d_magic2 = DISKMAGIC32;

	lp->d_npartitions = MAXPARTITIONS32;
	if (lp->d_interleave == 0)
		lp->d_interleave = 1;
	if (lp->d_rpm == 0)
		lp->d_rpm = 3600;
	if (lp->d_nsectors == 0)	/* sectors per track */
		lp->d_nsectors = 32;
	if (lp->d_ntracks == 0)		/* heads */
		lp->d_ntracks = 64;
	lp->d_ncylinders = 0;
	lp->d_bbsize = BBSIZE;
	lp->d_sbsize = SBSIZE;

	/*
	 * If the slice or GPT partition is really small we could
	 * wind up with an absurd calculation for ncylinders.
	 */
	while (lp->d_ncylinders < 4) {
		if (lp->d_ntracks > 1)
			lp->d_ntracks >>= 1;
		else if (lp->d_nsectors > 1)
			lp->d_nsectors >>= 1;
		else
			break;
		lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
		lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
	}

	/*
	 * Set or Modify the partition sizes to accomodate the slice,
	 * since we started with a copy of the virgin label stored
	 * in the whole-disk-slice and we are probably not a
	 * whole-disk slice.
	 */
	lp->d_secperunit = sp->ds_size;
	pp = &lp->d_partitions[RAW_PART];
	pp->p_offset = 0;
	pp->p_size = lp->d_secperunit;
	if (info->d_dsflags & DSO_COMPATPARTA) {
		pp = &lp->d_partitions[0];
		pp->p_offset = 0;
		pp->p_size = lp->d_secperunit;
		pp->p_fstype = FS_OTHER;
	}
	lp->d_checksum = 0;
	lp->d_checksum = dkcksum32(lp);

	kfree(template.opaque, M_DEVBUF);
}

static const char *
l32_fixlabel(const char *sname, struct diskslice *sp,
	     disklabel_t lpx, int writeflag)
{
	struct disklabel32 *lp;
	struct partition32 *pp;
	u_int64_t start;
	u_int64_t end;
	u_int64_t offset;
	int part;
	int warned;

	lp = lpx.lab32;

	/* These errors "can't happen" so don't bother reporting details. */
	if (lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32)
		return ("fixlabel: invalid magic");
	if (dkcksum32(lp) != 0)
		return ("fixlabel: invalid checksum");

	pp = &lp->d_partitions[RAW_PART];

	/*
	 * What a mess.  For ages old backwards compatibility the disklabel
	 * on-disk stores absolute offsets instead of slice-relative offsets.
	 * So fix it up when reading, writing, or snooping.
	 *
	 * The in-core label is always slice-relative.
	 */
	if (writeflag) {
		start = 0;
		offset = sp->ds_offset;
	} else {
		start = sp->ds_offset;
		offset = -sp->ds_offset;
	}
	if (pp->p_offset != start) {
		if (sname != NULL) {
			kprintf("%s: rejecting BSD label: "
				"raw partition offset != slice offset\n",
				sname);
			slice_info(sname, sp);
			partition_info(sname, RAW_PART, pp);
		}
		return ("fixlabel: raw partition offset != slice offset");
	}
	if (pp->p_size != sp->ds_size) {
		if (sname != NULL) {
			kprintf("%s: raw partition size != slice size\n", sname);
			slice_info(sname, sp);
			partition_info(sname, RAW_PART, pp);
		}
		if (pp->p_size > sp->ds_size) {
			if (sname == NULL)
				return ("fixlabel: raw partition size > slice size");
			kprintf("%s: truncating raw partition\n", sname);
			pp->p_size = sp->ds_size;
		}
	}
	end = start + sp->ds_size;
	if (start > end)
		return ("fixlabel: slice wraps");
	if (lp->d_secpercyl <= 0)
		return ("fixlabel: d_secpercyl <= 0");
	pp -= RAW_PART;
	warned = FALSE;
	for (part = 0; part < lp->d_npartitions; part++, pp++) {
		if (pp->p_offset != 0 || pp->p_size != 0) {
			if (pp->p_offset < start
			    || pp->p_offset + pp->p_size > end
			    || pp->p_offset + pp->p_size < pp->p_offset) {
				if (sname != NULL) {
					kprintf("%s: rejecting partition in "
						"BSD label: it isn't entirely "
						"within the slice\n",
						sname);
					if (!warned) {
						slice_info(sname, sp);
						warned = TRUE;
					}
					partition_info(sname, part, pp);
				}
				/* XXX else silently discard junk. */
				bzero(pp, sizeof *pp);
			} else {
				pp->p_offset += offset;
			}
		}
	}
	lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
	lp->d_secperunit = sp->ds_size;
	lp->d_checksum = 0;
	lp->d_checksum = dkcksum32(lp);
	return (NULL);
}

/*
 * Set the number of blocks at the beginning of the slice which have
 * been reserved for label operations.  This area will be write-protected
 * when accessed via the slice.
 */
static void
l32_adjust_label_reserved(struct diskslices *ssp, int slice,
			  struct diskslice *sp)
{
	/*struct disklabel32 *lp = sp->ds_label.lab32;*/
	sp->ds_reserved = SBSIZE / ssp->dss_secsize;
}

static void
partition_info(const char *sname, int part, struct partition32 *pp)
{
	kprintf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
		(u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
		(u_long)pp->p_size);
}

static void
slice_info(const char *sname, struct diskslice *sp)
{
	kprintf("%s: start %llu, end %llu, size %llu\n", sname,
	       (long long)sp->ds_offset,
	       (long long)sp->ds_offset + sp->ds_size - 1,
	       (long long)sp->ds_size);
}

struct disklabel_ops disklabel32_ops = {
	.labelsize = sizeof(struct disklabel32),
	.op_readdisklabel = l32_readdisklabel,
	.op_setdisklabel = l32_setdisklabel,
	.op_writedisklabel = l32_writedisklabel,
	.op_clone_label = l32_clone_label,
	.op_adjust_label_reserved = l32_adjust_label_reserved,
	.op_getpartbounds = l32_getpartbounds,
	.op_loadpartinfo = l32_loadpartinfo,
	.op_getnumparts = l32_getnumparts,
	.op_makevirginlabel = l32_makevirginlabel,
	.op_getpackname = l32_getpackname,
	.op_freedisklabel = l32_freedisklabel
};