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 25 /* 26 * ZFS volume emulation driver. 27 * 28 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. 29 * Volumes are accessed through the symbolic links named: 30 * 31 * /dev/zvol/dsk/<pool_name>/<dataset_name> 32 * /dev/zvol/rdsk/<pool_name>/<dataset_name> 33 * 34 * These links are created by the /dev filesystem (sdev_zvolops.c). 35 * Volumes are persistent through reboot. No user command needs to be 36 * run before opening and using a device. 37 */ 38 39 #include <sys/types.h> 40 #include <sys/param.h> 41 #include <sys/errno.h> 42 #include <sys/uio.h> 43 #include <sys/buf.h> 44 #include <sys/modctl.h> 45 #include <sys/open.h> 46 #include <sys/kmem.h> 47 #include <sys/conf.h> 48 #include <sys/cmn_err.h> 49 #include <sys/stat.h> 50 #include <sys/zap.h> 51 #include <sys/spa.h> 52 #include <sys/zio.h> 53 #include <sys/dmu_traverse.h> 54 #include <sys/dnode.h> 55 #include <sys/dsl_dataset.h> 56 #include <sys/dsl_prop.h> 57 #include <sys/dkio.h> 58 #include <sys/efi_partition.h> 59 #include <sys/byteorder.h> 60 #include <sys/pathname.h> 61 #include <sys/ddi.h> 62 #include <sys/sunddi.h> 63 #include <sys/crc32.h> 64 #include <sys/dirent.h> 65 #include <sys/policy.h> 66 #include <sys/fs/zfs.h> 67 #include <sys/zfs_ioctl.h> 68 #include <sys/mkdev.h> 69 #include <sys/zil.h> 70 #include <sys/refcount.h> 71 #include <sys/zfs_znode.h> 72 #include <sys/zfs_rlock.h> 73 #include <sys/vdev_disk.h> 74 #include <sys/vdev_impl.h> 75 #include <sys/zvol.h> 76 #include <sys/dumphdr.h> 77 #include <sys/zil_impl.h> 78 79 #include "zfs_namecheck.h" 80 81 static void *zvol_state; 82 static char *zvol_tag = "zvol_tag"; 83 84 #define ZVOL_DUMPSIZE "dumpsize" 85 86 /* 87 * This lock protects the zvol_state structure from being modified 88 * while it's being used, e.g. an open that comes in before a create 89 * finishes. It also protects temporary opens of the dataset so that, 90 * e.g., an open doesn't get a spurious EBUSY. 91 */ 92 static kmutex_t zvol_state_lock; 93 static uint32_t zvol_minors; 94 95 typedef struct zvol_extent { 96 list_node_t ze_node; 97 dva_t ze_dva; /* dva associated with this extent */ 98 uint64_t ze_nblks; /* number of blocks in extent */ 99 } zvol_extent_t; 100 101 /* 102 * The in-core state of each volume. 103 */ 104 typedef struct zvol_state { 105 char zv_name[MAXPATHLEN]; /* pool/dd name */ 106 uint64_t zv_volsize; /* amount of space we advertise */ 107 uint64_t zv_volblocksize; /* volume block size */ 108 minor_t zv_minor; /* minor number */ 109 uint8_t zv_min_bs; /* minimum addressable block shift */ 110 uint8_t zv_flags; /* readonly, dumpified, etc. */ 111 objset_t *zv_objset; /* objset handle */ 112 uint32_t zv_open_count[OTYPCNT]; /* open counts */ 113 uint32_t zv_total_opens; /* total open count */ 114 zilog_t *zv_zilog; /* ZIL handle */ 115 list_t zv_extents; /* List of extents for dump */ 116 znode_t zv_znode; /* for range locking */ 117 dmu_buf_t *zv_dbuf; /* bonus handle */ 118 } zvol_state_t; 119 120 /* 121 * zvol specific flags 122 */ 123 #define ZVOL_RDONLY 0x1 124 #define ZVOL_DUMPIFIED 0x2 125 #define ZVOL_EXCL 0x4 126 #define ZVOL_WCE 0x8 127 128 /* 129 * zvol maximum transfer in one DMU tx. 130 */ 131 int zvol_maxphys = DMU_MAX_ACCESS/2; 132 133 extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 134 nvlist_t *, nvlist_t **); 135 static int zvol_remove_zv(zvol_state_t *); 136 static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 137 static int zvol_dumpify(zvol_state_t *zv); 138 static int zvol_dump_fini(zvol_state_t *zv); 139 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 140 141 static void 142 zvol_size_changed(uint64_t volsize, major_t maj, minor_t min) 143 { 144 dev_t dev = makedevice(maj, min); 145 146 VERIFY(ddi_prop_update_int64(dev, zfs_dip, 147 "Size", volsize) == DDI_SUCCESS); 148 VERIFY(ddi_prop_update_int64(dev, zfs_dip, 149 "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 150 151 /* Notify specfs to invalidate the cached size */ 152 spec_size_invalidate(dev, VBLK); 153 spec_size_invalidate(dev, VCHR); 154 } 155 156 int 157 zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 158 { 159 if (volsize == 0) 160 return (EINVAL); 161 162 if (volsize % blocksize != 0) 163 return (EINVAL); 164 165 #ifdef _ILP32 166 if (volsize - 1 > SPEC_MAXOFFSET_T) 167 return (EOVERFLOW); 168 #endif 169 return (0); 170 } 171 172 int 173 zvol_check_volblocksize(uint64_t volblocksize) 174 { 175 if (volblocksize < SPA_MINBLOCKSIZE || 176 volblocksize > SPA_MAXBLOCKSIZE || 177 !ISP2(volblocksize)) 178 return (EDOM); 179 180 return (0); 181 } 182 183 int 184 zvol_get_stats(objset_t *os, nvlist_t *nv) 185 { 186 int error; 187 dmu_object_info_t doi; 188 uint64_t val; 189 190 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 191 if (error) 192 return (error); 193 194 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 195 196 error = dmu_object_info(os, ZVOL_OBJ, &doi); 197 198 if (error == 0) { 199 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 200 doi.doi_data_block_size); 201 } 202 203 return (error); 204 } 205 206 /* 207 * Find a free minor number. 208 */ 209 static minor_t 210 zvol_minor_alloc(void) 211 { 212 minor_t minor; 213 214 ASSERT(MUTEX_HELD(&zvol_state_lock)); 215 216 for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) 217 if (ddi_get_soft_state(zvol_state, minor) == NULL) 218 return (minor); 219 220 return (0); 221 } 222 223 static zvol_state_t * 224 zvol_minor_lookup(const char *name) 225 { 226 minor_t minor; 227 zvol_state_t *zv; 228 229 ASSERT(MUTEX_HELD(&zvol_state_lock)); 230 231 for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 232 zv = ddi_get_soft_state(zvol_state, minor); 233 if (zv == NULL) 234 continue; 235 if (strcmp(zv->zv_name, name) == 0) 236 return (zv); 237 } 238 239 return (NULL); 240 } 241 242 /* extent mapping arg */ 243 struct maparg { 244 zvol_state_t *ma_zv; 245 uint64_t ma_blks; 246 }; 247 248 /*ARGSUSED*/ 249 static int 250 zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 251 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 252 { 253 struct maparg *ma = arg; 254 zvol_extent_t *ze; 255 int bs = ma->ma_zv->zv_volblocksize; 256 257 if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 258 return (0); 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 (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_WRITE ZIL transaction that didn't get committed 361 * after a system failure 362 */ 363 static int 364 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 365 { 366 objset_t *os = zv->zv_objset; 367 char *data = (char *)(lr + 1); /* data follows lr_write_t */ 368 uint64_t offset, length; 369 dmu_tx_t *tx; 370 int error; 371 372 if (byteswap) 373 byteswap_uint64_array(lr, sizeof (*lr)); 374 375 offset = lr->lr_offset; 376 length = lr->lr_length; 377 378 /* If it's a dmu_sync() block, write the whole block */ 379 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 380 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 381 if (length < blocksize) { 382 offset -= offset % blocksize; 383 length = blocksize; 384 } 385 } 386 387 tx = dmu_tx_create(os); 388 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 389 error = dmu_tx_assign(tx, TXG_WAIT); 390 if (error) { 391 dmu_tx_abort(tx); 392 } else { 393 dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 394 dmu_tx_commit(tx); 395 } 396 397 return (error); 398 } 399 400 /* ARGSUSED */ 401 static int 402 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 403 { 404 return (ENOTSUP); 405 } 406 407 /* 408 * Callback vectors for replaying records. 409 * Only TX_WRITE is needed for zvol. 410 */ 411 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 412 zvol_replay_err, /* 0 no such transaction type */ 413 zvol_replay_err, /* TX_CREATE */ 414 zvol_replay_err, /* TX_MKDIR */ 415 zvol_replay_err, /* TX_MKXATTR */ 416 zvol_replay_err, /* TX_SYMLINK */ 417 zvol_replay_err, /* TX_REMOVE */ 418 zvol_replay_err, /* TX_RMDIR */ 419 zvol_replay_err, /* TX_LINK */ 420 zvol_replay_err, /* TX_RENAME */ 421 zvol_replay_write, /* TX_WRITE */ 422 zvol_replay_err, /* TX_TRUNCATE */ 423 zvol_replay_err, /* TX_SETATTR */ 424 zvol_replay_err, /* TX_ACL */ 425 zvol_replay_err, /* TX_CREATE_ACL */ 426 zvol_replay_err, /* TX_CREATE_ATTR */ 427 zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 428 zvol_replay_err, /* TX_MKDIR_ACL */ 429 zvol_replay_err, /* TX_MKDIR_ATTR */ 430 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 431 zvol_replay_err, /* TX_WRITE2 */ 432 }; 433 434 int 435 zvol_name2minor(const char *name, minor_t *minor) 436 { 437 zvol_state_t *zv; 438 439 mutex_enter(&zvol_state_lock); 440 zv = zvol_minor_lookup(name); 441 if (minor && zv) 442 *minor = zv->zv_minor; 443 mutex_exit(&zvol_state_lock); 444 return (zv ? 0 : -1); 445 } 446 447 /* 448 * Create a minor node (plus a whole lot more) for the specified volume. 449 */ 450 int 451 zvol_create_minor(const char *name) 452 { 453 zvol_state_t *zv; 454 objset_t *os; 455 dmu_object_info_t doi; 456 minor_t minor = 0; 457 char chrbuf[30], blkbuf[30]; 458 int error; 459 460 mutex_enter(&zvol_state_lock); 461 462 if (zvol_minor_lookup(name) != NULL) { 463 mutex_exit(&zvol_state_lock); 464 return (EEXIST); 465 } 466 467 /* lie and say we're read-only */ 468 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os); 469 470 if (error) { 471 mutex_exit(&zvol_state_lock); 472 return (error); 473 } 474 475 if ((minor = zvol_minor_alloc()) == 0) { 476 dmu_objset_disown(os, zvol_tag); 477 mutex_exit(&zvol_state_lock); 478 return (ENXIO); 479 } 480 481 if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) { 482 dmu_objset_disown(os, zvol_tag); 483 mutex_exit(&zvol_state_lock); 484 return (EAGAIN); 485 } 486 (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 487 (char *)name); 488 489 (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 490 491 if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 492 minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 493 ddi_soft_state_free(zvol_state, minor); 494 dmu_objset_disown(os, zvol_tag); 495 mutex_exit(&zvol_state_lock); 496 return (EAGAIN); 497 } 498 499 (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 500 501 if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 502 minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 503 ddi_remove_minor_node(zfs_dip, chrbuf); 504 ddi_soft_state_free(zvol_state, minor); 505 dmu_objset_disown(os, zvol_tag); 506 mutex_exit(&zvol_state_lock); 507 return (EAGAIN); 508 } 509 510 zv = ddi_get_soft_state(zvol_state, minor); 511 512 (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 513 zv->zv_min_bs = DEV_BSHIFT; 514 zv->zv_minor = minor; 515 zv->zv_objset = os; 516 if (dmu_objset_is_snapshot(os)) 517 zv->zv_flags |= ZVOL_RDONLY; 518 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 519 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 520 sizeof (rl_t), offsetof(rl_t, r_node)); 521 list_create(&zv->zv_extents, sizeof (zvol_extent_t), 522 offsetof(zvol_extent_t, ze_node)); 523 /* get and cache the blocksize */ 524 error = dmu_object_info(os, ZVOL_OBJ, &doi); 525 ASSERT(error == 0); 526 zv->zv_volblocksize = doi.doi_data_block_size; 527 528 zil_replay(os, zv, zvol_replay_vector); 529 dmu_objset_disown(os, zvol_tag); 530 zv->zv_objset = NULL; 531 532 zvol_minors++; 533 534 mutex_exit(&zvol_state_lock); 535 536 return (0); 537 } 538 539 /* 540 * Remove minor node for the specified volume. 541 */ 542 static int 543 zvol_remove_zv(zvol_state_t *zv) 544 { 545 char nmbuf[20]; 546 547 ASSERT(MUTEX_HELD(&zvol_state_lock)); 548 if (zv->zv_total_opens != 0) 549 return (EBUSY); 550 551 (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", zv->zv_minor); 552 ddi_remove_minor_node(zfs_dip, nmbuf); 553 554 (void) snprintf(nmbuf, sizeof (nmbuf), "%u", zv->zv_minor); 555 ddi_remove_minor_node(zfs_dip, nmbuf); 556 557 avl_destroy(&zv->zv_znode.z_range_avl); 558 mutex_destroy(&zv->zv_znode.z_range_lock); 559 560 ddi_soft_state_free(zvol_state, zv->zv_minor); 561 562 zvol_minors--; 563 return (0); 564 } 565 566 int 567 zvol_remove_minor(const char *name) 568 { 569 zvol_state_t *zv; 570 int rc; 571 572 mutex_enter(&zvol_state_lock); 573 if ((zv = zvol_minor_lookup(name)) == NULL) { 574 mutex_exit(&zvol_state_lock); 575 return (ENXIO); 576 } 577 rc = zvol_remove_zv(zv); 578 mutex_exit(&zvol_state_lock); 579 return (rc); 580 } 581 582 int 583 zvol_first_open(zvol_state_t *zv) 584 { 585 objset_t *os; 586 uint64_t volsize; 587 int error; 588 uint64_t readonly; 589 590 /* lie and say we're read-only */ 591 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 592 zvol_tag, &os); 593 if (error) 594 return (error); 595 596 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 597 if (error) { 598 ASSERT(error == 0); 599 dmu_objset_disown(os, zvol_tag); 600 return (error); 601 } 602 zv->zv_objset = os; 603 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); 604 if (error) { 605 dmu_objset_disown(os, zvol_tag); 606 return (error); 607 } 608 zv->zv_volsize = volsize; 609 zv->zv_zilog = zil_open(os, zvol_get_data); 610 zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip), 611 zv->zv_minor); 612 613 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 614 NULL) == 0); 615 if (readonly || dmu_objset_is_snapshot(os)) 616 zv->zv_flags |= ZVOL_RDONLY; 617 else 618 zv->zv_flags &= ~ZVOL_RDONLY; 619 return (error); 620 } 621 622 void 623 zvol_last_close(zvol_state_t *zv) 624 { 625 zil_close(zv->zv_zilog); 626 zv->zv_zilog = NULL; 627 dmu_buf_rele(zv->zv_dbuf, zvol_tag); 628 zv->zv_dbuf = NULL; 629 dmu_objset_disown(zv->zv_objset, zvol_tag); 630 zv->zv_objset = NULL; 631 } 632 633 int 634 zvol_prealloc(zvol_state_t *zv) 635 { 636 objset_t *os = zv->zv_objset; 637 dmu_tx_t *tx; 638 uint64_t refd, avail, usedobjs, availobjs; 639 uint64_t resid = zv->zv_volsize; 640 uint64_t off = 0; 641 642 /* Check the space usage before attempting to allocate the space */ 643 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 644 if (avail < zv->zv_volsize) 645 return (ENOSPC); 646 647 /* Free old extents if they exist */ 648 zvol_free_extents(zv); 649 650 while (resid != 0) { 651 int error; 652 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 653 654 tx = dmu_tx_create(os); 655 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 656 error = dmu_tx_assign(tx, TXG_WAIT); 657 if (error) { 658 dmu_tx_abort(tx); 659 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 660 return (error); 661 } 662 dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 663 dmu_tx_commit(tx); 664 off += bytes; 665 resid -= bytes; 666 } 667 txg_wait_synced(dmu_objset_pool(os), 0); 668 669 return (0); 670 } 671 672 int 673 zvol_update_volsize(objset_t *os, uint64_t volsize) 674 { 675 dmu_tx_t *tx; 676 int error; 677 678 ASSERT(MUTEX_HELD(&zvol_state_lock)); 679 680 tx = dmu_tx_create(os); 681 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 682 error = dmu_tx_assign(tx, TXG_WAIT); 683 if (error) { 684 dmu_tx_abort(tx); 685 return (error); 686 } 687 688 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 689 &volsize, tx); 690 dmu_tx_commit(tx); 691 692 if (error == 0) 693 error = dmu_free_long_range(os, 694 ZVOL_OBJ, volsize, DMU_OBJECT_END); 695 return (error); 696 } 697 698 void 699 zvol_remove_minors(const char *name) 700 { 701 zvol_state_t *zv; 702 char *namebuf; 703 minor_t minor; 704 705 namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP); 706 (void) strncpy(namebuf, name, strlen(name)); 707 (void) strcat(namebuf, "/"); 708 mutex_enter(&zvol_state_lock); 709 for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 710 711 zv = ddi_get_soft_state(zvol_state, minor); 712 if (zv == NULL) 713 continue; 714 if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0) 715 (void) zvol_remove_zv(zv); 716 } 717 kmem_free(namebuf, strlen(name) + 2); 718 719 mutex_exit(&zvol_state_lock); 720 } 721 722 int 723 zvol_set_volsize(const char *name, major_t maj, uint64_t volsize) 724 { 725 zvol_state_t *zv = NULL; 726 objset_t *os; 727 int error; 728 dmu_object_info_t doi; 729 uint64_t old_volsize = 0ULL; 730 uint64_t readonly; 731 732 mutex_enter(&zvol_state_lock); 733 zv = zvol_minor_lookup(name); 734 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 735 mutex_exit(&zvol_state_lock); 736 return (error); 737 } 738 739 if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 740 (error = zvol_check_volsize(volsize, 741 doi.doi_data_block_size)) != 0) 742 goto out; 743 744 VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, 745 NULL) == 0); 746 if (readonly) { 747 error = EROFS; 748 goto out; 749 } 750 751 error = zvol_update_volsize(os, volsize); 752 /* 753 * Reinitialize the dump area to the new size. If we 754 * failed to resize the dump area then restore it back to 755 * its original size. 756 */ 757 if (zv && error == 0) { 758 if (zv->zv_flags & ZVOL_DUMPIFIED) { 759 old_volsize = zv->zv_volsize; 760 zv->zv_volsize = volsize; 761 if ((error = zvol_dumpify(zv)) != 0 || 762 (error = dumpvp_resize()) != 0) { 763 (void) zvol_update_volsize(os, old_volsize); 764 zv->zv_volsize = old_volsize; 765 error = zvol_dumpify(zv); 766 } 767 } 768 if (error == 0) { 769 zv->zv_volsize = volsize; 770 zvol_size_changed(volsize, maj, zv->zv_minor); 771 } 772 } 773 774 /* 775 * Generate a LUN expansion event. 776 */ 777 if (zv && error == 0) { 778 sysevent_id_t eid; 779 nvlist_t *attr; 780 char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 781 782 (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 783 zv->zv_minor); 784 785 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 786 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 787 788 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 789 ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 790 791 nvlist_free(attr); 792 kmem_free(physpath, MAXPATHLEN); 793 } 794 795 out: 796 dmu_objset_rele(os, FTAG); 797 798 mutex_exit(&zvol_state_lock); 799 800 return (error); 801 } 802 803 /*ARGSUSED*/ 804 int 805 zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 806 { 807 minor_t minor = getminor(*devp); 808 zvol_state_t *zv; 809 int err = 0; 810 811 if (minor == 0) /* This is the control device */ 812 return (0); 813 814 mutex_enter(&zvol_state_lock); 815 816 zv = ddi_get_soft_state(zvol_state, minor); 817 if (zv == NULL) { 818 mutex_exit(&zvol_state_lock); 819 return (ENXIO); 820 } 821 822 if (zv->zv_total_opens == 0) 823 err = zvol_first_open(zv); 824 if (err) { 825 mutex_exit(&zvol_state_lock); 826 return (err); 827 } 828 if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 829 err = EROFS; 830 goto out; 831 } 832 if (zv->zv_flags & ZVOL_EXCL) { 833 err = EBUSY; 834 goto out; 835 } 836 if (flag & FEXCL) { 837 if (zv->zv_total_opens != 0) { 838 err = EBUSY; 839 goto out; 840 } 841 zv->zv_flags |= ZVOL_EXCL; 842 } 843 844 if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 845 zv->zv_open_count[otyp]++; 846 zv->zv_total_opens++; 847 } 848 mutex_exit(&zvol_state_lock); 849 850 return (err); 851 out: 852 if (zv->zv_total_opens == 0) 853 zvol_last_close(zv); 854 mutex_exit(&zvol_state_lock); 855 return (err); 856 } 857 858 /*ARGSUSED*/ 859 int 860 zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 861 { 862 minor_t minor = getminor(dev); 863 zvol_state_t *zv; 864 int error = 0; 865 866 if (minor == 0) /* This is the control device */ 867 return (0); 868 869 mutex_enter(&zvol_state_lock); 870 871 zv = ddi_get_soft_state(zvol_state, minor); 872 if (zv == NULL) { 873 mutex_exit(&zvol_state_lock); 874 return (ENXIO); 875 } 876 877 if (zv->zv_flags & ZVOL_EXCL) { 878 ASSERT(zv->zv_total_opens == 1); 879 zv->zv_flags &= ~ZVOL_EXCL; 880 } 881 882 /* 883 * If the open count is zero, this is a spurious close. 884 * That indicates a bug in the kernel / DDI framework. 885 */ 886 ASSERT(zv->zv_open_count[otyp] != 0); 887 ASSERT(zv->zv_total_opens != 0); 888 889 /* 890 * You may get multiple opens, but only one close. 891 */ 892 zv->zv_open_count[otyp]--; 893 zv->zv_total_opens--; 894 895 if (zv->zv_total_opens == 0) 896 zvol_last_close(zv); 897 898 mutex_exit(&zvol_state_lock); 899 return (error); 900 } 901 902 static void 903 zvol_get_done(zgd_t *zgd, int error) 904 { 905 if (zgd->zgd_db) 906 dmu_buf_rele(zgd->zgd_db, zgd); 907 908 zfs_range_unlock(zgd->zgd_rl); 909 910 if (error == 0 && zgd->zgd_bp) 911 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 912 913 kmem_free(zgd, sizeof (zgd_t)); 914 } 915 916 /* 917 * Get data to generate a TX_WRITE intent log record. 918 */ 919 static int 920 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 921 { 922 zvol_state_t *zv = arg; 923 objset_t *os = zv->zv_objset; 924 uint64_t object = ZVOL_OBJ; 925 uint64_t offset = lr->lr_offset; 926 uint64_t size = lr->lr_length; /* length of user data */ 927 blkptr_t *bp = &lr->lr_blkptr; 928 dmu_buf_t *db; 929 zgd_t *zgd; 930 int error; 931 932 ASSERT(zio != NULL); 933 ASSERT(size != 0); 934 935 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 936 zgd->zgd_zilog = zv->zv_zilog; 937 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); 938 939 /* 940 * Write records come in two flavors: immediate and indirect. 941 * For small writes it's cheaper to store the data with the 942 * log record (immediate); for large writes it's cheaper to 943 * sync the data and get a pointer to it (indirect) so that 944 * we don't have to write the data twice. 945 */ 946 if (buf != NULL) { /* immediate write */ 947 error = dmu_read(os, object, offset, size, buf, 948 DMU_READ_NO_PREFETCH); 949 } else { 950 size = zv->zv_volblocksize; 951 offset = P2ALIGN(offset, size); 952 error = dmu_buf_hold(os, object, offset, zgd, &db, 953 DMU_READ_NO_PREFETCH); 954 if (error == 0) { 955 zgd->zgd_db = db; 956 zgd->zgd_bp = bp; 957 958 ASSERT(db->db_offset == offset); 959 ASSERT(db->db_size == size); 960 961 error = dmu_sync(zio, lr->lr_common.lrc_txg, 962 zvol_get_done, zgd); 963 964 if (error == 0) 965 return (0); 966 } 967 } 968 969 zvol_get_done(zgd, error); 970 971 return (error); 972 } 973 974 /* 975 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 976 * 977 * We store data in the log buffers if it's small enough. 978 * Otherwise we will later flush the data out via dmu_sync(). 979 */ 980 ssize_t zvol_immediate_write_sz = 32768; 981 982 static void 983 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 984 boolean_t sync) 985 { 986 uint32_t blocksize = zv->zv_volblocksize; 987 zilog_t *zilog = zv->zv_zilog; 988 boolean_t slogging; 989 ssize_t immediate_write_sz; 990 991 if (zil_disable) 992 return; 993 994 if (zil_replaying(zilog, tx)) 995 return; 996 997 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 998 ? 0 : zvol_immediate_write_sz; 999 1000 slogging = spa_has_slogs(zilog->zl_spa) && 1001 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 1002 1003 while (resid) { 1004 itx_t *itx; 1005 lr_write_t *lr; 1006 ssize_t len; 1007 itx_wr_state_t write_state; 1008 1009 /* 1010 * Unlike zfs_log_write() we can be called with 1011 * upto DMU_MAX_ACCESS/2 (5MB) writes. 1012 */ 1013 if (blocksize > immediate_write_sz && !slogging && 1014 resid >= blocksize && off % blocksize == 0) { 1015 write_state = WR_INDIRECT; /* uses dmu_sync */ 1016 len = blocksize; 1017 } else if (sync) { 1018 write_state = WR_COPIED; 1019 len = MIN(ZIL_MAX_LOG_DATA, resid); 1020 } else { 1021 write_state = WR_NEED_COPY; 1022 len = MIN(ZIL_MAX_LOG_DATA, resid); 1023 } 1024 1025 itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1026 (write_state == WR_COPIED ? len : 0)); 1027 lr = (lr_write_t *)&itx->itx_lr; 1028 if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 1029 ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1030 zil_itx_destroy(itx); 1031 itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1032 lr = (lr_write_t *)&itx->itx_lr; 1033 write_state = WR_NEED_COPY; 1034 } 1035 1036 itx->itx_wr_state = write_state; 1037 if (write_state == WR_NEED_COPY) 1038 itx->itx_sod += len; 1039 lr->lr_foid = ZVOL_OBJ; 1040 lr->lr_offset = off; 1041 lr->lr_length = len; 1042 lr->lr_blkoff = 0; 1043 BP_ZERO(&lr->lr_blkptr); 1044 1045 itx->itx_private = zv; 1046 itx->itx_sync = sync; 1047 1048 (void) zil_itx_assign(zilog, itx, tx); 1049 1050 off += len; 1051 resid -= len; 1052 } 1053 } 1054 1055 static int 1056 zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size, 1057 boolean_t doread, boolean_t isdump) 1058 { 1059 vdev_disk_t *dvd; 1060 int c; 1061 int numerrors = 0; 1062 1063 for (c = 0; c < vd->vdev_children; c++) { 1064 ASSERT(vd->vdev_ops == &vdev_mirror_ops || 1065 vd->vdev_ops == &vdev_replacing_ops || 1066 vd->vdev_ops == &vdev_spare_ops); 1067 int err = zvol_dumpio_vdev(vd->vdev_child[c], 1068 addr, offset, size, doread, isdump); 1069 if (err != 0) { 1070 numerrors++; 1071 } else if (doread) { 1072 break; 1073 } 1074 } 1075 1076 if (!vd->vdev_ops->vdev_op_leaf) 1077 return (numerrors < vd->vdev_children ? 0 : EIO); 1078 1079 if (doread && !vdev_readable(vd)) 1080 return (EIO); 1081 else if (!doread && !vdev_writeable(vd)) 1082 return (EIO); 1083 1084 dvd = vd->vdev_tsd; 1085 ASSERT3P(dvd, !=, NULL); 1086 offset += VDEV_LABEL_START_SIZE; 1087 1088 if (ddi_in_panic() || isdump) { 1089 ASSERT(!doread); 1090 if (doread) 1091 return (EIO); 1092 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1093 lbtodb(size))); 1094 } else { 1095 return (vdev_disk_physio(dvd->vd_lh, addr, size, offset, 1096 doread ? B_READ : B_WRITE)); 1097 } 1098 } 1099 1100 static int 1101 zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 1102 boolean_t doread, boolean_t isdump) 1103 { 1104 vdev_t *vd; 1105 int error; 1106 zvol_extent_t *ze; 1107 spa_t *spa = dmu_objset_spa(zv->zv_objset); 1108 1109 /* Must be sector aligned, and not stradle a block boundary. */ 1110 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 1111 P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 1112 return (EINVAL); 1113 } 1114 ASSERT(size <= zv->zv_volblocksize); 1115 1116 /* Locate the extent this belongs to */ 1117 ze = list_head(&zv->zv_extents); 1118 while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 1119 offset -= ze->ze_nblks * zv->zv_volblocksize; 1120 ze = list_next(&zv->zv_extents, ze); 1121 } 1122 1123 if (!ddi_in_panic()) 1124 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 1125 1126 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 1127 offset += DVA_GET_OFFSET(&ze->ze_dva); 1128 error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump); 1129 1130 if (!ddi_in_panic()) 1131 spa_config_exit(spa, SCL_STATE, FTAG); 1132 1133 return (error); 1134 } 1135 1136 int 1137 zvol_strategy(buf_t *bp) 1138 { 1139 zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev)); 1140 uint64_t off, volsize; 1141 size_t resid; 1142 char *addr; 1143 objset_t *os; 1144 rl_t *rl; 1145 int error = 0; 1146 boolean_t doread = bp->b_flags & B_READ; 1147 boolean_t is_dump; 1148 boolean_t sync; 1149 1150 if (zv == NULL) { 1151 bioerror(bp, ENXIO); 1152 biodone(bp); 1153 return (0); 1154 } 1155 1156 if (getminor(bp->b_edev) == 0) { 1157 bioerror(bp, EINVAL); 1158 biodone(bp); 1159 return (0); 1160 } 1161 1162 if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) { 1163 bioerror(bp, EROFS); 1164 biodone(bp); 1165 return (0); 1166 } 1167 1168 off = ldbtob(bp->b_blkno); 1169 volsize = zv->zv_volsize; 1170 1171 os = zv->zv_objset; 1172 ASSERT(os != NULL); 1173 1174 bp_mapin(bp); 1175 addr = bp->b_un.b_addr; 1176 resid = bp->b_bcount; 1177 1178 if (resid > 0 && (off < 0 || off >= volsize)) { 1179 bioerror(bp, EIO); 1180 biodone(bp); 1181 return (0); 1182 } 1183 1184 is_dump = zv->zv_flags & ZVOL_DUMPIFIED; 1185 sync = !(bp->b_flags & B_ASYNC) && !doread && !is_dump && 1186 !(zv->zv_flags & ZVOL_WCE) && !zil_disable; 1187 1188 /* 1189 * There must be no buffer changes when doing a dmu_sync() because 1190 * we can't change the data whilst calculating the checksum. 1191 */ 1192 rl = zfs_range_lock(&zv->zv_znode, off, resid, 1193 doread ? RL_READER : RL_WRITER); 1194 1195 while (resid != 0 && off < volsize) { 1196 size_t size = MIN(resid, zvol_maxphys); 1197 if (is_dump) { 1198 size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 1199 error = zvol_dumpio(zv, addr, off, size, 1200 doread, B_FALSE); 1201 } else if (doread) { 1202 error = dmu_read(os, ZVOL_OBJ, off, size, addr, 1203 DMU_READ_PREFETCH); 1204 } else { 1205 dmu_tx_t *tx = dmu_tx_create(os); 1206 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1207 error = dmu_tx_assign(tx, TXG_WAIT); 1208 if (error) { 1209 dmu_tx_abort(tx); 1210 } else { 1211 dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1212 zvol_log_write(zv, tx, off, size, sync); 1213 dmu_tx_commit(tx); 1214 } 1215 } 1216 if (error) { 1217 /* convert checksum errors into IO errors */ 1218 if (error == ECKSUM) 1219 error = EIO; 1220 break; 1221 } 1222 off += size; 1223 addr += size; 1224 resid -= size; 1225 } 1226 zfs_range_unlock(rl); 1227 1228 if ((bp->b_resid = resid) == bp->b_bcount) 1229 bioerror(bp, off > volsize ? EINVAL : error); 1230 1231 if (sync) 1232 zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1233 biodone(bp); 1234 1235 return (0); 1236 } 1237 1238 /* 1239 * Set the buffer count to the zvol maximum transfer. 1240 * Using our own routine instead of the default minphys() 1241 * means that for larger writes we write bigger buffers on X86 1242 * (128K instead of 56K) and flush the disk write cache less often 1243 * (every zvol_maxphys - currently 1MB) instead of minphys (currently 1244 * 56K on X86 and 128K on sparc). 1245 */ 1246 void 1247 zvol_minphys(struct buf *bp) 1248 { 1249 if (bp->b_bcount > zvol_maxphys) 1250 bp->b_bcount = zvol_maxphys; 1251 } 1252 1253 int 1254 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1255 { 1256 minor_t minor = getminor(dev); 1257 zvol_state_t *zv; 1258 int error = 0; 1259 uint64_t size; 1260 uint64_t boff; 1261 uint64_t resid; 1262 1263 if (minor == 0) /* This is the control device */ 1264 return (ENXIO); 1265 1266 zv = ddi_get_soft_state(zvol_state, minor); 1267 if (zv == NULL) 1268 return (ENXIO); 1269 1270 boff = ldbtob(blkno); 1271 resid = ldbtob(nblocks); 1272 1273 VERIFY3U(boff + resid, <=, zv->zv_volsize); 1274 1275 while (resid) { 1276 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 1277 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1278 if (error) 1279 break; 1280 boff += size; 1281 addr += size; 1282 resid -= size; 1283 } 1284 1285 return (error); 1286 } 1287 1288 /*ARGSUSED*/ 1289 int 1290 zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1291 { 1292 minor_t minor = getminor(dev); 1293 zvol_state_t *zv; 1294 uint64_t volsize; 1295 rl_t *rl; 1296 int error = 0; 1297 1298 if (minor == 0) /* This is the control device */ 1299 return (ENXIO); 1300 1301 zv = ddi_get_soft_state(zvol_state, minor); 1302 if (zv == NULL) 1303 return (ENXIO); 1304 1305 volsize = zv->zv_volsize; 1306 if (uio->uio_resid > 0 && 1307 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 1308 return (EIO); 1309 1310 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1311 error = physio(zvol_strategy, NULL, dev, B_READ, 1312 zvol_minphys, uio); 1313 return (error); 1314 } 1315 1316 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1317 RL_READER); 1318 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1319 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1320 1321 /* don't read past the end */ 1322 if (bytes > volsize - uio->uio_loffset) 1323 bytes = volsize - uio->uio_loffset; 1324 1325 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1326 if (error) { 1327 /* convert checksum errors into IO errors */ 1328 if (error == ECKSUM) 1329 error = EIO; 1330 break; 1331 } 1332 } 1333 zfs_range_unlock(rl); 1334 return (error); 1335 } 1336 1337 /*ARGSUSED*/ 1338 int 1339 zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1340 { 1341 minor_t minor = getminor(dev); 1342 zvol_state_t *zv; 1343 uint64_t volsize; 1344 rl_t *rl; 1345 int error = 0; 1346 boolean_t sync; 1347 1348 if (minor == 0) /* This is the control device */ 1349 return (ENXIO); 1350 1351 zv = ddi_get_soft_state(zvol_state, minor); 1352 if (zv == NULL) 1353 return (ENXIO); 1354 1355 volsize = zv->zv_volsize; 1356 if (uio->uio_resid > 0 && 1357 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 1358 return (EIO); 1359 1360 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1361 error = physio(zvol_strategy, NULL, dev, B_WRITE, 1362 zvol_minphys, uio); 1363 return (error); 1364 } 1365 1366 sync = !(zv->zv_flags & ZVOL_WCE) && !zil_disable; 1367 1368 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1369 RL_WRITER); 1370 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1371 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1372 uint64_t off = uio->uio_loffset; 1373 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 1374 1375 if (bytes > volsize - off) /* don't write past the end */ 1376 bytes = volsize - off; 1377 1378 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1379 error = dmu_tx_assign(tx, TXG_WAIT); 1380 if (error) { 1381 dmu_tx_abort(tx); 1382 break; 1383 } 1384 error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx); 1385 if (error == 0) 1386 zvol_log_write(zv, tx, off, bytes, sync); 1387 dmu_tx_commit(tx); 1388 1389 if (error) 1390 break; 1391 } 1392 zfs_range_unlock(rl); 1393 if (sync) 1394 zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1395 return (error); 1396 } 1397 1398 int 1399 zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1400 { 1401 struct uuid uuid = EFI_RESERVED; 1402 efi_gpe_t gpe = { 0 }; 1403 uint32_t crc; 1404 dk_efi_t efi; 1405 int length; 1406 char *ptr; 1407 1408 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1409 return (EFAULT); 1410 ptr = (char *)(uintptr_t)efi.dki_data_64; 1411 length = efi.dki_length; 1412 /* 1413 * Some clients may attempt to request a PMBR for the 1414 * zvol. Currently this interface will return EINVAL to 1415 * such requests. These requests could be supported by 1416 * adding a check for lba == 0 and consing up an appropriate 1417 * PMBR. 1418 */ 1419 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1420 return (EINVAL); 1421 1422 gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1423 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1424 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1425 1426 if (efi.dki_lba == 1) { 1427 efi_gpt_t gpt = { 0 }; 1428 1429 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1430 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1431 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1432 gpt.efi_gpt_MyLBA = LE_64(1ULL); 1433 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1434 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1435 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1436 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1437 gpt.efi_gpt_SizeOfPartitionEntry = 1438 LE_32(sizeof (efi_gpe_t)); 1439 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1440 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1441 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1442 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1443 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1444 flag)) 1445 return (EFAULT); 1446 ptr += sizeof (gpt); 1447 length -= sizeof (gpt); 1448 } 1449 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1450 length), flag)) 1451 return (EFAULT); 1452 return (0); 1453 } 1454 1455 /* 1456 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1457 */ 1458 /*ARGSUSED*/ 1459 int 1460 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1461 { 1462 zvol_state_t *zv; 1463 struct dk_cinfo dki; 1464 struct dk_minfo dkm; 1465 struct dk_callback *dkc; 1466 int error = 0; 1467 rl_t *rl; 1468 1469 mutex_enter(&zvol_state_lock); 1470 1471 zv = ddi_get_soft_state(zvol_state, getminor(dev)); 1472 1473 if (zv == NULL) { 1474 mutex_exit(&zvol_state_lock); 1475 return (ENXIO); 1476 } 1477 ASSERT(zv->zv_total_opens > 0); 1478 1479 switch (cmd) { 1480 1481 case DKIOCINFO: 1482 bzero(&dki, sizeof (dki)); 1483 (void) strcpy(dki.dki_cname, "zvol"); 1484 (void) strcpy(dki.dki_dname, "zvol"); 1485 dki.dki_ctype = DKC_UNKNOWN; 1486 dki.dki_unit = getminor(dev); 1487 dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1488 mutex_exit(&zvol_state_lock); 1489 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1490 error = EFAULT; 1491 return (error); 1492 1493 case DKIOCGMEDIAINFO: 1494 bzero(&dkm, sizeof (dkm)); 1495 dkm.dki_lbsize = 1U << zv->zv_min_bs; 1496 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1497 dkm.dki_media_type = DK_UNKNOWN; 1498 mutex_exit(&zvol_state_lock); 1499 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1500 error = EFAULT; 1501 return (error); 1502 1503 case DKIOCGETEFI: 1504 { 1505 uint64_t vs = zv->zv_volsize; 1506 uint8_t bs = zv->zv_min_bs; 1507 1508 mutex_exit(&zvol_state_lock); 1509 error = zvol_getefi((void *)arg, flag, vs, bs); 1510 return (error); 1511 } 1512 1513 case DKIOCFLUSHWRITECACHE: 1514 dkc = (struct dk_callback *)arg; 1515 mutex_exit(&zvol_state_lock); 1516 zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1517 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1518 (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1519 error = 0; 1520 } 1521 return (error); 1522 1523 case DKIOCGETWCE: 1524 { 1525 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1526 if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1527 flag)) 1528 error = EFAULT; 1529 break; 1530 } 1531 case DKIOCSETWCE: 1532 { 1533 int wce; 1534 if (ddi_copyin((void *)arg, &wce, sizeof (int), 1535 flag)) { 1536 error = EFAULT; 1537 break; 1538 } 1539 if (wce) { 1540 zv->zv_flags |= ZVOL_WCE; 1541 mutex_exit(&zvol_state_lock); 1542 } else { 1543 zv->zv_flags &= ~ZVOL_WCE; 1544 mutex_exit(&zvol_state_lock); 1545 zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1546 } 1547 return (0); 1548 } 1549 1550 case DKIOCGGEOM: 1551 case DKIOCGVTOC: 1552 /* 1553 * commands using these (like prtvtoc) expect ENOTSUP 1554 * since we're emulating an EFI label 1555 */ 1556 error = ENOTSUP; 1557 break; 1558 1559 case DKIOCDUMPINIT: 1560 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1561 RL_WRITER); 1562 error = zvol_dumpify(zv); 1563 zfs_range_unlock(rl); 1564 break; 1565 1566 case DKIOCDUMPFINI: 1567 if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 1568 break; 1569 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1570 RL_WRITER); 1571 error = zvol_dump_fini(zv); 1572 zfs_range_unlock(rl); 1573 break; 1574 1575 default: 1576 error = ENOTTY; 1577 break; 1578 1579 } 1580 mutex_exit(&zvol_state_lock); 1581 return (error); 1582 } 1583 1584 int 1585 zvol_busy(void) 1586 { 1587 return (zvol_minors != 0); 1588 } 1589 1590 void 1591 zvol_init(void) 1592 { 1593 VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0); 1594 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL); 1595 } 1596 1597 void 1598 zvol_fini(void) 1599 { 1600 mutex_destroy(&zvol_state_lock); 1601 ddi_soft_state_fini(&zvol_state); 1602 } 1603 1604 static int 1605 zvol_dump_init(zvol_state_t *zv, boolean_t resize) 1606 { 1607 dmu_tx_t *tx; 1608 int error = 0; 1609 objset_t *os = zv->zv_objset; 1610 nvlist_t *nv = NULL; 1611 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 1612 1613 ASSERT(MUTEX_HELD(&zvol_state_lock)); 1614 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 1615 DMU_OBJECT_END); 1616 /* wait for dmu_free_long_range to actually free the blocks */ 1617 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1618 1619 tx = dmu_tx_create(os); 1620 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1621 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1622 error = dmu_tx_assign(tx, TXG_WAIT); 1623 if (error) { 1624 dmu_tx_abort(tx); 1625 return (error); 1626 } 1627 1628 /* 1629 * If we are resizing the dump device then we only need to 1630 * update the refreservation to match the newly updated 1631 * zvolsize. Otherwise, we save off the original state of the 1632 * zvol so that we can restore them if the zvol is ever undumpified. 1633 */ 1634 if (resize) { 1635 error = zap_update(os, ZVOL_ZAP_OBJ, 1636 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1637 &zv->zv_volsize, tx); 1638 } else { 1639 uint64_t checksum, compress, refresrv, vbs, dedup; 1640 1641 error = dsl_prop_get_integer(zv->zv_name, 1642 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 1643 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1644 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 1645 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1646 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 1647 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1648 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 1649 if (version >= SPA_VERSION_DEDUP) { 1650 error = error ? error : 1651 dsl_prop_get_integer(zv->zv_name, 1652 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 1653 } 1654 1655 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1656 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 1657 &compress, tx); 1658 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1659 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 1660 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1661 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1662 &refresrv, tx); 1663 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1664 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 1665 &vbs, tx); 1666 error = error ? error : dmu_object_set_blocksize( 1667 os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx); 1668 if (version >= SPA_VERSION_DEDUP) { 1669 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1670 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 1671 &dedup, tx); 1672 } 1673 if (error == 0) 1674 zv->zv_volblocksize = SPA_MAXBLOCKSIZE; 1675 } 1676 dmu_tx_commit(tx); 1677 1678 /* 1679 * We only need update the zvol's property if we are initializing 1680 * the dump area for the first time. 1681 */ 1682 if (!resize) { 1683 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1684 VERIFY(nvlist_add_uint64(nv, 1685 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 1686 VERIFY(nvlist_add_uint64(nv, 1687 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 1688 ZIO_COMPRESS_OFF) == 0); 1689 VERIFY(nvlist_add_uint64(nv, 1690 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 1691 ZIO_CHECKSUM_OFF) == 0); 1692 if (version >= SPA_VERSION_DEDUP) { 1693 VERIFY(nvlist_add_uint64(nv, 1694 zfs_prop_to_name(ZFS_PROP_DEDUP), 1695 ZIO_CHECKSUM_OFF) == 0); 1696 } 1697 1698 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 1699 nv, NULL); 1700 nvlist_free(nv); 1701 1702 if (error) 1703 return (error); 1704 } 1705 1706 /* Allocate the space for the dump */ 1707 error = zvol_prealloc(zv); 1708 return (error); 1709 } 1710 1711 static int 1712 zvol_dumpify(zvol_state_t *zv) 1713 { 1714 int error = 0; 1715 uint64_t dumpsize = 0; 1716 dmu_tx_t *tx; 1717 objset_t *os = zv->zv_objset; 1718 1719 if (zv->zv_flags & ZVOL_RDONLY) 1720 return (EROFS); 1721 1722 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 1723 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 1724 boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE; 1725 1726 if ((error = zvol_dump_init(zv, resize)) != 0) { 1727 (void) zvol_dump_fini(zv); 1728 return (error); 1729 } 1730 } 1731 1732 /* 1733 * Build up our lba mapping. 1734 */ 1735 error = zvol_get_lbas(zv); 1736 if (error) { 1737 (void) zvol_dump_fini(zv); 1738 return (error); 1739 } 1740 1741 tx = dmu_tx_create(os); 1742 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1743 error = dmu_tx_assign(tx, TXG_WAIT); 1744 if (error) { 1745 dmu_tx_abort(tx); 1746 (void) zvol_dump_fini(zv); 1747 return (error); 1748 } 1749 1750 zv->zv_flags |= ZVOL_DUMPIFIED; 1751 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 1752 &zv->zv_volsize, tx); 1753 dmu_tx_commit(tx); 1754 1755 if (error) { 1756 (void) zvol_dump_fini(zv); 1757 return (error); 1758 } 1759 1760 txg_wait_synced(dmu_objset_pool(os), 0); 1761 return (0); 1762 } 1763 1764 static int 1765 zvol_dump_fini(zvol_state_t *zv) 1766 { 1767 dmu_tx_t *tx; 1768 objset_t *os = zv->zv_objset; 1769 nvlist_t *nv; 1770 int error = 0; 1771 uint64_t checksum, compress, refresrv, vbs, dedup; 1772 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 1773 1774 /* 1775 * Attempt to restore the zvol back to its pre-dumpified state. 1776 * This is a best-effort attempt as it's possible that not all 1777 * of these properties were initialized during the dumpify process 1778 * (i.e. error during zvol_dump_init). 1779 */ 1780 1781 tx = dmu_tx_create(os); 1782 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1783 error = dmu_tx_assign(tx, TXG_WAIT); 1784 if (error) { 1785 dmu_tx_abort(tx); 1786 return (error); 1787 } 1788 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 1789 dmu_tx_commit(tx); 1790 1791 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1792 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 1793 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1794 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 1795 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1796 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 1797 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1798 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 1799 1800 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1801 (void) nvlist_add_uint64(nv, 1802 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 1803 (void) nvlist_add_uint64(nv, 1804 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 1805 (void) nvlist_add_uint64(nv, 1806 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 1807 if (version >= SPA_VERSION_DEDUP && 1808 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1809 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 1810 (void) nvlist_add_uint64(nv, 1811 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 1812 } 1813 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 1814 nv, NULL); 1815 nvlist_free(nv); 1816 1817 zvol_free_extents(zv); 1818 zv->zv_flags &= ~ZVOL_DUMPIFIED; 1819 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 1820 /* wait for dmu_free_long_range to actually free the blocks */ 1821 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1822 tx = dmu_tx_create(os); 1823 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1824 error = dmu_tx_assign(tx, TXG_WAIT); 1825 if (error) { 1826 dmu_tx_abort(tx); 1827 return (error); 1828 } 1829 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 1830 zv->zv_volblocksize = vbs; 1831 dmu_tx_commit(tx); 1832 1833 return (0); 1834 } 1835