Home | History | Annotate | Line # | Download | only in dev
dksubr.c revision 1.9.2.4
      1  1.9.2.4    skrll /* $NetBSD: dksubr.c,v 1.9.2.4 2004/09/18 14:44:28 skrll Exp $ */
      2      1.1    elric 
      3      1.1    elric /*-
      4      1.1    elric  * Copyright (c) 1996, 1997, 1998, 1999, 2002 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  * 3. All advertising materials mentioning features or use of this software
     19      1.1    elric  *    must display the following acknowledgement:
     20      1.1    elric  *        This product includes software developed by the NetBSD
     21      1.1    elric  *        Foundation, Inc. and its contributors.
     22      1.1    elric  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.1    elric  *    contributors may be used to endorse or promote products derived
     24      1.1    elric  *    from this software without specific prior written permission.
     25      1.1    elric  *
     26      1.1    elric  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.1    elric  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.1    elric  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.1    elric  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.1    elric  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.1    elric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.1    elric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.1    elric  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.1    elric  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.1    elric  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.1    elric  * POSSIBILITY OF SUCH DAMAGE.
     37      1.1    elric  */
     38      1.1    elric 
     39  1.9.2.2    skrll #include <sys/cdefs.h>
     40  1.9.2.4    skrll __KERNEL_RCSID(0, "$NetBSD: dksubr.c,v 1.9.2.4 2004/09/18 14:44:28 skrll Exp $");
     41  1.9.2.2    skrll 
     42      1.1    elric #include <sys/param.h>
     43      1.1    elric #include <sys/systm.h>
     44      1.1    elric #include <sys/stat.h>
     45      1.1    elric #include <sys/proc.h>
     46      1.1    elric #include <sys/ioctl.h>
     47      1.1    elric #include <sys/device.h>
     48      1.1    elric #include <sys/disk.h>
     49      1.1    elric #include <sys/disklabel.h>
     50      1.1    elric #include <sys/vnode.h>
     51      1.1    elric #include <sys/fcntl.h>
     52      1.1    elric #include <sys/namei.h>
     53      1.1    elric 
     54      1.1    elric #include <dev/dkvar.h>
     55      1.1    elric 
     56      1.1    elric int	dkdebug = 0;
     57      1.1    elric 
     58      1.1    elric #ifdef DEBUG
     59      1.1    elric #define DKDB_FOLLOW	0x1
     60      1.1    elric #define DKDB_INIT	0x2
     61      1.1    elric #define DKDB_VNODE	0x4
     62      1.1    elric 
     63      1.1    elric #define IFDEBUG(x,y)		if (dkdebug & (x)) y
     64      1.1    elric #define DPRINTF(x,y)		IFDEBUG(x, printf y)
     65      1.1    elric #define DPRINTF_FOLLOW(y)	DPRINTF(DKDB_FOLLOW, y)
     66      1.1    elric #else
     67      1.1    elric #define IFDEBUG(x,y)
     68      1.1    elric #define DPRINTF(x,y)
     69      1.1    elric #define DPRINTF_FOLLOW(y)
     70      1.1    elric #endif
     71      1.1    elric 
     72      1.1    elric #define DKLABELDEV(dev)	\
     73      1.1    elric 	(MAKEDISKDEV(major((dev)), DISKUNIT((dev)), RAW_PART))
     74      1.1    elric 
     75  1.9.2.3    skrll static void	dk_makedisklabel(struct dk_intf *, struct dk_softc *);
     76      1.1    elric 
     77      1.1    elric void
     78      1.1    elric dk_sc_init(struct dk_softc *dksc, void *osc, char *xname)
     79      1.1    elric {
     80      1.1    elric 
     81      1.1    elric 	memset(dksc, 0x0, sizeof(*dksc));
     82      1.1    elric 	dksc->sc_osc = osc;
     83      1.1    elric 	strncpy(dksc->sc_xname, xname, DK_XNAME_SIZE);
     84      1.1    elric 	dksc->sc_dkdev.dk_name = dksc->sc_xname;
     85      1.1    elric 	lockinit(&dksc->sc_lock, PRIBIO, "dklk", 0, 0);
     86      1.1    elric }
     87      1.1    elric 
     88      1.1    elric /* ARGSUSED */
     89      1.1    elric int
     90      1.1    elric dk_open(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
     91  1.9.2.4    skrll 	   int flags, int fmt, struct proc *p)
     92      1.1    elric {
     93      1.1    elric 	struct	disklabel *lp = dksc->sc_dkdev.dk_label;
     94      1.1    elric 	int	part = DISKPART(dev);
     95      1.1    elric 	int	pmask = 1 << part;
     96      1.1    elric 	int	ret = 0;
     97      1.1    elric 
     98      1.1    elric 	DPRINTF_FOLLOW(("dk_open(%s, %p, 0x%x, 0x%x)\n",
     99      1.1    elric 	    di->di_dkname, dksc, dev, flags));
    100      1.1    elric 
    101      1.1    elric 	if ((ret = lockmgr(&dksc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
    102      1.1    elric 		return ret;
    103      1.1    elric 
    104      1.1    elric 	part = DISKPART(dev);
    105      1.1    elric 	pmask = 1 << part;
    106      1.1    elric 
    107      1.1    elric 	/*
    108      1.1    elric 	 * If we're init'ed and there are no other open partitions then
    109      1.1    elric 	 * update the in-core disklabel.
    110      1.1    elric 	 */
    111      1.1    elric 	if ((dksc->sc_flags & DKF_INITED) && dksc->sc_dkdev.dk_openmask == 0)
    112      1.1    elric 		dk_getdisklabel(di, dksc, dev);
    113      1.1    elric 
    114      1.1    elric 	/* Fail if we can't find the partition. */
    115      1.1    elric 	if ((part != RAW_PART) &&
    116      1.1    elric 	    (((dksc->sc_flags & DKF_INITED) == 0) ||
    117      1.1    elric 	    ((part >= lp->d_npartitions) ||
    118      1.1    elric 	    (lp->d_partitions[part].p_fstype == FS_UNUSED)))) {
    119      1.1    elric 		ret = ENXIO;
    120      1.1    elric 		goto done;
    121      1.1    elric 	}
    122      1.1    elric 
    123      1.1    elric 	/* Mark our unit as open. */
    124      1.1    elric 	switch (fmt) {
    125      1.1    elric 	case S_IFCHR:
    126      1.1    elric 		dksc->sc_dkdev.dk_copenmask |= pmask;
    127      1.1    elric 		break;
    128      1.1    elric 	case S_IFBLK:
    129      1.1    elric 		dksc->sc_dkdev.dk_bopenmask |= pmask;
    130      1.1    elric 		break;
    131      1.1    elric 	}
    132      1.1    elric 
    133      1.1    elric 	dksc->sc_dkdev.dk_openmask =
    134      1.1    elric 	    dksc->sc_dkdev.dk_copenmask | dksc->sc_dkdev.dk_bopenmask;
    135      1.1    elric 
    136      1.1    elric done:
    137      1.1    elric 	lockmgr(&dksc->sc_lock, LK_RELEASE, NULL);
    138      1.1    elric 	return ret;
    139      1.1    elric }
    140      1.1    elric 
    141      1.1    elric /* ARGSUSED */
    142      1.1    elric int
    143      1.1    elric dk_close(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
    144  1.9.2.4    skrll 	    int flags, int fmt, struct proc *p)
    145      1.1    elric {
    146      1.1    elric 	int	part = DISKPART(dev);
    147      1.1    elric 	int	pmask = 1 << part;
    148      1.1    elric 	int	ret;
    149      1.1    elric 
    150      1.1    elric 	DPRINTF_FOLLOW(("dk_close(%s, %p, 0x%x, 0x%x)\n",
    151      1.1    elric 	    di->di_dkname, dksc, dev, flags));
    152      1.1    elric 
    153      1.1    elric 	if ((ret = lockmgr(&dksc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
    154      1.1    elric 		return ret;
    155      1.1    elric 
    156      1.1    elric 	switch (fmt) {
    157      1.1    elric 	case S_IFCHR:
    158      1.1    elric 		dksc->sc_dkdev.dk_copenmask &= ~pmask;
    159      1.1    elric 		break;
    160      1.1    elric 	case S_IFBLK:
    161      1.1    elric 		dksc->sc_dkdev.dk_bopenmask &= ~pmask;
    162      1.1    elric 		break;
    163      1.1    elric 	}
    164      1.1    elric 	dksc->sc_dkdev.dk_openmask =
    165      1.1    elric 	    dksc->sc_dkdev.dk_copenmask | dksc->sc_dkdev.dk_bopenmask;
    166      1.1    elric 
    167      1.1    elric 	lockmgr(&dksc->sc_lock, LK_RELEASE, NULL);
    168      1.1    elric 	return 0;
    169      1.1    elric }
    170      1.1    elric 
    171      1.1    elric void
    172      1.1    elric dk_strategy(struct dk_intf *di, struct dk_softc *dksc, struct buf *bp)
    173      1.1    elric {
    174      1.1    elric 	int	s;
    175      1.1    elric 	int	wlabel;
    176      1.1    elric 
    177      1.1    elric 	DPRINTF_FOLLOW(("dk_strategy(%s, %p, %p)\n",
    178      1.1    elric 	    di->di_dkname, dksc, bp));
    179      1.1    elric 
    180      1.1    elric 	if (!(dksc->sc_flags & DKF_INITED)) {
    181      1.1    elric 		DPRINTF_FOLLOW(("dk_stragy: not inited\n"));
    182      1.1    elric 		bp->b_error  = ENXIO;
    183      1.1    elric 		bp->b_flags |= B_ERROR;
    184      1.2    elric 		biodone(bp);
    185      1.1    elric 		return;
    186      1.1    elric 	}
    187      1.1    elric 
    188      1.1    elric 	/* XXX look for some more errors, c.f. ld.c */
    189      1.1    elric 
    190      1.1    elric 	bp->b_resid = bp->b_bcount;
    191      1.1    elric 
    192      1.1    elric 	/* If there is nothing to do, then we are done */
    193      1.1    elric 	if (bp->b_bcount == 0) {
    194      1.1    elric 		biodone(bp);
    195      1.1    elric 		return;
    196      1.1    elric 	}
    197      1.1    elric 
    198      1.1    elric 	wlabel = dksc->sc_flags & (DKF_WLABEL|DKF_LABELLING);
    199      1.1    elric 	if (DISKPART(bp->b_dev) != RAW_PART &&
    200      1.6  thorpej 	    bounds_check_with_label(&dksc->sc_dkdev, bp, wlabel) <= 0) {
    201      1.1    elric 		biodone(bp);
    202      1.1    elric 		return;
    203      1.1    elric 	}
    204      1.1    elric 
    205      1.1    elric 	/*
    206      1.1    elric 	 * Start the unit by calling the start routine
    207      1.1    elric 	 * provided by the individual driver.
    208      1.1    elric 	 */
    209      1.1    elric 	s = splbio();
    210  1.9.2.2    skrll 	BUFQ_PUT(&dksc->sc_bufq, bp);
    211  1.9.2.2    skrll 	dk_start(di, dksc);
    212      1.1    elric 	splx(s);
    213      1.1    elric 	return;
    214      1.1    elric }
    215      1.1    elric 
    216  1.9.2.2    skrll void
    217  1.9.2.2    skrll dk_start(struct dk_intf *di, struct dk_softc *dksc)
    218  1.9.2.2    skrll {
    219  1.9.2.2    skrll 	struct	buf *bp;
    220  1.9.2.2    skrll 
    221  1.9.2.2    skrll 	DPRINTF_FOLLOW(("dk_start(%s, %p)\n", di->di_dkname, dksc));
    222  1.9.2.2    skrll 
    223  1.9.2.2    skrll 	/* Process the work queue */
    224  1.9.2.2    skrll 	while ((bp = BUFQ_GET(&dksc->sc_bufq)) != NULL) {
    225  1.9.2.2    skrll 		if (di->di_diskstart(dksc, bp) != 0) {
    226  1.9.2.2    skrll 			BUFQ_PUT(&dksc->sc_bufq, bp);
    227  1.9.2.2    skrll 			break;
    228  1.9.2.2    skrll 		}
    229  1.9.2.2    skrll 	}
    230  1.9.2.2    skrll }
    231  1.9.2.2    skrll 
    232  1.9.2.2    skrll void
    233  1.9.2.2    skrll dk_iodone(struct dk_intf *di, struct dk_softc *dksc)
    234  1.9.2.2    skrll {
    235  1.9.2.2    skrll 
    236  1.9.2.2    skrll 	DPRINTF_FOLLOW(("dk_iodone(%s, %p)\n", di->di_dkname, dksc));
    237  1.9.2.2    skrll 
    238  1.9.2.2    skrll 	/* We kick the queue in case we are able to get more work done */
    239  1.9.2.2    skrll 	dk_start(di, dksc);
    240  1.9.2.2    skrll }
    241  1.9.2.2    skrll 
    242      1.1    elric int
    243      1.1    elric dk_size(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
    244      1.1    elric {
    245      1.1    elric 	struct	disklabel *lp;
    246      1.1    elric 	int	is_open;
    247      1.1    elric 	int	part;
    248      1.1    elric 	int	size;
    249      1.1    elric 
    250      1.1    elric 	if ((dksc->sc_flags & DKF_INITED) == 0)
    251      1.1    elric 		return -1;
    252      1.1    elric 
    253      1.1    elric 	part = DISKPART(dev);
    254      1.1    elric 	is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
    255      1.1    elric 
    256  1.9.2.4    skrll 	if (!is_open && di->di_open(dev, 0, S_IFBLK, curproc))
    257      1.1    elric 		return -1;
    258      1.1    elric 
    259      1.1    elric 	lp = dksc->sc_dkdev.dk_label;
    260      1.1    elric 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
    261      1.1    elric 		size = -1;
    262      1.1    elric 	else
    263      1.1    elric 		size = lp->d_partitions[part].p_size *
    264      1.1    elric 		    (lp->d_secsize / DEV_BSIZE);
    265      1.1    elric 
    266  1.9.2.4    skrll 	if (!is_open && di->di_close(dev, 0, S_IFBLK, curproc))
    267      1.1    elric 		return 1;
    268      1.1    elric 
    269      1.1    elric 	return size;
    270      1.1    elric }
    271      1.1    elric 
    272      1.1    elric int
    273      1.1    elric dk_ioctl(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
    274  1.9.2.4    skrll 	    u_long cmd, caddr_t data, int flag, struct proc *p)
    275      1.1    elric {
    276      1.1    elric 	struct	disklabel *lp;
    277      1.1    elric #ifdef __HAVE_OLD_DISKLABEL
    278      1.1    elric 	struct	disklabel newlabel;
    279      1.1    elric #endif
    280      1.1    elric 	int	error = 0;
    281      1.1    elric 
    282      1.1    elric 	DPRINTF_FOLLOW(("dk_ioctl(%s, %p, 0x%x, 0x%lx)\n",
    283      1.1    elric 	    di->di_dkname, dksc, dev, cmd));
    284      1.1    elric 
    285      1.1    elric 	/* ensure that the pseudo disk is open for writes for these commands */
    286      1.1    elric 	switch (cmd) {
    287      1.1    elric 	case DIOCSDINFO:
    288      1.1    elric 	case DIOCWDINFO:
    289      1.1    elric #ifdef __HAVE_OLD_DISKLABEL
    290      1.1    elric 	case ODIOCSDINFO:
    291      1.1    elric 	case ODIOCWDINFO:
    292      1.1    elric #endif
    293      1.1    elric 	case DIOCWLABEL:
    294      1.1    elric 		if ((flag & FWRITE) == 0)
    295      1.1    elric 			return EBADF;
    296      1.1    elric 	}
    297      1.1    elric 
    298      1.1    elric 	/* ensure that the pseudo-disk is initialized for these */
    299      1.1    elric 	switch (cmd) {
    300      1.1    elric 	case DIOCGDINFO:
    301      1.1    elric 	case DIOCSDINFO:
    302      1.1    elric 	case DIOCWDINFO:
    303      1.1    elric 	case DIOCGPART:
    304      1.1    elric 	case DIOCWLABEL:
    305      1.1    elric 	case DIOCGDEFLABEL:
    306      1.1    elric #ifdef __HAVE_OLD_DISKLABEL
    307      1.1    elric 	case ODIOCGDINFO:
    308      1.1    elric 	case ODIOCSDINFO:
    309      1.1    elric 	case ODIOCWDINFO:
    310      1.1    elric 	case ODIOCGDEFLABEL:
    311      1.1    elric #endif
    312      1.1    elric 		if ((dksc->sc_flags & DKF_INITED) == 0)
    313      1.1    elric 			return ENXIO;
    314      1.1    elric 	}
    315      1.1    elric 
    316      1.1    elric 	switch (cmd) {
    317      1.1    elric 	case DIOCGDINFO:
    318      1.1    elric 		*(struct disklabel *)data = *(dksc->sc_dkdev.dk_label);
    319      1.1    elric 		break;
    320      1.1    elric 
    321      1.1    elric #ifdef __HAVE_OLD_DISKLABEL
    322      1.1    elric 	case ODIOCGDINFO:
    323      1.1    elric 		newlabel = *(dksc->sc_dkdev.dk_label);
    324      1.1    elric 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    325      1.1    elric 			return ENOTTY;
    326      1.1    elric 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    327      1.1    elric 		break;
    328      1.1    elric #endif
    329      1.1    elric 
    330      1.1    elric 	case DIOCGPART:
    331      1.1    elric 		((struct partinfo *)data)->disklab = dksc->sc_dkdev.dk_label;
    332      1.1    elric 		((struct partinfo *)data)->part =
    333      1.1    elric 		    &dksc->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
    334      1.1    elric 		break;
    335      1.1    elric 
    336      1.1    elric 	case DIOCWDINFO:
    337      1.1    elric 	case DIOCSDINFO:
    338      1.1    elric #ifdef __HAVE_OLD_DISKLABEL
    339      1.1    elric 	case ODIOCWDINFO:
    340      1.1    elric 	case ODIOCSDINFO:
    341      1.1    elric #endif
    342      1.1    elric #ifdef __HAVE_OLD_DISKLABEL
    343      1.1    elric 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    344      1.1    elric 			memset(&newlabel, 0, sizeof newlabel);
    345      1.1    elric 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    346      1.1    elric 			lp = &newlabel;
    347      1.1    elric 		} else
    348      1.1    elric #endif
    349      1.1    elric 		lp = (struct disklabel *)data;
    350      1.1    elric 
    351      1.1    elric 		dksc->sc_flags |= DKF_LABELLING;
    352      1.1    elric 
    353      1.1    elric 		error = setdisklabel(dksc->sc_dkdev.dk_label,
    354      1.1    elric 		    lp, 0, dksc->sc_dkdev.dk_cpulabel);
    355      1.1    elric 		if (error == 0) {
    356      1.1    elric 			if (cmd == DIOCWDINFO
    357      1.1    elric #ifdef __HAVE_OLD_DISKLABEL
    358      1.1    elric 			    || cmd == ODIOCWDINFO
    359      1.1    elric #endif
    360      1.1    elric 			   )
    361      1.1    elric 				error = writedisklabel(DKLABELDEV(dev),
    362      1.1    elric 				    di->di_strategy, dksc->sc_dkdev.dk_label,
    363      1.1    elric 				    dksc->sc_dkdev.dk_cpulabel);
    364      1.1    elric 		}
    365      1.1    elric 
    366      1.1    elric 		dksc->sc_flags &= ~DKF_LABELLING;
    367      1.1    elric 		break;
    368      1.1    elric 
    369      1.1    elric 	case DIOCWLABEL:
    370      1.1    elric 		if (*(int *)data != 0)
    371      1.1    elric 			dksc->sc_flags |= DKF_WLABEL;
    372      1.1    elric 		else
    373      1.1    elric 			dksc->sc_flags &= ~DKF_WLABEL;
    374      1.1    elric 		break;
    375      1.1    elric 
    376      1.1    elric 	case DIOCGDEFLABEL:
    377      1.1    elric 		dk_getdefaultlabel(di, dksc, (struct disklabel *)data);
    378      1.1    elric 		break;
    379      1.1    elric 
    380      1.1    elric #ifdef __HAVE_OLD_DISKLABEL
    381      1.1    elric 	case ODIOCGDEFLABEL:
    382      1.1    elric 		dk_getdefaultlabel(di, dksc, &newlabel);
    383      1.1    elric 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    384      1.1    elric 			return ENOTTY;
    385      1.1    elric 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    386      1.1    elric 		break;
    387      1.1    elric #endif
    388      1.1    elric 
    389      1.1    elric 	default:
    390      1.1    elric 		error = ENOTTY;
    391      1.1    elric 	}
    392      1.1    elric 
    393      1.1    elric 	return error;
    394      1.1    elric }
    395      1.1    elric 
    396      1.1    elric /*
    397      1.1    elric  * dk_dump dumps all of physical memory into the partition specified.
    398      1.1    elric  * This requires substantially more framework than {s,w}ddump, and hence
    399      1.1    elric  * is probably much more fragile.
    400      1.1    elric  *
    401      1.1    elric  * XXX: we currently do not implement this.
    402      1.1    elric  */
    403      1.1    elric 
    404      1.1    elric #define DKF_READYFORDUMP	(DKF_INITED|DKF_TAKEDUMP)
    405      1.1    elric #define DKFF_READYFORDUMP(x)	(((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
    406      1.1    elric static volatile int	dk_dumping = 0;
    407      1.1    elric 
    408      1.1    elric /* ARGSUSED */
    409      1.1    elric int
    410      1.1    elric dk_dump(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
    411      1.1    elric 	   daddr_t blkno, caddr_t va, size_t size)
    412      1.1    elric {
    413      1.1    elric 
    414      1.1    elric 	/*
    415      1.1    elric 	 * ensure that we consider this device to be safe for dumping,
    416      1.1    elric 	 * and that the device is configured.
    417      1.1    elric 	 */
    418      1.1    elric 	if (!DKFF_READYFORDUMP(dksc->sc_flags))
    419      1.1    elric 		return ENXIO;
    420      1.1    elric 
    421      1.1    elric 	/* ensure that we are not already dumping */
    422      1.1    elric 	if (dk_dumping)
    423      1.1    elric 		return EFAULT;
    424      1.1    elric 	dk_dumping = 1;
    425      1.1    elric 
    426      1.1    elric 	/* XXX: unimplemented */
    427      1.1    elric 
    428      1.1    elric 	dk_dumping = 0;
    429      1.1    elric 
    430      1.1    elric 	/* XXX: actually for now, we are going to leave this alone */
    431      1.1    elric 	return ENXIO;
    432      1.1    elric }
    433      1.1    elric 
    434      1.1    elric /* ARGSUSED */
    435      1.1    elric void
    436      1.1    elric dk_getdefaultlabel(struct dk_intf *di, struct dk_softc *dksc,
    437      1.1    elric 		      struct disklabel *lp)
    438      1.1    elric {
    439      1.1    elric 	struct dk_geom *pdg = &dksc->sc_geom;
    440      1.4    elric 
    441      1.4    elric 	memset(lp, 0, sizeof(*lp));
    442      1.1    elric 
    443      1.1    elric 	lp->d_secperunit = dksc->sc_size;
    444      1.1    elric 	lp->d_secsize = pdg->pdg_secsize;
    445      1.1    elric 	lp->d_nsectors = pdg->pdg_nsectors;
    446      1.1    elric 	lp->d_ntracks = pdg->pdg_ntracks;
    447      1.1    elric 	lp->d_ncylinders = pdg->pdg_ncylinders;
    448      1.1    elric 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    449      1.1    elric 
    450      1.1    elric 	strncpy(lp->d_typename, di->di_dkname, sizeof(lp->d_typename));
    451      1.1    elric 	lp->d_type = di->di_dtype;
    452      1.1    elric 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    453      1.1    elric 	lp->d_rpm = 3600;
    454      1.1    elric 	lp->d_interleave = 1;
    455      1.1    elric 	lp->d_flags = 0;
    456      1.1    elric 
    457      1.1    elric 	lp->d_partitions[RAW_PART].p_offset = 0;
    458      1.1    elric 	lp->d_partitions[RAW_PART].p_size = dksc->sc_size;
    459      1.1    elric 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    460      1.1    elric 	lp->d_npartitions = RAW_PART + 1;
    461      1.1    elric 
    462      1.1    elric 	lp->d_magic = DISKMAGIC;
    463      1.1    elric 	lp->d_magic2 = DISKMAGIC;
    464      1.1    elric 	lp->d_checksum = dkcksum(dksc->sc_dkdev.dk_label);
    465      1.1    elric }
    466      1.1    elric 
    467      1.1    elric /* ARGSUSED */
    468      1.1    elric void
    469      1.1    elric dk_getdisklabel(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
    470      1.1    elric {
    471      1.1    elric 	struct	 disklabel *lp = dksc->sc_dkdev.dk_label;
    472      1.1    elric 	struct	 cpu_disklabel *clp = dksc->sc_dkdev.dk_cpulabel;
    473      1.1    elric 	struct	 partition *pp;
    474      1.1    elric 	int	 i;
    475      1.5      dsl 	const char	*errstring;
    476      1.1    elric 
    477      1.1    elric 	memset(clp, 0x0, sizeof(*clp));
    478      1.1    elric 	dk_getdefaultlabel(di, dksc, lp);
    479      1.1    elric 	errstring = readdisklabel(DKLABELDEV(dev), di->di_strategy,
    480      1.1    elric 	    dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
    481      1.1    elric 	if (errstring) {
    482      1.1    elric 		dk_makedisklabel(di, dksc);
    483      1.1    elric 		if (dksc->sc_flags & DKF_WARNLABEL)
    484      1.1    elric 			printf("%s: %s\n", dksc->sc_xname, errstring);
    485      1.1    elric 		return;
    486      1.1    elric 	}
    487      1.1    elric 
    488      1.1    elric 	if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
    489      1.1    elric 		return;
    490      1.1    elric 
    491      1.1    elric 	/* Sanity check */
    492      1.1    elric 	if (lp->d_secperunit != dksc->sc_size)
    493      1.1    elric 		printf("WARNING: %s: total sector size in disklabel (%d) "
    494      1.1    elric 		    "!= the size of %s (%lu)\n", dksc->sc_xname,
    495      1.1    elric 		    lp->d_secperunit, di->di_dkname, (u_long)dksc->sc_size);
    496      1.1    elric 
    497      1.1    elric 	for (i=0; i < lp->d_npartitions; i++) {
    498      1.1    elric 		pp = &lp->d_partitions[i];
    499      1.1    elric 		if (pp->p_offset + pp->p_size > dksc->sc_size)
    500      1.1    elric 			printf("WARNING: %s: end of partition `%c' exceeds "
    501      1.1    elric 			    "the size of %s (%lu)\n", dksc->sc_xname,
    502      1.1    elric 			    'a' + i, di->di_dkname, (u_long)dksc->sc_size);
    503      1.1    elric 	}
    504      1.1    elric }
    505      1.1    elric 
    506      1.1    elric /* ARGSUSED */
    507  1.9.2.3    skrll static void
    508      1.1    elric dk_makedisklabel(struct dk_intf *di, struct dk_softc *dksc)
    509      1.1    elric {
    510      1.1    elric 	struct	disklabel *lp = dksc->sc_dkdev.dk_label;
    511      1.1    elric 
    512      1.1    elric 	lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
    513      1.1    elric 	strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
    514      1.1    elric 	lp->d_checksum = dkcksum(lp);
    515      1.1    elric }
    516      1.1    elric 
    517      1.1    elric /* This function is taken from ccd.c:1.76  --rcd */
    518      1.1    elric 
    519      1.1    elric /*
    520      1.1    elric  * XXX this function looks too generic for dksubr.c, shouldn't we
    521      1.1    elric  *     put it somewhere better?
    522      1.1    elric  */
    523      1.1    elric 
    524      1.1    elric /*
    525      1.1    elric  * Lookup the provided name in the filesystem.  If the file exists,
    526      1.1    elric  * is a valid block device, and isn't being used by anyone else,
    527      1.1    elric  * set *vpp to the file's vnode.
    528      1.1    elric  */
    529      1.1    elric int
    530  1.9.2.4    skrll dk_lookup(path, p, vpp)
    531      1.1    elric 	char *path;
    532  1.9.2.4    skrll 	struct proc *p;
    533      1.1    elric 	struct vnode **vpp;	/* result */
    534      1.1    elric {
    535      1.1    elric 	struct nameidata nd;
    536      1.1    elric 	struct vnode *vp;
    537      1.1    elric 	struct vattr va;
    538      1.1    elric 	int error;
    539      1.1    elric 
    540  1.9.2.4    skrll 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p);
    541      1.1    elric 	if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    542      1.1    elric 		DPRINTF((DKDB_FOLLOW|DKDB_INIT),
    543      1.1    elric 		    ("dk_lookup: vn_open error = %d\n", error));
    544      1.1    elric 		return (error);
    545      1.1    elric 	}
    546      1.1    elric 	vp = nd.ni_vp;
    547      1.1    elric 
    548      1.1    elric 	if (vp->v_usecount > 1) {
    549      1.1    elric 		VOP_UNLOCK(vp, 0);
    550  1.9.2.4    skrll 		(void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
    551      1.1    elric 		return (EBUSY);
    552      1.1    elric 	}
    553      1.1    elric 
    554  1.9.2.4    skrll 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0) {
    555      1.1    elric 		DPRINTF((DKDB_FOLLOW|DKDB_INIT),
    556      1.1    elric 		    ("dk_lookup: getattr error = %d\n", error));
    557      1.1    elric 		VOP_UNLOCK(vp, 0);
    558  1.9.2.4    skrll 		(void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
    559      1.1    elric 		return (error);
    560      1.1    elric 	}
    561      1.1    elric 
    562      1.1    elric 	/* XXX: eventually we should handle VREG, too. */
    563      1.1    elric 	if (va.va_type != VBLK) {
    564      1.1    elric 		VOP_UNLOCK(vp, 0);
    565  1.9.2.4    skrll 		(void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
    566      1.1    elric 		return (ENOTBLK);
    567      1.1    elric 	}
    568      1.1    elric 
    569      1.1    elric 	IFDEBUG(DKDB_VNODE, vprint("dk_lookup: vnode info", vp));
    570      1.1    elric 
    571      1.1    elric 	VOP_UNLOCK(vp, 0);
    572      1.1    elric 	*vpp = vp;
    573      1.1    elric 	return (0);
    574      1.1    elric }
    575