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.
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.
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]
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>.
27 * ZFS volume emulation driver.
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:
32 * /dev/<pool_name>/<dataset_name>
34 * Volumes are persistent through reboot and module load. No user command
35 * needs to be run before opening and using a device.
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 * Copyright (c) 2024, Klara, Inc.
44 * Note on locking of zvol state structures.
46 * These structures are used to maintain internal state used to emulate block
47 * devices on top of zvols. In particular, management of device minor number
48 * operations - create, remove, rename, and set_snapdev - involves access to
49 * these structures. The zvol_state_lock is primarily used to protect the
50 * zvol_state_list. The zv->zv_state_lock is used to protect the contents
51 * of the zvol_state_t structures, as well as to make sure that when the
52 * time comes to remove the structure from the list, it is not in use, and
53 * therefore, it can be taken off zvol_state_list and freed.
55 * The zv_suspend_lock was introduced to allow for suspending I/O to a zvol,
56 * e.g. for the duration of receive and rollback operations. This lock can be
57 * held for significant periods of time. Given that it is undesirable to hold
58 * mutexes for long periods of time, the following lock ordering applies:
59 * - take zvol_state_lock if necessary, to protect zvol_state_list
60 * - take zv_suspend_lock if necessary, by the code path in question
61 * - take zv_state_lock to protect zvol_state_t
63 * The minor operations are issued to spa->spa_zvol_taskq queues, that are
64 * single-threaded (to preserve order of minor operations), and are executed
65 * through the zvol_task_cb that dispatches the specific operations. Therefore,
66 * these operations are serialized per pool. Consequently, we can be certain
67 * that for a given zvol, there is only one operation at a time in progress.
68 * That is why one can be sure that first, zvol_state_t for a given zvol is
69 * allocated and placed on zvol_state_list, and then other minor operations
70 * for this zvol are going to proceed in the order of issue.
74 #include <sys/dataset_kstats.h>
76 #include <sys/dmu_traverse.h>
77 #include <sys/dsl_dataset.h>
78 #include <sys/dsl_prop.h>
79 #include <sys/dsl_dir.h>
81 #include <sys/zfeature.h>
82 #include <sys/zil_impl.h>
83 #include <sys/dmu_tx.h>
85 #include <sys/zfs_rlock.h>
86 #include <sys/spa_impl.h>
88 #include <sys/zvol_impl.h>
90 unsigned int zvol_inhibit_dev
= 0;
91 unsigned int zvol_volmode
= ZFS_VOLMODE_GEOM
;
93 struct hlist_head
*zvol_htable
;
94 static list_t zvol_state_list
;
95 krwlock_t zvol_state_lock
;
98 ZVOL_ASYNC_REMOVE_MINORS
,
99 ZVOL_ASYNC_RENAME_MINORS
,
100 ZVOL_ASYNC_SET_SNAPDEV
,
101 ZVOL_ASYNC_SET_VOLMODE
,
107 char name1
[MAXNAMELEN
];
108 char name2
[MAXNAMELEN
];
113 zvol_name_hash(const char *name
)
115 uint64_t crc
= -1ULL;
116 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
117 for (const uint8_t *p
= (const uint8_t *)name
; *p
!= 0; p
++)
118 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (*p
)) & 0xFF];
123 * Find a zvol_state_t given the name and hash generated by zvol_name_hash.
124 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise,
125 * return (NULL) without the taking locks. The zv_suspend_lock is always taken
126 * before zv_state_lock. The mode argument indicates the mode (including none)
127 * for zv_suspend_lock to be taken.
130 zvol_find_by_name_hash(const char *name
, uint64_t hash
, int mode
)
133 struct hlist_node
*p
= NULL
;
135 rw_enter(&zvol_state_lock
, RW_READER
);
136 hlist_for_each(p
, ZVOL_HT_HEAD(hash
)) {
137 zv
= hlist_entry(p
, zvol_state_t
, zv_hlink
);
138 mutex_enter(&zv
->zv_state_lock
);
139 if (zv
->zv_hash
== hash
&& strcmp(zv
->zv_name
, name
) == 0) {
141 * this is the right zvol, take the locks in the
144 if (mode
!= RW_NONE
&&
145 !rw_tryenter(&zv
->zv_suspend_lock
, mode
)) {
146 mutex_exit(&zv
->zv_state_lock
);
147 rw_enter(&zv
->zv_suspend_lock
, mode
);
148 mutex_enter(&zv
->zv_state_lock
);
150 * zvol cannot be renamed as we continue
151 * to hold zvol_state_lock
153 ASSERT(zv
->zv_hash
== hash
&&
154 strcmp(zv
->zv_name
, name
) == 0);
156 rw_exit(&zvol_state_lock
);
159 mutex_exit(&zv
->zv_state_lock
);
161 rw_exit(&zvol_state_lock
);
167 * Find a zvol_state_t given the name.
168 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise,
169 * return (NULL) without the taking locks. The zv_suspend_lock is always taken
170 * before zv_state_lock. The mode argument indicates the mode (including none)
171 * for zv_suspend_lock to be taken.
173 static zvol_state_t
*
174 zvol_find_by_name(const char *name
, int mode
)
176 return (zvol_find_by_name_hash(name
, zvol_name_hash(name
), mode
));
180 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
183 zvol_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
185 zfs_creat_t
*zct
= arg
;
186 nvlist_t
*nvprops
= zct
->zct_props
;
188 uint64_t volblocksize
, volsize
;
190 VERIFY(nvlist_lookup_uint64(nvprops
,
191 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) == 0);
192 if (nvlist_lookup_uint64(nvprops
,
193 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &volblocksize
) != 0)
194 volblocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
197 * These properties must be removed from the list so the generic
198 * property setting step won't apply to them.
200 VERIFY(nvlist_remove_all(nvprops
,
201 zfs_prop_to_name(ZFS_PROP_VOLSIZE
)) == 0);
202 (void) nvlist_remove_all(nvprops
,
203 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
));
205 error
= dmu_object_claim(os
, ZVOL_OBJ
, DMU_OT_ZVOL
, volblocksize
,
209 error
= zap_create_claim(os
, ZVOL_ZAP_OBJ
, DMU_OT_ZVOL_PROP
,
213 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
, tx
);
218 * ZFS_IOC_OBJSET_STATS entry point.
221 zvol_get_stats(objset_t
*os
, nvlist_t
*nv
)
224 dmu_object_info_t
*doi
;
227 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &val
);
229 return (SET_ERROR(error
));
231 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLSIZE
, val
);
232 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
233 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
236 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLBLOCKSIZE
,
237 doi
->doi_data_block_size
);
240 kmem_free(doi
, sizeof (dmu_object_info_t
));
242 return (SET_ERROR(error
));
246 * Sanity check volume size.
249 zvol_check_volsize(uint64_t volsize
, uint64_t blocksize
)
252 return (SET_ERROR(EINVAL
));
254 if (volsize
% blocksize
!= 0)
255 return (SET_ERROR(EINVAL
));
258 if (volsize
- 1 > SPEC_MAXOFFSET_T
)
259 return (SET_ERROR(EOVERFLOW
));
265 * Ensure the zap is flushed then inform the VFS of the capacity change.
268 zvol_update_volsize(uint64_t volsize
, objset_t
*os
)
274 tx
= dmu_tx_create(os
);
275 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
276 dmu_tx_mark_netfree(tx
);
277 error
= dmu_tx_assign(tx
, TXG_WAIT
);
280 return (SET_ERROR(error
));
282 txg
= dmu_tx_get_txg(tx
);
284 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1,
288 txg_wait_synced(dmu_objset_pool(os
), txg
);
291 error
= dmu_free_long_range(os
,
292 ZVOL_OBJ
, volsize
, DMU_OBJECT_END
);
298 * Set ZFS_PROP_VOLSIZE set entry point. Note that modifying the volume
299 * size will result in a udev "change" event being generated.
302 zvol_set_volsize(const char *name
, uint64_t volsize
)
307 boolean_t owned
= B_FALSE
;
309 error
= dsl_prop_get_integer(name
,
310 zfs_prop_to_name(ZFS_PROP_READONLY
), &readonly
, NULL
);
312 return (SET_ERROR(error
));
314 return (SET_ERROR(EROFS
));
316 zvol_state_t
*zv
= zvol_find_by_name(name
, RW_READER
);
318 ASSERT(zv
== NULL
|| (MUTEX_HELD(&zv
->zv_state_lock
) &&
319 RW_READ_HELD(&zv
->zv_suspend_lock
)));
321 if (zv
== NULL
|| zv
->zv_objset
== NULL
) {
323 rw_exit(&zv
->zv_suspend_lock
);
324 if ((error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_FALSE
, B_TRUE
,
327 mutex_exit(&zv
->zv_state_lock
);
328 return (SET_ERROR(error
));
337 dmu_object_info_t
*doi
= kmem_alloc(sizeof (*doi
), KM_SLEEP
);
339 if ((error
= dmu_object_info(os
, ZVOL_OBJ
, doi
)) ||
340 (error
= zvol_check_volsize(volsize
, doi
->doi_data_block_size
)))
343 error
= zvol_update_volsize(volsize
, os
);
344 if (error
== 0 && zv
!= NULL
) {
345 zv
->zv_volsize
= volsize
;
349 kmem_free(doi
, sizeof (dmu_object_info_t
));
352 dmu_objset_disown(os
, B_TRUE
, FTAG
);
354 zv
->zv_objset
= NULL
;
356 rw_exit(&zv
->zv_suspend_lock
);
360 mutex_exit(&zv
->zv_state_lock
);
362 if (error
== 0 && zv
!= NULL
)
363 zvol_os_update_volsize(zv
, volsize
);
365 return (SET_ERROR(error
));
369 * Update volthreading.
372 zvol_set_volthreading(const char *name
, boolean_t value
)
374 zvol_state_t
*zv
= zvol_find_by_name(name
, RW_NONE
);
377 zv
->zv_threading
= value
;
378 mutex_exit(&zv
->zv_state_lock
);
383 * Update zvol ro property.
386 zvol_set_ro(const char *name
, boolean_t value
)
388 zvol_state_t
*zv
= zvol_find_by_name(name
, RW_NONE
);
392 zvol_os_set_disk_ro(zv
, 1);
393 zv
->zv_flags
|= ZVOL_RDONLY
;
395 zvol_os_set_disk_ro(zv
, 0);
396 zv
->zv_flags
&= ~ZVOL_RDONLY
;
398 mutex_exit(&zv
->zv_state_lock
);
403 * Sanity check volume block size.
406 zvol_check_volblocksize(const char *name
, uint64_t volblocksize
)
408 /* Record sizes above 128k need the feature to be enabled */
409 if (volblocksize
> SPA_OLD_MAXBLOCKSIZE
) {
413 if ((error
= spa_open(name
, &spa
, FTAG
)) != 0)
416 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
417 spa_close(spa
, FTAG
);
418 return (SET_ERROR(ENOTSUP
));
422 * We don't allow setting the property above 1MB,
423 * unless the tunable has been changed.
425 if (volblocksize
> zfs_max_recordsize
)
426 return (SET_ERROR(EDOM
));
428 spa_close(spa
, FTAG
);
431 if (volblocksize
< SPA_MINBLOCKSIZE
||
432 volblocksize
> SPA_MAXBLOCKSIZE
||
434 return (SET_ERROR(EDOM
));
440 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
441 * implement DKIOCFREE/free-long-range.
444 zvol_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
446 zvol_state_t
*zv
= arg1
;
447 lr_truncate_t
*lr
= arg2
;
448 uint64_t offset
, length
;
450 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
453 byteswap_uint64_array(lr
, sizeof (*lr
));
455 offset
= lr
->lr_offset
;
456 length
= lr
->lr_length
;
458 dmu_tx_t
*tx
= dmu_tx_create(zv
->zv_objset
);
459 dmu_tx_mark_netfree(tx
);
460 int error
= dmu_tx_assign(tx
, TXG_WAIT
);
464 (void) zil_replaying(zv
->zv_zilog
, tx
);
466 error
= dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
, offset
,
474 * Replay a TX_WRITE ZIL transaction that didn't get committed
475 * after a system failure
478 zvol_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
480 zvol_state_t
*zv
= arg1
;
481 lr_write_t
*lr
= arg2
;
482 objset_t
*os
= zv
->zv_objset
;
483 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
484 uint64_t offset
, length
;
488 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
491 byteswap_uint64_array(lr
, sizeof (*lr
));
493 offset
= lr
->lr_offset
;
494 length
= lr
->lr_length
;
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
;
505 tx
= dmu_tx_create(os
);
506 dmu_tx_hold_write(tx
, ZVOL_OBJ
, offset
, length
);
507 error
= dmu_tx_assign(tx
, TXG_WAIT
);
511 dmu_write(os
, ZVOL_OBJ
, offset
, length
, data
, tx
);
512 (void) zil_replaying(zv
->zv_zilog
, tx
);
520 zvol_replay_err(void *arg1
, void *arg2
, boolean_t byteswap
)
522 (void) arg1
, (void) arg2
, (void) byteswap
;
523 return (SET_ERROR(ENOTSUP
));
527 * Callback vectors for replaying records.
528 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
530 zil_replay_func_t
*const zvol_replay_vector
[TX_MAX_TYPE
] = {
531 zvol_replay_err
, /* no such transaction type */
532 zvol_replay_err
, /* TX_CREATE */
533 zvol_replay_err
, /* TX_MKDIR */
534 zvol_replay_err
, /* TX_MKXATTR */
535 zvol_replay_err
, /* TX_SYMLINK */
536 zvol_replay_err
, /* TX_REMOVE */
537 zvol_replay_err
, /* TX_RMDIR */
538 zvol_replay_err
, /* TX_LINK */
539 zvol_replay_err
, /* TX_RENAME */
540 zvol_replay_write
, /* TX_WRITE */
541 zvol_replay_truncate
, /* TX_TRUNCATE */
542 zvol_replay_err
, /* TX_SETATTR */
543 zvol_replay_err
, /* TX_ACL */
544 zvol_replay_err
, /* TX_CREATE_ATTR */
545 zvol_replay_err
, /* TX_CREATE_ACL_ATTR */
546 zvol_replay_err
, /* TX_MKDIR_ACL */
547 zvol_replay_err
, /* TX_MKDIR_ATTR */
548 zvol_replay_err
, /* TX_MKDIR_ACL_ATTR */
549 zvol_replay_err
, /* TX_WRITE2 */
550 zvol_replay_err
, /* TX_SETSAXATTR */
551 zvol_replay_err
, /* TX_RENAME_EXCHANGE */
552 zvol_replay_err
, /* TX_RENAME_WHITEOUT */
553 zvol_replay_err
, /* TX_CLONE_RANGE */
557 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
559 * We store data in the log buffers if it's small enough.
560 * Otherwise we will later flush the data out via dmu_sync().
562 static const ssize_t zvol_immediate_write_sz
= 32768;
565 zvol_log_write(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t offset
,
566 uint64_t size
, boolean_t commit
)
568 uint32_t blocksize
= zv
->zv_volblocksize
;
569 zilog_t
*zilog
= zv
->zv_zilog
;
570 itx_wr_state_t write_state
;
573 if (zil_replaying(zilog
, tx
))
576 if (zilog
->zl_logbias
== ZFS_LOGBIAS_THROUGHPUT
)
577 write_state
= WR_INDIRECT
;
578 else if (!spa_has_slogs(zilog
->zl_spa
) &&
579 size
>= blocksize
&& blocksize
> zvol_immediate_write_sz
)
580 write_state
= WR_INDIRECT
;
582 write_state
= WR_COPIED
;
584 write_state
= WR_NEED_COPY
;
589 itx_wr_state_t wr_state
= write_state
;
592 if (wr_state
== WR_COPIED
&& size
> zil_max_copied_data(zilog
))
593 wr_state
= WR_NEED_COPY
;
594 else if (wr_state
== WR_INDIRECT
)
595 len
= MIN(blocksize
- P2PHASE(offset
, blocksize
), size
);
597 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
) +
598 (wr_state
== WR_COPIED
? len
: 0));
599 lr
= (lr_write_t
*)&itx
->itx_lr
;
600 if (wr_state
== WR_COPIED
&& dmu_read_by_dnode(zv
->zv_dn
,
601 offset
, len
, lr
+1, DMU_READ_NO_PREFETCH
) != 0) {
602 zil_itx_destroy(itx
);
603 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
604 lr
= (lr_write_t
*)&itx
->itx_lr
;
605 wr_state
= WR_NEED_COPY
;
608 itx
->itx_wr_state
= wr_state
;
609 lr
->lr_foid
= ZVOL_OBJ
;
610 lr
->lr_offset
= offset
;
613 BP_ZERO(&lr
->lr_blkptr
);
615 itx
->itx_private
= zv
;
617 (void) zil_itx_assign(zilog
, itx
, tx
);
623 if (write_state
== WR_COPIED
|| write_state
== WR_NEED_COPY
) {
624 dsl_pool_wrlog_count(zilog
->zl_dmu_pool
, sz
, tx
->tx_txg
);
629 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
632 zvol_log_truncate(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t off
, uint64_t len
)
636 zilog_t
*zilog
= zv
->zv_zilog
;
638 if (zil_replaying(zilog
, tx
))
641 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
642 lr
= (lr_truncate_t
*)&itx
->itx_lr
;
643 lr
->lr_foid
= ZVOL_OBJ
;
647 zil_itx_assign(zilog
, itx
, tx
);
652 zvol_get_done(zgd_t
*zgd
, int error
)
656 dmu_buf_rele(zgd
->zgd_db
, zgd
);
658 zfs_rangelock_exit(zgd
->zgd_lr
);
660 kmem_free(zgd
, sizeof (zgd_t
));
664 * Get data to generate a TX_WRITE intent log record.
667 zvol_get_data(void *arg
, uint64_t arg2
, lr_write_t
*lr
, char *buf
,
668 struct lwb
*lwb
, zio_t
*zio
)
670 zvol_state_t
*zv
= arg
;
671 uint64_t offset
= lr
->lr_offset
;
672 uint64_t size
= lr
->lr_length
;
677 ASSERT3P(lwb
, !=, NULL
);
678 ASSERT3U(size
, !=, 0);
680 zgd
= kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
684 * Write records come in two flavors: immediate and indirect.
685 * For small writes it's cheaper to store the data with the
686 * log record (immediate); for large writes it's cheaper to
687 * sync the data and get a pointer to it (indirect) so that
688 * we don't have to write the data twice.
690 if (buf
!= NULL
) { /* immediate write */
691 zgd
->zgd_lr
= zfs_rangelock_enter(&zv
->zv_rangelock
, offset
,
693 error
= dmu_read_by_dnode(zv
->zv_dn
, offset
, size
, buf
,
694 DMU_READ_NO_PREFETCH
);
695 } else { /* indirect write */
696 ASSERT3P(zio
, !=, NULL
);
698 * Have to lock the whole block to ensure when it's written out
699 * and its checksum is being calculated that no one can change
700 * the data. Contrarily to zfs_get_data we need not re-check
701 * blocksize after we get the lock because it cannot be changed.
703 size
= zv
->zv_volblocksize
;
704 offset
= P2ALIGN_TYPED(offset
, size
, uint64_t);
705 zgd
->zgd_lr
= zfs_rangelock_enter(&zv
->zv_rangelock
, offset
,
707 error
= dmu_buf_hold_noread_by_dnode(zv
->zv_dn
, offset
, zgd
,
710 blkptr_t
*bp
= &lr
->lr_blkptr
;
716 ASSERT(db
->db_offset
== offset
);
717 ASSERT(db
->db_size
== size
);
719 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
727 zvol_get_done(zgd
, error
);
729 return (SET_ERROR(error
));
733 * The zvol_state_t's are inserted into zvol_state_list and zvol_htable.
737 zvol_insert(zvol_state_t
*zv
)
739 ASSERT(RW_WRITE_HELD(&zvol_state_lock
));
740 list_insert_head(&zvol_state_list
, zv
);
741 hlist_add_head(&zv
->zv_hlink
, ZVOL_HT_HEAD(zv
->zv_hash
));
745 * Simply remove the zvol from to list of zvols.
748 zvol_remove(zvol_state_t
*zv
)
750 ASSERT(RW_WRITE_HELD(&zvol_state_lock
));
751 list_remove(&zvol_state_list
, zv
);
752 hlist_del(&zv
->zv_hlink
);
756 * Setup zv after we just own the zv->objset
759 zvol_setup_zv(zvol_state_t
*zv
)
764 objset_t
*os
= zv
->zv_objset
;
766 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
767 ASSERT(RW_LOCK_HELD(&zv
->zv_suspend_lock
));
770 zv
->zv_flags
&= ~ZVOL_WRITTEN_TO
;
772 error
= dsl_prop_get_integer(zv
->zv_name
, "readonly", &ro
, NULL
);
774 return (SET_ERROR(error
));
776 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
778 return (SET_ERROR(error
));
780 error
= dnode_hold(os
, ZVOL_OBJ
, zv
, &zv
->zv_dn
);
782 return (SET_ERROR(error
));
784 zvol_os_set_capacity(zv
, volsize
>> 9);
785 zv
->zv_volsize
= volsize
;
787 if (ro
|| dmu_objset_is_snapshot(os
) ||
788 !spa_writeable(dmu_objset_spa(os
))) {
789 zvol_os_set_disk_ro(zv
, 1);
790 zv
->zv_flags
|= ZVOL_RDONLY
;
792 zvol_os_set_disk_ro(zv
, 0);
793 zv
->zv_flags
&= ~ZVOL_RDONLY
;
799 * Shutdown every zv_objset related stuff except zv_objset itself.
800 * The is the reverse of zvol_setup_zv.
803 zvol_shutdown_zv(zvol_state_t
*zv
)
805 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
) &&
806 RW_LOCK_HELD(&zv
->zv_suspend_lock
));
808 if (zv
->zv_flags
& ZVOL_WRITTEN_TO
) {
809 ASSERT(zv
->zv_zilog
!= NULL
);
810 zil_close(zv
->zv_zilog
);
815 dnode_rele(zv
->zv_dn
, zv
);
819 * Evict cached data. We must write out any dirty data before
820 * disowning the dataset.
822 if (zv
->zv_flags
& ZVOL_WRITTEN_TO
)
823 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
824 (void) dmu_objset_evict_dbufs(zv
->zv_objset
);
828 * return the proper tag for rollback and recv
831 zvol_tag(zvol_state_t
*zv
)
833 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
834 return (zv
->zv_open_count
> 0 ? zv
: NULL
);
838 * Suspend the zvol for recv and rollback.
841 zvol_suspend(const char *name
)
845 zv
= zvol_find_by_name(name
, RW_WRITER
);
850 /* block all I/O, release in zvol_resume. */
851 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
852 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
854 atomic_inc(&zv
->zv_suspend_ref
);
856 if (zv
->zv_open_count
> 0)
857 zvol_shutdown_zv(zv
);
860 * do not hold zv_state_lock across suspend/resume to
861 * avoid locking up zvol lookups
863 mutex_exit(&zv
->zv_state_lock
);
865 /* zv_suspend_lock is released in zvol_resume() */
870 zvol_resume(zvol_state_t
*zv
)
874 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
876 mutex_enter(&zv
->zv_state_lock
);
878 if (zv
->zv_open_count
> 0) {
879 VERIFY0(dmu_objset_hold(zv
->zv_name
, zv
, &zv
->zv_objset
));
880 VERIFY3P(zv
->zv_objset
->os_dsl_dataset
->ds_owner
, ==, zv
);
881 VERIFY(dsl_dataset_long_held(zv
->zv_objset
->os_dsl_dataset
));
882 dmu_objset_rele(zv
->zv_objset
, zv
);
884 error
= zvol_setup_zv(zv
);
887 mutex_exit(&zv
->zv_state_lock
);
889 rw_exit(&zv
->zv_suspend_lock
);
891 * We need this because we don't hold zvol_state_lock while releasing
892 * zv_suspend_lock. zvol_remove_minors_impl thus cannot check
893 * zv_suspend_lock to determine it is safe to free because rwlock is
894 * not inherent atomic.
896 atomic_dec(&zv
->zv_suspend_ref
);
898 if (zv
->zv_flags
& ZVOL_REMOVING
)
899 cv_broadcast(&zv
->zv_removing_cv
);
901 return (SET_ERROR(error
));
905 zvol_first_open(zvol_state_t
*zv
, boolean_t readonly
)
910 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
911 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
912 ASSERT(mutex_owned(&spa_namespace_lock
));
914 boolean_t ro
= (readonly
|| (strchr(zv
->zv_name
, '@') != NULL
));
915 error
= dmu_objset_own(zv
->zv_name
, DMU_OST_ZVOL
, ro
, B_TRUE
, zv
, &os
);
917 return (SET_ERROR(error
));
921 error
= zvol_setup_zv(zv
);
923 dmu_objset_disown(os
, 1, zv
);
924 zv
->zv_objset
= NULL
;
931 zvol_last_close(zvol_state_t
*zv
)
933 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
934 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
936 if (zv
->zv_flags
& ZVOL_REMOVING
)
937 cv_broadcast(&zv
->zv_removing_cv
);
939 zvol_shutdown_zv(zv
);
941 dmu_objset_disown(zv
->zv_objset
, 1, zv
);
942 zv
->zv_objset
= NULL
;
945 typedef struct minors_job
{
955 * Prefetch zvol dnodes for the minors_job
958 zvol_prefetch_minors_impl(void *arg
)
960 minors_job_t
*job
= arg
;
961 char *dsname
= job
->name
;
964 job
->error
= dmu_objset_own(dsname
, DMU_OST_ZVOL
, B_TRUE
, B_TRUE
,
966 if (job
->error
== 0) {
967 dmu_prefetch_dnode(os
, ZVOL_OBJ
, ZIO_PRIORITY_SYNC_READ
);
968 dmu_objset_disown(os
, B_TRUE
, FTAG
);
973 * Mask errors to continue dmu_objset_find() traversal
976 zvol_create_snap_minor_cb(const char *dsname
, void *arg
)
978 minors_job_t
*j
= arg
;
979 list_t
*minors_list
= j
->list
;
980 const char *name
= j
->name
;
982 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
984 /* skip the designated dataset */
985 if (name
&& strcmp(dsname
, name
) == 0)
988 /* at this point, the dsname should name a snapshot */
989 if (strchr(dsname
, '@') == 0) {
990 dprintf("zvol_create_snap_minor_cb(): "
991 "%s is not a snapshot name\n", dsname
);
994 char *n
= kmem_strdup(dsname
);
998 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
1000 job
->list
= minors_list
;
1002 list_insert_tail(minors_list
, job
);
1003 /* don't care if dispatch fails, because job->error is 0 */
1004 taskq_dispatch(system_taskq
, zvol_prefetch_minors_impl
, job
,
1012 * If spa_keystore_load_wkey() is called for an encrypted zvol,
1013 * we need to look for any clones also using the key. This function
1014 * is "best effort" - so we just skip over it if there are failures.
1017 zvol_add_clones(const char *dsname
, list_t
*minors_list
)
1019 /* Also check if it has clones */
1020 dsl_dir_t
*dd
= NULL
;
1021 dsl_pool_t
*dp
= NULL
;
1023 if (dsl_pool_hold(dsname
, FTAG
, &dp
) != 0)
1026 if (!spa_feature_is_enabled(dp
->dp_spa
,
1027 SPA_FEATURE_ENCRYPTION
))
1030 if (dsl_dir_hold(dp
, dsname
, FTAG
, &dd
, NULL
) != 0)
1033 if (dsl_dir_phys(dd
)->dd_clones
== 0)
1036 zap_cursor_t
*zc
= kmem_alloc(sizeof (zap_cursor_t
), KM_SLEEP
);
1037 zap_attribute_t
*za
= zap_attribute_alloc();
1038 objset_t
*mos
= dd
->dd_pool
->dp_meta_objset
;
1040 for (zap_cursor_init(zc
, mos
, dsl_dir_phys(dd
)->dd_clones
);
1041 zap_cursor_retrieve(zc
, za
) == 0;
1042 zap_cursor_advance(zc
)) {
1043 dsl_dataset_t
*clone
;
1046 if (dsl_dataset_hold_obj(dd
->dd_pool
,
1047 za
->za_first_integer
, FTAG
, &clone
) == 0) {
1049 char name
[ZFS_MAX_DATASET_NAME_LEN
];
1050 dsl_dataset_name(clone
, name
);
1052 char *n
= kmem_strdup(name
);
1053 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
1055 job
->list
= minors_list
;
1057 list_insert_tail(minors_list
, job
);
1059 dsl_dataset_rele(clone
, FTAG
);
1062 zap_cursor_fini(zc
);
1063 zap_attribute_free(za
);
1064 kmem_free(zc
, sizeof (zap_cursor_t
));
1068 dsl_dir_rele(dd
, FTAG
);
1069 dsl_pool_rele(dp
, FTAG
);
1073 * Mask errors to continue dmu_objset_find() traversal
1076 zvol_create_minors_cb(const char *dsname
, void *arg
)
1080 list_t
*minors_list
= arg
;
1082 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
1084 error
= dsl_prop_get_integer(dsname
, "snapdev", &snapdev
, NULL
);
1089 * Given the name and the 'snapdev' property, create device minor nodes
1090 * with the linkages to zvols/snapshots as needed.
1091 * If the name represents a zvol, create a minor node for the zvol, then
1092 * check if its snapshots are 'visible', and if so, iterate over the
1093 * snapshots and create device minor nodes for those.
1095 if (strchr(dsname
, '@') == 0) {
1097 char *n
= kmem_strdup(dsname
);
1101 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
1103 job
->list
= minors_list
;
1105 list_insert_tail(minors_list
, job
);
1106 /* don't care if dispatch fails, because job->error is 0 */
1107 taskq_dispatch(system_taskq
, zvol_prefetch_minors_impl
, job
,
1110 zvol_add_clones(dsname
, minors_list
);
1112 if (snapdev
== ZFS_SNAPDEV_VISIBLE
) {
1114 * traverse snapshots only, do not traverse children,
1115 * and skip the 'dsname'
1117 (void) dmu_objset_find(dsname
,
1118 zvol_create_snap_minor_cb
, (void *)job
,
1122 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
1130 * Create minors for the specified dataset, including children and snapshots.
1131 * Pay attention to the 'snapdev' property and iterate over the snapshots
1132 * only if they are 'visible'. This approach allows one to assure that the
1133 * snapshot metadata is read from disk only if it is needed.
1135 * The name can represent a dataset to be recursively scanned for zvols and
1136 * their snapshots, or a single zvol snapshot. If the name represents a
1137 * dataset, the scan is performed in two nested stages:
1138 * - scan the dataset for zvols, and
1139 * - for each zvol, create a minor node, then check if the zvol's snapshots
1140 * are 'visible', and only then iterate over the snapshots if needed
1142 * If the name represents a snapshot, a check is performed if the snapshot is
1143 * 'visible' (which also verifies that the parent is a zvol), and if so,
1144 * a minor node for that snapshot is created.
1147 zvol_create_minors_recursive(const char *name
)
1152 if (zvol_inhibit_dev
)
1156 * This is the list for prefetch jobs. Whenever we found a match
1157 * during dmu_objset_find, we insert a minors_job to the list and do
1158 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need
1159 * any lock because all list operation is done on the current thread.
1161 * We will use this list to do zvol_os_create_minor after prefetch
1162 * so we don't have to traverse using dmu_objset_find again.
1164 list_create(&minors_list
, sizeof (minors_job_t
),
1165 offsetof(minors_job_t
, link
));
1168 if (strchr(name
, '@') != NULL
) {
1171 int error
= dsl_prop_get_integer(name
, "snapdev",
1174 if (error
== 0 && snapdev
== ZFS_SNAPDEV_VISIBLE
)
1175 (void) zvol_os_create_minor(name
);
1177 fstrans_cookie_t cookie
= spl_fstrans_mark();
1178 (void) dmu_objset_find(name
, zvol_create_minors_cb
,
1179 &minors_list
, DS_FIND_CHILDREN
);
1180 spl_fstrans_unmark(cookie
);
1183 taskq_wait_outstanding(system_taskq
, 0);
1186 * Prefetch is completed, we can do zvol_os_create_minor
1189 while ((job
= list_remove_head(&minors_list
)) != NULL
) {
1191 (void) zvol_os_create_minor(job
->name
);
1192 kmem_strfree(job
->name
);
1193 kmem_free(job
, sizeof (minors_job_t
));
1196 list_destroy(&minors_list
);
1200 zvol_create_minor(const char *name
)
1203 * Note: the dsl_pool_config_lock must not be held.
1204 * Minor node creation needs to obtain the zvol_state_lock.
1205 * zvol_open() obtains the zvol_state_lock and then the dsl pool
1206 * config lock. Therefore, we can't have the config lock now if
1207 * we are going to wait for the zvol_state_lock, because it
1208 * would be a lock order inversion which could lead to deadlock.
1211 if (zvol_inhibit_dev
)
1214 if (strchr(name
, '@') != NULL
) {
1217 int error
= dsl_prop_get_integer(name
,
1218 "snapdev", &snapdev
, NULL
);
1220 if (error
== 0 && snapdev
== ZFS_SNAPDEV_VISIBLE
)
1221 (void) zvol_os_create_minor(name
);
1223 (void) zvol_os_create_minor(name
);
1228 * Remove minors for specified dataset including children and snapshots.
1232 * Remove the minor for a given zvol. This will do it all:
1233 * - flag the zvol for removal, so new requests are rejected
1234 * - wait until outstanding requests are completed
1235 * - remove it from lists
1237 * It's also usable as a taskq task, and smells nice too.
1240 zvol_remove_minor_task(void *arg
)
1242 zvol_state_t
*zv
= (zvol_state_t
*)arg
;
1244 ASSERT(!RW_LOCK_HELD(&zvol_state_lock
));
1245 ASSERT(!MUTEX_HELD(&zv
->zv_state_lock
));
1247 mutex_enter(&zv
->zv_state_lock
);
1248 while (zv
->zv_open_count
> 0 || atomic_read(&zv
->zv_suspend_ref
)) {
1249 zv
->zv_flags
|= ZVOL_REMOVING
;
1250 cv_wait(&zv
->zv_removing_cv
, &zv
->zv_state_lock
);
1252 mutex_exit(&zv
->zv_state_lock
);
1254 rw_enter(&zvol_state_lock
, RW_WRITER
);
1255 mutex_enter(&zv
->zv_state_lock
);
1258 zvol_os_clear_private(zv
);
1260 mutex_exit(&zv
->zv_state_lock
);
1261 rw_exit(&zvol_state_lock
);
1267 zvol_free_task(void *arg
)
1273 zvol_remove_minors_impl(const char *name
)
1275 zvol_state_t
*zv
, *zv_next
;
1276 int namelen
= ((name
) ? strlen(name
) : 0);
1278 list_t delay_list
, free_list
;
1280 if (zvol_inhibit_dev
)
1283 list_create(&delay_list
, sizeof (zvol_state_t
),
1284 offsetof(zvol_state_t
, zv_next
));
1285 list_create(&free_list
, sizeof (zvol_state_t
),
1286 offsetof(zvol_state_t
, zv_next
));
1288 rw_enter(&zvol_state_lock
, RW_WRITER
);
1290 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1291 zv_next
= list_next(&zvol_state_list
, zv
);
1293 mutex_enter(&zv
->zv_state_lock
);
1294 if (name
== NULL
|| strcmp(zv
->zv_name
, name
) == 0 ||
1295 (strncmp(zv
->zv_name
, name
, namelen
) == 0 &&
1296 (zv
->zv_name
[namelen
] == '/' ||
1297 zv
->zv_name
[namelen
] == '@'))) {
1299 * By holding zv_state_lock here, we guarantee that no
1300 * one is currently using this zv
1304 * If in use, try to throw everyone off and try again
1307 if (zv
->zv_open_count
> 0 ||
1308 atomic_read(&zv
->zv_suspend_ref
)) {
1309 zv
->zv_flags
|= ZVOL_REMOVING
;
1311 zv
->zv_objset
->os_spa
->spa_zvol_taskq
,
1312 zvol_remove_minor_task
, zv
, TQ_SLEEP
);
1313 if (t
== TASKQID_INVALID
) {
1315 * Couldn't create the task, so we'll
1316 * do it in place once the loop is
1319 list_insert_head(&delay_list
, zv
);
1321 mutex_exit(&zv
->zv_state_lock
);
1328 * Cleared while holding zvol_state_lock as a writer
1329 * which will prevent zvol_open() from opening it.
1331 zvol_os_clear_private(zv
);
1333 /* Drop zv_state_lock before zvol_free() */
1334 mutex_exit(&zv
->zv_state_lock
);
1336 /* Try parallel zv_free, if failed do it in place */
1337 t
= taskq_dispatch(system_taskq
, zvol_free_task
, zv
,
1339 if (t
== TASKQID_INVALID
)
1340 list_insert_head(&free_list
, zv
);
1342 mutex_exit(&zv
->zv_state_lock
);
1345 rw_exit(&zvol_state_lock
);
1347 /* Wait for zvols that we couldn't create a remove task for */
1348 while ((zv
= list_remove_head(&delay_list
)) != NULL
)
1349 zvol_remove_minor_task(zv
);
1351 /* Free any that we couldn't free in parallel earlier */
1352 while ((zv
= list_remove_head(&free_list
)) != NULL
)
1356 /* Remove minor for this specific volume only */
1358 zvol_remove_minor_impl(const char *name
)
1360 zvol_state_t
*zv
= NULL
, *zv_next
;
1362 if (zvol_inhibit_dev
)
1365 rw_enter(&zvol_state_lock
, RW_WRITER
);
1367 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1368 zv_next
= list_next(&zvol_state_list
, zv
);
1370 mutex_enter(&zv
->zv_state_lock
);
1371 if (strcmp(zv
->zv_name
, name
) == 0)
1372 /* Found, leave the the loop with zv_lock held */
1374 mutex_exit(&zv
->zv_state_lock
);
1378 rw_exit(&zvol_state_lock
);
1382 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1384 if (zv
->zv_open_count
> 0 || atomic_read(&zv
->zv_suspend_ref
)) {
1386 * In use, so try to throw everyone off, then wait
1389 zv
->zv_flags
|= ZVOL_REMOVING
;
1390 mutex_exit(&zv
->zv_state_lock
);
1391 rw_exit(&zvol_state_lock
);
1392 zvol_remove_minor_task(zv
);
1397 zvol_os_clear_private(zv
);
1399 mutex_exit(&zv
->zv_state_lock
);
1400 rw_exit(&zvol_state_lock
);
1406 * Rename minors for specified dataset including children and snapshots.
1409 zvol_rename_minors_impl(const char *oldname
, const char *newname
)
1411 zvol_state_t
*zv
, *zv_next
;
1414 if (zvol_inhibit_dev
)
1417 oldnamelen
= strlen(oldname
);
1419 rw_enter(&zvol_state_lock
, RW_READER
);
1421 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1422 zv_next
= list_next(&zvol_state_list
, zv
);
1424 mutex_enter(&zv
->zv_state_lock
);
1426 if (strcmp(zv
->zv_name
, oldname
) == 0) {
1427 zvol_os_rename_minor(zv
, newname
);
1428 } else if (strncmp(zv
->zv_name
, oldname
, oldnamelen
) == 0 &&
1429 (zv
->zv_name
[oldnamelen
] == '/' ||
1430 zv
->zv_name
[oldnamelen
] == '@')) {
1431 char *name
= kmem_asprintf("%s%c%s", newname
,
1432 zv
->zv_name
[oldnamelen
],
1433 zv
->zv_name
+ oldnamelen
+ 1);
1434 zvol_os_rename_minor(zv
, name
);
1438 mutex_exit(&zv
->zv_state_lock
);
1441 rw_exit(&zvol_state_lock
);
1444 typedef struct zvol_snapdev_cb_arg
{
1446 } zvol_snapdev_cb_arg_t
;
1449 zvol_set_snapdev_cb(const char *dsname
, void *param
)
1451 zvol_snapdev_cb_arg_t
*arg
= param
;
1453 if (strchr(dsname
, '@') == NULL
)
1456 switch (arg
->snapdev
) {
1457 case ZFS_SNAPDEV_VISIBLE
:
1458 (void) zvol_os_create_minor(dsname
);
1460 case ZFS_SNAPDEV_HIDDEN
:
1461 (void) zvol_remove_minor_impl(dsname
);
1469 zvol_set_snapdev_impl(char *name
, uint64_t snapdev
)
1471 zvol_snapdev_cb_arg_t arg
= {snapdev
};
1472 fstrans_cookie_t cookie
= spl_fstrans_mark();
1474 * The zvol_set_snapdev_sync() sets snapdev appropriately
1475 * in the dataset hierarchy. Here, we only scan snapshots.
1477 dmu_objset_find(name
, zvol_set_snapdev_cb
, &arg
, DS_FIND_SNAPSHOTS
);
1478 spl_fstrans_unmark(cookie
);
1482 zvol_set_volmode_impl(char *name
, uint64_t volmode
)
1484 fstrans_cookie_t cookie
;
1485 uint64_t old_volmode
;
1488 if (strchr(name
, '@') != NULL
)
1492 * It's unfortunate we need to remove minors before we create new ones:
1493 * this is necessary because our backing gendisk (zvol_state->zv_disk)
1494 * could be different when we set, for instance, volmode from "geom"
1495 * to "dev" (or vice versa).
1497 zv
= zvol_find_by_name(name
, RW_NONE
);
1498 if (zv
== NULL
&& volmode
== ZFS_VOLMODE_NONE
)
1501 old_volmode
= zv
->zv_volmode
;
1502 mutex_exit(&zv
->zv_state_lock
);
1503 if (old_volmode
== volmode
)
1505 zvol_wait_close(zv
);
1507 cookie
= spl_fstrans_mark();
1509 case ZFS_VOLMODE_NONE
:
1510 (void) zvol_remove_minor_impl(name
);
1512 case ZFS_VOLMODE_GEOM
:
1513 case ZFS_VOLMODE_DEV
:
1514 (void) zvol_remove_minor_impl(name
);
1515 (void) zvol_os_create_minor(name
);
1517 case ZFS_VOLMODE_DEFAULT
:
1518 (void) zvol_remove_minor_impl(name
);
1519 if (zvol_volmode
== ZFS_VOLMODE_NONE
)
1521 else /* if zvol_volmode is invalid defaults to "geom" */
1522 (void) zvol_os_create_minor(name
);
1525 spl_fstrans_unmark(cookie
);
1528 static zvol_task_t
*
1529 zvol_task_alloc(zvol_async_op_t op
, const char *name1
, const char *name2
,
1534 /* Never allow tasks on hidden names. */
1535 if (name1
[0] == '$')
1538 task
= kmem_zalloc(sizeof (zvol_task_t
), KM_SLEEP
);
1540 task
->value
= value
;
1542 strlcpy(task
->name1
, name1
, sizeof (task
->name1
));
1544 strlcpy(task
->name2
, name2
, sizeof (task
->name2
));
1550 zvol_task_free(zvol_task_t
*task
)
1552 kmem_free(task
, sizeof (zvol_task_t
));
1556 * The worker thread function performed asynchronously.
1559 zvol_task_cb(void *arg
)
1561 zvol_task_t
*task
= arg
;
1564 case ZVOL_ASYNC_REMOVE_MINORS
:
1565 zvol_remove_minors_impl(task
->name1
);
1567 case ZVOL_ASYNC_RENAME_MINORS
:
1568 zvol_rename_minors_impl(task
->name1
, task
->name2
);
1570 case ZVOL_ASYNC_SET_SNAPDEV
:
1571 zvol_set_snapdev_impl(task
->name1
, task
->value
);
1573 case ZVOL_ASYNC_SET_VOLMODE
:
1574 zvol_set_volmode_impl(task
->name1
, task
->value
);
1581 zvol_task_free(task
);
1584 typedef struct zvol_set_prop_int_arg
{
1585 const char *zsda_name
;
1586 uint64_t zsda_value
;
1587 zprop_source_t zsda_source
;
1588 zfs_prop_t zsda_prop
;
1589 } zvol_set_prop_int_arg_t
;
1592 * Sanity check the dataset for safe use by the sync task. No additional
1593 * conditions are imposed.
1596 zvol_set_common_check(void *arg
, dmu_tx_t
*tx
)
1598 zvol_set_prop_int_arg_t
*zsda
= arg
;
1599 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1603 error
= dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
);
1607 dsl_dir_rele(dd
, FTAG
);
1613 zvol_set_common_sync_cb(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
1615 zvol_set_prop_int_arg_t
*zsda
= arg
;
1616 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
1620 const char *prop_name
= zfs_prop_to_name(zsda
->zsda_prop
);
1621 dsl_dataset_name(ds
, dsname
);
1623 if (dsl_prop_get_int_ds(ds
, prop_name
, &prop
) != 0)
1626 switch (zsda
->zsda_prop
) {
1627 case ZFS_PROP_VOLMODE
:
1628 task
= zvol_task_alloc(ZVOL_ASYNC_SET_VOLMODE
, dsname
,
1631 case ZFS_PROP_SNAPDEV
:
1632 task
= zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV
, dsname
,
1643 (void) taskq_dispatch(dp
->dp_spa
->spa_zvol_taskq
, zvol_task_cb
,
1649 * Traverse all child datasets and apply the property appropriately.
1650 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
1651 * dataset and read the effective "property" on every child in the callback
1652 * function: this is because the value is not guaranteed to be the same in the
1653 * whole dataset hierarchy.
1656 zvol_set_common_sync(void *arg
, dmu_tx_t
*tx
)
1658 zvol_set_prop_int_arg_t
*zsda
= arg
;
1659 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1664 VERIFY0(dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
));
1666 error
= dsl_dataset_hold(dp
, zsda
->zsda_name
, FTAG
, &ds
);
1668 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(zsda
->zsda_prop
),
1669 zsda
->zsda_source
, sizeof (zsda
->zsda_value
), 1,
1670 &zsda
->zsda_value
, tx
);
1671 dsl_dataset_rele(ds
, FTAG
);
1674 dmu_objset_find_dp(dp
, dd
->dd_object
, zvol_set_common_sync_cb
,
1675 zsda
, DS_FIND_CHILDREN
);
1677 dsl_dir_rele(dd
, FTAG
);
1681 zvol_set_common(const char *ddname
, zfs_prop_t prop
, zprop_source_t source
,
1684 zvol_set_prop_int_arg_t zsda
;
1686 zsda
.zsda_name
= ddname
;
1687 zsda
.zsda_source
= source
;
1688 zsda
.zsda_value
= val
;
1689 zsda
.zsda_prop
= prop
;
1691 return (dsl_sync_task(ddname
, zvol_set_common_check
,
1692 zvol_set_common_sync
, &zsda
, 0, ZFS_SPACE_CHECK_NONE
));
1696 zvol_remove_minors(spa_t
*spa
, const char *name
, boolean_t async
)
1701 task
= zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS
, name
, NULL
, ~0ULL);
1705 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
1706 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
1707 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
1711 zvol_rename_minors(spa_t
*spa
, const char *name1
, const char *name2
,
1717 task
= zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS
, name1
, name2
, ~0ULL);
1721 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
1722 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
1723 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
1727 zvol_is_zvol(const char *name
)
1730 return (zvol_os_is_zvol(name
));
1734 zvol_init_impl(void)
1738 list_create(&zvol_state_list
, sizeof (zvol_state_t
),
1739 offsetof(zvol_state_t
, zv_next
));
1740 rw_init(&zvol_state_lock
, NULL
, RW_DEFAULT
, NULL
);
1742 zvol_htable
= kmem_alloc(ZVOL_HT_SIZE
* sizeof (struct hlist_head
),
1744 for (i
= 0; i
< ZVOL_HT_SIZE
; i
++)
1745 INIT_HLIST_HEAD(&zvol_htable
[i
]);
1751 zvol_fini_impl(void)
1753 zvol_remove_minors_impl(NULL
);
1756 * The call to "zvol_remove_minors_impl" may dispatch entries to
1757 * the system_taskq, but it doesn't wait for those entries to
1758 * complete before it returns. Thus, we must wait for all of the
1759 * removals to finish, before we can continue.
1761 taskq_wait_outstanding(system_taskq
, 0);
1763 kmem_free(zvol_htable
, ZVOL_HT_SIZE
* sizeof (struct hlist_head
));
1764 list_destroy(&zvol_state_list
);
1765 rw_destroy(&zvol_state_lock
);