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