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 $
49 #include <sys/param.h>
50 #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(struct disklabel *lp);
71 static void dsiodone(struct bio *bp);
72 static char *fixlabel(char *sname, struct diskslice *sp,
73 struct disklabel *lp, int writeflag);
74 static void free_ds_label(struct diskslices *ssp, int slice);
75 static void partition_info(char *sname, int part, struct partition *pp);
76 static void slice_info(char *sname, struct diskslice *sp);
77 static void set_ds_label(struct diskslices *ssp, int slice,
78 struct disklabel *lp);
79 static void set_ds_labeldevs(dev_t dev, struct diskslices *ssp);
80 static void set_ds_wlabel(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 dkmodpart(dev_t dev, int part)
128 return (makedev(major(dev), (minor(dev) & ~7) | part));
132 dkmodslice(dev_t dev, int slice)
134 return (makedev(major(dev), (minor(dev) & ~0x1f0000) | (slice << 16)));
140 return (((minor(dev) >> 16) & 0x1e0) | ((minor(dev) >> 3) & 0x1f));
144 * Determine the size of the transfer, and make sure it is
145 * within the boundaries of the partition. Adjust transfer
146 * if needed, and signal errors or early completion.
149 * o Split buffers that are too big for the device.
150 * o Check for overflow.
151 * o Finish cleaning this up.
156 struct diskslices *ssp;
161 struct disklabel *lp;
164 struct partition *pp;
166 daddr_t slicerel_secno;
167 struct diskslice *sp;
169 blkno = bp->bio_blkno;
171 printf("dscheck(%s): negative bio_blkno %ld\n",
172 devtoname(bp->bio_dev), (long)blkno);
173 bp->bio_error = EINVAL;
176 sp = &ssp->dss_slices[dkslice(bp->bio_dev)];
178 if (ssp->dss_secmult == 1) {
179 if (bp->bio_bcount % (u_long)DEV_BSIZE)
182 nsec = bp->bio_bcount >> DEV_BSHIFT;
183 } else if (ssp->dss_secshift != -1) {
184 if (bp->bio_bcount & (ssp->dss_secsize - 1))
186 if (blkno & (ssp->dss_secmult - 1))
188 secno = blkno >> ssp->dss_secshift;
189 nsec = bp->bio_bcount >> (DEV_BSHIFT + ssp->dss_secshift);
191 if (bp->bio_bcount % ssp->dss_secsize)
193 if (blkno % ssp->dss_secmult)
195 secno = blkno / ssp->dss_secmult;
196 nsec = bp->bio_bcount / ssp->dss_secsize;
199 labelsect = -LABELSECTOR - 1;
200 endsecno = sp->ds_size;
201 slicerel_secno = secno;
203 labelsect = lp->d_partitions[LABEL_PART].p_offset;
204 if (labelsect != 0) Debugger("labelsect != 0 in dscheck()");
205 pp = &lp->d_partitions[dkpart(bp->bio_dev)];
206 endsecno = pp->p_size;
207 slicerel_secno = pp->p_offset + secno;
210 /* overwriting disk label ? */
211 /* XXX should also protect bootstrap in first 8K */
212 if (slicerel_secno <= LABELSECTOR + labelsect &&
214 slicerel_secno + nsec > LABELSECTOR + labelsect &&
216 (bp->bio_cmd == BIO_WRITE) && sp->ds_wlabel == 0) {
217 bp->bio_error = EROFS;
221 #if defined(DOSBBSECTOR) && defined(notyet)
222 /* overwriting master boot record? */
223 if (slicerel_secno <= DOSBBSECTOR && (bp->bio_cmd == BIO_WRITE) &&
224 sp->ds_wlabel == 0) {
225 bp->bio_error = EROFS;
230 /* beyond partition? */
231 if (secno + nsec > endsecno) {
232 /* if exactly at end of disk, return an EOF */
233 if (secno == endsecno) {
234 bp->bio_resid = bp->bio_bcount;
237 /* or truncate if part of it fits */
238 nsec = endsecno - secno;
240 bp->bio_error = EINVAL;
243 bp->bio_bcount = nsec * ssp->dss_secsize;
246 bp->bio_pblkno = sp->ds_offset + slicerel_secno;
249 * Snoop on label accesses if the slice offset is nonzero. Fudge
250 * offsets in the label to keep the in-core label coherent with
253 if (slicerel_secno <= LABELSECTOR + labelsect
255 && slicerel_secno + nsec > LABELSECTOR + labelsect
257 && sp->ds_offset != 0) {
258 struct iodone_chain *ic;
260 ic = malloc(sizeof *ic , M_DEVBUF, M_WAITOK);
261 ic->ic_prev_flags = bp->bio_flags;
262 ic->ic_prev_iodone = bp->bio_done;
263 ic->ic_prev_iodone_chain = bp->bio_done_chain;
264 ic->ic_args[0].ia_long = (LABELSECTOR + labelsect -
265 slicerel_secno) * ssp->dss_secsize;
266 ic->ic_args[1].ia_ptr = sp;
267 bp->bio_done = dsiodone;
268 bp->bio_done_chain = ic;
269 if (!(bp->bio_cmd == BIO_READ)) {
271 * XXX even disklabel(8) writes directly so we need
272 * to adjust writes. Perhaps we should drop support
273 * for DIOCWLABEL (always write protect labels) and
274 * require the use of DIOCWDINFO.
276 * XXX probably need to copy the data to avoid even
277 * temporarily corrupting the in-core copy.
279 /* XXX need name here. */
280 msg = fixlabel((char *)NULL, sp,
282 (bp->bio_data + ic->ic_args[0].ia_long),
285 printf("dscheck(%s): %s\n",
286 devtoname(bp->bio_dev), msg);
287 bp->bio_error = EROFS;
296 "dscheck(%s): bio_bcount %ld is not on a sector boundary (ssize %d)\n",
297 devtoname(bp->bio_dev), bp->bio_bcount, ssp->dss_secsize);
298 bp->bio_error = EINVAL;
303 "dscheck(%s): bio_blkno %ld is not on a sector boundary (ssize %d)\n",
304 devtoname(bp->bio_dev), (long)blkno, ssp->dss_secsize);
305 bp->bio_error = EINVAL;
309 bp->bio_resid = bp->bio_bcount;
310 bp->bio_flags |= BIO_ERROR;
315 dsclose(dev, mode, ssp)
318 struct diskslices *ssp;
321 struct diskslice *sp;
323 sp = &ssp->dss_slices[dkslice(dev)];
324 mask = 1 << dkpart(dev);
325 sp->ds_openmask &= ~mask;
330 struct diskslices **sspp;
333 struct diskslice *sp;
334 struct diskslices *ssp;
336 for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
337 sp = &ssp->dss_slices[slice];
338 free_ds_label(ssp, slice);
345 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
346 * is subject to the same restriction as dsopen().
349 dsioctl(dev, cmd, data, flags, sspp)
354 struct diskslices **sspp;
357 struct disklabel *lp;
362 struct diskslice *sp;
363 struct diskslices *ssp;
364 struct partition *pp;
366 slice = dkslice(dev);
368 sp = &ssp->dss_slices[slice];
373 lp = (struct disklabel *)data;
374 if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
375 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
377 bzero(lp, sizeof(struct disklabel));
380 lp->d_magic = DISKMAGIC;
381 lp->d_magic2 = DISKMAGIC;
382 pp = &lp->d_partitions[RAW_PART];
384 pp->p_size = sp->ds_size;
386 lp->d_npartitions = MAXPARTITIONS;
387 if (lp->d_interleave == 0)
388 lp->d_interleave = 1;
391 if (lp->d_nsectors == 0)
393 if (lp->d_ntracks == 0)
396 lp->d_bbsize = BBSIZE;
397 lp->d_sbsize = SBSIZE;
398 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
399 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
400 lp->d_secperunit = sp->ds_size;
402 lp->d_checksum = dkcksum(lp);
408 *(struct disklabel *)data = *lp;
411 case DIOCGSECTORSIZE:
414 *(u_int *)data = lp->d_secsize;
420 *(off_t *)data = (off_t)lp->d_partitions[dkpart(dev)].p_size *
425 bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
430 if (slice == WHOLE_DISK_SLICE)
432 if (!(flags & FWRITE))
434 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
435 if (sp->ds_label == NULL)
436 bzero(lp, sizeof *lp);
438 bcopy(sp->ds_label, lp, sizeof *lp);
439 if (sp->ds_label == NULL)
442 openmask = sp->ds_openmask;
443 if (slice == COMPATIBILITY_SLICE)
444 openmask |= ssp->dss_slices[
445 ssp->dss_first_bsd_slice].ds_openmask;
446 else if (slice == ssp->dss_first_bsd_slice)
447 openmask |= ssp->dss_slices[
448 COMPATIBILITY_SLICE].ds_openmask;
450 error = setdisklabel(lp, (struct disklabel *)data,
452 /* XXX why doesn't setdisklabel() check this? */
453 if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
456 if (lp->d_secperunit > sp->ds_size)
458 for (part = 0; part < lp->d_npartitions; part++)
459 if (lp->d_partitions[part].p_size > sp->ds_size)
466 free_ds_label(ssp, slice);
467 set_ds_label(ssp, slice, lp);
468 set_ds_labeldevs(dev, ssp);
471 case DIOCSYNCSLICEINFO:
472 if (slice != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART)
475 for (slice = 0; slice < ssp->dss_nslices; slice++) {
476 openmask = ssp->dss_slices[slice].ds_openmask;
478 && (slice != WHOLE_DISK_SLICE
479 || openmask & ~(1 << RAW_PART)))
484 * Temporarily forget the current slices struct and read
486 * XXX should wait for current accesses on this disk to
487 * complete, then lock out future accesses and opens.
490 lp = malloc(sizeof *lp, M_DEVBUF, M_WAITOK);
491 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
492 error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, lp);
500 * Reopen everything. This is a no-op except in the "force"
501 * case and when the raw bdev and cdev are both open. Abort
504 for (slice = 0; slice < ssp->dss_nslices; slice++) {
505 for (openmask = ssp->dss_slices[slice].ds_openmask,
506 part = 0; openmask; openmask >>= 1, part++) {
509 error = dsopen(dkmodslice(dkmodpart(dev, part),
511 S_IFCHR, ssp->dss_oflags, sspp,
526 error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp);
530 * XXX this used to hack on dk_openpart to fake opening
531 * partition 0 in case that is used instead of dkpart(dev).
533 old_wlabel = sp->ds_wlabel;
534 set_ds_wlabel(ssp, slice, TRUE);
535 error = writedisklabel(dev, sp->ds_label);
536 /* XXX should invalidate in-core label if write failed. */
537 set_ds_wlabel(ssp, slice, old_wlabel);
542 if (slice == WHOLE_DISK_SLICE)
545 if (!(flags & FWRITE))
547 set_ds_wlabel(ssp, slice, *(int *)data != 0);
559 struct iodone_chain *ic;
562 ic = bp->bio_done_chain;
563 bp->bio_done = ic->ic_prev_iodone;
564 bp->bio_done_chain = ic->ic_prev_iodone_chain;
565 if (!(bp->bio_cmd == BIO_READ)
566 || (!(bp->bio_flags & BIO_ERROR) && bp->bio_error == 0)) {
567 msg = fixlabel((char *)NULL, ic->ic_args[1].ia_ptr,
569 (bp->bio_data + ic->ic_args[0].ia_long),
580 struct diskslices *ssp;
586 for (slice = 0; slice < ssp->dss_nslices; slice++)
587 if (ssp->dss_slices[slice].ds_openmask)
593 * Allocate a slices "struct" and initialize it to contain only an empty
594 * compatibility slice (pointing to itself), a whole disk slice (covering
595 * the disk as described by the label), and (nslices - BASE_SLICES) empty
596 * slices beginning at BASE_SLICE.
599 dsmakeslicestruct(nslices, lp)
601 struct disklabel *lp;
603 struct diskslice *sp;
604 struct diskslices *ssp;
606 ssp = malloc(offsetof(struct diskslices, dss_slices) +
607 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
608 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
609 ssp->dss_nslices = nslices;
611 ssp->dss_secmult = lp->d_secsize / DEV_BSIZE;
612 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
613 ssp->dss_secshift = -1;
615 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
616 ssp->dss_secsize = lp->d_secsize;
617 sp = &ssp->dss_slices[0];
618 bzero(sp, nslices * sizeof *sp);
619 sp[WHOLE_DISK_SLICE].ds_size = lp->d_secperunit;
624 dsname(dev, unit, slice, part, partname)
631 static char name[32];
634 dname = devsw(dev)->d_name;
635 if (strlen(dname) > 16)
636 dname = "nametoolong";
637 snprintf(name, sizeof(name), "%s%d", dname, unit);
639 if (slice != WHOLE_DISK_SLICE || part != RAW_PART) {
640 partname[0] = 'a' + part;
642 if (slice != COMPATIBILITY_SLICE)
643 snprintf(name + strlen(name),
644 sizeof(name) - strlen(name), "s%d", slice - 1);
650 * This should only be called when the unit is inactive and the strategy
651 * routine should not allow it to become active unless we call it. Our
652 * strategy routine must be special to allow activity.
655 dsopen(dev, mode, flags, sspp, lp)
659 struct diskslices **sspp;
660 struct disklabel *lp;
664 struct disklabel *lp1;
671 struct diskslice *sp;
672 struct diskslices *ssp;
675 dev->si_bsize_phys = lp->d_secsize;
678 if (lp->d_secsize % DEV_BSIZE) {
679 printf("%s: invalid sector size %lu\n", devtoname(dev),
680 (u_long)lp->d_secsize);
685 * XXX reinitialize the slice table unless there is an open device
686 * on the unit. This should only be done if the media has changed.
689 if (!dsisopen(ssp)) {
693 * Allocate a minimal slices "struct". This will become
694 * the final slices "struct" if we don't want real slices
695 * or if we can't find any real slices.
697 *sspp = dsmakeslicestruct(BASE_SLICE, lp);
699 if (!(flags & DSO_ONESLICE)) {
701 error = dsinit(dev, lp, sspp);
708 ssp->dss_oflags = flags;
711 * If there are no real slices, then make the compatiblity
712 * slice cover the whole disk.
714 if (ssp->dss_nslices == BASE_SLICE)
715 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
718 /* Point the compatibility slice at the BSD slice, if any. */
719 for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
720 sp = &ssp->dss_slices[slice];
721 if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
722 ssp->dss_first_bsd_slice = slice;
723 ssp->dss_slices[COMPATIBILITY_SLICE].ds_offset
725 ssp->dss_slices[COMPATIBILITY_SLICE].ds_size
727 ssp->dss_slices[COMPATIBILITY_SLICE].ds_type
733 ssp->dss_slices[WHOLE_DISK_SLICE].ds_label = clone_label(lp);
734 ssp->dss_slices[WHOLE_DISK_SLICE].ds_wlabel = TRUE;
737 /* Initialize secondary info for all slices. */
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);
748 sname = dsname(dev, unit, slice, RAW_PART, partname);
751 sname = dev1->si_name;
754 * XXX this should probably only be done for the need_init
755 * case, but there may be a problem with DIOCSYNCSLICEINFO.
757 set_ds_wlabel(ssp, slice, TRUE); /* XXX invert */
758 lp1 = clone_label(lp);
759 TRACE(("readdisklabel\n"));
760 if (flags & DSO_NOLABELS)
763 msg = readdisklabel(dev1, lp1);
766 * readdisklabel() returns NULL for success, and an
767 * error string for failure.
769 * If there isn't a label on the disk, and if the
770 * DSO_COMPATLABEL is set, we want to use the
771 * faked-up label provided by the caller.
773 * So we set msg to NULL to indicate that there is
774 * no failure (since we have a faked-up label),
775 * free lp1, and then clone it again from lp.
776 * (In case readdisklabel() modified lp1.)
778 if (msg != NULL && (flags & DSO_COMPATLABEL)) {
781 lp1 = clone_label(lp);
785 msg = fixlabel(sname, sp, lp1, FALSE);
786 if (msg == NULL && lp1->d_secsize != ssp->dss_secsize)
787 msg = "inconsistent sector size";
789 if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
790 log(LOG_WARNING, "%s: cannot find label (%s)\n",
795 if (lp1->d_flags & D_BADSECT) {
796 log(LOG_ERR, "%s: bad sector table not supported\n",
801 set_ds_label(ssp, slice, lp1);
802 set_ds_labeldevs(dev1, ssp);
803 set_ds_wlabel(ssp, slice, FALSE);
806 slice = dkslice(dev);
807 if (slice >= ssp->dss_nslices)
809 sp = &ssp->dss_slices[slice];
812 && (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions))
813 return (EINVAL); /* XXX needs translation */
815 sp->ds_openmask |= mask;
822 struct diskslices **sspp;
824 struct disklabel *lp;
827 struct diskslices *ssp;
829 slice = dkslice(dev);
832 if (ssp == NULL || slice >= ssp->dss_nslices
833 || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
834 if (devsw(dev)->d_open(dev, FREAD, S_IFCHR,
835 (struct thread *)NULL) != 0)
837 devsw(dev)->d_close(dev, FREAD, S_IFCHR, (struct thread *)NULL);
840 lp = ssp->dss_slices[slice].ds_label;
843 return ((int)lp->d_partitions[part].p_size);
847 free_ds_label(ssp, slice)
848 struct diskslices *ssp;
851 struct disklabel *lp;
852 struct diskslice *sp;
854 sp = &ssp->dss_slices[slice];
859 set_ds_label(ssp, slice, (struct disklabel *)NULL);
864 fixlabel(sname, sp, lp, writeflag)
866 struct diskslice *sp;
867 struct disklabel *lp;
873 struct partition *pp;
877 /* These errors "can't happen" so don't bother reporting details. */
878 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC)
879 return ("fixlabel: invalid magic");
880 if (dkcksum(lp) != 0)
881 return ("fixlabel: invalid checksum");
883 pp = &lp->d_partitions[RAW_PART];
886 offset = sp->ds_offset;
888 start = sp->ds_offset;
889 offset = -sp->ds_offset;
891 if (pp->p_offset != start) {
894 "%s: rejecting BSD label: raw partition offset != slice offset\n",
896 slice_info(sname, sp);
897 partition_info(sname, RAW_PART, pp);
899 return ("fixlabel: raw partition offset != slice offset");
901 if (pp->p_size != sp->ds_size) {
903 printf("%s: raw partition size != slice size\n", sname);
904 slice_info(sname, sp);
905 partition_info(sname, RAW_PART, pp);
907 if (pp->p_size > sp->ds_size) {
909 return ("fixlabel: raw partition size > slice size");
910 printf("%s: truncating raw partition\n", sname);
911 pp->p_size = sp->ds_size;
914 end = start + sp->ds_size;
916 return ("fixlabel: slice wraps");
917 if (lp->d_secpercyl <= 0)
918 return ("fixlabel: d_secpercyl <= 0");
921 for (part = 0; part < lp->d_npartitions; part++, pp++) {
922 if (pp->p_offset != 0 || pp->p_size != 0) {
923 if (pp->p_offset < start
924 || pp->p_offset + pp->p_size > end
925 || pp->p_offset + pp->p_size < pp->p_offset) {
928 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
931 slice_info(sname, sp);
934 partition_info(sname, part, pp);
936 /* XXX else silently discard junk. */
937 bzero(pp, sizeof *pp);
939 pp->p_offset += offset;
942 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
943 lp->d_secperunit = sp->ds_size;
945 lp->d_checksum = dkcksum(lp);
950 partition_info(sname, part, pp)
953 struct partition *pp;
955 printf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
956 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
961 slice_info(sname, sp)
963 struct diskslice *sp;
965 printf("%s: start %lu, end %lu, size %lu\n", sname,
966 sp->ds_offset, sp->ds_offset + sp->ds_size - 1, sp->ds_size);
970 set_ds_label(ssp, slice, lp)
971 struct diskslices *ssp;
973 struct disklabel *lp;
975 ssp->dss_slices[slice].ds_label = lp;
976 if (slice == COMPATIBILITY_SLICE)
977 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_label = lp;
978 else if (slice == ssp->dss_first_bsd_slice)
979 ssp->dss_slices[COMPATIBILITY_SLICE].ds_label = lp;
983 set_ds_labeldevs(dev, ssp)
985 struct diskslices *ssp;
991 set_ds_wlabel(ssp, slice, wlabel)
992 struct diskslices *ssp;
996 ssp->dss_slices[slice].ds_wlabel = wlabel;
997 if (slice == COMPATIBILITY_SLICE)
998 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_wlabel = wlabel;
999 else if (slice == ssp->dss_first_bsd_slice)
1000 ssp->dss_slices[COMPATIBILITY_SLICE].ds_wlabel = wlabel;