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