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