94 95#include <geom/geom.h> 96 97#include "zfs_namecheck.h" 98 99struct g_class zfs_zvol_class = { 100 .name = "ZFS::ZVOL", 101 .version = G_VERSION, 102}; 103 104DECLARE_GEOM_CLASS(zfs_zvol_class, zfs_zvol); 105 106void *zfsdev_state; 107static char *zvol_tag = "zvol_tag"; 108 109#define ZVOL_DUMPSIZE "dumpsize" 110 111/* 112 * The spa_namespace_lock protects the zfsdev_state structure from being 113 * modified while it's being used, e.g. an open that comes in before a 114 * create finishes. It also protects temporary opens of the dataset so that, 115 * e.g., an open doesn't get a spurious EBUSY. 116 */ 117static uint32_t zvol_minors; 118 119SYSCTL_DECL(_vfs_zfs); 120SYSCTL_NODE(_vfs_zfs, OID_AUTO, vol, CTLFLAG_RW, 0, "ZFS VOLUME"); 121static int volmode = ZFS_VOLMODE_GEOM; 122SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, mode, CTLFLAG_RWTUN, &volmode, 0, 123 "Expose as GEOM providers (1), device files (2) or neither"); 124 125typedef struct zvol_extent { 126 list_node_t ze_node; 127 dva_t ze_dva; /* dva associated with this extent */ 128 uint64_t ze_nblks; /* number of blocks in extent */ 129} zvol_extent_t; 130 131/* 132 * The in-core state of each volume. 133 */ 134typedef struct zvol_state { 135 LIST_ENTRY(zvol_state) zv_links; 136 char zv_name[MAXPATHLEN]; /* pool/dd name */ 137 uint64_t zv_volsize; /* amount of space we advertise */ 138 uint64_t zv_volblocksize; /* volume block size */ 139 struct cdev *zv_dev; /* non-GEOM device */ 140 struct g_provider *zv_provider; /* GEOM provider */ 141 uint8_t zv_min_bs; /* minimum addressable block shift */ 142 uint8_t zv_flags; /* readonly, dumpified, etc. */ 143 objset_t *zv_objset; /* objset handle */ 144 uint32_t zv_total_opens; /* total open count */ 145 zilog_t *zv_zilog; /* ZIL handle */ 146 list_t zv_extents; /* List of extents for dump */ 147 znode_t zv_znode; /* for range locking */ 148 dmu_buf_t *zv_dbuf; /* bonus handle */ 149 int zv_state; 150 int zv_volmode; /* Provide GEOM or cdev */ 151 struct bio_queue_head zv_queue; 152 struct mtx zv_queue_mtx; /* zv_queue mutex */ 153} zvol_state_t; 154 155static LIST_HEAD(, zvol_state) all_zvols; 156 157/* 158 * zvol specific flags 159 */ 160#define ZVOL_RDONLY 0x1 161#define ZVOL_DUMPIFIED 0x2 162#define ZVOL_EXCL 0x4 163#define ZVOL_WCE 0x8 164 165/* 166 * zvol maximum transfer in one DMU tx. 167 */ 168int zvol_maxphys = DMU_MAX_ACCESS/2; 169 170/* 171 * Toggle unmap functionality. 172 */ 173boolean_t zvol_unmap_enabled = B_TRUE; 174SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, unmap_enabled, CTLFLAG_RWTUN, 175 &zvol_unmap_enabled, 0, 176 "Enable UNMAP functionality"); 177 178static d_open_t zvol_d_open; 179static d_close_t zvol_d_close; 180static d_read_t zvol_read; 181static d_write_t zvol_write; 182static d_ioctl_t zvol_d_ioctl; 183static d_strategy_t zvol_strategy; 184 185static struct cdevsw zvol_cdevsw = { 186 .d_version = D_VERSION, 187 .d_open = zvol_d_open, 188 .d_close = zvol_d_close, 189 .d_read = zvol_read, 190 .d_write = zvol_write, 191 .d_ioctl = zvol_d_ioctl, 192 .d_strategy = zvol_strategy, 193 .d_name = "zvol", 194 .d_flags = D_DISK | D_TRACKCLOSE, 195}; 196 197extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 198 nvlist_t *, nvlist_t *); 199static void zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, 200 uint64_t len, boolean_t sync); 201static int zvol_remove_zv(zvol_state_t *); 202static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 203static int zvol_dumpify(zvol_state_t *zv); 204static int zvol_dump_fini(zvol_state_t *zv); 205static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 206 207static void zvol_geom_run(zvol_state_t *zv); 208static void zvol_geom_destroy(zvol_state_t *zv); 209static int zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace); 210static void zvol_geom_start(struct bio *bp); 211static void zvol_geom_worker(void *arg); 212 213static void 214zvol_size_changed(zvol_state_t *zv) 215{ 216#ifdef sun 217 dev_t dev = makedevice(maj, min); 218 219 VERIFY(ddi_prop_update_int64(dev, zfs_dip, 220 "Size", volsize) == DDI_SUCCESS); 221 VERIFY(ddi_prop_update_int64(dev, zfs_dip, 222 "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 223 224 /* Notify specfs to invalidate the cached size */ 225 spec_size_invalidate(dev, VBLK); 226 spec_size_invalidate(dev, VCHR); 227#else /* !sun */ 228 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 229 struct g_provider *pp; 230 231 pp = zv->zv_provider; 232 if (pp == NULL) 233 return; 234 g_topology_lock(); 235 g_resize_provider(pp, zv->zv_volsize); 236 g_topology_unlock(); 237 } 238#endif /* !sun */ 239} 240 241int 242zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 243{ 244 if (volsize == 0) 245 return (SET_ERROR(EINVAL)); 246 247 if (volsize % blocksize != 0) 248 return (SET_ERROR(EINVAL)); 249 250#ifdef _ILP32 251 if (volsize - 1 > SPEC_MAXOFFSET_T) 252 return (SET_ERROR(EOVERFLOW)); 253#endif 254 return (0); 255} 256 257int 258zvol_check_volblocksize(uint64_t volblocksize) 259{ 260 if (volblocksize < SPA_MINBLOCKSIZE || 261 volblocksize > SPA_OLD_MAXBLOCKSIZE || 262 !ISP2(volblocksize)) 263 return (SET_ERROR(EDOM)); 264 265 return (0); 266} 267 268int 269zvol_get_stats(objset_t *os, nvlist_t *nv) 270{ 271 int error; 272 dmu_object_info_t doi; 273 uint64_t val; 274 275 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 276 if (error) 277 return (error); 278 279 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 280 281 error = dmu_object_info(os, ZVOL_OBJ, &doi); 282 283 if (error == 0) { 284 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 285 doi.doi_data_block_size); 286 } 287 288 return (error); 289} 290 291static zvol_state_t * 292zvol_minor_lookup(const char *name) 293{ 294 zvol_state_t *zv; 295 296 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 297 298 LIST_FOREACH(zv, &all_zvols, zv_links) { 299 if (strcmp(zv->zv_name, name) == 0) 300 break; 301 } 302 303 return (zv); 304} 305 306/* extent mapping arg */ 307struct maparg { 308 zvol_state_t *ma_zv; 309 uint64_t ma_blks; 310}; 311 312/*ARGSUSED*/ 313static int 314zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 315 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 316{ 317 struct maparg *ma = arg; 318 zvol_extent_t *ze; 319 int bs = ma->ma_zv->zv_volblocksize; 320 321 if (BP_IS_HOLE(bp) || 322 zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 323 return (0); 324 325 VERIFY(!BP_IS_EMBEDDED(bp)); 326 327 VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 328 ma->ma_blks++; 329 330 /* Abort immediately if we have encountered gang blocks */ 331 if (BP_IS_GANG(bp)) 332 return (SET_ERROR(EFRAGS)); 333 334 /* 335 * See if the block is at the end of the previous extent. 336 */ 337 ze = list_tail(&ma->ma_zv->zv_extents); 338 if (ze && 339 DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 340 DVA_GET_OFFSET(BP_IDENTITY(bp)) == 341 DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 342 ze->ze_nblks++; 343 return (0); 344 } 345 346 dprintf_bp(bp, "%s", "next blkptr:"); 347 348 /* start a new extent */ 349 ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 350 ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 351 ze->ze_nblks = 1; 352 list_insert_tail(&ma->ma_zv->zv_extents, ze); 353 return (0); 354} 355 356static void 357zvol_free_extents(zvol_state_t *zv) 358{ 359 zvol_extent_t *ze; 360 361 while (ze = list_head(&zv->zv_extents)) { 362 list_remove(&zv->zv_extents, ze); 363 kmem_free(ze, sizeof (zvol_extent_t)); 364 } 365} 366 367static int 368zvol_get_lbas(zvol_state_t *zv) 369{ 370 objset_t *os = zv->zv_objset; 371 struct maparg ma; 372 int err; 373 374 ma.ma_zv = zv; 375 ma.ma_blks = 0; 376 zvol_free_extents(zv); 377 378 /* commit any in-flight changes before traversing the dataset */ 379 txg_wait_synced(dmu_objset_pool(os), 0); 380 err = traverse_dataset(dmu_objset_ds(os), 0, 381 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 382 if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 383 zvol_free_extents(zv); 384 return (err ? err : EIO); 385 } 386 387 return (0); 388} 389 390/* ARGSUSED */ 391void 392zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 393{ 394 zfs_creat_t *zct = arg; 395 nvlist_t *nvprops = zct->zct_props; 396 int error; 397 uint64_t volblocksize, volsize; 398 399 VERIFY(nvlist_lookup_uint64(nvprops, 400 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 401 if (nvlist_lookup_uint64(nvprops, 402 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 403 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 404 405 /* 406 * These properties must be removed from the list so the generic 407 * property setting step won't apply to them. 408 */ 409 VERIFY(nvlist_remove_all(nvprops, 410 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 411 (void) nvlist_remove_all(nvprops, 412 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 413 414 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 415 DMU_OT_NONE, 0, tx); 416 ASSERT(error == 0); 417 418 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 419 DMU_OT_NONE, 0, tx); 420 ASSERT(error == 0); 421 422 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 423 ASSERT(error == 0); 424} 425 426/* 427 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we 428 * implement DKIOCFREE/free-long-range. 429 */ 430static int 431zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap) 432{ 433 uint64_t offset, length; 434 435 if (byteswap) 436 byteswap_uint64_array(lr, sizeof (*lr)); 437 438 offset = lr->lr_offset; 439 length = lr->lr_length; 440 441 return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length)); 442} 443 444/* 445 * Replay a TX_WRITE ZIL transaction that didn't get committed 446 * after a system failure 447 */ 448static int 449zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 450{ 451 objset_t *os = zv->zv_objset; 452 char *data = (char *)(lr + 1); /* data follows lr_write_t */ 453 uint64_t offset, length; 454 dmu_tx_t *tx; 455 int error; 456 457 if (byteswap) 458 byteswap_uint64_array(lr, sizeof (*lr)); 459 460 offset = lr->lr_offset; 461 length = lr->lr_length; 462 463 /* If it's a dmu_sync() block, write the whole block */ 464 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 465 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 466 if (length < blocksize) { 467 offset -= offset % blocksize; 468 length = blocksize; 469 } 470 } 471 472 tx = dmu_tx_create(os); 473 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 474 error = dmu_tx_assign(tx, TXG_WAIT); 475 if (error) { 476 dmu_tx_abort(tx); 477 } else { 478 dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 479 dmu_tx_commit(tx); 480 } 481 482 return (error); 483} 484 485/* ARGSUSED */ 486static int 487zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 488{ 489 return (SET_ERROR(ENOTSUP)); 490} 491 492/* 493 * Callback vectors for replaying records. 494 * Only TX_WRITE and TX_TRUNCATE are needed for zvol. 495 */ 496zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 497 zvol_replay_err, /* 0 no such transaction type */ 498 zvol_replay_err, /* TX_CREATE */ 499 zvol_replay_err, /* TX_MKDIR */ 500 zvol_replay_err, /* TX_MKXATTR */ 501 zvol_replay_err, /* TX_SYMLINK */ 502 zvol_replay_err, /* TX_REMOVE */ 503 zvol_replay_err, /* TX_RMDIR */ 504 zvol_replay_err, /* TX_LINK */ 505 zvol_replay_err, /* TX_RENAME */ 506 zvol_replay_write, /* TX_WRITE */ 507 zvol_replay_truncate, /* TX_TRUNCATE */ 508 zvol_replay_err, /* TX_SETATTR */ 509 zvol_replay_err, /* TX_ACL */ 510 zvol_replay_err, /* TX_CREATE_ACL */ 511 zvol_replay_err, /* TX_CREATE_ATTR */ 512 zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 513 zvol_replay_err, /* TX_MKDIR_ACL */ 514 zvol_replay_err, /* TX_MKDIR_ATTR */ 515 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 516 zvol_replay_err, /* TX_WRITE2 */ 517}; 518 519#ifdef sun 520int 521zvol_name2minor(const char *name, minor_t *minor) 522{ 523 zvol_state_t *zv; 524 525 mutex_enter(&spa_namespace_lock); 526 zv = zvol_minor_lookup(name); 527 if (minor && zv) 528 *minor = zv->zv_minor; 529 mutex_exit(&spa_namespace_lock); 530 return (zv ? 0 : -1); 531} 532#endif /* sun */ 533 534/* 535 * Create a minor node (plus a whole lot more) for the specified volume. 536 */ 537int 538zvol_create_minor(const char *name) 539{ 540 zfs_soft_state_t *zs; 541 zvol_state_t *zv; 542 objset_t *os; 543 struct cdev *dev; 544 struct g_provider *pp; 545 struct g_geom *gp; 546 dmu_object_info_t doi; 547 uint64_t volsize, mode; 548 int error; 549 550 ZFS_LOG(1, "Creating ZVOL %s...", name); 551 552 mutex_enter(&spa_namespace_lock); 553 554 if (zvol_minor_lookup(name) != NULL) { 555 mutex_exit(&spa_namespace_lock); 556 return (SET_ERROR(EEXIST)); 557 } 558 559 /* lie and say we're read-only */ 560 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os); 561 562 if (error) { 563 mutex_exit(&spa_namespace_lock); 564 return (error); 565 } 566 567#ifdef sun 568 if ((minor = zfsdev_minor_alloc()) == 0) { 569 dmu_objset_disown(os, FTAG); 570 mutex_exit(&spa_namespace_lock); 571 return (SET_ERROR(ENXIO)); 572 } 573 574 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) { 575 dmu_objset_disown(os, FTAG); 576 mutex_exit(&spa_namespace_lock); 577 return (SET_ERROR(EAGAIN)); 578 } 579 (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 580 (char *)name); 581 582 (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 583 584 if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 585 minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 586 ddi_soft_state_free(zfsdev_state, minor); 587 dmu_objset_disown(os, FTAG); 588 mutex_exit(&spa_namespace_lock); 589 return (SET_ERROR(EAGAIN)); 590 } 591 592 (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 593 594 if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 595 minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 596 ddi_remove_minor_node(zfs_dip, chrbuf); 597 ddi_soft_state_free(zfsdev_state, minor); 598 dmu_objset_disown(os, FTAG); 599 mutex_exit(&spa_namespace_lock); 600 return (SET_ERROR(EAGAIN)); 601 } 602 603 zs = ddi_get_soft_state(zfsdev_state, minor); 604 zs->zss_type = ZSST_ZVOL; 605 zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP); 606#else /* !sun */ 607 608 zv = kmem_zalloc(sizeof(*zv), KM_SLEEP); 609 zv->zv_state = 0; 610 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 611 if (error) { 612 kmem_free(zv, sizeof(*zv)); 613 dmu_objset_disown(os, zvol_tag); 614 mutex_exit(&spa_namespace_lock); 615 return (error); 616 } 617 error = dsl_prop_get_integer(name, 618 zfs_prop_to_name(ZFS_PROP_VOLMODE), &mode, NULL); 619 if (error != 0 || mode == ZFS_VOLMODE_DEFAULT) 620 mode = volmode; 621 622 DROP_GIANT(); 623 zv->zv_volsize = volsize; 624 zv->zv_volmode = mode; 625 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 626 g_topology_lock(); 627 gp = g_new_geomf(&zfs_zvol_class, "zfs::zvol::%s", name); 628 gp->start = zvol_geom_start; 629 gp->access = zvol_geom_access; 630 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, name); 631 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND; 632 pp->sectorsize = DEV_BSIZE; 633 pp->mediasize = zv->zv_volsize; 634 pp->private = zv; 635 636 zv->zv_provider = pp; 637 bioq_init(&zv->zv_queue); 638 mtx_init(&zv->zv_queue_mtx, "zvol", NULL, MTX_DEF); 639 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) { 640 if (make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, 641 &dev, &zvol_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 642 0640, "%s/%s", ZVOL_DRIVER, name) != 0) { 643 kmem_free(zv, sizeof(*zv)); 644 dmu_objset_disown(os, FTAG); 645 mutex_exit(&spa_namespace_lock); 646 return (SET_ERROR(ENXIO)); 647 } 648 zv->zv_dev = dev; 649 dev->si_iosize_max = MAXPHYS; 650 dev->si_drv2 = zv; 651 } 652 LIST_INSERT_HEAD(&all_zvols, zv, zv_links); 653#endif /* !sun */ 654 655 (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 656 zv->zv_min_bs = DEV_BSHIFT; 657 zv->zv_objset = os; 658 if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os))) 659 zv->zv_flags |= ZVOL_RDONLY; 660 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 661 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 662 sizeof (rl_t), offsetof(rl_t, r_node)); 663 list_create(&zv->zv_extents, sizeof (zvol_extent_t), 664 offsetof(zvol_extent_t, ze_node)); 665 /* get and cache the blocksize */ 666 error = dmu_object_info(os, ZVOL_OBJ, &doi); 667 ASSERT(error == 0); 668 zv->zv_volblocksize = doi.doi_data_block_size; 669 670 if (spa_writeable(dmu_objset_spa(os))) { 671 if (zil_replay_disable) 672 zil_destroy(dmu_objset_zil(os), B_FALSE); 673 else 674 zil_replay(os, zv, zvol_replay_vector); 675 } 676 dmu_objset_disown(os, FTAG); 677 zv->zv_objset = NULL; 678 679 zvol_minors++; 680 681 mutex_exit(&spa_namespace_lock); 682 683#ifndef sun 684 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 685 zvol_geom_run(zv); 686 g_topology_unlock(); 687 } 688 PICKUP_GIANT(); 689#endif 690 691 ZFS_LOG(1, "ZVOL %s created.", name); 692 693 return (0); 694} 695 696/* 697 * Remove minor node for the specified volume. 698 */ 699static int 700zvol_remove_zv(zvol_state_t *zv) 701{ 702#ifdef sun 703 minor_t minor = zv->zv_minor; 704#endif 705 706 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 707 if (zv->zv_total_opens != 0) 708 return (SET_ERROR(EBUSY)); 709 710 ZFS_LOG(1, "ZVOL %s destroyed.", zv->zv_name); 711 712#ifdef sun 713 (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor); 714 ddi_remove_minor_node(zfs_dip, nmbuf); 715#else 716 LIST_REMOVE(zv, zv_links); 717 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 718 g_topology_lock(); 719 zvol_geom_destroy(zv); 720 g_topology_unlock(); 721 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) 722 destroy_dev(zv->zv_dev); 723#endif /* sun */ 724 725 avl_destroy(&zv->zv_znode.z_range_avl); 726 mutex_destroy(&zv->zv_znode.z_range_lock); 727 728 kmem_free(zv, sizeof(*zv)); 729 730 zvol_minors--; 731 return (0); 732} 733 734int 735zvol_remove_minor(const char *name) 736{ 737 zvol_state_t *zv; 738 int rc; 739 740 mutex_enter(&spa_namespace_lock); 741 if ((zv = zvol_minor_lookup(name)) == NULL) { 742 mutex_exit(&spa_namespace_lock); 743 return (SET_ERROR(ENXIO)); 744 } 745 rc = zvol_remove_zv(zv); 746 mutex_exit(&spa_namespace_lock); 747 return (rc); 748} 749 750int 751zvol_first_open(zvol_state_t *zv) 752{ 753 objset_t *os; 754 uint64_t volsize; 755 int error; 756 uint64_t readonly; 757 758 /* lie and say we're read-only */ 759 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 760 zvol_tag, &os); 761 if (error) 762 return (error); 763 764 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 765 if (error) { 766 ASSERT(error == 0); 767 dmu_objset_disown(os, zvol_tag); 768 return (error); 769 } 770 zv->zv_objset = os; 771 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); 772 if (error) { 773 dmu_objset_disown(os, zvol_tag); 774 return (error); 775 } 776 zv->zv_volsize = volsize; 777 zv->zv_zilog = zil_open(os, zvol_get_data); 778 zvol_size_changed(zv); 779 780 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 781 NULL) == 0); 782 if (readonly || dmu_objset_is_snapshot(os) || 783 !spa_writeable(dmu_objset_spa(os))) 784 zv->zv_flags |= ZVOL_RDONLY; 785 else 786 zv->zv_flags &= ~ZVOL_RDONLY; 787 return (error); 788} 789 790void 791zvol_last_close(zvol_state_t *zv) 792{ 793 zil_close(zv->zv_zilog); 794 zv->zv_zilog = NULL; 795 796 dmu_buf_rele(zv->zv_dbuf, zvol_tag); 797 zv->zv_dbuf = NULL; 798 799 /* 800 * Evict cached data 801 */ 802 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) && 803 !(zv->zv_flags & ZVOL_RDONLY)) 804 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 805 dmu_objset_evict_dbufs(zv->zv_objset); 806 807 dmu_objset_disown(zv->zv_objset, zvol_tag); 808 zv->zv_objset = NULL; 809} 810 811#ifdef sun 812int 813zvol_prealloc(zvol_state_t *zv) 814{ 815 objset_t *os = zv->zv_objset; 816 dmu_tx_t *tx; 817 uint64_t refd, avail, usedobjs, availobjs; 818 uint64_t resid = zv->zv_volsize; 819 uint64_t off = 0; 820 821 /* Check the space usage before attempting to allocate the space */ 822 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 823 if (avail < zv->zv_volsize) 824 return (SET_ERROR(ENOSPC)); 825 826 /* Free old extents if they exist */ 827 zvol_free_extents(zv); 828 829 while (resid != 0) { 830 int error; 831 uint64_t bytes = MIN(resid, SPA_OLD_MAXBLOCKSIZE); 832 833 tx = dmu_tx_create(os); 834 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 835 error = dmu_tx_assign(tx, TXG_WAIT); 836 if (error) { 837 dmu_tx_abort(tx); 838 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 839 return (error); 840 } 841 dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 842 dmu_tx_commit(tx); 843 off += bytes; 844 resid -= bytes; 845 } 846 txg_wait_synced(dmu_objset_pool(os), 0); 847 848 return (0); 849} 850#endif /* sun */ 851 852static int 853zvol_update_volsize(objset_t *os, uint64_t volsize) 854{ 855 dmu_tx_t *tx; 856 int error; 857 858 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 859 860 tx = dmu_tx_create(os); 861 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 862 dmu_tx_mark_netfree(tx); 863 error = dmu_tx_assign(tx, TXG_WAIT); 864 if (error) { 865 dmu_tx_abort(tx); 866 return (error); 867 } 868 869 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 870 &volsize, tx); 871 dmu_tx_commit(tx); 872 873 if (error == 0) 874 error = dmu_free_long_range(os, 875 ZVOL_OBJ, volsize, DMU_OBJECT_END); 876 return (error); 877} 878 879void 880zvol_remove_minors(const char *name) 881{ 882 zvol_state_t *zv, *tzv; 883 size_t namelen; 884 885 namelen = strlen(name); 886 887 DROP_GIANT(); 888 mutex_enter(&spa_namespace_lock); 889 890 LIST_FOREACH_SAFE(zv, &all_zvols, zv_links, tzv) { 891 if (strcmp(zv->zv_name, name) == 0 || 892 (strncmp(zv->zv_name, name, namelen) == 0 && 893 strlen(zv->zv_name) > namelen && (zv->zv_name[namelen] == '/' || 894 zv->zv_name[namelen] == '@'))) { 895 (void) zvol_remove_zv(zv); 896 } 897 } 898 899 mutex_exit(&spa_namespace_lock); 900 PICKUP_GIANT(); 901} 902 903int 904zvol_set_volsize(const char *name, major_t maj, uint64_t volsize) 905{ 906 zvol_state_t *zv = NULL; 907 objset_t *os; 908 int error; 909 dmu_object_info_t doi; 910 uint64_t old_volsize = 0ULL; 911 uint64_t readonly; 912 913 mutex_enter(&spa_namespace_lock); 914 zv = zvol_minor_lookup(name); 915 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 916 mutex_exit(&spa_namespace_lock); 917 return (error); 918 } 919 920 if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 921 (error = zvol_check_volsize(volsize, 922 doi.doi_data_block_size)) != 0) 923 goto out; 924 925 VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, 926 NULL) == 0); 927 if (readonly) { 928 error = EROFS; 929 goto out; 930 } 931 932 error = zvol_update_volsize(os, volsize); 933 /* 934 * Reinitialize the dump area to the new size. If we 935 * failed to resize the dump area then restore it back to 936 * its original size. 937 */ 938 if (zv && error == 0) { 939#ifdef ZVOL_DUMP 940 if (zv->zv_flags & ZVOL_DUMPIFIED) { 941 old_volsize = zv->zv_volsize; 942 zv->zv_volsize = volsize; 943 if ((error = zvol_dumpify(zv)) != 0 || 944 (error = dumpvp_resize()) != 0) { 945 (void) zvol_update_volsize(os, old_volsize); 946 zv->zv_volsize = old_volsize; 947 error = zvol_dumpify(zv); 948 } 949 } 950#endif /* ZVOL_DUMP */ 951 if (error == 0) { 952 zv->zv_volsize = volsize; 953 zvol_size_changed(zv); 954 } 955 } 956 957#ifdef sun 958 /* 959 * Generate a LUN expansion event. 960 */ 961 if (zv && error == 0) { 962 sysevent_id_t eid; 963 nvlist_t *attr; 964 char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 965 966 (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 967 zv->zv_minor); 968 969 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 970 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 971 972 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 973 ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 974 975 nvlist_free(attr); 976 kmem_free(physpath, MAXPATHLEN); 977 } 978#endif /* sun */ 979 980out: 981 dmu_objset_rele(os, FTAG); 982 983 mutex_exit(&spa_namespace_lock); 984 985 return (error); 986} 987 988/*ARGSUSED*/ 989static int 990zvol_open(struct g_provider *pp, int flag, int count) 991{ 992 zvol_state_t *zv; 993 int err = 0; 994 boolean_t locked = B_FALSE; 995 996 /* 997 * Protect against recursively entering spa_namespace_lock 998 * when spa_open() is used for a pool on a (local) ZVOL(s). 999 * This is needed since we replaced upstream zfsdev_state_lock 1000 * with spa_namespace_lock in the ZVOL code. 1001 * We are using the same trick as spa_open(). 1002 * Note that calls in zvol_first_open which need to resolve 1003 * pool name to a spa object will enter spa_open() 1004 * recursively, but that function already has all the 1005 * necessary protection. 1006 */ 1007 if (!MUTEX_HELD(&spa_namespace_lock)) { 1008 mutex_enter(&spa_namespace_lock); 1009 locked = B_TRUE; 1010 } 1011 1012 zv = pp->private; 1013 if (zv == NULL) { 1014 if (locked) 1015 mutex_exit(&spa_namespace_lock); 1016 return (SET_ERROR(ENXIO)); 1017 } 1018 1019 if (zv->zv_total_opens == 0) { 1020 err = zvol_first_open(zv); 1021 if (err) { 1022 if (locked) 1023 mutex_exit(&spa_namespace_lock); 1024 return (err); 1025 } 1026 pp->mediasize = zv->zv_volsize; 1027 pp->stripeoffset = 0; 1028 pp->stripesize = zv->zv_volblocksize; 1029 } 1030 if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 1031 err = SET_ERROR(EROFS); 1032 goto out; 1033 } 1034 if (zv->zv_flags & ZVOL_EXCL) { 1035 err = SET_ERROR(EBUSY); 1036 goto out; 1037 } 1038#ifdef FEXCL 1039 if (flag & FEXCL) { 1040 if (zv->zv_total_opens != 0) { 1041 err = SET_ERROR(EBUSY); 1042 goto out; 1043 } 1044 zv->zv_flags |= ZVOL_EXCL; 1045 } 1046#endif 1047 1048 zv->zv_total_opens += count; 1049 if (locked) 1050 mutex_exit(&spa_namespace_lock); 1051 1052 return (err); 1053out: 1054 if (zv->zv_total_opens == 0) 1055 zvol_last_close(zv); 1056 if (locked) 1057 mutex_exit(&spa_namespace_lock); 1058 return (err); 1059} 1060 1061/*ARGSUSED*/ 1062static int 1063zvol_close(struct g_provider *pp, int flag, int count) 1064{ 1065 zvol_state_t *zv; 1066 int error = 0; 1067 boolean_t locked = B_FALSE; 1068 1069 /* See comment in zvol_open(). */ 1070 if (!MUTEX_HELD(&spa_namespace_lock)) { 1071 mutex_enter(&spa_namespace_lock); 1072 locked = B_TRUE; 1073 } 1074 1075 zv = pp->private; 1076 if (zv == NULL) { 1077 if (locked) 1078 mutex_exit(&spa_namespace_lock); 1079 return (SET_ERROR(ENXIO)); 1080 } 1081 1082 if (zv->zv_flags & ZVOL_EXCL) { 1083 ASSERT(zv->zv_total_opens == 1); 1084 zv->zv_flags &= ~ZVOL_EXCL; 1085 } 1086 1087 /* 1088 * If the open count is zero, this is a spurious close. 1089 * That indicates a bug in the kernel / DDI framework. 1090 */ 1091 ASSERT(zv->zv_total_opens != 0); 1092 1093 /* 1094 * You may get multiple opens, but only one close. 1095 */ 1096 zv->zv_total_opens -= count; 1097 1098 if (zv->zv_total_opens == 0) 1099 zvol_last_close(zv); 1100 1101 if (locked) 1102 mutex_exit(&spa_namespace_lock); 1103 return (error); 1104} 1105 1106static void 1107zvol_get_done(zgd_t *zgd, int error) 1108{ 1109 if (zgd->zgd_db) 1110 dmu_buf_rele(zgd->zgd_db, zgd); 1111 1112 zfs_range_unlock(zgd->zgd_rl); 1113 1114 if (error == 0 && zgd->zgd_bp) 1115 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 1116 1117 kmem_free(zgd, sizeof (zgd_t)); 1118} 1119 1120/* 1121 * Get data to generate a TX_WRITE intent log record. 1122 */ 1123static int 1124zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 1125{ 1126 zvol_state_t *zv = arg; 1127 objset_t *os = zv->zv_objset; 1128 uint64_t object = ZVOL_OBJ; 1129 uint64_t offset = lr->lr_offset; 1130 uint64_t size = lr->lr_length; /* length of user data */ 1131 blkptr_t *bp = &lr->lr_blkptr; 1132 dmu_buf_t *db; 1133 zgd_t *zgd; 1134 int error; 1135 1136 ASSERT(zio != NULL); 1137 ASSERT(size != 0); 1138 1139 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 1140 zgd->zgd_zilog = zv->zv_zilog; 1141 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); 1142 1143 /* 1144 * Write records come in two flavors: immediate and indirect. 1145 * For small writes it's cheaper to store the data with the 1146 * log record (immediate); for large writes it's cheaper to 1147 * sync the data and get a pointer to it (indirect) so that 1148 * we don't have to write the data twice. 1149 */ 1150 if (buf != NULL) { /* immediate write */ 1151 error = dmu_read(os, object, offset, size, buf, 1152 DMU_READ_NO_PREFETCH); 1153 } else { 1154 size = zv->zv_volblocksize; 1155 offset = P2ALIGN(offset, size); 1156 error = dmu_buf_hold(os, object, offset, zgd, &db, 1157 DMU_READ_NO_PREFETCH); 1158 if (error == 0) { 1159 blkptr_t *obp = dmu_buf_get_blkptr(db); 1160 if (obp) { 1161 ASSERT(BP_IS_HOLE(bp)); 1162 *bp = *obp; 1163 } 1164 1165 zgd->zgd_db = db; 1166 zgd->zgd_bp = bp; 1167 1168 ASSERT(db->db_offset == offset); 1169 ASSERT(db->db_size == size); 1170 1171 error = dmu_sync(zio, lr->lr_common.lrc_txg, 1172 zvol_get_done, zgd); 1173 1174 if (error == 0) 1175 return (0); 1176 } 1177 } 1178 1179 zvol_get_done(zgd, error); 1180 1181 return (error); 1182} 1183 1184/* 1185 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 1186 * 1187 * We store data in the log buffers if it's small enough. 1188 * Otherwise we will later flush the data out via dmu_sync(). 1189 */ 1190ssize_t zvol_immediate_write_sz = 32768; 1191 1192static void 1193zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 1194 boolean_t sync) 1195{ 1196 uint32_t blocksize = zv->zv_volblocksize; 1197 zilog_t *zilog = zv->zv_zilog; 1198 boolean_t slogging; 1199 ssize_t immediate_write_sz; 1200 1201 if (zil_replaying(zilog, tx)) 1202 return; 1203 1204 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 1205 ? 0 : zvol_immediate_write_sz; 1206 1207 slogging = spa_has_slogs(zilog->zl_spa) && 1208 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 1209 1210 while (resid) { 1211 itx_t *itx; 1212 lr_write_t *lr; 1213 ssize_t len; 1214 itx_wr_state_t write_state; 1215 1216 /* 1217 * Unlike zfs_log_write() we can be called with 1218 * upto DMU_MAX_ACCESS/2 (5MB) writes. 1219 */ 1220 if (blocksize > immediate_write_sz && !slogging && 1221 resid >= blocksize && off % blocksize == 0) { 1222 write_state = WR_INDIRECT; /* uses dmu_sync */ 1223 len = blocksize; 1224 } else if (sync) { 1225 write_state = WR_COPIED; 1226 len = MIN(ZIL_MAX_LOG_DATA, resid); 1227 } else { 1228 write_state = WR_NEED_COPY; 1229 len = MIN(ZIL_MAX_LOG_DATA, resid); 1230 } 1231 1232 itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1233 (write_state == WR_COPIED ? len : 0)); 1234 lr = (lr_write_t *)&itx->itx_lr; 1235 if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 1236 ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1237 zil_itx_destroy(itx); 1238 itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1239 lr = (lr_write_t *)&itx->itx_lr; 1240 write_state = WR_NEED_COPY; 1241 } 1242 1243 itx->itx_wr_state = write_state; 1244 if (write_state == WR_NEED_COPY) 1245 itx->itx_sod += len; 1246 lr->lr_foid = ZVOL_OBJ; 1247 lr->lr_offset = off; 1248 lr->lr_length = len; 1249 lr->lr_blkoff = 0; 1250 BP_ZERO(&lr->lr_blkptr); 1251 1252 itx->itx_private = zv; 1253 itx->itx_sync = sync; 1254 1255 zil_itx_assign(zilog, itx, tx); 1256 1257 off += len; 1258 resid -= len; 1259 } 1260} 1261 1262#ifdef sun 1263static int 1264zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset, 1265 uint64_t size, boolean_t doread, boolean_t isdump) 1266{ 1267 vdev_disk_t *dvd; 1268 int c; 1269 int numerrors = 0; 1270 1271 if (vd->vdev_ops == &vdev_mirror_ops || 1272 vd->vdev_ops == &vdev_replacing_ops || 1273 vd->vdev_ops == &vdev_spare_ops) { 1274 for (c = 0; c < vd->vdev_children; c++) { 1275 int err = zvol_dumpio_vdev(vd->vdev_child[c], 1276 addr, offset, origoffset, size, doread, isdump); 1277 if (err != 0) { 1278 numerrors++; 1279 } else if (doread) { 1280 break; 1281 } 1282 } 1283 } 1284 1285 if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops) 1286 return (numerrors < vd->vdev_children ? 0 : EIO); 1287 1288 if (doread && !vdev_readable(vd)) 1289 return (SET_ERROR(EIO)); 1290 else if (!doread && !vdev_writeable(vd)) 1291 return (SET_ERROR(EIO)); 1292 1293 if (vd->vdev_ops == &vdev_raidz_ops) { 1294 return (vdev_raidz_physio(vd, 1295 addr, size, offset, origoffset, doread, isdump)); 1296 } 1297 1298 offset += VDEV_LABEL_START_SIZE; 1299 1300 if (ddi_in_panic() || isdump) { 1301 ASSERT(!doread); 1302 if (doread) 1303 return (SET_ERROR(EIO)); 1304 dvd = vd->vdev_tsd; 1305 ASSERT3P(dvd, !=, NULL); 1306 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1307 lbtodb(size))); 1308 } else { 1309 dvd = vd->vdev_tsd; 1310 ASSERT3P(dvd, !=, NULL); 1311 return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size, 1312 offset, doread ? B_READ : B_WRITE)); 1313 } 1314} 1315 1316static int 1317zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 1318 boolean_t doread, boolean_t isdump) 1319{ 1320 vdev_t *vd; 1321 int error; 1322 zvol_extent_t *ze; 1323 spa_t *spa = dmu_objset_spa(zv->zv_objset); 1324 1325 /* Must be sector aligned, and not stradle a block boundary. */ 1326 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 1327 P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 1328 return (SET_ERROR(EINVAL)); 1329 } 1330 ASSERT(size <= zv->zv_volblocksize); 1331 1332 /* Locate the extent this belongs to */ 1333 ze = list_head(&zv->zv_extents); 1334 while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 1335 offset -= ze->ze_nblks * zv->zv_volblocksize; 1336 ze = list_next(&zv->zv_extents, ze); 1337 } 1338 1339 if (ze == NULL) 1340 return (SET_ERROR(EINVAL)); 1341 1342 if (!ddi_in_panic()) 1343 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 1344 1345 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 1346 offset += DVA_GET_OFFSET(&ze->ze_dva); 1347 error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva), 1348 size, doread, isdump); 1349 1350 if (!ddi_in_panic()) 1351 spa_config_exit(spa, SCL_STATE, FTAG); 1352 1353 return (error); 1354} 1355#endif /* sun */ 1356 1357void 1358zvol_strategy(struct bio *bp) 1359{ 1360 zvol_state_t *zv; 1361 uint64_t off, volsize; 1362 size_t resid; 1363 char *addr; 1364 objset_t *os; 1365 rl_t *rl; 1366 int error = 0; 1367 boolean_t doread = 0; 1368 boolean_t is_dumpified; 1369 boolean_t sync; 1370 1371 if (bp->bio_to) 1372 zv = bp->bio_to->private; 1373 else 1374 zv = bp->bio_dev->si_drv2; 1375 1376 if (zv == NULL) { 1377 error = ENXIO; 1378 goto out; 1379 } 1380 1381 if (bp->bio_cmd != BIO_READ && (zv->zv_flags & ZVOL_RDONLY)) { 1382 error = EROFS; 1383 goto out; 1384 } 1385 1386 switch (bp->bio_cmd) { 1387 case BIO_FLUSH: 1388 goto sync; 1389 case BIO_READ: 1390 doread = 1; 1391 case BIO_WRITE: 1392 case BIO_DELETE: 1393 break; 1394 default: 1395 error = EOPNOTSUPP; 1396 goto out; 1397 } 1398 1399 off = bp->bio_offset; 1400 volsize = zv->zv_volsize; 1401 1402 os = zv->zv_objset; 1403 ASSERT(os != NULL); 1404 1405 addr = bp->bio_data; 1406 resid = bp->bio_length; 1407 1408 if (resid > 0 && (off < 0 || off >= volsize)) { 1409 error = EIO; 1410 goto out; 1411 } 1412 1413#ifdef illumos 1414 is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED; 1415#else 1416 is_dumpified = B_FALSE; 1417#endif 1418 sync = !doread && !is_dumpified && 1419 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS; 1420 1421 /* 1422 * There must be no buffer changes when doing a dmu_sync() because 1423 * we can't change the data whilst calculating the checksum. 1424 */ 1425 rl = zfs_range_lock(&zv->zv_znode, off, resid, 1426 doread ? RL_READER : RL_WRITER); 1427 1428 if (bp->bio_cmd == BIO_DELETE) { 1429 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 1430 error = dmu_tx_assign(tx, TXG_WAIT); 1431 if (error != 0) { 1432 dmu_tx_abort(tx); 1433 } else { 1434 zvol_log_truncate(zv, tx, off, resid, B_TRUE); 1435 dmu_tx_commit(tx); 1436 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 1437 off, resid); 1438 resid = 0; 1439 } 1440 goto unlock; 1441 } 1442 1443 while (resid != 0 && off < volsize) { 1444 size_t size = MIN(resid, zvol_maxphys); 1445#ifdef illumos 1446 if (is_dumpified) { 1447 size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 1448 error = zvol_dumpio(zv, addr, off, size, 1449 doread, B_FALSE); 1450 } else if (doread) { 1451#else 1452 if (doread) { 1453#endif 1454 error = dmu_read(os, ZVOL_OBJ, off, size, addr, 1455 DMU_READ_PREFETCH); 1456 } else { 1457 dmu_tx_t *tx = dmu_tx_create(os); 1458 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1459 error = dmu_tx_assign(tx, TXG_WAIT); 1460 if (error) { 1461 dmu_tx_abort(tx); 1462 } else { 1463 dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1464 zvol_log_write(zv, tx, off, size, sync); 1465 dmu_tx_commit(tx); 1466 } 1467 } 1468 if (error) { 1469 /* convert checksum errors into IO errors */ 1470 if (error == ECKSUM) 1471 error = SET_ERROR(EIO); 1472 break; 1473 } 1474 off += size; 1475 addr += size; 1476 resid -= size; 1477 } 1478unlock: 1479 zfs_range_unlock(rl); 1480 1481 bp->bio_completed = bp->bio_length - resid; 1482 if (bp->bio_completed < bp->bio_length && off > volsize) 1483 error = EINVAL; 1484 1485 if (sync) { 1486sync: 1487 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1488 } 1489out: 1490 if (bp->bio_to) 1491 g_io_deliver(bp, error); 1492 else 1493 biofinish(bp, NULL, error); 1494} 1495 1496#ifdef sun 1497/* 1498 * Set the buffer count to the zvol maximum transfer. 1499 * Using our own routine instead of the default minphys() 1500 * means that for larger writes we write bigger buffers on X86 1501 * (128K instead of 56K) and flush the disk write cache less often 1502 * (every zvol_maxphys - currently 1MB) instead of minphys (currently 1503 * 56K on X86 and 128K on sparc). 1504 */ 1505void 1506zvol_minphys(struct buf *bp) 1507{ 1508 if (bp->b_bcount > zvol_maxphys) 1509 bp->b_bcount = zvol_maxphys; 1510} 1511 1512int 1513zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1514{ 1515 minor_t minor = getminor(dev); 1516 zvol_state_t *zv; 1517 int error = 0; 1518 uint64_t size; 1519 uint64_t boff; 1520 uint64_t resid; 1521 1522 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1523 if (zv == NULL) 1524 return (SET_ERROR(ENXIO)); 1525 1526 if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0) 1527 return (SET_ERROR(EINVAL)); 1528 1529 boff = ldbtob(blkno); 1530 resid = ldbtob(nblocks); 1531 1532 VERIFY3U(boff + resid, <=, zv->zv_volsize); 1533 1534 while (resid) { 1535 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 1536 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1537 if (error) 1538 break; 1539 boff += size; 1540 addr += size; 1541 resid -= size; 1542 } 1543 1544 return (error); 1545} 1546 1547/*ARGSUSED*/ 1548int 1549zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1550{ 1551 minor_t minor = getminor(dev); 1552#else 1553int 1554zvol_read(struct cdev *dev, struct uio *uio, int ioflag) 1555{ 1556#endif 1557 zvol_state_t *zv; 1558 uint64_t volsize; 1559 rl_t *rl; 1560 int error = 0; 1561 1562#ifdef sun 1563 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1564 if (zv == NULL) 1565 return (SET_ERROR(ENXIO)); 1566#else 1567 zv = dev->si_drv2; 1568#endif 1569 1570 volsize = zv->zv_volsize; 1571 if (uio->uio_resid > 0 && 1572 (uio->uio_loffset < 0 || uio->uio_loffset > volsize)) 1573 return (SET_ERROR(EIO)); 1574 1575#ifdef illumos 1576 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1577 error = physio(zvol_strategy, NULL, dev, B_READ, 1578 zvol_minphys, uio); 1579 return (error); 1580 } 1581#endif 1582 1583 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1584 RL_READER); 1585 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1586 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1587 1588 /* don't read past the end */ 1589 if (bytes > volsize - uio->uio_loffset) 1590 bytes = volsize - uio->uio_loffset; 1591 1592 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1593 if (error) { 1594 /* convert checksum errors into IO errors */ 1595 if (error == ECKSUM) 1596 error = SET_ERROR(EIO); 1597 break; 1598 } 1599 } 1600 zfs_range_unlock(rl); 1601 return (error); 1602} 1603 1604#ifdef sun 1605/*ARGSUSED*/ 1606int 1607zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1608{ 1609 minor_t minor = getminor(dev); 1610#else 1611int 1612zvol_write(struct cdev *dev, struct uio *uio, int ioflag) 1613{ 1614#endif 1615 zvol_state_t *zv; 1616 uint64_t volsize; 1617 rl_t *rl; 1618 int error = 0; 1619 boolean_t sync; 1620 1621#ifdef sun 1622 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1623 if (zv == NULL) 1624 return (SET_ERROR(ENXIO)); 1625#else 1626 zv = dev->si_drv2; 1627#endif 1628 1629 volsize = zv->zv_volsize; 1630 if (uio->uio_resid > 0 && 1631 (uio->uio_loffset < 0 || uio->uio_loffset > volsize)) 1632 return (SET_ERROR(EIO)); 1633 1634#ifdef illumos 1635 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1636 error = physio(zvol_strategy, NULL, dev, B_WRITE, 1637 zvol_minphys, uio); 1638 return (error); 1639 } 1640#endif 1641 1642#ifdef sun 1643 sync = !(zv->zv_flags & ZVOL_WCE) || 1644#else 1645 sync = (ioflag & IO_SYNC) || 1646#endif 1647 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS); 1648 1649 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1650 RL_WRITER); 1651 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1652 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1653 uint64_t off = uio->uio_loffset; 1654 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 1655 1656 if (bytes > volsize - off) /* don't write past the end */ 1657 bytes = volsize - off; 1658 1659 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1660 error = dmu_tx_assign(tx, TXG_WAIT); 1661 if (error) { 1662 dmu_tx_abort(tx); 1663 break; 1664 } 1665 error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx); 1666 if (error == 0) 1667 zvol_log_write(zv, tx, off, bytes, sync); 1668 dmu_tx_commit(tx); 1669 1670 if (error) 1671 break; 1672 } 1673 zfs_range_unlock(rl); 1674 if (sync) 1675 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1676 return (error); 1677} 1678 1679#ifdef sun 1680int 1681zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1682{ 1683 struct uuid uuid = EFI_RESERVED; 1684 efi_gpe_t gpe = { 0 }; 1685 uint32_t crc; 1686 dk_efi_t efi; 1687 int length; 1688 char *ptr; 1689 1690 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1691 return (SET_ERROR(EFAULT)); 1692 ptr = (char *)(uintptr_t)efi.dki_data_64; 1693 length = efi.dki_length; 1694 /* 1695 * Some clients may attempt to request a PMBR for the 1696 * zvol. Currently this interface will return EINVAL to 1697 * such requests. These requests could be supported by 1698 * adding a check for lba == 0 and consing up an appropriate 1699 * PMBR. 1700 */ 1701 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1702 return (SET_ERROR(EINVAL)); 1703 1704 gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1705 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1706 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1707 1708 if (efi.dki_lba == 1) { 1709 efi_gpt_t gpt = { 0 }; 1710 1711 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1712 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1713 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1714 gpt.efi_gpt_MyLBA = LE_64(1ULL); 1715 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1716 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1717 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1718 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1719 gpt.efi_gpt_SizeOfPartitionEntry = 1720 LE_32(sizeof (efi_gpe_t)); 1721 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1722 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1723 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1724 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1725 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1726 flag)) 1727 return (SET_ERROR(EFAULT)); 1728 ptr += sizeof (gpt); 1729 length -= sizeof (gpt); 1730 } 1731 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1732 length), flag)) 1733 return (SET_ERROR(EFAULT)); 1734 return (0); 1735} 1736 1737/* 1738 * BEGIN entry points to allow external callers access to the volume. 1739 */ 1740/* 1741 * Return the volume parameters needed for access from an external caller. 1742 * These values are invariant as long as the volume is held open. 1743 */ 1744int 1745zvol_get_volume_params(minor_t minor, uint64_t *blksize, 1746 uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl, 1747 void **rl_hdl, void **bonus_hdl) 1748{ 1749 zvol_state_t *zv; 1750 1751 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1752 if (zv == NULL) 1753 return (SET_ERROR(ENXIO)); 1754 if (zv->zv_flags & ZVOL_DUMPIFIED) 1755 return (SET_ERROR(ENXIO)); 1756 1757 ASSERT(blksize && max_xfer_len && minor_hdl && 1758 objset_hdl && zil_hdl && rl_hdl && bonus_hdl); 1759 1760 *blksize = zv->zv_volblocksize; 1761 *max_xfer_len = (uint64_t)zvol_maxphys; 1762 *minor_hdl = zv; 1763 *objset_hdl = zv->zv_objset; 1764 *zil_hdl = zv->zv_zilog; 1765 *rl_hdl = &zv->zv_znode; 1766 *bonus_hdl = zv->zv_dbuf; 1767 return (0); 1768} 1769 1770/* 1771 * Return the current volume size to an external caller. 1772 * The size can change while the volume is open. 1773 */ 1774uint64_t 1775zvol_get_volume_size(void *minor_hdl) 1776{ 1777 zvol_state_t *zv = minor_hdl; 1778 1779 return (zv->zv_volsize); 1780} 1781 1782/* 1783 * Return the current WCE setting to an external caller. 1784 * The WCE setting can change while the volume is open. 1785 */ 1786int 1787zvol_get_volume_wce(void *minor_hdl) 1788{ 1789 zvol_state_t *zv = minor_hdl; 1790 1791 return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0); 1792} 1793 1794/* 1795 * Entry point for external callers to zvol_log_write 1796 */ 1797void 1798zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid, 1799 boolean_t sync) 1800{ 1801 zvol_state_t *zv = minor_hdl; 1802 1803 zvol_log_write(zv, tx, off, resid, sync); 1804} 1805/* 1806 * END entry points to allow external callers access to the volume. 1807 */ 1808#endif /* sun */ 1809 1810/* 1811 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE. 1812 */ 1813static void 1814zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len, 1815 boolean_t sync) 1816{ 1817 itx_t *itx; 1818 lr_truncate_t *lr; 1819 zilog_t *zilog = zv->zv_zilog; 1820 1821 if (zil_replaying(zilog, tx)) 1822 return; 1823 1824 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); 1825 lr = (lr_truncate_t *)&itx->itx_lr; 1826 lr->lr_foid = ZVOL_OBJ; 1827 lr->lr_offset = off; 1828 lr->lr_length = len; 1829 1830 itx->itx_sync = sync; 1831 zil_itx_assign(zilog, itx, tx); 1832} 1833 1834#ifdef sun 1835/* 1836 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1837 * Also a dirtbag dkio ioctl for unmap/free-block functionality. 1838 */ 1839/*ARGSUSED*/ 1840int 1841zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1842{ 1843 zvol_state_t *zv; 1844 struct dk_callback *dkc; 1845 int error = 0; 1846 rl_t *rl; 1847 1848 mutex_enter(&spa_namespace_lock); 1849 1850 zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL); 1851 1852 if (zv == NULL) { 1853 mutex_exit(&spa_namespace_lock); 1854 return (SET_ERROR(ENXIO)); 1855 } 1856 ASSERT(zv->zv_total_opens > 0); 1857 1858 switch (cmd) { 1859 1860 case DKIOCINFO: 1861 { 1862 struct dk_cinfo dki; 1863 1864 bzero(&dki, sizeof (dki)); 1865 (void) strcpy(dki.dki_cname, "zvol"); 1866 (void) strcpy(dki.dki_dname, "zvol"); 1867 dki.dki_ctype = DKC_UNKNOWN; 1868 dki.dki_unit = getminor(dev); 1869 dki.dki_maxtransfer = 1870 1 << (SPA_OLD_MAXBLOCKSHIFT - zv->zv_min_bs); 1871 mutex_exit(&spa_namespace_lock); 1872 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1873 error = SET_ERROR(EFAULT); 1874 return (error); 1875 } 1876 1877 case DKIOCGMEDIAINFO: 1878 { 1879 struct dk_minfo dkm; 1880 1881 bzero(&dkm, sizeof (dkm)); 1882 dkm.dki_lbsize = 1U << zv->zv_min_bs; 1883 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1884 dkm.dki_media_type = DK_UNKNOWN; 1885 mutex_exit(&spa_namespace_lock); 1886 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1887 error = SET_ERROR(EFAULT); 1888 return (error); 1889 } 1890 1891 case DKIOCGMEDIAINFOEXT: 1892 { 1893 struct dk_minfo_ext dkmext; 1894 1895 bzero(&dkmext, sizeof (dkmext)); 1896 dkmext.dki_lbsize = 1U << zv->zv_min_bs; 1897 dkmext.dki_pbsize = zv->zv_volblocksize; 1898 dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1899 dkmext.dki_media_type = DK_UNKNOWN; 1900 mutex_exit(&spa_namespace_lock); 1901 if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag)) 1902 error = SET_ERROR(EFAULT); 1903 return (error); 1904 } 1905 1906 case DKIOCGETEFI: 1907 { 1908 uint64_t vs = zv->zv_volsize; 1909 uint8_t bs = zv->zv_min_bs; 1910 1911 mutex_exit(&spa_namespace_lock); 1912 error = zvol_getefi((void *)arg, flag, vs, bs); 1913 return (error); 1914 } 1915 1916 case DKIOCFLUSHWRITECACHE: 1917 dkc = (struct dk_callback *)arg; 1918 mutex_exit(&spa_namespace_lock); 1919 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1920 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1921 (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1922 error = 0; 1923 } 1924 return (error); 1925 1926 case DKIOCGETWCE: 1927 { 1928 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1929 if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1930 flag)) 1931 error = SET_ERROR(EFAULT); 1932 break; 1933 } 1934 case DKIOCSETWCE: 1935 { 1936 int wce; 1937 if (ddi_copyin((void *)arg, &wce, sizeof (int), 1938 flag)) { 1939 error = SET_ERROR(EFAULT); 1940 break; 1941 } 1942 if (wce) { 1943 zv->zv_flags |= ZVOL_WCE; 1944 mutex_exit(&spa_namespace_lock); 1945 } else { 1946 zv->zv_flags &= ~ZVOL_WCE; 1947 mutex_exit(&spa_namespace_lock); 1948 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1949 } 1950 return (0); 1951 } 1952 1953 case DKIOCGGEOM: 1954 case DKIOCGVTOC: 1955 /* 1956 * commands using these (like prtvtoc) expect ENOTSUP 1957 * since we're emulating an EFI label 1958 */ 1959 error = SET_ERROR(ENOTSUP); 1960 break; 1961 1962 case DKIOCDUMPINIT: 1963 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1964 RL_WRITER); 1965 error = zvol_dumpify(zv); 1966 zfs_range_unlock(rl); 1967 break; 1968 1969 case DKIOCDUMPFINI: 1970 if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 1971 break; 1972 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1973 RL_WRITER); 1974 error = zvol_dump_fini(zv); 1975 zfs_range_unlock(rl); 1976 break; 1977 1978 case DKIOCFREE: 1979 { 1980 dkioc_free_t df; 1981 dmu_tx_t *tx; 1982 1983 if (!zvol_unmap_enabled) 1984 break; 1985 1986 if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) { 1987 error = SET_ERROR(EFAULT); 1988 break; 1989 } 1990 1991 /* 1992 * Apply Postel's Law to length-checking. If they overshoot, 1993 * just blank out until the end, if there's a need to blank 1994 * out anything. 1995 */ 1996 if (df.df_start >= zv->zv_volsize) 1997 break; /* No need to do anything... */ 1998 1999 mutex_exit(&spa_namespace_lock); 2000 2001 rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length, 2002 RL_WRITER); 2003 tx = dmu_tx_create(zv->zv_objset); 2004 dmu_tx_mark_netfree(tx); 2005 error = dmu_tx_assign(tx, TXG_WAIT); 2006 if (error != 0) { 2007 dmu_tx_abort(tx); 2008 } else { 2009 zvol_log_truncate(zv, tx, df.df_start, 2010 df.df_length, B_TRUE); 2011 dmu_tx_commit(tx); 2012 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 2013 df.df_start, df.df_length); 2014 } 2015 2016 zfs_range_unlock(rl); 2017 2018 if (error == 0) { 2019 /* 2020 * If the write-cache is disabled or 'sync' property 2021 * is set to 'always' then treat this as a synchronous 2022 * operation (i.e. commit to zil). 2023 */ 2024 if (!(zv->zv_flags & ZVOL_WCE) || 2025 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) 2026 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2027 2028 /* 2029 * If the caller really wants synchronous writes, and 2030 * can't wait for them, don't return until the write 2031 * is done. 2032 */ 2033 if (df.df_flags & DF_WAIT_SYNC) { 2034 txg_wait_synced( 2035 dmu_objset_pool(zv->zv_objset), 0); 2036 } 2037 } 2038 return (error); 2039 } 2040 2041 default: 2042 error = SET_ERROR(ENOTTY); 2043 break; 2044 2045 } 2046 mutex_exit(&spa_namespace_lock); 2047 return (error); 2048} 2049#endif /* sun */ 2050 2051int 2052zvol_busy(void) 2053{ 2054 return (zvol_minors != 0); 2055} 2056 2057void 2058zvol_init(void) 2059{ 2060 VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t), 2061 1) == 0); 2062 ZFS_LOG(1, "ZVOL Initialized."); 2063} 2064 2065void 2066zvol_fini(void) 2067{ 2068 ddi_soft_state_fini(&zfsdev_state); 2069 ZFS_LOG(1, "ZVOL Deinitialized."); 2070} 2071 2072#ifdef sun 2073/*ARGSUSED*/ 2074static int 2075zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx) 2076{ 2077 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 2078 2079 if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 2080 return (1); 2081 return (0); 2082} 2083 2084/*ARGSUSED*/ 2085static void 2086zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx) 2087{ 2088 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 2089 2090 spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx); 2091} 2092 2093static int 2094zvol_dump_init(zvol_state_t *zv, boolean_t resize) 2095{ 2096 dmu_tx_t *tx; 2097 int error; 2098 objset_t *os = zv->zv_objset; 2099 spa_t *spa = dmu_objset_spa(os); 2100 vdev_t *vd = spa->spa_root_vdev; 2101 nvlist_t *nv = NULL; 2102 uint64_t version = spa_version(spa); 2103 enum zio_checksum checksum; 2104 2105 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2106 ASSERT(vd->vdev_ops == &vdev_root_ops); 2107 2108 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 2109 DMU_OBJECT_END); 2110 /* wait for dmu_free_long_range to actually free the blocks */ 2111 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 2112 2113 /* 2114 * If the pool on which the dump device is being initialized has more 2115 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is 2116 * enabled. If so, bump that feature's counter to indicate that the 2117 * feature is active. We also check the vdev type to handle the 2118 * following case: 2119 * # zpool create test raidz disk1 disk2 disk3 2120 * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev), 2121 * the raidz vdev itself has 3 children. 2122 */ 2123 if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) { 2124 if (!spa_feature_is_enabled(spa, 2125 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 2126 return (SET_ERROR(ENOTSUP)); 2127 (void) dsl_sync_task(spa_name(spa), 2128 zfs_mvdev_dump_feature_check, 2129 zfs_mvdev_dump_activate_feature_sync, NULL, 2130 2, ZFS_SPACE_CHECK_RESERVED); 2131 } 2132 2133 tx = dmu_tx_create(os); 2134 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2135 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 2136 error = dmu_tx_assign(tx, TXG_WAIT); 2137 if (error) { 2138 dmu_tx_abort(tx); 2139 return (error); 2140 } 2141 2142 /* 2143 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum 2144 * function. Otherwise, use the old default -- OFF. 2145 */ 2146 checksum = spa_feature_is_active(spa, 2147 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY : 2148 ZIO_CHECKSUM_OFF; 2149 2150 /* 2151 * If we are resizing the dump device then we only need to 2152 * update the refreservation to match the newly updated 2153 * zvolsize. Otherwise, we save off the original state of the 2154 * zvol so that we can restore them if the zvol is ever undumpified. 2155 */ 2156 if (resize) { 2157 error = zap_update(os, ZVOL_ZAP_OBJ, 2158 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 2159 &zv->zv_volsize, tx); 2160 } else { 2161 uint64_t checksum, compress, refresrv, vbs, dedup; 2162 2163 error = dsl_prop_get_integer(zv->zv_name, 2164 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 2165 error = error ? error : dsl_prop_get_integer(zv->zv_name, 2166 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 2167 error = error ? error : dsl_prop_get_integer(zv->zv_name, 2168 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 2169 error = error ? error : dsl_prop_get_integer(zv->zv_name, 2170 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 2171 if (version >= SPA_VERSION_DEDUP) { 2172 error = error ? error : 2173 dsl_prop_get_integer(zv->zv_name, 2174 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 2175 } 2176 2177 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2178 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 2179 &compress, tx); 2180 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2181 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 2182 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2183 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 2184 &refresrv, tx); 2185 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2186 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 2187 &vbs, tx); 2188 error = error ? error : dmu_object_set_blocksize( 2189 os, ZVOL_OBJ, SPA_OLD_MAXBLOCKSIZE, 0, tx); 2190 if (version >= SPA_VERSION_DEDUP) { 2191 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2192 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 2193 &dedup, tx); 2194 } 2195 if (error == 0) 2196 zv->zv_volblocksize = SPA_OLD_MAXBLOCKSIZE; 2197 } 2198 dmu_tx_commit(tx); 2199 2200 /* 2201 * We only need update the zvol's property if we are initializing 2202 * the dump area for the first time. 2203 */ 2204 if (!resize) { 2205 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2206 VERIFY(nvlist_add_uint64(nv, 2207 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 2208 VERIFY(nvlist_add_uint64(nv, 2209 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 2210 ZIO_COMPRESS_OFF) == 0); 2211 VERIFY(nvlist_add_uint64(nv, 2212 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 2213 checksum) == 0); 2214 if (version >= SPA_VERSION_DEDUP) { 2215 VERIFY(nvlist_add_uint64(nv, 2216 zfs_prop_to_name(ZFS_PROP_DEDUP), 2217 ZIO_CHECKSUM_OFF) == 0); 2218 } 2219 2220 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 2221 nv, NULL); 2222 nvlist_free(nv); 2223 2224 if (error) 2225 return (error); 2226 } 2227 2228 /* Allocate the space for the dump */ 2229 error = zvol_prealloc(zv); 2230 return (error); 2231} 2232 2233static int 2234zvol_dumpify(zvol_state_t *zv) 2235{ 2236 int error = 0; 2237 uint64_t dumpsize = 0; 2238 dmu_tx_t *tx; 2239 objset_t *os = zv->zv_objset; 2240 2241 if (zv->zv_flags & ZVOL_RDONLY) 2242 return (SET_ERROR(EROFS)); 2243 2244 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 2245 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 2246 boolean_t resize = (dumpsize > 0); 2247 2248 if ((error = zvol_dump_init(zv, resize)) != 0) { 2249 (void) zvol_dump_fini(zv); 2250 return (error); 2251 } 2252 } 2253 2254 /* 2255 * Build up our lba mapping. 2256 */ 2257 error = zvol_get_lbas(zv); 2258 if (error) { 2259 (void) zvol_dump_fini(zv); 2260 return (error); 2261 } 2262 2263 tx = dmu_tx_create(os); 2264 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2265 error = dmu_tx_assign(tx, TXG_WAIT); 2266 if (error) { 2267 dmu_tx_abort(tx); 2268 (void) zvol_dump_fini(zv); 2269 return (error); 2270 } 2271 2272 zv->zv_flags |= ZVOL_DUMPIFIED; 2273 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 2274 &zv->zv_volsize, tx); 2275 dmu_tx_commit(tx); 2276 2277 if (error) { 2278 (void) zvol_dump_fini(zv); 2279 return (error); 2280 } 2281 2282 txg_wait_synced(dmu_objset_pool(os), 0); 2283 return (0); 2284} 2285 2286static int 2287zvol_dump_fini(zvol_state_t *zv) 2288{ 2289 dmu_tx_t *tx; 2290 objset_t *os = zv->zv_objset; 2291 nvlist_t *nv; 2292 int error = 0; 2293 uint64_t checksum, compress, refresrv, vbs, dedup; 2294 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 2295 2296 /* 2297 * Attempt to restore the zvol back to its pre-dumpified state. 2298 * This is a best-effort attempt as it's possible that not all 2299 * of these properties were initialized during the dumpify process 2300 * (i.e. error during zvol_dump_init). 2301 */ 2302 2303 tx = dmu_tx_create(os); 2304 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2305 error = dmu_tx_assign(tx, TXG_WAIT); 2306 if (error) { 2307 dmu_tx_abort(tx); 2308 return (error); 2309 } 2310 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 2311 dmu_tx_commit(tx); 2312 2313 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2314 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 2315 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2316 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 2317 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2318 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 2319 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2320 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 2321 2322 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2323 (void) nvlist_add_uint64(nv, 2324 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 2325 (void) nvlist_add_uint64(nv, 2326 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 2327 (void) nvlist_add_uint64(nv, 2328 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 2329 if (version >= SPA_VERSION_DEDUP && 2330 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2331 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 2332 (void) nvlist_add_uint64(nv, 2333 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 2334 } 2335 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 2336 nv, NULL); 2337 nvlist_free(nv); 2338 2339 zvol_free_extents(zv); 2340 zv->zv_flags &= ~ZVOL_DUMPIFIED; 2341 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 2342 /* wait for dmu_free_long_range to actually free the blocks */ 2343 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 2344 tx = dmu_tx_create(os); 2345 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 2346 error = dmu_tx_assign(tx, TXG_WAIT); 2347 if (error) { 2348 dmu_tx_abort(tx); 2349 return (error); 2350 } 2351 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 2352 zv->zv_volblocksize = vbs; 2353 dmu_tx_commit(tx); 2354 2355 return (0); 2356} 2357#endif /* sun */ 2358 2359static void 2360zvol_geom_run(zvol_state_t *zv) 2361{ 2362 struct g_provider *pp; 2363 2364 pp = zv->zv_provider; 2365 g_error_provider(pp, 0); 2366 2367 kproc_kthread_add(zvol_geom_worker, zv, &zfsproc, NULL, 0, 0, 2368 "zfskern", "zvol %s", pp->name + sizeof(ZVOL_DRIVER)); 2369} 2370 2371static void 2372zvol_geom_destroy(zvol_state_t *zv) 2373{ 2374 struct g_provider *pp; 2375 2376 g_topology_assert(); 2377 2378 mtx_lock(&zv->zv_queue_mtx); 2379 zv->zv_state = 1; 2380 wakeup_one(&zv->zv_queue); 2381 while (zv->zv_state != 2) 2382 msleep(&zv->zv_state, &zv->zv_queue_mtx, 0, "zvol:w", 0); 2383 mtx_destroy(&zv->zv_queue_mtx); 2384 2385 pp = zv->zv_provider; 2386 zv->zv_provider = NULL; 2387 pp->private = NULL; 2388 g_wither_geom(pp->geom, ENXIO); 2389} 2390 2391static int 2392zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace) 2393{ 2394 int count, error, flags; 2395 2396 g_topology_assert(); 2397 2398 /* 2399 * To make it easier we expect either open or close, but not both 2400 * at the same time. 2401 */ 2402 KASSERT((acr >= 0 && acw >= 0 && ace >= 0) || 2403 (acr <= 0 && acw <= 0 && ace <= 0), 2404 ("Unsupported access request to %s (acr=%d, acw=%d, ace=%d).", 2405 pp->name, acr, acw, ace)); 2406 2407 if (pp->private == NULL) { 2408 if (acr <= 0 && acw <= 0 && ace <= 0) 2409 return (0); 2410 return (pp->error); 2411 } 2412 2413 /* 2414 * We don't pass FEXCL flag to zvol_open()/zvol_close() if ace != 0, 2415 * because GEOM already handles that and handles it a bit differently. 2416 * GEOM allows for multiple read/exclusive consumers and ZFS allows 2417 * only one exclusive consumer, no matter if it is reader or writer. 2418 * I like better the way GEOM works so I'll leave it for GEOM to 2419 * decide what to do. 2420 */ 2421 2422 count = acr + acw + ace; 2423 if (count == 0) 2424 return (0); 2425 2426 flags = 0; 2427 if (acr != 0 || ace != 0) 2428 flags |= FREAD; 2429 if (acw != 0) 2430 flags |= FWRITE; 2431 2432 g_topology_unlock(); 2433 if (count > 0) 2434 error = zvol_open(pp, flags, count); 2435 else 2436 error = zvol_close(pp, flags, -count); 2437 g_topology_lock(); 2438 return (error); 2439} 2440 2441static void 2442zvol_geom_start(struct bio *bp) 2443{ 2444 zvol_state_t *zv; 2445 boolean_t first; 2446 2447 zv = bp->bio_to->private; 2448 ASSERT(zv != NULL); 2449 switch (bp->bio_cmd) { 2450 case BIO_FLUSH: 2451 if (!THREAD_CAN_SLEEP()) 2452 goto enqueue; 2453 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2454 g_io_deliver(bp, 0); 2455 break; 2456 case BIO_READ: 2457 case BIO_WRITE: 2458 case BIO_DELETE: 2459 if (!THREAD_CAN_SLEEP()) 2460 goto enqueue; 2461 zvol_strategy(bp); 2462 break; 2463 case BIO_GETATTR: { 2464 spa_t *spa = dmu_objset_spa(zv->zv_objset); 2465 uint64_t refd, avail, usedobjs, availobjs, val; 2466 2467 if (g_handleattr_int(bp, "GEOM::candelete", 1)) 2468 return; 2469 if (strcmp(bp->bio_attribute, "blocksavail") == 0) { 2470 dmu_objset_space(zv->zv_objset, &refd, &avail, 2471 &usedobjs, &availobjs); 2472 if (g_handleattr_off_t(bp, "blocksavail", 2473 avail / DEV_BSIZE)) 2474 return; 2475 } else if (strcmp(bp->bio_attribute, "blocksused") == 0) { 2476 dmu_objset_space(zv->zv_objset, &refd, &avail, 2477 &usedobjs, &availobjs); 2478 if (g_handleattr_off_t(bp, "blocksused", 2479 refd / DEV_BSIZE)) 2480 return; 2481 } else if (strcmp(bp->bio_attribute, "poolblocksavail") == 0) { 2482 avail = metaslab_class_get_space(spa_normal_class(spa)); 2483 avail -= metaslab_class_get_alloc(spa_normal_class(spa)); 2484 if (g_handleattr_off_t(bp, "poolblocksavail", 2485 avail / DEV_BSIZE)) 2486 return; 2487 } else if (strcmp(bp->bio_attribute, "poolblocksused") == 0) { 2488 refd = metaslab_class_get_alloc(spa_normal_class(spa)); 2489 if (g_handleattr_off_t(bp, "poolblocksused", 2490 refd / DEV_BSIZE)) 2491 return; 2492 } 2493 /* FALLTHROUGH */ 2494 } 2495 default: 2496 g_io_deliver(bp, EOPNOTSUPP); 2497 break; 2498 } 2499 return; 2500 2501enqueue: 2502 mtx_lock(&zv->zv_queue_mtx); 2503 first = (bioq_first(&zv->zv_queue) == NULL); 2504 bioq_insert_tail(&zv->zv_queue, bp); 2505 mtx_unlock(&zv->zv_queue_mtx); 2506 if (first) 2507 wakeup_one(&zv->zv_queue); 2508} 2509 2510static void 2511zvol_geom_worker(void *arg) 2512{ 2513 zvol_state_t *zv; 2514 struct bio *bp; 2515 2516 thread_lock(curthread); 2517 sched_prio(curthread, PRIBIO); 2518 thread_unlock(curthread); 2519 2520 zv = arg; 2521 for (;;) { 2522 mtx_lock(&zv->zv_queue_mtx); 2523 bp = bioq_takefirst(&zv->zv_queue); 2524 if (bp == NULL) { 2525 if (zv->zv_state == 1) { 2526 zv->zv_state = 2; 2527 wakeup(&zv->zv_state); 2528 mtx_unlock(&zv->zv_queue_mtx); 2529 kthread_exit(); 2530 } 2531 msleep(&zv->zv_queue, &zv->zv_queue_mtx, PRIBIO | PDROP, 2532 "zvol:io", 0); 2533 continue; 2534 } 2535 mtx_unlock(&zv->zv_queue_mtx); 2536 switch (bp->bio_cmd) { 2537 case BIO_FLUSH: 2538 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2539 g_io_deliver(bp, 0); 2540 break; 2541 case BIO_READ: 2542 case BIO_WRITE: 2543 zvol_strategy(bp); 2544 break; 2545 } 2546 } 2547} 2548 2549extern boolean_t dataset_name_hidden(const char *name); 2550 2551static int 2552zvol_create_snapshots(objset_t *os, const char *name) 2553{ 2554 uint64_t cookie, obj; 2555 char *sname; 2556 int error, len; 2557 2558 cookie = obj = 0; 2559 sname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2560 2561#if 0 2562 (void) dmu_objset_find(name, dmu_objset_prefetch, NULL, 2563 DS_FIND_SNAPSHOTS); 2564#endif 2565 2566 for (;;) { 2567 len = snprintf(sname, MAXPATHLEN, "%s@", name); 2568 if (len >= MAXPATHLEN) { 2569 dmu_objset_rele(os, FTAG); 2570 error = ENAMETOOLONG; 2571 break; 2572 } 2573 2574 dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 2575 error = dmu_snapshot_list_next(os, MAXPATHLEN - len, 2576 sname + len, &obj, &cookie, NULL); 2577 dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 2578 if (error != 0) { 2579 if (error == ENOENT) 2580 error = 0; 2581 break; 2582 } 2583 2584 if ((error = zvol_create_minor(sname)) != 0) { 2585 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n", 2586 sname, error); 2587 break; 2588 } 2589 } 2590 2591 kmem_free(sname, MAXPATHLEN); 2592 return (error); 2593} 2594 2595int 2596zvol_create_minors(const char *name) 2597{ 2598 uint64_t cookie; 2599 objset_t *os; 2600 char *osname, *p; 2601 int error, len; 2602 2603 if (dataset_name_hidden(name)) 2604 return (0); 2605 2606 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 2607 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n", 2608 name, error); 2609 return (error); 2610 } 2611 if (dmu_objset_type(os) == DMU_OST_ZVOL) { 2612 dsl_dataset_long_hold(os->os_dsl_dataset, FTAG); 2613 dsl_pool_rele(dmu_objset_pool(os), FTAG); 2614 error = zvol_create_minor(name); 2615 if (error == 0 || error == EEXIST) { 2616 error = zvol_create_snapshots(os, name); 2617 } else { 2618 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n", 2619 name, error); 2620 } 2621 dsl_dataset_long_rele(os->os_dsl_dataset, FTAG); 2622 dsl_dataset_rele(os->os_dsl_dataset, FTAG); 2623 return (error); 2624 } 2625 if (dmu_objset_type(os) != DMU_OST_ZFS) { 2626 dmu_objset_rele(os, FTAG); 2627 return (0); 2628 } 2629 2630 osname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2631 if (snprintf(osname, MAXPATHLEN, "%s/", name) >= MAXPATHLEN) { 2632 dmu_objset_rele(os, FTAG); 2633 kmem_free(osname, MAXPATHLEN); 2634 return (ENOENT); 2635 } 2636 p = osname + strlen(osname); 2637 len = MAXPATHLEN - (p - osname); 2638 2639#if 0 2640 /* Prefetch the datasets. */ 2641 cookie = 0; 2642 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) { 2643 if (!dataset_name_hidden(osname)) 2644 (void) dmu_objset_prefetch(osname, NULL); 2645 } 2646#endif 2647 2648 cookie = 0; 2649 while (dmu_dir_list_next(os, MAXPATHLEN - (p - osname), p, NULL, 2650 &cookie) == 0) { 2651 dmu_objset_rele(os, FTAG); 2652 (void)zvol_create_minors(osname); 2653 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 2654 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n", 2655 name, error); 2656 return (error); 2657 } 2658 } 2659 2660 dmu_objset_rele(os, FTAG); 2661 kmem_free(osname, MAXPATHLEN); 2662 return (0); 2663} 2664 2665static void 2666zvol_rename_minor(zvol_state_t *zv, const char *newname) 2667{ 2668 struct g_geom *gp; 2669 struct g_provider *pp; 2670 struct cdev *dev; 2671 2672 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2673 2674 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 2675 g_topology_lock(); 2676 pp = zv->zv_provider; 2677 ASSERT(pp != NULL); 2678 gp = pp->geom; 2679 ASSERT(gp != NULL); 2680 2681 zv->zv_provider = NULL; 2682 g_wither_provider(pp, ENXIO); 2683 2684 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, newname); 2685 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND; 2686 pp->sectorsize = DEV_BSIZE; 2687 pp->mediasize = zv->zv_volsize; 2688 pp->private = zv; 2689 zv->zv_provider = pp; 2690 g_error_provider(pp, 0); 2691 g_topology_unlock(); 2692 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) { 2693 dev = zv->zv_dev; 2694 ASSERT(dev != NULL); 2695 zv->zv_dev = NULL; 2696 destroy_dev(dev); 2697 2698 if (make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, 2699 &dev, &zvol_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 2700 0640, "%s/%s", ZVOL_DRIVER, newname) == 0) { 2701 zv->zv_dev = dev; 2702 dev->si_iosize_max = MAXPHYS; 2703 dev->si_drv2 = zv; 2704 } 2705 } 2706 strlcpy(zv->zv_name, newname, sizeof(zv->zv_name)); 2707} 2708 2709void 2710zvol_rename_minors(const char *oldname, const char *newname) 2711{ 2712 char name[MAXPATHLEN]; 2713 struct g_provider *pp; 2714 struct g_geom *gp; 2715 size_t oldnamelen, newnamelen; 2716 zvol_state_t *zv; 2717 char *namebuf; 2718 boolean_t locked = B_FALSE; 2719 2720 oldnamelen = strlen(oldname); 2721 newnamelen = strlen(newname); 2722 2723 DROP_GIANT(); 2724 /* See comment in zvol_open(). */ 2725 if (!MUTEX_HELD(&spa_namespace_lock)) { 2726 mutex_enter(&spa_namespace_lock); 2727 locked = B_TRUE; 2728 } 2729 2730 LIST_FOREACH(zv, &all_zvols, zv_links) { 2731 if (strcmp(zv->zv_name, oldname) == 0) { 2732 zvol_rename_minor(zv, newname); 2733 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 && 2734 (zv->zv_name[oldnamelen] == '/' || 2735 zv->zv_name[oldnamelen] == '@')) { 2736 snprintf(name, sizeof(name), "%s%c%s", newname, 2737 zv->zv_name[oldnamelen], 2738 zv->zv_name + oldnamelen + 1); 2739 zvol_rename_minor(zv, name); 2740 } 2741 } 2742 2743 if (locked) 2744 mutex_exit(&spa_namespace_lock); 2745 PICKUP_GIANT(); 2746} 2747 2748static int 2749zvol_d_open(struct cdev *dev, int flags, int fmt, struct thread *td) 2750{ 2751 zvol_state_t *zv; 2752 int err = 0; 2753 2754 mutex_enter(&spa_namespace_lock); 2755 zv = dev->si_drv2; 2756 if (zv == NULL) { 2757 mutex_exit(&spa_namespace_lock); 2758 return(ENXIO); /* zvol_create_minor() not done yet */ 2759 } 2760 2761 if (zv->zv_total_opens == 0) 2762 err = zvol_first_open(zv); 2763 if (err) { 2764 mutex_exit(&spa_namespace_lock); 2765 return (err); 2766 } 2767 if ((flags & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 2768 err = SET_ERROR(EROFS); 2769 goto out; 2770 } 2771 if (zv->zv_flags & ZVOL_EXCL) { 2772 err = SET_ERROR(EBUSY); 2773 goto out; 2774 } 2775#ifdef FEXCL 2776 if (flags & FEXCL) { 2777 if (zv->zv_total_opens != 0) { 2778 err = SET_ERROR(EBUSY); 2779 goto out; 2780 } 2781 zv->zv_flags |= ZVOL_EXCL; 2782 } 2783#endif 2784 2785 zv->zv_total_opens++; 2786 mutex_exit(&spa_namespace_lock); 2787 return (err); 2788out: 2789 if (zv->zv_total_opens == 0) 2790 zvol_last_close(zv); 2791 mutex_exit(&spa_namespace_lock); 2792 return (err); 2793} 2794 2795static int 2796zvol_d_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2797{ 2798 zvol_state_t *zv; 2799 int err = 0; 2800 2801 mutex_enter(&spa_namespace_lock); 2802 zv = dev->si_drv2; 2803 if (zv == NULL) { 2804 mutex_exit(&spa_namespace_lock); 2805 return(ENXIO); 2806 } 2807 2808 if (zv->zv_flags & ZVOL_EXCL) { 2809 ASSERT(zv->zv_total_opens == 1); 2810 zv->zv_flags &= ~ZVOL_EXCL; 2811 } 2812 2813 /* 2814 * If the open count is zero, this is a spurious close. 2815 * That indicates a bug in the kernel / DDI framework. 2816 */ 2817 ASSERT(zv->zv_total_opens != 0); 2818 2819 /* 2820 * You may get multiple opens, but only one close. 2821 */ 2822 zv->zv_total_opens--; 2823 2824 if (zv->zv_total_opens == 0) 2825 zvol_last_close(zv); 2826 2827 mutex_exit(&spa_namespace_lock); 2828 return (0); 2829} 2830 2831static int 2832zvol_d_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 2833{ 2834 zvol_state_t *zv; 2835 rl_t *rl; 2836 off_t offset, length, chunk; 2837 int i, error; 2838 u_int u; 2839 2840 zv = dev->si_drv2; 2841 2842 error = 0; 2843 KASSERT(zv->zv_total_opens > 0, 2844 ("Device with zero access count in zvol_d_ioctl")); 2845 2846 i = IOCPARM_LEN(cmd); 2847 switch (cmd) { 2848 case DIOCGSECTORSIZE: 2849 *(u_int *)data = DEV_BSIZE; 2850 break; 2851 case DIOCGMEDIASIZE: 2852 *(off_t *)data = zv->zv_volsize; 2853 break; 2854 case DIOCGFLUSH: 2855 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2856 break; 2857 case DIOCGDELETE: 2858 if (!zvol_unmap_enabled) 2859 break; 2860 2861 offset = ((off_t *)data)[0]; 2862 length = ((off_t *)data)[1]; 2863 if ((offset % DEV_BSIZE) != 0 || (length % DEV_BSIZE) != 0 || 2864 offset < 0 || offset >= zv->zv_volsize || 2865 length <= 0) { 2866 printf("%s: offset=%jd length=%jd\n", __func__, offset, 2867 length); 2868 error = EINVAL; 2869 break; 2870 } 2871 2872 rl = zfs_range_lock(&zv->zv_znode, offset, length, RL_WRITER); 2873 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 2874 error = dmu_tx_assign(tx, TXG_WAIT); 2875 if (error != 0) { 2876 dmu_tx_abort(tx); 2877 } else { 2878 zvol_log_truncate(zv, tx, offset, length, B_TRUE); 2879 dmu_tx_commit(tx); 2880 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 2881 offset, length); 2882 } 2883 zfs_range_unlock(rl); 2884 if (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS) 2885 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2886 break; 2887 case DIOCGSTRIPESIZE: 2888 *(off_t *)data = zv->zv_volblocksize; 2889 break; 2890 case DIOCGSTRIPEOFFSET: 2891 *(off_t *)data = 0; 2892 break; 2893 case DIOCGATTR: { 2894 spa_t *spa = dmu_objset_spa(zv->zv_objset); 2895 struct diocgattr_arg *arg = (struct diocgattr_arg *)data; 2896 uint64_t refd, avail, usedobjs, availobjs; 2897 2898 if (strcmp(arg->name, "blocksavail") == 0) { 2899 dmu_objset_space(zv->zv_objset, &refd, &avail, 2900 &usedobjs, &availobjs); 2901 arg->value.off = avail / DEV_BSIZE; 2902 } else if (strcmp(arg->name, "blocksused") == 0) { 2903 dmu_objset_space(zv->zv_objset, &refd, &avail, 2904 &usedobjs, &availobjs); 2905 arg->value.off = refd / DEV_BSIZE; 2906 } else if (strcmp(arg->name, "poolblocksavail") == 0) { 2907 avail = metaslab_class_get_space(spa_normal_class(spa)); 2908 avail -= metaslab_class_get_alloc(spa_normal_class(spa)); 2909 arg->value.off = avail / DEV_BSIZE; 2910 } else if (strcmp(arg->name, "poolblocksused") == 0) { 2911 refd = metaslab_class_get_alloc(spa_normal_class(spa)); 2912 arg->value.off = refd / DEV_BSIZE; 2913 } else 2914 error = ENOIOCTL; 2915 break; 2916 }
| 95 96#include <geom/geom.h> 97 98#include "zfs_namecheck.h" 99 100struct g_class zfs_zvol_class = { 101 .name = "ZFS::ZVOL", 102 .version = G_VERSION, 103}; 104 105DECLARE_GEOM_CLASS(zfs_zvol_class, zfs_zvol); 106 107void *zfsdev_state; 108static char *zvol_tag = "zvol_tag"; 109 110#define ZVOL_DUMPSIZE "dumpsize" 111 112/* 113 * The spa_namespace_lock protects the zfsdev_state structure from being 114 * modified while it's being used, e.g. an open that comes in before a 115 * create finishes. It also protects temporary opens of the dataset so that, 116 * e.g., an open doesn't get a spurious EBUSY. 117 */ 118static uint32_t zvol_minors; 119 120SYSCTL_DECL(_vfs_zfs); 121SYSCTL_NODE(_vfs_zfs, OID_AUTO, vol, CTLFLAG_RW, 0, "ZFS VOLUME"); 122static int volmode = ZFS_VOLMODE_GEOM; 123SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, mode, CTLFLAG_RWTUN, &volmode, 0, 124 "Expose as GEOM providers (1), device files (2) or neither"); 125 126typedef struct zvol_extent { 127 list_node_t ze_node; 128 dva_t ze_dva; /* dva associated with this extent */ 129 uint64_t ze_nblks; /* number of blocks in extent */ 130} zvol_extent_t; 131 132/* 133 * The in-core state of each volume. 134 */ 135typedef struct zvol_state { 136 LIST_ENTRY(zvol_state) zv_links; 137 char zv_name[MAXPATHLEN]; /* pool/dd name */ 138 uint64_t zv_volsize; /* amount of space we advertise */ 139 uint64_t zv_volblocksize; /* volume block size */ 140 struct cdev *zv_dev; /* non-GEOM device */ 141 struct g_provider *zv_provider; /* GEOM provider */ 142 uint8_t zv_min_bs; /* minimum addressable block shift */ 143 uint8_t zv_flags; /* readonly, dumpified, etc. */ 144 objset_t *zv_objset; /* objset handle */ 145 uint32_t zv_total_opens; /* total open count */ 146 zilog_t *zv_zilog; /* ZIL handle */ 147 list_t zv_extents; /* List of extents for dump */ 148 znode_t zv_znode; /* for range locking */ 149 dmu_buf_t *zv_dbuf; /* bonus handle */ 150 int zv_state; 151 int zv_volmode; /* Provide GEOM or cdev */ 152 struct bio_queue_head zv_queue; 153 struct mtx zv_queue_mtx; /* zv_queue mutex */ 154} zvol_state_t; 155 156static LIST_HEAD(, zvol_state) all_zvols; 157 158/* 159 * zvol specific flags 160 */ 161#define ZVOL_RDONLY 0x1 162#define ZVOL_DUMPIFIED 0x2 163#define ZVOL_EXCL 0x4 164#define ZVOL_WCE 0x8 165 166/* 167 * zvol maximum transfer in one DMU tx. 168 */ 169int zvol_maxphys = DMU_MAX_ACCESS/2; 170 171/* 172 * Toggle unmap functionality. 173 */ 174boolean_t zvol_unmap_enabled = B_TRUE; 175SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, unmap_enabled, CTLFLAG_RWTUN, 176 &zvol_unmap_enabled, 0, 177 "Enable UNMAP functionality"); 178 179static d_open_t zvol_d_open; 180static d_close_t zvol_d_close; 181static d_read_t zvol_read; 182static d_write_t zvol_write; 183static d_ioctl_t zvol_d_ioctl; 184static d_strategy_t zvol_strategy; 185 186static struct cdevsw zvol_cdevsw = { 187 .d_version = D_VERSION, 188 .d_open = zvol_d_open, 189 .d_close = zvol_d_close, 190 .d_read = zvol_read, 191 .d_write = zvol_write, 192 .d_ioctl = zvol_d_ioctl, 193 .d_strategy = zvol_strategy, 194 .d_name = "zvol", 195 .d_flags = D_DISK | D_TRACKCLOSE, 196}; 197 198extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 199 nvlist_t *, nvlist_t *); 200static void zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, 201 uint64_t len, boolean_t sync); 202static int zvol_remove_zv(zvol_state_t *); 203static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 204static int zvol_dumpify(zvol_state_t *zv); 205static int zvol_dump_fini(zvol_state_t *zv); 206static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 207 208static void zvol_geom_run(zvol_state_t *zv); 209static void zvol_geom_destroy(zvol_state_t *zv); 210static int zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace); 211static void zvol_geom_start(struct bio *bp); 212static void zvol_geom_worker(void *arg); 213 214static void 215zvol_size_changed(zvol_state_t *zv) 216{ 217#ifdef sun 218 dev_t dev = makedevice(maj, min); 219 220 VERIFY(ddi_prop_update_int64(dev, zfs_dip, 221 "Size", volsize) == DDI_SUCCESS); 222 VERIFY(ddi_prop_update_int64(dev, zfs_dip, 223 "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 224 225 /* Notify specfs to invalidate the cached size */ 226 spec_size_invalidate(dev, VBLK); 227 spec_size_invalidate(dev, VCHR); 228#else /* !sun */ 229 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 230 struct g_provider *pp; 231 232 pp = zv->zv_provider; 233 if (pp == NULL) 234 return; 235 g_topology_lock(); 236 g_resize_provider(pp, zv->zv_volsize); 237 g_topology_unlock(); 238 } 239#endif /* !sun */ 240} 241 242int 243zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 244{ 245 if (volsize == 0) 246 return (SET_ERROR(EINVAL)); 247 248 if (volsize % blocksize != 0) 249 return (SET_ERROR(EINVAL)); 250 251#ifdef _ILP32 252 if (volsize - 1 > SPEC_MAXOFFSET_T) 253 return (SET_ERROR(EOVERFLOW)); 254#endif 255 return (0); 256} 257 258int 259zvol_check_volblocksize(uint64_t volblocksize) 260{ 261 if (volblocksize < SPA_MINBLOCKSIZE || 262 volblocksize > SPA_OLD_MAXBLOCKSIZE || 263 !ISP2(volblocksize)) 264 return (SET_ERROR(EDOM)); 265 266 return (0); 267} 268 269int 270zvol_get_stats(objset_t *os, nvlist_t *nv) 271{ 272 int error; 273 dmu_object_info_t doi; 274 uint64_t val; 275 276 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 277 if (error) 278 return (error); 279 280 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 281 282 error = dmu_object_info(os, ZVOL_OBJ, &doi); 283 284 if (error == 0) { 285 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 286 doi.doi_data_block_size); 287 } 288 289 return (error); 290} 291 292static zvol_state_t * 293zvol_minor_lookup(const char *name) 294{ 295 zvol_state_t *zv; 296 297 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 298 299 LIST_FOREACH(zv, &all_zvols, zv_links) { 300 if (strcmp(zv->zv_name, name) == 0) 301 break; 302 } 303 304 return (zv); 305} 306 307/* extent mapping arg */ 308struct maparg { 309 zvol_state_t *ma_zv; 310 uint64_t ma_blks; 311}; 312 313/*ARGSUSED*/ 314static int 315zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 316 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 317{ 318 struct maparg *ma = arg; 319 zvol_extent_t *ze; 320 int bs = ma->ma_zv->zv_volblocksize; 321 322 if (BP_IS_HOLE(bp) || 323 zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 324 return (0); 325 326 VERIFY(!BP_IS_EMBEDDED(bp)); 327 328 VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 329 ma->ma_blks++; 330 331 /* Abort immediately if we have encountered gang blocks */ 332 if (BP_IS_GANG(bp)) 333 return (SET_ERROR(EFRAGS)); 334 335 /* 336 * See if the block is at the end of the previous extent. 337 */ 338 ze = list_tail(&ma->ma_zv->zv_extents); 339 if (ze && 340 DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 341 DVA_GET_OFFSET(BP_IDENTITY(bp)) == 342 DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 343 ze->ze_nblks++; 344 return (0); 345 } 346 347 dprintf_bp(bp, "%s", "next blkptr:"); 348 349 /* start a new extent */ 350 ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 351 ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 352 ze->ze_nblks = 1; 353 list_insert_tail(&ma->ma_zv->zv_extents, ze); 354 return (0); 355} 356 357static void 358zvol_free_extents(zvol_state_t *zv) 359{ 360 zvol_extent_t *ze; 361 362 while (ze = list_head(&zv->zv_extents)) { 363 list_remove(&zv->zv_extents, ze); 364 kmem_free(ze, sizeof (zvol_extent_t)); 365 } 366} 367 368static int 369zvol_get_lbas(zvol_state_t *zv) 370{ 371 objset_t *os = zv->zv_objset; 372 struct maparg ma; 373 int err; 374 375 ma.ma_zv = zv; 376 ma.ma_blks = 0; 377 zvol_free_extents(zv); 378 379 /* commit any in-flight changes before traversing the dataset */ 380 txg_wait_synced(dmu_objset_pool(os), 0); 381 err = traverse_dataset(dmu_objset_ds(os), 0, 382 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 383 if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 384 zvol_free_extents(zv); 385 return (err ? err : EIO); 386 } 387 388 return (0); 389} 390 391/* ARGSUSED */ 392void 393zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 394{ 395 zfs_creat_t *zct = arg; 396 nvlist_t *nvprops = zct->zct_props; 397 int error; 398 uint64_t volblocksize, volsize; 399 400 VERIFY(nvlist_lookup_uint64(nvprops, 401 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 402 if (nvlist_lookup_uint64(nvprops, 403 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 404 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 405 406 /* 407 * These properties must be removed from the list so the generic 408 * property setting step won't apply to them. 409 */ 410 VERIFY(nvlist_remove_all(nvprops, 411 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 412 (void) nvlist_remove_all(nvprops, 413 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 414 415 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 416 DMU_OT_NONE, 0, tx); 417 ASSERT(error == 0); 418 419 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 420 DMU_OT_NONE, 0, tx); 421 ASSERT(error == 0); 422 423 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 424 ASSERT(error == 0); 425} 426 427/* 428 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we 429 * implement DKIOCFREE/free-long-range. 430 */ 431static int 432zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap) 433{ 434 uint64_t offset, length; 435 436 if (byteswap) 437 byteswap_uint64_array(lr, sizeof (*lr)); 438 439 offset = lr->lr_offset; 440 length = lr->lr_length; 441 442 return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length)); 443} 444 445/* 446 * Replay a TX_WRITE ZIL transaction that didn't get committed 447 * after a system failure 448 */ 449static int 450zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 451{ 452 objset_t *os = zv->zv_objset; 453 char *data = (char *)(lr + 1); /* data follows lr_write_t */ 454 uint64_t offset, length; 455 dmu_tx_t *tx; 456 int error; 457 458 if (byteswap) 459 byteswap_uint64_array(lr, sizeof (*lr)); 460 461 offset = lr->lr_offset; 462 length = lr->lr_length; 463 464 /* If it's a dmu_sync() block, write the whole block */ 465 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 466 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 467 if (length < blocksize) { 468 offset -= offset % blocksize; 469 length = blocksize; 470 } 471 } 472 473 tx = dmu_tx_create(os); 474 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 475 error = dmu_tx_assign(tx, TXG_WAIT); 476 if (error) { 477 dmu_tx_abort(tx); 478 } else { 479 dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 480 dmu_tx_commit(tx); 481 } 482 483 return (error); 484} 485 486/* ARGSUSED */ 487static int 488zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 489{ 490 return (SET_ERROR(ENOTSUP)); 491} 492 493/* 494 * Callback vectors for replaying records. 495 * Only TX_WRITE and TX_TRUNCATE are needed for zvol. 496 */ 497zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 498 zvol_replay_err, /* 0 no such transaction type */ 499 zvol_replay_err, /* TX_CREATE */ 500 zvol_replay_err, /* TX_MKDIR */ 501 zvol_replay_err, /* TX_MKXATTR */ 502 zvol_replay_err, /* TX_SYMLINK */ 503 zvol_replay_err, /* TX_REMOVE */ 504 zvol_replay_err, /* TX_RMDIR */ 505 zvol_replay_err, /* TX_LINK */ 506 zvol_replay_err, /* TX_RENAME */ 507 zvol_replay_write, /* TX_WRITE */ 508 zvol_replay_truncate, /* TX_TRUNCATE */ 509 zvol_replay_err, /* TX_SETATTR */ 510 zvol_replay_err, /* TX_ACL */ 511 zvol_replay_err, /* TX_CREATE_ACL */ 512 zvol_replay_err, /* TX_CREATE_ATTR */ 513 zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 514 zvol_replay_err, /* TX_MKDIR_ACL */ 515 zvol_replay_err, /* TX_MKDIR_ATTR */ 516 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 517 zvol_replay_err, /* TX_WRITE2 */ 518}; 519 520#ifdef sun 521int 522zvol_name2minor(const char *name, minor_t *minor) 523{ 524 zvol_state_t *zv; 525 526 mutex_enter(&spa_namespace_lock); 527 zv = zvol_minor_lookup(name); 528 if (minor && zv) 529 *minor = zv->zv_minor; 530 mutex_exit(&spa_namespace_lock); 531 return (zv ? 0 : -1); 532} 533#endif /* sun */ 534 535/* 536 * Create a minor node (plus a whole lot more) for the specified volume. 537 */ 538int 539zvol_create_minor(const char *name) 540{ 541 zfs_soft_state_t *zs; 542 zvol_state_t *zv; 543 objset_t *os; 544 struct cdev *dev; 545 struct g_provider *pp; 546 struct g_geom *gp; 547 dmu_object_info_t doi; 548 uint64_t volsize, mode; 549 int error; 550 551 ZFS_LOG(1, "Creating ZVOL %s...", name); 552 553 mutex_enter(&spa_namespace_lock); 554 555 if (zvol_minor_lookup(name) != NULL) { 556 mutex_exit(&spa_namespace_lock); 557 return (SET_ERROR(EEXIST)); 558 } 559 560 /* lie and say we're read-only */ 561 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os); 562 563 if (error) { 564 mutex_exit(&spa_namespace_lock); 565 return (error); 566 } 567 568#ifdef sun 569 if ((minor = zfsdev_minor_alloc()) == 0) { 570 dmu_objset_disown(os, FTAG); 571 mutex_exit(&spa_namespace_lock); 572 return (SET_ERROR(ENXIO)); 573 } 574 575 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) { 576 dmu_objset_disown(os, FTAG); 577 mutex_exit(&spa_namespace_lock); 578 return (SET_ERROR(EAGAIN)); 579 } 580 (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 581 (char *)name); 582 583 (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 584 585 if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 586 minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 587 ddi_soft_state_free(zfsdev_state, minor); 588 dmu_objset_disown(os, FTAG); 589 mutex_exit(&spa_namespace_lock); 590 return (SET_ERROR(EAGAIN)); 591 } 592 593 (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 594 595 if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 596 minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 597 ddi_remove_minor_node(zfs_dip, chrbuf); 598 ddi_soft_state_free(zfsdev_state, minor); 599 dmu_objset_disown(os, FTAG); 600 mutex_exit(&spa_namespace_lock); 601 return (SET_ERROR(EAGAIN)); 602 } 603 604 zs = ddi_get_soft_state(zfsdev_state, minor); 605 zs->zss_type = ZSST_ZVOL; 606 zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP); 607#else /* !sun */ 608 609 zv = kmem_zalloc(sizeof(*zv), KM_SLEEP); 610 zv->zv_state = 0; 611 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 612 if (error) { 613 kmem_free(zv, sizeof(*zv)); 614 dmu_objset_disown(os, zvol_tag); 615 mutex_exit(&spa_namespace_lock); 616 return (error); 617 } 618 error = dsl_prop_get_integer(name, 619 zfs_prop_to_name(ZFS_PROP_VOLMODE), &mode, NULL); 620 if (error != 0 || mode == ZFS_VOLMODE_DEFAULT) 621 mode = volmode; 622 623 DROP_GIANT(); 624 zv->zv_volsize = volsize; 625 zv->zv_volmode = mode; 626 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 627 g_topology_lock(); 628 gp = g_new_geomf(&zfs_zvol_class, "zfs::zvol::%s", name); 629 gp->start = zvol_geom_start; 630 gp->access = zvol_geom_access; 631 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, name); 632 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND; 633 pp->sectorsize = DEV_BSIZE; 634 pp->mediasize = zv->zv_volsize; 635 pp->private = zv; 636 637 zv->zv_provider = pp; 638 bioq_init(&zv->zv_queue); 639 mtx_init(&zv->zv_queue_mtx, "zvol", NULL, MTX_DEF); 640 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) { 641 if (make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, 642 &dev, &zvol_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 643 0640, "%s/%s", ZVOL_DRIVER, name) != 0) { 644 kmem_free(zv, sizeof(*zv)); 645 dmu_objset_disown(os, FTAG); 646 mutex_exit(&spa_namespace_lock); 647 return (SET_ERROR(ENXIO)); 648 } 649 zv->zv_dev = dev; 650 dev->si_iosize_max = MAXPHYS; 651 dev->si_drv2 = zv; 652 } 653 LIST_INSERT_HEAD(&all_zvols, zv, zv_links); 654#endif /* !sun */ 655 656 (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 657 zv->zv_min_bs = DEV_BSHIFT; 658 zv->zv_objset = os; 659 if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os))) 660 zv->zv_flags |= ZVOL_RDONLY; 661 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 662 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 663 sizeof (rl_t), offsetof(rl_t, r_node)); 664 list_create(&zv->zv_extents, sizeof (zvol_extent_t), 665 offsetof(zvol_extent_t, ze_node)); 666 /* get and cache the blocksize */ 667 error = dmu_object_info(os, ZVOL_OBJ, &doi); 668 ASSERT(error == 0); 669 zv->zv_volblocksize = doi.doi_data_block_size; 670 671 if (spa_writeable(dmu_objset_spa(os))) { 672 if (zil_replay_disable) 673 zil_destroy(dmu_objset_zil(os), B_FALSE); 674 else 675 zil_replay(os, zv, zvol_replay_vector); 676 } 677 dmu_objset_disown(os, FTAG); 678 zv->zv_objset = NULL; 679 680 zvol_minors++; 681 682 mutex_exit(&spa_namespace_lock); 683 684#ifndef sun 685 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 686 zvol_geom_run(zv); 687 g_topology_unlock(); 688 } 689 PICKUP_GIANT(); 690#endif 691 692 ZFS_LOG(1, "ZVOL %s created.", name); 693 694 return (0); 695} 696 697/* 698 * Remove minor node for the specified volume. 699 */ 700static int 701zvol_remove_zv(zvol_state_t *zv) 702{ 703#ifdef sun 704 minor_t minor = zv->zv_minor; 705#endif 706 707 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 708 if (zv->zv_total_opens != 0) 709 return (SET_ERROR(EBUSY)); 710 711 ZFS_LOG(1, "ZVOL %s destroyed.", zv->zv_name); 712 713#ifdef sun 714 (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor); 715 ddi_remove_minor_node(zfs_dip, nmbuf); 716#else 717 LIST_REMOVE(zv, zv_links); 718 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 719 g_topology_lock(); 720 zvol_geom_destroy(zv); 721 g_topology_unlock(); 722 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) 723 destroy_dev(zv->zv_dev); 724#endif /* sun */ 725 726 avl_destroy(&zv->zv_znode.z_range_avl); 727 mutex_destroy(&zv->zv_znode.z_range_lock); 728 729 kmem_free(zv, sizeof(*zv)); 730 731 zvol_minors--; 732 return (0); 733} 734 735int 736zvol_remove_minor(const char *name) 737{ 738 zvol_state_t *zv; 739 int rc; 740 741 mutex_enter(&spa_namespace_lock); 742 if ((zv = zvol_minor_lookup(name)) == NULL) { 743 mutex_exit(&spa_namespace_lock); 744 return (SET_ERROR(ENXIO)); 745 } 746 rc = zvol_remove_zv(zv); 747 mutex_exit(&spa_namespace_lock); 748 return (rc); 749} 750 751int 752zvol_first_open(zvol_state_t *zv) 753{ 754 objset_t *os; 755 uint64_t volsize; 756 int error; 757 uint64_t readonly; 758 759 /* lie and say we're read-only */ 760 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 761 zvol_tag, &os); 762 if (error) 763 return (error); 764 765 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 766 if (error) { 767 ASSERT(error == 0); 768 dmu_objset_disown(os, zvol_tag); 769 return (error); 770 } 771 zv->zv_objset = os; 772 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); 773 if (error) { 774 dmu_objset_disown(os, zvol_tag); 775 return (error); 776 } 777 zv->zv_volsize = volsize; 778 zv->zv_zilog = zil_open(os, zvol_get_data); 779 zvol_size_changed(zv); 780 781 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 782 NULL) == 0); 783 if (readonly || dmu_objset_is_snapshot(os) || 784 !spa_writeable(dmu_objset_spa(os))) 785 zv->zv_flags |= ZVOL_RDONLY; 786 else 787 zv->zv_flags &= ~ZVOL_RDONLY; 788 return (error); 789} 790 791void 792zvol_last_close(zvol_state_t *zv) 793{ 794 zil_close(zv->zv_zilog); 795 zv->zv_zilog = NULL; 796 797 dmu_buf_rele(zv->zv_dbuf, zvol_tag); 798 zv->zv_dbuf = NULL; 799 800 /* 801 * Evict cached data 802 */ 803 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) && 804 !(zv->zv_flags & ZVOL_RDONLY)) 805 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 806 dmu_objset_evict_dbufs(zv->zv_objset); 807 808 dmu_objset_disown(zv->zv_objset, zvol_tag); 809 zv->zv_objset = NULL; 810} 811 812#ifdef sun 813int 814zvol_prealloc(zvol_state_t *zv) 815{ 816 objset_t *os = zv->zv_objset; 817 dmu_tx_t *tx; 818 uint64_t refd, avail, usedobjs, availobjs; 819 uint64_t resid = zv->zv_volsize; 820 uint64_t off = 0; 821 822 /* Check the space usage before attempting to allocate the space */ 823 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 824 if (avail < zv->zv_volsize) 825 return (SET_ERROR(ENOSPC)); 826 827 /* Free old extents if they exist */ 828 zvol_free_extents(zv); 829 830 while (resid != 0) { 831 int error; 832 uint64_t bytes = MIN(resid, SPA_OLD_MAXBLOCKSIZE); 833 834 tx = dmu_tx_create(os); 835 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 836 error = dmu_tx_assign(tx, TXG_WAIT); 837 if (error) { 838 dmu_tx_abort(tx); 839 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 840 return (error); 841 } 842 dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 843 dmu_tx_commit(tx); 844 off += bytes; 845 resid -= bytes; 846 } 847 txg_wait_synced(dmu_objset_pool(os), 0); 848 849 return (0); 850} 851#endif /* sun */ 852 853static int 854zvol_update_volsize(objset_t *os, uint64_t volsize) 855{ 856 dmu_tx_t *tx; 857 int error; 858 859 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 860 861 tx = dmu_tx_create(os); 862 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 863 dmu_tx_mark_netfree(tx); 864 error = dmu_tx_assign(tx, TXG_WAIT); 865 if (error) { 866 dmu_tx_abort(tx); 867 return (error); 868 } 869 870 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 871 &volsize, tx); 872 dmu_tx_commit(tx); 873 874 if (error == 0) 875 error = dmu_free_long_range(os, 876 ZVOL_OBJ, volsize, DMU_OBJECT_END); 877 return (error); 878} 879 880void 881zvol_remove_minors(const char *name) 882{ 883 zvol_state_t *zv, *tzv; 884 size_t namelen; 885 886 namelen = strlen(name); 887 888 DROP_GIANT(); 889 mutex_enter(&spa_namespace_lock); 890 891 LIST_FOREACH_SAFE(zv, &all_zvols, zv_links, tzv) { 892 if (strcmp(zv->zv_name, name) == 0 || 893 (strncmp(zv->zv_name, name, namelen) == 0 && 894 strlen(zv->zv_name) > namelen && (zv->zv_name[namelen] == '/' || 895 zv->zv_name[namelen] == '@'))) { 896 (void) zvol_remove_zv(zv); 897 } 898 } 899 900 mutex_exit(&spa_namespace_lock); 901 PICKUP_GIANT(); 902} 903 904int 905zvol_set_volsize(const char *name, major_t maj, uint64_t volsize) 906{ 907 zvol_state_t *zv = NULL; 908 objset_t *os; 909 int error; 910 dmu_object_info_t doi; 911 uint64_t old_volsize = 0ULL; 912 uint64_t readonly; 913 914 mutex_enter(&spa_namespace_lock); 915 zv = zvol_minor_lookup(name); 916 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 917 mutex_exit(&spa_namespace_lock); 918 return (error); 919 } 920 921 if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 922 (error = zvol_check_volsize(volsize, 923 doi.doi_data_block_size)) != 0) 924 goto out; 925 926 VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, 927 NULL) == 0); 928 if (readonly) { 929 error = EROFS; 930 goto out; 931 } 932 933 error = zvol_update_volsize(os, volsize); 934 /* 935 * Reinitialize the dump area to the new size. If we 936 * failed to resize the dump area then restore it back to 937 * its original size. 938 */ 939 if (zv && error == 0) { 940#ifdef ZVOL_DUMP 941 if (zv->zv_flags & ZVOL_DUMPIFIED) { 942 old_volsize = zv->zv_volsize; 943 zv->zv_volsize = volsize; 944 if ((error = zvol_dumpify(zv)) != 0 || 945 (error = dumpvp_resize()) != 0) { 946 (void) zvol_update_volsize(os, old_volsize); 947 zv->zv_volsize = old_volsize; 948 error = zvol_dumpify(zv); 949 } 950 } 951#endif /* ZVOL_DUMP */ 952 if (error == 0) { 953 zv->zv_volsize = volsize; 954 zvol_size_changed(zv); 955 } 956 } 957 958#ifdef sun 959 /* 960 * Generate a LUN expansion event. 961 */ 962 if (zv && error == 0) { 963 sysevent_id_t eid; 964 nvlist_t *attr; 965 char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 966 967 (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 968 zv->zv_minor); 969 970 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 971 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 972 973 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 974 ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 975 976 nvlist_free(attr); 977 kmem_free(physpath, MAXPATHLEN); 978 } 979#endif /* sun */ 980 981out: 982 dmu_objset_rele(os, FTAG); 983 984 mutex_exit(&spa_namespace_lock); 985 986 return (error); 987} 988 989/*ARGSUSED*/ 990static int 991zvol_open(struct g_provider *pp, int flag, int count) 992{ 993 zvol_state_t *zv; 994 int err = 0; 995 boolean_t locked = B_FALSE; 996 997 /* 998 * Protect against recursively entering spa_namespace_lock 999 * when spa_open() is used for a pool on a (local) ZVOL(s). 1000 * This is needed since we replaced upstream zfsdev_state_lock 1001 * with spa_namespace_lock in the ZVOL code. 1002 * We are using the same trick as spa_open(). 1003 * Note that calls in zvol_first_open which need to resolve 1004 * pool name to a spa object will enter spa_open() 1005 * recursively, but that function already has all the 1006 * necessary protection. 1007 */ 1008 if (!MUTEX_HELD(&spa_namespace_lock)) { 1009 mutex_enter(&spa_namespace_lock); 1010 locked = B_TRUE; 1011 } 1012 1013 zv = pp->private; 1014 if (zv == NULL) { 1015 if (locked) 1016 mutex_exit(&spa_namespace_lock); 1017 return (SET_ERROR(ENXIO)); 1018 } 1019 1020 if (zv->zv_total_opens == 0) { 1021 err = zvol_first_open(zv); 1022 if (err) { 1023 if (locked) 1024 mutex_exit(&spa_namespace_lock); 1025 return (err); 1026 } 1027 pp->mediasize = zv->zv_volsize; 1028 pp->stripeoffset = 0; 1029 pp->stripesize = zv->zv_volblocksize; 1030 } 1031 if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 1032 err = SET_ERROR(EROFS); 1033 goto out; 1034 } 1035 if (zv->zv_flags & ZVOL_EXCL) { 1036 err = SET_ERROR(EBUSY); 1037 goto out; 1038 } 1039#ifdef FEXCL 1040 if (flag & FEXCL) { 1041 if (zv->zv_total_opens != 0) { 1042 err = SET_ERROR(EBUSY); 1043 goto out; 1044 } 1045 zv->zv_flags |= ZVOL_EXCL; 1046 } 1047#endif 1048 1049 zv->zv_total_opens += count; 1050 if (locked) 1051 mutex_exit(&spa_namespace_lock); 1052 1053 return (err); 1054out: 1055 if (zv->zv_total_opens == 0) 1056 zvol_last_close(zv); 1057 if (locked) 1058 mutex_exit(&spa_namespace_lock); 1059 return (err); 1060} 1061 1062/*ARGSUSED*/ 1063static int 1064zvol_close(struct g_provider *pp, int flag, int count) 1065{ 1066 zvol_state_t *zv; 1067 int error = 0; 1068 boolean_t locked = B_FALSE; 1069 1070 /* See comment in zvol_open(). */ 1071 if (!MUTEX_HELD(&spa_namespace_lock)) { 1072 mutex_enter(&spa_namespace_lock); 1073 locked = B_TRUE; 1074 } 1075 1076 zv = pp->private; 1077 if (zv == NULL) { 1078 if (locked) 1079 mutex_exit(&spa_namespace_lock); 1080 return (SET_ERROR(ENXIO)); 1081 } 1082 1083 if (zv->zv_flags & ZVOL_EXCL) { 1084 ASSERT(zv->zv_total_opens == 1); 1085 zv->zv_flags &= ~ZVOL_EXCL; 1086 } 1087 1088 /* 1089 * If the open count is zero, this is a spurious close. 1090 * That indicates a bug in the kernel / DDI framework. 1091 */ 1092 ASSERT(zv->zv_total_opens != 0); 1093 1094 /* 1095 * You may get multiple opens, but only one close. 1096 */ 1097 zv->zv_total_opens -= count; 1098 1099 if (zv->zv_total_opens == 0) 1100 zvol_last_close(zv); 1101 1102 if (locked) 1103 mutex_exit(&spa_namespace_lock); 1104 return (error); 1105} 1106 1107static void 1108zvol_get_done(zgd_t *zgd, int error) 1109{ 1110 if (zgd->zgd_db) 1111 dmu_buf_rele(zgd->zgd_db, zgd); 1112 1113 zfs_range_unlock(zgd->zgd_rl); 1114 1115 if (error == 0 && zgd->zgd_bp) 1116 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 1117 1118 kmem_free(zgd, sizeof (zgd_t)); 1119} 1120 1121/* 1122 * Get data to generate a TX_WRITE intent log record. 1123 */ 1124static int 1125zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 1126{ 1127 zvol_state_t *zv = arg; 1128 objset_t *os = zv->zv_objset; 1129 uint64_t object = ZVOL_OBJ; 1130 uint64_t offset = lr->lr_offset; 1131 uint64_t size = lr->lr_length; /* length of user data */ 1132 blkptr_t *bp = &lr->lr_blkptr; 1133 dmu_buf_t *db; 1134 zgd_t *zgd; 1135 int error; 1136 1137 ASSERT(zio != NULL); 1138 ASSERT(size != 0); 1139 1140 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 1141 zgd->zgd_zilog = zv->zv_zilog; 1142 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); 1143 1144 /* 1145 * Write records come in two flavors: immediate and indirect. 1146 * For small writes it's cheaper to store the data with the 1147 * log record (immediate); for large writes it's cheaper to 1148 * sync the data and get a pointer to it (indirect) so that 1149 * we don't have to write the data twice. 1150 */ 1151 if (buf != NULL) { /* immediate write */ 1152 error = dmu_read(os, object, offset, size, buf, 1153 DMU_READ_NO_PREFETCH); 1154 } else { 1155 size = zv->zv_volblocksize; 1156 offset = P2ALIGN(offset, size); 1157 error = dmu_buf_hold(os, object, offset, zgd, &db, 1158 DMU_READ_NO_PREFETCH); 1159 if (error == 0) { 1160 blkptr_t *obp = dmu_buf_get_blkptr(db); 1161 if (obp) { 1162 ASSERT(BP_IS_HOLE(bp)); 1163 *bp = *obp; 1164 } 1165 1166 zgd->zgd_db = db; 1167 zgd->zgd_bp = bp; 1168 1169 ASSERT(db->db_offset == offset); 1170 ASSERT(db->db_size == size); 1171 1172 error = dmu_sync(zio, lr->lr_common.lrc_txg, 1173 zvol_get_done, zgd); 1174 1175 if (error == 0) 1176 return (0); 1177 } 1178 } 1179 1180 zvol_get_done(zgd, error); 1181 1182 return (error); 1183} 1184 1185/* 1186 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 1187 * 1188 * We store data in the log buffers if it's small enough. 1189 * Otherwise we will later flush the data out via dmu_sync(). 1190 */ 1191ssize_t zvol_immediate_write_sz = 32768; 1192 1193static void 1194zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 1195 boolean_t sync) 1196{ 1197 uint32_t blocksize = zv->zv_volblocksize; 1198 zilog_t *zilog = zv->zv_zilog; 1199 boolean_t slogging; 1200 ssize_t immediate_write_sz; 1201 1202 if (zil_replaying(zilog, tx)) 1203 return; 1204 1205 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 1206 ? 0 : zvol_immediate_write_sz; 1207 1208 slogging = spa_has_slogs(zilog->zl_spa) && 1209 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 1210 1211 while (resid) { 1212 itx_t *itx; 1213 lr_write_t *lr; 1214 ssize_t len; 1215 itx_wr_state_t write_state; 1216 1217 /* 1218 * Unlike zfs_log_write() we can be called with 1219 * upto DMU_MAX_ACCESS/2 (5MB) writes. 1220 */ 1221 if (blocksize > immediate_write_sz && !slogging && 1222 resid >= blocksize && off % blocksize == 0) { 1223 write_state = WR_INDIRECT; /* uses dmu_sync */ 1224 len = blocksize; 1225 } else if (sync) { 1226 write_state = WR_COPIED; 1227 len = MIN(ZIL_MAX_LOG_DATA, resid); 1228 } else { 1229 write_state = WR_NEED_COPY; 1230 len = MIN(ZIL_MAX_LOG_DATA, resid); 1231 } 1232 1233 itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1234 (write_state == WR_COPIED ? len : 0)); 1235 lr = (lr_write_t *)&itx->itx_lr; 1236 if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 1237 ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1238 zil_itx_destroy(itx); 1239 itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1240 lr = (lr_write_t *)&itx->itx_lr; 1241 write_state = WR_NEED_COPY; 1242 } 1243 1244 itx->itx_wr_state = write_state; 1245 if (write_state == WR_NEED_COPY) 1246 itx->itx_sod += len; 1247 lr->lr_foid = ZVOL_OBJ; 1248 lr->lr_offset = off; 1249 lr->lr_length = len; 1250 lr->lr_blkoff = 0; 1251 BP_ZERO(&lr->lr_blkptr); 1252 1253 itx->itx_private = zv; 1254 itx->itx_sync = sync; 1255 1256 zil_itx_assign(zilog, itx, tx); 1257 1258 off += len; 1259 resid -= len; 1260 } 1261} 1262 1263#ifdef sun 1264static int 1265zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset, 1266 uint64_t size, boolean_t doread, boolean_t isdump) 1267{ 1268 vdev_disk_t *dvd; 1269 int c; 1270 int numerrors = 0; 1271 1272 if (vd->vdev_ops == &vdev_mirror_ops || 1273 vd->vdev_ops == &vdev_replacing_ops || 1274 vd->vdev_ops == &vdev_spare_ops) { 1275 for (c = 0; c < vd->vdev_children; c++) { 1276 int err = zvol_dumpio_vdev(vd->vdev_child[c], 1277 addr, offset, origoffset, size, doread, isdump); 1278 if (err != 0) { 1279 numerrors++; 1280 } else if (doread) { 1281 break; 1282 } 1283 } 1284 } 1285 1286 if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops) 1287 return (numerrors < vd->vdev_children ? 0 : EIO); 1288 1289 if (doread && !vdev_readable(vd)) 1290 return (SET_ERROR(EIO)); 1291 else if (!doread && !vdev_writeable(vd)) 1292 return (SET_ERROR(EIO)); 1293 1294 if (vd->vdev_ops == &vdev_raidz_ops) { 1295 return (vdev_raidz_physio(vd, 1296 addr, size, offset, origoffset, doread, isdump)); 1297 } 1298 1299 offset += VDEV_LABEL_START_SIZE; 1300 1301 if (ddi_in_panic() || isdump) { 1302 ASSERT(!doread); 1303 if (doread) 1304 return (SET_ERROR(EIO)); 1305 dvd = vd->vdev_tsd; 1306 ASSERT3P(dvd, !=, NULL); 1307 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1308 lbtodb(size))); 1309 } else { 1310 dvd = vd->vdev_tsd; 1311 ASSERT3P(dvd, !=, NULL); 1312 return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size, 1313 offset, doread ? B_READ : B_WRITE)); 1314 } 1315} 1316 1317static int 1318zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 1319 boolean_t doread, boolean_t isdump) 1320{ 1321 vdev_t *vd; 1322 int error; 1323 zvol_extent_t *ze; 1324 spa_t *spa = dmu_objset_spa(zv->zv_objset); 1325 1326 /* Must be sector aligned, and not stradle a block boundary. */ 1327 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 1328 P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 1329 return (SET_ERROR(EINVAL)); 1330 } 1331 ASSERT(size <= zv->zv_volblocksize); 1332 1333 /* Locate the extent this belongs to */ 1334 ze = list_head(&zv->zv_extents); 1335 while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 1336 offset -= ze->ze_nblks * zv->zv_volblocksize; 1337 ze = list_next(&zv->zv_extents, ze); 1338 } 1339 1340 if (ze == NULL) 1341 return (SET_ERROR(EINVAL)); 1342 1343 if (!ddi_in_panic()) 1344 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 1345 1346 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 1347 offset += DVA_GET_OFFSET(&ze->ze_dva); 1348 error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva), 1349 size, doread, isdump); 1350 1351 if (!ddi_in_panic()) 1352 spa_config_exit(spa, SCL_STATE, FTAG); 1353 1354 return (error); 1355} 1356#endif /* sun */ 1357 1358void 1359zvol_strategy(struct bio *bp) 1360{ 1361 zvol_state_t *zv; 1362 uint64_t off, volsize; 1363 size_t resid; 1364 char *addr; 1365 objset_t *os; 1366 rl_t *rl; 1367 int error = 0; 1368 boolean_t doread = 0; 1369 boolean_t is_dumpified; 1370 boolean_t sync; 1371 1372 if (bp->bio_to) 1373 zv = bp->bio_to->private; 1374 else 1375 zv = bp->bio_dev->si_drv2; 1376 1377 if (zv == NULL) { 1378 error = ENXIO; 1379 goto out; 1380 } 1381 1382 if (bp->bio_cmd != BIO_READ && (zv->zv_flags & ZVOL_RDONLY)) { 1383 error = EROFS; 1384 goto out; 1385 } 1386 1387 switch (bp->bio_cmd) { 1388 case BIO_FLUSH: 1389 goto sync; 1390 case BIO_READ: 1391 doread = 1; 1392 case BIO_WRITE: 1393 case BIO_DELETE: 1394 break; 1395 default: 1396 error = EOPNOTSUPP; 1397 goto out; 1398 } 1399 1400 off = bp->bio_offset; 1401 volsize = zv->zv_volsize; 1402 1403 os = zv->zv_objset; 1404 ASSERT(os != NULL); 1405 1406 addr = bp->bio_data; 1407 resid = bp->bio_length; 1408 1409 if (resid > 0 && (off < 0 || off >= volsize)) { 1410 error = EIO; 1411 goto out; 1412 } 1413 1414#ifdef illumos 1415 is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED; 1416#else 1417 is_dumpified = B_FALSE; 1418#endif 1419 sync = !doread && !is_dumpified && 1420 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS; 1421 1422 /* 1423 * There must be no buffer changes when doing a dmu_sync() because 1424 * we can't change the data whilst calculating the checksum. 1425 */ 1426 rl = zfs_range_lock(&zv->zv_znode, off, resid, 1427 doread ? RL_READER : RL_WRITER); 1428 1429 if (bp->bio_cmd == BIO_DELETE) { 1430 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 1431 error = dmu_tx_assign(tx, TXG_WAIT); 1432 if (error != 0) { 1433 dmu_tx_abort(tx); 1434 } else { 1435 zvol_log_truncate(zv, tx, off, resid, B_TRUE); 1436 dmu_tx_commit(tx); 1437 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 1438 off, resid); 1439 resid = 0; 1440 } 1441 goto unlock; 1442 } 1443 1444 while (resid != 0 && off < volsize) { 1445 size_t size = MIN(resid, zvol_maxphys); 1446#ifdef illumos 1447 if (is_dumpified) { 1448 size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 1449 error = zvol_dumpio(zv, addr, off, size, 1450 doread, B_FALSE); 1451 } else if (doread) { 1452#else 1453 if (doread) { 1454#endif 1455 error = dmu_read(os, ZVOL_OBJ, off, size, addr, 1456 DMU_READ_PREFETCH); 1457 } else { 1458 dmu_tx_t *tx = dmu_tx_create(os); 1459 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1460 error = dmu_tx_assign(tx, TXG_WAIT); 1461 if (error) { 1462 dmu_tx_abort(tx); 1463 } else { 1464 dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1465 zvol_log_write(zv, tx, off, size, sync); 1466 dmu_tx_commit(tx); 1467 } 1468 } 1469 if (error) { 1470 /* convert checksum errors into IO errors */ 1471 if (error == ECKSUM) 1472 error = SET_ERROR(EIO); 1473 break; 1474 } 1475 off += size; 1476 addr += size; 1477 resid -= size; 1478 } 1479unlock: 1480 zfs_range_unlock(rl); 1481 1482 bp->bio_completed = bp->bio_length - resid; 1483 if (bp->bio_completed < bp->bio_length && off > volsize) 1484 error = EINVAL; 1485 1486 if (sync) { 1487sync: 1488 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1489 } 1490out: 1491 if (bp->bio_to) 1492 g_io_deliver(bp, error); 1493 else 1494 biofinish(bp, NULL, error); 1495} 1496 1497#ifdef sun 1498/* 1499 * Set the buffer count to the zvol maximum transfer. 1500 * Using our own routine instead of the default minphys() 1501 * means that for larger writes we write bigger buffers on X86 1502 * (128K instead of 56K) and flush the disk write cache less often 1503 * (every zvol_maxphys - currently 1MB) instead of minphys (currently 1504 * 56K on X86 and 128K on sparc). 1505 */ 1506void 1507zvol_minphys(struct buf *bp) 1508{ 1509 if (bp->b_bcount > zvol_maxphys) 1510 bp->b_bcount = zvol_maxphys; 1511} 1512 1513int 1514zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1515{ 1516 minor_t minor = getminor(dev); 1517 zvol_state_t *zv; 1518 int error = 0; 1519 uint64_t size; 1520 uint64_t boff; 1521 uint64_t resid; 1522 1523 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1524 if (zv == NULL) 1525 return (SET_ERROR(ENXIO)); 1526 1527 if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0) 1528 return (SET_ERROR(EINVAL)); 1529 1530 boff = ldbtob(blkno); 1531 resid = ldbtob(nblocks); 1532 1533 VERIFY3U(boff + resid, <=, zv->zv_volsize); 1534 1535 while (resid) { 1536 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 1537 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1538 if (error) 1539 break; 1540 boff += size; 1541 addr += size; 1542 resid -= size; 1543 } 1544 1545 return (error); 1546} 1547 1548/*ARGSUSED*/ 1549int 1550zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1551{ 1552 minor_t minor = getminor(dev); 1553#else 1554int 1555zvol_read(struct cdev *dev, struct uio *uio, int ioflag) 1556{ 1557#endif 1558 zvol_state_t *zv; 1559 uint64_t volsize; 1560 rl_t *rl; 1561 int error = 0; 1562 1563#ifdef sun 1564 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1565 if (zv == NULL) 1566 return (SET_ERROR(ENXIO)); 1567#else 1568 zv = dev->si_drv2; 1569#endif 1570 1571 volsize = zv->zv_volsize; 1572 if (uio->uio_resid > 0 && 1573 (uio->uio_loffset < 0 || uio->uio_loffset > volsize)) 1574 return (SET_ERROR(EIO)); 1575 1576#ifdef illumos 1577 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1578 error = physio(zvol_strategy, NULL, dev, B_READ, 1579 zvol_minphys, uio); 1580 return (error); 1581 } 1582#endif 1583 1584 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1585 RL_READER); 1586 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1587 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1588 1589 /* don't read past the end */ 1590 if (bytes > volsize - uio->uio_loffset) 1591 bytes = volsize - uio->uio_loffset; 1592 1593 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1594 if (error) { 1595 /* convert checksum errors into IO errors */ 1596 if (error == ECKSUM) 1597 error = SET_ERROR(EIO); 1598 break; 1599 } 1600 } 1601 zfs_range_unlock(rl); 1602 return (error); 1603} 1604 1605#ifdef sun 1606/*ARGSUSED*/ 1607int 1608zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1609{ 1610 minor_t minor = getminor(dev); 1611#else 1612int 1613zvol_write(struct cdev *dev, struct uio *uio, int ioflag) 1614{ 1615#endif 1616 zvol_state_t *zv; 1617 uint64_t volsize; 1618 rl_t *rl; 1619 int error = 0; 1620 boolean_t sync; 1621 1622#ifdef sun 1623 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1624 if (zv == NULL) 1625 return (SET_ERROR(ENXIO)); 1626#else 1627 zv = dev->si_drv2; 1628#endif 1629 1630 volsize = zv->zv_volsize; 1631 if (uio->uio_resid > 0 && 1632 (uio->uio_loffset < 0 || uio->uio_loffset > volsize)) 1633 return (SET_ERROR(EIO)); 1634 1635#ifdef illumos 1636 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1637 error = physio(zvol_strategy, NULL, dev, B_WRITE, 1638 zvol_minphys, uio); 1639 return (error); 1640 } 1641#endif 1642 1643#ifdef sun 1644 sync = !(zv->zv_flags & ZVOL_WCE) || 1645#else 1646 sync = (ioflag & IO_SYNC) || 1647#endif 1648 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS); 1649 1650 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1651 RL_WRITER); 1652 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1653 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1654 uint64_t off = uio->uio_loffset; 1655 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 1656 1657 if (bytes > volsize - off) /* don't write past the end */ 1658 bytes = volsize - off; 1659 1660 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1661 error = dmu_tx_assign(tx, TXG_WAIT); 1662 if (error) { 1663 dmu_tx_abort(tx); 1664 break; 1665 } 1666 error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx); 1667 if (error == 0) 1668 zvol_log_write(zv, tx, off, bytes, sync); 1669 dmu_tx_commit(tx); 1670 1671 if (error) 1672 break; 1673 } 1674 zfs_range_unlock(rl); 1675 if (sync) 1676 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1677 return (error); 1678} 1679 1680#ifdef sun 1681int 1682zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1683{ 1684 struct uuid uuid = EFI_RESERVED; 1685 efi_gpe_t gpe = { 0 }; 1686 uint32_t crc; 1687 dk_efi_t efi; 1688 int length; 1689 char *ptr; 1690 1691 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1692 return (SET_ERROR(EFAULT)); 1693 ptr = (char *)(uintptr_t)efi.dki_data_64; 1694 length = efi.dki_length; 1695 /* 1696 * Some clients may attempt to request a PMBR for the 1697 * zvol. Currently this interface will return EINVAL to 1698 * such requests. These requests could be supported by 1699 * adding a check for lba == 0 and consing up an appropriate 1700 * PMBR. 1701 */ 1702 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1703 return (SET_ERROR(EINVAL)); 1704 1705 gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1706 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1707 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1708 1709 if (efi.dki_lba == 1) { 1710 efi_gpt_t gpt = { 0 }; 1711 1712 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1713 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1714 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1715 gpt.efi_gpt_MyLBA = LE_64(1ULL); 1716 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1717 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1718 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1719 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1720 gpt.efi_gpt_SizeOfPartitionEntry = 1721 LE_32(sizeof (efi_gpe_t)); 1722 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1723 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1724 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1725 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1726 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1727 flag)) 1728 return (SET_ERROR(EFAULT)); 1729 ptr += sizeof (gpt); 1730 length -= sizeof (gpt); 1731 } 1732 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1733 length), flag)) 1734 return (SET_ERROR(EFAULT)); 1735 return (0); 1736} 1737 1738/* 1739 * BEGIN entry points to allow external callers access to the volume. 1740 */ 1741/* 1742 * Return the volume parameters needed for access from an external caller. 1743 * These values are invariant as long as the volume is held open. 1744 */ 1745int 1746zvol_get_volume_params(minor_t minor, uint64_t *blksize, 1747 uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl, 1748 void **rl_hdl, void **bonus_hdl) 1749{ 1750 zvol_state_t *zv; 1751 1752 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1753 if (zv == NULL) 1754 return (SET_ERROR(ENXIO)); 1755 if (zv->zv_flags & ZVOL_DUMPIFIED) 1756 return (SET_ERROR(ENXIO)); 1757 1758 ASSERT(blksize && max_xfer_len && minor_hdl && 1759 objset_hdl && zil_hdl && rl_hdl && bonus_hdl); 1760 1761 *blksize = zv->zv_volblocksize; 1762 *max_xfer_len = (uint64_t)zvol_maxphys; 1763 *minor_hdl = zv; 1764 *objset_hdl = zv->zv_objset; 1765 *zil_hdl = zv->zv_zilog; 1766 *rl_hdl = &zv->zv_znode; 1767 *bonus_hdl = zv->zv_dbuf; 1768 return (0); 1769} 1770 1771/* 1772 * Return the current volume size to an external caller. 1773 * The size can change while the volume is open. 1774 */ 1775uint64_t 1776zvol_get_volume_size(void *minor_hdl) 1777{ 1778 zvol_state_t *zv = minor_hdl; 1779 1780 return (zv->zv_volsize); 1781} 1782 1783/* 1784 * Return the current WCE setting to an external caller. 1785 * The WCE setting can change while the volume is open. 1786 */ 1787int 1788zvol_get_volume_wce(void *minor_hdl) 1789{ 1790 zvol_state_t *zv = minor_hdl; 1791 1792 return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0); 1793} 1794 1795/* 1796 * Entry point for external callers to zvol_log_write 1797 */ 1798void 1799zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid, 1800 boolean_t sync) 1801{ 1802 zvol_state_t *zv = minor_hdl; 1803 1804 zvol_log_write(zv, tx, off, resid, sync); 1805} 1806/* 1807 * END entry points to allow external callers access to the volume. 1808 */ 1809#endif /* sun */ 1810 1811/* 1812 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE. 1813 */ 1814static void 1815zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len, 1816 boolean_t sync) 1817{ 1818 itx_t *itx; 1819 lr_truncate_t *lr; 1820 zilog_t *zilog = zv->zv_zilog; 1821 1822 if (zil_replaying(zilog, tx)) 1823 return; 1824 1825 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); 1826 lr = (lr_truncate_t *)&itx->itx_lr; 1827 lr->lr_foid = ZVOL_OBJ; 1828 lr->lr_offset = off; 1829 lr->lr_length = len; 1830 1831 itx->itx_sync = sync; 1832 zil_itx_assign(zilog, itx, tx); 1833} 1834 1835#ifdef sun 1836/* 1837 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1838 * Also a dirtbag dkio ioctl for unmap/free-block functionality. 1839 */ 1840/*ARGSUSED*/ 1841int 1842zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1843{ 1844 zvol_state_t *zv; 1845 struct dk_callback *dkc; 1846 int error = 0; 1847 rl_t *rl; 1848 1849 mutex_enter(&spa_namespace_lock); 1850 1851 zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL); 1852 1853 if (zv == NULL) { 1854 mutex_exit(&spa_namespace_lock); 1855 return (SET_ERROR(ENXIO)); 1856 } 1857 ASSERT(zv->zv_total_opens > 0); 1858 1859 switch (cmd) { 1860 1861 case DKIOCINFO: 1862 { 1863 struct dk_cinfo dki; 1864 1865 bzero(&dki, sizeof (dki)); 1866 (void) strcpy(dki.dki_cname, "zvol"); 1867 (void) strcpy(dki.dki_dname, "zvol"); 1868 dki.dki_ctype = DKC_UNKNOWN; 1869 dki.dki_unit = getminor(dev); 1870 dki.dki_maxtransfer = 1871 1 << (SPA_OLD_MAXBLOCKSHIFT - zv->zv_min_bs); 1872 mutex_exit(&spa_namespace_lock); 1873 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1874 error = SET_ERROR(EFAULT); 1875 return (error); 1876 } 1877 1878 case DKIOCGMEDIAINFO: 1879 { 1880 struct dk_minfo dkm; 1881 1882 bzero(&dkm, sizeof (dkm)); 1883 dkm.dki_lbsize = 1U << zv->zv_min_bs; 1884 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1885 dkm.dki_media_type = DK_UNKNOWN; 1886 mutex_exit(&spa_namespace_lock); 1887 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1888 error = SET_ERROR(EFAULT); 1889 return (error); 1890 } 1891 1892 case DKIOCGMEDIAINFOEXT: 1893 { 1894 struct dk_minfo_ext dkmext; 1895 1896 bzero(&dkmext, sizeof (dkmext)); 1897 dkmext.dki_lbsize = 1U << zv->zv_min_bs; 1898 dkmext.dki_pbsize = zv->zv_volblocksize; 1899 dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1900 dkmext.dki_media_type = DK_UNKNOWN; 1901 mutex_exit(&spa_namespace_lock); 1902 if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag)) 1903 error = SET_ERROR(EFAULT); 1904 return (error); 1905 } 1906 1907 case DKIOCGETEFI: 1908 { 1909 uint64_t vs = zv->zv_volsize; 1910 uint8_t bs = zv->zv_min_bs; 1911 1912 mutex_exit(&spa_namespace_lock); 1913 error = zvol_getefi((void *)arg, flag, vs, bs); 1914 return (error); 1915 } 1916 1917 case DKIOCFLUSHWRITECACHE: 1918 dkc = (struct dk_callback *)arg; 1919 mutex_exit(&spa_namespace_lock); 1920 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1921 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1922 (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1923 error = 0; 1924 } 1925 return (error); 1926 1927 case DKIOCGETWCE: 1928 { 1929 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1930 if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1931 flag)) 1932 error = SET_ERROR(EFAULT); 1933 break; 1934 } 1935 case DKIOCSETWCE: 1936 { 1937 int wce; 1938 if (ddi_copyin((void *)arg, &wce, sizeof (int), 1939 flag)) { 1940 error = SET_ERROR(EFAULT); 1941 break; 1942 } 1943 if (wce) { 1944 zv->zv_flags |= ZVOL_WCE; 1945 mutex_exit(&spa_namespace_lock); 1946 } else { 1947 zv->zv_flags &= ~ZVOL_WCE; 1948 mutex_exit(&spa_namespace_lock); 1949 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1950 } 1951 return (0); 1952 } 1953 1954 case DKIOCGGEOM: 1955 case DKIOCGVTOC: 1956 /* 1957 * commands using these (like prtvtoc) expect ENOTSUP 1958 * since we're emulating an EFI label 1959 */ 1960 error = SET_ERROR(ENOTSUP); 1961 break; 1962 1963 case DKIOCDUMPINIT: 1964 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1965 RL_WRITER); 1966 error = zvol_dumpify(zv); 1967 zfs_range_unlock(rl); 1968 break; 1969 1970 case DKIOCDUMPFINI: 1971 if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 1972 break; 1973 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1974 RL_WRITER); 1975 error = zvol_dump_fini(zv); 1976 zfs_range_unlock(rl); 1977 break; 1978 1979 case DKIOCFREE: 1980 { 1981 dkioc_free_t df; 1982 dmu_tx_t *tx; 1983 1984 if (!zvol_unmap_enabled) 1985 break; 1986 1987 if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) { 1988 error = SET_ERROR(EFAULT); 1989 break; 1990 } 1991 1992 /* 1993 * Apply Postel's Law to length-checking. If they overshoot, 1994 * just blank out until the end, if there's a need to blank 1995 * out anything. 1996 */ 1997 if (df.df_start >= zv->zv_volsize) 1998 break; /* No need to do anything... */ 1999 2000 mutex_exit(&spa_namespace_lock); 2001 2002 rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length, 2003 RL_WRITER); 2004 tx = dmu_tx_create(zv->zv_objset); 2005 dmu_tx_mark_netfree(tx); 2006 error = dmu_tx_assign(tx, TXG_WAIT); 2007 if (error != 0) { 2008 dmu_tx_abort(tx); 2009 } else { 2010 zvol_log_truncate(zv, tx, df.df_start, 2011 df.df_length, B_TRUE); 2012 dmu_tx_commit(tx); 2013 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 2014 df.df_start, df.df_length); 2015 } 2016 2017 zfs_range_unlock(rl); 2018 2019 if (error == 0) { 2020 /* 2021 * If the write-cache is disabled or 'sync' property 2022 * is set to 'always' then treat this as a synchronous 2023 * operation (i.e. commit to zil). 2024 */ 2025 if (!(zv->zv_flags & ZVOL_WCE) || 2026 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) 2027 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2028 2029 /* 2030 * If the caller really wants synchronous writes, and 2031 * can't wait for them, don't return until the write 2032 * is done. 2033 */ 2034 if (df.df_flags & DF_WAIT_SYNC) { 2035 txg_wait_synced( 2036 dmu_objset_pool(zv->zv_objset), 0); 2037 } 2038 } 2039 return (error); 2040 } 2041 2042 default: 2043 error = SET_ERROR(ENOTTY); 2044 break; 2045 2046 } 2047 mutex_exit(&spa_namespace_lock); 2048 return (error); 2049} 2050#endif /* sun */ 2051 2052int 2053zvol_busy(void) 2054{ 2055 return (zvol_minors != 0); 2056} 2057 2058void 2059zvol_init(void) 2060{ 2061 VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t), 2062 1) == 0); 2063 ZFS_LOG(1, "ZVOL Initialized."); 2064} 2065 2066void 2067zvol_fini(void) 2068{ 2069 ddi_soft_state_fini(&zfsdev_state); 2070 ZFS_LOG(1, "ZVOL Deinitialized."); 2071} 2072 2073#ifdef sun 2074/*ARGSUSED*/ 2075static int 2076zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx) 2077{ 2078 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 2079 2080 if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 2081 return (1); 2082 return (0); 2083} 2084 2085/*ARGSUSED*/ 2086static void 2087zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx) 2088{ 2089 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 2090 2091 spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx); 2092} 2093 2094static int 2095zvol_dump_init(zvol_state_t *zv, boolean_t resize) 2096{ 2097 dmu_tx_t *tx; 2098 int error; 2099 objset_t *os = zv->zv_objset; 2100 spa_t *spa = dmu_objset_spa(os); 2101 vdev_t *vd = spa->spa_root_vdev; 2102 nvlist_t *nv = NULL; 2103 uint64_t version = spa_version(spa); 2104 enum zio_checksum checksum; 2105 2106 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2107 ASSERT(vd->vdev_ops == &vdev_root_ops); 2108 2109 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 2110 DMU_OBJECT_END); 2111 /* wait for dmu_free_long_range to actually free the blocks */ 2112 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 2113 2114 /* 2115 * If the pool on which the dump device is being initialized has more 2116 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is 2117 * enabled. If so, bump that feature's counter to indicate that the 2118 * feature is active. We also check the vdev type to handle the 2119 * following case: 2120 * # zpool create test raidz disk1 disk2 disk3 2121 * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev), 2122 * the raidz vdev itself has 3 children. 2123 */ 2124 if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) { 2125 if (!spa_feature_is_enabled(spa, 2126 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 2127 return (SET_ERROR(ENOTSUP)); 2128 (void) dsl_sync_task(spa_name(spa), 2129 zfs_mvdev_dump_feature_check, 2130 zfs_mvdev_dump_activate_feature_sync, NULL, 2131 2, ZFS_SPACE_CHECK_RESERVED); 2132 } 2133 2134 tx = dmu_tx_create(os); 2135 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2136 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 2137 error = dmu_tx_assign(tx, TXG_WAIT); 2138 if (error) { 2139 dmu_tx_abort(tx); 2140 return (error); 2141 } 2142 2143 /* 2144 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum 2145 * function. Otherwise, use the old default -- OFF. 2146 */ 2147 checksum = spa_feature_is_active(spa, 2148 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY : 2149 ZIO_CHECKSUM_OFF; 2150 2151 /* 2152 * If we are resizing the dump device then we only need to 2153 * update the refreservation to match the newly updated 2154 * zvolsize. Otherwise, we save off the original state of the 2155 * zvol so that we can restore them if the zvol is ever undumpified. 2156 */ 2157 if (resize) { 2158 error = zap_update(os, ZVOL_ZAP_OBJ, 2159 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 2160 &zv->zv_volsize, tx); 2161 } else { 2162 uint64_t checksum, compress, refresrv, vbs, dedup; 2163 2164 error = dsl_prop_get_integer(zv->zv_name, 2165 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 2166 error = error ? error : dsl_prop_get_integer(zv->zv_name, 2167 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 2168 error = error ? error : dsl_prop_get_integer(zv->zv_name, 2169 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 2170 error = error ? error : dsl_prop_get_integer(zv->zv_name, 2171 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 2172 if (version >= SPA_VERSION_DEDUP) { 2173 error = error ? error : 2174 dsl_prop_get_integer(zv->zv_name, 2175 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 2176 } 2177 2178 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2179 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 2180 &compress, tx); 2181 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2182 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 2183 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2184 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 2185 &refresrv, tx); 2186 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2187 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 2188 &vbs, tx); 2189 error = error ? error : dmu_object_set_blocksize( 2190 os, ZVOL_OBJ, SPA_OLD_MAXBLOCKSIZE, 0, tx); 2191 if (version >= SPA_VERSION_DEDUP) { 2192 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2193 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 2194 &dedup, tx); 2195 } 2196 if (error == 0) 2197 zv->zv_volblocksize = SPA_OLD_MAXBLOCKSIZE; 2198 } 2199 dmu_tx_commit(tx); 2200 2201 /* 2202 * We only need update the zvol's property if we are initializing 2203 * the dump area for the first time. 2204 */ 2205 if (!resize) { 2206 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2207 VERIFY(nvlist_add_uint64(nv, 2208 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 2209 VERIFY(nvlist_add_uint64(nv, 2210 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 2211 ZIO_COMPRESS_OFF) == 0); 2212 VERIFY(nvlist_add_uint64(nv, 2213 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 2214 checksum) == 0); 2215 if (version >= SPA_VERSION_DEDUP) { 2216 VERIFY(nvlist_add_uint64(nv, 2217 zfs_prop_to_name(ZFS_PROP_DEDUP), 2218 ZIO_CHECKSUM_OFF) == 0); 2219 } 2220 2221 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 2222 nv, NULL); 2223 nvlist_free(nv); 2224 2225 if (error) 2226 return (error); 2227 } 2228 2229 /* Allocate the space for the dump */ 2230 error = zvol_prealloc(zv); 2231 return (error); 2232} 2233 2234static int 2235zvol_dumpify(zvol_state_t *zv) 2236{ 2237 int error = 0; 2238 uint64_t dumpsize = 0; 2239 dmu_tx_t *tx; 2240 objset_t *os = zv->zv_objset; 2241 2242 if (zv->zv_flags & ZVOL_RDONLY) 2243 return (SET_ERROR(EROFS)); 2244 2245 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 2246 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 2247 boolean_t resize = (dumpsize > 0); 2248 2249 if ((error = zvol_dump_init(zv, resize)) != 0) { 2250 (void) zvol_dump_fini(zv); 2251 return (error); 2252 } 2253 } 2254 2255 /* 2256 * Build up our lba mapping. 2257 */ 2258 error = zvol_get_lbas(zv); 2259 if (error) { 2260 (void) zvol_dump_fini(zv); 2261 return (error); 2262 } 2263 2264 tx = dmu_tx_create(os); 2265 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2266 error = dmu_tx_assign(tx, TXG_WAIT); 2267 if (error) { 2268 dmu_tx_abort(tx); 2269 (void) zvol_dump_fini(zv); 2270 return (error); 2271 } 2272 2273 zv->zv_flags |= ZVOL_DUMPIFIED; 2274 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 2275 &zv->zv_volsize, tx); 2276 dmu_tx_commit(tx); 2277 2278 if (error) { 2279 (void) zvol_dump_fini(zv); 2280 return (error); 2281 } 2282 2283 txg_wait_synced(dmu_objset_pool(os), 0); 2284 return (0); 2285} 2286 2287static int 2288zvol_dump_fini(zvol_state_t *zv) 2289{ 2290 dmu_tx_t *tx; 2291 objset_t *os = zv->zv_objset; 2292 nvlist_t *nv; 2293 int error = 0; 2294 uint64_t checksum, compress, refresrv, vbs, dedup; 2295 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 2296 2297 /* 2298 * Attempt to restore the zvol back to its pre-dumpified state. 2299 * This is a best-effort attempt as it's possible that not all 2300 * of these properties were initialized during the dumpify process 2301 * (i.e. error during zvol_dump_init). 2302 */ 2303 2304 tx = dmu_tx_create(os); 2305 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2306 error = dmu_tx_assign(tx, TXG_WAIT); 2307 if (error) { 2308 dmu_tx_abort(tx); 2309 return (error); 2310 } 2311 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 2312 dmu_tx_commit(tx); 2313 2314 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2315 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 2316 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2317 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 2318 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2319 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 2320 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2321 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 2322 2323 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2324 (void) nvlist_add_uint64(nv, 2325 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 2326 (void) nvlist_add_uint64(nv, 2327 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 2328 (void) nvlist_add_uint64(nv, 2329 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 2330 if (version >= SPA_VERSION_DEDUP && 2331 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2332 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 2333 (void) nvlist_add_uint64(nv, 2334 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 2335 } 2336 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 2337 nv, NULL); 2338 nvlist_free(nv); 2339 2340 zvol_free_extents(zv); 2341 zv->zv_flags &= ~ZVOL_DUMPIFIED; 2342 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 2343 /* wait for dmu_free_long_range to actually free the blocks */ 2344 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 2345 tx = dmu_tx_create(os); 2346 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 2347 error = dmu_tx_assign(tx, TXG_WAIT); 2348 if (error) { 2349 dmu_tx_abort(tx); 2350 return (error); 2351 } 2352 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 2353 zv->zv_volblocksize = vbs; 2354 dmu_tx_commit(tx); 2355 2356 return (0); 2357} 2358#endif /* sun */ 2359 2360static void 2361zvol_geom_run(zvol_state_t *zv) 2362{ 2363 struct g_provider *pp; 2364 2365 pp = zv->zv_provider; 2366 g_error_provider(pp, 0); 2367 2368 kproc_kthread_add(zvol_geom_worker, zv, &zfsproc, NULL, 0, 0, 2369 "zfskern", "zvol %s", pp->name + sizeof(ZVOL_DRIVER)); 2370} 2371 2372static void 2373zvol_geom_destroy(zvol_state_t *zv) 2374{ 2375 struct g_provider *pp; 2376 2377 g_topology_assert(); 2378 2379 mtx_lock(&zv->zv_queue_mtx); 2380 zv->zv_state = 1; 2381 wakeup_one(&zv->zv_queue); 2382 while (zv->zv_state != 2) 2383 msleep(&zv->zv_state, &zv->zv_queue_mtx, 0, "zvol:w", 0); 2384 mtx_destroy(&zv->zv_queue_mtx); 2385 2386 pp = zv->zv_provider; 2387 zv->zv_provider = NULL; 2388 pp->private = NULL; 2389 g_wither_geom(pp->geom, ENXIO); 2390} 2391 2392static int 2393zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace) 2394{ 2395 int count, error, flags; 2396 2397 g_topology_assert(); 2398 2399 /* 2400 * To make it easier we expect either open or close, but not both 2401 * at the same time. 2402 */ 2403 KASSERT((acr >= 0 && acw >= 0 && ace >= 0) || 2404 (acr <= 0 && acw <= 0 && ace <= 0), 2405 ("Unsupported access request to %s (acr=%d, acw=%d, ace=%d).", 2406 pp->name, acr, acw, ace)); 2407 2408 if (pp->private == NULL) { 2409 if (acr <= 0 && acw <= 0 && ace <= 0) 2410 return (0); 2411 return (pp->error); 2412 } 2413 2414 /* 2415 * We don't pass FEXCL flag to zvol_open()/zvol_close() if ace != 0, 2416 * because GEOM already handles that and handles it a bit differently. 2417 * GEOM allows for multiple read/exclusive consumers and ZFS allows 2418 * only one exclusive consumer, no matter if it is reader or writer. 2419 * I like better the way GEOM works so I'll leave it for GEOM to 2420 * decide what to do. 2421 */ 2422 2423 count = acr + acw + ace; 2424 if (count == 0) 2425 return (0); 2426 2427 flags = 0; 2428 if (acr != 0 || ace != 0) 2429 flags |= FREAD; 2430 if (acw != 0) 2431 flags |= FWRITE; 2432 2433 g_topology_unlock(); 2434 if (count > 0) 2435 error = zvol_open(pp, flags, count); 2436 else 2437 error = zvol_close(pp, flags, -count); 2438 g_topology_lock(); 2439 return (error); 2440} 2441 2442static void 2443zvol_geom_start(struct bio *bp) 2444{ 2445 zvol_state_t *zv; 2446 boolean_t first; 2447 2448 zv = bp->bio_to->private; 2449 ASSERT(zv != NULL); 2450 switch (bp->bio_cmd) { 2451 case BIO_FLUSH: 2452 if (!THREAD_CAN_SLEEP()) 2453 goto enqueue; 2454 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2455 g_io_deliver(bp, 0); 2456 break; 2457 case BIO_READ: 2458 case BIO_WRITE: 2459 case BIO_DELETE: 2460 if (!THREAD_CAN_SLEEP()) 2461 goto enqueue; 2462 zvol_strategy(bp); 2463 break; 2464 case BIO_GETATTR: { 2465 spa_t *spa = dmu_objset_spa(zv->zv_objset); 2466 uint64_t refd, avail, usedobjs, availobjs, val; 2467 2468 if (g_handleattr_int(bp, "GEOM::candelete", 1)) 2469 return; 2470 if (strcmp(bp->bio_attribute, "blocksavail") == 0) { 2471 dmu_objset_space(zv->zv_objset, &refd, &avail, 2472 &usedobjs, &availobjs); 2473 if (g_handleattr_off_t(bp, "blocksavail", 2474 avail / DEV_BSIZE)) 2475 return; 2476 } else if (strcmp(bp->bio_attribute, "blocksused") == 0) { 2477 dmu_objset_space(zv->zv_objset, &refd, &avail, 2478 &usedobjs, &availobjs); 2479 if (g_handleattr_off_t(bp, "blocksused", 2480 refd / DEV_BSIZE)) 2481 return; 2482 } else if (strcmp(bp->bio_attribute, "poolblocksavail") == 0) { 2483 avail = metaslab_class_get_space(spa_normal_class(spa)); 2484 avail -= metaslab_class_get_alloc(spa_normal_class(spa)); 2485 if (g_handleattr_off_t(bp, "poolblocksavail", 2486 avail / DEV_BSIZE)) 2487 return; 2488 } else if (strcmp(bp->bio_attribute, "poolblocksused") == 0) { 2489 refd = metaslab_class_get_alloc(spa_normal_class(spa)); 2490 if (g_handleattr_off_t(bp, "poolblocksused", 2491 refd / DEV_BSIZE)) 2492 return; 2493 } 2494 /* FALLTHROUGH */ 2495 } 2496 default: 2497 g_io_deliver(bp, EOPNOTSUPP); 2498 break; 2499 } 2500 return; 2501 2502enqueue: 2503 mtx_lock(&zv->zv_queue_mtx); 2504 first = (bioq_first(&zv->zv_queue) == NULL); 2505 bioq_insert_tail(&zv->zv_queue, bp); 2506 mtx_unlock(&zv->zv_queue_mtx); 2507 if (first) 2508 wakeup_one(&zv->zv_queue); 2509} 2510 2511static void 2512zvol_geom_worker(void *arg) 2513{ 2514 zvol_state_t *zv; 2515 struct bio *bp; 2516 2517 thread_lock(curthread); 2518 sched_prio(curthread, PRIBIO); 2519 thread_unlock(curthread); 2520 2521 zv = arg; 2522 for (;;) { 2523 mtx_lock(&zv->zv_queue_mtx); 2524 bp = bioq_takefirst(&zv->zv_queue); 2525 if (bp == NULL) { 2526 if (zv->zv_state == 1) { 2527 zv->zv_state = 2; 2528 wakeup(&zv->zv_state); 2529 mtx_unlock(&zv->zv_queue_mtx); 2530 kthread_exit(); 2531 } 2532 msleep(&zv->zv_queue, &zv->zv_queue_mtx, PRIBIO | PDROP, 2533 "zvol:io", 0); 2534 continue; 2535 } 2536 mtx_unlock(&zv->zv_queue_mtx); 2537 switch (bp->bio_cmd) { 2538 case BIO_FLUSH: 2539 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2540 g_io_deliver(bp, 0); 2541 break; 2542 case BIO_READ: 2543 case BIO_WRITE: 2544 zvol_strategy(bp); 2545 break; 2546 } 2547 } 2548} 2549 2550extern boolean_t dataset_name_hidden(const char *name); 2551 2552static int 2553zvol_create_snapshots(objset_t *os, const char *name) 2554{ 2555 uint64_t cookie, obj; 2556 char *sname; 2557 int error, len; 2558 2559 cookie = obj = 0; 2560 sname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2561 2562#if 0 2563 (void) dmu_objset_find(name, dmu_objset_prefetch, NULL, 2564 DS_FIND_SNAPSHOTS); 2565#endif 2566 2567 for (;;) { 2568 len = snprintf(sname, MAXPATHLEN, "%s@", name); 2569 if (len >= MAXPATHLEN) { 2570 dmu_objset_rele(os, FTAG); 2571 error = ENAMETOOLONG; 2572 break; 2573 } 2574 2575 dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 2576 error = dmu_snapshot_list_next(os, MAXPATHLEN - len, 2577 sname + len, &obj, &cookie, NULL); 2578 dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 2579 if (error != 0) { 2580 if (error == ENOENT) 2581 error = 0; 2582 break; 2583 } 2584 2585 if ((error = zvol_create_minor(sname)) != 0) { 2586 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n", 2587 sname, error); 2588 break; 2589 } 2590 } 2591 2592 kmem_free(sname, MAXPATHLEN); 2593 return (error); 2594} 2595 2596int 2597zvol_create_minors(const char *name) 2598{ 2599 uint64_t cookie; 2600 objset_t *os; 2601 char *osname, *p; 2602 int error, len; 2603 2604 if (dataset_name_hidden(name)) 2605 return (0); 2606 2607 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 2608 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n", 2609 name, error); 2610 return (error); 2611 } 2612 if (dmu_objset_type(os) == DMU_OST_ZVOL) { 2613 dsl_dataset_long_hold(os->os_dsl_dataset, FTAG); 2614 dsl_pool_rele(dmu_objset_pool(os), FTAG); 2615 error = zvol_create_minor(name); 2616 if (error == 0 || error == EEXIST) { 2617 error = zvol_create_snapshots(os, name); 2618 } else { 2619 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n", 2620 name, error); 2621 } 2622 dsl_dataset_long_rele(os->os_dsl_dataset, FTAG); 2623 dsl_dataset_rele(os->os_dsl_dataset, FTAG); 2624 return (error); 2625 } 2626 if (dmu_objset_type(os) != DMU_OST_ZFS) { 2627 dmu_objset_rele(os, FTAG); 2628 return (0); 2629 } 2630 2631 osname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2632 if (snprintf(osname, MAXPATHLEN, "%s/", name) >= MAXPATHLEN) { 2633 dmu_objset_rele(os, FTAG); 2634 kmem_free(osname, MAXPATHLEN); 2635 return (ENOENT); 2636 } 2637 p = osname + strlen(osname); 2638 len = MAXPATHLEN - (p - osname); 2639 2640#if 0 2641 /* Prefetch the datasets. */ 2642 cookie = 0; 2643 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) { 2644 if (!dataset_name_hidden(osname)) 2645 (void) dmu_objset_prefetch(osname, NULL); 2646 } 2647#endif 2648 2649 cookie = 0; 2650 while (dmu_dir_list_next(os, MAXPATHLEN - (p - osname), p, NULL, 2651 &cookie) == 0) { 2652 dmu_objset_rele(os, FTAG); 2653 (void)zvol_create_minors(osname); 2654 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 2655 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n", 2656 name, error); 2657 return (error); 2658 } 2659 } 2660 2661 dmu_objset_rele(os, FTAG); 2662 kmem_free(osname, MAXPATHLEN); 2663 return (0); 2664} 2665 2666static void 2667zvol_rename_minor(zvol_state_t *zv, const char *newname) 2668{ 2669 struct g_geom *gp; 2670 struct g_provider *pp; 2671 struct cdev *dev; 2672 2673 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2674 2675 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 2676 g_topology_lock(); 2677 pp = zv->zv_provider; 2678 ASSERT(pp != NULL); 2679 gp = pp->geom; 2680 ASSERT(gp != NULL); 2681 2682 zv->zv_provider = NULL; 2683 g_wither_provider(pp, ENXIO); 2684 2685 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, newname); 2686 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND; 2687 pp->sectorsize = DEV_BSIZE; 2688 pp->mediasize = zv->zv_volsize; 2689 pp->private = zv; 2690 zv->zv_provider = pp; 2691 g_error_provider(pp, 0); 2692 g_topology_unlock(); 2693 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) { 2694 dev = zv->zv_dev; 2695 ASSERT(dev != NULL); 2696 zv->zv_dev = NULL; 2697 destroy_dev(dev); 2698 2699 if (make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, 2700 &dev, &zvol_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 2701 0640, "%s/%s", ZVOL_DRIVER, newname) == 0) { 2702 zv->zv_dev = dev; 2703 dev->si_iosize_max = MAXPHYS; 2704 dev->si_drv2 = zv; 2705 } 2706 } 2707 strlcpy(zv->zv_name, newname, sizeof(zv->zv_name)); 2708} 2709 2710void 2711zvol_rename_minors(const char *oldname, const char *newname) 2712{ 2713 char name[MAXPATHLEN]; 2714 struct g_provider *pp; 2715 struct g_geom *gp; 2716 size_t oldnamelen, newnamelen; 2717 zvol_state_t *zv; 2718 char *namebuf; 2719 boolean_t locked = B_FALSE; 2720 2721 oldnamelen = strlen(oldname); 2722 newnamelen = strlen(newname); 2723 2724 DROP_GIANT(); 2725 /* See comment in zvol_open(). */ 2726 if (!MUTEX_HELD(&spa_namespace_lock)) { 2727 mutex_enter(&spa_namespace_lock); 2728 locked = B_TRUE; 2729 } 2730 2731 LIST_FOREACH(zv, &all_zvols, zv_links) { 2732 if (strcmp(zv->zv_name, oldname) == 0) { 2733 zvol_rename_minor(zv, newname); 2734 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 && 2735 (zv->zv_name[oldnamelen] == '/' || 2736 zv->zv_name[oldnamelen] == '@')) { 2737 snprintf(name, sizeof(name), "%s%c%s", newname, 2738 zv->zv_name[oldnamelen], 2739 zv->zv_name + oldnamelen + 1); 2740 zvol_rename_minor(zv, name); 2741 } 2742 } 2743 2744 if (locked) 2745 mutex_exit(&spa_namespace_lock); 2746 PICKUP_GIANT(); 2747} 2748 2749static int 2750zvol_d_open(struct cdev *dev, int flags, int fmt, struct thread *td) 2751{ 2752 zvol_state_t *zv; 2753 int err = 0; 2754 2755 mutex_enter(&spa_namespace_lock); 2756 zv = dev->si_drv2; 2757 if (zv == NULL) { 2758 mutex_exit(&spa_namespace_lock); 2759 return(ENXIO); /* zvol_create_minor() not done yet */ 2760 } 2761 2762 if (zv->zv_total_opens == 0) 2763 err = zvol_first_open(zv); 2764 if (err) { 2765 mutex_exit(&spa_namespace_lock); 2766 return (err); 2767 } 2768 if ((flags & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 2769 err = SET_ERROR(EROFS); 2770 goto out; 2771 } 2772 if (zv->zv_flags & ZVOL_EXCL) { 2773 err = SET_ERROR(EBUSY); 2774 goto out; 2775 } 2776#ifdef FEXCL 2777 if (flags & FEXCL) { 2778 if (zv->zv_total_opens != 0) { 2779 err = SET_ERROR(EBUSY); 2780 goto out; 2781 } 2782 zv->zv_flags |= ZVOL_EXCL; 2783 } 2784#endif 2785 2786 zv->zv_total_opens++; 2787 mutex_exit(&spa_namespace_lock); 2788 return (err); 2789out: 2790 if (zv->zv_total_opens == 0) 2791 zvol_last_close(zv); 2792 mutex_exit(&spa_namespace_lock); 2793 return (err); 2794} 2795 2796static int 2797zvol_d_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2798{ 2799 zvol_state_t *zv; 2800 int err = 0; 2801 2802 mutex_enter(&spa_namespace_lock); 2803 zv = dev->si_drv2; 2804 if (zv == NULL) { 2805 mutex_exit(&spa_namespace_lock); 2806 return(ENXIO); 2807 } 2808 2809 if (zv->zv_flags & ZVOL_EXCL) { 2810 ASSERT(zv->zv_total_opens == 1); 2811 zv->zv_flags &= ~ZVOL_EXCL; 2812 } 2813 2814 /* 2815 * If the open count is zero, this is a spurious close. 2816 * That indicates a bug in the kernel / DDI framework. 2817 */ 2818 ASSERT(zv->zv_total_opens != 0); 2819 2820 /* 2821 * You may get multiple opens, but only one close. 2822 */ 2823 zv->zv_total_opens--; 2824 2825 if (zv->zv_total_opens == 0) 2826 zvol_last_close(zv); 2827 2828 mutex_exit(&spa_namespace_lock); 2829 return (0); 2830} 2831 2832static int 2833zvol_d_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 2834{ 2835 zvol_state_t *zv; 2836 rl_t *rl; 2837 off_t offset, length, chunk; 2838 int i, error; 2839 u_int u; 2840 2841 zv = dev->si_drv2; 2842 2843 error = 0; 2844 KASSERT(zv->zv_total_opens > 0, 2845 ("Device with zero access count in zvol_d_ioctl")); 2846 2847 i = IOCPARM_LEN(cmd); 2848 switch (cmd) { 2849 case DIOCGSECTORSIZE: 2850 *(u_int *)data = DEV_BSIZE; 2851 break; 2852 case DIOCGMEDIASIZE: 2853 *(off_t *)data = zv->zv_volsize; 2854 break; 2855 case DIOCGFLUSH: 2856 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2857 break; 2858 case DIOCGDELETE: 2859 if (!zvol_unmap_enabled) 2860 break; 2861 2862 offset = ((off_t *)data)[0]; 2863 length = ((off_t *)data)[1]; 2864 if ((offset % DEV_BSIZE) != 0 || (length % DEV_BSIZE) != 0 || 2865 offset < 0 || offset >= zv->zv_volsize || 2866 length <= 0) { 2867 printf("%s: offset=%jd length=%jd\n", __func__, offset, 2868 length); 2869 error = EINVAL; 2870 break; 2871 } 2872 2873 rl = zfs_range_lock(&zv->zv_znode, offset, length, RL_WRITER); 2874 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 2875 error = dmu_tx_assign(tx, TXG_WAIT); 2876 if (error != 0) { 2877 dmu_tx_abort(tx); 2878 } else { 2879 zvol_log_truncate(zv, tx, offset, length, B_TRUE); 2880 dmu_tx_commit(tx); 2881 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 2882 offset, length); 2883 } 2884 zfs_range_unlock(rl); 2885 if (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS) 2886 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2887 break; 2888 case DIOCGSTRIPESIZE: 2889 *(off_t *)data = zv->zv_volblocksize; 2890 break; 2891 case DIOCGSTRIPEOFFSET: 2892 *(off_t *)data = 0; 2893 break; 2894 case DIOCGATTR: { 2895 spa_t *spa = dmu_objset_spa(zv->zv_objset); 2896 struct diocgattr_arg *arg = (struct diocgattr_arg *)data; 2897 uint64_t refd, avail, usedobjs, availobjs; 2898 2899 if (strcmp(arg->name, "blocksavail") == 0) { 2900 dmu_objset_space(zv->zv_objset, &refd, &avail, 2901 &usedobjs, &availobjs); 2902 arg->value.off = avail / DEV_BSIZE; 2903 } else if (strcmp(arg->name, "blocksused") == 0) { 2904 dmu_objset_space(zv->zv_objset, &refd, &avail, 2905 &usedobjs, &availobjs); 2906 arg->value.off = refd / DEV_BSIZE; 2907 } else if (strcmp(arg->name, "poolblocksavail") == 0) { 2908 avail = metaslab_class_get_space(spa_normal_class(spa)); 2909 avail -= metaslab_class_get_alloc(spa_normal_class(spa)); 2910 arg->value.off = avail / DEV_BSIZE; 2911 } else if (strcmp(arg->name, "poolblocksused") == 0) { 2912 refd = metaslab_class_get_alloc(spa_normal_class(spa)); 2913 arg->value.off = refd / DEV_BSIZE; 2914 } else 2915 error = ENOIOCTL; 2916 break; 2917 }
|