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