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_PRIMARY; 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 static int 778 zvol_truncate(zvol_state_t *zv, uint64_t offset, uint64_t size) 779 { 780 dmu_tx_t *tx; 781 int error; 782 783 tx = dmu_tx_create(zv->zv_objset); 784 dmu_tx_hold_free(tx, ZVOL_OBJ, offset, size); 785 error = dmu_tx_assign(tx, TXG_WAIT); 786 if (error) { 787 dmu_tx_abort(tx); 788 return (error); 789 } 790 error = dmu_free_range(zv->zv_objset, ZVOL_OBJ, offset, size, tx); 791 dmu_tx_commit(tx); 792 return (0); 793 } 794 795 int 796 zvol_prealloc(zvol_state_t *zv) 797 { 798 objset_t *os = zv->zv_objset; 799 dmu_tx_t *tx; 800 void *data; 801 uint64_t refd, avail, usedobjs, availobjs; 802 uint64_t resid = zv->zv_volsize; 803 uint64_t off = 0; 804 805 /* Check the space usage before attempting to allocate the space */ 806 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 807 if (avail < zv->zv_volsize) 808 return (ENOSPC); 809 810 /* Free old extents if they exist */ 811 zvol_free_extents(zv); 812 813 /* allocate the blocks by writing each one */ 814 data = kmem_zalloc(SPA_MAXBLOCKSIZE, KM_SLEEP); 815 816 while (resid != 0) { 817 int error; 818 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 819 820 tx = dmu_tx_create(os); 821 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 822 error = dmu_tx_assign(tx, TXG_WAIT); 823 if (error) { 824 dmu_tx_abort(tx); 825 kmem_free(data, SPA_MAXBLOCKSIZE); 826 (void) zvol_truncate(zv, 0, off); 827 return (error); 828 } 829 dmu_write(os, ZVOL_OBJ, off, bytes, data, tx); 830 dmu_tx_commit(tx); 831 off += bytes; 832 resid -= bytes; 833 } 834 kmem_free(data, SPA_MAXBLOCKSIZE); 835 txg_wait_synced(dmu_objset_pool(os), 0); 836 837 return (0); 838 } 839 840 int 841 zvol_update_volsize(zvol_state_t *zv, major_t maj, uint64_t volsize) 842 { 843 dmu_tx_t *tx; 844 int error; 845 846 ASSERT(MUTEX_HELD(&zvol_state_lock)); 847 848 tx = dmu_tx_create(zv->zv_objset); 849 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 850 dmu_tx_hold_free(tx, ZVOL_OBJ, volsize, DMU_OBJECT_END); 851 error = dmu_tx_assign(tx, TXG_WAIT); 852 if (error) { 853 dmu_tx_abort(tx); 854 return (error); 855 } 856 857 error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1, 858 &volsize, tx); 859 dmu_tx_commit(tx); 860 861 if (error == 0) 862 error = zvol_truncate(zv, volsize, DMU_OBJECT_END); 863 864 if (error == 0) { 865 zv->zv_volsize = volsize; 866 zvol_size_changed(zv, maj); 867 } 868 return (error); 869 } 870 871 int 872 zvol_set_volsize(const char *name, major_t maj, uint64_t volsize) 873 { 874 zvol_state_t *zv; 875 int error; 876 dmu_object_info_t doi; 877 uint64_t old_volsize = 0ULL; 878 879 mutex_enter(&zvol_state_lock); 880 881 if ((zv = zvol_minor_lookup(name)) == NULL) { 882 mutex_exit(&zvol_state_lock); 883 return (ENXIO); 884 } 885 old_volsize = zv->zv_volsize; 886 887 if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 || 888 (error = zvol_check_volsize(volsize, 889 doi.doi_data_block_size)) != 0) { 890 mutex_exit(&zvol_state_lock); 891 return (error); 892 } 893 894 if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) { 895 mutex_exit(&zvol_state_lock); 896 return (EROFS); 897 } 898 899 error = zvol_update_volsize(zv, maj, volsize); 900 901 /* 902 * Reinitialize the dump area to the new size. If we 903 * failed to resize the dump area then restore the it back to 904 * it's original size. 905 */ 906 if (error == 0 && zv->zv_flags & ZVOL_DUMPIFIED) { 907 if ((error = zvol_dumpify(zv)) != 0 || 908 (error = dumpvp_resize()) != 0) { 909 (void) zvol_update_volsize(zv, maj, old_volsize); 910 error = zvol_dumpify(zv); 911 } 912 } 913 914 mutex_exit(&zvol_state_lock); 915 916 return (error); 917 } 918 919 int 920 zvol_set_volblocksize(const char *name, uint64_t volblocksize) 921 { 922 zvol_state_t *zv; 923 dmu_tx_t *tx; 924 int error; 925 926 mutex_enter(&zvol_state_lock); 927 928 if ((zv = zvol_minor_lookup(name)) == NULL) { 929 mutex_exit(&zvol_state_lock); 930 return (ENXIO); 931 } 932 if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) { 933 mutex_exit(&zvol_state_lock); 934 return (EROFS); 935 } 936 937 tx = dmu_tx_create(zv->zv_objset); 938 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 939 error = dmu_tx_assign(tx, TXG_WAIT); 940 if (error) { 941 dmu_tx_abort(tx); 942 } else { 943 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ, 944 volblocksize, 0, tx); 945 if (error == ENOTSUP) 946 error = EBUSY; 947 dmu_tx_commit(tx); 948 } 949 950 mutex_exit(&zvol_state_lock); 951 952 return (error); 953 } 954 955 /*ARGSUSED*/ 956 int 957 zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 958 { 959 minor_t minor = getminor(*devp); 960 zvol_state_t *zv; 961 962 if (minor == 0) /* This is the control device */ 963 return (0); 964 965 mutex_enter(&zvol_state_lock); 966 967 zv = ddi_get_soft_state(zvol_state, minor); 968 if (zv == NULL) { 969 mutex_exit(&zvol_state_lock); 970 return (ENXIO); 971 } 972 973 ASSERT(zv->zv_objset != NULL); 974 975 if ((flag & FWRITE) && 976 (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))) { 977 mutex_exit(&zvol_state_lock); 978 return (EROFS); 979 } 980 981 if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 982 zv->zv_open_count[otyp]++; 983 zv->zv_total_opens++; 984 } 985 986 mutex_exit(&zvol_state_lock); 987 988 return (0); 989 } 990 991 /*ARGSUSED*/ 992 int 993 zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 994 { 995 minor_t minor = getminor(dev); 996 zvol_state_t *zv; 997 998 if (minor == 0) /* This is the control device */ 999 return (0); 1000 1001 mutex_enter(&zvol_state_lock); 1002 1003 zv = ddi_get_soft_state(zvol_state, minor); 1004 if (zv == NULL) { 1005 mutex_exit(&zvol_state_lock); 1006 return (ENXIO); 1007 } 1008 1009 /* 1010 * The next statement is a workaround for the following DDI bug: 1011 * 6343604 specfs race: multiple "last-close" of the same device 1012 */ 1013 if (zv->zv_total_opens == 0) { 1014 mutex_exit(&zvol_state_lock); 1015 return (0); 1016 } 1017 1018 /* 1019 * If the open count is zero, this is a spurious close. 1020 * That indicates a bug in the kernel / DDI framework. 1021 */ 1022 ASSERT(zv->zv_open_count[otyp] != 0); 1023 ASSERT(zv->zv_total_opens != 0); 1024 1025 /* 1026 * You may get multiple opens, but only one close. 1027 */ 1028 zv->zv_open_count[otyp]--; 1029 zv->zv_total_opens--; 1030 1031 mutex_exit(&zvol_state_lock); 1032 1033 return (0); 1034 } 1035 1036 static void 1037 zvol_get_done(dmu_buf_t *db, void *vzgd) 1038 { 1039 zgd_t *zgd = (zgd_t *)vzgd; 1040 rl_t *rl = zgd->zgd_rl; 1041 1042 dmu_buf_rele(db, vzgd); 1043 zfs_range_unlock(rl); 1044 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 1045 kmem_free(zgd, sizeof (zgd_t)); 1046 } 1047 1048 /* 1049 * Get data to generate a TX_WRITE intent log record. 1050 */ 1051 static int 1052 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 1053 { 1054 zvol_state_t *zv = arg; 1055 objset_t *os = zv->zv_objset; 1056 dmu_buf_t *db; 1057 rl_t *rl; 1058 zgd_t *zgd; 1059 uint64_t boff; /* block starting offset */ 1060 int dlen = lr->lr_length; /* length of user data */ 1061 int error; 1062 1063 ASSERT(zio); 1064 ASSERT(dlen != 0); 1065 1066 /* 1067 * Write records come in two flavors: immediate and indirect. 1068 * For small writes it's cheaper to store the data with the 1069 * log record (immediate); for large writes it's cheaper to 1070 * sync the data and get a pointer to it (indirect) so that 1071 * we don't have to write the data twice. 1072 */ 1073 if (buf != NULL) /* immediate write */ 1074 return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf)); 1075 1076 zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP); 1077 zgd->zgd_zilog = zv->zv_zilog; 1078 zgd->zgd_bp = &lr->lr_blkptr; 1079 1080 /* 1081 * Lock the range of the block to ensure that when the data is 1082 * written out and its checksum is being calculated that no other 1083 * thread can change the block. 1084 */ 1085 boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t); 1086 rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize, 1087 RL_READER); 1088 zgd->zgd_rl = rl; 1089 1090 VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db)); 1091 error = dmu_sync(zio, db, &lr->lr_blkptr, 1092 lr->lr_common.lrc_txg, zvol_get_done, zgd); 1093 if (error == 0) 1094 zil_add_block(zv->zv_zilog, &lr->lr_blkptr); 1095 /* 1096 * If we get EINPROGRESS, then we need to wait for a 1097 * write IO initiated by dmu_sync() to complete before 1098 * we can release this dbuf. We will finish everything 1099 * up in the zvol_get_done() callback. 1100 */ 1101 if (error == EINPROGRESS) 1102 return (0); 1103 dmu_buf_rele(db, zgd); 1104 zfs_range_unlock(rl); 1105 kmem_free(zgd, sizeof (zgd_t)); 1106 return (error); 1107 } 1108 1109 /* 1110 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 1111 * 1112 * We store data in the log buffers if it's small enough. 1113 * Otherwise we will later flush the data out via dmu_sync(). 1114 */ 1115 ssize_t zvol_immediate_write_sz = 32768; 1116 1117 static void 1118 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t len) 1119 { 1120 uint32_t blocksize = zv->zv_volblocksize; 1121 lr_write_t *lr; 1122 1123 while (len) { 1124 ssize_t nbytes = MIN(len, blocksize - P2PHASE(off, blocksize)); 1125 itx_t *itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1126 1127 itx->itx_wr_state = 1128 len > zvol_immediate_write_sz ? WR_INDIRECT : WR_NEED_COPY; 1129 itx->itx_private = zv; 1130 lr = (lr_write_t *)&itx->itx_lr; 1131 lr->lr_foid = ZVOL_OBJ; 1132 lr->lr_offset = off; 1133 lr->lr_length = nbytes; 1134 lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t); 1135 BP_ZERO(&lr->lr_blkptr); 1136 1137 (void) zil_itx_assign(zv->zv_zilog, itx, tx); 1138 len -= nbytes; 1139 off += nbytes; 1140 } 1141 } 1142 1143 int 1144 zvol_dumpio(vdev_t *vd, uint64_t size, uint64_t offset, void *addr, 1145 int bflags, int isdump) 1146 { 1147 vdev_disk_t *dvd; 1148 int direction; 1149 int c; 1150 int numerrors = 0; 1151 1152 for (c = 0; c < vd->vdev_children; c++) { 1153 if (zvol_dumpio(vd->vdev_child[c], size, offset, 1154 addr, bflags, isdump) != 0) { 1155 numerrors++; 1156 } else if (bflags & B_READ) { 1157 break; 1158 } 1159 } 1160 1161 if (!vd->vdev_ops->vdev_op_leaf) 1162 return (numerrors < vd->vdev_children ? 0 : EIO); 1163 1164 if (!vdev_writeable(vd)) 1165 return (EIO); 1166 1167 dvd = vd->vdev_tsd; 1168 ASSERT3P(dvd, !=, NULL); 1169 direction = bflags & (B_WRITE | B_READ); 1170 ASSERT(ISP2(direction)); 1171 offset += VDEV_LABEL_START_SIZE; 1172 1173 if (ddi_in_panic() || isdump) { 1174 if (direction & B_READ) 1175 return (EIO); 1176 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1177 lbtodb(size))); 1178 } else { 1179 return (vdev_disk_physio(dvd->vd_lh, addr, size, offset, 1180 direction)); 1181 } 1182 } 1183 1184 int 1185 zvol_physio(zvol_state_t *zv, int bflags, uint64_t off, 1186 uint64_t size, void *addr, int isdump) 1187 { 1188 dva_t dva; 1189 vdev_t *vd; 1190 int error; 1191 spa_t *spa = dmu_objset_spa(zv->zv_objset); 1192 1193 ASSERT(size <= zv->zv_volblocksize); 1194 1195 /* restrict requests to multiples of the system block size */ 1196 if (P2PHASE(off, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE)) 1197 return (EINVAL); 1198 1199 if (zvol_get_dva(zv, off, &dva) != 0) 1200 return (EIO); 1201 1202 spa_config_enter(spa, RW_READER, FTAG); 1203 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva)); 1204 1205 error = zvol_dumpio(vd, size, 1206 DVA_GET_OFFSET(&dva) + (off % zv->zv_volblocksize), 1207 addr, bflags & (B_READ | B_WRITE | B_PHYS), isdump); 1208 1209 spa_config_exit(spa, FTAG); 1210 return (error); 1211 } 1212 1213 int 1214 zvol_strategy(buf_t *bp) 1215 { 1216 zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev)); 1217 uint64_t off, volsize; 1218 size_t size, resid; 1219 char *addr; 1220 objset_t *os; 1221 rl_t *rl; 1222 int error = 0; 1223 boolean_t reading, is_dump = zv->zv_flags & ZVOL_DUMPIFIED; 1224 1225 if (zv == NULL) { 1226 bioerror(bp, ENXIO); 1227 biodone(bp); 1228 return (0); 1229 } 1230 1231 if (getminor(bp->b_edev) == 0) { 1232 bioerror(bp, EINVAL); 1233 biodone(bp); 1234 return (0); 1235 } 1236 1237 if (!(bp->b_flags & B_READ) && 1238 (zv->zv_flags & ZVOL_RDONLY || 1239 zv->zv_mode & DS_MODE_READONLY)) { 1240 bioerror(bp, EROFS); 1241 biodone(bp); 1242 return (0); 1243 } 1244 1245 off = ldbtob(bp->b_blkno); 1246 volsize = zv->zv_volsize; 1247 1248 os = zv->zv_objset; 1249 ASSERT(os != NULL); 1250 1251 bp_mapin(bp); 1252 addr = bp->b_un.b_addr; 1253 resid = bp->b_bcount; 1254 1255 /* 1256 * There must be no buffer changes when doing a dmu_sync() because 1257 * we can't change the data whilst calculating the checksum. 1258 */ 1259 reading = bp->b_flags & B_READ; 1260 rl = zfs_range_lock(&zv->zv_znode, off, resid, 1261 reading ? RL_READER : RL_WRITER); 1262 1263 if (resid > volsize - off) /* don't write past the end */ 1264 resid = volsize - off; 1265 1266 while (resid != 0 && off < volsize) { 1267 1268 size = MIN(resid, zvol_maxphys); 1269 if (is_dump) { 1270 /* can't straddle a block boundary */ 1271 size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 1272 error = zvol_physio(zv, bp->b_flags, off, size, 1273 addr, 0); 1274 } else if (reading) { 1275 error = dmu_read(os, ZVOL_OBJ, off, size, addr); 1276 } else { 1277 dmu_tx_t *tx = dmu_tx_create(os); 1278 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1279 error = dmu_tx_assign(tx, TXG_WAIT); 1280 if (error) { 1281 dmu_tx_abort(tx); 1282 } else { 1283 dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1284 zvol_log_write(zv, tx, off, size); 1285 dmu_tx_commit(tx); 1286 } 1287 } 1288 if (error) 1289 break; 1290 off += size; 1291 addr += size; 1292 resid -= size; 1293 } 1294 zfs_range_unlock(rl); 1295 1296 if ((bp->b_resid = resid) == bp->b_bcount) 1297 bioerror(bp, off > volsize ? EINVAL : error); 1298 1299 if (!(bp->b_flags & B_ASYNC) && !reading && !zil_disable && !is_dump) 1300 zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1301 biodone(bp); 1302 1303 return (0); 1304 } 1305 1306 /* 1307 * Set the buffer count to the zvol maximum transfer. 1308 * Using our own routine instead of the default minphys() 1309 * means that for larger writes we write bigger buffers on X86 1310 * (128K instead of 56K) and flush the disk write cache less often 1311 * (every zvol_maxphys - currently 1MB) instead of minphys (currently 1312 * 56K on X86 and 128K on sparc). 1313 */ 1314 void 1315 zvol_minphys(struct buf *bp) 1316 { 1317 if (bp->b_bcount > zvol_maxphys) 1318 bp->b_bcount = zvol_maxphys; 1319 } 1320 1321 int 1322 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1323 { 1324 minor_t minor = getminor(dev); 1325 zvol_state_t *zv; 1326 int error = 0; 1327 uint64_t size; 1328 uint64_t boff; 1329 uint64_t resid; 1330 1331 if (minor == 0) /* This is the control device */ 1332 return (ENXIO); 1333 1334 zv = ddi_get_soft_state(zvol_state, minor); 1335 if (zv == NULL) 1336 return (ENXIO); 1337 1338 boff = ldbtob(blkno); 1339 resid = ldbtob(nblocks); 1340 if (boff + resid > zv->zv_volsize) { 1341 /* dump should know better than to write here */ 1342 ASSERT(blkno + resid <= zv->zv_volsize); 1343 return (EIO); 1344 } 1345 while (resid) { 1346 /* can't straddle a block boundary */ 1347 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 1348 1349 error = zvol_physio(zv, B_WRITE, boff, size, addr, 1); 1350 if (error) 1351 break; 1352 boff += size; 1353 addr += size; 1354 resid -= size; 1355 } 1356 1357 return (error); 1358 } 1359 1360 /*ARGSUSED*/ 1361 int 1362 zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1363 { 1364 minor_t minor = getminor(dev); 1365 zvol_state_t *zv; 1366 rl_t *rl; 1367 int error = 0; 1368 1369 if (minor == 0) /* This is the control device */ 1370 return (ENXIO); 1371 1372 zv = ddi_get_soft_state(zvol_state, minor); 1373 if (zv == NULL) 1374 return (ENXIO); 1375 1376 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1377 RL_READER); 1378 while (uio->uio_resid > 0) { 1379 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1380 1381 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1382 if (error) 1383 break; 1384 } 1385 zfs_range_unlock(rl); 1386 return (error); 1387 } 1388 1389 /*ARGSUSED*/ 1390 int 1391 zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1392 { 1393 minor_t minor = getminor(dev); 1394 zvol_state_t *zv; 1395 rl_t *rl; 1396 int error = 0; 1397 1398 if (minor == 0) /* This is the control device */ 1399 return (ENXIO); 1400 1401 zv = ddi_get_soft_state(zvol_state, minor); 1402 if (zv == NULL) 1403 return (ENXIO); 1404 1405 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1406 error = physio(zvol_strategy, NULL, dev, B_WRITE, 1407 zvol_minphys, uio); 1408 return (error); 1409 } 1410 1411 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1412 RL_WRITER); 1413 while (uio->uio_resid > 0) { 1414 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1415 uint64_t off = uio->uio_loffset; 1416 1417 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 1418 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1419 error = dmu_tx_assign(tx, TXG_WAIT); 1420 if (error) { 1421 dmu_tx_abort(tx); 1422 break; 1423 } 1424 error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx); 1425 if (error == 0) 1426 zvol_log_write(zv, tx, off, bytes); 1427 dmu_tx_commit(tx); 1428 1429 if (error) 1430 break; 1431 } 1432 zfs_range_unlock(rl); 1433 return (error); 1434 } 1435 1436 /* 1437 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1438 */ 1439 /*ARGSUSED*/ 1440 int 1441 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1442 { 1443 zvol_state_t *zv; 1444 struct dk_cinfo dki; 1445 struct dk_minfo dkm; 1446 dk_efi_t efi; 1447 struct dk_callback *dkc; 1448 struct uuid uuid = EFI_RESERVED; 1449 uint32_t crc; 1450 int error = 0; 1451 rl_t *rl; 1452 1453 mutex_enter(&zvol_state_lock); 1454 1455 zv = ddi_get_soft_state(zvol_state, getminor(dev)); 1456 1457 if (zv == NULL) { 1458 mutex_exit(&zvol_state_lock); 1459 return (ENXIO); 1460 } 1461 1462 switch (cmd) { 1463 1464 case DKIOCINFO: 1465 bzero(&dki, sizeof (dki)); 1466 (void) strcpy(dki.dki_cname, "zvol"); 1467 (void) strcpy(dki.dki_dname, "zvol"); 1468 dki.dki_ctype = DKC_UNKNOWN; 1469 dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1470 mutex_exit(&zvol_state_lock); 1471 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1472 error = EFAULT; 1473 return (error); 1474 1475 case DKIOCGMEDIAINFO: 1476 bzero(&dkm, sizeof (dkm)); 1477 dkm.dki_lbsize = 1U << zv->zv_min_bs; 1478 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1479 dkm.dki_media_type = DK_UNKNOWN; 1480 mutex_exit(&zvol_state_lock); 1481 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1482 error = EFAULT; 1483 return (error); 1484 1485 case DKIOCGETEFI: 1486 if (ddi_copyin((void *)arg, &efi, sizeof (dk_efi_t), flag)) { 1487 mutex_exit(&zvol_state_lock); 1488 return (EFAULT); 1489 } 1490 efi.dki_data = (void *)(uintptr_t)efi.dki_data_64; 1491 1492 /* 1493 * Some clients may attempt to request a PMBR for the 1494 * zvol. Currently this interface will return ENOTTY to 1495 * such requests. These requests could be supported by 1496 * adding a check for lba == 0 and consing up an appropriate 1497 * PMBR. 1498 */ 1499 if (efi.dki_lba == 1) { 1500 efi_gpt_t gpt; 1501 efi_gpe_t gpe; 1502 1503 bzero(&gpt, sizeof (gpt)); 1504 bzero(&gpe, sizeof (gpe)); 1505 1506 if (efi.dki_length < sizeof (gpt)) { 1507 mutex_exit(&zvol_state_lock); 1508 return (EINVAL); 1509 } 1510 1511 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1512 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1513 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1514 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1515 gpt.efi_gpt_LastUsableLBA = 1516 LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1); 1517 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1518 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1519 gpt.efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (gpe)); 1520 1521 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1522 gpe.efi_gpe_StartingLBA = gpt.efi_gpt_FirstUsableLBA; 1523 gpe.efi_gpe_EndingLBA = gpt.efi_gpt_LastUsableLBA; 1524 1525 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1526 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1527 1528 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1529 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1530 1531 mutex_exit(&zvol_state_lock); 1532 if (ddi_copyout(&gpt, efi.dki_data, sizeof (gpt), flag)) 1533 error = EFAULT; 1534 } else if (efi.dki_lba == 2) { 1535 efi_gpe_t gpe; 1536 1537 bzero(&gpe, sizeof (gpe)); 1538 1539 if (efi.dki_length < sizeof (gpe)) { 1540 mutex_exit(&zvol_state_lock); 1541 return (EINVAL); 1542 } 1543 1544 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1545 gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1546 gpe.efi_gpe_EndingLBA = 1547 LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1); 1548 1549 mutex_exit(&zvol_state_lock); 1550 if (ddi_copyout(&gpe, efi.dki_data, sizeof (gpe), flag)) 1551 error = EFAULT; 1552 } else { 1553 mutex_exit(&zvol_state_lock); 1554 error = EINVAL; 1555 } 1556 return (error); 1557 1558 case DKIOCFLUSHWRITECACHE: 1559 dkc = (struct dk_callback *)arg; 1560 zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1561 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1562 (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1563 error = 0; 1564 } 1565 break; 1566 1567 case DKIOCGGEOM: 1568 case DKIOCGVTOC: 1569 /* 1570 * commands using these (like prtvtoc) expect ENOTSUP 1571 * since we're emulating an EFI label 1572 */ 1573 error = ENOTSUP; 1574 break; 1575 1576 case DKIOCDUMPINIT: 1577 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1578 RL_WRITER); 1579 error = zvol_dumpify(zv); 1580 zfs_range_unlock(rl); 1581 break; 1582 1583 case DKIOCDUMPFINI: 1584 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1585 RL_WRITER); 1586 error = zvol_dump_fini(zv); 1587 zfs_range_unlock(rl); 1588 break; 1589 1590 default: 1591 error = ENOTTY; 1592 break; 1593 1594 } 1595 mutex_exit(&zvol_state_lock); 1596 return (error); 1597 } 1598 1599 int 1600 zvol_busy(void) 1601 { 1602 return (zvol_minors != 0); 1603 } 1604 1605 void 1606 zvol_init(void) 1607 { 1608 VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0); 1609 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL); 1610 } 1611 1612 void 1613 zvol_fini(void) 1614 { 1615 mutex_destroy(&zvol_state_lock); 1616 ddi_soft_state_fini(&zvol_state); 1617 } 1618 1619 static boolean_t 1620 zvol_is_swap(zvol_state_t *zv) 1621 { 1622 vnode_t *vp; 1623 boolean_t ret = B_FALSE; 1624 char *devpath; 1625 size_t devpathlen; 1626 int error; 1627 1628 devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(zv->zv_name) + 1; 1629 devpath = kmem_alloc(devpathlen, KM_SLEEP); 1630 (void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name); 1631 error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 1632 kmem_free(devpath, devpathlen); 1633 1634 ret = !error && IS_SWAPVP(common_specvp(vp)); 1635 1636 if (vp != NULL) 1637 VN_RELE(vp); 1638 1639 return (ret); 1640 } 1641 1642 static int 1643 zvol_dump_init(zvol_state_t *zv, boolean_t resize) 1644 { 1645 dmu_tx_t *tx; 1646 int error = 0; 1647 objset_t *os = zv->zv_objset; 1648 nvlist_t *nv = NULL; 1649 uint64_t checksum, compress, refresrv; 1650 1651 ASSERT(MUTEX_HELD(&zvol_state_lock)); 1652 1653 tx = dmu_tx_create(os); 1654 dmu_tx_hold_free(tx, ZVOL_OBJ, 0, DMU_OBJECT_END); 1655 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1656 error = dmu_tx_assign(tx, TXG_WAIT); 1657 if (error) { 1658 dmu_tx_abort(tx); 1659 return (error); 1660 } 1661 1662 /* 1663 * If we are resizing the dump device then we only need to 1664 * update the refreservation to match the newly updated 1665 * zvolsize. Otherwise, we save off the original state of the 1666 * zvol so that we can restore them if the zvol is ever undumpified. 1667 */ 1668 if (resize) { 1669 error = zap_update(os, ZVOL_ZAP_OBJ, 1670 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1671 &zv->zv_volsize, tx); 1672 } else { 1673 error = dsl_prop_get_integer(zv->zv_name, 1674 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 1675 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1676 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 1677 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1678 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 1679 1680 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1681 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 1682 &compress, tx); 1683 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1684 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 1685 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1686 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1687 &refresrv, tx); 1688 } 1689 dmu_tx_commit(tx); 1690 1691 /* Truncate the file */ 1692 if (!error) 1693 error = zvol_truncate(zv, 0, DMU_OBJECT_END); 1694 1695 if (error) 1696 return (error); 1697 1698 /* 1699 * We only need update the zvol's property if we are initializing 1700 * the dump area for the first time. 1701 */ 1702 if (!resize) { 1703 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1704 VERIFY(nvlist_add_uint64(nv, 1705 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 1706 VERIFY(nvlist_add_uint64(nv, 1707 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 1708 ZIO_COMPRESS_OFF) == 0); 1709 VERIFY(nvlist_add_uint64(nv, 1710 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 1711 ZIO_CHECKSUM_OFF) == 0); 1712 1713 error = zfs_set_prop_nvlist(zv->zv_name, nv); 1714 nvlist_free(nv); 1715 1716 if (error) 1717 return (error); 1718 } 1719 1720 /* Allocate the space for the dump */ 1721 error = zvol_prealloc(zv); 1722 return (error); 1723 } 1724 1725 static int 1726 zvol_dumpify(zvol_state_t *zv) 1727 { 1728 int error = 0; 1729 uint64_t dumpsize = 0; 1730 dmu_tx_t *tx; 1731 objset_t *os = zv->zv_objset; 1732 1733 if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) 1734 return (EROFS); 1735 1736 /* 1737 * We do not support swap devices acting as dump devices. 1738 */ 1739 if (zvol_is_swap(zv)) 1740 return (ENOTSUP); 1741 1742 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 1743 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 1744 boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE; 1745 1746 if ((error = zvol_dump_init(zv, resize)) != 0) { 1747 (void) zvol_dump_fini(zv); 1748 return (error); 1749 } 1750 } 1751 1752 /* 1753 * Build up our lba mapping. 1754 */ 1755 error = zvol_get_lbas(zv); 1756 if (error) { 1757 (void) zvol_dump_fini(zv); 1758 return (error); 1759 } 1760 1761 tx = dmu_tx_create(os); 1762 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1763 error = dmu_tx_assign(tx, TXG_WAIT); 1764 if (error) { 1765 dmu_tx_abort(tx); 1766 (void) zvol_dump_fini(zv); 1767 return (error); 1768 } 1769 1770 zv->zv_flags |= ZVOL_DUMPIFIED; 1771 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 1772 &zv->zv_volsize, tx); 1773 dmu_tx_commit(tx); 1774 1775 if (error) { 1776 (void) zvol_dump_fini(zv); 1777 return (error); 1778 } 1779 1780 txg_wait_synced(dmu_objset_pool(os), 0); 1781 return (0); 1782 } 1783 1784 static int 1785 zvol_dump_fini(zvol_state_t *zv) 1786 { 1787 dmu_tx_t *tx; 1788 objset_t *os = zv->zv_objset; 1789 nvlist_t *nv; 1790 int error = 0; 1791 uint64_t checksum, compress, refresrv; 1792 1793 tx = dmu_tx_create(os); 1794 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1795 error = dmu_tx_assign(tx, TXG_WAIT); 1796 if (error) { 1797 dmu_tx_abort(tx); 1798 return (error); 1799 } 1800 1801 /* 1802 * Attempt to restore the zvol back to its pre-dumpified state. 1803 * This is a best-effort attempt as it's possible that not all 1804 * of these properties were initialized during the dumpify process 1805 * (i.e. error during zvol_dump_init). 1806 */ 1807 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1808 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 1809 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1810 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 1811 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1812 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 1813 1814 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 1815 zvol_free_extents(zv); 1816 zv->zv_flags &= ~ZVOL_DUMPIFIED; 1817 dmu_tx_commit(tx); 1818 1819 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1820 (void) nvlist_add_uint64(nv, 1821 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 1822 (void) nvlist_add_uint64(nv, 1823 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 1824 (void) nvlist_add_uint64(nv, 1825 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 1826 (void) zfs_set_prop_nvlist(zv->zv_name, nv); 1827 nvlist_free(nv); 1828 1829 return (0); 1830 } 1831