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