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.3 2003/06/23 17:55:41 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>
62 #include <ufs/ffs/fs.h>
64 #define TRACE(str) do { if (ds_debug) printf str; } while (0)
66 typedef u_char bool_t;
68 static volatile bool_t ds_debug;
70 static struct disklabel *clone_label __P((struct disklabel *lp));
71 static void dsiodone __P((struct buf *bp));
72 static char *fixlabel __P((char *sname, struct diskslice *sp,
73 struct disklabel *lp, int writeflag));
74 static void free_ds_label __P((struct diskslices *ssp, int slice));
75 static void partition_info __P((char *sname, int part, struct partition *pp));
76 static void slice_info __P((char *sname, struct diskslice *sp));
77 static void set_ds_label __P((struct diskslices *ssp, int slice,
78 struct disklabel *lp));
79 static void set_ds_labeldevs __P((dev_t dev, struct diskslices *ssp));
80 static void set_ds_wlabel __P((struct diskslices *ssp, int slice,
84 * Duplicate a label for the whole disk, and initialize defaults in the
85 * copy for fields that are not already initialized. The caller only
86 * needs to initialize d_secsize and d_secperunit, and zero the fields
87 * that are to be defaulted.
89 static struct disklabel *
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.
138 struct diskslices *ssp;
143 struct disklabel *lp;
146 struct partition *pp;
148 daddr_t slicerel_secno;
149 struct diskslice *sp;
154 printf("dscheck(%s): negative b_blkno %ld\n",
155 devtoname(bp->b_dev), (long)blkno);
156 bp->b_error = EINVAL;
159 sp = &ssp->dss_slices[dkslice(bp->b_dev)];
161 if (ssp->dss_secmult == 1) {
162 if (bp->b_bcount % (u_long)DEV_BSIZE)
165 nsec = bp->b_bcount >> DEV_BSHIFT;
166 } else if (ssp->dss_secshift != -1) {
167 if (bp->b_bcount & (ssp->dss_secsize - 1))
169 if (blkno & (ssp->dss_secmult - 1))
171 secno = blkno >> ssp->dss_secshift;
172 nsec = bp->b_bcount >> (DEV_BSHIFT + ssp->dss_secshift);
174 if (bp->b_bcount % ssp->dss_secsize)
176 if (blkno % ssp->dss_secmult)
178 secno = blkno / ssp->dss_secmult;
179 nsec = bp->b_bcount / ssp->dss_secsize;
182 labelsect = -LABELSECTOR - 1;
183 endsecno = sp->ds_size;
184 slicerel_secno = secno;
186 labelsect = lp->d_partitions[LABEL_PART].p_offset;
187 if (labelsect != 0) Debugger("labelsect != 0 in dscheck()");
188 pp = &lp->d_partitions[dkpart(bp->b_dev)];
189 endsecno = pp->p_size;
190 slicerel_secno = pp->p_offset + secno;
193 /* overwriting disk label ? */
194 /* XXX should also protect bootstrap in first 8K */
195 if (slicerel_secno <= LABELSECTOR + labelsect &&
197 slicerel_secno + nsec > LABELSECTOR + labelsect &&
199 (bp->b_flags & B_READ) == 0 && sp->ds_wlabel == 0) {
204 #if defined(DOSBBSECTOR) && defined(notyet)
205 /* overwriting master boot record? */
206 if (slicerel_secno <= DOSBBSECTOR && (bp->b_flags & B_READ) == 0 &&
207 sp->ds_wlabel == 0) {
213 /* beyond partition? */
214 if (secno + nsec > endsecno) {
215 /* if exactly at end of disk, return an EOF */
216 if (secno == endsecno) {
217 bp->b_resid = bp->b_bcount;
220 /* or truncate if part of it fits */
221 nsec = endsecno - secno;
223 bp->b_error = EINVAL;
226 bp->b_bcount = nsec * ssp->dss_secsize;
229 bp->b_pblkno = sp->ds_offset + slicerel_secno;
232 * Snoop on label accesses if the slice offset is nonzero. Fudge
233 * offsets in the label to keep the in-core label coherent with
236 if (slicerel_secno <= LABELSECTOR + labelsect
238 && slicerel_secno + nsec > LABELSECTOR + labelsect
240 && sp->ds_offset != 0) {
241 struct iodone_chain *ic;
243 ic = malloc(sizeof *ic , M_DEVBUF, M_WAITOK);
244 ic->ic_prev_flags = bp->b_flags;
245 ic->ic_prev_iodone = bp->b_iodone;
246 ic->ic_prev_iodone_chain = bp->b_iodone_chain;
247 ic->ic_args[0].ia_long = (LABELSECTOR + labelsect -
248 slicerel_secno) * ssp->dss_secsize;
249 ic->ic_args[1].ia_ptr = sp;
250 bp->b_flags |= B_CALL;
251 bp->b_iodone = dsiodone;
252 bp->b_iodone_chain = ic;
253 if (!(bp->b_flags & B_READ)) {
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 if (bp->b_vp != NULL) {
265 bp->b_vp->v_numoutput++;
268 /* XXX need name here. */
269 msg = fixlabel((char *)NULL, sp,
271 (bp->b_data + ic->ic_args[0].ia_long),
274 printf("dscheck(%s): %s\n",
275 devtoname(bp->b_dev), msg);
285 "dscheck(%s): b_bcount %ld is not on a sector boundary (ssize %d)\n",
286 devtoname(bp->b_dev), bp->b_bcount, ssp->dss_secsize);
287 bp->b_error = EINVAL;
292 "dscheck(%s): b_blkno %ld is not on a sector boundary (ssize %d)\n",
293 devtoname(bp->b_dev), (long)blkno, ssp->dss_secsize);
294 bp->b_error = EINVAL;
298 bp->b_resid = bp->b_bcount;
299 bp->b_flags |= B_ERROR;
304 dsclose(dev, mode, ssp)
307 struct diskslices *ssp;
310 struct diskslice *sp;
312 sp = &ssp->dss_slices[dkslice(dev)];
313 mask = 1 << dkpart(dev);
314 sp->ds_openmask &= ~mask;
319 struct diskslices **sspp;
322 struct diskslice *sp;
323 struct diskslices *ssp;
325 for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
326 sp = &ssp->dss_slices[slice];
327 free_ds_label(ssp, slice);
334 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
335 * is subject to the same restriction as dsopen().
338 dsioctl(dev, cmd, data, flags, sspp)
343 struct diskslices **sspp;
346 struct disklabel *lp;
351 struct diskslice *sp;
352 struct diskslices *ssp;
353 struct partition *pp;
355 slice = dkslice(dev);
357 sp = &ssp->dss_slices[slice];
362 lp = (struct disklabel *)data;
363 if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
364 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
366 bzero(lp, sizeof(struct disklabel));
369 lp->d_magic = DISKMAGIC;
370 lp->d_magic2 = DISKMAGIC;
371 pp = &lp->d_partitions[RAW_PART];
373 pp->p_size = sp->ds_size;
375 lp->d_npartitions = MAXPARTITIONS;
376 if (lp->d_interleave == 0)
377 lp->d_interleave = 1;
380 if (lp->d_nsectors == 0)
382 if (lp->d_ntracks == 0)
385 lp->d_bbsize = BBSIZE;
386 lp->d_sbsize = SBSIZE;
387 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
388 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
389 lp->d_secperunit = sp->ds_size;
391 lp->d_checksum = dkcksum(lp);
397 *(struct disklabel *)data = *lp;
404 *(struct disklabel **)data = lp;
411 ((struct partinfo *)data)->disklab = lp;
412 ((struct partinfo *)data)->part
413 = &lp->d_partitions[dkpart(dev)];
417 bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
422 if (slice == WHOLE_DISK_SLICE)
424 if (!(flags & FWRITE))
426 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
427 if (sp->ds_label == NULL)
428 bzero(lp, sizeof *lp);
430 bcopy(sp->ds_label, lp, sizeof *lp);
431 if (sp->ds_label == NULL)
434 openmask = sp->ds_openmask;
435 if (slice == COMPATIBILITY_SLICE)
436 openmask |= ssp->dss_slices[
437 ssp->dss_first_bsd_slice].ds_openmask;
438 else if (slice == ssp->dss_first_bsd_slice)
439 openmask |= ssp->dss_slices[
440 COMPATIBILITY_SLICE].ds_openmask;
442 error = setdisklabel(lp, (struct disklabel *)data,
444 /* XXX why doesn't setdisklabel() check this? */
445 if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
448 if (lp->d_secperunit > sp->ds_size)
450 for (part = 0; part < lp->d_npartitions; part++)
451 if (lp->d_partitions[part].p_size > sp->ds_size)
458 free_ds_label(ssp, slice);
459 set_ds_label(ssp, slice, lp);
460 set_ds_labeldevs(dev, ssp);
463 case DIOCSYNCSLICEINFO:
464 if (slice != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART)
467 for (slice = 0; slice < ssp->dss_nslices; slice++) {
468 openmask = ssp->dss_slices[slice].ds_openmask;
470 && (slice != WHOLE_DISK_SLICE
471 || openmask & ~(1 << RAW_PART)))
476 * Temporarily forget the current slices struct and read
478 * XXX should wait for current accesses on this disk to
479 * complete, then lock out future accesses and opens.
482 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
483 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
484 error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, lp);
492 * Reopen everything. This is a no-op except in the "force"
493 * case and when the raw bdev and cdev are both open. Abort
496 for (slice = 0; slice < ssp->dss_nslices; slice++) {
497 for (openmask = ssp->dss_slices[slice].ds_openmask,
498 part = 0; openmask; openmask >>= 1, part++) {
501 error = dsopen(dkmodslice(dkmodpart(dev, part),
503 S_IFCHR, ssp->dss_oflags, sspp,
518 error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp);
522 * XXX this used to hack on dk_openpart to fake opening
523 * partition 0 in case that is used instead of dkpart(dev).
525 old_wlabel = sp->ds_wlabel;
526 set_ds_wlabel(ssp, slice, TRUE);
527 error = writedisklabel(dev, sp->ds_label);
528 /* XXX should invalidate in-core label if write failed. */
529 set_ds_wlabel(ssp, slice, old_wlabel);
534 if (slice == WHOLE_DISK_SLICE)
537 if (!(flags & FWRITE))
539 set_ds_wlabel(ssp, slice, *(int *)data != 0);
551 struct iodone_chain *ic;
554 ic = bp->b_iodone_chain;
555 bp->b_flags = (ic->ic_prev_flags & B_CALL)
556 | (bp->b_flags & ~(B_CALL | B_DONE));
557 bp->b_iodone = ic->ic_prev_iodone;
558 bp->b_iodone_chain = ic->ic_prev_iodone_chain;
559 if (!(bp->b_flags & B_READ)
560 || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
561 msg = fixlabel((char *)NULL, ic->ic_args[1].ia_ptr,
563 (bp->b_data + ic->ic_args[0].ia_long),
574 struct diskslices *ssp;
580 for (slice = 0; slice < ssp->dss_nslices; slice++)
581 if (ssp->dss_slices[slice].ds_openmask)
587 * Allocate a slices "struct" and initialize it to contain only an empty
588 * compatibility slice (pointing to itself), a whole disk slice (covering
589 * the disk as described by the label), and (nslices - BASE_SLICES) empty
590 * slices beginning at BASE_SLICE.
593 dsmakeslicestruct(nslices, lp)
595 struct disklabel *lp;
597 struct diskslice *sp;
598 struct diskslices *ssp;
600 ssp = malloc(offsetof(struct diskslices, dss_slices) +
601 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
602 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
603 ssp->dss_nslices = nslices;
605 ssp->dss_secmult = lp->d_secsize / DEV_BSIZE;
606 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
607 ssp->dss_secshift = -1;
609 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
610 ssp->dss_secsize = lp->d_secsize;
611 sp = &ssp->dss_slices[0];
612 bzero(sp, nslices * sizeof *sp);
613 sp[WHOLE_DISK_SLICE].ds_size = lp->d_secperunit;
618 dsname(dev, unit, slice, part, partname)
625 static char name[32];
628 dname = devsw(dev)->d_name;
629 if (strlen(dname) > 16)
630 dname = "nametoolong";
631 snprintf(name, sizeof(name), "%s%d", dname, unit);
633 if (slice != WHOLE_DISK_SLICE || part != RAW_PART) {
634 partname[0] = 'a' + part;
636 if (slice != COMPATIBILITY_SLICE)
637 snprintf(name + strlen(name),
638 sizeof(name) - strlen(name), "s%d", slice - 1);
644 * This should only be called when the unit is inactive and the strategy
645 * routine should not allow it to become active unless we call it. Our
646 * strategy routine must be special to allow activity.
649 dsopen(dev, mode, flags, sspp, lp)
653 struct diskslices **sspp;
654 struct disklabel *lp;
658 struct disklabel *lp1;
666 struct diskslice *sp;
667 struct diskslices *ssp;
670 dev->si_bsize_phys = lp->d_secsize;
673 if (lp->d_secsize % DEV_BSIZE) {
674 printf("%s: invalid sector size %lu\n", devtoname(dev),
675 (u_long)lp->d_secsize);
680 * XXX reinitialize the slice table unless there is an open device
681 * on the unit. This should only be done if the media has changed.
684 need_init = !dsisopen(ssp);
685 if (ssp != NULL && need_init)
689 * Allocate a minimal slices "struct". This will become
690 * the final slices "struct" if we don't want real slices
691 * or if we can't find any real slices.
693 *sspp = dsmakeslicestruct(BASE_SLICE, lp);
695 if (!(flags & DSO_ONESLICE)) {
697 error = dsinit(dev, lp, sspp);
704 ssp->dss_oflags = flags;
707 * If there are no real slices, then make the compatiblity
708 * slice cover the whole disk.
710 if (ssp->dss_nslices == BASE_SLICE)
711 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
714 /* Point the compatibility slice at the BSD slice, if any. */
715 for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
716 sp = &ssp->dss_slices[slice];
717 if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
718 ssp->dss_first_bsd_slice = slice;
719 ssp->dss_slices[COMPATIBILITY_SLICE].ds_offset
721 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
723 ssp->dss_slices[COMPATIBILITY_SLICE].ds_type
729 ssp->dss_slices[WHOLE_DISK_SLICE].ds_label = clone_label(lp);
730 ssp->dss_slices[WHOLE_DISK_SLICE].ds_wlabel = TRUE;
734 * Initialize secondary info for all slices. It is needed for more
735 * than the current slice in the DEVFS case. XXX DEVFS is no more.
737 for (slice = 0; slice < ssp->dss_nslices; slice++) {
738 sp = &ssp->dss_slices[slice];
739 if (sp->ds_label != NULL
741 && slice != WHOLE_DISK_SLICE
745 dev1 = dkmodslice(dkmodpart(dev, RAW_PART), slice);
746 sname = dsname(dev, unit, slice, RAW_PART, partname);
748 * XXX this should probably only be done for the need_init
749 * case, but there may be a problem with DIOCSYNCSLICEINFO.
751 set_ds_wlabel(ssp, slice, TRUE); /* XXX invert */
752 lp1 = clone_label(lp);
753 TRACE(("readdisklabel\n"));
754 if (flags & DSO_NOLABELS)
757 msg = readdisklabel(dev1, lp1);
760 * readdisklabel() returns NULL for success, and an
761 * error string for failure.
763 * If there isn't a label on the disk, and if the
764 * DSO_COMPATLABEL is set, we want to use the
765 * faked-up label provided by the caller.
767 * So we set msg to NULL to indicate that there is
768 * no failure (since we have a faked-up label),
769 * free lp1, and then clone it again from lp.
770 * (In case readdisklabel() modified lp1.)
772 if (msg != NULL && (flags & DSO_COMPATLABEL)) {
775 lp1 = clone_label(lp);
779 msg = fixlabel(sname, sp, lp1, FALSE);
780 if (msg == NULL && lp1->d_secsize != ssp->dss_secsize)
781 msg = "inconsistent sector size";
783 if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
784 log(LOG_WARNING, "%s: cannot find label (%s)\n",
789 if (lp1->d_flags & D_BADSECT) {
790 log(LOG_ERR, "%s: bad sector table not supported\n",
795 set_ds_label(ssp, slice, lp1);
796 set_ds_labeldevs(dev1, ssp);
797 set_ds_wlabel(ssp, slice, FALSE);
800 slice = dkslice(dev);
801 if (slice >= ssp->dss_nslices)
803 sp = &ssp->dss_slices[slice];
806 && (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions))
807 return (EINVAL); /* XXX needs translation */
809 sp->ds_openmask |= mask;
816 struct diskslices **sspp;
818 struct disklabel *lp;
821 struct diskslices *ssp;
823 slice = dkslice(dev);
826 if (ssp == NULL || slice >= ssp->dss_nslices
827 || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
828 if (devsw(dev)->d_open(dev, FREAD, S_IFCHR, NULL) != 0)
830 devsw(dev)->d_close(dev, FREAD, S_IFCHR, NULL);
833 lp = ssp->dss_slices[slice].ds_label;
836 return ((int)lp->d_partitions[part].p_size);
840 free_ds_label(ssp, slice)
841 struct diskslices *ssp;
844 struct disklabel *lp;
845 struct diskslice *sp;
847 sp = &ssp->dss_slices[slice];
852 set_ds_label(ssp, slice, (struct disklabel *)NULL);
856 fixlabel(sname, sp, lp, writeflag)
858 struct diskslice *sp;
859 struct disklabel *lp;
865 struct partition *pp;
869 /* These errors "can't happen" so don't bother reporting details. */
870 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC)
871 return ("fixlabel: invalid magic");
872 if (dkcksum(lp) != 0)
873 return ("fixlabel: invalid checksum");
875 pp = &lp->d_partitions[RAW_PART];
878 offset = sp->ds_offset;
880 start = sp->ds_offset;
881 offset = -sp->ds_offset;
883 if (pp->p_offset != start) {
886 "%s: rejecting BSD label: raw partition offset != slice offset\n",
888 slice_info(sname, sp);
889 partition_info(sname, RAW_PART, pp);
891 return ("fixlabel: raw partition offset != slice offset");
893 if (pp->p_size != sp->ds_size) {
895 printf("%s: raw partition size != slice size\n", sname);
896 slice_info(sname, sp);
897 partition_info(sname, RAW_PART, pp);
899 if (pp->p_size > sp->ds_size) {
901 return ("fixlabel: raw partition size > slice size");
902 printf("%s: truncating raw partition\n", sname);
903 pp->p_size = sp->ds_size;
906 end = start + sp->ds_size;
908 return ("fixlabel: slice wraps");
909 if (lp->d_secpercyl <= 0)
910 return ("fixlabel: d_secpercyl <= 0");
913 for (part = 0; part < lp->d_npartitions; part++, pp++) {
914 if (pp->p_offset != 0 || pp->p_size != 0) {
915 if (pp->p_offset < start
916 || pp->p_offset + pp->p_size > end
917 || pp->p_offset + pp->p_size < pp->p_offset) {
920 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
923 slice_info(sname, sp);
926 partition_info(sname, part, pp);
928 /* XXX else silently discard junk. */
929 bzero(pp, sizeof *pp);
931 pp->p_offset += offset;
934 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
935 lp->d_secperunit = sp->ds_size;
937 lp->d_checksum = dkcksum(lp);
942 partition_info(sname, part, pp)
945 struct partition *pp;
947 printf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
948 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
953 slice_info(sname, sp)
955 struct diskslice *sp;
957 printf("%s: start %lu, end %lu, size %lu\n", sname,
958 sp->ds_offset, sp->ds_offset + sp->ds_size - 1, sp->ds_size);
962 set_ds_label(ssp, slice, lp)
963 struct diskslices *ssp;
965 struct disklabel *lp;
967 ssp->dss_slices[slice].ds_label = lp;
968 if (slice == COMPATIBILITY_SLICE)
969 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_label = lp;
970 else if (slice == ssp->dss_first_bsd_slice)
971 ssp->dss_slices[COMPATIBILITY_SLICE].ds_label = lp;
974 /* XXX remove this? */
976 set_ds_labeldevs(dev, ssp)
978 struct diskslices *ssp;
983 set_ds_wlabel(ssp, slice, wlabel)
984 struct diskslices *ssp;
988 ssp->dss_slices[slice].ds_wlabel = wlabel;
989 if (slice == COMPATIBILITY_SLICE)
990 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_wlabel = wlabel;
991 else if (slice == ssp->dss_first_bsd_slice)
992 ssp->dss_slices[COMPATIBILITY_SLICE].ds_wlabel = wlabel;