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