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