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