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