Home | History | Annotate | Line # | Download | only in dkwedge
dk.c revision 1.91.2.2
      1 /*	$NetBSD: dk.c,v 1.91.2.2 2017/01/07 08:56:31 pgoyette Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2004, 2005, 2006, 2007 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.
      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: dk.c,v 1.91.2.2 2017/01/07 08:56:31 pgoyette Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_dkwedge.h"
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/proc.h>
     42 #include <sys/errno.h>
     43 #include <sys/pool.h>
     44 #include <sys/ioctl.h>
     45 #include <sys/disklabel.h>
     46 #include <sys/disk.h>
     47 #include <sys/fcntl.h>
     48 #include <sys/buf.h>
     49 #include <sys/bufq.h>
     50 #include <sys/vnode.h>
     51 #include <sys/stat.h>
     52 #include <sys/conf.h>
     53 #include <sys/callout.h>
     54 #include <sys/kernel.h>
     55 #include <sys/malloc.h>
     56 #include <sys/device.h>
     57 #include <sys/kauth.h>
     58 
     59 #include <miscfs/specfs/specdev.h>
     60 
     61 MALLOC_DEFINE(M_DKWEDGE, "dkwedge", "Disk wedge structures");
     62 
     63 typedef enum {
     64 	DKW_STATE_LARVAL	= 0,
     65 	DKW_STATE_RUNNING	= 1,
     66 	DKW_STATE_DYING		= 2,
     67 	DKW_STATE_DEAD		= 666
     68 } dkwedge_state_t;
     69 
     70 struct dkwedge_softc {
     71 	device_t	sc_dev;	/* pointer to our pseudo-device */
     72 	struct cfdata	sc_cfdata;	/* our cfdata structure */
     73 	uint8_t		sc_wname[128];	/* wedge name (Unicode, UTF-8) */
     74 
     75 	dkwedge_state_t sc_state;	/* state this wedge is in */
     76 
     77 	struct disk	*sc_parent;	/* parent disk */
     78 	daddr_t		sc_offset;	/* LBA offset of wedge in parent */
     79 	uint64_t	sc_size;	/* size of wedge in blocks */
     80 	char		sc_ptype[32];	/* partition type */
     81 	dev_t		sc_pdev;	/* cached parent's dev_t */
     82 					/* link on parent's wedge list */
     83 	LIST_ENTRY(dkwedge_softc) sc_plink;
     84 
     85 	struct disk	sc_dk;		/* our own disk structure */
     86 	struct bufq_state *sc_bufq;	/* buffer queue */
     87 	struct callout	sc_restart_ch;	/* callout to restart I/O */
     88 
     89 	kmutex_t	sc_iolock;
     90 	kcondvar_t	sc_dkdrn;
     91 	u_int		sc_iopend;	/* I/Os pending */
     92 	int		sc_flags;	/* flags (sc_iolock) */
     93 };
     94 
     95 #define	DK_F_WAIT_DRAIN		0x0001	/* waiting for I/O to drain */
     96 
     97 static void	dkstart(struct dkwedge_softc *);
     98 static void	dkiodone(struct buf *);
     99 static void	dkrestart(void *);
    100 static void	dkminphys(struct buf *);
    101 
    102 static int	dklastclose(struct dkwedge_softc *);
    103 static int	dkwedge_cleanup_parent(struct dkwedge_softc *, int);
    104 static int	dkwedge_detach(device_t, int);
    105 static void	dkwedge_delall1(struct disk *, bool);
    106 static int	dkwedge_del1(struct dkwedge_info *, int);
    107 static int	dk_open_parent(dev_t, int, struct vnode **);
    108 static int	dk_close_parent(struct vnode *, int);
    109 
    110 static dev_type_open(dkopen);
    111 static dev_type_close(dkclose);
    112 static dev_type_read(dkread);
    113 static dev_type_write(dkwrite);
    114 static dev_type_ioctl(dkioctl);
    115 static dev_type_strategy(dkstrategy);
    116 static dev_type_dump(dkdump);
    117 static dev_type_size(dksize);
    118 static dev_type_discard(dkdiscard);
    119 
    120 const struct bdevsw dk_bdevsw = {
    121 	.d_open = dkopen,
    122 	.d_close = dkclose,
    123 	.d_strategy = dkstrategy,
    124 	.d_ioctl = dkioctl,
    125 	.d_dump = dkdump,
    126 	.d_psize = dksize,
    127 	.d_discard = dkdiscard,
    128 	.d_flag = D_DISK | D_MPSAFE
    129 };
    130 
    131 const struct cdevsw dk_cdevsw = {
    132 	.d_open = dkopen,
    133 	.d_close = dkclose,
    134 	.d_read = dkread,
    135 	.d_write = dkwrite,
    136 	.d_ioctl = dkioctl,
    137 	.d_stop = nostop,
    138 	.d_tty = notty,
    139 	.d_poll = nopoll,
    140 	.d_mmap = nommap,
    141 	.d_kqfilter = nokqfilter,
    142 	.d_discard = dkdiscard,
    143 	.d_flag = D_DISK | D_MPSAFE
    144 };
    145 
    146 static struct dkwedge_softc **dkwedges;
    147 static u_int ndkwedges;
    148 static krwlock_t dkwedges_lock;
    149 
    150 static LIST_HEAD(, dkwedge_discovery_method) dkwedge_discovery_methods;
    151 static krwlock_t dkwedge_discovery_methods_lock;
    152 
    153 /*
    154  * dkwedge_match:
    155  *
    156  *	Autoconfiguration match function for pseudo-device glue.
    157  */
    158 static int
    159 dkwedge_match(device_t parent, cfdata_t match,
    160     void *aux)
    161 {
    162 
    163 	/* Pseudo-device; always present. */
    164 	return (1);
    165 }
    166 
    167 /*
    168  * dkwedge_attach:
    169  *
    170  *	Autoconfiguration attach function for pseudo-device glue.
    171  */
    172 static void
    173 dkwedge_attach(device_t parent, device_t self,
    174     void *aux)
    175 {
    176 
    177 	if (!pmf_device_register(self, NULL, NULL))
    178 		aprint_error_dev(self, "couldn't establish power handler\n");
    179 }
    180 
    181 CFDRIVER_DECL(dk, DV_DISK, NULL);
    182 CFATTACH_DECL3_NEW(dk, 0,
    183     dkwedge_match, dkwedge_attach, dkwedge_detach, NULL, NULL, NULL,
    184     DVF_DETACH_SHUTDOWN);
    185 
    186 /*
    187  * dkwedge_wait_drain:
    188  *
    189  *	Wait for I/O on the wedge to drain.
    190  */
    191 static void
    192 dkwedge_wait_drain(struct dkwedge_softc *sc)
    193 {
    194 
    195 	mutex_enter(&sc->sc_iolock);
    196 	while (sc->sc_iopend != 0) {
    197 		sc->sc_flags |= DK_F_WAIT_DRAIN;
    198 		cv_wait(&sc->sc_dkdrn, &sc->sc_iolock);
    199 	}
    200 	mutex_exit(&sc->sc_iolock);
    201 }
    202 
    203 /*
    204  * dkwedge_compute_pdev:
    205  *
    206  *	Compute the parent disk's dev_t.
    207  */
    208 static int
    209 dkwedge_compute_pdev(const char *pname, dev_t *pdevp, enum vtype type)
    210 {
    211 	const char *name, *cp;
    212 	devmajor_t pmaj;
    213 	int punit;
    214 	char devname[16];
    215 
    216 	name = pname;
    217 	switch (type) {
    218 	case VBLK:
    219 		pmaj = devsw_name2blk(name, devname, sizeof(devname));
    220 		break;
    221 	case VCHR:
    222 		pmaj = devsw_name2chr(name, devname, sizeof(devname));
    223 		break;
    224 	default:
    225 		pmaj = NODEVMAJOR;
    226 		break;
    227 	}
    228 	if (pmaj == NODEVMAJOR)
    229 		return (ENODEV);
    230 
    231 	name += strlen(devname);
    232 	for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++)
    233 		punit = (punit * 10) + (*cp - '0');
    234 	if (cp == name) {
    235 		/* Invalid parent disk name. */
    236 		return (ENODEV);
    237 	}
    238 
    239 	*pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART);
    240 
    241 	return (0);
    242 }
    243 
    244 /*
    245  * dkwedge_array_expand:
    246  *
    247  *	Expand the dkwedges array.
    248  */
    249 static void
    250 dkwedge_array_expand(void)
    251 {
    252 	int newcnt = ndkwedges + 16;
    253 	struct dkwedge_softc **newarray, **oldarray;
    254 
    255 	newarray = malloc(newcnt * sizeof(*newarray), M_DKWEDGE,
    256 	    M_WAITOK|M_ZERO);
    257 	if ((oldarray = dkwedges) != NULL)
    258 		memcpy(newarray, dkwedges, ndkwedges * sizeof(*newarray));
    259 	dkwedges = newarray;
    260 	ndkwedges = newcnt;
    261 	if (oldarray != NULL)
    262 		free(oldarray, M_DKWEDGE);
    263 }
    264 
    265 static void
    266 dk_set_geometry(struct dkwedge_softc *sc, struct disk *pdk)
    267 {
    268 	struct disk *dk = &sc->sc_dk;
    269 	struct disk_geom *dg = &dk->dk_geom;
    270 
    271 	memset(dg, 0, sizeof(*dg));
    272 
    273 	dg->dg_secperunit = sc->sc_size;
    274 	dg->dg_secsize = DEV_BSIZE << pdk->dk_blkshift;
    275 
    276 	/* fake numbers, 1 cylinder is 1 MB with default sector size */
    277 	dg->dg_nsectors = 32;
    278 	dg->dg_ntracks = 64;
    279 	dg->dg_ncylinders = dg->dg_secperunit / (dg->dg_nsectors * dg->dg_ntracks);
    280 
    281 	disk_set_info(sc->sc_dev, dk, NULL);
    282 }
    283 
    284 /*
    285  * dkwedge_add:		[exported function]
    286  *
    287  *	Add a disk wedge based on the provided information.
    288  *
    289  *	The incoming dkw_devname[] is ignored, instead being
    290  *	filled in and returned to the caller.
    291  */
    292 int
    293 dkwedge_add(struct dkwedge_info *dkw)
    294 {
    295 	struct dkwedge_softc *sc, *lsc;
    296 	struct disk *pdk;
    297 	u_int unit;
    298 	int error;
    299 	dev_t pdev;
    300 
    301 	dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0';
    302 	pdk = disk_find(dkw->dkw_parent);
    303 	if (pdk == NULL)
    304 		return (ENODEV);
    305 
    306 	error = dkwedge_compute_pdev(pdk->dk_name, &pdev, VBLK);
    307 	if (error)
    308 		return (error);
    309 
    310 	if (dkw->dkw_offset < 0)
    311 		return (EINVAL);
    312 
    313 	sc = malloc(sizeof(*sc), M_DKWEDGE, M_WAITOK|M_ZERO);
    314 	sc->sc_state = DKW_STATE_LARVAL;
    315 	sc->sc_parent = pdk;
    316 	sc->sc_pdev = pdev;
    317 	sc->sc_offset = dkw->dkw_offset;
    318 	sc->sc_size = dkw->dkw_size;
    319 
    320 	memcpy(sc->sc_wname, dkw->dkw_wname, sizeof(sc->sc_wname));
    321 	sc->sc_wname[sizeof(sc->sc_wname) - 1] = '\0';
    322 
    323 	memcpy(sc->sc_ptype, dkw->dkw_ptype, sizeof(sc->sc_ptype));
    324 	sc->sc_ptype[sizeof(sc->sc_ptype) - 1] = '\0';
    325 
    326 	bufq_alloc(&sc->sc_bufq, "fcfs", 0);
    327 
    328 	callout_init(&sc->sc_restart_ch, 0);
    329 	callout_setfunc(&sc->sc_restart_ch, dkrestart, sc);
    330 
    331 	mutex_init(&sc->sc_iolock, MUTEX_DEFAULT, IPL_BIO);
    332 	cv_init(&sc->sc_dkdrn, "dkdrn");
    333 
    334 	/*
    335 	 * Wedge will be added; increment the wedge count for the parent.
    336 	 * Only allow this to happend if RAW_PART is the only thing open.
    337 	 */
    338 	mutex_enter(&pdk->dk_openlock);
    339 	if (pdk->dk_openmask & ~(1 << RAW_PART))
    340 		error = EBUSY;
    341 	else {
    342 		/* Check for wedge overlap. */
    343 		LIST_FOREACH(lsc, &pdk->dk_wedges, sc_plink) {
    344 			daddr_t lastblk = sc->sc_offset + sc->sc_size - 1;
    345 			daddr_t llastblk = lsc->sc_offset + lsc->sc_size - 1;
    346 
    347 			if (sc->sc_offset >= lsc->sc_offset &&
    348 			    sc->sc_offset <= llastblk) {
    349 				/* Overlaps the tail of the existing wedge. */
    350 				break;
    351 			}
    352 			if (lastblk >= lsc->sc_offset &&
    353 			    lastblk <= llastblk) {
    354 				/* Overlaps the head of the existing wedge. */
    355 			    	break;
    356 			}
    357 		}
    358 		if (lsc != NULL) {
    359 			if (sc->sc_offset == lsc->sc_offset &&
    360 			    sc->sc_size == lsc->sc_size &&
    361 			    strcmp(sc->sc_wname, lsc->sc_wname) == 0)
    362 				error = EEXIST;
    363 			else
    364 				error = EINVAL;
    365 		} else {
    366 			pdk->dk_nwedges++;
    367 			LIST_INSERT_HEAD(&pdk->dk_wedges, sc, sc_plink);
    368 		}
    369 	}
    370 	mutex_exit(&pdk->dk_openlock);
    371 	if (error) {
    372 		cv_destroy(&sc->sc_dkdrn);
    373 		mutex_destroy(&sc->sc_iolock);
    374 		bufq_free(sc->sc_bufq);
    375 		free(sc, M_DKWEDGE);
    376 		return (error);
    377 	}
    378 
    379 	/* Fill in our cfdata for the pseudo-device glue. */
    380 	sc->sc_cfdata.cf_name = dk_cd.cd_name;
    381 	sc->sc_cfdata.cf_atname = dk_ca.ca_name;
    382 	/* sc->sc_cfdata.cf_unit set below */
    383 	sc->sc_cfdata.cf_fstate = FSTATE_STAR;
    384 
    385 	/* Insert the larval wedge into the array. */
    386 	rw_enter(&dkwedges_lock, RW_WRITER);
    387 	for (error = 0;;) {
    388 		struct dkwedge_softc **scpp;
    389 
    390 		/*
    391 		 * Check for a duplicate wname while searching for
    392 		 * a slot.
    393 		 */
    394 		for (scpp = NULL, unit = 0; unit < ndkwedges; unit++) {
    395 			if (dkwedges[unit] == NULL) {
    396 				if (scpp == NULL) {
    397 					scpp = &dkwedges[unit];
    398 					sc->sc_cfdata.cf_unit = unit;
    399 				}
    400 			} else {
    401 				/* XXX Unicode. */
    402 				if (strcmp(dkwedges[unit]->sc_wname,
    403 					   sc->sc_wname) == 0) {
    404 					error = EEXIST;
    405 					break;
    406 				}
    407 			}
    408 		}
    409 		if (error)
    410 			break;
    411 		KASSERT(unit == ndkwedges);
    412 		if (scpp == NULL)
    413 			dkwedge_array_expand();
    414 		else {
    415 			KASSERT(scpp == &dkwedges[sc->sc_cfdata.cf_unit]);
    416 			*scpp = sc;
    417 			break;
    418 		}
    419 	}
    420 	rw_exit(&dkwedges_lock);
    421 	if (error) {
    422 		mutex_enter(&pdk->dk_openlock);
    423 		pdk->dk_nwedges--;
    424 		LIST_REMOVE(sc, sc_plink);
    425 		mutex_exit(&pdk->dk_openlock);
    426 
    427 		cv_destroy(&sc->sc_dkdrn);
    428 		mutex_destroy(&sc->sc_iolock);
    429 		bufq_free(sc->sc_bufq);
    430 		free(sc, M_DKWEDGE);
    431 		return (error);
    432 	}
    433 
    434 	/*
    435 	 * Now that we know the unit #, attach a pseudo-device for
    436 	 * this wedge instance.  This will provide us with the
    437 	 * device_t necessary for glue to other parts of the system.
    438 	 *
    439 	 * This should never fail, unless we're almost totally out of
    440 	 * memory.
    441 	 */
    442 	if ((sc->sc_dev = config_attach_pseudo(&sc->sc_cfdata)) == NULL) {
    443 		aprint_error("%s%u: unable to attach pseudo-device\n",
    444 		    sc->sc_cfdata.cf_name, sc->sc_cfdata.cf_unit);
    445 
    446 		rw_enter(&dkwedges_lock, RW_WRITER);
    447 		dkwedges[sc->sc_cfdata.cf_unit] = NULL;
    448 		rw_exit(&dkwedges_lock);
    449 
    450 		mutex_enter(&pdk->dk_openlock);
    451 		pdk->dk_nwedges--;
    452 		LIST_REMOVE(sc, sc_plink);
    453 		mutex_exit(&pdk->dk_openlock);
    454 
    455 		cv_destroy(&sc->sc_dkdrn);
    456 		mutex_destroy(&sc->sc_iolock);
    457 		bufq_free(sc->sc_bufq);
    458 		free(sc, M_DKWEDGE);
    459 		return (ENOMEM);
    460 	}
    461 
    462 	/* Return the devname to the caller. */
    463 	strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
    464 		sizeof(dkw->dkw_devname));
    465 
    466 	/*
    467 	 * XXX Really ought to make the disk_attach() and the changing
    468 	 * of state to RUNNING atomic.
    469 	 */
    470 
    471 	disk_init(&sc->sc_dk, device_xname(sc->sc_dev), NULL);
    472 	dk_set_geometry(sc, pdk);
    473 	disk_attach(&sc->sc_dk);
    474 
    475 	/* Disk wedge is ready for use! */
    476 	sc->sc_state = DKW_STATE_RUNNING;
    477 
    478 	/* Announce our arrival. */
    479 	aprint_normal(
    480 	    "%s at %s: \"%s\", %"PRIu64" blocks at %"PRId64", type: %s\n",
    481 	    device_xname(sc->sc_dev), pdk->dk_name,
    482 	    sc->sc_wname,	/* XXX Unicode */
    483 	    sc->sc_size, sc->sc_offset,
    484 	    sc->sc_ptype[0] == '\0' ? "<unknown>" : sc->sc_ptype);
    485 
    486 	return (0);
    487 }
    488 
    489 /*
    490  * dkwedge_find:
    491  *
    492  *	Lookup a disk wedge based on the provided information.
    493  *	NOTE: We look up the wedge based on the wedge devname,
    494  *	not wname.
    495  *
    496  *	Return NULL if the wedge is not found, otherwise return
    497  *	the wedge's softc.  Assign the wedge's unit number to unitp
    498  *	if unitp is not NULL.
    499  */
    500 static struct dkwedge_softc *
    501 dkwedge_find(struct dkwedge_info *dkw, u_int *unitp)
    502 {
    503 	struct dkwedge_softc *sc = NULL;
    504 	u_int unit;
    505 
    506 	/* Find our softc. */
    507 	dkw->dkw_devname[sizeof(dkw->dkw_devname) - 1] = '\0';
    508 	rw_enter(&dkwedges_lock, RW_READER);
    509 	for (unit = 0; unit < ndkwedges; unit++) {
    510 		if ((sc = dkwedges[unit]) != NULL &&
    511 		    strcmp(device_xname(sc->sc_dev), dkw->dkw_devname) == 0 &&
    512 		    strcmp(sc->sc_parent->dk_name, dkw->dkw_parent) == 0) {
    513 			break;
    514 		}
    515 	}
    516 	rw_exit(&dkwedges_lock);
    517 	if (unit == ndkwedges)
    518 		return NULL;
    519 
    520 	if (unitp != NULL)
    521 		*unitp = unit;
    522 
    523 	return sc;
    524 }
    525 
    526 /*
    527  * dkwedge_del:		[exported function]
    528  *
    529  *	Delete a disk wedge based on the provided information.
    530  *	NOTE: We look up the wedge based on the wedge devname,
    531  *	not wname.
    532  */
    533 int
    534 dkwedge_del(struct dkwedge_info *dkw)
    535 {
    536 	return dkwedge_del1(dkw, 0);
    537 }
    538 
    539 int
    540 dkwedge_del1(struct dkwedge_info *dkw, int flags)
    541 {
    542 	struct dkwedge_softc *sc = NULL;
    543 
    544 	/* Find our softc. */
    545 	if ((sc = dkwedge_find(dkw, NULL)) == NULL)
    546 		return (ESRCH);
    547 
    548 	return config_detach(sc->sc_dev, flags);
    549 }
    550 
    551 static int
    552 dkwedge_cleanup_parent(struct dkwedge_softc *sc, int flags)
    553 {
    554 	struct disk *dk = &sc->sc_dk;
    555 	int rc;
    556 
    557 	rc = 0;
    558 	mutex_enter(&dk->dk_openlock);
    559 	if (dk->dk_openmask == 0)
    560 		/* nothing to do */
    561 		mutex_exit(&dk->dk_openlock);
    562 	else if ((flags & DETACH_FORCE) == 0) {
    563 		rc = EBUSY;
    564 		mutex_exit(&dk->dk_openlock);
    565 	}  else {
    566 		mutex_enter(&sc->sc_parent->dk_rawlock);
    567 		rc = dklastclose(sc); /* releases locks */
    568 	}
    569 
    570 	return rc;
    571 }
    572 
    573 /*
    574  * dkwedge_detach:
    575  *
    576  *	Autoconfiguration detach function for pseudo-device glue.
    577  */
    578 static int
    579 dkwedge_detach(device_t self, int flags)
    580 {
    581 	struct dkwedge_softc *sc = NULL;
    582 	u_int unit;
    583 	int bmaj, cmaj, rc;
    584 
    585 	rw_enter(&dkwedges_lock, RW_WRITER);
    586 	for (unit = 0; unit < ndkwedges; unit++) {
    587 		if ((sc = dkwedges[unit]) != NULL && sc->sc_dev == self)
    588 			break;
    589 	}
    590 	if (unit == ndkwedges)
    591 		rc = ENXIO;
    592 	else if ((rc = dkwedge_cleanup_parent(sc, flags)) == 0) {
    593 		/* Mark the wedge as dying. */
    594 		sc->sc_state = DKW_STATE_DYING;
    595 	}
    596 	rw_exit(&dkwedges_lock);
    597 
    598 	if (rc != 0)
    599 		return rc;
    600 
    601 	pmf_device_deregister(self);
    602 
    603 	/* Locate the wedge major numbers. */
    604 	bmaj = bdevsw_lookup_major(&dk_bdevsw);
    605 	cmaj = cdevsw_lookup_major(&dk_cdevsw);
    606 
    607 	/* Kill any pending restart. */
    608 	callout_stop(&sc->sc_restart_ch);
    609 
    610 	/*
    611 	 * dkstart() will kill any queued buffers now that the
    612 	 * state of the wedge is not RUNNING.  Once we've done
    613 	 * that, wait for any other pending I/O to complete.
    614 	 */
    615 	dkstart(sc);
    616 	dkwedge_wait_drain(sc);
    617 
    618 	/* Nuke the vnodes for any open instances. */
    619 	vdevgone(bmaj, unit, unit, VBLK);
    620 	vdevgone(cmaj, unit, unit, VCHR);
    621 
    622 	/* Clean up the parent. */
    623 	dkwedge_cleanup_parent(sc, flags | DETACH_FORCE);
    624 
    625 	/* Announce our departure. */
    626 	aprint_normal("%s at %s (%s) deleted\n", device_xname(sc->sc_dev),
    627 	    sc->sc_parent->dk_name,
    628 	    sc->sc_wname);	/* XXX Unicode */
    629 
    630 	mutex_enter(&sc->sc_parent->dk_openlock);
    631 	sc->sc_parent->dk_nwedges--;
    632 	LIST_REMOVE(sc, sc_plink);
    633 	mutex_exit(&sc->sc_parent->dk_openlock);
    634 
    635 	/* Delete our buffer queue. */
    636 	bufq_free(sc->sc_bufq);
    637 
    638 	/* Detach from the disk list. */
    639 	disk_detach(&sc->sc_dk);
    640 	disk_destroy(&sc->sc_dk);
    641 
    642 	/* Poof. */
    643 	rw_enter(&dkwedges_lock, RW_WRITER);
    644 	dkwedges[unit] = NULL;
    645 	sc->sc_state = DKW_STATE_DEAD;
    646 	rw_exit(&dkwedges_lock);
    647 
    648 	mutex_destroy(&sc->sc_iolock);
    649 	cv_destroy(&sc->sc_dkdrn);
    650 
    651 	free(sc, M_DKWEDGE);
    652 
    653 	return 0;
    654 }
    655 
    656 /*
    657  * dkwedge_delall:	[exported function]
    658  *
    659  *	Delete all of the wedges on the specified disk.  Used when
    660  *	a disk is being detached.
    661  */
    662 void
    663 dkwedge_delall(struct disk *pdk)
    664 {
    665 	dkwedge_delall1(pdk, false);
    666 }
    667 
    668 static void
    669 dkwedge_delall1(struct disk *pdk, bool idleonly)
    670 {
    671 	struct dkwedge_info dkw;
    672 	struct dkwedge_softc *sc;
    673 	int flags;
    674 
    675 	flags = DETACH_QUIET;
    676 	if (!idleonly) flags |= DETACH_FORCE;
    677 
    678 	for (;;) {
    679 		mutex_enter(&pdk->dk_openlock);
    680 		LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) {
    681 			if (!idleonly || sc->sc_dk.dk_openmask == 0)
    682 				break;
    683 		}
    684 		if (sc == NULL) {
    685 			KASSERT(idleonly || pdk->dk_nwedges == 0);
    686 			mutex_exit(&pdk->dk_openlock);
    687 			return;
    688 		}
    689 		strcpy(dkw.dkw_parent, pdk->dk_name);
    690 		strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
    691 			sizeof(dkw.dkw_devname));
    692 		mutex_exit(&pdk->dk_openlock);
    693 		(void) dkwedge_del1(&dkw, flags);
    694 	}
    695 }
    696 
    697 /*
    698  * dkwedge_list:	[exported function]
    699  *
    700  *	List all of the wedges on a particular disk.
    701  */
    702 int
    703 dkwedge_list(struct disk *pdk, struct dkwedge_list *dkwl, struct lwp *l)
    704 {
    705 	struct uio uio;
    706 	struct iovec iov;
    707 	struct dkwedge_softc *sc;
    708 	struct dkwedge_info dkw;
    709 	int error = 0;
    710 
    711 	iov.iov_base = dkwl->dkwl_buf;
    712 	iov.iov_len = dkwl->dkwl_bufsize;
    713 
    714 	uio.uio_iov = &iov;
    715 	uio.uio_iovcnt = 1;
    716 	uio.uio_offset = 0;
    717 	uio.uio_resid = dkwl->dkwl_bufsize;
    718 	uio.uio_rw = UIO_READ;
    719 	KASSERT(l == curlwp);
    720 	uio.uio_vmspace = l->l_proc->p_vmspace;
    721 
    722 	dkwl->dkwl_ncopied = 0;
    723 
    724 	mutex_enter(&pdk->dk_openlock);
    725 	LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) {
    726 		if (uio.uio_resid < sizeof(dkw))
    727 			break;
    728 
    729 		if (sc->sc_state != DKW_STATE_RUNNING)
    730 			continue;
    731 
    732 		strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
    733 			sizeof(dkw.dkw_devname));
    734 		memcpy(dkw.dkw_wname, sc->sc_wname, sizeof(dkw.dkw_wname));
    735 		dkw.dkw_wname[sizeof(dkw.dkw_wname) - 1] = '\0';
    736 		strcpy(dkw.dkw_parent, sc->sc_parent->dk_name);
    737 		dkw.dkw_offset = sc->sc_offset;
    738 		dkw.dkw_size = sc->sc_size;
    739 		strcpy(dkw.dkw_ptype, sc->sc_ptype);
    740 
    741 		error = uiomove(&dkw, sizeof(dkw), &uio);
    742 		if (error)
    743 			break;
    744 		dkwl->dkwl_ncopied++;
    745 	}
    746 	dkwl->dkwl_nwedges = pdk->dk_nwedges;
    747 	mutex_exit(&pdk->dk_openlock);
    748 
    749 	return (error);
    750 }
    751 
    752 device_t
    753 dkwedge_find_by_wname(const char *wname)
    754 {
    755 	device_t dv = NULL;
    756 	struct dkwedge_softc *sc;
    757 	int i;
    758 
    759 	rw_enter(&dkwedges_lock, RW_WRITER);
    760 	for (i = 0; i < ndkwedges; i++) {
    761 		if ((sc = dkwedges[i]) == NULL)
    762 			continue;
    763 		if (strcmp(sc->sc_wname, wname) == 0) {
    764 			if (dv != NULL) {
    765 				printf(
    766 				    "WARNING: double match for wedge name %s "
    767 				    "(%s, %s)\n", wname, device_xname(dv),
    768 				    device_xname(sc->sc_dev));
    769 				continue;
    770 			}
    771 			dv = sc->sc_dev;
    772 		}
    773 	}
    774 	rw_exit(&dkwedges_lock);
    775 	return dv;
    776 }
    777 
    778 device_t
    779 dkwedge_find_by_parent(const char *name, size_t *i)
    780 {
    781 	rw_enter(&dkwedges_lock, RW_WRITER);
    782 	for (; *i < (size_t)ndkwedges; (*i)++) {
    783 		struct dkwedge_softc *sc;
    784 		if ((sc = dkwedges[*i]) == NULL)
    785 			continue;
    786 		if (strcmp(sc->sc_parent->dk_name, name) != 0)
    787 			continue;
    788 		rw_exit(&dkwedges_lock);
    789 		return sc->sc_dev;
    790 	}
    791 	rw_exit(&dkwedges_lock);
    792 	return NULL;
    793 }
    794 
    795 void
    796 dkwedge_print_wnames(void)
    797 {
    798 	struct dkwedge_softc *sc;
    799 	int i;
    800 
    801 	rw_enter(&dkwedges_lock, RW_WRITER);
    802 	for (i = 0; i < ndkwedges; i++) {
    803 		if ((sc = dkwedges[i]) == NULL)
    804 			continue;
    805 		printf(" wedge:%s", sc->sc_wname);
    806 	}
    807 	rw_exit(&dkwedges_lock);
    808 }
    809 
    810 /*
    811  * We need a dummy object to stuff into the dkwedge discovery method link
    812  * set to ensure that there is always at least one object in the set.
    813  */
    814 static struct dkwedge_discovery_method dummy_discovery_method;
    815 __link_set_add_bss(dkwedge_methods, dummy_discovery_method);
    816 
    817 /*
    818  * dkwedge_init:
    819  *
    820  *	Initialize the disk wedge subsystem.
    821  */
    822 void
    823 dkwedge_init(void)
    824 {
    825 	__link_set_decl(dkwedge_methods, struct dkwedge_discovery_method);
    826 	struct dkwedge_discovery_method * const *ddmp;
    827 	struct dkwedge_discovery_method *lddm, *ddm;
    828 
    829 	rw_init(&dkwedges_lock);
    830 	rw_init(&dkwedge_discovery_methods_lock);
    831 
    832 	if (config_cfdriver_attach(&dk_cd) != 0)
    833 		panic("dkwedge: unable to attach cfdriver");
    834 	if (config_cfattach_attach(dk_cd.cd_name, &dk_ca) != 0)
    835 		panic("dkwedge: unable to attach cfattach");
    836 
    837 	rw_enter(&dkwedge_discovery_methods_lock, RW_WRITER);
    838 
    839 	LIST_INIT(&dkwedge_discovery_methods);
    840 
    841 	__link_set_foreach(ddmp, dkwedge_methods) {
    842 		ddm = *ddmp;
    843 		if (ddm == &dummy_discovery_method)
    844 			continue;
    845 		if (LIST_EMPTY(&dkwedge_discovery_methods)) {
    846 			LIST_INSERT_HEAD(&dkwedge_discovery_methods,
    847 					 ddm, ddm_list);
    848 			continue;
    849 		}
    850 		LIST_FOREACH(lddm, &dkwedge_discovery_methods, ddm_list) {
    851 			if (ddm->ddm_priority == lddm->ddm_priority) {
    852 				aprint_error("dk-method-%s: method \"%s\" "
    853 				    "already exists at priority %d\n",
    854 				    ddm->ddm_name, lddm->ddm_name,
    855 				    lddm->ddm_priority);
    856 				/* Not inserted. */
    857 				break;
    858 			}
    859 			if (ddm->ddm_priority < lddm->ddm_priority) {
    860 				/* Higher priority; insert before. */
    861 				LIST_INSERT_BEFORE(lddm, ddm, ddm_list);
    862 				break;
    863 			}
    864 			if (LIST_NEXT(lddm, ddm_list) == NULL) {
    865 				/* Last one; insert after. */
    866 				KASSERT(lddm->ddm_priority < ddm->ddm_priority);
    867 				LIST_INSERT_AFTER(lddm, ddm, ddm_list);
    868 				break;
    869 			}
    870 		}
    871 	}
    872 
    873 	rw_exit(&dkwedge_discovery_methods_lock);
    874 }
    875 
    876 #ifdef DKWEDGE_AUTODISCOVER
    877 int	dkwedge_autodiscover = 1;
    878 #else
    879 int	dkwedge_autodiscover = 0;
    880 #endif
    881 
    882 /*
    883  * dkwedge_discover:	[exported function]
    884  *
    885  *	Discover the wedges on a newly attached disk.
    886  *	Remove all unused wedges on the disk first.
    887  */
    888 void
    889 dkwedge_discover(struct disk *pdk)
    890 {
    891 	struct dkwedge_discovery_method *ddm;
    892 	struct vnode *vp;
    893 	int error;
    894 	dev_t pdev;
    895 
    896 	/*
    897 	 * Require people playing with wedges to enable this explicitly.
    898 	 */
    899 	if (dkwedge_autodiscover == 0)
    900 		return;
    901 
    902 	rw_enter(&dkwedge_discovery_methods_lock, RW_READER);
    903 
    904 	/*
    905 	 * Use the character device for scanning, the block device
    906 	 * is busy if there are already wedges attached.
    907 	 */
    908 	error = dkwedge_compute_pdev(pdk->dk_name, &pdev, VCHR);
    909 	if (error) {
    910 		aprint_error("%s: unable to compute pdev, error = %d\n",
    911 		    pdk->dk_name, error);
    912 		goto out;
    913 	}
    914 
    915 	error = cdevvp(pdev, &vp);
    916 	if (error) {
    917 		aprint_error("%s: unable to find vnode for pdev, error = %d\n",
    918 		    pdk->dk_name, error);
    919 		goto out;
    920 	}
    921 
    922 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    923 	if (error) {
    924 		aprint_error("%s: unable to lock vnode for pdev, error = %d\n",
    925 		    pdk->dk_name, error);
    926 		vrele(vp);
    927 		goto out;
    928 	}
    929 
    930 	error = VOP_OPEN(vp, FREAD | FSILENT, NOCRED);
    931 	if (error) {
    932 		if (error != ENODEV)
    933 			aprint_error("%s: unable to open device, error = %d\n",
    934 			    pdk->dk_name, error);
    935 		vput(vp);
    936 		goto out;
    937 	}
    938 	VOP_UNLOCK(vp);
    939 
    940 	/*
    941 	 * Remove unused wedges
    942 	 */
    943 	dkwedge_delall1(pdk, true);
    944 
    945 	/*
    946 	 * For each supported partition map type, look to see if
    947 	 * this map type exists.  If so, parse it and add the
    948 	 * corresponding wedges.
    949 	 */
    950 	LIST_FOREACH(ddm, &dkwedge_discovery_methods, ddm_list) {
    951 		error = (*ddm->ddm_discover)(pdk, vp);
    952 		if (error == 0) {
    953 			/* Successfully created wedges; we're done. */
    954 			break;
    955 		}
    956 	}
    957 
    958 	error = vn_close(vp, FREAD, NOCRED);
    959 	if (error) {
    960 		aprint_error("%s: unable to close device, error = %d\n",
    961 		    pdk->dk_name, error);
    962 		/* We'll just assume the vnode has been cleaned up. */
    963 	}
    964 
    965  out:
    966 	rw_exit(&dkwedge_discovery_methods_lock);
    967 }
    968 
    969 /*
    970  * dkwedge_read:
    971  *
    972  *	Read some data from the specified disk, used for
    973  *	partition discovery.
    974  */
    975 int
    976 dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
    977     void *tbuf, size_t len)
    978 {
    979 	buf_t *bp;
    980 	int error;
    981 	bool isopen;
    982 	dev_t bdev;
    983 	struct vnode *bdvp;
    984 
    985 	/*
    986 	 * The kernel cannot read from a character device vnode
    987 	 * as physio() only handles user memory.
    988 	 *
    989 	 * If the block device has already been opened by a wedge
    990 	 * use that vnode and temporarily bump the open counter.
    991 	 *
    992 	 * Otherwise try to open the block device.
    993 	 */
    994 
    995 	bdev = devsw_chr2blk(vp->v_rdev);
    996 
    997 	mutex_enter(&pdk->dk_rawlock);
    998 	if (pdk->dk_rawopens != 0) {
    999 		KASSERT(pdk->dk_rawvp != NULL);
   1000 		isopen = true;
   1001 		++pdk->dk_rawopens;
   1002 		bdvp = pdk->dk_rawvp;
   1003 		error = 0;
   1004 	} else {
   1005 		isopen = false;
   1006 		error = dk_open_parent(bdev, FREAD, &bdvp);
   1007 	}
   1008 	mutex_exit(&pdk->dk_rawlock);
   1009 
   1010 	if (error)
   1011 		return error;
   1012 
   1013 	bp = getiobuf(bdvp, true);
   1014 	bp->b_flags = B_READ;
   1015 	bp->b_cflags = BC_BUSY;
   1016 	bp->b_dev = bdev;
   1017 	bp->b_data = tbuf;
   1018 	bp->b_bufsize = bp->b_bcount = len;
   1019 	bp->b_blkno = blkno;
   1020 	bp->b_cylinder = 0;
   1021 	bp->b_error = 0;
   1022 
   1023 	VOP_STRATEGY(bdvp, bp);
   1024 	error = biowait(bp);
   1025 	putiobuf(bp);
   1026 
   1027 	mutex_enter(&pdk->dk_rawlock);
   1028 	if (isopen) {
   1029 		--pdk->dk_rawopens;
   1030 	} else {
   1031 		dk_close_parent(bdvp, FREAD);
   1032 	}
   1033 	mutex_exit(&pdk->dk_rawlock);
   1034 
   1035 	return error;
   1036 }
   1037 
   1038 /*
   1039  * dkwedge_lookup:
   1040  *
   1041  *	Look up a dkwedge_softc based on the provided dev_t.
   1042  */
   1043 static struct dkwedge_softc *
   1044 dkwedge_lookup(dev_t dev)
   1045 {
   1046 	int unit = minor(dev);
   1047 
   1048 	if (unit >= ndkwedges)
   1049 		return (NULL);
   1050 
   1051 	KASSERT(dkwedges != NULL);
   1052 
   1053 	return (dkwedges[unit]);
   1054 }
   1055 
   1056 static int
   1057 dk_open_parent(dev_t dev, int mode, struct vnode **vpp)
   1058 {
   1059 	struct vnode *vp;
   1060 	int error;
   1061 
   1062 	error = bdevvp(dev, &vp);
   1063 	if (error)
   1064 		return error;
   1065 
   1066 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1067 	if (error) {
   1068 		vrele(vp);
   1069 		return error;
   1070 	}
   1071 	error = VOP_OPEN(vp, mode, NOCRED);
   1072 	if (error) {
   1073 		vput(vp);
   1074 		return error;
   1075 	}
   1076 
   1077 	/* VOP_OPEN() doesn't do this for us. */
   1078 	if (mode & FWRITE) {
   1079 		mutex_enter(vp->v_interlock);
   1080 		vp->v_writecount++;
   1081 		mutex_exit(vp->v_interlock);
   1082 	}
   1083 
   1084 	VOP_UNLOCK(vp);
   1085 
   1086 	*vpp = vp;
   1087 
   1088 	return 0;
   1089 }
   1090 
   1091 static int
   1092 dk_close_parent(struct vnode *vp, int mode)
   1093 {
   1094 	int error;
   1095 
   1096 	error = vn_close(vp, mode, NOCRED);
   1097 	return error;
   1098 }
   1099 
   1100 /*
   1101  * dkopen:		[devsw entry point]
   1102  *
   1103  *	Open a wedge.
   1104  */
   1105 static int
   1106 dkopen(dev_t dev, int flags, int fmt, struct lwp *l)
   1107 {
   1108 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1109 	struct vnode *vp;
   1110 	int error = 0;
   1111 
   1112 	if (sc == NULL)
   1113 		return (ENODEV);
   1114 	if (sc->sc_state != DKW_STATE_RUNNING)
   1115 		return (ENXIO);
   1116 
   1117 	/*
   1118 	 * We go through a complicated little dance to only open the parent
   1119 	 * vnode once per wedge, no matter how many times the wedge is
   1120 	 * opened.  The reason?  We see one dkopen() per open call, but
   1121 	 * only dkclose() on the last close.
   1122 	 */
   1123 	mutex_enter(&sc->sc_dk.dk_openlock);
   1124 	mutex_enter(&sc->sc_parent->dk_rawlock);
   1125 	if (sc->sc_dk.dk_openmask == 0) {
   1126 		if (sc->sc_parent->dk_rawopens == 0) {
   1127 			KASSERT(sc->sc_parent->dk_rawvp == NULL);
   1128 			error = dk_open_parent(sc->sc_pdev, FREAD | FWRITE, &vp);
   1129 			if (error)
   1130 				goto popen_fail;
   1131 			sc->sc_parent->dk_rawvp = vp;
   1132 		}
   1133 		sc->sc_parent->dk_rawopens++;
   1134 	}
   1135 	if (fmt == S_IFCHR)
   1136 		sc->sc_dk.dk_copenmask |= 1;
   1137 	else
   1138 		sc->sc_dk.dk_bopenmask |= 1;
   1139 	sc->sc_dk.dk_openmask =
   1140 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
   1141 
   1142  popen_fail:
   1143 	mutex_exit(&sc->sc_parent->dk_rawlock);
   1144 	mutex_exit(&sc->sc_dk.dk_openlock);
   1145 	return (error);
   1146 }
   1147 
   1148 /*
   1149  * Caller must hold sc->sc_dk.dk_openlock and sc->sc_parent->dk_rawlock.
   1150  */
   1151 static int
   1152 dklastclose(struct dkwedge_softc *sc)
   1153 {
   1154 	int error = 0, doclose;
   1155 
   1156 	doclose = 0;
   1157 	if (sc->sc_parent->dk_rawopens > 0) {
   1158 		if (--sc->sc_parent->dk_rawopens == 0)
   1159 			doclose = 1;
   1160 	}
   1161 
   1162 	mutex_exit(&sc->sc_parent->dk_rawlock);
   1163 	mutex_exit(&sc->sc_dk.dk_openlock);
   1164 
   1165 	if (doclose) {
   1166 		KASSERT(sc->sc_parent->dk_rawvp != NULL);
   1167 		dk_close_parent(sc->sc_parent->dk_rawvp, FREAD | FWRITE);
   1168 		sc->sc_parent->dk_rawvp = NULL;
   1169 	}
   1170 
   1171 	return error;
   1172 }
   1173 
   1174 /*
   1175  * dkclose:		[devsw entry point]
   1176  *
   1177  *	Close a wedge.
   1178  */
   1179 static int
   1180 dkclose(dev_t dev, int flags, int fmt, struct lwp *l)
   1181 {
   1182 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1183 	int error = 0;
   1184 
   1185 	if (sc == NULL)
   1186 		return (ENODEV);
   1187 	if (sc->sc_state != DKW_STATE_RUNNING)
   1188 		return (ENXIO);
   1189 
   1190 	KASSERT(sc->sc_dk.dk_openmask != 0);
   1191 
   1192 	mutex_enter(&sc->sc_dk.dk_openlock);
   1193 	mutex_enter(&sc->sc_parent->dk_rawlock);
   1194 
   1195 	if (fmt == S_IFCHR)
   1196 		sc->sc_dk.dk_copenmask &= ~1;
   1197 	else
   1198 		sc->sc_dk.dk_bopenmask &= ~1;
   1199 	sc->sc_dk.dk_openmask =
   1200 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
   1201 
   1202 	if (sc->sc_dk.dk_openmask == 0)
   1203 		error = dklastclose(sc); /* releases locks */
   1204 	else {
   1205 		mutex_exit(&sc->sc_parent->dk_rawlock);
   1206 		mutex_exit(&sc->sc_dk.dk_openlock);
   1207 	}
   1208 
   1209 	return (error);
   1210 }
   1211 
   1212 /*
   1213  * dkstragegy:		[devsw entry point]
   1214  *
   1215  *	Perform I/O based on the wedge I/O strategy.
   1216  */
   1217 static void
   1218 dkstrategy(struct buf *bp)
   1219 {
   1220 	struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
   1221 	uint64_t p_size, p_offset;
   1222 
   1223 	if (sc == NULL) {
   1224 		bp->b_error = ENODEV;
   1225 		goto done;
   1226 	}
   1227 
   1228 	if (sc->sc_state != DKW_STATE_RUNNING ||
   1229 	    sc->sc_parent->dk_rawvp == NULL) {
   1230 		bp->b_error = ENXIO;
   1231 		goto done;
   1232 	}
   1233 
   1234 	/* If it's an empty transfer, wake up the top half now. */
   1235 	if (bp->b_bcount == 0)
   1236 		goto done;
   1237 
   1238 	p_offset = sc->sc_offset << sc->sc_parent->dk_blkshift;
   1239 	p_size   = sc->sc_size << sc->sc_parent->dk_blkshift;
   1240 
   1241 	/* Make sure it's in-range. */
   1242 	if (bounds_check_with_mediasize(bp, DEV_BSIZE, p_size) <= 0)
   1243 		goto done;
   1244 
   1245 	/* Translate it to the parent's raw LBA. */
   1246 	bp->b_rawblkno = bp->b_blkno + p_offset;
   1247 
   1248 	/* Place it in the queue and start I/O on the unit. */
   1249 	mutex_enter(&sc->sc_iolock);
   1250 	sc->sc_iopend++;
   1251 	bufq_put(sc->sc_bufq, bp);
   1252 	mutex_exit(&sc->sc_iolock);
   1253 
   1254 	dkstart(sc);
   1255 	return;
   1256 
   1257  done:
   1258 	bp->b_resid = bp->b_bcount;
   1259 	biodone(bp);
   1260 }
   1261 
   1262 /*
   1263  * dkstart:
   1264  *
   1265  *	Start I/O that has been enqueued on the wedge.
   1266  */
   1267 static void
   1268 dkstart(struct dkwedge_softc *sc)
   1269 {
   1270 	struct vnode *vp;
   1271 	struct buf *bp, *nbp;
   1272 
   1273 	mutex_enter(&sc->sc_iolock);
   1274 
   1275 	/* Do as much work as has been enqueued. */
   1276 	while ((bp = bufq_peek(sc->sc_bufq)) != NULL) {
   1277 
   1278 		if (sc->sc_state != DKW_STATE_RUNNING) {
   1279 			(void) bufq_get(sc->sc_bufq);
   1280 			if (sc->sc_iopend-- == 1 &&
   1281 			    (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
   1282 				sc->sc_flags &= ~DK_F_WAIT_DRAIN;
   1283 				cv_broadcast(&sc->sc_dkdrn);
   1284 			}
   1285 			mutex_exit(&sc->sc_iolock);
   1286 			bp->b_error = ENXIO;
   1287 			bp->b_resid = bp->b_bcount;
   1288 			biodone(bp);
   1289 			mutex_enter(&sc->sc_iolock);
   1290 			continue;
   1291 		}
   1292 
   1293 		/* fetch an I/O buf with sc_iolock dropped */
   1294 		mutex_exit(&sc->sc_iolock);
   1295 		nbp = getiobuf(sc->sc_parent->dk_rawvp, false);
   1296 		mutex_enter(&sc->sc_iolock);
   1297 		if (nbp == NULL) {
   1298 			/*
   1299 			 * No resources to run this request; leave the
   1300 			 * buffer queued up, and schedule a timer to
   1301 			 * restart the queue in 1/2 a second.
   1302 			 */
   1303 			callout_schedule(&sc->sc_restart_ch, hz / 2);
   1304 			break;
   1305 		}
   1306 
   1307 		/*
   1308 		 * fetch buf, this can fail if another thread
   1309 		 * has already processed the queue, it can also
   1310 		 * return a completely different buf.
   1311 		 */
   1312 		bp = bufq_get(sc->sc_bufq);
   1313 		if (bp == NULL) {
   1314 			mutex_exit(&sc->sc_iolock);
   1315 			putiobuf(nbp);
   1316 			mutex_enter(&sc->sc_iolock);
   1317 			continue;
   1318 		}
   1319 
   1320 		/* Instrumentation. */
   1321 		disk_busy(&sc->sc_dk);
   1322 
   1323 		/* release lock for VOP_STRATEGY */
   1324 		mutex_exit(&sc->sc_iolock);
   1325 
   1326 		nbp->b_data = bp->b_data;
   1327 		nbp->b_flags = bp->b_flags;
   1328 		nbp->b_oflags = bp->b_oflags;
   1329 		nbp->b_cflags = bp->b_cflags;
   1330 		nbp->b_iodone = dkiodone;
   1331 		nbp->b_proc = bp->b_proc;
   1332 		nbp->b_blkno = bp->b_rawblkno;
   1333 		nbp->b_dev = sc->sc_parent->dk_rawvp->v_rdev;
   1334 		nbp->b_bcount = bp->b_bcount;
   1335 		nbp->b_private = bp;
   1336 		BIO_COPYPRIO(nbp, bp);
   1337 
   1338 		vp = nbp->b_vp;
   1339 		if ((nbp->b_flags & B_READ) == 0) {
   1340 			mutex_enter(vp->v_interlock);
   1341 			vp->v_numoutput++;
   1342 			mutex_exit(vp->v_interlock);
   1343 		}
   1344 		VOP_STRATEGY(vp, nbp);
   1345 
   1346 		mutex_enter(&sc->sc_iolock);
   1347 	}
   1348 
   1349 	mutex_exit(&sc->sc_iolock);
   1350 }
   1351 
   1352 /*
   1353  * dkiodone:
   1354  *
   1355  *	I/O to a wedge has completed; alert the top half.
   1356  */
   1357 static void
   1358 dkiodone(struct buf *bp)
   1359 {
   1360 	struct buf *obp = bp->b_private;
   1361 	struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev);
   1362 
   1363 	if (bp->b_error != 0)
   1364 		obp->b_error = bp->b_error;
   1365 	obp->b_resid = bp->b_resid;
   1366 	putiobuf(bp);
   1367 
   1368 	mutex_enter(&sc->sc_iolock);
   1369 	if (sc->sc_iopend-- == 1 && (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
   1370 		sc->sc_flags &= ~DK_F_WAIT_DRAIN;
   1371 		cv_broadcast(&sc->sc_dkdrn);
   1372 	}
   1373 
   1374 	disk_unbusy(&sc->sc_dk, obp->b_bcount - obp->b_resid,
   1375 	    obp->b_flags & B_READ);
   1376 	mutex_exit(&sc->sc_iolock);
   1377 
   1378 	biodone(obp);
   1379 
   1380 	/* Kick the queue in case there is more work we can do. */
   1381 	dkstart(sc);
   1382 }
   1383 
   1384 /*
   1385  * dkrestart:
   1386  *
   1387  *	Restart the work queue after it was stalled due to
   1388  *	a resource shortage.  Invoked via a callout.
   1389  */
   1390 static void
   1391 dkrestart(void *v)
   1392 {
   1393 	struct dkwedge_softc *sc = v;
   1394 
   1395 	dkstart(sc);
   1396 }
   1397 
   1398 /*
   1399  * dkminphys:
   1400  *
   1401  *	Call parent's minphys function.
   1402  */
   1403 static void
   1404 dkminphys(struct buf *bp)
   1405 {
   1406 	struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
   1407 	dev_t dev;
   1408 
   1409 	dev = bp->b_dev;
   1410 	bp->b_dev = sc->sc_pdev;
   1411 	(*sc->sc_parent->dk_driver->d_minphys)(bp);
   1412 	bp->b_dev = dev;
   1413 }
   1414 
   1415 /*
   1416  * dkread:		[devsw entry point]
   1417  *
   1418  *	Read from a wedge.
   1419  */
   1420 static int
   1421 dkread(dev_t dev, struct uio *uio, int flags)
   1422 {
   1423 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1424 
   1425 	if (sc == NULL)
   1426 		return (ENODEV);
   1427 	if (sc->sc_state != DKW_STATE_RUNNING)
   1428 		return (ENXIO);
   1429 
   1430 	return (physio(dkstrategy, NULL, dev, B_READ, dkminphys, uio));
   1431 }
   1432 
   1433 /*
   1434  * dkwrite:		[devsw entry point]
   1435  *
   1436  *	Write to a wedge.
   1437  */
   1438 static int
   1439 dkwrite(dev_t dev, struct uio *uio, int flags)
   1440 {
   1441 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1442 
   1443 	if (sc == NULL)
   1444 		return (ENODEV);
   1445 	if (sc->sc_state != DKW_STATE_RUNNING)
   1446 		return (ENXIO);
   1447 
   1448 	return (physio(dkstrategy, NULL, dev, B_WRITE, dkminphys, uio));
   1449 }
   1450 
   1451 /*
   1452  * dkioctl:		[devsw entry point]
   1453  *
   1454  *	Perform an ioctl request on a wedge.
   1455  */
   1456 static int
   1457 dkioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1458 {
   1459 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1460 	int error = 0;
   1461 
   1462 	if (sc == NULL)
   1463 		return (ENODEV);
   1464 	if (sc->sc_state != DKW_STATE_RUNNING)
   1465 		return (ENXIO);
   1466 	if (sc->sc_parent->dk_rawvp == NULL)
   1467 		return (ENXIO);
   1468 
   1469 	/*
   1470 	 * We pass NODEV instead of our device to indicate we don't
   1471 	 * want to handle disklabel ioctls
   1472 	 */
   1473 	error = disk_ioctl(&sc->sc_dk, NODEV, cmd, data, flag, l);
   1474 	if (error != EPASSTHROUGH)
   1475 		return (error);
   1476 
   1477 	error = 0;
   1478 
   1479 	switch (cmd) {
   1480 	case DIOCCACHESYNC:
   1481 		/*
   1482 		 * XXX Do we really need to care about having a writable
   1483 		 * file descriptor here?
   1484 		 */
   1485 		if ((flag & FWRITE) == 0)
   1486 			error = EBADF;
   1487 		else
   1488 			error = VOP_IOCTL(sc->sc_parent->dk_rawvp,
   1489 					  cmd, data, flag,
   1490 					  l != NULL ? l->l_cred : NOCRED);
   1491 		break;
   1492 	case DIOCGWEDGEINFO:
   1493 	    {
   1494 		struct dkwedge_info *dkw = (void *) data;
   1495 
   1496 		strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
   1497 			sizeof(dkw->dkw_devname));
   1498 	    	memcpy(dkw->dkw_wname, sc->sc_wname, sizeof(dkw->dkw_wname));
   1499 		dkw->dkw_wname[sizeof(dkw->dkw_wname) - 1] = '\0';
   1500 		strcpy(dkw->dkw_parent, sc->sc_parent->dk_name);
   1501 		dkw->dkw_offset = sc->sc_offset;
   1502 		dkw->dkw_size = sc->sc_size;
   1503 		strcpy(dkw->dkw_ptype, sc->sc_ptype);
   1504 
   1505 		break;
   1506 	    }
   1507 
   1508 	default:
   1509 		error = ENOTTY;
   1510 	}
   1511 
   1512 	return (error);
   1513 }
   1514 
   1515 /*
   1516  * dkdiscard:		[devsw entry point]
   1517  *
   1518  *	Perform a discard-range request on a wedge.
   1519  */
   1520 static int
   1521 dkdiscard(dev_t dev, off_t pos, off_t len)
   1522 {
   1523 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1524 	unsigned shift;
   1525 	off_t offset, maxlen;
   1526 
   1527 	if (sc == NULL)
   1528 		return (ENODEV);
   1529 	if (sc->sc_state != DKW_STATE_RUNNING)
   1530 		return (ENXIO);
   1531 	if (sc->sc_parent->dk_rawvp == NULL)
   1532 		return (ENXIO);
   1533 
   1534 	shift = (sc->sc_parent->dk_blkshift + DEV_BSHIFT);
   1535 	KASSERT(__type_fit(off_t, sc->sc_size));
   1536 	KASSERT(__type_fit(off_t, sc->sc_offset));
   1537 	KASSERT(0 <= sc->sc_offset);
   1538 	KASSERT(sc->sc_size <= (__type_max(off_t) >> shift));
   1539 	KASSERT(sc->sc_offset <= ((__type_max(off_t) >> shift) - sc->sc_size));
   1540 	offset = ((off_t)sc->sc_offset << shift);
   1541 	maxlen = ((off_t)sc->sc_size << shift);
   1542 
   1543 	if (len > maxlen)
   1544 		return (EINVAL);
   1545 	if (pos > (maxlen - len))
   1546 		return (EINVAL);
   1547 
   1548 	pos += offset;
   1549 	return VOP_FDISCARD(sc->sc_parent->dk_rawvp, pos, len);
   1550 }
   1551 
   1552 /*
   1553  * dksize:		[devsw entry point]
   1554  *
   1555  *	Query the size of a wedge for the purpose of performing a dump
   1556  *	or for swapping to.
   1557  */
   1558 static int
   1559 dksize(dev_t dev)
   1560 {
   1561 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1562 	int rv = -1;
   1563 
   1564 	if (sc == NULL)
   1565 		return (-1);
   1566 	if (sc->sc_state != DKW_STATE_RUNNING)
   1567 		return (-1);
   1568 
   1569 	mutex_enter(&sc->sc_dk.dk_openlock);
   1570 	mutex_enter(&sc->sc_parent->dk_rawlock);
   1571 
   1572 	/* Our content type is static, no need to open the device. */
   1573 
   1574 	if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) == 0) {
   1575 		/* Saturate if we are larger than INT_MAX. */
   1576 		if (sc->sc_size > INT_MAX)
   1577 			rv = INT_MAX;
   1578 		else
   1579 			rv = (int) sc->sc_size;
   1580 	}
   1581 
   1582 	mutex_exit(&sc->sc_parent->dk_rawlock);
   1583 	mutex_exit(&sc->sc_dk.dk_openlock);
   1584 
   1585 	return (rv);
   1586 }
   1587 
   1588 /*
   1589  * dkdump:		[devsw entry point]
   1590  *
   1591  *	Perform a crash dump to a wedge.
   1592  */
   1593 static int
   1594 dkdump(dev_t dev, daddr_t blkno, void *va, size_t size)
   1595 {
   1596 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1597 	const struct bdevsw *bdev;
   1598 	int rv = 0;
   1599 
   1600 	if (sc == NULL)
   1601 		return (ENODEV);
   1602 	if (sc->sc_state != DKW_STATE_RUNNING)
   1603 		return (ENXIO);
   1604 
   1605 	mutex_enter(&sc->sc_dk.dk_openlock);
   1606 	mutex_enter(&sc->sc_parent->dk_rawlock);
   1607 
   1608 	/* Our content type is static, no need to open the device. */
   1609 
   1610 	if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) != 0 &&
   1611 	    strcmp(sc->sc_ptype, DKW_PTYPE_RAID) != 0) {
   1612 		rv = ENXIO;
   1613 		goto out;
   1614 	}
   1615 	if (size % DEV_BSIZE != 0) {
   1616 		rv = EINVAL;
   1617 		goto out;
   1618 	}
   1619 	if (blkno + size / DEV_BSIZE > sc->sc_size) {
   1620 		printf("%s: blkno (%" PRIu64 ") + size / DEV_BSIZE (%zu) > "
   1621 		    "sc->sc_size (%" PRIu64 ")\n", __func__, blkno,
   1622 		    size / DEV_BSIZE, sc->sc_size);
   1623 		rv = EINVAL;
   1624 		goto out;
   1625 	}
   1626 
   1627 	bdev = bdevsw_lookup_acquire(sc->sc_pdev);
   1628 	rv = (*bdev->d_dump)(sc->sc_pdev, blkno + sc->sc_offset, va, size);
   1629 	bdevsw_release(bdev);
   1630 
   1631 out:
   1632 	mutex_exit(&sc->sc_parent->dk_rawlock);
   1633 	mutex_exit(&sc->sc_dk.dk_openlock);
   1634 
   1635 	return rv;
   1636 }
   1637 
   1638 /*
   1639  * config glue
   1640  */
   1641 
   1642 /*
   1643  * dkwedge_find_partition
   1644  *
   1645  *	Find wedge corresponding to the specified parent name
   1646  *	and offset/length.
   1647  */
   1648 device_t
   1649 dkwedge_find_partition(device_t parent, daddr_t startblk, uint64_t nblks)
   1650 {
   1651 	struct dkwedge_softc *sc;
   1652 	int i;
   1653 	device_t wedge = NULL;
   1654 
   1655 	rw_enter(&dkwedges_lock, RW_READER);
   1656 	for (i = 0; i < ndkwedges; i++) {
   1657 		if ((sc = dkwedges[i]) == NULL)
   1658 			continue;
   1659 		if (strcmp(sc->sc_parent->dk_name, device_xname(parent)) == 0 &&
   1660 		    sc->sc_offset == startblk &&
   1661 		    sc->sc_size == nblks) {
   1662 			if (wedge) {
   1663 				printf("WARNING: double match for boot wedge "
   1664 				    "(%s, %s)\n",
   1665 				    device_xname(wedge),
   1666 				    device_xname(sc->sc_dev));
   1667 				continue;
   1668 			}
   1669 			wedge = sc->sc_dev;
   1670 		}
   1671 	}
   1672 	rw_exit(&dkwedges_lock);
   1673 
   1674 	return wedge;
   1675 }
   1676 
   1677 const char *
   1678 dkwedge_get_parent_name(dev_t dev)
   1679 {
   1680 	/* XXX: perhaps do this in lookup? */
   1681 	int bmaj = bdevsw_lookup_major(&dk_bdevsw);
   1682 	int cmaj = cdevsw_lookup_major(&dk_cdevsw);
   1683 	if (major(dev) != bmaj && major(dev) != cmaj)
   1684 		return NULL;
   1685 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1686 	if (sc == NULL)
   1687 		return NULL;
   1688 	return sc->sc_parent->dk_name;
   1689 }
   1690 
   1691