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 $
49 #include <sys/param.h>
50 #include <sys/systm.h>
53 #include <sys/disklabel.h>
54 #include <sys/diskslice.h>
55 #include <sys/fcntl.h>
56 #include <sys/malloc.h>
58 #include <sys/syslog.h>
59 #include <sys/vnode.h>
61 #include <ufs/ffs/fs.h>
63 #define TRACE(str) do { if (ds_debug) printf str; } while (0)
65 typedef u_char bool_t;
67 static volatile bool_t ds_debug;
69 static struct disklabel *clone_label __P((struct disklabel *lp));
70 static void dsiodone __P((struct buf *bp));
71 static char *fixlabel __P((char *sname, struct diskslice *sp,
72 struct disklabel *lp, int writeflag));
73 static void free_ds_label __P((struct diskslices *ssp, int slice));
74 static void partition_info __P((char *sname, int part, struct partition *pp));
75 static void slice_info __P((char *sname, struct diskslice *sp));
76 static void set_ds_label __P((struct diskslices *ssp, int slice,
77 struct disklabel *lp));
78 static void set_ds_labeldevs __P((dev_t dev, struct diskslices *ssp));
79 static void set_ds_wlabel __P((struct diskslices *ssp, int slice,
83 * Duplicate a label for the whole disk, and initialize defaults in the
84 * copy for fields that are not already initialized. The caller only
85 * needs to initialize d_secsize and d_secperunit, and zero the fields
86 * that are to be defaulted.
88 static struct disklabel *
92 struct disklabel *lp1;
94 lp1 = malloc(sizeof *lp1, M_DEVBUF, M_WAITOK);
97 if (lp1->d_typename[0] == '\0')
98 strncpy(lp1->d_typename, "amnesiac", sizeof(lp1->d_typename));
99 if (lp1->d_packname[0] == '\0')
100 strncpy(lp1->d_packname, "fictitious", sizeof(lp1->d_packname));
101 if (lp1->d_nsectors == 0)
102 lp1->d_nsectors = 32;
103 if (lp1->d_ntracks == 0)
105 lp1->d_secpercyl = lp1->d_nsectors * lp1->d_ntracks;
106 lp1->d_ncylinders = lp1->d_secperunit / lp1->d_secpercyl;
109 if (lp1->d_interleave == 0)
110 lp1->d_interleave = 1;
111 if (lp1->d_npartitions < RAW_PART + 1)
112 lp1->d_npartitions = MAXPARTITIONS;
113 if (lp1->d_bbsize == 0)
114 lp1->d_bbsize = BBSIZE;
115 if (lp1->d_sbsize == 0)
116 lp1->d_sbsize = SBSIZE;
117 lp1->d_partitions[RAW_PART].p_size = lp1->d_secperunit;
118 lp1->d_magic = DISKMAGIC;
119 lp1->d_magic2 = DISKMAGIC;
120 lp1->d_checksum = dkcksum(lp1);
125 * Determine the size of the transfer, and make sure it is
126 * within the boundaries of the partition. Adjust transfer
127 * if needed, and signal errors or early completion.
130 * o Split buffers that are too big for the device.
131 * o Check for overflow.
132 * o Finish cleaning this up.
137 struct diskslices *ssp;
142 struct disklabel *lp;
145 struct partition *pp;
147 daddr_t slicerel_secno;
148 struct diskslice *sp;
153 printf("dscheck(%s): negative b_blkno %ld\n",
154 devtoname(bp->b_dev), (long)blkno);
155 bp->b_error = EINVAL;
158 sp = &ssp->dss_slices[dkslice(bp->b_dev)];
160 if (ssp->dss_secmult == 1) {
161 if (bp->b_bcount % (u_long)DEV_BSIZE)
164 nsec = bp->b_bcount >> DEV_BSHIFT;
165 } else if (ssp->dss_secshift != -1) {
166 if (bp->b_bcount & (ssp->dss_secsize - 1))
168 if (blkno & (ssp->dss_secmult - 1))
170 secno = blkno >> ssp->dss_secshift;
171 nsec = bp->b_bcount >> (DEV_BSHIFT + ssp->dss_secshift);
173 if (bp->b_bcount % ssp->dss_secsize)
175 if (blkno % ssp->dss_secmult)
177 secno = blkno / ssp->dss_secmult;
178 nsec = bp->b_bcount / ssp->dss_secsize;
181 labelsect = -LABELSECTOR - 1;
182 endsecno = sp->ds_size;
183 slicerel_secno = secno;
185 labelsect = lp->d_partitions[LABEL_PART].p_offset;
186 if (labelsect != 0) Debugger("labelsect != 0 in dscheck()");
187 pp = &lp->d_partitions[dkpart(bp->b_dev)];
188 endsecno = pp->p_size;
189 slicerel_secno = pp->p_offset + secno;
192 /* overwriting disk label ? */
193 /* XXX should also protect bootstrap in first 8K */
194 if (slicerel_secno <= LABELSECTOR + labelsect &&
196 slicerel_secno + nsec > LABELSECTOR + labelsect &&
198 (bp->b_flags & B_READ) == 0 && sp->ds_wlabel == 0) {
203 #if defined(DOSBBSECTOR) && defined(notyet)
204 /* overwriting master boot record? */
205 if (slicerel_secno <= DOSBBSECTOR && (bp->b_flags & B_READ) == 0 &&
206 sp->ds_wlabel == 0) {
212 /* beyond partition? */
213 if (secno + nsec > endsecno) {
214 /* if exactly at end of disk, return an EOF */
215 if (secno == endsecno) {
216 bp->b_resid = bp->b_bcount;
219 /* or truncate if part of it fits */
220 nsec = endsecno - secno;
222 bp->b_error = EINVAL;
225 bp->b_bcount = nsec * ssp->dss_secsize;
228 bp->b_pblkno = sp->ds_offset + slicerel_secno;
231 * Snoop on label accesses if the slice offset is nonzero. Fudge
232 * offsets in the label to keep the in-core label coherent with
235 if (slicerel_secno <= LABELSECTOR + labelsect
237 && slicerel_secno + nsec > LABELSECTOR + labelsect
239 && sp->ds_offset != 0) {
240 struct iodone_chain *ic;
242 ic = malloc(sizeof *ic , M_DEVBUF, M_WAITOK);
243 ic->ic_prev_flags = bp->b_flags;
244 ic->ic_prev_iodone = bp->b_iodone;
245 ic->ic_prev_iodone_chain = bp->b_iodone_chain;
246 ic->ic_args[0].ia_long = (LABELSECTOR + labelsect -
247 slicerel_secno) * ssp->dss_secsize;
248 ic->ic_args[1].ia_ptr = sp;
249 bp->b_flags |= B_CALL;
250 bp->b_iodone = dsiodone;
251 bp->b_iodone_chain = ic;
252 if (!(bp->b_flags & B_READ)) {
254 * XXX even disklabel(8) writes directly so we need
255 * to adjust writes. Perhaps we should drop support
256 * for DIOCWLABEL (always write protect labels) and
257 * require the use of DIOCWDINFO.
259 * XXX probably need to copy the data to avoid even
260 * temporarily corrupting the in-core copy.
262 if (bp->b_vp != NULL) {
264 bp->b_vp->v_numoutput++;
267 /* XXX need name here. */
268 msg = fixlabel((char *)NULL, sp,
270 (bp->b_data + ic->ic_args[0].ia_long),
273 printf("dscheck(%s): %s\n",
274 devtoname(bp->b_dev), msg);
284 "dscheck(%s): b_bcount %ld is not on a sector boundary (ssize %d)\n",
285 devtoname(bp->b_dev), bp->b_bcount, ssp->dss_secsize);
286 bp->b_error = EINVAL;
291 "dscheck(%s): b_blkno %ld is not on a sector boundary (ssize %d)\n",
292 devtoname(bp->b_dev), (long)blkno, ssp->dss_secsize);
293 bp->b_error = EINVAL;
297 bp->b_resid = bp->b_bcount;
298 bp->b_flags |= B_ERROR;
303 dsclose(dev, mode, ssp)
306 struct diskslices *ssp;
309 struct diskslice *sp;
311 sp = &ssp->dss_slices[dkslice(dev)];
312 mask = 1 << dkpart(dev);
313 sp->ds_openmask &= ~mask;
318 struct diskslices **sspp;
321 struct diskslice *sp;
322 struct diskslices *ssp;
324 for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
325 sp = &ssp->dss_slices[slice];
326 free_ds_label(ssp, slice);
333 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
334 * is subject to the same restriction as dsopen().
337 dsioctl(dev, cmd, data, flags, sspp)
342 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);
459 set_ds_labeldevs(dev, ssp);
462 case DIOCSYNCSLICEINFO:
463 if (slice != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART)
466 for (slice = 0; slice < ssp->dss_nslices; slice++) {
467 openmask = ssp->dss_slices[slice].ds_openmask;
469 && (slice != WHOLE_DISK_SLICE
470 || openmask & ~(1 << RAW_PART)))
475 * Temporarily forget the current slices struct and read
477 * XXX should wait for current accesses on this disk to
478 * complete, then lock out future accesses and opens.
481 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
482 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
483 error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, lp);
491 * Reopen everything. This is a no-op except in the "force"
492 * case and when the raw bdev and cdev are both open. Abort
495 for (slice = 0; slice < ssp->dss_nslices; slice++) {
496 for (openmask = ssp->dss_slices[slice].ds_openmask,
497 part = 0; openmask; openmask >>= 1, part++) {
500 error = dsopen(dkmodslice(dkmodpart(dev, part),
502 S_IFCHR, ssp->dss_oflags, sspp,
517 error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp);
521 * XXX this used to hack on dk_openpart to fake opening
522 * partition 0 in case that is used instead of dkpart(dev).
524 old_wlabel = sp->ds_wlabel;
525 set_ds_wlabel(ssp, slice, TRUE);
526 error = writedisklabel(dev, sp->ds_label);
527 /* XXX should invalidate in-core label if write failed. */
528 set_ds_wlabel(ssp, slice, old_wlabel);
533 if (slice == WHOLE_DISK_SLICE)
536 if (!(flags & FWRITE))
538 set_ds_wlabel(ssp, slice, *(int *)data != 0);
550 struct iodone_chain *ic;
553 ic = bp->b_iodone_chain;
554 bp->b_flags = (ic->ic_prev_flags & B_CALL)
555 | (bp->b_flags & ~(B_CALL | B_DONE));
556 bp->b_iodone = ic->ic_prev_iodone;
557 bp->b_iodone_chain = ic->ic_prev_iodone_chain;
558 if (!(bp->b_flags & B_READ)
559 || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
560 msg = fixlabel((char *)NULL, ic->ic_args[1].ia_ptr,
562 (bp->b_data + ic->ic_args[0].ia_long),
573 struct diskslices *ssp;
579 for (slice = 0; slice < ssp->dss_nslices; slice++)
580 if (ssp->dss_slices[slice].ds_openmask)
586 * Allocate a slices "struct" and initialize it to contain only an empty
587 * compatibility slice (pointing to itself), a whole disk slice (covering
588 * the disk as described by the label), and (nslices - BASE_SLICES) empty
589 * slices beginning at BASE_SLICE.
592 dsmakeslicestruct(nslices, lp)
594 struct disklabel *lp;
596 struct diskslice *sp;
597 struct diskslices *ssp;
599 ssp = malloc(offsetof(struct diskslices, dss_slices) +
600 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
601 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
602 ssp->dss_nslices = nslices;
604 ssp->dss_secmult = lp->d_secsize / DEV_BSIZE;
605 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
606 ssp->dss_secshift = -1;
608 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
609 ssp->dss_secsize = lp->d_secsize;
610 sp = &ssp->dss_slices[0];
611 bzero(sp, nslices * sizeof *sp);
612 sp[WHOLE_DISK_SLICE].ds_size = lp->d_secperunit;
617 dsname(dev, unit, slice, part, partname)
624 static char name[32];
627 dname = devsw(dev)->d_name;
628 if (strlen(dname) > 16)
629 dname = "nametoolong";
630 snprintf(name, sizeof(name), "%s%d", dname, unit);
632 if (slice != WHOLE_DISK_SLICE || part != RAW_PART) {
633 partname[0] = 'a' + part;
635 if (slice != COMPATIBILITY_SLICE)
636 snprintf(name + strlen(name),
637 sizeof(name) - strlen(name), "s%d", slice - 1);
643 * This should only be called when the unit is inactive and the strategy
644 * routine should not allow it to become active unless we call it. Our
645 * strategy routine must be special to allow activity.
648 dsopen(dev, mode, flags, sspp, lp)
652 struct diskslices **sspp;
653 struct disklabel *lp;
657 struct disklabel *lp1;
665 struct diskslice *sp;
666 struct diskslices *ssp;
669 dev->si_bsize_phys = lp->d_secsize;
672 if (lp->d_secsize % DEV_BSIZE) {
673 printf("%s: invalid sector size %lu\n", devtoname(dev),
674 (u_long)lp->d_secsize);
679 * XXX reinitialize the slice table unless there is an open device
680 * on the unit. This should only be done if the media has changed.
683 need_init = !dsisopen(ssp);
684 if (ssp != NULL && need_init)
688 * Allocate a minimal slices "struct". This will become
689 * the final slices "struct" if we don't want real slices
690 * or if we can't find any real slices.
692 *sspp = dsmakeslicestruct(BASE_SLICE, lp);
694 if (!(flags & DSO_ONESLICE)) {
696 error = dsinit(dev, lp, sspp);
703 ssp->dss_oflags = flags;
706 * If there are no real slices, then make the compatiblity
707 * slice cover the whole disk.
709 if (ssp->dss_nslices == BASE_SLICE)
710 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
713 /* Point the compatibility slice at the BSD slice, if any. */
714 for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
715 sp = &ssp->dss_slices[slice];
716 if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
717 ssp->dss_first_bsd_slice = slice;
718 ssp->dss_slices[COMPATIBILITY_SLICE].ds_offset
720 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
722 ssp->dss_slices[COMPATIBILITY_SLICE].ds_type
728 ssp->dss_slices[WHOLE_DISK_SLICE].ds_label = clone_label(lp);
729 ssp->dss_slices[WHOLE_DISK_SLICE].ds_wlabel = TRUE;
733 * Initialize secondary info for all slices. It is needed for more
734 * than the current slice in the DEVFS case. XXX DEVFS is no more.
736 for (slice = 0; slice < ssp->dss_nslices; slice++) {
737 sp = &ssp->dss_slices[slice];
738 if (sp->ds_label != NULL
740 && slice != WHOLE_DISK_SLICE
744 dev1 = dkmodslice(dkmodpart(dev, RAW_PART), slice);
745 sname = dsname(dev, unit, slice, RAW_PART, partname);
747 * XXX this should probably only be done for the need_init
748 * case, but there may be a problem with DIOCSYNCSLICEINFO.
750 set_ds_wlabel(ssp, slice, TRUE); /* XXX invert */
751 lp1 = clone_label(lp);
752 TRACE(("readdisklabel\n"));
753 if (flags & DSO_NOLABELS)
756 msg = readdisklabel(dev1, lp1);
759 * readdisklabel() returns NULL for success, and an
760 * error string for failure.
762 * If there isn't a label on the disk, and if the
763 * DSO_COMPATLABEL is set, we want to use the
764 * faked-up label provided by the caller.
766 * So we set msg to NULL to indicate that there is
767 * no failure (since we have a faked-up label),
768 * free lp1, and then clone it again from lp.
769 * (In case readdisklabel() modified lp1.)
771 if (msg != NULL && (flags & DSO_COMPATLABEL)) {
774 lp1 = clone_label(lp);
778 msg = fixlabel(sname, sp, lp1, FALSE);
779 if (msg == NULL && lp1->d_secsize != ssp->dss_secsize)
780 msg = "inconsistent sector size";
782 if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
783 log(LOG_WARNING, "%s: cannot find label (%s)\n",
788 if (lp1->d_flags & D_BADSECT) {
789 log(LOG_ERR, "%s: bad sector table not supported\n",
794 set_ds_label(ssp, slice, lp1);
795 set_ds_labeldevs(dev1, ssp);
796 set_ds_wlabel(ssp, slice, FALSE);
799 slice = dkslice(dev);
800 if (slice >= ssp->dss_nslices)
802 sp = &ssp->dss_slices[slice];
805 && (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions))
806 return (EINVAL); /* XXX needs translation */
808 sp->ds_openmask |= mask;
815 struct diskslices **sspp;
817 struct disklabel *lp;
820 struct diskslices *ssp;
822 slice = dkslice(dev);
825 if (ssp == NULL || slice >= ssp->dss_nslices
826 || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
827 if (devsw(dev)->d_open(dev, FREAD, S_IFCHR,
828 (struct proc *)NULL) != 0)
830 devsw(dev)->d_close(dev, FREAD, S_IFCHR, (struct proc *)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;