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