Home | History | Annotate | Line # | Download | only in dev
      1 /* $NetBSD: dksubr.c,v 1.118 2025/02/18 17:09:34 mlelstv Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997, 1998, 1999, 2002, 2008 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 and Roland C. Dowdeswell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: dksubr.c,v 1.118 2025/02/18 17:09:34 mlelstv Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/stat.h>
     38 #include <sys/proc.h>
     39 #include <sys/ioctl.h>
     40 #include <sys/device.h>
     41 #include <sys/disk.h>
     42 #include <sys/disklabel.h>
     43 #include <sys/buf.h>
     44 #include <sys/bufq.h>
     45 #include <sys/vnode.h>
     46 #include <sys/fcntl.h>
     47 #include <sys/namei.h>
     48 #include <sys/module.h>
     49 #include <sys/syslog.h>
     50 
     51 #include <dev/dkvar.h>
     52 #include <miscfs/specfs/specdev.h> /* for v_rdev */
     53 
     54 int	dkdebug = 0;
     55 
     56 #ifdef DEBUG
     57 #define DKDB_FOLLOW	0x1
     58 #define DKDB_INIT	0x2
     59 #define DKDB_VNODE	0x4
     60 #define DKDB_DUMP	0x8
     61 
     62 #define IFDEBUG(x,y)		if (dkdebug & (x)) y
     63 #define DPRINTF(x,y)		IFDEBUG(x, printf y)
     64 #define DPRINTF_FOLLOW(y)	DPRINTF(DKDB_FOLLOW, y)
     65 #else
     66 #define IFDEBUG(x,y)
     67 #define DPRINTF(x,y)
     68 #define DPRINTF_FOLLOW(y)
     69 #endif
     70 
     71 #define DKF_READYFORDUMP	(DKF_INITED|DKF_TAKEDUMP)
     72 
     73 static int dk_subr_modcmd(modcmd_t, void *);
     74 
     75 #define DKLABELDEV(dev)	\
     76 	(MAKEDISKDEV(major((dev)), DISKUNIT((dev)), RAW_PART))
     77 
     78 static void	dk_makedisklabel(struct dk_softc *);
     79 static int	dk_translate(struct dk_softc *, struct buf *);
     80 
     81 void
     82 dk_init(struct dk_softc *dksc, device_t dev, int dtype)
     83 {
     84 
     85 	memset(dksc, 0x0, sizeof(*dksc));
     86 	dksc->sc_dtype = dtype;
     87 	dksc->sc_dev = dev;
     88 
     89 	strlcpy(dksc->sc_xname, device_xname(dev), DK_XNAME_SIZE);
     90 	dksc->sc_dkdev.dk_name = dksc->sc_xname;
     91 }
     92 
     93 void
     94 dk_attach(struct dk_softc *dksc)
     95 {
     96 	KASSERT(dksc->sc_dev != NULL);
     97 
     98 	mutex_init(&dksc->sc_iolock, MUTEX_DEFAULT, IPL_VM);
     99 	dksc->sc_flags |= DKF_READYFORDUMP;
    100 #ifdef DIAGNOSTIC
    101 	dksc->sc_flags |= DKF_WARNLABEL | DKF_LABELSANITY;
    102 #endif
    103 
    104 	if ((dksc->sc_flags & DKF_NO_RND) == 0) {
    105 		/* Attach the device into the rnd source list. */
    106 		rnd_attach_source(&dksc->sc_rnd_source, dksc->sc_xname,
    107 		    RND_TYPE_DISK, RND_FLAG_DEFAULT);
    108 	}
    109 }
    110 
    111 void
    112 dk_detach(struct dk_softc *dksc)
    113 {
    114 	if ((dksc->sc_flags & DKF_NO_RND) == 0) {
    115 		/* Unhook the entropy source. */
    116 		rnd_detach_source(&dksc->sc_rnd_source);
    117 	}
    118 
    119 	dksc->sc_flags &= ~DKF_READYFORDUMP;
    120 	mutex_destroy(&dksc->sc_iolock);
    121 }
    122 
    123 /* ARGSUSED */
    124 int
    125 dk_open(struct dk_softc *dksc, dev_t dev,
    126     int flags, int fmt, struct lwp *l)
    127 {
    128 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    129 	struct	disklabel *lp = dksc->sc_dkdev.dk_label;
    130 	int	part = DISKPART(dev);
    131 	int	pmask = 1 << part;
    132 	int	ret = 0;
    133 	struct disk *dk = &dksc->sc_dkdev;
    134 
    135 	DPRINTF_FOLLOW(("%s(%s, %p, 0x%"PRIx64", 0x%x)\n", __func__,
    136 	    dksc->sc_xname, dksc, dev, flags));
    137 
    138 	mutex_enter(&dk->dk_openlock);
    139 
    140 	/*
    141 	 * If there are wedges, and this is not RAW_PART, then we
    142 	 * need to fail.
    143 	 */
    144 	if (dk->dk_nwedges != 0 && part != RAW_PART) {
    145 		ret = EBUSY;
    146 		goto done;
    147 	}
    148 
    149 	/* If no dkdriver attached, bail */
    150 	if (dkd == NULL) {
    151 		ret = ENXIO;
    152 		goto done;
    153 	}
    154 
    155 	/*
    156 	 * initialize driver for the first opener
    157 	 */
    158 	if (dk->dk_openmask == 0 && dkd->d_firstopen != NULL) {
    159 		ret = (*dkd->d_firstopen)(dksc->sc_dev, dev, flags, fmt);
    160 		if (ret)
    161 			goto done;
    162 	}
    163 
    164 	/*
    165 	 * If we're init'ed and there are no other open partitions then
    166 	 * update the in-core disklabel.
    167 	 */
    168 	if ((dksc->sc_flags & DKF_INITED)) {
    169 		if ((dksc->sc_flags & DKF_VLABEL) == 0) {
    170 			dksc->sc_flags |= DKF_VLABEL;
    171 			dk_getdisklabel(dksc, dev);
    172 		}
    173 	}
    174 
    175 	/* Fail if we can't find the partition. */
    176 	if (part != RAW_PART &&
    177 	    ((dksc->sc_flags & DKF_VLABEL) == 0 ||
    178 	     part >= lp->d_npartitions ||
    179 	     lp->d_partitions[part].p_fstype == FS_UNUSED)) {
    180 		ret = ENXIO;
    181 		goto done;
    182 	}
    183 
    184 	/* Mark our unit as open. */
    185 	switch (fmt) {
    186 	case S_IFCHR:
    187 		dk->dk_copenmask |= pmask;
    188 		break;
    189 	case S_IFBLK:
    190 		dk->dk_bopenmask |= pmask;
    191 		break;
    192 	}
    193 
    194 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    195 
    196 done:
    197 	mutex_exit(&dk->dk_openlock);
    198 	return ret;
    199 }
    200 
    201 /* ARGSUSED */
    202 int
    203 dk_close(struct dk_softc *dksc, dev_t dev,
    204     int flags, int fmt, struct lwp *l)
    205 {
    206 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    207 	int	part = DISKPART(dev);
    208 	int	pmask = 1 << part;
    209 	struct disk *dk = &dksc->sc_dkdev;
    210 
    211 	DPRINTF_FOLLOW(("%s(%s, %p, 0x%"PRIx64", 0x%x)\n", __func__,
    212 	    dksc->sc_xname, dksc, dev, flags));
    213 
    214 	mutex_enter(&dk->dk_openlock);
    215 
    216 	switch (fmt) {
    217 	case S_IFCHR:
    218 		dk->dk_copenmask &= ~pmask;
    219 		break;
    220 	case S_IFBLK:
    221 		dk->dk_bopenmask &= ~pmask;
    222 		break;
    223 	}
    224 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    225 
    226 	if (dk->dk_openmask == 0) {
    227 		if (dkd->d_lastclose != NULL)
    228 			(*dkd->d_lastclose)(dksc->sc_dev);
    229 		if ((dksc->sc_flags & DKF_KLABEL) == 0)
    230 			dksc->sc_flags &= ~DKF_VLABEL;
    231 	}
    232 
    233 	mutex_exit(&dk->dk_openlock);
    234 	return 0;
    235 }
    236 
    237 static int
    238 dk_translate(struct dk_softc *dksc, struct buf *bp)
    239 {
    240 	int	part;
    241 	int	wlabel;
    242 	daddr_t	blkno;
    243 	struct disklabel *lp;
    244 	struct disk *dk;
    245 	uint64_t numsecs;
    246 	unsigned secsize;
    247 
    248 	lp = dksc->sc_dkdev.dk_label;
    249 	dk = &dksc->sc_dkdev;
    250 
    251 	part = DISKPART(bp->b_dev);
    252 	numsecs = dk->dk_geom.dg_secperunit;
    253 	secsize = dk->dk_geom.dg_secsize;
    254 
    255 	/*
    256 	 * The transfer must be a whole number of blocks and the offset must
    257 	 * not be negative.
    258 	 */
    259 	if ((bp->b_bcount % secsize) != 0 || bp->b_blkno < 0) {
    260 		bp->b_error = EINVAL;
    261 		goto done;
    262 	}
    263 
    264 	/* If there is nothing to do, then we are done */
    265 	if (bp->b_bcount == 0)
    266 		goto done;
    267 
    268 	wlabel = dksc->sc_flags & (DKF_WLABEL|DKF_LABELLING);
    269 	if (part == RAW_PART) {
    270 		uint64_t numblocks = btodb(numsecs * secsize);
    271 		if (bounds_check_with_mediasize(bp, DEV_BSIZE, numblocks) <= 0)
    272 			goto done;
    273 	} else {
    274 		if (bounds_check_with_label(&dksc->sc_dkdev, bp, wlabel) <= 0)
    275 			goto done;
    276 	}
    277 
    278 	/*
    279 	 * Convert the block number to absolute and put it in terms
    280 	 * of the device's logical block size.
    281 	 */
    282 	if (secsize >= DEV_BSIZE)
    283 		blkno = bp->b_blkno / (secsize / DEV_BSIZE);
    284 	else
    285 		blkno = bp->b_blkno * (DEV_BSIZE / secsize);
    286 
    287 	if (part != RAW_PART)
    288 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
    289 	bp->b_rawblkno = blkno;
    290 
    291 	return -1;
    292 
    293 done:
    294 	bp->b_resid = bp->b_bcount;
    295 	return bp->b_error;
    296 }
    297 
    298 static int
    299 dk_strategy1(struct dk_softc *dksc, struct buf *bp)
    300 {
    301 	int error;
    302 
    303 	DPRINTF_FOLLOW(("%s(%s, %p, %p)\n", __func__,
    304 	    dksc->sc_xname, dksc, bp));
    305 
    306 	if (!(dksc->sc_flags & DKF_INITED)) {
    307 		DPRINTF_FOLLOW(("%s: not inited\n", __func__));
    308 		bp->b_error = ENXIO;
    309 		bp->b_resid = bp->b_bcount;
    310 		biodone(bp);
    311 		return 1;
    312 	}
    313 
    314 	error = dk_translate(dksc, bp);
    315 	if (error >= 0) {
    316 		biodone(bp);
    317 		return 1;
    318 	}
    319 
    320 	return 0;
    321 }
    322 
    323 void
    324 dk_strategy(struct dk_softc *dksc, struct buf *bp)
    325 {
    326 	int error;
    327 
    328 	error = dk_strategy1(dksc, bp);
    329 	if (error)
    330 		return;
    331 
    332 	/*
    333 	 * Queue buffer and start unit
    334 	 */
    335 	dk_start(dksc, bp);
    336 }
    337 
    338 int
    339 dk_strategy_defer(struct dk_softc *dksc, struct buf *bp)
    340 {
    341 	int error;
    342 
    343 	error = dk_strategy1(dksc, bp);
    344 	if (error)
    345 		return error;
    346 
    347 	/*
    348 	 * Queue buffer only
    349 	 */
    350 	mutex_enter(&dksc->sc_iolock);
    351 	disk_wait(&dksc->sc_dkdev);
    352 	bufq_put(dksc->sc_bufq, bp);
    353 	mutex_exit(&dksc->sc_iolock);
    354 
    355 	return 0;
    356 }
    357 
    358 int
    359 dk_strategy_pending(struct dk_softc *dksc)
    360 {
    361 	struct buf *bp;
    362 
    363 	if (!(dksc->sc_flags & DKF_INITED)) {
    364 		DPRINTF_FOLLOW(("%s: not inited\n", __func__));
    365 		return 0;
    366 	}
    367 
    368 	mutex_enter(&dksc->sc_iolock);
    369 	bp = bufq_peek(dksc->sc_bufq);
    370 	mutex_exit(&dksc->sc_iolock);
    371 
    372 	return bp != NULL;
    373 }
    374 
    375 void
    376 dk_start(struct dk_softc *dksc, struct buf *bp)
    377 {
    378 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    379 	int error;
    380 
    381 	if (!(dksc->sc_flags & DKF_INITED)) {
    382 		DPRINTF_FOLLOW(("%s: not inited\n", __func__));
    383 		return;
    384 	}
    385 
    386 	mutex_enter(&dksc->sc_iolock);
    387 
    388 	if (bp != NULL) {
    389 		bp->b_ci = curcpu();
    390 		disk_wait(&dksc->sc_dkdev);
    391 		bufq_put(dksc->sc_bufq, bp);
    392 	}
    393 
    394 	/*
    395 	 * If another thread is running the queue, increment
    396 	 * busy counter to 2 so that the queue is retried,
    397 	 * because the driver may now accept additional
    398 	 * requests.
    399 	 */
    400 	if (dksc->sc_busy < 2)
    401 		dksc->sc_busy++;
    402 	if (dksc->sc_busy > 1)
    403 		goto done;
    404 
    405 	/*
    406 	 * Peeking at the buffer queue and committing the operation
    407 	 * only after success isn't atomic.
    408 	 *
    409 	 * So when a diskstart fails, the buffer is saved
    410 	 * and tried again before the next buffer is fetched.
    411 	 * dk_drain() handles flushing of a saved buffer.
    412 	 *
    413 	 * This keeps order of I/O operations, unlike bufq_put.
    414 	 */
    415 
    416 	while (dksc->sc_busy > 0) {
    417 
    418 		bp = dksc->sc_deferred;
    419 		dksc->sc_deferred = NULL;
    420 
    421 		if (bp == NULL)
    422 			bp = bufq_get(dksc->sc_bufq);
    423 
    424 		while (bp != NULL) {
    425 
    426 			disk_busy(&dksc->sc_dkdev);
    427 			mutex_exit(&dksc->sc_iolock);
    428 			error = dkd->d_diskstart(dksc->sc_dev, bp);
    429 			mutex_enter(&dksc->sc_iolock);
    430 			if (error == EAGAIN || error == ENOMEM) {
    431 				/*
    432 				 * Not a disk error. Retry later.
    433 				 */
    434 				KASSERT(dksc->sc_deferred == NULL);
    435 				dksc->sc_deferred = bp;
    436 				disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
    437 				disk_wait(&dksc->sc_dkdev);
    438 				break;
    439 			}
    440 
    441 			if (error != 0) {
    442 				bp->b_error = error;
    443 				bp->b_resid = bp->b_bcount;
    444 				mutex_exit(&dksc->sc_iolock);
    445 				dk_done(dksc, bp);
    446 				mutex_enter(&dksc->sc_iolock);
    447 			}
    448 
    449 			bp = bufq_get(dksc->sc_bufq);
    450 		}
    451 
    452 		dksc->sc_busy--;
    453 	}
    454 done:
    455 	mutex_exit(&dksc->sc_iolock);
    456 }
    457 
    458 void
    459 dk_done(struct dk_softc *dksc, struct buf *bp)
    460 {
    461 	struct disk *dk = &dksc->sc_dkdev;
    462 
    463 	if (bp->b_error != 0) {
    464 		struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
    465 
    466 		diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
    467 			dk->dk_label);
    468 		printf("\n");
    469 	}
    470 
    471 	mutex_enter(&dksc->sc_iolock);
    472 	disk_unbusy(dk, bp->b_bcount - bp->b_resid, (bp->b_flags & B_READ));
    473 	mutex_exit(&dksc->sc_iolock);
    474 
    475 	if ((dksc->sc_flags & DKF_NO_RND) == 0)
    476 		rnd_add_uint32(&dksc->sc_rnd_source, bp->b_rawblkno);
    477 
    478 	biodone(bp);
    479 }
    480 
    481 void
    482 dk_drain(struct dk_softc *dksc)
    483 {
    484 	struct buf *bp;
    485 
    486 	mutex_enter(&dksc->sc_iolock);
    487 	bp = dksc->sc_deferred;
    488 	dksc->sc_deferred = NULL;
    489 	if (bp != NULL) {
    490 		bp->b_error = EIO;
    491 		bp->b_resid = bp->b_bcount;
    492 		biodone(bp);
    493 	}
    494 	bufq_drain(dksc->sc_bufq);
    495 	mutex_exit(&dksc->sc_iolock);
    496 }
    497 
    498 int
    499 dk_discard(struct dk_softc *dksc, dev_t dev, off_t pos, off_t len)
    500 {
    501 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    502 	unsigned secsize = dksc->sc_dkdev.dk_geom.dg_secsize;
    503 	struct buf tmp, *bp = &tmp;
    504 	int maxsz;
    505 	int error = 0;
    506 
    507 	KASSERT(len >= 0);
    508 
    509 	DPRINTF_FOLLOW(("%s(%s, %p, 0x%"PRIx64", %jd, %jd)\n", __func__,
    510 	    dksc->sc_xname, dksc, dev, (intmax_t)pos, (intmax_t)len));
    511 
    512 	if (!(dksc->sc_flags & DKF_INITED)) {
    513 		DPRINTF_FOLLOW(("%s: not inited\n", __func__));
    514 		return ENXIO;
    515 	}
    516 
    517 	if (secsize == 0 || (pos % secsize) != 0 || (len % secsize) != 0)
    518 		return EINVAL;
    519 
    520 	/* largest value that b_bcount can store */
    521 	maxsz = rounddown(INT_MAX, secsize);
    522 
    523 	while (len > 0) {
    524 		/* enough data to please the bounds checking code */
    525 		bp->b_error = 0;
    526 		bp->b_dev = dev;
    527 		bp->b_blkno = (daddr_t)(pos / secsize);
    528 		bp->b_bcount = lmin(len, maxsz);
    529 		bp->b_flags = B_WRITE;
    530 
    531 		error = dk_translate(dksc, bp);
    532 		if (error >= 0)
    533 			break;
    534 
    535 		error = dkd->d_discard(dksc->sc_dev,
    536 			(off_t)bp->b_rawblkno * secsize,
    537 			(off_t)bp->b_bcount);
    538 		if (error)
    539 			break;
    540 
    541 		pos += bp->b_bcount;
    542 		len -= bp->b_bcount;
    543 	}
    544 
    545 	return error;
    546 }
    547 
    548 int
    549 dk_size(struct dk_softc *dksc, dev_t dev)
    550 {
    551 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    552 	struct	disklabel *lp;
    553 	int	is_open;
    554 	int	part;
    555 	int	size;
    556 
    557 	if ((dksc->sc_flags & DKF_INITED) == 0)
    558 		return -1;
    559 
    560 	part = DISKPART(dev);
    561 	is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
    562 
    563 	if (!is_open && dkd->d_open(dev, 0, S_IFBLK, curlwp))
    564 		return -1;
    565 
    566 	lp = dksc->sc_dkdev.dk_label;
    567 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
    568 		size = -1;
    569 	else
    570 		size = lp->d_partitions[part].p_size *
    571 		    (lp->d_secsize / DEV_BSIZE);
    572 
    573 	if (!is_open && dkd->d_close(dev, 0, S_IFBLK, curlwp))
    574 		return -1;
    575 
    576 	return size;
    577 }
    578 
    579 int
    580 dk_ioctl(struct dk_softc *dksc, dev_t dev,
    581 	    u_long cmd, void *data, int flag, struct lwp *l)
    582 {
    583 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    584 	struct	disklabel *lp;
    585 	struct	disk *dk = &dksc->sc_dkdev;
    586 #ifdef __HAVE_OLD_DISKLABEL
    587 	struct	disklabel newlabel;
    588 #endif
    589 	int	error;
    590 
    591 	DPRINTF_FOLLOW(("%s(%s, %p, 0x%"PRIx64", 0x%lx)\n", __func__,
    592 	    dksc->sc_xname, dksc, dev, cmd));
    593 
    594 	/* ensure that the pseudo disk is open for writes for these commands */
    595 	switch (cmd) {
    596 	case DIOCSDINFO:
    597 	case DIOCWDINFO:
    598 #ifdef __HAVE_OLD_DISKLABEL
    599 	case ODIOCSDINFO:
    600 	case ODIOCWDINFO:
    601 #endif
    602 	case DIOCKLABEL:
    603 	case DIOCWLABEL:
    604 	case DIOCAWEDGE:
    605 	case DIOCDWEDGE:
    606 	case DIOCSSTRATEGY:
    607 		if ((flag & FWRITE) == 0)
    608 			return EBADF;
    609 	}
    610 
    611 	/* ensure that the pseudo-disk is initialized for these */
    612 	switch (cmd) {
    613 	case DIOCGDINFO:
    614 	case DIOCSDINFO:
    615 	case DIOCWDINFO:
    616 	case DIOCGPARTINFO:
    617 	case DIOCKLABEL:
    618 	case DIOCWLABEL:
    619 	case DIOCGDEFLABEL:
    620 	case DIOCAWEDGE:
    621 	case DIOCDWEDGE:
    622 	case DIOCLWEDGES:
    623 	case DIOCMWEDGES:
    624 	case DIOCRMWEDGES:
    625 	case DIOCCACHESYNC:
    626 #ifdef __HAVE_OLD_DISKLABEL
    627 	case ODIOCGDINFO:
    628 	case ODIOCSDINFO:
    629 	case ODIOCWDINFO:
    630 	case ODIOCGDEFLABEL:
    631 #endif
    632 		if ((dksc->sc_flags & DKF_INITED) == 0)
    633 			return ENXIO;
    634 	}
    635 
    636 	error = disk_ioctl(dk, dev, cmd, data, flag, l);
    637 	if (error != EPASSTHROUGH)
    638 		return error;
    639 	else
    640 		error = 0;
    641 
    642 	switch (cmd) {
    643 	case DIOCWDINFO:
    644 	case DIOCSDINFO:
    645 #ifdef __HAVE_OLD_DISKLABEL
    646 	case ODIOCWDINFO:
    647 	case ODIOCSDINFO:
    648 #endif
    649 #ifdef __HAVE_OLD_DISKLABEL
    650 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    651 			memset(&newlabel, 0, sizeof newlabel);
    652 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    653 			lp = &newlabel;
    654 		} else
    655 #endif
    656 		lp = (struct disklabel *)data;
    657 
    658 		mutex_enter(&dk->dk_openlock);
    659 		dksc->sc_flags |= DKF_LABELLING;
    660 
    661 		error = setdisklabel(dksc->sc_dkdev.dk_label,
    662 		    lp, 0, dksc->sc_dkdev.dk_cpulabel);
    663 		if (error == 0) {
    664 			if (cmd == DIOCWDINFO
    665 #ifdef __HAVE_OLD_DISKLABEL
    666 			    || cmd == ODIOCWDINFO
    667 #endif
    668 			   )
    669 				error = writedisklabel(DKLABELDEV(dev),
    670 				    dkd->d_strategy, dksc->sc_dkdev.dk_label,
    671 				    dksc->sc_dkdev.dk_cpulabel);
    672 		}
    673 
    674 		dksc->sc_flags &= ~DKF_LABELLING;
    675 		mutex_exit(&dk->dk_openlock);
    676 		break;
    677 
    678 	case DIOCKLABEL:
    679 		if (*(int *)data != 0)
    680 			dksc->sc_flags |= DKF_KLABEL;
    681 		else
    682 			dksc->sc_flags &= ~DKF_KLABEL;
    683 		break;
    684 
    685 	case DIOCWLABEL:
    686 		if (*(int *)data != 0)
    687 			dksc->sc_flags |= DKF_WLABEL;
    688 		else
    689 			dksc->sc_flags &= ~DKF_WLABEL;
    690 		break;
    691 
    692 	case DIOCGDEFLABEL:
    693 		dk_getdefaultlabel(dksc, (struct disklabel *)data);
    694 		break;
    695 
    696 #ifdef __HAVE_OLD_DISKLABEL
    697 	case ODIOCGDEFLABEL:
    698 		dk_getdefaultlabel(dksc, &newlabel);
    699 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    700 			return ENOTTY;
    701 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    702 		break;
    703 #endif
    704 
    705 	case DIOCGSTRATEGY:
    706 	    {
    707 		struct disk_strategy *dks = (void *)data;
    708 
    709 		mutex_enter(&dksc->sc_iolock);
    710 		if (dksc->sc_bufq != NULL)
    711 			strlcpy(dks->dks_name,
    712 			    bufq_getstrategyname(dksc->sc_bufq),
    713 			    sizeof(dks->dks_name));
    714 		else
    715 			error = EINVAL;
    716 		mutex_exit(&dksc->sc_iolock);
    717 		dks->dks_paramlen = 0;
    718 		break;
    719 	    }
    720 
    721 	case DIOCSSTRATEGY:
    722 	    {
    723 		struct disk_strategy *dks = (void *)data;
    724 		struct bufq_state *new;
    725 		struct bufq_state *old;
    726 
    727 		if (dks->dks_param != NULL) {
    728 			return EINVAL;
    729 		}
    730 		dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
    731 		error = bufq_alloc(&new, dks->dks_name,
    732 		    BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
    733 		if (error) {
    734 			return error;
    735 		}
    736 		mutex_enter(&dksc->sc_iolock);
    737 		old = dksc->sc_bufq;
    738 		if (old)
    739 			bufq_move(new, old);
    740 		dksc->sc_bufq = new;
    741 		mutex_exit(&dksc->sc_iolock);
    742 		if (old)
    743 			bufq_free(old);
    744 		break;
    745 	    }
    746 
    747 	default:
    748 		error = ENOTTY;
    749 	}
    750 
    751 	return error;
    752 }
    753 
    754 /*
    755  * dk_dump dumps all of physical memory into the partition specified.
    756  * This requires substantially more framework than {s,w}ddump, and hence
    757  * is probably much more fragile.
    758  *
    759  */
    760 
    761 #define DKFF_READYFORDUMP(x)	(((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
    762 static volatile int	dk_dumping = 0;
    763 
    764 /* ARGSUSED */
    765 int
    766 dk_dump(struct dk_softc *dksc, dev_t dev,
    767     daddr_t blkno, void *vav, size_t size, int flags)
    768 {
    769 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    770 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    771 	char *va = vav;
    772 	struct disklabel *lp;
    773 	struct partition *p;
    774 	int part, towrt, maxblkcnt, nblk;
    775 	int maxxfer, rv = 0;
    776 
    777 	/*
    778 	 * ensure that we consider this device to be safe for dumping,
    779 	 * and that the device is configured.
    780 	 */
    781 	if (!DKFF_READYFORDUMP(dksc->sc_flags)) {
    782 		DPRINTF(DKDB_DUMP, ("%s: bad dump flags 0x%x\n", __func__,
    783 		    dksc->sc_flags));
    784 		return ENXIO;
    785 	}
    786 
    787 	/* ensure that we are not already dumping */
    788 	if (dk_dumping)
    789 		return EFAULT;
    790 	if ((flags & DK_DUMP_RECURSIVE) == 0)
    791 		dk_dumping = 1;
    792 
    793 	if (dkd->d_dumpblocks == NULL) {
    794 		DPRINTF(DKDB_DUMP, ("%s: no dumpblocks\n", __func__));
    795 		return ENXIO;
    796 	}
    797 
    798 	/* device specific max transfer size */
    799 	maxxfer = MAXPHYS;
    800 	if (dkd->d_iosize != NULL)
    801 		(*dkd->d_iosize)(dksc->sc_dev, &maxxfer);
    802 
    803 	/* Convert to disk sectors.  Request must be a multiple of size. */
    804 	part = DISKPART(dev);
    805 	lp = dksc->sc_dkdev.dk_label;
    806 	if ((size % lp->d_secsize) != 0) {
    807 		DPRINTF(DKDB_DUMP, ("%s: odd size %zu\n", __func__, size));
    808 		return EFAULT;
    809 	}
    810 	towrt = size / lp->d_secsize;
    811 	blkno = dbtob(blkno) / lp->d_secsize;   /* blkno in secsize units */
    812 
    813 	p = &lp->d_partitions[part];
    814 	if (part == RAW_PART) {
    815 		if (p->p_fstype != FS_UNUSED) {
    816 			DPRINTF(DKDB_DUMP, ("%s: bad fstype %d\n", __func__,
    817 			    p->p_fstype));
    818 			return ENXIO;
    819 		}
    820 		/* Check whether dump goes to a wedge */
    821 		if (dksc->sc_dkdev.dk_nwedges == 0) {
    822 			DPRINTF(DKDB_DUMP, ("%s: dump to raw\n", __func__));
    823 			return ENXIO;
    824 		}
    825 		/* Check transfer bounds against media size */
    826 		if (blkno < 0 || (blkno + towrt) > dg->dg_secperunit) {
    827 			DPRINTF(DKDB_DUMP, ("%s: out of bounds blkno=%jd, towrt=%d, "
    828 			    "nsects=%jd\n", __func__, (intmax_t)blkno, towrt, dg->dg_secperunit));
    829 			return EINVAL;
    830 		}
    831 	} else {
    832 		int nsects, sectoff;
    833 
    834 		if (p->p_fstype != FS_SWAP && p->p_fstype != FS_RAID) {
    835 			DPRINTF(DKDB_DUMP, ("%s: bad fstype %d\n", __func__,
    836 			    p->p_fstype));
    837 			return ENXIO;
    838 		}
    839 		nsects = p->p_size;
    840 		sectoff = p->p_offset;
    841 
    842 		/* Check transfer bounds against partition size. */
    843 		if ((blkno < 0) || ((blkno + towrt) > nsects)) {
    844 			DPRINTF(DKDB_DUMP, ("%s: out of bounds blkno=%jd, towrt=%d, "
    845 			    "nsects=%d\n", __func__, (intmax_t)blkno, towrt, nsects));
    846 			return EINVAL;
    847 		}
    848 
    849 		/* Offset block number to start of partition. */
    850 		blkno += sectoff;
    851 	}
    852 
    853 	/* Start dumping and return when done. */
    854 	maxblkcnt = howmany(maxxfer, lp->d_secsize);
    855 	while (towrt > 0) {
    856 		nblk = uimin(maxblkcnt, towrt);
    857 
    858 		if ((rv = (*dkd->d_dumpblocks)(dksc->sc_dev, va, blkno, nblk))
    859 		    != 0) {
    860 			DPRINTF(DKDB_DUMP, ("%s: dumpblocks %d\n", __func__,
    861 			    rv));
    862 			return rv;
    863 		}
    864 
    865 		towrt -= nblk;
    866 		blkno += nblk;
    867 		va += nblk * lp->d_secsize;
    868 	}
    869 
    870 	if ((flags & DK_DUMP_RECURSIVE) == 0)
    871 		dk_dumping = 0;
    872 
    873 	return 0;
    874 }
    875 
    876 /* ARGSUSED */
    877 void
    878 dk_getdefaultlabel(struct dk_softc *dksc, struct disklabel *lp)
    879 {
    880 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    881 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    882 
    883 	memset(lp, 0, sizeof(*lp));
    884 
    885 	if (dg->dg_secperunit > UINT32_MAX)
    886 		lp->d_secperunit = UINT32_MAX;
    887 	else
    888 		lp->d_secperunit = dg->dg_secperunit;
    889 	lp->d_secsize = dg->dg_secsize;
    890 	lp->d_nsectors = dg->dg_nsectors;
    891 	lp->d_ntracks = dg->dg_ntracks;
    892 	lp->d_ncylinders = dg->dg_ncylinders;
    893 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    894 
    895 	strlcpy(lp->d_typename, dksc->sc_xname, sizeof(lp->d_typename));
    896 	lp->d_type = dksc->sc_dtype;
    897 	strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    898 	lp->d_rpm = 3600;
    899 	lp->d_interleave = 1;
    900 	lp->d_flags = 0;
    901 
    902 	lp->d_partitions[RAW_PART].p_offset = 0;
    903 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
    904 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    905 	lp->d_npartitions = RAW_PART + 1;
    906 
    907 	lp->d_magic = DISKMAGIC;
    908 	lp->d_magic2 = DISKMAGIC;
    909 
    910 	if (dkd->d_label)
    911 		dkd->d_label(dksc->sc_dev, lp);
    912 
    913 	lp->d_checksum = dkcksum(lp);
    914 }
    915 
    916 /* ARGSUSED */
    917 void
    918 dk_getdisklabel(struct dk_softc *dksc, dev_t dev)
    919 {
    920 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    921 	struct	 disklabel *lp = dksc->sc_dkdev.dk_label;
    922 	struct	 cpu_disklabel *clp = dksc->sc_dkdev.dk_cpulabel;
    923 	struct   disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    924 	struct	 partition *pp;
    925 	int	 i, lpratio, dgratio;
    926 	const char	*errstring;
    927 
    928 	memset(clp, 0x0, sizeof(*clp));
    929 	dk_getdefaultlabel(dksc, lp);
    930 	errstring = readdisklabel(DKLABELDEV(dev), dkd->d_strategy,
    931 	    dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
    932 	if (errstring) {
    933 		dk_makedisklabel(dksc);
    934 		if (dksc->sc_flags & DKF_WARNLABEL)
    935 			printf("%s: %s\n", dksc->sc_xname, errstring);
    936 		return;
    937 	}
    938 
    939 	if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
    940 		return;
    941 
    942 	/* Convert sector counts to multiple of DEV_BSIZE for comparison */
    943 	lpratio = dgratio = 1;
    944 	if (lp->d_secsize > DEV_BSIZE)
    945 		lpratio = lp->d_secsize / DEV_BSIZE;
    946 	if (dg->dg_secsize > DEV_BSIZE)
    947 		dgratio = dg->dg_secsize / DEV_BSIZE;
    948 
    949 	/* Sanity check */
    950 	if ((uint64_t)lp->d_secperunit * lpratio > dg->dg_secperunit * dgratio)
    951 		printf("WARNING: %s: "
    952 		    "total unit size in disklabel (%" PRIu64 ") "
    953 		    "!= the size of %s (%" PRIu64 ")\n", dksc->sc_xname,
    954 		    (uint64_t)lp->d_secperunit * lpratio, dksc->sc_xname,
    955 		    dg->dg_secperunit * dgratio);
    956 	else if (lp->d_secperunit < UINT32_MAX &&
    957 	    (uint64_t)lp->d_secperunit * lpratio < dg->dg_secperunit * dgratio)
    958 		printf("%s: %" PRIu64 " trailing sectors not covered"
    959 		    " by disklabel\n", dksc->sc_xname,
    960 		    (dg->dg_secperunit * dgratio)
    961 		    - (lp->d_secperunit * lpratio));
    962 
    963 	for (i=0; i < lp->d_npartitions; i++) {
    964 		uint64_t pend;
    965 
    966 		pp = &lp->d_partitions[i];
    967 		pend = pp->p_offset + pp->p_size;
    968 		if (pend * lpratio > dg->dg_secperunit * dgratio)
    969 			printf("WARNING: %s: end of partition `%c' exceeds "
    970 			    "the size of %s (%" PRIu64 ")\n", dksc->sc_xname,
    971 			    'a' + i, dksc->sc_xname,
    972 			    dg->dg_secperunit * dgratio);
    973 	}
    974 }
    975 
    976 /*
    977  * Heuristic to conjure a disklabel if reading a disklabel failed.
    978  *
    979  * This is to allow the raw partition to be used for a filesystem
    980  * without caring about the write protected label sector.
    981  *
    982  * If the driver provides it's own callback, use that instead.
    983  */
    984 /* ARGSUSED */
    985 static void
    986 dk_makedisklabel(struct dk_softc *dksc)
    987 {
    988 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    989 	struct  disklabel *lp = dksc->sc_dkdev.dk_label;
    990 
    991 	strlcpy(lp->d_packname, "default label", sizeof(lp->d_packname));
    992 
    993 	if (dkd->d_label)
    994 		dkd->d_label(dksc->sc_dev, lp);
    995 	else
    996 		lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
    997 
    998 	lp->d_checksum = dkcksum(lp);
    999 }
   1000 
   1001 MODULE(MODULE_CLASS_MISC, dk_subr, NULL);
   1002 
   1003 static int
   1004 dk_subr_modcmd(modcmd_t cmd, void *arg)
   1005 {
   1006 	switch (cmd) {
   1007 	case MODULE_CMD_INIT:
   1008 	case MODULE_CMD_FINI:
   1009 		return 0;
   1010 	case MODULE_CMD_STAT:
   1011 	case MODULE_CMD_AUTOUNLOAD:
   1012 	default:
   1013 		return ENOTTY;
   1014 	}
   1015 }
   1016