Home | History | Annotate | Line # | Download | only in dkwedge
dk.c revision 1.142
      1  1.142  riastrad /*	$NetBSD: dk.c,v 1.142 2023/04/21 18:30:32 riastradh Exp $	*/
      2    1.1   thorpej 
      3    1.1   thorpej /*-
      4   1.27        ad  * Copyright (c) 2004, 2005, 2006, 2007 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  *
     19    1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20    1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21    1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22    1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23    1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24    1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25    1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26    1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27    1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28    1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29    1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30    1.1   thorpej  */
     31    1.1   thorpej 
     32    1.1   thorpej #include <sys/cdefs.h>
     33  1.142  riastrad __KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.142 2023/04/21 18:30:32 riastradh Exp $");
     34    1.1   thorpej 
     35   1.50     pooka #ifdef _KERNEL_OPT
     36    1.1   thorpej #include "opt_dkwedge.h"
     37   1.50     pooka #endif
     38    1.1   thorpej 
     39    1.1   thorpej #include <sys/param.h>
     40  1.133  riastrad #include <sys/types.h>
     41  1.133  riastrad 
     42    1.5      yamt #include <sys/buf.h>
     43    1.5      yamt #include <sys/bufq.h>
     44  1.133  riastrad #include <sys/callout.h>
     45    1.1   thorpej #include <sys/conf.h>
     46  1.133  riastrad #include <sys/device.h>
     47  1.133  riastrad #include <sys/disk.h>
     48  1.133  riastrad #include <sys/disklabel.h>
     49  1.133  riastrad #include <sys/errno.h>
     50  1.133  riastrad #include <sys/fcntl.h>
     51  1.133  riastrad #include <sys/ioctl.h>
     52  1.133  riastrad #include <sys/kauth.h>
     53    1.1   thorpej #include <sys/kernel.h>
     54    1.1   thorpej #include <sys/malloc.h>
     55  1.133  riastrad #include <sys/pool.h>
     56  1.133  riastrad #include <sys/proc.h>
     57  1.134  riastrad #include <sys/rwlock.h>
     58  1.133  riastrad #include <sys/stat.h>
     59  1.133  riastrad #include <sys/systm.h>
     60  1.133  riastrad #include <sys/vnode.h>
     61    1.1   thorpej 
     62    1.1   thorpej #include <miscfs/specfs/specdev.h>
     63    1.1   thorpej 
     64    1.1   thorpej MALLOC_DEFINE(M_DKWEDGE, "dkwedge", "Disk wedge structures");
     65    1.1   thorpej 
     66    1.1   thorpej typedef enum {
     67    1.1   thorpej 	DKW_STATE_LARVAL	= 0,
     68    1.1   thorpej 	DKW_STATE_RUNNING	= 1,
     69    1.1   thorpej 	DKW_STATE_DYING		= 2,
     70    1.1   thorpej 	DKW_STATE_DEAD		= 666
     71    1.1   thorpej } dkwedge_state_t;
     72    1.1   thorpej 
     73    1.1   thorpej struct dkwedge_softc {
     74   1.65       chs 	device_t	sc_dev;	/* pointer to our pseudo-device */
     75    1.2   thorpej 	struct cfdata	sc_cfdata;	/* our cfdata structure */
     76    1.1   thorpej 	uint8_t		sc_wname[128];	/* wedge name (Unicode, UTF-8) */
     77    1.1   thorpej 
     78    1.1   thorpej 	dkwedge_state_t sc_state;	/* state this wedge is in */
     79    1.1   thorpej 
     80    1.1   thorpej 	struct disk	*sc_parent;	/* parent disk */
     81    1.1   thorpej 	daddr_t		sc_offset;	/* LBA offset of wedge in parent */
     82  1.135  riastrad 	krwlock_t	sc_sizelock;
     83    1.1   thorpej 	uint64_t	sc_size;	/* size of wedge in blocks */
     84    1.1   thorpej 	char		sc_ptype[32];	/* partition type */
     85    1.1   thorpej 	dev_t		sc_pdev;	/* cached parent's dev_t */
     86    1.1   thorpej 					/* link on parent's wedge list */
     87    1.1   thorpej 	LIST_ENTRY(dkwedge_softc) sc_plink;
     88    1.1   thorpej 
     89    1.1   thorpej 	struct disk	sc_dk;		/* our own disk structure */
     90    1.9      yamt 	struct bufq_state *sc_bufq;	/* buffer queue */
     91    1.1   thorpej 	struct callout	sc_restart_ch;	/* callout to restart I/O */
     92    1.1   thorpej 
     93   1.92   mlelstv 	kmutex_t	sc_iolock;
     94   1.92   mlelstv 	kcondvar_t	sc_dkdrn;
     95    1.1   thorpej 	u_int		sc_iopend;	/* I/Os pending */
     96  1.142  riastrad 	bool		sc_iostop;	/* don't schedule restart */
     97  1.103   mlelstv 	int		sc_mode;	/* parent open mode */
     98    1.1   thorpej };
     99    1.1   thorpej 
    100  1.136  riastrad static int	dkwedge_match(device_t, cfdata_t, void *);
    101  1.136  riastrad static void	dkwedge_attach(device_t, device_t, void *);
    102  1.136  riastrad static int	dkwedge_detach(device_t, int);
    103  1.136  riastrad 
    104    1.1   thorpej static void	dkstart(struct dkwedge_softc *);
    105    1.1   thorpej static void	dkiodone(struct buf *);
    106    1.1   thorpej static void	dkrestart(void *);
    107   1.52  jakllsch static void	dkminphys(struct buf *);
    108    1.1   thorpej 
    109  1.118  riastrad static int	dkfirstopen(struct dkwedge_softc *, int);
    110  1.121  riastrad static void	dklastclose(struct dkwedge_softc *);
    111   1.74   mlelstv static int	dkwedge_cleanup_parent(struct dkwedge_softc *, int);
    112   1.47    dyoung static int	dkwedge_detach(device_t, int);
    113   1.74   mlelstv static void	dkwedge_delall1(struct disk *, bool);
    114   1.74   mlelstv static int	dkwedge_del1(struct dkwedge_info *, int);
    115   1.87   mlelstv static int	dk_open_parent(dev_t, int, struct vnode **);
    116   1.82   mlelstv static int	dk_close_parent(struct vnode *, int);
    117   1.46    dyoung 
    118    1.1   thorpej static dev_type_open(dkopen);
    119    1.1   thorpej static dev_type_close(dkclose);
    120  1.141  riastrad static dev_type_cancel(dkcancel);
    121    1.1   thorpej static dev_type_read(dkread);
    122    1.1   thorpej static dev_type_write(dkwrite);
    123    1.1   thorpej static dev_type_ioctl(dkioctl);
    124    1.1   thorpej static dev_type_strategy(dkstrategy);
    125    1.1   thorpej static dev_type_dump(dkdump);
    126    1.1   thorpej static dev_type_size(dksize);
    127   1.72  dholland static dev_type_discard(dkdiscard);
    128    1.1   thorpej 
    129  1.136  riastrad CFDRIVER_DECL(dk, DV_DISK, NULL);
    130  1.136  riastrad CFATTACH_DECL3_NEW(dk, 0,
    131  1.136  riastrad     dkwedge_match, dkwedge_attach, dkwedge_detach, NULL, NULL, NULL,
    132  1.136  riastrad     DVF_DETACH_SHUTDOWN);
    133  1.136  riastrad 
    134    1.1   thorpej const struct bdevsw dk_bdevsw = {
    135   1.68  dholland 	.d_open = dkopen,
    136   1.68  dholland 	.d_close = dkclose,
    137  1.141  riastrad 	.d_cancel = dkcancel,
    138   1.68  dholland 	.d_strategy = dkstrategy,
    139   1.68  dholland 	.d_ioctl = dkioctl,
    140   1.68  dholland 	.d_dump = dkdump,
    141   1.68  dholland 	.d_psize = dksize,
    142   1.72  dholland 	.d_discard = dkdiscard,
    143   1.92   mlelstv 	.d_flag = D_DISK | D_MPSAFE
    144    1.1   thorpej };
    145    1.1   thorpej 
    146    1.1   thorpej const struct cdevsw dk_cdevsw = {
    147   1.68  dholland 	.d_open = dkopen,
    148   1.68  dholland 	.d_close = dkclose,
    149  1.141  riastrad 	.d_cancel = dkcancel,
    150   1.68  dholland 	.d_read = dkread,
    151   1.68  dholland 	.d_write = dkwrite,
    152   1.68  dholland 	.d_ioctl = dkioctl,
    153   1.68  dholland 	.d_stop = nostop,
    154   1.68  dholland 	.d_tty = notty,
    155   1.68  dholland 	.d_poll = nopoll,
    156   1.68  dholland 	.d_mmap = nommap,
    157   1.68  dholland 	.d_kqfilter = nokqfilter,
    158   1.72  dholland 	.d_discard = dkdiscard,
    159   1.92   mlelstv 	.d_flag = D_DISK | D_MPSAFE
    160    1.1   thorpej };
    161    1.1   thorpej 
    162    1.1   thorpej static struct dkwedge_softc **dkwedges;
    163    1.1   thorpej static u_int ndkwedges;
    164   1.27        ad static krwlock_t dkwedges_lock;
    165    1.1   thorpej 
    166    1.1   thorpej static LIST_HEAD(, dkwedge_discovery_method) dkwedge_discovery_methods;
    167   1.27        ad static krwlock_t dkwedge_discovery_methods_lock;
    168    1.1   thorpej 
    169    1.1   thorpej /*
    170    1.2   thorpej  * dkwedge_match:
    171    1.2   thorpej  *
    172    1.2   thorpej  *	Autoconfiguration match function for pseudo-device glue.
    173    1.2   thorpej  */
    174    1.2   thorpej static int
    175  1.129  riastrad dkwedge_match(device_t parent, cfdata_t match, void *aux)
    176    1.2   thorpej {
    177    1.2   thorpej 
    178    1.2   thorpej 	/* Pseudo-device; always present. */
    179  1.128  riastrad 	return 1;
    180    1.2   thorpej }
    181    1.2   thorpej 
    182    1.2   thorpej /*
    183    1.2   thorpej  * dkwedge_attach:
    184    1.2   thorpej  *
    185    1.2   thorpej  *	Autoconfiguration attach function for pseudo-device glue.
    186    1.2   thorpej  */
    187    1.2   thorpej static void
    188  1.129  riastrad dkwedge_attach(device_t parent, device_t self, void *aux)
    189    1.2   thorpej {
    190    1.2   thorpej 
    191   1.31  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    192   1.31  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    193    1.2   thorpej }
    194    1.2   thorpej 
    195    1.2   thorpej /*
    196    1.1   thorpej  * dkwedge_wait_drain:
    197    1.1   thorpej  *
    198    1.1   thorpej  *	Wait for I/O on the wedge to drain.
    199    1.1   thorpej  */
    200    1.1   thorpej static void
    201    1.1   thorpej dkwedge_wait_drain(struct dkwedge_softc *sc)
    202    1.1   thorpej {
    203    1.1   thorpej 
    204   1.92   mlelstv 	mutex_enter(&sc->sc_iolock);
    205  1.110  riastrad 	while (sc->sc_iopend != 0)
    206   1.92   mlelstv 		cv_wait(&sc->sc_dkdrn, &sc->sc_iolock);
    207   1.92   mlelstv 	mutex_exit(&sc->sc_iolock);
    208    1.1   thorpej }
    209    1.1   thorpej 
    210    1.1   thorpej /*
    211    1.1   thorpej  * dkwedge_compute_pdev:
    212    1.1   thorpej  *
    213    1.1   thorpej  *	Compute the parent disk's dev_t.
    214    1.1   thorpej  */
    215    1.1   thorpej static int
    216   1.74   mlelstv dkwedge_compute_pdev(const char *pname, dev_t *pdevp, enum vtype type)
    217    1.1   thorpej {
    218    1.1   thorpej 	const char *name, *cp;
    219   1.63  drochner 	devmajor_t pmaj;
    220   1.63  drochner 	int punit;
    221    1.1   thorpej 	char devname[16];
    222    1.1   thorpej 
    223    1.1   thorpej 	name = pname;
    224   1.74   mlelstv 	switch (type) {
    225   1.74   mlelstv 	case VBLK:
    226   1.74   mlelstv 		pmaj = devsw_name2blk(name, devname, sizeof(devname));
    227   1.74   mlelstv 		break;
    228   1.74   mlelstv 	case VCHR:
    229   1.74   mlelstv 		pmaj = devsw_name2chr(name, devname, sizeof(devname));
    230   1.74   mlelstv 		break;
    231   1.74   mlelstv 	default:
    232   1.75   mlelstv 		pmaj = NODEVMAJOR;
    233   1.74   mlelstv 		break;
    234   1.74   mlelstv 	}
    235   1.75   mlelstv 	if (pmaj == NODEVMAJOR)
    236  1.132  riastrad 		return ENXIO;
    237    1.6     perry 
    238    1.1   thorpej 	name += strlen(devname);
    239    1.1   thorpej 	for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++)
    240    1.1   thorpej 		punit = (punit * 10) + (*cp - '0');
    241    1.1   thorpej 	if (cp == name) {
    242    1.1   thorpej 		/* Invalid parent disk name. */
    243  1.132  riastrad 		return ENXIO;
    244    1.1   thorpej 	}
    245    1.1   thorpej 
    246    1.1   thorpej 	*pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART);
    247    1.1   thorpej 
    248  1.128  riastrad 	return 0;
    249    1.1   thorpej }
    250    1.1   thorpej 
    251    1.1   thorpej /*
    252    1.1   thorpej  * dkwedge_array_expand:
    253    1.1   thorpej  *
    254    1.1   thorpej  *	Expand the dkwedges array.
    255  1.127  riastrad  *
    256  1.127  riastrad  *	Releases and reacquires dkwedges_lock as a writer.
    257    1.1   thorpej  */
    258  1.127  riastrad static int
    259    1.1   thorpej dkwedge_array_expand(void)
    260    1.1   thorpej {
    261    1.1   thorpej 
    262  1.127  riastrad 	const unsigned incr = 16;
    263  1.127  riastrad 	unsigned newcnt, oldcnt;
    264  1.127  riastrad 	struct dkwedge_softc **newarray = NULL, **oldarray = NULL;
    265  1.127  riastrad 
    266  1.127  riastrad 	KASSERT(rw_write_held(&dkwedges_lock));
    267  1.127  riastrad 
    268  1.127  riastrad 	oldcnt = ndkwedges;
    269  1.127  riastrad 	oldarray = dkwedges;
    270  1.127  riastrad 
    271  1.127  riastrad 	if (oldcnt >= INT_MAX - incr)
    272  1.127  riastrad 		return ENFILE;	/* XXX */
    273  1.127  riastrad 	newcnt = oldcnt + incr;
    274  1.127  riastrad 
    275  1.127  riastrad 	rw_exit(&dkwedges_lock);
    276    1.1   thorpej 	newarray = malloc(newcnt * sizeof(*newarray), M_DKWEDGE,
    277    1.1   thorpej 	    M_WAITOK|M_ZERO);
    278  1.127  riastrad 	rw_enter(&dkwedges_lock, RW_WRITER);
    279  1.127  riastrad 
    280  1.127  riastrad 	if (ndkwedges != oldcnt || dkwedges != oldarray) {
    281  1.127  riastrad 		oldarray = NULL; /* already recycled */
    282  1.127  riastrad 		goto out;
    283  1.127  riastrad 	}
    284  1.127  riastrad 
    285  1.127  riastrad 	if (oldarray != NULL)
    286    1.1   thorpej 		memcpy(newarray, dkwedges, ndkwedges * sizeof(*newarray));
    287    1.1   thorpej 	dkwedges = newarray;
    288  1.127  riastrad 	newarray = NULL;	/* transferred to dkwedges */
    289    1.1   thorpej 	ndkwedges = newcnt;
    290  1.127  riastrad 
    291  1.127  riastrad out:	rw_exit(&dkwedges_lock);
    292    1.1   thorpej 	if (oldarray != NULL)
    293    1.1   thorpej 		free(oldarray, M_DKWEDGE);
    294  1.127  riastrad 	if (newarray != NULL)
    295  1.127  riastrad 		free(newarray, M_DKWEDGE);
    296  1.127  riastrad 	rw_enter(&dkwedges_lock, RW_WRITER);
    297  1.127  riastrad 	return 0;
    298    1.1   thorpej }
    299    1.1   thorpej 
    300   1.48      haad static void
    301  1.135  riastrad dkwedge_size_init(struct dkwedge_softc *sc, uint64_t size)
    302  1.135  riastrad {
    303  1.135  riastrad 
    304  1.135  riastrad 	rw_init(&sc->sc_sizelock);
    305  1.135  riastrad 	sc->sc_size = size;
    306  1.135  riastrad }
    307  1.135  riastrad 
    308  1.135  riastrad static void
    309  1.135  riastrad dkwedge_size_fini(struct dkwedge_softc *sc)
    310  1.135  riastrad {
    311  1.135  riastrad 
    312  1.135  riastrad 	rw_destroy(&sc->sc_sizelock);
    313  1.135  riastrad }
    314  1.135  riastrad 
    315  1.135  riastrad static uint64_t
    316  1.135  riastrad dkwedge_size(struct dkwedge_softc *sc)
    317  1.135  riastrad {
    318  1.135  riastrad 	uint64_t size;
    319  1.135  riastrad 
    320  1.135  riastrad 	rw_enter(&sc->sc_sizelock, RW_READER);
    321  1.135  riastrad 	size = sc->sc_size;
    322  1.135  riastrad 	rw_exit(&sc->sc_sizelock);
    323  1.135  riastrad 
    324  1.135  riastrad 	return size;
    325  1.135  riastrad }
    326  1.135  riastrad 
    327  1.135  riastrad static void
    328  1.135  riastrad dkwedge_size_increase(struct dkwedge_softc *sc, uint64_t size)
    329  1.135  riastrad {
    330  1.135  riastrad 
    331  1.135  riastrad 	KASSERT(mutex_owned(&sc->sc_dk.dk_openlock));
    332  1.135  riastrad 
    333  1.135  riastrad 	rw_enter(&sc->sc_sizelock, RW_WRITER);
    334  1.135  riastrad 	KASSERTMSG(size >= sc->sc_size,
    335  1.135  riastrad 	    "decreasing dkwedge size from %"PRIu64" to %"PRIu64,
    336  1.135  riastrad 	    sc->sc_size, size);
    337  1.135  riastrad 	sc->sc_size = size;
    338  1.135  riastrad 	rw_exit(&sc->sc_sizelock);
    339  1.135  riastrad }
    340  1.135  riastrad 
    341  1.135  riastrad static void
    342   1.77   mlelstv dk_set_geometry(struct dkwedge_softc *sc, struct disk *pdk)
    343   1.48      haad {
    344   1.77   mlelstv 	struct disk *dk = &sc->sc_dk;
    345   1.77   mlelstv 	struct disk_geom *dg = &dk->dk_geom;
    346   1.48      haad 
    347  1.140  riastrad 	KASSERT(mutex_owned(&pdk->dk_openlock));
    348  1.140  riastrad 
    349   1.66  christos 	memset(dg, 0, sizeof(*dg));
    350   1.48      haad 
    351  1.135  riastrad 	dg->dg_secperunit = dkwedge_size(sc);
    352   1.77   mlelstv 	dg->dg_secsize = DEV_BSIZE << pdk->dk_blkshift;
    353   1.76   mlelstv 
    354   1.76   mlelstv 	/* fake numbers, 1 cylinder is 1 MB with default sector size */
    355   1.66  christos 	dg->dg_nsectors = 32;
    356   1.66  christos 	dg->dg_ntracks = 64;
    357  1.129  riastrad 	dg->dg_ncylinders =
    358  1.129  riastrad 	    dg->dg_secperunit / (dg->dg_nsectors * dg->dg_ntracks);
    359   1.48      haad 
    360   1.77   mlelstv 	disk_set_info(sc->sc_dev, dk, NULL);
    361   1.48      haad }
    362   1.48      haad 
    363    1.1   thorpej /*
    364    1.1   thorpej  * dkwedge_add:		[exported function]
    365    1.1   thorpej  *
    366    1.1   thorpej  *	Add a disk wedge based on the provided information.
    367    1.1   thorpej  *
    368    1.1   thorpej  *	The incoming dkw_devname[] is ignored, instead being
    369    1.1   thorpej  *	filled in and returned to the caller.
    370    1.1   thorpej  */
    371    1.1   thorpej int
    372    1.1   thorpej dkwedge_add(struct dkwedge_info *dkw)
    373    1.1   thorpej {
    374    1.1   thorpej 	struct dkwedge_softc *sc, *lsc;
    375    1.1   thorpej 	struct disk *pdk;
    376    1.1   thorpej 	u_int unit;
    377    1.1   thorpej 	int error;
    378    1.1   thorpej 	dev_t pdev;
    379    1.1   thorpej 
    380    1.1   thorpej 	dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0';
    381    1.1   thorpej 	pdk = disk_find(dkw->dkw_parent);
    382    1.1   thorpej 	if (pdk == NULL)
    383  1.132  riastrad 		return ENXIO;
    384    1.1   thorpej 
    385   1.74   mlelstv 	error = dkwedge_compute_pdev(pdk->dk_name, &pdev, VBLK);
    386    1.1   thorpej 	if (error)
    387  1.128  riastrad 		return error;
    388    1.1   thorpej 
    389    1.1   thorpej 	if (dkw->dkw_offset < 0)
    390  1.128  riastrad 		return EINVAL;
    391    1.1   thorpej 
    392  1.101  jmcneill 	/*
    393  1.101  jmcneill 	 * Check for an existing wedge at the same disk offset. Allow
    394  1.101  jmcneill 	 * updating a wedge if the only change is the size, and the new
    395  1.101  jmcneill 	 * size is larger than the old.
    396  1.101  jmcneill 	 */
    397  1.101  jmcneill 	sc = NULL;
    398  1.101  jmcneill 	mutex_enter(&pdk->dk_openlock);
    399  1.101  jmcneill 	LIST_FOREACH(lsc, &pdk->dk_wedges, sc_plink) {
    400  1.101  jmcneill 		if (lsc->sc_offset != dkw->dkw_offset)
    401  1.101  jmcneill 			continue;
    402  1.101  jmcneill 		if (strcmp(lsc->sc_wname, dkw->dkw_wname) != 0)
    403  1.101  jmcneill 			break;
    404  1.101  jmcneill 		if (strcmp(lsc->sc_ptype, dkw->dkw_ptype) != 0)
    405  1.101  jmcneill 			break;
    406  1.135  riastrad 		if (dkwedge_size(lsc) > dkw->dkw_size)
    407  1.101  jmcneill 			break;
    408  1.101  jmcneill 
    409  1.101  jmcneill 		sc = lsc;
    410  1.135  riastrad 		dkwedge_size_increase(sc, dkw->dkw_size);
    411  1.101  jmcneill 		dk_set_geometry(sc, pdk);
    412  1.101  jmcneill 
    413  1.101  jmcneill 		break;
    414  1.101  jmcneill 	}
    415  1.101  jmcneill 	mutex_exit(&pdk->dk_openlock);
    416  1.101  jmcneill 
    417  1.101  jmcneill 	if (sc != NULL)
    418  1.101  jmcneill 		goto announce;
    419  1.101  jmcneill 
    420    1.1   thorpej 	sc = malloc(sizeof(*sc), M_DKWEDGE, M_WAITOK|M_ZERO);
    421    1.1   thorpej 	sc->sc_state = DKW_STATE_LARVAL;
    422    1.1   thorpej 	sc->sc_parent = pdk;
    423    1.1   thorpej 	sc->sc_pdev = pdev;
    424    1.1   thorpej 	sc->sc_offset = dkw->dkw_offset;
    425  1.135  riastrad 	dkwedge_size_init(sc, dkw->dkw_size);
    426    1.1   thorpej 
    427    1.1   thorpej 	memcpy(sc->sc_wname, dkw->dkw_wname, sizeof(sc->sc_wname));
    428    1.1   thorpej 	sc->sc_wname[sizeof(sc->sc_wname) - 1] = '\0';
    429    1.1   thorpej 
    430    1.1   thorpej 	memcpy(sc->sc_ptype, dkw->dkw_ptype, sizeof(sc->sc_ptype));
    431    1.1   thorpej 	sc->sc_ptype[sizeof(sc->sc_ptype) - 1] = '\0';
    432    1.1   thorpej 
    433    1.9      yamt 	bufq_alloc(&sc->sc_bufq, "fcfs", 0);
    434    1.1   thorpej 
    435   1.26        ad 	callout_init(&sc->sc_restart_ch, 0);
    436    1.1   thorpej 	callout_setfunc(&sc->sc_restart_ch, dkrestart, sc);
    437    1.1   thorpej 
    438   1.92   mlelstv 	mutex_init(&sc->sc_iolock, MUTEX_DEFAULT, IPL_BIO);
    439   1.92   mlelstv 	cv_init(&sc->sc_dkdrn, "dkdrn");
    440   1.92   mlelstv 
    441    1.1   thorpej 	/*
    442    1.1   thorpej 	 * Wedge will be added; increment the wedge count for the parent.
    443  1.107    andvar 	 * Only allow this to happen if RAW_PART is the only thing open.
    444    1.1   thorpej 	 */
    445   1.27        ad 	mutex_enter(&pdk->dk_openlock);
    446    1.1   thorpej 	if (pdk->dk_openmask & ~(1 << RAW_PART))
    447    1.1   thorpej 		error = EBUSY;
    448    1.1   thorpej 	else {
    449    1.1   thorpej 		/* Check for wedge overlap. */
    450    1.1   thorpej 		LIST_FOREACH(lsc, &pdk->dk_wedges, sc_plink) {
    451  1.135  riastrad 			/* XXX arithmetic overflow */
    452  1.135  riastrad 			uint64_t size = dkwedge_size(sc);
    453  1.135  riastrad 			uint64_t lsize = dkwedge_size(lsc);
    454  1.135  riastrad 			daddr_t lastblk = sc->sc_offset + size - 1;
    455  1.135  riastrad 			daddr_t llastblk = lsc->sc_offset + lsize - 1;
    456    1.1   thorpej 
    457    1.1   thorpej 			if (sc->sc_offset >= lsc->sc_offset &&
    458    1.1   thorpej 			    sc->sc_offset <= llastblk) {
    459   1.63  drochner 				/* Overlaps the tail of the existing wedge. */
    460    1.1   thorpej 				break;
    461    1.1   thorpej 			}
    462    1.1   thorpej 			if (lastblk >= lsc->sc_offset &&
    463    1.1   thorpej 			    lastblk <= llastblk) {
    464    1.1   thorpej 				/* Overlaps the head of the existing wedge. */
    465    1.1   thorpej 			    	break;
    466    1.1   thorpej 			}
    467    1.1   thorpej 		}
    468   1.74   mlelstv 		if (lsc != NULL) {
    469   1.74   mlelstv 			if (sc->sc_offset == lsc->sc_offset &&
    470  1.135  riastrad 			    dkwedge_size(sc) == dkwedge_size(lsc) &&
    471   1.74   mlelstv 			    strcmp(sc->sc_wname, lsc->sc_wname) == 0)
    472   1.74   mlelstv 				error = EEXIST;
    473   1.74   mlelstv 			else
    474   1.74   mlelstv 				error = EINVAL;
    475   1.74   mlelstv 		} else {
    476    1.1   thorpej 			pdk->dk_nwedges++;
    477    1.1   thorpej 			LIST_INSERT_HEAD(&pdk->dk_wedges, sc, sc_plink);
    478    1.1   thorpej 		}
    479    1.1   thorpej 	}
    480   1.27        ad 	mutex_exit(&pdk->dk_openlock);
    481    1.1   thorpej 	if (error) {
    482   1.93   mlelstv 		cv_destroy(&sc->sc_dkdrn);
    483   1.93   mlelstv 		mutex_destroy(&sc->sc_iolock);
    484    1.9      yamt 		bufq_free(sc->sc_bufq);
    485  1.135  riastrad 		dkwedge_size_fini(sc);
    486    1.1   thorpej 		free(sc, M_DKWEDGE);
    487  1.128  riastrad 		return error;
    488    1.1   thorpej 	}
    489    1.1   thorpej 
    490    1.2   thorpej 	/* Fill in our cfdata for the pseudo-device glue. */
    491    1.2   thorpej 	sc->sc_cfdata.cf_name = dk_cd.cd_name;
    492    1.2   thorpej 	sc->sc_cfdata.cf_atname = dk_ca.ca_name;
    493    1.2   thorpej 	/* sc->sc_cfdata.cf_unit set below */
    494    1.8   nathanw 	sc->sc_cfdata.cf_fstate = FSTATE_STAR;
    495    1.2   thorpej 
    496    1.1   thorpej 	/* Insert the larval wedge into the array. */
    497   1.27        ad 	rw_enter(&dkwedges_lock, RW_WRITER);
    498    1.1   thorpej 	for (error = 0;;) {
    499    1.1   thorpej 		struct dkwedge_softc **scpp;
    500    1.1   thorpej 
    501    1.1   thorpej 		/*
    502    1.1   thorpej 		 * Check for a duplicate wname while searching for
    503    1.1   thorpej 		 * a slot.
    504    1.1   thorpej 		 */
    505    1.1   thorpej 		for (scpp = NULL, unit = 0; unit < ndkwedges; unit++) {
    506    1.1   thorpej 			if (dkwedges[unit] == NULL) {
    507    1.1   thorpej 				if (scpp == NULL) {
    508    1.1   thorpej 					scpp = &dkwedges[unit];
    509    1.2   thorpej 					sc->sc_cfdata.cf_unit = unit;
    510    1.1   thorpej 				}
    511    1.1   thorpej 			} else {
    512    1.1   thorpej 				/* XXX Unicode. */
    513    1.1   thorpej 				if (strcmp(dkwedges[unit]->sc_wname,
    514  1.129  riastrad 					sc->sc_wname) == 0) {
    515    1.1   thorpej 					error = EEXIST;
    516    1.1   thorpej 					break;
    517    1.1   thorpej 				}
    518    1.1   thorpej 			}
    519    1.1   thorpej 		}
    520    1.1   thorpej 		if (error)
    521    1.1   thorpej 			break;
    522    1.1   thorpej 		KASSERT(unit == ndkwedges);
    523  1.127  riastrad 		if (scpp == NULL) {
    524  1.127  riastrad 			error = dkwedge_array_expand();
    525  1.127  riastrad 			if (error)
    526  1.127  riastrad 				break;
    527  1.127  riastrad 		} else {
    528    1.2   thorpej 			KASSERT(scpp == &dkwedges[sc->sc_cfdata.cf_unit]);
    529    1.1   thorpej 			*scpp = sc;
    530    1.1   thorpej 			break;
    531    1.1   thorpej 		}
    532    1.1   thorpej 	}
    533   1.27        ad 	rw_exit(&dkwedges_lock);
    534    1.1   thorpej 	if (error) {
    535   1.27        ad 		mutex_enter(&pdk->dk_openlock);
    536    1.1   thorpej 		pdk->dk_nwedges--;
    537    1.1   thorpej 		LIST_REMOVE(sc, sc_plink);
    538   1.27        ad 		mutex_exit(&pdk->dk_openlock);
    539    1.1   thorpej 
    540   1.93   mlelstv 		cv_destroy(&sc->sc_dkdrn);
    541   1.93   mlelstv 		mutex_destroy(&sc->sc_iolock);
    542    1.9      yamt 		bufq_free(sc->sc_bufq);
    543  1.135  riastrad 		dkwedge_size_fini(sc);
    544    1.1   thorpej 		free(sc, M_DKWEDGE);
    545  1.128  riastrad 		return error;
    546    1.1   thorpej 	}
    547    1.1   thorpej 
    548    1.2   thorpej 	/*
    549    1.2   thorpej 	 * Now that we know the unit #, attach a pseudo-device for
    550    1.2   thorpej 	 * this wedge instance.  This will provide us with the
    551   1.65       chs 	 * device_t necessary for glue to other parts of the system.
    552    1.2   thorpej 	 *
    553    1.2   thorpej 	 * This should never fail, unless we're almost totally out of
    554    1.2   thorpej 	 * memory.
    555    1.2   thorpej 	 */
    556    1.2   thorpej 	if ((sc->sc_dev = config_attach_pseudo(&sc->sc_cfdata)) == NULL) {
    557    1.2   thorpej 		aprint_error("%s%u: unable to attach pseudo-device\n",
    558    1.2   thorpej 		    sc->sc_cfdata.cf_name, sc->sc_cfdata.cf_unit);
    559    1.2   thorpej 
    560   1.27        ad 		rw_enter(&dkwedges_lock, RW_WRITER);
    561  1.139  riastrad 		KASSERT(dkwedges[sc->sc_cfdata.cf_unit] == sc);
    562    1.2   thorpej 		dkwedges[sc->sc_cfdata.cf_unit] = NULL;
    563   1.27        ad 		rw_exit(&dkwedges_lock);
    564    1.2   thorpej 
    565   1.27        ad 		mutex_enter(&pdk->dk_openlock);
    566    1.2   thorpej 		pdk->dk_nwedges--;
    567    1.2   thorpej 		LIST_REMOVE(sc, sc_plink);
    568   1.27        ad 		mutex_exit(&pdk->dk_openlock);
    569    1.2   thorpej 
    570   1.93   mlelstv 		cv_destroy(&sc->sc_dkdrn);
    571   1.93   mlelstv 		mutex_destroy(&sc->sc_iolock);
    572    1.9      yamt 		bufq_free(sc->sc_bufq);
    573  1.135  riastrad 		dkwedge_size_fini(sc);
    574    1.2   thorpej 		free(sc, M_DKWEDGE);
    575  1.128  riastrad 		return ENOMEM;
    576    1.2   thorpej 	}
    577    1.1   thorpej 
    578    1.1   thorpej 	/*
    579    1.1   thorpej 	 * XXX Really ought to make the disk_attach() and the changing
    580    1.1   thorpej 	 * of state to RUNNING atomic.
    581    1.1   thorpej 	 */
    582    1.1   thorpej 
    583   1.36    cegger 	disk_init(&sc->sc_dk, device_xname(sc->sc_dev), NULL);
    584  1.140  riastrad 	mutex_enter(&pdk->dk_openlock);
    585   1.77   mlelstv 	dk_set_geometry(sc, pdk);
    586  1.140  riastrad 	mutex_exit(&pdk->dk_openlock);
    587    1.1   thorpej 	disk_attach(&sc->sc_dk);
    588    1.1   thorpej 
    589    1.1   thorpej 	/* Disk wedge is ready for use! */
    590    1.1   thorpej 	sc->sc_state = DKW_STATE_RUNNING;
    591    1.1   thorpej 
    592  1.101  jmcneill announce:
    593    1.1   thorpej 	/* Announce our arrival. */
    594   1.84  jmcneill 	aprint_normal(
    595   1.84  jmcneill 	    "%s at %s: \"%s\", %"PRIu64" blocks at %"PRId64", type: %s\n",
    596   1.84  jmcneill 	    device_xname(sc->sc_dev), pdk->dk_name,
    597   1.84  jmcneill 	    sc->sc_wname,	/* XXX Unicode */
    598  1.135  riastrad 	    dkwedge_size(sc), sc->sc_offset,
    599   1.84  jmcneill 	    sc->sc_ptype[0] == '\0' ? "<unknown>" : sc->sc_ptype);
    600    1.1   thorpej 
    601  1.112    martin 	/* Return the devname to the caller. */
    602  1.112    martin 	strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
    603  1.129  riastrad 	    sizeof(dkw->dkw_devname));
    604  1.112    martin 
    605  1.128  riastrad 	return 0;
    606    1.1   thorpej }
    607    1.1   thorpej 
    608    1.1   thorpej /*
    609   1.47    dyoung  * dkwedge_find:
    610    1.1   thorpej  *
    611   1.47    dyoung  *	Lookup a disk wedge based on the provided information.
    612    1.1   thorpej  *	NOTE: We look up the wedge based on the wedge devname,
    613    1.1   thorpej  *	not wname.
    614   1.47    dyoung  *
    615   1.47    dyoung  *	Return NULL if the wedge is not found, otherwise return
    616   1.47    dyoung  *	the wedge's softc.  Assign the wedge's unit number to unitp
    617   1.47    dyoung  *	if unitp is not NULL.
    618    1.1   thorpej  */
    619   1.47    dyoung static struct dkwedge_softc *
    620   1.47    dyoung dkwedge_find(struct dkwedge_info *dkw, u_int *unitp)
    621    1.1   thorpej {
    622    1.1   thorpej 	struct dkwedge_softc *sc = NULL;
    623    1.1   thorpej 	u_int unit;
    624    1.1   thorpej 
    625    1.1   thorpej 	/* Find our softc. */
    626    1.1   thorpej 	dkw->dkw_devname[sizeof(dkw->dkw_devname) - 1] = '\0';
    627   1.47    dyoung 	rw_enter(&dkwedges_lock, RW_READER);
    628    1.1   thorpej 	for (unit = 0; unit < ndkwedges; unit++) {
    629    1.1   thorpej 		if ((sc = dkwedges[unit]) != NULL &&
    630   1.36    cegger 		    strcmp(device_xname(sc->sc_dev), dkw->dkw_devname) == 0 &&
    631    1.1   thorpej 		    strcmp(sc->sc_parent->dk_name, dkw->dkw_parent) == 0) {
    632    1.1   thorpej 			break;
    633    1.1   thorpej 		}
    634    1.1   thorpej 	}
    635   1.27        ad 	rw_exit(&dkwedges_lock);
    636  1.137  riastrad 	if (sc == NULL)
    637   1.47    dyoung 		return NULL;
    638   1.47    dyoung 
    639   1.47    dyoung 	if (unitp != NULL)
    640   1.47    dyoung 		*unitp = unit;
    641   1.47    dyoung 
    642   1.47    dyoung 	return sc;
    643   1.47    dyoung }
    644   1.47    dyoung 
    645   1.47    dyoung /*
    646   1.47    dyoung  * dkwedge_del:		[exported function]
    647   1.47    dyoung  *
    648   1.47    dyoung  *	Delete a disk wedge based on the provided information.
    649   1.47    dyoung  *	NOTE: We look up the wedge based on the wedge devname,
    650   1.47    dyoung  *	not wname.
    651   1.47    dyoung  */
    652   1.47    dyoung int
    653   1.47    dyoung dkwedge_del(struct dkwedge_info *dkw)
    654   1.47    dyoung {
    655  1.129  riastrad 
    656   1.74   mlelstv 	return dkwedge_del1(dkw, 0);
    657   1.74   mlelstv }
    658   1.74   mlelstv 
    659   1.74   mlelstv int
    660   1.74   mlelstv dkwedge_del1(struct dkwedge_info *dkw, int flags)
    661   1.74   mlelstv {
    662   1.47    dyoung 	struct dkwedge_softc *sc = NULL;
    663   1.47    dyoung 
    664   1.47    dyoung 	/* Find our softc. */
    665   1.47    dyoung 	if ((sc = dkwedge_find(dkw, NULL)) == NULL)
    666  1.128  riastrad 		return ESRCH;
    667    1.1   thorpej 
    668   1.74   mlelstv 	return config_detach(sc->sc_dev, flags);
    669   1.47    dyoung }
    670   1.47    dyoung 
    671   1.47    dyoung static int
    672   1.74   mlelstv dkwedge_cleanup_parent(struct dkwedge_softc *sc, int flags)
    673   1.47    dyoung {
    674   1.47    dyoung 	struct disk *dk = &sc->sc_dk;
    675   1.47    dyoung 	int rc;
    676   1.47    dyoung 
    677   1.47    dyoung 	rc = 0;
    678   1.47    dyoung 	mutex_enter(&dk->dk_openlock);
    679  1.115  riastrad 	if (dk->dk_openmask == 0) {
    680   1.91   mlelstv 		/* nothing to do */
    681  1.115  riastrad 	} else if ((flags & DETACH_FORCE) == 0) {
    682   1.47    dyoung 		rc = EBUSY;
    683   1.90   mlelstv 	}  else {
    684   1.57    bouyer 		mutex_enter(&sc->sc_parent->dk_rawlock);
    685  1.121  riastrad 		dklastclose(sc);
    686  1.114  riastrad 		mutex_exit(&sc->sc_parent->dk_rawlock);
    687   1.57    bouyer 	}
    688  1.115  riastrad 	mutex_exit(&sc->sc_dk.dk_openlock);
    689   1.47    dyoung 
    690   1.47    dyoung 	return rc;
    691   1.47    dyoung }
    692   1.47    dyoung 
    693   1.47    dyoung /*
    694   1.47    dyoung  * dkwedge_detach:
    695   1.47    dyoung  *
    696   1.47    dyoung  *	Autoconfiguration detach function for pseudo-device glue.
    697   1.47    dyoung  */
    698   1.47    dyoung static int
    699   1.47    dyoung dkwedge_detach(device_t self, int flags)
    700   1.47    dyoung {
    701   1.47    dyoung 	struct dkwedge_softc *sc = NULL;
    702   1.47    dyoung 	u_int unit;
    703   1.92   mlelstv 	int bmaj, cmaj, rc;
    704   1.47    dyoung 
    705   1.47    dyoung 	rw_enter(&dkwedges_lock, RW_WRITER);
    706   1.47    dyoung 	for (unit = 0; unit < ndkwedges; unit++) {
    707   1.47    dyoung 		if ((sc = dkwedges[unit]) != NULL && sc->sc_dev == self)
    708   1.47    dyoung 			break;
    709   1.47    dyoung 	}
    710   1.47    dyoung 	if (unit == ndkwedges)
    711   1.47    dyoung 		rc = ENXIO;
    712   1.74   mlelstv 	else if ((rc = dkwedge_cleanup_parent(sc, flags)) == 0) {
    713   1.47    dyoung 		/* Mark the wedge as dying. */
    714   1.47    dyoung 		sc->sc_state = DKW_STATE_DYING;
    715   1.47    dyoung 	}
    716   1.47    dyoung 	rw_exit(&dkwedges_lock);
    717   1.47    dyoung 
    718   1.47    dyoung 	if (rc != 0)
    719   1.47    dyoung 		return rc;
    720   1.47    dyoung 
    721   1.47    dyoung 	pmf_device_deregister(self);
    722    1.1   thorpej 
    723    1.1   thorpej 	/* Locate the wedge major numbers. */
    724    1.1   thorpej 	bmaj = bdevsw_lookup_major(&dk_bdevsw);
    725    1.1   thorpej 	cmaj = cdevsw_lookup_major(&dk_cdevsw);
    726    1.1   thorpej 
    727    1.1   thorpej 	/* Kill any pending restart. */
    728  1.142  riastrad 	mutex_enter(&sc->sc_iolock);
    729  1.142  riastrad 	sc->sc_iostop = true;
    730  1.142  riastrad 	mutex_exit(&sc->sc_iolock);
    731  1.142  riastrad 	callout_halt(&sc->sc_restart_ch, NULL);
    732    1.1   thorpej 
    733    1.1   thorpej 	/*
    734    1.1   thorpej 	 * dkstart() will kill any queued buffers now that the
    735    1.1   thorpej 	 * state of the wedge is not RUNNING.  Once we've done
    736    1.1   thorpej 	 * that, wait for any other pending I/O to complete.
    737    1.1   thorpej 	 */
    738    1.1   thorpej 	dkstart(sc);
    739    1.1   thorpej 	dkwedge_wait_drain(sc);
    740    1.1   thorpej 
    741    1.1   thorpej 	/* Nuke the vnodes for any open instances. */
    742   1.14   thorpej 	vdevgone(bmaj, unit, unit, VBLK);
    743   1.14   thorpej 	vdevgone(cmaj, unit, unit, VCHR);
    744    1.1   thorpej 
    745    1.1   thorpej 	/* Clean up the parent. */
    746   1.74   mlelstv 	dkwedge_cleanup_parent(sc, flags | DETACH_FORCE);
    747    1.1   thorpej 
    748    1.1   thorpej 	/* Announce our departure. */
    749   1.36    cegger 	aprint_normal("%s at %s (%s) deleted\n", device_xname(sc->sc_dev),
    750    1.1   thorpej 	    sc->sc_parent->dk_name,
    751    1.1   thorpej 	    sc->sc_wname);	/* XXX Unicode */
    752    1.1   thorpej 
    753   1.27        ad 	mutex_enter(&sc->sc_parent->dk_openlock);
    754    1.1   thorpej 	sc->sc_parent->dk_nwedges--;
    755    1.1   thorpej 	LIST_REMOVE(sc, sc_plink);
    756   1.27        ad 	mutex_exit(&sc->sc_parent->dk_openlock);
    757    1.1   thorpej 
    758    1.1   thorpej 	/* Delete our buffer queue. */
    759    1.9      yamt 	bufq_free(sc->sc_bufq);
    760    1.1   thorpej 
    761    1.1   thorpej 	/* Detach from the disk list. */
    762    1.1   thorpej 	disk_detach(&sc->sc_dk);
    763   1.39    plunky 	disk_destroy(&sc->sc_dk);
    764    1.1   thorpej 
    765    1.1   thorpej 	/* Poof. */
    766   1.27        ad 	rw_enter(&dkwedges_lock, RW_WRITER);
    767  1.139  riastrad 	KASSERT(dkwedges[unit] == sc);
    768    1.1   thorpej 	dkwedges[unit] = NULL;
    769    1.1   thorpej 	sc->sc_state = DKW_STATE_DEAD;
    770   1.27        ad 	rw_exit(&dkwedges_lock);
    771    1.1   thorpej 
    772   1.92   mlelstv 	mutex_destroy(&sc->sc_iolock);
    773   1.92   mlelstv 	cv_destroy(&sc->sc_dkdrn);
    774  1.135  riastrad 	dkwedge_size_fini(sc);
    775   1.92   mlelstv 
    776    1.1   thorpej 	free(sc, M_DKWEDGE);
    777    1.1   thorpej 
    778   1.47    dyoung 	return 0;
    779    1.1   thorpej }
    780    1.1   thorpej 
    781    1.1   thorpej /*
    782    1.1   thorpej  * dkwedge_delall:	[exported function]
    783    1.1   thorpej  *
    784    1.1   thorpej  *	Delete all of the wedges on the specified disk.  Used when
    785    1.1   thorpej  *	a disk is being detached.
    786    1.1   thorpej  */
    787    1.1   thorpej void
    788    1.1   thorpej dkwedge_delall(struct disk *pdk)
    789    1.1   thorpej {
    790  1.129  riastrad 
    791   1.74   mlelstv 	dkwedge_delall1(pdk, false);
    792   1.74   mlelstv }
    793   1.74   mlelstv 
    794   1.74   mlelstv static void
    795   1.74   mlelstv dkwedge_delall1(struct disk *pdk, bool idleonly)
    796   1.74   mlelstv {
    797    1.1   thorpej 	struct dkwedge_info dkw;
    798    1.1   thorpej 	struct dkwedge_softc *sc;
    799   1.74   mlelstv 	int flags;
    800   1.74   mlelstv 
    801   1.74   mlelstv 	flags = DETACH_QUIET;
    802  1.129  riastrad 	if (!idleonly)
    803  1.129  riastrad 		flags |= DETACH_FORCE;
    804    1.1   thorpej 
    805    1.1   thorpej 	for (;;) {
    806   1.27        ad 		mutex_enter(&pdk->dk_openlock);
    807   1.74   mlelstv 		LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) {
    808   1.74   mlelstv 			if (!idleonly || sc->sc_dk.dk_openmask == 0)
    809   1.74   mlelstv 				break;
    810   1.74   mlelstv 		}
    811   1.74   mlelstv 		if (sc == NULL) {
    812   1.74   mlelstv 			KASSERT(idleonly || pdk->dk_nwedges == 0);
    813   1.27        ad 			mutex_exit(&pdk->dk_openlock);
    814    1.1   thorpej 			return;
    815    1.1   thorpej 		}
    816   1.94      maya 		strlcpy(dkw.dkw_parent, pdk->dk_name, sizeof(dkw.dkw_parent));
    817   1.36    cegger 		strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
    818  1.129  riastrad 		    sizeof(dkw.dkw_devname));
    819   1.27        ad 		mutex_exit(&pdk->dk_openlock);
    820   1.74   mlelstv 		(void) dkwedge_del1(&dkw, flags);
    821    1.1   thorpej 	}
    822    1.1   thorpej }
    823    1.1   thorpej 
    824    1.1   thorpej /*
    825    1.1   thorpej  * dkwedge_list:	[exported function]
    826    1.1   thorpej  *
    827    1.1   thorpej  *	List all of the wedges on a particular disk.
    828    1.1   thorpej  */
    829    1.1   thorpej int
    830   1.10  christos dkwedge_list(struct disk *pdk, struct dkwedge_list *dkwl, struct lwp *l)
    831    1.1   thorpej {
    832    1.1   thorpej 	struct uio uio;
    833    1.1   thorpej 	struct iovec iov;
    834    1.1   thorpej 	struct dkwedge_softc *sc;
    835    1.1   thorpej 	struct dkwedge_info dkw;
    836    1.1   thorpej 	int error = 0;
    837    1.1   thorpej 
    838    1.1   thorpej 	iov.iov_base = dkwl->dkwl_buf;
    839    1.1   thorpej 	iov.iov_len = dkwl->dkwl_bufsize;
    840    1.1   thorpej 
    841    1.1   thorpej 	uio.uio_iov = &iov;
    842    1.1   thorpej 	uio.uio_iovcnt = 1;
    843    1.1   thorpej 	uio.uio_offset = 0;
    844    1.1   thorpej 	uio.uio_resid = dkwl->dkwl_bufsize;
    845    1.1   thorpej 	uio.uio_rw = UIO_READ;
    846   1.51     pooka 	KASSERT(l == curlwp);
    847   1.51     pooka 	uio.uio_vmspace = l->l_proc->p_vmspace;
    848    1.1   thorpej 
    849    1.1   thorpej 	dkwl->dkwl_ncopied = 0;
    850    1.1   thorpej 
    851   1.27        ad 	mutex_enter(&pdk->dk_openlock);
    852    1.1   thorpej 	LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) {
    853    1.1   thorpej 		if (uio.uio_resid < sizeof(dkw))
    854    1.1   thorpej 			break;
    855    1.1   thorpej 
    856    1.1   thorpej 		if (sc->sc_state != DKW_STATE_RUNNING)
    857    1.1   thorpej 			continue;
    858    1.1   thorpej 
    859   1.36    cegger 		strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
    860  1.129  riastrad 		    sizeof(dkw.dkw_devname));
    861    1.1   thorpej 		memcpy(dkw.dkw_wname, sc->sc_wname, sizeof(dkw.dkw_wname));
    862    1.1   thorpej 		dkw.dkw_wname[sizeof(dkw.dkw_wname) - 1] = '\0';
    863   1.94      maya 		strlcpy(dkw.dkw_parent, sc->sc_parent->dk_name,
    864   1.94      maya 		    sizeof(dkw.dkw_parent));
    865    1.1   thorpej 		dkw.dkw_offset = sc->sc_offset;
    866  1.135  riastrad 		dkw.dkw_size = dkwedge_size(sc);
    867   1.94      maya 		strlcpy(dkw.dkw_ptype, sc->sc_ptype, sizeof(dkw.dkw_ptype));
    868    1.1   thorpej 
    869    1.1   thorpej 		error = uiomove(&dkw, sizeof(dkw), &uio);
    870    1.1   thorpej 		if (error)
    871    1.1   thorpej 			break;
    872    1.1   thorpej 		dkwl->dkwl_ncopied++;
    873    1.1   thorpej 	}
    874    1.1   thorpej 	dkwl->dkwl_nwedges = pdk->dk_nwedges;
    875   1.27        ad 	mutex_exit(&pdk->dk_openlock);
    876    1.1   thorpej 
    877  1.128  riastrad 	return error;
    878    1.1   thorpej }
    879    1.1   thorpej 
    880   1.25    dyoung device_t
    881   1.25    dyoung dkwedge_find_by_wname(const char *wname)
    882   1.25    dyoung {
    883   1.25    dyoung 	device_t dv = NULL;
    884   1.25    dyoung 	struct dkwedge_softc *sc;
    885   1.25    dyoung 	int i;
    886   1.25    dyoung 
    887   1.27        ad 	rw_enter(&dkwedges_lock, RW_WRITER);
    888   1.25    dyoung 	for (i = 0; i < ndkwedges; i++) {
    889   1.25    dyoung 		if ((sc = dkwedges[i]) == NULL)
    890   1.25    dyoung 			continue;
    891   1.25    dyoung 		if (strcmp(sc->sc_wname, wname) == 0) {
    892   1.25    dyoung 			if (dv != NULL) {
    893   1.25    dyoung 				printf(
    894   1.25    dyoung 				    "WARNING: double match for wedge name %s "
    895   1.25    dyoung 				    "(%s, %s)\n", wname, device_xname(dv),
    896   1.25    dyoung 				    device_xname(sc->sc_dev));
    897   1.25    dyoung 				continue;
    898   1.25    dyoung 			}
    899   1.25    dyoung 			dv = sc->sc_dev;
    900   1.25    dyoung 		}
    901   1.25    dyoung 	}
    902   1.27        ad 	rw_exit(&dkwedges_lock);
    903   1.25    dyoung 	return dv;
    904   1.25    dyoung }
    905   1.25    dyoung 
    906   1.89  christos device_t
    907   1.89  christos dkwedge_find_by_parent(const char *name, size_t *i)
    908   1.89  christos {
    909  1.129  riastrad 
    910   1.89  christos 	rw_enter(&dkwedges_lock, RW_WRITER);
    911   1.89  christos 	for (; *i < (size_t)ndkwedges; (*i)++) {
    912   1.89  christos 		struct dkwedge_softc *sc;
    913   1.89  christos 		if ((sc = dkwedges[*i]) == NULL)
    914   1.89  christos 			continue;
    915   1.89  christos 		if (strcmp(sc->sc_parent->dk_name, name) != 0)
    916   1.89  christos 			continue;
    917   1.89  christos 		rw_exit(&dkwedges_lock);
    918   1.89  christos 		return sc->sc_dev;
    919   1.89  christos 	}
    920   1.89  christos 	rw_exit(&dkwedges_lock);
    921   1.89  christos 	return NULL;
    922   1.89  christos }
    923   1.89  christos 
    924   1.25    dyoung void
    925   1.25    dyoung dkwedge_print_wnames(void)
    926   1.25    dyoung {
    927   1.25    dyoung 	struct dkwedge_softc *sc;
    928   1.25    dyoung 	int i;
    929   1.25    dyoung 
    930   1.27        ad 	rw_enter(&dkwedges_lock, RW_WRITER);
    931   1.25    dyoung 	for (i = 0; i < ndkwedges; i++) {
    932   1.25    dyoung 		if ((sc = dkwedges[i]) == NULL)
    933   1.25    dyoung 			continue;
    934   1.25    dyoung 		printf(" wedge:%s", sc->sc_wname);
    935   1.25    dyoung 	}
    936   1.27        ad 	rw_exit(&dkwedges_lock);
    937   1.25    dyoung }
    938   1.25    dyoung 
    939    1.1   thorpej /*
    940   1.18  uebayasi  * We need a dummy object to stuff into the dkwedge discovery method link
    941    1.1   thorpej  * set to ensure that there is always at least one object in the set.
    942    1.1   thorpej  */
    943    1.1   thorpej static struct dkwedge_discovery_method dummy_discovery_method;
    944    1.1   thorpej __link_set_add_bss(dkwedge_methods, dummy_discovery_method);
    945    1.1   thorpej 
    946    1.1   thorpej /*
    947   1.27        ad  * dkwedge_init:
    948    1.1   thorpej  *
    949   1.27        ad  *	Initialize the disk wedge subsystem.
    950    1.1   thorpej  */
    951   1.27        ad void
    952   1.27        ad dkwedge_init(void)
    953    1.1   thorpej {
    954    1.1   thorpej 	__link_set_decl(dkwedge_methods, struct dkwedge_discovery_method);
    955    1.1   thorpej 	struct dkwedge_discovery_method * const *ddmp;
    956    1.1   thorpej 	struct dkwedge_discovery_method *lddm, *ddm;
    957    1.1   thorpej 
    958   1.27        ad 	rw_init(&dkwedges_lock);
    959   1.27        ad 	rw_init(&dkwedge_discovery_methods_lock);
    960   1.27        ad 
    961   1.27        ad 	if (config_cfdriver_attach(&dk_cd) != 0)
    962   1.27        ad 		panic("dkwedge: unable to attach cfdriver");
    963   1.27        ad 	if (config_cfattach_attach(dk_cd.cd_name, &dk_ca) != 0)
    964   1.27        ad 		panic("dkwedge: unable to attach cfattach");
    965    1.1   thorpej 
    966   1.27        ad 	rw_enter(&dkwedge_discovery_methods_lock, RW_WRITER);
    967    1.1   thorpej 
    968    1.1   thorpej 	LIST_INIT(&dkwedge_discovery_methods);
    969    1.1   thorpej 
    970    1.1   thorpej 	__link_set_foreach(ddmp, dkwedge_methods) {
    971    1.1   thorpej 		ddm = *ddmp;
    972    1.1   thorpej 		if (ddm == &dummy_discovery_method)
    973    1.1   thorpej 			continue;
    974    1.1   thorpej 		if (LIST_EMPTY(&dkwedge_discovery_methods)) {
    975    1.1   thorpej 			LIST_INSERT_HEAD(&dkwedge_discovery_methods,
    976  1.129  riastrad 			    ddm, ddm_list);
    977    1.1   thorpej 			continue;
    978    1.1   thorpej 		}
    979    1.1   thorpej 		LIST_FOREACH(lddm, &dkwedge_discovery_methods, ddm_list) {
    980    1.1   thorpej 			if (ddm->ddm_priority == lddm->ddm_priority) {
    981    1.1   thorpej 				aprint_error("dk-method-%s: method \"%s\" "
    982    1.1   thorpej 				    "already exists at priority %d\n",
    983    1.1   thorpej 				    ddm->ddm_name, lddm->ddm_name,
    984    1.1   thorpej 				    lddm->ddm_priority);
    985    1.1   thorpej 				/* Not inserted. */
    986    1.1   thorpej 				break;
    987    1.1   thorpej 			}
    988    1.1   thorpej 			if (ddm->ddm_priority < lddm->ddm_priority) {
    989    1.1   thorpej 				/* Higher priority; insert before. */
    990    1.1   thorpej 				LIST_INSERT_BEFORE(lddm, ddm, ddm_list);
    991    1.1   thorpej 				break;
    992    1.1   thorpej 			}
    993    1.1   thorpej 			if (LIST_NEXT(lddm, ddm_list) == NULL) {
    994    1.1   thorpej 				/* Last one; insert after. */
    995    1.1   thorpej 				KASSERT(lddm->ddm_priority < ddm->ddm_priority);
    996    1.1   thorpej 				LIST_INSERT_AFTER(lddm, ddm, ddm_list);
    997    1.1   thorpej 				break;
    998    1.1   thorpej 			}
    999    1.1   thorpej 		}
   1000    1.1   thorpej 	}
   1001    1.1   thorpej 
   1002   1.27        ad 	rw_exit(&dkwedge_discovery_methods_lock);
   1003    1.1   thorpej }
   1004    1.1   thorpej 
   1005    1.1   thorpej #ifdef DKWEDGE_AUTODISCOVER
   1006    1.1   thorpej int	dkwedge_autodiscover = 1;
   1007    1.1   thorpej #else
   1008    1.1   thorpej int	dkwedge_autodiscover = 0;
   1009    1.1   thorpej #endif
   1010    1.1   thorpej 
   1011    1.1   thorpej /*
   1012    1.1   thorpej  * dkwedge_discover:	[exported function]
   1013    1.1   thorpej  *
   1014    1.1   thorpej  *	Discover the wedges on a newly attached disk.
   1015   1.74   mlelstv  *	Remove all unused wedges on the disk first.
   1016    1.1   thorpej  */
   1017    1.1   thorpej void
   1018    1.1   thorpej dkwedge_discover(struct disk *pdk)
   1019    1.1   thorpej {
   1020    1.1   thorpej 	struct dkwedge_discovery_method *ddm;
   1021    1.1   thorpej 	struct vnode *vp;
   1022    1.1   thorpej 	int error;
   1023    1.1   thorpej 	dev_t pdev;
   1024    1.1   thorpej 
   1025    1.1   thorpej 	/*
   1026    1.1   thorpej 	 * Require people playing with wedges to enable this explicitly.
   1027    1.1   thorpej 	 */
   1028    1.1   thorpej 	if (dkwedge_autodiscover == 0)
   1029    1.1   thorpej 		return;
   1030    1.1   thorpej 
   1031   1.27        ad 	rw_enter(&dkwedge_discovery_methods_lock, RW_READER);
   1032    1.1   thorpej 
   1033   1.74   mlelstv 	/*
   1034   1.74   mlelstv 	 * Use the character device for scanning, the block device
   1035   1.74   mlelstv 	 * is busy if there are already wedges attached.
   1036   1.74   mlelstv 	 */
   1037   1.74   mlelstv 	error = dkwedge_compute_pdev(pdk->dk_name, &pdev, VCHR);
   1038    1.1   thorpej 	if (error) {
   1039    1.1   thorpej 		aprint_error("%s: unable to compute pdev, error = %d\n",
   1040    1.1   thorpej 		    pdk->dk_name, error);
   1041    1.1   thorpej 		goto out;
   1042    1.1   thorpej 	}
   1043    1.1   thorpej 
   1044   1.74   mlelstv 	error = cdevvp(pdev, &vp);
   1045    1.1   thorpej 	if (error) {
   1046    1.1   thorpej 		aprint_error("%s: unable to find vnode for pdev, error = %d\n",
   1047    1.1   thorpej 		    pdk->dk_name, error);
   1048    1.1   thorpej 		goto out;
   1049    1.1   thorpej 	}
   1050    1.1   thorpej 
   1051    1.1   thorpej 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1052    1.1   thorpej 	if (error) {
   1053    1.1   thorpej 		aprint_error("%s: unable to lock vnode for pdev, error = %d\n",
   1054    1.1   thorpej 		    pdk->dk_name, error);
   1055    1.1   thorpej 		vrele(vp);
   1056    1.1   thorpej 		goto out;
   1057    1.1   thorpej 	}
   1058    1.1   thorpej 
   1059   1.62  jmcneill 	error = VOP_OPEN(vp, FREAD | FSILENT, NOCRED);
   1060    1.1   thorpej 	if (error) {
   1061  1.132  riastrad 		if (error != ENXIO)
   1062   1.67     soren 			aprint_error("%s: unable to open device, error = %d\n",
   1063   1.67     soren 			    pdk->dk_name, error);
   1064    1.1   thorpej 		vput(vp);
   1065    1.1   thorpej 		goto out;
   1066    1.1   thorpej 	}
   1067   1.56   hannken 	VOP_UNLOCK(vp);
   1068    1.1   thorpej 
   1069    1.1   thorpej 	/*
   1070   1.74   mlelstv 	 * Remove unused wedges
   1071   1.74   mlelstv 	 */
   1072   1.74   mlelstv 	dkwedge_delall1(pdk, true);
   1073   1.74   mlelstv 
   1074   1.74   mlelstv 	/*
   1075    1.1   thorpej 	 * For each supported partition map type, look to see if
   1076    1.1   thorpej 	 * this map type exists.  If so, parse it and add the
   1077    1.1   thorpej 	 * corresponding wedges.
   1078    1.1   thorpej 	 */
   1079    1.1   thorpej 	LIST_FOREACH(ddm, &dkwedge_discovery_methods, ddm_list) {
   1080    1.1   thorpej 		error = (*ddm->ddm_discover)(pdk, vp);
   1081    1.1   thorpej 		if (error == 0) {
   1082    1.1   thorpej 			/* Successfully created wedges; we're done. */
   1083    1.1   thorpej 			break;
   1084    1.1   thorpej 		}
   1085    1.1   thorpej 	}
   1086    1.1   thorpej 
   1087   1.35        ad 	error = vn_close(vp, FREAD, NOCRED);
   1088    1.1   thorpej 	if (error) {
   1089    1.1   thorpej 		aprint_error("%s: unable to close device, error = %d\n",
   1090    1.1   thorpej 		    pdk->dk_name, error);
   1091    1.1   thorpej 		/* We'll just assume the vnode has been cleaned up. */
   1092    1.1   thorpej 	}
   1093   1.75   mlelstv 
   1094  1.129  riastrad out:
   1095   1.27        ad 	rw_exit(&dkwedge_discovery_methods_lock);
   1096    1.1   thorpej }
   1097    1.1   thorpej 
   1098    1.1   thorpej /*
   1099    1.1   thorpej  * dkwedge_read:
   1100    1.1   thorpej  *
   1101   1.37       agc  *	Read some data from the specified disk, used for
   1102    1.1   thorpej  *	partition discovery.
   1103    1.1   thorpej  */
   1104    1.1   thorpej int
   1105   1.20  christos dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
   1106   1.19  christos     void *tbuf, size_t len)
   1107    1.1   thorpej {
   1108   1.74   mlelstv 	buf_t *bp;
   1109   1.81   mlelstv 	int error;
   1110   1.82   mlelstv 	bool isopen;
   1111   1.82   mlelstv 	dev_t bdev;
   1112   1.83     pooka 	struct vnode *bdvp;
   1113   1.74   mlelstv 
   1114   1.74   mlelstv 	/*
   1115   1.74   mlelstv 	 * The kernel cannot read from a character device vnode
   1116   1.74   mlelstv 	 * as physio() only handles user memory.
   1117   1.74   mlelstv 	 *
   1118   1.82   mlelstv 	 * If the block device has already been opened by a wedge
   1119   1.82   mlelstv 	 * use that vnode and temporarily bump the open counter.
   1120   1.82   mlelstv 	 *
   1121   1.82   mlelstv 	 * Otherwise try to open the block device.
   1122   1.74   mlelstv 	 */
   1123    1.1   thorpej 
   1124   1.82   mlelstv 	bdev = devsw_chr2blk(vp->v_rdev);
   1125   1.82   mlelstv 
   1126   1.82   mlelstv 	mutex_enter(&pdk->dk_rawlock);
   1127   1.82   mlelstv 	if (pdk->dk_rawopens != 0) {
   1128   1.82   mlelstv 		KASSERT(pdk->dk_rawvp != NULL);
   1129   1.82   mlelstv 		isopen = true;
   1130   1.82   mlelstv 		++pdk->dk_rawopens;
   1131   1.83     pooka 		bdvp = pdk->dk_rawvp;
   1132   1.87   mlelstv 		error = 0;
   1133   1.82   mlelstv 	} else {
   1134   1.82   mlelstv 		isopen = false;
   1135   1.87   mlelstv 		error = dk_open_parent(bdev, FREAD, &bdvp);
   1136   1.82   mlelstv 	}
   1137   1.82   mlelstv 	mutex_exit(&pdk->dk_rawlock);
   1138   1.82   mlelstv 
   1139   1.87   mlelstv 	if (error)
   1140   1.87   mlelstv 		return error;
   1141   1.82   mlelstv 
   1142   1.83     pooka 	bp = getiobuf(bdvp, true);
   1143   1.41        ad 	bp->b_flags = B_READ;
   1144   1.74   mlelstv 	bp->b_cflags = BC_BUSY;
   1145   1.82   mlelstv 	bp->b_dev = bdev;
   1146   1.41        ad 	bp->b_data = tbuf;
   1147   1.75   mlelstv 	bp->b_bufsize = bp->b_bcount = len;
   1148   1.74   mlelstv 	bp->b_blkno = blkno;
   1149   1.75   mlelstv 	bp->b_cylinder = 0;
   1150   1.75   mlelstv 	bp->b_error = 0;
   1151   1.74   mlelstv 
   1152   1.83     pooka 	VOP_STRATEGY(bdvp, bp);
   1153   1.74   mlelstv 	error = biowait(bp);
   1154   1.41        ad 	putiobuf(bp);
   1155    1.1   thorpej 
   1156   1.82   mlelstv 	mutex_enter(&pdk->dk_rawlock);
   1157   1.82   mlelstv 	if (isopen) {
   1158   1.82   mlelstv 		--pdk->dk_rawopens;
   1159   1.82   mlelstv 	} else {
   1160   1.83     pooka 		dk_close_parent(bdvp, FREAD);
   1161   1.82   mlelstv 	}
   1162   1.82   mlelstv 	mutex_exit(&pdk->dk_rawlock);
   1163   1.74   mlelstv 
   1164   1.74   mlelstv 	return error;
   1165    1.1   thorpej }
   1166    1.1   thorpej 
   1167    1.1   thorpej /*
   1168    1.1   thorpej  * dkwedge_lookup:
   1169    1.1   thorpej  *
   1170    1.1   thorpej  *	Look up a dkwedge_softc based on the provided dev_t.
   1171    1.1   thorpej  */
   1172    1.1   thorpej static struct dkwedge_softc *
   1173    1.1   thorpej dkwedge_lookup(dev_t dev)
   1174    1.1   thorpej {
   1175  1.137  riastrad 	const int unit = minor(dev);
   1176  1.137  riastrad 	struct dkwedge_softc *sc;
   1177    1.1   thorpej 
   1178  1.137  riastrad 	rw_enter(&dkwedges_lock, RW_READER);
   1179  1.137  riastrad 	if (unit < 0 || unit >= ndkwedges)
   1180  1.137  riastrad 		sc = NULL;
   1181  1.137  riastrad 	else
   1182  1.137  riastrad 		sc = dkwedges[unit];
   1183  1.137  riastrad 	rw_exit(&dkwedges_lock);
   1184    1.1   thorpej 
   1185  1.137  riastrad 	return sc;
   1186    1.1   thorpej }
   1187    1.1   thorpej 
   1188   1.87   mlelstv static int
   1189   1.87   mlelstv dk_open_parent(dev_t dev, int mode, struct vnode **vpp)
   1190   1.82   mlelstv {
   1191   1.82   mlelstv 	struct vnode *vp;
   1192   1.82   mlelstv 	int error;
   1193   1.82   mlelstv 
   1194   1.82   mlelstv 	error = bdevvp(dev, &vp);
   1195   1.82   mlelstv 	if (error)
   1196   1.87   mlelstv 		return error;
   1197   1.82   mlelstv 
   1198   1.82   mlelstv 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1199   1.82   mlelstv 	if (error) {
   1200   1.82   mlelstv 		vrele(vp);
   1201   1.87   mlelstv 		return error;
   1202   1.82   mlelstv 	}
   1203   1.82   mlelstv 	error = VOP_OPEN(vp, mode, NOCRED);
   1204   1.82   mlelstv 	if (error) {
   1205   1.82   mlelstv 		vput(vp);
   1206   1.87   mlelstv 		return error;
   1207   1.82   mlelstv 	}
   1208   1.82   mlelstv 
   1209   1.82   mlelstv 	/* VOP_OPEN() doesn't do this for us. */
   1210   1.82   mlelstv 	if (mode & FWRITE) {
   1211   1.82   mlelstv 		mutex_enter(vp->v_interlock);
   1212   1.82   mlelstv 		vp->v_writecount++;
   1213   1.82   mlelstv 		mutex_exit(vp->v_interlock);
   1214   1.82   mlelstv 	}
   1215   1.82   mlelstv 
   1216   1.82   mlelstv 	VOP_UNLOCK(vp);
   1217   1.82   mlelstv 
   1218   1.87   mlelstv 	*vpp = vp;
   1219   1.87   mlelstv 
   1220   1.87   mlelstv 	return 0;
   1221   1.82   mlelstv }
   1222   1.82   mlelstv 
   1223   1.82   mlelstv static int
   1224   1.82   mlelstv dk_close_parent(struct vnode *vp, int mode)
   1225   1.82   mlelstv {
   1226   1.82   mlelstv 	int error;
   1227   1.82   mlelstv 
   1228   1.82   mlelstv 	error = vn_close(vp, mode, NOCRED);
   1229   1.82   mlelstv 	return error;
   1230   1.82   mlelstv }
   1231   1.82   mlelstv 
   1232    1.1   thorpej /*
   1233    1.1   thorpej  * dkopen:		[devsw entry point]
   1234    1.1   thorpej  *
   1235    1.1   thorpej  *	Open a wedge.
   1236    1.1   thorpej  */
   1237    1.1   thorpej static int
   1238   1.20  christos dkopen(dev_t dev, int flags, int fmt, struct lwp *l)
   1239    1.1   thorpej {
   1240    1.1   thorpej 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1241   1.14   thorpej 	int error = 0;
   1242    1.1   thorpej 
   1243    1.1   thorpej 	if (sc == NULL)
   1244  1.132  riastrad 		return ENXIO;
   1245    1.1   thorpej 	if (sc->sc_state != DKW_STATE_RUNNING)
   1246  1.128  riastrad 		return ENXIO;
   1247    1.1   thorpej 
   1248    1.1   thorpej 	/*
   1249    1.1   thorpej 	 * We go through a complicated little dance to only open the parent
   1250    1.1   thorpej 	 * vnode once per wedge, no matter how many times the wedge is
   1251    1.1   thorpej 	 * opened.  The reason?  We see one dkopen() per open call, but
   1252    1.1   thorpej 	 * only dkclose() on the last close.
   1253    1.1   thorpej 	 */
   1254   1.27        ad 	mutex_enter(&sc->sc_dk.dk_openlock);
   1255   1.27        ad 	mutex_enter(&sc->sc_parent->dk_rawlock);
   1256    1.3   thorpej 	if (sc->sc_dk.dk_openmask == 0) {
   1257  1.118  riastrad 		error = dkfirstopen(sc, flags);
   1258  1.118  riastrad 		if (error)
   1259  1.118  riastrad 			goto popen_fail;
   1260  1.104   mlelstv 	}
   1261  1.104   mlelstv 	KASSERT(sc->sc_mode != 0);
   1262  1.104   mlelstv 	if (flags & ~sc->sc_mode & FWRITE) {
   1263  1.103   mlelstv 		error = EROFS;
   1264  1.103   mlelstv 		goto popen_fail;
   1265    1.1   thorpej 	}
   1266   1.17       dbj 	if (fmt == S_IFCHR)
   1267   1.17       dbj 		sc->sc_dk.dk_copenmask |= 1;
   1268   1.17       dbj 	else
   1269   1.17       dbj 		sc->sc_dk.dk_bopenmask |= 1;
   1270   1.17       dbj 	sc->sc_dk.dk_openmask =
   1271   1.17       dbj 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
   1272    1.1   thorpej 
   1273  1.129  riastrad popen_fail:
   1274   1.27        ad 	mutex_exit(&sc->sc_parent->dk_rawlock);
   1275   1.27        ad 	mutex_exit(&sc->sc_dk.dk_openlock);
   1276  1.128  riastrad 	return error;
   1277    1.1   thorpej }
   1278    1.1   thorpej 
   1279   1.46    dyoung static int
   1280  1.118  riastrad dkfirstopen(struct dkwedge_softc *sc, int flags)
   1281  1.118  riastrad {
   1282  1.118  riastrad 	struct dkwedge_softc *nsc;
   1283  1.118  riastrad 	struct vnode *vp;
   1284  1.118  riastrad 	int mode;
   1285  1.118  riastrad 	int error;
   1286  1.118  riastrad 
   1287  1.118  riastrad 	KASSERT(mutex_owned(&sc->sc_dk.dk_openlock));
   1288  1.118  riastrad 	KASSERT(mutex_owned(&sc->sc_parent->dk_rawlock));
   1289  1.118  riastrad 
   1290  1.118  riastrad 	if (sc->sc_parent->dk_rawopens == 0) {
   1291  1.118  riastrad 		KASSERT(sc->sc_parent->dk_rawvp == NULL);
   1292  1.118  riastrad 		/*
   1293  1.118  riastrad 		 * Try open read-write. If this fails for EROFS
   1294  1.118  riastrad 		 * and wedge is read-only, retry to open read-only.
   1295  1.118  riastrad 		 */
   1296  1.118  riastrad 		mode = FREAD | FWRITE;
   1297  1.118  riastrad 		error = dk_open_parent(sc->sc_pdev, mode, &vp);
   1298  1.118  riastrad 		if (error == EROFS && (flags & FWRITE) == 0) {
   1299  1.118  riastrad 			mode &= ~FWRITE;
   1300  1.118  riastrad 			error = dk_open_parent(sc->sc_pdev, mode, &vp);
   1301  1.118  riastrad 		}
   1302  1.118  riastrad 		if (error)
   1303  1.118  riastrad 			return error;
   1304  1.138  riastrad 		KASSERT(vp != NULL);
   1305  1.118  riastrad 		sc->sc_parent->dk_rawvp = vp;
   1306  1.118  riastrad 	} else {
   1307  1.118  riastrad 		/*
   1308  1.118  riastrad 		 * Retrieve mode from an already opened wedge.
   1309  1.125  riastrad 		 *
   1310  1.125  riastrad 		 * At this point, dk_rawopens is bounded by the number
   1311  1.125  riastrad 		 * of dkwedge devices in the system, which is limited
   1312  1.125  riastrad 		 * by autoconf device numbering to INT_MAX.  Since
   1313  1.125  riastrad 		 * dk_rawopens is unsigned, this can't overflow.
   1314  1.118  riastrad 		 */
   1315  1.125  riastrad 		KASSERT(sc->sc_parent->dk_rawopens < UINT_MAX);
   1316  1.138  riastrad 		KASSERT(sc->sc_parent->dk_rawvp != NULL);
   1317  1.118  riastrad 		mode = 0;
   1318  1.118  riastrad 		LIST_FOREACH(nsc, &sc->sc_parent->dk_wedges, sc_plink) {
   1319  1.118  riastrad 			if (nsc == sc || nsc->sc_dk.dk_openmask == 0)
   1320  1.118  riastrad 				continue;
   1321  1.118  riastrad 			mode = nsc->sc_mode;
   1322  1.118  riastrad 			break;
   1323  1.118  riastrad 		}
   1324  1.118  riastrad 	}
   1325  1.118  riastrad 	sc->sc_mode = mode;
   1326  1.118  riastrad 	sc->sc_parent->dk_rawopens++;
   1327  1.118  riastrad 
   1328  1.118  riastrad 	return 0;
   1329  1.118  riastrad }
   1330  1.118  riastrad 
   1331  1.121  riastrad static void
   1332   1.46    dyoung dklastclose(struct dkwedge_softc *sc)
   1333   1.46    dyoung {
   1334  1.104   mlelstv 
   1335  1.117  riastrad 	KASSERT(mutex_owned(&sc->sc_dk.dk_openlock));
   1336  1.117  riastrad 	KASSERT(mutex_owned(&sc->sc_parent->dk_rawlock));
   1337  1.126  riastrad 	KASSERT(sc->sc_parent->dk_rawopens > 0);
   1338  1.126  riastrad 	KASSERT(sc->sc_parent->dk_rawvp != NULL);
   1339  1.117  riastrad 
   1340  1.120  riastrad 	if (--sc->sc_parent->dk_rawopens == 0) {
   1341  1.120  riastrad 		struct vnode *const vp = sc->sc_parent->dk_rawvp;
   1342  1.120  riastrad 		const int mode = sc->sc_mode;
   1343   1.74   mlelstv 
   1344  1.120  riastrad 		sc->sc_parent->dk_rawvp = NULL;
   1345  1.120  riastrad 		sc->sc_mode = 0;
   1346   1.74   mlelstv 
   1347  1.104   mlelstv 		dk_close_parent(vp, mode);
   1348   1.74   mlelstv 	}
   1349   1.46    dyoung }
   1350   1.46    dyoung 
   1351   1.46    dyoung /*
   1352    1.1   thorpej  * dkclose:		[devsw entry point]
   1353    1.1   thorpej  *
   1354    1.1   thorpej  *	Close a wedge.
   1355    1.1   thorpej  */
   1356    1.1   thorpej static int
   1357   1.20  christos dkclose(dev_t dev, int flags, int fmt, struct lwp *l)
   1358    1.1   thorpej {
   1359    1.1   thorpej 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1360    1.1   thorpej 
   1361   1.59  christos 	if (sc == NULL)
   1362  1.132  riastrad 		return ENXIO;
   1363   1.59  christos 	if (sc->sc_state != DKW_STATE_RUNNING)
   1364  1.121  riastrad 		return ENXIO;
   1365   1.59  christos 
   1366   1.27        ad 	mutex_enter(&sc->sc_dk.dk_openlock);
   1367  1.122  riastrad 	mutex_enter(&sc->sc_parent->dk_rawlock);
   1368    1.1   thorpej 
   1369  1.123  riastrad 	KASSERT(sc->sc_dk.dk_openmask != 0);
   1370  1.123  riastrad 
   1371    1.3   thorpej 	if (fmt == S_IFCHR)
   1372    1.3   thorpej 		sc->sc_dk.dk_copenmask &= ~1;
   1373    1.3   thorpej 	else
   1374    1.3   thorpej 		sc->sc_dk.dk_bopenmask &= ~1;
   1375    1.3   thorpej 	sc->sc_dk.dk_openmask =
   1376    1.3   thorpej 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
   1377    1.3   thorpej 
   1378  1.104   mlelstv 	if (sc->sc_dk.dk_openmask == 0) {
   1379  1.121  riastrad 		dklastclose(sc);
   1380   1.90   mlelstv 	}
   1381    1.1   thorpej 
   1382  1.122  riastrad 	mutex_exit(&sc->sc_parent->dk_rawlock);
   1383  1.115  riastrad 	mutex_exit(&sc->sc_dk.dk_openlock);
   1384  1.115  riastrad 
   1385  1.121  riastrad 	return 0;
   1386    1.1   thorpej }
   1387    1.1   thorpej 
   1388    1.1   thorpej /*
   1389  1.141  riastrad  * dkcancel:		[devsw entry point]
   1390  1.141  riastrad  *
   1391  1.141  riastrad  *	Cancel any pending I/O operations waiting on a wedge.
   1392  1.141  riastrad  */
   1393  1.141  riastrad static int
   1394  1.141  riastrad dkcancel(dev_t dev, int flags, int fmt, struct lwp *l)
   1395  1.141  riastrad {
   1396  1.141  riastrad 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1397  1.141  riastrad 
   1398  1.141  riastrad 	KASSERT(sc != NULL);
   1399  1.141  riastrad 	KASSERT(sc->sc_dev != NULL);
   1400  1.141  riastrad 
   1401  1.141  riastrad 	/*
   1402  1.141  riastrad 	 * Disk I/O is expected to complete or fail within a reasonable
   1403  1.141  riastrad 	 * timeframe -- it's storage, not communication.  Further, the
   1404  1.141  riastrad 	 * character and block device interface guarantees that prior
   1405  1.141  riastrad 	 * reads and writes have completed or failed by the time close
   1406  1.141  riastrad 	 * returns -- we are not to cancel them here.  If the parent
   1407  1.141  riastrad 	 * device's hardware is gone, the parent driver can make them
   1408  1.141  riastrad 	 * fail.  Nothing for dk(4) itself to do.
   1409  1.141  riastrad 	 */
   1410  1.141  riastrad 
   1411  1.141  riastrad 	return 0;
   1412  1.141  riastrad }
   1413  1.141  riastrad 
   1414  1.141  riastrad /*
   1415  1.131  riastrad  * dkstrategy:		[devsw entry point]
   1416    1.1   thorpej  *
   1417    1.1   thorpej  *	Perform I/O based on the wedge I/O strategy.
   1418    1.1   thorpej  */
   1419    1.1   thorpej static void
   1420    1.1   thorpej dkstrategy(struct buf *bp)
   1421    1.1   thorpej {
   1422    1.1   thorpej 	struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
   1423   1.54   mlelstv 	uint64_t p_size, p_offset;
   1424    1.1   thorpej 
   1425   1.59  christos 	if (sc == NULL) {
   1426  1.132  riastrad 		bp->b_error = ENXIO;
   1427   1.59  christos 		goto done;
   1428   1.59  christos 	}
   1429   1.60  christos 
   1430   1.60  christos 	if (sc->sc_state != DKW_STATE_RUNNING ||
   1431   1.60  christos 	    sc->sc_parent->dk_rawvp == NULL) {
   1432    1.1   thorpej 		bp->b_error = ENXIO;
   1433    1.1   thorpej 		goto done;
   1434    1.1   thorpej 	}
   1435    1.1   thorpej 
   1436    1.1   thorpej 	/* If it's an empty transfer, wake up the top half now. */
   1437    1.1   thorpej 	if (bp->b_bcount == 0)
   1438    1.1   thorpej 		goto done;
   1439    1.1   thorpej 
   1440   1.54   mlelstv 	p_offset = sc->sc_offset << sc->sc_parent->dk_blkshift;
   1441  1.135  riastrad 	p_size = dkwedge_size(sc) << sc->sc_parent->dk_blkshift;
   1442   1.54   mlelstv 
   1443    1.1   thorpej 	/* Make sure it's in-range. */
   1444   1.54   mlelstv 	if (bounds_check_with_mediasize(bp, DEV_BSIZE, p_size) <= 0)
   1445    1.1   thorpej 		goto done;
   1446    1.1   thorpej 
   1447    1.1   thorpej 	/* Translate it to the parent's raw LBA. */
   1448   1.54   mlelstv 	bp->b_rawblkno = bp->b_blkno + p_offset;
   1449    1.1   thorpej 
   1450    1.1   thorpej 	/* Place it in the queue and start I/O on the unit. */
   1451   1.92   mlelstv 	mutex_enter(&sc->sc_iolock);
   1452    1.1   thorpej 	sc->sc_iopend++;
   1453   1.96   mlelstv 	disk_wait(&sc->sc_dk);
   1454   1.43      yamt 	bufq_put(sc->sc_bufq, bp);
   1455   1.92   mlelstv 	mutex_exit(&sc->sc_iolock);
   1456   1.92   mlelstv 
   1457    1.1   thorpej 	dkstart(sc);
   1458    1.1   thorpej 	return;
   1459    1.1   thorpej 
   1460  1.129  riastrad done:
   1461    1.1   thorpej 	bp->b_resid = bp->b_bcount;
   1462    1.1   thorpej 	biodone(bp);
   1463    1.1   thorpej }
   1464    1.1   thorpej 
   1465    1.1   thorpej /*
   1466    1.1   thorpej  * dkstart:
   1467    1.1   thorpej  *
   1468    1.1   thorpej  *	Start I/O that has been enqueued on the wedge.
   1469    1.1   thorpej  */
   1470    1.1   thorpej static void
   1471    1.1   thorpej dkstart(struct dkwedge_softc *sc)
   1472    1.1   thorpej {
   1473   1.32        ad 	struct vnode *vp;
   1474    1.1   thorpej 	struct buf *bp, *nbp;
   1475    1.1   thorpej 
   1476   1.92   mlelstv 	mutex_enter(&sc->sc_iolock);
   1477   1.92   mlelstv 
   1478    1.1   thorpej 	/* Do as much work as has been enqueued. */
   1479   1.43      yamt 	while ((bp = bufq_peek(sc->sc_bufq)) != NULL) {
   1480  1.142  riastrad 		if (sc->sc_iostop) {
   1481   1.43      yamt 			(void) bufq_get(sc->sc_bufq);
   1482  1.110  riastrad 			if (--sc->sc_iopend == 0)
   1483   1.92   mlelstv 				cv_broadcast(&sc->sc_dkdrn);
   1484   1.92   mlelstv 			mutex_exit(&sc->sc_iolock);
   1485    1.1   thorpej 			bp->b_error = ENXIO;
   1486    1.1   thorpej 			bp->b_resid = bp->b_bcount;
   1487    1.1   thorpej 			biodone(bp);
   1488   1.92   mlelstv 			mutex_enter(&sc->sc_iolock);
   1489   1.92   mlelstv 			continue;
   1490    1.1   thorpej 		}
   1491    1.1   thorpej 
   1492   1.92   mlelstv 		/* fetch an I/O buf with sc_iolock dropped */
   1493   1.92   mlelstv 		mutex_exit(&sc->sc_iolock);
   1494   1.32        ad 		nbp = getiobuf(sc->sc_parent->dk_rawvp, false);
   1495   1.92   mlelstv 		mutex_enter(&sc->sc_iolock);
   1496    1.1   thorpej 		if (nbp == NULL) {
   1497    1.1   thorpej 			/*
   1498    1.1   thorpej 			 * No resources to run this request; leave the
   1499    1.1   thorpej 			 * buffer queued up, and schedule a timer to
   1500    1.1   thorpej 			 * restart the queue in 1/2 a second.
   1501    1.1   thorpej 			 */
   1502  1.142  riastrad 			if (!sc->sc_iostop)
   1503  1.142  riastrad 				callout_schedule(&sc->sc_restart_ch, hz/2);
   1504   1.92   mlelstv 			break;
   1505   1.92   mlelstv 		}
   1506   1.92   mlelstv 
   1507   1.92   mlelstv 		/*
   1508   1.92   mlelstv 		 * fetch buf, this can fail if another thread
   1509   1.92   mlelstv 		 * has already processed the queue, it can also
   1510   1.92   mlelstv 		 * return a completely different buf.
   1511   1.92   mlelstv 		 */
   1512   1.92   mlelstv 		bp = bufq_get(sc->sc_bufq);
   1513   1.92   mlelstv 		if (bp == NULL) {
   1514   1.92   mlelstv 			mutex_exit(&sc->sc_iolock);
   1515   1.92   mlelstv 			putiobuf(nbp);
   1516   1.92   mlelstv 			mutex_enter(&sc->sc_iolock);
   1517   1.92   mlelstv 			continue;
   1518    1.1   thorpej 		}
   1519    1.1   thorpej 
   1520   1.92   mlelstv 		/* Instrumentation. */
   1521   1.92   mlelstv 		disk_busy(&sc->sc_dk);
   1522   1.92   mlelstv 
   1523   1.92   mlelstv 		/* release lock for VOP_STRATEGY */
   1524   1.92   mlelstv 		mutex_exit(&sc->sc_iolock);
   1525    1.1   thorpej 
   1526    1.1   thorpej 		nbp->b_data = bp->b_data;
   1527   1.32        ad 		nbp->b_flags = bp->b_flags;
   1528   1.32        ad 		nbp->b_oflags = bp->b_oflags;
   1529   1.32        ad 		nbp->b_cflags = bp->b_cflags;
   1530    1.1   thorpej 		nbp->b_iodone = dkiodone;
   1531    1.1   thorpej 		nbp->b_proc = bp->b_proc;
   1532    1.1   thorpej 		nbp->b_blkno = bp->b_rawblkno;
   1533    1.1   thorpej 		nbp->b_dev = sc->sc_parent->dk_rawvp->v_rdev;
   1534    1.1   thorpej 		nbp->b_bcount = bp->b_bcount;
   1535    1.1   thorpej 		nbp->b_private = bp;
   1536    1.1   thorpej 		BIO_COPYPRIO(nbp, bp);
   1537    1.1   thorpej 
   1538   1.32        ad 		vp = nbp->b_vp;
   1539   1.32        ad 		if ((nbp->b_flags & B_READ) == 0) {
   1540   1.61     rmind 			mutex_enter(vp->v_interlock);
   1541   1.32        ad 			vp->v_numoutput++;
   1542   1.61     rmind 			mutex_exit(vp->v_interlock);
   1543   1.32        ad 		}
   1544   1.32        ad 		VOP_STRATEGY(vp, nbp);
   1545   1.92   mlelstv 
   1546   1.92   mlelstv 		mutex_enter(&sc->sc_iolock);
   1547    1.1   thorpej 	}
   1548   1.92   mlelstv 
   1549   1.92   mlelstv 	mutex_exit(&sc->sc_iolock);
   1550    1.1   thorpej }
   1551    1.1   thorpej 
   1552    1.1   thorpej /*
   1553    1.1   thorpej  * dkiodone:
   1554    1.1   thorpej  *
   1555    1.1   thorpej  *	I/O to a wedge has completed; alert the top half.
   1556    1.1   thorpej  */
   1557    1.1   thorpej static void
   1558    1.1   thorpej dkiodone(struct buf *bp)
   1559    1.1   thorpej {
   1560    1.1   thorpej 	struct buf *obp = bp->b_private;
   1561    1.1   thorpej 	struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev);
   1562    1.1   thorpej 
   1563   1.28        ad 	if (bp->b_error != 0)
   1564    1.1   thorpej 		obp->b_error = bp->b_error;
   1565    1.1   thorpej 	obp->b_resid = bp->b_resid;
   1566   1.11      yamt 	putiobuf(bp);
   1567    1.1   thorpej 
   1568   1.92   mlelstv 	mutex_enter(&sc->sc_iolock);
   1569  1.110  riastrad 	if (--sc->sc_iopend == 0)
   1570   1.92   mlelstv 		cv_broadcast(&sc->sc_dkdrn);
   1571    1.1   thorpej 
   1572    1.1   thorpej 	disk_unbusy(&sc->sc_dk, obp->b_bcount - obp->b_resid,
   1573    1.1   thorpej 	    obp->b_flags & B_READ);
   1574   1.92   mlelstv 	mutex_exit(&sc->sc_iolock);
   1575    1.1   thorpej 
   1576    1.1   thorpej 	biodone(obp);
   1577    1.1   thorpej 
   1578    1.1   thorpej 	/* Kick the queue in case there is more work we can do. */
   1579    1.1   thorpej 	dkstart(sc);
   1580    1.1   thorpej }
   1581    1.1   thorpej 
   1582    1.1   thorpej /*
   1583    1.1   thorpej  * dkrestart:
   1584    1.1   thorpej  *
   1585    1.1   thorpej  *	Restart the work queue after it was stalled due to
   1586    1.1   thorpej  *	a resource shortage.  Invoked via a callout.
   1587    1.1   thorpej  */
   1588    1.1   thorpej static void
   1589    1.1   thorpej dkrestart(void *v)
   1590    1.1   thorpej {
   1591    1.1   thorpej 	struct dkwedge_softc *sc = v;
   1592    1.1   thorpej 
   1593    1.1   thorpej 	dkstart(sc);
   1594    1.1   thorpej }
   1595    1.1   thorpej 
   1596    1.1   thorpej /*
   1597   1.52  jakllsch  * dkminphys:
   1598   1.52  jakllsch  *
   1599   1.52  jakllsch  *	Call parent's minphys function.
   1600   1.52  jakllsch  */
   1601   1.52  jakllsch static void
   1602   1.52  jakllsch dkminphys(struct buf *bp)
   1603   1.52  jakllsch {
   1604   1.52  jakllsch 	struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
   1605   1.52  jakllsch 	dev_t dev;
   1606   1.52  jakllsch 
   1607   1.52  jakllsch 	dev = bp->b_dev;
   1608   1.52  jakllsch 	bp->b_dev = sc->sc_pdev;
   1609  1.102   mlelstv 	if (sc->sc_parent->dk_driver && sc->sc_parent->dk_driver->d_minphys)
   1610  1.102   mlelstv 		(*sc->sc_parent->dk_driver->d_minphys)(bp);
   1611  1.102   mlelstv 	else
   1612  1.102   mlelstv 		minphys(bp);
   1613   1.52  jakllsch 	bp->b_dev = dev;
   1614   1.52  jakllsch }
   1615   1.52  jakllsch 
   1616   1.52  jakllsch /*
   1617    1.1   thorpej  * dkread:		[devsw entry point]
   1618    1.1   thorpej  *
   1619    1.1   thorpej  *	Read from a wedge.
   1620    1.1   thorpej  */
   1621    1.1   thorpej static int
   1622   1.20  christos dkread(dev_t dev, struct uio *uio, int flags)
   1623    1.1   thorpej {
   1624    1.1   thorpej 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1625    1.1   thorpej 
   1626   1.59  christos 	if (sc == NULL)
   1627  1.132  riastrad 		return ENXIO;
   1628    1.1   thorpej 	if (sc->sc_state != DKW_STATE_RUNNING)
   1629  1.128  riastrad 		return ENXIO;
   1630    1.6     perry 
   1631  1.128  riastrad 	return physio(dkstrategy, NULL, dev, B_READ, dkminphys, uio);
   1632    1.1   thorpej }
   1633    1.1   thorpej 
   1634    1.1   thorpej /*
   1635    1.1   thorpej  * dkwrite:		[devsw entry point]
   1636    1.1   thorpej  *
   1637    1.1   thorpej  *	Write to a wedge.
   1638    1.1   thorpej  */
   1639    1.1   thorpej static int
   1640   1.20  christos dkwrite(dev_t dev, struct uio *uio, int flags)
   1641    1.1   thorpej {
   1642    1.1   thorpej 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1643    1.1   thorpej 
   1644   1.59  christos 	if (sc == NULL)
   1645  1.132  riastrad 		return ENXIO;
   1646    1.1   thorpej 	if (sc->sc_state != DKW_STATE_RUNNING)
   1647  1.128  riastrad 		return ENXIO;
   1648    1.6     perry 
   1649  1.128  riastrad 	return physio(dkstrategy, NULL, dev, B_WRITE, dkminphys, uio);
   1650    1.1   thorpej }
   1651    1.1   thorpej 
   1652    1.1   thorpej /*
   1653    1.1   thorpej  * dkioctl:		[devsw entry point]
   1654    1.1   thorpej  *
   1655    1.1   thorpej  *	Perform an ioctl request on a wedge.
   1656    1.1   thorpej  */
   1657    1.1   thorpej static int
   1658   1.22  christos dkioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1659    1.1   thorpej {
   1660    1.1   thorpej 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1661    1.1   thorpej 	int error = 0;
   1662    1.1   thorpej 
   1663   1.59  christos 	if (sc == NULL)
   1664  1.132  riastrad 		return ENXIO;
   1665    1.1   thorpej 	if (sc->sc_state != DKW_STATE_RUNNING)
   1666  1.128  riastrad 		return ENXIO;
   1667   1.60  christos 	if (sc->sc_parent->dk_rawvp == NULL)
   1668  1.128  riastrad 		return ENXIO;
   1669    1.1   thorpej 
   1670   1.78  christos 	/*
   1671   1.79  christos 	 * We pass NODEV instead of our device to indicate we don't
   1672   1.78  christos 	 * want to handle disklabel ioctls
   1673   1.78  christos 	 */
   1674   1.79  christos 	error = disk_ioctl(&sc->sc_dk, NODEV, cmd, data, flag, l);
   1675   1.48      haad 	if (error != EPASSTHROUGH)
   1676  1.128  riastrad 		return error;
   1677   1.48      haad 
   1678   1.48      haad 	error = 0;
   1679  1.109    simonb 
   1680    1.1   thorpej 	switch (cmd) {
   1681   1.95  jdolecek 	case DIOCGSTRATEGY:
   1682   1.95  jdolecek 	case DIOCGCACHE:
   1683    1.4   thorpej 	case DIOCCACHESYNC:
   1684   1.95  jdolecek 		error = VOP_IOCTL(sc->sc_parent->dk_rawvp, cmd, data, flag,
   1685  1.129  riastrad 		    l != NULL ? l->l_cred : NOCRED);
   1686    1.4   thorpej 		break;
   1687  1.129  riastrad 	case DIOCGWEDGEINFO: {
   1688  1.130  riastrad 		struct dkwedge_info *dkw = data;
   1689    1.1   thorpej 
   1690   1.36    cegger 		strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
   1691  1.129  riastrad 		    sizeof(dkw->dkw_devname));
   1692    1.1   thorpej 	    	memcpy(dkw->dkw_wname, sc->sc_wname, sizeof(dkw->dkw_wname));
   1693    1.1   thorpej 		dkw->dkw_wname[sizeof(dkw->dkw_wname) - 1] = '\0';
   1694   1.94      maya 		strlcpy(dkw->dkw_parent, sc->sc_parent->dk_name,
   1695   1.94      maya 		    sizeof(dkw->dkw_parent));
   1696    1.1   thorpej 		dkw->dkw_offset = sc->sc_offset;
   1697  1.135  riastrad 		dkw->dkw_size = dkwedge_size(sc);
   1698   1.94      maya 		strlcpy(dkw->dkw_ptype, sc->sc_ptype, sizeof(dkw->dkw_ptype));
   1699    1.1   thorpej 
   1700    1.1   thorpej 		break;
   1701  1.129  riastrad 	}
   1702  1.129  riastrad 	case DIOCGSECTORALIGN: {
   1703  1.100  riastrad 		struct disk_sectoralign *dsa = data;
   1704  1.100  riastrad 		uint32_t r;
   1705  1.100  riastrad 
   1706  1.100  riastrad 		error = VOP_IOCTL(sc->sc_parent->dk_rawvp, cmd, dsa, flag,
   1707  1.100  riastrad 		    l != NULL ? l->l_cred : NOCRED);
   1708  1.100  riastrad 		if (error)
   1709  1.100  riastrad 			break;
   1710    1.1   thorpej 
   1711  1.100  riastrad 		r = sc->sc_offset % dsa->dsa_alignment;
   1712  1.100  riastrad 		if (r < dsa->dsa_firstaligned)
   1713  1.100  riastrad 			dsa->dsa_firstaligned = dsa->dsa_firstaligned - r;
   1714  1.100  riastrad 		else
   1715  1.100  riastrad 			dsa->dsa_firstaligned = (dsa->dsa_firstaligned +
   1716  1.100  riastrad 			    dsa->dsa_alignment) - r;
   1717  1.100  riastrad 		break;
   1718  1.129  riastrad 	}
   1719    1.1   thorpej 	default:
   1720    1.1   thorpej 		error = ENOTTY;
   1721    1.1   thorpej 	}
   1722    1.1   thorpej 
   1723  1.128  riastrad 	return error;
   1724    1.1   thorpej }
   1725    1.1   thorpej 
   1726    1.1   thorpej /*
   1727   1.72  dholland  * dkdiscard:		[devsw entry point]
   1728   1.72  dholland  *
   1729   1.72  dholland  *	Perform a discard-range request on a wedge.
   1730   1.72  dholland  */
   1731   1.72  dholland static int
   1732   1.72  dholland dkdiscard(dev_t dev, off_t pos, off_t len)
   1733   1.72  dholland {
   1734   1.72  dholland 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1735  1.135  riastrad 	uint64_t size = dkwedge_size(sc);
   1736   1.73  riastrad 	unsigned shift;
   1737   1.73  riastrad 	off_t offset, maxlen;
   1738  1.111   hannken 	int error;
   1739   1.72  dholland 
   1740   1.72  dholland 	if (sc == NULL)
   1741  1.132  riastrad 		return ENXIO;
   1742   1.72  dholland 	if (sc->sc_state != DKW_STATE_RUNNING)
   1743  1.128  riastrad 		return ENXIO;
   1744   1.72  dholland 	if (sc->sc_parent->dk_rawvp == NULL)
   1745  1.128  riastrad 		return ENXIO;
   1746   1.72  dholland 
   1747  1.135  riastrad 	/* XXX check bounds on size/offset up front */
   1748   1.73  riastrad 	shift = (sc->sc_parent->dk_blkshift + DEV_BSHIFT);
   1749  1.135  riastrad 	KASSERT(__type_fit(off_t, size));
   1750   1.73  riastrad 	KASSERT(__type_fit(off_t, sc->sc_offset));
   1751   1.73  riastrad 	KASSERT(0 <= sc->sc_offset);
   1752  1.135  riastrad 	KASSERT(size <= (__type_max(off_t) >> shift));
   1753  1.135  riastrad 	KASSERT(sc->sc_offset <= ((__type_max(off_t) >> shift) - size));
   1754   1.73  riastrad 	offset = ((off_t)sc->sc_offset << shift);
   1755  1.135  riastrad 	maxlen = ((off_t)size << shift);
   1756   1.73  riastrad 
   1757   1.73  riastrad 	if (len > maxlen)
   1758  1.128  riastrad 		return EINVAL;
   1759   1.73  riastrad 	if (pos > (maxlen - len))
   1760  1.128  riastrad 		return EINVAL;
   1761   1.73  riastrad 
   1762   1.73  riastrad 	pos += offset;
   1763  1.111   hannken 
   1764  1.111   hannken 	vn_lock(sc->sc_parent->dk_rawvp, LK_EXCLUSIVE | LK_RETRY);
   1765  1.111   hannken 	error = VOP_FDISCARD(sc->sc_parent->dk_rawvp, pos, len);
   1766  1.111   hannken 	VOP_UNLOCK(sc->sc_parent->dk_rawvp);
   1767  1.111   hannken 
   1768  1.111   hannken 	return error;
   1769   1.72  dholland }
   1770   1.72  dholland 
   1771   1.72  dholland /*
   1772    1.1   thorpej  * dksize:		[devsw entry point]
   1773    1.1   thorpej  *
   1774    1.1   thorpej  *	Query the size of a wedge for the purpose of performing a dump
   1775    1.1   thorpej  *	or for swapping to.
   1776    1.1   thorpej  */
   1777    1.1   thorpej static int
   1778    1.1   thorpej dksize(dev_t dev)
   1779    1.1   thorpej {
   1780   1.13   thorpej 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1781  1.106   mlelstv 	uint64_t p_size;
   1782   1.13   thorpej 	int rv = -1;
   1783   1.13   thorpej 
   1784   1.13   thorpej 	if (sc == NULL)
   1785  1.128  riastrad 		return -1;
   1786   1.13   thorpej 	if (sc->sc_state != DKW_STATE_RUNNING)
   1787  1.128  riastrad 		return -1;
   1788   1.13   thorpej 
   1789   1.27        ad 	mutex_enter(&sc->sc_dk.dk_openlock);
   1790   1.27        ad 	mutex_enter(&sc->sc_parent->dk_rawlock);
   1791    1.1   thorpej 
   1792   1.13   thorpej 	/* Our content type is static, no need to open the device. */
   1793   1.13   thorpej 
   1794  1.135  riastrad 	p_size = dkwedge_size(sc) << sc->sc_parent->dk_blkshift;
   1795   1.13   thorpej 	if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) == 0) {
   1796   1.13   thorpej 		/* Saturate if we are larger than INT_MAX. */
   1797  1.106   mlelstv 		if (p_size > INT_MAX)
   1798   1.13   thorpej 			rv = INT_MAX;
   1799   1.13   thorpej 		else
   1800  1.129  riastrad 			rv = (int)p_size;
   1801   1.13   thorpej 	}
   1802   1.13   thorpej 
   1803   1.27        ad 	mutex_exit(&sc->sc_parent->dk_rawlock);
   1804   1.27        ad 	mutex_exit(&sc->sc_dk.dk_openlock);
   1805   1.13   thorpej 
   1806  1.128  riastrad 	return rv;
   1807    1.1   thorpej }
   1808    1.1   thorpej 
   1809    1.1   thorpej /*
   1810    1.1   thorpej  * dkdump:		[devsw entry point]
   1811    1.1   thorpej  *
   1812    1.1   thorpej  *	Perform a crash dump to a wedge.
   1813    1.1   thorpej  */
   1814    1.1   thorpej static int
   1815   1.23    dyoung dkdump(dev_t dev, daddr_t blkno, void *va, size_t size)
   1816    1.1   thorpej {
   1817   1.23    dyoung 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1818   1.23    dyoung 	const struct bdevsw *bdev;
   1819  1.106   mlelstv 	uint64_t p_size, p_offset;
   1820   1.23    dyoung 	int rv = 0;
   1821   1.23    dyoung 
   1822   1.23    dyoung 	if (sc == NULL)
   1823  1.132  riastrad 		return ENXIO;
   1824   1.23    dyoung 	if (sc->sc_state != DKW_STATE_RUNNING)
   1825  1.128  riastrad 		return ENXIO;
   1826   1.23    dyoung 
   1827   1.27        ad 	mutex_enter(&sc->sc_dk.dk_openlock);
   1828   1.27        ad 	mutex_enter(&sc->sc_parent->dk_rawlock);
   1829   1.23    dyoung 
   1830   1.23    dyoung 	/* Our content type is static, no need to open the device. */
   1831   1.23    dyoung 
   1832   1.88   mlelstv 	if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) != 0 &&
   1833   1.99  riastrad 	    strcmp(sc->sc_ptype, DKW_PTYPE_RAID) != 0 &&
   1834   1.99  riastrad 	    strcmp(sc->sc_ptype, DKW_PTYPE_CGD) != 0) {
   1835   1.23    dyoung 		rv = ENXIO;
   1836   1.23    dyoung 		goto out;
   1837   1.23    dyoung 	}
   1838   1.23    dyoung 	if (size % DEV_BSIZE != 0) {
   1839   1.23    dyoung 		rv = EINVAL;
   1840   1.23    dyoung 		goto out;
   1841   1.23    dyoung 	}
   1842  1.106   mlelstv 
   1843  1.106   mlelstv 	p_offset = sc->sc_offset << sc->sc_parent->dk_blkshift;
   1844  1.135  riastrad 	p_size = dkwedge_size(sc) << sc->sc_parent->dk_blkshift;
   1845  1.106   mlelstv 
   1846  1.129  riastrad 	if (blkno < 0 || blkno + size/DEV_BSIZE > p_size) {
   1847   1.23    dyoung 		printf("%s: blkno (%" PRIu64 ") + size / DEV_BSIZE (%zu) > "
   1848  1.106   mlelstv 		    "p_size (%" PRIu64 ")\n", __func__, blkno,
   1849  1.129  riastrad 		    size/DEV_BSIZE, p_size);
   1850   1.23    dyoung 		rv = EINVAL;
   1851   1.23    dyoung 		goto out;
   1852   1.23    dyoung 	}
   1853   1.23    dyoung 
   1854   1.23    dyoung 	bdev = bdevsw_lookup(sc->sc_pdev);
   1855  1.106   mlelstv 	rv = (*bdev->d_dump)(sc->sc_pdev, blkno + p_offset, va, size);
   1856   1.23    dyoung 
   1857   1.23    dyoung out:
   1858   1.27        ad 	mutex_exit(&sc->sc_parent->dk_rawlock);
   1859   1.27        ad 	mutex_exit(&sc->sc_dk.dk_openlock);
   1860    1.1   thorpej 
   1861   1.23    dyoung 	return rv;
   1862    1.1   thorpej }
   1863   1.49     pooka 
   1864   1.49     pooka /*
   1865   1.49     pooka  * config glue
   1866   1.49     pooka  */
   1867   1.49     pooka 
   1868   1.64   mlelstv /*
   1869   1.64   mlelstv  * dkwedge_find_partition
   1870   1.64   mlelstv  *
   1871   1.64   mlelstv  *	Find wedge corresponding to the specified parent name
   1872   1.64   mlelstv  *	and offset/length.
   1873   1.64   mlelstv  */
   1874   1.64   mlelstv device_t
   1875   1.64   mlelstv dkwedge_find_partition(device_t parent, daddr_t startblk, uint64_t nblks)
   1876   1.49     pooka {
   1877   1.64   mlelstv 	struct dkwedge_softc *sc;
   1878   1.64   mlelstv 	int i;
   1879   1.64   mlelstv 	device_t wedge = NULL;
   1880   1.49     pooka 
   1881   1.64   mlelstv 	rw_enter(&dkwedges_lock, RW_READER);
   1882   1.64   mlelstv 	for (i = 0; i < ndkwedges; i++) {
   1883   1.64   mlelstv 		if ((sc = dkwedges[i]) == NULL)
   1884   1.64   mlelstv 			continue;
   1885   1.64   mlelstv 		if (strcmp(sc->sc_parent->dk_name, device_xname(parent)) == 0 &&
   1886   1.64   mlelstv 		    sc->sc_offset == startblk &&
   1887  1.135  riastrad 		    dkwedge_size(sc) == nblks) {
   1888   1.64   mlelstv 			if (wedge) {
   1889   1.64   mlelstv 				printf("WARNING: double match for boot wedge "
   1890   1.64   mlelstv 				    "(%s, %s)\n",
   1891   1.64   mlelstv 				    device_xname(wedge),
   1892   1.64   mlelstv 				    device_xname(sc->sc_dev));
   1893   1.64   mlelstv 				continue;
   1894   1.64   mlelstv 			}
   1895   1.64   mlelstv 			wedge = sc->sc_dev;
   1896   1.64   mlelstv 		}
   1897   1.49     pooka 	}
   1898   1.64   mlelstv 	rw_exit(&dkwedges_lock);
   1899   1.49     pooka 
   1900   1.64   mlelstv 	return wedge;
   1901   1.64   mlelstv }
   1902   1.49     pooka 
   1903   1.69  christos const char *
   1904   1.69  christos dkwedge_get_parent_name(dev_t dev)
   1905   1.69  christos {
   1906   1.69  christos 	/* XXX: perhaps do this in lookup? */
   1907   1.69  christos 	int bmaj = bdevsw_lookup_major(&dk_bdevsw);
   1908   1.69  christos 	int cmaj = cdevsw_lookup_major(&dk_cdevsw);
   1909  1.129  riastrad 
   1910   1.69  christos 	if (major(dev) != bmaj && major(dev) != cmaj)
   1911   1.69  christos 		return NULL;
   1912   1.69  christos 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
   1913   1.69  christos 	if (sc == NULL)
   1914   1.69  christos 		return NULL;
   1915   1.69  christos 	return sc->sc_parent->dk_name;
   1916   1.69  christos }
   1917