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