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