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