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