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.4 2003/07/22 17:03: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/fcntl.h>
57 #include <sys/malloc.h>
59 #include <sys/syslog.h>
60 #include <sys/vnode.h>
61 #include <sys/device.h>
63 #include <ufs/ffs/fs.h>
65 #define TRACE(str) do { if (ds_debug) printf str; } while (0)
67 typedef u_char bool_t;
69 static volatile bool_t ds_debug;
71 static struct disklabel *clone_label __P((struct disklabel *lp));
72 static void dsiodone __P((struct buf *bp));
73 static char *fixlabel __P((char *sname, struct diskslice *sp,
74 struct disklabel *lp, int writeflag));
75 static void free_ds_label __P((struct diskslices *ssp, int slice));
76 static void partition_info __P((char *sname, int part, struct partition *pp));
77 static void slice_info __P((char *sname, struct diskslice *sp));
78 static void set_ds_label __P((struct diskslices *ssp, int slice,
79 struct disklabel *lp));
80 static void set_ds_labeldevs __P((dev_t dev, struct diskslices *ssp));
81 static void set_ds_wlabel __P((struct diskslices *ssp, int slice,
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 *
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.
139 struct diskslices *ssp;
144 struct disklabel *lp;
147 struct partition *pp;
149 daddr_t slicerel_secno;
150 struct diskslice *sp;
155 printf("dscheck(%s): negative b_blkno %ld\n",
156 devtoname(bp->b_dev), (long)blkno);
157 bp->b_error = EINVAL;
160 sp = &ssp->dss_slices[dkslice(bp->b_dev)];
162 if (ssp->dss_secmult == 1) {
163 if (bp->b_bcount % (u_long)DEV_BSIZE)
166 nsec = bp->b_bcount >> DEV_BSHIFT;
167 } else if (ssp->dss_secshift != -1) {
168 if (bp->b_bcount & (ssp->dss_secsize - 1))
170 if (blkno & (ssp->dss_secmult - 1))
172 secno = blkno >> ssp->dss_secshift;
173 nsec = bp->b_bcount >> (DEV_BSHIFT + ssp->dss_secshift);
175 if (bp->b_bcount % ssp->dss_secsize)
177 if (blkno % ssp->dss_secmult)
179 secno = blkno / ssp->dss_secmult;
180 nsec = bp->b_bcount / ssp->dss_secsize;
183 labelsect = -LABELSECTOR - 1;
184 endsecno = sp->ds_size;
185 slicerel_secno = secno;
187 labelsect = lp->d_partitions[LABEL_PART].p_offset;
188 if (labelsect != 0) Debugger("labelsect != 0 in dscheck()");
189 pp = &lp->d_partitions[dkpart(bp->b_dev)];
190 endsecno = pp->p_size;
191 slicerel_secno = pp->p_offset + secno;
194 /* overwriting disk label ? */
195 /* XXX should also protect bootstrap in first 8K */
196 if (slicerel_secno <= LABELSECTOR + labelsect &&
198 slicerel_secno + nsec > LABELSECTOR + labelsect &&
200 (bp->b_flags & B_READ) == 0 && sp->ds_wlabel == 0) {
205 #if defined(DOSBBSECTOR) && defined(notyet)
206 /* overwriting master boot record? */
207 if (slicerel_secno <= DOSBBSECTOR && (bp->b_flags & B_READ) == 0 &&
208 sp->ds_wlabel == 0) {
214 /* beyond partition? */
215 if (secno + nsec > endsecno) {
216 /* if exactly at end of disk, return an EOF */
217 if (secno == endsecno) {
218 bp->b_resid = bp->b_bcount;
221 /* or truncate if part of it fits */
222 nsec = endsecno - secno;
224 bp->b_error = EINVAL;
227 bp->b_bcount = nsec * ssp->dss_secsize;
230 bp->b_pblkno = sp->ds_offset + slicerel_secno;
233 * Snoop on label accesses if the slice offset is nonzero. Fudge
234 * offsets in the label to keep the in-core label coherent with
237 if (slicerel_secno <= LABELSECTOR + labelsect
239 && slicerel_secno + nsec > LABELSECTOR + labelsect
241 && sp->ds_offset != 0) {
242 struct iodone_chain *ic;
244 ic = malloc(sizeof *ic , M_DEVBUF, M_WAITOK);
245 ic->ic_prev_flags = bp->b_flags;
246 ic->ic_prev_iodone = bp->b_iodone;
247 ic->ic_prev_iodone_chain = bp->b_iodone_chain;
248 ic->ic_args[0].ia_long = (LABELSECTOR + labelsect -
249 slicerel_secno) * ssp->dss_secsize;
250 ic->ic_args[1].ia_ptr = sp;
251 bp->b_flags |= B_CALL;
252 bp->b_iodone = dsiodone;
253 bp->b_iodone_chain = ic;
254 if (!(bp->b_flags & B_READ)) {
256 * XXX even disklabel(8) writes directly so we need
257 * to adjust writes. Perhaps we should drop support
258 * for DIOCWLABEL (always write protect labels) and
259 * require the use of DIOCWDINFO.
261 * XXX probably need to copy the data to avoid even
262 * temporarily corrupting the in-core copy.
264 if (bp->b_vp != NULL) {
266 bp->b_vp->v_numoutput++;
269 /* XXX need name here. */
270 msg = fixlabel((char *)NULL, sp,
272 (bp->b_data + ic->ic_args[0].ia_long),
275 printf("dscheck(%s): %s\n",
276 devtoname(bp->b_dev), msg);
286 "dscheck(%s): b_bcount %ld is not on a sector boundary (ssize %d)\n",
287 devtoname(bp->b_dev), bp->b_bcount, ssp->dss_secsize);
288 bp->b_error = EINVAL;
293 "dscheck(%s): b_blkno %ld is not on a sector boundary (ssize %d)\n",
294 devtoname(bp->b_dev), (long)blkno, ssp->dss_secsize);
295 bp->b_error = EINVAL;
299 bp->b_resid = bp->b_bcount;
300 bp->b_flags |= B_ERROR;
305 dsclose(dev, mode, ssp)
308 struct diskslices *ssp;
311 struct diskslice *sp;
313 sp = &ssp->dss_slices[dkslice(dev)];
314 mask = 1 << dkpart(dev);
315 sp->ds_openmask &= ~mask;
320 struct diskslices **sspp;
323 struct diskslice *sp;
324 struct diskslices *ssp;
326 for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
327 sp = &ssp->dss_slices[slice];
328 free_ds_label(ssp, slice);
335 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
336 * is subject to the same restriction as dsopen().
339 dsioctl(dev, cmd, data, flags, sspp)
344 struct diskslices **sspp;
347 struct disklabel *lp;
352 struct diskslice *sp;
353 struct diskslices *ssp;
354 struct partition *pp;
356 slice = dkslice(dev);
358 sp = &ssp->dss_slices[slice];
363 lp = (struct disklabel *)data;
364 if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
365 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
367 bzero(lp, sizeof(struct disklabel));
370 lp->d_magic = DISKMAGIC;
371 lp->d_magic2 = DISKMAGIC;
372 pp = &lp->d_partitions[RAW_PART];
374 pp->p_size = sp->ds_size;
376 lp->d_npartitions = MAXPARTITIONS;
377 if (lp->d_interleave == 0)
378 lp->d_interleave = 1;
381 if (lp->d_nsectors == 0)
383 if (lp->d_ntracks == 0)
386 lp->d_bbsize = BBSIZE;
387 lp->d_sbsize = SBSIZE;
388 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
389 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
390 lp->d_secperunit = sp->ds_size;
392 lp->d_checksum = dkcksum(lp);
398 *(struct disklabel *)data = *lp;
405 *(struct disklabel **)data = lp;
412 ((struct partinfo *)data)->disklab = lp;
413 ((struct partinfo *)data)->part
414 = &lp->d_partitions[dkpart(dev)];
418 bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
423 if (slice == WHOLE_DISK_SLICE)
425 if (!(flags & FWRITE))
427 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
428 if (sp->ds_label == NULL)
429 bzero(lp, sizeof *lp);
431 bcopy(sp->ds_label, lp, sizeof *lp);
432 if (sp->ds_label == NULL)
435 openmask = sp->ds_openmask;
436 if (slice == COMPATIBILITY_SLICE)
437 openmask |= ssp->dss_slices[
438 ssp->dss_first_bsd_slice].ds_openmask;
439 else if (slice == ssp->dss_first_bsd_slice)
440 openmask |= ssp->dss_slices[
441 COMPATIBILITY_SLICE].ds_openmask;
443 error = setdisklabel(lp, (struct disklabel *)data,
445 /* XXX why doesn't setdisklabel() check this? */
446 if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
449 if (lp->d_secperunit > sp->ds_size)
451 for (part = 0; part < lp->d_npartitions; part++)
452 if (lp->d_partitions[part].p_size > sp->ds_size)
459 free_ds_label(ssp, slice);
460 set_ds_label(ssp, slice, lp);
461 set_ds_labeldevs(dev, ssp);
464 case DIOCSYNCSLICEINFO:
465 if (slice != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART)
468 for (slice = 0; slice < ssp->dss_nslices; slice++) {
469 openmask = ssp->dss_slices[slice].ds_openmask;
471 && (slice != WHOLE_DISK_SLICE
472 || openmask & ~(1 << RAW_PART)))
477 * Temporarily forget the current slices struct and read
479 * XXX should wait for current accesses on this disk to
480 * complete, then lock out future accesses and opens.
483 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
484 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
485 error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, lp);
493 * Reopen everything. This is a no-op except in the "force"
494 * case and when the raw bdev and cdev are both open. Abort
497 for (slice = 0; slice < ssp->dss_nslices; slice++) {
498 for (openmask = ssp->dss_slices[slice].ds_openmask,
499 part = 0; openmask; openmask >>= 1, part++) {
502 error = dsopen(dkmodslice(dkmodpart(dev, part),
504 S_IFCHR, ssp->dss_oflags, sspp,
519 error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp);
523 * XXX this used to hack on dk_openpart to fake opening
524 * partition 0 in case that is used instead of dkpart(dev).
526 old_wlabel = sp->ds_wlabel;
527 set_ds_wlabel(ssp, slice, TRUE);
528 error = writedisklabel(dev, sp->ds_label);
529 /* XXX should invalidate in-core label if write failed. */
530 set_ds_wlabel(ssp, slice, old_wlabel);
535 if (slice == WHOLE_DISK_SLICE)
538 if (!(flags & FWRITE))
540 set_ds_wlabel(ssp, slice, *(int *)data != 0);
552 struct iodone_chain *ic;
555 ic = bp->b_iodone_chain;
556 bp->b_flags = (ic->ic_prev_flags & B_CALL)
557 | (bp->b_flags & ~(B_CALL | B_DONE));
558 bp->b_iodone = ic->ic_prev_iodone;
559 bp->b_iodone_chain = ic->ic_prev_iodone_chain;
560 if (!(bp->b_flags & B_READ)
561 || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
562 msg = fixlabel((char *)NULL, ic->ic_args[1].ia_ptr,
564 (bp->b_data + ic->ic_args[0].ia_long),
575 struct diskslices *ssp;
581 for (slice = 0; slice < ssp->dss_nslices; slice++)
582 if (ssp->dss_slices[slice].ds_openmask)
588 * Allocate a slices "struct" and initialize it to contain only an empty
589 * compatibility slice (pointing to itself), a whole disk slice (covering
590 * the disk as described by the label), and (nslices - BASE_SLICES) empty
591 * slices beginning at BASE_SLICE.
594 dsmakeslicestruct(nslices, lp)
596 struct disklabel *lp;
598 struct diskslice *sp;
599 struct diskslices *ssp;
601 ssp = malloc(offsetof(struct diskslices, dss_slices) +
602 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
603 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
604 ssp->dss_nslices = nslices;
606 ssp->dss_secmult = lp->d_secsize / DEV_BSIZE;
607 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
608 ssp->dss_secshift = -1;
610 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
611 ssp->dss_secsize = lp->d_secsize;
612 sp = &ssp->dss_slices[0];
613 bzero(sp, nslices * sizeof *sp);
614 sp[WHOLE_DISK_SLICE].ds_size = lp->d_secperunit;
619 dsname(dev, unit, slice, part, partname)
626 static char name[32];
629 dname = dev_dname(dev);
630 if (strlen(dname) > 16)
631 dname = "nametoolong";
632 snprintf(name, sizeof(name), "%s%d", dname, unit);
634 if (slice != WHOLE_DISK_SLICE || part != RAW_PART) {
635 partname[0] = 'a' + part;
637 if (slice != COMPATIBILITY_SLICE)
638 snprintf(name + strlen(name),
639 sizeof(name) - strlen(name), "s%d", slice - 1);
645 * This should only be called when the unit is inactive and the strategy
646 * routine should not allow it to become active unless we call it. Our
647 * strategy routine must be special to allow activity.
650 dsopen(dev, mode, flags, sspp, lp)
654 struct diskslices **sspp;
655 struct disklabel *lp;
659 struct disklabel *lp1;
667 struct diskslice *sp;
668 struct diskslices *ssp;
671 dev->si_bsize_phys = lp->d_secsize;
674 if (lp->d_secsize % DEV_BSIZE) {
675 printf("%s: invalid sector size %lu\n", devtoname(dev),
676 (u_long)lp->d_secsize);
681 * XXX reinitialize the slice table unless there is an open device
682 * on the unit. This should only be done if the media has changed.
685 need_init = !dsisopen(ssp);
686 if (ssp != NULL && need_init)
690 * Allocate a minimal slices "struct". This will become
691 * the final slices "struct" if we don't want real slices
692 * or if we can't find any real slices.
694 *sspp = dsmakeslicestruct(BASE_SLICE, lp);
696 if (!(flags & DSO_ONESLICE)) {
698 error = dsinit(dev, lp, sspp);
705 ssp->dss_oflags = flags;
708 * If there are no real slices, then make the compatiblity
709 * slice cover the whole disk.
711 if (ssp->dss_nslices == BASE_SLICE)
712 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
715 /* Point the compatibility slice at the BSD slice, if any. */
716 for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
717 sp = &ssp->dss_slices[slice];
718 if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
719 ssp->dss_first_bsd_slice = slice;
720 ssp->dss_slices[COMPATIBILITY_SLICE].ds_offset
722 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
724 ssp->dss_slices[COMPATIBILITY_SLICE].ds_type
730 ssp->dss_slices[WHOLE_DISK_SLICE].ds_label = clone_label(lp);
731 ssp->dss_slices[WHOLE_DISK_SLICE].ds_wlabel = TRUE;
735 * Initialize secondary info for all slices. It is needed for more
736 * than the current slice in the DEVFS case. XXX DEVFS is no more.
738 for (slice = 0; slice < ssp->dss_nslices; slice++) {
739 sp = &ssp->dss_slices[slice];
740 if (sp->ds_label != NULL
742 && slice != WHOLE_DISK_SLICE
746 dev1 = dkmodslice(dkmodpart(dev, RAW_PART), slice);
747 sname = dsname(dev, unit, slice, RAW_PART, partname);
749 * XXX this should probably only be done for the need_init
750 * case, but there may be a problem with DIOCSYNCSLICEINFO.
752 set_ds_wlabel(ssp, slice, TRUE); /* XXX invert */
753 lp1 = clone_label(lp);
754 TRACE(("readdisklabel\n"));
755 if (flags & DSO_NOLABELS)
758 msg = readdisklabel(dev1, lp1);
761 * readdisklabel() returns NULL for success, and an
762 * error string for failure.
764 * If there isn't a label on the disk, and if the
765 * DSO_COMPATLABEL is set, we want to use the
766 * faked-up label provided by the caller.
768 * So we set msg to NULL to indicate that there is
769 * no failure (since we have a faked-up label),
770 * free lp1, and then clone it again from lp.
771 * (In case readdisklabel() modified lp1.)
773 if (msg != NULL && (flags & DSO_COMPATLABEL)) {
776 lp1 = clone_label(lp);
780 msg = fixlabel(sname, sp, lp1, FALSE);
781 if (msg == NULL && lp1->d_secsize != ssp->dss_secsize)
782 msg = "inconsistent sector size";
784 if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
785 log(LOG_WARNING, "%s: cannot find label (%s)\n",
790 if (lp1->d_flags & D_BADSECT) {
791 log(LOG_ERR, "%s: bad sector table not supported\n",
796 set_ds_label(ssp, slice, lp1);
797 set_ds_labeldevs(dev1, ssp);
798 set_ds_wlabel(ssp, slice, FALSE);
801 slice = dkslice(dev);
802 if (slice >= ssp->dss_nslices)
804 sp = &ssp->dss_slices[slice];
807 && (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions))
808 return (EINVAL); /* XXX needs translation */
810 sp->ds_openmask |= mask;
817 struct diskslices **sspp;
819 struct disklabel *lp;
822 struct diskslices *ssp;
824 slice = dkslice(dev);
827 if (ssp == NULL || slice >= ssp->dss_nslices
828 || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
829 if (dev_dopen(dev, FREAD, S_IFCHR, NULL) != 0)
831 dev_dclose(dev, FREAD, S_IFCHR, NULL);
834 lp = ssp->dss_slices[slice].ds_label;
837 return ((int)lp->d_partitions[part].p_size);
841 free_ds_label(ssp, slice)
842 struct diskslices *ssp;
845 struct disklabel *lp;
846 struct diskslice *sp;
848 sp = &ssp->dss_slices[slice];
853 set_ds_label(ssp, slice, (struct disklabel *)NULL);
857 fixlabel(sname, sp, lp, writeflag)
859 struct diskslice *sp;
860 struct disklabel *lp;
866 struct partition *pp;
870 /* These errors "can't happen" so don't bother reporting details. */
871 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC)
872 return ("fixlabel: invalid magic");
873 if (dkcksum(lp) != 0)
874 return ("fixlabel: invalid checksum");
876 pp = &lp->d_partitions[RAW_PART];
879 offset = sp->ds_offset;
881 start = sp->ds_offset;
882 offset = -sp->ds_offset;
884 if (pp->p_offset != start) {
887 "%s: rejecting BSD label: raw partition offset != slice offset\n",
889 slice_info(sname, sp);
890 partition_info(sname, RAW_PART, pp);
892 return ("fixlabel: raw partition offset != slice offset");
894 if (pp->p_size != sp->ds_size) {
896 printf("%s: raw partition size != slice size\n", sname);
897 slice_info(sname, sp);
898 partition_info(sname, RAW_PART, pp);
900 if (pp->p_size > sp->ds_size) {
902 return ("fixlabel: raw partition size > slice size");
903 printf("%s: truncating raw partition\n", sname);
904 pp->p_size = sp->ds_size;
907 end = start + sp->ds_size;
909 return ("fixlabel: slice wraps");
910 if (lp->d_secpercyl <= 0)
911 return ("fixlabel: d_secpercyl <= 0");
914 for (part = 0; part < lp->d_npartitions; part++, pp++) {
915 if (pp->p_offset != 0 || pp->p_size != 0) {
916 if (pp->p_offset < start
917 || pp->p_offset + pp->p_size > end
918 || pp->p_offset + pp->p_size < pp->p_offset) {
921 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
924 slice_info(sname, sp);
927 partition_info(sname, part, pp);
929 /* XXX else silently discard junk. */
930 bzero(pp, sizeof *pp);
932 pp->p_offset += offset;
935 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
936 lp->d_secperunit = sp->ds_size;
938 lp->d_checksum = dkcksum(lp);
943 partition_info(sname, part, pp)
946 struct partition *pp;
948 printf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
949 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
954 slice_info(sname, sp)
956 struct diskslice *sp;
958 printf("%s: start %lu, end %lu, size %lu\n", sname,
959 sp->ds_offset, sp->ds_offset + sp->ds_size - 1, sp->ds_size);
963 set_ds_label(ssp, slice, lp)
964 struct diskslices *ssp;
966 struct disklabel *lp;
968 ssp->dss_slices[slice].ds_label = lp;
969 if (slice == COMPATIBILITY_SLICE)
970 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_label = lp;
971 else if (slice == ssp->dss_first_bsd_slice)
972 ssp->dss_slices[COMPATIBILITY_SLICE].ds_label = lp;
975 /* XXX remove this? */
977 set_ds_labeldevs(dev, ssp)
979 struct diskslices *ssp;
984 set_ds_wlabel(ssp, slice, wlabel)
985 struct diskslices *ssp;
989 ssp->dss_slices[slice].ds_wlabel = wlabel;
990 if (slice == COMPATIBILITY_SLICE)
991 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_wlabel = wlabel;
992 else if (slice == ssp->dss_first_bsd_slice)
993 ssp->dss_slices[COMPATIBILITY_SLICE].ds_wlabel = wlabel;