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