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