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