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