2 * Copyright (c) 1994 Bruce D. Evans.
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
11 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * from: @(#)wd.c 7.2 (Berkeley) 5/9/91
43 * from: wd.c,v 1.55 1994/10/22 01:57:12 phk Exp $
44 * from: @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91
45 * from: ufs_disksubr.c,v 1.8 1994/06/07 01:21:39 phk Exp $
46 * $FreeBSD: src/sys/kern/subr_diskslice.c,v 1.82.2.6 2001/07/24 09:49:41 dd Exp $
47 * $DragonFly: src/sys/kern/subr_diskslice.c,v 1.19 2006/05/04 18:32:22 dillon Exp $
50 #include <sys/param.h>
51 #include <sys/systm.h>
54 #include <sys/disklabel.h>
55 #include <sys/diskslice.h>
56 #include <sys/diskmbr.h>
57 #include <sys/fcntl.h>
58 #include <sys/malloc.h>
60 #include <sys/syslog.h>
61 #include <sys/vnode.h>
62 #include <sys/device.h>
63 #include <sys/thread2.h>
65 #include <vfs/ufs/dinode.h> /* XXX used only for fs.h */
66 #include <vfs/ufs/fs.h> /* XXX used only to get BBSIZE/SBSIZE */
68 #define TRACE(str) do { if (ds_debug) printf str; } while (0)
70 typedef u_char bool_t;
72 static volatile bool_t ds_debug;
74 static struct disklabel *clone_label (struct disklabel *lp);
75 static void dsiodone (struct bio *bio);
76 static char *fixlabel (char *sname, struct diskslice *sp,
77 struct disklabel *lp, int writeflag);
78 static void free_ds_label (struct diskslices *ssp, int slice);
79 static void partition_info (char *sname, int part, struct partition *pp);
80 static void slice_info (char *sname, struct diskslice *sp);
81 static void set_ds_label (struct diskslices *ssp, int slice,
82 struct disklabel *lp);
83 static void set_ds_wlabel (struct diskslices *ssp, int slice, int wlabel);
86 * Duplicate a label for the whole disk, and initialize defaults in the
87 * copy for fields that are not already initialized. The caller only
88 * needs to initialize d_secsize and d_secperunit, and zero the fields
89 * that are to be defaulted.
91 static struct disklabel *
92 clone_label(struct disklabel *lp)
94 struct disklabel *lp1;
96 lp1 = malloc(sizeof *lp1, M_DEVBUF, M_WAITOK);
99 if (lp1->d_typename[0] == '\0')
100 strncpy(lp1->d_typename, "amnesiac", sizeof(lp1->d_typename));
101 if (lp1->d_packname[0] == '\0')
102 strncpy(lp1->d_packname, "fictitious", sizeof(lp1->d_packname));
103 if (lp1->d_nsectors == 0)
104 lp1->d_nsectors = 32;
105 if (lp1->d_ntracks == 0)
107 lp1->d_secpercyl = lp1->d_nsectors * lp1->d_ntracks;
108 lp1->d_ncylinders = lp1->d_secperunit / lp1->d_secpercyl;
111 if (lp1->d_interleave == 0)
112 lp1->d_interleave = 1;
113 if (lp1->d_npartitions < RAW_PART + 1)
114 lp1->d_npartitions = MAXPARTITIONS;
115 if (lp1->d_bbsize == 0)
116 lp1->d_bbsize = BBSIZE;
117 if (lp1->d_sbsize == 0)
118 lp1->d_sbsize = SBSIZE;
119 lp1->d_partitions[RAW_PART].p_size = lp1->d_secperunit;
120 lp1->d_magic = DISKMAGIC;
121 lp1->d_magic2 = DISKMAGIC;
122 lp1->d_checksum = dkcksum(lp1);
127 * Determine the size of the transfer, and make sure it is
128 * within the boundaries of the partition. Adjust transfer
129 * if needed, and signal errors or early completion.
132 * o Split buffers that are too big for the device.
133 * o Check for overflow.
134 * o Finish cleaning this up.
136 * This function returns 1 on success, 0 if transfer equates
137 * to EOF (end of disk) or -1 on failure. The appropriate
138 * 'errno' value is also set in bp->b_error and bp->b_flags
139 * is marked with B_ERROR.
142 dscheck(dev_t dev, struct bio *bio, struct diskslices *ssp)
144 struct buf *bp = bio->bio_buf;
148 struct disklabel *lp;
151 struct partition *pp;
153 daddr_t slicerel_secno;
154 struct diskslice *sp;
158 if (bio->bio_offset < 0) {
159 printf("dscheck(%s): negative bio_offset %lld\n",
160 devtoname(dev), bio->bio_offset);
163 sp = &ssp->dss_slices[dkslice(dev)];
166 if (ssp->dss_secmult == 1) {
169 } else if (ssp->dss_secshift != -1) {
170 shift = DEV_BSHIFT + ssp->dss_secshift;
172 mask = (1 << shift) - 1;
173 if ((int)bp->b_bcount & mask)
175 if ((int)bio->bio_offset & mask)
177 secno = (daddr_t)(bio->bio_offset >> shift);
178 nsec = bp->b_bcount >> shift;
180 if (bp->b_bcount % ssp->dss_secsize)
182 if (bio->bio_offset % ssp->dss_secsize)
184 secno = (daddr_t)(bio->bio_offset / ssp->dss_secsize);
185 nsec = bp->b_bcount / ssp->dss_secsize;
188 labelsect = -LABELSECTOR - 1;
189 endsecno = sp->ds_size;
190 slicerel_secno = secno;
192 labelsect = lp->d_partitions[LABEL_PART].p_offset;
194 Debugger("labelsect != 0 in dscheck()");
195 pp = &lp->d_partitions[dkpart(dev)];
196 endsecno = pp->p_size;
197 slicerel_secno = pp->p_offset + secno;
200 /* overwriting disk label ? */
201 /* XXX should also protect bootstrap in first 8K */
202 if (slicerel_secno <= LABELSECTOR + labelsect &&
204 slicerel_secno + nsec > LABELSECTOR + labelsect &&
206 bp->b_cmd != BUF_CMD_READ && sp->ds_wlabel == 0) {
211 #if defined(DOSBBSECTOR) && defined(notyet)
212 /* overwriting master boot record? */
213 if (slicerel_secno <= DOSBBSECTOR && bp->b_cmd != BUF_CMD_READ &&
214 sp->ds_wlabel == 0) {
223 if (secno + nsec > endsecno) {
225 * Return an error if beyond the end of the disk, or
226 * if B_BNOCLIP is set. Tell the system that we do not
227 * need to keep the buffer around.
229 if (secno > endsecno || (bp->b_flags & B_BNOCLIP))
233 * If exactly at end of disk, return an EOF. Throw away
234 * the buffer contents, if any, by setting B_INVAL.
236 if (secno == endsecno) {
237 bp->b_resid = bp->b_bcount;
238 bp->b_flags |= B_INVAL;
245 nsec = endsecno - secno;
246 bp->b_bcount = nsec * ssp->dss_secsize;
249 nbio = push_bio(bio);
250 nbio->bio_offset = (off_t)(sp->ds_offset + slicerel_secno) *
254 * Snoop on label accesses if the slice offset is nonzero. Fudge
255 * offsets in the label to keep the in-core label coherent with
258 if (slicerel_secno <= LABELSECTOR + labelsect
260 && slicerel_secno + nsec > LABELSECTOR + labelsect
262 && sp->ds_offset != 0) {
263 nbio->bio_done = dsiodone;
264 nbio->bio_caller_info1.ptr = sp;
265 nbio->bio_caller_info2.offset = (off_t)(LABELSECTOR + labelsect -
266 slicerel_secno) * ssp->dss_secsize;
267 if (bp->b_cmd != BUF_CMD_READ) {
269 * XXX even disklabel(8) writes directly so we need
270 * to adjust writes. Perhaps we should drop support
271 * for DIOCWLABEL (always write protect labels) and
272 * require the use of DIOCWDINFO.
274 * XXX probably need to copy the data to avoid even
275 * temporarily corrupting the in-core copy.
277 /* XXX need name here. */
281 (bp->b_data + (int)nbio->bio_caller_info2.offset),
284 printf("dscheck(%s): %s\n",
285 devtoname(dev), msg);
296 "dscheck(%s): b_bcount %d is not on a sector boundary (ssize %d)\n",
297 devtoname(dev), bp->b_bcount, ssp->dss_secsize);
302 "dscheck(%s): bio_offset %lld is not on a sector boundary (ssize %d)\n",
303 devtoname(dev), bio->bio_offset, ssp->dss_secsize);
305 bp->b_error = EINVAL;
309 * Terminate the I/O with a ranging error. Since the buffer is
310 * either illegal or beyond the file EOF, mark it B_INVAL as well.
312 bp->b_resid = bp->b_bcount;
313 bp->b_flags |= B_ERROR | B_INVAL;
316 * Caller must biodone() the originally passed bio if NULL is
323 dsclose(dev_t dev, int mode, struct diskslices *ssp)
326 struct diskslice *sp;
328 sp = &ssp->dss_slices[dkslice(dev)];
329 mask = 1 << dkpart(dev);
330 sp->ds_openmask &= ~mask;
334 dsgone(struct diskslices **sspp)
337 struct diskslice *sp;
338 struct diskslices *ssp;
340 for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
341 sp = &ssp->dss_slices[slice];
342 free_ds_label(ssp, slice);
349 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
350 * is subject to the same restriction as dsopen().
353 dsioctl(dev_t dev, u_long cmd, caddr_t data,
354 int flags, struct diskslices **sspp)
357 struct disklabel *lp;
362 struct diskslice *sp;
363 struct diskslices *ssp;
364 struct partition *pp;
366 slice = dkslice(dev);
368 sp = &ssp->dss_slices[slice];
373 lp = (struct disklabel *)data;
374 if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
375 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
377 bzero(lp, sizeof(struct disklabel));
380 lp->d_magic = DISKMAGIC;
381 lp->d_magic2 = DISKMAGIC;
382 pp = &lp->d_partitions[RAW_PART];
384 pp->p_size = sp->ds_size;
386 lp->d_npartitions = MAXPARTITIONS;
387 if (lp->d_interleave == 0)
388 lp->d_interleave = 1;
391 if (lp->d_nsectors == 0)
393 if (lp->d_ntracks == 0)
396 lp->d_bbsize = BBSIZE;
397 lp->d_sbsize = SBSIZE;
398 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
399 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
400 lp->d_secperunit = sp->ds_size;
402 lp->d_checksum = dkcksum(lp);
408 *(struct disklabel *)data = *lp;
415 *(struct disklabel **)data = lp;
422 ((struct partinfo *)data)->disklab = lp;
423 ((struct partinfo *)data)->part
424 = &lp->d_partitions[dkpart(dev)];
428 bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
433 if (slice == WHOLE_DISK_SLICE)
435 if (!(flags & FWRITE))
437 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
438 if (sp->ds_label == NULL)
439 bzero(lp, sizeof *lp);
441 bcopy(sp->ds_label, lp, sizeof *lp);
442 if (sp->ds_label == NULL)
445 openmask = sp->ds_openmask;
446 if (slice == COMPATIBILITY_SLICE)
447 openmask |= ssp->dss_slices[
448 ssp->dss_first_bsd_slice].ds_openmask;
449 else if (slice == ssp->dss_first_bsd_slice)
450 openmask |= ssp->dss_slices[
451 COMPATIBILITY_SLICE].ds_openmask;
453 error = setdisklabel(lp, (struct disklabel *)data,
455 /* XXX why doesn't setdisklabel() check this? */
456 if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
459 if (lp->d_secperunit > sp->ds_size)
461 for (part = 0; part < lp->d_npartitions; part++)
462 if (lp->d_partitions[part].p_size > sp->ds_size)
469 free_ds_label(ssp, slice);
470 set_ds_label(ssp, slice, lp);
473 case DIOCSYNCSLICEINFO:
474 if (slice != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART)
477 for (slice = 0; slice < ssp->dss_nslices; slice++) {
478 openmask = ssp->dss_slices[slice].ds_openmask;
480 && (slice != WHOLE_DISK_SLICE
481 || openmask & ~(1 << RAW_PART)))
486 * Temporarily forget the current slices struct and read
488 * XXX should wait for current accesses on this disk to
489 * complete, then lock out future accesses and opens.
492 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
493 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
494 error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, lp);
502 * Reopen everything. This is a no-op except in the "force"
503 * case and when the raw bdev and cdev are both open. Abort
506 for (slice = 0; slice < ssp->dss_nslices; slice++) {
507 for (openmask = ssp->dss_slices[slice].ds_openmask,
508 part = 0; openmask; openmask >>= 1, part++) {
511 error = dsopen(dkmodslice(dkmodpart(dev, part),
513 S_IFCHR, ssp->dss_oflags, sspp,
528 error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp);
532 * XXX this used to hack on dk_openpart to fake opening
533 * partition 0 in case that is used instead of dkpart(dev).
535 old_wlabel = sp->ds_wlabel;
536 set_ds_wlabel(ssp, slice, TRUE);
537 error = writedisklabel(dev, sp->ds_label);
538 /* XXX should invalidate in-core label if write failed. */
539 set_ds_wlabel(ssp, slice, old_wlabel);
543 if (slice == WHOLE_DISK_SLICE)
545 if (!(flags & FWRITE))
547 set_ds_wlabel(ssp, slice, *(int *)data != 0);
556 * Chain the bio_done. b_cmd remains valid through such chaining.
559 dsiodone(struct bio *bio)
561 struct buf *bp = bio->bio_buf;
564 if (bp->b_cmd != BUF_CMD_READ
565 || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
566 msg = fixlabel(NULL, bio->bio_caller_info1.ptr,
568 (bp->b_data + (int)bio->bio_caller_info2.offset),
573 biodone(bio->bio_prev);
577 dsisopen(struct diskslices *ssp)
583 for (slice = 0; slice < ssp->dss_nslices; slice++) {
584 if (ssp->dss_slices[slice].ds_openmask)
591 * Allocate a slices "struct" and initialize it to contain only an empty
592 * compatibility slice (pointing to itself), a whole disk slice (covering
593 * the disk as described by the label), and (nslices - BASE_SLICES) empty
594 * slices beginning at BASE_SLICE.
597 dsmakeslicestruct(int nslices, struct disklabel *lp)
599 struct diskslice *sp;
600 struct diskslices *ssp;
602 ssp = malloc(offsetof(struct diskslices, dss_slices) +
603 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
604 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
605 ssp->dss_nslices = nslices;
607 ssp->dss_secmult = lp->d_secsize / DEV_BSIZE;
608 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
609 ssp->dss_secshift = -1;
611 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
612 ssp->dss_secsize = lp->d_secsize;
613 sp = &ssp->dss_slices[0];
614 bzero(sp, nslices * sizeof *sp);
615 sp[WHOLE_DISK_SLICE].ds_size = lp->d_secperunit;
620 dsname(dev_t dev, int unit, int slice, int part, char *partname)
622 static char name[32];
625 dname = dev_dname(dev);
626 if (strlen(dname) > 16)
627 dname = "nametoolong";
628 snprintf(name, sizeof(name), "%s%d", dname, unit);
630 if (slice != WHOLE_DISK_SLICE || part != RAW_PART) {
631 partname[0] = 'a' + part;
633 if (slice != COMPATIBILITY_SLICE) {
634 snprintf(name + strlen(name),
635 sizeof(name) - strlen(name), "s%d", slice - 1);
642 * This should only be called when the unit is inactive and the strategy
643 * routine should not allow it to become active unless we call it. Our
644 * strategy routine must be special to allow activity.
647 dsopen(dev_t dev, int mode, u_int flags,
648 struct diskslices **sspp, struct disklabel *lp)
652 struct disklabel *lp1;
660 struct diskslice *sp;
661 struct diskslices *ssp;
664 dev->si_bsize_phys = lp->d_secsize;
667 if (lp->d_secsize % DEV_BSIZE) {
668 printf("%s: invalid sector size %lu\n", devtoname(dev),
669 (u_long)lp->d_secsize);
674 * XXX reinitialize the slice table unless there is an open device
675 * on the unit. This should only be done if the media has changed.
678 need_init = !dsisopen(ssp);
679 if (ssp != NULL && need_init)
683 * Allocate a minimal slices "struct". This will become
684 * the final slices "struct" if we don't want real slices
685 * or if we can't find any real slices.
687 *sspp = dsmakeslicestruct(BASE_SLICE, lp);
689 if (!(flags & DSO_ONESLICE)) {
691 error = dsinit(dev, lp, sspp);
698 ssp->dss_oflags = flags;
701 * If there are no real slices, then make the compatiblity
702 * slice cover the whole disk.
704 if (ssp->dss_nslices == BASE_SLICE)
705 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
708 /* Point the compatibility slice at the BSD slice, if any. */
709 for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
710 sp = &ssp->dss_slices[slice];
711 if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
712 ssp->dss_first_bsd_slice = slice;
713 ssp->dss_slices[COMPATIBILITY_SLICE].ds_offset
715 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
717 ssp->dss_slices[COMPATIBILITY_SLICE].ds_type
723 ssp->dss_slices[WHOLE_DISK_SLICE].ds_label = clone_label(lp);
724 ssp->dss_slices[WHOLE_DISK_SLICE].ds_wlabel = TRUE;
728 * Initialize secondary info for all slices. It is needed for more
729 * than the current slice in the DEVFS case. XXX DEVFS is no more.
731 for (slice = 0; slice < ssp->dss_nslices; slice++) {
732 sp = &ssp->dss_slices[slice];
733 if (sp->ds_label != NULL)
735 dev1 = dkmodslice(dkmodpart(dev, RAW_PART), slice);
736 sname = dsname(dev, unit, slice, RAW_PART, partname);
738 * XXX this should probably only be done for the need_init
739 * case, but there may be a problem with DIOCSYNCSLICEINFO.
741 set_ds_wlabel(ssp, slice, TRUE); /* XXX invert */
742 lp1 = clone_label(lp);
743 TRACE(("readdisklabel\n"));
744 if (flags & DSO_NOLABELS)
747 msg = readdisklabel(dev1, lp1);
750 * readdisklabel() returns NULL for success, and an
751 * error string for failure.
753 * If there isn't a label on the disk, and if the
754 * DSO_COMPATLABEL is set, we want to use the
755 * faked-up label provided by the caller.
757 * So we set msg to NULL to indicate that there is
758 * no failure (since we have a faked-up label),
759 * free lp1, and then clone it again from lp.
760 * (In case readdisklabel() modified lp1.)
762 if (msg != NULL && (flags & DSO_COMPATLABEL)) {
765 lp1 = clone_label(lp);
769 msg = fixlabel(sname, sp, lp1, FALSE);
770 if (msg == NULL && lp1->d_secsize != ssp->dss_secsize)
771 msg = "inconsistent sector size";
773 if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
774 log(LOG_WARNING, "%s: cannot find label (%s)\n",
779 if (lp1->d_flags & D_BADSECT) {
780 log(LOG_ERR, "%s: bad sector table not supported\n",
785 set_ds_label(ssp, slice, lp1);
786 set_ds_wlabel(ssp, slice, FALSE);
789 slice = dkslice(dev);
790 if (slice >= ssp->dss_nslices)
792 sp = &ssp->dss_slices[slice];
795 && (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions))
796 return (EINVAL); /* XXX needs translation */
798 sp->ds_openmask |= mask;
803 dssize(dev_t dev, struct diskslices **sspp)
805 struct disklabel *lp;
808 struct diskslices *ssp;
810 slice = dkslice(dev);
813 if (ssp == NULL || slice >= ssp->dss_nslices
814 || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
815 if (dev_dopen(dev, FREAD, S_IFCHR, NULL) != 0)
817 dev_dclose(dev, FREAD, S_IFCHR, NULL);
820 lp = ssp->dss_slices[slice].ds_label;
823 return ((int)lp->d_partitions[part].p_size);
827 free_ds_label(struct diskslices *ssp, int slice)
829 struct disklabel *lp;
830 struct diskslice *sp;
832 sp = &ssp->dss_slices[slice];
837 set_ds_label(ssp, slice, (struct disklabel *)NULL);
841 fixlabel(char *sname, struct diskslice *sp, struct disklabel *lp, int writeflag)
846 struct partition *pp;
850 /* These errors "can't happen" so don't bother reporting details. */
851 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC)
852 return ("fixlabel: invalid magic");
853 if (dkcksum(lp) != 0)
854 return ("fixlabel: invalid checksum");
856 pp = &lp->d_partitions[RAW_PART];
859 offset = sp->ds_offset;
861 start = sp->ds_offset;
862 offset = -sp->ds_offset;
864 if (pp->p_offset != start) {
867 "%s: rejecting BSD label: raw partition offset != slice offset\n",
869 slice_info(sname, sp);
870 partition_info(sname, RAW_PART, pp);
872 return ("fixlabel: raw partition offset != slice offset");
874 if (pp->p_size != sp->ds_size) {
876 printf("%s: raw partition size != slice size\n", sname);
877 slice_info(sname, sp);
878 partition_info(sname, RAW_PART, pp);
880 if (pp->p_size > sp->ds_size) {
882 return ("fixlabel: raw partition size > slice size");
883 printf("%s: truncating raw partition\n", sname);
884 pp->p_size = sp->ds_size;
887 end = start + sp->ds_size;
889 return ("fixlabel: slice wraps");
890 if (lp->d_secpercyl <= 0)
891 return ("fixlabel: d_secpercyl <= 0");
894 for (part = 0; part < lp->d_npartitions; part++, pp++) {
895 if (pp->p_offset != 0 || pp->p_size != 0) {
896 if (pp->p_offset < start
897 || pp->p_offset + pp->p_size > end
898 || pp->p_offset + pp->p_size < pp->p_offset) {
901 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
904 slice_info(sname, sp);
907 partition_info(sname, part, pp);
909 /* XXX else silently discard junk. */
910 bzero(pp, sizeof *pp);
912 pp->p_offset += offset;
915 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
916 lp->d_secperunit = sp->ds_size;
918 lp->d_checksum = dkcksum(lp);
923 partition_info(char *sname, int part, struct partition *pp)
925 printf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
926 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
931 slice_info(char *sname, struct diskslice *sp)
933 printf("%s: start %lu, end %lu, size %lu\n", sname,
934 sp->ds_offset, sp->ds_offset + sp->ds_size - 1, sp->ds_size);
938 set_ds_label(struct diskslices *ssp, int slice, struct disklabel *lp)
940 ssp->dss_slices[slice].ds_label = lp;
941 if (slice == COMPATIBILITY_SLICE)
942 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_label = lp;
943 else if (slice == ssp->dss_first_bsd_slice)
944 ssp->dss_slices[COMPATIBILITY_SLICE].ds_label = lp;
948 set_ds_wlabel(struct diskslices *ssp, int slice, int wlabel)
950 ssp->dss_slices[slice].ds_wlabel = wlabel;
951 if (slice == COMPATIBILITY_SLICE)
952 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_wlabel = wlabel;
953 else if (slice == ssp->dss_first_bsd_slice)
954 ssp->dss_slices[COMPATIBILITY_SLICE].ds_wlabel = wlabel;