Home | History | Annotate | Line # | Download | only in kern
subr_disk.c revision 1.108
      1 /*	$NetBSD: subr_disk.c,v 1.108 2014/12/31 19:52:06 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997, 1999, 2000, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1988, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  * (c) UNIX System Laboratories, Inc.
     37  * All or some portions of this file are derived from material licensed
     38  * to the University of California by American Telephone and Telegraph
     39  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40  * the permission of UNIX System Laboratories, Inc.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.108 2014/12/31 19:52:06 christos Exp $");
     71 
     72 #include <sys/param.h>
     73 #include <sys/kernel.h>
     74 #include <sys/kmem.h>
     75 #include <sys/buf.h>
     76 #include <sys/fcntl.h>
     77 #include <sys/syslog.h>
     78 #include <sys/disklabel.h>
     79 #include <sys/disk.h>
     80 #include <sys/sysctl.h>
     81 #include <lib/libkern/libkern.h>
     82 
     83 /*
     84  * Compute checksum for disk label.
     85  */
     86 u_int
     87 dkcksum(struct disklabel *lp)
     88 {
     89 
     90 	return dkcksum_sized(lp, lp->d_npartitions);
     91 }
     92 
     93 u_int
     94 dkcksum_sized(struct disklabel *lp, size_t npartitions)
     95 {
     96 	uint16_t *start, *end;
     97 	uint16_t sum = 0;
     98 
     99 	start = (uint16_t *)lp;
    100 	end = (uint16_t *)&lp->d_partitions[npartitions];
    101 	while (start < end)
    102 		sum ^= *start++;
    103 	return sum;
    104 }
    105 
    106 /*
    107  * Disk error is the preface to plaintive error messages
    108  * about failing disk transfers.  It prints messages of the form
    109 
    110 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
    111 
    112  * if the offset of the error in the transfer and a disk label
    113  * are both available.  blkdone should be -1 if the position of the error
    114  * is unknown; the disklabel pointer may be null from drivers that have not
    115  * been converted to use them.  The message is printed with printf
    116  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
    117  * The message should be completed (with at least a newline) with printf
    118  * or addlog, respectively.  There is no trailing space.
    119  */
    120 #ifndef PRIdaddr
    121 #define PRIdaddr PRId64
    122 #endif
    123 void
    124 diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
    125     int blkdone, const struct disklabel *lp)
    126 {
    127 	int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
    128 	void (*pr)(const char *, ...) __printflike(1, 2);
    129 	char partname = 'a' + part;
    130 	daddr_t sn;
    131 
    132 	if (/*CONSTCOND*/0)
    133 		/* Compiler will error this is the format is wrong... */
    134 		printf("%" PRIdaddr, bp->b_blkno);
    135 
    136 	if (pri != LOG_PRINTF) {
    137 		static const char fmt[] = "";
    138 		log(pri, fmt);
    139 		pr = addlog;
    140 	} else
    141 		pr = printf;
    142 	(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
    143 	    bp->b_flags & B_READ ? "read" : "writ");
    144 	sn = bp->b_blkno;
    145 	if (bp->b_bcount <= DEV_BSIZE)
    146 		(*pr)("%" PRIdaddr, sn);
    147 	else {
    148 		if (blkdone >= 0) {
    149 			sn += blkdone;
    150 			(*pr)("%" PRIdaddr " of ", sn);
    151 		}
    152 		(*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
    153 		    bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
    154 	}
    155 	if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
    156 		sn += lp->d_partitions[part].p_offset;
    157 		(*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
    158 		    dname, unit, sn, sn / lp->d_secpercyl);
    159 		sn %= lp->d_secpercyl;
    160 		(*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
    161 		    sn / lp->d_nsectors, sn % lp->d_nsectors);
    162 	}
    163 }
    164 
    165 /*
    166  * Searches the iostatlist for the disk corresponding to the
    167  * name provided.
    168  */
    169 struct disk *
    170 disk_find(const char *name)
    171 {
    172 	struct io_stats *stat;
    173 
    174 	stat = iostat_find(name);
    175 
    176 	if ((stat != NULL) && (stat->io_type == IOSTAT_DISK))
    177 		return stat->io_parent;
    178 
    179 	return (NULL);
    180 }
    181 
    182 void
    183 disk_init(struct disk *diskp, const char *name, const struct dkdriver *driver)
    184 {
    185 	u_int blocksize = DEV_BSIZE;
    186 
    187 	/*
    188 	 * Initialize the wedge-related locks and other fields.
    189 	 */
    190 	mutex_init(&diskp->dk_rawlock, MUTEX_DEFAULT, IPL_NONE);
    191 	mutex_init(&diskp->dk_openlock, MUTEX_DEFAULT, IPL_NONE);
    192 	LIST_INIT(&diskp->dk_wedges);
    193 	diskp->dk_nwedges = 0;
    194 	diskp->dk_labelsector = LABELSECTOR;
    195 	diskp->dk_blkshift = DK_BSIZE2BLKSHIFT(blocksize);
    196 	diskp->dk_byteshift = DK_BSIZE2BYTESHIFT(blocksize);
    197 	diskp->dk_name = name;
    198 	diskp->dk_driver = driver;
    199 }
    200 
    201 /*
    202  * Attach a disk.
    203  */
    204 void
    205 disk_attach(struct disk *diskp)
    206 {
    207 
    208 	/*
    209 	 * Allocate and initialize the disklabel structures.
    210 	 */
    211 	diskp->dk_label = kmem_zalloc(sizeof(struct disklabel), KM_SLEEP);
    212 	diskp->dk_cpulabel = kmem_zalloc(sizeof(struct cpu_disklabel),
    213 	    KM_SLEEP);
    214 	if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
    215 		panic("disk_attach: can't allocate storage for disklabel");
    216 
    217 	/*
    218 	 * Set up the stats collection.
    219 	 */
    220 	diskp->dk_stats = iostat_alloc(IOSTAT_DISK, diskp, diskp->dk_name);
    221 }
    222 
    223 int
    224 disk_begindetach(struct disk *dk, int (*lastclose)(device_t),
    225     device_t self, int flags)
    226 {
    227 	int rc;
    228 
    229 	rc = 0;
    230 	mutex_enter(&dk->dk_openlock);
    231 	if (dk->dk_openmask == 0)
    232 		;	/* nothing to do */
    233 	else if ((flags & DETACH_FORCE) == 0)
    234 		rc = EBUSY;
    235 	else if (lastclose != NULL)
    236 		rc = (*lastclose)(self);
    237 	mutex_exit(&dk->dk_openlock);
    238 
    239 	return rc;
    240 }
    241 
    242 /*
    243  * Detach a disk.
    244  */
    245 void
    246 disk_detach(struct disk *diskp)
    247 {
    248 
    249 	/*
    250 	 * Remove from the drivelist.
    251 	 */
    252 	iostat_free(diskp->dk_stats);
    253 
    254 	/*
    255 	 * Release the disk-info dictionary.
    256 	 */
    257 	if (diskp->dk_info) {
    258 		prop_object_release(diskp->dk_info);
    259 		diskp->dk_info = NULL;
    260 	}
    261 
    262 	/*
    263 	 * Free the space used by the disklabel structures.
    264 	 */
    265 	kmem_free(diskp->dk_label, sizeof(*diskp->dk_label));
    266 	kmem_free(diskp->dk_cpulabel, sizeof(*diskp->dk_cpulabel));
    267 }
    268 
    269 void
    270 disk_destroy(struct disk *diskp)
    271 {
    272 
    273 	mutex_destroy(&diskp->dk_openlock);
    274 	mutex_destroy(&diskp->dk_rawlock);
    275 }
    276 
    277 /*
    278  * Mark the disk as busy for metrics collection.
    279  */
    280 void
    281 disk_busy(struct disk *diskp)
    282 {
    283 
    284 	iostat_busy(diskp->dk_stats);
    285 }
    286 
    287 /*
    288  * Finished disk operations, gather metrics.
    289  */
    290 void
    291 disk_unbusy(struct disk *diskp, long bcount, int read)
    292 {
    293 
    294 	iostat_unbusy(diskp->dk_stats, bcount, read);
    295 }
    296 
    297 /*
    298  * Return true if disk has an I/O operation in flight.
    299  */
    300 bool
    301 disk_isbusy(struct disk *diskp)
    302 {
    303 
    304 	return iostat_isbusy(diskp->dk_stats);
    305 }
    306 
    307 /*
    308  * Set the physical blocksize of a disk, in bytes.
    309  * Only necessary if blocksize != DEV_BSIZE.
    310  */
    311 void
    312 disk_blocksize(struct disk *diskp, int blocksize)
    313 {
    314 
    315 	diskp->dk_blkshift = DK_BSIZE2BLKSHIFT(blocksize);
    316 	diskp->dk_byteshift = DK_BSIZE2BYTESHIFT(blocksize);
    317 	diskp->dk_geom.dg_secsize = DEV_BSIZE << diskp->dk_blkshift;
    318 }
    319 
    320 /*
    321  * Bounds checking against the media size, used for the raw partition.
    322  * secsize, mediasize and b_blkno must all be the same units.
    323  * Possibly this has to be DEV_BSIZE (512).
    324  */
    325 int
    326 bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
    327 {
    328 	int64_t sz;
    329 
    330 	sz = howmany(bp->b_bcount, secsize);
    331 
    332 	if (bp->b_blkno + sz > mediasize) {
    333 		sz = mediasize - bp->b_blkno;
    334 		if (sz == 0) {
    335 			/* If exactly at end of disk, return EOF. */
    336 			bp->b_resid = bp->b_bcount;
    337 			return 0;
    338 		}
    339 		if (sz < 0) {
    340 			/* If past end of disk, return EINVAL. */
    341 			bp->b_error = EINVAL;
    342 			return 0;
    343 		}
    344 		/* Otherwise, truncate request. */
    345 		bp->b_bcount = sz * secsize;
    346 	}
    347 
    348 	return 1;
    349 }
    350 
    351 /*
    352  * Determine the size of the transfer, and make sure it is
    353  * within the boundaries of the partition. Adjust transfer
    354  * if needed, and signal errors or early completion.
    355  */
    356 int
    357 bounds_check_with_label(struct disk *dk, struct buf *bp, int wlabel)
    358 {
    359 	struct disklabel *lp = dk->dk_label;
    360 	struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
    361 	uint64_t p_size, p_offset, labelsector;
    362 	int64_t sz;
    363 
    364 	/* Protect against division by zero. XXX: Should never happen?!?! */
    365 	if (lp->d_secpercyl == 0) {
    366 		bp->b_error = EINVAL;
    367 		return -1;
    368 	}
    369 
    370 	p_size = (uint64_t)p->p_size << dk->dk_blkshift;
    371 	p_offset = (uint64_t)p->p_offset << dk->dk_blkshift;
    372 #if RAW_PART == 3
    373 	labelsector = lp->d_partitions[2].p_offset;
    374 #else
    375 	labelsector = lp->d_partitions[RAW_PART].p_offset;
    376 #endif
    377 	labelsector = (labelsector + dk->dk_labelsector) << dk->dk_blkshift;
    378 
    379 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    380 	if ((bp->b_blkno + sz) > p_size) {
    381 		sz = p_size - bp->b_blkno;
    382 		if (sz == 0) {
    383 			/* If exactly at end of disk, return EOF. */
    384 			bp->b_resid = bp->b_bcount;
    385 			return 0;
    386 		}
    387 		if (sz < 0) {
    388 			/* If past end of disk, return EINVAL. */
    389 			bp->b_error = EINVAL;
    390 			return -1;
    391 		}
    392 		/* Otherwise, truncate request. */
    393 		bp->b_bcount = sz << DEV_BSHIFT;
    394 	}
    395 
    396 	/* Overwriting disk label? */
    397 	if (bp->b_blkno + p_offset <= labelsector &&
    398 	    bp->b_blkno + p_offset + sz > labelsector &&
    399 	    (bp->b_flags & B_READ) == 0 && !wlabel) {
    400 		bp->b_error = EROFS;
    401 		return -1;
    402 	}
    403 
    404 	/* calculate cylinder for disksort to order transfers with */
    405 	bp->b_cylinder = (bp->b_blkno + p->p_offset) /
    406 	    (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
    407 	return 1;
    408 }
    409 
    410 int
    411 disk_read_sectors(void (*strat)(struct buf *), const struct disklabel *lp,
    412     struct buf *bp, unsigned int sector, int count)
    413 {
    414 	bp->b_blkno = sector;
    415 	bp->b_bcount = count * lp->d_secsize;
    416 	bp->b_flags = (bp->b_flags & ~B_WRITE) | B_READ;
    417 	bp->b_oflags &= ~BO_DONE;
    418 	bp->b_cylinder = sector / lp->d_secpercyl;
    419 	(*strat)(bp);
    420 	return biowait(bp);
    421 }
    422 
    423 const char *
    424 convertdisklabel(struct disklabel *lp, void (*strat)(struct buf *),
    425     struct buf *bp, uint32_t secperunit)
    426 {
    427 	struct partition rp, *altp, *p;
    428 	int geom_ok;
    429 
    430 	memset(&rp, 0, sizeof(rp));
    431 	rp.p_size = secperunit;
    432 	rp.p_fstype = FS_UNUSED;
    433 
    434 	/* If we can seek to d_secperunit - 1, believe the disk geometry. */
    435 	if (secperunit != 0 &&
    436 	    disk_read_sectors(strat, lp, bp, secperunit - 1, 1) == 0)
    437 		geom_ok = 1;
    438 	else
    439 		geom_ok = 0;
    440 
    441 #if 0
    442 	printf("%s: secperunit (%" PRIu32 ") %s\n", __func__,
    443 	    secperunit, geom_ok ? "ok" : "not ok");
    444 #endif
    445 
    446 	p = &lp->d_partitions[RAW_PART];
    447 	if (RAW_PART == 'c' - 'a')
    448 		altp = &lp->d_partitions['d' - 'a'];
    449 	else
    450 		altp = &lp->d_partitions['c' - 'a'];
    451 
    452 	if (lp->d_npartitions > RAW_PART && p->p_offset == 0 && p->p_size != 0)
    453 		;	/* already a raw partition */
    454 	else if (lp->d_npartitions > MAX('c', 'd') - 'a' &&
    455 		 altp->p_offset == 0 && altp->p_size != 0) {
    456 		/* alternate partition ('c' or 'd') is suitable for raw slot,
    457 		 * swap with 'd' or 'c'.
    458 		 */
    459 		rp = *p;
    460 		*p = *altp;
    461 		*altp = rp;
    462 	} else if (lp->d_npartitions <= RAW_PART &&
    463 	           lp->d_npartitions > 'c' - 'a') {
    464 		/* No raw partition is present, but the alternate is present.
    465 		 * Copy alternate to raw partition.
    466 		 */
    467 		lp->d_npartitions = RAW_PART + 1;
    468 		*p = *altp;
    469 	} else if (!geom_ok)
    470 		return "no raw partition and disk reports bad geometry";
    471 	else if (lp->d_npartitions <= RAW_PART) {
    472 		memset(&lp->d_partitions[lp->d_npartitions], 0,
    473 		    sizeof(struct partition) * (RAW_PART - lp->d_npartitions));
    474 		*p = rp;
    475 		lp->d_npartitions = RAW_PART + 1;
    476 	} else if (lp->d_npartitions < MAXPARTITIONS) {
    477 		memmove(p + 1, p,
    478 		    sizeof(struct partition) * (lp->d_npartitions - RAW_PART));
    479 		*p = rp;
    480 		lp->d_npartitions++;
    481 	} else
    482 		return "no raw partition and partition table is full";
    483 	return NULL;
    484 }
    485 
    486 /*
    487  * disk_ioctl --
    488  *	Generic disk ioctl handling.
    489  */
    490 int
    491 disk_ioctl(struct disk *dk, dev_t dev, u_long cmd, void *data, int flag,
    492     struct lwp *l)
    493 {
    494 	struct dkwedge_info *dkw;
    495 	struct partinfo *pt;
    496 #ifdef __HAVE_OLD_DISKLABEL
    497 	struct disklabel newlabel;
    498 #endif
    499 
    500 	switch (cmd) {
    501 	case DIOCGDINFO:
    502 		if (dev == 0)
    503 			return EPASSTHROUGH;
    504 		memcpy(data, dk->dk_label, sizeof (*dk->dk_label));
    505 		return 0;
    506 
    507 #ifdef __HAVE_OLD_DISKLABEL
    508 	case ODIOCGDINFO:
    509 		if (dev == 0)
    510 			return EPASSTHROUGH;
    511 		memcpy(&newlabel, dk->dk_label, sizeof(newlabel));
    512 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    513 			return ENOTTY;
    514 		memcpy(data, &newlabel, sizeof(struct olddisklabel));
    515 		return 0;
    516 #endif
    517 
    518 	case DIOCGPART:
    519 		if (dev == 0)
    520 			return EPASSTHROUGH;
    521 		pt = data;
    522 		pt->disklab = dk->dk_label;
    523 		pt->part = &dk->dk_label->d_partitions[DISKPART(dev)];
    524 		return 0;
    525 
    526 	case DIOCAWEDGE:
    527 		if ((flag & FWRITE) == 0)
    528 			return EBADF;
    529 
    530 		dkw = data;
    531 		strlcpy(dkw->dkw_parent, dk->dk_name, sizeof(dkw->dkw_parent));
    532 		return dkwedge_add(dkw);
    533 
    534 	case DIOCDWEDGE:
    535 		if ((flag & FWRITE) == 0)
    536 			return EBADF;
    537 
    538 		dkw = data;
    539 		strlcpy(dkw->dkw_parent, dk->dk_name, sizeof(dkw->dkw_parent));
    540 		return dkwedge_del(dkw);
    541 
    542 	case DIOCLWEDGES:
    543 		return dkwedge_list(dk, data, l);
    544 
    545 	case DIOCMWEDGES:
    546 		if ((flag & FWRITE) == 0)
    547 			return EBADF;
    548 
    549 		dkwedge_discover(dk);
    550 		return 0;
    551 
    552 	case DIOCGDISKINFO:
    553 		if (dk->dk_info == NULL)
    554 			return ENOTSUP;
    555 		return prop_dictionary_copyout_ioctl(data, cmd, dk->dk_info);
    556 
    557 	case DIOCGSECTORSIZE:
    558 		*(u_int *)data = dk->dk_geom.dg_secsize;
    559 		return 0;
    560 
    561 	case DIOCGMEDIASIZE:
    562 		*(off_t *)data = (off_t)dk->dk_geom.dg_secsize *
    563 		    dk->dk_geom.dg_secperunit;
    564 		return 0;
    565 
    566 	default:
    567 		return EPASSTHROUGH;
    568 	}
    569 }
    570 
    571 void
    572 disk_set_info(device_t dev, struct disk *dk, const char *type)
    573 {
    574 	struct disk_geom *dg = &dk->dk_geom;
    575 
    576 	if (dg->dg_secperunit == 0 && dg->dg_ncylinders == 0) {
    577 #ifdef DIAGNOSTIC
    578 		printf("%s: secperunit and ncylinders are zero\n", dk->dk_name);
    579 #endif
    580 		return;
    581 	}
    582 
    583 	if (dg->dg_secperunit == 0) {
    584 		if (dg->dg_nsectors == 0 || dg->dg_ntracks == 0) {
    585 #ifdef DIAGNOSTIC
    586 			printf("%s: secperunit and (sectors or tracks) "
    587 			    "are zero\n", dk->dk_name);
    588 #endif
    589 			return;
    590 		}
    591 		dg->dg_secperunit = (int64_t) dg->dg_nsectors *
    592 		    dg->dg_ntracks * dg->dg_ncylinders;
    593 	}
    594 
    595 	if (dg->dg_ncylinders == 0) {
    596 		if (dg->dg_ntracks && dg->dg_nsectors)
    597 			dg->dg_ncylinders = dg->dg_secperunit /
    598 			    (dg->dg_ntracks * dg->dg_nsectors);
    599 	}
    600 
    601 	if (dg->dg_secsize == 0) {
    602 #ifdef DIAGNOSTIC
    603 		printf("%s: fixing 0 sector size\n", dk->dk_name);
    604 #endif
    605 		dg->dg_secsize = DEV_BSIZE;
    606 	}
    607 
    608 	dk->dk_blkshift = DK_BSIZE2BLKSHIFT(dg->dg_secsize);
    609 	dk->dk_byteshift = DK_BSIZE2BYTESHIFT(dg->dg_secsize);
    610 
    611 	prop_dictionary_t disk_info, odisk_info, geom;
    612 
    613 	disk_info = prop_dictionary_create();
    614 	geom = prop_dictionary_create();
    615 
    616 	prop_dictionary_set_uint64(geom, "sectors-per-unit",
    617 	    dg->dg_secperunit);
    618 
    619 	prop_dictionary_set_uint32(geom, "sector-size", dg->dg_secsize);
    620 
    621 	if (dg->dg_nsectors)
    622 		prop_dictionary_set_uint16(geom, "sectors-per-track",
    623 		    dg->dg_nsectors);
    624 
    625 	if (dg->dg_ntracks)
    626 		prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
    627 		    dg->dg_ntracks);
    628 
    629 	if (dg->dg_ncylinders)
    630 		prop_dictionary_set_uint64(geom, "cylinders-per-unit",
    631 		    dg->dg_ncylinders);
    632 
    633 	prop_dictionary_set(disk_info, "geometry", geom);
    634 
    635 	if (type)
    636 		prop_dictionary_set_cstring_nocopy(disk_info, "type", type);
    637 
    638 	prop_object_release(geom);
    639 
    640 	odisk_info = dk->dk_info;
    641 	dk->dk_info = disk_info;
    642 
    643 	if (dev)
    644 		prop_dictionary_set(device_properties(dev), "disk-info",
    645 		    disk_info);
    646 
    647 	/*
    648 	 * Don't release disk_info here; we keep a reference to it.
    649 	 * disk_detach() will release it when we go away.
    650 	 */
    651 	if (odisk_info)
    652 		prop_object_release(odisk_info);
    653 }
    654