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