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 if (error == 0) { 954 zgd->zgd_db = db; 955 zgd->zgd_bp = bp; 956 957 ASSERT(db->db_offset == offset); 958 ASSERT(db->db_size == size); 959 960 error = dmu_sync(zio, lr->lr_common.lrc_txg, 961 zvol_get_done, zgd); 962 963 if (error == 0) 964 return (0); 965 } 966 } 967 968 zvol_get_done(zgd, error); 969 970 return (error); 971 } 972 973 /* 974 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 975 * 976 * We store data in the log buffers if it's small enough. 977 * Otherwise we will later flush the data out via dmu_sync(). 978 */ 979 ssize_t zvol_immediate_write_sz = 32768; 980 981 static void 982 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 983 boolean_t sync) 984 { 985 uint32_t blocksize = zv->zv_volblocksize; 986 zilog_t *zilog = zv->zv_zilog; 987 boolean_t slogging; 988 ssize_t immediate_write_sz; 989 990 if (zil_disable) 991 return; 992 993 if (zil_replaying(zilog, tx)) 994 return; 995 996 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 997 ? 0 : zvol_immediate_write_sz; 998 999 slogging = spa_has_slogs(zilog->zl_spa) && 1000 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 1001 1002 while (resid) { 1003 itx_t *itx; 1004 lr_write_t *lr; 1005 ssize_t len; 1006 itx_wr_state_t write_state; 1007 1008 /* 1009 * Unlike zfs_log_write() we can be called with 1010 * upto DMU_MAX_ACCESS/2 (5MB) writes. 1011 */ 1012 if (blocksize > immediate_write_sz && !slogging && 1013 resid >= blocksize && off % blocksize == 0) { 1014 write_state = WR_INDIRECT; /* uses dmu_sync */ 1015 len = blocksize; 1016 } else if (sync) { 1017 write_state = WR_COPIED; 1018 len = MIN(ZIL_MAX_LOG_DATA, resid); 1019 } else { 1020 write_state = WR_NEED_COPY; 1021 len = MIN(ZIL_MAX_LOG_DATA, resid); 1022 } 1023 1024 itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1025 (write_state == WR_COPIED ? len : 0)); 1026 lr = (lr_write_t *)&itx->itx_lr; 1027 if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 1028 ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1029 zil_itx_destroy(itx); 1030 itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1031 lr = (lr_write_t *)&itx->itx_lr; 1032 write_state = WR_NEED_COPY; 1033 } 1034 1035 itx->itx_wr_state = write_state; 1036 if (write_state == WR_NEED_COPY) 1037 itx->itx_sod += len; 1038 lr->lr_foid = ZVOL_OBJ; 1039 lr->lr_offset = off; 1040 lr->lr_length = len; 1041 lr->lr_blkoff = 0; 1042 BP_ZERO(&lr->lr_blkptr); 1043 1044 itx->itx_private = zv; 1045 itx->itx_sync = sync; 1046 1047 (void) zil_itx_assign(zilog, itx, tx); 1048 1049 off += len; 1050 resid -= len; 1051 } 1052 } 1053 1054 static int 1055 zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size, 1056 boolean_t doread, boolean_t isdump) 1057 { 1058 vdev_disk_t *dvd; 1059 int c; 1060 int numerrors = 0; 1061 1062 for (c = 0; c < vd->vdev_children; c++) { 1063 ASSERT(vd->vdev_ops == &vdev_mirror_ops || 1064 vd->vdev_ops == &vdev_replacing_ops || 1065 vd->vdev_ops == &vdev_spare_ops); 1066 int err = zvol_dumpio_vdev(vd->vdev_child[c], 1067 addr, offset, size, doread, isdump); 1068 if (err != 0) { 1069 numerrors++; 1070 } else if (doread) { 1071 break; 1072 } 1073 } 1074 1075 if (!vd->vdev_ops->vdev_op_leaf) 1076 return (numerrors < vd->vdev_children ? 0 : EIO); 1077 1078 if (doread && !vdev_readable(vd)) 1079 return (EIO); 1080 else if (!doread && !vdev_writeable(vd)) 1081 return (EIO); 1082 1083 dvd = vd->vdev_tsd; 1084 ASSERT3P(dvd, !=, NULL); 1085 offset += VDEV_LABEL_START_SIZE; 1086 1087 if (ddi_in_panic() || isdump) { 1088 ASSERT(!doread); 1089 if (doread) 1090 return (EIO); 1091 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1092 lbtodb(size))); 1093 } else { 1094 return (vdev_disk_physio(dvd->vd_lh, addr, size, offset, 1095 doread ? B_READ : B_WRITE)); 1096 } 1097 } 1098 1099 static int 1100 zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 1101 boolean_t doread, boolean_t isdump) 1102 { 1103 vdev_t *vd; 1104 int error; 1105 zvol_extent_t *ze; 1106 spa_t *spa = dmu_objset_spa(zv->zv_objset); 1107 1108 /* Must be sector aligned, and not stradle a block boundary. */ 1109 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 1110 P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 1111 return (EINVAL); 1112 } 1113 ASSERT(size <= zv->zv_volblocksize); 1114 1115 /* Locate the extent this belongs to */ 1116 ze = list_head(&zv->zv_extents); 1117 while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 1118 offset -= ze->ze_nblks * zv->zv_volblocksize; 1119 ze = list_next(&zv->zv_extents, ze); 1120 } 1121 1122 if (!ddi_in_panic()) 1123 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 1124 1125 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 1126 offset += DVA_GET_OFFSET(&ze->ze_dva); 1127 error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump); 1128 1129 if (!ddi_in_panic()) 1130 spa_config_exit(spa, SCL_STATE, FTAG); 1131 1132 return (error); 1133 } 1134 1135 int 1136 zvol_strategy(buf_t *bp) 1137 { 1138 zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev)); 1139 uint64_t off, volsize; 1140 size_t resid; 1141 char *addr; 1142 objset_t *os; 1143 rl_t *rl; 1144 int error = 0; 1145 boolean_t doread = bp->b_flags & B_READ; 1146 boolean_t is_dump; 1147 boolean_t sync; 1148 1149 if (zv == NULL) { 1150 bioerror(bp, ENXIO); 1151 biodone(bp); 1152 return (0); 1153 } 1154 1155 if (getminor(bp->b_edev) == 0) { 1156 bioerror(bp, EINVAL); 1157 biodone(bp); 1158 return (0); 1159 } 1160 1161 if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) { 1162 bioerror(bp, EROFS); 1163 biodone(bp); 1164 return (0); 1165 } 1166 1167 off = ldbtob(bp->b_blkno); 1168 volsize = zv->zv_volsize; 1169 1170 os = zv->zv_objset; 1171 ASSERT(os != NULL); 1172 1173 bp_mapin(bp); 1174 addr = bp->b_un.b_addr; 1175 resid = bp->b_bcount; 1176 1177 if (resid > 0 && (off < 0 || off >= volsize)) { 1178 bioerror(bp, EIO); 1179 biodone(bp); 1180 return (0); 1181 } 1182 1183 is_dump = zv->zv_flags & ZVOL_DUMPIFIED; 1184 sync = !(bp->b_flags & B_ASYNC) && !doread && !is_dump && 1185 !(zv->zv_flags & ZVOL_WCE) && !zil_disable; 1186 1187 /* 1188 * There must be no buffer changes when doing a dmu_sync() because 1189 * we can't change the data whilst calculating the checksum. 1190 */ 1191 rl = zfs_range_lock(&zv->zv_znode, off, resid, 1192 doread ? RL_READER : RL_WRITER); 1193 1194 while (resid != 0 && off < volsize) { 1195 size_t size = MIN(resid, zvol_maxphys); 1196 if (is_dump) { 1197 size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 1198 error = zvol_dumpio(zv, addr, off, size, 1199 doread, B_FALSE); 1200 } else if (doread) { 1201 error = dmu_read(os, ZVOL_OBJ, off, size, addr, 1202 DMU_READ_PREFETCH); 1203 } else { 1204 dmu_tx_t *tx = dmu_tx_create(os); 1205 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1206 error = dmu_tx_assign(tx, TXG_WAIT); 1207 if (error) { 1208 dmu_tx_abort(tx); 1209 } else { 1210 dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1211 zvol_log_write(zv, tx, off, size, sync); 1212 dmu_tx_commit(tx); 1213 } 1214 } 1215 if (error) { 1216 /* convert checksum errors into IO errors */ 1217 if (error == ECKSUM) 1218 error = EIO; 1219 break; 1220 } 1221 off += size; 1222 addr += size; 1223 resid -= size; 1224 } 1225 zfs_range_unlock(rl); 1226 1227 if ((bp->b_resid = resid) == bp->b_bcount) 1228 bioerror(bp, off > volsize ? EINVAL : error); 1229 1230 if (sync) 1231 zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1232 biodone(bp); 1233 1234 return (0); 1235 } 1236 1237 /* 1238 * Set the buffer count to the zvol maximum transfer. 1239 * Using our own routine instead of the default minphys() 1240 * means that for larger writes we write bigger buffers on X86 1241 * (128K instead of 56K) and flush the disk write cache less often 1242 * (every zvol_maxphys - currently 1MB) instead of minphys (currently 1243 * 56K on X86 and 128K on sparc). 1244 */ 1245 void 1246 zvol_minphys(struct buf *bp) 1247 { 1248 if (bp->b_bcount > zvol_maxphys) 1249 bp->b_bcount = zvol_maxphys; 1250 } 1251 1252 int 1253 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1254 { 1255 minor_t minor = getminor(dev); 1256 zvol_state_t *zv; 1257 int error = 0; 1258 uint64_t size; 1259 uint64_t boff; 1260 uint64_t resid; 1261 1262 if (minor == 0) /* This is the control device */ 1263 return (ENXIO); 1264 1265 zv = ddi_get_soft_state(zvol_state, minor); 1266 if (zv == NULL) 1267 return (ENXIO); 1268 1269 boff = ldbtob(blkno); 1270 resid = ldbtob(nblocks); 1271 1272 VERIFY3U(boff + resid, <=, zv->zv_volsize); 1273 1274 while (resid) { 1275 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 1276 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1277 if (error) 1278 break; 1279 boff += size; 1280 addr += size; 1281 resid -= size; 1282 } 1283 1284 return (error); 1285 } 1286 1287 /*ARGSUSED*/ 1288 int 1289 zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1290 { 1291 minor_t minor = getminor(dev); 1292 zvol_state_t *zv; 1293 uint64_t volsize; 1294 rl_t *rl; 1295 int error = 0; 1296 1297 if (minor == 0) /* This is the control device */ 1298 return (ENXIO); 1299 1300 zv = ddi_get_soft_state(zvol_state, minor); 1301 if (zv == NULL) 1302 return (ENXIO); 1303 1304 volsize = zv->zv_volsize; 1305 if (uio->uio_resid > 0 && 1306 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 1307 return (EIO); 1308 1309 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1310 error = physio(zvol_strategy, NULL, dev, B_READ, 1311 zvol_minphys, uio); 1312 return (error); 1313 } 1314 1315 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1316 RL_READER); 1317 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1318 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1319 1320 /* don't read past the end */ 1321 if (bytes > volsize - uio->uio_loffset) 1322 bytes = volsize - uio->uio_loffset; 1323 1324 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1325 if (error) { 1326 /* convert checksum errors into IO errors */ 1327 if (error == ECKSUM) 1328 error = EIO; 1329 break; 1330 } 1331 } 1332 zfs_range_unlock(rl); 1333 return (error); 1334 } 1335 1336 /*ARGSUSED*/ 1337 int 1338 zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1339 { 1340 minor_t minor = getminor(dev); 1341 zvol_state_t *zv; 1342 uint64_t volsize; 1343 rl_t *rl; 1344 int error = 0; 1345 boolean_t sync; 1346 1347 if (minor == 0) /* This is the control device */ 1348 return (ENXIO); 1349 1350 zv = ddi_get_soft_state(zvol_state, minor); 1351 if (zv == NULL) 1352 return (ENXIO); 1353 1354 volsize = zv->zv_volsize; 1355 if (uio->uio_resid > 0 && 1356 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 1357 return (EIO); 1358 1359 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1360 error = physio(zvol_strategy, NULL, dev, B_WRITE, 1361 zvol_minphys, uio); 1362 return (error); 1363 } 1364 1365 sync = !(zv->zv_flags & ZVOL_WCE) && !zil_disable; 1366 1367 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1368 RL_WRITER); 1369 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1370 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1371 uint64_t off = uio->uio_loffset; 1372 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 1373 1374 if (bytes > volsize - off) /* don't write past the end */ 1375 bytes = volsize - off; 1376 1377 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1378 error = dmu_tx_assign(tx, TXG_WAIT); 1379 if (error) { 1380 dmu_tx_abort(tx); 1381 break; 1382 } 1383 error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx); 1384 if (error == 0) 1385 zvol_log_write(zv, tx, off, bytes, sync); 1386 dmu_tx_commit(tx); 1387 1388 if (error) 1389 break; 1390 } 1391 zfs_range_unlock(rl); 1392 if (sync) 1393 zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1394 return (error); 1395 } 1396 1397 int 1398 zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1399 { 1400 struct uuid uuid = EFI_RESERVED; 1401 efi_gpe_t gpe = { 0 }; 1402 uint32_t crc; 1403 dk_efi_t efi; 1404 int length; 1405 char *ptr; 1406 1407 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1408 return (EFAULT); 1409 ptr = (char *)(uintptr_t)efi.dki_data_64; 1410 length = efi.dki_length; 1411 /* 1412 * Some clients may attempt to request a PMBR for the 1413 * zvol. Currently this interface will return EINVAL to 1414 * such requests. These requests could be supported by 1415 * adding a check for lba == 0 and consing up an appropriate 1416 * PMBR. 1417 */ 1418 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1419 return (EINVAL); 1420 1421 gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1422 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1423 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1424 1425 if (efi.dki_lba == 1) { 1426 efi_gpt_t gpt = { 0 }; 1427 1428 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1429 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1430 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1431 gpt.efi_gpt_MyLBA = LE_64(1ULL); 1432 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1433 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1434 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1435 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1436 gpt.efi_gpt_SizeOfPartitionEntry = 1437 LE_32(sizeof (efi_gpe_t)); 1438 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1439 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1440 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1441 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1442 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1443 flag)) 1444 return (EFAULT); 1445 ptr += sizeof (gpt); 1446 length -= sizeof (gpt); 1447 } 1448 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1449 length), flag)) 1450 return (EFAULT); 1451 return (0); 1452 } 1453 1454 /* 1455 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1456 */ 1457 /*ARGSUSED*/ 1458 int 1459 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1460 { 1461 zvol_state_t *zv; 1462 struct dk_cinfo dki; 1463 struct dk_minfo dkm; 1464 struct dk_callback *dkc; 1465 int error = 0; 1466 rl_t *rl; 1467 1468 mutex_enter(&zvol_state_lock); 1469 1470 zv = ddi_get_soft_state(zvol_state, getminor(dev)); 1471 1472 if (zv == NULL) { 1473 mutex_exit(&zvol_state_lock); 1474 return (ENXIO); 1475 } 1476 ASSERT(zv->zv_total_opens > 0); 1477 1478 switch (cmd) { 1479 1480 case DKIOCINFO: 1481 bzero(&dki, sizeof (dki)); 1482 (void) strcpy(dki.dki_cname, "zvol"); 1483 (void) strcpy(dki.dki_dname, "zvol"); 1484 dki.dki_ctype = DKC_UNKNOWN; 1485 dki.dki_unit = getminor(dev); 1486 dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1487 mutex_exit(&zvol_state_lock); 1488 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1489 error = EFAULT; 1490 return (error); 1491 1492 case DKIOCGMEDIAINFO: 1493 bzero(&dkm, sizeof (dkm)); 1494 dkm.dki_lbsize = 1U << zv->zv_min_bs; 1495 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1496 dkm.dki_media_type = DK_UNKNOWN; 1497 mutex_exit(&zvol_state_lock); 1498 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1499 error = EFAULT; 1500 return (error); 1501 1502 case DKIOCGETEFI: 1503 { 1504 uint64_t vs = zv->zv_volsize; 1505 uint8_t bs = zv->zv_min_bs; 1506 1507 mutex_exit(&zvol_state_lock); 1508 error = zvol_getefi((void *)arg, flag, vs, bs); 1509 return (error); 1510 } 1511 1512 case DKIOCFLUSHWRITECACHE: 1513 dkc = (struct dk_callback *)arg; 1514 mutex_exit(&zvol_state_lock); 1515 zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1516 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1517 (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1518 error = 0; 1519 } 1520 return (error); 1521 1522 case DKIOCGETWCE: 1523 { 1524 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1525 if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1526 flag)) 1527 error = EFAULT; 1528 break; 1529 } 1530 case DKIOCSETWCE: 1531 { 1532 int wce; 1533 if (ddi_copyin((void *)arg, &wce, sizeof (int), 1534 flag)) { 1535 error = EFAULT; 1536 break; 1537 } 1538 if (wce) { 1539 zv->zv_flags |= ZVOL_WCE; 1540 mutex_exit(&zvol_state_lock); 1541 } else { 1542 zv->zv_flags &= ~ZVOL_WCE; 1543 mutex_exit(&zvol_state_lock); 1544 zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1545 } 1546 return (0); 1547 } 1548 1549 case DKIOCGGEOM: 1550 case DKIOCGVTOC: 1551 /* 1552 * commands using these (like prtvtoc) expect ENOTSUP 1553 * since we're emulating an EFI label 1554 */ 1555 error = ENOTSUP; 1556 break; 1557 1558 case DKIOCDUMPINIT: 1559 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1560 RL_WRITER); 1561 error = zvol_dumpify(zv); 1562 zfs_range_unlock(rl); 1563 break; 1564 1565 case DKIOCDUMPFINI: 1566 if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 1567 break; 1568 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1569 RL_WRITER); 1570 error = zvol_dump_fini(zv); 1571 zfs_range_unlock(rl); 1572 break; 1573 1574 default: 1575 error = ENOTTY; 1576 break; 1577 1578 } 1579 mutex_exit(&zvol_state_lock); 1580 return (error); 1581 } 1582 1583 int 1584 zvol_busy(void) 1585 { 1586 return (zvol_minors != 0); 1587 } 1588 1589 void 1590 zvol_init(void) 1591 { 1592 VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0); 1593 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL); 1594 } 1595 1596 void 1597 zvol_fini(void) 1598 { 1599 mutex_destroy(&zvol_state_lock); 1600 ddi_soft_state_fini(&zvol_state); 1601 } 1602 1603 static int 1604 zvol_dump_init(zvol_state_t *zv, boolean_t resize) 1605 { 1606 dmu_tx_t *tx; 1607 int error = 0; 1608 objset_t *os = zv->zv_objset; 1609 nvlist_t *nv = NULL; 1610 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 1611 1612 ASSERT(MUTEX_HELD(&zvol_state_lock)); 1613 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 1614 DMU_OBJECT_END); 1615 /* wait for dmu_free_long_range to actually free the blocks */ 1616 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1617 1618 tx = dmu_tx_create(os); 1619 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1620 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1621 error = dmu_tx_assign(tx, TXG_WAIT); 1622 if (error) { 1623 dmu_tx_abort(tx); 1624 return (error); 1625 } 1626 1627 /* 1628 * If we are resizing the dump device then we only need to 1629 * update the refreservation to match the newly updated 1630 * zvolsize. Otherwise, we save off the original state of the 1631 * zvol so that we can restore them if the zvol is ever undumpified. 1632 */ 1633 if (resize) { 1634 error = zap_update(os, ZVOL_ZAP_OBJ, 1635 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1636 &zv->zv_volsize, tx); 1637 } else { 1638 uint64_t checksum, compress, refresrv, vbs, dedup; 1639 1640 error = dsl_prop_get_integer(zv->zv_name, 1641 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 1642 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1643 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 1644 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1645 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 1646 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1647 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 1648 if (version >= SPA_VERSION_DEDUP) { 1649 error = error ? error : 1650 dsl_prop_get_integer(zv->zv_name, 1651 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 1652 } 1653 1654 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1655 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 1656 &compress, tx); 1657 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1658 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 1659 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1660 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1661 &refresrv, tx); 1662 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1663 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 1664 &vbs, tx); 1665 error = error ? error : dmu_object_set_blocksize( 1666 os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx); 1667 if (version >= SPA_VERSION_DEDUP) { 1668 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1669 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 1670 &dedup, tx); 1671 } 1672 if (error == 0) 1673 zv->zv_volblocksize = SPA_MAXBLOCKSIZE; 1674 } 1675 dmu_tx_commit(tx); 1676 1677 /* 1678 * We only need update the zvol's property if we are initializing 1679 * the dump area for the first time. 1680 */ 1681 if (!resize) { 1682 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1683 VERIFY(nvlist_add_uint64(nv, 1684 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 1685 VERIFY(nvlist_add_uint64(nv, 1686 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 1687 ZIO_COMPRESS_OFF) == 0); 1688 VERIFY(nvlist_add_uint64(nv, 1689 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 1690 ZIO_CHECKSUM_OFF) == 0); 1691 if (version >= SPA_VERSION_DEDUP) { 1692 VERIFY(nvlist_add_uint64(nv, 1693 zfs_prop_to_name(ZFS_PROP_DEDUP), 1694 ZIO_CHECKSUM_OFF) == 0); 1695 } 1696 1697 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 1698 nv, NULL); 1699 nvlist_free(nv); 1700 1701 if (error) 1702 return (error); 1703 } 1704 1705 /* Allocate the space for the dump */ 1706 error = zvol_prealloc(zv); 1707 return (error); 1708 } 1709 1710 static int 1711 zvol_dumpify(zvol_state_t *zv) 1712 { 1713 int error = 0; 1714 uint64_t dumpsize = 0; 1715 dmu_tx_t *tx; 1716 objset_t *os = zv->zv_objset; 1717 1718 if (zv->zv_flags & ZVOL_RDONLY) 1719 return (EROFS); 1720 1721 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 1722 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 1723 boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE; 1724 1725 if ((error = zvol_dump_init(zv, resize)) != 0) { 1726 (void) zvol_dump_fini(zv); 1727 return (error); 1728 } 1729 } 1730 1731 /* 1732 * Build up our lba mapping. 1733 */ 1734 error = zvol_get_lbas(zv); 1735 if (error) { 1736 (void) zvol_dump_fini(zv); 1737 return (error); 1738 } 1739 1740 tx = dmu_tx_create(os); 1741 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1742 error = dmu_tx_assign(tx, TXG_WAIT); 1743 if (error) { 1744 dmu_tx_abort(tx); 1745 (void) zvol_dump_fini(zv); 1746 return (error); 1747 } 1748 1749 zv->zv_flags |= ZVOL_DUMPIFIED; 1750 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 1751 &zv->zv_volsize, tx); 1752 dmu_tx_commit(tx); 1753 1754 if (error) { 1755 (void) zvol_dump_fini(zv); 1756 return (error); 1757 } 1758 1759 txg_wait_synced(dmu_objset_pool(os), 0); 1760 return (0); 1761 } 1762 1763 static int 1764 zvol_dump_fini(zvol_state_t *zv) 1765 { 1766 dmu_tx_t *tx; 1767 objset_t *os = zv->zv_objset; 1768 nvlist_t *nv; 1769 int error = 0; 1770 uint64_t checksum, compress, refresrv, vbs, dedup; 1771 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 1772 1773 /* 1774 * Attempt to restore the zvol back to its pre-dumpified state. 1775 * This is a best-effort attempt as it's possible that not all 1776 * of these properties were initialized during the dumpify process 1777 * (i.e. error during zvol_dump_init). 1778 */ 1779 1780 tx = dmu_tx_create(os); 1781 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1782 error = dmu_tx_assign(tx, TXG_WAIT); 1783 if (error) { 1784 dmu_tx_abort(tx); 1785 return (error); 1786 } 1787 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 1788 dmu_tx_commit(tx); 1789 1790 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1791 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 1792 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1793 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 1794 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1795 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 1796 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1797 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 1798 1799 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1800 (void) nvlist_add_uint64(nv, 1801 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 1802 (void) nvlist_add_uint64(nv, 1803 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 1804 (void) nvlist_add_uint64(nv, 1805 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 1806 if (version >= SPA_VERSION_DEDUP && 1807 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1808 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 1809 (void) nvlist_add_uint64(nv, 1810 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 1811 } 1812 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 1813 nv, NULL); 1814 nvlist_free(nv); 1815 1816 zvol_free_extents(zv); 1817 zv->zv_flags &= ~ZVOL_DUMPIFIED; 1818 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 1819 /* wait for dmu_free_long_range to actually free the blocks */ 1820 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1821 tx = dmu_tx_create(os); 1822 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1823 error = dmu_tx_assign(tx, TXG_WAIT); 1824 if (error) { 1825 dmu_tx_abort(tx); 1826 return (error); 1827 } 1828 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 1829 zv->zv_volblocksize = vbs; 1830 dmu_tx_commit(tx); 1831 1832 return (0); 1833 } 1834