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.12 2005/08/26 12:45:53 hmp 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 buf *bp);
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(struct buf *bp, struct diskslices *ssp)
146 struct disklabel *lp;
149 struct partition *pp;
151 daddr_t slicerel_secno;
152 struct diskslice *sp;
156 printf("dscheck(%s): negative b_blkno %ld\n",
157 devtoname(bp->b_dev), (long)blkno);
158 bp->b_error = EINVAL;
161 sp = &ssp->dss_slices[dkslice(bp->b_dev)];
163 if (ssp->dss_secmult == 1) {
164 if (bp->b_bcount % (u_long)DEV_BSIZE)
167 nsec = bp->b_bcount >> DEV_BSHIFT;
168 } else if (ssp->dss_secshift != -1) {
169 if (bp->b_bcount & (ssp->dss_secsize - 1))
171 if (blkno & (ssp->dss_secmult - 1))
173 secno = blkno >> ssp->dss_secshift;
174 nsec = bp->b_bcount >> (DEV_BSHIFT + ssp->dss_secshift);
176 if (bp->b_bcount % ssp->dss_secsize)
178 if (blkno % ssp->dss_secmult)
180 secno = blkno / ssp->dss_secmult;
181 nsec = bp->b_bcount / ssp->dss_secsize;
184 labelsect = -LABELSECTOR - 1;
185 endsecno = sp->ds_size;
186 slicerel_secno = secno;
188 labelsect = lp->d_partitions[LABEL_PART].p_offset;
190 Debugger("labelsect != 0 in dscheck()");
191 pp = &lp->d_partitions[dkpart(bp->b_dev)];
192 endsecno = pp->p_size;
193 slicerel_secno = pp->p_offset + secno;
196 /* overwriting disk label ? */
197 /* XXX should also protect bootstrap in first 8K */
198 if (slicerel_secno <= LABELSECTOR + labelsect &&
200 slicerel_secno + nsec > LABELSECTOR + labelsect &&
202 (bp->b_flags & B_READ) == 0 && sp->ds_wlabel == 0) {
207 #if defined(DOSBBSECTOR) && defined(notyet)
208 /* overwriting master boot record? */
209 if (slicerel_secno <= DOSBBSECTOR && (bp->b_flags & B_READ) == 0 &&
210 sp->ds_wlabel == 0) {
216 /* beyond partition? */
217 if (secno + nsec > endsecno) {
218 /* if exactly at end of disk, return an EOF */
219 if (secno == endsecno) {
220 bp->b_resid = bp->b_bcount;
223 /* or truncate if part of it fits */
224 nsec = endsecno - secno;
226 bp->b_error = EINVAL;
229 bp->b_bcount = nsec * ssp->dss_secsize;
232 bp->b_pblkno = sp->ds_offset + slicerel_secno;
235 * Snoop on label accesses if the slice offset is nonzero. Fudge
236 * offsets in the label to keep the in-core label coherent with
239 if (slicerel_secno <= LABELSECTOR + labelsect
241 && slicerel_secno + nsec > LABELSECTOR + labelsect
243 && sp->ds_offset != 0) {
244 struct iodone_chain *ic;
246 ic = malloc(sizeof *ic , M_DEVBUF, M_WAITOK);
247 ic->ic_prev_flags = bp->b_flags;
248 ic->ic_prev_iodone = bp->b_iodone;
249 ic->ic_prev_iodone_chain = bp->b_iodone_chain;
250 ic->ic_args[0].ia_long = (LABELSECTOR + labelsect -
251 slicerel_secno) * ssp->dss_secsize;
252 ic->ic_args[1].ia_ptr = sp;
253 bp->b_iodone = dsiodone;
254 bp->b_iodone_chain = ic;
255 if (!(bp->b_flags & B_READ)) {
257 * XXX even disklabel(8) writes directly so we need
258 * to adjust writes. Perhaps we should drop support
259 * for DIOCWLABEL (always write protect labels) and
260 * require the use of DIOCWDINFO.
262 * XXX probably need to copy the data to avoid even
263 * temporarily corrupting the in-core copy.
265 if (bp->b_vp != NULL) {
267 bp->b_vp->v_numoutput++;
270 /* XXX need name here. */
271 msg = fixlabel((char *)NULL, sp,
273 (bp->b_data + ic->ic_args[0].ia_long),
276 printf("dscheck(%s): %s\n",
277 devtoname(bp->b_dev), msg);
287 "dscheck(%s): b_bcount %ld is not on a sector boundary (ssize %d)\n",
288 devtoname(bp->b_dev), bp->b_bcount, ssp->dss_secsize);
289 bp->b_error = EINVAL;
294 "dscheck(%s): b_blkno %ld is not on a sector boundary (ssize %d)\n",
295 devtoname(bp->b_dev), (long)blkno, ssp->dss_secsize);
296 bp->b_error = EINVAL;
300 bp->b_resid = bp->b_bcount;
301 bp->b_flags |= B_ERROR;
306 dsclose(dev_t dev, int mode, struct diskslices *ssp)
309 struct diskslice *sp;
311 sp = &ssp->dss_slices[dkslice(dev)];
312 mask = 1 << dkpart(dev);
313 sp->ds_openmask &= ~mask;
317 dsgone(struct diskslices **sspp)
320 struct diskslice *sp;
321 struct diskslices *ssp;
323 for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
324 sp = &ssp->dss_slices[slice];
325 free_ds_label(ssp, slice);
332 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
333 * is subject to the same restriction as dsopen().
336 dsioctl(dev_t dev, u_long cmd, caddr_t data,
337 int flags, struct diskslices **sspp)
340 struct disklabel *lp;
345 struct diskslice *sp;
346 struct diskslices *ssp;
347 struct partition *pp;
349 slice = dkslice(dev);
351 sp = &ssp->dss_slices[slice];
356 lp = (struct disklabel *)data;
357 if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
358 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
360 bzero(lp, sizeof(struct disklabel));
363 lp->d_magic = DISKMAGIC;
364 lp->d_magic2 = DISKMAGIC;
365 pp = &lp->d_partitions[RAW_PART];
367 pp->p_size = sp->ds_size;
369 lp->d_npartitions = MAXPARTITIONS;
370 if (lp->d_interleave == 0)
371 lp->d_interleave = 1;
374 if (lp->d_nsectors == 0)
376 if (lp->d_ntracks == 0)
379 lp->d_bbsize = BBSIZE;
380 lp->d_sbsize = SBSIZE;
381 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
382 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
383 lp->d_secperunit = sp->ds_size;
385 lp->d_checksum = dkcksum(lp);
391 *(struct disklabel *)data = *lp;
398 *(struct disklabel **)data = lp;
405 ((struct partinfo *)data)->disklab = lp;
406 ((struct partinfo *)data)->part
407 = &lp->d_partitions[dkpart(dev)];
411 bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
416 if (slice == WHOLE_DISK_SLICE)
418 if (!(flags & FWRITE))
420 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
421 if (sp->ds_label == NULL)
422 bzero(lp, sizeof *lp);
424 bcopy(sp->ds_label, lp, sizeof *lp);
425 if (sp->ds_label == NULL)
428 openmask = sp->ds_openmask;
429 if (slice == COMPATIBILITY_SLICE)
430 openmask |= ssp->dss_slices[
431 ssp->dss_first_bsd_slice].ds_openmask;
432 else if (slice == ssp->dss_first_bsd_slice)
433 openmask |= ssp->dss_slices[
434 COMPATIBILITY_SLICE].ds_openmask;
436 error = setdisklabel(lp, (struct disklabel *)data,
438 /* XXX why doesn't setdisklabel() check this? */
439 if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
442 if (lp->d_secperunit > sp->ds_size)
444 for (part = 0; part < lp->d_npartitions; part++)
445 if (lp->d_partitions[part].p_size > sp->ds_size)
452 free_ds_label(ssp, slice);
453 set_ds_label(ssp, slice, lp);
456 case DIOCSYNCSLICEINFO:
457 if (slice != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART)
460 for (slice = 0; slice < ssp->dss_nslices; slice++) {
461 openmask = ssp->dss_slices[slice].ds_openmask;
463 && (slice != WHOLE_DISK_SLICE
464 || openmask & ~(1 << RAW_PART)))
469 * Temporarily forget the current slices struct and read
471 * XXX should wait for current accesses on this disk to
472 * complete, then lock out future accesses and opens.
475 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
476 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
477 error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, lp);
485 * Reopen everything. This is a no-op except in the "force"
486 * case and when the raw bdev and cdev are both open. Abort
489 for (slice = 0; slice < ssp->dss_nslices; slice++) {
490 for (openmask = ssp->dss_slices[slice].ds_openmask,
491 part = 0; openmask; openmask >>= 1, part++) {
494 error = dsopen(dkmodslice(dkmodpart(dev, part),
496 S_IFCHR, ssp->dss_oflags, sspp,
511 error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp);
515 * XXX this used to hack on dk_openpart to fake opening
516 * partition 0 in case that is used instead of dkpart(dev).
518 old_wlabel = sp->ds_wlabel;
519 set_ds_wlabel(ssp, slice, TRUE);
520 error = writedisklabel(dev, sp->ds_label);
521 /* XXX should invalidate in-core label if write failed. */
522 set_ds_wlabel(ssp, slice, old_wlabel);
526 if (slice == WHOLE_DISK_SLICE)
528 if (!(flags & FWRITE))
530 set_ds_wlabel(ssp, slice, *(int *)data != 0);
539 dsiodone(struct buf *bp)
541 struct iodone_chain *ic;
544 ic = bp->b_iodone_chain;
545 bp->b_flags = bp->b_flags & ~B_DONE;
546 bp->b_iodone = ic->ic_prev_iodone;
547 bp->b_iodone_chain = ic->ic_prev_iodone_chain;
548 if (!(bp->b_flags & B_READ)
549 || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
550 msg = fixlabel((char *)NULL, ic->ic_args[1].ia_ptr,
552 (bp->b_data + ic->ic_args[0].ia_long),
562 dsisopen(struct diskslices *ssp)
568 for (slice = 0; slice < ssp->dss_nslices; slice++) {
569 if (ssp->dss_slices[slice].ds_openmask)
576 * Allocate a slices "struct" and initialize it to contain only an empty
577 * compatibility slice (pointing to itself), a whole disk slice (covering
578 * the disk as described by the label), and (nslices - BASE_SLICES) empty
579 * slices beginning at BASE_SLICE.
582 dsmakeslicestruct(int nslices, struct disklabel *lp)
584 struct diskslice *sp;
585 struct diskslices *ssp;
587 ssp = malloc(offsetof(struct diskslices, dss_slices) +
588 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
589 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
590 ssp->dss_nslices = nslices;
592 ssp->dss_secmult = lp->d_secsize / DEV_BSIZE;
593 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
594 ssp->dss_secshift = -1;
596 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
597 ssp->dss_secsize = lp->d_secsize;
598 sp = &ssp->dss_slices[0];
599 bzero(sp, nslices * sizeof *sp);
600 sp[WHOLE_DISK_SLICE].ds_size = lp->d_secperunit;
605 dsname(dev_t dev, int unit, int slice, int part, char *partname)
607 static char name[32];
610 dname = dev_dname(dev);
611 if (strlen(dname) > 16)
612 dname = "nametoolong";
613 snprintf(name, sizeof(name), "%s%d", dname, unit);
615 if (slice != WHOLE_DISK_SLICE || part != RAW_PART) {
616 partname[0] = 'a' + part;
618 if (slice != COMPATIBILITY_SLICE) {
619 snprintf(name + strlen(name),
620 sizeof(name) - strlen(name), "s%d", slice - 1);
627 * This should only be called when the unit is inactive and the strategy
628 * routine should not allow it to become active unless we call it. Our
629 * strategy routine must be special to allow activity.
632 dsopen(dev_t dev, int mode, u_int flags,
633 struct diskslices **sspp, struct disklabel *lp)
637 struct disklabel *lp1;
645 struct diskslice *sp;
646 struct diskslices *ssp;
649 dev->si_bsize_phys = lp->d_secsize;
652 if (lp->d_secsize % DEV_BSIZE) {
653 printf("%s: invalid sector size %lu\n", devtoname(dev),
654 (u_long)lp->d_secsize);
659 * XXX reinitialize the slice table unless there is an open device
660 * on the unit. This should only be done if the media has changed.
663 need_init = !dsisopen(ssp);
664 if (ssp != NULL && need_init)
668 * Allocate a minimal slices "struct". This will become
669 * the final slices "struct" if we don't want real slices
670 * or if we can't find any real slices.
672 *sspp = dsmakeslicestruct(BASE_SLICE, lp);
674 if (!(flags & DSO_ONESLICE)) {
676 error = dsinit(dev, lp, sspp);
683 ssp->dss_oflags = flags;
686 * If there are no real slices, then make the compatiblity
687 * slice cover the whole disk.
689 if (ssp->dss_nslices == BASE_SLICE)
690 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
693 /* Point the compatibility slice at the BSD slice, if any. */
694 for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
695 sp = &ssp->dss_slices[slice];
696 if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
697 ssp->dss_first_bsd_slice = slice;
698 ssp->dss_slices[COMPATIBILITY_SLICE].ds_offset
700 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
702 ssp->dss_slices[COMPATIBILITY_SLICE].ds_type
708 ssp->dss_slices[WHOLE_DISK_SLICE].ds_label = clone_label(lp);
709 ssp->dss_slices[WHOLE_DISK_SLICE].ds_wlabel = TRUE;
713 * Initialize secondary info for all slices. It is needed for more
714 * than the current slice in the DEVFS case. XXX DEVFS is no more.
716 for (slice = 0; slice < ssp->dss_nslices; slice++) {
717 sp = &ssp->dss_slices[slice];
718 if (sp->ds_label != NULL)
720 dev1 = dkmodslice(dkmodpart(dev, RAW_PART), slice);
721 sname = dsname(dev, unit, slice, RAW_PART, partname);
723 * XXX this should probably only be done for the need_init
724 * case, but there may be a problem with DIOCSYNCSLICEINFO.
726 set_ds_wlabel(ssp, slice, TRUE); /* XXX invert */
727 lp1 = clone_label(lp);
728 TRACE(("readdisklabel\n"));
729 if (flags & DSO_NOLABELS)
732 msg = readdisklabel(dev1, lp1);
735 * readdisklabel() returns NULL for success, and an
736 * error string for failure.
738 * If there isn't a label on the disk, and if the
739 * DSO_COMPATLABEL is set, we want to use the
740 * faked-up label provided by the caller.
742 * So we set msg to NULL to indicate that there is
743 * no failure (since we have a faked-up label),
744 * free lp1, and then clone it again from lp.
745 * (In case readdisklabel() modified lp1.)
747 if (msg != NULL && (flags & DSO_COMPATLABEL)) {
750 lp1 = clone_label(lp);
754 msg = fixlabel(sname, sp, lp1, FALSE);
755 if (msg == NULL && lp1->d_secsize != ssp->dss_secsize)
756 msg = "inconsistent sector size";
758 if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
759 log(LOG_WARNING, "%s: cannot find label (%s)\n",
764 if (lp1->d_flags & D_BADSECT) {
765 log(LOG_ERR, "%s: bad sector table not supported\n",
770 set_ds_label(ssp, slice, lp1);
771 set_ds_wlabel(ssp, slice, FALSE);
774 slice = dkslice(dev);
775 if (slice >= ssp->dss_nslices)
777 sp = &ssp->dss_slices[slice];
780 && (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions))
781 return (EINVAL); /* XXX needs translation */
783 sp->ds_openmask |= mask;
788 dssize(dev_t dev, struct diskslices **sspp)
790 struct disklabel *lp;
793 struct diskslices *ssp;
795 slice = dkslice(dev);
798 if (ssp == NULL || slice >= ssp->dss_nslices
799 || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
800 if (dev_dopen(dev, FREAD, S_IFCHR, NULL) != 0)
802 dev_dclose(dev, FREAD, S_IFCHR, NULL);
805 lp = ssp->dss_slices[slice].ds_label;
808 return ((int)lp->d_partitions[part].p_size);
812 free_ds_label(struct diskslices *ssp, int slice)
814 struct disklabel *lp;
815 struct diskslice *sp;
817 sp = &ssp->dss_slices[slice];
822 set_ds_label(ssp, slice, (struct disklabel *)NULL);
826 fixlabel(char *sname, struct diskslice *sp, struct disklabel *lp, int writeflag)
831 struct partition *pp;
835 /* These errors "can't happen" so don't bother reporting details. */
836 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC)
837 return ("fixlabel: invalid magic");
838 if (dkcksum(lp) != 0)
839 return ("fixlabel: invalid checksum");
841 pp = &lp->d_partitions[RAW_PART];
844 offset = sp->ds_offset;
846 start = sp->ds_offset;
847 offset = -sp->ds_offset;
849 if (pp->p_offset != start) {
852 "%s: rejecting BSD label: raw partition offset != slice offset\n",
854 slice_info(sname, sp);
855 partition_info(sname, RAW_PART, pp);
857 return ("fixlabel: raw partition offset != slice offset");
859 if (pp->p_size != sp->ds_size) {
861 printf("%s: raw partition size != slice size\n", sname);
862 slice_info(sname, sp);
863 partition_info(sname, RAW_PART, pp);
865 if (pp->p_size > sp->ds_size) {
867 return ("fixlabel: raw partition size > slice size");
868 printf("%s: truncating raw partition\n", sname);
869 pp->p_size = sp->ds_size;
872 end = start + sp->ds_size;
874 return ("fixlabel: slice wraps");
875 if (lp->d_secpercyl <= 0)
876 return ("fixlabel: d_secpercyl <= 0");
879 for (part = 0; part < lp->d_npartitions; part++, pp++) {
880 if (pp->p_offset != 0 || pp->p_size != 0) {
881 if (pp->p_offset < start
882 || pp->p_offset + pp->p_size > end
883 || pp->p_offset + pp->p_size < pp->p_offset) {
886 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
889 slice_info(sname, sp);
892 partition_info(sname, part, pp);
894 /* XXX else silently discard junk. */
895 bzero(pp, sizeof *pp);
897 pp->p_offset += offset;
900 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
901 lp->d_secperunit = sp->ds_size;
903 lp->d_checksum = dkcksum(lp);
908 partition_info(char *sname, int part, struct partition *pp)
910 printf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
911 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
916 slice_info(char *sname, struct diskslice *sp)
918 printf("%s: start %lu, end %lu, size %lu\n", sname,
919 sp->ds_offset, sp->ds_offset + sp->ds_size - 1, sp->ds_size);
923 set_ds_label(struct diskslices *ssp, int slice, struct disklabel *lp)
925 ssp->dss_slices[slice].ds_label = lp;
926 if (slice == COMPATIBILITY_SLICE)
927 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_label = lp;
928 else if (slice == ssp->dss_first_bsd_slice)
929 ssp->dss_slices[COMPATIBILITY_SLICE].ds_label = lp;
933 set_ds_wlabel(struct diskslices *ssp, int slice, int wlabel)
935 ssp->dss_slices[slice].ds_wlabel = wlabel;
936 if (slice == COMPATIBILITY_SLICE)
937 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_wlabel = wlabel;
938 else if (slice == ssp->dss_first_bsd_slice)
939 ssp->dss_slices[COMPATIBILITY_SLICE].ds_wlabel = wlabel;