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