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.18 2006/05/03 20:44:49 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);
161 bp->b_error = EINVAL;
164 sp = &ssp->dss_slices[dkslice(dev)];
167 if (ssp->dss_secmult == 1) {
170 } else if (ssp->dss_secshift != -1) {
171 shift = DEV_BSHIFT + ssp->dss_secshift;
173 mask = (1 << shift) - 1;
174 if ((int)bp->b_bcount & mask)
176 if ((int)bio->bio_offset & mask)
178 secno = (daddr_t)(bio->bio_offset >> shift);
179 nsec = bp->b_bcount >> shift;
181 if (bp->b_bcount % ssp->dss_secsize)
183 if (bio->bio_offset % ssp->dss_secsize)
185 secno = (daddr_t)(bio->bio_offset / ssp->dss_secsize);
186 nsec = bp->b_bcount / ssp->dss_secsize;
189 labelsect = -LABELSECTOR - 1;
190 endsecno = sp->ds_size;
191 slicerel_secno = secno;
193 labelsect = lp->d_partitions[LABEL_PART].p_offset;
195 Debugger("labelsect != 0 in dscheck()");
196 pp = &lp->d_partitions[dkpart(dev)];
197 endsecno = pp->p_size;
198 slicerel_secno = pp->p_offset + secno;
201 /* overwriting disk label ? */
202 /* XXX should also protect bootstrap in first 8K */
203 if (slicerel_secno <= LABELSECTOR + labelsect &&
205 slicerel_secno + nsec > LABELSECTOR + labelsect &&
207 bp->b_cmd != BUF_CMD_READ && sp->ds_wlabel == 0) {
212 #if defined(DOSBBSECTOR) && defined(notyet)
213 /* overwriting master boot record? */
214 if (slicerel_secno <= DOSBBSECTOR && bp->b_cmd != BUF_CMD_READ &&
215 sp->ds_wlabel == 0) {
221 /* beyond partition? */
222 if (secno + nsec > endsecno) {
224 * If exactly at end of disk, return an EOF. There's no
225 * point keeping the buffer around so mark it B_INVAL as
228 if (secno == endsecno) {
229 bp->b_resid = bp->b_bcount;
230 bp->b_flags |= B_INVAL;
233 /* or truncate if part of it fits */
234 nsec = endsecno - secno;
236 bp->b_error = EINVAL;
239 bp->b_bcount = nsec * ssp->dss_secsize;
242 nbio = push_bio(bio);
243 nbio->bio_offset = (off_t)(sp->ds_offset + slicerel_secno) * ssp->dss_secsize;
246 * Snoop on label accesses if the slice offset is nonzero. Fudge
247 * offsets in the label to keep the in-core label coherent with
250 if (slicerel_secno <= LABELSECTOR + labelsect
252 && slicerel_secno + nsec > LABELSECTOR + labelsect
254 && sp->ds_offset != 0) {
255 nbio->bio_done = dsiodone;
256 nbio->bio_caller_info1.ptr = sp;
257 nbio->bio_caller_info2.offset = (off_t)(LABELSECTOR + labelsect -
258 slicerel_secno) * ssp->dss_secsize;
259 if (bp->b_cmd != BUF_CMD_READ) {
261 * XXX even disklabel(8) writes directly so we need
262 * to adjust writes. Perhaps we should drop support
263 * for DIOCWLABEL (always write protect labels) and
264 * require the use of DIOCWDINFO.
266 * XXX probably need to copy the data to avoid even
267 * temporarily corrupting the in-core copy.
269 /* XXX need name here. */
273 (bp->b_data + (int)nbio->bio_caller_info2.offset),
276 printf("dscheck(%s): %s\n",
277 devtoname(dev), msg);
288 "dscheck(%s): b_bcount %d is not on a sector boundary (ssize %d)\n",
289 devtoname(dev), bp->b_bcount, ssp->dss_secsize);
290 bp->b_error = EINVAL;
295 "dscheck(%s): bio_offset %lld is not on a sector boundary (ssize %d)\n",
296 devtoname(dev), bio->bio_offset, ssp->dss_secsize);
297 bp->b_error = EINVAL;
302 * Terminate the I/O with a ranging error. Since the buffer is
303 * either illegal or beyond the file EOF, mark it B_INVAL as well.
305 bp->b_resid = bp->b_bcount;
306 bp->b_flags |= B_ERROR | B_INVAL;
311 dsclose(dev_t dev, int mode, struct diskslices *ssp)
314 struct diskslice *sp;
316 sp = &ssp->dss_slices[dkslice(dev)];
317 mask = 1 << dkpart(dev);
318 sp->ds_openmask &= ~mask;
322 dsgone(struct diskslices **sspp)
325 struct diskslice *sp;
326 struct diskslices *ssp;
328 for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
329 sp = &ssp->dss_slices[slice];
330 free_ds_label(ssp, slice);
337 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
338 * is subject to the same restriction as dsopen().
341 dsioctl(dev_t dev, u_long cmd, caddr_t data,
342 int flags, struct diskslices **sspp)
345 struct disklabel *lp;
350 struct diskslice *sp;
351 struct diskslices *ssp;
352 struct partition *pp;
354 slice = dkslice(dev);
356 sp = &ssp->dss_slices[slice];
361 lp = (struct disklabel *)data;
362 if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
363 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
365 bzero(lp, sizeof(struct disklabel));
368 lp->d_magic = DISKMAGIC;
369 lp->d_magic2 = DISKMAGIC;
370 pp = &lp->d_partitions[RAW_PART];
372 pp->p_size = sp->ds_size;
374 lp->d_npartitions = MAXPARTITIONS;
375 if (lp->d_interleave == 0)
376 lp->d_interleave = 1;
379 if (lp->d_nsectors == 0)
381 if (lp->d_ntracks == 0)
384 lp->d_bbsize = BBSIZE;
385 lp->d_sbsize = SBSIZE;
386 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
387 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
388 lp->d_secperunit = sp->ds_size;
390 lp->d_checksum = dkcksum(lp);
396 *(struct disklabel *)data = *lp;
403 *(struct disklabel **)data = lp;
410 ((struct partinfo *)data)->disklab = lp;
411 ((struct partinfo *)data)->part
412 = &lp->d_partitions[dkpart(dev)];
416 bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
421 if (slice == WHOLE_DISK_SLICE)
423 if (!(flags & FWRITE))
425 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
426 if (sp->ds_label == NULL)
427 bzero(lp, sizeof *lp);
429 bcopy(sp->ds_label, lp, sizeof *lp);
430 if (sp->ds_label == NULL)
433 openmask = sp->ds_openmask;
434 if (slice == COMPATIBILITY_SLICE)
435 openmask |= ssp->dss_slices[
436 ssp->dss_first_bsd_slice].ds_openmask;
437 else if (slice == ssp->dss_first_bsd_slice)
438 openmask |= ssp->dss_slices[
439 COMPATIBILITY_SLICE].ds_openmask;
441 error = setdisklabel(lp, (struct disklabel *)data,
443 /* XXX why doesn't setdisklabel() check this? */
444 if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
447 if (lp->d_secperunit > sp->ds_size)
449 for (part = 0; part < lp->d_npartitions; part++)
450 if (lp->d_partitions[part].p_size > sp->ds_size)
457 free_ds_label(ssp, slice);
458 set_ds_label(ssp, slice, lp);
461 case DIOCSYNCSLICEINFO:
462 if (slice != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART)
465 for (slice = 0; slice < ssp->dss_nslices; slice++) {
466 openmask = ssp->dss_slices[slice].ds_openmask;
468 && (slice != WHOLE_DISK_SLICE
469 || openmask & ~(1 << RAW_PART)))
474 * Temporarily forget the current slices struct and read
476 * XXX should wait for current accesses on this disk to
477 * complete, then lock out future accesses and opens.
480 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
481 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
482 error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, lp);
490 * Reopen everything. This is a no-op except in the "force"
491 * case and when the raw bdev and cdev are both open. Abort
494 for (slice = 0; slice < ssp->dss_nslices; slice++) {
495 for (openmask = ssp->dss_slices[slice].ds_openmask,
496 part = 0; openmask; openmask >>= 1, part++) {
499 error = dsopen(dkmodslice(dkmodpart(dev, part),
501 S_IFCHR, ssp->dss_oflags, sspp,
516 error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp);
520 * XXX this used to hack on dk_openpart to fake opening
521 * partition 0 in case that is used instead of dkpart(dev).
523 old_wlabel = sp->ds_wlabel;
524 set_ds_wlabel(ssp, slice, TRUE);
525 error = writedisklabel(dev, sp->ds_label);
526 /* XXX should invalidate in-core label if write failed. */
527 set_ds_wlabel(ssp, slice, old_wlabel);
531 if (slice == WHOLE_DISK_SLICE)
533 if (!(flags & FWRITE))
535 set_ds_wlabel(ssp, slice, *(int *)data != 0);
544 * Chain the bio_done. b_cmd remains valid through such chaining.
547 dsiodone(struct bio *bio)
549 struct buf *bp = bio->bio_buf;
552 if (bp->b_cmd != BUF_CMD_READ
553 || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
554 msg = fixlabel(NULL, bio->bio_caller_info1.ptr,
556 (bp->b_data + (int)bio->bio_caller_info2.offset),
561 biodone(bio->bio_prev);
565 dsisopen(struct diskslices *ssp)
571 for (slice = 0; slice < ssp->dss_nslices; slice++) {
572 if (ssp->dss_slices[slice].ds_openmask)
579 * Allocate a slices "struct" and initialize it to contain only an empty
580 * compatibility slice (pointing to itself), a whole disk slice (covering
581 * the disk as described by the label), and (nslices - BASE_SLICES) empty
582 * slices beginning at BASE_SLICE.
585 dsmakeslicestruct(int nslices, struct disklabel *lp)
587 struct diskslice *sp;
588 struct diskslices *ssp;
590 ssp = malloc(offsetof(struct diskslices, dss_slices) +
591 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
592 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
593 ssp->dss_nslices = nslices;
595 ssp->dss_secmult = lp->d_secsize / DEV_BSIZE;
596 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
597 ssp->dss_secshift = -1;
599 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
600 ssp->dss_secsize = lp->d_secsize;
601 sp = &ssp->dss_slices[0];
602 bzero(sp, nslices * sizeof *sp);
603 sp[WHOLE_DISK_SLICE].ds_size = lp->d_secperunit;
608 dsname(dev_t dev, int unit, int slice, int part, char *partname)
610 static char name[32];
613 dname = dev_dname(dev);
614 if (strlen(dname) > 16)
615 dname = "nametoolong";
616 snprintf(name, sizeof(name), "%s%d", dname, unit);
618 if (slice != WHOLE_DISK_SLICE || part != RAW_PART) {
619 partname[0] = 'a' + part;
621 if (slice != COMPATIBILITY_SLICE) {
622 snprintf(name + strlen(name),
623 sizeof(name) - strlen(name), "s%d", slice - 1);
630 * This should only be called when the unit is inactive and the strategy
631 * routine should not allow it to become active unless we call it. Our
632 * strategy routine must be special to allow activity.
635 dsopen(dev_t dev, int mode, u_int flags,
636 struct diskslices **sspp, struct disklabel *lp)
640 struct disklabel *lp1;
648 struct diskslice *sp;
649 struct diskslices *ssp;
652 dev->si_bsize_phys = lp->d_secsize;
655 if (lp->d_secsize % DEV_BSIZE) {
656 printf("%s: invalid sector size %lu\n", devtoname(dev),
657 (u_long)lp->d_secsize);
662 * XXX reinitialize the slice table unless there is an open device
663 * on the unit. This should only be done if the media has changed.
666 need_init = !dsisopen(ssp);
667 if (ssp != NULL && need_init)
671 * Allocate a minimal slices "struct". This will become
672 * the final slices "struct" if we don't want real slices
673 * or if we can't find any real slices.
675 *sspp = dsmakeslicestruct(BASE_SLICE, lp);
677 if (!(flags & DSO_ONESLICE)) {
679 error = dsinit(dev, lp, sspp);
686 ssp->dss_oflags = flags;
689 * If there are no real slices, then make the compatiblity
690 * slice cover the whole disk.
692 if (ssp->dss_nslices == BASE_SLICE)
693 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
696 /* Point the compatibility slice at the BSD slice, if any. */
697 for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
698 sp = &ssp->dss_slices[slice];
699 if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
700 ssp->dss_first_bsd_slice = slice;
701 ssp->dss_slices[COMPATIBILITY_SLICE].ds_offset
703 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
705 ssp->dss_slices[COMPATIBILITY_SLICE].ds_type
711 ssp->dss_slices[WHOLE_DISK_SLICE].ds_label = clone_label(lp);
712 ssp->dss_slices[WHOLE_DISK_SLICE].ds_wlabel = TRUE;
716 * Initialize secondary info for all slices. It is needed for more
717 * than the current slice in the DEVFS case. XXX DEVFS is no more.
719 for (slice = 0; slice < ssp->dss_nslices; slice++) {
720 sp = &ssp->dss_slices[slice];
721 if (sp->ds_label != NULL)
723 dev1 = dkmodslice(dkmodpart(dev, RAW_PART), slice);
724 sname = dsname(dev, unit, slice, RAW_PART, partname);
726 * XXX this should probably only be done for the need_init
727 * case, but there may be a problem with DIOCSYNCSLICEINFO.
729 set_ds_wlabel(ssp, slice, TRUE); /* XXX invert */
730 lp1 = clone_label(lp);
731 TRACE(("readdisklabel\n"));
732 if (flags & DSO_NOLABELS)
735 msg = readdisklabel(dev1, lp1);
738 * readdisklabel() returns NULL for success, and an
739 * error string for failure.
741 * If there isn't a label on the disk, and if the
742 * DSO_COMPATLABEL is set, we want to use the
743 * faked-up label provided by the caller.
745 * So we set msg to NULL to indicate that there is
746 * no failure (since we have a faked-up label),
747 * free lp1, and then clone it again from lp.
748 * (In case readdisklabel() modified lp1.)
750 if (msg != NULL && (flags & DSO_COMPATLABEL)) {
753 lp1 = clone_label(lp);
757 msg = fixlabel(sname, sp, lp1, FALSE);
758 if (msg == NULL && lp1->d_secsize != ssp->dss_secsize)
759 msg = "inconsistent sector size";
761 if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
762 log(LOG_WARNING, "%s: cannot find label (%s)\n",
767 if (lp1->d_flags & D_BADSECT) {
768 log(LOG_ERR, "%s: bad sector table not supported\n",
773 set_ds_label(ssp, slice, lp1);
774 set_ds_wlabel(ssp, slice, FALSE);
777 slice = dkslice(dev);
778 if (slice >= ssp->dss_nslices)
780 sp = &ssp->dss_slices[slice];
783 && (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions))
784 return (EINVAL); /* XXX needs translation */
786 sp->ds_openmask |= mask;
791 dssize(dev_t dev, struct diskslices **sspp)
793 struct disklabel *lp;
796 struct diskslices *ssp;
798 slice = dkslice(dev);
801 if (ssp == NULL || slice >= ssp->dss_nslices
802 || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
803 if (dev_dopen(dev, FREAD, S_IFCHR, NULL) != 0)
805 dev_dclose(dev, FREAD, S_IFCHR, NULL);
808 lp = ssp->dss_slices[slice].ds_label;
811 return ((int)lp->d_partitions[part].p_size);
815 free_ds_label(struct diskslices *ssp, int slice)
817 struct disklabel *lp;
818 struct diskslice *sp;
820 sp = &ssp->dss_slices[slice];
825 set_ds_label(ssp, slice, (struct disklabel *)NULL);
829 fixlabel(char *sname, struct diskslice *sp, struct disklabel *lp, int writeflag)
834 struct partition *pp;
838 /* These errors "can't happen" so don't bother reporting details. */
839 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC)
840 return ("fixlabel: invalid magic");
841 if (dkcksum(lp) != 0)
842 return ("fixlabel: invalid checksum");
844 pp = &lp->d_partitions[RAW_PART];
847 offset = sp->ds_offset;
849 start = sp->ds_offset;
850 offset = -sp->ds_offset;
852 if (pp->p_offset != start) {
855 "%s: rejecting BSD label: raw partition offset != slice offset\n",
857 slice_info(sname, sp);
858 partition_info(sname, RAW_PART, pp);
860 return ("fixlabel: raw partition offset != slice offset");
862 if (pp->p_size != sp->ds_size) {
864 printf("%s: raw partition size != slice size\n", sname);
865 slice_info(sname, sp);
866 partition_info(sname, RAW_PART, pp);
868 if (pp->p_size > sp->ds_size) {
870 return ("fixlabel: raw partition size > slice size");
871 printf("%s: truncating raw partition\n", sname);
872 pp->p_size = sp->ds_size;
875 end = start + sp->ds_size;
877 return ("fixlabel: slice wraps");
878 if (lp->d_secpercyl <= 0)
879 return ("fixlabel: d_secpercyl <= 0");
882 for (part = 0; part < lp->d_npartitions; part++, pp++) {
883 if (pp->p_offset != 0 || pp->p_size != 0) {
884 if (pp->p_offset < start
885 || pp->p_offset + pp->p_size > end
886 || pp->p_offset + pp->p_size < pp->p_offset) {
889 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
892 slice_info(sname, sp);
895 partition_info(sname, part, pp);
897 /* XXX else silently discard junk. */
898 bzero(pp, sizeof *pp);
900 pp->p_offset += offset;
903 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
904 lp->d_secperunit = sp->ds_size;
906 lp->d_checksum = dkcksum(lp);
911 partition_info(char *sname, int part, struct partition *pp)
913 printf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
914 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
919 slice_info(char *sname, struct diskslice *sp)
921 printf("%s: start %lu, end %lu, size %lu\n", sname,
922 sp->ds_offset, sp->ds_offset + sp->ds_size - 1, sp->ds_size);
926 set_ds_label(struct diskslices *ssp, int slice, struct disklabel *lp)
928 ssp->dss_slices[slice].ds_label = lp;
929 if (slice == COMPATIBILITY_SLICE)
930 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_label = lp;
931 else if (slice == ssp->dss_first_bsd_slice)
932 ssp->dss_slices[COMPATIBILITY_SLICE].ds_label = lp;
936 set_ds_wlabel(struct diskslices *ssp, int slice, int wlabel)
938 ssp->dss_slices[slice].ds_wlabel = wlabel;
939 if (slice == COMPATIBILITY_SLICE)
940 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_wlabel = wlabel;
941 else if (slice == ssp->dss_first_bsd_slice)
942 ssp->dss_slices[COMPATIBILITY_SLICE].ds_wlabel = wlabel;