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