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.14 2006/03/24 18:35:33 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;
147 struct disklabel *lp;
150 struct partition *pp;
152 daddr_t slicerel_secno;
153 struct diskslice *sp;
157 if (bio->bio_offset < 0) {
158 printf("dscheck(%s): negative bio_offset %lld\n",
159 devtoname(dev), bio->bio_offset);
160 bp->b_error = EINVAL;
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_flags & B_READ) == 0 && sp->ds_wlabel == 0) {
211 #if defined(DOSBBSECTOR) && defined(notyet)
212 /* overwriting master boot record? */
213 if (slicerel_secno <= DOSBBSECTOR && (bp->b_flags & B_READ) == 0 &&
214 sp->ds_wlabel == 0) {
220 /* beyond partition? */
221 if (secno + nsec > endsecno) {
222 /* if exactly at end of disk, return an EOF */
223 if (secno == endsecno) {
224 bp->b_resid = bp->b_bcount;
227 /* or truncate if part of it fits */
228 nsec = endsecno - secno;
230 bp->b_error = EINVAL;
233 bp->b_bcount = nsec * ssp->dss_secsize;
236 nbio = push_bio(bio);
237 nbio->bio_offset = (off_t)(sp->ds_offset + slicerel_secno) * ssp->dss_secsize;
240 * Snoop on label accesses if the slice offset is nonzero. Fudge
241 * offsets in the label to keep the in-core label coherent with
244 if (slicerel_secno <= LABELSECTOR + labelsect
246 && slicerel_secno + nsec > LABELSECTOR + labelsect
248 && sp->ds_offset != 0) {
249 nbio->bio_done = dsiodone;
250 nbio->bio_caller_info1.ptr = sp;
251 nbio->bio_caller_info2.offset = (off_t)(LABELSECTOR + labelsect -
252 slicerel_secno) * ssp->dss_secsize;
253 if ((bp->b_flags & B_READ) == 0) {
255 * XXX even disklabel(8) writes directly so we need
256 * to adjust writes. Perhaps we should drop support
257 * for DIOCWLABEL (always write protect labels) and
258 * require the use of DIOCWDINFO.
260 * XXX probably need to copy the data to avoid even
261 * temporarily corrupting the in-core copy.
263 /* XXX need name here. */
267 (bp->b_data + (int)nbio->bio_caller_info2.offset),
270 printf("dscheck(%s): %s\n",
271 devtoname(dev), msg);
282 "dscheck(%s): b_bcount %d is not on a sector boundary (ssize %d)\n",
283 devtoname(dev), bp->b_bcount, ssp->dss_secsize);
284 bp->b_error = EINVAL;
289 "dscheck(%s): bio_offset %lld is not on a sector boundary (ssize %d)\n",
290 devtoname(dev), bio->bio_offset, ssp->dss_secsize);
291 bp->b_error = EINVAL;
295 bp->b_resid = bp->b_bcount;
296 bp->b_flags |= B_ERROR;
301 dsclose(dev_t dev, int mode, struct diskslices *ssp)
304 struct diskslice *sp;
306 sp = &ssp->dss_slices[dkslice(dev)];
307 mask = 1 << dkpart(dev);
308 sp->ds_openmask &= ~mask;
312 dsgone(struct diskslices **sspp)
315 struct diskslice *sp;
316 struct diskslices *ssp;
318 for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
319 sp = &ssp->dss_slices[slice];
320 free_ds_label(ssp, slice);
327 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
328 * is subject to the same restriction as dsopen().
331 dsioctl(dev_t dev, u_long cmd, caddr_t data,
332 int flags, struct diskslices **sspp)
335 struct disklabel *lp;
340 struct diskslice *sp;
341 struct diskslices *ssp;
342 struct partition *pp;
344 slice = dkslice(dev);
346 sp = &ssp->dss_slices[slice];
351 lp = (struct disklabel *)data;
352 if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
353 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
355 bzero(lp, sizeof(struct disklabel));
358 lp->d_magic = DISKMAGIC;
359 lp->d_magic2 = DISKMAGIC;
360 pp = &lp->d_partitions[RAW_PART];
362 pp->p_size = sp->ds_size;
364 lp->d_npartitions = MAXPARTITIONS;
365 if (lp->d_interleave == 0)
366 lp->d_interleave = 1;
369 if (lp->d_nsectors == 0)
371 if (lp->d_ntracks == 0)
374 lp->d_bbsize = BBSIZE;
375 lp->d_sbsize = SBSIZE;
376 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
377 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
378 lp->d_secperunit = sp->ds_size;
380 lp->d_checksum = dkcksum(lp);
386 *(struct disklabel *)data = *lp;
393 *(struct disklabel **)data = lp;
400 ((struct partinfo *)data)->disklab = lp;
401 ((struct partinfo *)data)->part
402 = &lp->d_partitions[dkpart(dev)];
406 bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
411 if (slice == WHOLE_DISK_SLICE)
413 if (!(flags & FWRITE))
415 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
416 if (sp->ds_label == NULL)
417 bzero(lp, sizeof *lp);
419 bcopy(sp->ds_label, lp, sizeof *lp);
420 if (sp->ds_label == NULL)
423 openmask = sp->ds_openmask;
424 if (slice == COMPATIBILITY_SLICE)
425 openmask |= ssp->dss_slices[
426 ssp->dss_first_bsd_slice].ds_openmask;
427 else if (slice == ssp->dss_first_bsd_slice)
428 openmask |= ssp->dss_slices[
429 COMPATIBILITY_SLICE].ds_openmask;
431 error = setdisklabel(lp, (struct disklabel *)data,
433 /* XXX why doesn't setdisklabel() check this? */
434 if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
437 if (lp->d_secperunit > sp->ds_size)
439 for (part = 0; part < lp->d_npartitions; part++)
440 if (lp->d_partitions[part].p_size > sp->ds_size)
447 free_ds_label(ssp, slice);
448 set_ds_label(ssp, slice, lp);
451 case DIOCSYNCSLICEINFO:
452 if (slice != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART)
455 for (slice = 0; slice < ssp->dss_nslices; slice++) {
456 openmask = ssp->dss_slices[slice].ds_openmask;
458 && (slice != WHOLE_DISK_SLICE
459 || openmask & ~(1 << RAW_PART)))
464 * Temporarily forget the current slices struct and read
466 * XXX should wait for current accesses on this disk to
467 * complete, then lock out future accesses and opens.
470 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
471 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
472 error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, lp);
480 * Reopen everything. This is a no-op except in the "force"
481 * case and when the raw bdev and cdev are both open. Abort
484 for (slice = 0; slice < ssp->dss_nslices; slice++) {
485 for (openmask = ssp->dss_slices[slice].ds_openmask,
486 part = 0; openmask; openmask >>= 1, part++) {
489 error = dsopen(dkmodslice(dkmodpart(dev, part),
491 S_IFCHR, ssp->dss_oflags, sspp,
506 error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp);
510 * XXX this used to hack on dk_openpart to fake opening
511 * partition 0 in case that is used instead of dkpart(dev).
513 old_wlabel = sp->ds_wlabel;
514 set_ds_wlabel(ssp, slice, TRUE);
515 error = writedisklabel(dev, sp->ds_label);
516 /* XXX should invalidate in-core label if write failed. */
517 set_ds_wlabel(ssp, slice, old_wlabel);
521 if (slice == WHOLE_DISK_SLICE)
523 if (!(flags & FWRITE))
525 set_ds_wlabel(ssp, slice, *(int *)data != 0);
534 dsiodone(struct bio *bio)
536 struct buf *bp = bio->bio_buf;
539 bp->b_flags = bp->b_flags & ~B_DONE;
540 if (!(bp->b_flags & B_READ)
541 || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
542 msg = fixlabel(NULL, bio->bio_caller_info1.ptr,
544 (bp->b_data + (int)bio->bio_caller_info2.offset),
549 biodone(bio->bio_prev);
553 dsisopen(struct diskslices *ssp)
559 for (slice = 0; slice < ssp->dss_nslices; slice++) {
560 if (ssp->dss_slices[slice].ds_openmask)
567 * Allocate a slices "struct" and initialize it to contain only an empty
568 * compatibility slice (pointing to itself), a whole disk slice (covering
569 * the disk as described by the label), and (nslices - BASE_SLICES) empty
570 * slices beginning at BASE_SLICE.
573 dsmakeslicestruct(int nslices, struct disklabel *lp)
575 struct diskslice *sp;
576 struct diskslices *ssp;
578 ssp = malloc(offsetof(struct diskslices, dss_slices) +
579 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
580 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
581 ssp->dss_nslices = nslices;
583 ssp->dss_secmult = lp->d_secsize / DEV_BSIZE;
584 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
585 ssp->dss_secshift = -1;
587 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
588 ssp->dss_secsize = lp->d_secsize;
589 sp = &ssp->dss_slices[0];
590 bzero(sp, nslices * sizeof *sp);
591 sp[WHOLE_DISK_SLICE].ds_size = lp->d_secperunit;
596 dsname(dev_t dev, int unit, int slice, int part, char *partname)
598 static char name[32];
601 dname = dev_dname(dev);
602 if (strlen(dname) > 16)
603 dname = "nametoolong";
604 snprintf(name, sizeof(name), "%s%d", dname, unit);
606 if (slice != WHOLE_DISK_SLICE || part != RAW_PART) {
607 partname[0] = 'a' + part;
609 if (slice != COMPATIBILITY_SLICE) {
610 snprintf(name + strlen(name),
611 sizeof(name) - strlen(name), "s%d", slice - 1);
618 * This should only be called when the unit is inactive and the strategy
619 * routine should not allow it to become active unless we call it. Our
620 * strategy routine must be special to allow activity.
623 dsopen(dev_t dev, int mode, u_int flags,
624 struct diskslices **sspp, struct disklabel *lp)
628 struct disklabel *lp1;
636 struct diskslice *sp;
637 struct diskslices *ssp;
640 dev->si_bsize_phys = lp->d_secsize;
643 if (lp->d_secsize % DEV_BSIZE) {
644 printf("%s: invalid sector size %lu\n", devtoname(dev),
645 (u_long)lp->d_secsize);
650 * XXX reinitialize the slice table unless there is an open device
651 * on the unit. This should only be done if the media has changed.
654 need_init = !dsisopen(ssp);
655 if (ssp != NULL && need_init)
659 * Allocate a minimal slices "struct". This will become
660 * the final slices "struct" if we don't want real slices
661 * or if we can't find any real slices.
663 *sspp = dsmakeslicestruct(BASE_SLICE, lp);
665 if (!(flags & DSO_ONESLICE)) {
667 error = dsinit(dev, lp, sspp);
674 ssp->dss_oflags = flags;
677 * If there are no real slices, then make the compatiblity
678 * slice cover the whole disk.
680 if (ssp->dss_nslices == BASE_SLICE)
681 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
684 /* Point the compatibility slice at the BSD slice, if any. */
685 for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
686 sp = &ssp->dss_slices[slice];
687 if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
688 ssp->dss_first_bsd_slice = slice;
689 ssp->dss_slices[COMPATIBILITY_SLICE].ds_offset
691 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
693 ssp->dss_slices[COMPATIBILITY_SLICE].ds_type
699 ssp->dss_slices[WHOLE_DISK_SLICE].ds_label = clone_label(lp);
700 ssp->dss_slices[WHOLE_DISK_SLICE].ds_wlabel = TRUE;
704 * Initialize secondary info for all slices. It is needed for more
705 * than the current slice in the DEVFS case. XXX DEVFS is no more.
707 for (slice = 0; slice < ssp->dss_nslices; slice++) {
708 sp = &ssp->dss_slices[slice];
709 if (sp->ds_label != NULL)
711 dev1 = dkmodslice(dkmodpart(dev, RAW_PART), slice);
712 sname = dsname(dev, unit, slice, RAW_PART, partname);
714 * XXX this should probably only be done for the need_init
715 * case, but there may be a problem with DIOCSYNCSLICEINFO.
717 set_ds_wlabel(ssp, slice, TRUE); /* XXX invert */
718 lp1 = clone_label(lp);
719 TRACE(("readdisklabel\n"));
720 if (flags & DSO_NOLABELS)
723 msg = readdisklabel(dev1, lp1);
726 * readdisklabel() returns NULL for success, and an
727 * error string for failure.
729 * If there isn't a label on the disk, and if the
730 * DSO_COMPATLABEL is set, we want to use the
731 * faked-up label provided by the caller.
733 * So we set msg to NULL to indicate that there is
734 * no failure (since we have a faked-up label),
735 * free lp1, and then clone it again from lp.
736 * (In case readdisklabel() modified lp1.)
738 if (msg != NULL && (flags & DSO_COMPATLABEL)) {
741 lp1 = clone_label(lp);
745 msg = fixlabel(sname, sp, lp1, FALSE);
746 if (msg == NULL && lp1->d_secsize != ssp->dss_secsize)
747 msg = "inconsistent sector size";
749 if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
750 log(LOG_WARNING, "%s: cannot find label (%s)\n",
755 if (lp1->d_flags & D_BADSECT) {
756 log(LOG_ERR, "%s: bad sector table not supported\n",
761 set_ds_label(ssp, slice, lp1);
762 set_ds_wlabel(ssp, slice, FALSE);
765 slice = dkslice(dev);
766 if (slice >= ssp->dss_nslices)
768 sp = &ssp->dss_slices[slice];
771 && (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions))
772 return (EINVAL); /* XXX needs translation */
774 sp->ds_openmask |= mask;
779 dssize(dev_t dev, struct diskslices **sspp)
781 struct disklabel *lp;
784 struct diskslices *ssp;
786 slice = dkslice(dev);
789 if (ssp == NULL || slice >= ssp->dss_nslices
790 || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
791 if (dev_dopen(dev, FREAD, S_IFCHR, NULL) != 0)
793 dev_dclose(dev, FREAD, S_IFCHR, NULL);
796 lp = ssp->dss_slices[slice].ds_label;
799 return ((int)lp->d_partitions[part].p_size);
803 free_ds_label(struct diskslices *ssp, int slice)
805 struct disklabel *lp;
806 struct diskslice *sp;
808 sp = &ssp->dss_slices[slice];
813 set_ds_label(ssp, slice, (struct disklabel *)NULL);
817 fixlabel(char *sname, struct diskslice *sp, struct disklabel *lp, int writeflag)
822 struct partition *pp;
826 /* These errors "can't happen" so don't bother reporting details. */
827 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC)
828 return ("fixlabel: invalid magic");
829 if (dkcksum(lp) != 0)
830 return ("fixlabel: invalid checksum");
832 pp = &lp->d_partitions[RAW_PART];
835 offset = sp->ds_offset;
837 start = sp->ds_offset;
838 offset = -sp->ds_offset;
840 if (pp->p_offset != start) {
843 "%s: rejecting BSD label: raw partition offset != slice offset\n",
845 slice_info(sname, sp);
846 partition_info(sname, RAW_PART, pp);
848 return ("fixlabel: raw partition offset != slice offset");
850 if (pp->p_size != sp->ds_size) {
852 printf("%s: raw partition size != slice size\n", sname);
853 slice_info(sname, sp);
854 partition_info(sname, RAW_PART, pp);
856 if (pp->p_size > sp->ds_size) {
858 return ("fixlabel: raw partition size > slice size");
859 printf("%s: truncating raw partition\n", sname);
860 pp->p_size = sp->ds_size;
863 end = start + sp->ds_size;
865 return ("fixlabel: slice wraps");
866 if (lp->d_secpercyl <= 0)
867 return ("fixlabel: d_secpercyl <= 0");
870 for (part = 0; part < lp->d_npartitions; part++, pp++) {
871 if (pp->p_offset != 0 || pp->p_size != 0) {
872 if (pp->p_offset < start
873 || pp->p_offset + pp->p_size > end
874 || pp->p_offset + pp->p_size < pp->p_offset) {
877 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
880 slice_info(sname, sp);
883 partition_info(sname, part, pp);
885 /* XXX else silently discard junk. */
886 bzero(pp, sizeof *pp);
888 pp->p_offset += offset;
891 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
892 lp->d_secperunit = sp->ds_size;
894 lp->d_checksum = dkcksum(lp);
899 partition_info(char *sname, int part, struct partition *pp)
901 printf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
902 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
907 slice_info(char *sname, struct diskslice *sp)
909 printf("%s: start %lu, end %lu, size %lu\n", sname,
910 sp->ds_offset, sp->ds_offset + sp->ds_size - 1, sp->ds_size);
914 set_ds_label(struct diskslices *ssp, int slice, struct disklabel *lp)
916 ssp->dss_slices[slice].ds_label = lp;
917 if (slice == COMPATIBILITY_SLICE)
918 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_label = lp;
919 else if (slice == ssp->dss_first_bsd_slice)
920 ssp->dss_slices[COMPATIBILITY_SLICE].ds_label = lp;
924 set_ds_wlabel(struct diskslices *ssp, int slice, int wlabel)
926 ssp->dss_slices[slice].ds_wlabel = wlabel;
927 if (slice == COMPATIBILITY_SLICE)
928 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_wlabel = wlabel;
929 else if (slice == ssp->dss_first_bsd_slice)
930 ssp->dss_slices[COMPATIBILITY_SLICE].ds_wlabel = wlabel;