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.44 2007/06/17 09:56:19 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;
112 lp1->d_secperunit = (u_int)info->d_media_blocks;
114 if (lp1->d_typename[0] == '\0')
115 strncpy(lp1->d_typename, "amnesiac", sizeof(lp1->d_typename));
116 if (lp1->d_packname[0] == '\0')
117 strncpy(lp1->d_packname, "fictitious", sizeof(lp1->d_packname));
118 if (lp1->d_nsectors == 0)
119 lp1->d_nsectors = 32;
120 if (lp1->d_ntracks == 0)
122 lp1->d_secpercyl = lp1->d_nsectors * lp1->d_ntracks;
123 lp1->d_ncylinders = lp1->d_secperunit / lp1->d_secpercyl;
126 if (lp1->d_interleave == 0)
127 lp1->d_interleave = 1;
128 if (lp1->d_npartitions < RAW_PART + 1)
129 lp1->d_npartitions = MAXPARTITIONS;
130 if (lp1->d_bbsize == 0)
131 lp1->d_bbsize = BBSIZE;
132 if (lp1->d_sbsize == 0)
133 lp1->d_sbsize = SBSIZE;
136 * Used by various devices to create a compatibility slice which
137 * allows us to mount root from devices which do not have a
138 * disklabel. Particularly: CDs.
140 lp1->d_partitions[RAW_PART].p_size = lp1->d_secperunit;
141 if (info->d_dsflags & DSO_COMPATPARTA) {
142 lp1->d_partitions[0].p_size = lp1->d_secperunit;
143 lp1->d_partitions[0].p_fstype = FS_OTHER;
145 lp1->d_magic = DISKMAGIC;
146 lp1->d_magic2 = DISKMAGIC;
147 lp1->d_checksum = dkcksum(lp1);
152 * Determine the size of the transfer, and make sure it is
153 * within the boundaries of the partition. Adjust transfer
154 * if needed, and signal errors or early completion.
157 * o Split buffers that are too big for the device.
158 * o Check for overflow.
159 * o Finish cleaning this up.
161 * This function returns 1 on success, 0 if transfer equates
162 * to EOF (end of disk) or -1 on failure. The appropriate
163 * 'errno' value is also set in bp->b_error and bp->b_flags
164 * is marked with B_ERROR.
167 dscheck(cdev_t dev, struct bio *bio, struct diskslices *ssp)
169 struct buf *bp = bio->bio_buf;
171 struct disklabel *lp;
176 u_int64_t slicerel_secno;
177 struct diskslice *sp;
184 slice = dkslice(dev);
187 if (bio->bio_offset < 0) {
188 kprintf("dscheck(%s): negative bio_offset %lld\n",
189 devtoname(dev), bio->bio_offset);
192 if (slice >= ssp->dss_nslices) {
193 kprintf("dscheck(%s): slice too large %d/%d\n",
194 devtoname(dev), slice, ssp->dss_nslices);
197 sp = &ssp->dss_slices[slice];
200 * Calculate secno and nsec
202 if (ssp->dss_secmult == 1) {
205 } else if (ssp->dss_secshift != -1) {
206 shift = DEV_BSHIFT + ssp->dss_secshift;
208 mask = (1 << shift) - 1;
209 if ((int)bp->b_bcount & mask)
211 if ((int)bio->bio_offset & mask)
213 secno = bio->bio_offset >> shift;
214 nsec = bp->b_bcount >> shift;
216 if (bp->b_bcount % ssp->dss_secsize)
218 if (bio->bio_offset % ssp->dss_secsize)
220 secno = bio->bio_offset / ssp->dss_secsize;
221 nsec = bp->b_bcount / ssp->dss_secsize;
225 * Calculate slice-relative sector number end slice-relative
228 if (slice == WHOLE_DISK_SLICE) {
230 * Labels have not been allowed on whole-disks for a while.
231 * This really puts the nail in the coffin... no disk
232 * snooping will occur even if you tried to write a label
233 * without a slice structure.
235 * Accesses to the WHOLE_DISK_SLICE do not use a disklabel
236 * and partition numbers are special-cased. Currently numbers
237 * less then 128 are not allowed. Partition numbers >= 128
238 * are encoded in the high 8 bits of the 64 bit buffer offset
239 * and are fed directly through to the device with no
240 * further interpretation. In particular, no sector
241 * translation interpretation should occur because the
242 * sector size for the special raw access may not be the
243 * same as the nominal sector size for the device.
247 kprintf("dscheck(%s): illegal partition number (%d) "
248 "for WHOLE_DISK_SLICE access\n",
249 devtoname(dev), part);
251 } else if (part != WHOLE_SLICE_PART) {
252 nbio = push_bio(bio);
253 nbio->bio_offset = bio->bio_offset |
254 (u_int64_t)part << 56;
259 * sp->ds_size is for the whole disk in the WHOLE_DISK_SLICE,
260 * there are no reserved areas.
262 endsecno = sp->ds_size;
263 slicerel_secno = secno;
265 } else if (part == WHOLE_SLICE_PART) {
267 * We are accessing a slice. Enable snooping of the bsd
268 * label. Note that snooping only occurs if ds_reserved
269 * is also non-zero. ds_reserved will be non-zero if
270 * an in-core label is present or snooping has been
271 * explicitly requested via an ioctl().
273 * NOTE! opens on a whole-slice partition will not attempt
274 * to read a disklabel in, so there may not be an in-core
275 * disklabel even if there is one on the disk.
277 endsecno = sp->ds_size;
278 slicerel_secno = secno;
280 } else if ((lp = sp->ds_label) && part < lp->d_npartitions) {
282 * A disklabel is present and a partition is explicitly being
283 * accessed (verses a whole-slice). Snooping of the
284 * partition is not supported even if the disklabel is
285 * accessible. Of course, the reserved area is still
288 struct partition *pp;
290 pp = &lp->d_partitions[dkpart(dev)];
291 endsecno = pp->p_size;
292 slicerel_secno = pp->p_offset + secno;
296 * Partition out of bounds
298 kprintf("dscheck(%s): partition out of bounds %d/%d\n",
300 part, lp->d_npartitions);
304 * Attempt to access partition when no disklabel present
306 kprintf("dscheck(%s): attempt to access non-existant partition\n",
312 * Disallow writes to reserved areas unless ds_wlabel allows it.
314 if (slicerel_secno < sp->ds_reserved && nsec &&
315 bp->b_cmd != BUF_CMD_READ && sp->ds_wlabel == 0) {
321 * If we get here, bio_offset must be on a block boundary and
322 * the sector size must be a power of 2.
324 if ((bio->bio_offset & (ssp->dss_secsize - 1)) ||
325 (ssp->dss_secsize ^ (ssp->dss_secsize - 1)) !=
326 ((ssp->dss_secsize << 1) - 1)) {
327 kprintf("%s: invalid BIO offset, not sector aligned or"
328 " invalid sector size (not power of 2) %08llx %d\n",
329 devtoname(dev), bio->bio_offset, ssp->dss_secsize);
336 if (secno + nsec > endsecno) {
338 * Return an error if beyond the end of the disk, or
339 * if B_BNOCLIP is set. Tell the system that we do not
340 * need to keep the buffer around.
342 if (secno > endsecno || (bp->b_flags & B_BNOCLIP))
346 * If exactly at end of disk, return an EOF. Throw away
347 * the buffer contents, if any, by setting B_INVAL.
349 if (secno == endsecno) {
350 bp->b_resid = bp->b_bcount;
351 bp->b_flags |= B_INVAL;
358 nsec = endsecno - secno;
359 bp->b_bcount = nsec * ssp->dss_secsize;
362 nbio = push_bio(bio);
363 nbio->bio_offset = (off_t)(sp->ds_offset + slicerel_secno) *
367 * Snoop reads and writes to the label area - only done if
368 * snoop is non-zero, ds_reserved is non-zero, and the
369 * read covers the label sector.
371 if (snoop && slicerel_secno < sp->ds_reserved &&
372 slicerel_secno <= LABELSECTOR &&
373 nsec && slicerel_secno + nsec > LABELSECTOR) {
375 * Set up our own callback on I/O completion to handle
376 * undoing the fixup we did for the write as well as
377 * doing the fixup for a read.
379 * Set info2.offset to the offset within the buffer containing
380 * the start of the label.
382 nbio->bio_done = dsiodone;
383 nbio->bio_caller_info1.ptr = sp;
384 nbio->bio_caller_info2.offset =
385 (LABELSECTOR - slicerel_secno) * ssp->dss_secsize;
386 if (bp->b_cmd != BUF_CMD_READ) {
390 (bp->b_data + (int)nbio->bio_caller_info2.offset),
393 kprintf("dscheck(%s): %s\n",
394 devtoname(dev), msg);
405 "dscheck(%s): b_bcount %d is not on a sector boundary (ssize %d)\n",
406 devtoname(dev), bp->b_bcount, ssp->dss_secsize);
411 "dscheck(%s): bio_offset %lld is not on a sector boundary (ssize %d)\n",
412 devtoname(dev), bio->bio_offset, ssp->dss_secsize);
414 bp->b_error = EINVAL;
418 * Terminate the I/O with a ranging error. Since the buffer is
419 * either illegal or beyond the file EOF, mark it B_INVAL as well.
421 bp->b_resid = bp->b_bcount;
422 bp->b_flags |= B_ERROR | B_INVAL;
425 * Caller must biodone() the originally passed bio if NULL is
432 dsclose(cdev_t dev, int mode, struct diskslices *ssp)
436 struct diskslice *sp;
438 slice = dkslice(dev);
440 if (slice < ssp->dss_nslices) {
441 sp = &ssp->dss_slices[slice];
447 dsgone(struct diskslices **sspp)
450 struct diskslice *sp;
451 struct diskslices *ssp;
453 for (slice = 0, ssp = *sspp; slice < ssp->dss_nslices; slice++) {
454 sp = &ssp->dss_slices[slice];
455 free_ds_label(ssp, slice);
457 kfree(ssp, M_DEVBUF);
462 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
463 * is subject to the same restriction as dsopen().
466 dsioctl(cdev_t dev, u_long cmd, caddr_t data, int flags,
467 struct diskslices **sspp, struct disk_info *info)
470 struct disklabel *lp;
472 u_int32_t openmask[DKMAXPARTITIONS/(sizeof(u_int32_t)*8)];
473 u_int64_t old_reserved;
476 struct diskslice *sp;
477 struct diskslices *ssp;
478 struct partition *pp;
480 slice = dkslice(dev);
483 if (slice >= ssp->dss_nslices)
485 sp = &ssp->dss_slices[slice];
491 * You can only retrieve a virgin disklabel on the whole
492 * disk slice or whole-slice partition.
494 if (slice != WHOLE_DISK_SLICE &&
495 part != WHOLE_SLICE_PART) {
499 lp = (struct disklabel *)data;
500 if (ssp->dss_slices[WHOLE_DISK_SLICE].ds_label) {
501 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
503 bzero(lp, sizeof(struct disklabel));
505 lp->d_magic = DISKMAGIC;
506 lp->d_magic2 = DISKMAGIC;
508 lp->d_npartitions = MAXPARTITIONS;
509 if (lp->d_interleave == 0)
510 lp->d_interleave = 1;
513 if (lp->d_nsectors == 0) /* sectors per track */
515 if (lp->d_ntracks == 0) /* heads */
517 lp->d_ncylinders = 0;
518 lp->d_bbsize = BBSIZE;
519 lp->d_sbsize = SBSIZE;
522 * If the slice or GPT partition is really small we could
523 * wind up with an absurd calculation for ncylinders.
525 while (lp->d_ncylinders < 4) {
526 if (lp->d_ntracks > 1)
528 else if (lp->d_nsectors > 1)
529 lp->d_nsectors >>= 1;
532 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
533 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
537 * Set or Modify the partition sizes to accomodate the slice,
538 * since we started with a copy of the virgin label stored
539 * in the whole-disk-slice and we are probably not a
542 lp->d_secperunit = sp->ds_size;
543 pp = &lp->d_partitions[RAW_PART];
545 pp->p_size = lp->d_secperunit;
546 if (info->d_dsflags & DSO_COMPATPARTA) {
547 pp = &lp->d_partitions[0];
549 pp->p_size = lp->d_secperunit;
550 pp->p_fstype = FS_OTHER;
553 lp->d_checksum = dkcksum(lp);
558 * You can only retrieve a disklabel on the whole
561 * We do not support labels directly on whole-disks
562 * any more (that is, disks without slices), unless the
563 * device driver has asked for a compatible label (e.g.
564 * for a CD) to allow booting off of storage that is
565 * otherwise unlabeled.
568 if (part != WHOLE_SLICE_PART)
570 if (slice == WHOLE_DISK_SLICE &&
571 (info->d_dsflags & DSO_COMPATLABEL) == 0) {
574 if (sp->ds_label == NULL) {
575 error = dsreadandsetlabel(dev, info->d_dsflags,
579 *(struct disklabel *)data = *sp->ds_label;
584 struct partinfo *dpart = (void *)data;
587 * The disk management layer may not have read the
588 * disklabel yet because simply opening a slice no
589 * longer 'probes' the disk that way. Be sure we
592 * We ignore any error.
594 if (sp->ds_label == NULL && part == WHOLE_SLICE_PART &&
595 slice != WHOLE_DISK_SLICE) {
596 dsreadandsetlabel(dev, info->d_dsflags,
600 bzero(dpart, sizeof(*dpart));
601 dpart->media_offset = (u_int64_t)sp->ds_offset *
602 info->d_media_blksize;
603 dpart->media_size = (u_int64_t)sp->ds_size *
604 info->d_media_blksize;
605 dpart->media_blocks = sp->ds_size;
606 dpart->media_blksize = info->d_media_blksize;
607 dpart->reserved_blocks= sp->ds_reserved;
609 if (slice != WHOLE_DISK_SLICE &&
610 part != WHOLE_SLICE_PART) {
613 if (lp == NULL || part >= lp->d_npartitions)
616 p = &lp->d_partitions[part];
617 dpart->fstype = p->p_fstype;
618 dpart->media_offset += (u_int64_t)p->p_offset *
619 info->d_media_blksize;
620 dpart->media_size = (u_int64_t)p->p_size *
621 info->d_media_blksize;
622 dpart->media_blocks = (u_int64_t)p->p_size;
625 * partition starting sector (p_offset)
626 * requires slice's reserved areas to be
629 if (dpart->reserved_blocks > p->p_offset)
630 dpart->reserved_blocks -= p->p_offset;
632 dpart->reserved_blocks = 0;
636 * Load remaining fields from the info structure
638 dpart->d_nheads = info->d_nheads;
639 dpart->d_ncylinders = info->d_ncylinders;
640 dpart->d_secpertrack = info->d_secpertrack;
641 dpart->d_secpercyl = info->d_secpercyl;
646 bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
652 * You can write a disklabel on the whole disk slice or
653 * whole-slice partition.
655 if (slice != WHOLE_DISK_SLICE &&
656 part != WHOLE_SLICE_PART) {
661 * We no longer support writing disklabels directly to media
662 * without there being a slice. Keep this as a separate
665 if (slice == WHOLE_DISK_SLICE)
668 if (!(flags & FWRITE))
670 lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK);
671 if (sp->ds_label == NULL)
672 bzero(lp, sizeof *lp);
674 bcopy(sp->ds_label, lp, sizeof *lp);
675 if (sp->ds_label == NULL) {
676 bzero(openmask, sizeof(openmask));
678 bcopy(sp->ds_openmask, openmask, sizeof(openmask));
680 /* no longer supported, s0 is a real slice for GPT */
681 if (slice == COMPATIBILITY_SLICE) {
682 dssetmaskfrommask(&ssp->dss_slices[
683 ssp->dss_first_bsd_slice],
685 } else if (slice == ssp->dss_first_bsd_slice) {
686 dssetmaskfrommask(&ssp->dss_slices[
687 COMPATIBILITY_SLICE],
692 error = setdisklabel(lp, (struct disklabel *)data, openmask);
693 /* XXX why doesn't setdisklabel() check this? */
694 if (error == 0 && lp->d_partitions[RAW_PART].p_offset != 0)
697 if (lp->d_secperunit > sp->ds_size)
699 for (part = 0; part < lp->d_npartitions; part++)
700 if (lp->d_partitions[part].p_size > sp->ds_size)
707 free_ds_label(ssp, slice);
708 set_ds_label(ssp, slice, lp);
711 case DIOCSYNCSLICEINFO:
713 * This ioctl can only be done on the whole disk
715 if (slice != WHOLE_DISK_SLICE || part != WHOLE_SLICE_PART)
718 if (*(int *)data == 0) {
719 for (slice = 0; slice < ssp->dss_nslices; slice++) {
720 struct diskslice *ds = &ssp->dss_slices[slice];
722 switch(dscountmask(ds)) {
726 if (slice != WHOLE_DISK_SLICE)
728 if (!dschkmask(ds, RAW_PART))
738 * Temporarily forget the current slices struct and read
743 * XXX should wait for current accesses on this disk to
744 * complete, then lock out future accesses and opens.
747 lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK);
748 *lp = *ssp->dss_slices[WHOLE_DISK_SLICE].ds_label;
749 error = dsopen(dev, S_IFCHR, ssp->dss_oflags, sspp, info);
757 * Reopen everything. This is a no-op except in the "force"
758 * case and when the raw bdev and cdev are both open. Abort
761 for (slice = 0; slice < ssp->dss_nslices; slice++) {
762 for (part = 0; part < DKMAXPARTITIONS; ++part) {
763 if (!dschkmask(&ssp->dss_slices[slice], part))
765 error = dsopen(dkmodslice(dkmodpart(dev, part),
767 S_IFCHR, ssp->dss_oflags, sspp,
782 error = dsioctl(dev, DIOCSDINFO, data, flags, &ssp, info);
787 * Set the reserved area
789 old_wlabel = sp->ds_wlabel;
790 set_ds_wlabel(ssp, slice, TRUE);
791 old_reserved = sp->ds_reserved;
792 sp->ds_reserved = SBSIZE / ssp->dss_secsize;
793 error = writedisklabel(dev, sp->ds_label);
794 set_ds_wlabel(ssp, slice, old_wlabel);
795 sp->ds_reserved = old_reserved;
796 /* XXX should invalidate in-core label if write failed. */
801 * Set label snooping even if there is no label present.
803 if (slice == WHOLE_DISK_SLICE || part != WHOLE_SLICE_PART)
807 sp->ds_reserved = SBSIZE / ssp->dss_secsize;
815 if (slice == WHOLE_DISK_SLICE)
817 if (!(flags & FWRITE))
819 set_ds_wlabel(ssp, slice, *(int *)data != 0);
828 * Chain the bio_done. b_cmd remains valid through such chaining.
831 dsiodone(struct bio *bio)
833 struct buf *bp = bio->bio_buf;
836 if (bp->b_cmd != BUF_CMD_READ
837 || (!(bp->b_flags & B_ERROR) && bp->b_error == 0)) {
838 msg = fixlabel(NULL, bio->bio_caller_info1.ptr,
840 (bp->b_data + (int)bio->bio_caller_info2.offset),
843 kprintf("%s\n", msg);
845 biodone(bio->bio_prev);
849 dsisopen(struct diskslices *ssp)
856 for (slice = 0; slice < ssp->dss_nslices; slice++) {
857 if (dscountmask(&ssp->dss_slices[slice]))
864 * Allocate a slices "struct" and initialize it to contain only an empty
865 * compatibility slice (pointing to itself), a whole disk slice (covering
866 * the disk as described by the label), and (nslices - BASE_SLICES) empty
867 * slices beginning at BASE_SLICE.
869 * Note that the compatibility slice is no longer really a compatibility
870 * slice. It is slice 0 if a GPT label is present, and the dangerously
871 * dedicated slice if no slice table otherwise exists. Else it is 0-sized.
874 dsmakeslicestruct(int nslices, struct disk_info *info)
876 struct diskslice *sp;
877 struct diskslices *ssp;
879 ssp = kmalloc(offsetof(struct diskslices, dss_slices) +
880 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
881 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
882 ssp->dss_nslices = nslices;
886 * Figure out if we can use shifts or whether we have to
887 * use mod/multply to translate byte offsets into sector numbers.
889 if ((info->d_media_blksize ^ (info->d_media_blksize - 1)) ==
890 (info->d_media_blksize << 1) - 1) {
891 ssp->dss_secmult = info->d_media_blksize / DEV_BSIZE;
892 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
893 ssp->dss_secshift = -1;
895 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
897 ssp->dss_secmult = 0;
898 ssp->dss_secshift = -1;
900 ssp->dss_secsize = info->d_media_blksize;
901 sp = &ssp->dss_slices[0];
902 bzero(sp, nslices * sizeof *sp);
903 sp[WHOLE_DISK_SLICE].ds_size = info->d_media_blocks;
908 dsname(cdev_t dev, int unit, int slice, int part, char *partname)
910 static char name[32];
914 dname = dev_dname(dev);
915 if (strlen(dname) > 16)
916 dname = "nametoolong";
917 ksnprintf(name, sizeof(name), "%s%d", dname, unit);
921 if (slice != WHOLE_DISK_SLICE) {
923 * slice or slice + partition. BASE_SLICE is s1, but
924 * the compatibility slice (0) needs to be s0.
926 used += ksnprintf(name + used, sizeof(name) - used,
927 "s%d", (slice ? slice - BASE_SLICE + 1 : 0));
928 if (part != WHOLE_SLICE_PART) {
929 used += ksnprintf(name + used, sizeof(name) - used,
931 partname[0] = 'a' + part;
934 } else if (part == WHOLE_SLICE_PART) {
936 * whole-disk-device, raw access to disk
938 /* no string extension */
939 } else if (part > 128) {
941 * whole-disk-device, extended raw access partitions.
942 * (typically used to access CD audio tracks)
944 used += ksnprintf(name + used, sizeof(name) - used,
948 * whole-disk-device, illegal partition number
950 used += ksnprintf(name + used, sizeof(name) - used,
957 * This should only be called when the unit is inactive and the strategy
958 * routine should not allow it to become active unless we call it. Our
959 * strategy routine must be special to allow activity.
962 dsopen(cdev_t dev, int mode, u_int flags,
963 struct diskslices **sspp, struct disk_info *info)
968 struct diskslice *sp;
969 struct diskslices *ssp;
973 dev->si_bsize_phys = info->d_media_blksize;
976 * Do not attempt to read the slice table or disk label when
977 * accessing the whole-disk slice or a while-slice partition.
979 if (dkslice(dev) == WHOLE_DISK_SLICE)
980 flags |= DSO_ONESLICE | DSO_NOLABELS;
981 if (dkpart(dev) == WHOLE_SLICE_PART)
982 flags |= DSO_NOLABELS;
985 * Reinitialize the slice table unless there is an open device
988 * It would be nice if we didn't have to do this but when a
989 * user is slicing and partitioning up a disk it is a lot safer
990 * to not take any chances.
993 need_init = !dsisopen(ssp);
994 if (ssp != NULL && need_init)
998 * Allocate a minimal slices "struct". This will become
999 * the final slices "struct" if we don't want real slices
1000 * or if we can't find any real slices.
1002 * Then scan the disk
1004 *sspp = dsmakeslicestruct(BASE_SLICE, info);
1006 if ((flags & DSO_ONESLICE) == 0) {
1007 TRACE(("mbrinit\n"));
1008 error = mbrinit(dev, info, sspp);
1015 ssp->dss_oflags = flags;
1018 * If there are no real slices, then make the compatiblity
1019 * slice cover the whole disk.
1021 if (ssp->dss_nslices == BASE_SLICE) {
1022 sp = &ssp->dss_slices[COMPATIBILITY_SLICE];
1024 sp->ds_size = info->d_media_blocks;
1025 sp->ds_reserved = 0;
1029 * Set dss_first_bsd_slice to point at the first BSD
1032 for (slice = BASE_SLICE; slice < ssp->dss_nslices; slice++) {
1033 sp = &ssp->dss_slices[slice];
1034 if (sp->ds_type == DOSPTYP_386BSD /* XXX */) {
1036 struct diskslice *csp;
1039 ssp->dss_first_bsd_slice = slice;
1042 * no longer supported, s0 is a real slice
1045 csp = &ssp->dss_slices[COMPATIBILITY_SLICE];
1046 csp->ds_offset = sp->ds_offset;
1047 csp->ds_size = sp->ds_size;
1048 csp->ds_type = sp->ds_type;
1049 csp->ds_reserved = sp->ds_reserved;
1056 * By definition accesses via the whole-disk device do not
1057 * specify any reserved areas. The whole disk may be read
1058 * or written by the whole-disk device.
1060 * ds_label for a whole-disk device is only used as a
1063 sp = &ssp->dss_slices[WHOLE_DISK_SLICE];
1064 sp->ds_label = clone_label(info, NULL);
1065 sp->ds_wlabel = TRUE;
1066 sp->ds_reserved = 0;
1070 * Load the disklabel for the slice being accessed unless it is
1071 * a whole-disk-slice or a whole-slice-partition (as determined
1074 * We could scan all slices here and try to load up their
1075 * disklabels, but that would cause us to access slices that
1076 * the user may otherwise not intend us to access, or corrupted
1079 * XXX if there are no opens on the slice we may want to re-read
1080 * the disklabel anyway, even if we have one cached.
1082 slice = dkslice(dev);
1083 if (slice >= ssp->dss_nslices)
1085 sp = &ssp->dss_slices[slice];
1088 if ((flags & DSO_NOLABELS) == 0 && sp->ds_label == NULL) {
1089 dev1 = dkmodslice(dkmodpart(dev, WHOLE_SLICE_PART), slice);
1092 * If opening a raw disk we do not try to
1093 * read the disklabel now. No interpretation of raw disks
1094 * (e.g. like 'da0') ever occurs. We will try to read the
1095 * disklabel for a raw slice if asked to via DIOC* ioctls.
1097 * Access to the label area is disallowed by default. Note
1098 * however that accesses via WHOLE_DISK_SLICE, and accesses
1099 * via WHOLE_SLICE_PART for slices without valid disklabels,
1100 * will allow writes and ignore the flag.
1102 set_ds_wlabel(ssp, slice, FALSE);
1103 dsreadandsetlabel(dev1, flags, ssp, sp, info);
1107 * If opening a particular partition the disklabel must exist and
1108 * the partition must be present in the label.
1110 * If the partition is the special whole-disk-slice no partition
1113 if (part != WHOLE_SLICE_PART && slice != WHOLE_DISK_SLICE) {
1114 if (sp->ds_label == NULL || part >= sp->ds_label->d_npartitions)
1117 dssetmask(sp, part);
1120 * Do not allow special raw-extension partitions to be opened
1121 * if the device doesn't support them. Raw-extension partitions
1122 * are typically used to handle CD tracks.
1124 if (slice == WHOLE_DISK_SLICE && part >= 128 &&
1125 part != WHOLE_SLICE_PART) {
1126 if ((info->d_dsflags & DSO_RAWEXTENSIONS) == 0)
1133 * Attempt to read the disklabel. If successful, store it in sp->ds_label.
1135 * If we cannot read the disklabel and DSO_COMPATLABEL is set, we construct
1136 * a fake label covering the whole disk.
1140 dsreadandsetlabel(cdev_t dev, u_int flags,
1141 struct diskslices *ssp, struct diskslice *sp,
1142 struct disk_info *info)
1144 struct disklabel *lp1;
1148 int slice = dkslice(dev);
1149 u_int64_t old_reserved;
1151 sname = dsname(dev, dkunit(dev), slice, WHOLE_SLICE_PART, partname);
1152 lp1 = clone_label(info, sp);
1153 old_reserved = sp->ds_reserved;
1154 sp->ds_reserved = 0;
1155 msg = readdisklabel(dev, lp1);
1156 sp->ds_reserved = old_reserved;
1158 if (msg != NULL && (flags & DSO_COMPATLABEL)) {
1160 kfree(lp1, M_DEVBUF);
1161 lp1 = clone_label(info, sp);
1164 msg = fixlabel(sname, sp, lp1, FALSE);
1165 if (msg == NULL && lp1->d_secsize != info->d_media_blksize)
1166 msg = "inconsistent sector size";
1168 if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
1169 log(LOG_WARNING, "%s: cannot find label (%s)\n",
1171 kfree(lp1, M_DEVBUF);
1173 set_ds_label(ssp, slice, lp1);
1174 set_ds_wlabel(ssp, slice, FALSE);
1176 return (msg ? EINVAL : 0);
1180 dssize(cdev_t dev, struct diskslices **sspp)
1182 struct disklabel *lp;
1185 struct diskslices *ssp;
1187 slice = dkslice(dev);
1190 if (ssp == NULL || slice >= ssp->dss_nslices
1191 || !dschkmask(&ssp->dss_slices[slice], part)) {
1192 if (dev_dopen(dev, FREAD, S_IFCHR, proc0.p_ucred) != 0)
1194 dev_dclose(dev, FREAD, S_IFCHR);
1197 lp = ssp->dss_slices[slice].ds_label;
1200 return ((int64_t)lp->d_partitions[part].p_size);
1204 free_ds_label(struct diskslices *ssp, int slice)
1206 struct disklabel *lp;
1207 struct diskslice *sp;
1209 sp = &ssp->dss_slices[slice];
1213 kfree(lp, M_DEVBUF);
1214 set_ds_label(ssp, slice, (struct disklabel *)NULL);
1218 fixlabel(const char *sname, struct diskslice *sp, struct disklabel *lp, int writeflag)
1224 struct partition *pp;
1227 /* These errors "can't happen" so don't bother reporting details. */
1228 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC)
1229 return ("fixlabel: invalid magic");
1230 if (dkcksum(lp) != 0)
1231 return ("fixlabel: invalid checksum");
1233 pp = &lp->d_partitions[RAW_PART];
1236 * What a mess. For ages old backwards compatibility the disklabel
1237 * on-disk stores absolute offsets instead of slice-relative offsets.
1238 * So fix it up when reading, writing, or snooping.
1240 * The in-core label is always slice-relative.
1244 offset = sp->ds_offset;
1246 start = sp->ds_offset;
1247 offset = -sp->ds_offset;
1249 if (pp->p_offset != start) {
1250 if (sname != NULL) {
1252 "%s: rejecting BSD label: raw partition offset != slice offset\n",
1254 slice_info(sname, sp);
1255 partition_info(sname, RAW_PART, pp);
1257 return ("fixlabel: raw partition offset != slice offset");
1259 if (pp->p_size != sp->ds_size) {
1260 if (sname != NULL) {
1261 kprintf("%s: raw partition size != slice size\n", sname);
1262 slice_info(sname, sp);
1263 partition_info(sname, RAW_PART, pp);
1265 if (pp->p_size > sp->ds_size) {
1267 return ("fixlabel: raw partition size > slice size");
1268 kprintf("%s: truncating raw partition\n", sname);
1269 pp->p_size = sp->ds_size;
1272 end = start + sp->ds_size;
1274 return ("fixlabel: slice wraps");
1275 if (lp->d_secpercyl <= 0)
1276 return ("fixlabel: d_secpercyl <= 0");
1279 for (part = 0; part < lp->d_npartitions; part++, pp++) {
1280 if (pp->p_offset != 0 || pp->p_size != 0) {
1281 if (pp->p_offset < start
1282 || pp->p_offset + pp->p_size > end
1283 || pp->p_offset + pp->p_size < pp->p_offset) {
1284 if (sname != NULL) {
1286 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
1289 slice_info(sname, sp);
1292 partition_info(sname, part, pp);
1294 /* XXX else silently discard junk. */
1295 bzero(pp, sizeof *pp);
1297 pp->p_offset += offset;
1301 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
1302 lp->d_secperunit = sp->ds_size;
1304 lp->d_checksum = dkcksum(lp);
1309 partition_info(const char *sname, int part, struct partition *pp)
1311 kprintf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
1312 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
1313 (u_long)pp->p_size);
1317 slice_info(const char *sname, struct diskslice *sp)
1319 kprintf("%s: start %llu, end %llu, size %llu\n", sname,
1320 sp->ds_offset, sp->ds_offset + sp->ds_size - 1, sp->ds_size);
1324 set_ds_label(struct diskslices *ssp, int slice, struct disklabel *lp)
1326 struct diskslice *sp1 = &ssp->dss_slices[slice];
1328 struct diskslice *sp2;
1330 if (slice == COMPATIBILITY_SLICE)
1331 sp2 = &ssp->dss_slices[ssp->dss_first_bsd_slice];
1332 else if (slice == ssp->dss_first_bsd_slice)
1333 sp2 = &ssp->dss_slices[COMPATIBILITY_SLICE];
1344 * If the slice is not the whole-disk slice, setup the reserved
1347 * The reserved area for the original bsd disklabel, inclusive of
1348 * the label and space for boot2, is 15 sectors. If you've
1349 * noticed people traditionally skipping 16 sectors its because
1350 * the sector numbers start at the beginning of the slice rather
1351 * then the beginning of the disklabel and traditional dos slices
1352 * reserve a sector at the beginning for the boot code.
1354 * NOTE! With the traditional bsdlabel, the first N bytes of boot2
1355 * overlap with the disklabel. The disklabel program checks that
1358 * When clearing a label, the bsdlabel reserved area is reset.
1360 if (slice != WHOLE_DISK_SLICE) {
1363 * BSD uses in-band labels, meaning the label itself
1364 * is accessible from partitions within the label.
1365 * We must reserved the area taken up by the label
1366 * itself to prevent mistakes from wiping it.
1368 sp1->ds_reserved = SBSIZE / ssp->dss_secsize;
1371 sp2->ds_reserved = sp1->ds_reserved;
1374 sp1->ds_reserved = 0;
1377 sp2->ds_reserved = sp1->ds_reserved;
1384 set_ds_wlabel(struct diskslices *ssp, int slice, int wlabel)
1386 ssp->dss_slices[slice].ds_wlabel = wlabel;
1388 if (slice == COMPATIBILITY_SLICE)
1389 ssp->dss_slices[ssp->dss_first_bsd_slice].ds_wlabel = wlabel;
1390 else if (slice == ssp->dss_first_bsd_slice)
1391 ssp->dss_slices[COMPATIBILITY_SLICE].ds_wlabel = wlabel;