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.35 2007/05/19 00:52:01 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 disk_info *info,
77 struct diskslice *sp);
78 static void dsiodone (struct bio *bio);
79 static char *fixlabel (const char *sname, struct diskslice *sp,
80 struct disklabel *lp, int writeflag);
81 static int dsreadandsetlabel(cdev_t dev, u_int flags,
82 struct diskslices *ssp, struct diskslice *sp,
83 struct disk_info *info);
84 static void free_ds_label (struct diskslices *ssp, int slice);
85 static void partition_info (const char *sname, int part, struct partition *pp);
86 static void slice_info (const char *sname, struct diskslice *sp);
87 static void set_ds_label (struct diskslices *ssp, int slice,
88 struct disklabel *lp);
89 static void set_ds_wlabel (struct diskslices *ssp, int slice, int wlabel);
92 * Create a disklabel based on a disk_info structure, initializing
93 * the appropriate fields and creating a raw partition that covers the
96 * If a diskslice is passed, the label is truncated to the slice
98 static struct disklabel *
99 clone_label(struct disk_info *info, struct diskslice *sp)
101 struct disklabel *lp1;
103 lp1 = kmalloc(sizeof *lp1, M_DEVBUF, M_WAITOK | M_ZERO);
104 lp1->d_nsectors = info->d_secpertrack;
105 lp1->d_ntracks = info->d_nheads;
106 lp1->d_secpercyl = info->d_secpercyl;
107 lp1->d_secsize = info->d_media_blksize;
110 lp1->d_secperunit = (u_int)sp->ds_size;
111 lp1->d_partitions[RAW_PART].p_size = lp1->d_secperunit;
113 lp1->d_secperunit = (u_int)info->d_media_blocks;
114 lp1->d_partitions[RAW_PART].p_size = lp1->d_secperunit;
118 * Used by the CD driver to create a compatibility slice which
119 * allows us to mount root from the CD.
121 if (info->d_dsflags & DSO_COMPATPARTA) {
122 lp1->d_partitions[0].p_size = lp1->d_secperunit;
123 lp1->d_partitions[0].p_fstype = FS_OTHER;
126 if (lp1->d_typename[0] == '\0')
127 strncpy(lp1->d_typename, "amnesiac", sizeof(lp1->d_typename));
128 if (lp1->d_packname[0] == '\0')
129 strncpy(lp1->d_packname, "fictitious", sizeof(lp1->d_packname));
130 if (lp1->d_nsectors == 0)
131 lp1->d_nsectors = 32;
132 if (lp1->d_ntracks == 0)
134 lp1->d_secpercyl = lp1->d_nsectors * lp1->d_ntracks;
135 lp1->d_ncylinders = lp1->d_secperunit / lp1->d_secpercyl;
138 if (lp1->d_interleave == 0)
139 lp1->d_interleave = 1;
140 if (lp1->d_npartitions < RAW_PART + 1)
141 lp1->d_npartitions = MAXPARTITIONS;
142 if (lp1->d_bbsize == 0)
143 lp1->d_bbsize = BBSIZE;
144 if (lp1->d_sbsize == 0)
145 lp1->d_sbsize = SBSIZE;
146 lp1->d_partitions[RAW_PART].p_size = lp1->d_secperunit;
147 lp1->d_magic = DISKMAGIC;
148 lp1->d_magic2 = DISKMAGIC;
149 lp1->d_checksum = dkcksum(lp1);
154 * Determine the size of the transfer, and make sure it is
155 * within the boundaries of the partition. Adjust transfer
156 * if needed, and signal errors or early completion.
159 * o Split buffers that are too big for the device.
160 * o Check for overflow.
161 * o Finish cleaning this up.
163 * This function returns 1 on success, 0 if transfer equates
164 * to EOF (end of disk) or -1 on failure. The appropriate
165 * 'errno' value is also set in bp->b_error and bp->b_flags
166 * is marked with B_ERROR.
169 dscheck(cdev_t dev, struct bio *bio, struct diskslices *ssp)
171 struct buf *bp = bio->bio_buf;
173 struct disklabel *lp;
179 u_int64_t slicerel_secno;
180 struct diskslice *sp;
186 slice = dkslice(dev);
189 if (bio->bio_offset < 0) {
190 kprintf("dscheck(%s): negative bio_offset %lld\n",
191 devtoname(dev), bio->bio_offset);
194 if (slice >= ssp->dss_nslices) {
195 kprintf("dscheck(%s): slice too large %d/%d\n",
196 devtoname(dev), slice, ssp->dss_nslices);
199 sp = &ssp->dss_slices[slice];
202 * Calculate secno and nsec
204 if (ssp->dss_secmult == 1) {
207 } else if (ssp->dss_secshift != -1) {
208 shift = DEV_BSHIFT + ssp->dss_secshift;
210 mask = (1 << shift) - 1;
211 if ((int)bp->b_bcount & mask)
213 if ((int)bio->bio_offset & mask)
215 secno = bio->bio_offset >> shift;
216 nsec = bp->b_bcount >> shift;
218 if (bp->b_bcount % ssp->dss_secsize)
220 if (bio->bio_offset % ssp->dss_secsize)
222 secno = bio->bio_offset / ssp->dss_secsize;
223 nsec = bp->b_bcount / ssp->dss_secsize;
227 * Calculate slice-relative sector number end slice-relative
231 if (slice == WHOLE_DISK_SLICE || lp == NULL) {
233 * Labels have not been allowed on whole-disks for a while.
234 * This really puts the nail in the coffin... no disk
235 * snooping will occur even if you tried to write a label
236 * without a slice structure.
239 labelsect = -LABELSECTOR - 1;
240 endsecno = sp->ds_size;
241 slicerel_secno = secno;
242 } else if (part == WHOLE_SLICE_PART) {
244 * XXX messy snoop case. Both disklabel -r -w and
245 * DIOCWDINFO depend on write snooping to properly
246 * correct the partition offsets for the label being
247 * written to disk. In both cases ds_label will be
248 * non-NULL. This is a mess because it violates the
249 * idea of being able to write to the raw slice.
253 endsecno = sp->ds_size;
254 slicerel_secno = secno;
255 } else if (part >= lp->d_npartitions) {
256 kprintf("dscheck(%s): partition out of bounds %d/%d\n",
258 part, lp->d_npartitions);
261 struct partition *pp;
264 pp = &lp->d_partitions[dkpart(dev)];
265 endsecno = pp->p_size;
266 slicerel_secno = pp->p_offset + secno;
269 /* overwriting disk label ? */
270 /* XXX should also protect bootstrap in first 8K */
271 if (slicerel_secno <= LABELSECTOR + labelsect &&
273 slicerel_secno + nsec > LABELSECTOR + labelsect &&
275 bp->b_cmd != BUF_CMD_READ && sp->ds_wlabel == 0) {
280 #if defined(DOSBBSECTOR) && defined(notyet)
281 /* overwriting master boot record? */
282 if (slicerel_secno <= DOSBBSECTOR && bp->b_cmd != BUF_CMD_READ &&
283 sp->ds_wlabel == 0) {
292 if (secno + nsec > endsecno) {
294 * Return an error if beyond the end of the disk, or
295 * if B_BNOCLIP is set. Tell the system that we do not
296 * need to keep the buffer around.
298 if (secno > endsecno || (bp->b_flags & B_BNOCLIP))
302 * If exactly at end of disk, return an EOF. Throw away
303 * the buffer contents, if any, by setting B_INVAL.
305 if (secno == endsecno) {
306 bp->b_resid = bp->b_bcount;
307 bp->b_flags |= B_INVAL;
314 nsec = endsecno - secno;
315 bp->b_bcount = nsec * ssp->dss_secsize;
318 nbio = push_bio(bio);
319 nbio->bio_offset = (off_t)(sp->ds_offset + slicerel_secno) *
323 * Snoop on label accesses if the slice offset is nonzero. Fudge
324 * offsets in the label to keep the in-core label coherent with
327 if (slicerel_secno <= LABELSECTOR + labelsect
329 && slicerel_secno + nsec > LABELSECTOR + labelsect
331 && sp->ds_offset != 0) {
332 nbio->bio_done = dsiodone;
333 nbio->bio_caller_info1.ptr = sp;
334 nbio->bio_caller_info2.offset =
335 (off_t)(LABELSECTOR + labelsect - slicerel_secno) *
337 if (bp->b_cmd != BUF_CMD_READ) {
339 * XXX even disklabel(8) writes directly so we need
340 * to adjust writes. Perhaps we should drop support
341 * for DIOCWLABEL (always write protect labels) and
342 * require the use of DIOCWDINFO.
344 * XXX probably need to copy the data to avoid even
345 * temporarily corrupting the in-core copy.
347 /* XXX need name here. */
348 kprintf("SNOOP LABEL WRITE\n");
352 (bp->b_data + (int)nbio->bio_caller_info2.offset),
355 kprintf("dscheck(%s): %s\n",
356 devtoname(dev), msg);
367 "dscheck(%s): b_bcount %d is not on a sector boundary (ssize %d)\n",
368 devtoname(dev), bp->b_bcount, ssp->dss_secsize);
373 "dscheck(%s): bio_offset %lld is not on a sector boundary (ssize %d)\n",
374 devtoname(dev), bio->bio_offset, ssp->dss_secsize);
376 bp->b_error = EINVAL;
380 * Terminate the I/O with a ranging error. Since the buffer is
381 * either illegal or beyond the file EOF, mark it B_INVAL as well.
383 bp->b_resid = bp->b_bcount;
384 bp->b_flags |= B_ERROR | B_INVAL;
387 * Caller must biodone() the originally passed bio if NULL is
394 dsclose(cdev_t dev, int mode, struct diskslices *ssp)
398 struct diskslice *sp;
400 slice = dkslice(dev);
402 if (slice < ssp->dss_nslices) {
403 sp = &ssp->dss_slices[slice];
404 if (part < sizeof(sp->ds_openmask) * 8)
405 sp->ds_openmask &= ~(1 << part);
410 dsgone(struct diskslices **sspp)
413 struct diskslice *sp;
414 struct diskslices *ssp;
416 for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
417 sp = &ssp->dss_slices[slice];
418 free_ds_label(ssp, slice);
420 kfree(ssp, M_DEVBUF);
425 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
426 * is subject to the same restriction as dsopen().
429 dsioctl(cdev_t dev, u_long cmd, caddr_t data, int flags,
430 struct diskslices **sspp, struct disk_info *info)
433 struct disklabel *lp;
438 struct diskslice *sp;
439 struct diskslices *ssp;
440 struct partition *pp;
442 slice = dkslice(dev);
445 if (slice >= ssp->dss_nslices)
447 sp = &ssp->dss_slices[slice];
453 * You can only retrieve a virgin disklabel on the whole
454 * disk slice or whole-slice partition.
456 if (slice != WHOLE_DISK_SLICE &&
457 part != WHOLE_SLICE_PART) {
461 lp = (struct disklabel *)data;
462 if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
463 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
465 bzero(lp, sizeof(struct disklabel));
468 lp->d_magic = DISKMAGIC;
469 lp->d_magic2 = DISKMAGIC;
470 pp = &lp->d_partitions[RAW_PART];
472 pp->p_size = sp->ds_size;
474 lp->d_npartitions = MAXPARTITIONS;
475 if (lp->d_interleave == 0)
476 lp->d_interleave = 1;
479 if (lp->d_nsectors == 0)
481 if (lp->d_ntracks == 0)
484 lp->d_bbsize = BBSIZE;
485 lp->d_sbsize = SBSIZE;
486 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
487 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
488 lp->d_secperunit = sp->ds_size;
490 lp->d_checksum = dkcksum(lp);
495 * You can only retrieve a disklabel on the whole
498 * We do not support labels directly on whole-disks
499 * any more (that is, disks without slices), unless the
500 * device driver has asked for a compatible label (e.g.
501 * for a CD) to allow booting off of storage that is
502 * otherwise unlabeled.
505 if (part != WHOLE_SLICE_PART)
507 if (slice == WHOLE_DISK_SLICE &&
508 (info->d_dsflags & DSO_COMPATLABEL) == 0) {
511 if (sp->ds_label == NULL) {
512 error = dsreadandsetlabel(dev, info->d_dsflags,
516 *(struct disklabel *)data = *sp->ds_label;
521 struct partinfo *dpart = (void *)data;
523 bzero(dpart, sizeof(*dpart));
524 dpart->media_offset = (u_int64_t)sp->ds_offset *
525 info->d_media_blksize;
526 dpart->media_size = (u_int64_t)sp->ds_size *
527 info->d_media_blksize;
528 dpart->media_blocks = sp->ds_size;
529 dpart->media_blksize = info->d_media_blksize;
530 dpart->skip_platform = sp->ds_skip_platform;
531 dpart->skip_bsdlabel = sp->ds_skip_bsdlabel;
533 if (slice != WHOLE_DISK_SLICE &&
534 part != WHOLE_SLICE_PART) {
537 if (lp == NULL || part >= lp->d_npartitions)
540 p = &lp->d_partitions[part];
541 dpart->fstype = p->p_fstype;
542 dpart->media_offset += (u_int64_t)p->p_offset *
543 info->d_media_blksize;
544 dpart->media_size = (u_int64_t)p->p_size *
545 info->d_media_blksize;
546 dpart->media_blocks = (u_int64_t)p->p_size;
549 * partition starting sector (p_offset)
550 * requires slice's reserved areas to be
553 if (dpart->skip_platform > p->p_offset)
554 dpart->skip_platform -= p->p_offset;
556 dpart->skip_platform = 0;
557 if (dpart->skip_bsdlabel > p->p_offset)
558 dpart->skip_bsdlabel -= p->p_offset;
560 dpart->skip_bsdlabel = 0;
566 bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
572 * You can write a disklabel on the whole disk slice or
573 * whole-slice partition.
575 if (slice != WHOLE_DISK_SLICE &&
576 part != WHOLE_SLICE_PART) {
581 * We no longer support writing disklabels directly to media
582 * without there being a slice. Keep this as a separate
585 if (slice == WHOLE_DISK_SLICE)
588 if (!(flags & FWRITE))
590 lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK);
591 if (sp->ds_label == NULL)
592 bzero(lp, sizeof *lp);
594 bcopy(sp->ds_label, lp, sizeof *lp);
595 if (sp->ds_label == NULL) {
598 openmask = sp->ds_openmask;
599 if (slice == COMPATIBILITY_SLICE) {
600 openmask |= ssp->dss_slices[
601 ssp->dss_first_bsd_slice].ds_openmask;
602 } else if (slice == ssp->dss_first_bsd_slice) {
603 openmask |= ssp->dss_slices[
604 COMPATIBILITY_SLICE].ds_openmask;
607 error = setdisklabel(lp, (struct disklabel *)data,
609 /* XXX why doesn't setdisklabel() check this? */
610 if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
613 if (lp->d_secperunit > sp->ds_size)
615 for (part = 0; part < lp->d_npartitions; part++)
616 if (lp->d_partitions[part].p_size > sp->ds_size)
623 free_ds_label(ssp, slice);
624 set_ds_label(ssp, slice, lp);
627 case DIOCSYNCSLICEINFO:
629 * This ioctl can only be done on the whole disk
631 if (slice != WHOLE_DISK_SLICE || part != WHOLE_SLICE_PART)
634 if (*(int *)data == 0) {
635 for (slice = 0; slice < ssp->dss_nslices; slice++) {
636 openmask = ssp->dss_slices[slice].ds_openmask;
638 (slice != WHOLE_DISK_SLICE ||
639 openmask & ~(1 << RAW_PART))) {
646 * Temporarily forget the current slices struct and read
651 * XXX should wait for current accesses on this disk to
652 * complete, then lock out future accesses and opens.
655 lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK);
656 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
657 error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, info);
665 * Reopen everything. This is a no-op except in the "force"
666 * case and when the raw bdev and cdev are both open. Abort
669 for (slice = 0; slice < ssp->dss_nslices; slice++) {
670 for (openmask = ssp->dss_slices[slice].ds_openmask,
671 part = 0; openmask; openmask >>= 1, part++) {
674 error = dsopen(dkmodslice(dkmodpart(dev, part),
676 S_IFCHR, ssp->dss_oflags, sspp,
691 error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp, info);
695 * XXX this used to hack on dk_openpart to fake opening
696 * partition 0 in case that is used instead of dkpart(dev).
698 old_wlabel = sp->ds_wlabel;
699 set_ds_wlabel(ssp, slice, TRUE);
700 error = writedisklabel(dev, sp->ds_label);
701 /* XXX should invalidate in-core label if write failed. */
702 set_ds_wlabel(ssp, slice, old_wlabel);
706 if (slice == WHOLE_DISK_SLICE)
708 if (!(flags & FWRITE))
710 set_ds_wlabel(ssp, slice, *(int *)data != 0);
719 * Chain the bio_done. b_cmd remains valid through such chaining.
722 dsiodone(struct bio *bio)
724 struct buf *bp = bio->bio_buf;
727 if (bp->b_cmd != BUF_CMD_READ
728 || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
729 msg = fixlabel(NULL, bio->bio_caller_info1.ptr,
731 (bp->b_data + (int)bio->bio_caller_info2.offset),
734 kprintf("%s\n", msg);
736 biodone(bio->bio_prev);
740 dsisopen(struct diskslices *ssp)
746 for (slice = 0; slice < ssp->dss_nslices; slice++) {
747 if (ssp->dss_slices[slice].ds_openmask)
754 * Allocate a slices "struct" and initialize it to contain only an empty
755 * compatibility slice (pointing to itself), a whole disk slice (covering
756 * the disk as described by the label), and (nslices - BASE_SLICES) empty
757 * slices beginning at BASE_SLICE.
760 dsmakeslicestruct(int nslices, struct disk_info *info)
762 struct diskslice *sp;
763 struct diskslices *ssp;
765 ssp = kmalloc(offsetof(struct diskslices, dss_slices) +
766 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
767 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
768 ssp->dss_nslices = nslices;
770 ssp->dss_secmult = info->d_media_blksize / DEV_BSIZE;
771 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
772 ssp->dss_secshift = -1;
774 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
775 ssp->dss_secsize = info->d_media_blksize;
776 sp = &ssp->dss_slices[0];
777 bzero(sp, nslices * sizeof *sp);
778 sp[WHOLE_DISK_SLICE].ds_size = info->d_media_blocks;
783 dsname(cdev_t dev, int unit, int slice, int part, char *partname)
785 static char name[32];
789 dname = dev_dname(dev);
790 if (strlen(dname) > 16)
791 dname = "nametoolong";
792 ksnprintf(name, sizeof(name), "%s%d", dname, unit);
796 if (slice != WHOLE_DISK_SLICE) {
797 if (slice != COMPATIBILITY_SLICE) {
798 used += ksnprintf(name + used, sizeof(name) - used,
799 "s%d", slice - BASE_SLICE + 1);
801 if (part != WHOLE_SLICE_PART) {
802 used += ksnprintf(name + used, sizeof(name) - used,
804 partname[0] = 'a' + part;
812 * This should only be called when the unit is inactive and the strategy
813 * routine should not allow it to become active unless we call it. Our
814 * strategy routine must be special to allow activity.
817 dsopen(cdev_t dev, int mode, u_int flags,
818 struct diskslices **sspp, struct disk_info *info)
824 struct diskslice *sp;
825 struct diskslices *ssp;
829 dev->si_bsize_phys = info->d_media_blksize;
831 if (info->d_media_blksize % DEV_BSIZE) {
832 kprintf("%s: invalid sector size %lu\n", devtoname(dev),
833 (u_long)info->d_media_blksize);
838 * Do not attempt to read the slice table or disk label when
839 * accessing the raw disk.
841 if (dkslice(dev) == WHOLE_DISK_SLICE)
842 flags |= DSO_ONESLICE | DSO_NOLABELS;
843 if (dkpart(dev) == WHOLE_SLICE_PART)
844 flags |= DSO_NOLABELS;
847 * XXX reinitialize the slice table unless there is an open device
848 * on the unit. This should only be done if the media has changed.
851 need_init = !dsisopen(ssp);
852 if (ssp != NULL && need_init)
856 * Allocate a minimal slices "struct". This will become
857 * the final slices "struct" if we don't want real slices
858 * or if we can't find any real slices.
862 *sspp = dsmakeslicestruct(BASE_SLICE, info);
864 if ((flags & DSO_ONESLICE) == 0) {
865 TRACE(("mbrinit\n"));
866 error = mbrinit(dev, info, sspp);
873 ssp->dss_oflags = flags;
876 * If there are no real slices, then make the compatiblity
877 * slice cover the whole disk.
879 * no sectors are reserved for the platform (ds_skip_platform
880 * will be 0) in this case. This means that if a disklabel
881 * is installed it will be directly installed in sector 0
882 * unless DSO_COMPATMBR is requested.
884 if (ssp->dss_nslices == BASE_SLICE) {
885 sp = &ssp->dss_slices[COMPATIBILITY_SLICE];
887 sp->ds_size = info->d_media_blocks;
888 if (info->d_dsflags & DSO_COMPATMBR)
889 sp->ds_skip_platform = 1;
893 * Point the compatibility slice at the BSD slice, if any.
895 for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
896 sp = &ssp->dss_slices[slice];
897 if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
898 struct diskslice *csp;
900 csp = &ssp->dss_slices[COMPATIBILITY_SLICE];
901 ssp->dss_first_bsd_slice = slice;
902 csp->ds_offset = sp->ds_offset;
903 csp->ds_size = sp->ds_size;
904 csp->ds_type = sp->ds_type;
905 csp->ds_skip_platform = sp->ds_skip_platform;
906 csp->ds_skip_bsdlabel = sp->ds_skip_bsdlabel;
912 * By definition accesses via the whole-disk device do not
913 * specify any reserved areas. The whole disk may be read
914 * or written by the whole-disk device.
916 * ds_label for a whole-disk device is only used as a
919 sp = &ssp->dss_slices[WHOLE_DISK_SLICE];
920 sp->ds_label = clone_label(info, NULL);
921 sp->ds_wlabel = TRUE;
922 sp->ds_skip_platform = 0;
923 sp->ds_skip_bsdlabel = 0;
927 * Initialize secondary info for all slices. It is needed for more
928 * than the current slice in the DEVFS case. XXX DEVFS is no more.
930 * Attempt to read the disklabel for each slice, creating a virgin
931 * label if a slice does not have one.
933 for (slice = 0; slice < ssp->dss_nslices; slice++) {
934 sp = &ssp->dss_slices[slice];
935 if (sp->ds_label != NULL)
938 dev1 = dkmodslice(dkmodpart(dev, WHOLE_SLICE_PART), slice);
941 * If opening a raw disk or raw slice we do not try to
942 * read the disklabel, and we allow access to the whole
945 set_ds_wlabel(ssp, slice, TRUE);
946 if ((flags & DSO_NOLABELS) == 0)
947 dsreadandsetlabel(dev1, flags, ssp, sp, info);
950 slice = dkslice(dev);
951 if (slice >= ssp->dss_nslices)
953 sp = &ssp->dss_slices[slice];
957 * If opening a particular partition the disklabel must exist and
958 * the partition must be present in the label.
960 * If the partition is the special whole-disk-slice no partition
963 if (part != WHOLE_SLICE_PART && slice != WHOLE_DISK_SLICE) {
964 if (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions)
966 if (part < sizeof(sp->ds_openmask) * 8) {
968 sp->ds_openmask |= mask;
973 * If the slice is the whole-disk slice, the partition must be set
974 * to the whole slice partition.
976 if (slice == WHOLE_DISK_SLICE && part != WHOLE_SLICE_PART)
982 * Attempt to read the disklabel. If successful, store it in sp->ds_label.
984 * If we cannot read the disklabel and DSO_COMPATLABEL is set, we construct
985 * a fake label covering the whole disk.
989 dsreadandsetlabel(cdev_t dev, u_int flags,
990 struct diskslices *ssp, struct diskslice *sp,
991 struct disk_info *info)
993 struct disklabel *lp1;
997 int slice = dkslice(dev);
999 sname = dsname(dev, dkunit(dev), slice, WHOLE_SLICE_PART, partname);
1000 lp1 = clone_label(info, sp);
1001 msg = readdisklabel(dev, lp1);
1003 if (msg != NULL && (flags & DSO_COMPATLABEL)) {
1005 kfree(lp1, M_DEVBUF);
1006 lp1 = clone_label(info, sp);
1009 msg = fixlabel(sname, sp, lp1, FALSE);
1010 if (msg == NULL && lp1->d_secsize != info->d_media_blksize)
1011 msg = "inconsistent sector size";
1013 if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
1014 log(LOG_WARNING, "%s: cannot find label (%s)\n",
1016 kfree(lp1, M_DEVBUF);
1018 set_ds_label(ssp, slice, lp1);
1019 set_ds_wlabel(ssp, slice, FALSE);
1021 return (msg ? EINVAL : 0);
1025 dssize(cdev_t dev, struct diskslices **sspp)
1027 struct disklabel *lp;
1030 struct diskslices *ssp;
1032 slice = dkslice(dev);
1035 if (ssp == NULL || slice >= ssp->dss_nslices
1036 || !(ssp->dss_slices[slice].ds_openmask & (1 << part))) {
1037 if (dev_dopen(dev, FREAD, S_IFCHR, proc0.p_ucred) != 0)
1039 dev_dclose(dev, FREAD, S_IFCHR);
1042 lp = ssp->dss_slices[slice].ds_label;
1045 return ((int64_t)lp->d_partitions[part].p_size);
1049 free_ds_label(struct diskslices *ssp, int slice)
1051 struct disklabel *lp;
1052 struct diskslice *sp;
1054 sp = &ssp->dss_slices[slice];
1058 kfree(lp, M_DEVBUF);
1059 set_ds_label(ssp, slice, (struct disklabel *)NULL);
1063 fixlabel(const char *sname, struct diskslice *sp, struct disklabel *lp, int writeflag)
1069 struct partition *pp;
1072 /* These errors "can't happen" so don't bother reporting details. */
1073 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC)
1074 return ("fixlabel: invalid magic");
1075 if (dkcksum(lp) != 0)
1076 return ("fixlabel: invalid checksum");
1078 pp = &lp->d_partitions[RAW_PART];
1081 * What a mess. For ages old backwards compatibility the disklabel
1082 * on-disk stores absolute offsets instead of slice-relative offsets.
1083 * So fix it up when reading, writing, or snooping.
1085 * The in-core label is always slice-relative.
1089 offset = sp->ds_offset;
1091 start = sp->ds_offset;
1092 offset = -sp->ds_offset;
1094 if (pp->p_offset != start) {
1095 if (sname != NULL) {
1097 "%s: rejecting BSD label: raw partition offset != slice offset\n",
1099 slice_info(sname, sp);
1100 partition_info(sname, RAW_PART, pp);
1102 return ("fixlabel: raw partition offset != slice offset");
1104 if (pp->p_size != sp->ds_size) {
1105 if (sname != NULL) {
1106 kprintf("%s: raw partition size != slice size\n", sname);
1107 slice_info(sname, sp);
1108 partition_info(sname, RAW_PART, pp);
1110 if (pp->p_size > sp->ds_size) {
1112 return ("fixlabel: raw partition size > slice size");
1113 kprintf("%s: truncating raw partition\n", sname);
1114 pp->p_size = sp->ds_size;
1117 end = start + sp->ds_size;
1119 return ("fixlabel: slice wraps");
1120 if (lp->d_secpercyl <= 0)
1121 return ("fixlabel: d_secpercyl <= 0");
1124 for (part = 0; part < lp->d_npartitions; part++, pp++) {
1125 if (pp->p_offset != 0 || pp->p_size != 0) {
1126 if (pp->p_offset < start
1127 || pp->p_offset + pp->p_size > end
1128 || pp->p_offset + pp->p_size < pp->p_offset) {
1129 if (sname != NULL) {
1131 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
1134 slice_info(sname, sp);
1137 partition_info(sname, part, pp);
1139 /* XXX else silently discard junk. */
1140 bzero(pp, sizeof *pp);
1142 pp->p_offset += offset;
1146 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
1147 lp->d_secperunit = sp->ds_size;
1149 lp->d_checksum = dkcksum(lp);
1154 partition_info(const char *sname, int part, struct partition *pp)
1156 kprintf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
1157 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
1158 (u_long)pp->p_size);
1162 slice_info(const char *sname, struct diskslice *sp)
1164 kprintf("%s: start %llu, end %llu, size %llu\n", sname,
1165 sp->ds_offset, sp->ds_offset + sp->ds_size - 1, sp->ds_size);
1169 set_ds_label(struct diskslices *ssp, int slice, struct disklabel *lp)
1171 struct diskslice *sp1 = &ssp->dss_slices[slice];
1172 struct diskslice *sp2;
1174 if (slice == COMPATIBILITY_SLICE)
1175 sp2 = &ssp->dss_slices[ssp->dss_first_bsd_slice];
1176 else if (slice == ssp->dss_first_bsd_slice)
1177 sp2 = &ssp->dss_slices[COMPATIBILITY_SLICE];
1185 * If the slice is not the whole-disk slice, setup the reserved
1188 * The reserved area for the original bsd disklabel, inclusive of
1189 * the label and space for boot2, is 15 sectors. If you've
1190 * noticed people traditionally skipping 16 sectors its because
1191 * the sector numbers start at the beginning of the slice rather
1192 * then the beginning of the disklabel and traditional dos slices
1193 * reserve a sector at the beginning for the boot code.
1195 * NOTE! With the traditional bsdlabel, the first N bytes of boot2
1196 * overlap with the disklabel. The disklabel program checks that
1199 if (slice != WHOLE_DISK_SLICE) {
1200 sp1->ds_skip_bsdlabel = sp1->ds_skip_platform + 15;
1202 sp2->ds_skip_bsdlabel = sp1->ds_skip_bsdlabel;
1207 set_ds_wlabel(struct diskslices *ssp, int slice, int wlabel)
1209 ssp->dss_slices[slice].ds_wlabel = wlabel;
1210 if (slice == COMPATIBILITY_SLICE)
1211 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_wlabel = wlabel;
1212 else if (slice == ssp->dss_first_bsd_slice)
1213 ssp->dss_slices[COMPATIBILITY_SLICE].ds_wlabel = wlabel;