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 2007 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/dsl_prop.h> 57 #include <sys/dkio.h> 58 #include <sys/efi_partition.h> 59 #include <sys/byteorder.h> 60 #include <sys/pathname.h> 61 #include <sys/ddi.h> 62 #include <sys/sunddi.h> 63 #include <sys/crc32.h> 64 #include <sys/dirent.h> 65 #include <sys/policy.h> 66 #include <sys/fs/zfs.h> 67 #include <sys/zfs_ioctl.h> 68 #include <sys/mkdev.h> 69 #include <sys/zil.h> 70 #include <sys/refcount.h> 71 #include <sys/zfs_znode.h> 72 #include <sys/zfs_rlock.h> 73 74 #include "zfs_namecheck.h" 75 76 #define ZVOL_OBJ 1ULL 77 #define ZVOL_ZAP_OBJ 2ULL 78 79 static void *zvol_state; 80 81 /* 82 * This lock protects the zvol_state structure from being modified 83 * while it's being used, e.g. an open that comes in before a create 84 * finishes. It also protects temporary opens of the dataset so that, 85 * e.g., an open doesn't get a spurious EBUSY. 86 */ 87 static kmutex_t zvol_state_lock; 88 static uint32_t zvol_minors; 89 90 /* 91 * The in-core state of each volume. 92 */ 93 typedef struct zvol_state { 94 char zv_name[MAXPATHLEN]; /* pool/dd name */ 95 uint64_t zv_volsize; /* amount of space we advertise */ 96 uint64_t zv_volblocksize; /* volume block size */ 97 minor_t zv_minor; /* minor number */ 98 uint8_t zv_min_bs; /* minimum addressable block shift */ 99 uint8_t zv_readonly; /* hard readonly; like write-protect */ 100 objset_t *zv_objset; /* objset handle */ 101 uint32_t zv_mode; /* DS_MODE_* flags at open time */ 102 uint32_t zv_open_count[OTYPCNT]; /* open counts */ 103 uint32_t zv_total_opens; /* total open count */ 104 zilog_t *zv_zilog; /* ZIL handle */ 105 uint64_t zv_txg_assign; /* txg to assign during ZIL replay */ 106 znode_t zv_znode; /* for range locking */ 107 } zvol_state_t; 108 109 /* 110 * zvol maximum transfer in one DMU tx. 111 */ 112 int zvol_maxphys = DMU_MAX_ACCESS/2; 113 114 static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 115 116 static void 117 zvol_size_changed(zvol_state_t *zv, dev_t dev) 118 { 119 dev = makedevice(getmajor(dev), zv->zv_minor); 120 121 VERIFY(ddi_prop_update_int64(dev, zfs_dip, 122 "Size", zv->zv_volsize) == DDI_SUCCESS); 123 VERIFY(ddi_prop_update_int64(dev, zfs_dip, 124 "Nblocks", lbtodb(zv->zv_volsize)) == DDI_SUCCESS); 125 } 126 127 int 128 zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 129 { 130 if (volsize == 0) 131 return (EINVAL); 132 133 if (volsize % blocksize != 0) 134 return (EINVAL); 135 136 #ifdef _ILP32 137 if (volsize - 1 > SPEC_MAXOFFSET_T) 138 return (EOVERFLOW); 139 #endif 140 return (0); 141 } 142 143 int 144 zvol_check_volblocksize(uint64_t volblocksize) 145 { 146 if (volblocksize < SPA_MINBLOCKSIZE || 147 volblocksize > SPA_MAXBLOCKSIZE || 148 !ISP2(volblocksize)) 149 return (EDOM); 150 151 return (0); 152 } 153 154 static void 155 zvol_readonly_changed_cb(void *arg, uint64_t newval) 156 { 157 zvol_state_t *zv = arg; 158 159 zv->zv_readonly = (uint8_t)newval; 160 } 161 162 int 163 zvol_get_stats(objset_t *os, nvlist_t *nv) 164 { 165 int error; 166 dmu_object_info_t doi; 167 uint64_t val; 168 169 170 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 171 if (error) 172 return (error); 173 174 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 175 176 error = dmu_object_info(os, ZVOL_OBJ, &doi); 177 178 if (error == 0) { 179 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 180 doi.doi_data_block_size); 181 } 182 183 return (error); 184 } 185 186 /* 187 * Find a free minor number. 188 */ 189 static minor_t 190 zvol_minor_alloc(void) 191 { 192 minor_t minor; 193 194 ASSERT(MUTEX_HELD(&zvol_state_lock)); 195 196 for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) 197 if (ddi_get_soft_state(zvol_state, minor) == NULL) 198 return (minor); 199 200 return (0); 201 } 202 203 static zvol_state_t * 204 zvol_minor_lookup(const char *name) 205 { 206 minor_t minor; 207 zvol_state_t *zv; 208 209 ASSERT(MUTEX_HELD(&zvol_state_lock)); 210 211 for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 212 zv = ddi_get_soft_state(zvol_state, minor); 213 if (zv == NULL) 214 continue; 215 if (strcmp(zv->zv_name, name) == 0) 216 break; 217 } 218 219 return (zv); 220 } 221 222 void 223 zvol_create_cb(objset_t *os, void *arg, dmu_tx_t *tx) 224 { 225 zfs_create_data_t *zc = arg; 226 int error; 227 uint64_t volblocksize, volsize; 228 229 VERIFY(nvlist_lookup_uint64(zc->zc_props, 230 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 231 if (nvlist_lookup_uint64(zc->zc_props, 232 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 233 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 234 235 /* 236 * These properites must be removed from the list so the generic 237 * property setting step won't apply to them. 238 */ 239 VERIFY(nvlist_remove_all(zc->zc_props, 240 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 241 (void) nvlist_remove_all(zc->zc_props, 242 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 243 244 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 245 DMU_OT_NONE, 0, tx); 246 ASSERT(error == 0); 247 248 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 249 DMU_OT_NONE, 0, tx); 250 ASSERT(error == 0); 251 252 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 253 ASSERT(error == 0); 254 } 255 256 /* 257 * Replay a TX_WRITE ZIL transaction that didn't get committed 258 * after a system failure 259 */ 260 static int 261 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 262 { 263 objset_t *os = zv->zv_objset; 264 char *data = (char *)(lr + 1); /* data follows lr_write_t */ 265 uint64_t off = lr->lr_offset; 266 uint64_t len = lr->lr_length; 267 dmu_tx_t *tx; 268 int error; 269 270 if (byteswap) 271 byteswap_uint64_array(lr, sizeof (*lr)); 272 273 tx = dmu_tx_create(os); 274 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len); 275 error = dmu_tx_assign(tx, zv->zv_txg_assign); 276 if (error) { 277 dmu_tx_abort(tx); 278 } else { 279 dmu_write(os, ZVOL_OBJ, off, len, data, tx); 280 dmu_tx_commit(tx); 281 } 282 283 return (error); 284 } 285 286 /* ARGSUSED */ 287 static int 288 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 289 { 290 return (ENOTSUP); 291 } 292 293 /* 294 * Callback vectors for replaying records. 295 * Only TX_WRITE is needed for zvol. 296 */ 297 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 298 zvol_replay_err, /* 0 no such transaction type */ 299 zvol_replay_err, /* TX_CREATE */ 300 zvol_replay_err, /* TX_MKDIR */ 301 zvol_replay_err, /* TX_MKXATTR */ 302 zvol_replay_err, /* TX_SYMLINK */ 303 zvol_replay_err, /* TX_REMOVE */ 304 zvol_replay_err, /* TX_RMDIR */ 305 zvol_replay_err, /* TX_LINK */ 306 zvol_replay_err, /* TX_RENAME */ 307 zvol_replay_write, /* TX_WRITE */ 308 zvol_replay_err, /* TX_TRUNCATE */ 309 zvol_replay_err, /* TX_SETATTR */ 310 zvol_replay_err, /* TX_ACL */ 311 }; 312 313 /* 314 * Create a minor node for the specified volume. 315 */ 316 int 317 zvol_create_minor(const char *name, dev_t dev) 318 { 319 zvol_state_t *zv; 320 objset_t *os; 321 dmu_object_info_t doi; 322 uint64_t volsize; 323 minor_t minor = 0; 324 struct pathname linkpath; 325 int ds_mode = DS_MODE_PRIMARY; 326 vnode_t *vp = NULL; 327 char *devpath; 328 size_t devpathlen = strlen(ZVOL_FULL_DEV_DIR) + 1 + strlen(name) + 1; 329 char chrbuf[30], blkbuf[30]; 330 int error; 331 332 mutex_enter(&zvol_state_lock); 333 334 if ((zv = zvol_minor_lookup(name)) != NULL) { 335 mutex_exit(&zvol_state_lock); 336 return (EEXIST); 337 } 338 339 if (strchr(name, '@') != 0) 340 ds_mode |= DS_MODE_READONLY; 341 342 error = dmu_objset_open(name, DMU_OST_ZVOL, ds_mode, &os); 343 344 if (error) { 345 mutex_exit(&zvol_state_lock); 346 return (error); 347 } 348 349 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 350 351 if (error) { 352 dmu_objset_close(os); 353 mutex_exit(&zvol_state_lock); 354 return (error); 355 } 356 357 /* 358 * If there's an existing /dev/zvol symlink, try to use the 359 * same minor number we used last time. 360 */ 361 devpath = kmem_alloc(devpathlen, KM_SLEEP); 362 363 (void) sprintf(devpath, "%s/%s", ZVOL_FULL_DEV_DIR, name); 364 365 error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp); 366 367 kmem_free(devpath, devpathlen); 368 369 if (error == 0 && vp->v_type != VLNK) 370 error = EINVAL; 371 372 if (error == 0) { 373 pn_alloc(&linkpath); 374 error = pn_getsymlink(vp, &linkpath, kcred); 375 if (error == 0) { 376 char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV); 377 if (ms != NULL) { 378 ms += strlen(ZVOL_PSEUDO_DEV); 379 minor = stoi(&ms); 380 } 381 } 382 pn_free(&linkpath); 383 } 384 385 if (vp != NULL) 386 VN_RELE(vp); 387 388 /* 389 * If we found a minor but it's already in use, we must pick a new one. 390 */ 391 if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL) 392 minor = 0; 393 394 if (minor == 0) 395 minor = zvol_minor_alloc(); 396 397 if (minor == 0) { 398 dmu_objset_close(os); 399 mutex_exit(&zvol_state_lock); 400 return (ENXIO); 401 } 402 403 if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) { 404 dmu_objset_close(os); 405 mutex_exit(&zvol_state_lock); 406 return (EAGAIN); 407 } 408 409 (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 410 (char *)name); 411 412 (void) sprintf(chrbuf, "%uc,raw", minor); 413 414 if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 415 minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 416 ddi_soft_state_free(zvol_state, minor); 417 dmu_objset_close(os); 418 mutex_exit(&zvol_state_lock); 419 return (EAGAIN); 420 } 421 422 (void) sprintf(blkbuf, "%uc", minor); 423 424 if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 425 minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 426 ddi_remove_minor_node(zfs_dip, chrbuf); 427 ddi_soft_state_free(zvol_state, minor); 428 dmu_objset_close(os); 429 mutex_exit(&zvol_state_lock); 430 return (EAGAIN); 431 } 432 433 zv = ddi_get_soft_state(zvol_state, minor); 434 435 (void) strcpy(zv->zv_name, name); 436 zv->zv_min_bs = DEV_BSHIFT; 437 zv->zv_minor = minor; 438 zv->zv_volsize = volsize; 439 zv->zv_objset = os; 440 zv->zv_mode = ds_mode; 441 zv->zv_zilog = zil_open(os, zvol_get_data); 442 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 443 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 444 sizeof (rl_t), offsetof(rl_t, r_node)); 445 446 447 /* get and cache the blocksize */ 448 error = dmu_object_info(os, ZVOL_OBJ, &doi); 449 ASSERT(error == 0); 450 zv->zv_volblocksize = doi.doi_data_block_size; 451 452 zil_replay(os, zv, &zv->zv_txg_assign, zvol_replay_vector); 453 454 zvol_size_changed(zv, dev); 455 456 /* XXX this should handle the possible i/o error */ 457 VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset), 458 "readonly", zvol_readonly_changed_cb, zv) == 0); 459 460 zvol_minors++; 461 462 mutex_exit(&zvol_state_lock); 463 464 return (0); 465 } 466 467 /* 468 * Remove minor node for the specified volume. 469 */ 470 int 471 zvol_remove_minor(const char *name) 472 { 473 zvol_state_t *zv; 474 char namebuf[30]; 475 476 mutex_enter(&zvol_state_lock); 477 478 if ((zv = zvol_minor_lookup(name)) == NULL) { 479 mutex_exit(&zvol_state_lock); 480 return (ENXIO); 481 } 482 483 if (zv->zv_total_opens != 0) { 484 mutex_exit(&zvol_state_lock); 485 return (EBUSY); 486 } 487 488 (void) sprintf(namebuf, "%uc,raw", zv->zv_minor); 489 ddi_remove_minor_node(zfs_dip, namebuf); 490 491 (void) sprintf(namebuf, "%uc", zv->zv_minor); 492 ddi_remove_minor_node(zfs_dip, namebuf); 493 494 VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset), 495 "readonly", zvol_readonly_changed_cb, zv) == 0); 496 497 zil_close(zv->zv_zilog); 498 zv->zv_zilog = NULL; 499 dmu_objset_close(zv->zv_objset); 500 zv->zv_objset = NULL; 501 avl_destroy(&zv->zv_znode.z_range_avl); 502 mutex_destroy(&zv->zv_znode.z_range_lock); 503 504 ddi_soft_state_free(zvol_state, zv->zv_minor); 505 506 zvol_minors--; 507 508 mutex_exit(&zvol_state_lock); 509 510 return (0); 511 } 512 513 int 514 zvol_set_volsize(const char *name, dev_t dev, uint64_t volsize) 515 { 516 zvol_state_t *zv; 517 dmu_tx_t *tx; 518 int error; 519 dmu_object_info_t doi; 520 521 mutex_enter(&zvol_state_lock); 522 523 if ((zv = zvol_minor_lookup(name)) == NULL) { 524 mutex_exit(&zvol_state_lock); 525 return (ENXIO); 526 } 527 528 if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 || 529 (error = zvol_check_volsize(volsize, 530 doi.doi_data_block_size)) != 0) { 531 mutex_exit(&zvol_state_lock); 532 return (error); 533 } 534 535 if (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY)) { 536 mutex_exit(&zvol_state_lock); 537 return (EROFS); 538 } 539 540 tx = dmu_tx_create(zv->zv_objset); 541 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 542 dmu_tx_hold_free(tx, ZVOL_OBJ, volsize, DMU_OBJECT_END); 543 error = dmu_tx_assign(tx, TXG_WAIT); 544 if (error) { 545 dmu_tx_abort(tx); 546 mutex_exit(&zvol_state_lock); 547 return (error); 548 } 549 550 error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1, 551 &volsize, tx); 552 if (error == 0) { 553 error = dmu_free_range(zv->zv_objset, ZVOL_OBJ, volsize, 554 DMU_OBJECT_END, tx); 555 } 556 557 dmu_tx_commit(tx); 558 559 if (error == 0) { 560 zv->zv_volsize = volsize; 561 zvol_size_changed(zv, dev); 562 } 563 564 mutex_exit(&zvol_state_lock); 565 566 return (error); 567 } 568 569 int 570 zvol_set_volblocksize(const char *name, uint64_t volblocksize) 571 { 572 zvol_state_t *zv; 573 dmu_tx_t *tx; 574 int error; 575 576 mutex_enter(&zvol_state_lock); 577 578 if ((zv = zvol_minor_lookup(name)) == NULL) { 579 mutex_exit(&zvol_state_lock); 580 return (ENXIO); 581 } 582 583 if (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY)) { 584 mutex_exit(&zvol_state_lock); 585 return (EROFS); 586 } 587 588 tx = dmu_tx_create(zv->zv_objset); 589 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 590 error = dmu_tx_assign(tx, TXG_WAIT); 591 if (error) { 592 dmu_tx_abort(tx); 593 } else { 594 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ, 595 volblocksize, 0, tx); 596 if (error == ENOTSUP) 597 error = EBUSY; 598 dmu_tx_commit(tx); 599 } 600 601 mutex_exit(&zvol_state_lock); 602 603 return (error); 604 } 605 606 /*ARGSUSED*/ 607 int 608 zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 609 { 610 minor_t minor = getminor(*devp); 611 zvol_state_t *zv; 612 613 if (minor == 0) /* This is the control device */ 614 return (0); 615 616 mutex_enter(&zvol_state_lock); 617 618 zv = ddi_get_soft_state(zvol_state, minor); 619 if (zv == NULL) { 620 mutex_exit(&zvol_state_lock); 621 return (ENXIO); 622 } 623 624 ASSERT(zv->zv_objset != NULL); 625 626 if ((flag & FWRITE) && 627 (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY))) { 628 mutex_exit(&zvol_state_lock); 629 return (EROFS); 630 } 631 632 if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 633 zv->zv_open_count[otyp]++; 634 zv->zv_total_opens++; 635 } 636 637 mutex_exit(&zvol_state_lock); 638 639 return (0); 640 } 641 642 /*ARGSUSED*/ 643 int 644 zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 645 { 646 minor_t minor = getminor(dev); 647 zvol_state_t *zv; 648 649 if (minor == 0) /* This is the control device */ 650 return (0); 651 652 mutex_enter(&zvol_state_lock); 653 654 zv = ddi_get_soft_state(zvol_state, minor); 655 if (zv == NULL) { 656 mutex_exit(&zvol_state_lock); 657 return (ENXIO); 658 } 659 660 /* 661 * The next statement is a workaround for the following DDI bug: 662 * 6343604 specfs race: multiple "last-close" of the same device 663 */ 664 if (zv->zv_total_opens == 0) { 665 mutex_exit(&zvol_state_lock); 666 return (0); 667 } 668 669 /* 670 * If the open count is zero, this is a spurious close. 671 * That indicates a bug in the kernel / DDI framework. 672 */ 673 ASSERT(zv->zv_open_count[otyp] != 0); 674 ASSERT(zv->zv_total_opens != 0); 675 676 /* 677 * You may get multiple opens, but only one close. 678 */ 679 zv->zv_open_count[otyp]--; 680 zv->zv_total_opens--; 681 682 mutex_exit(&zvol_state_lock); 683 684 return (0); 685 } 686 687 static void 688 zvol_get_done(dmu_buf_t *db, void *vzgd) 689 { 690 zgd_t *zgd = (zgd_t *)vzgd; 691 rl_t *rl = zgd->zgd_rl; 692 693 dmu_buf_rele(db, vzgd); 694 zfs_range_unlock(rl); 695 zil_add_vdev(zgd->zgd_zilog, DVA_GET_VDEV(BP_IDENTITY(zgd->zgd_bp))); 696 kmem_free(zgd, sizeof (zgd_t)); 697 } 698 699 /* 700 * Get data to generate a TX_WRITE intent log record. 701 */ 702 static int 703 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 704 { 705 zvol_state_t *zv = arg; 706 objset_t *os = zv->zv_objset; 707 dmu_buf_t *db; 708 rl_t *rl; 709 zgd_t *zgd; 710 uint64_t boff; /* block starting offset */ 711 int dlen = lr->lr_length; /* length of user data */ 712 int error; 713 714 ASSERT(zio); 715 ASSERT(dlen != 0); 716 717 /* 718 * Write records come in two flavors: immediate and indirect. 719 * For small writes it's cheaper to store the data with the 720 * log record (immediate); for large writes it's cheaper to 721 * sync the data and get a pointer to it (indirect) so that 722 * we don't have to write the data twice. 723 */ 724 if (buf != NULL) /* immediate write */ 725 return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf)); 726 727 zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP); 728 zgd->zgd_zilog = zv->zv_zilog; 729 zgd->zgd_bp = &lr->lr_blkptr; 730 731 /* 732 * Lock the range of the block to ensure that when the data is 733 * written out and it's checksum is being calculated that no other 734 * thread can change the block. 735 */ 736 boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t); 737 rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize, 738 RL_READER); 739 zgd->zgd_rl = rl; 740 741 VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db)); 742 error = dmu_sync(zio, db, &lr->lr_blkptr, 743 lr->lr_common.lrc_txg, zvol_get_done, zgd); 744 if (error == 0) 745 zil_add_vdev(zv->zv_zilog, 746 DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr))); 747 /* 748 * If we get EINPROGRESS, then we need to wait for a 749 * write IO initiated by dmu_sync() to complete before 750 * we can release this dbuf. We will finish everything 751 * up in the zvol_get_done() callback. 752 */ 753 if (error == EINPROGRESS) 754 return (0); 755 dmu_buf_rele(db, zgd); 756 zfs_range_unlock(rl); 757 kmem_free(zgd, sizeof (zgd_t)); 758 return (error); 759 } 760 761 /* 762 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 763 * 764 * We store data in the log buffers if it's small enough. 765 * Otherwise we will later flush the data out via dmu_sync(). 766 */ 767 ssize_t zvol_immediate_write_sz = 32768; 768 769 static void 770 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t len) 771 { 772 uint32_t blocksize = zv->zv_volblocksize; 773 lr_write_t *lr; 774 775 while (len) { 776 ssize_t nbytes = MIN(len, blocksize - P2PHASE(off, blocksize)); 777 itx_t *itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 778 779 itx->itx_wr_state = 780 len > zvol_immediate_write_sz ? WR_INDIRECT : WR_NEED_COPY; 781 itx->itx_private = zv; 782 lr = (lr_write_t *)&itx->itx_lr; 783 lr->lr_foid = ZVOL_OBJ; 784 lr->lr_offset = off; 785 lr->lr_length = nbytes; 786 lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t); 787 BP_ZERO(&lr->lr_blkptr); 788 789 (void) zil_itx_assign(zv->zv_zilog, itx, tx); 790 len -= nbytes; 791 off += nbytes; 792 } 793 } 794 795 int 796 zvol_strategy(buf_t *bp) 797 { 798 zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev)); 799 uint64_t off, volsize; 800 size_t size, resid; 801 char *addr; 802 objset_t *os; 803 rl_t *rl; 804 int error = 0; 805 int sync; 806 boolean_t reading; 807 808 if (zv == NULL) { 809 bioerror(bp, ENXIO); 810 biodone(bp); 811 return (0); 812 } 813 814 if (getminor(bp->b_edev) == 0) { 815 bioerror(bp, EINVAL); 816 biodone(bp); 817 return (0); 818 } 819 820 if ((zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY)) && 821 !(bp->b_flags & B_READ)) { 822 bioerror(bp, EROFS); 823 biodone(bp); 824 return (0); 825 } 826 827 off = ldbtob(bp->b_blkno); 828 volsize = zv->zv_volsize; 829 830 os = zv->zv_objset; 831 ASSERT(os != NULL); 832 sync = !(bp->b_flags & B_ASYNC) && !(zil_disable); 833 834 bp_mapin(bp); 835 addr = bp->b_un.b_addr; 836 resid = bp->b_bcount; 837 838 /* 839 * There must be no buffer changes when doing a dmu_sync() because 840 * we can't change the data whilst calculating the checksum. 841 * A better approach than a per zvol rwlock would be to lock ranges. 842 */ 843 reading = bp->b_flags & B_READ; 844 rl = zfs_range_lock(&zv->zv_znode, off, resid, 845 reading ? RL_READER : RL_WRITER); 846 847 while (resid != 0 && off < volsize) { 848 849 size = MIN(resid, zvol_maxphys); /* zvol_maxphys per tx */ 850 851 if (size > volsize - off) /* don't write past the end */ 852 size = volsize - off; 853 854 if (reading) { 855 error = dmu_read(os, ZVOL_OBJ, off, size, addr); 856 } else { 857 dmu_tx_t *tx = dmu_tx_create(os); 858 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 859 error = dmu_tx_assign(tx, TXG_WAIT); 860 if (error) { 861 dmu_tx_abort(tx); 862 } else { 863 dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 864 zvol_log_write(zv, tx, off, size); 865 dmu_tx_commit(tx); 866 } 867 } 868 if (error) 869 break; 870 off += size; 871 addr += size; 872 resid -= size; 873 } 874 zfs_range_unlock(rl); 875 876 if ((bp->b_resid = resid) == bp->b_bcount) 877 bioerror(bp, off > volsize ? EINVAL : error); 878 879 if (sync) 880 zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 881 882 biodone(bp); 883 884 return (0); 885 } 886 887 /* 888 * Set the buffer count to the zvol maximum transfer. 889 * Using our own routine instead of the default minphys() 890 * means that for larger writes we write bigger buffers on X86 891 * (128K instead of 56K) and flush the disk write cache less often 892 * (every zvol_maxphys - currently 1MB) instead of minphys (currently 893 * 56K on X86 and 128K on sparc). 894 */ 895 void 896 zvol_minphys(struct buf *bp) 897 { 898 if (bp->b_bcount > zvol_maxphys) 899 bp->b_bcount = zvol_maxphys; 900 } 901 902 /*ARGSUSED*/ 903 int 904 zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 905 { 906 zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(dev)); 907 rl_t *rl; 908 int error = 0; 909 910 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 911 RL_READER); 912 while (uio->uio_resid > 0) { 913 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 914 915 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 916 if (error) 917 break; 918 } 919 zfs_range_unlock(rl); 920 return (error); 921 } 922 923 /*ARGSUSED*/ 924 int 925 zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 926 { 927 zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(dev)); 928 rl_t *rl; 929 int error = 0; 930 931 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 932 RL_WRITER); 933 while (uio->uio_resid > 0) { 934 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 935 uint64_t off = uio->uio_loffset; 936 937 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 938 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 939 error = dmu_tx_assign(tx, TXG_WAIT); 940 if (error) { 941 dmu_tx_abort(tx); 942 break; 943 } 944 error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx); 945 if (error == 0) 946 zvol_log_write(zv, tx, off, bytes); 947 dmu_tx_commit(tx); 948 949 if (error) 950 break; 951 } 952 zfs_range_unlock(rl); 953 return (error); 954 } 955 956 /* 957 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 958 */ 959 /*ARGSUSED*/ 960 int 961 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 962 { 963 zvol_state_t *zv; 964 struct dk_cinfo dkc; 965 struct dk_minfo dkm; 966 dk_efi_t efi; 967 struct uuid uuid = EFI_RESERVED; 968 uint32_t crc; 969 int error = 0; 970 971 mutex_enter(&zvol_state_lock); 972 973 zv = ddi_get_soft_state(zvol_state, getminor(dev)); 974 975 if (zv == NULL) { 976 mutex_exit(&zvol_state_lock); 977 return (ENXIO); 978 } 979 980 switch (cmd) { 981 982 case DKIOCINFO: 983 bzero(&dkc, sizeof (dkc)); 984 (void) strcpy(dkc.dki_cname, "zvol"); 985 (void) strcpy(dkc.dki_dname, "zvol"); 986 dkc.dki_ctype = DKC_UNKNOWN; 987 dkc.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 988 mutex_exit(&zvol_state_lock); 989 if (ddi_copyout(&dkc, (void *)arg, sizeof (dkc), flag)) 990 error = EFAULT; 991 return (error); 992 993 case DKIOCGMEDIAINFO: 994 bzero(&dkm, sizeof (dkm)); 995 dkm.dki_lbsize = 1U << zv->zv_min_bs; 996 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 997 dkm.dki_media_type = DK_UNKNOWN; 998 mutex_exit(&zvol_state_lock); 999 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1000 error = EFAULT; 1001 return (error); 1002 1003 case DKIOCGETEFI: 1004 if (ddi_copyin((void *)arg, &efi, sizeof (dk_efi_t), flag)) { 1005 mutex_exit(&zvol_state_lock); 1006 return (EFAULT); 1007 } 1008 efi.dki_data = (void *)(uintptr_t)efi.dki_data_64; 1009 1010 /* 1011 * Some clients may attempt to request a PMBR for the 1012 * zvol. Currently this interface will return ENOTTY to 1013 * such requests. These requests could be supported by 1014 * adding a check for lba == 0 and consing up an appropriate 1015 * RMBR. 1016 */ 1017 if (efi.dki_lba == 1) { 1018 efi_gpt_t gpt; 1019 efi_gpe_t gpe; 1020 1021 bzero(&gpt, sizeof (gpt)); 1022 bzero(&gpe, sizeof (gpe)); 1023 1024 if (efi.dki_length < sizeof (gpt)) { 1025 mutex_exit(&zvol_state_lock); 1026 return (EINVAL); 1027 } 1028 1029 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1030 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1031 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1032 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1033 gpt.efi_gpt_LastUsableLBA = 1034 LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1); 1035 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1036 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1037 gpt.efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (gpe)); 1038 1039 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1040 gpe.efi_gpe_StartingLBA = gpt.efi_gpt_FirstUsableLBA; 1041 gpe.efi_gpe_EndingLBA = gpt.efi_gpt_LastUsableLBA; 1042 1043 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1044 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1045 1046 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1047 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1048 1049 mutex_exit(&zvol_state_lock); 1050 if (ddi_copyout(&gpt, efi.dki_data, sizeof (gpt), flag)) 1051 error = EFAULT; 1052 } else if (efi.dki_lba == 2) { 1053 efi_gpe_t gpe; 1054 1055 bzero(&gpe, sizeof (gpe)); 1056 1057 if (efi.dki_length < sizeof (gpe)) { 1058 mutex_exit(&zvol_state_lock); 1059 return (EINVAL); 1060 } 1061 1062 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1063 gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1064 gpe.efi_gpe_EndingLBA = 1065 LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1); 1066 1067 mutex_exit(&zvol_state_lock); 1068 if (ddi_copyout(&gpe, efi.dki_data, sizeof (gpe), flag)) 1069 error = EFAULT; 1070 } else { 1071 mutex_exit(&zvol_state_lock); 1072 error = EINVAL; 1073 } 1074 return (error); 1075 1076 case DKIOCFLUSHWRITECACHE: 1077 zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1078 break; 1079 1080 case DKIOCGGEOM: 1081 case DKIOCGVTOC: 1082 /* commands using these (like prtvtoc) expect ENOTSUP */ 1083 error = ENOTSUP; 1084 break; 1085 1086 default: 1087 error = ENOTTY; 1088 break; 1089 1090 } 1091 mutex_exit(&zvol_state_lock); 1092 return (error); 1093 } 1094 1095 int 1096 zvol_busy(void) 1097 { 1098 return (zvol_minors != 0); 1099 } 1100 1101 void 1102 zvol_init(void) 1103 { 1104 VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0); 1105 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL); 1106 } 1107 1108 void 1109 zvol_fini(void) 1110 { 1111 mutex_destroy(&zvol_state_lock); 1112 ddi_soft_state_fini(&zvol_state); 1113 } 1114