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