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