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