Home | History | Annotate | Line # | Download | only in dev
vnd.c revision 1.48
      1 /*	$NetBSD: vnd.c,v 1.48 1997/10/09 08:11:12 jtc 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;
    389 
    390 		nra = 0;
    391 		VOP_LOCK(vnd->sc_vp);
    392 		error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
    393 		VOP_UNLOCK(vnd->sc_vp);
    394 
    395 		if (error == 0 && (long)nbn == -1)
    396 			error = EIO;
    397 
    398 		/*
    399 		 * If there was an error or a hole in the file...punt.
    400 		 * Note that we may have to wait for any operations
    401 		 * that we have already fired off before releasing
    402 		 * the buffer.
    403 		 *
    404 		 * XXX we could deal with holes here but it would be
    405 		 * a hassle (in the write case).
    406 		 */
    407 		if (error) {
    408 			vnx->vx_error = error;
    409 			s = splbio();
    410 			if (vnx->vx_pending == 0) {
    411 				bp->b_error = error;
    412 				bp->b_flags |= B_ERROR;
    413 				putvndxfer(vnx);
    414 				goto done;
    415 			}
    416 			splx(s);
    417 			return;
    418 		}
    419 
    420 #ifdef DEBUG
    421 		if (!dovndcluster)
    422 			nra = 0;
    423 #endif
    424 
    425 		if ((off = bn % bsize) != 0)
    426 			sz = bsize - off;
    427 		else
    428 			sz = (1 + nra) * bsize;
    429 		if (resid < sz)
    430 			sz = resid;
    431 #ifdef DEBUG
    432 		if (vnddebug & VDB_IO)
    433 			printf("vndstrategy: vp %p/%p bn 0x%x/0x%x sz 0x%x\n",
    434 			    vnd->sc_vp, vp, bn, nbn, sz);
    435 #endif
    436 
    437 		nbp = getvndbuf();
    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 		if (vp->v_type == VBLK || vp->v_type == VCHR)
    443 			nbp->vb_buf.b_dev = vp->v_rdev;
    444 		else
    445 			nbp->vb_buf.b_dev = NODEV;
    446 		nbp->vb_buf.b_data = addr;
    447 		nbp->vb_buf.b_blkno = nbn + btodb(off);
    448 		nbp->vb_buf.b_proc = bp->b_proc;
    449 		nbp->vb_buf.b_iodone = vndiodone;
    450 		nbp->vb_buf.b_vp = vp;
    451 		nbp->vb_buf.b_rcred = vnd->sc_cred;	/* XXX crdup? */
    452 		nbp->vb_buf.b_wcred = vnd->sc_cred;	/* XXX crdup? */
    453 		if (bp->b_dirtyend == 0) {
    454 			nbp->vb_buf.b_dirtyoff = 0;
    455 			nbp->vb_buf.b_dirtyend = sz;
    456 		} else {
    457 			nbp->vb_buf.b_dirtyoff =
    458 			    max(0, bp->b_dirtyoff - (bp->b_bcount - resid));
    459 			nbp->vb_buf.b_dirtyend =
    460 			    min(sz,
    461 				max(0, bp->b_dirtyend - (bp->b_bcount-resid)));
    462 		}
    463 		if (bp->b_validend == 0) {
    464 			nbp->vb_buf.b_validoff = 0;
    465 			nbp->vb_buf.b_validend = sz;
    466 		} else {
    467 			nbp->vb_buf.b_validoff =
    468 			    max(0, bp->b_validoff - (bp->b_bcount - resid));
    469 			nbp->vb_buf.b_validend =
    470 			    min(sz,
    471 				max(0, bp->b_validend - (bp->b_bcount-resid)));
    472 		}
    473 
    474 		nbp->vb_xfer = vnx;
    475 
    476 		/*
    477 		 * Just sort by block number
    478 		 */
    479 		nbp->vb_buf.b_cylin = nbp->vb_buf.b_blkno;
    480 		s = splbio();
    481 		vnx->vx_pending++;
    482 		disksort(&vnd->sc_tab, &nbp->vb_buf);
    483 		if (vnd->sc_tab.b_active < vnd->sc_maxactive) {
    484 			vnd->sc_tab.b_active++;
    485 			vndstart(vnd);
    486 		}
    487 		splx(s);
    488 		bn += sz;
    489 		addr += sz;
    490 	}
    491 	return;
    492 
    493  done:
    494 	biodone(bp);
    495 }
    496 
    497 /*
    498  * Feed requests sequentially.
    499  * We do it this way to keep from flooding NFS servers if we are connected
    500  * to an NFS file.  This places the burden on the client rather than the
    501  * server.
    502  */
    503 void
    504 vndstart(vnd)
    505 	register struct vnd_softc *vnd;
    506 {
    507 	register struct buf *bp;
    508 
    509 	/*
    510 	 * Dequeue now since lower level strategy routine might
    511 	 * queue using same links
    512 	 */
    513 	bp = vnd->sc_tab.b_actf;
    514 	vnd->sc_tab.b_actf = bp->b_actf;
    515 #ifdef DEBUG
    516 	if (vnddebug & VDB_IO)
    517 		printf("vndstart(%ld): bp %p vp %p blkno 0x%x addr %p cnt 0x%lx\n",
    518 		    (long) (vnd-vnd_softc), bp, bp->b_vp, bp->b_blkno,
    519 		    bp->b_data, bp->b_bcount);
    520 #endif
    521 
    522 	/* Instrumentation. */
    523 	disk_busy(&vnd->sc_dkdev);
    524 
    525 	if ((bp->b_flags & B_READ) == 0)
    526 		bp->b_vp->v_numoutput++;
    527 	VOP_STRATEGY(bp);
    528 }
    529 
    530 void
    531 vndiodone(bp)
    532 	struct buf *bp;
    533 {
    534 	register struct vndbuf *vbp = (struct vndbuf *) bp;
    535 	register struct vndxfer *vnx = (struct vndxfer *)vbp->vb_xfer;
    536 	register struct buf *pbp = vnx->vx_bp;
    537 	register struct vnd_softc *vnd = &vnd_softc[vndunit(pbp->b_dev)];
    538 	int s, resid;
    539 
    540 	s = splbio();
    541 #ifdef DEBUG
    542 	if (vnddebug & VDB_IO)
    543 		printf("vndiodone(%ld): vbp %p vp %p blkno 0x%x addr %p cnt 0x%lx\n",
    544 		    (long) (vnd-vnd_softc), vbp, vbp->vb_buf.b_vp,
    545 		    vbp->vb_buf.b_blkno, vbp->vb_buf.b_data,
    546 		    vbp->vb_buf.b_bcount);
    547 #endif
    548 
    549 	resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
    550 	pbp->b_resid -= resid;
    551 	disk_unbusy(&vnd->sc_dkdev, resid);
    552 	vnx->vx_pending--;
    553 
    554 	if (vbp->vb_buf.b_error) {
    555 #ifdef DEBUG
    556 		if (vnddebug & VDB_IO)
    557 			printf("vndiodone: vbp %p error %d\n", vbp,
    558 			    vbp->vb_buf.b_error);
    559 #endif
    560 		vnx->vx_error = vbp->vb_buf.b_error;
    561 	}
    562 	putvndbuf(vbp);
    563 
    564 	/*
    565 	 * Wrap up this transaction if it has run to completion or, in
    566 	 * case of an error, when all auxiliary buffers have returned.
    567 	 */
    568 	if (pbp->b_resid == 0 || (vnx->vx_error && vnx->vx_pending == 0)) {
    569 
    570 		if (vnx->vx_error != 0) {
    571 			pbp->b_flags |= B_ERROR;
    572 			pbp->b_error = vnx->vx_error;
    573 		}
    574 		putvndxfer(vnx);
    575 #ifdef DEBUG
    576 		if (vnddebug & VDB_IO)
    577 			printf("vndiodone: pbp %p iodone\n", pbp);
    578 #endif
    579 		biodone(pbp);
    580 	}
    581 
    582 	if (vnd->sc_tab.b_actf)
    583 		vndstart(vnd);
    584 	else
    585 		vnd->sc_tab.b_active--;
    586 	splx(s);
    587 }
    588 
    589 /* ARGSUSED */
    590 int
    591 vndread(dev, uio, flags)
    592 	dev_t dev;
    593 	struct uio *uio;
    594 	int flags;
    595 {
    596 	int unit = vndunit(dev);
    597 	struct vnd_softc *sc;
    598 
    599 #ifdef DEBUG
    600 	if (vnddebug & VDB_FOLLOW)
    601 		printf("vndread(0x%x, %p)\n", dev, uio);
    602 #endif
    603 
    604 	if (unit >= numvnd)
    605 		return (ENXIO);
    606 	sc = &vnd_softc[unit];
    607 
    608 	if ((sc->sc_flags & VNF_INITED) == 0)
    609 		return (ENXIO);
    610 
    611 	return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
    612 }
    613 
    614 /* ARGSUSED */
    615 int
    616 vndwrite(dev, uio, flags)
    617 	dev_t dev;
    618 	struct uio *uio;
    619 	int flags;
    620 {
    621 	int unit = vndunit(dev);
    622 	struct vnd_softc *sc;
    623 
    624 #ifdef DEBUG
    625 	if (vnddebug & VDB_FOLLOW)
    626 		printf("vndwrite(0x%x, %p)\n", dev, uio);
    627 #endif
    628 
    629 	if (unit >= numvnd)
    630 		return (ENXIO);
    631 	sc = &vnd_softc[unit];
    632 
    633 	if ((sc->sc_flags & VNF_INITED) == 0)
    634 		return (ENXIO);
    635 
    636 	return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
    637 }
    638 
    639 /* ARGSUSED */
    640 int
    641 vndioctl(dev, cmd, data, flag, p)
    642 	dev_t dev;
    643 	u_long cmd;
    644 	caddr_t data;
    645 	int flag;
    646 	struct proc *p;
    647 {
    648 	int unit = vndunit(dev);
    649 	register struct vnd_softc *vnd;
    650 	struct vnd_ioctl *vio;
    651 	struct vattr vattr;
    652 	struct nameidata nd;
    653 	int error, part, pmask;
    654 	size_t geomsize;
    655 
    656 #ifdef DEBUG
    657 	if (vnddebug & VDB_FOLLOW)
    658 		printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
    659 		    dev, cmd, data, flag, p, unit);
    660 #endif
    661 	error = suser(p->p_ucred, &p->p_acflag);
    662 	if (error)
    663 		return (error);
    664 	if (unit >= numvnd)
    665 		return (ENXIO);
    666 
    667 	vnd = &vnd_softc[unit];
    668 	vio = (struct vnd_ioctl *)data;
    669 
    670 	/* Must be open for writes for these commands... */
    671 	switch (cmd) {
    672 	case VNDIOCSET:
    673 	case VNDIOCCLR:
    674 	case DIOCSDINFO:
    675 	case DIOCWDINFO:
    676 	case DIOCWLABEL:
    677 		if ((flag & FWRITE) == 0)
    678 			return (EBADF);
    679 	}
    680 
    681 	/* Must be initialized for these... */
    682 	switch (cmd) {
    683 	case VNDIOCCLR:
    684 	case DIOCGDINFO:
    685 	case DIOCSDINFO:
    686 	case DIOCWDINFO:
    687 	case DIOCGPART:
    688 	case DIOCWLABEL:
    689 	case DIOCGDEFLABEL:
    690 		if ((vnd->sc_flags & VNF_INITED) == 0)
    691 			return (ENXIO);
    692 	}
    693 
    694 	switch (cmd) {
    695 	case VNDIOCSET:
    696 		if (vnd->sc_flags & VNF_INITED)
    697 			return (EBUSY);
    698 
    699 		if ((error = vndlock(vnd)) != 0)
    700 			return (error);
    701 
    702 		/*
    703 		 * Always open for read and write.
    704 		 * This is probably bogus, but it lets vn_open()
    705 		 * weed out directories, sockets, etc. so we don't
    706 		 * have to worry about them.
    707 		 */
    708 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, p);
    709 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    710 			vndunlock(vnd);
    711 			return(error);
    712 		}
    713 		error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p);
    714 		if (error) {
    715 			VOP_UNLOCK(nd.ni_vp);
    716 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
    717 			vndunlock(vnd);
    718 			return(error);
    719 		}
    720 		VOP_UNLOCK(nd.ni_vp);
    721 		vnd->sc_vp = nd.ni_vp;
    722 		vnd->sc_size = btodb(vattr.va_size);	/* note truncation */
    723 
    724 		/*
    725 		 * Use pseudo-geometry specified.  If none was provided,
    726 		 * use "standard" Adaptec fictitious geometry.
    727 		 */
    728 		if (vio->vnd_flags & VNDIOF_HASGEOM) {
    729 
    730 			bcopy(&vio->vnd_geom, &vnd->sc_geom,
    731 			    sizeof(vio->vnd_geom));
    732 
    733 			/*
    734 			 * Sanity-check the sector size.
    735 			 * XXX Don't allow secsize < DEV_BSIZE.  Should
    736 			 * XXX we?
    737 			 */
    738 			if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
    739 			    (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0) {
    740 				(void) vn_close(nd.ni_vp, FREAD|FWRITE,
    741 				    p->p_ucred, p);
    742 				vndunlock(vnd);
    743 				return (EINVAL);
    744 			}
    745 
    746 			/*
    747 			 * Compute the size (in DEV_BSIZE blocks) specified
    748 			 * by the geometry.
    749 			 */
    750 			geomsize = (vnd->sc_geom.vng_nsectors *
    751 			    vnd->sc_geom.vng_ntracks *
    752 			    vnd->sc_geom.vng_ncylinders) *
    753 			    (vnd->sc_geom.vng_secsize / DEV_BSIZE);
    754 
    755 			/*
    756 			 * Sanity-check the size against the specified
    757 			 * geometry.
    758 			 */
    759 			if (vnd->sc_size < geomsize) {
    760 				(void) vn_close(nd.ni_vp, FREAD|FWRITE,
    761 				    p->p_ucred, p);
    762 				vndunlock(vnd);
    763 				return (EINVAL);
    764 			}
    765 		} else {
    766 			/*
    767 			 * Size must be at least 2048 DEV_BSIZE blocks
    768 			 * (1M) in order to use this geometry.
    769 			 */
    770 			if (vnd->sc_size < (32 * 64)) {
    771 				vndunlock(vnd);
    772 				return (EINVAL);
    773 			}
    774 
    775 			vnd->sc_geom.vng_secsize = DEV_BSIZE;
    776 			vnd->sc_geom.vng_nsectors = 32;
    777 			vnd->sc_geom.vng_ntracks = 64;
    778 			vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
    779 
    780 			/*
    781 			 * Compute the actual size allowed by this geometry.
    782 			 */
    783 			geomsize = 32 * 64 * vnd->sc_geom.vng_ncylinders;
    784 		}
    785 
    786 		/*
    787 		 * Truncate the size to that specified by
    788 		 * the geometry.
    789 		 * XXX Should we even bother with this?
    790 		 */
    791 		vnd->sc_size = geomsize;
    792 
    793 		if ((error = vndsetcred(vnd, p->p_ucred)) != 0) {
    794 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
    795 			vndunlock(vnd);
    796 			return(error);
    797 		}
    798 		vndthrottle(vnd, vnd->sc_vp);
    799 		vio->vnd_size = dbtob(vnd->sc_size);
    800 		vnd->sc_flags |= VNF_INITED;
    801 #ifdef DEBUG
    802 		if (vnddebug & VDB_INIT)
    803 			printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
    804 			    vnd->sc_vp, (unsigned long) vnd->sc_size,
    805 			    vnd->sc_geom.vng_secsize,
    806 			    vnd->sc_geom.vng_nsectors,
    807 			    vnd->sc_geom.vng_ntracks,
    808 			    vnd->sc_geom.vng_ncylinders);
    809 #endif
    810 
    811 		/* Attach the disk. */
    812 		bzero(vnd->sc_xname, sizeof(vnd->sc_xname));	/* XXX */
    813 		sprintf(vnd->sc_xname, "vnd%d", unit);		/* XXX */
    814 		vnd->sc_dkdev.dk_name = vnd->sc_xname;
    815 		disk_attach(&vnd->sc_dkdev);
    816 
    817 		/* Try and read the disklabel. */
    818 		vndgetdisklabel(dev);
    819 
    820 		vndunlock(vnd);
    821 
    822 		break;
    823 
    824 	case VNDIOCCLR:
    825 		if ((error = vndlock(vnd)) != 0)
    826 			return (error);
    827 
    828 		/*
    829 		 * Don't unconfigure if any other partitions are open
    830 		 * or if both the character and block flavors of this
    831 		 * partition are open.
    832 		 */
    833 		part = DISKPART(dev);
    834 		pmask = (1 << part);
    835 		if ((vnd->sc_dkdev.dk_openmask & ~pmask) ||
    836 		    ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
    837 		    (vnd->sc_dkdev.dk_copenmask & pmask))) {
    838 			vndunlock(vnd);
    839 			return (EBUSY);
    840 		}
    841 
    842 		vndclear(vnd);
    843 #ifdef DEBUG
    844 		if (vnddebug & VDB_INIT)
    845 			printf("vndioctl: CLRed\n");
    846 #endif
    847 
    848 		/* Detatch the disk. */
    849 		disk_detach(&vnd->sc_dkdev);
    850 
    851 		vndunlock(vnd);
    852 
    853 		break;
    854 
    855 	case DIOCGDINFO:
    856 		*(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
    857 		break;
    858 
    859 	case DIOCGPART:
    860 		((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
    861 		((struct partinfo *)data)->part =
    862 		    &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
    863 		break;
    864 
    865 	case DIOCWDINFO:
    866 	case DIOCSDINFO:
    867 		if ((error = vndlock(vnd)) != 0)
    868 			return (error);
    869 
    870 		vnd->sc_flags |= VNF_LABELLING;
    871 
    872 		error = setdisklabel(vnd->sc_dkdev.dk_label,
    873 		    (struct disklabel *)data, 0, vnd->sc_dkdev.dk_cpulabel);
    874 		if (error == 0) {
    875 			if (cmd == DIOCWDINFO)
    876 				error = writedisklabel(VNDLABELDEV(dev),
    877 				    vndstrategy, vnd->sc_dkdev.dk_label,
    878 				    vnd->sc_dkdev.dk_cpulabel);
    879 		}
    880 
    881 		vnd->sc_flags &= ~VNF_LABELLING;
    882 
    883 		vndunlock(vnd);
    884 
    885 		if (error)
    886 			return (error);
    887 		break;
    888 
    889 	case DIOCWLABEL:
    890 		if (*(int *)data != 0)
    891 			vnd->sc_flags |= VNF_WLABEL;
    892 		else
    893 			vnd->sc_flags &= ~VNF_WLABEL;
    894 		break;
    895 
    896 	case DIOCGDEFLABEL:
    897 		vndgetdefaultlabel(vnd, (struct disklabel *)data);
    898 		break;
    899 
    900 	default:
    901 		return (ENOTTY);
    902 	}
    903 
    904 	return (0);
    905 }
    906 
    907 /*
    908  * Duplicate the current processes' credentials.  Since we are called only
    909  * as the result of a SET ioctl and only root can do that, any future access
    910  * to this "disk" is essentially as root.  Note that credentials may change
    911  * if some other uid can write directly to the mapped file (NFS).
    912  */
    913 int
    914 vndsetcred(vnd, cred)
    915 	register struct vnd_softc *vnd;
    916 	struct ucred *cred;
    917 {
    918 	struct uio auio;
    919 	struct iovec aiov;
    920 	char *tmpbuf;
    921 	int error;
    922 
    923 	vnd->sc_cred = crdup(cred);
    924 	tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
    925 
    926 	/* XXX: Horrible kludge to establish credentials for NFS */
    927 	aiov.iov_base = tmpbuf;
    928 	aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
    929 	auio.uio_iov = &aiov;
    930 	auio.uio_iovcnt = 1;
    931 	auio.uio_offset = 0;
    932 	auio.uio_rw = UIO_READ;
    933 	auio.uio_segflg = UIO_SYSSPACE;
    934 	auio.uio_resid = aiov.iov_len;
    935 	VOP_LOCK(vnd->sc_vp);
    936 	error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
    937 	VOP_UNLOCK(vnd->sc_vp);
    938 
    939 	free(tmpbuf, M_TEMP);
    940 	return (error);
    941 }
    942 
    943 /*
    944  * Set maxactive based on FS type
    945  */
    946 void
    947 vndthrottle(vnd, vp)
    948 	register struct vnd_softc *vnd;
    949 	struct vnode *vp;
    950 {
    951 #ifdef NFS
    952 	extern int (**nfsv2_vnodeop_p) __P((void *));
    953 
    954 	if (vp->v_op == nfsv2_vnodeop_p)
    955 		vnd->sc_maxactive = 2;
    956 	else
    957 #endif
    958 		vnd->sc_maxactive = 8;
    959 
    960 	if (vnd->sc_maxactive < 1)
    961 		vnd->sc_maxactive = 1;
    962 }
    963 
    964 void
    965 vndshutdown()
    966 {
    967 	register struct vnd_softc *vnd;
    968 
    969 	for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
    970 		if (vnd->sc_flags & VNF_INITED)
    971 			vndclear(vnd);
    972 }
    973 
    974 void
    975 vndclear(vnd)
    976 	register struct vnd_softc *vnd;
    977 {
    978 	register struct vnode *vp = vnd->sc_vp;
    979 	struct proc *p = curproc;		/* XXX */
    980 
    981 #ifdef DEBUG
    982 	if (vnddebug & VDB_FOLLOW)
    983 		printf("vndclear(%p): vp %p\n", vnd, vp);
    984 #endif
    985 	vnd->sc_flags &= ~VNF_INITED;
    986 	if (vp == (struct vnode *)0)
    987 		panic("vndioctl: null vp");
    988 	(void) vn_close(vp, FREAD|FWRITE, vnd->sc_cred, p);
    989 	crfree(vnd->sc_cred);
    990 	vnd->sc_vp = (struct vnode *)0;
    991 	vnd->sc_cred = (struct ucred *)0;
    992 	vnd->sc_size = 0;
    993 }
    994 
    995 int
    996 vndsize(dev)
    997 	dev_t dev;
    998 {
    999 	struct vnd_softc *sc;
   1000 	struct disklabel *lp;
   1001 	int part, unit, omask;
   1002 	int size;
   1003 
   1004 	unit = vndunit(dev);
   1005 	if (unit >= numvnd)
   1006 		return (-1);
   1007 	sc = &vnd_softc[unit];
   1008 
   1009 	if ((sc->sc_flags & VNF_INITED) == 0)
   1010 		return (-1);
   1011 
   1012 	part = DISKPART(dev);
   1013 	omask = sc->sc_dkdev.dk_openmask & (1 << part);
   1014 	lp = sc->sc_dkdev.dk_label;
   1015 
   1016 	if (omask == 0 && vndopen(dev, 0, S_IFBLK, curproc))
   1017 		return (-1);
   1018 
   1019 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
   1020 		size = -1;
   1021 	else
   1022 		size = lp->d_partitions[part].p_size *
   1023 		    (lp->d_secsize / DEV_BSIZE);
   1024 
   1025 	if (omask == 0 && vndclose(dev, 0, S_IFBLK, curproc))
   1026 		return (-1);
   1027 
   1028 	return (size);
   1029 }
   1030 
   1031 int
   1032 vnddump(dev, blkno, va, size)
   1033 	dev_t dev;
   1034 	daddr_t blkno;
   1035 	caddr_t va;
   1036 	size_t size;
   1037 {
   1038 
   1039 	/* Not implemented. */
   1040 	return ENXIO;
   1041 }
   1042 
   1043 void
   1044 vndgetdefaultlabel(sc, lp)
   1045 	struct vnd_softc *sc;
   1046 	struct disklabel *lp;
   1047 {
   1048 	struct vndgeom *vng = &sc->sc_geom;
   1049 	struct partition *pp;
   1050 
   1051 	bzero(lp, sizeof(*lp));
   1052 
   1053 	lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
   1054 	lp->d_secsize = vng->vng_secsize;
   1055 	lp->d_nsectors = vng->vng_nsectors;
   1056 	lp->d_ntracks = vng->vng_ntracks;
   1057 	lp->d_ncylinders = vng->vng_ncylinders;
   1058 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1059 
   1060 	strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
   1061 	lp->d_type = DTYPE_VND;
   1062 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1063 	lp->d_rpm = 3600;
   1064 	lp->d_interleave = 1;
   1065 	lp->d_flags = 0;
   1066 
   1067 	pp = &lp->d_partitions[RAW_PART];
   1068 	pp->p_offset = 0;
   1069 	pp->p_size = lp->d_secperunit;
   1070 	pp->p_fstype = FS_UNUSED;
   1071 	lp->d_npartitions = RAW_PART + 1;
   1072 
   1073 	lp->d_magic = DISKMAGIC;
   1074 	lp->d_magic2 = DISKMAGIC;
   1075 	lp->d_checksum = dkcksum(lp);
   1076 }
   1077 
   1078 /*
   1079  * Read the disklabel from a vnd.  If one is not present, create a fake one.
   1080  */
   1081 void
   1082 vndgetdisklabel(dev)
   1083 	dev_t dev;
   1084 {
   1085 	struct vnd_softc *sc = &vnd_softc[vndunit(dev)];
   1086 	char *errstring;
   1087 	struct disklabel *lp = sc->sc_dkdev.dk_label;
   1088 	struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
   1089 	int i;
   1090 
   1091 	bzero(clp, sizeof(*clp));
   1092 
   1093 	vndgetdefaultlabel(sc, lp);
   1094 
   1095 	/*
   1096 	 * Call the generic disklabel extraction routine.
   1097 	 */
   1098 	errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
   1099 	if (errstring) {
   1100 		/*
   1101 		 * Lack of disklabel is common, but we print the warning
   1102 		 * anyway, since it might contain other useful information.
   1103 		 */
   1104 		printf("%s: %s\n", sc->sc_xname, errstring);
   1105 
   1106 		/*
   1107 		 * For historical reasons, if there's no disklabel
   1108 		 * present, all partitions must be FS_BSDFFS and
   1109 		 * occupy the entire disk.
   1110 		 */
   1111 		for (i = 0; i < MAXPARTITIONS; i++) {
   1112 			lp->d_partitions[i].p_size = lp->d_secperunit;
   1113 			lp->d_partitions[i].p_offset = 0;
   1114 			lp->d_partitions[i].p_fstype = FS_BSDFFS;
   1115 		}
   1116 
   1117 		strncpy(lp->d_packname, "default label",
   1118 		    sizeof(lp->d_packname));
   1119 
   1120 		lp->d_checksum = dkcksum(lp);
   1121 	}
   1122 }
   1123 
   1124 /*
   1125  * Wait interruptibly for an exclusive lock.
   1126  *
   1127  * XXX
   1128  * Several drivers do this; it should be abstracted and made MP-safe.
   1129  */
   1130 static int
   1131 vndlock(sc)
   1132 	struct vnd_softc *sc;
   1133 {
   1134 	int error;
   1135 
   1136 	while ((sc->sc_flags & VNF_LOCKED) != 0) {
   1137 		sc->sc_flags |= VNF_WANTED;
   1138 		if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
   1139 			return (error);
   1140 	}
   1141 	sc->sc_flags |= VNF_LOCKED;
   1142 	return (0);
   1143 }
   1144 
   1145 /*
   1146  * Unlock and wake up any waiters.
   1147  */
   1148 static void
   1149 vndunlock(sc)
   1150 	struct vnd_softc *sc;
   1151 {
   1152 
   1153 	sc->sc_flags &= ~VNF_LOCKED;
   1154 	if ((sc->sc_flags & VNF_WANTED) != 0) {
   1155 		sc->sc_flags &= ~VNF_WANTED;
   1156 		wakeup(sc);
   1157 	}
   1158 }
   1159