1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * 24 * Portions Copyright 2010 Robert Milkowski 25 * 26 * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 27 * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 28 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 29 */ 30 31 /* 32 * ZFS volume emulation driver. 33 * 34 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. 35 * Volumes are accessed through the symbolic links named: 36 * 37 * /dev/zvol/dsk/<pool_name>/<dataset_name> 38 * /dev/zvol/rdsk/<pool_name>/<dataset_name> 39 * 40 * These links are created by the /dev filesystem (sdev_zvolops.c). 41 * Volumes are persistent through reboot. No user command needs to be 42 * run before opening and using a device. 43 */ 44 45 #include <sys/types.h> 46 #include <sys/param.h> 47 #include <sys/errno.h> 48 #include <sys/uio.h> 49 #include <sys/buf.h> 50 #include <sys/modctl.h> 51 #include <sys/open.h> 52 #include <sys/kmem.h> 53 #include <sys/conf.h> 54 #include <sys/cmn_err.h> 55 #include <sys/stat.h> 56 #include <sys/zap.h> 57 #include <sys/spa.h> 58 #include <sys/spa_impl.h> 59 #include <sys/zio.h> 60 #include <sys/dmu_traverse.h> 61 #include <sys/dnode.h> 62 #include <sys/dsl_dataset.h> 63 #include <sys/dsl_prop.h> 64 #include <sys/dkio.h> 65 #include <sys/efi_partition.h> 66 #include <sys/byteorder.h> 67 #include <sys/pathname.h> 68 #include <sys/ddi.h> 69 #include <sys/sunddi.h> 70 #include <sys/crc32.h> 71 #include <sys/dirent.h> 72 #include <sys/policy.h> 73 #include <sys/fs/zfs.h> 74 #include <sys/zfs_ioctl.h> 75 #include <sys/mkdev.h> 76 #include <sys/zil.h> 77 #include <sys/refcount.h> 78 #include <sys/zfs_znode.h> 79 #include <sys/zfs_rlock.h> 80 #include <sys/vdev_disk.h> 81 #include <sys/vdev_impl.h> 82 #include <sys/vdev_raidz.h> 83 #include <sys/zvol.h> 84 #include <sys/dumphdr.h> 85 #include <sys/zil_impl.h> 86 #include <sys/dbuf.h> 87 #include <sys/dmu_tx.h> 88 #include <sys/zfeature.h> 89 #include <sys/zio_checksum.h> 90 91 #include "zfs_namecheck.h" 92 93 void *zfsdev_state; 94 static char *zvol_tag = "zvol_tag"; 95 96 #define ZVOL_DUMPSIZE "dumpsize" 97 98 /* 99 * This lock protects the zfsdev_state structure from being modified 100 * while it's being used, e.g. an open that comes in before a create 101 * finishes. It also protects temporary opens of the dataset so that, 102 * e.g., an open doesn't get a spurious EBUSY. 103 */ 104 kmutex_t zfsdev_state_lock; 105 static uint32_t zvol_minors; 106 107 typedef struct zvol_extent { 108 list_node_t ze_node; 109 dva_t ze_dva; /* dva associated with this extent */ 110 uint64_t ze_nblks; /* number of blocks in extent */ 111 } zvol_extent_t; 112 113 /* 114 * The in-core state of each volume. 115 */ 116 typedef struct zvol_state { 117 char zv_name[MAXPATHLEN]; /* pool/dd name */ 118 uint64_t zv_volsize; /* amount of space we advertise */ 119 uint64_t zv_volblocksize; /* volume block size */ 120 minor_t zv_minor; /* minor number */ 121 uint8_t zv_min_bs; /* minimum addressable block shift */ 122 uint8_t zv_flags; /* readonly, dumpified, etc. */ 123 objset_t *zv_objset; /* objset handle */ 124 uint32_t zv_open_count[OTYPCNT]; /* open counts */ 125 uint32_t zv_total_opens; /* total open count */ 126 zilog_t *zv_zilog; /* ZIL handle */ 127 list_t zv_extents; /* List of extents for dump */ 128 znode_t zv_znode; /* for range locking */ 129 dmu_buf_t *zv_dbuf; /* bonus handle */ 130 } zvol_state_t; 131 132 /* 133 * zvol specific flags 134 */ 135 #define ZVOL_RDONLY 0x1 136 #define ZVOL_DUMPIFIED 0x2 137 #define ZVOL_EXCL 0x4 138 #define ZVOL_WCE 0x8 139 140 /* 141 * zvol maximum transfer in one DMU tx. 142 */ 143 int zvol_maxphys = DMU_MAX_ACCESS/2; 144 145 /* 146 * Toggle unmap functionality. 147 */ 148 boolean_t zvol_unmap_enabled = B_TRUE; 149 150 extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 151 nvlist_t *, nvlist_t *); 152 static int zvol_remove_zv(zvol_state_t *); 153 static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 154 static int zvol_dumpify(zvol_state_t *zv); 155 static int zvol_dump_fini(zvol_state_t *zv); 156 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 157 158 static void 159 zvol_size_changed(zvol_state_t *zv, uint64_t volsize) 160 { 161 dev_t dev = makedevice(ddi_driver_major(zfs_dip), zv->zv_minor); 162 163 zv->zv_volsize = volsize; 164 VERIFY(ddi_prop_update_int64(dev, zfs_dip, 165 "Size", volsize) == DDI_SUCCESS); 166 VERIFY(ddi_prop_update_int64(dev, zfs_dip, 167 "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 168 169 /* Notify specfs to invalidate the cached size */ 170 spec_size_invalidate(dev, VBLK); 171 spec_size_invalidate(dev, VCHR); 172 } 173 174 int 175 zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 176 { 177 if (volsize == 0) 178 return (SET_ERROR(EINVAL)); 179 180 if (volsize % blocksize != 0) 181 return (SET_ERROR(EINVAL)); 182 183 #ifdef _ILP32 184 if (volsize - 1 > SPEC_MAXOFFSET_T) 185 return (SET_ERROR(EOVERFLOW)); 186 #endif 187 return (0); 188 } 189 190 int 191 zvol_check_volblocksize(uint64_t volblocksize) 192 { 193 if (volblocksize < SPA_MINBLOCKSIZE || 194 volblocksize > SPA_MAXBLOCKSIZE || 195 !ISP2(volblocksize)) 196 return (SET_ERROR(EDOM)); 197 198 return (0); 199 } 200 201 int 202 zvol_get_stats(objset_t *os, nvlist_t *nv) 203 { 204 int error; 205 dmu_object_info_t doi; 206 uint64_t val; 207 208 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 209 if (error) 210 return (error); 211 212 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 213 214 error = dmu_object_info(os, ZVOL_OBJ, &doi); 215 216 if (error == 0) { 217 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 218 doi.doi_data_block_size); 219 } 220 221 return (error); 222 } 223 224 static zvol_state_t * 225 zvol_minor_lookup(const char *name) 226 { 227 minor_t minor; 228 zvol_state_t *zv; 229 230 ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 231 232 for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) { 233 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 234 if (zv == NULL) 235 continue; 236 if (strcmp(zv->zv_name, name) == 0) 237 return (zv); 238 } 239 240 return (NULL); 241 } 242 243 /* extent mapping arg */ 244 struct maparg { 245 zvol_state_t *ma_zv; 246 uint64_t ma_blks; 247 }; 248 249 /*ARGSUSED*/ 250 static int 251 zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 252 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 253 { 254 struct maparg *ma = arg; 255 zvol_extent_t *ze; 256 int bs = ma->ma_zv->zv_volblocksize; 257 258 if (BP_IS_HOLE(bp) || 259 zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 260 return (0); 261 262 VERIFY(!BP_IS_EMBEDDED(bp)); 263 264 VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 265 ma->ma_blks++; 266 267 /* Abort immediately if we have encountered gang blocks */ 268 if (BP_IS_GANG(bp)) 269 return (SET_ERROR(EFRAGS)); 270 271 /* 272 * See if the block is at the end of the previous extent. 273 */ 274 ze = list_tail(&ma->ma_zv->zv_extents); 275 if (ze && 276 DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 277 DVA_GET_OFFSET(BP_IDENTITY(bp)) == 278 DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 279 ze->ze_nblks++; 280 return (0); 281 } 282 283 dprintf_bp(bp, "%s", "next blkptr:"); 284 285 /* start a new extent */ 286 ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 287 ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 288 ze->ze_nblks = 1; 289 list_insert_tail(&ma->ma_zv->zv_extents, ze); 290 return (0); 291 } 292 293 static void 294 zvol_free_extents(zvol_state_t *zv) 295 { 296 zvol_extent_t *ze; 297 298 while (ze = list_head(&zv->zv_extents)) { 299 list_remove(&zv->zv_extents, ze); 300 kmem_free(ze, sizeof (zvol_extent_t)); 301 } 302 } 303 304 static int 305 zvol_get_lbas(zvol_state_t *zv) 306 { 307 objset_t *os = zv->zv_objset; 308 struct maparg ma; 309 int err; 310 311 ma.ma_zv = zv; 312 ma.ma_blks = 0; 313 zvol_free_extents(zv); 314 315 /* commit any in-flight changes before traversing the dataset */ 316 txg_wait_synced(dmu_objset_pool(os), 0); 317 err = traverse_dataset(dmu_objset_ds(os), 0, 318 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 319 if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 320 zvol_free_extents(zv); 321 return (err ? err : EIO); 322 } 323 324 return (0); 325 } 326 327 /* ARGSUSED */ 328 void 329 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 330 { 331 zfs_creat_t *zct = arg; 332 nvlist_t *nvprops = zct->zct_props; 333 int error; 334 uint64_t volblocksize, volsize; 335 336 VERIFY(nvlist_lookup_uint64(nvprops, 337 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 338 if (nvlist_lookup_uint64(nvprops, 339 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 340 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 341 342 /* 343 * These properties must be removed from the list so the generic 344 * property setting step won't apply to them. 345 */ 346 VERIFY(nvlist_remove_all(nvprops, 347 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 348 (void) nvlist_remove_all(nvprops, 349 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 350 351 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 352 DMU_OT_NONE, 0, tx); 353 ASSERT(error == 0); 354 355 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 356 DMU_OT_NONE, 0, tx); 357 ASSERT(error == 0); 358 359 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 360 ASSERT(error == 0); 361 } 362 363 /* 364 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we 365 * implement DKIOCFREE/free-long-range. 366 */ 367 static int 368 zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap) 369 { 370 uint64_t offset, length; 371 372 if (byteswap) 373 byteswap_uint64_array(lr, sizeof (*lr)); 374 375 offset = lr->lr_offset; 376 length = lr->lr_length; 377 378 return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length)); 379 } 380 381 /* 382 * Replay a TX_WRITE ZIL transaction that didn't get committed 383 * after a system failure 384 */ 385 static int 386 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 387 { 388 objset_t *os = zv->zv_objset; 389 char *data = (char *)(lr + 1); /* data follows lr_write_t */ 390 uint64_t offset, length; 391 dmu_tx_t *tx; 392 int error; 393 394 if (byteswap) 395 byteswap_uint64_array(lr, sizeof (*lr)); 396 397 offset = lr->lr_offset; 398 length = lr->lr_length; 399 400 /* If it's a dmu_sync() block, write the whole block */ 401 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 402 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 403 if (length < blocksize) { 404 offset -= offset % blocksize; 405 length = blocksize; 406 } 407 } 408 409 tx = dmu_tx_create(os); 410 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 411 error = dmu_tx_assign(tx, TXG_WAIT); 412 if (error) { 413 dmu_tx_abort(tx); 414 } else { 415 dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 416 dmu_tx_commit(tx); 417 } 418 419 return (error); 420 } 421 422 /* ARGSUSED */ 423 static int 424 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 425 { 426 return (SET_ERROR(ENOTSUP)); 427 } 428 429 /* 430 * Callback vectors for replaying records. 431 * Only TX_WRITE and TX_TRUNCATE are needed for zvol. 432 */ 433 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 434 zvol_replay_err, /* 0 no such transaction type */ 435 zvol_replay_err, /* TX_CREATE */ 436 zvol_replay_err, /* TX_MKDIR */ 437 zvol_replay_err, /* TX_MKXATTR */ 438 zvol_replay_err, /* TX_SYMLINK */ 439 zvol_replay_err, /* TX_REMOVE */ 440 zvol_replay_err, /* TX_RMDIR */ 441 zvol_replay_err, /* TX_LINK */ 442 zvol_replay_err, /* TX_RENAME */ 443 zvol_replay_write, /* TX_WRITE */ 444 zvol_replay_truncate, /* TX_TRUNCATE */ 445 zvol_replay_err, /* TX_SETATTR */ 446 zvol_replay_err, /* TX_ACL */ 447 zvol_replay_err, /* TX_CREATE_ACL */ 448 zvol_replay_err, /* TX_CREATE_ATTR */ 449 zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 450 zvol_replay_err, /* TX_MKDIR_ACL */ 451 zvol_replay_err, /* TX_MKDIR_ATTR */ 452 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 453 zvol_replay_err, /* TX_WRITE2 */ 454 }; 455 456 int 457 zvol_name2minor(const char *name, minor_t *minor) 458 { 459 zvol_state_t *zv; 460 461 mutex_enter(&zfsdev_state_lock); 462 zv = zvol_minor_lookup(name); 463 if (minor && zv) 464 *minor = zv->zv_minor; 465 mutex_exit(&zfsdev_state_lock); 466 return (zv ? 0 : -1); 467 } 468 469 /* 470 * Create a minor node (plus a whole lot more) for the specified volume. 471 */ 472 int 473 zvol_create_minor(const char *name) 474 { 475 zfs_soft_state_t *zs; 476 zvol_state_t *zv; 477 objset_t *os; 478 dmu_object_info_t doi; 479 minor_t minor = 0; 480 char chrbuf[30], blkbuf[30]; 481 int error; 482 483 mutex_enter(&zfsdev_state_lock); 484 485 if (zvol_minor_lookup(name) != NULL) { 486 mutex_exit(&zfsdev_state_lock); 487 return (SET_ERROR(EEXIST)); 488 } 489 490 /* lie and say we're read-only */ 491 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os); 492 493 if (error) { 494 mutex_exit(&zfsdev_state_lock); 495 return (error); 496 } 497 498 if ((minor = zfsdev_minor_alloc()) == 0) { 499 dmu_objset_disown(os, FTAG); 500 mutex_exit(&zfsdev_state_lock); 501 return (SET_ERROR(ENXIO)); 502 } 503 504 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) { 505 dmu_objset_disown(os, FTAG); 506 mutex_exit(&zfsdev_state_lock); 507 return (SET_ERROR(EAGAIN)); 508 } 509 (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 510 (char *)name); 511 512 (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 513 514 if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 515 minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 516 ddi_soft_state_free(zfsdev_state, minor); 517 dmu_objset_disown(os, FTAG); 518 mutex_exit(&zfsdev_state_lock); 519 return (SET_ERROR(EAGAIN)); 520 } 521 522 (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 523 524 if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 525 minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 526 ddi_remove_minor_node(zfs_dip, chrbuf); 527 ddi_soft_state_free(zfsdev_state, minor); 528 dmu_objset_disown(os, FTAG); 529 mutex_exit(&zfsdev_state_lock); 530 return (SET_ERROR(EAGAIN)); 531 } 532 533 zs = ddi_get_soft_state(zfsdev_state, minor); 534 zs->zss_type = ZSST_ZVOL; 535 zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP); 536 (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 537 zv->zv_min_bs = DEV_BSHIFT; 538 zv->zv_minor = minor; 539 zv->zv_objset = os; 540 if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os))) 541 zv->zv_flags |= ZVOL_RDONLY; 542 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 543 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 544 sizeof (rl_t), offsetof(rl_t, r_node)); 545 list_create(&zv->zv_extents, sizeof (zvol_extent_t), 546 offsetof(zvol_extent_t, ze_node)); 547 /* get and cache the blocksize */ 548 error = dmu_object_info(os, ZVOL_OBJ, &doi); 549 ASSERT(error == 0); 550 zv->zv_volblocksize = doi.doi_data_block_size; 551 552 if (spa_writeable(dmu_objset_spa(os))) { 553 if (zil_replay_disable) 554 zil_destroy(dmu_objset_zil(os), B_FALSE); 555 else 556 zil_replay(os, zv, zvol_replay_vector); 557 } 558 dmu_objset_disown(os, FTAG); 559 zv->zv_objset = NULL; 560 561 zvol_minors++; 562 563 mutex_exit(&zfsdev_state_lock); 564 565 return (0); 566 } 567 568 /* 569 * Remove minor node for the specified volume. 570 */ 571 static int 572 zvol_remove_zv(zvol_state_t *zv) 573 { 574 char nmbuf[20]; 575 minor_t minor = zv->zv_minor; 576 577 ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 578 if (zv->zv_total_opens != 0) 579 return (SET_ERROR(EBUSY)); 580 581 (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor); 582 ddi_remove_minor_node(zfs_dip, nmbuf); 583 584 (void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor); 585 ddi_remove_minor_node(zfs_dip, nmbuf); 586 587 avl_destroy(&zv->zv_znode.z_range_avl); 588 mutex_destroy(&zv->zv_znode.z_range_lock); 589 590 kmem_free(zv, sizeof (zvol_state_t)); 591 592 ddi_soft_state_free(zfsdev_state, minor); 593 594 zvol_minors--; 595 return (0); 596 } 597 598 int 599 zvol_remove_minor(const char *name) 600 { 601 zvol_state_t *zv; 602 int rc; 603 604 mutex_enter(&zfsdev_state_lock); 605 if ((zv = zvol_minor_lookup(name)) == NULL) { 606 mutex_exit(&zfsdev_state_lock); 607 return (SET_ERROR(ENXIO)); 608 } 609 rc = zvol_remove_zv(zv); 610 mutex_exit(&zfsdev_state_lock); 611 return (rc); 612 } 613 614 int 615 zvol_first_open(zvol_state_t *zv) 616 { 617 objset_t *os; 618 uint64_t volsize; 619 int error; 620 uint64_t readonly; 621 622 /* lie and say we're read-only */ 623 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 624 zvol_tag, &os); 625 if (error) 626 return (error); 627 628 zv->zv_objset = os; 629 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 630 if (error) { 631 ASSERT(error == 0); 632 dmu_objset_disown(os, zvol_tag); 633 return (error); 634 } 635 636 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); 637 if (error) { 638 dmu_objset_disown(os, zvol_tag); 639 return (error); 640 } 641 642 zvol_size_changed(zv, volsize); 643 zv->zv_zilog = zil_open(os, zvol_get_data); 644 645 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 646 NULL) == 0); 647 if (readonly || dmu_objset_is_snapshot(os) || 648 !spa_writeable(dmu_objset_spa(os))) 649 zv->zv_flags |= ZVOL_RDONLY; 650 else 651 zv->zv_flags &= ~ZVOL_RDONLY; 652 return (error); 653 } 654 655 void 656 zvol_last_close(zvol_state_t *zv) 657 { 658 zil_close(zv->zv_zilog); 659 zv->zv_zilog = NULL; 660 661 dmu_buf_rele(zv->zv_dbuf, zvol_tag); 662 zv->zv_dbuf = NULL; 663 664 /* 665 * Evict cached data 666 */ 667 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) && 668 !(zv->zv_flags & ZVOL_RDONLY)) 669 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 670 dmu_objset_evict_dbufs(zv->zv_objset); 671 672 dmu_objset_disown(zv->zv_objset, zvol_tag); 673 zv->zv_objset = NULL; 674 } 675 676 int 677 zvol_prealloc(zvol_state_t *zv) 678 { 679 objset_t *os = zv->zv_objset; 680 dmu_tx_t *tx; 681 uint64_t refd, avail, usedobjs, availobjs; 682 uint64_t resid = zv->zv_volsize; 683 uint64_t off = 0; 684 685 /* Check the space usage before attempting to allocate the space */ 686 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 687 if (avail < zv->zv_volsize) 688 return (SET_ERROR(ENOSPC)); 689 690 /* Free old extents if they exist */ 691 zvol_free_extents(zv); 692 693 while (resid != 0) { 694 int error; 695 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 696 697 tx = dmu_tx_create(os); 698 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 699 error = dmu_tx_assign(tx, TXG_WAIT); 700 if (error) { 701 dmu_tx_abort(tx); 702 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 703 return (error); 704 } 705 dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 706 dmu_tx_commit(tx); 707 off += bytes; 708 resid -= bytes; 709 } 710 txg_wait_synced(dmu_objset_pool(os), 0); 711 712 return (0); 713 } 714 715 static int 716 zvol_update_volsize(objset_t *os, uint64_t volsize) 717 { 718 dmu_tx_t *tx; 719 int error; 720 721 ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 722 723 tx = dmu_tx_create(os); 724 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 725 dmu_tx_mark_netfree(tx); 726 error = dmu_tx_assign(tx, TXG_WAIT); 727 if (error) { 728 dmu_tx_abort(tx); 729 return (error); 730 } 731 732 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 733 &volsize, tx); 734 dmu_tx_commit(tx); 735 736 if (error == 0) 737 error = dmu_free_long_range(os, 738 ZVOL_OBJ, volsize, DMU_OBJECT_END); 739 return (error); 740 } 741 742 void 743 zvol_remove_minors(const char *name) 744 { 745 zvol_state_t *zv; 746 char *namebuf; 747 minor_t minor; 748 749 namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP); 750 (void) strncpy(namebuf, name, strlen(name)); 751 (void) strcat(namebuf, "/"); 752 mutex_enter(&zfsdev_state_lock); 753 for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) { 754 755 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 756 if (zv == NULL) 757 continue; 758 if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0) 759 (void) zvol_remove_zv(zv); 760 } 761 kmem_free(namebuf, strlen(name) + 2); 762 763 mutex_exit(&zfsdev_state_lock); 764 } 765 766 static int 767 zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize) 768 { 769 uint64_t old_volsize = 0ULL; 770 int error = 0; 771 772 ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 773 774 /* 775 * Reinitialize the dump area to the new size. If we 776 * failed to resize the dump area then restore it back to 777 * its original size. We must set the new volsize prior 778 * to calling dumpvp_resize() to ensure that the devices' 779 * size(9P) is not visible by the dump subsystem. 780 */ 781 old_volsize = zv->zv_volsize; 782 zvol_size_changed(zv, volsize); 783 784 if (zv->zv_flags & ZVOL_DUMPIFIED) { 785 if ((error = zvol_dumpify(zv)) != 0 || 786 (error = dumpvp_resize()) != 0) { 787 int dumpify_error; 788 789 (void) zvol_update_volsize(zv->zv_objset, old_volsize); 790 zvol_size_changed(zv, old_volsize); 791 dumpify_error = zvol_dumpify(zv); 792 error = dumpify_error ? dumpify_error : error; 793 } 794 } 795 796 /* 797 * Generate a LUN expansion event. 798 */ 799 if (error == 0) { 800 sysevent_id_t eid; 801 nvlist_t *attr; 802 char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 803 804 (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 805 zv->zv_minor); 806 807 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 808 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 809 810 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 811 ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 812 813 nvlist_free(attr); 814 kmem_free(physpath, MAXPATHLEN); 815 } 816 return (error); 817 } 818 819 int 820 zvol_set_volsize(const char *name, uint64_t volsize) 821 { 822 zvol_state_t *zv = NULL; 823 objset_t *os; 824 int error; 825 dmu_object_info_t doi; 826 uint64_t readonly; 827 boolean_t owned = B_FALSE; 828 829 error = dsl_prop_get_integer(name, 830 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL); 831 if (error != 0) 832 return (error); 833 if (readonly) 834 return (SET_ERROR(EROFS)); 835 836 mutex_enter(&zfsdev_state_lock); 837 zv = zvol_minor_lookup(name); 838 839 if (zv == NULL || zv->zv_objset == NULL) { 840 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE, 841 FTAG, &os)) != 0) { 842 mutex_exit(&zfsdev_state_lock); 843 return (error); 844 } 845 owned = B_TRUE; 846 if (zv != NULL) 847 zv->zv_objset = os; 848 } else { 849 os = zv->zv_objset; 850 } 851 852 if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 853 (error = zvol_check_volsize(volsize, doi.doi_data_block_size)) != 0) 854 goto out; 855 856 error = zvol_update_volsize(os, volsize); 857 858 if (error == 0 && zv != NULL) 859 error = zvol_update_live_volsize(zv, volsize); 860 out: 861 if (owned) { 862 dmu_objset_disown(os, FTAG); 863 if (zv != NULL) 864 zv->zv_objset = NULL; 865 } 866 mutex_exit(&zfsdev_state_lock); 867 return (error); 868 } 869 870 /*ARGSUSED*/ 871 int 872 zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 873 { 874 zvol_state_t *zv; 875 int err = 0; 876 877 mutex_enter(&zfsdev_state_lock); 878 879 zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL); 880 if (zv == NULL) { 881 mutex_exit(&zfsdev_state_lock); 882 return (SET_ERROR(ENXIO)); 883 } 884 885 if (zv->zv_total_opens == 0) 886 err = zvol_first_open(zv); 887 if (err) { 888 mutex_exit(&zfsdev_state_lock); 889 return (err); 890 } 891 if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 892 err = SET_ERROR(EROFS); 893 goto out; 894 } 895 if (zv->zv_flags & ZVOL_EXCL) { 896 err = SET_ERROR(EBUSY); 897 goto out; 898 } 899 if (flag & FEXCL) { 900 if (zv->zv_total_opens != 0) { 901 err = SET_ERROR(EBUSY); 902 goto out; 903 } 904 zv->zv_flags |= ZVOL_EXCL; 905 } 906 907 if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 908 zv->zv_open_count[otyp]++; 909 zv->zv_total_opens++; 910 } 911 mutex_exit(&zfsdev_state_lock); 912 913 return (err); 914 out: 915 if (zv->zv_total_opens == 0) 916 zvol_last_close(zv); 917 mutex_exit(&zfsdev_state_lock); 918 return (err); 919 } 920 921 /*ARGSUSED*/ 922 int 923 zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 924 { 925 minor_t minor = getminor(dev); 926 zvol_state_t *zv; 927 int error = 0; 928 929 mutex_enter(&zfsdev_state_lock); 930 931 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 932 if (zv == NULL) { 933 mutex_exit(&zfsdev_state_lock); 934 return (SET_ERROR(ENXIO)); 935 } 936 937 if (zv->zv_flags & ZVOL_EXCL) { 938 ASSERT(zv->zv_total_opens == 1); 939 zv->zv_flags &= ~ZVOL_EXCL; 940 } 941 942 /* 943 * If the open count is zero, this is a spurious close. 944 * That indicates a bug in the kernel / DDI framework. 945 */ 946 ASSERT(zv->zv_open_count[otyp] != 0); 947 ASSERT(zv->zv_total_opens != 0); 948 949 /* 950 * You may get multiple opens, but only one close. 951 */ 952 zv->zv_open_count[otyp]--; 953 zv->zv_total_opens--; 954 955 if (zv->zv_total_opens == 0) 956 zvol_last_close(zv); 957 958 mutex_exit(&zfsdev_state_lock); 959 return (error); 960 } 961 962 static void 963 zvol_get_done(zgd_t *zgd, int error) 964 { 965 if (zgd->zgd_db) 966 dmu_buf_rele(zgd->zgd_db, zgd); 967 968 zfs_range_unlock(zgd->zgd_rl); 969 970 if (error == 0 && zgd->zgd_bp) 971 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 972 973 kmem_free(zgd, sizeof (zgd_t)); 974 } 975 976 /* 977 * Get data to generate a TX_WRITE intent log record. 978 */ 979 static int 980 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 981 { 982 zvol_state_t *zv = arg; 983 objset_t *os = zv->zv_objset; 984 uint64_t object = ZVOL_OBJ; 985 uint64_t offset = lr->lr_offset; 986 uint64_t size = lr->lr_length; /* length of user data */ 987 blkptr_t *bp = &lr->lr_blkptr; 988 dmu_buf_t *db; 989 zgd_t *zgd; 990 int error; 991 992 ASSERT(zio != NULL); 993 ASSERT(size != 0); 994 995 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 996 zgd->zgd_zilog = zv->zv_zilog; 997 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); 998 999 /* 1000 * Write records come in two flavors: immediate and indirect. 1001 * For small writes it's cheaper to store the data with the 1002 * log record (immediate); for large writes it's cheaper to 1003 * sync the data and get a pointer to it (indirect) so that 1004 * we don't have to write the data twice. 1005 */ 1006 if (buf != NULL) { /* immediate write */ 1007 error = dmu_read(os, object, offset, size, buf, 1008 DMU_READ_NO_PREFETCH); 1009 } else { 1010 size = zv->zv_volblocksize; 1011 offset = P2ALIGN(offset, size); 1012 error = dmu_buf_hold(os, object, offset, zgd, &db, 1013 DMU_READ_NO_PREFETCH); 1014 if (error == 0) { 1015 blkptr_t *obp = dmu_buf_get_blkptr(db); 1016 if (obp) { 1017 ASSERT(BP_IS_HOLE(bp)); 1018 *bp = *obp; 1019 } 1020 1021 zgd->zgd_db = db; 1022 zgd->zgd_bp = bp; 1023 1024 ASSERT(db->db_offset == offset); 1025 ASSERT(db->db_size == size); 1026 1027 error = dmu_sync(zio, lr->lr_common.lrc_txg, 1028 zvol_get_done, zgd); 1029 1030 if (error == 0) 1031 return (0); 1032 } 1033 } 1034 1035 zvol_get_done(zgd, error); 1036 1037 return (error); 1038 } 1039 1040 /* 1041 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 1042 * 1043 * We store data in the log buffers if it's small enough. 1044 * Otherwise we will later flush the data out via dmu_sync(). 1045 */ 1046 ssize_t zvol_immediate_write_sz = 32768; 1047 1048 static void 1049 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 1050 boolean_t sync) 1051 { 1052 uint32_t blocksize = zv->zv_volblocksize; 1053 zilog_t *zilog = zv->zv_zilog; 1054 boolean_t slogging; 1055 ssize_t immediate_write_sz; 1056 1057 if (zil_replaying(zilog, tx)) 1058 return; 1059 1060 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 1061 ? 0 : zvol_immediate_write_sz; 1062 1063 slogging = spa_has_slogs(zilog->zl_spa) && 1064 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 1065 1066 while (resid) { 1067 itx_t *itx; 1068 lr_write_t *lr; 1069 ssize_t len; 1070 itx_wr_state_t write_state; 1071 1072 /* 1073 * Unlike zfs_log_write() we can be called with 1074 * upto DMU_MAX_ACCESS/2 (5MB) writes. 1075 */ 1076 if (blocksize > immediate_write_sz && !slogging && 1077 resid >= blocksize && off % blocksize == 0) { 1078 write_state = WR_INDIRECT; /* uses dmu_sync */ 1079 len = blocksize; 1080 } else if (sync) { 1081 write_state = WR_COPIED; 1082 len = MIN(ZIL_MAX_LOG_DATA, resid); 1083 } else { 1084 write_state = WR_NEED_COPY; 1085 len = MIN(ZIL_MAX_LOG_DATA, resid); 1086 } 1087 1088 itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1089 (write_state == WR_COPIED ? len : 0)); 1090 lr = (lr_write_t *)&itx->itx_lr; 1091 if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 1092 ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1093 zil_itx_destroy(itx); 1094 itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1095 lr = (lr_write_t *)&itx->itx_lr; 1096 write_state = WR_NEED_COPY; 1097 } 1098 1099 itx->itx_wr_state = write_state; 1100 if (write_state == WR_NEED_COPY) 1101 itx->itx_sod += len; 1102 lr->lr_foid = ZVOL_OBJ; 1103 lr->lr_offset = off; 1104 lr->lr_length = len; 1105 lr->lr_blkoff = 0; 1106 BP_ZERO(&lr->lr_blkptr); 1107 1108 itx->itx_private = zv; 1109 itx->itx_sync = sync; 1110 1111 zil_itx_assign(zilog, itx, tx); 1112 1113 off += len; 1114 resid -= len; 1115 } 1116 } 1117 1118 static int 1119 zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset, 1120 uint64_t size, boolean_t doread, boolean_t isdump) 1121 { 1122 vdev_disk_t *dvd; 1123 int c; 1124 int numerrors = 0; 1125 1126 if (vd->vdev_ops == &vdev_mirror_ops || 1127 vd->vdev_ops == &vdev_replacing_ops || 1128 vd->vdev_ops == &vdev_spare_ops) { 1129 for (c = 0; c < vd->vdev_children; c++) { 1130 int err = zvol_dumpio_vdev(vd->vdev_child[c], 1131 addr, offset, origoffset, size, doread, isdump); 1132 if (err != 0) { 1133 numerrors++; 1134 } else if (doread) { 1135 break; 1136 } 1137 } 1138 } 1139 1140 if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops) 1141 return (numerrors < vd->vdev_children ? 0 : EIO); 1142 1143 if (doread && !vdev_readable(vd)) 1144 return (SET_ERROR(EIO)); 1145 else if (!doread && !vdev_writeable(vd)) 1146 return (SET_ERROR(EIO)); 1147 1148 if (vd->vdev_ops == &vdev_raidz_ops) { 1149 return (vdev_raidz_physio(vd, 1150 addr, size, offset, origoffset, doread, isdump)); 1151 } 1152 1153 offset += VDEV_LABEL_START_SIZE; 1154 1155 if (ddi_in_panic() || isdump) { 1156 ASSERT(!doread); 1157 if (doread) 1158 return (SET_ERROR(EIO)); 1159 dvd = vd->vdev_tsd; 1160 ASSERT3P(dvd, !=, NULL); 1161 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1162 lbtodb(size))); 1163 } else { 1164 dvd = vd->vdev_tsd; 1165 ASSERT3P(dvd, !=, NULL); 1166 return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size, 1167 offset, doread ? B_READ : B_WRITE)); 1168 } 1169 } 1170 1171 static int 1172 zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 1173 boolean_t doread, boolean_t isdump) 1174 { 1175 vdev_t *vd; 1176 int error; 1177 zvol_extent_t *ze; 1178 spa_t *spa = dmu_objset_spa(zv->zv_objset); 1179 1180 /* Must be sector aligned, and not stradle a block boundary. */ 1181 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 1182 P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 1183 return (SET_ERROR(EINVAL)); 1184 } 1185 ASSERT(size <= zv->zv_volblocksize); 1186 1187 /* Locate the extent this belongs to */ 1188 ze = list_head(&zv->zv_extents); 1189 while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 1190 offset -= ze->ze_nblks * zv->zv_volblocksize; 1191 ze = list_next(&zv->zv_extents, ze); 1192 } 1193 1194 if (ze == NULL) 1195 return (SET_ERROR(EINVAL)); 1196 1197 if (!ddi_in_panic()) 1198 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 1199 1200 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 1201 offset += DVA_GET_OFFSET(&ze->ze_dva); 1202 error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva), 1203 size, doread, isdump); 1204 1205 if (!ddi_in_panic()) 1206 spa_config_exit(spa, SCL_STATE, FTAG); 1207 1208 return (error); 1209 } 1210 1211 int 1212 zvol_strategy(buf_t *bp) 1213 { 1214 zfs_soft_state_t *zs = NULL; 1215 zvol_state_t *zv; 1216 uint64_t off, volsize; 1217 size_t resid; 1218 char *addr; 1219 objset_t *os; 1220 rl_t *rl; 1221 int error = 0; 1222 boolean_t doread = bp->b_flags & B_READ; 1223 boolean_t is_dumpified; 1224 boolean_t sync; 1225 1226 if (getminor(bp->b_edev) == 0) { 1227 error = SET_ERROR(EINVAL); 1228 } else { 1229 zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev)); 1230 if (zs == NULL) 1231 error = SET_ERROR(ENXIO); 1232 else if (zs->zss_type != ZSST_ZVOL) 1233 error = SET_ERROR(EINVAL); 1234 } 1235 1236 if (error) { 1237 bioerror(bp, error); 1238 biodone(bp); 1239 return (0); 1240 } 1241 1242 zv = zs->zss_data; 1243 1244 if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) { 1245 bioerror(bp, EROFS); 1246 biodone(bp); 1247 return (0); 1248 } 1249 1250 off = ldbtob(bp->b_blkno); 1251 volsize = zv->zv_volsize; 1252 1253 os = zv->zv_objset; 1254 ASSERT(os != NULL); 1255 1256 bp_mapin(bp); 1257 addr = bp->b_un.b_addr; 1258 resid = bp->b_bcount; 1259 1260 if (resid > 0 && (off < 0 || off >= volsize)) { 1261 bioerror(bp, EIO); 1262 biodone(bp); 1263 return (0); 1264 } 1265 1266 is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED; 1267 sync = ((!(bp->b_flags & B_ASYNC) && 1268 !(zv->zv_flags & ZVOL_WCE)) || 1269 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) && 1270 !doread && !is_dumpified; 1271 1272 /* 1273 * There must be no buffer changes when doing a dmu_sync() because 1274 * we can't change the data whilst calculating the checksum. 1275 */ 1276 rl = zfs_range_lock(&zv->zv_znode, off, resid, 1277 doread ? RL_READER : RL_WRITER); 1278 1279 while (resid != 0 && off < volsize) { 1280 size_t size = MIN(resid, zvol_maxphys); 1281 if (is_dumpified) { 1282 size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 1283 error = zvol_dumpio(zv, addr, off, size, 1284 doread, B_FALSE); 1285 } else if (doread) { 1286 error = dmu_read(os, ZVOL_OBJ, off, size, addr, 1287 DMU_READ_PREFETCH); 1288 } else { 1289 dmu_tx_t *tx = dmu_tx_create(os); 1290 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1291 error = dmu_tx_assign(tx, TXG_WAIT); 1292 if (error) { 1293 dmu_tx_abort(tx); 1294 } else { 1295 dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1296 zvol_log_write(zv, tx, off, size, sync); 1297 dmu_tx_commit(tx); 1298 } 1299 } 1300 if (error) { 1301 /* convert checksum errors into IO errors */ 1302 if (error == ECKSUM) 1303 error = SET_ERROR(EIO); 1304 break; 1305 } 1306 off += size; 1307 addr += size; 1308 resid -= size; 1309 } 1310 zfs_range_unlock(rl); 1311 1312 if ((bp->b_resid = resid) == bp->b_bcount) 1313 bioerror(bp, off > volsize ? EINVAL : error); 1314 1315 if (sync) 1316 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1317 biodone(bp); 1318 1319 return (0); 1320 } 1321 1322 /* 1323 * Set the buffer count to the zvol maximum transfer. 1324 * Using our own routine instead of the default minphys() 1325 * means that for larger writes we write bigger buffers on X86 1326 * (128K instead of 56K) and flush the disk write cache less often 1327 * (every zvol_maxphys - currently 1MB) instead of minphys (currently 1328 * 56K on X86 and 128K on sparc). 1329 */ 1330 void 1331 zvol_minphys(struct buf *bp) 1332 { 1333 if (bp->b_bcount > zvol_maxphys) 1334 bp->b_bcount = zvol_maxphys; 1335 } 1336 1337 int 1338 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1339 { 1340 minor_t minor = getminor(dev); 1341 zvol_state_t *zv; 1342 int error = 0; 1343 uint64_t size; 1344 uint64_t boff; 1345 uint64_t resid; 1346 1347 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1348 if (zv == NULL) 1349 return (SET_ERROR(ENXIO)); 1350 1351 if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0) 1352 return (SET_ERROR(EINVAL)); 1353 1354 boff = ldbtob(blkno); 1355 resid = ldbtob(nblocks); 1356 1357 VERIFY3U(boff + resid, <=, zv->zv_volsize); 1358 1359 while (resid) { 1360 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 1361 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1362 if (error) 1363 break; 1364 boff += size; 1365 addr += size; 1366 resid -= size; 1367 } 1368 1369 return (error); 1370 } 1371 1372 /*ARGSUSED*/ 1373 int 1374 zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1375 { 1376 minor_t minor = getminor(dev); 1377 zvol_state_t *zv; 1378 uint64_t volsize; 1379 rl_t *rl; 1380 int error = 0; 1381 1382 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1383 if (zv == NULL) 1384 return (SET_ERROR(ENXIO)); 1385 1386 volsize = zv->zv_volsize; 1387 if (uio->uio_resid > 0 && 1388 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 1389 return (SET_ERROR(EIO)); 1390 1391 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1392 error = physio(zvol_strategy, NULL, dev, B_READ, 1393 zvol_minphys, uio); 1394 return (error); 1395 } 1396 1397 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1398 RL_READER); 1399 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1400 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1401 1402 /* don't read past the end */ 1403 if (bytes > volsize - uio->uio_loffset) 1404 bytes = volsize - uio->uio_loffset; 1405 1406 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1407 if (error) { 1408 /* convert checksum errors into IO errors */ 1409 if (error == ECKSUM) 1410 error = SET_ERROR(EIO); 1411 break; 1412 } 1413 } 1414 zfs_range_unlock(rl); 1415 return (error); 1416 } 1417 1418 /*ARGSUSED*/ 1419 int 1420 zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1421 { 1422 minor_t minor = getminor(dev); 1423 zvol_state_t *zv; 1424 uint64_t volsize; 1425 rl_t *rl; 1426 int error = 0; 1427 boolean_t sync; 1428 1429 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1430 if (zv == NULL) 1431 return (SET_ERROR(ENXIO)); 1432 1433 volsize = zv->zv_volsize; 1434 if (uio->uio_resid > 0 && 1435 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 1436 return (SET_ERROR(EIO)); 1437 1438 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1439 error = physio(zvol_strategy, NULL, dev, B_WRITE, 1440 zvol_minphys, uio); 1441 return (error); 1442 } 1443 1444 sync = !(zv->zv_flags & ZVOL_WCE) || 1445 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS); 1446 1447 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1448 RL_WRITER); 1449 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1450 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1451 uint64_t off = uio->uio_loffset; 1452 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 1453 1454 if (bytes > volsize - off) /* don't write past the end */ 1455 bytes = volsize - off; 1456 1457 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1458 error = dmu_tx_assign(tx, TXG_WAIT); 1459 if (error) { 1460 dmu_tx_abort(tx); 1461 break; 1462 } 1463 error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx); 1464 if (error == 0) 1465 zvol_log_write(zv, tx, off, bytes, sync); 1466 dmu_tx_commit(tx); 1467 1468 if (error) 1469 break; 1470 } 1471 zfs_range_unlock(rl); 1472 if (sync) 1473 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1474 return (error); 1475 } 1476 1477 int 1478 zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1479 { 1480 struct uuid uuid = EFI_RESERVED; 1481 efi_gpe_t gpe = { 0 }; 1482 uint32_t crc; 1483 dk_efi_t efi; 1484 int length; 1485 char *ptr; 1486 1487 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1488 return (SET_ERROR(EFAULT)); 1489 ptr = (char *)(uintptr_t)efi.dki_data_64; 1490 length = efi.dki_length; 1491 /* 1492 * Some clients may attempt to request a PMBR for the 1493 * zvol. Currently this interface will return EINVAL to 1494 * such requests. These requests could be supported by 1495 * adding a check for lba == 0 and consing up an appropriate 1496 * PMBR. 1497 */ 1498 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1499 return (SET_ERROR(EINVAL)); 1500 1501 gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1502 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1503 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1504 1505 if (efi.dki_lba == 1) { 1506 efi_gpt_t gpt = { 0 }; 1507 1508 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1509 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1510 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1511 gpt.efi_gpt_MyLBA = LE_64(1ULL); 1512 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1513 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1514 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1515 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1516 gpt.efi_gpt_SizeOfPartitionEntry = 1517 LE_32(sizeof (efi_gpe_t)); 1518 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1519 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1520 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1521 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1522 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1523 flag)) 1524 return (SET_ERROR(EFAULT)); 1525 ptr += sizeof (gpt); 1526 length -= sizeof (gpt); 1527 } 1528 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1529 length), flag)) 1530 return (SET_ERROR(EFAULT)); 1531 return (0); 1532 } 1533 1534 /* 1535 * BEGIN entry points to allow external callers access to the volume. 1536 */ 1537 /* 1538 * Return the volume parameters needed for access from an external caller. 1539 * These values are invariant as long as the volume is held open. 1540 */ 1541 int 1542 zvol_get_volume_params(minor_t minor, uint64_t *blksize, 1543 uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl, 1544 void **rl_hdl, void **bonus_hdl) 1545 { 1546 zvol_state_t *zv; 1547 1548 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1549 if (zv == NULL) 1550 return (SET_ERROR(ENXIO)); 1551 if (zv->zv_flags & ZVOL_DUMPIFIED) 1552 return (SET_ERROR(ENXIO)); 1553 1554 ASSERT(blksize && max_xfer_len && minor_hdl && 1555 objset_hdl && zil_hdl && rl_hdl && bonus_hdl); 1556 1557 *blksize = zv->zv_volblocksize; 1558 *max_xfer_len = (uint64_t)zvol_maxphys; 1559 *minor_hdl = zv; 1560 *objset_hdl = zv->zv_objset; 1561 *zil_hdl = zv->zv_zilog; 1562 *rl_hdl = &zv->zv_znode; 1563 *bonus_hdl = zv->zv_dbuf; 1564 return (0); 1565 } 1566 1567 /* 1568 * Return the current volume size to an external caller. 1569 * The size can change while the volume is open. 1570 */ 1571 uint64_t 1572 zvol_get_volume_size(void *minor_hdl) 1573 { 1574 zvol_state_t *zv = minor_hdl; 1575 1576 return (zv->zv_volsize); 1577 } 1578 1579 /* 1580 * Return the current WCE setting to an external caller. 1581 * The WCE setting can change while the volume is open. 1582 */ 1583 int 1584 zvol_get_volume_wce(void *minor_hdl) 1585 { 1586 zvol_state_t *zv = minor_hdl; 1587 1588 return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0); 1589 } 1590 1591 /* 1592 * Entry point for external callers to zvol_log_write 1593 */ 1594 void 1595 zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid, 1596 boolean_t sync) 1597 { 1598 zvol_state_t *zv = minor_hdl; 1599 1600 zvol_log_write(zv, tx, off, resid, sync); 1601 } 1602 /* 1603 * END entry points to allow external callers access to the volume. 1604 */ 1605 1606 /* 1607 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE. 1608 */ 1609 static void 1610 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len, 1611 boolean_t sync) 1612 { 1613 itx_t *itx; 1614 lr_truncate_t *lr; 1615 zilog_t *zilog = zv->zv_zilog; 1616 1617 if (zil_replaying(zilog, tx)) 1618 return; 1619 1620 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); 1621 lr = (lr_truncate_t *)&itx->itx_lr; 1622 lr->lr_foid = ZVOL_OBJ; 1623 lr->lr_offset = off; 1624 lr->lr_length = len; 1625 1626 itx->itx_sync = sync; 1627 zil_itx_assign(zilog, itx, tx); 1628 } 1629 1630 /* 1631 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1632 * Also a dirtbag dkio ioctl for unmap/free-block functionality. 1633 */ 1634 /*ARGSUSED*/ 1635 int 1636 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1637 { 1638 zvol_state_t *zv; 1639 struct dk_callback *dkc; 1640 int error = 0; 1641 rl_t *rl; 1642 1643 mutex_enter(&zfsdev_state_lock); 1644 1645 zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL); 1646 1647 if (zv == NULL) { 1648 mutex_exit(&zfsdev_state_lock); 1649 return (SET_ERROR(ENXIO)); 1650 } 1651 ASSERT(zv->zv_total_opens > 0); 1652 1653 switch (cmd) { 1654 1655 case DKIOCINFO: 1656 { 1657 struct dk_cinfo dki; 1658 1659 bzero(&dki, sizeof (dki)); 1660 (void) strcpy(dki.dki_cname, "zvol"); 1661 (void) strcpy(dki.dki_dname, "zvol"); 1662 dki.dki_ctype = DKC_UNKNOWN; 1663 dki.dki_unit = getminor(dev); 1664 dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1665 mutex_exit(&zfsdev_state_lock); 1666 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1667 error = SET_ERROR(EFAULT); 1668 return (error); 1669 } 1670 1671 case DKIOCGMEDIAINFO: 1672 { 1673 struct dk_minfo dkm; 1674 1675 bzero(&dkm, sizeof (dkm)); 1676 dkm.dki_lbsize = 1U << zv->zv_min_bs; 1677 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1678 dkm.dki_media_type = DK_UNKNOWN; 1679 mutex_exit(&zfsdev_state_lock); 1680 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1681 error = SET_ERROR(EFAULT); 1682 return (error); 1683 } 1684 1685 case DKIOCGMEDIAINFOEXT: 1686 { 1687 struct dk_minfo_ext dkmext; 1688 1689 bzero(&dkmext, sizeof (dkmext)); 1690 dkmext.dki_lbsize = 1U << zv->zv_min_bs; 1691 dkmext.dki_pbsize = zv->zv_volblocksize; 1692 dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1693 dkmext.dki_media_type = DK_UNKNOWN; 1694 mutex_exit(&zfsdev_state_lock); 1695 if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag)) 1696 error = SET_ERROR(EFAULT); 1697 return (error); 1698 } 1699 1700 case DKIOCGETEFI: 1701 { 1702 uint64_t vs = zv->zv_volsize; 1703 uint8_t bs = zv->zv_min_bs; 1704 1705 mutex_exit(&zfsdev_state_lock); 1706 error = zvol_getefi((void *)arg, flag, vs, bs); 1707 return (error); 1708 } 1709 1710 case DKIOCFLUSHWRITECACHE: 1711 dkc = (struct dk_callback *)arg; 1712 mutex_exit(&zfsdev_state_lock); 1713 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1714 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1715 (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1716 error = 0; 1717 } 1718 return (error); 1719 1720 case DKIOCGETWCE: 1721 { 1722 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1723 if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1724 flag)) 1725 error = SET_ERROR(EFAULT); 1726 break; 1727 } 1728 case DKIOCSETWCE: 1729 { 1730 int wce; 1731 if (ddi_copyin((void *)arg, &wce, sizeof (int), 1732 flag)) { 1733 error = SET_ERROR(EFAULT); 1734 break; 1735 } 1736 if (wce) { 1737 zv->zv_flags |= ZVOL_WCE; 1738 mutex_exit(&zfsdev_state_lock); 1739 } else { 1740 zv->zv_flags &= ~ZVOL_WCE; 1741 mutex_exit(&zfsdev_state_lock); 1742 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1743 } 1744 return (0); 1745 } 1746 1747 case DKIOCGGEOM: 1748 case DKIOCGVTOC: 1749 /* 1750 * commands using these (like prtvtoc) expect ENOTSUP 1751 * since we're emulating an EFI label 1752 */ 1753 error = SET_ERROR(ENOTSUP); 1754 break; 1755 1756 case DKIOCDUMPINIT: 1757 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1758 RL_WRITER); 1759 error = zvol_dumpify(zv); 1760 zfs_range_unlock(rl); 1761 break; 1762 1763 case DKIOCDUMPFINI: 1764 if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 1765 break; 1766 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1767 RL_WRITER); 1768 error = zvol_dump_fini(zv); 1769 zfs_range_unlock(rl); 1770 break; 1771 1772 case DKIOCFREE: 1773 { 1774 dkioc_free_t df; 1775 dmu_tx_t *tx; 1776 1777 if (!zvol_unmap_enabled) 1778 break; 1779 1780 if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) { 1781 error = SET_ERROR(EFAULT); 1782 break; 1783 } 1784 1785 /* 1786 * Apply Postel's Law to length-checking. If they overshoot, 1787 * just blank out until the end, if there's a need to blank 1788 * out anything. 1789 */ 1790 if (df.df_start >= zv->zv_volsize) 1791 break; /* No need to do anything... */ 1792 1793 mutex_exit(&zfsdev_state_lock); 1794 1795 rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length, 1796 RL_WRITER); 1797 tx = dmu_tx_create(zv->zv_objset); 1798 dmu_tx_mark_netfree(tx); 1799 error = dmu_tx_assign(tx, TXG_WAIT); 1800 if (error != 0) { 1801 dmu_tx_abort(tx); 1802 } else { 1803 zvol_log_truncate(zv, tx, df.df_start, 1804 df.df_length, B_TRUE); 1805 dmu_tx_commit(tx); 1806 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 1807 df.df_start, df.df_length); 1808 } 1809 1810 zfs_range_unlock(rl); 1811 1812 if (error == 0) { 1813 /* 1814 * If the write-cache is disabled or 'sync' property 1815 * is set to 'always' then treat this as a synchronous 1816 * operation (i.e. commit to zil). 1817 */ 1818 if (!(zv->zv_flags & ZVOL_WCE) || 1819 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) 1820 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1821 1822 /* 1823 * If the caller really wants synchronous writes, and 1824 * can't wait for them, don't return until the write 1825 * is done. 1826 */ 1827 if (df.df_flags & DF_WAIT_SYNC) { 1828 txg_wait_synced( 1829 dmu_objset_pool(zv->zv_objset), 0); 1830 } 1831 } 1832 return (error); 1833 } 1834 1835 default: 1836 error = SET_ERROR(ENOTTY); 1837 break; 1838 1839 } 1840 mutex_exit(&zfsdev_state_lock); 1841 return (error); 1842 } 1843 1844 int 1845 zvol_busy(void) 1846 { 1847 return (zvol_minors != 0); 1848 } 1849 1850 void 1851 zvol_init(void) 1852 { 1853 VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t), 1854 1) == 0); 1855 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL); 1856 } 1857 1858 void 1859 zvol_fini(void) 1860 { 1861 mutex_destroy(&zfsdev_state_lock); 1862 ddi_soft_state_fini(&zfsdev_state); 1863 } 1864 1865 /*ARGSUSED*/ 1866 static int 1867 zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx) 1868 { 1869 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1870 1871 if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 1872 return (1); 1873 return (0); 1874 } 1875 1876 /*ARGSUSED*/ 1877 static void 1878 zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx) 1879 { 1880 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1881 1882 spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx); 1883 } 1884 1885 static int 1886 zvol_dump_init(zvol_state_t *zv, boolean_t resize) 1887 { 1888 dmu_tx_t *tx; 1889 int error; 1890 objset_t *os = zv->zv_objset; 1891 spa_t *spa = dmu_objset_spa(os); 1892 vdev_t *vd = spa->spa_root_vdev; 1893 nvlist_t *nv = NULL; 1894 uint64_t version = spa_version(spa); 1895 enum zio_checksum checksum; 1896 1897 ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 1898 ASSERT(vd->vdev_ops == &vdev_root_ops); 1899 1900 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 1901 DMU_OBJECT_END); 1902 /* wait for dmu_free_long_range to actually free the blocks */ 1903 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1904 1905 /* 1906 * If the pool on which the dump device is being initialized has more 1907 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is 1908 * enabled. If so, bump that feature's counter to indicate that the 1909 * feature is active. We also check the vdev type to handle the 1910 * following case: 1911 * # zpool create test raidz disk1 disk2 disk3 1912 * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev), 1913 * the raidz vdev itself has 3 children. 1914 */ 1915 if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) { 1916 if (!spa_feature_is_enabled(spa, 1917 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 1918 return (SET_ERROR(ENOTSUP)); 1919 (void) dsl_sync_task(spa_name(spa), 1920 zfs_mvdev_dump_feature_check, 1921 zfs_mvdev_dump_activate_feature_sync, NULL, 1922 2, ZFS_SPACE_CHECK_RESERVED); 1923 } 1924 1925 tx = dmu_tx_create(os); 1926 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1927 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1928 error = dmu_tx_assign(tx, TXG_WAIT); 1929 if (error) { 1930 dmu_tx_abort(tx); 1931 return (error); 1932 } 1933 1934 /* 1935 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum 1936 * function. Otherwise, use the old default -- OFF. 1937 */ 1938 checksum = spa_feature_is_active(spa, 1939 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY : 1940 ZIO_CHECKSUM_OFF; 1941 1942 /* 1943 * If we are resizing the dump device then we only need to 1944 * update the refreservation to match the newly updated 1945 * zvolsize. Otherwise, we save off the original state of the 1946 * zvol so that we can restore them if the zvol is ever undumpified. 1947 */ 1948 if (resize) { 1949 error = zap_update(os, ZVOL_ZAP_OBJ, 1950 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1951 &zv->zv_volsize, tx); 1952 } else { 1953 uint64_t checksum, compress, refresrv, vbs, dedup; 1954 1955 error = dsl_prop_get_integer(zv->zv_name, 1956 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 1957 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1958 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 1959 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1960 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 1961 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1962 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 1963 if (version >= SPA_VERSION_DEDUP) { 1964 error = error ? error : 1965 dsl_prop_get_integer(zv->zv_name, 1966 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 1967 } 1968 1969 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1970 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 1971 &compress, tx); 1972 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1973 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 1974 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1975 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1976 &refresrv, tx); 1977 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1978 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 1979 &vbs, tx); 1980 error = error ? error : dmu_object_set_blocksize( 1981 os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx); 1982 if (version >= SPA_VERSION_DEDUP) { 1983 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1984 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 1985 &dedup, tx); 1986 } 1987 if (error == 0) 1988 zv->zv_volblocksize = SPA_MAXBLOCKSIZE; 1989 } 1990 dmu_tx_commit(tx); 1991 1992 /* 1993 * We only need update the zvol's property if we are initializing 1994 * the dump area for the first time. 1995 */ 1996 if (!resize) { 1997 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1998 VERIFY(nvlist_add_uint64(nv, 1999 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 2000 VERIFY(nvlist_add_uint64(nv, 2001 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 2002 ZIO_COMPRESS_OFF) == 0); 2003 VERIFY(nvlist_add_uint64(nv, 2004 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 2005 checksum) == 0); 2006 if (version >= SPA_VERSION_DEDUP) { 2007 VERIFY(nvlist_add_uint64(nv, 2008 zfs_prop_to_name(ZFS_PROP_DEDUP), 2009 ZIO_CHECKSUM_OFF) == 0); 2010 } 2011 2012 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 2013 nv, NULL); 2014 nvlist_free(nv); 2015 2016 if (error) 2017 return (error); 2018 } 2019 2020 /* Allocate the space for the dump */ 2021 error = zvol_prealloc(zv); 2022 return (error); 2023 } 2024 2025 static int 2026 zvol_dumpify(zvol_state_t *zv) 2027 { 2028 int error = 0; 2029 uint64_t dumpsize = 0; 2030 dmu_tx_t *tx; 2031 objset_t *os = zv->zv_objset; 2032 2033 if (zv->zv_flags & ZVOL_RDONLY) 2034 return (SET_ERROR(EROFS)); 2035 2036 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 2037 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 2038 boolean_t resize = (dumpsize > 0); 2039 2040 if ((error = zvol_dump_init(zv, resize)) != 0) { 2041 (void) zvol_dump_fini(zv); 2042 return (error); 2043 } 2044 } 2045 2046 /* 2047 * Build up our lba mapping. 2048 */ 2049 error = zvol_get_lbas(zv); 2050 if (error) { 2051 (void) zvol_dump_fini(zv); 2052 return (error); 2053 } 2054 2055 tx = dmu_tx_create(os); 2056 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2057 error = dmu_tx_assign(tx, TXG_WAIT); 2058 if (error) { 2059 dmu_tx_abort(tx); 2060 (void) zvol_dump_fini(zv); 2061 return (error); 2062 } 2063 2064 zv->zv_flags |= ZVOL_DUMPIFIED; 2065 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 2066 &zv->zv_volsize, tx); 2067 dmu_tx_commit(tx); 2068 2069 if (error) { 2070 (void) zvol_dump_fini(zv); 2071 return (error); 2072 } 2073 2074 txg_wait_synced(dmu_objset_pool(os), 0); 2075 return (0); 2076 } 2077 2078 static int 2079 zvol_dump_fini(zvol_state_t *zv) 2080 { 2081 dmu_tx_t *tx; 2082 objset_t *os = zv->zv_objset; 2083 nvlist_t *nv; 2084 int error = 0; 2085 uint64_t checksum, compress, refresrv, vbs, dedup; 2086 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 2087 2088 /* 2089 * Attempt to restore the zvol back to its pre-dumpified state. 2090 * This is a best-effort attempt as it's possible that not all 2091 * of these properties were initialized during the dumpify process 2092 * (i.e. error during zvol_dump_init). 2093 */ 2094 2095 tx = dmu_tx_create(os); 2096 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2097 error = dmu_tx_assign(tx, TXG_WAIT); 2098 if (error) { 2099 dmu_tx_abort(tx); 2100 return (error); 2101 } 2102 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 2103 dmu_tx_commit(tx); 2104 2105 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2106 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 2107 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2108 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 2109 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2110 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 2111 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2112 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 2113 2114 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2115 (void) nvlist_add_uint64(nv, 2116 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 2117 (void) nvlist_add_uint64(nv, 2118 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 2119 (void) nvlist_add_uint64(nv, 2120 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 2121 if (version >= SPA_VERSION_DEDUP && 2122 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2123 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 2124 (void) nvlist_add_uint64(nv, 2125 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 2126 } 2127 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 2128 nv, NULL); 2129 nvlist_free(nv); 2130 2131 zvol_free_extents(zv); 2132 zv->zv_flags &= ~ZVOL_DUMPIFIED; 2133 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 2134 /* wait for dmu_free_long_range to actually free the blocks */ 2135 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 2136 tx = dmu_tx_create(os); 2137 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 2138 error = dmu_tx_assign(tx, TXG_WAIT); 2139 if (error) { 2140 dmu_tx_abort(tx); 2141 return (error); 2142 } 2143 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 2144 zv->zv_volblocksize = vbs; 2145 dmu_tx_commit(tx); 2146 2147 return (0); 2148 } 2149