Home | History | Annotate | Line # | Download | only in dev
dksubr.c revision 1.92
      1  1.92   mlelstv /* $NetBSD: dksubr.c,v 1.92 2016/11/28 08:42:20 mlelstv Exp $ */
      2   1.1     elric 
      3   1.1     elric /*-
      4  1.34        ad  * Copyright (c) 1996, 1997, 1998, 1999, 2002, 2008 The NetBSD Foundation, Inc.
      5   1.1     elric  * All rights reserved.
      6   1.1     elric  *
      7   1.1     elric  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     elric  * by Jason R. Thorpe and Roland C. Dowdeswell.
      9   1.1     elric  *
     10   1.1     elric  * Redistribution and use in source and binary forms, with or without
     11   1.1     elric  * modification, are permitted provided that the following conditions
     12   1.1     elric  * are met:
     13   1.1     elric  * 1. Redistributions of source code must retain the above copyright
     14   1.1     elric  *    notice, this list of conditions and the following disclaimer.
     15   1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     elric  *    documentation and/or other materials provided with the distribution.
     18   1.1     elric  *
     19   1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1     elric  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1     elric  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1     elric  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1     elric  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1     elric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1     elric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1     elric  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1     elric  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1     elric  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1     elric  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1     elric  */
     31  1.10     lukem 
     32  1.10     lukem #include <sys/cdefs.h>
     33  1.92   mlelstv __KERNEL_RCSID(0, "$NetBSD: dksubr.c,v 1.92 2016/11/28 08:42:20 mlelstv Exp $");
     34   1.1     elric 
     35   1.1     elric #include <sys/param.h>
     36   1.1     elric #include <sys/systm.h>
     37   1.1     elric #include <sys/stat.h>
     38   1.1     elric #include <sys/proc.h>
     39   1.1     elric #include <sys/ioctl.h>
     40   1.1     elric #include <sys/device.h>
     41   1.1     elric #include <sys/disk.h>
     42   1.1     elric #include <sys/disklabel.h>
     43  1.14      yamt #include <sys/buf.h>
     44  1.14      yamt #include <sys/bufq.h>
     45   1.1     elric #include <sys/vnode.h>
     46   1.1     elric #include <sys/fcntl.h>
     47   1.1     elric #include <sys/namei.h>
     48  1.49  pgoyette #include <sys/module.h>
     49  1.60   mlelstv #include <sys/syslog.h>
     50   1.1     elric 
     51   1.1     elric #include <dev/dkvar.h>
     52  1.51   hannken #include <miscfs/specfs/specdev.h> /* for v_rdev */
     53   1.1     elric 
     54  1.44     elric int	dkdebug = 0;
     55   1.1     elric 
     56   1.1     elric #ifdef DEBUG
     57   1.1     elric #define DKDB_FOLLOW	0x1
     58   1.1     elric #define DKDB_INIT	0x2
     59   1.1     elric #define DKDB_VNODE	0x4
     60  1.77  christos #define DKDB_DUMP	0x8
     61   1.1     elric 
     62   1.1     elric #define IFDEBUG(x,y)		if (dkdebug & (x)) y
     63   1.1     elric #define DPRINTF(x,y)		IFDEBUG(x, printf y)
     64   1.1     elric #define DPRINTF_FOLLOW(y)	DPRINTF(DKDB_FOLLOW, y)
     65   1.1     elric #else
     66   1.1     elric #define IFDEBUG(x,y)
     67   1.1     elric #define DPRINTF(x,y)
     68   1.1     elric #define DPRINTF_FOLLOW(y)
     69   1.1     elric #endif
     70   1.1     elric 
     71  1.77  christos #define DKF_READYFORDUMP	(DKF_INITED|DKF_TAKEDUMP)
     72  1.77  christos 
     73  1.49  pgoyette static int dk_subr_modcmd(modcmd_t, void *);
     74  1.49  pgoyette 
     75   1.1     elric #define DKLABELDEV(dev)	\
     76   1.1     elric 	(MAKEDISKDEV(major((dev)), DISKUNIT((dev)), RAW_PART))
     77   1.1     elric 
     78  1.60   mlelstv static void	dk_makedisklabel(struct dk_softc *);
     79  1.71   mlelstv static int	dk_translate(struct dk_softc *, struct buf *);
     80  1.74   mlelstv static void	dk_done1(struct dk_softc *, struct buf *, bool);
     81   1.1     elric 
     82   1.1     elric void
     83  1.60   mlelstv dk_init(struct dk_softc *dksc, device_t dev, int dtype)
     84   1.1     elric {
     85   1.1     elric 
     86   1.1     elric 	memset(dksc, 0x0, sizeof(*dksc));
     87  1.60   mlelstv 	dksc->sc_dtype = dtype;
     88  1.60   mlelstv 	dksc->sc_dev = dev;
     89  1.60   mlelstv 
     90  1.62   mlelstv 	strlcpy(dksc->sc_xname, device_xname(dev), DK_XNAME_SIZE);
     91   1.1     elric 	dksc->sc_dkdev.dk_name = dksc->sc_xname;
     92   1.1     elric }
     93   1.1     elric 
     94  1.60   mlelstv void
     95  1.60   mlelstv dk_attach(struct dk_softc *dksc)
     96  1.60   mlelstv {
     97  1.84   mlelstv 	KASSERT(dksc->sc_dev != NULL);
     98  1.84   mlelstv 
     99  1.74   mlelstv 	mutex_init(&dksc->sc_iolock, MUTEX_DEFAULT, IPL_VM);
    100  1.77  christos 	dksc->sc_flags |= DKF_READYFORDUMP;
    101  1.61   mlelstv #ifdef DIAGNOSTIC
    102  1.61   mlelstv 	dksc->sc_flags |= DKF_WARNLABEL | DKF_LABELSANITY;
    103  1.61   mlelstv #endif
    104  1.76   mlelstv 
    105  1.76   mlelstv 	/* Attach the device into the rnd source list. */
    106  1.76   mlelstv 	rnd_attach_source(&dksc->sc_rnd_source, dksc->sc_xname,
    107  1.76   mlelstv 	    RND_TYPE_DISK, RND_FLAG_DEFAULT);
    108  1.60   mlelstv }
    109  1.60   mlelstv 
    110  1.60   mlelstv void
    111  1.60   mlelstv dk_detach(struct dk_softc *dksc)
    112  1.60   mlelstv {
    113  1.76   mlelstv 	/* Unhook the entropy source. */
    114  1.76   mlelstv 	rnd_detach_source(&dksc->sc_rnd_source);
    115  1.76   mlelstv 
    116  1.77  christos 	dksc->sc_flags &= ~DKF_READYFORDUMP;
    117  1.71   mlelstv 	mutex_destroy(&dksc->sc_iolock);
    118  1.60   mlelstv }
    119  1.60   mlelstv 
    120   1.1     elric /* ARGSUSED */
    121   1.1     elric int
    122  1.60   mlelstv dk_open(struct dk_softc *dksc, dev_t dev,
    123  1.27  christos     int flags, int fmt, struct lwp *l)
    124   1.1     elric {
    125  1.92   mlelstv 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    126   1.1     elric 	struct	disklabel *lp = dksc->sc_dkdev.dk_label;
    127   1.1     elric 	int	part = DISKPART(dev);
    128   1.1     elric 	int	pmask = 1 << part;
    129   1.1     elric 	int	ret = 0;
    130  1.16      yamt 	struct disk *dk = &dksc->sc_dkdev;
    131   1.1     elric 
    132  1.77  christos 	DPRINTF_FOLLOW(("%s(%s, %p, 0x%"PRIx64", 0x%x)\n", __func__,
    133  1.60   mlelstv 	    dksc->sc_xname, dksc, dev, flags));
    134   1.1     elric 
    135  1.30        ad 	mutex_enter(&dk->dk_openlock);
    136  1.16      yamt 
    137  1.16      yamt 	/*
    138  1.16      yamt 	 * If there are wedges, and this is not RAW_PART, then we
    139  1.16      yamt 	 * need to fail.
    140  1.16      yamt 	 */
    141  1.16      yamt 	if (dk->dk_nwedges != 0 && part != RAW_PART) {
    142  1.16      yamt 		ret = EBUSY;
    143  1.16      yamt 		goto done;
    144  1.16      yamt 	}
    145  1.16      yamt 
    146   1.1     elric 	/*
    147  1.92   mlelstv 	 * initialize driver for the first opener
    148  1.92   mlelstv 	 */
    149  1.92   mlelstv 	if (dk->dk_openmask == 0 && dkd->d_firstopen != NULL) {
    150  1.92   mlelstv 		ret = (*dkd->d_firstopen)(dksc->sc_dev, dev, flags, fmt);
    151  1.92   mlelstv 		if (ret)
    152  1.92   mlelstv 			goto done;
    153  1.92   mlelstv 	}
    154  1.92   mlelstv 
    155  1.92   mlelstv 	/*
    156   1.1     elric 	 * If we're init'ed and there are no other open partitions then
    157   1.1     elric 	 * update the in-core disklabel.
    158   1.1     elric 	 */
    159  1.16      yamt 	if ((dksc->sc_flags & DKF_INITED)) {
    160  1.60   mlelstv 		if ((dksc->sc_flags & DKF_VLABEL) == 0) {
    161  1.60   mlelstv 			dksc->sc_flags |= DKF_VLABEL;
    162  1.60   mlelstv 			dk_getdisklabel(dksc, dev);
    163  1.16      yamt 		}
    164  1.16      yamt 	}
    165   1.1     elric 
    166   1.1     elric 	/* Fail if we can't find the partition. */
    167  1.60   mlelstv 	if (part != RAW_PART &&
    168  1.60   mlelstv 	    ((dksc->sc_flags & DKF_VLABEL) == 0 ||
    169  1.60   mlelstv 	     part >= lp->d_npartitions ||
    170  1.60   mlelstv 	     lp->d_partitions[part].p_fstype == FS_UNUSED)) {
    171   1.1     elric 		ret = ENXIO;
    172   1.1     elric 		goto done;
    173   1.1     elric 	}
    174   1.1     elric 
    175   1.1     elric 	/* Mark our unit as open. */
    176   1.1     elric 	switch (fmt) {
    177   1.1     elric 	case S_IFCHR:
    178  1.16      yamt 		dk->dk_copenmask |= pmask;
    179   1.1     elric 		break;
    180   1.1     elric 	case S_IFBLK:
    181  1.16      yamt 		dk->dk_bopenmask |= pmask;
    182   1.1     elric 		break;
    183   1.1     elric 	}
    184   1.1     elric 
    185  1.16      yamt 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    186   1.1     elric 
    187   1.1     elric done:
    188  1.30        ad 	mutex_exit(&dk->dk_openlock);
    189   1.1     elric 	return ret;
    190   1.1     elric }
    191   1.1     elric 
    192   1.1     elric /* ARGSUSED */
    193   1.1     elric int
    194  1.60   mlelstv dk_close(struct dk_softc *dksc, dev_t dev,
    195  1.27  christos     int flags, int fmt, struct lwp *l)
    196   1.1     elric {
    197  1.60   mlelstv 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    198   1.1     elric 	int	part = DISKPART(dev);
    199   1.1     elric 	int	pmask = 1 << part;
    200  1.16      yamt 	struct disk *dk = &dksc->sc_dkdev;
    201   1.1     elric 
    202  1.77  christos 	DPRINTF_FOLLOW(("%s(%s, %p, 0x%"PRIx64", 0x%x)\n", __func__,
    203  1.60   mlelstv 	    dksc->sc_xname, dksc, dev, flags));
    204   1.1     elric 
    205  1.30        ad 	mutex_enter(&dk->dk_openlock);
    206   1.1     elric 
    207   1.1     elric 	switch (fmt) {
    208   1.1     elric 	case S_IFCHR:
    209  1.16      yamt 		dk->dk_copenmask &= ~pmask;
    210   1.1     elric 		break;
    211   1.1     elric 	case S_IFBLK:
    212  1.16      yamt 		dk->dk_bopenmask &= ~pmask;
    213   1.1     elric 		break;
    214   1.1     elric 	}
    215  1.16      yamt 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    216   1.1     elric 
    217  1.60   mlelstv 	if (dk->dk_openmask == 0) {
    218  1.60   mlelstv 		if (dkd->d_lastclose != NULL)
    219  1.60   mlelstv 			(*dkd->d_lastclose)(dksc->sc_dev);
    220  1.64   mlelstv 		if ((dksc->sc_flags & DKF_KLABEL) == 0)
    221  1.64   mlelstv 			dksc->sc_flags &= ~DKF_VLABEL;
    222  1.60   mlelstv 	}
    223  1.60   mlelstv 
    224  1.30        ad 	mutex_exit(&dk->dk_openlock);
    225   1.1     elric 	return 0;
    226   1.1     elric }
    227   1.1     elric 
    228  1.71   mlelstv static int
    229  1.71   mlelstv dk_translate(struct dk_softc *dksc, struct buf *bp)
    230   1.1     elric {
    231  1.71   mlelstv 	int	part;
    232   1.1     elric 	int	wlabel;
    233  1.18      yamt 	daddr_t	blkno;
    234  1.55   mlelstv 	struct disklabel *lp;
    235  1.55   mlelstv 	struct disk *dk;
    236  1.55   mlelstv 	uint64_t numsecs;
    237  1.55   mlelstv 	unsigned secsize;
    238   1.1     elric 
    239  1.55   mlelstv 	lp = dksc->sc_dkdev.dk_label;
    240  1.55   mlelstv 	dk = &dksc->sc_dkdev;
    241  1.55   mlelstv 
    242  1.55   mlelstv 	part = DISKPART(bp->b_dev);
    243  1.55   mlelstv 	numsecs = dk->dk_geom.dg_secperunit;
    244  1.55   mlelstv 	secsize = dk->dk_geom.dg_secsize;
    245   1.1     elric 
    246  1.55   mlelstv 	/*
    247  1.55   mlelstv 	 * The transfer must be a whole number of blocks and the offset must
    248  1.55   mlelstv 	 * not be negative.
    249  1.67     skrll 	 */
    250  1.72   mlelstv 	if ((bp->b_bcount % secsize) != 0 || bp->b_blkno < 0) {
    251  1.72   mlelstv 		bp->b_error = EINVAL;
    252  1.72   mlelstv 		goto done;
    253  1.72   mlelstv 	}
    254  1.55   mlelstv 
    255   1.1     elric 	/* If there is nothing to do, then we are done */
    256  1.71   mlelstv 	if (bp->b_bcount == 0)
    257  1.72   mlelstv 		goto done;
    258   1.1     elric 
    259   1.1     elric 	wlabel = dksc->sc_flags & (DKF_WLABEL|DKF_LABELLING);
    260  1.55   mlelstv 	if (part == RAW_PART) {
    261  1.82   mlelstv 		uint64_t numblocks = btodb(numsecs * secsize);
    262  1.82   mlelstv 		if (bounds_check_with_mediasize(bp, DEV_BSIZE, numblocks) <= 0)
    263  1.72   mlelstv 			goto done;
    264  1.55   mlelstv 	} else {
    265  1.71   mlelstv 		if (bounds_check_with_label(&dksc->sc_dkdev, bp, wlabel) <= 0)
    266  1.72   mlelstv 			goto done;
    267   1.1     elric 	}
    268   1.1     elric 
    269  1.67     skrll 	/*
    270  1.66   mlelstv 	 * Convert the block number to absolute and put it in terms
    271  1.66   mlelstv 	 * of the device's logical block size.
    272  1.66   mlelstv 	 */
    273  1.66   mlelstv 	if (secsize >= DEV_BSIZE)
    274  1.66   mlelstv 		blkno = bp->b_blkno / (secsize / DEV_BSIZE);
    275  1.66   mlelstv 	else
    276  1.66   mlelstv 		blkno = bp->b_blkno * (DEV_BSIZE / secsize);
    277  1.66   mlelstv 
    278  1.55   mlelstv 	if (part != RAW_PART)
    279  1.55   mlelstv 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
    280  1.18      yamt 	bp->b_rawblkno = blkno;
    281  1.18      yamt 
    282  1.71   mlelstv 	return -1;
    283  1.72   mlelstv 
    284  1.72   mlelstv done:
    285  1.72   mlelstv 	bp->b_resid = bp->b_bcount;
    286  1.72   mlelstv 	return bp->b_error;
    287  1.71   mlelstv }
    288  1.71   mlelstv 
    289  1.85   mlelstv static int
    290  1.85   mlelstv dk_strategy1(struct dk_softc *dksc, struct buf *bp)
    291  1.71   mlelstv {
    292  1.71   mlelstv 	int error;
    293  1.71   mlelstv 
    294  1.77  christos 	DPRINTF_FOLLOW(("%s(%s, %p, %p)\n", __func__,
    295  1.71   mlelstv 	    dksc->sc_xname, dksc, bp));
    296  1.71   mlelstv 
    297  1.71   mlelstv 	if (!(dksc->sc_flags & DKF_INITED)) {
    298  1.77  christos 		DPRINTF_FOLLOW(("%s: not inited\n", __func__));
    299  1.89   mlelstv 		bp->b_error = ENXIO;
    300  1.89   mlelstv 		bp->b_resid = bp->b_bcount;
    301  1.71   mlelstv 		biodone(bp);
    302  1.85   mlelstv 		return 1;
    303  1.71   mlelstv 	}
    304  1.71   mlelstv 
    305  1.71   mlelstv 	error = dk_translate(dksc, bp);
    306  1.71   mlelstv 	if (error >= 0) {
    307  1.71   mlelstv 		biodone(bp);
    308  1.85   mlelstv 		return 1;
    309  1.85   mlelstv 	}
    310  1.85   mlelstv 
    311  1.85   mlelstv 	return 0;
    312  1.85   mlelstv }
    313  1.85   mlelstv 
    314  1.85   mlelstv void
    315  1.85   mlelstv dk_strategy(struct dk_softc *dksc, struct buf *bp)
    316  1.85   mlelstv {
    317  1.85   mlelstv 	int error;
    318  1.85   mlelstv 
    319  1.85   mlelstv 	error = dk_strategy1(dksc, bp);
    320  1.85   mlelstv 	if (error)
    321  1.71   mlelstv 		return;
    322  1.71   mlelstv 
    323   1.1     elric 	/*
    324  1.71   mlelstv 	 * Queue buffer and start unit
    325   1.1     elric 	 */
    326  1.71   mlelstv 	dk_start(dksc, bp);
    327  1.71   mlelstv }
    328  1.71   mlelstv 
    329  1.85   mlelstv int
    330  1.85   mlelstv dk_strategy_defer(struct dk_softc *dksc, struct buf *bp)
    331  1.85   mlelstv {
    332  1.85   mlelstv 	int error;
    333  1.85   mlelstv 
    334  1.85   mlelstv 	error = dk_strategy1(dksc, bp);
    335  1.85   mlelstv 	if (error)
    336  1.85   mlelstv 		return error;
    337  1.85   mlelstv 
    338  1.85   mlelstv 	/*
    339  1.85   mlelstv 	 * Queue buffer only
    340  1.85   mlelstv 	 */
    341  1.85   mlelstv 	mutex_enter(&dksc->sc_iolock);
    342  1.85   mlelstv 	bufq_put(dksc->sc_bufq, bp);
    343  1.85   mlelstv 	mutex_exit(&dksc->sc_iolock);
    344  1.85   mlelstv 
    345  1.85   mlelstv 	return 0;
    346  1.85   mlelstv }
    347  1.85   mlelstv 
    348  1.85   mlelstv int
    349  1.85   mlelstv dk_strategy_pending(struct dk_softc *dksc)
    350  1.85   mlelstv {
    351  1.85   mlelstv 	struct buf *bp;
    352  1.85   mlelstv 
    353  1.85   mlelstv 	if (!(dksc->sc_flags & DKF_INITED)) {
    354  1.85   mlelstv 		DPRINTF_FOLLOW(("%s: not inited\n", __func__));
    355  1.85   mlelstv 		return 0;
    356  1.85   mlelstv 	}
    357  1.85   mlelstv 
    358  1.85   mlelstv 	mutex_enter(&dksc->sc_iolock);
    359  1.85   mlelstv 	bp = bufq_peek(dksc->sc_bufq);
    360  1.85   mlelstv 	mutex_exit(&dksc->sc_iolock);
    361  1.85   mlelstv 
    362  1.85   mlelstv 	return bp != NULL;
    363  1.85   mlelstv }
    364  1.85   mlelstv 
    365  1.71   mlelstv void
    366  1.71   mlelstv dk_start(struct dk_softc *dksc, struct buf *bp)
    367  1.71   mlelstv {
    368  1.71   mlelstv 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    369  1.71   mlelstv 	int error;
    370  1.71   mlelstv 
    371  1.84   mlelstv 	if (!(dksc->sc_flags & DKF_INITED)) {
    372  1.84   mlelstv 		DPRINTF_FOLLOW(("%s: not inited\n", __func__));
    373  1.84   mlelstv 		return;
    374  1.84   mlelstv 	}
    375  1.84   mlelstv 
    376  1.71   mlelstv 	mutex_enter(&dksc->sc_iolock);
    377  1.71   mlelstv 
    378  1.71   mlelstv 	if (bp != NULL)
    379  1.71   mlelstv 		bufq_put(dksc->sc_bufq, bp);
    380  1.71   mlelstv 
    381  1.75   mlelstv 	if (dksc->sc_busy)
    382  1.75   mlelstv 		goto done;
    383  1.75   mlelstv 	dksc->sc_busy = true;
    384  1.75   mlelstv 
    385  1.74   mlelstv 	/*
    386  1.74   mlelstv 	 * Peeking at the buffer queue and committing the operation
    387  1.74   mlelstv 	 * only after success isn't atomic.
    388  1.74   mlelstv 	 *
    389  1.74   mlelstv 	 * So when a diskstart fails, the buffer is saved
    390  1.74   mlelstv 	 * and tried again before the next buffer is fetched.
    391  1.91  jdolecek 	 * dk_drain() handles flushing of a saved buffer.
    392  1.74   mlelstv 	 *
    393  1.74   mlelstv 	 * This keeps order of I/O operations, unlike bufq_put.
    394  1.74   mlelstv 	 */
    395  1.74   mlelstv 
    396  1.91  jdolecek 	bp = dksc->sc_deferred;
    397  1.91  jdolecek 	dksc->sc_deferred = NULL;
    398  1.91  jdolecek 
    399  1.91  jdolecek 	if (bp == NULL)
    400  1.91  jdolecek 		bp = bufq_get(dksc->sc_bufq);
    401  1.91  jdolecek 
    402  1.91  jdolecek 	while (bp != NULL) {
    403  1.71   mlelstv 
    404  1.71   mlelstv 		disk_busy(&dksc->sc_dkdev);
    405  1.74   mlelstv 		mutex_exit(&dksc->sc_iolock);
    406  1.71   mlelstv 		error = dkd->d_diskstart(dksc->sc_dev, bp);
    407  1.74   mlelstv 		mutex_enter(&dksc->sc_iolock);
    408  1.71   mlelstv 		if (error == EAGAIN) {
    409  1.91  jdolecek 			dksc->sc_deferred = bp;
    410  1.71   mlelstv 			disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
    411  1.71   mlelstv 			break;
    412  1.71   mlelstv 		}
    413  1.71   mlelstv 
    414  1.71   mlelstv 		if (error != 0) {
    415  1.71   mlelstv 			bp->b_error = error;
    416  1.71   mlelstv 			bp->b_resid = bp->b_bcount;
    417  1.74   mlelstv 			dk_done1(dksc, bp, false);
    418  1.71   mlelstv 		}
    419  1.91  jdolecek 
    420  1.91  jdolecek 		bp = bufq_get(dksc->sc_bufq);
    421  1.71   mlelstv 	}
    422  1.71   mlelstv 
    423  1.75   mlelstv 	dksc->sc_busy = false;
    424  1.75   mlelstv done:
    425  1.71   mlelstv 	mutex_exit(&dksc->sc_iolock);
    426   1.1     elric }
    427   1.1     elric 
    428  1.74   mlelstv static void
    429  1.74   mlelstv dk_done1(struct dk_softc *dksc, struct buf *bp, bool lock)
    430  1.60   mlelstv {
    431  1.60   mlelstv 	struct disk *dk = &dksc->sc_dkdev;
    432  1.60   mlelstv 
    433  1.60   mlelstv 	if (bp->b_error != 0) {
    434  1.68   mlelstv 		struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
    435  1.68   mlelstv 
    436  1.68   mlelstv 		diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
    437  1.60   mlelstv 			dk->dk_label);
    438  1.60   mlelstv 		printf("\n");
    439  1.60   mlelstv 	}
    440  1.60   mlelstv 
    441  1.74   mlelstv 	if (lock)
    442  1.74   mlelstv 		mutex_enter(&dksc->sc_iolock);
    443  1.60   mlelstv 	disk_unbusy(dk, bp->b_bcount - bp->b_resid, (bp->b_flags & B_READ));
    444  1.74   mlelstv 	if (lock)
    445  1.74   mlelstv 		mutex_exit(&dksc->sc_iolock);
    446  1.71   mlelstv 
    447  1.76   mlelstv 	rnd_add_uint32(&dksc->sc_rnd_source, bp->b_rawblkno);
    448  1.71   mlelstv 
    449  1.60   mlelstv 	biodone(bp);
    450  1.60   mlelstv }
    451  1.60   mlelstv 
    452  1.74   mlelstv void
    453  1.74   mlelstv dk_done(struct dk_softc *dksc, struct buf *bp)
    454  1.74   mlelstv {
    455  1.74   mlelstv 	dk_done1(dksc, bp, true);
    456  1.74   mlelstv }
    457  1.74   mlelstv 
    458  1.74   mlelstv void
    459  1.74   mlelstv dk_drain(struct dk_softc *dksc)
    460  1.74   mlelstv {
    461  1.74   mlelstv 	struct buf *bp;
    462  1.74   mlelstv 
    463  1.74   mlelstv 	mutex_enter(&dksc->sc_iolock);
    464  1.91  jdolecek 	bp = dksc->sc_deferred;
    465  1.91  jdolecek 	dksc->sc_deferred = NULL;
    466  1.91  jdolecek 	if (bp != NULL) {
    467  1.74   mlelstv 		bp->b_error = EIO;
    468  1.74   mlelstv 		bp->b_resid = bp->b_bcount;
    469  1.74   mlelstv 		biodone(bp);
    470  1.74   mlelstv 	}
    471  1.74   mlelstv 	bufq_drain(dksc->sc_bufq);
    472  1.74   mlelstv 	mutex_exit(&dksc->sc_iolock);
    473  1.74   mlelstv }
    474  1.74   mlelstv 
    475   1.1     elric int
    476  1.71   mlelstv dk_discard(struct dk_softc *dksc, dev_t dev, off_t pos, off_t len)
    477  1.71   mlelstv {
    478  1.71   mlelstv 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    479  1.71   mlelstv 	unsigned secsize = dksc->sc_dkdev.dk_geom.dg_secsize;
    480  1.71   mlelstv 	struct buf tmp, *bp = &tmp;
    481  1.71   mlelstv 	int error;
    482  1.71   mlelstv 
    483  1.77  christos 	DPRINTF_FOLLOW(("%s(%s, %p, 0x"PRIx64", %jd, %jd)\n", __func__,
    484  1.71   mlelstv 	    dksc->sc_xname, dksc, (intmax_t)pos, (intmax_t)len));
    485  1.71   mlelstv 
    486  1.71   mlelstv 	if (!(dksc->sc_flags & DKF_INITED)) {
    487  1.77  christos 		DPRINTF_FOLLOW(("%s: not inited\n", __func__));
    488  1.71   mlelstv 		return ENXIO;
    489  1.71   mlelstv 	}
    490  1.71   mlelstv 
    491  1.71   mlelstv 	if (secsize == 0 || (pos % secsize) != 0)
    492  1.71   mlelstv 		return EINVAL;
    493  1.71   mlelstv 
    494  1.71   mlelstv 	/* enough data to please the bounds checking code */
    495  1.71   mlelstv 	bp->b_dev = dev;
    496  1.71   mlelstv 	bp->b_blkno = (daddr_t)(pos / secsize);
    497  1.71   mlelstv 	bp->b_bcount = len;
    498  1.71   mlelstv 	bp->b_flags = B_WRITE;
    499  1.71   mlelstv 
    500  1.71   mlelstv 	error = dk_translate(dksc, bp);
    501  1.71   mlelstv 	if (error >= 0)
    502  1.71   mlelstv 		return error;
    503  1.71   mlelstv 
    504  1.71   mlelstv 	error = dkd->d_discard(dksc->sc_dev,
    505  1.71   mlelstv 		(off_t)bp->b_rawblkno * secsize,
    506  1.71   mlelstv 		(off_t)bp->b_bcount);
    507  1.71   mlelstv 
    508  1.71   mlelstv 	return error;
    509  1.71   mlelstv }
    510  1.71   mlelstv 
    511  1.71   mlelstv int
    512  1.60   mlelstv dk_size(struct dk_softc *dksc, dev_t dev)
    513   1.1     elric {
    514  1.60   mlelstv 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    515   1.1     elric 	struct	disklabel *lp;
    516   1.1     elric 	int	is_open;
    517   1.1     elric 	int	part;
    518   1.1     elric 	int	size;
    519   1.1     elric 
    520   1.1     elric 	if ((dksc->sc_flags & DKF_INITED) == 0)
    521   1.1     elric 		return -1;
    522   1.1     elric 
    523   1.1     elric 	part = DISKPART(dev);
    524   1.1     elric 	is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
    525   1.1     elric 
    526  1.60   mlelstv 	if (!is_open && dkd->d_open(dev, 0, S_IFBLK, curlwp))
    527   1.1     elric 		return -1;
    528   1.1     elric 
    529   1.1     elric 	lp = dksc->sc_dkdev.dk_label;
    530   1.1     elric 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
    531   1.1     elric 		size = -1;
    532   1.1     elric 	else
    533   1.1     elric 		size = lp->d_partitions[part].p_size *
    534   1.1     elric 		    (lp->d_secsize / DEV_BSIZE);
    535   1.1     elric 
    536  1.60   mlelstv 	if (!is_open && dkd->d_close(dev, 0, S_IFBLK, curlwp))
    537  1.59   mlelstv 		return -1;
    538   1.1     elric 
    539   1.1     elric 	return size;
    540   1.1     elric }
    541   1.1     elric 
    542   1.1     elric int
    543  1.60   mlelstv dk_ioctl(struct dk_softc *dksc, dev_t dev,
    544  1.28  christos 	    u_long cmd, void *data, int flag, struct lwp *l)
    545   1.1     elric {
    546  1.60   mlelstv 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    547   1.1     elric 	struct	disklabel *lp;
    548  1.57  christos 	struct	disk *dk = &dksc->sc_dkdev;
    549   1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    550   1.1     elric 	struct	disklabel newlabel;
    551   1.1     elric #endif
    552  1.57  christos 	int	error;
    553   1.1     elric 
    554  1.77  christos 	DPRINTF_FOLLOW(("%s(%s, %p, 0x%"PRIx64", 0x%lx)\n", __func__,
    555  1.60   mlelstv 	    dksc->sc_xname, dksc, dev, cmd));
    556   1.1     elric 
    557   1.1     elric 	/* ensure that the pseudo disk is open for writes for these commands */
    558   1.1     elric 	switch (cmd) {
    559   1.1     elric 	case DIOCSDINFO:
    560   1.1     elric 	case DIOCWDINFO:
    561   1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    562   1.1     elric 	case ODIOCSDINFO:
    563   1.1     elric 	case ODIOCWDINFO:
    564   1.1     elric #endif
    565  1.69   mlelstv 	case DIOCKLABEL:
    566   1.1     elric 	case DIOCWLABEL:
    567  1.43     elric 	case DIOCAWEDGE:
    568  1.70   mlelstv 	case DIOCDWEDGE:
    569  1.69   mlelstv 	case DIOCSSTRATEGY:
    570   1.1     elric 		if ((flag & FWRITE) == 0)
    571   1.1     elric 			return EBADF;
    572   1.1     elric 	}
    573   1.1     elric 
    574   1.1     elric 	/* ensure that the pseudo-disk is initialized for these */
    575   1.1     elric 	switch (cmd) {
    576   1.1     elric 	case DIOCGDINFO:
    577   1.1     elric 	case DIOCSDINFO:
    578   1.1     elric 	case DIOCWDINFO:
    579  1.83  christos 	case DIOCGPARTINFO:
    580  1.60   mlelstv 	case DIOCKLABEL:
    581   1.1     elric 	case DIOCWLABEL:
    582   1.1     elric 	case DIOCGDEFLABEL:
    583  1.43     elric 	case DIOCAWEDGE:
    584  1.43     elric 	case DIOCDWEDGE:
    585  1.43     elric 	case DIOCLWEDGES:
    586  1.54   mlelstv 	case DIOCMWEDGES:
    587  1.43     elric 	case DIOCCACHESYNC:
    588   1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    589   1.1     elric 	case ODIOCGDINFO:
    590   1.1     elric 	case ODIOCSDINFO:
    591   1.1     elric 	case ODIOCWDINFO:
    592   1.1     elric 	case ODIOCGDEFLABEL:
    593   1.1     elric #endif
    594   1.1     elric 		if ((dksc->sc_flags & DKF_INITED) == 0)
    595   1.1     elric 			return ENXIO;
    596   1.1     elric 	}
    597   1.1     elric 
    598  1.58  christos 	error = disk_ioctl(dk, dev, cmd, data, flag, l);
    599  1.57  christos 	if (error != EPASSTHROUGH)
    600  1.57  christos 		return error;
    601  1.57  christos 	else
    602  1.57  christos 		error = 0;
    603  1.57  christos 
    604   1.1     elric 	switch (cmd) {
    605   1.1     elric 	case DIOCWDINFO:
    606   1.1     elric 	case DIOCSDINFO:
    607   1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    608   1.1     elric 	case ODIOCWDINFO:
    609   1.1     elric 	case ODIOCSDINFO:
    610   1.1     elric #endif
    611   1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    612   1.1     elric 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    613   1.1     elric 			memset(&newlabel, 0, sizeof newlabel);
    614   1.1     elric 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    615   1.1     elric 			lp = &newlabel;
    616   1.1     elric 		} else
    617   1.1     elric #endif
    618   1.1     elric 		lp = (struct disklabel *)data;
    619   1.1     elric 
    620  1.30        ad 		mutex_enter(&dk->dk_openlock);
    621   1.1     elric 		dksc->sc_flags |= DKF_LABELLING;
    622   1.1     elric 
    623   1.1     elric 		error = setdisklabel(dksc->sc_dkdev.dk_label,
    624   1.1     elric 		    lp, 0, dksc->sc_dkdev.dk_cpulabel);
    625   1.1     elric 		if (error == 0) {
    626   1.1     elric 			if (cmd == DIOCWDINFO
    627   1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    628   1.1     elric 			    || cmd == ODIOCWDINFO
    629   1.1     elric #endif
    630   1.1     elric 			   )
    631   1.1     elric 				error = writedisklabel(DKLABELDEV(dev),
    632  1.60   mlelstv 				    dkd->d_strategy, dksc->sc_dkdev.dk_label,
    633   1.1     elric 				    dksc->sc_dkdev.dk_cpulabel);
    634   1.1     elric 		}
    635   1.1     elric 
    636   1.1     elric 		dksc->sc_flags &= ~DKF_LABELLING;
    637  1.30        ad 		mutex_exit(&dk->dk_openlock);
    638   1.1     elric 		break;
    639   1.1     elric 
    640  1.60   mlelstv 	case DIOCKLABEL:
    641  1.60   mlelstv 		if (*(int *)data != 0)
    642  1.60   mlelstv 			dksc->sc_flags |= DKF_KLABEL;
    643  1.60   mlelstv 		else
    644  1.60   mlelstv 			dksc->sc_flags &= ~DKF_KLABEL;
    645  1.60   mlelstv 		break;
    646  1.60   mlelstv 
    647   1.1     elric 	case DIOCWLABEL:
    648   1.1     elric 		if (*(int *)data != 0)
    649   1.1     elric 			dksc->sc_flags |= DKF_WLABEL;
    650   1.1     elric 		else
    651   1.1     elric 			dksc->sc_flags &= ~DKF_WLABEL;
    652   1.1     elric 		break;
    653   1.1     elric 
    654   1.1     elric 	case DIOCGDEFLABEL:
    655  1.60   mlelstv 		dk_getdefaultlabel(dksc, (struct disklabel *)data);
    656   1.1     elric 		break;
    657   1.1     elric 
    658   1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    659   1.1     elric 	case ODIOCGDEFLABEL:
    660  1.60   mlelstv 		dk_getdefaultlabel(dksc, &newlabel);
    661   1.1     elric 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    662   1.1     elric 			return ENOTTY;
    663   1.1     elric 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    664   1.1     elric 		break;
    665   1.1     elric #endif
    666   1.1     elric 
    667  1.21      yamt 	case DIOCGSTRATEGY:
    668  1.21      yamt 	    {
    669  1.21      yamt 		struct disk_strategy *dks = (void *)data;
    670  1.21      yamt 
    671  1.71   mlelstv 		mutex_enter(&dksc->sc_iolock);
    672  1.87   mlelstv 		if (dksc->sc_bufq != NULL)
    673  1.88  christos 			strlcpy(dks->dks_name,
    674  1.88  christos 			    bufq_getstrategyname(dksc->sc_bufq),
    675  1.87   mlelstv 			    sizeof(dks->dks_name));
    676  1.87   mlelstv 		else
    677  1.87   mlelstv 			error = EINVAL;
    678  1.71   mlelstv 		mutex_exit(&dksc->sc_iolock);
    679  1.21      yamt 		dks->dks_paramlen = 0;
    680  1.88  christos 		break;
    681  1.21      yamt 	    }
    682  1.67     skrll 
    683  1.21      yamt 	case DIOCSSTRATEGY:
    684  1.21      yamt 	    {
    685  1.21      yamt 		struct disk_strategy *dks = (void *)data;
    686  1.21      yamt 		struct bufq_state *new;
    687  1.21      yamt 		struct bufq_state *old;
    688  1.21      yamt 
    689  1.21      yamt 		if (dks->dks_param != NULL) {
    690  1.21      yamt 			return EINVAL;
    691  1.21      yamt 		}
    692  1.21      yamt 		dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
    693  1.21      yamt 		error = bufq_alloc(&new, dks->dks_name,
    694  1.21      yamt 		    BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
    695  1.21      yamt 		if (error) {
    696  1.21      yamt 			return error;
    697  1.21      yamt 		}
    698  1.71   mlelstv 		mutex_enter(&dksc->sc_iolock);
    699  1.21      yamt 		old = dksc->sc_bufq;
    700  1.88  christos 		if (old)
    701  1.88  christos 			bufq_move(new, old);
    702  1.21      yamt 		dksc->sc_bufq = new;
    703  1.71   mlelstv 		mutex_exit(&dksc->sc_iolock);
    704  1.88  christos 		if (old)
    705  1.88  christos 			bufq_free(old);
    706  1.88  christos 		break;
    707  1.21      yamt 	    }
    708  1.21      yamt 
    709   1.1     elric 	default:
    710   1.1     elric 		error = ENOTTY;
    711   1.1     elric 	}
    712   1.1     elric 
    713   1.1     elric 	return error;
    714   1.1     elric }
    715   1.1     elric 
    716   1.1     elric /*
    717   1.1     elric  * dk_dump dumps all of physical memory into the partition specified.
    718   1.1     elric  * This requires substantially more framework than {s,w}ddump, and hence
    719   1.1     elric  * is probably much more fragile.
    720   1.1     elric  *
    721   1.1     elric  */
    722   1.1     elric 
    723   1.1     elric #define DKFF_READYFORDUMP(x)	(((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
    724   1.1     elric static volatile int	dk_dumping = 0;
    725   1.1     elric 
    726   1.1     elric /* ARGSUSED */
    727   1.1     elric int
    728  1.60   mlelstv dk_dump(struct dk_softc *dksc, dev_t dev,
    729  1.60   mlelstv     daddr_t blkno, void *vav, size_t size)
    730   1.1     elric {
    731  1.60   mlelstv 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    732  1.60   mlelstv 	char *va = vav;
    733  1.60   mlelstv 	struct disklabel *lp;
    734  1.77  christos 	struct partition *p;
    735  1.60   mlelstv 	int part, towrt, nsects, sectoff, maxblkcnt, nblk;
    736  1.60   mlelstv 	int maxxfer, rv = 0;
    737   1.1     elric 
    738   1.1     elric 	/*
    739   1.1     elric 	 * ensure that we consider this device to be safe for dumping,
    740   1.1     elric 	 * and that the device is configured.
    741   1.1     elric 	 */
    742  1.77  christos 	if (!DKFF_READYFORDUMP(dksc->sc_flags)) {
    743  1.78  christos 		DPRINTF(DKDB_DUMP, ("%s: bad dump flags 0x%x\n", __func__,
    744  1.77  christos 		    dksc->sc_flags));
    745   1.1     elric 		return ENXIO;
    746  1.77  christos 	}
    747   1.1     elric 
    748   1.1     elric 	/* ensure that we are not already dumping */
    749   1.1     elric 	if (dk_dumping)
    750   1.1     elric 		return EFAULT;
    751   1.1     elric 	dk_dumping = 1;
    752   1.1     elric 
    753  1.77  christos 	if (dkd->d_dumpblocks == NULL) {
    754  1.78  christos 		DPRINTF(DKDB_DUMP, ("%s: no dumpblocks\n", __func__));
    755  1.60   mlelstv 		return ENXIO;
    756  1.77  christos 	}
    757  1.60   mlelstv 
    758  1.60   mlelstv 	/* device specific max transfer size */
    759  1.60   mlelstv 	maxxfer = MAXPHYS;
    760  1.60   mlelstv 	if (dkd->d_iosize != NULL)
    761  1.60   mlelstv 		(*dkd->d_iosize)(dksc->sc_dev, &maxxfer);
    762  1.60   mlelstv 
    763  1.60   mlelstv 	/* Convert to disk sectors.  Request must be a multiple of size. */
    764  1.60   mlelstv 	part = DISKPART(dev);
    765  1.60   mlelstv 	lp = dksc->sc_dkdev.dk_label;
    766  1.77  christos 	if ((size % lp->d_secsize) != 0) {
    767  1.78  christos 		DPRINTF(DKDB_DUMP, ("%s: odd size %zu\n", __func__, size));
    768  1.77  christos 		return EFAULT;
    769  1.77  christos 	}
    770  1.60   mlelstv 	towrt = size / lp->d_secsize;
    771  1.60   mlelstv 	blkno = dbtob(blkno) / lp->d_secsize;   /* blkno in secsize units */
    772  1.60   mlelstv 
    773  1.77  christos 	p = &lp->d_partitions[part];
    774  1.77  christos 	if (p->p_fstype != FS_SWAP) {
    775  1.78  christos 		DPRINTF(DKDB_DUMP, ("%s: bad fstype %d\n", __func__,
    776  1.77  christos 		    p->p_fstype));
    777  1.77  christos 		return ENXIO;
    778  1.77  christos 	}
    779  1.77  christos 	nsects = p->p_size;
    780  1.77  christos 	sectoff = p->p_offset;
    781  1.60   mlelstv 
    782  1.60   mlelstv 	/* Check transfer bounds against partition size. */
    783  1.77  christos 	if ((blkno < 0) || ((blkno + towrt) > nsects)) {
    784  1.81  christos 		DPRINTF(DKDB_DUMP, ("%s: out of bounds blkno=%jd, towrt=%d, "
    785  1.80  christos 		    "nsects=%d\n", __func__, (intmax_t)blkno, towrt, nsects));
    786  1.77  christos 		return EINVAL;
    787  1.77  christos 	}
    788  1.60   mlelstv 
    789  1.60   mlelstv 	/* Offset block number to start of partition. */
    790  1.60   mlelstv 	blkno += sectoff;
    791  1.60   mlelstv 
    792  1.60   mlelstv 	/* Start dumping and return when done. */
    793  1.60   mlelstv 	maxblkcnt = howmany(maxxfer, lp->d_secsize);
    794  1.60   mlelstv 	while (towrt > 0) {
    795  1.60   mlelstv 		nblk = min(maxblkcnt, towrt);
    796  1.60   mlelstv 
    797  1.77  christos 		if ((rv = (*dkd->d_dumpblocks)(dksc->sc_dev, va, blkno, nblk))
    798  1.77  christos 		    != 0) {
    799  1.78  christos 			DPRINTF(DKDB_DUMP, ("%s: dumpblocks %d\n", __func__,
    800  1.77  christos 			    rv));
    801  1.77  christos 			return rv;
    802  1.77  christos 		}
    803  1.60   mlelstv 
    804  1.60   mlelstv 		towrt -= nblk;
    805  1.60   mlelstv 		blkno += nblk;
    806  1.60   mlelstv 		va += nblk * lp->d_secsize;
    807  1.60   mlelstv 	}
    808   1.1     elric 
    809   1.1     elric 	dk_dumping = 0;
    810   1.1     elric 
    811  1.60   mlelstv 	return 0;
    812   1.1     elric }
    813   1.1     elric 
    814   1.1     elric /* ARGSUSED */
    815   1.1     elric void
    816  1.63  christos dk_getdefaultlabel(struct dk_softc *dksc, struct disklabel *lp)
    817   1.1     elric {
    818  1.47  christos 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    819   1.4     elric 
    820   1.4     elric 	memset(lp, 0, sizeof(*lp));
    821   1.1     elric 
    822  1.52   mlelstv 	if (dg->dg_secperunit > UINT32_MAX)
    823  1.52   mlelstv 		lp->d_secperunit = UINT32_MAX;
    824  1.52   mlelstv 	else
    825  1.52   mlelstv 		lp->d_secperunit = dg->dg_secperunit;
    826  1.47  christos 	lp->d_secsize = dg->dg_secsize;
    827  1.47  christos 	lp->d_nsectors = dg->dg_nsectors;
    828  1.47  christos 	lp->d_ntracks = dg->dg_ntracks;
    829  1.47  christos 	lp->d_ncylinders = dg->dg_ncylinders;
    830   1.1     elric 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    831   1.1     elric 
    832  1.63  christos 	strlcpy(lp->d_typename, dksc->sc_xname, sizeof(lp->d_typename));
    833  1.60   mlelstv 	lp->d_type = dksc->sc_dtype;
    834  1.63  christos 	strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    835   1.1     elric 	lp->d_rpm = 3600;
    836   1.1     elric 	lp->d_interleave = 1;
    837   1.1     elric 	lp->d_flags = 0;
    838   1.1     elric 
    839   1.1     elric 	lp->d_partitions[RAW_PART].p_offset = 0;
    840  1.52   mlelstv 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
    841   1.1     elric 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    842   1.1     elric 	lp->d_npartitions = RAW_PART + 1;
    843   1.1     elric 
    844   1.1     elric 	lp->d_magic = DISKMAGIC;
    845   1.1     elric 	lp->d_magic2 = DISKMAGIC;
    846   1.1     elric 	lp->d_checksum = dkcksum(dksc->sc_dkdev.dk_label);
    847   1.1     elric }
    848   1.1     elric 
    849   1.1     elric /* ARGSUSED */
    850   1.1     elric void
    851  1.60   mlelstv dk_getdisklabel(struct dk_softc *dksc, dev_t dev)
    852   1.1     elric {
    853  1.60   mlelstv 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    854   1.1     elric 	struct	 disklabel *lp = dksc->sc_dkdev.dk_label;
    855   1.1     elric 	struct	 cpu_disklabel *clp = dksc->sc_dkdev.dk_cpulabel;
    856  1.60   mlelstv 	struct   disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    857   1.1     elric 	struct	 partition *pp;
    858   1.1     elric 	int	 i;
    859   1.5       dsl 	const char	*errstring;
    860   1.1     elric 
    861   1.1     elric 	memset(clp, 0x0, sizeof(*clp));
    862  1.60   mlelstv 	dk_getdefaultlabel(dksc, lp);
    863  1.60   mlelstv 	errstring = readdisklabel(DKLABELDEV(dev), dkd->d_strategy,
    864   1.1     elric 	    dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
    865   1.1     elric 	if (errstring) {
    866  1.60   mlelstv 		dk_makedisklabel(dksc);
    867   1.1     elric 		if (dksc->sc_flags & DKF_WARNLABEL)
    868   1.1     elric 			printf("%s: %s\n", dksc->sc_xname, errstring);
    869   1.1     elric 		return;
    870   1.1     elric 	}
    871   1.1     elric 
    872   1.1     elric 	if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
    873   1.1     elric 		return;
    874   1.1     elric 
    875   1.1     elric 	/* Sanity check */
    876  1.53   mlelstv 	if (lp->d_secperunit < UINT32_MAX ?
    877  1.53   mlelstv 		lp->d_secperunit != dg->dg_secperunit :
    878  1.53   mlelstv 		lp->d_secperunit > dg->dg_secperunit)
    879  1.53   mlelstv 		printf("WARNING: %s: total sector size in disklabel (%ju) "
    880  1.53   mlelstv 		    "!= the size of %s (%ju)\n", dksc->sc_xname,
    881  1.60   mlelstv 		    (uintmax_t)lp->d_secperunit, dksc->sc_xname,
    882  1.53   mlelstv 		    (uintmax_t)dg->dg_secperunit);
    883   1.1     elric 
    884   1.1     elric 	for (i=0; i < lp->d_npartitions; i++) {
    885   1.1     elric 		pp = &lp->d_partitions[i];
    886  1.48  christos 		if (pp->p_offset + pp->p_size > dg->dg_secperunit)
    887   1.1     elric 			printf("WARNING: %s: end of partition `%c' exceeds "
    888  1.53   mlelstv 			    "the size of %s (%ju)\n", dksc->sc_xname,
    889  1.60   mlelstv 			    'a' + i, dksc->sc_xname,
    890  1.53   mlelstv 			    (uintmax_t)dg->dg_secperunit);
    891   1.1     elric 	}
    892   1.1     elric }
    893   1.1     elric 
    894   1.1     elric /* ARGSUSED */
    895  1.13   thorpej static void
    896  1.60   mlelstv dk_makedisklabel(struct dk_softc *dksc)
    897   1.1     elric {
    898   1.1     elric 	struct	disklabel *lp = dksc->sc_dkdev.dk_label;
    899   1.1     elric 
    900   1.1     elric 	lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
    901  1.63  christos 	strlcpy(lp->d_packname, "default label", sizeof(lp->d_packname));
    902   1.1     elric 	lp->d_checksum = dkcksum(lp);
    903   1.1     elric }
    904   1.1     elric 
    905   1.1     elric /* This function is taken from ccd.c:1.76  --rcd */
    906   1.1     elric 
    907   1.1     elric /*
    908   1.1     elric  * XXX this function looks too generic for dksubr.c, shouldn't we
    909   1.1     elric  *     put it somewhere better?
    910   1.1     elric  */
    911   1.1     elric 
    912   1.1     elric /*
    913   1.1     elric  * Lookup the provided name in the filesystem.  If the file exists,
    914   1.1     elric  * is a valid block device, and isn't being used by anyone else,
    915   1.1     elric  * set *vpp to the file's vnode.
    916   1.1     elric  */
    917   1.1     elric int
    918  1.42  dholland dk_lookup(struct pathbuf *pb, struct lwp *l, struct vnode **vpp)
    919   1.1     elric {
    920   1.1     elric 	struct nameidata nd;
    921   1.1     elric 	struct vnode *vp;
    922  1.24  christos 	int     error;
    923   1.1     elric 
    924  1.24  christos 	if (l == NULL)
    925  1.24  christos 		return ESRCH;	/* Is ESRCH the best choice? */
    926  1.24  christos 
    927  1.42  dholland 	NDINIT(&nd, LOOKUP, FOLLOW, pb);
    928  1.24  christos 	if ((error = vn_open(&nd, FREAD | FWRITE, 0)) != 0) {
    929   1.1     elric 		DPRINTF((DKDB_FOLLOW|DKDB_INIT),
    930  1.77  christos 		    ("%s: vn_open error = %d\n", __func__, error));
    931  1.24  christos 		return error;
    932   1.1     elric 	}
    933  1.24  christos 
    934   1.1     elric 	vp = nd.ni_vp;
    935  1.51   hannken 	if (vp->v_type != VBLK) {
    936  1.51   hannken 		error = ENOTBLK;
    937  1.24  christos 		goto out;
    938   1.1     elric 	}
    939   1.1     elric 
    940  1.51   hannken 	/* Reopen as anonymous vnode to protect against forced unmount. */
    941  1.51   hannken 	if ((error = bdevvp(vp->v_rdev, vpp)) != 0)
    942  1.24  christos 		goto out;
    943  1.51   hannken 	VOP_UNLOCK(vp);
    944  1.51   hannken 	if ((error = vn_close(vp, FREAD | FWRITE, l->l_cred)) != 0) {
    945  1.51   hannken 		vrele(*vpp);
    946  1.51   hannken 		return error;
    947  1.51   hannken 	}
    948  1.51   hannken 	if ((error = VOP_OPEN(*vpp, FREAD | FWRITE, l->l_cred)) != 0) {
    949  1.51   hannken 		vrele(*vpp);
    950  1.51   hannken 		return error;
    951  1.24  christos 	}
    952  1.51   hannken 	mutex_enter((*vpp)->v_interlock);
    953  1.51   hannken 	(*vpp)->v_writecount++;
    954  1.51   hannken 	mutex_exit((*vpp)->v_interlock);
    955  1.24  christos 
    956  1.51   hannken 	IFDEBUG(DKDB_VNODE, vprint("dk_lookup: vnode info", *vpp));
    957   1.1     elric 
    958  1.24  christos 	return 0;
    959  1.24  christos out:
    960  1.41   hannken 	VOP_UNLOCK(vp);
    961  1.35        ad 	(void) vn_close(vp, FREAD | FWRITE, l->l_cred);
    962  1.24  christos 	return error;
    963   1.1     elric }
    964  1.49  pgoyette 
    965  1.49  pgoyette MODULE(MODULE_CLASS_MISC, dk_subr, NULL);
    966  1.49  pgoyette 
    967  1.49  pgoyette static int
    968  1.49  pgoyette dk_subr_modcmd(modcmd_t cmd, void *arg)
    969  1.49  pgoyette {
    970  1.49  pgoyette 	switch (cmd) {
    971  1.49  pgoyette 	case MODULE_CMD_INIT:
    972  1.49  pgoyette 	case MODULE_CMD_FINI:
    973  1.49  pgoyette 		return 0;
    974  1.49  pgoyette 	case MODULE_CMD_STAT:
    975  1.49  pgoyette 	case MODULE_CMD_AUTOUNLOAD:
    976  1.49  pgoyette 	default:
    977  1.49  pgoyette 		return ENOTTY;
    978  1.49  pgoyette 	}
    979  1.49  pgoyette }
    980