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.41 2007/05/21 04:21:05 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
;
177 u_int64_t slicerel_secno
;
178 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.
261 labelsect
= 0; /* ignore any reserved sectors, do not sniff */
262 endsecno
= sp
->ds_size
;
263 slicerel_secno
= secno
;
264 } else if (part
== WHOLE_SLICE_PART
) {
266 * We are accessing a slice. Snoop the label and check
267 * reserved blocks only if a label is present, otherwise
268 * do not. A label may be present if (1) there are active
269 * opens on the disk (not necessarily this slice) or
270 * (2) the disklabel program has written an in-core label
271 * and now wants to write it out, or (3) the management layer
272 * is trying to write out an in-core layer. In case (2) and
273 * (3) we MUST snoop the write or the on-disk version of the
274 * disklabel will not be properly translated.
276 * NOTE! opens on a whole-slice partition will not attempt
277 * to read a disklabel in.
279 if ((lp
= sp
->ds_label
) != NULL
) {
280 labelsect
= sp
->ds_skip_bsdlabel
;
284 endsecno
= sp
->ds_size
;
285 slicerel_secno
= secno
;
286 } else if ((lp
= sp
->ds_label
) && part
< lp
->d_npartitions
) {
288 * Acesss through disklabel, partition present.
290 struct partition
*pp
;
292 labelsect
= sp
->ds_skip_bsdlabel
;
293 pp
= &lp
->d_partitions
[dkpart(dev
)];
294 endsecno
= pp
->p_size
;
295 slicerel_secno
= pp
->p_offset
+ secno
;
298 * Partition out of bounds
300 kprintf("dscheck(%s): partition out of bounds %d/%d\n",
302 part
, lp
->d_npartitions
);
306 * Attempt to access partition when no disklabel present
308 kprintf("dscheck(%s): attempt to access non-existant partition\n",
314 * labelsect will reflect the extent of any reserved blocks from
315 * the beginning of the slice. We only check the slice reserved
316 * fields (sp->ds_skip_platform and sp->ds_skip_bsdlabel) if
317 * labelsect is non-zero, otherwise we ignore them. When labelsect
318 * is non-zero, sp->ds_skip_platform indicates the sector where the
321 * First determine if an attempt is being made to write to a
322 * reserved area when such writes are not allowed.
325 if (slicerel_secno
< 16 && nsec
&&
326 bp
->b_cmd
!= BUF_CMD_READ
) {
327 kprintf("Attempt to write to reserved sector %lld labelsect %lld label %p/%p skip_plat %d skip_bsd %d WLABEL %d\n",
331 sp
->ds_skip_platform
,
332 sp
->ds_skip_bsdlabel
,
336 if (slicerel_secno
< labelsect
&& nsec
&&
337 bp
->b_cmd
!= BUF_CMD_READ
&& sp
->ds_wlabel
== 0) {
343 * If we get here, bio_offset must be on a block boundary and
344 * the sector size must be a power of 2.
346 if ((bio
->bio_offset
& (ssp
->dss_secsize
- 1)) ||
347 (ssp
->dss_secsize
^ (ssp
->dss_secsize
- 1)) !=
348 ((ssp
->dss_secsize
<< 1) - 1)) {
349 kprintf("%s: invalid BIO offset, not sector aligned or"
350 " invalid sector size (not power of 2) %08llx %d\n",
351 devtoname(dev
), bio
->bio_offset
, ssp
->dss_secsize
);
358 if (secno
+ nsec
> endsecno
) {
360 * Return an error if beyond the end of the disk, or
361 * if B_BNOCLIP is set. Tell the system that we do not
362 * need to keep the buffer around.
364 if (secno
> endsecno
|| (bp
->b_flags
& B_BNOCLIP
))
368 * If exactly at end of disk, return an EOF. Throw away
369 * the buffer contents, if any, by setting B_INVAL.
371 if (secno
== endsecno
) {
372 bp
->b_resid
= bp
->b_bcount
;
373 bp
->b_flags
|= B_INVAL
;
380 nsec
= endsecno
- secno
;
381 bp
->b_bcount
= nsec
* ssp
->dss_secsize
;
384 nbio
= push_bio(bio
);
385 nbio
->bio_offset
= (off_t
)(sp
->ds_offset
+ slicerel_secno
) *
389 * Snoop writes to the label area when labelsect is non-zero.
390 * The label sector starts at sector sp->ds_skip_platform within
391 * the slice and ends before sector sp->ds_skip_bsdlabel. The
392 * write must contain the label sector for us to be able to snoop it.
394 * We have to adjust the label's fields to the on-disk format on
395 * a write and then adjust them back on completion of the write,
398 * SNOOPs are required for disklabel -r and the DIOC* ioctls also
399 * depend on it on the backend for label operations. XXX
401 * NOTE! ds_skip_platform is usually set to non-zero by the slice
402 * scanning code, indicating that the slice has reserved boot
403 * sector(s). It is also set for compatibility reasons via
404 * the DSO_COMPATMBR flag. But it is not a requirement and it
405 * can be 0, indicating that the disklabel (if present) is stored
406 * at the beginning of the slice. In most cases ds_skip_platform
409 * ds_skip_bsdlabel is inclusive of ds_skip_platform. If they are
410 * the same then there is no label present, even if non-zero.
412 if (slicerel_secno
< labelsect
&& /* also checks labelsect!=0 */
413 sp
->ds_skip_platform
< labelsect
&& /* degenerate case */
414 slicerel_secno
<= sp
->ds_skip_platform
&&
415 slicerel_secno
+ nsec
> sp
->ds_skip_platform
) {
417 * Set up our own callback on I/O completion to handle
418 * undoing the fixup we did for the write as well as
419 * doing the fixup for a read.
421 nbio
->bio_done
= dsiodone
;
422 nbio
->bio_caller_info1
.ptr
= sp
;
423 nbio
->bio_caller_info2
.offset
=
424 (sp
->ds_skip_platform
- slicerel_secno
) * ssp
->dss_secsize
;
425 if (bp
->b_cmd
!= BUF_CMD_READ
) {
429 (bp
->b_data
+ (int)nbio
->bio_caller_info2
.offset
),
432 kprintf("dscheck(%s): %s\n",
433 devtoname(dev
), msg
);
444 "dscheck(%s): b_bcount %d is not on a sector boundary (ssize %d)\n",
445 devtoname(dev
), bp
->b_bcount
, ssp
->dss_secsize
);
450 "dscheck(%s): bio_offset %lld is not on a sector boundary (ssize %d)\n",
451 devtoname(dev
), bio
->bio_offset
, ssp
->dss_secsize
);
453 bp
->b_error
= EINVAL
;
457 * Terminate the I/O with a ranging error. Since the buffer is
458 * either illegal or beyond the file EOF, mark it B_INVAL as well.
460 bp
->b_resid
= bp
->b_bcount
;
461 bp
->b_flags
|= B_ERROR
| B_INVAL
;
464 * Caller must biodone() the originally passed bio if NULL is
471 dsclose(cdev_t dev
, int mode
, struct diskslices
*ssp
)
475 struct diskslice
*sp
;
477 slice
= dkslice(dev
);
479 if (slice
< ssp
->dss_nslices
) {
480 sp
= &ssp
->dss_slices
[slice
];
481 if (part
< sizeof(sp
->ds_openmask
) * 8)
482 sp
->ds_openmask
&= ~(1 << part
);
487 dsgone(struct diskslices
**sspp
)
490 struct diskslice
*sp
;
491 struct diskslices
*ssp
;
493 for (slice
= 0, ssp
= *sspp
; slice
< ssp
->dss_nslices
; slice
++) {
494 sp
= &ssp
->dss_slices
[slice
];
495 free_ds_label(ssp
, slice
);
497 kfree(ssp
, M_DEVBUF
);
502 * For the "write" commands (DIOCSDINFO and DIOCWDINFO), this
503 * is subject to the same restriction as dsopen().
506 dsioctl(cdev_t dev
, u_long cmd
, caddr_t data
, int flags
,
507 struct diskslices
**sspp
, struct disk_info
*info
)
510 struct disklabel
*lp
;
515 struct diskslice
*sp
;
516 struct diskslices
*ssp
;
517 struct partition
*pp
;
519 slice
= dkslice(dev
);
522 if (slice
>= ssp
->dss_nslices
)
524 sp
= &ssp
->dss_slices
[slice
];
530 * You can only retrieve a virgin disklabel on the whole
531 * disk slice or whole-slice partition.
533 if (slice
!= WHOLE_DISK_SLICE
&&
534 part
!= WHOLE_SLICE_PART
) {
538 lp
= (struct disklabel
*)data
;
539 if (ssp
->dss_slices
[WHOLE_DISK_SLICE
].ds_label
) {
540 *lp
= *ssp
->dss_slices
[WHOLE_DISK_SLICE
].ds_label
;
542 bzero(lp
, sizeof(struct disklabel
));
544 lp
->d_magic
= DISKMAGIC
;
545 lp
->d_magic2
= DISKMAGIC
;
547 lp
->d_npartitions
= MAXPARTITIONS
;
548 if (lp
->d_interleave
== 0)
549 lp
->d_interleave
= 1;
552 if (lp
->d_nsectors
== 0)
554 if (lp
->d_ntracks
== 0)
557 lp
->d_bbsize
= BBSIZE
;
558 lp
->d_sbsize
= SBSIZE
;
559 lp
->d_secpercyl
= lp
->d_nsectors
* lp
->d_ntracks
;
560 lp
->d_ncylinders
= sp
->ds_size
/ lp
->d_secpercyl
;
563 * Set or Modify the partition sizes to accomodate the slice,
564 * since we started with a copy of the virgin label stored
565 * in the whole-disk-slice and we are probably not a
568 lp
->d_secperunit
= sp
->ds_size
;
569 pp
= &lp
->d_partitions
[RAW_PART
];
571 pp
->p_size
= lp
->d_secperunit
;
572 if (info
->d_dsflags
& DSO_COMPATPARTA
) {
573 pp
= &lp
->d_partitions
[0];
575 pp
->p_size
= lp
->d_secperunit
;
576 pp
->p_fstype
= FS_OTHER
;
579 lp
->d_checksum
= dkcksum(lp
);
584 * You can only retrieve a disklabel on the whole
587 * We do not support labels directly on whole-disks
588 * any more (that is, disks without slices), unless the
589 * device driver has asked for a compatible label (e.g.
590 * for a CD) to allow booting off of storage that is
591 * otherwise unlabeled.
594 if (part
!= WHOLE_SLICE_PART
)
596 if (slice
== WHOLE_DISK_SLICE
&&
597 (info
->d_dsflags
& DSO_COMPATLABEL
) == 0) {
600 if (sp
->ds_label
== NULL
) {
601 error
= dsreadandsetlabel(dev
, info
->d_dsflags
,
605 *(struct disklabel
*)data
= *sp
->ds_label
;
610 struct partinfo
*dpart
= (void *)data
;
613 * If accessing a whole-slice partition the disk
614 * management layer may not have tried to read the
615 * disklabel. We have to try to read the label
616 * in order to properly initialize the ds_skip_*
619 * We ignore any error.
621 if (sp
->ds_label
== NULL
&& part
== WHOLE_SLICE_PART
&&
622 slice
!= WHOLE_DISK_SLICE
) {
623 dsreadandsetlabel(dev
, info
->d_dsflags
,
627 bzero(dpart
, sizeof(*dpart
));
628 dpart
->media_offset
= (u_int64_t
)sp
->ds_offset
*
629 info
->d_media_blksize
;
630 dpart
->media_size
= (u_int64_t
)sp
->ds_size
*
631 info
->d_media_blksize
;
632 dpart
->media_blocks
= sp
->ds_size
;
633 dpart
->media_blksize
= info
->d_media_blksize
;
634 dpart
->skip_platform
= sp
->ds_skip_platform
;
635 dpart
->skip_bsdlabel
= sp
->ds_skip_bsdlabel
;
637 if (slice
!= WHOLE_DISK_SLICE
&&
638 part
!= WHOLE_SLICE_PART
) {
641 if (lp
== NULL
|| part
>= lp
->d_npartitions
)
644 p
= &lp
->d_partitions
[part
];
645 dpart
->fstype
= p
->p_fstype
;
646 dpart
->media_offset
+= (u_int64_t
)p
->p_offset
*
647 info
->d_media_blksize
;
648 dpart
->media_size
= (u_int64_t
)p
->p_size
*
649 info
->d_media_blksize
;
650 dpart
->media_blocks
= (u_int64_t
)p
->p_size
;
653 * partition starting sector (p_offset)
654 * requires slice's reserved areas to be
657 if (dpart
->skip_platform
> p
->p_offset
)
658 dpart
->skip_platform
-= p
->p_offset
;
660 dpart
->skip_platform
= 0;
661 if (dpart
->skip_bsdlabel
> p
->p_offset
)
662 dpart
->skip_bsdlabel
-= p
->p_offset
;
664 dpart
->skip_bsdlabel
= 0;
668 * Load remaining fields from the info structure
670 dpart
->d_nheads
= info
->d_nheads
;
671 dpart
->d_ncylinders
= info
->d_ncylinders
;
672 dpart
->d_secpertrack
= info
->d_secpertrack
;
673 dpart
->d_secpercyl
= info
->d_secpercyl
;
678 bcopy(ssp
, data
, (char *)&ssp
->dss_slices
[ssp
->dss_nslices
] -
684 * You can write a disklabel on the whole disk slice or
685 * whole-slice partition.
687 if (slice
!= WHOLE_DISK_SLICE
&&
688 part
!= WHOLE_SLICE_PART
) {
693 * We no longer support writing disklabels directly to media
694 * without there being a slice. Keep this as a separate
697 if (slice
== WHOLE_DISK_SLICE
)
700 if (!(flags
& FWRITE
))
702 lp
= kmalloc(sizeof *lp
, M_DEVBUF
, M_WAITOK
);
703 if (sp
->ds_label
== NULL
)
704 bzero(lp
, sizeof *lp
);
706 bcopy(sp
->ds_label
, lp
, sizeof *lp
);
707 if (sp
->ds_label
== NULL
) {
710 openmask
= sp
->ds_openmask
;
711 if (slice
== COMPATIBILITY_SLICE
) {
712 openmask
|= ssp
->dss_slices
[
713 ssp
->dss_first_bsd_slice
].ds_openmask
;
714 } else if (slice
== ssp
->dss_first_bsd_slice
) {
715 openmask
|= ssp
->dss_slices
[
716 COMPATIBILITY_SLICE
].ds_openmask
;
719 error
= setdisklabel(lp
, (struct disklabel
*)data
,
721 /* XXX why doesn't setdisklabel() check this? */
722 if (error
== 0 && lp
->d_partitions
[RAW_PART
].p_offset
!= 0)
725 if (lp
->d_secperunit
> sp
->ds_size
)
727 for (part
= 0; part
< lp
->d_npartitions
; part
++)
728 if (lp
->d_partitions
[part
].p_size
> sp
->ds_size
)
735 free_ds_label(ssp
, slice
);
736 set_ds_label(ssp
, slice
, lp
);
739 case DIOCSYNCSLICEINFO
:
741 * This ioctl can only be done on the whole disk
743 if (slice
!= WHOLE_DISK_SLICE
|| part
!= WHOLE_SLICE_PART
)
746 if (*(int *)data
== 0) {
747 for (slice
= 0; slice
< ssp
->dss_nslices
; slice
++) {
748 openmask
= ssp
->dss_slices
[slice
].ds_openmask
;
750 (slice
!= WHOLE_DISK_SLICE
||
751 openmask
& ~(1 << RAW_PART
))) {
758 * Temporarily forget the current slices struct and read
763 * XXX should wait for current accesses on this disk to
764 * complete, then lock out future accesses and opens.
767 lp
= kmalloc(sizeof *lp
, M_DEVBUF
, M_WAITOK
);
768 *lp
= *ssp
->dss_slices
[WHOLE_DISK_SLICE
].ds_label
;
769 error
= dsopen(dev
, S_IFCHR
, ssp
->dss_oflags
, sspp
, info
);
777 * Reopen everything. This is a no-op except in the "force"
778 * case and when the raw bdev and cdev are both open. Abort
781 for (slice
= 0; slice
< ssp
->dss_nslices
; slice
++) {
782 for (openmask
= ssp
->dss_slices
[slice
].ds_openmask
,
783 part
= 0; openmask
; openmask
>>= 1, part
++) {
786 error
= dsopen(dkmodslice(dkmodpart(dev
, part
),
788 S_IFCHR
, ssp
->dss_oflags
, sspp
,
803 error
= dsioctl(dev
, DIOCSDINFO
, data
, flags
, &ssp
, info
);
807 * XXX this used to hack on dk_openpart to fake opening
808 * partition 0 in case that is used instead of dkpart(dev).
810 old_wlabel
= sp
->ds_wlabel
;
811 set_ds_wlabel(ssp
, slice
, TRUE
);
812 error
= writedisklabel(dev
, sp
->ds_label
);
813 /* XXX should invalidate in-core label if write failed. */
814 set_ds_wlabel(ssp
, slice
, old_wlabel
);
818 if (slice
== WHOLE_DISK_SLICE
)
820 if (!(flags
& FWRITE
))
822 set_ds_wlabel(ssp
, slice
, *(int *)data
!= 0);
831 * Chain the bio_done. b_cmd remains valid through such chaining.
834 dsiodone(struct bio
*bio
)
836 struct buf
*bp
= bio
->bio_buf
;
839 if (bp
->b_cmd
!= BUF_CMD_READ
840 || (!(bp
->b_flags
& B_ERROR
) && bp
->b_error
== 0)) {
841 msg
= fixlabel(NULL
, bio
->bio_caller_info1
.ptr
,
843 (bp
->b_data
+ (int)bio
->bio_caller_info2
.offset
),
846 kprintf("%s\n", msg
);
848 biodone(bio
->bio_prev
);
852 dsisopen(struct diskslices
*ssp
)
858 for (slice
= 0; slice
< ssp
->dss_nslices
; slice
++) {
859 if (ssp
->dss_slices
[slice
].ds_openmask
)
866 * Allocate a slices "struct" and initialize it to contain only an empty
867 * compatibility slice (pointing to itself), a whole disk slice (covering
868 * the disk as described by the label), and (nslices - BASE_SLICES) empty
869 * slices beginning at BASE_SLICE.
872 dsmakeslicestruct(int nslices
, struct disk_info
*info
)
874 struct diskslice
*sp
;
875 struct diskslices
*ssp
;
877 ssp
= kmalloc(offsetof(struct diskslices
, dss_slices
) +
878 nslices
* sizeof *sp
, M_DEVBUF
, M_WAITOK
);
879 ssp
->dss_first_bsd_slice
= COMPATIBILITY_SLICE
;
880 ssp
->dss_nslices
= nslices
;
884 * Figure out if we can use shifts or whether we have to
885 * use mod/multply to translate byte offsets into sector numbers.
887 if ((info
->d_media_blksize
^ (info
->d_media_blksize
- 1)) ==
888 (info
->d_media_blksize
<< 1) - 1) {
889 ssp
->dss_secmult
= info
->d_media_blksize
/ DEV_BSIZE
;
890 if (ssp
->dss_secmult
& (ssp
->dss_secmult
- 1))
891 ssp
->dss_secshift
= -1;
893 ssp
->dss_secshift
= ffs(ssp
->dss_secmult
) - 1;
895 ssp
->dss_secmult
= 0;
896 ssp
->dss_secshift
= -1;
898 ssp
->dss_secsize
= info
->d_media_blksize
;
899 sp
= &ssp
->dss_slices
[0];
900 bzero(sp
, nslices
* sizeof *sp
);
901 sp
[WHOLE_DISK_SLICE
].ds_size
= info
->d_media_blocks
;
906 dsname(cdev_t dev
, int unit
, int slice
, int part
, char *partname
)
908 static char name
[32];
912 dname
= dev_dname(dev
);
913 if (strlen(dname
) > 16)
914 dname
= "nametoolong";
915 ksnprintf(name
, sizeof(name
), "%s%d", dname
, unit
);
919 if (slice
!= WHOLE_DISK_SLICE
) {
921 * slice or slice + partition. BASE_SLICE is s1, but
922 * the compatibility slice (0) needs to be s0.
924 used
+= ksnprintf(name
+ used
, sizeof(name
) - used
,
925 "s%d", (slice
? slice
- BASE_SLICE
+ 1 : 0));
926 if (part
!= WHOLE_SLICE_PART
) {
927 used
+= ksnprintf(name
+ used
, sizeof(name
) - used
,
929 partname
[0] = 'a' + part
;
932 } else if (part
== WHOLE_SLICE_PART
) {
934 * whole-disk-device, raw access to disk
936 /* no string extension */
937 } else if (part
> 128) {
939 * whole-disk-device, extended raw access partitions.
940 * (typically used to access CD audio tracks)
942 used
+= ksnprintf(name
+ used
, sizeof(name
) - used
,
946 * whole-disk-device, illegal partition number
948 used
+= ksnprintf(name
+ used
, sizeof(name
) - used
,
955 * This should only be called when the unit is inactive and the strategy
956 * routine should not allow it to become active unless we call it. Our
957 * strategy routine must be special to allow activity.
960 dsopen(cdev_t dev
, int mode
, u_int flags
,
961 struct diskslices
**sspp
, struct disk_info
*info
)
966 struct diskslice
*sp
;
967 struct diskslices
*ssp
;
971 dev
->si_bsize_phys
= info
->d_media_blksize
;
974 * Do not attempt to read the slice table or disk label when
975 * accessing the whole-disk slice or a while-slice partition.
977 if (dkslice(dev
) == WHOLE_DISK_SLICE
)
978 flags
|= DSO_ONESLICE
| DSO_NOLABELS
;
979 if (dkpart(dev
) == WHOLE_SLICE_PART
)
980 flags
|= DSO_NOLABELS
;
983 * Reinitialize the slice table unless there is an open device
986 * It would be nice if we didn't have to do this but when a
987 * user is slicing and partitioning up a disk it is a lot safer
988 * to not take any chances.
991 need_init
= !dsisopen(ssp
);
992 if (ssp
!= NULL
&& need_init
)
996 * Allocate a minimal slices "struct". This will become
997 * the final slices "struct" if we don't want real slices
998 * or if we can't find any real slices.
1000 * Then scan the disk
1002 *sspp
= dsmakeslicestruct(BASE_SLICE
, info
);
1004 if ((flags
& DSO_ONESLICE
) == 0) {
1005 TRACE(("mbrinit\n"));
1006 error
= mbrinit(dev
, info
, sspp
);
1013 ssp
->dss_oflags
= flags
;
1016 * If there are no real slices, then make the compatiblity
1017 * slice cover the whole disk.
1019 * no sectors are reserved for the platform (ds_skip_platform
1020 * will be 0) in this case. This means that if a disklabel
1021 * is installed it will be directly installed in sector 0
1022 * unless DSO_COMPATMBR is requested.
1024 if (ssp
->dss_nslices
== BASE_SLICE
) {
1025 sp
= &ssp
->dss_slices
[COMPATIBILITY_SLICE
];
1027 sp
->ds_size
= info
->d_media_blocks
;
1028 if (info
->d_dsflags
& DSO_COMPATMBR
) {
1029 sp
->ds_skip_platform
= 1;
1030 sp
->ds_skip_bsdlabel
= sp
->ds_skip_platform
;
1032 sp
->ds_skip_platform
= 0;
1033 sp
->ds_skip_bsdlabel
= 0;
1038 * Point the compatibility slice at the BSD slice, if any.
1040 for (slice
= BASE_SLICE
; slice
< ssp
->dss_nslices
; slice
++) {
1041 sp
= &ssp
->dss_slices
[slice
];
1042 if (sp
->ds_type
== DOSPTYP_386BSD
/* XXX */) {
1043 struct diskslice
*csp
;
1045 csp
= &ssp
->dss_slices
[COMPATIBILITY_SLICE
];
1046 ssp
->dss_first_bsd_slice
= slice
;
1047 csp
->ds_offset
= sp
->ds_offset
;
1048 csp
->ds_size
= sp
->ds_size
;
1049 csp
->ds_type
= sp
->ds_type
;
1050 csp
->ds_skip_platform
= sp
->ds_skip_platform
;
1051 csp
->ds_skip_bsdlabel
= sp
->ds_skip_bsdlabel
;
1057 * By definition accesses via the whole-disk device do not
1058 * specify any reserved areas. The whole disk may be read
1059 * or written by the whole-disk device.
1061 * ds_label for a whole-disk device is only used as a
1064 sp
= &ssp
->dss_slices
[WHOLE_DISK_SLICE
];
1065 sp
->ds_label
= clone_label(info
, NULL
);
1066 sp
->ds_wlabel
= TRUE
;
1067 sp
->ds_skip_platform
= 0;
1068 sp
->ds_skip_bsdlabel
= 0;
1072 * Load the disklabel for the slice being accessed unless it is
1073 * a whole-disk-slice or a whole-slice-partition (as determined
1076 * We could scan all slices here and try to load up their
1077 * disklabels, but that would cause us to access slices that
1078 * the user may otherwise not intend us to access, or corrupted
1081 * XXX if there are no opens on the slice we may want to re-read
1082 * the disklabel anyway, even if we have one cached.
1084 slice
= dkslice(dev
);
1085 if (slice
>= ssp
->dss_nslices
)
1087 sp
= &ssp
->dss_slices
[slice
];
1090 if ((flags
& DSO_NOLABELS
) == 0 && sp
->ds_label
== NULL
) {
1091 dev1
= dkmodslice(dkmodpart(dev
, WHOLE_SLICE_PART
), slice
);
1094 * If opening a raw disk we do not try to
1095 * read the disklabel now. No interpretation of raw disks
1096 * (e.g. like 'da0') ever occurs. We will try to read the
1097 * disklabel for a raw slice if asked to via DIOC* ioctls.
1099 * Access to the label area is disallowed by default. Note
1100 * however that accesses via WHOLE_DISK_SLICE, and accesses
1101 * via WHOLE_SLICE_PART for slices without valid disklabels,
1102 * will allow writes and ignore the flag.
1104 set_ds_wlabel(ssp
, slice
, FALSE
);
1105 dsreadandsetlabel(dev1
, flags
, ssp
, sp
, info
);
1109 * If opening a particular partition the disklabel must exist and
1110 * the partition must be present in the label.
1112 * If the partition is the special whole-disk-slice no partition
1115 if (part
!= WHOLE_SLICE_PART
&& slice
!= WHOLE_DISK_SLICE
) {
1116 if (sp
->ds_label
== NULL
|| part
>= sp
->ds_label
->d_npartitions
)
1118 if (part
< sizeof(sp
->ds_openmask
) * 8) {
1119 sp
->ds_openmask
|= 1 << part
;
1124 * Do not allow special raw-extension partitions to be opened
1125 * if the device doesn't support them. Raw-extension partitions
1126 * are typically used to handle CD tracks.
1128 if (slice
== WHOLE_DISK_SLICE
&& part
>= 128 &&
1129 part
!= WHOLE_SLICE_PART
) {
1130 if ((info
->d_dsflags
& DSO_RAWEXTENSIONS
) == 0)
1137 * Attempt to read the disklabel. If successful, store it in sp->ds_label.
1139 * If we cannot read the disklabel and DSO_COMPATLABEL is set, we construct
1140 * a fake label covering the whole disk.
1144 dsreadandsetlabel(cdev_t dev
, u_int flags
,
1145 struct diskslices
*ssp
, struct diskslice
*sp
,
1146 struct disk_info
*info
)
1148 struct disklabel
*lp1
;
1152 int slice
= dkslice(dev
);
1154 sname
= dsname(dev
, dkunit(dev
), slice
, WHOLE_SLICE_PART
, partname
);
1155 lp1
= clone_label(info
, sp
);
1156 msg
= readdisklabel(dev
, lp1
);
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 || !(ssp
->dss_slices
[slice
].ds_openmask
& (1 << 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
];
1327 struct diskslice
*sp2
;
1329 if (slice
== COMPATIBILITY_SLICE
)
1330 sp2
= &ssp
->dss_slices
[ssp
->dss_first_bsd_slice
];
1331 else if (slice
== ssp
->dss_first_bsd_slice
)
1332 sp2
= &ssp
->dss_slices
[COMPATIBILITY_SLICE
];
1340 * If the slice is not the whole-disk slice, setup the reserved
1343 * The reserved area for the original bsd disklabel, inclusive of
1344 * the label and space for boot2, is 15 sectors. If you've
1345 * noticed people traditionally skipping 16 sectors its because
1346 * the sector numbers start at the beginning of the slice rather
1347 * then the beginning of the disklabel and traditional dos slices
1348 * reserve a sector at the beginning for the boot code.
1350 * NOTE! With the traditional bsdlabel, the first N bytes of boot2
1351 * overlap with the disklabel. The disklabel program checks that
1354 * When clearing a label, the bsdlabel reserved area is reset.
1356 if (slice
!= WHOLE_DISK_SLICE
) {
1359 * leave room for the disklabel and boot2 -
1360 * traditional label only. XXX bad hack. Such
1361 * labels cannot install a boot area due to
1362 * insufficient space.
1364 int lsects
= SBSIZE
/ ssp
->dss_secsize
-
1365 sp1
->ds_skip_platform
;
1368 sp1
->ds_skip_bsdlabel
= sp1
->ds_skip_platform
+ lsects
;
1370 sp2
->ds_skip_bsdlabel
= sp1
->ds_skip_bsdlabel
;
1372 sp1
->ds_skip_bsdlabel
= sp1
->ds_skip_platform
;
1374 sp2
->ds_skip_bsdlabel
= sp1
->ds_skip_platform
;
1380 set_ds_wlabel(struct diskslices
*ssp
, int slice
, int wlabel
)
1382 ssp
->dss_slices
[slice
].ds_wlabel
= wlabel
;
1383 if (slice
== COMPATIBILITY_SLICE
)
1384 ssp
->dss_slices
[ssp
->dss_first_bsd_slice
].ds_wlabel
= wlabel
;
1385 else if (slice
== ssp
->dss_first_bsd_slice
)
1386 ssp
->dss_slices
[COMPATIBILITY_SLICE
].ds_wlabel
= wlabel
;