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