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. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: @(#)wd.c 7.2 (Berkeley) 5/9/91
39 * from: wd.c,v 1.55 1994/10/22 01:57:12 phk Exp $
40 * from: @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91
41 * from: ufs_disksubr.c,v 1.8 1994/06/07 01:21:39 phk Exp $
42 * $FreeBSD: src/sys/kern/subr_diskslice.c,v 1.82.2.6 2001/07/24 09:49:41 dd Exp $
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/disklabel.h>
50 #include <sys/disklabel32.h>
51 #include <sys/disklabel64.h>
52 #include <sys/diskslice.h>
54 #include <sys/diskmbr.h>
55 #include <sys/fcntl.h>
56 #include <sys/malloc.h>
58 #include <sys/syslog.h>
60 #include <sys/vnode.h>
61 #include <sys/device.h>
63 #include <vfs/ufs/dinode.h> /* XXX used only for fs.h */
64 #include <vfs/ufs/fs.h> /* XXX used only to get BBSIZE/SBSIZE */
65 #include <sys/devfs.h>
67 static int dsreadandsetlabel(cdev_t dev, u_int flags,
68 struct diskslices *ssp, struct diskslice *sp,
69 struct disk_info *info);
70 static void free_ds_label (struct diskslices *ssp, int slice);
71 static void set_ds_label (struct diskslices *ssp, int slice, disklabel_t lp,
73 static void set_ds_wlabel (struct diskslices *ssp, int slice, int wlabel);
76 * Determine the size of the transfer, and make sure it is
77 * within the boundaries of the partition. Adjust transfer
78 * if needed, and signal errors or early completion.
81 * o Split buffers that are too big for the device.
82 * o Check for overflow.
83 * o Finish cleaning this up.
85 * This function returns 1 on success, 0 if transfer equates
86 * to EOF (end of disk) or -1 on failure. The appropriate
87 * 'errno' value is also set in bp->b_error and bp->b_flags
88 * is marked with B_ERROR.
91 dscheck(cdev_t dev, struct bio *bio, struct diskslices *ssp)
93 struct buf *bp = bio->bio_buf;
100 u_int64_t slicerel_secno;
101 struct diskslice *sp;
107 slice = dkslice(dev);
110 if (bio->bio_offset < 0) {
111 kprintf("dscheck(%s): negative bio_offset %lld\n",
112 devtoname(dev), (long long)bio->bio_offset);
115 if (slice >= ssp->dss_nslices) {
116 kprintf("dscheck(%s): slice too large %d/%d\n",
117 devtoname(dev), slice, ssp->dss_nslices);
120 sp = &ssp->dss_slices[slice];
122 * Calculate secno and nsec
124 if (ssp->dss_secmult == 1) {
127 } else if (ssp->dss_secshift != -1) {
128 shift = DEV_BSHIFT + ssp->dss_secshift;
130 mask = (1 << shift) - 1;
131 if ((int)bp->b_bcount & mask)
133 if ((int)bio->bio_offset & mask)
135 secno = bio->bio_offset >> shift;
136 nsec = bp->b_bcount >> shift;
138 if (bp->b_bcount % ssp->dss_secsize)
140 if (bio->bio_offset % ssp->dss_secsize)
142 secno = bio->bio_offset / ssp->dss_secsize;
143 nsec = bp->b_bcount / ssp->dss_secsize;
147 * Calculate slice-relative sector number end slice-relative
150 if (slice == WHOLE_DISK_SLICE) {
152 * Labels have not been allowed on whole-disks for a while.
153 * This really puts the nail in the coffin.
155 * Accesses to the WHOLE_DISK_SLICE do not use a disklabel
156 * and partition numbers are special-cased. Currently numbers
157 * less then 128 are not allowed. Partition numbers >= 128
158 * are encoded in the high 8 bits of the 64 bit buffer offset
159 * and are fed directly through to the device with no
160 * further interpretation. In particular, no sector
161 * translation interpretation should occur because the
162 * sector size for the special raw access may not be the
163 * same as the nominal sector size for the device.
167 kprintf("dscheck(%s): illegal partition number (%d) "
168 "for WHOLE_DISK_SLICE access\n",
169 devtoname(dev), part);
171 } else if (part != WHOLE_SLICE_PART) {
172 nbio = push_bio(bio);
173 nbio->bio_offset = bio->bio_offset |
174 (u_int64_t)part << 56;
178 * If writing to the raw disk request a
179 * reprobe on the last close.
181 if (bp->b_cmd == BUF_CMD_WRITE)
182 sp->ds_flags |= DSF_REPROBE;
186 * sp->ds_size is for the whole disk in the WHOLE_DISK_SLICE,
187 * there are no reserved areas.
189 endsecno = sp->ds_size;
190 slicerel_secno = secno;
191 } else if (part == WHOLE_SLICE_PART) {
193 * NOTE! opens on a whole-slice partition will not attempt
194 * to read a disklabel in, so there may not be an in-core
195 * disklabel even if there is one on the disk.
197 endsecno = sp->ds_size;
198 slicerel_secno = secno;
199 } else if ((lp = sp->ds_label).opaque != NULL) {
201 * A label is present, extract the partition. Snooping of
202 * the disklabel is not supported even if accessible. Of
203 * course, the reserved area is still write protected.
206 if (ops->op_getpartbounds(ssp, lp, part,
207 &slicerel_secno, &endsecno)) {
208 kprintf("dscheck(%s): partition %d out of bounds\n",
209 devtoname(dev), part);
212 slicerel_secno += secno;
215 * Attempt to access partition when no disklabel present
217 kprintf("dscheck(%s): attempt to access non-existent partition\n",
223 * Disallow writes to reserved areas unless ds_wlabel allows it.
224 * If the reserved area is written to request a reprobe of the
225 * disklabel when the slice is closed.
227 if (slicerel_secno < sp->ds_reserved && nsec &&
228 bp->b_cmd == BUF_CMD_WRITE) {
229 if (sp->ds_wlabel == 0) {
233 sp->ds_flags |= DSF_REPROBE;
237 * If we get here, bio_offset must be on a block boundary and
238 * the sector size must be a power of 2.
240 if ((bio->bio_offset & (ssp->dss_secsize - 1)) ||
241 (ssp->dss_secsize ^ (ssp->dss_secsize - 1)) !=
242 ((ssp->dss_secsize << 1) - 1)) {
243 kprintf("%s: invalid BIO offset, not sector aligned or"
244 " invalid sector size (not power of 2) %08llx %d\n",
245 devtoname(dev), (long long)bio->bio_offset,
253 if (secno + nsec > endsecno) {
255 * Return an error if beyond the end of the disk, or
256 * if B_BNOCLIP is set. Tell the system that we do not
257 * need to keep the buffer around.
259 if (secno > endsecno || (bp->b_flags & B_BNOCLIP))
263 * If exactly at end of disk, return an EOF. Throw away
264 * the buffer contents, if any, by setting B_INVAL.
266 if (secno == endsecno) {
267 bp->b_resid = bp->b_bcount;
268 bp->b_flags |= B_INVAL;
275 nsec = endsecno - secno;
276 bp->b_bcount = nsec * ssp->dss_secsize;
279 nbio = push_bio(bio);
280 nbio->bio_offset = (off_t)(sp->ds_offset + slicerel_secno) *
286 "dscheck(%s): b_bcount %d is not on a sector boundary (ssize %d)\n",
287 devtoname(dev), bp->b_bcount, ssp->dss_secsize);
292 "dscheck(%s): bio_offset %lld is not on a sector boundary (ssize %d)\n",
293 devtoname(dev), (long long)bio->bio_offset, ssp->dss_secsize);
295 bp->b_error = EINVAL;
299 * Terminate the I/O with a ranging error. Since the buffer is
300 * either illegal or beyond the file EOF, mark it B_INVAL as well.
302 bp->b_resid = bp->b_bcount;
303 bp->b_flags |= B_ERROR | B_INVAL;
306 * Caller must biodone() the originally passed bio if NULL is
313 * dsclose() - close a cooked disk slice.
315 * WARNING! The passed diskslices and related diskslice structures may
316 * be invalidated or replaced by this function, callers must
317 * reload from the disk structure for continued access.
320 dsclose(cdev_t dev, int mode, struct diskslices *ssp)
324 struct diskslice *sp;
326 slice = dkslice(dev);
328 if (slice < ssp->dss_nslices) {
329 sp = &ssp->dss_slices[slice];
331 if (sp->ds_flags & DSF_REPROBE) {
332 sp->ds_flags &= ~DSF_REPROBE;
333 if (slice == WHOLE_DISK_SLICE) {
334 disk_msg_send_sync(DISK_DISK_REPROBE,
338 disk_msg_send_sync(DISK_SLICE_REPROBE,
342 /* ssp and sp may both be invalid after reprobe */
348 dsgone(struct diskslices **sspp)
351 struct diskslices *ssp;
353 if ((ssp = *sspp) != NULL) {
354 for (slice = 0; slice < ssp->dss_nslices; slice++)
355 free_ds_label(ssp, slice);
356 kfree(ssp, M_DEVBUF);
362 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
363 * is subject to the same restriction as dsopen().
366 dsioctl(cdev_t dev, u_long cmd, caddr_t data, int flags,
367 struct diskslices **sspp, struct disk_info *info)
374 u_int32_t openmask[DKMAXPARTITIONS/(sizeof(u_int32_t)*8)];
377 struct diskslice *sp;
378 struct diskslices *ssp;
380 slice = dkslice(dev);
385 if (slice >= ssp->dss_nslices)
387 sp = &ssp->dss_slices[slice];
389 ops = sp->ds_ops; /* may be NULL if no label */
393 ops = &disklabel32_ops;
396 if (cmd != DIOCGDVIRGIN32)
397 ops = &disklabel64_ops;
399 * You can only retrieve a virgin disklabel on the whole
400 * disk slice or whole-slice partition.
402 if (slice != WHOLE_DISK_SLICE &&
403 part != WHOLE_SLICE_PART) {
408 ops->op_makevirginlabel(lp, ssp, sp, info);
414 * You can only retrieve a disklabel on the whole
417 * We do not support labels directly on whole-disks
418 * any more (that is, disks without slices), unless the
419 * device driver has asked for a compatible label (e.g.
420 * for a CD) to allow booting off of storage that is
421 * otherwise unlabeled.
424 if (part != WHOLE_SLICE_PART)
426 if (slice == WHOLE_DISK_SLICE &&
427 (info->d_dsflags & DSO_COMPATLABEL) == 0) {
430 if (sp->ds_label.opaque == NULL) {
431 error = dsreadandsetlabel(dev, info->d_dsflags,
433 ops = sp->ds_ops; /* may be NULL */
437 * The type of label we found must match the type of
440 if (error == 0 && IOCPARM_LEN(cmd) != ops->labelsize)
443 bcopy(sp->ds_label.opaque, data, ops->labelsize);
448 struct partinfo *dpart = (void *)data;
451 * The disk management layer may not have read the
452 * disklabel yet because simply opening a slice no
453 * longer 'probes' the disk that way. Be sure we
456 * We ignore any error.
458 if (sp->ds_label.opaque == NULL &&
459 part == WHOLE_SLICE_PART &&
460 slice != WHOLE_DISK_SLICE) {
461 dsreadandsetlabel(dev, info->d_dsflags,
463 ops = sp->ds_ops; /* may be NULL */
466 bzero(dpart, sizeof(*dpart));
467 dpart->media_offset = (u_int64_t)sp->ds_offset *
468 info->d_media_blksize;
469 dpart->media_size = (u_int64_t)sp->ds_size *
470 info->d_media_blksize;
471 dpart->media_blocks = sp->ds_size;
472 dpart->media_blksize = info->d_media_blksize;
473 dpart->reserved_blocks= sp->ds_reserved;
474 dpart->fstype_uuid = sp->ds_type_uuid;
475 dpart->storage_uuid = sp->ds_stor_uuid;
477 if (slice != WHOLE_DISK_SLICE &&
478 part != WHOLE_SLICE_PART) {
481 if (lp.opaque == NULL)
483 if (ops->op_getpartbounds(ssp, lp, part,
487 ops->op_loadpartinfo(lp, part, dpart);
488 dpart->media_offset += start *
489 info->d_media_blksize;
490 dpart->media_size = blocks *
491 info->d_media_blksize;
492 dpart->media_blocks = blocks;
495 * partition starting sector (p_offset)
496 * requires slice's reserved areas to be
499 if (dpart->reserved_blocks > start)
500 dpart->reserved_blocks -= start;
502 dpart->reserved_blocks = 0;
506 * Load remaining fields from the info structure
508 dpart->d_nheads = info->d_nheads;
509 dpart->d_ncylinders = info->d_ncylinders;
510 dpart->d_secpertrack = info->d_secpertrack;
511 dpart->d_secpercyl = info->d_secpercyl;
516 bcopy(ssp, data, (char *)&ssp->dss_slices[ssp->dss_nslices] -
521 ops = &disklabel32_ops;
524 if (cmd != DIOCSDINFO32)
525 ops = &disklabel64_ops;
527 * You can write a disklabel on the whole disk slice or
528 * whole-slice partition.
530 if (slice != WHOLE_DISK_SLICE &&
531 part != WHOLE_SLICE_PART) {
536 * We no longer support writing disklabels directly to media
537 * without there being a slice. Keep this as a separate
540 if (slice == WHOLE_DISK_SLICE)
542 if (!(flags & FWRITE))
546 * If an existing label is present it must be the same
547 * type as the label being passed by the ioctl.
549 if (sp->ds_label.opaque && sp->ds_ops != ops)
553 * Create a temporary copy of the existing label
554 * (if present) so setdisklabel can compare it against
557 lp.opaque = kmalloc(ops->labelsize, M_DEVBUF, M_WAITOK);
558 if (sp->ds_label.opaque == NULL)
559 bzero(lp.opaque, ops->labelsize);
561 bcopy(sp->ds_label.opaque, lp.opaque, ops->labelsize);
562 if (sp->ds_label.opaque == NULL) {
563 bzero(openmask, sizeof(openmask));
565 bcopy(sp->ds_openmask, openmask, sizeof(openmask));
568 error = ops->op_setdisklabel(lp, lptmp, ssp, sp, openmask);
569 disk_msg_send_sync(DISK_SLICE_REPROBE, dev->si_disk, sp);
572 kfree(lp.opaque, M_DEVBUF);
575 free_ds_label(ssp, slice);
576 set_ds_label(ssp, slice, lp, ops);
579 case DIOCSYNCSLICEINFO:
581 * This ioctl can only be done on the whole disk
583 if (slice != WHOLE_DISK_SLICE || part != WHOLE_SLICE_PART)
586 if (*(int *)data == 0) {
587 for (slice = 0; slice < ssp->dss_nslices; slice++) {
588 struct diskslice *ds = &ssp->dss_slices[slice];
590 switch(dscountmask(ds)) {
594 if (slice != WHOLE_DISK_SLICE)
596 if (!dschkmask(ds, RAW_PART))
605 disk_msg_send_sync(DISK_DISK_REPROBE, dev->si_disk, NULL);
611 error = dsioctl(dev, ((cmd == DIOCWDINFO32) ?
612 DIOCSDINFO32 : DIOCSDINFO64),
613 data, flags, &ssp, info);
614 if (error == 0 && sp->ds_label.opaque == NULL)
616 if (part != WHOLE_SLICE_PART)
622 * Allow the reserved area to be written, reload ops
623 * because the DIOCSDINFO op above may have installed
627 old_wlabel = sp->ds_wlabel;
628 set_ds_wlabel(ssp, slice, TRUE);
629 error = ops->op_writedisklabel(dev, ssp, sp, sp->ds_label);
630 disk_msg_send_sync(DISK_SLICE_REPROBE, dev->si_disk, sp);
632 set_ds_wlabel(ssp, slice, old_wlabel);
633 /* XXX should invalidate in-core label if write failed. */
637 if (slice == WHOLE_DISK_SLICE)
639 if (!(flags & FWRITE))
641 set_ds_wlabel(ssp, slice, *(int *)data != 0);
650 dsisopen(struct diskslices *ssp)
656 for (slice = 0; slice < ssp->dss_nslices; slice++) {
657 if (dscountmask(&ssp->dss_slices[slice]))
664 * Allocate a slices "struct" and initialize it to contain only an empty
665 * compatibility slice (pointing to itself), a whole disk slice (covering
666 * the disk as described by the label), and (nslices - BASE_SLICES) empty
667 * slices beginning at BASE_SLICE.
669 * Note that the compatibility slice is no longer really a compatibility
670 * slice. It is slice 0 if a GPT label is present, and the dangerously
671 * dedicated slice if no slice table otherwise exists. Else it is 0-sized.
674 dsmakeslicestruct(int nslices, struct disk_info *info)
676 struct diskslice *sp;
677 struct diskslices *ssp;
679 ssp = kmalloc(offsetof(struct diskslices, dss_slices) +
680 nslices * sizeof *sp, M_DEVBUF, M_WAITOK);
681 ssp->dss_first_bsd_slice = COMPATIBILITY_SLICE;
682 ssp->dss_nslices = nslices;
686 * Figure out if we can use shifts or whether we have to
687 * use mod/multply to translate byte offsets into sector numbers.
689 if ((info->d_media_blksize ^ (info->d_media_blksize - 1)) ==
690 (info->d_media_blksize << 1) - 1) {
691 ssp->dss_secmult = info->d_media_blksize / DEV_BSIZE;
692 if (ssp->dss_secmult & (ssp->dss_secmult - 1))
693 ssp->dss_secshift = -1;
695 ssp->dss_secshift = ffs(ssp->dss_secmult) - 1;
697 ssp->dss_secmult = 0;
698 ssp->dss_secshift = -1;
700 ssp->dss_secsize = info->d_media_blksize;
701 sp = &ssp->dss_slices[0];
702 bzero(sp, nslices * sizeof *sp);
703 sp[WHOLE_DISK_SLICE].ds_size = info->d_media_blocks;
708 dsname(cdev_t dev, int unit, int slice, int part, char *partname)
714 * This should only be called when the unit is inactive and the strategy
715 * routine should not allow it to become active unless we call it. Our
716 * strategy routine must be special to allow activity.
719 dsopen(cdev_t dev, int mode, u_int flags,
720 struct diskslices **sspp, struct disk_info *info)
722 struct diskslice *sp;
723 struct diskslices *ssp;
728 dev->si_bsize_phys = info->d_media_blksize;
729 slice = dkslice(dev);
731 sp = &ssp->dss_slices[slice];
738 * Attempt to read the disklabel. If successful, store it in sp->ds_label.
740 * If we cannot read the disklabel and DSO_COMPATLABEL is set, we construct
741 * a fake label covering the whole disk.
745 dsreadandsetlabel(cdev_t dev, u_int flags,
746 struct diskslices *ssp, struct diskslice *sp,
747 struct disk_info *info)
754 int slice = dkslice(dev);
757 * Probe the disklabel
760 sname = dsname(dev, dkunit(dev), slice, WHOLE_SLICE_PART, partname);
761 ops = &disklabel32_ops;
762 msg = ops->op_readdisklabel(dev, sp, &lp, info);
763 if (msg && strcmp(msg, "no disk label") == 0) {
764 ops = &disklabel64_ops;
765 msg = disklabel64_ops.op_readdisklabel(dev, sp, &lp, info);
769 * If we failed and COMPATLABEL is set, create a dummy disklabel.
771 if (msg != NULL && (flags & DSO_COMPATLABEL)) {
773 if (sp->ds_size >= 0x100000000ULL)
774 ops = &disklabel64_ops;
776 ops = &disklabel32_ops;
777 lp = ops->op_clone_label(info, sp);
780 if (sp->ds_type == DOSPTYP_386BSD ||
781 sp->ds_type == DOSPTYP_DFLYBSD) {
782 log(LOG_WARNING, "%s: cannot find label (%s)\n",
786 kfree(lp.opaque, M_DEVBUF);
788 set_ds_label(ssp, slice, lp, ops);
789 set_ds_wlabel(ssp, slice, FALSE);
791 return (msg ? EINVAL : 0);
795 dssize(cdev_t dev, struct diskslices **sspp)
801 struct diskslices *ssp;
805 slice = dkslice(dev);
808 if (ssp == NULL || slice >= ssp->dss_nslices
809 || !dschkmask(&ssp->dss_slices[slice], part)) {
810 if (dev_dopen(dev, FREAD, S_IFCHR, proc0.p_ucred, NULL) != 0)
812 dev_dclose(dev, FREAD, S_IFCHR, NULL);
815 lp = ssp->dss_slices[slice].ds_label;
816 if (part == WHOLE_SLICE_PART) {
817 blocks = ssp->dss_slices[slice].ds_size;
818 } else if (lp.opaque == NULL) {
819 blocks = (u_int64_t)-1;
821 ops = ssp->dss_slices[slice].ds_ops;
822 if (ops->op_getpartbounds(ssp, lp, part, &start, &blocks))
825 return ((int64_t)blocks);
829 free_ds_label(struct diskslices *ssp, int slice)
831 struct diskslice *sp;
834 sp = &ssp->dss_slices[slice];
836 if (lp.opaque != NULL) {
837 kfree(lp.opaque, M_DEVBUF);
839 set_ds_label(ssp, slice, lp, NULL);
844 set_ds_label(struct diskslices *ssp, int slice,
845 disklabel_t lp, disklabel_ops_t ops)
847 struct diskslice *sp = &ssp->dss_slices[slice];
851 if (lp.opaque && slice != WHOLE_DISK_SLICE)
852 ops->op_adjust_label_reserved(ssp, slice, sp);
858 set_ds_wlabel(struct diskslices *ssp, int slice, int wlabel)
860 ssp->dss_slices[slice].ds_wlabel = wlabel;