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