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