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