Home | History | Annotate | Line # | Download | only in dev
dksubr.c revision 1.54.2.2
      1  1.54.2.2     skrll /* $NetBSD: dksubr.c,v 1.54.2.2 2015/06/06 14:40:06 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.2     skrll __KERNEL_RCSID(0, "$NetBSD: dksubr.c,v 1.54.2.2 2015/06/06 14:40:06 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.1     elric 
     77       1.1     elric void
     78  1.54.2.2     skrll dk_init(struct dk_softc *dksc, device_t dev, int dtype)
     79       1.1     elric {
     80       1.1     elric 
     81       1.1     elric 	memset(dksc, 0x0, sizeof(*dksc));
     82  1.54.2.2     skrll 	dksc->sc_dtype = dtype;
     83  1.54.2.2     skrll 	dksc->sc_dev = dev;
     84  1.54.2.2     skrll 
     85  1.54.2.2     skrll 	strlcpy(dksc->sc_xname, device_xname(dev), DK_XNAME_SIZE);
     86       1.1     elric 	dksc->sc_dkdev.dk_name = dksc->sc_xname;
     87       1.1     elric }
     88       1.1     elric 
     89  1.54.2.2     skrll void
     90  1.54.2.2     skrll dk_attach(struct dk_softc *dksc)
     91  1.54.2.2     skrll {
     92  1.54.2.2     skrll 	dksc->sc_flags |= DKF_INITED;
     93  1.54.2.2     skrll #ifdef DIAGNOSTIC
     94  1.54.2.2     skrll 	dksc->sc_flags |= DKF_WARNLABEL | DKF_LABELSANITY;
     95  1.54.2.2     skrll #endif
     96  1.54.2.2     skrll }
     97  1.54.2.2     skrll 
     98  1.54.2.2     skrll void
     99  1.54.2.2     skrll dk_detach(struct dk_softc *dksc)
    100  1.54.2.2     skrll {
    101  1.54.2.2     skrll 	dksc->sc_flags &= ~DKF_INITED;
    102  1.54.2.2     skrll }
    103  1.54.2.2     skrll 
    104       1.1     elric /* ARGSUSED */
    105       1.1     elric int
    106  1.54.2.2     skrll dk_open(struct dk_softc *dksc, dev_t dev,
    107      1.27  christos     int flags, int fmt, struct lwp *l)
    108       1.1     elric {
    109       1.1     elric 	struct	disklabel *lp = dksc->sc_dkdev.dk_label;
    110       1.1     elric 	int	part = DISKPART(dev);
    111       1.1     elric 	int	pmask = 1 << part;
    112       1.1     elric 	int	ret = 0;
    113      1.16      yamt 	struct disk *dk = &dksc->sc_dkdev;
    114       1.1     elric 
    115      1.39    cegger 	DPRINTF_FOLLOW(("dk_open(%s, %p, 0x%"PRIx64", 0x%x)\n",
    116  1.54.2.2     skrll 	    dksc->sc_xname, dksc, dev, flags));
    117       1.1     elric 
    118      1.30        ad 	mutex_enter(&dk->dk_openlock);
    119       1.1     elric 	part = DISKPART(dev);
    120      1.16      yamt 
    121      1.16      yamt 	/*
    122      1.16      yamt 	 * If there are wedges, and this is not RAW_PART, then we
    123      1.16      yamt 	 * need to fail.
    124      1.16      yamt 	 */
    125      1.16      yamt 	if (dk->dk_nwedges != 0 && part != RAW_PART) {
    126      1.16      yamt 		ret = EBUSY;
    127      1.16      yamt 		goto done;
    128      1.16      yamt 	}
    129      1.16      yamt 
    130       1.1     elric 	pmask = 1 << part;
    131       1.1     elric 
    132       1.1     elric 	/*
    133       1.1     elric 	 * If we're init'ed and there are no other open partitions then
    134       1.1     elric 	 * update the in-core disklabel.
    135       1.1     elric 	 */
    136      1.16      yamt 	if ((dksc->sc_flags & DKF_INITED)) {
    137  1.54.2.2     skrll 		if ((dksc->sc_flags & DKF_VLABEL) == 0) {
    138  1.54.2.2     skrll 			dksc->sc_flags |= DKF_VLABEL;
    139  1.54.2.2     skrll 			dk_getdisklabel(dksc, dev);
    140      1.16      yamt 		}
    141      1.16      yamt 	}
    142       1.1     elric 
    143       1.1     elric 	/* Fail if we can't find the partition. */
    144  1.54.2.2     skrll 	if (part != RAW_PART &&
    145  1.54.2.2     skrll 	    ((dksc->sc_flags & DKF_VLABEL) == 0 ||
    146  1.54.2.2     skrll 	     part >= lp->d_npartitions ||
    147  1.54.2.2     skrll 	     lp->d_partitions[part].p_fstype == FS_UNUSED)) {
    148       1.1     elric 		ret = ENXIO;
    149       1.1     elric 		goto done;
    150       1.1     elric 	}
    151       1.1     elric 
    152       1.1     elric 	/* Mark our unit as open. */
    153       1.1     elric 	switch (fmt) {
    154       1.1     elric 	case S_IFCHR:
    155      1.16      yamt 		dk->dk_copenmask |= pmask;
    156       1.1     elric 		break;
    157       1.1     elric 	case S_IFBLK:
    158      1.16      yamt 		dk->dk_bopenmask |= pmask;
    159       1.1     elric 		break;
    160       1.1     elric 	}
    161       1.1     elric 
    162      1.16      yamt 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    163       1.1     elric 
    164       1.1     elric done:
    165      1.30        ad 	mutex_exit(&dk->dk_openlock);
    166       1.1     elric 	return ret;
    167       1.1     elric }
    168       1.1     elric 
    169       1.1     elric /* ARGSUSED */
    170       1.1     elric int
    171  1.54.2.2     skrll dk_close(struct dk_softc *dksc, dev_t dev,
    172      1.27  christos     int flags, int fmt, struct lwp *l)
    173       1.1     elric {
    174  1.54.2.2     skrll 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    175       1.1     elric 	int	part = DISKPART(dev);
    176       1.1     elric 	int	pmask = 1 << part;
    177      1.16      yamt 	struct disk *dk = &dksc->sc_dkdev;
    178       1.1     elric 
    179      1.39    cegger 	DPRINTF_FOLLOW(("dk_close(%s, %p, 0x%"PRIx64", 0x%x)\n",
    180  1.54.2.2     skrll 	    dksc->sc_xname, dksc, dev, flags));
    181       1.1     elric 
    182      1.30        ad 	mutex_enter(&dk->dk_openlock);
    183       1.1     elric 
    184       1.1     elric 	switch (fmt) {
    185       1.1     elric 	case S_IFCHR:
    186      1.16      yamt 		dk->dk_copenmask &= ~pmask;
    187       1.1     elric 		break;
    188       1.1     elric 	case S_IFBLK:
    189      1.16      yamt 		dk->dk_bopenmask &= ~pmask;
    190       1.1     elric 		break;
    191       1.1     elric 	}
    192      1.16      yamt 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    193       1.1     elric 
    194  1.54.2.2     skrll 	if (dk->dk_openmask == 0) {
    195  1.54.2.2     skrll 		if (dkd->d_lastclose != NULL)
    196  1.54.2.2     skrll 			(*dkd->d_lastclose)(dksc->sc_dev);
    197  1.54.2.2     skrll 	}
    198  1.54.2.2     skrll 
    199  1.54.2.2     skrll 	if ((dksc->sc_flags & DKF_KLABEL) == 0)
    200  1.54.2.2     skrll 		dksc->sc_flags &= ~DKF_VLABEL;
    201  1.54.2.2     skrll 
    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.1     elric void
    207  1.54.2.2     skrll dk_strategy(struct dk_softc *dksc, struct buf *bp)
    208       1.1     elric {
    209  1.54.2.2     skrll 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    210  1.54.2.1     skrll 	int	s, part;
    211       1.1     elric 	int	wlabel;
    212      1.18      yamt 	daddr_t	blkno;
    213  1.54.2.1     skrll 	struct disklabel *lp;
    214  1.54.2.1     skrll 	struct disk *dk;
    215  1.54.2.1     skrll 	uint64_t numsecs;
    216  1.54.2.1     skrll 	unsigned secsize;
    217       1.1     elric 
    218       1.1     elric 	DPRINTF_FOLLOW(("dk_strategy(%s, %p, %p)\n",
    219  1.54.2.2     skrll 	    dksc->sc_xname, dksc, bp));
    220       1.1     elric 
    221       1.1     elric 	if (!(dksc->sc_flags & DKF_INITED)) {
    222      1.25       dan 		DPRINTF_FOLLOW(("dk_strategy: not inited\n"));
    223       1.1     elric 		bp->b_error  = ENXIO;
    224       1.2     elric 		biodone(bp);
    225       1.1     elric 		return;
    226       1.1     elric 	}
    227       1.1     elric 
    228  1.54.2.1     skrll 	lp = dksc->sc_dkdev.dk_label;
    229  1.54.2.1     skrll 	dk = &dksc->sc_dkdev;
    230  1.54.2.1     skrll 
    231  1.54.2.1     skrll 	part = DISKPART(bp->b_dev);
    232  1.54.2.1     skrll 	numsecs = dk->dk_geom.dg_secperunit;
    233  1.54.2.1     skrll 	secsize = dk->dk_geom.dg_secsize;
    234       1.1     elric 
    235       1.1     elric 	bp->b_resid = bp->b_bcount;
    236       1.1     elric 
    237  1.54.2.1     skrll 	/*
    238  1.54.2.1     skrll 	 * The transfer must be a whole number of blocks and the offset must
    239  1.54.2.1     skrll 	 * not be negative.
    240  1.54.2.2     skrll 	 */
    241  1.54.2.1     skrll 	if ((bp->b_bcount % secsize) != 0 || bp->b_blkno < 0) {
    242  1.54.2.1     skrll 		bp->b_error = EINVAL;
    243  1.54.2.1     skrll 		biodone(bp);
    244  1.54.2.1     skrll 		return;
    245  1.54.2.2     skrll 	}
    246  1.54.2.1     skrll 
    247       1.1     elric 	/* If there is nothing to do, then we are done */
    248       1.1     elric 	if (bp->b_bcount == 0) {
    249       1.1     elric 		biodone(bp);
    250       1.1     elric 		return;
    251       1.1     elric 	}
    252       1.1     elric 
    253       1.1     elric 	wlabel = dksc->sc_flags & (DKF_WLABEL|DKF_LABELLING);
    254  1.54.2.1     skrll 	if (part == RAW_PART) {
    255  1.54.2.1     skrll 		if (bounds_check_with_mediasize(bp, DEV_BSIZE, numsecs) <= 0) {
    256  1.54.2.1     skrll 			biodone(bp);
    257  1.54.2.1     skrll 			return;
    258  1.54.2.1     skrll 		}
    259  1.54.2.1     skrll 	} else {
    260  1.54.2.1     skrll 		if (bounds_check_with_label(&dksc->sc_dkdev, bp, wlabel) <= 0) {
    261  1.54.2.1     skrll 			biodone(bp);
    262  1.54.2.1     skrll 			return;
    263  1.54.2.1     skrll 		}
    264       1.1     elric 	}
    265       1.1     elric 
    266      1.18      yamt 	blkno = bp->b_blkno;
    267  1.54.2.1     skrll 	if (part != RAW_PART)
    268  1.54.2.1     skrll 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
    269      1.18      yamt 	bp->b_rawblkno = blkno;
    270      1.18      yamt 
    271       1.1     elric 	/*
    272       1.1     elric 	 * Start the unit by calling the start routine
    273       1.1     elric 	 * provided by the individual driver.
    274       1.1     elric 	 */
    275       1.1     elric 	s = splbio();
    276      1.40      yamt 	bufq_put(dksc->sc_bufq, bp);
    277  1.54.2.2     skrll 	dkd->d_diskstart(dksc->sc_dev);
    278       1.1     elric 	splx(s);
    279       1.1     elric 	return;
    280       1.1     elric }
    281       1.1     elric 
    282  1.54.2.2     skrll void
    283  1.54.2.2     skrll dk_done(struct dk_softc *dksc, struct buf *bp)
    284  1.54.2.2     skrll {
    285  1.54.2.2     skrll 	struct disk *dk = &dksc->sc_dkdev;
    286  1.54.2.2     skrll 
    287  1.54.2.2     skrll 	if (bp->b_error != 0) {
    288  1.54.2.2     skrll 		diskerr(bp, dksc->sc_xname, "error", LOG_PRINTF, 0,
    289  1.54.2.2     skrll 			dk->dk_label);
    290  1.54.2.2     skrll 		printf("\n");
    291  1.54.2.2     skrll 	}
    292  1.54.2.2     skrll 
    293  1.54.2.2     skrll 	disk_unbusy(dk, bp->b_bcount - bp->b_resid, (bp->b_flags & B_READ));
    294  1.54.2.2     skrll #ifdef notyet
    295  1.54.2.2     skrll 	rnd_add_uint(&dksc->sc_rnd_source, bp->b_rawblkno);
    296  1.54.2.2     skrll #endif
    297  1.54.2.2     skrll 	biodone(bp);
    298  1.54.2.2     skrll }
    299  1.54.2.2     skrll 
    300       1.1     elric int
    301  1.54.2.2     skrll dk_size(struct dk_softc *dksc, dev_t dev)
    302       1.1     elric {
    303  1.54.2.2     skrll 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    304       1.1     elric 	struct	disklabel *lp;
    305       1.1     elric 	int	is_open;
    306       1.1     elric 	int	part;
    307       1.1     elric 	int	size;
    308       1.1     elric 
    309       1.1     elric 	if ((dksc->sc_flags & DKF_INITED) == 0)
    310       1.1     elric 		return -1;
    311       1.1     elric 
    312       1.1     elric 	part = DISKPART(dev);
    313       1.1     elric 	is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
    314       1.1     elric 
    315  1.54.2.2     skrll 	if (!is_open && dkd->d_open(dev, 0, S_IFBLK, curlwp))
    316       1.1     elric 		return -1;
    317       1.1     elric 
    318       1.1     elric 	lp = dksc->sc_dkdev.dk_label;
    319       1.1     elric 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
    320       1.1     elric 		size = -1;
    321       1.1     elric 	else
    322       1.1     elric 		size = lp->d_partitions[part].p_size *
    323       1.1     elric 		    (lp->d_secsize / DEV_BSIZE);
    324       1.1     elric 
    325  1.54.2.2     skrll 	if (!is_open && dkd->d_close(dev, 0, S_IFBLK, curlwp))
    326  1.54.2.2     skrll 		return -1;
    327       1.1     elric 
    328       1.1     elric 	return size;
    329       1.1     elric }
    330       1.1     elric 
    331       1.1     elric int
    332  1.54.2.2     skrll dk_ioctl(struct dk_softc *dksc, dev_t dev,
    333      1.28  christos 	    u_long cmd, void *data, int flag, struct lwp *l)
    334       1.1     elric {
    335  1.54.2.2     skrll 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    336       1.1     elric 	struct	disklabel *lp;
    337  1.54.2.1     skrll 	struct	disk *dk = &dksc->sc_dkdev;
    338       1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    339       1.1     elric 	struct	disklabel newlabel;
    340       1.1     elric #endif
    341  1.54.2.1     skrll 	int	error;
    342       1.1     elric 
    343      1.39    cegger 	DPRINTF_FOLLOW(("dk_ioctl(%s, %p, 0x%"PRIx64", 0x%lx)\n",
    344  1.54.2.2     skrll 	    dksc->sc_xname, dksc, dev, cmd));
    345       1.1     elric 
    346       1.1     elric 	/* ensure that the pseudo disk is open for writes for these commands */
    347       1.1     elric 	switch (cmd) {
    348       1.1     elric 	case DIOCSDINFO:
    349       1.1     elric 	case DIOCWDINFO:
    350       1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    351       1.1     elric 	case ODIOCSDINFO:
    352       1.1     elric 	case ODIOCWDINFO:
    353       1.1     elric #endif
    354       1.1     elric 	case DIOCWLABEL:
    355      1.43     elric 	case DIOCAWEDGE:
    356      1.43     elric 	case DIOCDWEDGE:
    357       1.1     elric 		if ((flag & FWRITE) == 0)
    358       1.1     elric 			return EBADF;
    359       1.1     elric 	}
    360       1.1     elric 
    361       1.1     elric 	/* ensure that the pseudo-disk is initialized for these */
    362       1.1     elric 	switch (cmd) {
    363       1.1     elric 	case DIOCGDINFO:
    364       1.1     elric 	case DIOCSDINFO:
    365       1.1     elric 	case DIOCWDINFO:
    366       1.1     elric 	case DIOCGPART:
    367  1.54.2.2     skrll 	case DIOCKLABEL:
    368       1.1     elric 	case DIOCWLABEL:
    369       1.1     elric 	case DIOCGDEFLABEL:
    370      1.43     elric 	case DIOCAWEDGE:
    371      1.43     elric 	case DIOCDWEDGE:
    372      1.43     elric 	case DIOCLWEDGES:
    373      1.54   mlelstv 	case DIOCMWEDGES:
    374      1.43     elric 	case DIOCCACHESYNC:
    375       1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    376       1.1     elric 	case ODIOCGDINFO:
    377       1.1     elric 	case ODIOCSDINFO:
    378       1.1     elric 	case ODIOCWDINFO:
    379       1.1     elric 	case ODIOCGDEFLABEL:
    380       1.1     elric #endif
    381       1.1     elric 		if ((dksc->sc_flags & DKF_INITED) == 0)
    382       1.1     elric 			return ENXIO;
    383       1.1     elric 	}
    384       1.1     elric 
    385  1.54.2.1     skrll 	error = disk_ioctl(dk, dev, cmd, data, flag, l);
    386  1.54.2.1     skrll 	if (error != EPASSTHROUGH)
    387  1.54.2.1     skrll 		return error;
    388  1.54.2.1     skrll 	else
    389  1.54.2.1     skrll 		error = 0;
    390       1.1     elric 
    391  1.54.2.1     skrll 	switch (cmd) {
    392       1.1     elric 	case DIOCWDINFO:
    393       1.1     elric 	case DIOCSDINFO:
    394       1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    395       1.1     elric 	case ODIOCWDINFO:
    396       1.1     elric 	case ODIOCSDINFO:
    397       1.1     elric #endif
    398       1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    399       1.1     elric 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    400       1.1     elric 			memset(&newlabel, 0, sizeof newlabel);
    401       1.1     elric 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    402       1.1     elric 			lp = &newlabel;
    403       1.1     elric 		} else
    404       1.1     elric #endif
    405       1.1     elric 		lp = (struct disklabel *)data;
    406       1.1     elric 
    407      1.30        ad 		mutex_enter(&dk->dk_openlock);
    408       1.1     elric 		dksc->sc_flags |= DKF_LABELLING;
    409       1.1     elric 
    410       1.1     elric 		error = setdisklabel(dksc->sc_dkdev.dk_label,
    411       1.1     elric 		    lp, 0, dksc->sc_dkdev.dk_cpulabel);
    412       1.1     elric 		if (error == 0) {
    413       1.1     elric 			if (cmd == DIOCWDINFO
    414       1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    415       1.1     elric 			    || cmd == ODIOCWDINFO
    416       1.1     elric #endif
    417       1.1     elric 			   )
    418       1.1     elric 				error = writedisklabel(DKLABELDEV(dev),
    419  1.54.2.2     skrll 				    dkd->d_strategy, dksc->sc_dkdev.dk_label,
    420       1.1     elric 				    dksc->sc_dkdev.dk_cpulabel);
    421       1.1     elric 		}
    422       1.1     elric 
    423       1.1     elric 		dksc->sc_flags &= ~DKF_LABELLING;
    424      1.30        ad 		mutex_exit(&dk->dk_openlock);
    425       1.1     elric 		break;
    426       1.1     elric 
    427  1.54.2.2     skrll 	case DIOCKLABEL:
    428  1.54.2.2     skrll 		if ((flag & FWRITE) == 0)
    429  1.54.2.2     skrll 			return (EBADF);
    430  1.54.2.2     skrll 		if (*(int *)data != 0)
    431  1.54.2.2     skrll 			dksc->sc_flags |= DKF_KLABEL;
    432  1.54.2.2     skrll 		else
    433  1.54.2.2     skrll 			dksc->sc_flags &= ~DKF_KLABEL;
    434  1.54.2.2     skrll 		break;
    435  1.54.2.2     skrll 
    436       1.1     elric 	case DIOCWLABEL:
    437       1.1     elric 		if (*(int *)data != 0)
    438       1.1     elric 			dksc->sc_flags |= DKF_WLABEL;
    439       1.1     elric 		else
    440       1.1     elric 			dksc->sc_flags &= ~DKF_WLABEL;
    441       1.1     elric 		break;
    442       1.1     elric 
    443       1.1     elric 	case DIOCGDEFLABEL:
    444  1.54.2.2     skrll 		dk_getdefaultlabel(dksc, (struct disklabel *)data);
    445       1.1     elric 		break;
    446       1.1     elric 
    447       1.1     elric #ifdef __HAVE_OLD_DISKLABEL
    448       1.1     elric 	case ODIOCGDEFLABEL:
    449  1.54.2.2     skrll 		dk_getdefaultlabel(dksc, &newlabel);
    450       1.1     elric 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    451       1.1     elric 			return ENOTTY;
    452       1.1     elric 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    453       1.1     elric 		break;
    454       1.1     elric #endif
    455       1.1     elric 
    456      1.21      yamt 	case DIOCGSTRATEGY:
    457      1.21      yamt 	    {
    458      1.21      yamt 		struct disk_strategy *dks = (void *)data;
    459      1.21      yamt 		int s;
    460      1.21      yamt 
    461      1.21      yamt 		s = splbio();
    462      1.21      yamt 		strlcpy(dks->dks_name, bufq_getstrategyname(dksc->sc_bufq),
    463      1.21      yamt 		    sizeof(dks->dks_name));
    464      1.21      yamt 		splx(s);
    465      1.21      yamt 		dks->dks_paramlen = 0;
    466      1.21      yamt 
    467      1.21      yamt 		return 0;
    468      1.21      yamt 	    }
    469  1.54.2.2     skrll 
    470      1.21      yamt 	case DIOCSSTRATEGY:
    471      1.21      yamt 	    {
    472      1.21      yamt 		struct disk_strategy *dks = (void *)data;
    473      1.21      yamt 		struct bufq_state *new;
    474      1.21      yamt 		struct bufq_state *old;
    475      1.21      yamt 		int s;
    476      1.21      yamt 
    477      1.21      yamt 		if ((flag & FWRITE) == 0) {
    478      1.21      yamt 			return EBADF;
    479      1.21      yamt 		}
    480      1.21      yamt 		if (dks->dks_param != NULL) {
    481      1.21      yamt 			return EINVAL;
    482      1.21      yamt 		}
    483      1.21      yamt 		dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
    484      1.21      yamt 		error = bufq_alloc(&new, dks->dks_name,
    485      1.21      yamt 		    BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
    486      1.21      yamt 		if (error) {
    487      1.21      yamt 			return error;
    488      1.21      yamt 		}
    489      1.21      yamt 		s = splbio();
    490      1.21      yamt 		old = dksc->sc_bufq;
    491      1.21      yamt 		bufq_move(new, old);
    492      1.21      yamt 		dksc->sc_bufq = new;
    493      1.21      yamt 		splx(s);
    494      1.21      yamt 		bufq_free(old);
    495      1.21      yamt 
    496      1.21      yamt 		return 0;
    497      1.21      yamt 	    }
    498      1.21      yamt 
    499       1.1     elric 	default:
    500       1.1     elric 		error = ENOTTY;
    501       1.1     elric 	}
    502       1.1     elric 
    503       1.1     elric 	return error;
    504       1.1     elric }
    505       1.1     elric 
    506       1.1     elric /*
    507       1.1     elric  * dk_dump dumps all of physical memory into the partition specified.
    508       1.1     elric  * This requires substantially more framework than {s,w}ddump, and hence
    509       1.1     elric  * is probably much more fragile.
    510       1.1     elric  *
    511       1.1     elric  */
    512       1.1     elric 
    513       1.1     elric #define DKF_READYFORDUMP	(DKF_INITED|DKF_TAKEDUMP)
    514       1.1     elric #define DKFF_READYFORDUMP(x)	(((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
    515       1.1     elric static volatile int	dk_dumping = 0;
    516       1.1     elric 
    517       1.1     elric /* ARGSUSED */
    518       1.1     elric int
    519  1.54.2.2     skrll dk_dump(struct dk_softc *dksc, dev_t dev,
    520  1.54.2.2     skrll     daddr_t blkno, void *vav, size_t size)
    521       1.1     elric {
    522  1.54.2.2     skrll 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    523  1.54.2.2     skrll 	char *va = vav;
    524  1.54.2.2     skrll 	struct disklabel *lp;
    525  1.54.2.2     skrll 	int part, towrt, nsects, sectoff, maxblkcnt, nblk;
    526  1.54.2.2     skrll 	int maxxfer, rv = 0;
    527       1.1     elric 
    528       1.1     elric 	/*
    529       1.1     elric 	 * ensure that we consider this device to be safe for dumping,
    530       1.1     elric 	 * and that the device is configured.
    531       1.1     elric 	 */
    532       1.1     elric 	if (!DKFF_READYFORDUMP(dksc->sc_flags))
    533       1.1     elric 		return ENXIO;
    534       1.1     elric 
    535       1.1     elric 	/* ensure that we are not already dumping */
    536       1.1     elric 	if (dk_dumping)
    537       1.1     elric 		return EFAULT;
    538       1.1     elric 	dk_dumping = 1;
    539       1.1     elric 
    540  1.54.2.2     skrll 	if (dkd->d_dumpblocks == NULL)
    541  1.54.2.2     skrll 		return ENXIO;
    542  1.54.2.2     skrll 
    543  1.54.2.2     skrll 	/* device specific max transfer size */
    544  1.54.2.2     skrll 	maxxfer = MAXPHYS;
    545  1.54.2.2     skrll 	if (dkd->d_iosize != NULL)
    546  1.54.2.2     skrll 		(*dkd->d_iosize)(dksc->sc_dev, &maxxfer);
    547  1.54.2.2     skrll 
    548  1.54.2.2     skrll 	/* Convert to disk sectors.  Request must be a multiple of size. */
    549  1.54.2.2     skrll 	part = DISKPART(dev);
    550  1.54.2.2     skrll 	lp = dksc->sc_dkdev.dk_label;
    551  1.54.2.2     skrll 	if ((size % lp->d_secsize) != 0)
    552  1.54.2.2     skrll 		return (EFAULT);
    553  1.54.2.2     skrll 	towrt = size / lp->d_secsize;
    554  1.54.2.2     skrll 	blkno = dbtob(blkno) / lp->d_secsize;   /* blkno in secsize units */
    555  1.54.2.2     skrll 
    556  1.54.2.2     skrll 	nsects = lp->d_partitions[part].p_size;
    557  1.54.2.2     skrll 	sectoff = lp->d_partitions[part].p_offset;
    558  1.54.2.2     skrll 
    559  1.54.2.2     skrll 	/* Check transfer bounds against partition size. */
    560  1.54.2.2     skrll 	if ((blkno < 0) || ((blkno + towrt) > nsects))
    561  1.54.2.2     skrll 		return (EINVAL);
    562  1.54.2.2     skrll 
    563  1.54.2.2     skrll 	/* Offset block number to start of partition. */
    564  1.54.2.2     skrll 	blkno += sectoff;
    565  1.54.2.2     skrll 
    566  1.54.2.2     skrll 	/* Start dumping and return when done. */
    567  1.54.2.2     skrll 	maxblkcnt = howmany(maxxfer, lp->d_secsize);
    568  1.54.2.2     skrll 	while (towrt > 0) {
    569  1.54.2.2     skrll 		nblk = min(maxblkcnt, towrt);
    570  1.54.2.2     skrll 
    571  1.54.2.2     skrll 		if ((rv = (*dkd->d_dumpblocks)(dksc->sc_dev, va, blkno, nblk)) != 0)
    572  1.54.2.2     skrll 			return (rv);
    573  1.54.2.2     skrll 
    574  1.54.2.2     skrll 		towrt -= nblk;
    575  1.54.2.2     skrll 		blkno += nblk;
    576  1.54.2.2     skrll 		va += nblk * lp->d_secsize;
    577  1.54.2.2     skrll 	}
    578       1.1     elric 
    579       1.1     elric 	dk_dumping = 0;
    580       1.1     elric 
    581  1.54.2.2     skrll 	return 0;
    582       1.1     elric }
    583       1.1     elric 
    584       1.1     elric /* ARGSUSED */
    585       1.1     elric void
    586  1.54.2.2     skrll dk_getdefaultlabel(struct dk_softc *dksc, struct disklabel *lp)
    587       1.1     elric {
    588      1.47  christos 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    589       1.4     elric 
    590       1.4     elric 	memset(lp, 0, sizeof(*lp));
    591       1.1     elric 
    592      1.52   mlelstv 	if (dg->dg_secperunit > UINT32_MAX)
    593      1.52   mlelstv 		lp->d_secperunit = UINT32_MAX;
    594      1.52   mlelstv 	else
    595      1.52   mlelstv 		lp->d_secperunit = dg->dg_secperunit;
    596      1.47  christos 	lp->d_secsize = dg->dg_secsize;
    597      1.47  christos 	lp->d_nsectors = dg->dg_nsectors;
    598      1.47  christos 	lp->d_ntracks = dg->dg_ntracks;
    599      1.47  christos 	lp->d_ncylinders = dg->dg_ncylinders;
    600       1.1     elric 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    601       1.1     elric 
    602  1.54.2.2     skrll 	strlcpy(lp->d_typename, dksc->sc_xname, sizeof(lp->d_typename));
    603  1.54.2.2     skrll 	lp->d_type = dksc->sc_dtype;
    604  1.54.2.2     skrll 	strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    605       1.1     elric 	lp->d_rpm = 3600;
    606       1.1     elric 	lp->d_interleave = 1;
    607       1.1     elric 	lp->d_flags = 0;
    608       1.1     elric 
    609       1.1     elric 	lp->d_partitions[RAW_PART].p_offset = 0;
    610      1.52   mlelstv 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
    611       1.1     elric 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    612       1.1     elric 	lp->d_npartitions = RAW_PART + 1;
    613       1.1     elric 
    614       1.1     elric 	lp->d_magic = DISKMAGIC;
    615       1.1     elric 	lp->d_magic2 = DISKMAGIC;
    616       1.1     elric 	lp->d_checksum = dkcksum(dksc->sc_dkdev.dk_label);
    617       1.1     elric }
    618       1.1     elric 
    619       1.1     elric /* ARGSUSED */
    620       1.1     elric void
    621  1.54.2.2     skrll dk_getdisklabel(struct dk_softc *dksc, dev_t dev)
    622       1.1     elric {
    623  1.54.2.2     skrll 	const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
    624       1.1     elric 	struct	 disklabel *lp = dksc->sc_dkdev.dk_label;
    625       1.1     elric 	struct	 cpu_disklabel *clp = dksc->sc_dkdev.dk_cpulabel;
    626  1.54.2.2     skrll 	struct   disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    627       1.1     elric 	struct	 partition *pp;
    628       1.1     elric 	int	 i;
    629       1.5       dsl 	const char	*errstring;
    630       1.1     elric 
    631       1.1     elric 	memset(clp, 0x0, sizeof(*clp));
    632  1.54.2.2     skrll 	dk_getdefaultlabel(dksc, lp);
    633  1.54.2.2     skrll 	errstring = readdisklabel(DKLABELDEV(dev), dkd->d_strategy,
    634       1.1     elric 	    dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
    635       1.1     elric 	if (errstring) {
    636  1.54.2.2     skrll 		dk_makedisklabel(dksc);
    637       1.1     elric 		if (dksc->sc_flags & DKF_WARNLABEL)
    638       1.1     elric 			printf("%s: %s\n", dksc->sc_xname, errstring);
    639       1.1     elric 		return;
    640       1.1     elric 	}
    641       1.1     elric 
    642       1.1     elric 	if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
    643       1.1     elric 		return;
    644       1.1     elric 
    645       1.1     elric 	/* Sanity check */
    646      1.53   mlelstv 	if (lp->d_secperunit < UINT32_MAX ?
    647      1.53   mlelstv 		lp->d_secperunit != dg->dg_secperunit :
    648      1.53   mlelstv 		lp->d_secperunit > dg->dg_secperunit)
    649      1.53   mlelstv 		printf("WARNING: %s: total sector size in disklabel (%ju) "
    650      1.53   mlelstv 		    "!= the size of %s (%ju)\n", dksc->sc_xname,
    651  1.54.2.2     skrll 		    (uintmax_t)lp->d_secperunit, dksc->sc_xname,
    652      1.53   mlelstv 		    (uintmax_t)dg->dg_secperunit);
    653       1.1     elric 
    654       1.1     elric 	for (i=0; i < lp->d_npartitions; i++) {
    655       1.1     elric 		pp = &lp->d_partitions[i];
    656      1.48  christos 		if (pp->p_offset + pp->p_size > dg->dg_secperunit)
    657       1.1     elric 			printf("WARNING: %s: end of partition `%c' exceeds "
    658      1.53   mlelstv 			    "the size of %s (%ju)\n", dksc->sc_xname,
    659  1.54.2.2     skrll 			    'a' + i, dksc->sc_xname,
    660      1.53   mlelstv 			    (uintmax_t)dg->dg_secperunit);
    661       1.1     elric 	}
    662       1.1     elric }
    663       1.1     elric 
    664       1.1     elric /* ARGSUSED */
    665      1.13   thorpej static void
    666  1.54.2.2     skrll dk_makedisklabel(struct dk_softc *dksc)
    667       1.1     elric {
    668       1.1     elric 	struct	disklabel *lp = dksc->sc_dkdev.dk_label;
    669       1.1     elric 
    670       1.1     elric 	lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
    671  1.54.2.2     skrll 	strlcpy(lp->d_packname, "default label", sizeof(lp->d_packname));
    672       1.1     elric 	lp->d_checksum = dkcksum(lp);
    673       1.1     elric }
    674       1.1     elric 
    675       1.1     elric /* This function is taken from ccd.c:1.76  --rcd */
    676       1.1     elric 
    677       1.1     elric /*
    678       1.1     elric  * XXX this function looks too generic for dksubr.c, shouldn't we
    679       1.1     elric  *     put it somewhere better?
    680       1.1     elric  */
    681       1.1     elric 
    682       1.1     elric /*
    683       1.1     elric  * Lookup the provided name in the filesystem.  If the file exists,
    684       1.1     elric  * is a valid block device, and isn't being used by anyone else,
    685       1.1     elric  * set *vpp to the file's vnode.
    686       1.1     elric  */
    687       1.1     elric int
    688      1.42  dholland dk_lookup(struct pathbuf *pb, struct lwp *l, struct vnode **vpp)
    689       1.1     elric {
    690       1.1     elric 	struct nameidata nd;
    691       1.1     elric 	struct vnode *vp;
    692      1.24  christos 	int     error;
    693       1.1     elric 
    694      1.24  christos 	if (l == NULL)
    695      1.24  christos 		return ESRCH;	/* Is ESRCH the best choice? */
    696      1.24  christos 
    697      1.42  dholland 	NDINIT(&nd, LOOKUP, FOLLOW, pb);
    698      1.24  christos 	if ((error = vn_open(&nd, FREAD | FWRITE, 0)) != 0) {
    699       1.1     elric 		DPRINTF((DKDB_FOLLOW|DKDB_INIT),
    700       1.1     elric 		    ("dk_lookup: vn_open error = %d\n", error));
    701      1.24  christos 		return error;
    702       1.1     elric 	}
    703      1.24  christos 
    704       1.1     elric 	vp = nd.ni_vp;
    705      1.51   hannken 	if (vp->v_type != VBLK) {
    706      1.51   hannken 		error = ENOTBLK;
    707      1.24  christos 		goto out;
    708       1.1     elric 	}
    709       1.1     elric 
    710      1.51   hannken 	/* Reopen as anonymous vnode to protect against forced unmount. */
    711      1.51   hannken 	if ((error = bdevvp(vp->v_rdev, vpp)) != 0)
    712      1.24  christos 		goto out;
    713      1.51   hannken 	VOP_UNLOCK(vp);
    714      1.51   hannken 	if ((error = vn_close(vp, FREAD | FWRITE, l->l_cred)) != 0) {
    715      1.51   hannken 		vrele(*vpp);
    716      1.51   hannken 		return error;
    717      1.51   hannken 	}
    718      1.51   hannken 	if ((error = VOP_OPEN(*vpp, FREAD | FWRITE, l->l_cred)) != 0) {
    719      1.51   hannken 		vrele(*vpp);
    720      1.51   hannken 		return error;
    721      1.24  christos 	}
    722      1.51   hannken 	mutex_enter((*vpp)->v_interlock);
    723      1.51   hannken 	(*vpp)->v_writecount++;
    724      1.51   hannken 	mutex_exit((*vpp)->v_interlock);
    725      1.24  christos 
    726      1.51   hannken 	IFDEBUG(DKDB_VNODE, vprint("dk_lookup: vnode info", *vpp));
    727       1.1     elric 
    728      1.24  christos 	return 0;
    729      1.24  christos out:
    730      1.41   hannken 	VOP_UNLOCK(vp);
    731      1.35        ad 	(void) vn_close(vp, FREAD | FWRITE, l->l_cred);
    732      1.24  christos 	return error;
    733       1.1     elric }
    734      1.49  pgoyette 
    735      1.49  pgoyette MODULE(MODULE_CLASS_MISC, dk_subr, NULL);
    736      1.49  pgoyette 
    737      1.49  pgoyette static int
    738      1.49  pgoyette dk_subr_modcmd(modcmd_t cmd, void *arg)
    739      1.49  pgoyette {
    740      1.49  pgoyette 	switch (cmd) {
    741      1.49  pgoyette 	case MODULE_CMD_INIT:
    742      1.49  pgoyette 	case MODULE_CMD_FINI:
    743      1.49  pgoyette 		return 0;
    744      1.49  pgoyette 	case MODULE_CMD_STAT:
    745      1.49  pgoyette 	case MODULE_CMD_AUTOUNLOAD:
    746      1.49  pgoyette 	default:
    747      1.49  pgoyette 		return ENOTTY;
    748      1.49  pgoyette 	}
    749      1.49  pgoyette }
    750