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