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