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