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.7 2003/11/10 06:12:13 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>
64 #include <vfs/ufs/fs.h>
66 #define TRACE(str) do { if (ds_debug) printf str; } while (0)
68 typedef u_char bool_t;
70 static volatile bool_t ds_debug;
72 static struct disklabel *clone_label (struct disklabel *lp);
73 static void dsiodone (struct buf *bp);
74 static char *fixlabel (char *sname, struct diskslice *sp,
75 struct disklabel *lp, int writeflag);
76 static void free_ds_label (struct diskslices *ssp, int slice);
77 static void partition_info (char *sname, int part, struct partition *pp);
78 static void slice_info (char *sname, struct diskslice *sp);
79 static void set_ds_label (struct diskslices *ssp, int slice,
80 struct disklabel *lp);
81 static void set_ds_labeldevs (dev_t dev, struct diskslices *ssp);
82 static void set_ds_wlabel (struct diskslices *ssp, int slice,
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 *
95 struct disklabel *lp1;
97 lp1 = malloc(sizeof *lp1, M_DEVBUF, M_WAITOK);
100 if (lp1->d_typename[0] == '\0')
101 strncpy(lp1->d_typename, "amnesiac", sizeof(lp1->d_typename));
102 if (lp1->d_packname[0] == '\0')
103 strncpy(lp1->d_packname, "fictitious", sizeof(lp1->d_packname));
104 if (lp1->d_nsectors == 0)
105 lp1->d_nsectors = 32;
106 if (lp1->d_ntracks == 0)
108 lp1->d_secpercyl = lp1->d_nsectors * lp1->d_ntracks;
109 lp1->d_ncylinders = lp1->d_secperunit / lp1->d_secpercyl;
112 if (lp1->d_interleave == 0)
113 lp1->d_interleave = 1;
114 if (lp1->d_npartitions < RAW_PART + 1)
115 lp1->d_npartitions = MAXPARTITIONS;
116 if (lp1->d_bbsize == 0)
117 lp1->d_bbsize = BBSIZE;
118 if (lp1->d_sbsize == 0)
119 lp1->d_sbsize = SBSIZE;
120 lp1->d_partitions[RAW_PART].p_size = lp1->d_secperunit;
121 lp1->d_magic = DISKMAGIC;
122 lp1->d_magic2 = DISKMAGIC;
123 lp1->d_checksum = dkcksum(lp1);
128 * Determine the size of the transfer, and make sure it is
129 * within the boundaries of the partition. Adjust transfer
130 * if needed, and signal errors or early completion.
133 * o Split buffers that are too big for the device.
134 * o Check for overflow.
135 * o Finish cleaning this up.
140 struct diskslices *ssp;
145 struct disklabel *lp;
148 struct partition *pp;
150 daddr_t slicerel_secno;
151 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;
189 if (labelsect != 0) Debugger("labelsect != 0 in dscheck()");
190 pp = &lp->d_partitions[dkpart(bp->b_dev)];
191 endsecno = pp->p_size;
192 slicerel_secno = pp->p_offset + secno;
195 /* overwriting disk label ? */
196 /* XXX should also protect bootstrap in first 8K */
197 if (slicerel_secno <= LABELSECTOR + labelsect &&
199 slicerel_secno + nsec > LABELSECTOR + labelsect &&
201 (bp->b_flags & B_READ) == 0 && sp->ds_wlabel == 0) {
206 #if defined(DOSBBSECTOR) && defined(notyet)
207 /* overwriting master boot record? */
208 if (slicerel_secno <= DOSBBSECTOR && (bp->b_flags & B_READ) == 0 &&
209 sp->ds_wlabel == 0) {
215 /* beyond partition? */
216 if (secno + nsec > endsecno) {
217 /* if exactly at end of disk, return an EOF */
218 if (secno == endsecno) {
219 bp->b_resid = bp->b_bcount;
222 /* or truncate if part of it fits */
223 nsec = endsecno - secno;
225 bp->b_error = EINVAL;
228 bp->b_bcount = nsec * ssp->dss_secsize;
231 bp->b_pblkno = sp->ds_offset + slicerel_secno;
234 * Snoop on label accesses if the slice offset is nonzero. Fudge
235 * offsets in the label to keep the in-core label coherent with
238 if (slicerel_secno <= LABELSECTOR + labelsect
240 && slicerel_secno + nsec > LABELSECTOR + labelsect
242 && sp->ds_offset != 0) {
243 struct iodone_chain *ic;
245 ic = malloc(sizeof *ic , M_DEVBUF, M_WAITOK);
246 ic->ic_prev_flags = bp->b_flags;
247 ic->ic_prev_iodone = bp->b_iodone;
248 ic->ic_prev_iodone_chain = bp->b_iodone_chain;
249 ic->ic_args[0].ia_long = (LABELSECTOR + labelsect -
250 slicerel_secno) * ssp->dss_secsize;
251 ic->ic_args[1].ia_ptr = sp;
252 bp->b_flags |= B_CALL;
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, mode, ssp)
309 struct diskslices *ssp;
312 struct diskslice *sp;
314 sp = &ssp->dss_slices[dkslice(dev)];
315 mask = 1 << dkpart(dev);
316 sp->ds_openmask &= ~mask;
321 struct diskslices **sspp;
324 struct diskslice *sp;
325 struct diskslices *ssp;
327 for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
328 sp = &ssp->dss_slices[slice];
329 free_ds_label(ssp, slice);
336 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
337 * is subject to the same restriction as dsopen().
340 dsioctl(dev, cmd, data, flags, sspp)
345 struct diskslices **sspp;
348 struct disklabel *lp;
353 struct diskslice *sp;
354 struct diskslices *ssp;
355 struct partition *pp;
357 slice = dkslice(dev);
359 sp = &ssp->dss_slices[slice];
364 lp = (struct disklabel *)data;
365 if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
366 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
368 bzero(lp, sizeof(struct disklabel));
371 lp->d_magic = DISKMAGIC;
372 lp->d_magic2 = DISKMAGIC;
373 pp = &lp->d_partitions[RAW_PART];
375 pp->p_size = sp->ds_size;
377 lp->d_npartitions = MAXPARTITIONS;
378 if (lp->d_interleave == 0)
379 lp->d_interleave = 1;
382 if (lp->d_nsectors == 0)
384 if (lp->d_ntracks == 0)
387 lp->d_bbsize = BBSIZE;
388 lp->d_sbsize = SBSIZE;
389 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
390 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
391 lp->d_secperunit = sp->ds_size;
393 lp->d_checksum = dkcksum(lp);
399 *(struct disklabel *)data = *lp;
406 *(struct disklabel **)data = lp;
413 ((struct partinfo *)data)->disklab = lp;
414 ((struct partinfo *)data)->part
415 = &lp->d_partitions[dkpart(dev)];
419 bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
424 if (slice == WHOLE_DISK_SLICE)
426 if (!(flags & FWRITE))
428 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
429 if (sp->ds_label == NULL)
430 bzero(lp, sizeof *lp);
432 bcopy(sp->ds_label, lp, sizeof *lp);
433 if (sp->ds_label == NULL)
436 openmask = sp->ds_openmask;
437 if (slice == COMPATIBILITY_SLICE)
438 openmask |= ssp->dss_slices[
439 ssp->dss_first_bsd_slice].ds_openmask;
440 else if (slice == ssp->dss_first_bsd_slice)
441 openmask |= ssp->dss_slices[
442 COMPATIBILITY_SLICE].ds_openmask;
444 error = setdisklabel(lp, (struct disklabel *)data,
446 /* XXX why doesn't setdisklabel() check this? */
447 if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
450 if (lp->d_secperunit > sp->ds_size)
452 for (part = 0; part < lp->d_npartitions; part++)
453 if (lp->d_partitions[part].p_size > sp->ds_size)
460 free_ds_label(ssp, slice);
461 set_ds_label(ssp, slice, lp);
462 set_ds_labeldevs(dev, ssp);
465 case DIOCSYNCSLICEINFO:
466 if (slice != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART)
469 for (slice = 0; slice < ssp->dss_nslices; slice++) {
470 openmask = ssp->dss_slices[slice].ds_openmask;
472 && (slice != WHOLE_DISK_SLICE
473 || openmask & ~(1 << RAW_PART)))
478 * Temporarily forget the current slices struct and read
480 * XXX should wait for current accesses on this disk to
481 * complete, then lock out future accesses and opens.
484 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
485 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
486 error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, lp);
494 * Reopen everything. This is a no-op except in the "force"
495 * case and when the raw bdev and cdev are both open. Abort
498 for (slice = 0; slice < ssp->dss_nslices; slice++) {
499 for (openmask = ssp->dss_slices[slice].ds_openmask,
500 part = 0; openmask; openmask >>= 1, part++) {
503 error = dsopen(dkmodslice(dkmodpart(dev, part),
505 S_IFCHR, ssp->dss_oflags, sspp,
520 error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp);
524 * XXX this used to hack on dk_openpart to fake opening
525 * partition 0 in case that is used instead of dkpart(dev).
527 old_wlabel = sp->ds_wlabel;
528 set_ds_wlabel(ssp, slice, TRUE);
529 error = writedisklabel(dev, sp->ds_label);
530 /* XXX should invalidate in-core label if write failed. */
531 set_ds_wlabel(ssp, slice, old_wlabel);
536 if (slice == WHOLE_DISK_SLICE)
539 if (!(flags & FWRITE))
541 set_ds_wlabel(ssp, slice, *(int *)data != 0);
553 struct iodone_chain *ic;
556 ic = bp->b_iodone_chain;
557 bp->b_flags = (ic->ic_prev_flags & B_CALL)
558 | (bp->b_flags & ~(B_CALL | B_DONE));
559 bp->b_iodone = ic->ic_prev_iodone;
560 bp->b_iodone_chain = ic->ic_prev_iodone_chain;
561 if (!(bp->b_flags & B_READ)
562 || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
563 msg = fixlabel((char *)NULL, ic->ic_args[1].ia_ptr,
565 (bp->b_data + ic->ic_args[0].ia_long),
576 struct diskslices *ssp;
582 for (slice = 0; slice < ssp->dss_nslices; slice++)
583 if (ssp->dss_slices[slice].ds_openmask)
589 * Allocate a slices "struct" and initialize it to contain only an empty
590 * compatibility slice (pointing to itself), a whole disk slice (covering
591 * the disk as described by the label), and (nslices - BASE_SLICES) empty
592 * slices beginning at BASE_SLICE.
595 dsmakeslicestruct(nslices, lp)
597 struct disklabel *lp;
599 struct diskslice *sp;
600 struct diskslices *ssp;
602 ssp = malloc(offsetof(struct diskslices, dss_slices) +
603 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
604 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
605 ssp->dss_nslices = nslices;
607 ssp->dss_secmult = lp->d_secsize / DEV_BSIZE;
608 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
609 ssp->dss_secshift = -1;
611 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
612 ssp->dss_secsize = lp->d_secsize;
613 sp = &ssp->dss_slices[0];
614 bzero(sp, nslices * sizeof *sp);
615 sp[WHOLE_DISK_SLICE].ds_size = lp->d_secperunit;
620 dsname(dev, unit, slice, part, partname)
627 static char name[32];
630 dname = dev_dname(dev);
631 if (strlen(dname) > 16)
632 dname = "nametoolong";
633 snprintf(name, sizeof(name), "%s%d", dname, unit);
635 if (slice != WHOLE_DISK_SLICE || part != RAW_PART) {
636 partname[0] = 'a' + part;
638 if (slice != COMPATIBILITY_SLICE)
639 snprintf(name + strlen(name),
640 sizeof(name) - strlen(name), "s%d", slice - 1);
646 * This should only be called when the unit is inactive and the strategy
647 * routine should not allow it to become active unless we call it. Our
648 * strategy routine must be special to allow activity.
651 dsopen(dev, mode, flags, sspp, lp)
655 struct diskslices **sspp;
656 struct disklabel *lp;
660 struct disklabel *lp1;
668 struct diskslice *sp;
669 struct diskslices *ssp;
672 dev->si_bsize_phys = lp->d_secsize;
675 if (lp->d_secsize % DEV_BSIZE) {
676 printf("%s: invalid sector size %lu\n", devtoname(dev),
677 (u_long)lp->d_secsize);
682 * XXX reinitialize the slice table unless there is an open device
683 * on the unit. This should only be done if the media has changed.
686 need_init = !dsisopen(ssp);
687 if (ssp != NULL && need_init)
691 * Allocate a minimal slices "struct". This will become
692 * the final slices "struct" if we don't want real slices
693 * or if we can't find any real slices.
695 *sspp = dsmakeslicestruct(BASE_SLICE, lp);
697 if (!(flags & DSO_ONESLICE)) {
699 error = dsinit(dev, lp, sspp);
706 ssp->dss_oflags = flags;
709 * If there are no real slices, then make the compatiblity
710 * slice cover the whole disk.
712 if (ssp->dss_nslices == BASE_SLICE)
713 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
716 /* Point the compatibility slice at the BSD slice, if any. */
717 for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
718 sp = &ssp->dss_slices[slice];
719 if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
720 ssp->dss_first_bsd_slice = slice;
721 ssp->dss_slices[COMPATIBILITY_SLICE].ds_offset
723 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
725 ssp->dss_slices[COMPATIBILITY_SLICE].ds_type
731 ssp->dss_slices[WHOLE_DISK_SLICE].ds_label = clone_label(lp);
732 ssp->dss_slices[WHOLE_DISK_SLICE].ds_wlabel = TRUE;
736 * Initialize secondary info for all slices. It is needed for more
737 * than the current slice in the DEVFS case. XXX DEVFS is no more.
739 for (slice = 0; slice < ssp->dss_nslices; slice++) {
740 sp = &ssp->dss_slices[slice];
741 if (sp->ds_label != NULL
743 && slice != WHOLE_DISK_SLICE
747 dev1 = dkmodslice(dkmodpart(dev, RAW_PART), slice);
748 sname = dsname(dev, unit, slice, RAW_PART, partname);
750 * XXX this should probably only be done for the need_init
751 * case, but there may be a problem with DIOCSYNCSLICEINFO.
753 set_ds_wlabel(ssp, slice, TRUE); /* XXX invert */
754 lp1 = clone_label(lp);
755 TRACE(("readdisklabel\n"));
756 if (flags & DSO_NOLABELS)
759 msg = readdisklabel(dev1, lp1);
762 * readdisklabel() returns NULL for success, and an
763 * error string for failure.
765 * If there isn't a label on the disk, and if the
766 * DSO_COMPATLABEL is set, we want to use the
767 * faked-up label provided by the caller.
769 * So we set msg to NULL to indicate that there is
770 * no failure (since we have a faked-up label),
771 * free lp1, and then clone it again from lp.
772 * (In case readdisklabel() modified lp1.)
774 if (msg != NULL && (flags & DSO_COMPATLABEL)) {
777 lp1 = clone_label(lp);
781 msg = fixlabel(sname, sp, lp1, FALSE);
782 if (msg == NULL && lp1->d_secsize != ssp->dss_secsize)
783 msg = "inconsistent sector size";
785 if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
786 log(LOG_WARNING, "%s: cannot find label (%s)\n",
791 if (lp1->d_flags & D_BADSECT) {
792 log(LOG_ERR, "%s: bad sector table not supported\n",
797 set_ds_label(ssp, slice, lp1);
798 set_ds_labeldevs(dev1, ssp);
799 set_ds_wlabel(ssp, slice, FALSE);
802 slice = dkslice(dev);
803 if (slice >= ssp->dss_nslices)
805 sp = &ssp->dss_slices[slice];
808 && (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions))
809 return (EINVAL); /* XXX needs translation */
811 sp->ds_openmask |= mask;
818 struct diskslices **sspp;
820 struct disklabel *lp;
823 struct diskslices *ssp;
825 slice = dkslice(dev);
828 if (ssp == NULL || slice >= ssp->dss_nslices
829 || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
830 if (dev_dopen(dev, FREAD, S_IFCHR, NULL) != 0)
832 dev_dclose(dev, FREAD, S_IFCHR, NULL);
835 lp = ssp->dss_slices[slice].ds_label;
838 return ((int)lp->d_partitions[part].p_size);
842 free_ds_label(ssp, slice)
843 struct diskslices *ssp;
846 struct disklabel *lp;
847 struct diskslice *sp;
849 sp = &ssp->dss_slices[slice];
854 set_ds_label(ssp, slice, (struct disklabel *)NULL);
858 fixlabel(sname, sp, lp, writeflag)
860 struct diskslice *sp;
861 struct disklabel *lp;
867 struct partition *pp;
871 /* These errors "can't happen" so don't bother reporting details. */
872 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC)
873 return ("fixlabel: invalid magic");
874 if (dkcksum(lp) != 0)
875 return ("fixlabel: invalid checksum");
877 pp = &lp->d_partitions[RAW_PART];
880 offset = sp->ds_offset;
882 start = sp->ds_offset;
883 offset = -sp->ds_offset;
885 if (pp->p_offset != start) {
888 "%s: rejecting BSD label: raw partition offset != slice offset\n",
890 slice_info(sname, sp);
891 partition_info(sname, RAW_PART, pp);
893 return ("fixlabel: raw partition offset != slice offset");
895 if (pp->p_size != sp->ds_size) {
897 printf("%s: raw partition size != slice size\n", sname);
898 slice_info(sname, sp);
899 partition_info(sname, RAW_PART, pp);
901 if (pp->p_size > sp->ds_size) {
903 return ("fixlabel: raw partition size > slice size");
904 printf("%s: truncating raw partition\n", sname);
905 pp->p_size = sp->ds_size;
908 end = start + sp->ds_size;
910 return ("fixlabel: slice wraps");
911 if (lp->d_secpercyl <= 0)
912 return ("fixlabel: d_secpercyl <= 0");
915 for (part = 0; part < lp->d_npartitions; part++, pp++) {
916 if (pp->p_offset != 0 || pp->p_size != 0) {
917 if (pp->p_offset < start
918 || pp->p_offset + pp->p_size > end
919 || pp->p_offset + pp->p_size < pp->p_offset) {
922 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
925 slice_info(sname, sp);
928 partition_info(sname, part, pp);
930 /* XXX else silently discard junk. */
931 bzero(pp, sizeof *pp);
933 pp->p_offset += offset;
936 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
937 lp->d_secperunit = sp->ds_size;
939 lp->d_checksum = dkcksum(lp);
944 partition_info(sname, part, pp)
947 struct partition *pp;
949 printf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
950 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
955 slice_info(sname, sp)
957 struct diskslice *sp;
959 printf("%s: start %lu, end %lu, size %lu\n", sname,
960 sp->ds_offset, sp->ds_offset + sp->ds_size - 1, sp->ds_size);
964 set_ds_label(ssp, slice, lp)
965 struct diskslices *ssp;
967 struct disklabel *lp;
969 ssp->dss_slices[slice].ds_label = lp;
970 if (slice == COMPATIBILITY_SLICE)
971 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_label = lp;
972 else if (slice == ssp->dss_first_bsd_slice)
973 ssp->dss_slices[COMPATIBILITY_SLICE].ds_label = lp;
976 /* XXX remove this? */
978 set_ds_labeldevs(dev, ssp)
980 struct diskslices *ssp;
985 set_ds_wlabel(ssp, slice, wlabel)
986 struct diskslices *ssp;
990 ssp->dss_slices[slice].ds_wlabel = wlabel;
991 if (slice == COMPATIBILITY_SLICE)
992 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_wlabel = wlabel;
993 else if (slice == ssp->dss_first_bsd_slice)
994 ssp->dss_slices[COMPATIBILITY_SLICE].ds_wlabel = wlabel;