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