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