Home | History | Annotate | Line # | Download | only in dev
vnd.c revision 1.80
      1 /*	$NetBSD: vnd.c,v 1.80 2002/06/21 19:09:31 atatat Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997, 1998 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.
      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 /*
     40  * Copyright (c) 1988 University of Utah.
     41  * Copyright (c) 1990, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * This code is derived from software contributed to Berkeley by
     45  * the Systems Programming Group of the University of Utah Computer
     46  * Science Department.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. All advertising materials mentioning features or use of this software
     57  *    must display the following acknowledgement:
     58  *	This product includes software developed by the University of
     59  *	California, Berkeley and its contributors.
     60  * 4. Neither the name of the University nor the names of its contributors
     61  *    may be used to endorse or promote products derived from this software
     62  *    without specific prior written permission.
     63  *
     64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     74  * SUCH DAMAGE.
     75  *
     76  * from: Utah $Hdr: vn.c 1.13 94/04/02$
     77  *
     78  *	@(#)vn.c	8.9 (Berkeley) 5/14/95
     79  */
     80 
     81 /*
     82  * Vnode disk driver.
     83  *
     84  * Block/character interface to a vnode.  Allows one to treat a file
     85  * as a disk (e.g. build a filesystem in it, mount it, etc.).
     86  *
     87  * NOTE 1: This uses the VOP_BMAP/VOP_STRATEGY interface to the vnode
     88  * instead of a simple VOP_RDWR.  We do this to avoid distorting the
     89  * local buffer cache.
     90  *
     91  * NOTE 2: There is a security issue involved with this driver.
     92  * Once mounted all access to the contents of the "mapped" file via
     93  * the special file is controlled by the permissions on the special
     94  * file, the protection of the mapped file is ignored (effectively,
     95  * by using root credentials in all transactions).
     96  *
     97  * NOTE 3: Doesn't interact with leases, should it?
     98  */
     99 
    100 #include <sys/cdefs.h>
    101 __KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.80 2002/06/21 19:09:31 atatat Exp $");
    102 
    103 #if defined(_KERNEL_OPT)
    104 #include "fs_nfs.h"
    105 #endif
    106 
    107 #include <sys/param.h>
    108 #include <sys/systm.h>
    109 #include <sys/namei.h>
    110 #include <sys/proc.h>
    111 #include <sys/errno.h>
    112 #include <sys/buf.h>
    113 #include <sys/malloc.h>
    114 #include <sys/ioctl.h>
    115 #include <sys/disklabel.h>
    116 #include <sys/device.h>
    117 #include <sys/disk.h>
    118 #include <sys/stat.h>
    119 #include <sys/mount.h>
    120 #include <sys/vnode.h>
    121 #include <sys/file.h>
    122 #include <sys/uio.h>
    123 #include <sys/conf.h>
    124 
    125 #include <miscfs/specfs/specdev.h>
    126 
    127 #include <dev/vndvar.h>
    128 
    129 #if defined(VNDDEBUG) && !defined(DEBUG)
    130 #define	DEBUG
    131 #endif
    132 
    133 #ifdef DEBUG
    134 int dovndcluster = 1;
    135 #define	VDB_FOLLOW	0x01
    136 #define	VDB_INIT	0x02
    137 #define	VDB_IO		0x04
    138 #define	VDB_LABEL	0x08
    139 int vnddebug = 0x00;
    140 #endif
    141 
    142 #define	vndunit(x)	DISKUNIT(x)
    143 
    144 struct vndxfer {
    145 	struct buf	*vx_bp;		/* Pointer to parent buffer */
    146 	int		vx_error;
    147 	int		vx_pending;	/* # of pending aux buffers */
    148 	int		vx_flags;
    149 #define VX_BUSY		1
    150 };
    151 
    152 struct vndbuf {
    153 	struct buf	vb_buf;
    154 	struct vndxfer	*vb_xfer;
    155 };
    156 
    157 #define	VND_GETXFER(vnd)	pool_get(&(vnd)->sc_vxpool, PR_NOWAIT)
    158 #define	VND_PUTXFER(vnd, vx)	pool_put(&(vnd)->sc_vxpool, (vx))
    159 
    160 #define	VND_GETBUF(vnd)		pool_get(&(vnd)->sc_vbpool, PR_NOWAIT)
    161 #define	VND_PUTBUF(vnd, vb)	pool_put(&(vnd)->sc_vbpool, (vb))
    162 
    163 struct vnd_softc *vnd_softc;
    164 int numvnd = 0;
    165 
    166 #define	VNDLABELDEV(dev) \
    167 	(MAKEDISKDEV(major((dev)), vndunit((dev)), RAW_PART))
    168 
    169 /* called by main() at boot time */
    170 void	vndattach __P((int));
    171 void	vnddetach __P((void));
    172 
    173 void	vndclear __P((struct vnd_softc *));
    174 void	vndstart __P((struct vnd_softc *));
    175 int	vndsetcred __P((struct vnd_softc *, struct ucred *));
    176 void	vndthrottle __P((struct vnd_softc *, struct vnode *));
    177 void	vndiodone __P((struct buf *));
    178 void	vndshutdown __P((void));
    179 
    180 void	vndgetdefaultlabel __P((struct vnd_softc *, struct disklabel *));
    181 void	vndgetdisklabel __P((dev_t));
    182 
    183 static	int vndlock __P((struct vnd_softc *));
    184 static	void vndunlock __P((struct vnd_softc *));
    185 
    186 void
    187 vndattach(num)
    188 	int num;
    189 {
    190 	int i;
    191 	char *mem;
    192 
    193 	if (num <= 0)
    194 		return;
    195 	i = num * sizeof(struct vnd_softc);
    196 	mem = malloc(i, M_DEVBUF, M_NOWAIT|M_ZERO);
    197 	if (mem == NULL) {
    198 		printf("WARNING: no memory for vnode disks\n");
    199 		return;
    200 	}
    201 	vnd_softc = (struct vnd_softc *)mem;
    202 	numvnd = num;
    203 
    204 	for (i = 0; i < numvnd; i++)
    205 		BUFQ_INIT(&vnd_softc[i].sc_tab);
    206 }
    207 
    208 void
    209 vnddetach()
    210 {
    211 
    212 	free(vnd_softc, M_DEVBUF);
    213 }
    214 
    215 int
    216 vndopen(dev, flags, mode, p)
    217 	dev_t dev;
    218 	int flags, mode;
    219 	struct proc *p;
    220 {
    221 	int unit = vndunit(dev);
    222 	struct vnd_softc *sc;
    223 	int error = 0, part, pmask;
    224 	struct disklabel *lp;
    225 
    226 #ifdef DEBUG
    227 	if (vnddebug & VDB_FOLLOW)
    228 		printf("vndopen(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, p);
    229 #endif
    230 	if (unit >= numvnd)
    231 		return (ENXIO);
    232 	sc = &vnd_softc[unit];
    233 
    234 	if ((error = vndlock(sc)) != 0)
    235 		return (error);
    236 
    237 	lp = sc->sc_dkdev.dk_label;
    238 
    239 	part = DISKPART(dev);
    240 	pmask = (1 << part);
    241 
    242 	/*
    243 	 * If we're initialized, check to see if there are any other
    244 	 * open partitions.  If not, then it's safe to update the
    245 	 * in-core disklabel.
    246 	 */
    247 	if ((sc->sc_flags & VNF_INITED) && (sc->sc_dkdev.dk_openmask == 0))
    248 		vndgetdisklabel(dev);
    249 
    250 	/* Check that the partitions exists. */
    251 	if (part != RAW_PART) {
    252 		if (((sc->sc_flags & VNF_INITED) == 0) ||
    253 		    ((part >= lp->d_npartitions) ||
    254 		     (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
    255 			error = ENXIO;
    256 			goto done;
    257 		}
    258 	}
    259 
    260 	/* Prevent our unit from being unconfigured while open. */
    261 	switch (mode) {
    262 	case S_IFCHR:
    263 		sc->sc_dkdev.dk_copenmask |= pmask;
    264 		break;
    265 
    266 	case S_IFBLK:
    267 		sc->sc_dkdev.dk_bopenmask |= pmask;
    268 		break;
    269 	}
    270 	sc->sc_dkdev.dk_openmask =
    271 	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
    272 
    273  done:
    274 	vndunlock(sc);
    275 	return (error);
    276 }
    277 
    278 int
    279 vndclose(dev, flags, mode, p)
    280 	dev_t dev;
    281 	int flags, mode;
    282 	struct proc *p;
    283 {
    284 	int unit = vndunit(dev);
    285 	struct vnd_softc *sc;
    286 	int error = 0, part;
    287 
    288 #ifdef DEBUG
    289 	if (vnddebug & VDB_FOLLOW)
    290 		printf("vndclose(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, p);
    291 #endif
    292 
    293 	if (unit >= numvnd)
    294 		return (ENXIO);
    295 	sc = &vnd_softc[unit];
    296 
    297 	if ((error = vndlock(sc)) != 0)
    298 		return (error);
    299 
    300 	part = DISKPART(dev);
    301 
    302 	/* ...that much closer to allowing unconfiguration... */
    303 	switch (mode) {
    304 	case S_IFCHR:
    305 		sc->sc_dkdev.dk_copenmask &= ~(1 << part);
    306 		break;
    307 
    308 	case S_IFBLK:
    309 		sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
    310 		break;
    311 	}
    312 	sc->sc_dkdev.dk_openmask =
    313 	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
    314 
    315 	vndunlock(sc);
    316 	return (0);
    317 }
    318 
    319 /*
    320  * Break the request into bsize pieces and submit using VOP_BMAP/VOP_STRATEGY.
    321  */
    322 void
    323 vndstrategy(bp)
    324 	struct buf *bp;
    325 {
    326 	int unit = vndunit(bp->b_dev);
    327 	struct vnd_softc *vnd = &vnd_softc[unit];
    328 	struct vndxfer *vnx;
    329 	int s, bsize, resid;
    330 	off_t bn;
    331 	caddr_t addr;
    332 	int sz, flags, error, wlabel;
    333 	struct disklabel *lp;
    334 	struct partition *pp;
    335 
    336 #ifdef DEBUG
    337 	if (vnddebug & VDB_FOLLOW)
    338 		printf("vndstrategy(%p): unit %d\n", bp, unit);
    339 #endif
    340 	if ((vnd->sc_flags & VNF_INITED) == 0) {
    341 		bp->b_error = ENXIO;
    342 		bp->b_flags |= B_ERROR;
    343 		goto done;
    344 	}
    345 
    346 	/* If it's a nil transfer, wake up the top half now. */
    347 	if (bp->b_bcount == 0)
    348 		goto done;
    349 
    350 	lp = vnd->sc_dkdev.dk_label;
    351 
    352 	/*
    353 	 * The transfer must be a whole number of blocks.
    354 	 */
    355 	if ((bp->b_bcount % lp->d_secsize) != 0) {
    356 		bp->b_error = EINVAL;
    357 		bp->b_flags |= B_ERROR;
    358 		goto done;
    359 	}
    360 
    361 	/*
    362 	 * Do bounds checking and adjust transfer.  If there's an error,
    363 	 * the bounds check will flag that for us.
    364 	 */
    365 	wlabel = vnd->sc_flags & (VNF_WLABEL|VNF_LABELLING);
    366 	if (DISKPART(bp->b_dev) != RAW_PART)
    367 		if (bounds_check_with_label(bp, lp, wlabel) <= 0)
    368 			goto done;
    369 
    370 	bp->b_resid = bp->b_bcount;
    371 
    372 	/*
    373 	 * Put the block number in terms of the logical blocksize
    374 	 * of the "device".
    375 	 */
    376 	bn = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    377 
    378 	/*
    379 	 * Translate the partition-relative block number to an absolute.
    380 	 */
    381 	if (DISKPART(bp->b_dev) != RAW_PART) {
    382 		pp = &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
    383 		bn += pp->p_offset;
    384 	}
    385 
    386 	/* ...and convert to a byte offset within the file. */
    387 	bn *= lp->d_secsize;
    388 
    389 	if (vnd->sc_vp->v_mount == NULL) {
    390 		bp->b_error = ENXIO;
    391 		bp->b_flags |= B_ERROR;
    392 		goto done;
    393 	}
    394  	bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
    395 	addr = bp->b_data;
    396 	flags = (bp->b_flags & (B_READ|B_ASYNC)) | B_CALL;
    397 
    398 	/* Allocate a header for this transfer and link it to the buffer */
    399 	s = splbio();
    400 	vnx = VND_GETXFER(vnd);
    401 	splx(s);
    402 	vnx->vx_flags = VX_BUSY;
    403 	vnx->vx_error = 0;
    404 	vnx->vx_pending = 0;
    405 	vnx->vx_bp = bp;
    406 
    407 	for (resid = bp->b_resid; resid; resid -= sz) {
    408 		struct vndbuf *nbp;
    409 		struct vnode *vp;
    410 		daddr_t nbn;
    411 		int off, nra;
    412 
    413 		nra = 0;
    414 		vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
    415 		error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
    416 		VOP_UNLOCK(vnd->sc_vp, 0);
    417 
    418 		if (error == 0 && (long)nbn == -1)
    419 			error = EIO;
    420 
    421 		/*
    422 		 * If there was an error or a hole in the file...punt.
    423 		 * Note that we may have to wait for any operations
    424 		 * that we have already fired off before releasing
    425 		 * the buffer.
    426 		 *
    427 		 * XXX we could deal with holes here but it would be
    428 		 * a hassle (in the write case).
    429 		 */
    430 		if (error) {
    431 			s = splbio();
    432 			vnx->vx_error = error;
    433 			goto out;
    434 		}
    435 
    436 #ifdef DEBUG
    437 		if (!dovndcluster)
    438 			nra = 0;
    439 #endif
    440 
    441 		if ((off = bn % bsize) != 0)
    442 			sz = bsize - off;
    443 		else
    444 			sz = (1 + nra) * bsize;
    445 		if (resid < sz)
    446 			sz = resid;
    447 #ifdef DEBUG
    448 		if (vnddebug & VDB_IO)
    449 			printf("vndstrategy: vp %p/%p bn 0x%qx/0x%x sz 0x%x\n",
    450 			    vnd->sc_vp, vp, (long long)bn, nbn, sz);
    451 #endif
    452 
    453 		s = splbio();
    454 		nbp = VND_GETBUF(vnd);
    455 		splx(s);
    456 		nbp->vb_buf.b_flags = flags;
    457 		nbp->vb_buf.b_bcount = sz;
    458 		nbp->vb_buf.b_bufsize = round_page((ulong)addr + sz)
    459 		    - trunc_page((ulong) addr);
    460 		nbp->vb_buf.b_error = 0;
    461 		nbp->vb_buf.b_data = addr;
    462 		nbp->vb_buf.b_blkno = nbp->vb_buf.b_rawblkno = nbn + btodb(off);
    463 		nbp->vb_buf.b_proc = bp->b_proc;
    464 		nbp->vb_buf.b_iodone = vndiodone;
    465 		nbp->vb_buf.b_vp = NULLVP;
    466 		LIST_INIT(&nbp->vb_buf.b_dep);
    467 
    468 		nbp->vb_xfer = vnx;
    469 
    470 		/*
    471 		 * Just sort by block number
    472 		 */
    473 		s = splbio();
    474 		if (vnx->vx_error != 0) {
    475 			VND_PUTBUF(vnd, nbp);
    476 			goto out;
    477 		}
    478 		vnx->vx_pending++;
    479 		bgetvp(vp, &nbp->vb_buf);
    480 		disksort_blkno(&vnd->sc_tab, &nbp->vb_buf);
    481 		vndstart(vnd);
    482 		splx(s);
    483 		bn += sz;
    484 		addr += sz;
    485 	}
    486 
    487 	s = splbio();
    488 
    489 out: /* Arrive here at splbio */
    490 	vnx->vx_flags &= ~VX_BUSY;
    491 	if (vnx->vx_pending == 0) {
    492 		if (vnx->vx_error != 0) {
    493 			bp->b_error = vnx->vx_error;
    494 			bp->b_flags |= B_ERROR;
    495 		}
    496 		VND_PUTXFER(vnd, vnx);
    497 		biodone(bp);
    498 	}
    499 	splx(s);
    500 	return;
    501 
    502  done:
    503 	biodone(bp);
    504 }
    505 
    506 /*
    507  * Feed requests sequentially.
    508  * We do it this way to keep from flooding NFS servers if we are connected
    509  * to an NFS file.  This places the burden on the client rather than the
    510  * server.
    511  */
    512 void
    513 vndstart(vnd)
    514 	struct vnd_softc *vnd;
    515 {
    516 	struct buf	*bp;
    517 
    518 	/*
    519 	 * Dequeue now since lower level strategy routine might
    520 	 * queue using same links
    521 	 */
    522 
    523 	if ((vnd->sc_flags & VNF_BUSY) != 0)
    524 		return;
    525 
    526 	vnd->sc_flags |= VNF_BUSY;
    527 
    528 	while (vnd->sc_active < vnd->sc_maxactive) {
    529 		bp = BUFQ_FIRST(&vnd->sc_tab);
    530 		if (bp == NULL)
    531 			break;
    532 		BUFQ_REMOVE(&vnd->sc_tab, bp);
    533 		vnd->sc_active++;
    534 #ifdef DEBUG
    535 		if (vnddebug & VDB_IO)
    536 			printf("vndstart(%ld): bp %p vp %p blkno 0x%x"
    537 				" flags %lx addr %p cnt 0x%lx\n",
    538 			    (long) (vnd-vnd_softc), bp, bp->b_vp, bp->b_blkno,
    539 			    bp->b_flags, bp->b_data, bp->b_bcount);
    540 #endif
    541 
    542 		/* Instrumentation. */
    543 		disk_busy(&vnd->sc_dkdev);
    544 
    545 		if ((bp->b_flags & B_READ) == 0)
    546 			bp->b_vp->v_numoutput++;
    547 		VOP_STRATEGY(bp);
    548 	}
    549 	vnd->sc_flags &= ~VNF_BUSY;
    550 }
    551 
    552 void
    553 vndiodone(bp)
    554 	struct buf *bp;
    555 {
    556 	struct vndbuf *vbp = (struct vndbuf *) bp;
    557 	struct vndxfer *vnx = (struct vndxfer *)vbp->vb_xfer;
    558 	struct buf *pbp = vnx->vx_bp;
    559 	struct vnd_softc *vnd = &vnd_softc[vndunit(pbp->b_dev)];
    560 	int s, resid;
    561 
    562 	s = splbio();
    563 #ifdef DEBUG
    564 	if (vnddebug & VDB_IO)
    565 		printf("vndiodone(%ld): vbp %p vp %p blkno 0x%x addr %p cnt 0x%lx\n",
    566 		    (long) (vnd-vnd_softc), vbp, vbp->vb_buf.b_vp,
    567 		    vbp->vb_buf.b_blkno, vbp->vb_buf.b_data,
    568 		    vbp->vb_buf.b_bcount);
    569 #endif
    570 
    571 	resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
    572 	pbp->b_resid -= resid;
    573 	disk_unbusy(&vnd->sc_dkdev, resid);
    574 	vnx->vx_pending--;
    575 
    576 	if (vbp->vb_buf.b_error) {
    577 #ifdef DEBUG
    578 		if (vnddebug & VDB_IO)
    579 			printf("vndiodone: vbp %p error %d\n", vbp,
    580 			    vbp->vb_buf.b_error);
    581 #endif
    582 		vnx->vx_error = vbp->vb_buf.b_error;
    583 	}
    584 
    585 	if (vbp->vb_buf.b_vp != NULLVP)
    586 		brelvp(&vbp->vb_buf);
    587 
    588 	VND_PUTBUF(vnd, vbp);
    589 
    590 	/*
    591 	 * Wrap up this transaction if it has run to completion or, in
    592 	 * case of an error, when all auxiliary buffers have returned.
    593 	 */
    594 	if (vnx->vx_error != 0) {
    595 		pbp->b_flags |= B_ERROR;
    596 		pbp->b_error = vnx->vx_error;
    597 		if ((vnx->vx_flags & VX_BUSY) == 0 && vnx->vx_pending == 0) {
    598 
    599 #ifdef DEBUG
    600 			if (vnddebug & VDB_IO)
    601 				printf("vndiodone: pbp %p iodone: error %d\n",
    602 					pbp, vnx->vx_error);
    603 #endif
    604 			VND_PUTXFER(vnd, vnx);
    605 			biodone(pbp);
    606 		}
    607 	} else if (pbp->b_resid == 0) {
    608 
    609 #ifdef DIAGNOSTIC
    610 		if (vnx->vx_pending != 0)
    611 			panic("vndiodone: vnx pending: %d", vnx->vx_pending);
    612 #endif
    613 
    614 		if ((vnx->vx_flags & VX_BUSY) == 0) {
    615 #ifdef DEBUG
    616 			if (vnddebug & VDB_IO)
    617 				printf("vndiodone: pbp %p iodone\n", pbp);
    618 #endif
    619 			VND_PUTXFER(vnd, vnx);
    620 			biodone(pbp);
    621 		}
    622 	}
    623 
    624 	vnd->sc_active--;
    625 	vndstart(vnd);
    626 	splx(s);
    627 }
    628 
    629 /* ARGSUSED */
    630 int
    631 vndread(dev, uio, flags)
    632 	dev_t dev;
    633 	struct uio *uio;
    634 	int flags;
    635 {
    636 	int unit = vndunit(dev);
    637 	struct vnd_softc *sc;
    638 
    639 #ifdef DEBUG
    640 	if (vnddebug & VDB_FOLLOW)
    641 		printf("vndread(0x%x, %p)\n", dev, uio);
    642 #endif
    643 
    644 	if (unit >= numvnd)
    645 		return (ENXIO);
    646 	sc = &vnd_softc[unit];
    647 
    648 	if ((sc->sc_flags & VNF_INITED) == 0)
    649 		return (ENXIO);
    650 
    651 	return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
    652 }
    653 
    654 /* ARGSUSED */
    655 int
    656 vndwrite(dev, uio, flags)
    657 	dev_t dev;
    658 	struct uio *uio;
    659 	int flags;
    660 {
    661 	int unit = vndunit(dev);
    662 	struct vnd_softc *sc;
    663 
    664 #ifdef DEBUG
    665 	if (vnddebug & VDB_FOLLOW)
    666 		printf("vndwrite(0x%x, %p)\n", dev, uio);
    667 #endif
    668 
    669 	if (unit >= numvnd)
    670 		return (ENXIO);
    671 	sc = &vnd_softc[unit];
    672 
    673 	if ((sc->sc_flags & VNF_INITED) == 0)
    674 		return (ENXIO);
    675 
    676 	return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
    677 }
    678 
    679 /* ARGSUSED */
    680 int
    681 vndioctl(dev, cmd, data, flag, p)
    682 	dev_t dev;
    683 	u_long cmd;
    684 	caddr_t data;
    685 	int flag;
    686 	struct proc *p;
    687 {
    688 	int unit = vndunit(dev);
    689 	struct vnd_softc *vnd;
    690 	struct vnd_ioctl *vio;
    691 	struct vattr vattr;
    692 	struct nameidata nd;
    693 	int error, part, pmask;
    694 	size_t geomsize;
    695 #ifdef __HAVE_OLD_DISKLABEL
    696 	struct disklabel newlabel;
    697 #endif
    698 
    699 #ifdef DEBUG
    700 	if (vnddebug & VDB_FOLLOW)
    701 		printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
    702 		    dev, cmd, data, flag, p, unit);
    703 #endif
    704 	error = suser(p->p_ucred, &p->p_acflag);
    705 	if (error)
    706 		return (error);
    707 	if (unit >= numvnd)
    708 		return (ENXIO);
    709 
    710 	vnd = &vnd_softc[unit];
    711 	vio = (struct vnd_ioctl *)data;
    712 
    713 	/* Must be open for writes for these commands... */
    714 	switch (cmd) {
    715 	case VNDIOCSET:
    716 	case VNDIOCCLR:
    717 	case DIOCSDINFO:
    718 	case DIOCWDINFO:
    719 #ifdef __HAVE_OLD_DISKLABEL
    720 	case ODIOCSDINFO:
    721 	case ODIOCWDINFO:
    722 #endif
    723 	case DIOCWLABEL:
    724 		if ((flag & FWRITE) == 0)
    725 			return (EBADF);
    726 	}
    727 
    728 	/* Must be initialized for these... */
    729 	switch (cmd) {
    730 	case VNDIOCCLR:
    731 	case DIOCGDINFO:
    732 	case DIOCSDINFO:
    733 	case DIOCWDINFO:
    734 	case DIOCGPART:
    735 	case DIOCWLABEL:
    736 	case DIOCGDEFLABEL:
    737 #ifdef __HAVE_OLD_DISKLABEL
    738 	case ODIOCGDINFO:
    739 	case ODIOCSDINFO:
    740 	case ODIOCWDINFO:
    741 	case ODIOCGDEFLABEL:
    742 #endif
    743 		if ((vnd->sc_flags & VNF_INITED) == 0)
    744 			return (ENXIO);
    745 	}
    746 
    747 	switch (cmd) {
    748 	case VNDIOCSET:
    749 		if (vnd->sc_flags & VNF_INITED)
    750 			return (EBUSY);
    751 
    752 		if ((error = vndlock(vnd)) != 0)
    753 			return (error);
    754 
    755 		/*
    756 		 * Always open for read and write.
    757 		 * This is probably bogus, but it lets vn_open()
    758 		 * weed out directories, sockets, etc. so we don't
    759 		 * have to worry about them.
    760 		 */
    761 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, p);
    762 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    763 			vndunlock(vnd);
    764 			return(error);
    765 		}
    766 		error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p);
    767 		if (error) {
    768 			VOP_UNLOCK(nd.ni_vp, 0);
    769 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
    770 			vndunlock(vnd);
    771 			return(error);
    772 		}
    773 		VOP_UNLOCK(nd.ni_vp, 0);
    774 		vnd->sc_vp = nd.ni_vp;
    775 		vnd->sc_size = btodb(vattr.va_size);	/* note truncation */
    776 
    777 		/*
    778 		 * Use pseudo-geometry specified.  If none was provided,
    779 		 * use "standard" Adaptec fictitious geometry.
    780 		 */
    781 		if (vio->vnd_flags & VNDIOF_HASGEOM) {
    782 
    783 			memcpy(&vnd->sc_geom, &vio->vnd_geom,
    784 			    sizeof(vio->vnd_geom));
    785 
    786 			/*
    787 			 * Sanity-check the sector size.
    788 			 * XXX Don't allow secsize < DEV_BSIZE.  Should
    789 			 * XXX we?
    790 			 */
    791 			if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
    792 			    (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0) {
    793 				(void) vn_close(nd.ni_vp, FREAD|FWRITE,
    794 				    p->p_ucred, p);
    795 				vndunlock(vnd);
    796 				return (EINVAL);
    797 			}
    798 
    799 			/*
    800 			 * Compute the size (in DEV_BSIZE blocks) specified
    801 			 * by the geometry.
    802 			 */
    803 			geomsize = (vnd->sc_geom.vng_nsectors *
    804 			    vnd->sc_geom.vng_ntracks *
    805 			    vnd->sc_geom.vng_ncylinders) *
    806 			    (vnd->sc_geom.vng_secsize / DEV_BSIZE);
    807 
    808 			/*
    809 			 * Sanity-check the size against the specified
    810 			 * geometry.
    811 			 */
    812 			if (vnd->sc_size < geomsize) {
    813 				(void) vn_close(nd.ni_vp, FREAD|FWRITE,
    814 				    p->p_ucred, p);
    815 				vndunlock(vnd);
    816 				return (EINVAL);
    817 			}
    818 		} else {
    819 			/*
    820 			 * Size must be at least 2048 DEV_BSIZE blocks
    821 			 * (1M) in order to use this geometry.
    822 			 */
    823 			if (vnd->sc_size < (32 * 64)) {
    824 				vndunlock(vnd);
    825 				return (EINVAL);
    826 			}
    827 
    828 			vnd->sc_geom.vng_secsize = DEV_BSIZE;
    829 			vnd->sc_geom.vng_nsectors = 32;
    830 			vnd->sc_geom.vng_ntracks = 64;
    831 			vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
    832 
    833 			/*
    834 			 * Compute the actual size allowed by this geometry.
    835 			 */
    836 			geomsize = 32 * 64 * vnd->sc_geom.vng_ncylinders;
    837 		}
    838 
    839 		/*
    840 		 * Truncate the size to that specified by
    841 		 * the geometry.
    842 		 * XXX Should we even bother with this?
    843 		 */
    844 		vnd->sc_size = geomsize;
    845 
    846 		if ((error = vndsetcred(vnd, p->p_ucred)) != 0) {
    847 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
    848 			vndunlock(vnd);
    849 			return(error);
    850 		}
    851 		vndthrottle(vnd, vnd->sc_vp);
    852 		vio->vnd_size = dbtob(vnd->sc_size);
    853 		vnd->sc_flags |= VNF_INITED;
    854 #ifdef DEBUG
    855 		if (vnddebug & VDB_INIT)
    856 			printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
    857 			    vnd->sc_vp, (unsigned long) vnd->sc_size,
    858 			    vnd->sc_geom.vng_secsize,
    859 			    vnd->sc_geom.vng_nsectors,
    860 			    vnd->sc_geom.vng_ntracks,
    861 			    vnd->sc_geom.vng_ncylinders);
    862 #endif
    863 
    864 		/* Attach the disk. */
    865 		memset(vnd->sc_xname, 0, sizeof(vnd->sc_xname)); /* XXX */
    866 		sprintf(vnd->sc_xname, "vnd%d", unit);		/* XXX */
    867 		vnd->sc_dkdev.dk_name = vnd->sc_xname;
    868 		disk_attach(&vnd->sc_dkdev);
    869 
    870 		/* Initialize the xfer and buffer pools. */
    871 		pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
    872 		    0, 0, "vndxpl", NULL);
    873 		pool_init(&vnd->sc_vbpool, sizeof(struct vndbuf), 0,
    874 		    0, 0, "vndbpl", NULL);
    875 
    876 		/* Try and read the disklabel. */
    877 		vndgetdisklabel(dev);
    878 
    879 		vndunlock(vnd);
    880 
    881 		break;
    882 
    883 	case VNDIOCCLR:
    884 		if ((error = vndlock(vnd)) != 0)
    885 			return (error);
    886 
    887 		/*
    888 		 * Don't unconfigure if any other partitions are open
    889 		 * or if both the character and block flavors of this
    890 		 * partition are open.
    891 		 */
    892 		part = DISKPART(dev);
    893 		pmask = (1 << part);
    894 		if ((vnd->sc_dkdev.dk_openmask & ~pmask) ||
    895 		    ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
    896 		    (vnd->sc_dkdev.dk_copenmask & pmask))) {
    897 			vndunlock(vnd);
    898 			return (EBUSY);
    899 		}
    900 
    901 		vndclear(vnd);
    902 #ifdef DEBUG
    903 		if (vnddebug & VDB_INIT)
    904 			printf("vndioctl: CLRed\n");
    905 #endif
    906 
    907 		/* Destroy the xfer and buffer pools. */
    908 		pool_destroy(&vnd->sc_vxpool);
    909 		pool_destroy(&vnd->sc_vbpool);
    910 
    911 		/* Detatch the disk. */
    912 		disk_detach(&vnd->sc_dkdev);
    913 
    914 		vndunlock(vnd);
    915 
    916 		break;
    917 
    918 	case VNDIOCGET: {
    919 		struct vnd_user *vnu;
    920 		struct vattr va;
    921 
    922 		vnu = (struct vnd_user *)data;
    923 
    924 		if (vnu->vnu_unit == -1)
    925 			vnu->vnu_unit = unit;
    926 		if (vnu->vnu_unit >= numvnd)
    927 			return (ENXIO);
    928 		if (vnu->vnu_unit < 0)
    929 			return (EINVAL);
    930 
    931 		vnd = &vnd_softc[vnu->vnu_unit];
    932 
    933 		if (vnd->sc_flags & VNF_INITED) {
    934 			error = VOP_GETATTR(vnd->sc_vp, &va, p->p_ucred, p);
    935 			if (error)
    936 				return (error);
    937 			vnu->vnu_dev = va.va_fsid;
    938 			vnu->vnu_ino = va.va_fileid;
    939 		}
    940 		else {
    941 			/* unused is not an error */
    942 			vnu->vnu_dev = 0;
    943 			vnu->vnu_ino = 0;
    944 		}
    945 
    946 		break;
    947 	}
    948 
    949 	case DIOCGDINFO:
    950 		*(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
    951 		break;
    952 
    953 #ifdef __HAVE_OLD_DISKLABEL
    954 	case ODIOCGDINFO:
    955 		newlabel = *(vnd->sc_dkdev.dk_label);
    956 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    957 			return ENOTTY;
    958 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    959 		break;
    960 #endif
    961 
    962 	case DIOCGPART:
    963 		((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
    964 		((struct partinfo *)data)->part =
    965 		    &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
    966 		break;
    967 
    968 	case DIOCWDINFO:
    969 	case DIOCSDINFO:
    970 #ifdef __HAVE_OLD_DISKLABEL
    971 	case ODIOCWDINFO:
    972 	case ODIOCSDINFO:
    973 #endif
    974 	{
    975 		struct disklabel *lp;
    976 
    977 		if ((error = vndlock(vnd)) != 0)
    978 			return (error);
    979 
    980 		vnd->sc_flags |= VNF_LABELLING;
    981 
    982 #ifdef __HAVE_OLD_DISKLABEL
    983 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    984 			memset(&newlabel, 0, sizeof newlabel);
    985 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    986 			lp = &newlabel;
    987 		} else
    988 #endif
    989 		lp = (struct disklabel *)data;
    990 
    991 		error = setdisklabel(vnd->sc_dkdev.dk_label,
    992 		    lp, 0, vnd->sc_dkdev.dk_cpulabel);
    993 		if (error == 0) {
    994 			if (cmd == DIOCWDINFO
    995 #ifdef __HAVE_OLD_DISKLABEL
    996 			    || cmd == ODIOCWDINFO
    997 #endif
    998 			   )
    999 				error = writedisklabel(VNDLABELDEV(dev),
   1000 				    vndstrategy, vnd->sc_dkdev.dk_label,
   1001 				    vnd->sc_dkdev.dk_cpulabel);
   1002 		}
   1003 
   1004 		vnd->sc_flags &= ~VNF_LABELLING;
   1005 
   1006 		vndunlock(vnd);
   1007 
   1008 		if (error)
   1009 			return (error);
   1010 		break;
   1011 	}
   1012 
   1013 	case DIOCWLABEL:
   1014 		if (*(int *)data != 0)
   1015 			vnd->sc_flags |= VNF_WLABEL;
   1016 		else
   1017 			vnd->sc_flags &= ~VNF_WLABEL;
   1018 		break;
   1019 
   1020 	case DIOCGDEFLABEL:
   1021 		vndgetdefaultlabel(vnd, (struct disklabel *)data);
   1022 		break;
   1023 
   1024 #ifdef __HAVE_OLD_DISKLABEL
   1025 	case ODIOCGDEFLABEL:
   1026 		vndgetdefaultlabel(vnd, &newlabel);
   1027 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1028 			return ENOTTY;
   1029 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
   1030 		break;
   1031 #endif
   1032 
   1033 	default:
   1034 		return (ENOTTY);
   1035 	}
   1036 
   1037 	return (0);
   1038 }
   1039 
   1040 /*
   1041  * Duplicate the current processes' credentials.  Since we are called only
   1042  * as the result of a SET ioctl and only root can do that, any future access
   1043  * to this "disk" is essentially as root.  Note that credentials may change
   1044  * if some other uid can write directly to the mapped file (NFS).
   1045  */
   1046 int
   1047 vndsetcred(vnd, cred)
   1048 	struct vnd_softc *vnd;
   1049 	struct ucred *cred;
   1050 {
   1051 	struct uio auio;
   1052 	struct iovec aiov;
   1053 	char *tmpbuf;
   1054 	int error;
   1055 
   1056 	vnd->sc_cred = crdup(cred);
   1057 	tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
   1058 
   1059 	/* XXX: Horrible kludge to establish credentials for NFS */
   1060 	aiov.iov_base = tmpbuf;
   1061 	aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
   1062 	auio.uio_iov = &aiov;
   1063 	auio.uio_iovcnt = 1;
   1064 	auio.uio_offset = 0;
   1065 	auio.uio_rw = UIO_READ;
   1066 	auio.uio_segflg = UIO_SYSSPACE;
   1067 	auio.uio_resid = aiov.iov_len;
   1068 	vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
   1069 	error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
   1070 	if (error == 0) {
   1071 		/*
   1072 		 * Because vnd does all IO directly through the vnode
   1073 		 * we need to flush (at least) the buffer from the above
   1074 		 * VOP_READ from the buffer cache to prevent cache
   1075 		 * incoherencies.  Also, be careful to write dirty
   1076 		 * buffers back to stable storage.
   1077 		 */
   1078 		error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
   1079 			    curproc, 0, 0);
   1080 	}
   1081 	VOP_UNLOCK(vnd->sc_vp, 0);
   1082 
   1083 	free(tmpbuf, M_TEMP);
   1084 	return (error);
   1085 }
   1086 
   1087 /*
   1088  * Set maxactive based on FS type
   1089  */
   1090 void
   1091 vndthrottle(vnd, vp)
   1092 	struct vnd_softc *vnd;
   1093 	struct vnode *vp;
   1094 {
   1095 #ifdef NFS
   1096 	extern int (**nfsv2_vnodeop_p) __P((void *));
   1097 
   1098 	if (vp->v_op == nfsv2_vnodeop_p)
   1099 		vnd->sc_maxactive = 2;
   1100 	else
   1101 #endif
   1102 		vnd->sc_maxactive = 8;
   1103 
   1104 	if (vnd->sc_maxactive < 1)
   1105 		vnd->sc_maxactive = 1;
   1106 }
   1107 
   1108 void
   1109 vndshutdown()
   1110 {
   1111 	struct vnd_softc *vnd;
   1112 
   1113 	for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
   1114 		if (vnd->sc_flags & VNF_INITED)
   1115 			vndclear(vnd);
   1116 }
   1117 
   1118 void
   1119 vndclear(vnd)
   1120 	struct vnd_softc *vnd;
   1121 {
   1122 	struct vnode *vp = vnd->sc_vp;
   1123 	struct proc *p = curproc;		/* XXX */
   1124 
   1125 #ifdef DEBUG
   1126 	if (vnddebug & VDB_FOLLOW)
   1127 		printf("vndclear(%p): vp %p\n", vnd, vp);
   1128 #endif
   1129 	vnd->sc_flags &= ~VNF_INITED;
   1130 	if (vp == (struct vnode *)0)
   1131 		panic("vndioctl: null vp");
   1132 	(void) vn_close(vp, FREAD|FWRITE, vnd->sc_cred, p);
   1133 	crfree(vnd->sc_cred);
   1134 	vnd->sc_vp = (struct vnode *)0;
   1135 	vnd->sc_cred = (struct ucred *)0;
   1136 	vnd->sc_size = 0;
   1137 }
   1138 
   1139 int
   1140 vndsize(dev)
   1141 	dev_t dev;
   1142 {
   1143 	struct vnd_softc *sc;
   1144 	struct disklabel *lp;
   1145 	int part, unit, omask;
   1146 	int size;
   1147 
   1148 	unit = vndunit(dev);
   1149 	if (unit >= numvnd)
   1150 		return (-1);
   1151 	sc = &vnd_softc[unit];
   1152 
   1153 	if ((sc->sc_flags & VNF_INITED) == 0)
   1154 		return (-1);
   1155 
   1156 	part = DISKPART(dev);
   1157 	omask = sc->sc_dkdev.dk_openmask & (1 << part);
   1158 	lp = sc->sc_dkdev.dk_label;
   1159 
   1160 	if (omask == 0 && vndopen(dev, 0, S_IFBLK, curproc))
   1161 		return (-1);
   1162 
   1163 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
   1164 		size = -1;
   1165 	else
   1166 		size = lp->d_partitions[part].p_size *
   1167 		    (lp->d_secsize / DEV_BSIZE);
   1168 
   1169 	if (omask == 0 && vndclose(dev, 0, S_IFBLK, curproc))
   1170 		return (-1);
   1171 
   1172 	return (size);
   1173 }
   1174 
   1175 int
   1176 vnddump(dev, blkno, va, size)
   1177 	dev_t dev;
   1178 	daddr_t blkno;
   1179 	caddr_t va;
   1180 	size_t size;
   1181 {
   1182 
   1183 	/* Not implemented. */
   1184 	return ENXIO;
   1185 }
   1186 
   1187 void
   1188 vndgetdefaultlabel(sc, lp)
   1189 	struct vnd_softc *sc;
   1190 	struct disklabel *lp;
   1191 {
   1192 	struct vndgeom *vng = &sc->sc_geom;
   1193 	struct partition *pp;
   1194 
   1195 	memset(lp, 0, sizeof(*lp));
   1196 
   1197 	lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
   1198 	lp->d_secsize = vng->vng_secsize;
   1199 	lp->d_nsectors = vng->vng_nsectors;
   1200 	lp->d_ntracks = vng->vng_ntracks;
   1201 	lp->d_ncylinders = vng->vng_ncylinders;
   1202 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1203 
   1204 	strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
   1205 	lp->d_type = DTYPE_VND;
   1206 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1207 	lp->d_rpm = 3600;
   1208 	lp->d_interleave = 1;
   1209 	lp->d_flags = 0;
   1210 
   1211 	pp = &lp->d_partitions[RAW_PART];
   1212 	pp->p_offset = 0;
   1213 	pp->p_size = lp->d_secperunit;
   1214 	pp->p_fstype = FS_UNUSED;
   1215 	lp->d_npartitions = RAW_PART + 1;
   1216 
   1217 	lp->d_magic = DISKMAGIC;
   1218 	lp->d_magic2 = DISKMAGIC;
   1219 	lp->d_checksum = dkcksum(lp);
   1220 }
   1221 
   1222 /*
   1223  * Read the disklabel from a vnd.  If one is not present, create a fake one.
   1224  */
   1225 void
   1226 vndgetdisklabel(dev)
   1227 	dev_t dev;
   1228 {
   1229 	struct vnd_softc *sc = &vnd_softc[vndunit(dev)];
   1230 	char *errstring;
   1231 	struct disklabel *lp = sc->sc_dkdev.dk_label;
   1232 	struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
   1233 	int i;
   1234 
   1235 	memset(clp, 0, sizeof(*clp));
   1236 
   1237 	vndgetdefaultlabel(sc, lp);
   1238 
   1239 	/*
   1240 	 * Call the generic disklabel extraction routine.
   1241 	 */
   1242 	errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
   1243 	if (errstring) {
   1244 		/*
   1245 		 * Lack of disklabel is common, but we print the warning
   1246 		 * anyway, since it might contain other useful information.
   1247 		 */
   1248 		printf("%s: %s\n", sc->sc_xname, errstring);
   1249 
   1250 		/*
   1251 		 * For historical reasons, if there's no disklabel
   1252 		 * present, all partitions must be FS_BSDFFS and
   1253 		 * occupy the entire disk.
   1254 		 */
   1255 		for (i = 0; i < MAXPARTITIONS; i++) {
   1256 			/*
   1257 			 * Don't wipe out port specific hack (such as
   1258 			 * dos partition hack of i386 port).
   1259 			 */
   1260 			if (lp->d_partitions[i].p_fstype != FS_UNUSED)
   1261 				continue;
   1262 
   1263 			lp->d_partitions[i].p_size = lp->d_secperunit;
   1264 			lp->d_partitions[i].p_offset = 0;
   1265 			lp->d_partitions[i].p_fstype = FS_BSDFFS;
   1266 		}
   1267 
   1268 		strncpy(lp->d_packname, "default label",
   1269 		    sizeof(lp->d_packname));
   1270 
   1271 		lp->d_checksum = dkcksum(lp);
   1272 	}
   1273 }
   1274 
   1275 /*
   1276  * Wait interruptibly for an exclusive lock.
   1277  *
   1278  * XXX
   1279  * Several drivers do this; it should be abstracted and made MP-safe.
   1280  */
   1281 static int
   1282 vndlock(sc)
   1283 	struct vnd_softc *sc;
   1284 {
   1285 	int error;
   1286 
   1287 	while ((sc->sc_flags & VNF_LOCKED) != 0) {
   1288 		sc->sc_flags |= VNF_WANTED;
   1289 		if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
   1290 			return (error);
   1291 	}
   1292 	sc->sc_flags |= VNF_LOCKED;
   1293 	return (0);
   1294 }
   1295 
   1296 /*
   1297  * Unlock and wake up any waiters.
   1298  */
   1299 static void
   1300 vndunlock(sc)
   1301 	struct vnd_softc *sc;
   1302 {
   1303 
   1304 	sc->sc_flags &= ~VNF_LOCKED;
   1305 	if ((sc->sc_flags & VNF_WANTED) != 0) {
   1306 		sc->sc_flags &= ~VNF_WANTED;
   1307 		wakeup(sc);
   1308 	}
   1309 }
   1310