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