Home | History | Annotate | Line # | Download | only in dev
vnd.c revision 1.146
      1 /*	$NetBSD: vnd.c,v 1.146 2006/03/30 15:19:45 cube Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1990, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * the Systems Programming Group of the University of Utah Computer
     45  * Science Department.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  * from: Utah $Hdr: vn.c 1.13 94/04/02$
     72  *
     73  *	@(#)vn.c	8.9 (Berkeley) 5/14/95
     74  */
     75 
     76 /*
     77  * Copyright (c) 1988 University of Utah.
     78  *
     79  * This code is derived from software contributed to Berkeley by
     80  * the Systems Programming Group of the University of Utah Computer
     81  * Science Department.
     82  *
     83  * Redistribution and use in source and binary forms, with or without
     84  * modification, are permitted provided that the following conditions
     85  * are met:
     86  * 1. Redistributions of source code must retain the above copyright
     87  *    notice, this list of conditions and the following disclaimer.
     88  * 2. Redistributions in binary form must reproduce the above copyright
     89  *    notice, this list of conditions and the following disclaimer in the
     90  *    documentation and/or other materials provided with the distribution.
     91  * 3. All advertising materials mentioning features or use of this software
     92  *    must display the following acknowledgement:
     93  *	This product includes software developed by the University of
     94  *	California, Berkeley and its contributors.
     95  * 4. Neither the name of the University nor the names of its contributors
     96  *    may be used to endorse or promote products derived from this software
     97  *    without specific prior written permission.
     98  *
     99  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    100  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    101  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    102  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    103  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    104  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    105  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    106  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    107  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    108  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    109  * SUCH DAMAGE.
    110  *
    111  * from: Utah $Hdr: vn.c 1.13 94/04/02$
    112  *
    113  *	@(#)vn.c	8.9 (Berkeley) 5/14/95
    114  */
    115 
    116 /*
    117  * Vnode disk driver.
    118  *
    119  * Block/character interface to a vnode.  Allows one to treat a file
    120  * as a disk (e.g. build a filesystem in it, mount it, etc.).
    121  *
    122  * NOTE 1: This uses the VOP_BMAP/VOP_STRATEGY interface to the vnode
    123  * instead of a simple VOP_RDWR.  We do this to avoid distorting the
    124  * local buffer cache.
    125  *
    126  * NOTE 2: There is a security issue involved with this driver.
    127  * Once mounted all access to the contents of the "mapped" file via
    128  * the special file is controlled by the permissions on the special
    129  * file, the protection of the mapped file is ignored (effectively,
    130  * by using root credentials in all transactions).
    131  *
    132  * NOTE 3: Doesn't interact with leases, should it?
    133  */
    134 
    135 #include <sys/cdefs.h>
    136 __KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.146 2006/03/30 15:19:45 cube Exp $");
    137 
    138 #if defined(_KERNEL_OPT)
    139 #include "fs_nfs.h"
    140 #include "opt_vnd.h"
    141 #endif
    142 
    143 #include <sys/param.h>
    144 #include <sys/systm.h>
    145 #include <sys/namei.h>
    146 #include <sys/proc.h>
    147 #include <sys/kthread.h>
    148 #include <sys/errno.h>
    149 #include <sys/buf.h>
    150 #include <sys/bufq.h>
    151 #include <sys/malloc.h>
    152 #include <sys/ioctl.h>
    153 #include <sys/disklabel.h>
    154 #include <sys/device.h>
    155 #include <sys/disk.h>
    156 #include <sys/stat.h>
    157 #include <sys/mount.h>
    158 #include <sys/vnode.h>
    159 #include <sys/file.h>
    160 #include <sys/uio.h>
    161 #include <sys/conf.h>
    162 #include <net/zlib.h>
    163 
    164 #include <miscfs/specfs/specdev.h>
    165 
    166 #include <dev/vndvar.h>
    167 
    168 #if defined(VNDDEBUG) && !defined(DEBUG)
    169 #define DEBUG
    170 #endif
    171 
    172 #ifdef DEBUG
    173 int dovndcluster = 1;
    174 #define VDB_FOLLOW	0x01
    175 #define VDB_INIT	0x02
    176 #define VDB_IO		0x04
    177 #define VDB_LABEL	0x08
    178 int vnddebug = 0x00;
    179 #endif
    180 
    181 #define vndunit(x)	DISKUNIT(x)
    182 
    183 struct vndxfer {
    184 	struct buf vx_buf;
    185 	struct vnd_softc *vx_vnd;
    186 };
    187 #define	VND_BUFTOXFER(bp)	((struct vndxfer *)(void *)bp)
    188 
    189 #define VND_GETXFER(vnd)	pool_get(&(vnd)->sc_vxpool, PR_WAITOK)
    190 #define VND_PUTXFER(vnd, vx)	pool_put(&(vnd)->sc_vxpool, (vx))
    191 
    192 #define VNDLABELDEV(dev) \
    193     (MAKEDISKDEV(major((dev)), vndunit((dev)), RAW_PART))
    194 
    195 /* called by main() at boot time (XXX: and the LKM driver) */
    196 void	vndattach(int);
    197 
    198 static void	vndclear(struct vnd_softc *, int);
    199 static int	vndsetcred(struct vnd_softc *, struct ucred *);
    200 static void	vndthrottle(struct vnd_softc *, struct vnode *);
    201 static void	vndiodone(struct buf *);
    202 #if 0
    203 static void	vndshutdown(void);
    204 #endif
    205 
    206 static void	vndgetdefaultlabel(struct vnd_softc *, struct disklabel *);
    207 static void	vndgetdisklabel(dev_t, struct vnd_softc *);
    208 
    209 static int	vndlock(struct vnd_softc *);
    210 static void	vndunlock(struct vnd_softc *);
    211 #ifdef VND_COMPRESSION
    212 static void	compstrategy(struct buf *, off_t);
    213 static void	*vnd_alloc(void *, u_int, u_int);
    214 static void	vnd_free(void *, void *);
    215 #endif /* VND_COMPRESSION */
    216 
    217 static void vndthread(void *);
    218 
    219 static dev_type_open(vndopen);
    220 static dev_type_close(vndclose);
    221 static dev_type_read(vndread);
    222 static dev_type_write(vndwrite);
    223 static dev_type_ioctl(vndioctl);
    224 static dev_type_strategy(vndstrategy);
    225 static dev_type_dump(vnddump);
    226 static dev_type_size(vndsize);
    227 
    228 const struct bdevsw vnd_bdevsw = {
    229 	vndopen, vndclose, vndstrategy, vndioctl, vnddump, vndsize, D_DISK
    230 };
    231 
    232 const struct cdevsw vnd_cdevsw = {
    233 	vndopen, vndclose, vndread, vndwrite, vndioctl,
    234 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    235 };
    236 
    237 static int	vnd_match(struct device *, struct cfdata *, void *);
    238 static void	vnd_attach(struct device *, struct device *, void *);
    239 static int	vnd_detach(struct device *, int);
    240 
    241 CFATTACH_DECL(vnd, sizeof(struct vnd_softc),
    242     vnd_match, vnd_attach, vnd_detach, NULL);
    243 extern struct cfdriver vnd_cd;
    244 
    245 static struct vnd_softc	*vnd_spawn(int);
    246 int	vnd_destroy(struct device *);
    247 
    248 void
    249 vndattach(int num)
    250 {
    251 	int error;
    252 
    253 	error = config_cfattach_attach(vnd_cd.cd_name, &vnd_ca);
    254 	if (error)
    255 		aprint_error("%s: unable to register cfattach\n",
    256 		    vnd_cd.cd_name);
    257 }
    258 
    259 static int
    260 vnd_match(struct device *self, struct cfdata *cfdata, void *aux)
    261 {
    262 	return 1;
    263 }
    264 
    265 static void
    266 vnd_attach(struct device *parent, struct device *self, void *aux)
    267 {
    268 	struct vnd_softc *sc = (struct vnd_softc *)self;
    269 
    270 	sc->sc_comp_offsets = NULL;
    271 	sc->sc_comp_buff = NULL;
    272 	sc->sc_comp_decombuf = NULL;
    273 	bufq_alloc(&sc->sc_tab, "disksort", BUFQ_SORT_RAWBLOCK);
    274 	pseudo_disk_init(&sc->sc_dkdev);
    275 }
    276 
    277 static int
    278 vnd_detach(struct device *self, int flags)
    279 {
    280 	struct vnd_softc *sc = (struct vnd_softc *)self;
    281 	if (sc->sc_flags & VNF_INITED)
    282 		return EBUSY;
    283 
    284 	bufq_free(sc->sc_tab);
    285 
    286 	return 0;
    287 }
    288 
    289 static struct vnd_softc *
    290 vnd_spawn(int unit)
    291 {
    292 	struct cfdata *cf;
    293 
    294 	cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
    295 	cf->cf_name = vnd_cd.cd_name;
    296 	cf->cf_atname = vnd_cd.cd_name;
    297 	cf->cf_unit = unit;
    298 	cf->cf_fstate = FSTATE_STAR;
    299 
    300 	return (struct vnd_softc *)config_attach_pseudo(cf);
    301 }
    302 
    303 int
    304 vnd_destroy(struct device *dev)
    305 {
    306 	int error;
    307 	struct cfdata *cf;
    308 
    309 	cf = device_cfdata(dev);
    310 	error = config_detach(dev, DETACH_QUIET);
    311 	if (error)
    312 		return error;
    313 	free(cf, M_DEVBUF);
    314 	return 0;
    315 }
    316 
    317 static int
    318 vndopen(dev_t dev, int flags, int mode, struct lwp *l)
    319 {
    320 	int unit = vndunit(dev);
    321 	struct vnd_softc *sc;
    322 	int error = 0, part, pmask;
    323 	struct disklabel *lp;
    324 
    325 #ifdef DEBUG
    326 	if (vnddebug & VDB_FOLLOW)
    327 		printf("vndopen(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, l);
    328 #endif
    329 	sc = device_lookup(&vnd_cd, unit);
    330 	if (sc == NULL) {
    331 		sc = vnd_spawn(unit);
    332 		if (sc == NULL)
    333 			return ENOMEM;
    334 	}
    335 
    336 	if ((error = vndlock(sc)) != 0)
    337 		return (error);
    338 
    339 	lp = sc->sc_dkdev.dk_label;
    340 
    341 	part = DISKPART(dev);
    342 	pmask = (1 << part);
    343 
    344 	/*
    345 	 * If we're initialized, check to see if there are any other
    346 	 * open partitions.  If not, then it's safe to update the
    347 	 * in-core disklabel.  Only read the disklabel if it is
    348 	 * not already valid.
    349 	 */
    350 	if ((sc->sc_flags & (VNF_INITED|VNF_VLABEL)) == VNF_INITED &&
    351 	    sc->sc_dkdev.dk_openmask == 0)
    352 		vndgetdisklabel(dev, sc);
    353 
    354 	/* Check that the partitions exists. */
    355 	if (part != RAW_PART) {
    356 		if (((sc->sc_flags & VNF_INITED) == 0) ||
    357 		    ((part >= lp->d_npartitions) ||
    358 		     (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
    359 			error = ENXIO;
    360 			goto done;
    361 		}
    362 	}
    363 
    364 	/* Prevent our unit from being unconfigured while open. */
    365 	switch (mode) {
    366 	case S_IFCHR:
    367 		sc->sc_dkdev.dk_copenmask |= pmask;
    368 		break;
    369 
    370 	case S_IFBLK:
    371 		sc->sc_dkdev.dk_bopenmask |= pmask;
    372 		break;
    373 	}
    374 	sc->sc_dkdev.dk_openmask =
    375 	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
    376 
    377  done:
    378 	vndunlock(sc);
    379 	return (error);
    380 }
    381 
    382 static int
    383 vndclose(dev_t dev, int flags, int mode, struct lwp *l)
    384 {
    385 	int unit = vndunit(dev);
    386 	struct vnd_softc *sc;
    387 	int error = 0, part;
    388 
    389 #ifdef DEBUG
    390 	if (vnddebug & VDB_FOLLOW)
    391 		printf("vndclose(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, l);
    392 #endif
    393 	sc = device_lookup(&vnd_cd, unit);
    394 	if (sc == NULL)
    395 		return ENXIO;
    396 
    397 	if ((error = vndlock(sc)) != 0)
    398 		return (error);
    399 
    400 	part = DISKPART(dev);
    401 
    402 	/* ...that much closer to allowing unconfiguration... */
    403 	switch (mode) {
    404 	case S_IFCHR:
    405 		sc->sc_dkdev.dk_copenmask &= ~(1 << part);
    406 		break;
    407 
    408 	case S_IFBLK:
    409 		sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
    410 		break;
    411 	}
    412 	sc->sc_dkdev.dk_openmask =
    413 	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
    414 
    415 	vndunlock(sc);
    416 
    417 	if ((sc->sc_flags & VNF_INITED) == 0) {
    418 		if ((error = vnd_destroy((struct device *)sc)) != 0) {
    419 			aprint_error("%s: unable to detach instance\n",
    420 			    sc->sc_dev.dv_xname);
    421 			return error;
    422 		}
    423 	}
    424 
    425 	return (0);
    426 }
    427 
    428 /*
    429  * Queue the request, and wakeup the kernel thread to handle it.
    430  */
    431 static void
    432 vndstrategy(struct buf *bp)
    433 {
    434 	int unit = vndunit(bp->b_dev);
    435 	struct vnd_softc *vnd =
    436 	    (struct vnd_softc *)device_lookup(&vnd_cd, unit);
    437 	struct disklabel *lp = vnd->sc_dkdev.dk_label;
    438 	daddr_t blkno;
    439 	int s = splbio();
    440 
    441 	bp->b_resid = bp->b_bcount;
    442 
    443 	if ((vnd->sc_flags & VNF_INITED) == 0) {
    444 		bp->b_error = ENXIO;
    445 		bp->b_flags |= B_ERROR;
    446 		goto done;
    447 	}
    448 
    449 	/*
    450 	 * The transfer must be a whole number of blocks.
    451 	 */
    452 	if ((bp->b_bcount % lp->d_secsize) != 0) {
    453 		bp->b_error = EINVAL;
    454 		bp->b_flags |= B_ERROR;
    455 		goto done;
    456 	}
    457 
    458 	/*
    459 	 * check if we're read-only.
    460 	 */
    461 	if ((vnd->sc_flags & VNF_READONLY) && !(bp->b_flags & B_READ)) {
    462 		bp->b_error = EACCES;
    463 		bp->b_flags |= B_ERROR;
    464 		goto done;
    465 	}
    466 
    467 	/*
    468 	 * Do bounds checking and adjust transfer.  If there's an error,
    469 	 * the bounds check will flag that for us.
    470 	 */
    471 	if (DISKPART(bp->b_dev) == RAW_PART) {
    472 		if (bounds_check_with_mediasize(bp, DEV_BSIZE,
    473 		    vnd->sc_size) <= 0)
    474 			goto done;
    475 	} else {
    476 		if (bounds_check_with_label(&vnd->sc_dkdev,
    477 		    bp, vnd->sc_flags & (VNF_WLABEL|VNF_LABELLING)) <= 0)
    478 			goto done;
    479 	}
    480 
    481 	/* If it's a nil transfer, wake up the top half now. */
    482 	if (bp->b_bcount == 0)
    483 		goto done;
    484 
    485 	/*
    486 	 * Put the block number in terms of the logical blocksize
    487 	 * of the "device".
    488 	 */
    489 
    490 	blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    491 
    492 	/*
    493 	 * Translate the partition-relative block number to an absolute.
    494 	 */
    495 	if (DISKPART(bp->b_dev) != RAW_PART) {
    496 		struct partition *pp;
    497 
    498 		pp = &vnd->sc_dkdev.dk_label->d_partitions[
    499 		    DISKPART(bp->b_dev)];
    500 		blkno += pp->p_offset;
    501 	}
    502 	bp->b_rawblkno = blkno;
    503 
    504 #ifdef DEBUG
    505 	if (vnddebug & VDB_FOLLOW)
    506 		printf("vndstrategy(%p): unit %d\n", bp, unit);
    507 #endif
    508 	BUFQ_PUT(vnd->sc_tab, bp);
    509 	wakeup(&vnd->sc_tab);
    510 	splx(s);
    511 	return;
    512 done:
    513 	biodone(bp);
    514 	splx(s);
    515 }
    516 
    517 static void
    518 vndthread(void *arg)
    519 {
    520 	struct vnd_softc *vnd = arg;
    521 	struct mount *mp;
    522 	int s, bsize;
    523 	int sz, error;
    524 	struct disklabel *lp;
    525 
    526 	s = splbio();
    527 	vnd->sc_flags |= VNF_KTHREAD;
    528 	wakeup(&vnd->sc_kthread);
    529 
    530 	/*
    531 	 * Dequeue requests, break them into bsize pieces and submit using
    532 	 * VOP_BMAP/VOP_STRATEGY.
    533 	 */
    534 	while ((vnd->sc_flags & VNF_VUNCONF) == 0) {
    535 		struct vndxfer *vnx;
    536 		off_t offset;
    537 		int resid;
    538 		int skipped = 0;
    539 		off_t bn;
    540 		int flags;
    541 		struct buf *obp;
    542 		struct buf *bp;
    543 
    544 		obp = BUFQ_GET(vnd->sc_tab);
    545 		if (obp == NULL) {
    546 			tsleep(&vnd->sc_tab, PRIBIO, "vndbp", 0);
    547 			continue;
    548 		};
    549 		splx(s);
    550 		flags = obp->b_flags;
    551 #ifdef DEBUG
    552 		if (vnddebug & VDB_FOLLOW)
    553 			printf("vndthread(%p\n", obp);
    554 #endif
    555 		lp = vnd->sc_dkdev.dk_label;
    556 
    557 		/* convert to a byte offset within the file. */
    558 		bn = obp->b_rawblkno * lp->d_secsize;
    559 
    560 		if (vnd->sc_vp->v_mount == NULL) {
    561 			obp->b_error = ENXIO;
    562 			obp->b_flags |= B_ERROR;
    563 			goto done;
    564 		}
    565 #ifdef VND_COMPRESSION
    566 		/* handle a compressed read */
    567 		if ((flags & B_READ) != 0 && (vnd->sc_flags & VNF_COMP)) {
    568 			compstrategy(obp, bn);
    569 			goto done;
    570 		}
    571 #endif /* VND_COMPRESSION */
    572 
    573 		bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
    574 
    575 		/*
    576 		 * Allocate a header for this transfer and link it to the
    577 		 * buffer
    578 		 */
    579 		s = splbio();
    580 		vnx = VND_GETXFER(vnd);
    581 		splx(s);
    582 		vnx->vx_vnd = vnd;
    583 
    584 		bp = &vnx->vx_buf;
    585 		BUF_INIT(bp);
    586 		bp->b_flags = (obp->b_flags & B_READ) | B_CALL;
    587 		bp->b_iodone = vndiodone;
    588 		bp->b_private = obp;
    589 		bp->b_vp = vnd->sc_vp;
    590 		bp->b_data = obp->b_data;
    591 		bp->b_bcount = bp->b_resid = obp->b_bcount;
    592 		BIO_COPYPRIO(bp, obp);
    593 
    594 		s = splbio();
    595 		while (vnd->sc_active >= vnd->sc_maxactive) {
    596 			tsleep(&vnd->sc_tab, PRIBIO, "vndac", 0);
    597 		}
    598 		vnd->sc_active++;
    599 		splx(s);
    600 
    601 		if ((flags & B_READ) == 0) {
    602 			s = splbio();
    603 			V_INCR_NUMOUTPUT(bp->b_vp);
    604 			splx(s);
    605 
    606 			vn_start_write(vnd->sc_vp, &mp, V_WAIT);
    607 		}
    608 
    609 		/* Instrumentation. */
    610 		disk_busy(&vnd->sc_dkdev);
    611 
    612 		/*
    613 		 * Feed requests sequentially.
    614 		 * We do it this way to keep from flooding NFS servers if we
    615 		 * are connected to an NFS file.  This places the burden on
    616 		 * the client rather than the server.
    617 		 */
    618 		error = 0;
    619 		for (offset = 0, resid = bp->b_resid; resid;
    620 		    resid -= sz, offset += sz) {
    621 			struct buf *nbp;
    622 			struct vnode *vp;
    623 			daddr_t nbn;
    624 			int off, nra;
    625 
    626 			nra = 0;
    627 			vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
    628 			error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
    629 			VOP_UNLOCK(vnd->sc_vp, 0);
    630 
    631 			if (error == 0 && (long)nbn == -1)
    632 				error = EIO;
    633 
    634 			/*
    635 			 * If there was an error or a hole in the file...punt.
    636 			 * Note that we may have to wait for any operations
    637 			 * that we have already fired off before releasing
    638 			 * the buffer.
    639 			 *
    640 			 * XXX we could deal with holes here but it would be
    641 			 * a hassle (in the write case).
    642 			 */
    643 			if (error) {
    644 				skipped += resid;
    645 				break;
    646 			}
    647 
    648 #ifdef DEBUG
    649 			if (!dovndcluster)
    650 				nra = 0;
    651 #endif
    652 
    653 			off = bn % bsize;
    654 			sz = MIN(((off_t)1 + nra) * bsize - off, resid);
    655 #ifdef	DEBUG
    656 			if (vnddebug & VDB_IO)
    657 				printf("vndstrategy: vp %p/%p bn 0x%qx/0x%" PRIx64
    658 				       " sz 0x%x\n",
    659 				    vnd->sc_vp, vp, (long long)bn, nbn, sz);
    660 #endif
    661 
    662 			nbp = getiobuf();
    663 			nestiobuf_setup(bp, nbp, offset, sz);
    664 			nbp->b_blkno = nbn + btodb(off);
    665 
    666 #if 0 /* XXX #ifdef DEBUG */
    667 			if (vnddebug & VDB_IO)
    668 				printf("vndstart(%ld): bp %p vp %p blkno "
    669 				    "0x%" PRIx64 " flags %x addr %p cnt 0x%x\n",
    670 				    (long) (vnd-vnd_softc), &nbp->vb_buf,
    671 				    nbp->vb_buf.b_vp, nbp->vb_buf.b_blkno,
    672 				    nbp->vb_buf.b_flags, nbp->vb_buf.b_data,
    673 				    nbp->vb_buf.b_bcount);
    674 #endif
    675 			VOP_STRATEGY(vp, nbp);
    676 			bn += sz;
    677 		}
    678 		nestiobuf_done(bp, skipped, error);
    679 
    680 		if ((flags & B_READ) == 0)
    681 			vn_finished_write(mp, 0);
    682 
    683 		s = splbio();
    684 		continue;
    685 done:
    686 		biodone(obp);
    687 		s = splbio();
    688 	}
    689 
    690 	vnd->sc_flags &= (~VNF_KTHREAD | VNF_VUNCONF);
    691 	wakeup(&vnd->sc_kthread);
    692 	splx(s);
    693 	kthread_exit(0);
    694 }
    695 
    696 static void
    697 vndiodone(struct buf *bp)
    698 {
    699 	struct vndxfer *vnx = VND_BUFTOXFER(bp);
    700 	struct vnd_softc *vnd = vnx->vx_vnd;
    701 	struct buf *obp = bp->b_private;
    702 
    703 	KASSERT(&vnx->vx_buf == bp);
    704 	KASSERT(vnd->sc_active > 0);
    705 #ifdef DEBUG
    706 	if (vnddebug & VDB_IO) {
    707 		printf("vndiodone1: bp %p iodone: error %d\n",
    708 		    bp, (bp->b_flags & B_ERROR) != 0 ? bp->b_error : 0);
    709 	}
    710 #endif
    711 	disk_unbusy(&vnd->sc_dkdev, bp->b_bcount - bp->b_resid,
    712 	    (bp->b_flags & B_READ));
    713 	vnd->sc_active--;
    714 	if (vnd->sc_active == 0) {
    715 		wakeup(&vnd->sc_tab);
    716 	}
    717 	obp->b_flags |= bp->b_flags & B_ERROR;
    718 	obp->b_error = bp->b_error;
    719 	obp->b_resid = bp->b_resid;
    720 	VND_PUTXFER(vnd, vnx);
    721 	biodone(obp);
    722 }
    723 
    724 /* ARGSUSED */
    725 static int
    726 vndread(dev_t dev, struct uio *uio, int flags)
    727 {
    728 	int unit = vndunit(dev);
    729 	struct vnd_softc *sc;
    730 
    731 #ifdef DEBUG
    732 	if (vnddebug & VDB_FOLLOW)
    733 		printf("vndread(0x%x, %p)\n", dev, uio);
    734 #endif
    735 
    736 	sc = device_lookup(&vnd_cd, unit);
    737 	if (sc == NULL)
    738 		return ENXIO;
    739 
    740 	if ((sc->sc_flags & VNF_INITED) == 0)
    741 		return (ENXIO);
    742 
    743 	return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
    744 }
    745 
    746 /* ARGSUSED */
    747 static int
    748 vndwrite(dev_t dev, struct uio *uio, int flags)
    749 {
    750 	int unit = vndunit(dev);
    751 	struct vnd_softc *sc;
    752 
    753 #ifdef DEBUG
    754 	if (vnddebug & VDB_FOLLOW)
    755 		printf("vndwrite(0x%x, %p)\n", dev, uio);
    756 #endif
    757 
    758 	sc = device_lookup(&vnd_cd, unit);
    759 	if (sc == NULL)
    760 		return ENXIO;
    761 
    762 	if ((sc->sc_flags & VNF_INITED) == 0)
    763 		return (ENXIO);
    764 
    765 	return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
    766 }
    767 
    768 static int
    769 vnd_cget(struct lwp *l, int unit, int *un, struct vattr *va)
    770 {
    771 	struct vnd_softc *vnd;
    772 
    773 	if (*un == -1)
    774 		*un = unit;
    775 	if (*un < 0)
    776 		return EINVAL;
    777 
    778 	vnd = device_lookup(&vnd_cd, *un);
    779 	if (vnd == NULL)
    780 		return (*un >= vnd_cd.cd_ndevs) ? ENXIO : -1;
    781 
    782 	if ((vnd->sc_flags & VNF_INITED) == 0)
    783 		return -1;
    784 
    785 	return VOP_GETATTR(vnd->sc_vp, va, l->l_proc->p_ucred, l);
    786 }
    787 
    788 /* ARGSUSED */
    789 static int
    790 vndioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    791 {
    792 	int unit = vndunit(dev);
    793 	struct vnd_softc *vnd;
    794 	struct vnd_ioctl *vio;
    795 	struct vattr vattr;
    796 	struct nameidata nd;
    797 	int error, part, pmask;
    798 	size_t geomsize;
    799 	struct proc *p =  (l != NULL) ? l->l_proc : NULL;
    800 	int fflags;
    801 #ifdef __HAVE_OLD_DISKLABEL
    802 	struct disklabel newlabel;
    803 #endif
    804 
    805 #ifdef DEBUG
    806 	if (vnddebug & VDB_FOLLOW)
    807 		printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
    808 		    dev, cmd, data, flag, p, unit);
    809 #endif
    810 	vnd = device_lookup(&vnd_cd, unit);
    811 	if (vnd == NULL &&
    812 #ifdef COMPAT_30
    813 	    cmd != VNDIOOCGET &&
    814 #endif
    815 	    cmd != VNDIOCGET)
    816 		return ENXIO;
    817 	vio = (struct vnd_ioctl *)data;
    818 
    819 	/* Must be open for writes for these commands... */
    820 	switch (cmd) {
    821 	case VNDIOCSET:
    822 	case VNDIOCCLR:
    823 	case DIOCSDINFO:
    824 	case DIOCWDINFO:
    825 #ifdef __HAVE_OLD_DISKLABEL
    826 	case ODIOCSDINFO:
    827 	case ODIOCWDINFO:
    828 #endif
    829 	case DIOCKLABEL:
    830 	case DIOCWLABEL:
    831 		if ((flag & FWRITE) == 0)
    832 			return (EBADF);
    833 	}
    834 
    835 	/* Must be initialized for these... */
    836 	switch (cmd) {
    837 	case VNDIOCCLR:
    838 	case DIOCGDINFO:
    839 	case DIOCSDINFO:
    840 	case DIOCWDINFO:
    841 	case DIOCGPART:
    842 	case DIOCKLABEL:
    843 	case DIOCWLABEL:
    844 	case DIOCGDEFLABEL:
    845 #ifdef __HAVE_OLD_DISKLABEL
    846 	case ODIOCGDINFO:
    847 	case ODIOCSDINFO:
    848 	case ODIOCWDINFO:
    849 	case ODIOCGDEFLABEL:
    850 #endif
    851 		if ((vnd->sc_flags & VNF_INITED) == 0)
    852 			return (ENXIO);
    853 	}
    854 
    855 	switch (cmd) {
    856 	case VNDIOCSET:
    857 		if (vnd->sc_flags & VNF_INITED)
    858 			return (EBUSY);
    859 
    860 		if ((error = vndlock(vnd)) != 0)
    861 			return (error);
    862 
    863 		fflags = FREAD;
    864 		if ((vio->vnd_flags & VNDIOF_READONLY) == 0)
    865 			fflags |= FWRITE;
    866 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, l);
    867 		if ((error = vn_open(&nd, fflags, 0)) != 0)
    868 			goto unlock_and_exit;
    869 		KASSERT(l);
    870 		error = VOP_GETATTR(nd.ni_vp, &vattr, l->l_proc->p_ucred, l);
    871 		if (!error && nd.ni_vp->v_type != VREG)
    872 			error = EOPNOTSUPP;
    873 		if (error) {
    874 			VOP_UNLOCK(nd.ni_vp, 0);
    875 			goto close_and_exit;
    876 		}
    877 
    878 		/* If using a compressed file, initialize its info */
    879 		/* (or abort with an error if kernel has no compression) */
    880 		if (vio->vnd_flags & VNF_COMP) {
    881 #ifdef VND_COMPRESSION
    882 			struct vnd_comp_header *ch;
    883 			int i;
    884 			u_int32_t comp_size;
    885 			u_int32_t comp_maxsize;
    886 
    887 			/* allocate space for compresed file header */
    888 			ch = malloc(sizeof(struct vnd_comp_header),
    889 			M_TEMP, M_WAITOK);
    890 
    891 			/* read compressed file header */
    892 			error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)ch,
    893 			  sizeof(struct vnd_comp_header), 0, UIO_SYSSPACE,
    894 			  IO_UNIT|IO_NODELOCKED, p->p_ucred, NULL, NULL);
    895 			if(error) {
    896 				free(ch, M_TEMP);
    897 				VOP_UNLOCK(nd.ni_vp, 0);
    898 				goto close_and_exit;
    899 			}
    900 
    901 			/* save some header info */
    902 			vnd->sc_comp_blksz = ntohl(ch->block_size);
    903 			/* note last offset is the file byte size */
    904 			vnd->sc_comp_numoffs = ntohl(ch->num_blocks)+1;
    905 			free(ch, M_TEMP);
    906 			if(vnd->sc_comp_blksz % DEV_BSIZE !=0) {
    907 				VOP_UNLOCK(nd.ni_vp, 0);
    908 				error = EINVAL;
    909 				goto close_and_exit;
    910 			}
    911 			if(sizeof(struct vnd_comp_header) +
    912 			  sizeof(u_int64_t) * vnd->sc_comp_numoffs >
    913 			  vattr.va_size) {
    914 				VOP_UNLOCK(nd.ni_vp, 0);
    915 				error = EINVAL;
    916 				goto close_and_exit;
    917 			}
    918 
    919 			/* set decompressed file size */
    920 			vattr.va_size =
    921 			  (vnd->sc_comp_numoffs - 1) * vnd->sc_comp_blksz;
    922 
    923 			/* allocate space for all the compressed offsets */
    924 			vnd->sc_comp_offsets =
    925 			malloc(sizeof(u_int64_t) * vnd->sc_comp_numoffs,
    926 			M_DEVBUF, M_WAITOK);
    927 
    928 			/* read in the offsets */
    929 			error = vn_rdwr(UIO_READ, nd.ni_vp,
    930 			  (caddr_t)vnd->sc_comp_offsets,
    931 			  sizeof(u_int64_t) * vnd->sc_comp_numoffs,
    932 			  sizeof(struct vnd_comp_header), UIO_SYSSPACE,
    933 			  IO_UNIT|IO_NODELOCKED, p->p_ucred, NULL, NULL);
    934 			if(error) {
    935 				VOP_UNLOCK(nd.ni_vp, 0);
    936 				goto close_and_exit;
    937 			}
    938 			/*
    939 			 * find largest block size (used for allocation limit).
    940 			 * Also convert offset to native byte order.
    941 			 */
    942 			comp_maxsize = 0;
    943 			for (i = 0; i < vnd->sc_comp_numoffs - 1; i++) {
    944 				vnd->sc_comp_offsets[i] =
    945 				  be64toh(vnd->sc_comp_offsets[i]);
    946 				comp_size = be64toh(vnd->sc_comp_offsets[i + 1])
    947 				  - vnd->sc_comp_offsets[i];
    948 				if (comp_size > comp_maxsize)
    949 					comp_maxsize = comp_size;
    950 			}
    951 			vnd->sc_comp_offsets[vnd->sc_comp_numoffs - 1] =
    952 			  be64toh(vnd->sc_comp_offsets[vnd->sc_comp_numoffs - 1]);
    953 
    954 			/* create compressed data buffer */
    955 			vnd->sc_comp_buff = malloc(comp_maxsize,
    956 			  M_DEVBUF, M_WAITOK);
    957 
    958 			/* create decompressed buffer */
    959 			vnd->sc_comp_decombuf = malloc(vnd->sc_comp_blksz,
    960 			  M_DEVBUF, M_WAITOK);
    961 			vnd->sc_comp_buffblk = -1;
    962 
    963 			/* Initialize decompress stream */
    964 			bzero(&vnd->sc_comp_stream, sizeof(z_stream));
    965 			vnd->sc_comp_stream.zalloc = vnd_alloc;
    966 			vnd->sc_comp_stream.zfree = vnd_free;
    967 			error = inflateInit2(&vnd->sc_comp_stream, MAX_WBITS);
    968 			if(error) {
    969 				if(vnd->sc_comp_stream.msg)
    970 					printf("vnd%d: compressed file, %s\n",
    971 					  unit, vnd->sc_comp_stream.msg);
    972 				VOP_UNLOCK(nd.ni_vp, 0);
    973 				error = EINVAL;
    974 				goto close_and_exit;
    975 			}
    976 
    977 			vnd->sc_flags |= VNF_COMP | VNF_READONLY;
    978 #else /* !VND_COMPRESSION */
    979 			VOP_UNLOCK(nd.ni_vp, 0);
    980 			error = EOPNOTSUPP;
    981 			goto close_and_exit;
    982 #endif /* VND_COMPRESSION */
    983 		}
    984 
    985 		VOP_UNLOCK(nd.ni_vp, 0);
    986 		vnd->sc_vp = nd.ni_vp;
    987 		vnd->sc_size = btodb(vattr.va_size);	/* note truncation */
    988 
    989 		/*
    990 		 * Use pseudo-geometry specified.  If none was provided,
    991 		 * use "standard" Adaptec fictitious geometry.
    992 		 */
    993 		if (vio->vnd_flags & VNDIOF_HASGEOM) {
    994 
    995 			memcpy(&vnd->sc_geom, &vio->vnd_geom,
    996 			    sizeof(vio->vnd_geom));
    997 
    998 			/*
    999 			 * Sanity-check the sector size.
   1000 			 * XXX Don't allow secsize < DEV_BSIZE.	 Should
   1001 			 * XXX we?
   1002 			 */
   1003 			if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
   1004 			    (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0 ||
   1005 			    vnd->sc_geom.vng_ncylinders == 0 ||
   1006 			    (vnd->sc_geom.vng_ntracks *
   1007 			     vnd->sc_geom.vng_nsectors) == 0) {
   1008 				error = EINVAL;
   1009 				goto close_and_exit;
   1010 			}
   1011 
   1012 			/*
   1013 			 * Compute the size (in DEV_BSIZE blocks) specified
   1014 			 * by the geometry.
   1015 			 */
   1016 			geomsize = (vnd->sc_geom.vng_nsectors *
   1017 			    vnd->sc_geom.vng_ntracks *
   1018 			    vnd->sc_geom.vng_ncylinders) *
   1019 			    (vnd->sc_geom.vng_secsize / DEV_BSIZE);
   1020 
   1021 			/*
   1022 			 * Sanity-check the size against the specified
   1023 			 * geometry.
   1024 			 */
   1025 			if (vnd->sc_size < geomsize) {
   1026 				error = EINVAL;
   1027 				goto close_and_exit;
   1028 			}
   1029 		} else if (vnd->sc_size >= (32 * 64)) {
   1030 			/*
   1031 			 * Size must be at least 2048 DEV_BSIZE blocks
   1032 			 * (1M) in order to use this geometry.
   1033 			 */
   1034 			vnd->sc_geom.vng_secsize = DEV_BSIZE;
   1035 			vnd->sc_geom.vng_nsectors = 32;
   1036 			vnd->sc_geom.vng_ntracks = 64;
   1037 			vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
   1038 		} else {
   1039 			vnd->sc_geom.vng_secsize = DEV_BSIZE;
   1040 			vnd->sc_geom.vng_nsectors = 1;
   1041 			vnd->sc_geom.vng_ntracks = 1;
   1042 			vnd->sc_geom.vng_ncylinders = vnd->sc_size;
   1043 		}
   1044 
   1045 		if (vio->vnd_flags & VNDIOF_READONLY) {
   1046 			vnd->sc_flags |= VNF_READONLY;
   1047 		}
   1048 
   1049 		if ((error = vndsetcred(vnd, p->p_ucred)) != 0)
   1050 			goto close_and_exit;
   1051 
   1052 		vndthrottle(vnd, vnd->sc_vp);
   1053 		vio->vnd_size = dbtob(vnd->sc_size);
   1054 		vnd->sc_flags |= VNF_INITED;
   1055 
   1056 		/* create the kernel thread, wait for it to be up */
   1057 		error = kthread_create1(vndthread, vnd, &vnd->sc_kthread,
   1058 		    vnd->sc_dev.dv_xname);
   1059 		if (error)
   1060 			goto close_and_exit;
   1061 		while ((vnd->sc_flags & VNF_KTHREAD) == 0) {
   1062 			tsleep(&vnd->sc_kthread, PRIBIO, "vndthr", 0);
   1063 		}
   1064 #ifdef DEBUG
   1065 		if (vnddebug & VDB_INIT)
   1066 			printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
   1067 			    vnd->sc_vp, (unsigned long) vnd->sc_size,
   1068 			    vnd->sc_geom.vng_secsize,
   1069 			    vnd->sc_geom.vng_nsectors,
   1070 			    vnd->sc_geom.vng_ntracks,
   1071 			    vnd->sc_geom.vng_ncylinders);
   1072 #endif
   1073 
   1074 		/* Attach the disk. */
   1075 		vnd->sc_dkdev.dk_name = vnd->sc_dev.dv_xname;
   1076 		pseudo_disk_attach(&vnd->sc_dkdev);
   1077 
   1078 		/* Initialize the xfer and buffer pools. */
   1079 		pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
   1080 		    0, 0, "vndxpl", NULL);
   1081 
   1082 		/* Try and read the disklabel. */
   1083 		vndgetdisklabel(dev, vnd);
   1084 
   1085 		vndunlock(vnd);
   1086 
   1087 		break;
   1088 
   1089 close_and_exit:
   1090 		(void) vn_close(nd.ni_vp, fflags, p->p_ucred, l);
   1091 unlock_and_exit:
   1092 #ifdef VND_COMPRESSION
   1093 		/* free any allocated memory (for compressed file) */
   1094 		if(vnd->sc_comp_offsets) {
   1095 			free(vnd->sc_comp_offsets, M_DEVBUF);
   1096 			vnd->sc_comp_offsets = NULL;
   1097 		}
   1098 		if(vnd->sc_comp_buff) {
   1099 			free(vnd->sc_comp_buff, M_DEVBUF);
   1100 			vnd->sc_comp_buff = NULL;
   1101 		}
   1102 		if(vnd->sc_comp_decombuf) {
   1103 			free(vnd->sc_comp_decombuf, M_DEVBUF);
   1104 			vnd->sc_comp_decombuf = NULL;
   1105 		}
   1106 #endif /* VND_COMPRESSION */
   1107 		vndunlock(vnd);
   1108 		return (error);
   1109 
   1110 	case VNDIOCCLR:
   1111 		if ((error = vndlock(vnd)) != 0)
   1112 			return (error);
   1113 
   1114 		/*
   1115 		 * Don't unconfigure if any other partitions are open
   1116 		 * or if both the character and block flavors of this
   1117 		 * partition are open.
   1118 		 */
   1119 		part = DISKPART(dev);
   1120 		pmask = (1 << part);
   1121 		if (((vnd->sc_dkdev.dk_openmask & ~pmask) ||
   1122 		    ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
   1123 		    (vnd->sc_dkdev.dk_copenmask & pmask))) &&
   1124 			!(vio->vnd_flags & VNDIOF_FORCE)) {
   1125 			vndunlock(vnd);
   1126 			return (EBUSY);
   1127 		}
   1128 
   1129 		/*
   1130 		 * XXX vndclear() might call vndclose() implicitely;
   1131 		 * release lock to avoid recursion
   1132 		 */
   1133 		vndunlock(vnd);
   1134 		vndclear(vnd, minor(dev));
   1135 #ifdef DEBUG
   1136 		if (vnddebug & VDB_INIT)
   1137 			printf("vndioctl: CLRed\n");
   1138 #endif
   1139 
   1140 		/* Destroy the xfer and buffer pools. */
   1141 		pool_destroy(&vnd->sc_vxpool);
   1142 
   1143 		/* Detatch the disk. */
   1144 		pseudo_disk_detach(&vnd->sc_dkdev);
   1145 		break;
   1146 
   1147 #ifdef COMPAT_30
   1148 	case VNDIOOCGET: {
   1149 		struct vnd_ouser *vnu;
   1150 		struct vattr va;
   1151 		vnu = (struct vnd_ouser *)data;
   1152 		KASSERT(l);
   1153 		switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
   1154 		case 0:
   1155 			vnu->vnu_dev = va.va_fsid;
   1156 			vnu->vnu_ino = va.va_fileid;
   1157 			break;
   1158 		case -1:
   1159 			/* unused is not an error */
   1160 			vnu->vnu_dev = 0;
   1161 			vnu->vnu_ino = 0;
   1162 			break;
   1163 		default:
   1164 			return error;
   1165 		}
   1166 		break;
   1167 	}
   1168 #endif
   1169 	case VNDIOCGET: {
   1170 		struct vnd_user *vnu;
   1171 		struct vattr va;
   1172 		vnu = (struct vnd_user *)data;
   1173 		KASSERT(l);
   1174 		switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
   1175 		case 0:
   1176 			vnu->vnu_dev = va.va_fsid;
   1177 			vnu->vnu_ino = va.va_fileid;
   1178 			break;
   1179 		case -1:
   1180 			/* unused is not an error */
   1181 			vnu->vnu_dev = 0;
   1182 			vnu->vnu_ino = 0;
   1183 			break;
   1184 		default:
   1185 			return error;
   1186 		}
   1187 		break;
   1188 	}
   1189 
   1190 	case DIOCGDINFO:
   1191 		*(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
   1192 		break;
   1193 
   1194 #ifdef __HAVE_OLD_DISKLABEL
   1195 	case ODIOCGDINFO:
   1196 		newlabel = *(vnd->sc_dkdev.dk_label);
   1197 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1198 			return ENOTTY;
   1199 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
   1200 		break;
   1201 #endif
   1202 
   1203 	case DIOCGPART:
   1204 		((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
   1205 		((struct partinfo *)data)->part =
   1206 		    &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
   1207 		break;
   1208 
   1209 	case DIOCWDINFO:
   1210 	case DIOCSDINFO:
   1211 #ifdef __HAVE_OLD_DISKLABEL
   1212 	case ODIOCWDINFO:
   1213 	case ODIOCSDINFO:
   1214 #endif
   1215 	{
   1216 		struct disklabel *lp;
   1217 
   1218 		if ((error = vndlock(vnd)) != 0)
   1219 			return (error);
   1220 
   1221 		vnd->sc_flags |= VNF_LABELLING;
   1222 
   1223 #ifdef __HAVE_OLD_DISKLABEL
   1224 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
   1225 			memset(&newlabel, 0, sizeof newlabel);
   1226 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
   1227 			lp = &newlabel;
   1228 		} else
   1229 #endif
   1230 		lp = (struct disklabel *)data;
   1231 
   1232 		error = setdisklabel(vnd->sc_dkdev.dk_label,
   1233 		    lp, 0, vnd->sc_dkdev.dk_cpulabel);
   1234 		if (error == 0) {
   1235 			if (cmd == DIOCWDINFO
   1236 #ifdef __HAVE_OLD_DISKLABEL
   1237 			    || cmd == ODIOCWDINFO
   1238 #endif
   1239 			   )
   1240 				error = writedisklabel(VNDLABELDEV(dev),
   1241 				    vndstrategy, vnd->sc_dkdev.dk_label,
   1242 				    vnd->sc_dkdev.dk_cpulabel);
   1243 		}
   1244 
   1245 		vnd->sc_flags &= ~VNF_LABELLING;
   1246 
   1247 		vndunlock(vnd);
   1248 
   1249 		if (error)
   1250 			return (error);
   1251 		break;
   1252 	}
   1253 
   1254 	case DIOCKLABEL:
   1255 		if (*(int *)data != 0)
   1256 			vnd->sc_flags |= VNF_KLABEL;
   1257 		else
   1258 			vnd->sc_flags &= ~VNF_KLABEL;
   1259 		break;
   1260 
   1261 	case DIOCWLABEL:
   1262 		if (*(int *)data != 0)
   1263 			vnd->sc_flags |= VNF_WLABEL;
   1264 		else
   1265 			vnd->sc_flags &= ~VNF_WLABEL;
   1266 		break;
   1267 
   1268 	case DIOCGDEFLABEL:
   1269 		vndgetdefaultlabel(vnd, (struct disklabel *)data);
   1270 		break;
   1271 
   1272 #ifdef __HAVE_OLD_DISKLABEL
   1273 	case ODIOCGDEFLABEL:
   1274 		vndgetdefaultlabel(vnd, &newlabel);
   1275 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1276 			return ENOTTY;
   1277 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
   1278 		break;
   1279 #endif
   1280 
   1281 	default:
   1282 		return (ENOTTY);
   1283 	}
   1284 
   1285 	return (0);
   1286 }
   1287 
   1288 /*
   1289  * Duplicate the current processes' credentials.  Since we are called only
   1290  * as the result of a SET ioctl and only root can do that, any future access
   1291  * to this "disk" is essentially as root.  Note that credentials may change
   1292  * if some other uid can write directly to the mapped file (NFS).
   1293  */
   1294 static int
   1295 vndsetcred(struct vnd_softc *vnd, struct ucred *cred)
   1296 {
   1297 	struct uio auio;
   1298 	struct iovec aiov;
   1299 	char *tmpbuf;
   1300 	int error;
   1301 
   1302 	vnd->sc_cred = crdup(cred);
   1303 	tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
   1304 
   1305 	/* XXX: Horrible kludge to establish credentials for NFS */
   1306 	aiov.iov_base = tmpbuf;
   1307 	aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
   1308 	auio.uio_iov = &aiov;
   1309 	auio.uio_iovcnt = 1;
   1310 	auio.uio_offset = 0;
   1311 	auio.uio_rw = UIO_READ;
   1312 	auio.uio_resid = aiov.iov_len;
   1313 	UIO_SETUP_SYSSPACE(&auio);
   1314 	vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
   1315 	error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
   1316 	if (error == 0) {
   1317 		/*
   1318 		 * Because vnd does all IO directly through the vnode
   1319 		 * we need to flush (at least) the buffer from the above
   1320 		 * VOP_READ from the buffer cache to prevent cache
   1321 		 * incoherencies.  Also, be careful to write dirty
   1322 		 * buffers back to stable storage.
   1323 		 */
   1324 		error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
   1325 			    curlwp, 0, 0);
   1326 	}
   1327 	VOP_UNLOCK(vnd->sc_vp, 0);
   1328 
   1329 	free(tmpbuf, M_TEMP);
   1330 	return (error);
   1331 }
   1332 
   1333 /*
   1334  * Set maxactive based on FS type
   1335  */
   1336 static void
   1337 vndthrottle(struct vnd_softc *vnd, struct vnode *vp)
   1338 {
   1339 #ifdef NFS
   1340 	extern int (**nfsv2_vnodeop_p)(void *);
   1341 
   1342 	if (vp->v_op == nfsv2_vnodeop_p)
   1343 		vnd->sc_maxactive = 2;
   1344 	else
   1345 #endif
   1346 		vnd->sc_maxactive = 8;
   1347 
   1348 	if (vnd->sc_maxactive < 1)
   1349 		vnd->sc_maxactive = 1;
   1350 }
   1351 
   1352 #if 0
   1353 static void
   1354 vndshutdown(void)
   1355 {
   1356 	struct vnd_softc *vnd;
   1357 
   1358 	for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
   1359 		if (vnd->sc_flags & VNF_INITED)
   1360 			vndclear(vnd);
   1361 }
   1362 #endif
   1363 
   1364 static void
   1365 vndclear(struct vnd_softc *vnd, int myminor)
   1366 {
   1367 	struct vnode *vp = vnd->sc_vp;
   1368 	struct lwp *l = curlwp;
   1369 	int fflags = FREAD;
   1370 	int bmaj, cmaj, i, mn;
   1371 	int s;
   1372 
   1373 #ifdef DEBUG
   1374 	if (vnddebug & VDB_FOLLOW)
   1375 		printf("vndclear(%p): vp %p\n", vnd, vp);
   1376 #endif
   1377 	/* locate the major number */
   1378 	bmaj = bdevsw_lookup_major(&vnd_bdevsw);
   1379 	cmaj = cdevsw_lookup_major(&vnd_cdevsw);
   1380 
   1381 	/* Nuke the vnodes for any open instances */
   1382 	for (i = 0; i < MAXPARTITIONS; i++) {
   1383 		mn = DISKMINOR(device_unit(&vnd->sc_dev), i);
   1384 		vdevgone(bmaj, mn, mn, VBLK);
   1385 		if (mn != myminor) /* XXX avoid to kill own vnode */
   1386 			vdevgone(cmaj, mn, mn, VCHR);
   1387 	}
   1388 
   1389 	if ((vnd->sc_flags & VNF_READONLY) == 0)
   1390 		fflags |= FWRITE;
   1391 
   1392 	s = splbio();
   1393 	bufq_drain(vnd->sc_tab);
   1394 	splx(s);
   1395 
   1396 	vnd->sc_flags |= VNF_VUNCONF;
   1397 	wakeup(&vnd->sc_tab);
   1398 	while (vnd->sc_flags & VNF_KTHREAD)
   1399 		tsleep(&vnd->sc_kthread, PRIBIO, "vnthr", 0);
   1400 
   1401 #ifdef VND_COMPRESSION
   1402 	/* free the compressed file buffers */
   1403 	if(vnd->sc_flags & VNF_COMP) {
   1404 		if(vnd->sc_comp_offsets) {
   1405 			free(vnd->sc_comp_offsets, M_DEVBUF);
   1406 			vnd->sc_comp_offsets = NULL;
   1407 		}
   1408 		if(vnd->sc_comp_buff) {
   1409 			free(vnd->sc_comp_buff, M_DEVBUF);
   1410 			vnd->sc_comp_buff = NULL;
   1411 		}
   1412 		if(vnd->sc_comp_decombuf) {
   1413 			free(vnd->sc_comp_decombuf, M_DEVBUF);
   1414 			vnd->sc_comp_decombuf = NULL;
   1415 		}
   1416 	}
   1417 #endif /* VND_COMPRESSION */
   1418 	vnd->sc_flags &=
   1419 	    ~(VNF_INITED | VNF_READONLY | VNF_VLABEL
   1420 	      | VNF_VUNCONF | VNF_COMP);
   1421 	if (vp == (struct vnode *)0)
   1422 		panic("vndclear: null vp");
   1423 	(void) vn_close(vp, fflags, vnd->sc_cred, l);
   1424 	crfree(vnd->sc_cred);
   1425 	vnd->sc_vp = (struct vnode *)0;
   1426 	vnd->sc_cred = (struct ucred *)0;
   1427 	vnd->sc_size = 0;
   1428 }
   1429 
   1430 static int
   1431 vndsize(dev_t dev)
   1432 {
   1433 	struct vnd_softc *sc;
   1434 	struct disklabel *lp;
   1435 	int part, unit, omask;
   1436 	int size;
   1437 
   1438 	unit = vndunit(dev);
   1439 	sc = (struct vnd_softc *)device_lookup(&vnd_cd, unit);
   1440 	if (sc == NULL)
   1441 		return -1;
   1442 
   1443 	if ((sc->sc_flags & VNF_INITED) == 0)
   1444 		return (-1);
   1445 
   1446 	part = DISKPART(dev);
   1447 	omask = sc->sc_dkdev.dk_openmask & (1 << part);
   1448 	lp = sc->sc_dkdev.dk_label;
   1449 
   1450 	if (omask == 0 && vndopen(dev, 0, S_IFBLK, curlwp))	/* XXX */
   1451 		return (-1);
   1452 
   1453 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
   1454 		size = -1;
   1455 	else
   1456 		size = lp->d_partitions[part].p_size *
   1457 		    (lp->d_secsize / DEV_BSIZE);
   1458 
   1459 	if (omask == 0 && vndclose(dev, 0, S_IFBLK, curlwp))	/* XXX */
   1460 		return (-1);
   1461 
   1462 	return (size);
   1463 }
   1464 
   1465 static int
   1466 vnddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
   1467 {
   1468 
   1469 	/* Not implemented. */
   1470 	return ENXIO;
   1471 }
   1472 
   1473 static void
   1474 vndgetdefaultlabel(struct vnd_softc *sc, struct disklabel *lp)
   1475 {
   1476 	struct vndgeom *vng = &sc->sc_geom;
   1477 	struct partition *pp;
   1478 
   1479 	memset(lp, 0, sizeof(*lp));
   1480 
   1481 	lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
   1482 	lp->d_secsize = vng->vng_secsize;
   1483 	lp->d_nsectors = vng->vng_nsectors;
   1484 	lp->d_ntracks = vng->vng_ntracks;
   1485 	lp->d_ncylinders = vng->vng_ncylinders;
   1486 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1487 
   1488 	strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
   1489 	lp->d_type = DTYPE_VND;
   1490 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1491 	lp->d_rpm = 3600;
   1492 	lp->d_interleave = 1;
   1493 	lp->d_flags = 0;
   1494 
   1495 	pp = &lp->d_partitions[RAW_PART];
   1496 	pp->p_offset = 0;
   1497 	pp->p_size = lp->d_secperunit;
   1498 	pp->p_fstype = FS_UNUSED;
   1499 	lp->d_npartitions = RAW_PART + 1;
   1500 
   1501 	lp->d_magic = DISKMAGIC;
   1502 	lp->d_magic2 = DISKMAGIC;
   1503 	lp->d_checksum = dkcksum(lp);
   1504 }
   1505 
   1506 /*
   1507  * Read the disklabel from a vnd.  If one is not present, create a fake one.
   1508  */
   1509 static void
   1510 vndgetdisklabel(dev_t dev, struct vnd_softc *sc)
   1511 {
   1512 	const char *errstring;
   1513 	struct disklabel *lp = sc->sc_dkdev.dk_label;
   1514 	struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
   1515 	int i;
   1516 
   1517 	memset(clp, 0, sizeof(*clp));
   1518 
   1519 	vndgetdefaultlabel(sc, lp);
   1520 
   1521 	/*
   1522 	 * Call the generic disklabel extraction routine.
   1523 	 */
   1524 	errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
   1525 	if (errstring) {
   1526 		/*
   1527 		 * Lack of disklabel is common, but we print the warning
   1528 		 * anyway, since it might contain other useful information.
   1529 		 */
   1530 		printf("%s: %s\n", sc->sc_dev.dv_xname, errstring);
   1531 
   1532 		/*
   1533 		 * For historical reasons, if there's no disklabel
   1534 		 * present, all partitions must be FS_BSDFFS and
   1535 		 * occupy the entire disk.
   1536 		 */
   1537 		for (i = 0; i < MAXPARTITIONS; i++) {
   1538 			/*
   1539 			 * Don't wipe out port specific hack (such as
   1540 			 * dos partition hack of i386 port).
   1541 			 */
   1542 			if (lp->d_partitions[i].p_size != 0)
   1543 				continue;
   1544 
   1545 			lp->d_partitions[i].p_size = lp->d_secperunit;
   1546 			lp->d_partitions[i].p_offset = 0;
   1547 			lp->d_partitions[i].p_fstype = FS_BSDFFS;
   1548 		}
   1549 
   1550 		strncpy(lp->d_packname, "default label",
   1551 		    sizeof(lp->d_packname));
   1552 
   1553 		lp->d_npartitions = MAXPARTITIONS;
   1554 		lp->d_checksum = dkcksum(lp);
   1555 	}
   1556 
   1557 	/* In-core label now valid. */
   1558 	sc->sc_flags |= VNF_VLABEL;
   1559 }
   1560 
   1561 /*
   1562  * Wait interruptibly for an exclusive lock.
   1563  *
   1564  * XXX
   1565  * Several drivers do this; it should be abstracted and made MP-safe.
   1566  */
   1567 static int
   1568 vndlock(struct vnd_softc *sc)
   1569 {
   1570 	int error;
   1571 
   1572 	while ((sc->sc_flags & VNF_LOCKED) != 0) {
   1573 		sc->sc_flags |= VNF_WANTED;
   1574 		if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
   1575 			return (error);
   1576 	}
   1577 	sc->sc_flags |= VNF_LOCKED;
   1578 	return (0);
   1579 }
   1580 
   1581 /*
   1582  * Unlock and wake up any waiters.
   1583  */
   1584 static void
   1585 vndunlock(struct vnd_softc *sc)
   1586 {
   1587 
   1588 	sc->sc_flags &= ~VNF_LOCKED;
   1589 	if ((sc->sc_flags & VNF_WANTED) != 0) {
   1590 		sc->sc_flags &= ~VNF_WANTED;
   1591 		wakeup(sc);
   1592 	}
   1593 }
   1594 
   1595 #ifdef VND_COMPRESSION
   1596 /* compressed file read */
   1597 static void
   1598 compstrategy(struct buf *bp, off_t bn)
   1599 {
   1600 	int error;
   1601 	int unit = vndunit(bp->b_dev);
   1602 	struct vnd_softc *vnd =
   1603 	    (struct vnd_softc *)device_lookup(&vnd_cd, unit);
   1604 	u_int32_t comp_block;
   1605 	struct uio auio;
   1606 	caddr_t addr;
   1607 	int s;
   1608 
   1609 	/* set up constants for data move */
   1610 	auio.uio_rw = UIO_READ;
   1611 	UIO_SETUP_SYSSPACE(&auio);
   1612 
   1613 	/* read, and transfer the data */
   1614 	addr = bp->b_data;
   1615 	s = splbio();
   1616 	while (bp->b_resid > 0) {
   1617 		unsigned length;
   1618 		size_t length_in_buffer;
   1619 		u_int32_t offset_in_buffer;
   1620 		struct iovec aiov;
   1621 
   1622 		/* calculate the compressed block number */
   1623 		comp_block = bn / (off_t)vnd->sc_comp_blksz;
   1624 
   1625 		/* check for good block number */
   1626 		if (comp_block >= vnd->sc_comp_numoffs) {
   1627 			bp->b_error = EINVAL;
   1628 			bp->b_flags |= B_ERROR;
   1629 			splx(s);
   1630 			return;
   1631 		}
   1632 
   1633 		/* read in the compressed block, if not in buffer */
   1634 		if (comp_block != vnd->sc_comp_buffblk) {
   1635 			length = vnd->sc_comp_offsets[comp_block + 1] -
   1636 			    vnd->sc_comp_offsets[comp_block];
   1637 			vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
   1638 			error = vn_rdwr(UIO_READ, vnd->sc_vp, vnd->sc_comp_buff,
   1639 			    length, vnd->sc_comp_offsets[comp_block],
   1640 			    UIO_SYSSPACE, IO_UNIT, vnd->sc_cred, NULL, NULL);
   1641 			if (error) {
   1642 				bp->b_error = error;
   1643 				bp->b_flags |= B_ERROR;
   1644 				VOP_UNLOCK(vnd->sc_vp, 0);
   1645 				splx(s);
   1646 				return;
   1647 			}
   1648 			/* uncompress the buffer */
   1649 			vnd->sc_comp_stream.next_in = vnd->sc_comp_buff;
   1650 			vnd->sc_comp_stream.avail_in = length;
   1651 			vnd->sc_comp_stream.next_out = vnd->sc_comp_decombuf;
   1652 			vnd->sc_comp_stream.avail_out = vnd->sc_comp_blksz;
   1653 			inflateReset(&vnd->sc_comp_stream);
   1654 			error = inflate(&vnd->sc_comp_stream, Z_FINISH);
   1655 			if (error != Z_STREAM_END) {
   1656 				if (vnd->sc_comp_stream.msg)
   1657 					printf("%s: compressed file, %s\n",
   1658 					    vnd->sc_dev.dv_xname,
   1659 					    vnd->sc_comp_stream.msg);
   1660 				bp->b_error = EBADMSG;
   1661 				bp->b_flags |= B_ERROR;
   1662 				VOP_UNLOCK(vnd->sc_vp, 0);
   1663 				splx(s);
   1664 				return;
   1665 			}
   1666 			vnd->sc_comp_buffblk = comp_block;
   1667 			VOP_UNLOCK(vnd->sc_vp, 0);
   1668 		}
   1669 
   1670 		/* transfer the usable uncompressed data */
   1671 		offset_in_buffer = bn % (off_t)vnd->sc_comp_blksz;
   1672 		length_in_buffer = vnd->sc_comp_blksz - offset_in_buffer;
   1673 		if (length_in_buffer > bp->b_resid)
   1674 			length_in_buffer = bp->b_resid;
   1675 		auio.uio_iov = &aiov;
   1676 		auio.uio_iovcnt = 1;
   1677 		aiov.iov_base = addr;
   1678 		aiov.iov_len = length_in_buffer;
   1679 		auio.uio_resid = aiov.iov_len;
   1680 		auio.uio_offset = 0;
   1681 		error = uiomove(vnd->sc_comp_decombuf + offset_in_buffer,
   1682 		    length_in_buffer, &auio);
   1683 		if (error) {
   1684 			bp->b_error = error;
   1685 			bp->b_flags |= B_ERROR;
   1686 			splx(s);
   1687 			return;
   1688 		}
   1689 
   1690 		bn += length_in_buffer;
   1691 		addr += length_in_buffer;
   1692 		bp->b_resid -= length_in_buffer;
   1693 	}
   1694 	splx(s);
   1695 }
   1696 
   1697 /* compression memory allocation routines */
   1698 static void *
   1699 vnd_alloc(void *aux, u_int items, u_int siz)
   1700 {
   1701 	return malloc(items * siz, M_TEMP, M_NOWAIT);
   1702 }
   1703 
   1704 static void
   1705 vnd_free(void *aux, void *ptr)
   1706 {
   1707 	free(ptr, M_TEMP);
   1708 }
   1709 #endif /* VND_COMPRESSION */
   1710