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