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