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