xref: /netbsd-src/sys/kern/subr_disk.c (revision 76c7fc5f6b13ed0b1508e6b313e88e59977ed78e)
1 /*	$NetBSD: subr_disk.c,v 1.129 2019/09/30 23:23:59 cnst 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.129 2019/09/30 23:23:59 cnst 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 if 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  * Rename a disk.
203  */
204 void
205 disk_rename(struct disk *diskp, const char *name)
206 {
207 
208 	diskp->dk_name = name;
209 	iostat_rename(diskp->dk_stats, diskp->dk_name);
210 }
211 
212 /*
213  * Attach a disk.
214  */
215 void
216 disk_attach(struct disk *diskp)
217 {
218 
219 	/*
220 	 * Allocate and initialize the disklabel structures.
221 	 */
222 	diskp->dk_label = kmem_zalloc(sizeof(struct disklabel), KM_SLEEP);
223 	diskp->dk_cpulabel = kmem_zalloc(sizeof(struct cpu_disklabel),
224 	    KM_SLEEP);
225 
226 	/*
227 	 * Set up the stats collection.
228 	 */
229 	diskp->dk_stats = iostat_alloc(IOSTAT_DISK, diskp, diskp->dk_name);
230 }
231 
232 int
233 disk_begindetach(struct disk *dk, int (*lastclose)(device_t),
234     device_t self, int flags)
235 {
236 	int rc;
237 
238 	rc = 0;
239 	mutex_enter(&dk->dk_openlock);
240 	if (dk->dk_openmask == 0)
241 		;	/* nothing to do */
242 	else if ((flags & DETACH_FORCE) == 0)
243 		rc = EBUSY;
244 	else if (lastclose != NULL)
245 		rc = (*lastclose)(self);
246 	mutex_exit(&dk->dk_openlock);
247 
248 	return rc;
249 }
250 
251 /*
252  * Detach a disk.
253  */
254 void
255 disk_detach(struct disk *diskp)
256 {
257 
258 	/*
259 	 * Remove from the drivelist.
260 	 */
261 	iostat_free(diskp->dk_stats);
262 
263 	/*
264 	 * Release the disk-info dictionary.
265 	 */
266 	if (diskp->dk_info) {
267 		prop_object_release(diskp->dk_info);
268 		diskp->dk_info = NULL;
269 	}
270 
271 	/*
272 	 * Free the space used by the disklabel structures.
273 	 */
274 	kmem_free(diskp->dk_label, sizeof(*diskp->dk_label));
275 	kmem_free(diskp->dk_cpulabel, sizeof(*diskp->dk_cpulabel));
276 }
277 
278 void
279 disk_destroy(struct disk *diskp)
280 {
281 
282 	mutex_destroy(&diskp->dk_openlock);
283 	mutex_destroy(&diskp->dk_rawlock);
284 }
285 
286 /*
287  * Mark the disk as having work queued for metrics collection.
288  */
289 void
290 disk_wait(struct disk *diskp)
291 {
292 
293 	iostat_wait(diskp->dk_stats);
294 }
295 
296 /*
297  * Mark the disk as busy for metrics collection.
298  */
299 void
300 disk_busy(struct disk *diskp)
301 {
302 
303 	iostat_busy(diskp->dk_stats);
304 }
305 
306 /*
307  * Finished disk operations, gather metrics.
308  */
309 void
310 disk_unbusy(struct disk *diskp, long bcount, int read)
311 {
312 
313 	iostat_unbusy(diskp->dk_stats, bcount, read);
314 }
315 
316 /*
317  * Return true if disk has an I/O operation in flight.
318  */
319 bool
320 disk_isbusy(struct disk *diskp)
321 {
322 
323 	return iostat_isbusy(diskp->dk_stats);
324 }
325 
326 /*
327  * Bounds checking against the media size, used for the raw partition.
328  * secsize, mediasize and b_blkno must all be the same units.
329  * Possibly this has to be DEV_BSIZE (512).
330  */
331 int
332 bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
333 {
334 	int64_t sz;
335 
336 	if (bp->b_blkno < 0) {
337 		/* Reject negative offsets immediately. */
338 		bp->b_error = EINVAL;
339 		return 0;
340 	}
341 
342 	sz = howmany((int64_t)bp->b_bcount, secsize);
343 
344 	/*
345 	 * bp->b_bcount is a 32-bit value, and we rejected a negative
346 	 * bp->b_blkno already, so "bp->b_blkno + sz" cannot overflow.
347 	 */
348 
349 	if (bp->b_blkno + sz > mediasize) {
350 		sz = mediasize - bp->b_blkno;
351 		if (sz == 0) {
352 			/* If exactly at end of disk, return EOF. */
353 			bp->b_resid = bp->b_bcount;
354 			return 0;
355 		}
356 		if (sz < 0) {
357 			/* If past end of disk, return EINVAL. */
358 			bp->b_error = EINVAL;
359 			return 0;
360 		}
361 		/* Otherwise, truncate request. */
362 		bp->b_bcount = sz * secsize;
363 	}
364 
365 	return 1;
366 }
367 
368 /*
369  * Determine the size of the transfer, and make sure it is
370  * within the boundaries of the partition. Adjust transfer
371  * if needed, and signal errors or early completion.
372  */
373 int
374 bounds_check_with_label(struct disk *dk, struct buf *bp, int wlabel)
375 {
376 	struct disklabel *lp = dk->dk_label;
377 	struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
378 	uint64_t p_size, p_offset, labelsector;
379 	int64_t sz;
380 
381 	if (bp->b_blkno < 0) {
382 		/* Reject negative offsets immediately. */
383 		bp->b_error = EINVAL;
384 		return -1;
385 	}
386 
387 	/* Protect against division by zero. XXX: Should never happen?!?! */
388 	if ((lp->d_secsize / DEV_BSIZE) == 0 || lp->d_secpercyl == 0) {
389 		bp->b_error = EINVAL;
390 		return -1;
391 	}
392 
393 	p_size = (uint64_t)p->p_size << dk->dk_blkshift;
394 	p_offset = (uint64_t)p->p_offset << dk->dk_blkshift;
395 #if RAW_PART == 3
396 	labelsector = lp->d_partitions[2].p_offset;
397 #else
398 	labelsector = lp->d_partitions[RAW_PART].p_offset;
399 #endif
400 	labelsector = (labelsector + dk->dk_labelsector) << dk->dk_blkshift;
401 
402 	sz = howmany((int64_t)bp->b_bcount, DEV_BSIZE);
403 
404 	/*
405 	 * bp->b_bcount is a 32-bit value, and we rejected a negative
406 	 * bp->b_blkno already, so "bp->b_blkno + sz" cannot overflow.
407 	 */
408 
409 	if (bp->b_blkno + sz > p_size) {
410 		sz = p_size - bp->b_blkno;
411 		if (sz == 0) {
412 			/* If exactly at end of disk, return EOF. */
413 			bp->b_resid = bp->b_bcount;
414 			return 0;
415 		}
416 		if (sz < 0) {
417 			/* If past end of disk, return EINVAL. */
418 			bp->b_error = EINVAL;
419 			return -1;
420 		}
421 		/* Otherwise, truncate request. */
422 		bp->b_bcount = sz << DEV_BSHIFT;
423 	}
424 
425 	/* Overwriting disk label? */
426 	if (bp->b_blkno + p_offset <= labelsector &&
427 	    bp->b_blkno + p_offset + sz > labelsector &&
428 	    (bp->b_flags & B_READ) == 0 && !wlabel) {
429 		bp->b_error = EROFS;
430 		return -1;
431 	}
432 
433 	/* calculate cylinder for disksort to order transfers with */
434 	bp->b_cylinder = (bp->b_blkno + p->p_offset) /
435 	    (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
436 	return 1;
437 }
438 
439 int
440 disk_read_sectors(void (*strat)(struct buf *), const struct disklabel *lp,
441     struct buf *bp, unsigned int sector, int count)
442 {
443 	bp->b_blkno = btodb((off_t)sector * lp->d_secsize);
444 	bp->b_bcount = count * lp->d_secsize;
445 	bp->b_flags = (bp->b_flags & ~B_WRITE) | B_READ;
446 	bp->b_oflags &= ~BO_DONE;
447 	bp->b_cylinder = sector / lp->d_secpercyl;
448 	(*strat)(bp);
449 	return biowait(bp);
450 }
451 
452 const char *
453 convertdisklabel(struct disklabel *lp, void (*strat)(struct buf *),
454     struct buf *bp, uint32_t secperunit)
455 {
456 	struct partition rp, *altp, *p;
457 	int geom_ok;
458 	const char *str;
459 
460 	memset(&rp, 0, sizeof(rp));
461 	rp.p_size = secperunit;
462 	rp.p_fstype = FS_UNUSED;
463 
464 	/* If we can seek to d_secperunit - 1, believe the disk geometry. */
465 	if (secperunit != 0 &&
466 	    disk_read_sectors(strat, lp, bp, secperunit - 1, 1) == 0)
467 		geom_ok = 1;
468 	else
469 		geom_ok = 0;
470 
471 #if 0
472 	printf("%s: secperunit (%" PRIu32 ") %s\n", __func__,
473 	    secperunit, geom_ok ? "ok" : "not ok");
474 #endif
475 
476 	p = &lp->d_partitions[RAW_PART];
477 	if (RAW_PART == 'c' - 'a')
478 		altp = &lp->d_partitions['d' - 'a'];
479 	else
480 		altp = &lp->d_partitions['c' - 'a'];
481 
482 	if (lp->d_npartitions > RAW_PART && p->p_offset == 0 && p->p_size != 0)
483 		return NULL;	/* already a raw partition */
484 	else if (lp->d_npartitions > MAX('c', 'd') - 'a' &&
485 		 altp->p_offset == 0 && altp->p_size != 0) {
486 		/* alternate partition ('c' or 'd') is suitable for raw slot,
487 		 * swap with 'd' or 'c'.
488 		 */
489 		rp = *p;
490 		*p = *altp;
491 		*altp = rp;
492 		return NULL;
493 	} else if (lp->d_npartitions <= RAW_PART &&
494 	           lp->d_npartitions > 'c' - 'a') {
495 		/* No raw partition is present, but the alternate is present.
496 		 * Copy alternate to raw partition.
497 		 */
498 		lp->d_npartitions = RAW_PART + 1;
499 		*p = *altp;
500 		return NULL;
501 	} else if (!geom_ok)
502 		str = "no raw partition and disk reports bad geometry";
503 	else if (lp->d_npartitions <= RAW_PART) {
504 		memset(&lp->d_partitions[lp->d_npartitions], 0,
505 		    sizeof(struct partition) * (RAW_PART - lp->d_npartitions));
506 		*p = rp;
507 		lp->d_npartitions = RAW_PART + 1;
508 		return NULL;
509 	} else if (lp->d_npartitions < MAXPARTITIONS) {
510 		memmove(p + 1, p,
511 		    sizeof(struct partition) * (lp->d_npartitions - RAW_PART));
512 		*p = rp;
513 		lp->d_npartitions++;
514 		return NULL;
515 	} else
516 		str = "no raw partition and partition table is full";
517 #ifdef DIAGNOSTIC
518 	printf("Bad partition: %s\n", str);
519 	printf("type = %u, subtype = %u, typename = %s\n",
520 	    lp->d_type, lp->d_subtype, lp->d_typename);
521 	printf("secsize = %u, nsectors = %u, ntracks = %u\n",
522 	    lp->d_secsize, lp->d_nsectors, lp->d_ntracks);
523 	printf("ncylinders = %u, secpercyl = %u, secperunit = %u\n",
524 	    lp->d_ncylinders, lp->d_secpercyl, lp->d_secperunit);
525 	printf("npartitions = %u\n", lp->d_npartitions);
526 
527 	for (size_t i = 0; i < MIN(lp->d_npartitions, MAXPARTITIONS); i++) {
528 		p = &lp->d_partitions[i];
529 		printf("\t%c: offset = %u size = %u fstype = %u\n",
530 		    (char)(i + 'a'), p->p_offset, p->p_size, p->p_fstype);
531 	}
532 #endif
533 	return str;
534 }
535 
536 /*
537  * disk_ioctl --
538  *	Generic disk ioctl handling.
539  */
540 int
541 disk_ioctl(struct disk *dk, dev_t dev, u_long cmd, void *data, int flag,
542     struct lwp *l)
543 {
544 	struct dkwedge_info *dkw;
545 	struct partinfo *pi;
546 	struct partition *dp;
547 #ifdef __HAVE_OLD_DISKLABEL
548 	struct disklabel newlabel;
549 #endif
550 
551 	switch (cmd) {
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 	default:
566 		break;
567 	}
568 
569 	if (dev == NODEV)
570 		return EPASSTHROUGH;
571 
572 	/* The following should be moved to dk_ioctl */
573 	switch (cmd) {
574 	case DIOCGDINFO:
575 		if (dk->dk_label == NULL)
576 			return EBUSY;
577 		memcpy(data, dk->dk_label, sizeof (*dk->dk_label));
578 		return 0;
579 
580 #ifdef __HAVE_OLD_DISKLABEL
581 	case ODIOCGDINFO:
582 		if (dk->dk_label == NULL)
583 			return EBUSY;
584 		memcpy(&newlabel, dk->dk_label, sizeof(newlabel));
585 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
586 			return ENOTTY;
587 		memcpy(data, &newlabel, sizeof(struct olddisklabel));
588 		return 0;
589 #endif
590 
591 	case DIOCGPARTINFO:
592 		pi = data;
593 		memset(pi, 0, sizeof(*pi));
594 		pi->pi_secsize = dk->dk_geom.dg_secsize;
595 		pi->pi_bsize = MAX(BLKDEV_IOSIZE, pi->pi_secsize);
596 
597 		if (DISKPART(dev) == RAW_PART) {
598 			pi->pi_size = dk->dk_geom.dg_secperunit;
599 			return 0;
600 		}
601 
602 		if (dk->dk_label == NULL)
603 			return EBUSY;
604 
605 		dp = &dk->dk_label->d_partitions[DISKPART(dev)];
606 		pi->pi_offset = dp->p_offset;
607 		pi->pi_size = dp->p_size;
608 
609 		pi->pi_fstype = dp->p_fstype;
610 		pi->pi_frag = dp->p_frag;
611 		pi->pi_fsize = dp->p_fsize;
612 		pi->pi_cpg = dp->p_cpg;
613 
614 		/*
615 		 * dholland 20130616: XXX this logic should not be
616 		 * here. It is here because the old buffer cache
617 		 * demands that all accesses to the same blocks need
618 		 * to be the same size; but it only works for FFS and
619 		 * nowadays I think it'll fail silently if the size
620 		 * info in the disklabel is wrong. (Or missing.) The
621 		 * buffer cache needs to be smarter; or failing that
622 		 * we need a reliable way here to get the right block
623 		 * size; or a reliable way to guarantee that (a) the
624 		 * fs is not mounted when we get here and (b) any
625 		 * buffers generated here will get purged when the fs
626 		 * does get mounted.
627 		 */
628 		if (dp->p_fstype == FS_BSDFFS &&
629 		    dp->p_frag != 0 && dp->p_fsize != 0)
630 			pi->pi_bsize = dp->p_frag * dp->p_fsize;
631 		return 0;
632 
633 	case DIOCAWEDGE:
634 		if ((flag & FWRITE) == 0)
635 			return EBADF;
636 
637 		dkw = data;
638 		strlcpy(dkw->dkw_parent, dk->dk_name, sizeof(dkw->dkw_parent));
639 		return dkwedge_add(dkw);
640 
641 	case DIOCDWEDGE:
642 		if ((flag & FWRITE) == 0)
643 			return EBADF;
644 
645 		dkw = data;
646 		strlcpy(dkw->dkw_parent, dk->dk_name, sizeof(dkw->dkw_parent));
647 		return dkwedge_del(dkw);
648 
649 	case DIOCLWEDGES:
650 		return dkwedge_list(dk, data, l);
651 
652 	case DIOCMWEDGES:
653 		if ((flag & FWRITE) == 0)
654 			return EBADF;
655 
656 		dkwedge_discover(dk);
657 		return 0;
658 
659 	case DIOCRMWEDGES:
660 		if ((flag & FWRITE) == 0)
661 			return EBADF;
662 
663 		dkwedge_delall(dk);
664 		return 0;
665 
666 	default:
667 		return EPASSTHROUGH;
668 	}
669 }
670 
671 void
672 disk_set_info(device_t dev, struct disk *dk, const char *type)
673 {
674 	struct disk_geom *dg = &dk->dk_geom;
675 
676 	if (dg->dg_secsize == 0) {
677 #ifdef DIAGNOSTIC
678 		printf("%s: fixing 0 sector size\n", dk->dk_name);
679 #endif
680 		dg->dg_secsize = DEV_BSIZE;
681 	}
682 
683 	dk->dk_blkshift = DK_BSIZE2BLKSHIFT(dg->dg_secsize);
684 	dk->dk_byteshift = DK_BSIZE2BYTESHIFT(dg->dg_secsize);
685 
686 	if (dg->dg_secperunit == 0 && dg->dg_ncylinders == 0) {
687 #ifdef DIAGNOSTIC
688 		printf("%s: secperunit and ncylinders are zero\n", dk->dk_name);
689 #endif
690 		return;
691 	}
692 
693 	if (dg->dg_secperunit == 0) {
694 		if (dg->dg_nsectors == 0 || dg->dg_ntracks == 0) {
695 #ifdef DIAGNOSTIC
696 			printf("%s: secperunit and (sectors or tracks) "
697 			    "are zero\n", dk->dk_name);
698 #endif
699 			return;
700 		}
701 		dg->dg_secperunit = (int64_t) dg->dg_nsectors *
702 		    dg->dg_ntracks * dg->dg_ncylinders;
703 	}
704 
705 	if (dg->dg_ncylinders == 0) {
706 		if (dg->dg_ntracks && dg->dg_nsectors)
707 			dg->dg_ncylinders = dg->dg_secperunit /
708 			    (dg->dg_ntracks * dg->dg_nsectors);
709 	}
710 
711 	prop_dictionary_t disk_info, odisk_info, geom;
712 
713 	disk_info = prop_dictionary_create();
714 	geom = prop_dictionary_create();
715 
716 	prop_dictionary_set_uint64(geom, "sectors-per-unit",
717 	    dg->dg_secperunit);
718 
719 	prop_dictionary_set_uint32(geom, "sector-size", dg->dg_secsize);
720 
721 	if (dg->dg_nsectors)
722 		prop_dictionary_set_uint16(geom, "sectors-per-track",
723 		    dg->dg_nsectors);
724 
725 	if (dg->dg_ntracks)
726 		prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
727 		    dg->dg_ntracks);
728 
729 	if (dg->dg_ncylinders)
730 		prop_dictionary_set_uint64(geom, "cylinders-per-unit",
731 		    dg->dg_ncylinders);
732 
733 	prop_dictionary_set(disk_info, "geometry", geom);
734 
735 	if (type)
736 		prop_dictionary_set_cstring_nocopy(disk_info, "type", type);
737 
738 	prop_object_release(geom);
739 
740 	odisk_info = dk->dk_info;
741 	dk->dk_info = disk_info;
742 
743 	if (dev)
744 		prop_dictionary_set(device_properties(dev), "disk-info",
745 		    disk_info);
746 
747 	/*
748 	 * Don't release disk_info here; we keep a reference to it.
749 	 * disk_detach() will release it when we go away.
750 	 */
751 	if (odisk_info)
752 		prop_object_release(odisk_info);
753 }
754