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.13 2006/02/17 19:18:06 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/fs.h>
67 #define TRACE(str) do { if (ds_debug) printf str; } while (0)
69 typedef u_char bool_t;
71 static volatile bool_t ds_debug;
73 static struct disklabel *clone_label (struct disklabel *lp);
74 static void dsiodone (struct bio *bio);
75 static char *fixlabel (char *sname, struct diskslice *sp,
76 struct disklabel *lp, int writeflag);
77 static void free_ds_label (struct diskslices *ssp, int slice);
78 static void partition_info (char *sname, int part, struct partition *pp);
79 static void slice_info (char *sname, struct diskslice *sp);
80 static void set_ds_label (struct diskslices *ssp, int slice,
81 struct disklabel *lp);
82 static void set_ds_wlabel (struct diskslices *ssp, int slice, int wlabel);
85 * Duplicate a label for the whole disk, and initialize defaults in the
86 * copy for fields that are not already initialized. The caller only
87 * needs to initialize d_secsize and d_secperunit, and zero the fields
88 * that are to be defaulted.
90 static struct disklabel *
91 clone_label(struct disklabel *lp)
93 struct disklabel *lp1;
95 lp1 = malloc(sizeof *lp1, M_DEVBUF, M_WAITOK);
98 if (lp1->d_typename[0] == '\0')
99 strncpy(lp1->d_typename, "amnesiac", sizeof(lp1->d_typename));
100 if (lp1->d_packname[0] == '\0')
101 strncpy(lp1->d_packname, "fictitious", sizeof(lp1->d_packname));
102 if (lp1->d_nsectors == 0)
103 lp1->d_nsectors = 32;
104 if (lp1->d_ntracks == 0)
106 lp1->d_secpercyl = lp1->d_nsectors * lp1->d_ntracks;
107 lp1->d_ncylinders = lp1->d_secperunit / lp1->d_secpercyl;
110 if (lp1->d_interleave == 0)
111 lp1->d_interleave = 1;
112 if (lp1->d_npartitions < RAW_PART + 1)
113 lp1->d_npartitions = MAXPARTITIONS;
114 if (lp1->d_bbsize == 0)
115 lp1->d_bbsize = BBSIZE;
116 if (lp1->d_sbsize == 0)
117 lp1->d_sbsize = SBSIZE;
118 lp1->d_partitions[RAW_PART].p_size = lp1->d_secperunit;
119 lp1->d_magic = DISKMAGIC;
120 lp1->d_magic2 = DISKMAGIC;
121 lp1->d_checksum = dkcksum(lp1);
126 * Determine the size of the transfer, and make sure it is
127 * within the boundaries of the partition. Adjust transfer
128 * if needed, and signal errors or early completion.
131 * o Split buffers that are too big for the device.
132 * o Check for overflow.
133 * o Finish cleaning this up.
135 * This function returns 1 on success, 0 if transfer equates
136 * to EOF (end of disk) or -1 on failure. The appropriate
137 * 'errno' value is also set in bp->b_error and bp->b_flags
138 * is marked with B_ERROR.
141 dscheck(dev_t dev, struct bio *bio, struct diskslices *ssp)
143 struct buf *bp = bio->bio_buf;
148 struct disklabel *lp;
151 struct partition *pp;
153 daddr_t slicerel_secno;
154 struct diskslice *sp;
156 blkno = bio->bio_blkno;
158 printf("dscheck(%s): negative bio_blkno %ld\n",
159 devtoname(dev), (long)blkno);
160 bp->b_error = EINVAL;
163 sp = &ssp->dss_slices[dkslice(dev)];
165 if (ssp->dss_secmult == 1) {
166 if (bp->b_bcount % (u_long)DEV_BSIZE)
169 nsec = bp->b_bcount >> DEV_BSHIFT;
170 } else if (ssp->dss_secshift != -1) {
171 if (bp->b_bcount & (ssp->dss_secsize - 1))
173 if (blkno & (ssp->dss_secmult - 1))
175 secno = blkno >> ssp->dss_secshift;
176 nsec = bp->b_bcount >> (DEV_BSHIFT + ssp->dss_secshift);
178 if (bp->b_bcount % ssp->dss_secsize)
180 if (blkno % ssp->dss_secmult)
182 secno = blkno / ssp->dss_secmult;
183 nsec = bp->b_bcount / ssp->dss_secsize;
186 labelsect = -LABELSECTOR - 1;
187 endsecno = sp->ds_size;
188 slicerel_secno = secno;
190 labelsect = lp->d_partitions[LABEL_PART].p_offset;
192 Debugger("labelsect != 0 in dscheck()");
193 pp = &lp->d_partitions[dkpart(dev)];
194 endsecno = pp->p_size;
195 slicerel_secno = pp->p_offset + secno;
198 /* overwriting disk label ? */
199 /* XXX should also protect bootstrap in first 8K */
200 if (slicerel_secno <= LABELSECTOR + labelsect &&
202 slicerel_secno + nsec > LABELSECTOR + labelsect &&
204 (bp->b_flags & B_READ) == 0 && sp->ds_wlabel == 0) {
209 #if defined(DOSBBSECTOR) && defined(notyet)
210 /* overwriting master boot record? */
211 if (slicerel_secno <= DOSBBSECTOR && (bp->b_flags & B_READ) == 0 &&
212 sp->ds_wlabel == 0) {
218 /* beyond partition? */
219 if (secno + nsec > endsecno) {
220 /* if exactly at end of disk, return an EOF */
221 if (secno == endsecno) {
222 bp->b_resid = bp->b_bcount;
225 /* or truncate if part of it fits */
226 nsec = endsecno - secno;
228 bp->b_error = EINVAL;
231 bp->b_bcount = nsec * ssp->dss_secsize;
234 nbio = push_bio(bio);
235 nbio->bio_blkno = sp->ds_offset + slicerel_secno;
238 * Snoop on label accesses if the slice offset is nonzero. Fudge
239 * offsets in the label to keep the in-core label coherent with
242 if (slicerel_secno <= LABELSECTOR + labelsect
244 && slicerel_secno + nsec > LABELSECTOR + labelsect
246 && sp->ds_offset != 0) {
247 nbio->bio_done = dsiodone;
248 nbio->bio_caller_info1.ptr = sp;
249 nbio->bio_caller_info2.offset = (off_t)(LABELSECTOR + labelsect -
250 slicerel_secno) * ssp->dss_secsize;
251 if ((bp->b_flags & B_READ) == 0) {
253 * XXX even disklabel(8) writes directly so we need
254 * to adjust writes. Perhaps we should drop support
255 * for DIOCWLABEL (always write protect labels) and
256 * require the use of DIOCWDINFO.
258 * XXX probably need to copy the data to avoid even
259 * temporarily corrupting the in-core copy.
261 /* XXX need name here. */
265 (bp->b_data + (int)nbio->bio_caller_info2.offset),
268 printf("dscheck(%s): %s\n",
269 devtoname(dev), msg);
280 "dscheck(%s): b_bcount %ld is not on a sector boundary (ssize %d)\n",
281 devtoname(dev), bp->b_bcount, ssp->dss_secsize);
282 bp->b_error = EINVAL;
287 "dscheck(%s): bio_blkno %ld is not on a sector boundary (ssize %d)\n",
288 devtoname(dev), (long)blkno, ssp->dss_secsize);
289 bp->b_error = EINVAL;
293 bp->b_resid = bp->b_bcount;
294 bp->b_flags |= B_ERROR;
299 dsclose(dev_t dev, int mode, struct diskslices *ssp)
302 struct diskslice *sp;
304 sp = &ssp->dss_slices[dkslice(dev)];
305 mask = 1 << dkpart(dev);
306 sp->ds_openmask &= ~mask;
310 dsgone(struct diskslices **sspp)
313 struct diskslice *sp;
314 struct diskslices *ssp;
316 for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
317 sp = &ssp->dss_slices[slice];
318 free_ds_label(ssp, slice);
325 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
326 * is subject to the same restriction as dsopen().
329 dsioctl(dev_t dev, u_long cmd, caddr_t data,
330 int flags, struct diskslices **sspp)
333 struct disklabel *lp;
338 struct diskslice *sp;
339 struct diskslices *ssp;
340 struct partition *pp;
342 slice = dkslice(dev);
344 sp = &ssp->dss_slices[slice];
349 lp = (struct disklabel *)data;
350 if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
351 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
353 bzero(lp, sizeof(struct disklabel));
356 lp->d_magic = DISKMAGIC;
357 lp->d_magic2 = DISKMAGIC;
358 pp = &lp->d_partitions[RAW_PART];
360 pp->p_size = sp->ds_size;
362 lp->d_npartitions = MAXPARTITIONS;
363 if (lp->d_interleave == 0)
364 lp->d_interleave = 1;
367 if (lp->d_nsectors == 0)
369 if (lp->d_ntracks == 0)
372 lp->d_bbsize = BBSIZE;
373 lp->d_sbsize = SBSIZE;
374 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
375 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
376 lp->d_secperunit = sp->ds_size;
378 lp->d_checksum = dkcksum(lp);
384 *(struct disklabel *)data = *lp;
391 *(struct disklabel **)data = lp;
398 ((struct partinfo *)data)->disklab = lp;
399 ((struct partinfo *)data)->part
400 = &lp->d_partitions[dkpart(dev)];
404 bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
409 if (slice == WHOLE_DISK_SLICE)
411 if (!(flags & FWRITE))
413 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
414 if (sp->ds_label == NULL)
415 bzero(lp, sizeof *lp);
417 bcopy(sp->ds_label, lp, sizeof *lp);
418 if (sp->ds_label == NULL)
421 openmask = sp->ds_openmask;
422 if (slice == COMPATIBILITY_SLICE)
423 openmask |= ssp->dss_slices[
424 ssp->dss_first_bsd_slice].ds_openmask;
425 else if (slice == ssp->dss_first_bsd_slice)
426 openmask |= ssp->dss_slices[
427 COMPATIBILITY_SLICE].ds_openmask;
429 error = setdisklabel(lp, (struct disklabel *)data,
431 /* XXX why doesn't setdisklabel() check this? */
432 if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
435 if (lp->d_secperunit > sp->ds_size)
437 for (part = 0; part < lp->d_npartitions; part++)
438 if (lp->d_partitions[part].p_size > sp->ds_size)
445 free_ds_label(ssp, slice);
446 set_ds_label(ssp, slice, lp);
449 case DIOCSYNCSLICEINFO:
450 if (slice != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART)
453 for (slice = 0; slice < ssp->dss_nslices; slice++) {
454 openmask = ssp->dss_slices[slice].ds_openmask;
456 && (slice != WHOLE_DISK_SLICE
457 || openmask & ~(1 << RAW_PART)))
462 * Temporarily forget the current slices struct and read
464 * XXX should wait for current accesses on this disk to
465 * complete, then lock out future accesses and opens.
468 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
469 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
470 error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, lp);
478 * Reopen everything. This is a no-op except in the "force"
479 * case and when the raw bdev and cdev are both open. Abort
482 for (slice = 0; slice < ssp->dss_nslices; slice++) {
483 for (openmask = ssp->dss_slices[slice].ds_openmask,
484 part = 0; openmask; openmask >>= 1, part++) {
487 error = dsopen(dkmodslice(dkmodpart(dev, part),
489 S_IFCHR, ssp->dss_oflags, sspp,
504 error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp);
508 * XXX this used to hack on dk_openpart to fake opening
509 * partition 0 in case that is used instead of dkpart(dev).
511 old_wlabel = sp->ds_wlabel;
512 set_ds_wlabel(ssp, slice, TRUE);
513 error = writedisklabel(dev, sp->ds_label);
514 /* XXX should invalidate in-core label if write failed. */
515 set_ds_wlabel(ssp, slice, old_wlabel);
519 if (slice == WHOLE_DISK_SLICE)
521 if (!(flags & FWRITE))
523 set_ds_wlabel(ssp, slice, *(int *)data != 0);
532 dsiodone(struct bio *bio)
534 struct buf *bp = bio->bio_buf;
537 bp->b_flags = bp->b_flags & ~B_DONE;
538 if (!(bp->b_flags & B_READ)
539 || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
540 msg = fixlabel(NULL, bio->bio_caller_info1.ptr,
542 (bp->b_data + (int)bio->bio_caller_info2.offset),
547 biodone(bio->bio_prev);
551 dsisopen(struct diskslices *ssp)
557 for (slice = 0; slice < ssp->dss_nslices; slice++) {
558 if (ssp->dss_slices[slice].ds_openmask)
565 * Allocate a slices "struct" and initialize it to contain only an empty
566 * compatibility slice (pointing to itself), a whole disk slice (covering
567 * the disk as described by the label), and (nslices - BASE_SLICES) empty
568 * slices beginning at BASE_SLICE.
571 dsmakeslicestruct(int nslices, struct disklabel *lp)
573 struct diskslice *sp;
574 struct diskslices *ssp;
576 ssp = malloc(offsetof(struct diskslices, dss_slices) +
577 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
578 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
579 ssp->dss_nslices = nslices;
581 ssp->dss_secmult = lp->d_secsize / DEV_BSIZE;
582 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
583 ssp->dss_secshift = -1;
585 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
586 ssp->dss_secsize = lp->d_secsize;
587 sp = &ssp->dss_slices[0];
588 bzero(sp, nslices * sizeof *sp);
589 sp[WHOLE_DISK_SLICE].ds_size = lp->d_secperunit;
594 dsname(dev_t dev, int unit, int slice, int part, char *partname)
596 static char name[32];
599 dname = dev_dname(dev);
600 if (strlen(dname) > 16)
601 dname = "nametoolong";
602 snprintf(name, sizeof(name), "%s%d", dname, unit);
604 if (slice != WHOLE_DISK_SLICE || part != RAW_PART) {
605 partname[0] = 'a' + part;
607 if (slice != COMPATIBILITY_SLICE) {
608 snprintf(name + strlen(name),
609 sizeof(name) - strlen(name), "s%d", slice - 1);
616 * This should only be called when the unit is inactive and the strategy
617 * routine should not allow it to become active unless we call it. Our
618 * strategy routine must be special to allow activity.
621 dsopen(dev_t dev, int mode, u_int flags,
622 struct diskslices **sspp, struct disklabel *lp)
626 struct disklabel *lp1;
634 struct diskslice *sp;
635 struct diskslices *ssp;
638 dev->si_bsize_phys = lp->d_secsize;
641 if (lp->d_secsize % DEV_BSIZE) {
642 printf("%s: invalid sector size %lu\n", devtoname(dev),
643 (u_long)lp->d_secsize);
648 * XXX reinitialize the slice table unless there is an open device
649 * on the unit. This should only be done if the media has changed.
652 need_init = !dsisopen(ssp);
653 if (ssp != NULL && need_init)
657 * Allocate a minimal slices "struct". This will become
658 * the final slices "struct" if we don't want real slices
659 * or if we can't find any real slices.
661 *sspp = dsmakeslicestruct(BASE_SLICE, lp);
663 if (!(flags & DSO_ONESLICE)) {
665 error = dsinit(dev, lp, sspp);
672 ssp->dss_oflags = flags;
675 * If there are no real slices, then make the compatiblity
676 * slice cover the whole disk.
678 if (ssp->dss_nslices == BASE_SLICE)
679 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
682 /* Point the compatibility slice at the BSD slice, if any. */
683 for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
684 sp = &ssp->dss_slices[slice];
685 if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
686 ssp->dss_first_bsd_slice = slice;
687 ssp->dss_slices[COMPATIBILITY_SLICE].ds_offset
689 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
691 ssp->dss_slices[COMPATIBILITY_SLICE].ds_type
697 ssp->dss_slices[WHOLE_DISK_SLICE].ds_label = clone_label(lp);
698 ssp->dss_slices[WHOLE_DISK_SLICE].ds_wlabel = TRUE;
702 * Initialize secondary info for all slices. It is needed for more
703 * than the current slice in the DEVFS case. XXX DEVFS is no more.
705 for (slice = 0; slice < ssp->dss_nslices; slice++) {
706 sp = &ssp->dss_slices[slice];
707 if (sp->ds_label != NULL)
709 dev1 = dkmodslice(dkmodpart(dev, RAW_PART), slice);
710 sname = dsname(dev, unit, slice, RAW_PART, partname);
712 * XXX this should probably only be done for the need_init
713 * case, but there may be a problem with DIOCSYNCSLICEINFO.
715 set_ds_wlabel(ssp, slice, TRUE); /* XXX invert */
716 lp1 = clone_label(lp);
717 TRACE(("readdisklabel\n"));
718 if (flags & DSO_NOLABELS)
721 msg = readdisklabel(dev1, lp1);
724 * readdisklabel() returns NULL for success, and an
725 * error string for failure.
727 * If there isn't a label on the disk, and if the
728 * DSO_COMPATLABEL is set, we want to use the
729 * faked-up label provided by the caller.
731 * So we set msg to NULL to indicate that there is
732 * no failure (since we have a faked-up label),
733 * free lp1, and then clone it again from lp.
734 * (In case readdisklabel() modified lp1.)
736 if (msg != NULL && (flags & DSO_COMPATLABEL)) {
739 lp1 = clone_label(lp);
743 msg = fixlabel(sname, sp, lp1, FALSE);
744 if (msg == NULL && lp1->d_secsize != ssp->dss_secsize)
745 msg = "inconsistent sector size";
747 if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
748 log(LOG_WARNING, "%s: cannot find label (%s)\n",
753 if (lp1->d_flags & D_BADSECT) {
754 log(LOG_ERR, "%s: bad sector table not supported\n",
759 set_ds_label(ssp, slice, lp1);
760 set_ds_wlabel(ssp, slice, FALSE);
763 slice = dkslice(dev);
764 if (slice >= ssp->dss_nslices)
766 sp = &ssp->dss_slices[slice];
769 && (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions))
770 return (EINVAL); /* XXX needs translation */
772 sp->ds_openmask |= mask;
777 dssize(dev_t dev, struct diskslices **sspp)
779 struct disklabel *lp;
782 struct diskslices *ssp;
784 slice = dkslice(dev);
787 if (ssp == NULL || slice >= ssp->dss_nslices
788 || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
789 if (dev_dopen(dev, FREAD, S_IFCHR, NULL) != 0)
791 dev_dclose(dev, FREAD, S_IFCHR, NULL);
794 lp = ssp->dss_slices[slice].ds_label;
797 return ((int)lp->d_partitions[part].p_size);
801 free_ds_label(struct diskslices *ssp, int slice)
803 struct disklabel *lp;
804 struct diskslice *sp;
806 sp = &ssp->dss_slices[slice];
811 set_ds_label(ssp, slice, (struct disklabel *)NULL);
815 fixlabel(char *sname, struct diskslice *sp, struct disklabel *lp, int writeflag)
820 struct partition *pp;
824 /* These errors "can't happen" so don't bother reporting details. */
825 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC)
826 return ("fixlabel: invalid magic");
827 if (dkcksum(lp) != 0)
828 return ("fixlabel: invalid checksum");
830 pp = &lp->d_partitions[RAW_PART];
833 offset = sp->ds_offset;
835 start = sp->ds_offset;
836 offset = -sp->ds_offset;
838 if (pp->p_offset != start) {
841 "%s: rejecting BSD label: raw partition offset != slice offset\n",
843 slice_info(sname, sp);
844 partition_info(sname, RAW_PART, pp);
846 return ("fixlabel: raw partition offset != slice offset");
848 if (pp->p_size != sp->ds_size) {
850 printf("%s: raw partition size != slice size\n", sname);
851 slice_info(sname, sp);
852 partition_info(sname, RAW_PART, pp);
854 if (pp->p_size > sp->ds_size) {
856 return ("fixlabel: raw partition size > slice size");
857 printf("%s: truncating raw partition\n", sname);
858 pp->p_size = sp->ds_size;
861 end = start + sp->ds_size;
863 return ("fixlabel: slice wraps");
864 if (lp->d_secpercyl <= 0)
865 return ("fixlabel: d_secpercyl <= 0");
868 for (part = 0; part < lp->d_npartitions; part++, pp++) {
869 if (pp->p_offset != 0 || pp->p_size != 0) {
870 if (pp->p_offset < start
871 || pp->p_offset + pp->p_size > end
872 || pp->p_offset + pp->p_size < pp->p_offset) {
875 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
878 slice_info(sname, sp);
881 partition_info(sname, part, pp);
883 /* XXX else silently discard junk. */
884 bzero(pp, sizeof *pp);
886 pp->p_offset += offset;
889 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
890 lp->d_secperunit = sp->ds_size;
892 lp->d_checksum = dkcksum(lp);
897 partition_info(char *sname, int part, struct partition *pp)
899 printf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
900 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
905 slice_info(char *sname, struct diskslice *sp)
907 printf("%s: start %lu, end %lu, size %lu\n", sname,
908 sp->ds_offset, sp->ds_offset + sp->ds_size - 1, sp->ds_size);
912 set_ds_label(struct diskslices *ssp, int slice, struct disklabel *lp)
914 ssp->dss_slices[slice].ds_label = lp;
915 if (slice == COMPATIBILITY_SLICE)
916 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_label = lp;
917 else if (slice == ssp->dss_first_bsd_slice)
918 ssp->dss_slices[COMPATIBILITY_SLICE].ds_label = lp;
922 set_ds_wlabel(struct diskslices *ssp, int slice, int wlabel)
924 ssp->dss_slices[slice].ds_wlabel = wlabel;
925 if (slice == COMPATIBILITY_SLICE)
926 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_wlabel = wlabel;
927 else if (slice == ssp->dss_first_bsd_slice)
928 ssp->dss_slices[COMPATIBILITY_SLICE].ds_wlabel = wlabel;