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