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