2 * Copyright (c) 2003-2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * ----------------------------------------------------------------------------
35 * "THE BEER-WARE LICENSE" (Revision 42):
36 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
37 * can do whatever you want with this stuff. If we meet some day, and you think
38 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
39 * ----------------------------------------------------------------------------
41 * Copyright (c) 1982, 1986, 1988, 1993
42 * The Regents of the University of California. All rights reserved.
43 * (c) UNIX System Laboratories, Inc.
44 * All or some portions of this file are derived from material licensed
45 * to the University of California by American Telephone and Telegraph
46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47 * the permission of UNIX System Laboratories, Inc.
49 * Copyright (c) 1994 Bruce D. Evans.
50 * All rights reserved.
52 * Copyright (c) 1990 The Regents of the University of California.
53 * All rights reserved.
55 * This code is derived from software contributed to Berkeley by
58 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
59 * All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * 2. Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * 3. Neither the name of the University nor the names of its contributors
70 * may be used to endorse or promote products derived from this software
71 * without specific prior written permission.
73 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
74 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
77 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85 * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
86 * $FreeBSD: src/sys/kern/subr_disk.c,v 1.20.2.6 2001/10/05 07:14:57 peter Exp $
87 * $FreeBSD: src/sys/ufs/ufs/ufs_disksubr.c,v 1.44.2.3 2001/03/05 05:42:19 obrien Exp $
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/kernel.h>
94 #include <sys/sysctl.h>
97 #include <sys/disklabel.h>
98 #include <sys/disklabel32.h>
99 #include <sys/diskslice.h>
100 #include <sys/disk.h>
101 #include <sys/dtype.h> /* DTYPE_* constants */
102 #include <machine/md_var.h>
103 #include <sys/ctype.h>
104 #include <sys/syslog.h>
105 #include <sys/device.h>
106 #include <sys/msgport.h>
107 #include <sys/msgport2.h>
108 #include <sys/buf2.h>
110 #include <vfs/ufs/dinode.h> /* XXX used only for fs.h */
111 #include <vfs/ufs/fs.h> /* XXX used only to get BBSIZE/SBSIZE */
113 static void partition_info(const char *sname, int part, struct partition32 *pp);
114 static void slice_info(const char *sname, struct diskslice *sp);
115 static const char *l32_fixlabel(const char *sname, struct diskslice *sp,
116 disklabel_t lpx, int writeflag);
119 * Retrieve the partition start and extent, in blocks. Return 0 on success,
123 l32_getpartbounds(struct diskslices *ssp, disklabel_t lp, u_int32_t part,
124 u_int64_t *start, u_int64_t *blocks)
126 struct partition32 *pp;
128 if (part >= lp.lab32->d_npartitions)
130 pp = &lp.lab32->d_partitions[part];
131 *start = pp->p_offset;
132 *blocks = pp->p_size;
137 l32_loadpartinfo(disklabel_t lp, u_int32_t part, struct partinfo *dpart)
139 struct partition32 *pp;
140 const size_t uuid_size = sizeof(struct uuid);
142 bzero(&dpart->fstype_uuid, uuid_size);
143 bzero(&dpart->storage_uuid, uuid_size);
144 if (part < lp.lab32->d_npartitions) {
145 pp = &lp.lab32->d_partitions[part];
146 dpart->fstype = pp->p_fstype;
153 l32_getnumparts(disklabel_t lp)
155 return(lp.lab32->d_npartitions);
159 l32_getpackname(disklabel_t lp, char *buf, size_t bytes)
163 if (lp.lab32->d_packname[0] == 0) {
167 slen = strnlen(lp.lab32->d_packname, sizeof(lp.lab32->d_packname));
170 bcopy(lp.lab32->d_packname, buf, slen);
177 l32_freedisklabel(disklabel_t *lpp)
179 kfree((*lpp).lab32, M_DEVBUF);
184 * Attempt to read a disk label from a device.
186 * Returns NULL on sucess, and an error string on failure
189 l32_readdisklabel(cdev_t dev, struct diskslice *sp, disklabel_t *lpp,
190 struct disk_info *info)
194 struct disklabel32 *dlp;
195 const char *msg = NULL;
196 int secsize = info->d_media_blksize;
198 bp = getpbuf_mem(NULL);
199 KKASSERT(secsize <= bp->b_bufsize);
200 bp->b_bio1.bio_offset = (off_t)LABELSECTOR32 * secsize;
201 bp->b_bio1.bio_done = biodone_sync;
202 bp->b_bio1.bio_flags |= BIO_SYNC;
203 bp->b_bcount = secsize;
204 bp->b_flags &= ~B_INVAL;
205 bp->b_cmd = BUF_CMD_READ;
206 bp->b_flags |= B_FAILONDIS;
207 dev_dstrategy(dev, &bp->b_bio1);
208 if (biowait(&bp->b_bio1, "labrd"))
210 else for (dlp = (struct disklabel32 *)bp->b_data;
211 dlp <= (struct disklabel32 *)((char *)bp->b_data +
212 secsize - sizeof(*dlp));
213 dlp = (struct disklabel32 *)((char *)dlp + sizeof(long))) {
214 if (dlp->d_magic != DISKMAGIC32 ||
215 dlp->d_magic2 != DISKMAGIC32) {
217 * NOTE! dsreadandsetlabel() does a strcmp() on
221 msg = "no disk label";
222 } else if (dlp->d_npartitions > MAXPARTITIONS32 ||
223 dkcksum32(dlp) != 0) {
224 msg = "disk label corrupted";
227 msg = l32_fixlabel(NULL, sp, lpx, FALSE);
229 (*lpp).lab32 = kmalloc(sizeof(*dlp),
230 M_DEVBUF, M_WAITOK|M_ZERO);
231 *(*lpp).lab32 = *dlp;
236 bp->b_flags |= B_INVAL | B_AGE;
243 * Check new disk label for sensibility before setting it.
246 l32_setdisklabel(disklabel_t olpx, disklabel_t nlpx, struct diskslices *ssp,
247 struct diskslice *sp, u_int32_t *openmask)
249 struct disklabel32 *olp, *nlp;
250 struct partition32 *opp, *npp;
258 * Check it is actually a disklabel we are looking at.
260 if (nlp->d_magic != DISKMAGIC32 || nlp->d_magic2 != DISKMAGIC32 ||
265 * For each partition that we think is open, check the new disklabel
266 * for compatibility. Ignore special partitions (>= 128).
270 if (openmask[i >> 5] == 0) {
274 if ((openmask[i >> 5] & (1 << (i & 31))) == 0) {
278 if (nlp->d_npartitions <= i)
280 opp = &olp->d_partitions[i];
281 npp = &nlp->d_partitions[i];
282 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
285 * Copy internally-set partition information
286 * if new label doesn't include it. XXX
287 * (If we are using it then we had better stay the same type)
288 * This is possibly dubious, as someone else noted (XXX)
290 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
291 npp->p_fstype = opp->p_fstype;
292 npp->p_fsize = opp->p_fsize;
293 npp->p_frag = opp->p_frag;
294 npp->p_cpg = opp->p_cpg;
299 nlp->d_checksum = dkcksum32(nlp);
302 if (olp->d_partitions[RAW_PART].p_offset)
304 if (olp->d_secperunit > sp->ds_size)
306 for (part = 0; part < olp->d_npartitions; ++part) {
307 if (olp->d_partitions[part].p_size > sp->ds_size)
314 * Write disk label back to device after modification.
317 l32_writedisklabel(cdev_t dev, struct diskslices *ssp, struct diskslice *sp,
320 struct disklabel32 *lp;
321 struct disklabel32 *dlp;
328 if (lp->d_partitions[RAW_PART].p_offset != 0)
329 return (EXDEV); /* not quite right */
331 bp = getpbuf_mem(NULL);
332 KKASSERT((int)lp->d_secsize <= bp->b_bufsize);
333 bp->b_bio1.bio_offset = (off_t)LABELSECTOR32 * lp->d_secsize;
334 bp->b_bio1.bio_done = biodone_sync;
335 bp->b_bio1.bio_flags |= BIO_SYNC;
336 bp->b_bcount = lp->d_secsize;
337 bp->b_flags |= B_FAILONDIS;
341 * We read the label first to see if it's there,
342 * in which case we will put ours at the same offset into the block..
343 * (I think this is stupid [Julian])
344 * Note that you can't write a label out over a corrupted label!
345 * (also stupid.. how do you write the first one? by raw writes?)
347 bp->b_flags &= ~B_INVAL;
348 bp->b_cmd = BUF_CMD_READ;
349 KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
350 dev_dstrategy(dev, &bp->b_bio1);
351 error = biowait(&bp->b_bio1, "labrd");
354 for (dlp = (struct disklabel32 *)bp->b_data;
355 dlp <= (struct disklabel32 *)
356 ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
357 dlp = (struct disklabel32 *)((char *)dlp + sizeof(long))) {
358 if (dlp->d_magic == DISKMAGIC32 &&
359 dlp->d_magic2 == DISKMAGIC32 && dkcksum32(dlp) == 0) {
362 msg = l32_fixlabel(NULL, sp, lpx, TRUE);
366 bp->b_cmd = BUF_CMD_WRITE;
367 bp->b_bio1.bio_done = biodone_sync;
368 bp->b_bio1.bio_flags |= BIO_SYNC;
369 KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
370 dev_dstrategy(dev, &bp->b_bio1);
371 error = biowait(&bp->b_bio1, "labwr");
379 bzero(bp->b_data, lp->d_secsize);
380 dlp = (struct disklabel32 *)bp->b_data;
382 bp->b_flags &= ~B_INVAL;
383 bp->b_cmd = BUF_CMD_WRITE;
384 bp->b_bio1.bio_done = biodone_sync;
385 bp->b_bio1.bio_flags |= BIO_SYNC;
387 error = biowait(&bp->b_bio1, "labwr");
389 bp->b_flags |= B_INVAL | B_AGE;
396 * Create a disklabel based on a disk_info structure, initializing
397 * the appropriate fields and creating a raw partition that covers the
400 * If a diskslice is passed, the label is truncated to the slice
403 l32_clone_label(struct disk_info *info, struct diskslice *sp)
405 struct disklabel32 *lp;
408 lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK | M_ZERO);
409 lp->d_nsectors = info->d_secpertrack;
410 lp->d_ntracks = info->d_nheads;
411 lp->d_secpercyl = info->d_secpercyl;
412 lp->d_secsize = info->d_media_blksize;
415 lp->d_secperunit = (u_int)sp->ds_size;
417 lp->d_secperunit = (u_int)info->d_media_blocks;
419 if (lp->d_typename[0] == '\0')
420 strncpy(lp->d_typename, "amnesiac", sizeof(lp->d_typename));
421 if (lp->d_packname[0] == '\0')
422 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
423 if (lp->d_nsectors == 0)
425 if (lp->d_ntracks == 0)
427 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
428 lp->d_ncylinders = lp->d_secperunit / lp->d_secpercyl;
431 if (lp->d_interleave == 0)
432 lp->d_interleave = 1;
433 if (lp->d_npartitions < RAW_PART + 1)
434 lp->d_npartitions = MAXPARTITIONS32;
435 if (lp->d_bbsize == 0)
436 lp->d_bbsize = BBSIZE;
437 if (lp->d_sbsize == 0)
438 lp->d_sbsize = SBSIZE;
441 * Used by various devices to create a compatibility slice which
442 * allows us to mount root from devices which do not have a
443 * disklabel. Particularly: CDs.
445 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
446 if (info->d_dsflags & DSO_COMPATPARTA) {
447 lp->d_partitions[0].p_size = lp->d_secperunit;
448 lp->d_partitions[0].p_fstype = FS_OTHER;
450 lp->d_magic = DISKMAGIC32;
451 lp->d_magic2 = DISKMAGIC32;
452 lp->d_checksum = dkcksum32(lp);
458 l32_makevirginlabel(disklabel_t lpx, struct diskslices *ssp,
459 struct diskslice *sp, struct disk_info *info)
461 struct disklabel32 *lp = lpx.lab32;
462 struct partition32 *pp;
463 disklabel_t template;
465 template = l32_clone_label(info, NULL);
466 bcopy(template.opaque, lp, sizeof(struct disklabel32));
468 lp->d_magic = DISKMAGIC32;
469 lp->d_magic2 = DISKMAGIC32;
471 lp->d_npartitions = MAXPARTITIONS32;
472 if (lp->d_interleave == 0)
473 lp->d_interleave = 1;
476 if (lp->d_nsectors == 0) /* sectors per track */
478 if (lp->d_ntracks == 0) /* heads */
480 lp->d_ncylinders = 0;
481 lp->d_bbsize = BBSIZE;
482 lp->d_sbsize = SBSIZE;
485 * If the slice or GPT partition is really small we could
486 * wind up with an absurd calculation for ncylinders.
488 while (lp->d_ncylinders < 4) {
489 if (lp->d_ntracks > 1)
491 else if (lp->d_nsectors > 1)
492 lp->d_nsectors >>= 1;
495 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
496 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
500 * Set or Modify the partition sizes to accomodate the slice,
501 * since we started with a copy of the virgin label stored
502 * in the whole-disk-slice and we are probably not a
505 lp->d_secperunit = sp->ds_size;
506 pp = &lp->d_partitions[RAW_PART];
508 pp->p_size = lp->d_secperunit;
509 if (info->d_dsflags & DSO_COMPATPARTA) {
510 pp = &lp->d_partitions[0];
512 pp->p_size = lp->d_secperunit;
513 pp->p_fstype = FS_OTHER;
516 lp->d_checksum = dkcksum32(lp);
518 kfree(template.opaque, M_DEVBUF);
522 l32_fixlabel(const char *sname, struct diskslice *sp,
523 disklabel_t lpx, int writeflag)
525 struct disklabel32 *lp;
526 struct partition32 *pp;
535 /* These errors "can't happen" so don't bother reporting details. */
536 if (lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32)
537 return ("fixlabel: invalid magic");
538 if (dkcksum32(lp) != 0)
539 return ("fixlabel: invalid checksum");
541 pp = &lp->d_partitions[RAW_PART];
544 * What a mess. For ages old backwards compatibility the disklabel
545 * on-disk stores absolute offsets instead of slice-relative offsets.
546 * So fix it up when reading, writing, or snooping.
548 * The in-core label is always slice-relative.
552 offset = sp->ds_offset;
554 start = sp->ds_offset;
555 offset = -sp->ds_offset;
557 if (pp->p_offset != start) {
560 "%s: rejecting BSD label: raw partition offset != slice offset\n",
562 slice_info(sname, sp);
563 partition_info(sname, RAW_PART, pp);
565 return ("fixlabel: raw partition offset != slice offset");
567 if (pp->p_size != sp->ds_size) {
569 kprintf("%s: raw partition size != slice size\n", sname);
570 slice_info(sname, sp);
571 partition_info(sname, RAW_PART, pp);
573 if (pp->p_size > sp->ds_size) {
575 return ("fixlabel: raw partition size > slice size");
576 kprintf("%s: truncating raw partition\n", sname);
577 pp->p_size = sp->ds_size;
580 end = start + sp->ds_size;
582 return ("fixlabel: slice wraps");
583 if (lp->d_secpercyl <= 0)
584 return ("fixlabel: d_secpercyl <= 0");
587 for (part = 0; part < lp->d_npartitions; part++, pp++) {
588 if (pp->p_offset != 0 || pp->p_size != 0) {
589 if (pp->p_offset < start
590 || pp->p_offset + pp->p_size > end
591 || pp->p_offset + pp->p_size < pp->p_offset) {
594 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
597 slice_info(sname, sp);
600 partition_info(sname, part, pp);
602 /* XXX else silently discard junk. */
603 bzero(pp, sizeof *pp);
605 pp->p_offset += offset;
609 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
610 lp->d_secperunit = sp->ds_size;
612 lp->d_checksum = dkcksum32(lp);
617 * Set the number of blocks at the beginning of the slice which have
618 * been reserved for label operations. This area will be write-protected
619 * when accessed via the slice.
622 l32_adjust_label_reserved(struct diskslices *ssp, int slice,
623 struct diskslice *sp)
625 /*struct disklabel32 *lp = sp->ds_label.lab32;*/
626 sp->ds_reserved = SBSIZE / ssp->dss_secsize;
630 partition_info(const char *sname, int part, struct partition32 *pp)
632 kprintf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
633 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
638 slice_info(const char *sname, struct diskslice *sp)
640 kprintf("%s: start %llu, end %llu, size %llu\n", sname,
641 (long long)sp->ds_offset,
642 (long long)sp->ds_offset + sp->ds_size - 1,
643 (long long)sp->ds_size);
646 struct disklabel_ops disklabel32_ops = {
647 .labelsize = sizeof(struct disklabel32),
648 .op_readdisklabel = l32_readdisklabel,
649 .op_setdisklabel = l32_setdisklabel,
650 .op_writedisklabel = l32_writedisklabel,
651 .op_clone_label = l32_clone_label,
652 .op_adjust_label_reserved = l32_adjust_label_reserved,
653 .op_getpartbounds = l32_getpartbounds,
654 .op_loadpartinfo = l32_loadpartinfo,
655 .op_getnumparts = l32_getnumparts,
656 .op_makevirginlabel = l32_makevirginlabel,
657 .op_getpackname = l32_getpackname,
658 .op_freedisklabel = l32_freedisklabel