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 http://www.opensolaris.org/os/licensing.
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.
43 * Note on locking of zvol state structures.
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.
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
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.
73 #include <sys/dataset_kstats.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>
80 #include <sys/zfeature.h>
81 #include <sys/zil_impl.h>
82 #include <sys/dmu_tx.h>
84 #include <sys/zfs_rlock.h>
85 #include <sys/spa_impl.h>
87 #include <sys/zvol_impl.h>
89 unsigned int zvol_inhibit_dev
= 0;
90 unsigned int zvol_volmode
= ZFS_VOLMODE_GEOM
;
92 struct hlist_head
*zvol_htable
;
93 static list_t zvol_state_list
;
94 krwlock_t zvol_state_lock
;
95 static const zvol_platform_ops_t
*ops
;
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
)
116 uint64_t crc
= -1ULL;
117 const uint8_t *p
= (const uint8_t *)name
;
118 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
119 for (i
= 0; i
< MAXNAMELEN
- 1 && *p
; i
++, p
++) {
120 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (*p
)) & 0xFF];
126 * Find a zvol_state_t given the name and hash generated by zvol_name_hash.
127 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise,
128 * return (NULL) without the taking locks. The zv_suspend_lock is always taken
129 * before zv_state_lock. The mode argument indicates the mode (including none)
130 * for zv_suspend_lock to be taken.
133 zvol_find_by_name_hash(const char *name
, uint64_t hash
, int mode
)
136 struct hlist_node
*p
= NULL
;
138 rw_enter(&zvol_state_lock
, RW_READER
);
139 hlist_for_each(p
, ZVOL_HT_HEAD(hash
)) {
140 zv
= hlist_entry(p
, zvol_state_t
, zv_hlink
);
141 mutex_enter(&zv
->zv_state_lock
);
142 if (zv
->zv_hash
== hash
&&
143 strncmp(zv
->zv_name
, name
, MAXNAMELEN
) == 0) {
145 * this is the right zvol, take the locks in the
148 if (mode
!= RW_NONE
&&
149 !rw_tryenter(&zv
->zv_suspend_lock
, mode
)) {
150 mutex_exit(&zv
->zv_state_lock
);
151 rw_enter(&zv
->zv_suspend_lock
, mode
);
152 mutex_enter(&zv
->zv_state_lock
);
154 * zvol cannot be renamed as we continue
155 * to hold zvol_state_lock
157 ASSERT(zv
->zv_hash
== hash
&&
158 strncmp(zv
->zv_name
, name
, MAXNAMELEN
)
161 rw_exit(&zvol_state_lock
);
164 mutex_exit(&zv
->zv_state_lock
);
166 rw_exit(&zvol_state_lock
);
172 * Find a zvol_state_t given the name.
173 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise,
174 * return (NULL) without the taking locks. The zv_suspend_lock is always taken
175 * before zv_state_lock. The mode argument indicates the mode (including none)
176 * for zv_suspend_lock to be taken.
178 static zvol_state_t
*
179 zvol_find_by_name(const char *name
, int mode
)
181 return (zvol_find_by_name_hash(name
, zvol_name_hash(name
), mode
));
185 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
188 zvol_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
190 zfs_creat_t
*zct
= arg
;
191 nvlist_t
*nvprops
= zct
->zct_props
;
193 uint64_t volblocksize
, volsize
;
195 VERIFY(nvlist_lookup_uint64(nvprops
,
196 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) == 0);
197 if (nvlist_lookup_uint64(nvprops
,
198 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &volblocksize
) != 0)
199 volblocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
202 * These properties must be removed from the list so the generic
203 * property setting step won't apply to them.
205 VERIFY(nvlist_remove_all(nvprops
,
206 zfs_prop_to_name(ZFS_PROP_VOLSIZE
)) == 0);
207 (void) nvlist_remove_all(nvprops
,
208 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
));
210 error
= dmu_object_claim(os
, ZVOL_OBJ
, DMU_OT_ZVOL
, volblocksize
,
214 error
= zap_create_claim(os
, ZVOL_ZAP_OBJ
, DMU_OT_ZVOL_PROP
,
218 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
, tx
);
223 * ZFS_IOC_OBJSET_STATS entry point.
226 zvol_get_stats(objset_t
*os
, nvlist_t
*nv
)
229 dmu_object_info_t
*doi
;
232 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &val
);
234 return (SET_ERROR(error
));
236 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLSIZE
, val
);
237 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
238 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
241 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLBLOCKSIZE
,
242 doi
->doi_data_block_size
);
245 kmem_free(doi
, sizeof (dmu_object_info_t
));
247 return (SET_ERROR(error
));
251 * Sanity check volume size.
254 zvol_check_volsize(uint64_t volsize
, uint64_t blocksize
)
257 return (SET_ERROR(EINVAL
));
259 if (volsize
% blocksize
!= 0)
260 return (SET_ERROR(EINVAL
));
263 if (volsize
- 1 > SPEC_MAXOFFSET_T
)
264 return (SET_ERROR(EOVERFLOW
));
270 * Ensure the zap is flushed then inform the VFS of the capacity change.
273 zvol_update_volsize(uint64_t volsize
, objset_t
*os
)
279 tx
= dmu_tx_create(os
);
280 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
281 dmu_tx_mark_netfree(tx
);
282 error
= dmu_tx_assign(tx
, TXG_WAIT
);
285 return (SET_ERROR(error
));
287 txg
= dmu_tx_get_txg(tx
);
289 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1,
293 txg_wait_synced(dmu_objset_pool(os
), txg
);
296 error
= dmu_free_long_range(os
,
297 ZVOL_OBJ
, volsize
, DMU_OBJECT_END
);
303 * Set ZFS_PROP_VOLSIZE set entry point. Note that modifying the volume
304 * size will result in a udev "change" event being generated.
307 zvol_set_volsize(const char *name
, uint64_t volsize
)
312 boolean_t owned
= B_FALSE
;
314 error
= dsl_prop_get_integer(name
,
315 zfs_prop_to_name(ZFS_PROP_READONLY
), &readonly
, NULL
);
317 return (SET_ERROR(error
));
319 return (SET_ERROR(EROFS
));
321 zvol_state_t
*zv
= zvol_find_by_name(name
, RW_READER
);
323 ASSERT(zv
== NULL
|| (MUTEX_HELD(&zv
->zv_state_lock
) &&
324 RW_READ_HELD(&zv
->zv_suspend_lock
)));
326 if (zv
== NULL
|| zv
->zv_objset
== NULL
) {
328 rw_exit(&zv
->zv_suspend_lock
);
329 if ((error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_FALSE
, B_TRUE
,
332 mutex_exit(&zv
->zv_state_lock
);
333 return (SET_ERROR(error
));
342 dmu_object_info_t
*doi
= kmem_alloc(sizeof (*doi
), KM_SLEEP
);
344 if ((error
= dmu_object_info(os
, ZVOL_OBJ
, doi
)) ||
345 (error
= zvol_check_volsize(volsize
, doi
->doi_data_block_size
)))
348 error
= zvol_update_volsize(volsize
, os
);
349 if (error
== 0 && zv
!= NULL
) {
350 zv
->zv_volsize
= volsize
;
354 kmem_free(doi
, sizeof (dmu_object_info_t
));
357 dmu_objset_disown(os
, B_TRUE
, FTAG
);
359 zv
->zv_objset
= NULL
;
361 rw_exit(&zv
->zv_suspend_lock
);
365 mutex_exit(&zv
->zv_state_lock
);
367 if (error
== 0 && zv
!= NULL
)
368 ops
->zv_update_volsize(zv
, volsize
);
370 return (SET_ERROR(error
));
374 * Sanity check volume block size.
377 zvol_check_volblocksize(const char *name
, uint64_t volblocksize
)
379 /* Record sizes above 128k need the feature to be enabled */
380 if (volblocksize
> SPA_OLD_MAXBLOCKSIZE
) {
384 if ((error
= spa_open(name
, &spa
, FTAG
)) != 0)
387 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
388 spa_close(spa
, FTAG
);
389 return (SET_ERROR(ENOTSUP
));
393 * We don't allow setting the property above 1MB,
394 * unless the tunable has been changed.
396 if (volblocksize
> zfs_max_recordsize
)
397 return (SET_ERROR(EDOM
));
399 spa_close(spa
, FTAG
);
402 if (volblocksize
< SPA_MINBLOCKSIZE
||
403 volblocksize
> SPA_MAXBLOCKSIZE
||
405 return (SET_ERROR(EDOM
));
411 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
412 * implement DKIOCFREE/free-long-range.
415 zvol_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
417 zvol_state_t
*zv
= arg1
;
418 lr_truncate_t
*lr
= arg2
;
419 uint64_t offset
, length
;
422 byteswap_uint64_array(lr
, sizeof (*lr
));
424 offset
= lr
->lr_offset
;
425 length
= lr
->lr_length
;
427 dmu_tx_t
*tx
= dmu_tx_create(zv
->zv_objset
);
428 dmu_tx_mark_netfree(tx
);
429 int error
= dmu_tx_assign(tx
, TXG_WAIT
);
433 zil_replaying(zv
->zv_zilog
, tx
);
435 error
= dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
, offset
,
443 * Replay a TX_WRITE ZIL transaction that didn't get committed
444 * after a system failure
447 zvol_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
449 zvol_state_t
*zv
= arg1
;
450 lr_write_t
*lr
= arg2
;
451 objset_t
*os
= zv
->zv_objset
;
452 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
453 uint64_t offset
, length
;
458 byteswap_uint64_array(lr
, sizeof (*lr
));
460 offset
= lr
->lr_offset
;
461 length
= lr
->lr_length
;
463 /* If it's a dmu_sync() block, write the whole block */
464 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
465 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
466 if (length
< blocksize
) {
467 offset
-= offset
% blocksize
;
472 tx
= dmu_tx_create(os
);
473 dmu_tx_hold_write(tx
, ZVOL_OBJ
, offset
, length
);
474 error
= dmu_tx_assign(tx
, TXG_WAIT
);
478 dmu_write(os
, ZVOL_OBJ
, offset
, length
, data
, tx
);
479 zil_replaying(zv
->zv_zilog
, tx
);
487 zvol_replay_err(void *arg1
, void *arg2
, boolean_t byteswap
)
489 (void) arg1
, (void) arg2
, (void) byteswap
;
490 return (SET_ERROR(ENOTSUP
));
494 * Callback vectors for replaying records.
495 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
497 zil_replay_func_t
*const zvol_replay_vector
[TX_MAX_TYPE
] = {
498 zvol_replay_err
, /* no such transaction type */
499 zvol_replay_err
, /* TX_CREATE */
500 zvol_replay_err
, /* TX_MKDIR */
501 zvol_replay_err
, /* TX_MKXATTR */
502 zvol_replay_err
, /* TX_SYMLINK */
503 zvol_replay_err
, /* TX_REMOVE */
504 zvol_replay_err
, /* TX_RMDIR */
505 zvol_replay_err
, /* TX_LINK */
506 zvol_replay_err
, /* TX_RENAME */
507 zvol_replay_write
, /* TX_WRITE */
508 zvol_replay_truncate
, /* TX_TRUNCATE */
509 zvol_replay_err
, /* TX_SETATTR */
510 zvol_replay_err
, /* TX_ACL */
511 zvol_replay_err
, /* TX_CREATE_ATTR */
512 zvol_replay_err
, /* TX_CREATE_ACL_ATTR */
513 zvol_replay_err
, /* TX_MKDIR_ACL */
514 zvol_replay_err
, /* TX_MKDIR_ATTR */
515 zvol_replay_err
, /* TX_MKDIR_ACL_ATTR */
516 zvol_replay_err
, /* TX_WRITE2 */
520 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
522 * We store data in the log buffers if it's small enough.
523 * Otherwise we will later flush the data out via dmu_sync().
525 static const ssize_t zvol_immediate_write_sz
= 32768;
528 zvol_log_write(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t offset
,
529 uint64_t size
, int sync
)
531 uint32_t blocksize
= zv
->zv_volblocksize
;
532 zilog_t
*zilog
= zv
->zv_zilog
;
533 itx_wr_state_t write_state
;
536 if (zil_replaying(zilog
, tx
))
539 if (zilog
->zl_logbias
== ZFS_LOGBIAS_THROUGHPUT
)
540 write_state
= WR_INDIRECT
;
541 else if (!spa_has_slogs(zilog
->zl_spa
) &&
542 size
>= blocksize
&& blocksize
> zvol_immediate_write_sz
)
543 write_state
= WR_INDIRECT
;
545 write_state
= WR_COPIED
;
547 write_state
= WR_NEED_COPY
;
552 itx_wr_state_t wr_state
= write_state
;
555 if (wr_state
== WR_COPIED
&& size
> zil_max_copied_data(zilog
))
556 wr_state
= WR_NEED_COPY
;
557 else if (wr_state
== WR_INDIRECT
)
558 len
= MIN(blocksize
- P2PHASE(offset
, blocksize
), size
);
560 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
) +
561 (wr_state
== WR_COPIED
? len
: 0));
562 lr
= (lr_write_t
*)&itx
->itx_lr
;
563 if (wr_state
== WR_COPIED
&& dmu_read_by_dnode(zv
->zv_dn
,
564 offset
, len
, lr
+1, DMU_READ_NO_PREFETCH
) != 0) {
565 zil_itx_destroy(itx
);
566 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
567 lr
= (lr_write_t
*)&itx
->itx_lr
;
568 wr_state
= WR_NEED_COPY
;
571 itx
->itx_wr_state
= wr_state
;
572 lr
->lr_foid
= ZVOL_OBJ
;
573 lr
->lr_offset
= offset
;
576 BP_ZERO(&lr
->lr_blkptr
);
578 itx
->itx_private
= zv
;
579 itx
->itx_sync
= sync
;
581 (void) zil_itx_assign(zilog
, itx
, tx
);
587 if (write_state
== WR_COPIED
|| write_state
== WR_NEED_COPY
) {
588 dsl_pool_wrlog_count(zilog
->zl_dmu_pool
, sz
, tx
->tx_txg
);
593 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
596 zvol_log_truncate(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t off
, uint64_t len
,
601 zilog_t
*zilog
= zv
->zv_zilog
;
603 if (zil_replaying(zilog
, tx
))
606 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
607 lr
= (lr_truncate_t
*)&itx
->itx_lr
;
608 lr
->lr_foid
= ZVOL_OBJ
;
612 itx
->itx_sync
= sync
;
613 zil_itx_assign(zilog
, itx
, tx
);
619 zvol_get_done(zgd_t
*zgd
, int error
)
622 dmu_buf_rele(zgd
->zgd_db
, zgd
);
624 zfs_rangelock_exit(zgd
->zgd_lr
);
626 kmem_free(zgd
, sizeof (zgd_t
));
630 * Get data to generate a TX_WRITE intent log record.
633 zvol_get_data(void *arg
, uint64_t arg2
, lr_write_t
*lr
, char *buf
,
634 struct lwb
*lwb
, zio_t
*zio
)
636 zvol_state_t
*zv
= arg
;
637 uint64_t offset
= lr
->lr_offset
;
638 uint64_t size
= lr
->lr_length
;
643 ASSERT3P(lwb
, !=, NULL
);
644 ASSERT3P(zio
, !=, NULL
);
645 ASSERT3U(size
, !=, 0);
647 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
651 * Write records come in two flavors: immediate and indirect.
652 * For small writes it's cheaper to store the data with the
653 * log record (immediate); for large writes it's cheaper to
654 * sync the data and get a pointer to it (indirect) so that
655 * we don't have to write the data twice.
657 if (buf
!= NULL
) { /* immediate write */
658 zgd
->zgd_lr
= zfs_rangelock_enter(&zv
->zv_rangelock
, offset
,
660 error
= dmu_read_by_dnode(zv
->zv_dn
, offset
, size
, buf
,
661 DMU_READ_NO_PREFETCH
);
662 } else { /* indirect write */
664 * Have to lock the whole block to ensure when it's written out
665 * and its checksum is being calculated that no one can change
666 * the data. Contrarily to zfs_get_data we need not re-check
667 * blocksize after we get the lock because it cannot be changed.
669 size
= zv
->zv_volblocksize
;
670 offset
= P2ALIGN_TYPED(offset
, size
, uint64_t);
671 zgd
->zgd_lr
= zfs_rangelock_enter(&zv
->zv_rangelock
, offset
,
673 error
= dmu_buf_hold_by_dnode(zv
->zv_dn
, offset
, zgd
, &db
,
674 DMU_READ_NO_PREFETCH
);
676 blkptr_t
*bp
= &lr
->lr_blkptr
;
682 ASSERT(db
->db_offset
== offset
);
683 ASSERT(db
->db_size
== size
);
685 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
693 zvol_get_done(zgd
, error
);
695 return (SET_ERROR(error
));
699 * The zvol_state_t's are inserted into zvol_state_list and zvol_htable.
703 zvol_insert(zvol_state_t
*zv
)
705 ASSERT(RW_WRITE_HELD(&zvol_state_lock
));
706 list_insert_head(&zvol_state_list
, zv
);
707 hlist_add_head(&zv
->zv_hlink
, ZVOL_HT_HEAD(zv
->zv_hash
));
711 * Simply remove the zvol from to list of zvols.
714 zvol_remove(zvol_state_t
*zv
)
716 ASSERT(RW_WRITE_HELD(&zvol_state_lock
));
717 list_remove(&zvol_state_list
, zv
);
718 hlist_del(&zv
->zv_hlink
);
722 * Setup zv after we just own the zv->objset
725 zvol_setup_zv(zvol_state_t
*zv
)
730 objset_t
*os
= zv
->zv_objset
;
732 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
733 ASSERT(RW_LOCK_HELD(&zv
->zv_suspend_lock
));
736 zv
->zv_flags
&= ~ZVOL_WRITTEN_TO
;
738 error
= dsl_prop_get_integer(zv
->zv_name
, "readonly", &ro
, NULL
);
740 return (SET_ERROR(error
));
742 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
744 return (SET_ERROR(error
));
746 error
= dnode_hold(os
, ZVOL_OBJ
, zv
, &zv
->zv_dn
);
748 return (SET_ERROR(error
));
750 ops
->zv_set_capacity(zv
, volsize
>> 9);
751 zv
->zv_volsize
= volsize
;
753 if (ro
|| dmu_objset_is_snapshot(os
) ||
754 !spa_writeable(dmu_objset_spa(os
))) {
755 ops
->zv_set_disk_ro(zv
, 1);
756 zv
->zv_flags
|= ZVOL_RDONLY
;
758 ops
->zv_set_disk_ro(zv
, 0);
759 zv
->zv_flags
&= ~ZVOL_RDONLY
;
765 * Shutdown every zv_objset related stuff except zv_objset itself.
766 * The is the reverse of zvol_setup_zv.
769 zvol_shutdown_zv(zvol_state_t
*zv
)
771 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
) &&
772 RW_LOCK_HELD(&zv
->zv_suspend_lock
));
774 if (zv
->zv_flags
& ZVOL_WRITTEN_TO
) {
775 ASSERT(zv
->zv_zilog
!= NULL
);
776 zil_close(zv
->zv_zilog
);
781 dnode_rele(zv
->zv_dn
, zv
);
785 * Evict cached data. We must write out any dirty data before
786 * disowning the dataset.
788 if (zv
->zv_flags
& ZVOL_WRITTEN_TO
)
789 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
790 (void) dmu_objset_evict_dbufs(zv
->zv_objset
);
794 * return the proper tag for rollback and recv
797 zvol_tag(zvol_state_t
*zv
)
799 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
800 return (zv
->zv_open_count
> 0 ? zv
: NULL
);
804 * Suspend the zvol for recv and rollback.
807 zvol_suspend(const char *name
)
811 zv
= zvol_find_by_name(name
, RW_WRITER
);
816 /* block all I/O, release in zvol_resume. */
817 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
818 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
820 atomic_inc(&zv
->zv_suspend_ref
);
822 if (zv
->zv_open_count
> 0)
823 zvol_shutdown_zv(zv
);
826 * do not hold zv_state_lock across suspend/resume to
827 * avoid locking up zvol lookups
829 mutex_exit(&zv
->zv_state_lock
);
831 /* zv_suspend_lock is released in zvol_resume() */
836 zvol_resume(zvol_state_t
*zv
)
840 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
842 mutex_enter(&zv
->zv_state_lock
);
844 if (zv
->zv_open_count
> 0) {
845 VERIFY0(dmu_objset_hold(zv
->zv_name
, zv
, &zv
->zv_objset
));
846 VERIFY3P(zv
->zv_objset
->os_dsl_dataset
->ds_owner
, ==, zv
);
847 VERIFY(dsl_dataset_long_held(zv
->zv_objset
->os_dsl_dataset
));
848 dmu_objset_rele(zv
->zv_objset
, zv
);
850 error
= zvol_setup_zv(zv
);
853 mutex_exit(&zv
->zv_state_lock
);
855 rw_exit(&zv
->zv_suspend_lock
);
857 * We need this because we don't hold zvol_state_lock while releasing
858 * zv_suspend_lock. zvol_remove_minors_impl thus cannot check
859 * zv_suspend_lock to determine it is safe to free because rwlock is
860 * not inherent atomic.
862 atomic_dec(&zv
->zv_suspend_ref
);
864 return (SET_ERROR(error
));
868 zvol_first_open(zvol_state_t
*zv
, boolean_t readonly
)
873 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
874 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
875 ASSERT(mutex_owned(&spa_namespace_lock
));
877 boolean_t ro
= (readonly
|| (strchr(zv
->zv_name
, '@') != NULL
));
878 error
= dmu_objset_own(zv
->zv_name
, DMU_OST_ZVOL
, ro
, B_TRUE
, zv
, &os
);
880 return (SET_ERROR(error
));
884 error
= zvol_setup_zv(zv
);
886 dmu_objset_disown(os
, 1, zv
);
887 zv
->zv_objset
= NULL
;
894 zvol_last_close(zvol_state_t
*zv
)
896 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
897 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
899 zvol_shutdown_zv(zv
);
901 dmu_objset_disown(zv
->zv_objset
, 1, zv
);
902 zv
->zv_objset
= NULL
;
905 typedef struct minors_job
{
915 * Prefetch zvol dnodes for the minors_job
918 zvol_prefetch_minors_impl(void *arg
)
920 minors_job_t
*job
= arg
;
921 char *dsname
= job
->name
;
924 job
->error
= dmu_objset_own(dsname
, DMU_OST_ZVOL
, B_TRUE
, B_TRUE
,
926 if (job
->error
== 0) {
927 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, 0, ZIO_PRIORITY_SYNC_READ
);
928 dmu_objset_disown(os
, B_TRUE
, FTAG
);
933 * Mask errors to continue dmu_objset_find() traversal
936 zvol_create_snap_minor_cb(const char *dsname
, void *arg
)
938 minors_job_t
*j
= arg
;
939 list_t
*minors_list
= j
->list
;
940 const char *name
= j
->name
;
942 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
944 /* skip the designated dataset */
945 if (name
&& strcmp(dsname
, name
) == 0)
948 /* at this point, the dsname should name a snapshot */
949 if (strchr(dsname
, '@') == 0) {
950 dprintf("zvol_create_snap_minor_cb(): "
951 "%s is not a snapshot name\n", dsname
);
954 char *n
= kmem_strdup(dsname
);
958 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
960 job
->list
= minors_list
;
962 list_insert_tail(minors_list
, job
);
963 /* don't care if dispatch fails, because job->error is 0 */
964 taskq_dispatch(system_taskq
, zvol_prefetch_minors_impl
, job
,
972 * If spa_keystore_load_wkey() is called for an encrypted zvol,
973 * we need to look for any clones also using the key. This function
974 * is "best effort" - so we just skip over it if there are failures.
977 zvol_add_clones(const char *dsname
, list_t
*minors_list
)
979 /* Also check if it has clones */
980 dsl_dir_t
*dd
= NULL
;
981 dsl_pool_t
*dp
= NULL
;
983 if (dsl_pool_hold(dsname
, FTAG
, &dp
) != 0)
986 if (!spa_feature_is_enabled(dp
->dp_spa
,
987 SPA_FEATURE_ENCRYPTION
))
990 if (dsl_dir_hold(dp
, dsname
, FTAG
, &dd
, NULL
) != 0)
993 if (dsl_dir_phys(dd
)->dd_clones
== 0)
996 zap_cursor_t
*zc
= kmem_alloc(sizeof (zap_cursor_t
), KM_SLEEP
);
997 zap_attribute_t
*za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
998 objset_t
*mos
= dd
->dd_pool
->dp_meta_objset
;
1000 for (zap_cursor_init(zc
, mos
, dsl_dir_phys(dd
)->dd_clones
);
1001 zap_cursor_retrieve(zc
, za
) == 0;
1002 zap_cursor_advance(zc
)) {
1003 dsl_dataset_t
*clone
;
1006 if (dsl_dataset_hold_obj(dd
->dd_pool
,
1007 za
->za_first_integer
, FTAG
, &clone
) == 0) {
1009 char name
[ZFS_MAX_DATASET_NAME_LEN
];
1010 dsl_dataset_name(clone
, name
);
1012 char *n
= kmem_strdup(name
);
1013 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
1015 job
->list
= minors_list
;
1017 list_insert_tail(minors_list
, job
);
1019 dsl_dataset_rele(clone
, FTAG
);
1022 zap_cursor_fini(zc
);
1023 kmem_free(za
, sizeof (zap_attribute_t
));
1024 kmem_free(zc
, sizeof (zap_cursor_t
));
1028 dsl_dir_rele(dd
, FTAG
);
1030 dsl_pool_rele(dp
, FTAG
);
1034 * Mask errors to continue dmu_objset_find() traversal
1037 zvol_create_minors_cb(const char *dsname
, void *arg
)
1041 list_t
*minors_list
= arg
;
1043 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
1045 error
= dsl_prop_get_integer(dsname
, "snapdev", &snapdev
, NULL
);
1050 * Given the name and the 'snapdev' property, create device minor nodes
1051 * with the linkages to zvols/snapshots as needed.
1052 * If the name represents a zvol, create a minor node for the zvol, then
1053 * check if its snapshots are 'visible', and if so, iterate over the
1054 * snapshots and create device minor nodes for those.
1056 if (strchr(dsname
, '@') == 0) {
1058 char *n
= kmem_strdup(dsname
);
1062 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
1064 job
->list
= minors_list
;
1066 list_insert_tail(minors_list
, job
);
1067 /* don't care if dispatch fails, because job->error is 0 */
1068 taskq_dispatch(system_taskq
, zvol_prefetch_minors_impl
, job
,
1071 zvol_add_clones(dsname
, minors_list
);
1073 if (snapdev
== ZFS_SNAPDEV_VISIBLE
) {
1075 * traverse snapshots only, do not traverse children,
1076 * and skip the 'dsname'
1078 error
= dmu_objset_find(dsname
,
1079 zvol_create_snap_minor_cb
, (void *)job
,
1083 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
1091 * Create minors for the specified dataset, including children and snapshots.
1092 * Pay attention to the 'snapdev' property and iterate over the snapshots
1093 * only if they are 'visible'. This approach allows one to assure that the
1094 * snapshot metadata is read from disk only if it is needed.
1096 * The name can represent a dataset to be recursively scanned for zvols and
1097 * their snapshots, or a single zvol snapshot. If the name represents a
1098 * dataset, the scan is performed in two nested stages:
1099 * - scan the dataset for zvols, and
1100 * - for each zvol, create a minor node, then check if the zvol's snapshots
1101 * are 'visible', and only then iterate over the snapshots if needed
1103 * If the name represents a snapshot, a check is performed if the snapshot is
1104 * 'visible' (which also verifies that the parent is a zvol), and if so,
1105 * a minor node for that snapshot is created.
1108 zvol_create_minors_recursive(const char *name
)
1113 if (zvol_inhibit_dev
)
1117 * This is the list for prefetch jobs. Whenever we found a match
1118 * during dmu_objset_find, we insert a minors_job to the list and do
1119 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need
1120 * any lock because all list operation is done on the current thread.
1122 * We will use this list to do zvol_create_minor_impl after prefetch
1123 * so we don't have to traverse using dmu_objset_find again.
1125 list_create(&minors_list
, sizeof (minors_job_t
),
1126 offsetof(minors_job_t
, link
));
1129 if (strchr(name
, '@') != NULL
) {
1132 int error
= dsl_prop_get_integer(name
, "snapdev",
1135 if (error
== 0 && snapdev
== ZFS_SNAPDEV_VISIBLE
)
1136 (void) ops
->zv_create_minor(name
);
1138 fstrans_cookie_t cookie
= spl_fstrans_mark();
1139 (void) dmu_objset_find(name
, zvol_create_minors_cb
,
1140 &minors_list
, DS_FIND_CHILDREN
);
1141 spl_fstrans_unmark(cookie
);
1144 taskq_wait_outstanding(system_taskq
, 0);
1147 * Prefetch is completed, we can do zvol_create_minor_impl
1150 while ((job
= list_head(&minors_list
)) != NULL
) {
1151 list_remove(&minors_list
, job
);
1153 (void) ops
->zv_create_minor(job
->name
);
1154 kmem_strfree(job
->name
);
1155 kmem_free(job
, sizeof (minors_job_t
));
1158 list_destroy(&minors_list
);
1162 zvol_create_minor(const char *name
)
1165 * Note: the dsl_pool_config_lock must not be held.
1166 * Minor node creation needs to obtain the zvol_state_lock.
1167 * zvol_open() obtains the zvol_state_lock and then the dsl pool
1168 * config lock. Therefore, we can't have the config lock now if
1169 * we are going to wait for the zvol_state_lock, because it
1170 * would be a lock order inversion which could lead to deadlock.
1173 if (zvol_inhibit_dev
)
1176 if (strchr(name
, '@') != NULL
) {
1179 int error
= dsl_prop_get_integer(name
,
1180 "snapdev", &snapdev
, NULL
);
1182 if (error
== 0 && snapdev
== ZFS_SNAPDEV_VISIBLE
)
1183 (void) ops
->zv_create_minor(name
);
1185 (void) ops
->zv_create_minor(name
);
1190 * Remove minors for specified dataset including children and snapshots.
1194 zvol_free_task(void *arg
)
1200 zvol_remove_minors_impl(const char *name
)
1202 zvol_state_t
*zv
, *zv_next
;
1203 int namelen
= ((name
) ? strlen(name
) : 0);
1207 if (zvol_inhibit_dev
)
1210 list_create(&free_list
, sizeof (zvol_state_t
),
1211 offsetof(zvol_state_t
, zv_next
));
1213 rw_enter(&zvol_state_lock
, RW_WRITER
);
1215 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1216 zv_next
= list_next(&zvol_state_list
, zv
);
1218 mutex_enter(&zv
->zv_state_lock
);
1219 if (name
== NULL
|| strcmp(zv
->zv_name
, name
) == 0 ||
1220 (strncmp(zv
->zv_name
, name
, namelen
) == 0 &&
1221 (zv
->zv_name
[namelen
] == '/' ||
1222 zv
->zv_name
[namelen
] == '@'))) {
1224 * By holding zv_state_lock here, we guarantee that no
1225 * one is currently using this zv
1228 /* If in use, leave alone */
1229 if (zv
->zv_open_count
> 0 ||
1230 atomic_read(&zv
->zv_suspend_ref
)) {
1231 mutex_exit(&zv
->zv_state_lock
);
1238 * Cleared while holding zvol_state_lock as a writer
1239 * which will prevent zvol_open() from opening it.
1241 ops
->zv_clear_private(zv
);
1243 /* Drop zv_state_lock before zvol_free() */
1244 mutex_exit(&zv
->zv_state_lock
);
1246 /* Try parallel zv_free, if failed do it in place */
1247 t
= taskq_dispatch(system_taskq
, zvol_free_task
, zv
,
1249 if (t
== TASKQID_INVALID
)
1250 list_insert_head(&free_list
, zv
);
1252 mutex_exit(&zv
->zv_state_lock
);
1255 rw_exit(&zvol_state_lock
);
1257 /* Drop zvol_state_lock before calling zvol_free() */
1258 while ((zv
= list_head(&free_list
)) != NULL
) {
1259 list_remove(&free_list
, zv
);
1264 /* Remove minor for this specific volume only */
1266 zvol_remove_minor_impl(const char *name
)
1268 zvol_state_t
*zv
= NULL
, *zv_next
;
1270 if (zvol_inhibit_dev
)
1273 rw_enter(&zvol_state_lock
, RW_WRITER
);
1275 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1276 zv_next
= list_next(&zvol_state_list
, zv
);
1278 mutex_enter(&zv
->zv_state_lock
);
1279 if (strcmp(zv
->zv_name
, name
) == 0) {
1281 * By holding zv_state_lock here, we guarantee that no
1282 * one is currently using this zv
1285 /* If in use, leave alone */
1286 if (zv
->zv_open_count
> 0 ||
1287 atomic_read(&zv
->zv_suspend_ref
)) {
1288 mutex_exit(&zv
->zv_state_lock
);
1293 ops
->zv_clear_private(zv
);
1294 mutex_exit(&zv
->zv_state_lock
);
1297 mutex_exit(&zv
->zv_state_lock
);
1301 /* Drop zvol_state_lock before calling zvol_free() */
1302 rw_exit(&zvol_state_lock
);
1309 * Rename minors for specified dataset including children and snapshots.
1312 zvol_rename_minors_impl(const char *oldname
, const char *newname
)
1314 zvol_state_t
*zv
, *zv_next
;
1317 if (zvol_inhibit_dev
)
1320 oldnamelen
= strlen(oldname
);
1322 rw_enter(&zvol_state_lock
, RW_READER
);
1324 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1325 zv_next
= list_next(&zvol_state_list
, zv
);
1327 mutex_enter(&zv
->zv_state_lock
);
1329 if (strcmp(zv
->zv_name
, oldname
) == 0) {
1330 ops
->zv_rename_minor(zv
, newname
);
1331 } else if (strncmp(zv
->zv_name
, oldname
, oldnamelen
) == 0 &&
1332 (zv
->zv_name
[oldnamelen
] == '/' ||
1333 zv
->zv_name
[oldnamelen
] == '@')) {
1334 char *name
= kmem_asprintf("%s%c%s", newname
,
1335 zv
->zv_name
[oldnamelen
],
1336 zv
->zv_name
+ oldnamelen
+ 1);
1337 ops
->zv_rename_minor(zv
, name
);
1341 mutex_exit(&zv
->zv_state_lock
);
1344 rw_exit(&zvol_state_lock
);
1347 typedef struct zvol_snapdev_cb_arg
{
1349 } zvol_snapdev_cb_arg_t
;
1352 zvol_set_snapdev_cb(const char *dsname
, void *param
)
1354 zvol_snapdev_cb_arg_t
*arg
= param
;
1356 if (strchr(dsname
, '@') == NULL
)
1359 switch (arg
->snapdev
) {
1360 case ZFS_SNAPDEV_VISIBLE
:
1361 (void) ops
->zv_create_minor(dsname
);
1363 case ZFS_SNAPDEV_HIDDEN
:
1364 (void) zvol_remove_minor_impl(dsname
);
1372 zvol_set_snapdev_impl(char *name
, uint64_t snapdev
)
1374 zvol_snapdev_cb_arg_t arg
= {snapdev
};
1375 fstrans_cookie_t cookie
= spl_fstrans_mark();
1377 * The zvol_set_snapdev_sync() sets snapdev appropriately
1378 * in the dataset hierarchy. Here, we only scan snapshots.
1380 dmu_objset_find(name
, zvol_set_snapdev_cb
, &arg
, DS_FIND_SNAPSHOTS
);
1381 spl_fstrans_unmark(cookie
);
1385 zvol_set_volmode_impl(char *name
, uint64_t volmode
)
1387 fstrans_cookie_t cookie
;
1388 uint64_t old_volmode
;
1391 if (strchr(name
, '@') != NULL
)
1395 * It's unfortunate we need to remove minors before we create new ones:
1396 * this is necessary because our backing gendisk (zvol_state->zv_disk)
1397 * could be different when we set, for instance, volmode from "geom"
1398 * to "dev" (or vice versa).
1400 zv
= zvol_find_by_name(name
, RW_NONE
);
1401 if (zv
== NULL
&& volmode
== ZFS_VOLMODE_NONE
)
1404 old_volmode
= zv
->zv_volmode
;
1405 mutex_exit(&zv
->zv_state_lock
);
1406 if (old_volmode
== volmode
)
1408 zvol_wait_close(zv
);
1410 cookie
= spl_fstrans_mark();
1412 case ZFS_VOLMODE_NONE
:
1413 (void) zvol_remove_minor_impl(name
);
1415 case ZFS_VOLMODE_GEOM
:
1416 case ZFS_VOLMODE_DEV
:
1417 (void) zvol_remove_minor_impl(name
);
1418 (void) ops
->zv_create_minor(name
);
1420 case ZFS_VOLMODE_DEFAULT
:
1421 (void) zvol_remove_minor_impl(name
);
1422 if (zvol_volmode
== ZFS_VOLMODE_NONE
)
1424 else /* if zvol_volmode is invalid defaults to "geom" */
1425 (void) ops
->zv_create_minor(name
);
1428 spl_fstrans_unmark(cookie
);
1431 static zvol_task_t
*
1432 zvol_task_alloc(zvol_async_op_t op
, const char *name1
, const char *name2
,
1437 /* Never allow tasks on hidden names. */
1438 if (name1
[0] == '$')
1441 task
= kmem_zalloc(sizeof (zvol_task_t
), KM_SLEEP
);
1443 task
->value
= value
;
1445 strlcpy(task
->name1
, name1
, MAXNAMELEN
);
1447 strlcpy(task
->name2
, name2
, MAXNAMELEN
);
1453 zvol_task_free(zvol_task_t
*task
)
1455 kmem_free(task
, sizeof (zvol_task_t
));
1459 * The worker thread function performed asynchronously.
1462 zvol_task_cb(void *arg
)
1464 zvol_task_t
*task
= arg
;
1467 case ZVOL_ASYNC_REMOVE_MINORS
:
1468 zvol_remove_minors_impl(task
->name1
);
1470 case ZVOL_ASYNC_RENAME_MINORS
:
1471 zvol_rename_minors_impl(task
->name1
, task
->name2
);
1473 case ZVOL_ASYNC_SET_SNAPDEV
:
1474 zvol_set_snapdev_impl(task
->name1
, task
->value
);
1476 case ZVOL_ASYNC_SET_VOLMODE
:
1477 zvol_set_volmode_impl(task
->name1
, task
->value
);
1484 zvol_task_free(task
);
1487 typedef struct zvol_set_prop_int_arg
{
1488 const char *zsda_name
;
1489 uint64_t zsda_value
;
1490 zprop_source_t zsda_source
;
1492 } zvol_set_prop_int_arg_t
;
1495 * Sanity check the dataset for safe use by the sync task. No additional
1496 * conditions are imposed.
1499 zvol_set_snapdev_check(void *arg
, dmu_tx_t
*tx
)
1501 zvol_set_prop_int_arg_t
*zsda
= arg
;
1502 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1506 error
= dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
);
1510 dsl_dir_rele(dd
, FTAG
);
1517 zvol_set_snapdev_sync_cb(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
1519 char dsname
[MAXNAMELEN
];
1523 dsl_dataset_name(ds
, dsname
);
1524 if (dsl_prop_get_int_ds(ds
, "snapdev", &snapdev
) != 0)
1526 task
= zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV
, dsname
, NULL
, snapdev
);
1530 (void) taskq_dispatch(dp
->dp_spa
->spa_zvol_taskq
, zvol_task_cb
,
1536 * Traverse all child datasets and apply snapdev appropriately.
1537 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
1538 * dataset and read the effective "snapdev" on every child in the callback
1539 * function: this is because the value is not guaranteed to be the same in the
1540 * whole dataset hierarchy.
1543 zvol_set_snapdev_sync(void *arg
, dmu_tx_t
*tx
)
1545 zvol_set_prop_int_arg_t
*zsda
= arg
;
1546 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1551 VERIFY0(dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
));
1554 error
= dsl_dataset_hold(dp
, zsda
->zsda_name
, FTAG
, &ds
);
1556 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_SNAPDEV
),
1557 zsda
->zsda_source
, sizeof (zsda
->zsda_value
), 1,
1558 &zsda
->zsda_value
, zsda
->zsda_tx
);
1559 dsl_dataset_rele(ds
, FTAG
);
1561 dmu_objset_find_dp(dp
, dd
->dd_object
, zvol_set_snapdev_sync_cb
,
1562 zsda
, DS_FIND_CHILDREN
);
1564 dsl_dir_rele(dd
, FTAG
);
1568 zvol_set_snapdev(const char *ddname
, zprop_source_t source
, uint64_t snapdev
)
1570 zvol_set_prop_int_arg_t zsda
;
1572 zsda
.zsda_name
= ddname
;
1573 zsda
.zsda_source
= source
;
1574 zsda
.zsda_value
= snapdev
;
1576 return (dsl_sync_task(ddname
, zvol_set_snapdev_check
,
1577 zvol_set_snapdev_sync
, &zsda
, 0, ZFS_SPACE_CHECK_NONE
));
1581 * Sanity check the dataset for safe use by the sync task. No additional
1582 * conditions are imposed.
1585 zvol_set_volmode_check(void *arg
, dmu_tx_t
*tx
)
1587 zvol_set_prop_int_arg_t
*zsda
= arg
;
1588 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1592 error
= dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
);
1596 dsl_dir_rele(dd
, FTAG
);
1603 zvol_set_volmode_sync_cb(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
1605 char dsname
[MAXNAMELEN
];
1609 dsl_dataset_name(ds
, dsname
);
1610 if (dsl_prop_get_int_ds(ds
, "volmode", &volmode
) != 0)
1612 task
= zvol_task_alloc(ZVOL_ASYNC_SET_VOLMODE
, dsname
, NULL
, volmode
);
1616 (void) taskq_dispatch(dp
->dp_spa
->spa_zvol_taskq
, zvol_task_cb
,
1622 * Traverse all child datasets and apply volmode appropriately.
1623 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
1624 * dataset and read the effective "volmode" on every child in the callback
1625 * function: this is because the value is not guaranteed to be the same in the
1626 * whole dataset hierarchy.
1629 zvol_set_volmode_sync(void *arg
, dmu_tx_t
*tx
)
1631 zvol_set_prop_int_arg_t
*zsda
= arg
;
1632 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1637 VERIFY0(dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
));
1640 error
= dsl_dataset_hold(dp
, zsda
->zsda_name
, FTAG
, &ds
);
1642 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_VOLMODE
),
1643 zsda
->zsda_source
, sizeof (zsda
->zsda_value
), 1,
1644 &zsda
->zsda_value
, zsda
->zsda_tx
);
1645 dsl_dataset_rele(ds
, FTAG
);
1648 dmu_objset_find_dp(dp
, dd
->dd_object
, zvol_set_volmode_sync_cb
,
1649 zsda
, DS_FIND_CHILDREN
);
1651 dsl_dir_rele(dd
, FTAG
);
1655 zvol_set_volmode(const char *ddname
, zprop_source_t source
, uint64_t volmode
)
1657 zvol_set_prop_int_arg_t zsda
;
1659 zsda
.zsda_name
= ddname
;
1660 zsda
.zsda_source
= source
;
1661 zsda
.zsda_value
= volmode
;
1663 return (dsl_sync_task(ddname
, zvol_set_volmode_check
,
1664 zvol_set_volmode_sync
, &zsda
, 0, ZFS_SPACE_CHECK_NONE
));
1668 zvol_remove_minors(spa_t
*spa
, const char *name
, boolean_t async
)
1673 task
= zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS
, name
, NULL
, ~0ULL);
1677 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
1678 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
1679 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
1683 zvol_rename_minors(spa_t
*spa
, const char *name1
, const char *name2
,
1689 task
= zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS
, name1
, name2
, ~0ULL);
1693 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
1694 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
1695 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
1699 zvol_is_zvol(const char *name
)
1702 return (ops
->zv_is_zvol(name
));
1706 zvol_register_ops(const zvol_platform_ops_t
*zvol_ops
)
1712 zvol_init_impl(void)
1716 list_create(&zvol_state_list
, sizeof (zvol_state_t
),
1717 offsetof(zvol_state_t
, zv_next
));
1718 rw_init(&zvol_state_lock
, NULL
, RW_DEFAULT
, NULL
);
1720 zvol_htable
= kmem_alloc(ZVOL_HT_SIZE
* sizeof (struct hlist_head
),
1722 for (i
= 0; i
< ZVOL_HT_SIZE
; i
++)
1723 INIT_HLIST_HEAD(&zvol_htable
[i
]);
1729 zvol_fini_impl(void)
1731 zvol_remove_minors_impl(NULL
);
1734 * The call to "zvol_remove_minors_impl" may dispatch entries to
1735 * the system_taskq, but it doesn't wait for those entries to
1736 * complete before it returns. Thus, we must wait for all of the
1737 * removals to finish, before we can continue.
1739 taskq_wait_outstanding(system_taskq
, 0);
1741 kmem_free(zvol_htable
, ZVOL_HT_SIZE
* sizeof (struct hlist_head
));
1742 list_destroy(&zvol_state_list
);
1743 rw_destroy(&zvol_state_lock
);