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