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 }
|