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