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