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 https://opensource.org/licenses/CDDL-1.0. 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 (C) 2008-2010 Lawrence Livermore National Security, LLC. 23 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). 24 * Rewritten for Linux by Brian Behlendorf <behlendorf1@llnl.gov>. 25 * LLNL-CODE-403049. 26 * 27 * ZFS volume emulation driver. 28 * 29 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. 30 * Volumes are accessed through the symbolic links named: 31 * 32 * /dev/<pool_name>/<dataset_name> 33 * 34 * Volumes are persistent through reboot and module load. No user command 35 * needs to be run before opening and using a device. 36 * 37 * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 38 * Copyright (c) 2016 Actifio, Inc. All rights reserved. 39 * Copyright (c) 2012, 2019 by Delphix. All rights reserved. 40 * Copyright (c) 2024, Klara, Inc. 41 */ 42 43 /* 44 * Note on locking of zvol state structures. 45 * 46 * These structures are used to maintain internal state used to emulate block 47 * devices on top of zvols. In particular, management of device minor number 48 * operations - create, remove, rename, and set_snapdev - involves access to 49 * these structures. The zvol_state_lock is primarily used to protect the 50 * zvol_state_list. The zv->zv_state_lock is used to protect the contents 51 * of the zvol_state_t structures, as well as to make sure that when the 52 * time comes to remove the structure from the list, it is not in use, and 53 * therefore, it can be taken off zvol_state_list and freed. 54 * 55 * The zv_suspend_lock was introduced to allow for suspending I/O to a zvol, 56 * e.g. for the duration of receive and rollback operations. This lock can be 57 * held for significant periods of time. Given that it is undesirable to hold 58 * mutexes for long periods of time, the following lock ordering applies: 59 * - take zvol_state_lock if necessary, to protect zvol_state_list 60 * - take zv_suspend_lock if necessary, by the code path in question 61 * - take zv_state_lock to protect zvol_state_t 62 * 63 * The minor operations are issued to spa->spa_zvol_taskq queues, that are 64 * single-threaded (to preserve order of minor operations), and are executed 65 * through the zvol_task_cb that dispatches the specific operations. Therefore, 66 * these operations are serialized per pool. Consequently, we can be certain 67 * that for a given zvol, there is only one operation at a time in progress. 68 * That is why one can be sure that first, zvol_state_t for a given zvol is 69 * allocated and placed on zvol_state_list, and then other minor operations 70 * for this zvol are going to proceed in the order of issue. 71 * 72 */ 73 74 #include <sys/dataset_kstats.h> 75 #include <sys/dbuf.h> 76 #include <sys/dmu_traverse.h> 77 #include <sys/dsl_dataset.h> 78 #include <sys/dsl_prop.h> 79 #include <sys/dsl_dir.h> 80 #include <sys/zap.h> 81 #include <sys/zfeature.h> 82 #include <sys/zil_impl.h> 83 #include <sys/dmu_tx.h> 84 #include <sys/zio.h> 85 #include <sys/zfs_rlock.h> 86 #include <sys/spa_impl.h> 87 #include <sys/zvol.h> 88 #include <sys/zvol_impl.h> 89 90 unsigned int zvol_inhibit_dev = 0; 91 unsigned int zvol_volmode = ZFS_VOLMODE_GEOM; 92 93 struct hlist_head *zvol_htable; 94 static list_t zvol_state_list; 95 krwlock_t zvol_state_lock; 96 extern int zfs_bclone_wait_dirty; 97 98 typedef enum { 99 ZVOL_ASYNC_REMOVE_MINORS, 100 ZVOL_ASYNC_RENAME_MINORS, 101 ZVOL_ASYNC_SET_SNAPDEV, 102 ZVOL_ASYNC_SET_VOLMODE, 103 ZVOL_ASYNC_MAX 104 } zvol_async_op_t; 105 106 typedef struct { 107 zvol_async_op_t op; 108 char name1[MAXNAMELEN]; 109 char name2[MAXNAMELEN]; 110 uint64_t value; 111 } zvol_task_t; 112 113 uint64_t 114 zvol_name_hash(const char *name) 115 { 116 uint64_t crc = -1ULL; 117 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 118 for (const uint8_t *p = (const uint8_t *)name; *p != 0; p++) 119 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (*p)) & 0xFF]; 120 return (crc); 121 } 122 123 /* 124 * Find a zvol_state_t given the name and hash generated by zvol_name_hash. 125 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise, 126 * return (NULL) without the taking locks. The zv_suspend_lock is always taken 127 * before zv_state_lock. The mode argument indicates the mode (including none) 128 * for zv_suspend_lock to be taken. 129 */ 130 zvol_state_t * 131 zvol_find_by_name_hash(const char *name, uint64_t hash, int mode) 132 { 133 zvol_state_t *zv; 134 struct hlist_node *p = NULL; 135 136 rw_enter(&zvol_state_lock, RW_READER); 137 hlist_for_each(p, ZVOL_HT_HEAD(hash)) { 138 zv = hlist_entry(p, zvol_state_t, zv_hlink); 139 mutex_enter(&zv->zv_state_lock); 140 if (zv->zv_hash == hash && strcmp(zv->zv_name, name) == 0) { 141 /* 142 * this is the right zvol, take the locks in the 143 * right order 144 */ 145 if (mode != RW_NONE && 146 !rw_tryenter(&zv->zv_suspend_lock, mode)) { 147 mutex_exit(&zv->zv_state_lock); 148 rw_enter(&zv->zv_suspend_lock, mode); 149 mutex_enter(&zv->zv_state_lock); 150 /* 151 * zvol cannot be renamed as we continue 152 * to hold zvol_state_lock 153 */ 154 ASSERT(zv->zv_hash == hash && 155 strcmp(zv->zv_name, name) == 0); 156 } 157 rw_exit(&zvol_state_lock); 158 return (zv); 159 } 160 mutex_exit(&zv->zv_state_lock); 161 } 162 rw_exit(&zvol_state_lock); 163 164 return (NULL); 165 } 166 167 /* 168 * Find a zvol_state_t given the name. 169 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise, 170 * return (NULL) without the taking locks. The zv_suspend_lock is always taken 171 * before zv_state_lock. The mode argument indicates the mode (including none) 172 * for zv_suspend_lock to be taken. 173 */ 174 static zvol_state_t * 175 zvol_find_by_name(const char *name, int mode) 176 { 177 return (zvol_find_by_name_hash(name, zvol_name_hash(name), mode)); 178 } 179 180 /* 181 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation. 182 */ 183 void 184 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 185 { 186 zfs_creat_t *zct = arg; 187 nvlist_t *nvprops = zct->zct_props; 188 int error; 189 uint64_t volblocksize, volsize; 190 191 VERIFY(nvlist_lookup_uint64(nvprops, 192 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 193 if (nvlist_lookup_uint64(nvprops, 194 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 195 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 196 197 /* 198 * These properties must be removed from the list so the generic 199 * property setting step won't apply to them. 200 */ 201 VERIFY(nvlist_remove_all(nvprops, 202 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 203 (void) nvlist_remove_all(nvprops, 204 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 205 206 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 207 DMU_OT_NONE, 0, tx); 208 ASSERT(error == 0); 209 210 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 211 DMU_OT_NONE, 0, tx); 212 ASSERT(error == 0); 213 214 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 215 ASSERT(error == 0); 216 } 217 218 /* 219 * ZFS_IOC_OBJSET_STATS entry point. 220 */ 221 int 222 zvol_get_stats(objset_t *os, nvlist_t *nv) 223 { 224 int error; 225 dmu_object_info_t *doi; 226 uint64_t val; 227 228 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 229 if (error) 230 return (SET_ERROR(error)); 231 232 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 233 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP); 234 error = dmu_object_info(os, ZVOL_OBJ, doi); 235 236 if (error == 0) { 237 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 238 doi->doi_data_block_size); 239 } 240 241 kmem_free(doi, sizeof (dmu_object_info_t)); 242 243 return (SET_ERROR(error)); 244 } 245 246 /* 247 * Sanity check volume size. 248 */ 249 int 250 zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 251 { 252 if (volsize == 0) 253 return (SET_ERROR(EINVAL)); 254 255 if (volsize % blocksize != 0) 256 return (SET_ERROR(EINVAL)); 257 258 #ifdef _ILP32 259 if (volsize - 1 > SPEC_MAXOFFSET_T) 260 return (SET_ERROR(EOVERFLOW)); 261 #endif 262 return (0); 263 } 264 265 /* 266 * Ensure the zap is flushed then inform the VFS of the capacity change. 267 */ 268 static int 269 zvol_update_volsize(uint64_t volsize, objset_t *os) 270 { 271 dmu_tx_t *tx; 272 int error; 273 uint64_t txg; 274 275 tx = dmu_tx_create(os); 276 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 277 dmu_tx_mark_netfree(tx); 278 error = dmu_tx_assign(tx, TXG_WAIT); 279 if (error) { 280 dmu_tx_abort(tx); 281 return (SET_ERROR(error)); 282 } 283 txg = dmu_tx_get_txg(tx); 284 285 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 286 &volsize, tx); 287 dmu_tx_commit(tx); 288 289 txg_wait_synced(dmu_objset_pool(os), txg); 290 291 if (error == 0) 292 error = dmu_free_long_range(os, 293 ZVOL_OBJ, volsize, DMU_OBJECT_END); 294 295 return (error); 296 } 297 298 /* 299 * Set ZFS_PROP_VOLSIZE set entry point. Note that modifying the volume 300 * size will result in a udev "change" event being generated. 301 */ 302 int 303 zvol_set_volsize(const char *name, uint64_t volsize) 304 { 305 objset_t *os = NULL; 306 uint64_t readonly; 307 int error; 308 boolean_t owned = B_FALSE; 309 310 error = dsl_prop_get_integer(name, 311 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL); 312 if (error != 0) 313 return (SET_ERROR(error)); 314 if (readonly) 315 return (SET_ERROR(EROFS)); 316 317 zvol_state_t *zv = zvol_find_by_name(name, RW_READER); 318 319 ASSERT(zv == NULL || (MUTEX_HELD(&zv->zv_state_lock) && 320 RW_READ_HELD(&zv->zv_suspend_lock))); 321 322 if (zv == NULL || zv->zv_objset == NULL) { 323 if (zv != NULL) 324 rw_exit(&zv->zv_suspend_lock); 325 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE, B_TRUE, 326 FTAG, &os)) != 0) { 327 if (zv != NULL) 328 mutex_exit(&zv->zv_state_lock); 329 return (SET_ERROR(error)); 330 } 331 owned = B_TRUE; 332 if (zv != NULL) 333 zv->zv_objset = os; 334 } else { 335 os = zv->zv_objset; 336 } 337 338 dmu_object_info_t *doi = kmem_alloc(sizeof (*doi), KM_SLEEP); 339 340 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) || 341 (error = zvol_check_volsize(volsize, doi->doi_data_block_size))) 342 goto out; 343 344 error = zvol_update_volsize(volsize, os); 345 if (error == 0 && zv != NULL) { 346 zv->zv_volsize = volsize; 347 zv->zv_changed = 1; 348 } 349 out: 350 kmem_free(doi, sizeof (dmu_object_info_t)); 351 352 if (owned) { 353 dmu_objset_disown(os, B_TRUE, FTAG); 354 if (zv != NULL) 355 zv->zv_objset = NULL; 356 } else { 357 rw_exit(&zv->zv_suspend_lock); 358 } 359 360 if (zv != NULL) 361 mutex_exit(&zv->zv_state_lock); 362 363 if (error == 0 && zv != NULL) 364 zvol_os_update_volsize(zv, volsize); 365 366 return (SET_ERROR(error)); 367 } 368 369 /* 370 * Update volthreading. 371 */ 372 int 373 zvol_set_volthreading(const char *name, boolean_t value) 374 { 375 zvol_state_t *zv = zvol_find_by_name(name, RW_NONE); 376 if (zv == NULL) 377 return (ENOENT); 378 zv->zv_threading = value; 379 mutex_exit(&zv->zv_state_lock); 380 return (0); 381 } 382 383 /* 384 * Update zvol ro property. 385 */ 386 int 387 zvol_set_ro(const char *name, boolean_t value) 388 { 389 zvol_state_t *zv = zvol_find_by_name(name, RW_NONE); 390 if (zv == NULL) 391 return (-1); 392 if (value) { 393 zvol_os_set_disk_ro(zv, 1); 394 zv->zv_flags |= ZVOL_RDONLY; 395 } else { 396 zvol_os_set_disk_ro(zv, 0); 397 zv->zv_flags &= ~ZVOL_RDONLY; 398 } 399 mutex_exit(&zv->zv_state_lock); 400 return (0); 401 } 402 403 /* 404 * Sanity check volume block size. 405 */ 406 int 407 zvol_check_volblocksize(const char *name, uint64_t volblocksize) 408 { 409 /* Record sizes above 128k need the feature to be enabled */ 410 if (volblocksize > SPA_OLD_MAXBLOCKSIZE) { 411 spa_t *spa; 412 int error; 413 414 if ((error = spa_open(name, &spa, FTAG)) != 0) 415 return (error); 416 417 if (!spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { 418 spa_close(spa, FTAG); 419 return (SET_ERROR(ENOTSUP)); 420 } 421 422 /* 423 * We don't allow setting the property above 1MB, 424 * unless the tunable has been changed. 425 */ 426 if (volblocksize > zfs_max_recordsize) 427 return (SET_ERROR(EDOM)); 428 429 spa_close(spa, FTAG); 430 } 431 432 if (volblocksize < SPA_MINBLOCKSIZE || 433 volblocksize > SPA_MAXBLOCKSIZE || 434 !ISP2(volblocksize)) 435 return (SET_ERROR(EDOM)); 436 437 return (0); 438 } 439 440 /* 441 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we 442 * implement DKIOCFREE/free-long-range. 443 */ 444 static int 445 zvol_replay_truncate(void *arg1, void *arg2, boolean_t byteswap) 446 { 447 zvol_state_t *zv = arg1; 448 lr_truncate_t *lr = arg2; 449 uint64_t offset, length; 450 451 ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr)); 452 453 if (byteswap) 454 byteswap_uint64_array(lr, sizeof (*lr)); 455 456 offset = lr->lr_offset; 457 length = lr->lr_length; 458 459 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 460 dmu_tx_mark_netfree(tx); 461 int error = dmu_tx_assign(tx, TXG_WAIT); 462 if (error != 0) { 463 dmu_tx_abort(tx); 464 } else { 465 (void) zil_replaying(zv->zv_zilog, tx); 466 dmu_tx_commit(tx); 467 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, 468 length); 469 } 470 471 return (error); 472 } 473 474 /* 475 * Replay a TX_WRITE ZIL transaction that didn't get committed 476 * after a system failure 477 */ 478 static int 479 zvol_replay_write(void *arg1, void *arg2, boolean_t byteswap) 480 { 481 zvol_state_t *zv = arg1; 482 lr_write_t *lr = arg2; 483 objset_t *os = zv->zv_objset; 484 char *data = (char *)(lr + 1); /* data follows lr_write_t */ 485 uint64_t offset, length; 486 dmu_tx_t *tx; 487 int error; 488 489 ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr)); 490 491 if (byteswap) 492 byteswap_uint64_array(lr, sizeof (*lr)); 493 494 offset = lr->lr_offset; 495 length = lr->lr_length; 496 497 /* If it's a dmu_sync() block, write the whole block */ 498 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 499 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 500 if (length < blocksize) { 501 offset -= offset % blocksize; 502 length = blocksize; 503 } 504 } 505 506 tx = dmu_tx_create(os); 507 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 508 error = dmu_tx_assign(tx, TXG_WAIT); 509 if (error) { 510 dmu_tx_abort(tx); 511 } else { 512 dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 513 (void) zil_replaying(zv->zv_zilog, tx); 514 dmu_tx_commit(tx); 515 } 516 517 return (error); 518 } 519 520 /* 521 * Replay a TX_CLONE_RANGE ZIL transaction that didn't get committed 522 * after a system failure 523 */ 524 static int 525 zvol_replay_clone_range(void *arg1, void *arg2, boolean_t byteswap) 526 { 527 zvol_state_t *zv = arg1; 528 lr_clone_range_t *lr = arg2; 529 objset_t *os = zv->zv_objset; 530 dmu_tx_t *tx; 531 int error; 532 uint64_t blksz; 533 uint64_t off; 534 uint64_t len; 535 536 ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr)); 537 ASSERT3U(lr->lr_common.lrc_reclen, >=, offsetof(lr_clone_range_t, 538 lr_bps[lr->lr_nbps])); 539 540 if (byteswap) 541 byteswap_uint64_array(lr, sizeof (*lr)); 542 543 ASSERT(spa_feature_is_enabled(dmu_objset_spa(os), 544 SPA_FEATURE_BLOCK_CLONING)); 545 546 off = lr->lr_offset; 547 len = lr->lr_length; 548 blksz = lr->lr_blksz; 549 550 if ((off % blksz) != 0) { 551 return (SET_ERROR(EINVAL)); 552 } 553 554 error = dnode_hold(os, ZVOL_OBJ, zv, &zv->zv_dn); 555 if (error != 0 || !zv->zv_dn) 556 return (error); 557 tx = dmu_tx_create(os); 558 dmu_tx_hold_clone_by_dnode(tx, zv->zv_dn, off, len); 559 error = dmu_tx_assign(tx, TXG_WAIT); 560 if (error != 0) { 561 dmu_tx_abort(tx); 562 goto out; 563 } 564 error = dmu_brt_clone(zv->zv_objset, ZVOL_OBJ, off, len, 565 tx, lr->lr_bps, lr->lr_nbps); 566 if (error != 0) { 567 dmu_tx_commit(tx); 568 goto out; 569 } 570 571 /* 572 * zil_replaying() not only check if we are replaying ZIL, but also 573 * updates the ZIL header to record replay progress. 574 */ 575 VERIFY(zil_replaying(zv->zv_zilog, tx)); 576 dmu_tx_commit(tx); 577 578 out: 579 dnode_rele(zv->zv_dn, zv); 580 zv->zv_dn = NULL; 581 return (error); 582 } 583 584 int 585 zvol_clone_range(zvol_state_t *zv_src, uint64_t inoff, zvol_state_t *zv_dst, 586 uint64_t outoff, uint64_t len) 587 { 588 zilog_t *zilog_dst; 589 zfs_locked_range_t *inlr, *outlr; 590 objset_t *inos, *outos; 591 dmu_tx_t *tx; 592 blkptr_t *bps; 593 size_t maxblocks; 594 int error = EINVAL; 595 596 rw_enter(&zv_dst->zv_suspend_lock, RW_READER); 597 if (zv_dst->zv_zilog == NULL) { 598 rw_exit(&zv_dst->zv_suspend_lock); 599 rw_enter(&zv_dst->zv_suspend_lock, RW_WRITER); 600 if (zv_dst->zv_zilog == NULL) { 601 zv_dst->zv_zilog = zil_open(zv_dst->zv_objset, 602 zvol_get_data, &zv_dst->zv_kstat.dk_zil_sums); 603 zv_dst->zv_flags |= ZVOL_WRITTEN_TO; 604 VERIFY0((zv_dst->zv_zilog->zl_header->zh_flags & 605 ZIL_REPLAY_NEEDED)); 606 } 607 rw_downgrade(&zv_dst->zv_suspend_lock); 608 } 609 if (zv_src != zv_dst) 610 rw_enter(&zv_src->zv_suspend_lock, RW_READER); 611 612 inos = zv_src->zv_objset; 613 outos = zv_dst->zv_objset; 614 615 /* 616 * Sanity checks 617 */ 618 if (!spa_feature_is_enabled(dmu_objset_spa(outos), 619 SPA_FEATURE_BLOCK_CLONING)) { 620 error = EOPNOTSUPP; 621 goto out; 622 } 623 if (dmu_objset_spa(inos) != dmu_objset_spa(outos)) { 624 error = EXDEV; 625 goto out; 626 } 627 if (inos->os_encrypted != outos->os_encrypted) { 628 error = EXDEV; 629 goto out; 630 } 631 if (zv_src->zv_volblocksize != zv_dst->zv_volblocksize) { 632 error = EINVAL; 633 goto out; 634 } 635 if (inoff >= zv_src->zv_volsize || outoff >= zv_dst->zv_volsize) { 636 error = 0; 637 goto out; 638 } 639 640 /* 641 * Do not read beyond boundary 642 */ 643 if (len > zv_src->zv_volsize - inoff) 644 len = zv_src->zv_volsize - inoff; 645 if (len > zv_dst->zv_volsize - outoff) 646 len = zv_dst->zv_volsize - outoff; 647 if (len == 0) { 648 error = 0; 649 goto out; 650 } 651 652 /* 653 * No overlapping if we are cloning within the same file 654 */ 655 if (zv_src == zv_dst) { 656 if (inoff < outoff + len && outoff < inoff + len) { 657 error = EINVAL; 658 goto out; 659 } 660 } 661 662 /* 663 * Offsets and length must be at block boundaries 664 */ 665 if ((inoff % zv_src->zv_volblocksize) != 0 || 666 (outoff % zv_dst->zv_volblocksize) != 0) { 667 error = EINVAL; 668 goto out; 669 } 670 671 /* 672 * Length must be multiple of block size 673 */ 674 if ((len % zv_src->zv_volblocksize) != 0) { 675 error = EINVAL; 676 goto out; 677 } 678 679 zilog_dst = zv_dst->zv_zilog; 680 maxblocks = zil_max_log_data(zilog_dst, sizeof (lr_clone_range_t)) / 681 sizeof (bps[0]); 682 bps = vmem_alloc(sizeof (bps[0]) * maxblocks, KM_SLEEP); 683 /* 684 * Maintain predictable lock order. 685 */ 686 if (zv_src < zv_dst || (zv_src == zv_dst && inoff < outoff)) { 687 inlr = zfs_rangelock_enter(&zv_src->zv_rangelock, inoff, len, 688 RL_READER); 689 outlr = zfs_rangelock_enter(&zv_dst->zv_rangelock, outoff, len, 690 RL_WRITER); 691 } else { 692 outlr = zfs_rangelock_enter(&zv_dst->zv_rangelock, outoff, len, 693 RL_WRITER); 694 inlr = zfs_rangelock_enter(&zv_src->zv_rangelock, inoff, len, 695 RL_READER); 696 } 697 698 while (len > 0) { 699 uint64_t size, last_synced_txg; 700 size_t nbps = maxblocks; 701 size = MIN(zv_src->zv_volblocksize * maxblocks, len); 702 last_synced_txg = spa_last_synced_txg( 703 dmu_objset_spa(zv_src->zv_objset)); 704 error = dmu_read_l0_bps(zv_src->zv_objset, ZVOL_OBJ, inoff, 705 size, bps, &nbps); 706 if (error != 0) { 707 /* 708 * If we are trying to clone a block that was created 709 * in the current transaction group, the error will be 710 * EAGAIN here. Based on zfs_bclone_wait_dirty either 711 * return a shortened range to the caller so it can 712 * fallback, or wait for the next TXG and check again. 713 */ 714 if (error == EAGAIN && zfs_bclone_wait_dirty) { 715 txg_wait_synced(dmu_objset_pool 716 (zv_src->zv_objset), last_synced_txg + 1); 717 continue; 718 } 719 break; 720 } 721 722 tx = dmu_tx_create(zv_dst->zv_objset); 723 dmu_tx_hold_clone_by_dnode(tx, zv_dst->zv_dn, outoff, size); 724 error = dmu_tx_assign(tx, TXG_WAIT); 725 if (error != 0) { 726 dmu_tx_abort(tx); 727 break; 728 } 729 error = dmu_brt_clone(zv_dst->zv_objset, ZVOL_OBJ, outoff, size, 730 tx, bps, nbps); 731 if (error != 0) { 732 dmu_tx_commit(tx); 733 break; 734 } 735 zvol_log_clone_range(zilog_dst, tx, TX_CLONE_RANGE, outoff, 736 size, zv_src->zv_volblocksize, bps, nbps); 737 dmu_tx_commit(tx); 738 inoff += size; 739 outoff += size; 740 len -= size; 741 } 742 vmem_free(bps, sizeof (bps[0]) * maxblocks); 743 zfs_rangelock_exit(outlr); 744 zfs_rangelock_exit(inlr); 745 if (error == 0 && zv_dst->zv_objset->os_sync == ZFS_SYNC_ALWAYS) { 746 zil_commit(zilog_dst, ZVOL_OBJ); 747 } 748 out: 749 if (zv_src != zv_dst) 750 rw_exit(&zv_src->zv_suspend_lock); 751 rw_exit(&zv_dst->zv_suspend_lock); 752 return (SET_ERROR(error)); 753 } 754 755 /* 756 * Handles TX_CLONE_RANGE transactions. 757 */ 758 void 759 zvol_log_clone_range(zilog_t *zilog, dmu_tx_t *tx, int txtype, uint64_t off, 760 uint64_t len, uint64_t blksz, const blkptr_t *bps, size_t nbps) 761 { 762 itx_t *itx; 763 lr_clone_range_t *lr; 764 uint64_t partlen, max_log_data; 765 size_t partnbps; 766 767 if (zil_replaying(zilog, tx)) 768 return; 769 770 max_log_data = zil_max_log_data(zilog, sizeof (lr_clone_range_t)); 771 772 while (nbps > 0) { 773 partnbps = MIN(nbps, max_log_data / sizeof (bps[0])); 774 partlen = partnbps * blksz; 775 ASSERT3U(partlen, <, len + blksz); 776 partlen = MIN(partlen, len); 777 778 itx = zil_itx_create(txtype, 779 sizeof (*lr) + sizeof (bps[0]) * partnbps); 780 lr = (lr_clone_range_t *)&itx->itx_lr; 781 lr->lr_foid = ZVOL_OBJ; 782 lr->lr_offset = off; 783 lr->lr_length = partlen; 784 lr->lr_blksz = blksz; 785 lr->lr_nbps = partnbps; 786 memcpy(lr->lr_bps, bps, sizeof (bps[0]) * partnbps); 787 788 zil_itx_assign(zilog, itx, tx); 789 790 bps += partnbps; 791 ASSERT3U(nbps, >=, partnbps); 792 nbps -= partnbps; 793 off += partlen; 794 ASSERT3U(len, >=, partlen); 795 len -= partlen; 796 } 797 } 798 799 static int 800 zvol_replay_err(void *arg1, void *arg2, boolean_t byteswap) 801 { 802 (void) arg1, (void) arg2, (void) byteswap; 803 return (SET_ERROR(ENOTSUP)); 804 } 805 806 /* 807 * Callback vectors for replaying records. 808 * Only TX_WRITE and TX_TRUNCATE are needed for zvol. 809 */ 810 zil_replay_func_t *const zvol_replay_vector[TX_MAX_TYPE] = { 811 zvol_replay_err, /* no such transaction type */ 812 zvol_replay_err, /* TX_CREATE */ 813 zvol_replay_err, /* TX_MKDIR */ 814 zvol_replay_err, /* TX_MKXATTR */ 815 zvol_replay_err, /* TX_SYMLINK */ 816 zvol_replay_err, /* TX_REMOVE */ 817 zvol_replay_err, /* TX_RMDIR */ 818 zvol_replay_err, /* TX_LINK */ 819 zvol_replay_err, /* TX_RENAME */ 820 zvol_replay_write, /* TX_WRITE */ 821 zvol_replay_truncate, /* TX_TRUNCATE */ 822 zvol_replay_err, /* TX_SETATTR */ 823 zvol_replay_err, /* TX_ACL_V0 */ 824 zvol_replay_err, /* TX_ACL */ 825 zvol_replay_err, /* TX_CREATE_ACL */ 826 zvol_replay_err, /* TX_CREATE_ATTR */ 827 zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 828 zvol_replay_err, /* TX_MKDIR_ACL */ 829 zvol_replay_err, /* TX_MKDIR_ATTR */ 830 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 831 zvol_replay_err, /* TX_WRITE2 */ 832 zvol_replay_err, /* TX_SETSAXATTR */ 833 zvol_replay_err, /* TX_RENAME_EXCHANGE */ 834 zvol_replay_err, /* TX_RENAME_WHITEOUT */ 835 zvol_replay_clone_range, /* TX_CLONE_RANGE */ 836 }; 837 838 /* 839 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 840 * 841 * We store data in the log buffers if it's small enough. 842 * Otherwise we will later flush the data out via dmu_sync(). 843 */ 844 static const ssize_t zvol_immediate_write_sz = 32768; 845 846 void 847 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset, 848 uint64_t size, boolean_t commit) 849 { 850 uint32_t blocksize = zv->zv_volblocksize; 851 zilog_t *zilog = zv->zv_zilog; 852 itx_wr_state_t write_state; 853 uint64_t sz = size; 854 855 if (zil_replaying(zilog, tx)) 856 return; 857 858 if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 859 write_state = WR_INDIRECT; 860 else if (!spa_has_slogs(zilog->zl_spa) && 861 size >= blocksize && blocksize > zvol_immediate_write_sz) 862 write_state = WR_INDIRECT; 863 else if (commit) 864 write_state = WR_COPIED; 865 else 866 write_state = WR_NEED_COPY; 867 868 while (size) { 869 itx_t *itx; 870 lr_write_t *lr; 871 itx_wr_state_t wr_state = write_state; 872 ssize_t len = size; 873 874 if (wr_state == WR_COPIED && size > zil_max_copied_data(zilog)) 875 wr_state = WR_NEED_COPY; 876 else if (wr_state == WR_INDIRECT) 877 len = MIN(blocksize - P2PHASE(offset, blocksize), size); 878 879 itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 880 (wr_state == WR_COPIED ? len : 0)); 881 lr = (lr_write_t *)&itx->itx_lr; 882 if (wr_state == WR_COPIED && dmu_read_by_dnode(zv->zv_dn, 883 offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) { 884 zil_itx_destroy(itx); 885 itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 886 lr = (lr_write_t *)&itx->itx_lr; 887 wr_state = WR_NEED_COPY; 888 } 889 890 itx->itx_wr_state = wr_state; 891 lr->lr_foid = ZVOL_OBJ; 892 lr->lr_offset = offset; 893 lr->lr_length = len; 894 lr->lr_blkoff = 0; 895 BP_ZERO(&lr->lr_blkptr); 896 897 itx->itx_private = zv; 898 899 (void) zil_itx_assign(zilog, itx, tx); 900 901 offset += len; 902 size -= len; 903 } 904 905 if (write_state == WR_COPIED || write_state == WR_NEED_COPY) { 906 dsl_pool_wrlog_count(zilog->zl_dmu_pool, sz, tx->tx_txg); 907 } 908 } 909 910 /* 911 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE. 912 */ 913 void 914 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len) 915 { 916 itx_t *itx; 917 lr_truncate_t *lr; 918 zilog_t *zilog = zv->zv_zilog; 919 920 if (zil_replaying(zilog, tx)) 921 return; 922 923 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); 924 lr = (lr_truncate_t *)&itx->itx_lr; 925 lr->lr_foid = ZVOL_OBJ; 926 lr->lr_offset = off; 927 lr->lr_length = len; 928 929 zil_itx_assign(zilog, itx, tx); 930 } 931 932 933 static void 934 zvol_get_done(zgd_t *zgd, int error) 935 { 936 (void) error; 937 if (zgd->zgd_db) 938 dmu_buf_rele(zgd->zgd_db, zgd); 939 940 zfs_rangelock_exit(zgd->zgd_lr); 941 942 kmem_free(zgd, sizeof (zgd_t)); 943 } 944 945 /* 946 * Get data to generate a TX_WRITE intent log record. 947 */ 948 int 949 zvol_get_data(void *arg, uint64_t arg2, lr_write_t *lr, char *buf, 950 struct lwb *lwb, zio_t *zio) 951 { 952 zvol_state_t *zv = arg; 953 uint64_t offset = lr->lr_offset; 954 uint64_t size = lr->lr_length; 955 dmu_buf_t *db; 956 zgd_t *zgd; 957 int error; 958 959 ASSERT3P(lwb, !=, NULL); 960 ASSERT3U(size, !=, 0); 961 962 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 963 zgd->zgd_lwb = lwb; 964 965 /* 966 * Write records come in two flavors: immediate and indirect. 967 * For small writes it's cheaper to store the data with the 968 * log record (immediate); for large writes it's cheaper to 969 * sync the data and get a pointer to it (indirect) so that 970 * we don't have to write the data twice. 971 */ 972 if (buf != NULL) { /* immediate write */ 973 zgd->zgd_lr = zfs_rangelock_enter(&zv->zv_rangelock, offset, 974 size, RL_READER); 975 error = dmu_read_by_dnode(zv->zv_dn, offset, size, buf, 976 DMU_READ_NO_PREFETCH); 977 } else { /* indirect write */ 978 ASSERT3P(zio, !=, NULL); 979 /* 980 * Have to lock the whole block to ensure when it's written out 981 * and its checksum is being calculated that no one can change 982 * the data. Contrarily to zfs_get_data we need not re-check 983 * blocksize after we get the lock because it cannot be changed. 984 */ 985 size = zv->zv_volblocksize; 986 offset = P2ALIGN_TYPED(offset, size, uint64_t); 987 zgd->zgd_lr = zfs_rangelock_enter(&zv->zv_rangelock, offset, 988 size, RL_READER); 989 error = dmu_buf_hold_noread_by_dnode(zv->zv_dn, offset, zgd, 990 &db); 991 if (error == 0) { 992 blkptr_t *bp = &lr->lr_blkptr; 993 994 zgd->zgd_db = db; 995 zgd->zgd_bp = bp; 996 997 ASSERT(db != NULL); 998 ASSERT(db->db_offset == offset); 999 ASSERT(db->db_size == size); 1000 1001 error = dmu_sync(zio, lr->lr_common.lrc_txg, 1002 zvol_get_done, zgd); 1003 1004 if (error == 0) 1005 return (0); 1006 } 1007 } 1008 1009 zvol_get_done(zgd, error); 1010 1011 return (SET_ERROR(error)); 1012 } 1013 1014 /* 1015 * The zvol_state_t's are inserted into zvol_state_list and zvol_htable. 1016 */ 1017 1018 void 1019 zvol_insert(zvol_state_t *zv) 1020 { 1021 ASSERT(RW_WRITE_HELD(&zvol_state_lock)); 1022 list_insert_head(&zvol_state_list, zv); 1023 hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash)); 1024 } 1025 1026 /* 1027 * Simply remove the zvol from to list of zvols. 1028 */ 1029 static void 1030 zvol_remove(zvol_state_t *zv) 1031 { 1032 ASSERT(RW_WRITE_HELD(&zvol_state_lock)); 1033 list_remove(&zvol_state_list, zv); 1034 hlist_del(&zv->zv_hlink); 1035 } 1036 1037 /* 1038 * Setup zv after we just own the zv->objset 1039 */ 1040 static int 1041 zvol_setup_zv(zvol_state_t *zv) 1042 { 1043 uint64_t volsize; 1044 int error; 1045 uint64_t ro; 1046 objset_t *os = zv->zv_objset; 1047 1048 ASSERT(MUTEX_HELD(&zv->zv_state_lock)); 1049 ASSERT(RW_LOCK_HELD(&zv->zv_suspend_lock)); 1050 1051 zv->zv_zilog = NULL; 1052 zv->zv_flags &= ~ZVOL_WRITTEN_TO; 1053 1054 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL); 1055 if (error) 1056 return (SET_ERROR(error)); 1057 1058 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 1059 if (error) 1060 return (SET_ERROR(error)); 1061 1062 error = dnode_hold(os, ZVOL_OBJ, zv, &zv->zv_dn); 1063 if (error) 1064 return (SET_ERROR(error)); 1065 1066 zvol_os_set_capacity(zv, volsize >> 9); 1067 zv->zv_volsize = volsize; 1068 1069 if (ro || dmu_objset_is_snapshot(os) || 1070 !spa_writeable(dmu_objset_spa(os))) { 1071 zvol_os_set_disk_ro(zv, 1); 1072 zv->zv_flags |= ZVOL_RDONLY; 1073 } else { 1074 zvol_os_set_disk_ro(zv, 0); 1075 zv->zv_flags &= ~ZVOL_RDONLY; 1076 } 1077 return (0); 1078 } 1079 1080 /* 1081 * Shutdown every zv_objset related stuff except zv_objset itself. 1082 * The is the reverse of zvol_setup_zv. 1083 */ 1084 static void 1085 zvol_shutdown_zv(zvol_state_t *zv) 1086 { 1087 ASSERT(MUTEX_HELD(&zv->zv_state_lock) && 1088 RW_LOCK_HELD(&zv->zv_suspend_lock)); 1089 1090 if (zv->zv_flags & ZVOL_WRITTEN_TO) { 1091 ASSERT(zv->zv_zilog != NULL); 1092 zil_close(zv->zv_zilog); 1093 } 1094 1095 zv->zv_zilog = NULL; 1096 1097 dnode_rele(zv->zv_dn, zv); 1098 zv->zv_dn = NULL; 1099 1100 /* 1101 * Evict cached data. We must write out any dirty data before 1102 * disowning the dataset. 1103 */ 1104 if (zv->zv_flags & ZVOL_WRITTEN_TO) 1105 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1106 (void) dmu_objset_evict_dbufs(zv->zv_objset); 1107 } 1108 1109 /* 1110 * return the proper tag for rollback and recv 1111 */ 1112 void * 1113 zvol_tag(zvol_state_t *zv) 1114 { 1115 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock)); 1116 return (zv->zv_open_count > 0 ? zv : NULL); 1117 } 1118 1119 /* 1120 * Suspend the zvol for recv and rollback. 1121 */ 1122 zvol_state_t * 1123 zvol_suspend(const char *name) 1124 { 1125 zvol_state_t *zv; 1126 1127 zv = zvol_find_by_name(name, RW_WRITER); 1128 1129 if (zv == NULL) 1130 return (NULL); 1131 1132 /* block all I/O, release in zvol_resume. */ 1133 ASSERT(MUTEX_HELD(&zv->zv_state_lock)); 1134 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock)); 1135 1136 atomic_inc(&zv->zv_suspend_ref); 1137 1138 if (zv->zv_open_count > 0) 1139 zvol_shutdown_zv(zv); 1140 1141 /* 1142 * do not hold zv_state_lock across suspend/resume to 1143 * avoid locking up zvol lookups 1144 */ 1145 mutex_exit(&zv->zv_state_lock); 1146 1147 /* zv_suspend_lock is released in zvol_resume() */ 1148 return (zv); 1149 } 1150 1151 int 1152 zvol_resume(zvol_state_t *zv) 1153 { 1154 int error = 0; 1155 1156 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock)); 1157 1158 mutex_enter(&zv->zv_state_lock); 1159 1160 if (zv->zv_open_count > 0) { 1161 VERIFY0(dmu_objset_hold(zv->zv_name, zv, &zv->zv_objset)); 1162 VERIFY3P(zv->zv_objset->os_dsl_dataset->ds_owner, ==, zv); 1163 VERIFY(dsl_dataset_long_held(zv->zv_objset->os_dsl_dataset)); 1164 dmu_objset_rele(zv->zv_objset, zv); 1165 1166 error = zvol_setup_zv(zv); 1167 } 1168 1169 mutex_exit(&zv->zv_state_lock); 1170 1171 rw_exit(&zv->zv_suspend_lock); 1172 /* 1173 * We need this because we don't hold zvol_state_lock while releasing 1174 * zv_suspend_lock. zvol_remove_minors_impl thus cannot check 1175 * zv_suspend_lock to determine it is safe to free because rwlock is 1176 * not inherent atomic. 1177 */ 1178 atomic_dec(&zv->zv_suspend_ref); 1179 1180 if (zv->zv_flags & ZVOL_REMOVING) 1181 cv_broadcast(&zv->zv_removing_cv); 1182 1183 return (SET_ERROR(error)); 1184 } 1185 1186 int 1187 zvol_first_open(zvol_state_t *zv, boolean_t readonly) 1188 { 1189 objset_t *os; 1190 int error; 1191 1192 ASSERT(RW_READ_HELD(&zv->zv_suspend_lock)); 1193 ASSERT(MUTEX_HELD(&zv->zv_state_lock)); 1194 ASSERT(mutex_owned(&spa_namespace_lock)); 1195 1196 boolean_t ro = (readonly || (strchr(zv->zv_name, '@') != NULL)); 1197 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, ro, B_TRUE, zv, &os); 1198 if (error) 1199 return (SET_ERROR(error)); 1200 1201 zv->zv_objset = os; 1202 1203 error = zvol_setup_zv(zv); 1204 if (error) { 1205 dmu_objset_disown(os, 1, zv); 1206 zv->zv_objset = NULL; 1207 } 1208 1209 return (error); 1210 } 1211 1212 void 1213 zvol_last_close(zvol_state_t *zv) 1214 { 1215 ASSERT(RW_READ_HELD(&zv->zv_suspend_lock)); 1216 ASSERT(MUTEX_HELD(&zv->zv_state_lock)); 1217 1218 if (zv->zv_flags & ZVOL_REMOVING) 1219 cv_broadcast(&zv->zv_removing_cv); 1220 1221 zvol_shutdown_zv(zv); 1222 1223 dmu_objset_disown(zv->zv_objset, 1, zv); 1224 zv->zv_objset = NULL; 1225 } 1226 1227 typedef struct minors_job { 1228 list_t *list; 1229 list_node_t link; 1230 /* input */ 1231 char *name; 1232 /* output */ 1233 int error; 1234 } minors_job_t; 1235 1236 /* 1237 * Prefetch zvol dnodes for the minors_job 1238 */ 1239 static void 1240 zvol_prefetch_minors_impl(void *arg) 1241 { 1242 minors_job_t *job = arg; 1243 char *dsname = job->name; 1244 objset_t *os = NULL; 1245 1246 job->error = dmu_objset_own(dsname, DMU_OST_ZVOL, B_TRUE, B_TRUE, 1247 FTAG, &os); 1248 if (job->error == 0) { 1249 dmu_prefetch_dnode(os, ZVOL_OBJ, ZIO_PRIORITY_SYNC_READ); 1250 dmu_objset_disown(os, B_TRUE, FTAG); 1251 } 1252 } 1253 1254 /* 1255 * Mask errors to continue dmu_objset_find() traversal 1256 */ 1257 static int 1258 zvol_create_snap_minor_cb(const char *dsname, void *arg) 1259 { 1260 minors_job_t *j = arg; 1261 list_t *minors_list = j->list; 1262 const char *name = j->name; 1263 1264 ASSERT0(MUTEX_HELD(&spa_namespace_lock)); 1265 1266 /* skip the designated dataset */ 1267 if (name && strcmp(dsname, name) == 0) 1268 return (0); 1269 1270 /* at this point, the dsname should name a snapshot */ 1271 if (strchr(dsname, '@') == 0) { 1272 dprintf("zvol_create_snap_minor_cb(): " 1273 "%s is not a snapshot name\n", dsname); 1274 } else { 1275 minors_job_t *job; 1276 char *n = kmem_strdup(dsname); 1277 if (n == NULL) 1278 return (0); 1279 1280 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP); 1281 job->name = n; 1282 job->list = minors_list; 1283 job->error = 0; 1284 list_insert_tail(minors_list, job); 1285 /* don't care if dispatch fails, because job->error is 0 */ 1286 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job, 1287 TQ_SLEEP); 1288 } 1289 1290 return (0); 1291 } 1292 1293 /* 1294 * If spa_keystore_load_wkey() is called for an encrypted zvol, 1295 * we need to look for any clones also using the key. This function 1296 * is "best effort" - so we just skip over it if there are failures. 1297 */ 1298 static void 1299 zvol_add_clones(const char *dsname, list_t *minors_list) 1300 { 1301 /* Also check if it has clones */ 1302 dsl_dir_t *dd = NULL; 1303 dsl_pool_t *dp = NULL; 1304 1305 if (dsl_pool_hold(dsname, FTAG, &dp) != 0) 1306 return; 1307 1308 if (!spa_feature_is_enabled(dp->dp_spa, 1309 SPA_FEATURE_ENCRYPTION)) 1310 goto out; 1311 1312 if (dsl_dir_hold(dp, dsname, FTAG, &dd, NULL) != 0) 1313 goto out; 1314 1315 if (dsl_dir_phys(dd)->dd_clones == 0) 1316 goto out; 1317 1318 zap_cursor_t *zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP); 1319 zap_attribute_t *za = zap_attribute_alloc(); 1320 objset_t *mos = dd->dd_pool->dp_meta_objset; 1321 1322 for (zap_cursor_init(zc, mos, dsl_dir_phys(dd)->dd_clones); 1323 zap_cursor_retrieve(zc, za) == 0; 1324 zap_cursor_advance(zc)) { 1325 dsl_dataset_t *clone; 1326 minors_job_t *job; 1327 1328 if (dsl_dataset_hold_obj(dd->dd_pool, 1329 za->za_first_integer, FTAG, &clone) == 0) { 1330 1331 char name[ZFS_MAX_DATASET_NAME_LEN]; 1332 dsl_dataset_name(clone, name); 1333 1334 char *n = kmem_strdup(name); 1335 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP); 1336 job->name = n; 1337 job->list = minors_list; 1338 job->error = 0; 1339 list_insert_tail(minors_list, job); 1340 1341 dsl_dataset_rele(clone, FTAG); 1342 } 1343 } 1344 zap_cursor_fini(zc); 1345 zap_attribute_free(za); 1346 kmem_free(zc, sizeof (zap_cursor_t)); 1347 1348 out: 1349 if (dd != NULL) 1350 dsl_dir_rele(dd, FTAG); 1351 dsl_pool_rele(dp, FTAG); 1352 } 1353 1354 /* 1355 * Mask errors to continue dmu_objset_find() traversal 1356 */ 1357 static int 1358 zvol_create_minors_cb(const char *dsname, void *arg) 1359 { 1360 uint64_t snapdev; 1361 int error; 1362 list_t *minors_list = arg; 1363 1364 ASSERT0(MUTEX_HELD(&spa_namespace_lock)); 1365 1366 error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL); 1367 if (error) 1368 return (0); 1369 1370 /* 1371 * Given the name and the 'snapdev' property, create device minor nodes 1372 * with the linkages to zvols/snapshots as needed. 1373 * If the name represents a zvol, create a minor node for the zvol, then 1374 * check if its snapshots are 'visible', and if so, iterate over the 1375 * snapshots and create device minor nodes for those. 1376 */ 1377 if (strchr(dsname, '@') == 0) { 1378 minors_job_t *job; 1379 char *n = kmem_strdup(dsname); 1380 if (n == NULL) 1381 return (0); 1382 1383 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP); 1384 job->name = n; 1385 job->list = minors_list; 1386 job->error = 0; 1387 list_insert_tail(minors_list, job); 1388 /* don't care if dispatch fails, because job->error is 0 */ 1389 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job, 1390 TQ_SLEEP); 1391 1392 zvol_add_clones(dsname, minors_list); 1393 1394 if (snapdev == ZFS_SNAPDEV_VISIBLE) { 1395 /* 1396 * traverse snapshots only, do not traverse children, 1397 * and skip the 'dsname' 1398 */ 1399 (void) dmu_objset_find(dsname, 1400 zvol_create_snap_minor_cb, (void *)job, 1401 DS_FIND_SNAPSHOTS); 1402 } 1403 } else { 1404 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n", 1405 dsname); 1406 } 1407 1408 return (0); 1409 } 1410 1411 /* 1412 * Create minors for the specified dataset, including children and snapshots. 1413 * Pay attention to the 'snapdev' property and iterate over the snapshots 1414 * only if they are 'visible'. This approach allows one to assure that the 1415 * snapshot metadata is read from disk only if it is needed. 1416 * 1417 * The name can represent a dataset to be recursively scanned for zvols and 1418 * their snapshots, or a single zvol snapshot. If the name represents a 1419 * dataset, the scan is performed in two nested stages: 1420 * - scan the dataset for zvols, and 1421 * - for each zvol, create a minor node, then check if the zvol's snapshots 1422 * are 'visible', and only then iterate over the snapshots if needed 1423 * 1424 * If the name represents a snapshot, a check is performed if the snapshot is 1425 * 'visible' (which also verifies that the parent is a zvol), and if so, 1426 * a minor node for that snapshot is created. 1427 */ 1428 void 1429 zvol_create_minors_recursive(const char *name) 1430 { 1431 list_t minors_list; 1432 minors_job_t *job; 1433 1434 if (zvol_inhibit_dev) 1435 return; 1436 1437 /* 1438 * This is the list for prefetch jobs. Whenever we found a match 1439 * during dmu_objset_find, we insert a minors_job to the list and do 1440 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need 1441 * any lock because all list operation is done on the current thread. 1442 * 1443 * We will use this list to do zvol_os_create_minor after prefetch 1444 * so we don't have to traverse using dmu_objset_find again. 1445 */ 1446 list_create(&minors_list, sizeof (minors_job_t), 1447 offsetof(minors_job_t, link)); 1448 1449 1450 if (strchr(name, '@') != NULL) { 1451 uint64_t snapdev; 1452 1453 int error = dsl_prop_get_integer(name, "snapdev", 1454 &snapdev, NULL); 1455 1456 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE) 1457 (void) zvol_os_create_minor(name); 1458 } else { 1459 fstrans_cookie_t cookie = spl_fstrans_mark(); 1460 (void) dmu_objset_find(name, zvol_create_minors_cb, 1461 &minors_list, DS_FIND_CHILDREN); 1462 spl_fstrans_unmark(cookie); 1463 } 1464 1465 taskq_wait_outstanding(system_taskq, 0); 1466 1467 /* 1468 * Prefetch is completed, we can do zvol_os_create_minor 1469 * sequentially. 1470 */ 1471 while ((job = list_remove_head(&minors_list)) != NULL) { 1472 if (!job->error) 1473 (void) zvol_os_create_minor(job->name); 1474 kmem_strfree(job->name); 1475 kmem_free(job, sizeof (minors_job_t)); 1476 } 1477 1478 list_destroy(&minors_list); 1479 } 1480 1481 void 1482 zvol_create_minor(const char *name) 1483 { 1484 /* 1485 * Note: the dsl_pool_config_lock must not be held. 1486 * Minor node creation needs to obtain the zvol_state_lock. 1487 * zvol_open() obtains the zvol_state_lock and then the dsl pool 1488 * config lock. Therefore, we can't have the config lock now if 1489 * we are going to wait for the zvol_state_lock, because it 1490 * would be a lock order inversion which could lead to deadlock. 1491 */ 1492 1493 if (zvol_inhibit_dev) 1494 return; 1495 1496 if (strchr(name, '@') != NULL) { 1497 uint64_t snapdev; 1498 1499 int error = dsl_prop_get_integer(name, 1500 "snapdev", &snapdev, NULL); 1501 1502 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE) 1503 (void) zvol_os_create_minor(name); 1504 } else { 1505 (void) zvol_os_create_minor(name); 1506 } 1507 } 1508 1509 /* 1510 * Remove minors for specified dataset including children and snapshots. 1511 */ 1512 1513 /* 1514 * Remove the minor for a given zvol. This will do it all: 1515 * - flag the zvol for removal, so new requests are rejected 1516 * - wait until outstanding requests are completed 1517 * - remove it from lists 1518 * - free it 1519 * It's also usable as a taskq task, and smells nice too. 1520 */ 1521 static void 1522 zvol_remove_minor_task(void *arg) 1523 { 1524 zvol_state_t *zv = (zvol_state_t *)arg; 1525 1526 ASSERT(!RW_LOCK_HELD(&zvol_state_lock)); 1527 ASSERT(!MUTEX_HELD(&zv->zv_state_lock)); 1528 1529 mutex_enter(&zv->zv_state_lock); 1530 while (zv->zv_open_count > 0 || atomic_read(&zv->zv_suspend_ref)) { 1531 zv->zv_flags |= ZVOL_REMOVING; 1532 cv_wait(&zv->zv_removing_cv, &zv->zv_state_lock); 1533 } 1534 mutex_exit(&zv->zv_state_lock); 1535 1536 rw_enter(&zvol_state_lock, RW_WRITER); 1537 mutex_enter(&zv->zv_state_lock); 1538 1539 zvol_remove(zv); 1540 zvol_os_clear_private(zv); 1541 1542 mutex_exit(&zv->zv_state_lock); 1543 rw_exit(&zvol_state_lock); 1544 1545 zvol_os_free(zv); 1546 } 1547 1548 static void 1549 zvol_free_task(void *arg) 1550 { 1551 zvol_os_free(arg); 1552 } 1553 1554 void 1555 zvol_remove_minors_impl(const char *name) 1556 { 1557 zvol_state_t *zv, *zv_next; 1558 int namelen = ((name) ? strlen(name) : 0); 1559 taskqid_t t; 1560 list_t delay_list, free_list; 1561 1562 if (zvol_inhibit_dev) 1563 return; 1564 1565 list_create(&delay_list, sizeof (zvol_state_t), 1566 offsetof(zvol_state_t, zv_next)); 1567 list_create(&free_list, sizeof (zvol_state_t), 1568 offsetof(zvol_state_t, zv_next)); 1569 1570 rw_enter(&zvol_state_lock, RW_WRITER); 1571 1572 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) { 1573 zv_next = list_next(&zvol_state_list, zv); 1574 1575 mutex_enter(&zv->zv_state_lock); 1576 if (name == NULL || strcmp(zv->zv_name, name) == 0 || 1577 (strncmp(zv->zv_name, name, namelen) == 0 && 1578 (zv->zv_name[namelen] == '/' || 1579 zv->zv_name[namelen] == '@'))) { 1580 /* 1581 * By holding zv_state_lock here, we guarantee that no 1582 * one is currently using this zv 1583 */ 1584 1585 /* 1586 * If in use, try to throw everyone off and try again 1587 * later. 1588 */ 1589 if (zv->zv_open_count > 0 || 1590 atomic_read(&zv->zv_suspend_ref)) { 1591 zv->zv_flags |= ZVOL_REMOVING; 1592 t = taskq_dispatch( 1593 zv->zv_objset->os_spa->spa_zvol_taskq, 1594 zvol_remove_minor_task, zv, TQ_SLEEP); 1595 if (t == TASKQID_INVALID) { 1596 /* 1597 * Couldn't create the task, so we'll 1598 * do it in place once the loop is 1599 * finished. 1600 */ 1601 list_insert_head(&delay_list, zv); 1602 } 1603 mutex_exit(&zv->zv_state_lock); 1604 continue; 1605 } 1606 1607 zvol_remove(zv); 1608 1609 /* 1610 * Cleared while holding zvol_state_lock as a writer 1611 * which will prevent zvol_open() from opening it. 1612 */ 1613 zvol_os_clear_private(zv); 1614 1615 /* Drop zv_state_lock before zvol_free() */ 1616 mutex_exit(&zv->zv_state_lock); 1617 1618 /* Try parallel zv_free, if failed do it in place */ 1619 t = taskq_dispatch(system_taskq, zvol_free_task, zv, 1620 TQ_SLEEP); 1621 if (t == TASKQID_INVALID) 1622 list_insert_head(&free_list, zv); 1623 } else { 1624 mutex_exit(&zv->zv_state_lock); 1625 } 1626 } 1627 rw_exit(&zvol_state_lock); 1628 1629 /* Wait for zvols that we couldn't create a remove task for */ 1630 while ((zv = list_remove_head(&delay_list)) != NULL) 1631 zvol_remove_minor_task(zv); 1632 1633 /* Free any that we couldn't free in parallel earlier */ 1634 while ((zv = list_remove_head(&free_list)) != NULL) 1635 zvol_os_free(zv); 1636 } 1637 1638 /* Remove minor for this specific volume only */ 1639 static void 1640 zvol_remove_minor_impl(const char *name) 1641 { 1642 zvol_state_t *zv = NULL, *zv_next; 1643 1644 if (zvol_inhibit_dev) 1645 return; 1646 1647 rw_enter(&zvol_state_lock, RW_WRITER); 1648 1649 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) { 1650 zv_next = list_next(&zvol_state_list, zv); 1651 1652 mutex_enter(&zv->zv_state_lock); 1653 if (strcmp(zv->zv_name, name) == 0) 1654 /* Found, leave the the loop with zv_lock held */ 1655 break; 1656 mutex_exit(&zv->zv_state_lock); 1657 } 1658 1659 if (zv == NULL) { 1660 rw_exit(&zvol_state_lock); 1661 return; 1662 } 1663 1664 ASSERT(MUTEX_HELD(&zv->zv_state_lock)); 1665 1666 if (zv->zv_open_count > 0 || atomic_read(&zv->zv_suspend_ref)) { 1667 /* 1668 * In use, so try to throw everyone off, then wait 1669 * until finished. 1670 */ 1671 zv->zv_flags |= ZVOL_REMOVING; 1672 mutex_exit(&zv->zv_state_lock); 1673 rw_exit(&zvol_state_lock); 1674 zvol_remove_minor_task(zv); 1675 return; 1676 } 1677 1678 zvol_remove(zv); 1679 zvol_os_clear_private(zv); 1680 1681 mutex_exit(&zv->zv_state_lock); 1682 rw_exit(&zvol_state_lock); 1683 1684 zvol_os_free(zv); 1685 } 1686 1687 /* 1688 * Rename minors for specified dataset including children and snapshots. 1689 */ 1690 static void 1691 zvol_rename_minors_impl(const char *oldname, const char *newname) 1692 { 1693 zvol_state_t *zv, *zv_next; 1694 int oldnamelen; 1695 1696 if (zvol_inhibit_dev) 1697 return; 1698 1699 oldnamelen = strlen(oldname); 1700 1701 rw_enter(&zvol_state_lock, RW_READER); 1702 1703 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) { 1704 zv_next = list_next(&zvol_state_list, zv); 1705 1706 mutex_enter(&zv->zv_state_lock); 1707 1708 if (strcmp(zv->zv_name, oldname) == 0) { 1709 zvol_os_rename_minor(zv, newname); 1710 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 && 1711 (zv->zv_name[oldnamelen] == '/' || 1712 zv->zv_name[oldnamelen] == '@')) { 1713 char *name = kmem_asprintf("%s%c%s", newname, 1714 zv->zv_name[oldnamelen], 1715 zv->zv_name + oldnamelen + 1); 1716 zvol_os_rename_minor(zv, name); 1717 kmem_strfree(name); 1718 } 1719 1720 mutex_exit(&zv->zv_state_lock); 1721 } 1722 1723 rw_exit(&zvol_state_lock); 1724 } 1725 1726 typedef struct zvol_snapdev_cb_arg { 1727 uint64_t snapdev; 1728 } zvol_snapdev_cb_arg_t; 1729 1730 static int 1731 zvol_set_snapdev_cb(const char *dsname, void *param) 1732 { 1733 zvol_snapdev_cb_arg_t *arg = param; 1734 1735 if (strchr(dsname, '@') == NULL) 1736 return (0); 1737 1738 switch (arg->snapdev) { 1739 case ZFS_SNAPDEV_VISIBLE: 1740 (void) zvol_os_create_minor(dsname); 1741 break; 1742 case ZFS_SNAPDEV_HIDDEN: 1743 (void) zvol_remove_minor_impl(dsname); 1744 break; 1745 } 1746 1747 return (0); 1748 } 1749 1750 static void 1751 zvol_set_snapdev_impl(char *name, uint64_t snapdev) 1752 { 1753 zvol_snapdev_cb_arg_t arg = {snapdev}; 1754 fstrans_cookie_t cookie = spl_fstrans_mark(); 1755 /* 1756 * The zvol_set_snapdev_sync() sets snapdev appropriately 1757 * in the dataset hierarchy. Here, we only scan snapshots. 1758 */ 1759 dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS); 1760 spl_fstrans_unmark(cookie); 1761 } 1762 1763 static void 1764 zvol_set_volmode_impl(char *name, uint64_t volmode) 1765 { 1766 fstrans_cookie_t cookie; 1767 uint64_t old_volmode; 1768 zvol_state_t *zv; 1769 1770 if (strchr(name, '@') != NULL) 1771 return; 1772 1773 /* 1774 * It's unfortunate we need to remove minors before we create new ones: 1775 * this is necessary because our backing gendisk (zvol_state->zv_disk) 1776 * could be different when we set, for instance, volmode from "geom" 1777 * to "dev" (or vice versa). 1778 */ 1779 zv = zvol_find_by_name(name, RW_NONE); 1780 if (zv == NULL && volmode == ZFS_VOLMODE_NONE) 1781 return; 1782 if (zv != NULL) { 1783 old_volmode = zv->zv_volmode; 1784 mutex_exit(&zv->zv_state_lock); 1785 if (old_volmode == volmode) 1786 return; 1787 zvol_wait_close(zv); 1788 } 1789 cookie = spl_fstrans_mark(); 1790 switch (volmode) { 1791 case ZFS_VOLMODE_NONE: 1792 (void) zvol_remove_minor_impl(name); 1793 break; 1794 case ZFS_VOLMODE_GEOM: 1795 case ZFS_VOLMODE_DEV: 1796 (void) zvol_remove_minor_impl(name); 1797 (void) zvol_os_create_minor(name); 1798 break; 1799 case ZFS_VOLMODE_DEFAULT: 1800 (void) zvol_remove_minor_impl(name); 1801 if (zvol_volmode == ZFS_VOLMODE_NONE) 1802 break; 1803 else /* if zvol_volmode is invalid defaults to "geom" */ 1804 (void) zvol_os_create_minor(name); 1805 break; 1806 } 1807 spl_fstrans_unmark(cookie); 1808 } 1809 1810 static zvol_task_t * 1811 zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2, 1812 uint64_t value) 1813 { 1814 zvol_task_t *task; 1815 1816 /* Never allow tasks on hidden names. */ 1817 if (name1[0] == '$') 1818 return (NULL); 1819 1820 task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP); 1821 task->op = op; 1822 task->value = value; 1823 1824 strlcpy(task->name1, name1, sizeof (task->name1)); 1825 if (name2 != NULL) 1826 strlcpy(task->name2, name2, sizeof (task->name2)); 1827 1828 return (task); 1829 } 1830 1831 static void 1832 zvol_task_free(zvol_task_t *task) 1833 { 1834 kmem_free(task, sizeof (zvol_task_t)); 1835 } 1836 1837 /* 1838 * The worker thread function performed asynchronously. 1839 */ 1840 static void 1841 zvol_task_cb(void *arg) 1842 { 1843 zvol_task_t *task = arg; 1844 1845 switch (task->op) { 1846 case ZVOL_ASYNC_REMOVE_MINORS: 1847 zvol_remove_minors_impl(task->name1); 1848 break; 1849 case ZVOL_ASYNC_RENAME_MINORS: 1850 zvol_rename_minors_impl(task->name1, task->name2); 1851 break; 1852 case ZVOL_ASYNC_SET_SNAPDEV: 1853 zvol_set_snapdev_impl(task->name1, task->value); 1854 break; 1855 case ZVOL_ASYNC_SET_VOLMODE: 1856 zvol_set_volmode_impl(task->name1, task->value); 1857 break; 1858 default: 1859 VERIFY(0); 1860 break; 1861 } 1862 1863 zvol_task_free(task); 1864 } 1865 1866 typedef struct zvol_set_prop_int_arg { 1867 const char *zsda_name; 1868 uint64_t zsda_value; 1869 zprop_source_t zsda_source; 1870 zfs_prop_t zsda_prop; 1871 } zvol_set_prop_int_arg_t; 1872 1873 /* 1874 * Sanity check the dataset for safe use by the sync task. No additional 1875 * conditions are imposed. 1876 */ 1877 static int 1878 zvol_set_common_check(void *arg, dmu_tx_t *tx) 1879 { 1880 zvol_set_prop_int_arg_t *zsda = arg; 1881 dsl_pool_t *dp = dmu_tx_pool(tx); 1882 dsl_dir_t *dd; 1883 int error; 1884 1885 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL); 1886 if (error != 0) 1887 return (error); 1888 1889 dsl_dir_rele(dd, FTAG); 1890 1891 return (error); 1892 } 1893 1894 static int 1895 zvol_set_common_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 1896 { 1897 zvol_set_prop_int_arg_t *zsda = arg; 1898 char dsname[ZFS_MAX_DATASET_NAME_LEN]; 1899 zvol_task_t *task; 1900 uint64_t prop; 1901 1902 const char *prop_name = zfs_prop_to_name(zsda->zsda_prop); 1903 dsl_dataset_name(ds, dsname); 1904 1905 if (dsl_prop_get_int_ds(ds, prop_name, &prop) != 0) 1906 return (0); 1907 1908 switch (zsda->zsda_prop) { 1909 case ZFS_PROP_VOLMODE: 1910 task = zvol_task_alloc(ZVOL_ASYNC_SET_VOLMODE, dsname, 1911 NULL, prop); 1912 break; 1913 case ZFS_PROP_SNAPDEV: 1914 task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname, 1915 NULL, prop); 1916 break; 1917 default: 1918 task = NULL; 1919 break; 1920 } 1921 1922 if (task == NULL) 1923 return (0); 1924 1925 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb, 1926 task, TQ_SLEEP); 1927 return (0); 1928 } 1929 1930 /* 1931 * Traverse all child datasets and apply the property appropriately. 1932 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel 1933 * dataset and read the effective "property" on every child in the callback 1934 * function: this is because the value is not guaranteed to be the same in the 1935 * whole dataset hierarchy. 1936 */ 1937 static void 1938 zvol_set_common_sync(void *arg, dmu_tx_t *tx) 1939 { 1940 zvol_set_prop_int_arg_t *zsda = arg; 1941 dsl_pool_t *dp = dmu_tx_pool(tx); 1942 dsl_dir_t *dd; 1943 dsl_dataset_t *ds; 1944 int error; 1945 1946 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL)); 1947 1948 error = dsl_dataset_hold(dp, zsda->zsda_name, FTAG, &ds); 1949 if (error == 0) { 1950 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(zsda->zsda_prop), 1951 zsda->zsda_source, sizeof (zsda->zsda_value), 1, 1952 &zsda->zsda_value, tx); 1953 dsl_dataset_rele(ds, FTAG); 1954 } 1955 1956 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_common_sync_cb, 1957 zsda, DS_FIND_CHILDREN); 1958 1959 dsl_dir_rele(dd, FTAG); 1960 } 1961 1962 int 1963 zvol_set_common(const char *ddname, zfs_prop_t prop, zprop_source_t source, 1964 uint64_t val) 1965 { 1966 zvol_set_prop_int_arg_t zsda; 1967 1968 zsda.zsda_name = ddname; 1969 zsda.zsda_source = source; 1970 zsda.zsda_value = val; 1971 zsda.zsda_prop = prop; 1972 1973 return (dsl_sync_task(ddname, zvol_set_common_check, 1974 zvol_set_common_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE)); 1975 } 1976 1977 void 1978 zvol_remove_minors(spa_t *spa, const char *name, boolean_t async) 1979 { 1980 zvol_task_t *task; 1981 taskqid_t id; 1982 1983 task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL); 1984 if (task == NULL) 1985 return; 1986 1987 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP); 1988 if ((async == B_FALSE) && (id != TASKQID_INVALID)) 1989 taskq_wait_id(spa->spa_zvol_taskq, id); 1990 } 1991 1992 void 1993 zvol_rename_minors(spa_t *spa, const char *name1, const char *name2, 1994 boolean_t async) 1995 { 1996 zvol_task_t *task; 1997 taskqid_t id; 1998 1999 task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL); 2000 if (task == NULL) 2001 return; 2002 2003 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP); 2004 if ((async == B_FALSE) && (id != TASKQID_INVALID)) 2005 taskq_wait_id(spa->spa_zvol_taskq, id); 2006 } 2007 2008 boolean_t 2009 zvol_is_zvol(const char *name) 2010 { 2011 2012 return (zvol_os_is_zvol(name)); 2013 } 2014 2015 int 2016 zvol_init_impl(void) 2017 { 2018 int i; 2019 2020 list_create(&zvol_state_list, sizeof (zvol_state_t), 2021 offsetof(zvol_state_t, zv_next)); 2022 rw_init(&zvol_state_lock, NULL, RW_DEFAULT, NULL); 2023 2024 zvol_htable = kmem_alloc(ZVOL_HT_SIZE * sizeof (struct hlist_head), 2025 KM_SLEEP); 2026 for (i = 0; i < ZVOL_HT_SIZE; i++) 2027 INIT_HLIST_HEAD(&zvol_htable[i]); 2028 2029 return (0); 2030 } 2031 2032 void 2033 zvol_fini_impl(void) 2034 { 2035 zvol_remove_minors_impl(NULL); 2036 2037 /* 2038 * The call to "zvol_remove_minors_impl" may dispatch entries to 2039 * the system_taskq, but it doesn't wait for those entries to 2040 * complete before it returns. Thus, we must wait for all of the 2041 * removals to finish, before we can continue. 2042 */ 2043 taskq_wait_outstanding(system_taskq, 0); 2044 2045 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head)); 2046 list_destroy(&zvol_state_list); 2047 rw_destroy(&zvol_state_lock); 2048 } 2049