Home | History | Annotate | Line # | Download | only in dkwedge
dk.c revision 1.46
      1 /*	$NetBSD: dk.c,v 1.46 2009/07/02 00:56:48 dyoung 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.46 2009/07/02 00:56:48 dyoung 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 int	dklastclose(struct dkwedge_softc *);
     98 
     99 static dev_type_open(dkopen);
    100 static dev_type_close(dkclose);
    101 static dev_type_read(dkread);
    102 static dev_type_write(dkwrite);
    103 static dev_type_ioctl(dkioctl);
    104 static dev_type_strategy(dkstrategy);
    105 static dev_type_dump(dkdump);
    106 static dev_type_size(dksize);
    107 
    108 const struct bdevsw dk_bdevsw = {
    109 	dkopen, dkclose, dkstrategy, dkioctl, dkdump, dksize, D_DISK
    110 };
    111 
    112 const struct cdevsw dk_cdevsw = {
    113 	dkopen, dkclose, dkread, dkwrite, dkioctl,
    114 	    nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    115 };
    116 
    117 static struct dkwedge_softc **dkwedges;
    118 static u_int ndkwedges;
    119 static krwlock_t dkwedges_lock;
    120 
    121 static LIST_HEAD(, dkwedge_discovery_method) dkwedge_discovery_methods;
    122 static krwlock_t dkwedge_discovery_methods_lock;
    123 
    124 /*
    125  * dkwedge_match:
    126  *
    127  *	Autoconfiguration match function for pseudo-device glue.
    128  */
    129 static int
    130 dkwedge_match(device_t parent, cfdata_t match,
    131     void *aux)
    132 {
    133 
    134 	/* Pseudo-device; always present. */
    135 	return (1);
    136 }
    137 
    138 /*
    139  * dkwedge_attach:
    140  *
    141  *	Autoconfiguration attach function for pseudo-device glue.
    142  */
    143 static void
    144 dkwedge_attach(device_t parent, device_t self,
    145     void *aux)
    146 {
    147 
    148 	if (!pmf_device_register(self, NULL, NULL))
    149 		aprint_error_dev(self, "couldn't establish power handler\n");
    150 }
    151 
    152 /*
    153  * dkwedge_detach:
    154  *
    155  *	Autoconfiguration detach function for pseudo-device glue.
    156  */
    157 static int
    158 dkwedge_detach(device_t self, int flags)
    159 {
    160 
    161 	pmf_device_deregister(self);
    162 	/* Always succeeds. */
    163 	return (0);
    164 }
    165 
    166 CFDRIVER_DECL(dk, DV_DISK, NULL);
    167 CFATTACH_DECL_NEW(dk, 0,
    168 	      dkwedge_match, dkwedge_attach, dkwedge_detach, NULL);
    169 
    170 /*
    171  * dkwedge_wait_drain:
    172  *
    173  *	Wait for I/O on the wedge to drain.
    174  *	NOTE: Must be called at splbio()!
    175  */
    176 static void
    177 dkwedge_wait_drain(struct dkwedge_softc *sc)
    178 {
    179 
    180 	while (sc->sc_iopend != 0) {
    181 		sc->sc_flags |= DK_F_WAIT_DRAIN;
    182 		(void) tsleep(&sc->sc_iopend, PRIBIO, "dkdrn", 0);
    183 	}
    184 }
    185 
    186 /*
    187  * dkwedge_compute_pdev:
    188  *
    189  *	Compute the parent disk's dev_t.
    190  */
    191 static int
    192 dkwedge_compute_pdev(const char *pname, dev_t *pdevp)
    193 {
    194 	const char *name, *cp;
    195 	int punit, pmaj;
    196 	char devname[16];
    197 
    198 	name = pname;
    199 	if ((pmaj = devsw_name2blk(name, devname, sizeof(devname))) == -1)
    200 		return (ENODEV);
    201 
    202 	name += strlen(devname);
    203 	for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++)
    204 		punit = (punit * 10) + (*cp - '0');
    205 	if (cp == name) {
    206 		/* Invalid parent disk name. */
    207 		return (ENODEV);
    208 	}
    209 
    210 	*pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART);
    211 
    212 	return (0);
    213 }
    214 
    215 /*
    216  * dkwedge_array_expand:
    217  *
    218  *	Expand the dkwedges array.
    219  */
    220 static void
    221 dkwedge_array_expand(void)
    222 {
    223 	int newcnt = ndkwedges + 16;
    224 	struct dkwedge_softc **newarray, **oldarray;
    225 
    226 	newarray = malloc(newcnt * sizeof(*newarray), M_DKWEDGE,
    227 	    M_WAITOK|M_ZERO);
    228 	if ((oldarray = dkwedges) != NULL)
    229 		memcpy(newarray, dkwedges, ndkwedges * sizeof(*newarray));
    230 	dkwedges = newarray;
    231 	ndkwedges = newcnt;
    232 	if (oldarray != NULL)
    233 		free(oldarray, M_DKWEDGE);
    234 }
    235 
    236 /*
    237  * dkwedge_add:		[exported function]
    238  *
    239  *	Add a disk wedge based on the provided information.
    240  *
    241  *	The incoming dkw_devname[] is ignored, instead being
    242  *	filled in and returned to the caller.
    243  */
    244 int
    245 dkwedge_add(struct dkwedge_info *dkw)
    246 {
    247 	struct dkwedge_softc *sc, *lsc;
    248 	struct disk *pdk;
    249 	u_int unit;
    250 	int error;
    251 	dev_t pdev;
    252 
    253 	dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0';
    254 	pdk = disk_find(dkw->dkw_parent);
    255 	if (pdk == NULL)
    256 		return (ENODEV);
    257 
    258 	error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
    259 	if (error)
    260 		return (error);
    261 
    262 	if (dkw->dkw_offset < 0)
    263 		return (EINVAL);
    264 
    265 	sc = malloc(sizeof(*sc), M_DKWEDGE, M_WAITOK|M_ZERO);
    266 	sc->sc_state = DKW_STATE_LARVAL;
    267 	sc->sc_parent = pdk;
    268 	sc->sc_pdev = pdev;
    269 	sc->sc_offset = dkw->dkw_offset;
    270 	sc->sc_size = dkw->dkw_size;
    271 
    272 	memcpy(sc->sc_wname, dkw->dkw_wname, sizeof(sc->sc_wname));
    273 	sc->sc_wname[sizeof(sc->sc_wname) - 1] = '\0';
    274 
    275 	memcpy(sc->sc_ptype, dkw->dkw_ptype, sizeof(sc->sc_ptype));
    276 	sc->sc_ptype[sizeof(sc->sc_ptype) - 1] = '\0';
    277 
    278 	bufq_alloc(&sc->sc_bufq, "fcfs", 0);
    279 
    280 	callout_init(&sc->sc_restart_ch, 0);
    281 	callout_setfunc(&sc->sc_restart_ch, dkrestart, sc);
    282 
    283 	/*
    284 	 * Wedge will be added; increment the wedge count for the parent.
    285 	 * Only allow this to happend if RAW_PART is the only thing open.
    286 	 */
    287 	mutex_enter(&pdk->dk_openlock);
    288 	if (pdk->dk_openmask & ~(1 << RAW_PART))
    289 		error = EBUSY;
    290 	else {
    291 		/* Check for wedge overlap. */
    292 		LIST_FOREACH(lsc, &pdk->dk_wedges, sc_plink) {
    293 			daddr_t lastblk = sc->sc_offset + sc->sc_size - 1;
    294 			daddr_t llastblk = lsc->sc_offset + lsc->sc_size - 1;
    295 
    296 			if (sc->sc_offset >= lsc->sc_offset &&
    297 			    sc->sc_offset <= llastblk) {
    298 				/* Overlaps the tail of the exsiting wedge. */
    299 				break;
    300 			}
    301 			if (lastblk >= lsc->sc_offset &&
    302 			    lastblk <= llastblk) {
    303 				/* Overlaps the head of the existing wedge. */
    304 			    	break;
    305 			}
    306 		}
    307 		if (lsc != NULL)
    308 			error = EINVAL;
    309 		else {
    310 			pdk->dk_nwedges++;
    311 			LIST_INSERT_HEAD(&pdk->dk_wedges, sc, sc_plink);
    312 		}
    313 	}
    314 	mutex_exit(&pdk->dk_openlock);
    315 	if (error) {
    316 		bufq_free(sc->sc_bufq);
    317 		free(sc, M_DKWEDGE);
    318 		return (error);
    319 	}
    320 
    321 	/* Fill in our cfdata for the pseudo-device glue. */
    322 	sc->sc_cfdata.cf_name = dk_cd.cd_name;
    323 	sc->sc_cfdata.cf_atname = dk_ca.ca_name;
    324 	/* sc->sc_cfdata.cf_unit set below */
    325 	sc->sc_cfdata.cf_fstate = FSTATE_STAR;
    326 
    327 	/* Insert the larval wedge into the array. */
    328 	rw_enter(&dkwedges_lock, RW_WRITER);
    329 	for (error = 0;;) {
    330 		struct dkwedge_softc **scpp;
    331 
    332 		/*
    333 		 * Check for a duplicate wname while searching for
    334 		 * a slot.
    335 		 */
    336 		for (scpp = NULL, unit = 0; unit < ndkwedges; unit++) {
    337 			if (dkwedges[unit] == NULL) {
    338 				if (scpp == NULL) {
    339 					scpp = &dkwedges[unit];
    340 					sc->sc_cfdata.cf_unit = unit;
    341 				}
    342 			} else {
    343 				/* XXX Unicode. */
    344 				if (strcmp(dkwedges[unit]->sc_wname,
    345 					   sc->sc_wname) == 0) {
    346 					error = EEXIST;
    347 					break;
    348 				}
    349 			}
    350 		}
    351 		if (error)
    352 			break;
    353 		KASSERT(unit == ndkwedges);
    354 		if (scpp == NULL)
    355 			dkwedge_array_expand();
    356 		else {
    357 			KASSERT(scpp == &dkwedges[sc->sc_cfdata.cf_unit]);
    358 			*scpp = sc;
    359 			break;
    360 		}
    361 	}
    362 	rw_exit(&dkwedges_lock);
    363 	if (error) {
    364 		mutex_enter(&pdk->dk_openlock);
    365 		pdk->dk_nwedges--;
    366 		LIST_REMOVE(sc, sc_plink);
    367 		mutex_exit(&pdk->dk_openlock);
    368 
    369 		bufq_free(sc->sc_bufq);
    370 		free(sc, M_DKWEDGE);
    371 		return (error);
    372 	}
    373 
    374 	/*
    375 	 * Now that we know the unit #, attach a pseudo-device for
    376 	 * this wedge instance.  This will provide us with the
    377 	 * "struct device" necessary for glue to other parts of the
    378 	 * system.
    379 	 *
    380 	 * This should never fail, unless we're almost totally out of
    381 	 * memory.
    382 	 */
    383 	if ((sc->sc_dev = config_attach_pseudo(&sc->sc_cfdata)) == NULL) {
    384 		aprint_error("%s%u: unable to attach pseudo-device\n",
    385 		    sc->sc_cfdata.cf_name, sc->sc_cfdata.cf_unit);
    386 
    387 		rw_enter(&dkwedges_lock, RW_WRITER);
    388 		dkwedges[sc->sc_cfdata.cf_unit] = NULL;
    389 		rw_exit(&dkwedges_lock);
    390 
    391 		mutex_enter(&pdk->dk_openlock);
    392 		pdk->dk_nwedges--;
    393 		LIST_REMOVE(sc, sc_plink);
    394 		mutex_exit(&pdk->dk_openlock);
    395 
    396 		bufq_free(sc->sc_bufq);
    397 		free(sc, M_DKWEDGE);
    398 		return (ENOMEM);
    399 	}
    400 
    401 	/* Return the devname to the caller. */
    402 	strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
    403 		sizeof(dkw->dkw_devname));
    404 
    405 	/*
    406 	 * XXX Really ought to make the disk_attach() and the changing
    407 	 * of state to RUNNING atomic.
    408 	 */
    409 
    410 	disk_init(&sc->sc_dk, device_xname(sc->sc_dev), NULL);
    411 	disk_attach(&sc->sc_dk);
    412 
    413 	/* Disk wedge is ready for use! */
    414 	sc->sc_state = DKW_STATE_RUNNING;
    415 
    416 	/* Announce our arrival. */
    417 	aprint_normal("%s at %s: %s\n", device_xname(sc->sc_dev), pdk->dk_name,
    418 	    sc->sc_wname);	/* XXX Unicode */
    419 	aprint_normal("%s: %"PRIu64" blocks at %"PRId64", type: %s\n",
    420 	    device_xname(sc->sc_dev), sc->sc_size, sc->sc_offset, sc->sc_ptype);
    421 
    422 	return (0);
    423 }
    424 
    425 /*
    426  * dkwedge_del:		[exported function]
    427  *
    428  *	Delete a disk wedge based on the provided information.
    429  *	NOTE: We look up the wedge based on the wedge devname,
    430  *	not wname.
    431  */
    432 int
    433 dkwedge_del(struct dkwedge_info *dkw)
    434 {
    435 	struct dkwedge_softc *sc = NULL;
    436 	u_int unit;
    437 	int bmaj, cmaj, s;
    438 
    439 	/* Find our softc. */
    440 	dkw->dkw_devname[sizeof(dkw->dkw_devname) - 1] = '\0';
    441 	rw_enter(&dkwedges_lock, RW_WRITER);
    442 	for (unit = 0; unit < ndkwedges; unit++) {
    443 		if ((sc = dkwedges[unit]) != NULL &&
    444 		    strcmp(device_xname(sc->sc_dev), dkw->dkw_devname) == 0 &&
    445 		    strcmp(sc->sc_parent->dk_name, dkw->dkw_parent) == 0) {
    446 			/* Mark the wedge as dying. */
    447 			sc->sc_state = DKW_STATE_DYING;
    448 			break;
    449 		}
    450 	}
    451 	rw_exit(&dkwedges_lock);
    452 	if (unit == ndkwedges)
    453 		return (ESRCH);
    454 
    455 	KASSERT(sc != NULL);
    456 
    457 	/* Locate the wedge major numbers. */
    458 	bmaj = bdevsw_lookup_major(&dk_bdevsw);
    459 	cmaj = cdevsw_lookup_major(&dk_cdevsw);
    460 
    461 	/* Kill any pending restart. */
    462 	callout_stop(&sc->sc_restart_ch);
    463 
    464 	/*
    465 	 * dkstart() will kill any queued buffers now that the
    466 	 * state of the wedge is not RUNNING.  Once we've done
    467 	 * that, wait for any other pending I/O to complete.
    468 	 */
    469 	s = splbio();
    470 	dkstart(sc);
    471 	dkwedge_wait_drain(sc);
    472 	splx(s);
    473 
    474 	/* Nuke the vnodes for any open instances. */
    475 	vdevgone(bmaj, unit, unit, VBLK);
    476 	vdevgone(cmaj, unit, unit, VCHR);
    477 
    478 	/* Clean up the parent. */
    479 	mutex_enter(&sc->sc_dk.dk_openlock);
    480 	mutex_enter(&sc->sc_parent->dk_rawlock);
    481 	if (sc->sc_dk.dk_openmask) {
    482 		if (sc->sc_parent->dk_rawopens-- == 1) {
    483 			KASSERT(sc->sc_parent->dk_rawvp != NULL);
    484 			(void) vn_close(sc->sc_parent->dk_rawvp, FREAD | FWRITE,
    485 			    NOCRED);
    486 			sc->sc_parent->dk_rawvp = NULL;
    487 		}
    488 		sc->sc_dk.dk_openmask = 0;
    489 	}
    490 	mutex_exit(&sc->sc_parent->dk_rawlock);
    491 	mutex_exit(&sc->sc_dk.dk_openlock);
    492 
    493 	/* Announce our departure. */
    494 	aprint_normal("%s at %s (%s) deleted\n", device_xname(sc->sc_dev),
    495 	    sc->sc_parent->dk_name,
    496 	    sc->sc_wname);	/* XXX Unicode */
    497 
    498 	/* Delete our pseudo-device. */
    499 	(void) config_detach(sc->sc_dev, DETACH_FORCE | DETACH_QUIET);
    500 
    501 	mutex_enter(&sc->sc_parent->dk_openlock);
    502 	sc->sc_parent->dk_nwedges--;
    503 	LIST_REMOVE(sc, sc_plink);
    504 	mutex_exit(&sc->sc_parent->dk_openlock);
    505 
    506 	/* Delete our buffer queue. */
    507 	bufq_free(sc->sc_bufq);
    508 
    509 	/* Detach from the disk list. */
    510 	disk_detach(&sc->sc_dk);
    511 	disk_destroy(&sc->sc_dk);
    512 
    513 	/* Poof. */
    514 	rw_enter(&dkwedges_lock, RW_WRITER);
    515 	dkwedges[unit] = NULL;
    516 	sc->sc_state = DKW_STATE_DEAD;
    517 	rw_exit(&dkwedges_lock);
    518 
    519 	free(sc, M_DKWEDGE);
    520 
    521 	return (0);
    522 }
    523 
    524 /*
    525  * dkwedge_delall:	[exported function]
    526  *
    527  *	Delete all of the wedges on the specified disk.  Used when
    528  *	a disk is being detached.
    529  */
    530 void
    531 dkwedge_delall(struct disk *pdk)
    532 {
    533 	struct dkwedge_info dkw;
    534 	struct dkwedge_softc *sc;
    535 
    536 	for (;;) {
    537 		mutex_enter(&pdk->dk_openlock);
    538 		if ((sc = LIST_FIRST(&pdk->dk_wedges)) == NULL) {
    539 			KASSERT(pdk->dk_nwedges == 0);
    540 			mutex_exit(&pdk->dk_openlock);
    541 			return;
    542 		}
    543 		strcpy(dkw.dkw_parent, pdk->dk_name);
    544 		strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
    545 			sizeof(dkw.dkw_devname));
    546 		mutex_exit(&pdk->dk_openlock);
    547 		(void) dkwedge_del(&dkw);
    548 	}
    549 }
    550 
    551 /*
    552  * dkwedge_list:	[exported function]
    553  *
    554  *	List all of the wedges on a particular disk.
    555  *	If p == NULL, the buffer is in kernel space.  Otherwise, it is
    556  *	in user space of the specified process.
    557  */
    558 int
    559 dkwedge_list(struct disk *pdk, struct dkwedge_list *dkwl, struct lwp *l)
    560 {
    561 	struct uio uio;
    562 	struct iovec iov;
    563 	struct dkwedge_softc *sc;
    564 	struct dkwedge_info dkw;
    565 	struct vmspace *vm;
    566 	int error = 0;
    567 
    568 	iov.iov_base = dkwl->dkwl_buf;
    569 	iov.iov_len = dkwl->dkwl_bufsize;
    570 
    571 	uio.uio_iov = &iov;
    572 	uio.uio_iovcnt = 1;
    573 	uio.uio_offset = 0;
    574 	uio.uio_resid = dkwl->dkwl_bufsize;
    575 	uio.uio_rw = UIO_READ;
    576 	if (l == NULL) {
    577 		UIO_SETUP_SYSSPACE(&uio);
    578 	} else {
    579 		error = proc_vmspace_getref(l->l_proc, &vm);
    580 		if (error) {
    581 			return error;
    582 		}
    583 		uio.uio_vmspace = vm;
    584 	}
    585 
    586 	dkwl->dkwl_ncopied = 0;
    587 
    588 	mutex_enter(&pdk->dk_openlock);
    589 	LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) {
    590 		if (uio.uio_resid < sizeof(dkw))
    591 			break;
    592 
    593 		if (sc->sc_state != DKW_STATE_RUNNING)
    594 			continue;
    595 
    596 		strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
    597 			sizeof(dkw.dkw_devname));
    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 	mutex_exit(&pdk->dk_openlock);
    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(device_t 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, device_xname(parent)) == 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 				    device_xname(booted_wedge),
    684 				    device_xname(sc->sc_dev));
    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);
    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);
    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 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 *bp;
    857 	int result;
    858 
    859 	bp = getiobuf(vp, true);
    860 
    861 	bp->b_dev = vp->v_rdev;
    862 	bp->b_blkno = blkno;
    863 	bp->b_bcount = len;
    864 	bp->b_resid = len;
    865 	bp->b_flags = B_READ;
    866 	bp->b_data = tbuf;
    867 	SET(bp->b_cflags, BC_BUSY);	/* mark buffer busy */
    868 
    869 	VOP_STRATEGY(vp, bp);
    870 	result = biowait(bp);
    871 	putiobuf(bp);
    872 
    873 	return result;
    874 }
    875 
    876 /*
    877  * dkwedge_lookup:
    878  *
    879  *	Look up a dkwedge_softc based on the provided dev_t.
    880  */
    881 static struct dkwedge_softc *
    882 dkwedge_lookup(dev_t dev)
    883 {
    884 	int unit = minor(dev);
    885 
    886 	if (unit >= ndkwedges)
    887 		return (NULL);
    888 
    889 	KASSERT(dkwedges != NULL);
    890 
    891 	return (dkwedges[unit]);
    892 }
    893 
    894 /*
    895  * dkopen:		[devsw entry point]
    896  *
    897  *	Open a wedge.
    898  */
    899 static int
    900 dkopen(dev_t dev, int flags, int fmt, struct lwp *l)
    901 {
    902 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
    903 	struct vnode *vp;
    904 	int error = 0;
    905 
    906 	if (sc == NULL)
    907 		return (ENODEV);
    908 
    909 	if (sc->sc_state != DKW_STATE_RUNNING)
    910 		return (ENXIO);
    911 
    912 	/*
    913 	 * We go through a complicated little dance to only open the parent
    914 	 * vnode once per wedge, no matter how many times the wedge is
    915 	 * opened.  The reason?  We see one dkopen() per open call, but
    916 	 * only dkclose() on the last close.
    917 	 */
    918 	mutex_enter(&sc->sc_dk.dk_openlock);
    919 	mutex_enter(&sc->sc_parent->dk_rawlock);
    920 	if (sc->sc_dk.dk_openmask == 0) {
    921 		if (sc->sc_parent->dk_rawopens == 0) {
    922 			KASSERT(sc->sc_parent->dk_rawvp == NULL);
    923 			error = bdevvp(sc->sc_pdev, &vp);
    924 			if (error)
    925 				goto popen_fail;
    926 			error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    927 			if (error) {
    928 				vrele(vp);
    929 				goto popen_fail;
    930 			}
    931 			error = VOP_OPEN(vp, FREAD | FWRITE, NOCRED);
    932 			if (error) {
    933 				vput(vp);
    934 				goto popen_fail;
    935 			}
    936 			/* VOP_OPEN() doesn't do this for us. */
    937 			mutex_enter(&vp->v_interlock);
    938 			vp->v_writecount++;
    939 			mutex_exit(&vp->v_interlock);
    940 			VOP_UNLOCK(vp, 0);
    941 			sc->sc_parent->dk_rawvp = vp;
    942 		}
    943 		sc->sc_parent->dk_rawopens++;
    944 	}
    945 	if (fmt == S_IFCHR)
    946 		sc->sc_dk.dk_copenmask |= 1;
    947 	else
    948 		sc->sc_dk.dk_bopenmask |= 1;
    949 	sc->sc_dk.dk_openmask =
    950 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    951 
    952  popen_fail:
    953 	mutex_exit(&sc->sc_parent->dk_rawlock);
    954 	mutex_exit(&sc->sc_dk.dk_openlock);
    955 	return (error);
    956 }
    957 
    958 /*
    959  * Caller must hold sc->sc_dk.dk_openlock and sc->sc_parent->dk_rawlock.
    960  */
    961 static int
    962 dklastclose(struct dkwedge_softc *sc)
    963 {
    964 	int error = 0;
    965 
    966 	if (sc->sc_parent->dk_rawopens-- == 1) {
    967 		KASSERT(sc->sc_parent->dk_rawvp != NULL);
    968 		error = vn_close(sc->sc_parent->dk_rawvp,
    969 		    FREAD | FWRITE, NOCRED);
    970 		sc->sc_parent->dk_rawvp = NULL;
    971 	}
    972 	return error;
    973 }
    974 
    975 /*
    976  * dkclose:		[devsw entry point]
    977  *
    978  *	Close a wedge.
    979  */
    980 static int
    981 dkclose(dev_t dev, int flags, int fmt, struct lwp *l)
    982 {
    983 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
    984 	int error = 0;
    985 
    986 	KASSERT(sc->sc_dk.dk_openmask != 0);
    987 
    988 	mutex_enter(&sc->sc_dk.dk_openlock);
    989 	mutex_enter(&sc->sc_parent->dk_rawlock);
    990 
    991 	if (fmt == S_IFCHR)
    992 		sc->sc_dk.dk_copenmask &= ~1;
    993 	else
    994 		sc->sc_dk.dk_bopenmask &= ~1;
    995 	sc->sc_dk.dk_openmask =
    996 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    997 
    998 	if (sc->sc_dk.dk_openmask == 0)
    999 		error = dklastclose(sc);
   1000 
   1001 	mutex_exit(&sc->sc_parent->dk_rawlock);
   1002 	mutex_exit(&sc->sc_dk.dk_openlock);
   1003 
   1004 	return (error);
   1005 }
   1006 
   1007 /*
   1008  * dkstragegy:		[devsw entry point]
   1009  *
   1010  *	Perform I/O based on the wedge I/O strategy.
   1011  */
   1012 static void
   1013 dkstrategy(struct buf *bp)
   1014 {
   1015 	struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
   1016 	int s;
   1017 
   1018 	if (sc->sc_state != DKW_STATE_RUNNING) {
   1019 		bp->b_error = ENXIO;
   1020 		goto done;
   1021 	}
   1022 
   1023 	/* If it's an empty transfer, wake up the top half now. */
   1024 	if (bp->b_bcount == 0)
   1025 		goto done;
   1026 
   1027 	/* Make sure it's in-range. */
   1028 	if (bounds_check_with_mediasize(bp, DEV_BSIZE, sc->sc_size) <= 0)
   1029 		goto done;
   1030 
   1031 	/* Translate it to the parent's raw LBA. */
   1032 	bp->b_rawblkno = bp->b_blkno + sc->sc_offset;
   1033 
   1034 	/* Place it in the queue and start I/O on the unit. */
   1035 	s = splbio();
   1036 	sc->sc_iopend++;
   1037 	bufq_put(sc->sc_bufq, bp);
   1038 	dkstart(sc);
   1039 	splx(s);
   1040 	return;
   1041 
   1042  done:
   1043 	bp->b_resid = bp->b_bcount;
   1044 	biodone(bp);
   1045 }
   1046 
   1047 /*
   1048  * dkstart:
   1049  *
   1050  *	Start I/O that has been enqueued on the wedge.
   1051  *	NOTE: Must be called at splbio()!
   1052  */
   1053 static void
   1054 dkstart(struct dkwedge_softc *sc)
   1055 {
   1056 	struct vnode *vp;
   1057 	struct buf *bp, *nbp;
   1058 
   1059 	/* Do as much work as has been enqueued. */
   1060 	while ((bp = bufq_peek(sc->sc_bufq)) != NULL) {
   1061 		if (sc->sc_state != DKW_STATE_RUNNING) {
   1062 			(void) bufq_get(sc->sc_bufq);
   1063 			if (sc->sc_iopend-- == 1 &&
   1064 			    (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
   1065 				sc->sc_flags &= ~DK_F_WAIT_DRAIN;
   1066 				wakeup(&sc->sc_iopend);
   1067 			}
   1068 			bp->b_error = ENXIO;
   1069 			bp->b_resid = bp->b_bcount;
   1070 			biodone(bp);
   1071 		}
   1072 
   1073 		/* Instrumentation. */
   1074 		disk_busy(&sc->sc_dk);
   1075 
   1076 		nbp = getiobuf(sc->sc_parent->dk_rawvp, false);
   1077 		if (nbp == NULL) {
   1078 			/*
   1079 			 * No resources to run this request; leave the
   1080 			 * buffer queued up, and schedule a timer to
   1081 			 * restart the queue in 1/2 a second.
   1082 			 */
   1083 			disk_unbusy(&sc->sc_dk, 0, bp->b_flags & B_READ);
   1084 			callout_schedule(&sc->sc_restart_ch, hz / 2);
   1085 			return;
   1086 		}
   1087 
   1088 		(void) bufq_get(sc->sc_bufq);
   1089 
   1090 		nbp->b_data = bp->b_data;
   1091 		nbp->b_flags = bp->b_flags;
   1092 		nbp->b_oflags = bp->b_oflags;
   1093 		nbp->b_cflags = bp->b_cflags;
   1094 		nbp->b_iodone = dkiodone;
   1095 		nbp->b_proc = bp->b_proc;
   1096 		nbp->b_blkno = bp->b_rawblkno;
   1097 		nbp->b_dev = sc->sc_parent->dk_rawvp->v_rdev;
   1098 		nbp->b_bcount = bp->b_bcount;
   1099 		nbp->b_private = bp;
   1100 		BIO_COPYPRIO(nbp, bp);
   1101 
   1102 		vp = nbp->b_vp;
   1103 		if ((nbp->b_flags & B_READ) == 0) {
   1104 			mutex_enter(&vp->v_interlock);
   1105 			vp->v_numoutput++;
   1106 			mutex_exit(&vp->v_interlock);
   1107 		}
   1108 		VOP_STRATEGY(vp, nbp);
   1109 	}
   1110 }
   1111 
   1112 /*
   1113  * dkiodone:
   1114  *
   1115  *	I/O to a wedge has completed; alert the top half.
   1116  *	NOTE: Must be called at splbio()!
   1117  */
   1118 static void
   1119 dkiodone(struct buf *bp)
   1120 {
   1121 	struct buf *obp = bp->b_private;
   1122 	struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev);
   1123 
   1124 	if (bp->b_error != 0)
   1125 		obp->b_error = bp->b_error;
   1126 	obp->b_resid = bp->b_resid;
   1127 	putiobuf(bp);
   1128 
   1129 	if (sc->sc_iopend-- == 1 && (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
   1130 		sc->sc_flags &= ~DK_F_WAIT_DRAIN;
   1131 		wakeup(&sc->sc_iopend);
   1132 	}
   1133 
   1134 	disk_unbusy(&sc->sc_dk, obp->b_bcount - obp->b_resid,
   1135 	    obp->b_flags & B_READ);
   1136 
   1137 	biodone(obp);
   1138 
   1139 	/* Kick the queue in case there is more work we can do. */
   1140 	dkstart(sc);
   1141 }
   1142 
   1143 /*
   1144  * dkrestart:
   1145  *
   1146  *	Restart the work queue after it was stalled due to
   1147  *	a resource shortage.  Invoked via a callout.
   1148  */
   1149 static void
   1150 dkrestart(void *v)
   1151 {
   1152 	struct dkwedge_softc *sc = v;
   1153 	int s;
   1154 
   1155 	s = splbio();
   1156 	dkstart(sc);
   1157 	splx(s);
   1158 }
   1159 
   1160 /*
   1161  * dkread:		[devsw entry point]
   1162  *
   1163  *	Read from a wedge.
   1164  */
   1165 static int
   1166 dkread(dev_t dev, struct uio *uio, int flags)
   1167 {
   1168 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1169 
   1170 	if (sc->sc_state != DKW_STATE_RUNNING)
   1171 		return (ENXIO);
   1172 
   1173 	return (physio(dkstrategy, NULL, dev, B_READ,
   1174 		       sc->sc_parent->dk_driver->d_minphys, uio));
   1175 }
   1176 
   1177 /*
   1178  * dkwrite:		[devsw entry point]
   1179  *
   1180  *	Write to a wedge.
   1181  */
   1182 static int
   1183 dkwrite(dev_t dev, struct uio *uio, int flags)
   1184 {
   1185 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1186 
   1187 	if (sc->sc_state != DKW_STATE_RUNNING)
   1188 		return (ENXIO);
   1189 
   1190 	return (physio(dkstrategy, NULL, dev, B_WRITE,
   1191 		       sc->sc_parent->dk_driver->d_minphys, uio));
   1192 }
   1193 
   1194 /*
   1195  * dkioctl:		[devsw entry point]
   1196  *
   1197  *	Perform an ioctl request on a wedge.
   1198  */
   1199 static int
   1200 dkioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1201 {
   1202 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1203 	int error = 0;
   1204 
   1205 	if (sc->sc_state != DKW_STATE_RUNNING)
   1206 		return (ENXIO);
   1207 
   1208 	switch (cmd) {
   1209 	case DIOCCACHESYNC:
   1210 		/*
   1211 		 * XXX Do we really need to care about having a writable
   1212 		 * file descriptor here?
   1213 		 */
   1214 		if ((flag & FWRITE) == 0)
   1215 			error = EBADF;
   1216 		else
   1217 			error = VOP_IOCTL(sc->sc_parent->dk_rawvp,
   1218 					  cmd, data, flag,
   1219 					  l != NULL ? l->l_cred : NOCRED);
   1220 		break;
   1221 	case DIOCGWEDGEINFO:
   1222 	    {
   1223 	    	struct dkwedge_info *dkw = (void *) data;
   1224 
   1225 		strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
   1226 			sizeof(dkw->dkw_devname));
   1227 	    	memcpy(dkw->dkw_wname, sc->sc_wname, sizeof(dkw->dkw_wname));
   1228 		dkw->dkw_wname[sizeof(dkw->dkw_wname) - 1] = '\0';
   1229 		strcpy(dkw->dkw_parent, sc->sc_parent->dk_name);
   1230 		dkw->dkw_offset = sc->sc_offset;
   1231 		dkw->dkw_size = sc->sc_size;
   1232 		strcpy(dkw->dkw_ptype, sc->sc_ptype);
   1233 
   1234 		break;
   1235 	    }
   1236 
   1237 	default:
   1238 		error = ENOTTY;
   1239 	}
   1240 
   1241 	return (error);
   1242 }
   1243 
   1244 /*
   1245  * dksize:		[devsw entry point]
   1246  *
   1247  *	Query the size of a wedge for the purpose of performing a dump
   1248  *	or for swapping to.
   1249  */
   1250 static int
   1251 dksize(dev_t dev)
   1252 {
   1253 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1254 	int rv = -1;
   1255 
   1256 	if (sc == NULL)
   1257 		return (-1);
   1258 
   1259 	if (sc->sc_state != DKW_STATE_RUNNING)
   1260 		return (ENXIO);
   1261 
   1262 	mutex_enter(&sc->sc_dk.dk_openlock);
   1263 	mutex_enter(&sc->sc_parent->dk_rawlock);
   1264 
   1265 	/* Our content type is static, no need to open the device. */
   1266 
   1267 	if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) == 0) {
   1268 		/* Saturate if we are larger than INT_MAX. */
   1269 		if (sc->sc_size > INT_MAX)
   1270 			rv = INT_MAX;
   1271 		else
   1272 			rv = (int) sc->sc_size;
   1273 	}
   1274 
   1275 	mutex_exit(&sc->sc_parent->dk_rawlock);
   1276 	mutex_exit(&sc->sc_dk.dk_openlock);
   1277 
   1278 	return (rv);
   1279 }
   1280 
   1281 /*
   1282  * dkdump:		[devsw entry point]
   1283  *
   1284  *	Perform a crash dump to a wedge.
   1285  */
   1286 static int
   1287 dkdump(dev_t dev, daddr_t blkno, void *va, size_t size)
   1288 {
   1289 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1290 	const struct bdevsw *bdev;
   1291 	int rv = 0;
   1292 
   1293 	if (sc == NULL)
   1294 		return (-1);
   1295 
   1296 	if (sc->sc_state != DKW_STATE_RUNNING)
   1297 		return (ENXIO);
   1298 
   1299 	mutex_enter(&sc->sc_dk.dk_openlock);
   1300 	mutex_enter(&sc->sc_parent->dk_rawlock);
   1301 
   1302 	/* Our content type is static, no need to open the device. */
   1303 
   1304 	if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) != 0) {
   1305 		rv = ENXIO;
   1306 		goto out;
   1307 	}
   1308 	if (size % DEV_BSIZE != 0) {
   1309 		rv = EINVAL;
   1310 		goto out;
   1311 	}
   1312 	if (blkno + size / DEV_BSIZE > sc->sc_size) {
   1313 		printf("%s: blkno (%" PRIu64 ") + size / DEV_BSIZE (%zu) > "
   1314 		    "sc->sc_size (%" PRIu64 ")\n", __func__, blkno,
   1315 		    size / DEV_BSIZE, sc->sc_size);
   1316 		rv = EINVAL;
   1317 		goto out;
   1318 	}
   1319 
   1320 	bdev = bdevsw_lookup(sc->sc_pdev);
   1321 	rv = (*bdev->d_dump)(sc->sc_pdev, blkno + sc->sc_offset, va, size);
   1322 
   1323 out:
   1324 	mutex_exit(&sc->sc_parent->dk_rawlock);
   1325 	mutex_exit(&sc->sc_dk.dk_openlock);
   1326 
   1327 	return rv;
   1328 }
   1329