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