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 if (byteswap) 450 byteswap_uint64_array(lr, sizeof (*lr)); 451 452 offset = lr->lr_offset; 453 length = lr->lr_length; 454 455 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 456 dmu_tx_mark_netfree(tx); 457 int error = dmu_tx_assign(tx, TXG_WAIT); 458 if (error != 0) { 459 dmu_tx_abort(tx); 460 } else { 461 (void) zil_replaying(zv->zv_zilog, tx); 462 dmu_tx_commit(tx); 463 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, 464 length); 465 } 466 467 return (error); 468 } 469 470 /* 471 * Replay a TX_WRITE ZIL transaction that didn't get committed 472 * after a system failure 473 */ 474 static int 475 zvol_replay_write(void *arg1, void *arg2, boolean_t byteswap) 476 { 477 zvol_state_t *zv = arg1; 478 lr_write_t *lr = arg2; 479 objset_t *os = zv->zv_objset; 480 char *data = (char *)(lr + 1); /* data follows lr_write_t */ 481 uint64_t offset, length; 482 dmu_tx_t *tx; 483 int error; 484 485 if (byteswap) 486 byteswap_uint64_array(lr, sizeof (*lr)); 487 488 offset = lr->lr_offset; 489 length = lr->lr_length; 490 491 /* If it's a dmu_sync() block, write the whole block */ 492 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 493 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 494 if (length < blocksize) { 495 offset -= offset % blocksize; 496 length = blocksize; 497 } 498 } 499 500 tx = dmu_tx_create(os); 501 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 502 error = dmu_tx_assign(tx, TXG_WAIT); 503 if (error) { 504 dmu_tx_abort(tx); 505 } else { 506 dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 507 (void) zil_replaying(zv->zv_zilog, tx); 508 dmu_tx_commit(tx); 509 } 510 511 return (error); 512 } 513 514 /* 515 * Replay a TX_CLONE_RANGE ZIL transaction that didn't get committed 516 * after a system failure. 517 * 518 * TODO: For now we drop block cloning transations for ZVOLs as they are 519 * unsupported, but we still need to inform BRT about that as we 520 * claimed them during pool import. 521 * This situation can occur when we try to import a pool from a ZFS 522 * version supporting block cloning for ZVOLs into a system that 523 * has this ZFS version, that doesn't support block cloning for ZVOLs. 524 */ 525 static int 526 zvol_replay_clone_range(void *arg1, void *arg2, boolean_t byteswap) 527 { 528 char name[ZFS_MAX_DATASET_NAME_LEN]; 529 zvol_state_t *zv = arg1; 530 objset_t *os = zv->zv_objset; 531 lr_clone_range_t *lr = arg2; 532 blkptr_t *bp; 533 dmu_tx_t *tx; 534 spa_t *spa; 535 uint_t ii; 536 int error; 537 538 dmu_objset_name(os, name); 539 cmn_err(CE_WARN, "ZFS dropping block cloning transaction for %s.", 540 name); 541 542 if (byteswap) 543 byteswap_uint64_array(lr, sizeof (*lr)); 544 545 tx = dmu_tx_create(os); 546 error = dmu_tx_assign(tx, TXG_WAIT); 547 if (error) { 548 dmu_tx_abort(tx); 549 return (error); 550 } 551 552 spa = os->os_spa; 553 554 for (ii = 0; ii < lr->lr_nbps; ii++) { 555 bp = &lr->lr_bps[ii]; 556 557 if (!BP_IS_HOLE(bp)) { 558 zio_free(spa, dmu_tx_get_txg(tx), bp); 559 } 560 } 561 562 (void) zil_replaying(zv->zv_zilog, tx); 563 dmu_tx_commit(tx); 564 565 return (0); 566 } 567 568 static int 569 zvol_replay_err(void *arg1, void *arg2, boolean_t byteswap) 570 { 571 (void) arg1, (void) arg2, (void) byteswap; 572 return (SET_ERROR(ENOTSUP)); 573 } 574 575 /* 576 * Callback vectors for replaying records. 577 * Only TX_WRITE and TX_TRUNCATE are needed for zvol. 578 */ 579 zil_replay_func_t *const zvol_replay_vector[TX_MAX_TYPE] = { 580 zvol_replay_err, /* no such transaction type */ 581 zvol_replay_err, /* TX_CREATE */ 582 zvol_replay_err, /* TX_MKDIR */ 583 zvol_replay_err, /* TX_MKXATTR */ 584 zvol_replay_err, /* TX_SYMLINK */ 585 zvol_replay_err, /* TX_REMOVE */ 586 zvol_replay_err, /* TX_RMDIR */ 587 zvol_replay_err, /* TX_LINK */ 588 zvol_replay_err, /* TX_RENAME */ 589 zvol_replay_write, /* TX_WRITE */ 590 zvol_replay_truncate, /* TX_TRUNCATE */ 591 zvol_replay_err, /* TX_SETATTR */ 592 zvol_replay_err, /* TX_ACL */ 593 zvol_replay_err, /* TX_CREATE_ATTR */ 594 zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 595 zvol_replay_err, /* TX_MKDIR_ACL */ 596 zvol_replay_err, /* TX_MKDIR_ATTR */ 597 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 598 zvol_replay_err, /* TX_WRITE2 */ 599 zvol_replay_err, /* TX_SETSAXATTR */ 600 zvol_replay_err, /* TX_RENAME_EXCHANGE */ 601 zvol_replay_err, /* TX_RENAME_WHITEOUT */ 602 zvol_replay_clone_range /* TX_CLONE_RANGE */ 603 }; 604 605 /* 606 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 607 * 608 * We store data in the log buffers if it's small enough. 609 * Otherwise we will later flush the data out via dmu_sync(). 610 */ 611 static const ssize_t zvol_immediate_write_sz = 32768; 612 613 void 614 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset, 615 uint64_t size, boolean_t commit) 616 { 617 uint32_t blocksize = zv->zv_volblocksize; 618 zilog_t *zilog = zv->zv_zilog; 619 itx_wr_state_t write_state; 620 uint64_t sz = size; 621 622 if (zil_replaying(zilog, tx)) 623 return; 624 625 if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 626 write_state = WR_INDIRECT; 627 else if (!spa_has_slogs(zilog->zl_spa) && 628 size >= blocksize && blocksize > zvol_immediate_write_sz) 629 write_state = WR_INDIRECT; 630 else if (commit) 631 write_state = WR_COPIED; 632 else 633 write_state = WR_NEED_COPY; 634 635 while (size) { 636 itx_t *itx; 637 lr_write_t *lr; 638 itx_wr_state_t wr_state = write_state; 639 ssize_t len = size; 640 641 if (wr_state == WR_COPIED && size > zil_max_copied_data(zilog)) 642 wr_state = WR_NEED_COPY; 643 else if (wr_state == WR_INDIRECT) 644 len = MIN(blocksize - P2PHASE(offset, blocksize), size); 645 646 itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 647 (wr_state == WR_COPIED ? len : 0)); 648 lr = (lr_write_t *)&itx->itx_lr; 649 if (wr_state == WR_COPIED && dmu_read_by_dnode(zv->zv_dn, 650 offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) { 651 zil_itx_destroy(itx); 652 itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 653 lr = (lr_write_t *)&itx->itx_lr; 654 wr_state = WR_NEED_COPY; 655 } 656 657 itx->itx_wr_state = wr_state; 658 lr->lr_foid = ZVOL_OBJ; 659 lr->lr_offset = offset; 660 lr->lr_length = len; 661 lr->lr_blkoff = 0; 662 BP_ZERO(&lr->lr_blkptr); 663 664 itx->itx_private = zv; 665 666 (void) zil_itx_assign(zilog, itx, tx); 667 668 offset += len; 669 size -= len; 670 } 671 672 if (write_state == WR_COPIED || write_state == WR_NEED_COPY) { 673 dsl_pool_wrlog_count(zilog->zl_dmu_pool, sz, tx->tx_txg); 674 } 675 } 676 677 /* 678 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE. 679 */ 680 void 681 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len) 682 { 683 itx_t *itx; 684 lr_truncate_t *lr; 685 zilog_t *zilog = zv->zv_zilog; 686 687 if (zil_replaying(zilog, tx)) 688 return; 689 690 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); 691 lr = (lr_truncate_t *)&itx->itx_lr; 692 lr->lr_foid = ZVOL_OBJ; 693 lr->lr_offset = off; 694 lr->lr_length = len; 695 696 zil_itx_assign(zilog, itx, tx); 697 } 698 699 700 static void 701 zvol_get_done(zgd_t *zgd, int error) 702 { 703 (void) error; 704 if (zgd->zgd_db) 705 dmu_buf_rele(zgd->zgd_db, zgd); 706 707 zfs_rangelock_exit(zgd->zgd_lr); 708 709 kmem_free(zgd, sizeof (zgd_t)); 710 } 711 712 /* 713 * Get data to generate a TX_WRITE intent log record. 714 */ 715 int 716 zvol_get_data(void *arg, uint64_t arg2, lr_write_t *lr, char *buf, 717 struct lwb *lwb, zio_t *zio) 718 { 719 zvol_state_t *zv = arg; 720 uint64_t offset = lr->lr_offset; 721 uint64_t size = lr->lr_length; 722 dmu_buf_t *db; 723 zgd_t *zgd; 724 int error; 725 726 ASSERT3P(lwb, !=, NULL); 727 ASSERT3U(size, !=, 0); 728 729 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 730 zgd->zgd_lwb = lwb; 731 732 /* 733 * Write records come in two flavors: immediate and indirect. 734 * For small writes it's cheaper to store the data with the 735 * log record (immediate); for large writes it's cheaper to 736 * sync the data and get a pointer to it (indirect) so that 737 * we don't have to write the data twice. 738 */ 739 if (buf != NULL) { /* immediate write */ 740 zgd->zgd_lr = zfs_rangelock_enter(&zv->zv_rangelock, offset, 741 size, RL_READER); 742 error = dmu_read_by_dnode(zv->zv_dn, offset, size, buf, 743 DMU_READ_NO_PREFETCH); 744 } else { /* indirect write */ 745 ASSERT3P(zio, !=, NULL); 746 /* 747 * Have to lock the whole block to ensure when it's written out 748 * and its checksum is being calculated that no one can change 749 * the data. Contrarily to zfs_get_data we need not re-check 750 * blocksize after we get the lock because it cannot be changed. 751 */ 752 size = zv->zv_volblocksize; 753 offset = P2ALIGN_TYPED(offset, size, uint64_t); 754 zgd->zgd_lr = zfs_rangelock_enter(&zv->zv_rangelock, offset, 755 size, RL_READER); 756 error = dmu_buf_hold_noread_by_dnode(zv->zv_dn, offset, zgd, 757 &db); 758 if (error == 0) { 759 blkptr_t *bp = &lr->lr_blkptr; 760 761 zgd->zgd_db = db; 762 zgd->zgd_bp = bp; 763 764 ASSERT(db != NULL); 765 ASSERT(db->db_offset == offset); 766 ASSERT(db->db_size == size); 767 768 error = dmu_sync(zio, lr->lr_common.lrc_txg, 769 zvol_get_done, zgd); 770 771 if (error == 0) 772 return (0); 773 } 774 } 775 776 zvol_get_done(zgd, error); 777 778 return (SET_ERROR(error)); 779 } 780 781 /* 782 * The zvol_state_t's are inserted into zvol_state_list and zvol_htable. 783 */ 784 785 void 786 zvol_insert(zvol_state_t *zv) 787 { 788 ASSERT(RW_WRITE_HELD(&zvol_state_lock)); 789 list_insert_head(&zvol_state_list, zv); 790 hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash)); 791 } 792 793 /* 794 * Simply remove the zvol from to list of zvols. 795 */ 796 static void 797 zvol_remove(zvol_state_t *zv) 798 { 799 ASSERT(RW_WRITE_HELD(&zvol_state_lock)); 800 list_remove(&zvol_state_list, zv); 801 hlist_del(&zv->zv_hlink); 802 } 803 804 /* 805 * Setup zv after we just own the zv->objset 806 */ 807 static int 808 zvol_setup_zv(zvol_state_t *zv) 809 { 810 uint64_t volsize; 811 int error; 812 uint64_t ro; 813 objset_t *os = zv->zv_objset; 814 815 ASSERT(MUTEX_HELD(&zv->zv_state_lock)); 816 ASSERT(RW_LOCK_HELD(&zv->zv_suspend_lock)); 817 818 zv->zv_zilog = NULL; 819 zv->zv_flags &= ~ZVOL_WRITTEN_TO; 820 821 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL); 822 if (error) 823 return (SET_ERROR(error)); 824 825 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 826 if (error) 827 return (SET_ERROR(error)); 828 829 error = dnode_hold(os, ZVOL_OBJ, zv, &zv->zv_dn); 830 if (error) 831 return (SET_ERROR(error)); 832 833 zvol_os_set_capacity(zv, volsize >> 9); 834 zv->zv_volsize = volsize; 835 836 if (ro || dmu_objset_is_snapshot(os) || 837 !spa_writeable(dmu_objset_spa(os))) { 838 zvol_os_set_disk_ro(zv, 1); 839 zv->zv_flags |= ZVOL_RDONLY; 840 } else { 841 zvol_os_set_disk_ro(zv, 0); 842 zv->zv_flags &= ~ZVOL_RDONLY; 843 } 844 return (0); 845 } 846 847 /* 848 * Shutdown every zv_objset related stuff except zv_objset itself. 849 * The is the reverse of zvol_setup_zv. 850 */ 851 static void 852 zvol_shutdown_zv(zvol_state_t *zv) 853 { 854 ASSERT(MUTEX_HELD(&zv->zv_state_lock) && 855 RW_LOCK_HELD(&zv->zv_suspend_lock)); 856 857 if (zv->zv_flags & ZVOL_WRITTEN_TO) { 858 ASSERT(zv->zv_zilog != NULL); 859 zil_close(zv->zv_zilog); 860 } 861 862 zv->zv_zilog = NULL; 863 864 dnode_rele(zv->zv_dn, zv); 865 zv->zv_dn = NULL; 866 867 /* 868 * Evict cached data. We must write out any dirty data before 869 * disowning the dataset. 870 */ 871 if (zv->zv_flags & ZVOL_WRITTEN_TO) 872 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 873 (void) dmu_objset_evict_dbufs(zv->zv_objset); 874 } 875 876 /* 877 * return the proper tag for rollback and recv 878 */ 879 void * 880 zvol_tag(zvol_state_t *zv) 881 { 882 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock)); 883 return (zv->zv_open_count > 0 ? zv : NULL); 884 } 885 886 /* 887 * Suspend the zvol for recv and rollback. 888 */ 889 zvol_state_t * 890 zvol_suspend(const char *name) 891 { 892 zvol_state_t *zv; 893 894 zv = zvol_find_by_name(name, RW_WRITER); 895 896 if (zv == NULL) 897 return (NULL); 898 899 /* block all I/O, release in zvol_resume. */ 900 ASSERT(MUTEX_HELD(&zv->zv_state_lock)); 901 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock)); 902 903 atomic_inc(&zv->zv_suspend_ref); 904 905 if (zv->zv_open_count > 0) 906 zvol_shutdown_zv(zv); 907 908 /* 909 * do not hold zv_state_lock across suspend/resume to 910 * avoid locking up zvol lookups 911 */ 912 mutex_exit(&zv->zv_state_lock); 913 914 /* zv_suspend_lock is released in zvol_resume() */ 915 return (zv); 916 } 917 918 int 919 zvol_resume(zvol_state_t *zv) 920 { 921 int error = 0; 922 923 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock)); 924 925 mutex_enter(&zv->zv_state_lock); 926 927 if (zv->zv_open_count > 0) { 928 VERIFY0(dmu_objset_hold(zv->zv_name, zv, &zv->zv_objset)); 929 VERIFY3P(zv->zv_objset->os_dsl_dataset->ds_owner, ==, zv); 930 VERIFY(dsl_dataset_long_held(zv->zv_objset->os_dsl_dataset)); 931 dmu_objset_rele(zv->zv_objset, zv); 932 933 error = zvol_setup_zv(zv); 934 } 935 936 mutex_exit(&zv->zv_state_lock); 937 938 rw_exit(&zv->zv_suspend_lock); 939 /* 940 * We need this because we don't hold zvol_state_lock while releasing 941 * zv_suspend_lock. zvol_remove_minors_impl thus cannot check 942 * zv_suspend_lock to determine it is safe to free because rwlock is 943 * not inherent atomic. 944 */ 945 atomic_dec(&zv->zv_suspend_ref); 946 947 return (SET_ERROR(error)); 948 } 949 950 int 951 zvol_first_open(zvol_state_t *zv, boolean_t readonly) 952 { 953 objset_t *os; 954 int error; 955 956 ASSERT(RW_READ_HELD(&zv->zv_suspend_lock)); 957 ASSERT(MUTEX_HELD(&zv->zv_state_lock)); 958 ASSERT(mutex_owned(&spa_namespace_lock)); 959 960 boolean_t ro = (readonly || (strchr(zv->zv_name, '@') != NULL)); 961 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, ro, B_TRUE, zv, &os); 962 if (error) 963 return (SET_ERROR(error)); 964 965 zv->zv_objset = os; 966 967 error = zvol_setup_zv(zv); 968 if (error) { 969 dmu_objset_disown(os, 1, zv); 970 zv->zv_objset = NULL; 971 } 972 973 return (error); 974 } 975 976 void 977 zvol_last_close(zvol_state_t *zv) 978 { 979 ASSERT(RW_READ_HELD(&zv->zv_suspend_lock)); 980 ASSERT(MUTEX_HELD(&zv->zv_state_lock)); 981 982 zvol_shutdown_zv(zv); 983 984 dmu_objset_disown(zv->zv_objset, 1, zv); 985 zv->zv_objset = NULL; 986 } 987 988 typedef struct minors_job { 989 list_t *list; 990 list_node_t link; 991 /* input */ 992 char *name; 993 /* output */ 994 int error; 995 } minors_job_t; 996 997 /* 998 * Prefetch zvol dnodes for the minors_job 999 */ 1000 static void 1001 zvol_prefetch_minors_impl(void *arg) 1002 { 1003 minors_job_t *job = arg; 1004 char *dsname = job->name; 1005 objset_t *os = NULL; 1006 1007 job->error = dmu_objset_own(dsname, DMU_OST_ZVOL, B_TRUE, B_TRUE, 1008 FTAG, &os); 1009 if (job->error == 0) { 1010 dmu_prefetch_dnode(os, ZVOL_OBJ, ZIO_PRIORITY_SYNC_READ); 1011 dmu_objset_disown(os, B_TRUE, FTAG); 1012 } 1013 } 1014 1015 /* 1016 * Mask errors to continue dmu_objset_find() traversal 1017 */ 1018 static int 1019 zvol_create_snap_minor_cb(const char *dsname, void *arg) 1020 { 1021 minors_job_t *j = arg; 1022 list_t *minors_list = j->list; 1023 const char *name = j->name; 1024 1025 ASSERT0(MUTEX_HELD(&spa_namespace_lock)); 1026 1027 /* skip the designated dataset */ 1028 if (name && strcmp(dsname, name) == 0) 1029 return (0); 1030 1031 /* at this point, the dsname should name a snapshot */ 1032 if (strchr(dsname, '@') == 0) { 1033 dprintf("zvol_create_snap_minor_cb(): " 1034 "%s is not a snapshot name\n", dsname); 1035 } else { 1036 minors_job_t *job; 1037 char *n = kmem_strdup(dsname); 1038 if (n == NULL) 1039 return (0); 1040 1041 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP); 1042 job->name = n; 1043 job->list = minors_list; 1044 job->error = 0; 1045 list_insert_tail(minors_list, job); 1046 /* don't care if dispatch fails, because job->error is 0 */ 1047 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job, 1048 TQ_SLEEP); 1049 } 1050 1051 return (0); 1052 } 1053 1054 /* 1055 * If spa_keystore_load_wkey() is called for an encrypted zvol, 1056 * we need to look for any clones also using the key. This function 1057 * is "best effort" - so we just skip over it if there are failures. 1058 */ 1059 static void 1060 zvol_add_clones(const char *dsname, list_t *minors_list) 1061 { 1062 /* Also check if it has clones */ 1063 dsl_dir_t *dd = NULL; 1064 dsl_pool_t *dp = NULL; 1065 1066 if (dsl_pool_hold(dsname, FTAG, &dp) != 0) 1067 return; 1068 1069 if (!spa_feature_is_enabled(dp->dp_spa, 1070 SPA_FEATURE_ENCRYPTION)) 1071 goto out; 1072 1073 if (dsl_dir_hold(dp, dsname, FTAG, &dd, NULL) != 0) 1074 goto out; 1075 1076 if (dsl_dir_phys(dd)->dd_clones == 0) 1077 goto out; 1078 1079 zap_cursor_t *zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP); 1080 zap_attribute_t *za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 1081 objset_t *mos = dd->dd_pool->dp_meta_objset; 1082 1083 for (zap_cursor_init(zc, mos, dsl_dir_phys(dd)->dd_clones); 1084 zap_cursor_retrieve(zc, za) == 0; 1085 zap_cursor_advance(zc)) { 1086 dsl_dataset_t *clone; 1087 minors_job_t *job; 1088 1089 if (dsl_dataset_hold_obj(dd->dd_pool, 1090 za->za_first_integer, FTAG, &clone) == 0) { 1091 1092 char name[ZFS_MAX_DATASET_NAME_LEN]; 1093 dsl_dataset_name(clone, name); 1094 1095 char *n = kmem_strdup(name); 1096 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP); 1097 job->name = n; 1098 job->list = minors_list; 1099 job->error = 0; 1100 list_insert_tail(minors_list, job); 1101 1102 dsl_dataset_rele(clone, FTAG); 1103 } 1104 } 1105 zap_cursor_fini(zc); 1106 kmem_free(za, sizeof (zap_attribute_t)); 1107 kmem_free(zc, sizeof (zap_cursor_t)); 1108 1109 out: 1110 if (dd != NULL) 1111 dsl_dir_rele(dd, FTAG); 1112 dsl_pool_rele(dp, FTAG); 1113 } 1114 1115 /* 1116 * Mask errors to continue dmu_objset_find() traversal 1117 */ 1118 static int 1119 zvol_create_minors_cb(const char *dsname, void *arg) 1120 { 1121 uint64_t snapdev; 1122 int error; 1123 list_t *minors_list = arg; 1124 1125 ASSERT0(MUTEX_HELD(&spa_namespace_lock)); 1126 1127 error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL); 1128 if (error) 1129 return (0); 1130 1131 /* 1132 * Given the name and the 'snapdev' property, create device minor nodes 1133 * with the linkages to zvols/snapshots as needed. 1134 * If the name represents a zvol, create a minor node for the zvol, then 1135 * check if its snapshots are 'visible', and if so, iterate over the 1136 * snapshots and create device minor nodes for those. 1137 */ 1138 if (strchr(dsname, '@') == 0) { 1139 minors_job_t *job; 1140 char *n = kmem_strdup(dsname); 1141 if (n == NULL) 1142 return (0); 1143 1144 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP); 1145 job->name = n; 1146 job->list = minors_list; 1147 job->error = 0; 1148 list_insert_tail(minors_list, job); 1149 /* don't care if dispatch fails, because job->error is 0 */ 1150 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job, 1151 TQ_SLEEP); 1152 1153 zvol_add_clones(dsname, minors_list); 1154 1155 if (snapdev == ZFS_SNAPDEV_VISIBLE) { 1156 /* 1157 * traverse snapshots only, do not traverse children, 1158 * and skip the 'dsname' 1159 */ 1160 (void) dmu_objset_find(dsname, 1161 zvol_create_snap_minor_cb, (void *)job, 1162 DS_FIND_SNAPSHOTS); 1163 } 1164 } else { 1165 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n", 1166 dsname); 1167 } 1168 1169 return (0); 1170 } 1171 1172 /* 1173 * Create minors for the specified dataset, including children and snapshots. 1174 * Pay attention to the 'snapdev' property and iterate over the snapshots 1175 * only if they are 'visible'. This approach allows one to assure that the 1176 * snapshot metadata is read from disk only if it is needed. 1177 * 1178 * The name can represent a dataset to be recursively scanned for zvols and 1179 * their snapshots, or a single zvol snapshot. If the name represents a 1180 * dataset, the scan is performed in two nested stages: 1181 * - scan the dataset for zvols, and 1182 * - for each zvol, create a minor node, then check if the zvol's snapshots 1183 * are 'visible', and only then iterate over the snapshots if needed 1184 * 1185 * If the name represents a snapshot, a check is performed if the snapshot is 1186 * 'visible' (which also verifies that the parent is a zvol), and if so, 1187 * a minor node for that snapshot is created. 1188 */ 1189 void 1190 zvol_create_minors_recursive(const char *name) 1191 { 1192 list_t minors_list; 1193 minors_job_t *job; 1194 1195 if (zvol_inhibit_dev) 1196 return; 1197 1198 /* 1199 * This is the list for prefetch jobs. Whenever we found a match 1200 * during dmu_objset_find, we insert a minors_job to the list and do 1201 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need 1202 * any lock because all list operation is done on the current thread. 1203 * 1204 * We will use this list to do zvol_os_create_minor after prefetch 1205 * so we don't have to traverse using dmu_objset_find again. 1206 */ 1207 list_create(&minors_list, sizeof (minors_job_t), 1208 offsetof(minors_job_t, link)); 1209 1210 1211 if (strchr(name, '@') != NULL) { 1212 uint64_t snapdev; 1213 1214 int error = dsl_prop_get_integer(name, "snapdev", 1215 &snapdev, NULL); 1216 1217 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE) 1218 (void) zvol_os_create_minor(name); 1219 } else { 1220 fstrans_cookie_t cookie = spl_fstrans_mark(); 1221 (void) dmu_objset_find(name, zvol_create_minors_cb, 1222 &minors_list, DS_FIND_CHILDREN); 1223 spl_fstrans_unmark(cookie); 1224 } 1225 1226 taskq_wait_outstanding(system_taskq, 0); 1227 1228 /* 1229 * Prefetch is completed, we can do zvol_os_create_minor 1230 * sequentially. 1231 */ 1232 while ((job = list_remove_head(&minors_list)) != NULL) { 1233 if (!job->error) 1234 (void) zvol_os_create_minor(job->name); 1235 kmem_strfree(job->name); 1236 kmem_free(job, sizeof (minors_job_t)); 1237 } 1238 1239 list_destroy(&minors_list); 1240 } 1241 1242 void 1243 zvol_create_minor(const char *name) 1244 { 1245 /* 1246 * Note: the dsl_pool_config_lock must not be held. 1247 * Minor node creation needs to obtain the zvol_state_lock. 1248 * zvol_open() obtains the zvol_state_lock and then the dsl pool 1249 * config lock. Therefore, we can't have the config lock now if 1250 * we are going to wait for the zvol_state_lock, because it 1251 * would be a lock order inversion which could lead to deadlock. 1252 */ 1253 1254 if (zvol_inhibit_dev) 1255 return; 1256 1257 if (strchr(name, '@') != NULL) { 1258 uint64_t snapdev; 1259 1260 int error = dsl_prop_get_integer(name, 1261 "snapdev", &snapdev, NULL); 1262 1263 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE) 1264 (void) zvol_os_create_minor(name); 1265 } else { 1266 (void) zvol_os_create_minor(name); 1267 } 1268 } 1269 1270 /* 1271 * Remove minors for specified dataset including children and snapshots. 1272 */ 1273 1274 static void 1275 zvol_free_task(void *arg) 1276 { 1277 zvol_os_free(arg); 1278 } 1279 1280 void 1281 zvol_remove_minors_impl(const char *name) 1282 { 1283 zvol_state_t *zv, *zv_next; 1284 int namelen = ((name) ? strlen(name) : 0); 1285 taskqid_t t; 1286 list_t free_list; 1287 1288 if (zvol_inhibit_dev) 1289 return; 1290 1291 list_create(&free_list, sizeof (zvol_state_t), 1292 offsetof(zvol_state_t, zv_next)); 1293 1294 rw_enter(&zvol_state_lock, RW_WRITER); 1295 1296 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) { 1297 zv_next = list_next(&zvol_state_list, zv); 1298 1299 mutex_enter(&zv->zv_state_lock); 1300 if (name == NULL || strcmp(zv->zv_name, name) == 0 || 1301 (strncmp(zv->zv_name, name, namelen) == 0 && 1302 (zv->zv_name[namelen] == '/' || 1303 zv->zv_name[namelen] == '@'))) { 1304 /* 1305 * By holding zv_state_lock here, we guarantee that no 1306 * one is currently using this zv 1307 */ 1308 1309 /* If in use, leave alone */ 1310 if (zv->zv_open_count > 0 || 1311 atomic_read(&zv->zv_suspend_ref)) { 1312 mutex_exit(&zv->zv_state_lock); 1313 continue; 1314 } 1315 1316 zvol_remove(zv); 1317 1318 /* 1319 * Cleared while holding zvol_state_lock as a writer 1320 * which will prevent zvol_open() from opening it. 1321 */ 1322 zvol_os_clear_private(zv); 1323 1324 /* Drop zv_state_lock before zvol_free() */ 1325 mutex_exit(&zv->zv_state_lock); 1326 1327 /* Try parallel zv_free, if failed do it in place */ 1328 t = taskq_dispatch(system_taskq, zvol_free_task, zv, 1329 TQ_SLEEP); 1330 if (t == TASKQID_INVALID) 1331 list_insert_head(&free_list, zv); 1332 } else { 1333 mutex_exit(&zv->zv_state_lock); 1334 } 1335 } 1336 rw_exit(&zvol_state_lock); 1337 1338 /* Drop zvol_state_lock before calling zvol_free() */ 1339 while ((zv = list_remove_head(&free_list)) != NULL) 1340 zvol_os_free(zv); 1341 } 1342 1343 /* Remove minor for this specific volume only */ 1344 static void 1345 zvol_remove_minor_impl(const char *name) 1346 { 1347 zvol_state_t *zv = NULL, *zv_next; 1348 1349 if (zvol_inhibit_dev) 1350 return; 1351 1352 rw_enter(&zvol_state_lock, RW_WRITER); 1353 1354 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) { 1355 zv_next = list_next(&zvol_state_list, zv); 1356 1357 mutex_enter(&zv->zv_state_lock); 1358 if (strcmp(zv->zv_name, name) == 0) { 1359 /* 1360 * By holding zv_state_lock here, we guarantee that no 1361 * one is currently using this zv 1362 */ 1363 1364 /* If in use, leave alone */ 1365 if (zv->zv_open_count > 0 || 1366 atomic_read(&zv->zv_suspend_ref)) { 1367 mutex_exit(&zv->zv_state_lock); 1368 continue; 1369 } 1370 zvol_remove(zv); 1371 1372 zvol_os_clear_private(zv); 1373 mutex_exit(&zv->zv_state_lock); 1374 break; 1375 } else { 1376 mutex_exit(&zv->zv_state_lock); 1377 } 1378 } 1379 1380 /* Drop zvol_state_lock before calling zvol_free() */ 1381 rw_exit(&zvol_state_lock); 1382 1383 if (zv != NULL) 1384 zvol_os_free(zv); 1385 } 1386 1387 /* 1388 * Rename minors for specified dataset including children and snapshots. 1389 */ 1390 static void 1391 zvol_rename_minors_impl(const char *oldname, const char *newname) 1392 { 1393 zvol_state_t *zv, *zv_next; 1394 int oldnamelen; 1395 1396 if (zvol_inhibit_dev) 1397 return; 1398 1399 oldnamelen = strlen(oldname); 1400 1401 rw_enter(&zvol_state_lock, RW_READER); 1402 1403 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) { 1404 zv_next = list_next(&zvol_state_list, zv); 1405 1406 mutex_enter(&zv->zv_state_lock); 1407 1408 if (strcmp(zv->zv_name, oldname) == 0) { 1409 zvol_os_rename_minor(zv, newname); 1410 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 && 1411 (zv->zv_name[oldnamelen] == '/' || 1412 zv->zv_name[oldnamelen] == '@')) { 1413 char *name = kmem_asprintf("%s%c%s", newname, 1414 zv->zv_name[oldnamelen], 1415 zv->zv_name + oldnamelen + 1); 1416 zvol_os_rename_minor(zv, name); 1417 kmem_strfree(name); 1418 } 1419 1420 mutex_exit(&zv->zv_state_lock); 1421 } 1422 1423 rw_exit(&zvol_state_lock); 1424 } 1425 1426 typedef struct zvol_snapdev_cb_arg { 1427 uint64_t snapdev; 1428 } zvol_snapdev_cb_arg_t; 1429 1430 static int 1431 zvol_set_snapdev_cb(const char *dsname, void *param) 1432 { 1433 zvol_snapdev_cb_arg_t *arg = param; 1434 1435 if (strchr(dsname, '@') == NULL) 1436 return (0); 1437 1438 switch (arg->snapdev) { 1439 case ZFS_SNAPDEV_VISIBLE: 1440 (void) zvol_os_create_minor(dsname); 1441 break; 1442 case ZFS_SNAPDEV_HIDDEN: 1443 (void) zvol_remove_minor_impl(dsname); 1444 break; 1445 } 1446 1447 return (0); 1448 } 1449 1450 static void 1451 zvol_set_snapdev_impl(char *name, uint64_t snapdev) 1452 { 1453 zvol_snapdev_cb_arg_t arg = {snapdev}; 1454 fstrans_cookie_t cookie = spl_fstrans_mark(); 1455 /* 1456 * The zvol_set_snapdev_sync() sets snapdev appropriately 1457 * in the dataset hierarchy. Here, we only scan snapshots. 1458 */ 1459 dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS); 1460 spl_fstrans_unmark(cookie); 1461 } 1462 1463 static void 1464 zvol_set_volmode_impl(char *name, uint64_t volmode) 1465 { 1466 fstrans_cookie_t cookie; 1467 uint64_t old_volmode; 1468 zvol_state_t *zv; 1469 1470 if (strchr(name, '@') != NULL) 1471 return; 1472 1473 /* 1474 * It's unfortunate we need to remove minors before we create new ones: 1475 * this is necessary because our backing gendisk (zvol_state->zv_disk) 1476 * could be different when we set, for instance, volmode from "geom" 1477 * to "dev" (or vice versa). 1478 */ 1479 zv = zvol_find_by_name(name, RW_NONE); 1480 if (zv == NULL && volmode == ZFS_VOLMODE_NONE) 1481 return; 1482 if (zv != NULL) { 1483 old_volmode = zv->zv_volmode; 1484 mutex_exit(&zv->zv_state_lock); 1485 if (old_volmode == volmode) 1486 return; 1487 zvol_wait_close(zv); 1488 } 1489 cookie = spl_fstrans_mark(); 1490 switch (volmode) { 1491 case ZFS_VOLMODE_NONE: 1492 (void) zvol_remove_minor_impl(name); 1493 break; 1494 case ZFS_VOLMODE_GEOM: 1495 case ZFS_VOLMODE_DEV: 1496 (void) zvol_remove_minor_impl(name); 1497 (void) zvol_os_create_minor(name); 1498 break; 1499 case ZFS_VOLMODE_DEFAULT: 1500 (void) zvol_remove_minor_impl(name); 1501 if (zvol_volmode == ZFS_VOLMODE_NONE) 1502 break; 1503 else /* if zvol_volmode is invalid defaults to "geom" */ 1504 (void) zvol_os_create_minor(name); 1505 break; 1506 } 1507 spl_fstrans_unmark(cookie); 1508 } 1509 1510 static zvol_task_t * 1511 zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2, 1512 uint64_t value) 1513 { 1514 zvol_task_t *task; 1515 1516 /* Never allow tasks on hidden names. */ 1517 if (name1[0] == '$') 1518 return (NULL); 1519 1520 task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP); 1521 task->op = op; 1522 task->value = value; 1523 1524 strlcpy(task->name1, name1, sizeof (task->name1)); 1525 if (name2 != NULL) 1526 strlcpy(task->name2, name2, sizeof (task->name2)); 1527 1528 return (task); 1529 } 1530 1531 static void 1532 zvol_task_free(zvol_task_t *task) 1533 { 1534 kmem_free(task, sizeof (zvol_task_t)); 1535 } 1536 1537 /* 1538 * The worker thread function performed asynchronously. 1539 */ 1540 static void 1541 zvol_task_cb(void *arg) 1542 { 1543 zvol_task_t *task = arg; 1544 1545 switch (task->op) { 1546 case ZVOL_ASYNC_REMOVE_MINORS: 1547 zvol_remove_minors_impl(task->name1); 1548 break; 1549 case ZVOL_ASYNC_RENAME_MINORS: 1550 zvol_rename_minors_impl(task->name1, task->name2); 1551 break; 1552 case ZVOL_ASYNC_SET_SNAPDEV: 1553 zvol_set_snapdev_impl(task->name1, task->value); 1554 break; 1555 case ZVOL_ASYNC_SET_VOLMODE: 1556 zvol_set_volmode_impl(task->name1, task->value); 1557 break; 1558 default: 1559 VERIFY(0); 1560 break; 1561 } 1562 1563 zvol_task_free(task); 1564 } 1565 1566 typedef struct zvol_set_prop_int_arg { 1567 const char *zsda_name; 1568 uint64_t zsda_value; 1569 zprop_source_t zsda_source; 1570 zfs_prop_t zsda_prop; 1571 } zvol_set_prop_int_arg_t; 1572 1573 /* 1574 * Sanity check the dataset for safe use by the sync task. No additional 1575 * conditions are imposed. 1576 */ 1577 static int 1578 zvol_set_common_check(void *arg, dmu_tx_t *tx) 1579 { 1580 zvol_set_prop_int_arg_t *zsda = arg; 1581 dsl_pool_t *dp = dmu_tx_pool(tx); 1582 dsl_dir_t *dd; 1583 int error; 1584 1585 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL); 1586 if (error != 0) 1587 return (error); 1588 1589 dsl_dir_rele(dd, FTAG); 1590 1591 return (error); 1592 } 1593 1594 static int 1595 zvol_set_common_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 1596 { 1597 zvol_set_prop_int_arg_t *zsda = arg; 1598 char dsname[ZFS_MAX_DATASET_NAME_LEN]; 1599 zvol_task_t *task; 1600 uint64_t prop; 1601 1602 const char *prop_name = zfs_prop_to_name(zsda->zsda_prop); 1603 dsl_dataset_name(ds, dsname); 1604 1605 if (dsl_prop_get_int_ds(ds, prop_name, &prop) != 0) 1606 return (0); 1607 1608 switch (zsda->zsda_prop) { 1609 case ZFS_PROP_VOLMODE: 1610 task = zvol_task_alloc(ZVOL_ASYNC_SET_VOLMODE, dsname, 1611 NULL, prop); 1612 break; 1613 case ZFS_PROP_SNAPDEV: 1614 task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname, 1615 NULL, prop); 1616 break; 1617 default: 1618 task = NULL; 1619 break; 1620 } 1621 1622 if (task == NULL) 1623 return (0); 1624 1625 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb, 1626 task, TQ_SLEEP); 1627 return (0); 1628 } 1629 1630 /* 1631 * Traverse all child datasets and apply the property appropriately. 1632 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel 1633 * dataset and read the effective "property" on every child in the callback 1634 * function: this is because the value is not guaranteed to be the same in the 1635 * whole dataset hierarchy. 1636 */ 1637 static void 1638 zvol_set_common_sync(void *arg, dmu_tx_t *tx) 1639 { 1640 zvol_set_prop_int_arg_t *zsda = arg; 1641 dsl_pool_t *dp = dmu_tx_pool(tx); 1642 dsl_dir_t *dd; 1643 dsl_dataset_t *ds; 1644 int error; 1645 1646 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL)); 1647 1648 error = dsl_dataset_hold(dp, zsda->zsda_name, FTAG, &ds); 1649 if (error == 0) { 1650 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(zsda->zsda_prop), 1651 zsda->zsda_source, sizeof (zsda->zsda_value), 1, 1652 &zsda->zsda_value, tx); 1653 dsl_dataset_rele(ds, FTAG); 1654 } 1655 1656 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_common_sync_cb, 1657 zsda, DS_FIND_CHILDREN); 1658 1659 dsl_dir_rele(dd, FTAG); 1660 } 1661 1662 int 1663 zvol_set_common(const char *ddname, zfs_prop_t prop, zprop_source_t source, 1664 uint64_t val) 1665 { 1666 zvol_set_prop_int_arg_t zsda; 1667 1668 zsda.zsda_name = ddname; 1669 zsda.zsda_source = source; 1670 zsda.zsda_value = val; 1671 zsda.zsda_prop = prop; 1672 1673 return (dsl_sync_task(ddname, zvol_set_common_check, 1674 zvol_set_common_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE)); 1675 } 1676 1677 void 1678 zvol_remove_minors(spa_t *spa, const char *name, boolean_t async) 1679 { 1680 zvol_task_t *task; 1681 taskqid_t id; 1682 1683 task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL); 1684 if (task == NULL) 1685 return; 1686 1687 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP); 1688 if ((async == B_FALSE) && (id != TASKQID_INVALID)) 1689 taskq_wait_id(spa->spa_zvol_taskq, id); 1690 } 1691 1692 void 1693 zvol_rename_minors(spa_t *spa, const char *name1, const char *name2, 1694 boolean_t async) 1695 { 1696 zvol_task_t *task; 1697 taskqid_t id; 1698 1699 task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL); 1700 if (task == NULL) 1701 return; 1702 1703 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP); 1704 if ((async == B_FALSE) && (id != TASKQID_INVALID)) 1705 taskq_wait_id(spa->spa_zvol_taskq, id); 1706 } 1707 1708 boolean_t 1709 zvol_is_zvol(const char *name) 1710 { 1711 1712 return (zvol_os_is_zvol(name)); 1713 } 1714 1715 int 1716 zvol_init_impl(void) 1717 { 1718 int i; 1719 1720 list_create(&zvol_state_list, sizeof (zvol_state_t), 1721 offsetof(zvol_state_t, zv_next)); 1722 rw_init(&zvol_state_lock, NULL, RW_DEFAULT, NULL); 1723 1724 zvol_htable = kmem_alloc(ZVOL_HT_SIZE * sizeof (struct hlist_head), 1725 KM_SLEEP); 1726 for (i = 0; i < ZVOL_HT_SIZE; i++) 1727 INIT_HLIST_HEAD(&zvol_htable[i]); 1728 1729 return (0); 1730 } 1731 1732 void 1733 zvol_fini_impl(void) 1734 { 1735 zvol_remove_minors_impl(NULL); 1736 1737 /* 1738 * The call to "zvol_remove_minors_impl" may dispatch entries to 1739 * the system_taskq, but it doesn't wait for those entries to 1740 * complete before it returns. Thus, we must wait for all of the 1741 * removals to finish, before we can continue. 1742 */ 1743 taskq_wait_outstanding(system_taskq, 0); 1744 1745 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head)); 1746 list_destroy(&zvol_state_list); 1747 rw_destroy(&zvol_state_lock); 1748 } 1749