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