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