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.28 2007/05/15 00:01:04 dillon Exp $
50 #include <sys/param.h>
51 #include <sys/systm.h>
54 #include <sys/disklabel.h>
55 #include <sys/diskslice.h>
57 #include <sys/diskmbr.h>
58 #include <sys/fcntl.h>
59 #include <sys/malloc.h>
61 #include <sys/syslog.h>
63 #include <sys/vnode.h>
64 #include <sys/device.h>
65 #include <sys/thread2.h>
67 #include <vfs/ufs/dinode.h> /* XXX used only for fs.h */
68 #include <vfs/ufs/fs.h> /* XXX used only to get BBSIZE/SBSIZE */
70 #define TRACE(str) do { if (ds_debug) kprintf str; } while (0)
72 typedef u_char bool_t;
74 static volatile bool_t ds_debug;
76 static struct disklabel *clone_label (struct disklabel *lp);
77 static void dsiodone (struct bio *bio);
78 static char *fixlabel (char *sname, struct diskslice *sp,
79 struct disklabel *lp, int writeflag);
80 static void free_ds_label (struct diskslices *ssp, int slice);
81 static void partition_info (char *sname, int part, struct partition *pp);
82 static void slice_info (char *sname, struct diskslice *sp);
83 static void set_ds_label (struct diskslices *ssp, int slice,
84 struct disklabel *lp);
85 static void set_ds_wlabel (struct diskslices *ssp, int slice, int wlabel);
88 * Duplicate a label for the whole disk, and initialize defaults in the
89 * copy for fields that are not already initialized. The caller only
90 * needs to initialize d_secsize and d_secperunit, and zero the fields
91 * that are to be defaulted.
93 static struct disklabel *
94 clone_label(struct disklabel *lp)
96 struct disklabel *lp1;
98 lp1 = kmalloc(sizeof *lp1, M_DEVBUF, M_WAITOK);
101 if (lp1->d_typename[0] == '\0')
102 strncpy(lp1->d_typename, "amnesiac", sizeof(lp1->d_typename));
103 if (lp1->d_packname[0] == '\0')
104 strncpy(lp1->d_packname, "fictitious", sizeof(lp1->d_packname));
105 if (lp1->d_nsectors == 0)
106 lp1->d_nsectors = 32;
107 if (lp1->d_ntracks == 0)
109 lp1->d_secpercyl = lp1->d_nsectors * lp1->d_ntracks;
110 lp1->d_ncylinders = lp1->d_secperunit / lp1->d_secpercyl;
113 if (lp1->d_interleave == 0)
114 lp1->d_interleave = 1;
115 if (lp1->d_npartitions < RAW_PART + 1)
116 lp1->d_npartitions = MAXPARTITIONS;
117 if (lp1->d_bbsize == 0)
118 lp1->d_bbsize = BBSIZE;
119 if (lp1->d_sbsize == 0)
120 lp1->d_sbsize = SBSIZE;
121 lp1->d_partitions[RAW_PART].p_size = lp1->d_secperunit;
122 lp1->d_magic = DISKMAGIC;
123 lp1->d_magic2 = DISKMAGIC;
124 lp1->d_checksum = dkcksum(lp1);
129 * Determine the size of the transfer, and make sure it is
130 * within the boundaries of the partition. Adjust transfer
131 * if needed, and signal errors or early completion.
134 * o Split buffers that are too big for the device.
135 * o Check for overflow.
136 * o Finish cleaning this up.
138 * This function returns 1 on success, 0 if transfer equates
139 * to EOF (end of disk) or -1 on failure. The appropriate
140 * 'errno' value is also set in bp->b_error and bp->b_flags
141 * is marked with B_ERROR.
144 dscheck(cdev_t dev, struct bio *bio, struct diskslices *ssp)
146 struct buf *bp = bio->bio_buf;
150 struct disklabel *lp;
153 struct partition *pp;
155 daddr_t slicerel_secno;
156 struct diskslice *sp;
160 if (bio->bio_offset < 0) {
161 kprintf("dscheck(%s): negative bio_offset %lld\n",
162 devtoname(dev), bio->bio_offset);
165 sp = &ssp->dss_slices[dkslice(dev)];
168 if (ssp->dss_secmult == 1) {
171 } else if (ssp->dss_secshift != -1) {
172 shift = DEV_BSHIFT + ssp->dss_secshift;
174 mask = (1 << shift) - 1;
175 if ((int)bp->b_bcount & mask)
177 if ((int)bio->bio_offset & mask)
179 secno = (daddr_t)(bio->bio_offset >> shift);
180 nsec = bp->b_bcount >> shift;
182 if (bp->b_bcount % ssp->dss_secsize)
184 if (bio->bio_offset % ssp->dss_secsize)
186 secno = (daddr_t)(bio->bio_offset / ssp->dss_secsize);
187 nsec = bp->b_bcount / ssp->dss_secsize;
190 labelsect = -LABELSECTOR - 1;
191 endsecno = sp->ds_size;
192 slicerel_secno = secno;
194 labelsect = lp->d_partitions[LABEL_PART].p_offset;
196 Debugger("labelsect != 0 in dscheck()");
197 pp = &lp->d_partitions[dkpart(dev)];
198 endsecno = pp->p_size;
199 slicerel_secno = pp->p_offset + secno;
202 /* overwriting disk label ? */
203 /* XXX should also protect bootstrap in first 8K */
204 if (slicerel_secno <= LABELSECTOR + labelsect &&
206 slicerel_secno + nsec > LABELSECTOR + labelsect &&
208 bp->b_cmd != BUF_CMD_READ && sp->ds_wlabel == 0) {
213 #if defined(DOSBBSECTOR) && defined(notyet)
214 /* overwriting master boot record? */
215 if (slicerel_secno <= DOSBBSECTOR && bp->b_cmd != BUF_CMD_READ &&
216 sp->ds_wlabel == 0) {
225 if (secno + nsec > endsecno) {
227 * Return an error if beyond the end of the disk, or
228 * if B_BNOCLIP is set. Tell the system that we do not
229 * need to keep the buffer around.
231 if (secno > endsecno || (bp->b_flags & B_BNOCLIP))
235 * If exactly at end of disk, return an EOF. Throw away
236 * the buffer contents, if any, by setting B_INVAL.
238 if (secno == endsecno) {
239 bp->b_resid = bp->b_bcount;
240 bp->b_flags |= B_INVAL;
247 nsec = endsecno - secno;
248 bp->b_bcount = nsec * ssp->dss_secsize;
251 nbio = push_bio(bio);
252 nbio->bio_offset = (off_t)(sp->ds_offset + slicerel_secno) *
256 * Snoop on label accesses if the slice offset is nonzero. Fudge
257 * offsets in the label to keep the in-core label coherent with
260 if (slicerel_secno <= LABELSECTOR + labelsect
262 && slicerel_secno + nsec > LABELSECTOR + labelsect
264 && sp->ds_offset != 0) {
265 nbio->bio_done = dsiodone;
266 nbio->bio_caller_info1.ptr = sp;
267 nbio->bio_caller_info2.offset = (off_t)(LABELSECTOR + labelsect -
268 slicerel_secno) * ssp->dss_secsize;
269 if (bp->b_cmd != BUF_CMD_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. */
283 (bp->b_data + (int)nbio->bio_caller_info2.offset),
286 kprintf("dscheck(%s): %s\n",
287 devtoname(dev), msg);
298 "dscheck(%s): b_bcount %d is not on a sector boundary (ssize %d)\n",
299 devtoname(dev), bp->b_bcount, ssp->dss_secsize);
304 "dscheck(%s): bio_offset %lld is not on a sector boundary (ssize %d)\n",
305 devtoname(dev), bio->bio_offset, ssp->dss_secsize);
307 bp->b_error = EINVAL;
311 * Terminate the I/O with a ranging error. Since the buffer is
312 * either illegal or beyond the file EOF, mark it B_INVAL as well.
314 bp->b_resid = bp->b_bcount;
315 bp->b_flags |= B_ERROR | B_INVAL;
318 * Caller must biodone() the originally passed bio if NULL is
325 dsclose(cdev_t dev, int mode, struct diskslices *ssp)
328 struct diskslice *sp;
330 sp = &ssp->dss_slices[dkslice(dev)];
331 mask = 1 << dkpart(dev);
332 sp->ds_openmask &= ~mask;
336 dsgone(struct diskslices **sspp)
339 struct diskslice *sp;
340 struct diskslices *ssp;
342 for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
343 sp = &ssp->dss_slices[slice];
344 free_ds_label(ssp, slice);
346 kfree(ssp, M_DEVBUF);
351 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
352 * is subject to the same restriction as dsopen().
355 dsioctl(cdev_t dev, u_long cmd, caddr_t data,
356 int flags, struct diskslices **sspp)
359 struct disklabel *lp;
364 struct diskslice *sp;
365 struct diskslices *ssp;
366 struct partition *pp;
368 slice = dkslice(dev);
370 sp = &ssp->dss_slices[slice];
375 lp = (struct disklabel *)data;
376 if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
377 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
379 bzero(lp, sizeof(struct disklabel));
382 lp->d_magic = DISKMAGIC;
383 lp->d_magic2 = DISKMAGIC;
384 pp = &lp->d_partitions[RAW_PART];
386 pp->p_size = sp->ds_size;
388 lp->d_npartitions = MAXPARTITIONS;
389 if (lp->d_interleave == 0)
390 lp->d_interleave = 1;
393 if (lp->d_nsectors == 0)
395 if (lp->d_ntracks == 0)
398 lp->d_bbsize = BBSIZE;
399 lp->d_sbsize = SBSIZE;
400 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
401 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
402 lp->d_secperunit = sp->ds_size;
404 lp->d_checksum = dkcksum(lp);
410 *(struct disklabel *)data = *lp;
417 *(struct disklabel **)data = lp;
424 ((struct partinfo *)data)->disklab = lp;
425 ((struct partinfo *)data)->part
426 = &lp->d_partitions[dkpart(dev)];
430 bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
435 if (slice == WHOLE_DISK_SLICE)
437 if (!(flags & FWRITE))
439 lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK);
440 if (sp->ds_label == NULL)
441 bzero(lp, sizeof *lp);
443 bcopy(sp->ds_label, lp, sizeof *lp);
444 if (sp->ds_label == NULL)
447 openmask = sp->ds_openmask;
448 if (slice == COMPATIBILITY_SLICE)
449 openmask |= ssp->dss_slices[
450 ssp->dss_first_bsd_slice].ds_openmask;
451 else if (slice == ssp->dss_first_bsd_slice)
452 openmask |= ssp->dss_slices[
453 COMPATIBILITY_SLICE].ds_openmask;
455 error = setdisklabel(lp, (struct disklabel *)data,
457 /* XXX why doesn't setdisklabel() check this? */
458 if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
461 if (lp->d_secperunit > sp->ds_size)
463 for (part = 0; part < lp->d_npartitions; part++)
464 if (lp->d_partitions[part].p_size > sp->ds_size)
471 free_ds_label(ssp, slice);
472 set_ds_label(ssp, slice, lp);
475 case DIOCSYNCSLICEINFO:
476 if (slice != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART)
479 for (slice = 0; slice < ssp->dss_nslices; slice++) {
480 openmask = ssp->dss_slices[slice].ds_openmask;
482 && (slice != WHOLE_DISK_SLICE
483 || openmask & ~(1 << RAW_PART)))
488 * Temporarily forget the current slices struct and read
490 * XXX should wait for current accesses on this disk to
491 * complete, then lock out future accesses and opens.
494 lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK);
495 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
496 error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, lp);
504 * Reopen everything. This is a no-op except in the "force"
505 * case and when the raw bdev and cdev are both open. Abort
508 for (slice = 0; slice < ssp->dss_nslices; slice++) {
509 for (openmask = ssp->dss_slices[slice].ds_openmask,
510 part = 0; openmask; openmask >>= 1, part++) {
513 error = dsopen(dkmodslice(dkmodpart(dev, part),
515 S_IFCHR, ssp->dss_oflags, sspp,
530 error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp);
534 * XXX this used to hack on dk_openpart to fake opening
535 * partition 0 in case that is used instead of dkpart(dev).
537 old_wlabel = sp->ds_wlabel;
538 set_ds_wlabel(ssp, slice, TRUE);
539 error = writedisklabel(dev, sp->ds_label);
540 /* XXX should invalidate in-core label if write failed. */
541 set_ds_wlabel(ssp, slice, old_wlabel);
545 if (slice == WHOLE_DISK_SLICE)
547 if (!(flags & FWRITE))
549 set_ds_wlabel(ssp, slice, *(int *)data != 0);
558 * Chain the bio_done. b_cmd remains valid through such chaining.
561 dsiodone(struct bio *bio)
563 struct buf *bp = bio->bio_buf;
566 if (bp->b_cmd != BUF_CMD_READ
567 || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
568 msg = fixlabel(NULL, bio->bio_caller_info1.ptr,
570 (bp->b_data + (int)bio->bio_caller_info2.offset),
573 kprintf("%s\n", msg);
575 biodone(bio->bio_prev);
579 dsisopen(struct diskslices *ssp)
585 for (slice = 0; slice < ssp->dss_nslices; slice++) {
586 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(int nslices, struct disklabel *lp)
601 struct diskslice *sp;
602 struct diskslices *ssp;
604 ssp = kmalloc(offsetof(struct diskslices, dss_slices) +
605 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
606 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
607 ssp->dss_nslices = nslices;
609 ssp->dss_secmult = lp->d_secsize / DEV_BSIZE;
610 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
611 ssp->dss_secshift = -1;
613 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
614 ssp->dss_secsize = lp->d_secsize;
615 sp = &ssp->dss_slices[0];
616 bzero(sp, nslices * sizeof *sp);
617 sp[WHOLE_DISK_SLICE].ds_size = lp->d_secperunit;
622 dsname(cdev_t dev, int unit, int slice, int part, char *partname)
624 static char name[32];
627 dname = dev_dname(dev);
628 if (strlen(dname) > 16)
629 dname = "nametoolong";
630 ksnprintf(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 ksnprintf(name + strlen(name),
637 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(cdev_t dev, int mode, u_int flags,
650 struct diskslices **sspp, struct disklabel *lp)
654 struct disklabel *lp1;
662 struct diskslice *sp;
663 struct diskslices *ssp;
666 dev->si_bsize_phys = lp->d_secsize;
669 if (lp->d_secsize % DEV_BSIZE) {
670 kprintf("%s: invalid sector size %lu\n", devtoname(dev),
671 (u_long)lp->d_secsize);
676 * Do not attempt to read the slice table or disk label when
677 * accessing the raw disk.
679 if (dkslice(dev) == WHOLE_DISK_SLICE && dkpart(dev) == RAW_PART) {
680 flags |= DSO_ONESLICE | DSO_NOLABELS;
684 * XXX reinitialize the slice table unless there is an open device
685 * on the unit. This should only be done if the media has changed.
688 need_init = !dsisopen(ssp);
689 if (ssp != NULL && need_init)
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;
738 * Initialize secondary info for all slices. It is needed for more
739 * than the current slice in the DEVFS case. XXX DEVFS is no more.
741 for (slice = 0; slice < ssp->dss_nslices; slice++) {
742 sp = &ssp->dss_slices[slice];
743 if (sp->ds_label != NULL)
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)) {
774 kfree(lp1, M_DEVBUF);
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",
786 kfree(lp1, M_DEVBUF);
789 set_ds_label(ssp, slice, lp1);
790 set_ds_wlabel(ssp, slice, FALSE);
793 slice = dkslice(dev);
794 if (slice >= ssp->dss_nslices)
796 sp = &ssp->dss_slices[slice];
799 && (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions))
800 return (EINVAL); /* XXX needs translation */
802 sp->ds_openmask |= mask;
807 dssize(cdev_t dev, struct diskslices **sspp)
809 struct disklabel *lp;
812 struct diskslices *ssp;
814 slice = dkslice(dev);
817 if (ssp == NULL || slice >= ssp->dss_nslices
818 || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
819 if (dev_dopen(dev, FREAD, S_IFCHR, proc0.p_ucred) != 0)
821 dev_dclose(dev, FREAD, S_IFCHR);
824 lp = ssp->dss_slices[slice].ds_label;
827 return ((int)lp->d_partitions[part].p_size);
831 free_ds_label(struct diskslices *ssp, int slice)
833 struct disklabel *lp;
834 struct diskslice *sp;
836 sp = &ssp->dss_slices[slice];
841 set_ds_label(ssp, slice, (struct disklabel *)NULL);
845 fixlabel(char *sname, struct diskslice *sp, struct disklabel *lp, int writeflag)
850 struct partition *pp;
854 /* These errors "can't happen" so don't bother reporting details. */
855 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC)
856 return ("fixlabel: invalid magic");
857 if (dkcksum(lp) != 0)
858 return ("fixlabel: invalid checksum");
860 pp = &lp->d_partitions[RAW_PART];
863 offset = sp->ds_offset;
865 start = sp->ds_offset;
866 offset = -sp->ds_offset;
868 if (pp->p_offset != start) {
871 "%s: rejecting BSD label: raw partition offset != slice offset\n",
873 slice_info(sname, sp);
874 partition_info(sname, RAW_PART, pp);
876 return ("fixlabel: raw partition offset != slice offset");
878 if (pp->p_size != sp->ds_size) {
880 kprintf("%s: raw partition size != slice size\n", sname);
881 slice_info(sname, sp);
882 partition_info(sname, RAW_PART, pp);
884 if (pp->p_size > sp->ds_size) {
886 return ("fixlabel: raw partition size > slice size");
887 kprintf("%s: truncating raw partition\n", sname);
888 pp->p_size = sp->ds_size;
891 end = start + sp->ds_size;
893 return ("fixlabel: slice wraps");
894 if (lp->d_secpercyl <= 0)
895 return ("fixlabel: d_secpercyl <= 0");
898 for (part = 0; part < lp->d_npartitions; part++, pp++) {
899 if (pp->p_offset != 0 || pp->p_size != 0) {
900 if (pp->p_offset < start
901 || pp->p_offset + pp->p_size > end
902 || pp->p_offset + pp->p_size < pp->p_offset) {
905 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
908 slice_info(sname, sp);
911 partition_info(sname, part, pp);
913 /* XXX else silently discard junk. */
914 bzero(pp, sizeof *pp);
916 pp->p_offset += offset;
919 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
920 lp->d_secperunit = sp->ds_size;
922 lp->d_checksum = dkcksum(lp);
927 partition_info(char *sname, int part, struct partition *pp)
929 kprintf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
930 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
935 slice_info(char *sname, struct diskslice *sp)
937 kprintf("%s: start %lu, end %lu, size %lu\n", sname,
938 sp->ds_offset, sp->ds_offset + sp->ds_size - 1, sp->ds_size);
942 set_ds_label(struct diskslices *ssp, int slice, struct disklabel *lp)
944 ssp->dss_slices[slice].ds_label = lp;
945 if (slice == COMPATIBILITY_SLICE)
946 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_label = lp;
947 else if (slice == ssp->dss_first_bsd_slice)
948 ssp->dss_slices[COMPATIBILITY_SLICE].ds_label = lp;
952 set_ds_wlabel(struct diskslices *ssp, int slice, int wlabel)
954 ssp->dss_slices[slice].ds_wlabel = wlabel;
955 if (slice == COMPATIBILITY_SLICE)
956 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_wlabel = wlabel;
957 else if (slice == ssp->dss_first_bsd_slice)
958 ssp->dss_slices[COMPATIBILITY_SLICE].ds_wlabel = wlabel;