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